cleanup namespace of filter lists
[lttngtop.git] / src / lttngtop.c
1 /*
2 * Copyright (C) 2011-2012 Julien Desfossez
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <babeltrace/babeltrace.h>
23 #include <babeltrace/ctf/events.h>
24 #include <babeltrace/ctf/callbacks.h>
25 #include <babeltrace/ctf/iterator.h>
26 #include <fcntl.h>
27 #include <pthread.h>
28 #include <popt.h>
29 #include <stdlib.h>
30 #include <ftw.h>
31 #include <dirent.h>
32 #include <ctype.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <fts.h>
39 #include <assert.h>
40 #include <sys/mman.h>
41 #include <lttng/lttng.h>
42 #include <lttng/lttngtop-helper.h>
43 #include <babeltrace/lttngtopmmappacketseek.h>
44
45 #include "lttngtoptypes.h"
46 #include "cputop.h"
47 #include "iostreamtop.h"
48 #include "cursesdisplay.h"
49 #include "common.h"
50
51 #define DEFAULT_FILE_ARRAY_SIZE 1
52
53 const char *opt_input_path;
54 static int opt_textdump;
55 static int opt_child;
56
57 int quit = 0;
58
59 struct lttngtop *copy;
60 pthread_t display_thread;
61 pthread_t timer_thread;
62
63 unsigned long refresh_display = 1 * NSEC_PER_SEC;
64 unsigned long last_display_update = 0;
65
66 /* list of FDs available for being read with snapshots */
67 struct mmap_stream_list mmap_list;
68 GPtrArray *lttng_consumer_stream_array;
69 int sessiond_metadata, consumerd_metadata;
70 struct lttng_consumer_local_data *ctx = NULL;
71 /* list of snapshots currently not consumed */
72 GPtrArray *available_snapshots;
73 sem_t metadata_available;
74 int reload_trace = 0;
75
76 int last_textdump_print_newline = 1;
77
78 enum {
79 OPT_NONE = 0,
80 OPT_HELP,
81 OPT_TEXTDUMP,
82 OPT_PID,
83 OPT_CHILD,
84 OPT_HOSTNAME,
85 OPT_KPROBES,
86 };
87
88 static struct poptOption long_options[] = {
89 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
90 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
91 { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL },
92 { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL },
93 { "pid", 'p', POPT_ARG_STRING, &opt_tid, OPT_PID, NULL, NULL },
94 { "hostname", 'n', POPT_ARG_STRING, &opt_hostname, OPT_HOSTNAME, NULL, NULL },
95 { "kprobes", 'k', POPT_ARG_STRING, &opt_kprobes, OPT_KPROBES, NULL, NULL },
96 { NULL, 0, 0, NULL, 0, NULL, NULL },
97 };
98
99 static void handle_textdump_sigterm(int signal)
100 {
101 quit = 1;
102 lttng_destroy_session("test");
103 }
104
105 void *refresh_thread(void *p)
106 {
107 struct mmap_stream *mmap_info;
108
109 while (1) {
110 if (quit) {
111 sem_post(&pause_sem);
112 sem_post(&timer);
113 sem_post(&end_trace_sem);
114 sem_post(&goodtodisplay);
115 sem_post(&goodtoupdate);
116 pthread_exit(0);
117 }
118 if (!opt_input_path) {
119 bt_list_for_each_entry(mmap_info, &mmap_list.head, list)
120 helper_kernctl_buffer_flush(mmap_info->fd);
121 }
122 sem_wait(&pause_sem);
123 sem_post(&pause_sem);
124 sem_post(&timer);
125 sleep(refresh_display/NSEC_PER_SEC);
126 }
127 }
128
129 void *ncurses_display(void *p)
130 {
131 unsigned int current_display_index = 0;
132
133 sem_wait(&bootstrap);
134 /*
135 * Prevent the 1 second delay when we hit ESC
136 */
137 ESCDELAY = 0;
138 init_ncurses();
139
140 while (1) {
141 sem_wait(&timer);
142 sem_wait(&goodtodisplay);
143 sem_wait(&pause_sem);
144
145 if (quit) {
146 sem_post(&pause_sem);
147 sem_post(&timer);
148 reset_ncurses();
149 pthread_exit(0);
150 }
151
152 copy = g_ptr_array_index(copies, current_display_index);
153 assert(copy);
154 display(current_display_index++);
155
156 sem_post(&goodtoupdate);
157 sem_post(&pause_sem);
158 }
159 }
160
161 void print_fields(struct bt_ctf_event *event)
162 {
163 unsigned int cnt, i;
164 const struct definition *const * list;
165 const struct definition *scope;
166 enum ctf_type_id type;
167 const char *str;
168
169 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_FIELDS);
170
171 bt_ctf_get_field_list(event, scope, &list, &cnt);
172 for (i = 0; i < cnt; i++) {
173 if (i != 0)
174 printf(", ");
175 printf("%s = ", bt_ctf_field_name(list[i]));
176 type = bt_ctf_field_type(list[i]);
177 if (type == CTF_TYPE_INTEGER) {
178 if (bt_ctf_get_int_signedness(list[i]) == 0)
179 printf("%" PRIu64 "", bt_ctf_get_uint64(list[i]));
180 else
181 printf("%" PRId64 "", bt_ctf_get_int64(list[i]));
182 } else if (type == CTF_TYPE_STRING) {
183 printf("%s", bt_ctf_get_string(list[i]));
184 } else if (type == CTF_TYPE_ARRAY) {
185 str = bt_ctf_get_char_array(list[i]);
186 if (!bt_ctf_field_get_error() && str)
187 printf("%s", str);
188 }
189 }
190 }
191
192 /*
193 * hook on each event to check the timestamp and refresh the display if
194 * necessary
195 */
196 enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
197 {
198 unsigned long timestamp;
199 struct tm start;
200 uint64_t ts_nsec_start;
201 int pid, cpu_id;
202 int64_t syscall_ret;
203 const struct definition *scope;
204 const char *hostname, *procname;
205
206 timestamp = bt_ctf_get_timestamp(call_data);
207
208 start = format_timestamp(timestamp);
209 ts_nsec_start = timestamp % NSEC_PER_SEC;
210
211 pid = get_context_pid(call_data);
212 if (pid == -1ULL && opt_tid) {
213 goto error;
214 }
215
216 hostname = get_context_hostname(call_data);
217 if (opt_tid || opt_hostname)
218 if (!lookup_filter_tid_list(pid))
219 goto end;
220
221 cpu_id = get_cpu_id(call_data);
222 procname = get_context_comm(call_data);
223
224 if ((strcmp(bt_ctf_event_name(call_data), "exit_syscall") == 0) &&
225 !last_textdump_print_newline) {
226 scope = bt_ctf_get_top_level_scope(call_data,
227 BT_EVENT_FIELDS);
228 syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data,
229 scope, "_ret"));
230 printf("= %ld\n", syscall_ret);
231 last_textdump_print_newline = 1;
232 } else {
233 /* we might have lost the exit_syscall event, so need to
234 * print the newline in this case */
235 if (last_textdump_print_newline == 0)
236 printf("\n");
237 printf("%02d:%02d:%02d.%09" PRIu64 " (%s) (cpu %d) [%s (%d)] %s (",
238 start.tm_hour, start.tm_min, start.tm_sec,
239 ts_nsec_start, hostname, cpu_id, procname, pid,
240 bt_ctf_event_name(call_data));
241 print_fields(call_data);
242 printf(") ");
243 if (strncmp(bt_ctf_event_name(call_data), "sys_", 4) != 0) {
244 printf("\n");
245 last_textdump_print_newline = 1;
246 } else {
247 last_textdump_print_newline = 0;
248 }
249 }
250
251 end:
252 return BT_CB_OK;
253 error:
254 return BT_CB_ERROR_STOP;
255 }
256
257 enum bt_cb_ret handle_kprobes(struct bt_ctf_event *call_data, void *private_data)
258 {
259 int i;
260 struct kprobes *kprobe;
261
262 /* for kprobes */
263 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
264 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
265 if (strcmp(bt_ctf_event_name(call_data), kprobe->probe_name) == 0) {
266 kprobe->count++;
267 }
268 }
269
270 return BT_CB_OK;
271 }
272
273 /*
274 * hook on each event to check the timestamp and refresh the display if
275 * necessary
276 */
277 enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
278 {
279 unsigned long timestamp;
280
281 timestamp = bt_ctf_get_timestamp(call_data);
282 if (timestamp == -1ULL)
283 goto error;
284
285 if (last_display_update == 0)
286 last_display_update = timestamp;
287
288 if (timestamp - last_display_update >= refresh_display) {
289 sem_wait(&goodtoupdate);
290 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
291 timestamp));
292 sem_post(&goodtodisplay);
293 sem_post(&bootstrap);
294 last_display_update = timestamp;
295 }
296 return BT_CB_OK;
297
298 error:
299 fprintf(stderr, "check_timestamp callback error\n");
300 return BT_CB_ERROR_STOP;
301 }
302
303 /*
304 * get_perf_counter : get or create and return a perf_counter struct for
305 * either a process or a cpu (only one of the 2 parameters mandatory)
306 */
307 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
308 struct cputime *cpu)
309 {
310 struct perfcounter *ret;
311 GHashTable *table;
312
313 if (proc)
314 table = proc->perf;
315 else if (cpu)
316 table = cpu->perf;
317 else
318 goto error;
319
320 ret = g_hash_table_lookup(table, (gpointer) name);
321 if (ret)
322 goto end;
323
324 ret = g_new0(struct perfcounter, 1);
325 /* by default, make it visible in the UI */
326 ret->visible = 1;
327 g_hash_table_insert(table, (gpointer) strdup(name), ret);
328
329 end:
330 return ret;
331
332 error:
333 return NULL;
334 }
335
336 void update_perf_value(struct processtop *proc, struct cputime *cpu,
337 const char *name, int value)
338 {
339 struct perfcounter *cpu_perf, *process_perf;
340
341 cpu_perf = get_perf_counter(name, NULL, cpu);
342 if (cpu_perf->count < value) {
343 process_perf = get_perf_counter(name, proc, NULL);
344 process_perf->count += value - cpu_perf->count;
345 cpu_perf->count = value;
346 }
347 }
348
349 void extract_perf_counter_scope(const struct bt_ctf_event *event,
350 const struct bt_definition *scope,
351 struct processtop *proc,
352 struct cputime *cpu)
353 {
354 struct bt_definition const * const *list = NULL;
355 const struct bt_definition *field;
356 unsigned int count;
357 struct perfcounter *perfcounter;
358 GHashTableIter iter;
359 gpointer key;
360 int ret;
361
362 if (!scope)
363 goto end;
364
365 ret = bt_ctf_get_field_list(event, scope, &list, &count);
366 if (ret < 0)
367 goto end;
368
369 if (count == 0)
370 goto end;
371
372 g_hash_table_iter_init(&iter, global_perf_liszt);
373 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) {
374 field = bt_ctf_get_field(event, scope, (char *) key);
375 if (field) {
376 int value = bt_ctf_get_uint64(field);
377 if (bt_ctf_field_get_error())
378 continue;
379 update_perf_value(proc, cpu, (char *) key, value);
380 }
381 }
382
383 end:
384 return;
385 }
386
387 void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
388 {
389 struct cputime *cpu;
390 const struct bt_definition *scope;
391
392 cpu = get_cpu(get_cpu_id(event));
393
394 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
395 extract_perf_counter_scope(event, scope, proc, cpu);
396
397 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
398 extract_perf_counter_scope(event, scope, proc, cpu);
399
400 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
401 extract_perf_counter_scope(event, scope, proc, cpu);
402 }
403
404 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
405 void *private_data)
406 {
407 int pid, tid, ppid, vpid, vtid, vppid;
408 char *comm, *hostname;
409 struct processtop *parent, *child;
410 unsigned long timestamp;
411
412 timestamp = bt_ctf_get_timestamp(call_data);
413 if (timestamp == -1ULL)
414 goto error;
415
416 pid = get_context_pid(call_data);
417 if (pid == -1ULL) {
418 goto error;
419 }
420
421 tid = get_context_tid(call_data);
422 if (tid == -1ULL) {
423 goto error;
424 }
425 ppid = get_context_ppid(call_data);
426 if (ppid == -1ULL) {
427 goto error;
428 }
429 vpid = get_context_vpid(call_data);
430 if (pid == -1ULL) {
431 vpid = -1;
432 }
433 vtid = get_context_vtid(call_data);
434 if (tid == -1ULL) {
435 vtid = -1;
436 }
437 vppid = get_context_vppid(call_data);
438 if (ppid == -1ULL) {
439 vppid = -1;
440 }
441 comm = get_context_comm(call_data);
442 if (!comm) {
443 goto error;
444 }
445 /* optional */
446 hostname = get_context_hostname(call_data);
447
448 /* find or create the current process */
449 child = find_process_tid(&lttngtop, tid, comm);
450 if (!child)
451 child = add_proc(&lttngtop, tid, comm, timestamp, hostname);
452 if (!child)
453 goto end;
454 update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm, hostname);
455
456 if (pid != tid) {
457 /* find or create the parent */
458 parent = find_process_tid(&lttngtop, pid, comm);
459 if (!parent) {
460 parent = add_proc(&lttngtop, pid, comm, timestamp, hostname);
461 if (parent)
462 parent->pid = pid;
463 }
464
465 /* attach the parent to the current process */
466 child->threadparent = parent;
467 add_thread(parent, child);
468 }
469
470 update_perf_counter(child, call_data);
471
472 end:
473 return BT_CB_OK;
474
475 error:
476 return BT_CB_ERROR_STOP;
477 }
478
479 void init_lttngtop()
480 {
481 copies = g_ptr_array_new();
482 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
483 global_filter_list = g_hash_table_new(g_str_hash, g_str_equal);
484
485 sem_init(&goodtodisplay, 0, 0);
486 sem_init(&goodtoupdate, 0, 1);
487 sem_init(&timer, 0, 1);
488 sem_init(&bootstrap, 0, 0);
489 sem_init(&pause_sem, 0, 1);
490 sem_init(&end_trace_sem, 0, 0);
491
492 reset_global_counters();
493 lttngtop.nbproc = 0;
494 lttngtop.nbthreads = 0;
495 lttngtop.nbfiles = 0;
496
497 lttngtop.process_hash_table = g_hash_table_new(g_direct_hash,
498 g_direct_equal);
499 lttngtop.process_table = g_ptr_array_new();
500 lttngtop.files_table = g_ptr_array_new();
501 lttngtop.cpu_table = g_ptr_array_new();
502
503 toggle_filter = -1;
504 }
505
506 void usage(FILE *fp)
507 {
508 fprintf(fp, "LTTngTop %s\n\n", VERSION);
509 fprintf(fp, "Usage : lttngtop [OPTIONS] [TRACE]\n");
510 fprintf(fp, " TRACE Path to the trace to analyse (no trace path for live tracing)\n");
511 fprintf(fp, " -h, --help This help message\n");
512 fprintf(fp, " -t, --textdump Display live events in text-only\n");
513 fprintf(fp, " -p, --pid Comma-separated list of PIDs to display\n");
514 fprintf(fp, " -f, --child Follow threads associated with selected PIDs\n");
515 fprintf(fp, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
516 fprintf(fp, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
517 }
518
519 /*
520 * Parse probe options.
521 * Shamelessly stolen from lttng-tools :
522 * src/bin/lttng/commands/enable_events.c
523 */
524 static struct kprobes *parse_probe_opts(char *opt)
525 {
526 char s_hex[19];
527 char name[LTTNG_SYMBOL_NAME_LEN];
528 struct kprobes *kprobe;
529 int ret;
530
531 /*
532 kprobe->probe_addr = 0;
533 kprobe->probe_offset = 0;
534 asprintf(&kprobe->probe_name, "probe_sys_open");
535 asprintf(&kprobe->symbol_name, "sys_open");
536 */
537
538 if (opt == NULL) {
539 kprobe = NULL;
540 goto end;
541 }
542
543 kprobe = g_new0(struct kprobes, 1);
544
545 /* Check for symbol+offset */
546 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
547 if (ret == 2) {
548 asprintf(&kprobe->probe_name, "probe_%s", name);
549 asprintf(&kprobe->symbol_name, "%s", name);
550
551 if (strlen(s_hex) == 0) {
552 fprintf(stderr, "Invalid probe offset %s", s_hex);
553 ret = -1;
554 goto end;
555 }
556 kprobe->probe_offset = strtoul(s_hex, NULL, 0);
557 kprobe->probe_addr = 0;
558 goto end;
559 }
560
561 /* Check for symbol */
562 if (isalpha(name[0])) {
563 ret = sscanf(opt, "%s", name);
564 if (ret == 1) {
565 asprintf(&kprobe->probe_name, "probe_%s", name);
566 asprintf(&kprobe->symbol_name, "%s", name);
567 kprobe->probe_offset = 0;
568 kprobe->probe_addr = 0;
569 goto end;
570 }
571 }
572
573 /* Check for address */
574 ret = sscanf(opt, "%s", s_hex);
575 if (ret > 0) {
576 if (strlen(s_hex) == 0) {
577 fprintf(stderr, "Invalid probe address %s", s_hex);
578 ret = -1;
579 goto end;
580 }
581 asprintf(&kprobe->probe_name, "probe_%s", s_hex);
582 kprobe->probe_offset = 0;
583 kprobe->probe_addr = strtoul(s_hex, NULL, 0);
584 goto end;
585 }
586
587 /* No match */
588 kprobe = NULL;
589
590 end:
591 return kprobe;
592 }
593
594 /*
595 * Return 0 if caller should continue, < 0 if caller should return
596 * error, > 0 if caller should exit without reporting error.
597 */
598 static int parse_options(int argc, char **argv)
599 {
600 poptContext pc;
601 int opt, ret = 0;
602 char *tmp_str;
603 int *tid;
604
605 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
606 poptReadDefaultConfig(pc, 0);
607
608 while ((opt = poptGetNextOpt(pc)) != -1) {
609 switch (opt) {
610 case OPT_HELP:
611 usage(stdout);
612 ret = 1; /* exit cleanly */
613 goto end;
614 case OPT_TEXTDUMP:
615 opt_textdump = 1;
616 break;
617 case OPT_CHILD:
618 opt_textdump = 1;
619 opt_child = 1;
620 break;
621 case OPT_PID:
622 toggle_filter = 1;
623 tid_filter_list = g_hash_table_new(g_str_hash,
624 g_str_equal);
625 tmp_str = strtok(opt_tid, ",");
626 while (tmp_str) {
627 tid = malloc(sizeof(int));
628 *tid = atoi(tmp_str);
629 g_hash_table_insert(tid_filter_list,
630 (gpointer) tid, tid);
631 tmp_str = strtok(NULL, ",");
632 }
633 break;
634 case OPT_HOSTNAME:
635 toggle_filter = 1;
636 hostname_filter_list = g_hash_table_new(g_str_hash,
637 g_str_equal);
638 tmp_str = strtok(opt_hostname, ",");
639 while (tmp_str) {
640 char *new_str = strdup(tmp_str);
641 g_hash_table_insert(hostname_filter_list,
642 (gpointer) new_str,
643 (gpointer) new_str);
644 tmp_str = strtok(NULL, ",");
645 }
646 break;
647 case OPT_KPROBES:
648 lttngtop.kprobes_table = g_ptr_array_new();
649 tmp_str = strtok(opt_kprobes, ",");
650 while (tmp_str) {
651 struct kprobes *kprobe;
652
653 kprobe = parse_probe_opts(tmp_str);
654 if (kprobe) {
655 g_ptr_array_add(
656 lttngtop.kprobes_table,
657 kprobe);
658 } else {
659 ret = -EINVAL;
660 goto end;
661 }
662 tmp_str = strtok(NULL, ",");
663 }
664 break;
665 default:
666 ret = -EINVAL;
667 goto end;
668 }
669 }
670
671 opt_input_path = poptGetArg(pc);
672
673 end:
674 if (pc) {
675 poptFreeContext(pc);
676 }
677 return ret;
678 }
679
680 void iter_trace(struct bt_context *bt_ctx)
681 {
682 struct bt_ctf_iter *iter;
683 struct bt_iter_pos begin_pos;
684 struct kprobes *kprobe;
685 const struct bt_ctf_event *event;
686 int i;
687 int ret = 0;
688
689 begin_pos.type = BT_SEEK_BEGIN;
690 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
691
692 /* at each event, verify the status of the process table */
693 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
694 fix_process_table,
695 NULL, NULL, NULL);
696 if (opt_textdump) {
697 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
698 print_timestamp,
699 NULL, NULL, NULL);
700 } else {
701 /* at each event check if we need to refresh */
702 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
703 check_timestamp,
704 NULL, NULL, NULL);
705 /* to handle the scheduling events */
706 bt_ctf_iter_add_callback(iter,
707 g_quark_from_static_string("sched_switch"),
708 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
709 /* to clean up the process table */
710 bt_ctf_iter_add_callback(iter,
711 g_quark_from_static_string("sched_process_free"),
712 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
713 /* to get all the process from the statedumps */
714 bt_ctf_iter_add_callback(iter,
715 g_quark_from_static_string(
716 "lttng_statedump_process_state"),
717 NULL, 0, handle_statedump_process_state,
718 NULL, NULL, NULL);
719
720 /* for IO top */
721 bt_ctf_iter_add_callback(iter,
722 g_quark_from_static_string("exit_syscall"),
723 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
724 bt_ctf_iter_add_callback(iter,
725 g_quark_from_static_string("sys_write"),
726 NULL, 0, handle_sys_write, NULL, NULL, NULL);
727 bt_ctf_iter_add_callback(iter,
728 g_quark_from_static_string("sys_read"),
729 NULL, 0, handle_sys_read, NULL, NULL, NULL);
730 bt_ctf_iter_add_callback(iter,
731 g_quark_from_static_string("sys_open"),
732 NULL, 0, handle_sys_open, NULL, NULL, NULL);
733 bt_ctf_iter_add_callback(iter,
734 g_quark_from_static_string("sys_close"),
735 NULL, 0, handle_sys_close, NULL, NULL, NULL);
736 bt_ctf_iter_add_callback(iter,
737 g_quark_from_static_string(
738 "lttng_statedump_file_descriptor"),
739 NULL, 0, handle_statedump_file_descriptor,
740 NULL, NULL, NULL);
741
742 /* for kprobes */
743 if (lttngtop.kprobes_table) {
744 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
745 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
746 bt_ctf_iter_add_callback(iter,
747 g_quark_from_static_string(
748 kprobe->probe_name),
749 NULL, 0, handle_kprobes,
750 NULL, NULL, NULL);
751 }
752 }
753 }
754
755 while ((event = bt_ctf_iter_read_event(iter)) != NULL) {
756 if (quit || reload_trace)
757 goto end_iter;
758 ret = bt_iter_next(bt_ctf_get_iter(iter));
759 if (ret < 0)
760 goto end_iter;
761 }
762
763 /* block until quit, we reached the end of the trace */
764 sem_wait(&end_trace_sem);
765
766 end_iter:
767 bt_ctf_iter_destroy(iter);
768 }
769
770 /*
771 * bt_context_add_traces_recursive: Open a trace recursively
772 * (copied from BSD code in converter/babeltrace.c)
773 *
774 * Find each trace present in the subdirectory starting from the given
775 * path, and add them to the context. The packet_seek parameter can be
776 * NULL: this specify to use the default format packet_seek.
777 *
778 * Return: 0 on success, nonzero on failure.
779 * Unable to open toplevel: failure.
780 * Unable to open some subdirectory or file: warn and continue;
781 */
782 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
783 const char *format_str,
784 void (*packet_seek)(struct bt_stream_pos *pos,
785 size_t offset, int whence))
786 {
787 FTS *tree;
788 FTSENT *node;
789 GArray *trace_ids;
790 char lpath[PATH_MAX];
791 char * const paths[2] = { lpath, NULL };
792 int ret = -1;
793
794 /*
795 * Need to copy path, because fts_open can change it.
796 * It is the pointer array, not the strings, that are constant.
797 */
798 strncpy(lpath, path, PATH_MAX);
799 lpath[PATH_MAX - 1] = '\0';
800
801 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
802 if (tree == NULL) {
803 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
804 path);
805 return -EINVAL;
806 }
807
808 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
809
810 while ((node = fts_read(tree))) {
811 int dirfd, metafd;
812
813 if (!(node->fts_info & FTS_D))
814 continue;
815
816 dirfd = open(node->fts_accpath, 0);
817 if (dirfd < 0) {
818 fprintf(stderr, "[error] [Context] Unable to open trace "
819 "directory file descriptor.\n");
820 ret = dirfd;
821 goto error;
822 }
823 metafd = openat(dirfd, "metadata", O_RDONLY);
824 if (metafd < 0) {
825 close(dirfd);
826 ret = -1;
827 continue;
828 } else {
829 int trace_id;
830
831 ret = close(metafd);
832 if (ret < 0) {
833 perror("close");
834 goto error;
835 }
836 ret = close(dirfd);
837 if (ret < 0) {
838 perror("close");
839 goto error;
840 }
841
842 trace_id = bt_context_add_trace(ctx,
843 node->fts_accpath, format_str,
844 packet_seek, NULL, NULL);
845 if (trace_id < 0) {
846 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
847 "for reading.\n", node->fts_accpath, path);
848 /* Allow to skip erroneous traces. */
849 continue;
850 }
851 g_array_append_val(trace_ids, trace_id);
852 }
853 }
854
855 g_array_free(trace_ids, TRUE);
856 return ret;
857
858 error:
859 return ret;
860 }
861
862 static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
863 int field_cnt, int *tid_check, int *pid_check,
864 int *procname_check, int *ppid_check)
865 {
866 int j;
867 struct perfcounter *global;
868 const char *name;
869
870 for (j = 0; j < field_cnt; j++) {
871 name = bt_ctf_get_decl_field_name(field_list[j]);
872 if (*tid_check == 0) {
873 if (strncmp(name, "tid", 3) == 0)
874 (*tid_check)++;
875 }
876 if (*pid_check == 0) {
877 if (strncmp(name, "pid", 3) == 0)
878 (*pid_check)++;
879 }
880 if (*ppid_check == 0) {
881 if (strncmp(name, "ppid", 4) == 0)
882 (*ppid_check)++;
883 }
884 if (*procname_check == 0) {
885 if (strncmp(name, "procname", 8) == 0)
886 (*procname_check)++;
887 }
888 if (strncmp(name, "perf_", 5) == 0) {
889 global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
890 if (!global) {
891 global = g_new0(struct perfcounter, 1);
892 /* by default, sort on the first perf context */
893 if (g_hash_table_size(global_perf_liszt) == 0)
894 global->sort = 1;
895 global->visible = 1;
896 g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
897 }
898 }
899 }
900
901 if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
902 *procname_check == 1)
903 return 0;
904
905 return -1;
906 }
907
908 /*
909 * check_requirements: check if the required context informations are available
910 *
911 * If each mandatory context information is available for at least in one
912 * event, return 0 otherwise return -1.
913 */
914 int check_requirements(struct bt_context *ctx)
915 {
916 unsigned int i, evt_cnt, field_cnt;
917 struct bt_ctf_event_decl *const * evt_list;
918 const struct bt_ctf_field_decl *const * field_list;
919 int tid_check = 0;
920 int pid_check = 0;
921 int procname_check = 0;
922 int ppid_check = 0;
923 int ret = 0;
924
925 bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt);
926 for (i = 0; i < evt_cnt; i++) {
927 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT,
928 &field_list, &field_cnt);
929 ret = check_field_requirements(field_list, field_cnt,
930 &tid_check, &pid_check, &procname_check,
931 &ppid_check);
932
933 bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT,
934 &field_list, &field_cnt);
935 ret = check_field_requirements(field_list, field_cnt,
936 &tid_check, &pid_check, &procname_check,
937 &ppid_check);
938
939 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT,
940 &field_list, &field_cnt);
941 ret = check_field_requirements(field_list, field_cnt,
942 &tid_check, &pid_check, &procname_check,
943 &ppid_check);
944 }
945
946 if (tid_check == 0) {
947 ret = -1;
948 fprintf(stderr, "[error] missing tid context information\n");
949 }
950 if (pid_check == 0) {
951 ret = -1;
952 fprintf(stderr, "[error] missing pid context information\n");
953 }
954 if (ppid_check == 0) {
955 ret = -1;
956 fprintf(stderr, "[error] missing ppid context information\n");
957 }
958 if (procname_check == 0) {
959 ret = -1;
960 fprintf(stderr, "[error] missing procname context information\n");
961 }
962
963 return ret;
964 }
965
966 ssize_t read_subbuffer(struct lttng_consumer_stream *kconsumerd_fd,
967 struct lttng_consumer_local_data *ctx)
968 {
969 unsigned long len;
970 int err;
971 long ret = 0;
972 int infd = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
973
974 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_SPLICE) {
975 /* Get the next subbuffer */
976 err = helper_kernctl_get_next_subbuf(infd);
977 if (err != 0) {
978 ret = errno;
979 perror("Reserving sub buffer failed (everything is normal, "
980 "it is due to concurrency)");
981 goto end;
982 }
983 /* read the whole subbuffer */
984 err = helper_kernctl_get_padded_subbuf_size(infd, &len);
985 if (err != 0) {
986 ret = errno;
987 perror("Getting sub-buffer len failed.");
988 goto end;
989 }
990
991 /* splice the subbuffer to the tracefile */
992 ret = helper_lttng_consumer_on_read_subbuffer_splice(ctx, kconsumerd_fd, len);
993 if (ret < 0) {
994 /*
995 * display the error but continue processing to try
996 * to release the subbuffer
997 */
998 fprintf(stderr,"Error splicing to tracefile\n");
999 }
1000 err = helper_kernctl_put_next_subbuf(infd);
1001 if (err != 0) {
1002 ret = errno;
1003 perror("Reserving sub buffer failed (everything is normal, "
1004 "it is due to concurrency)");
1005 goto end;
1006 }
1007 sem_post(&metadata_available);
1008 }
1009
1010 end:
1011 return 0;
1012 }
1013
1014 int on_update_fd(int key, uint32_t state)
1015 {
1016 /* let the lib handle the metadata FD */
1017 if (key == sessiond_metadata)
1018 return 0;
1019 return 1;
1020 }
1021
1022 int on_recv_fd(struct lttng_consumer_stream *kconsumerd_fd)
1023 {
1024 int ret;
1025 struct mmap_stream *new_mmap_stream;
1026
1027 /* Opening the tracefile in write mode */
1028 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd) != NULL) {
1029 ret = open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd),
1030 O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
1031 if (ret < 0) {
1032 perror("open");
1033 goto end;
1034 }
1035 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd, ret);
1036 }
1037
1038 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_MMAP) {
1039 new_mmap_stream = malloc(sizeof(struct mmap_stream));
1040 new_mmap_stream->fd = helper_get_lttng_consumer_stream_wait_fd(
1041 kconsumerd_fd);
1042 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
1043
1044 g_ptr_array_add(lttng_consumer_stream_array, kconsumerd_fd);
1045 /* keep mmap FDs internally */
1046 ret = 1;
1047 } else {
1048 consumerd_metadata = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
1049 sessiond_metadata = helper_get_lttng_consumer_stream_key(kconsumerd_fd);
1050 ret = 0;
1051 }
1052
1053 reload_trace = 1;
1054
1055 end:
1056 return ret;
1057 }
1058
1059 void live_consume(struct bt_context **bt_ctx)
1060 {
1061 int ret;
1062 FILE *metadata_fp;
1063
1064 sem_wait(&metadata_available);
1065 if (access("/tmp/livesession/kernel/metadata", F_OK) != 0) {
1066 fprintf(stderr,"no metadata\n");
1067 goto end;
1068 }
1069 metadata_fp = fopen("/tmp/livesession/kernel/metadata", "r");
1070
1071 *bt_ctx = bt_context_create();
1072 ret = bt_context_add_trace(*bt_ctx, NULL, "ctf",
1073 lttngtop_ctf_packet_seek, &mmap_list, metadata_fp);
1074 if (ret < 0) {
1075 printf("Error adding trace\n");
1076 goto end;
1077 }
1078
1079 end:
1080 return;
1081 }
1082
1083 int setup_consumer(char *command_sock_path, pthread_t *threads,
1084 struct lttng_consumer_local_data *ctx)
1085 {
1086 int ret = 0;
1087
1088 ctx = helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL,
1089 read_subbuffer, NULL, on_recv_fd, on_update_fd);
1090 if (!ctx)
1091 goto end;
1092
1093 unlink(command_sock_path);
1094 helper_lttng_consumer_set_command_sock_path(ctx, command_sock_path);
1095 helper_lttng_consumer_init();
1096
1097 /* Create the thread to manage the receive of fd */
1098 ret = pthread_create(&threads[0], NULL, helper_lttng_consumer_thread_receive_fds,
1099 (void *) ctx);
1100 if (ret != 0) {
1101 perror("pthread_create receive fd");
1102 goto end;
1103 }
1104 /* Create thread to manage the polling/writing of traces */
1105 ret = pthread_create(&threads[1], NULL, helper_lttng_consumer_thread_poll_fds,
1106 (void *) ctx);
1107 if (ret != 0) {
1108 perror("pthread_create poll fd");
1109 goto end;
1110 }
1111
1112 end:
1113 return ret;
1114 }
1115
1116 int enable_kprobes(struct lttng_handle *handle, char *channel_name)
1117 {
1118 struct lttng_event ev;
1119 struct kprobes *kprobe;
1120 int ret = 0;
1121 int i;
1122
1123 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
1124 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
1125
1126 memset(&ev, '\0', sizeof(struct lttng_event));
1127 ev.type = LTTNG_EVENT_PROBE;
1128 if (kprobe->symbol_name)
1129 sprintf(ev.attr.probe.symbol_name, "%s", kprobe->symbol_name);
1130 sprintf(ev.name, "%s", kprobe->probe_name);
1131 ev.attr.probe.addr = kprobe->probe_addr;
1132 ev.attr.probe.offset = kprobe->probe_offset;
1133 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
1134 fprintf(stderr,"error enabling kprobes : %s\n",
1135 helper_lttcomm_get_readable_code(ret));
1136 goto end;
1137 }
1138 }
1139
1140 end:
1141 return ret;
1142 }
1143
1144 int setup_live_tracing()
1145 {
1146 struct lttng_domain dom;
1147 struct lttng_channel chan;
1148 char *channel_name = "mmapchan";
1149 struct lttng_event ev;
1150 int ret = 0;
1151 char *command_sock_path = "/tmp/consumerd_sock";
1152 static pthread_t threads[2]; /* recv_fd, poll */
1153 struct lttng_event_context kctxpid, kctxcomm, kctxppid, kctxtid;
1154
1155 struct lttng_handle *handle;
1156
1157 BT_INIT_LIST_HEAD(&mmap_list.head);
1158
1159 lttng_consumer_stream_array = g_ptr_array_new();
1160
1161 if ((ret = setup_consumer(command_sock_path, threads, ctx)) < 0) {
1162 fprintf(stderr,"error setting up consumer\n");
1163 goto error;
1164 }
1165
1166 available_snapshots = g_ptr_array_new();
1167
1168 /* setup the session */
1169 dom.type = LTTNG_DOMAIN_KERNEL;
1170
1171 ret = unlink("/tmp/livesession");
1172
1173 lttng_destroy_session("test");
1174 if ((ret = lttng_create_session("test", "/tmp/livesession")) < 0) {
1175 fprintf(stderr,"error creating the session : %s\n",
1176 helper_lttcomm_get_readable_code(ret));
1177 goto error;
1178 }
1179
1180 if ((handle = lttng_create_handle("test", &dom)) == NULL) {
1181 fprintf(stderr,"error creating handle\n");
1182 goto error_session;
1183 }
1184
1185 /*
1186 * FIXME : need to let the
1187 * helper_lttng_consumer_thread_receive_fds create the
1188 * socket.
1189 * Cleaner solution ?
1190 */
1191 while (access(command_sock_path, F_OK)) {
1192 sleep(0.1);
1193 }
1194
1195 if ((ret = lttng_register_consumer(handle, command_sock_path)) < 0) {
1196 fprintf(stderr,"error registering consumer : %s\n",
1197 helper_lttcomm_get_readable_code(ret));
1198 goto error_session;
1199 }
1200
1201 strcpy(chan.name, channel_name);
1202 chan.attr.overwrite = 0;
1203 if (opt_tid && opt_textdump) {
1204 chan.attr.subbuf_size = 32768;
1205 chan.attr.num_subbuf = 8;
1206 } else {
1207 //chan.attr.subbuf_size = 1048576; /* 1MB */
1208 chan.attr.subbuf_size = 2097152; /* 1MB */
1209 chan.attr.num_subbuf = 4;
1210 }
1211 chan.attr.switch_timer_interval = 0;
1212 chan.attr.read_timer_interval = 200;
1213 chan.attr.output = LTTNG_EVENT_MMAP;
1214
1215 if ((ret = lttng_enable_channel(handle, &chan)) < 0) {
1216 fprintf(stderr,"error creating channel : %s\n",
1217 helper_lttcomm_get_readable_code(ret));
1218 goto error_session;
1219 }
1220
1221 memset(&ev, '\0', sizeof(struct lttng_event));
1222 //sprintf(ev.name, "sched_switch");
1223 ev.type = LTTNG_EVENT_TRACEPOINT;
1224 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
1225 fprintf(stderr,"error enabling event : %s\n",
1226 helper_lttcomm_get_readable_code(ret));
1227 goto error_session;
1228 }
1229
1230 memset(&ev, '\0', sizeof(struct lttng_event));
1231 ev.type = LTTNG_EVENT_SYSCALL;
1232 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
1233 fprintf(stderr,"error enabling syscalls : %s\n",
1234 helper_lttcomm_get_readable_code(ret));
1235 goto error_session;
1236 }
1237
1238 if (lttngtop.kprobes_table) {
1239 ret = enable_kprobes(handle, channel_name);
1240 if (ret < 0) {
1241 goto error_session;
1242 }
1243 }
1244
1245 kctxpid.ctx = LTTNG_EVENT_CONTEXT_PID;
1246 lttng_add_context(handle, &kctxpid, NULL, NULL);
1247 kctxtid.ctx = LTTNG_EVENT_CONTEXT_TID;
1248 lttng_add_context(handle, &kctxtid, NULL, NULL);
1249 kctxppid.ctx = LTTNG_EVENT_CONTEXT_PPID;
1250 lttng_add_context(handle, &kctxppid, NULL, NULL);
1251 kctxcomm.ctx = LTTNG_EVENT_CONTEXT_PROCNAME;
1252 lttng_add_context(handle, &kctxcomm, NULL, NULL);
1253 kctxpid.ctx = LTTNG_EVENT_CONTEXT_VPID;
1254 lttng_add_context(handle, &kctxpid, NULL, NULL);
1255 kctxtid.ctx = LTTNG_EVENT_CONTEXT_VTID;
1256 lttng_add_context(handle, &kctxtid, NULL, NULL);
1257 kctxtid.ctx = LTTNG_EVENT_CONTEXT_HOSTNAME;
1258 lttng_add_context(handle, &kctxtid, NULL, NULL);
1259
1260 if ((ret = lttng_start_tracing("test")) < 0) {
1261 fprintf(stderr,"error starting tracing : %s\n",
1262 helper_lttcomm_get_readable_code(ret));
1263 goto error_session;
1264 }
1265
1266 helper_kernctl_buffer_flush(consumerd_metadata);
1267
1268 /* block until metadata is ready */
1269 sem_init(&metadata_available, 0, 0);
1270
1271 return 0;
1272
1273 error_session:
1274 lttng_destroy_session("test");
1275 error:
1276 return -1;
1277 }
1278
1279 int main(int argc, char **argv)
1280 {
1281 int ret;
1282 struct bt_context *bt_ctx = NULL;
1283 struct mmap_stream *mmap_info;
1284 unsigned long mmap_len;
1285
1286 init_lttngtop();
1287 ret = parse_options(argc, argv);
1288 if (ret < 0) {
1289 fprintf(stdout, "Error parsing options.\n\n");
1290 usage(stdout);
1291 exit(EXIT_FAILURE);
1292 } else if (ret > 0) {
1293 exit(EXIT_SUCCESS);
1294 }
1295
1296 if (!opt_input_path) {
1297 if (opt_textdump) {
1298 signal(SIGTERM, handle_textdump_sigterm);
1299 signal(SIGINT, handle_textdump_sigterm);
1300 }
1301 ret = setup_live_tracing();
1302 if (ret < 0) {
1303 goto end;
1304 }
1305 if (!opt_textdump) {
1306 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1307 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1308 }
1309 while (!quit) {
1310 reload_trace = 0;
1311 live_consume(&bt_ctx);
1312 iter_trace(bt_ctx);
1313 ret = bt_context_remove_trace(bt_ctx, 0);
1314 if (ret != 0)
1315 fprintf(stderr, "error removing trace\n");
1316 if (bt_ctx) {
1317 bt_context_put(bt_ctx);
1318 }
1319
1320 /*
1321 * since we receive all FDs every time there is an
1322 * update and the FD number is different every time,
1323 * we don't know which one are valid.
1324 * so we check if all FDs are usable with a simple
1325 * ioctl call.
1326 */
1327 bt_list_for_each_entry(mmap_info, &mmap_list.head, list) {
1328 ret = helper_kernctl_get_mmap_len(mmap_info->fd, &mmap_len);
1329 if (ret != 0) {
1330 bt_list_del(&mmap_info->list);
1331 }
1332 }
1333 sem_post(&metadata_available);
1334 }
1335
1336 pthread_join(timer_thread, NULL);
1337 quit = 1;
1338 pthread_join(display_thread, NULL);
1339
1340 lttng_stop_tracing("test");
1341 lttng_destroy_session("test");
1342
1343 goto end;
1344 } else {
1345 //init_lttngtop();
1346
1347 bt_ctx = bt_context_create();
1348 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
1349 if (ret < 0) {
1350 fprintf(stderr, "[error] Opening the trace\n");
1351 goto end;
1352 }
1353
1354 ret = check_requirements(bt_ctx);
1355 if (ret < 0) {
1356 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
1357 goto end;
1358 }
1359 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1360 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1361
1362 iter_trace(bt_ctx);
1363
1364 pthread_join(display_thread, NULL);
1365 quit = 1;
1366 pthread_join(timer_thread, NULL);
1367 }
1368
1369 end:
1370 if (bt_ctx)
1371 bt_context_put(bt_ctx);
1372
1373 return 0;
1374 }
This page took 0.09618 seconds and 4 git commands to generate.