2 * Copyright (C) 2013 Julien Desfossez
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;
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.
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.
22 #include <babeltrace/babeltrace.h>
23 #include <babeltrace/ctf/events.h>
24 #include <babeltrace/ctf/callbacks.h>
25 #include <babeltrace/ctf/iterator.h>
37 #include <sys/types.h>
43 #define LTTNG_SYMBOL_NAME_LEN 256
45 #ifdef LTTNGTOP_MMAP_LIVE
46 #include <lttng/lttngtop-helper.h>
47 #include <babeltrace/lttngtopmmappacketseek.h>
48 #include "mmap-live.h"
49 #endif /* LTTNGTOP_MMAP_LIVE */
51 #include "lttngtoptypes.h"
53 #include "iostreamtop.h"
55 #include "network-live.h"
56 #include "lttng-session.h"
58 #ifdef HAVE_LIBNCURSES
59 #include "cursesdisplay.h"
62 #define NET_URL_PREFIX "net://"
63 #define NET4_URL_PREFIX "net4://"
64 #define NET6_URL_PREFIX "net6://"
66 #define DEFAULT_FILE_ARRAY_SIZE 1
68 const char *opt_input_path
;
75 /* We need at least one valid trace to start processing. */
78 struct lttngtop
*copy
;
79 pthread_t display_thread
;
80 pthread_t timer_thread
;
82 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
83 unsigned long last_display_update
= 0;
84 unsigned long last_event_ts
= 0;
85 struct syscall
*last_syscall
;
87 /* list of FDs available for being read with snapshots */
88 struct bt_mmap_stream_list mmap_list
;
89 GPtrArray
*lttng_consumer_stream_array
;
90 int sessiond_metadata
, consumerd_metadata
;
91 struct lttng_consumer_local_data
*ctx
= NULL
;
92 /* list of snapshots currently not consumed */
93 GPtrArray
*available_snapshots
;
94 sem_t metadata_available
;
113 OPT_CREATE_LOCAL_SESSION
,
114 OPT_CREATE_LIVE_SESSION
,
117 static struct poptOption long_options
[] = {
118 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
119 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
120 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
121 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
122 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
123 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
124 { "procname", 'n', POPT_ARG_STRING
, &opt_procname
, OPT_PROCNAME
, NULL
, NULL
},
125 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
126 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
127 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
128 { "all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
129 { "output", 'o', POPT_ARG_STRING
, &opt_output
, OPT_OUTPUT_FILE
, NULL
, NULL
},
130 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
131 { "gui-test", 'g', POPT_ARG_NONE
, NULL
, OPT_GUI_TEST
, NULL
, NULL
},
132 { "create-local-session", 0, POPT_ARG_NONE
, NULL
, OPT_CREATE_LOCAL_SESSION
, NULL
, NULL
},
133 { "create-live-session", 0, POPT_ARG_NONE
, NULL
, OPT_CREATE_LIVE_SESSION
, NULL
, NULL
},
134 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
137 #ifdef LTTNGTOP_MMAP_LIVE
138 static void handle_textdump_sigterm(int signal
)
141 lttng_destroy_session("test");
145 void *refresh_thread(void *p
)
149 sem_post(&pause_sem
);
151 sem_post(&end_trace_sem
);
152 sem_post(&goodtodisplay
);
153 sem_post(&goodtoupdate
);
156 if (!opt_input_path
) {
157 #ifdef LTTNGTOP_MMAP_LIVE
158 mmap_live_flush(mmap_list
);
161 sem_wait(&pause_sem
);
162 sem_post(&pause_sem
);
164 sleep(refresh_display
/NSEC_PER_SEC
);
168 #ifdef HAVE_LIBNCURSES
169 void *ncurses_display(void *p
)
171 unsigned int current_display_index
= 0;
173 sem_wait(&bootstrap
);
175 * Prevent the 1 second delay when we hit ESC
182 sem_wait(&goodtodisplay
);
183 sem_wait(&pause_sem
);
186 sem_post(&pause_sem
);
192 copy
= g_ptr_array_index(copies
, current_display_index
);
194 display(current_display_index
++);
196 sem_post(&goodtoupdate
);
197 sem_post(&pause_sem
);
200 #endif /* HAVE_LIBNCURSES */
202 void print_fields(struct bt_ctf_event
*event
, const char *procname
,
206 const struct bt_definition
*const * list
;
207 const struct bt_declaration
*l
;
208 const struct bt_definition
*scope
;
209 enum ctf_type_id type
;
211 struct processtop
*current_proc
;
212 struct files
*current_file
;
213 int fd
, fd_value
= -1;
215 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
217 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
218 for (i
= 0; i
< cnt
; i
++) {
220 fprintf(output
, ", ");
221 fprintf(output
, "%s = ", bt_ctf_field_name(list
[i
]));
222 l
= bt_ctf_get_decl_from_def(list
[i
]);
223 if (strncmp(bt_ctf_field_name(list
[i
]), "fd", 2) == 0)
227 type
= bt_ctf_field_type(l
);
228 if (type
== CTF_TYPE_INTEGER
) {
229 if (bt_ctf_get_int_signedness(l
) == 0) {
230 fd_value
= bt_ctf_get_uint64(list
[i
]);
231 fprintf(output
, "%" PRIu64
, bt_ctf_get_uint64(list
[i
]));
233 fd_value
= bt_ctf_get_int64(list
[i
]);
234 fprintf(output
, "%" PRId64
, bt_ctf_get_int64(list
[i
]));
236 } else if (type
== CTF_TYPE_STRING
) {
237 fprintf(output
, "%s", bt_ctf_get_string(list
[i
]));
238 } else if (type
== CTF_TYPE_ARRAY
) {
239 str
= bt_ctf_get_char_array(list
[i
]);
240 if (!bt_ctf_field_get_error() && str
)
241 fprintf(output
, "%s", str
);
244 current_proc
= find_process_tid(<tngtop
, pid
, procname
);
247 current_file
= get_file(current_proc
, fd_value
);
248 if (!current_file
|| !current_file
->name
)
250 fprintf(output
, "<%s>", current_file
->name
);
256 * hook on each event to check the timestamp and refresh the display if
259 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
261 unsigned long timestamp
;
264 uint64_t ts_nsec_start
;
265 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
266 const struct bt_definition
*scope
;
267 const char *hostname
, *procname
;
269 char *from_syscall
= NULL
;
270 int syscall_exit
= 0;
272 timestamp
= bt_ctf_get_timestamp(call_data
);
274 /* can happen in network live when tracing is idle */
275 if (timestamp
< last_event_ts
)
278 last_event_ts
= timestamp
;
280 start
= format_timestamp(timestamp
);
281 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
283 pid
= get_context_pid(call_data
);
284 if (pid
== -1ULL && opt_tid
) {
288 tid
= get_context_tid(call_data
);
290 hostname
= get_context_hostname(call_data
);
295 if (opt_tid
|| opt_procname
|| opt_exec_name
) {
296 if (!lookup_filter_tid_list(lookup
)) {
297 /* To display when a process of ours in getting scheduled in */
298 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
301 scope
= bt_ctf_get_top_level_scope(call_data
,
303 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
304 scope
, "_next_tid"));
305 if (bt_ctf_field_get_error()) {
306 fprintf(stderr
, "Missing next_tid field\n");
309 if (!lookup_filter_tid_list(next_tid
)) {
314 fprintf(output
, "%c[1m", 27);
316 } else if (!opt_all
) {
321 fprintf(output
, "%c[1m", 27);
325 if (((strncmp(bt_ctf_event_name(call_data
),
326 "exit_syscall", 12)) == 0) ||
327 ((strncmp(bt_ctf_event_name(call_data
),
328 "syscall_exit", 12)) == 0)) {
332 if (last_syscall
&& !syscall_exit
) {
334 fprintf(output
, " ...interrupted...\n");
337 cpu_id
= get_cpu_id(call_data
);
338 procname
= get_context_comm(call_data
);
339 if ((strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) ||
340 (strncmp(bt_ctf_event_name(call_data
), "syscall_entry", 13) == 0)){
341 cpu
= get_cpu(cpu_id
);
342 cpu
->current_syscall
= g_new0(struct syscall
, 1);
343 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
344 cpu
->current_syscall
->ts_start
= timestamp
;
345 cpu
->current_syscall
->cpu_id
= cpu_id
;
346 last_syscall
= cpu
->current_syscall
;
348 } else if (syscall_exit
) {
351 /* Return code of a syscall if it was the last displayed event. */
352 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
353 if (last_syscall
->cpu_id
== cpu_id
) {
356 delta
= timestamp
- last_syscall
->ts_start
;
357 scope
= bt_ctf_get_top_level_scope(call_data
,
359 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
362 fprintf(output
, "= %" PRId64
" (%" PRIu64
".%09" PRIu64
"s)\n",
363 syscall_ret
, delta
/ NSEC_PER_SEC
,
364 delta
% NSEC_PER_SEC
);
369 fprintf(output
, " ...interrupted...\n");
373 cpu
= get_cpu(cpu_id
);
374 if (cpu
->current_syscall
) {
375 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
376 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
377 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
378 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
379 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
380 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
381 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
382 cpu_id
, cpu
->current_syscall
->name
);
386 free(cpu
->current_syscall
->name
);
387 g_free(cpu
->current_syscall
);
388 cpu
->current_syscall
= NULL
;
395 delta
= timestamp
- prev_ts
;
398 fprintf(output
, "%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
399 "(cpu %d) [%s (%d/%d)] %s (",
400 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
401 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
402 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
403 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
404 bt_ctf_event_name(call_data
));
405 print_fields(call_data
, procname
, pid
);
406 fprintf(output
, ")%s%c", (from_syscall
) ? from_syscall
: "",
407 (!current_syscall
) ? '\n' : ' ');
410 if (opt_all
&& (opt_tid
|| opt_procname
|| opt_exec_name
))
411 fprintf(output
, "%c[0m", 27);
416 return BT_CB_ERROR_STOP
;
418 return BT_CB_OK_STOP
;
421 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
424 struct kprobes
*kprobe
;
427 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
428 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
429 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
438 * hook on each event to check the timestamp and refresh the display if
441 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
443 unsigned long timestamp
;
445 timestamp
= bt_ctf_get_timestamp(call_data
);
446 if (timestamp
== -1ULL)
449 /* can happen in network live when tracing is idle */
450 if (timestamp
< last_event_ts
)
453 last_event_ts
= timestamp
;
455 if (last_display_update
== 0)
456 last_display_update
= timestamp
;
458 if (timestamp
- last_display_update
>= refresh_display
) {
459 sem_wait(&goodtoupdate
);
460 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
462 sem_post(&goodtodisplay
);
463 sem_post(&bootstrap
);
464 last_display_update
= timestamp
;
469 fprintf(stderr
, "check_timestamp callback error\n");
470 return BT_CB_ERROR_STOP
;
473 return BT_CB_OK_STOP
;
477 * get_perf_counter : get or create and return a perf_counter struct for
478 * either a process or a cpu (only one of the 2 parameters mandatory)
480 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
483 struct perfcounter
*ret
;
493 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
497 ret
= g_new0(struct perfcounter
, 1);
498 /* by default, make it visible in the UI */
500 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
509 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
510 const char *name
, int value
)
512 struct perfcounter
*cpu_perf
, *process_perf
;
514 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
515 if (cpu_perf
->count
< value
) {
516 process_perf
= get_perf_counter(name
, proc
, NULL
);
517 process_perf
->count
+= value
- cpu_perf
->count
;
518 cpu_perf
->count
= value
;
522 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
523 const struct bt_definition
*scope
,
524 struct processtop
*proc
,
527 struct bt_definition
const * const *list
= NULL
;
528 const struct bt_definition
*field
;
530 struct perfcounter
*perfcounter
;
538 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
545 g_hash_table_iter_init(&iter
, global_perf_liszt
);
546 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
547 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
549 int value
= bt_ctf_get_uint64(field
);
550 if (bt_ctf_field_get_error())
552 update_perf_value(proc
, cpu
, (char *) key
, value
);
560 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
563 const struct bt_definition
*scope
;
565 cpu
= get_cpu(get_cpu_id(event
));
567 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
568 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
570 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
571 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
573 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
574 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
577 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
580 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
581 char *comm
, *hostname
;
582 struct processtop
*parent
, *child
;
583 unsigned long timestamp
;
585 timestamp
= bt_ctf_get_timestamp(call_data
);
586 if (timestamp
== -1ULL)
589 pid
= get_context_pid(call_data
);
594 tid
= get_context_tid(call_data
);
598 ppid
= get_context_ppid(call_data
);
602 vpid
= get_context_vpid(call_data
);
606 vtid
= get_context_vtid(call_data
);
610 vppid
= get_context_vppid(call_data
);
614 comm
= get_context_comm(call_data
);
619 hostname
= get_context_hostname(call_data
);
621 /* find or create the current process */
622 child
= find_process_tid(<tngtop
, tid
, comm
);
624 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
627 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
629 if (opt_procname
&& lookup_procname(comm
) &&
630 !lookup_filter_tid_list(tid
)) {
633 tmp_tid
= malloc(sizeof(int));
635 printf("ADDING %s %d\n", comm
, tid
);
636 g_hash_table_insert(global_filter_list
,
637 (gpointer
) tmp_tid
, tmp_tid
);
641 /* find or create the parent */
642 parent
= find_process_tid(<tngtop
, pid
, comm
);
644 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
649 /* attach the parent to the current process */
650 child
->threadparent
= parent
;
651 add_thread(parent
, child
);
654 update_perf_counter(child
, call_data
);
660 return BT_CB_ERROR_STOP
;
665 copies
= g_ptr_array_new();
666 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
667 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
668 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
669 global_procname_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
670 tid_filter_list
= g_hash_table_new(g_str_hash
,
673 sem_init(&goodtodisplay
, 0, 0);
674 sem_init(&goodtoupdate
, 0, 1);
675 sem_init(&timer
, 0, 1);
676 sem_init(&bootstrap
, 0, 0);
677 sem_init(&pause_sem
, 0, 1);
678 sem_init(&end_trace_sem
, 0, 0);
680 reset_global_counters();
682 lttngtop
.nbthreads
= 0;
683 lttngtop
.nbfiles
= 0;
685 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
687 lttngtop
.process_table
= g_ptr_array_new();
688 lttngtop
.files_table
= g_ptr_array_new();
689 lttngtop
.cpu_table
= g_ptr_array_new();
696 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
697 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
698 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
699 fprintf(fp
, " -h, --help This help message\n");
700 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
701 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
702 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
703 fprintf(fp
, " -n, --procname Comma-separated list of procnames to display (require procname context in trace)\n");
704 fprintf(fp
, " -a, --all In textdump mode, display all events but write in bold the processes we are interested in (-f, -p and -n)\n");
705 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
706 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
707 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
708 fprintf(fp
, " -o, --output <filename> In textdump, output the log in <filename>\n");
709 fprintf(fp
, " -g, --gui-test Test if the ncurses support is compiled in (return 0 if it is)\n");
710 fprintf(fp
, " --create-local-session Setup a LTTng local session with all the right parameters\n");
711 fprintf(fp
, " --create-live-session Setup a LTTng live session on localhost with all the right parameters\n");
715 * Parse probe options.
716 * Shamelessly stolen from lttng-tools :
717 * src/bin/lttng/commands/enable_events.c
719 static struct kprobes
*parse_probe_opts(char *opt
)
722 char name
[LTTNG_SYMBOL_NAME_LEN
];
723 struct kprobes
*kprobe
;
727 kprobe->probe_addr = 0;
728 kprobe->probe_offset = 0;
729 asprintf(&kprobe->probe_name, "probe_sys_open");
730 asprintf(&kprobe->symbol_name, "sys_open");
738 kprobe
= g_new0(struct kprobes
, 1);
740 /* Check for symbol+offset */
741 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
743 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
744 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
746 if (strlen(s_hex
) == 0) {
747 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
751 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
752 kprobe
->probe_addr
= 0;
756 /* Check for symbol */
757 if (isalpha(name
[0])) {
758 ret
= sscanf(opt
, "%s", name
);
760 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
761 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
762 kprobe
->probe_offset
= 0;
763 kprobe
->probe_addr
= 0;
768 /* Check for address */
769 ret
= sscanf(opt
, "%s", s_hex
);
771 if (strlen(s_hex
) == 0) {
772 fprintf(stderr
, "Invalid probe address %s", s_hex
);
776 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
777 kprobe
->probe_offset
= 0;
778 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
790 * Return 0 if caller should continue, < 0 if caller should return
791 * error, > 0 if caller should exit without reporting error.
793 static int parse_options(int argc
, char **argv
)
803 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
804 poptReadDefaultConfig(pc
, 0);
806 while ((opt
= poptGetNextOpt(pc
)) != -1) {
810 ret
= 1; /* exit cleanly */
813 #ifdef HAVE_LIBNCURSES
819 case OPT_CREATE_LOCAL_SESSION
:
820 ret
= create_local_session();
822 case OPT_CREATE_LIVE_SESSION
:
823 ret
= create_live_local_session(NULL
, NULL
, 1);
836 tmp_str
= strtok(opt_tid
, ",");
838 tid
= malloc(sizeof(int));
839 *tid
= atoi(tmp_str
);
840 g_hash_table_insert(tid_filter_list
,
841 (gpointer
) tid
, tid
);
842 tmp_str
= strtok(NULL
, ",");
846 /* start reading the live trace from the beginning */
851 tmp_str
= strtok(opt_procname
, ",");
853 add_procname_list(tmp_str
, 1);
854 tmp_str
= strtok(NULL
, ",");
857 case OPT_RELAY_HOSTNAME
:
861 lttngtop
.kprobes_table
= g_ptr_array_new();
862 tmp_str
= strtok(opt_kprobes
, ",");
864 struct kprobes
*kprobe
;
866 kprobe
= parse_probe_opts(tmp_str
);
869 lttngtop
.kprobes_table
,
875 tmp_str
= strtok(NULL
, ",");
878 case OPT_OUTPUT_FILE
:
881 babeltrace_verbose
= 1;
889 opt_exec_name
= NULL
;
890 opt_exec_argv
= NULL
;
891 for (i
= 0; i
< argc
; i
++) {
892 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
893 opt_exec_name
= argv
[i
+ 1];
894 opt_exec_argv
= &argv
[i
+ 1];
898 if (!opt_exec_name
) {
899 opt_input_path
= poptGetArg(pc
);
902 opt_output
= strdup("/dev/stdout");
904 output
= fopen(opt_output
, "w");
906 perror("Error opening output file");
918 void iter_trace(struct bt_context
*bt_ctx
)
920 struct bt_ctf_iter
*iter
;
921 struct bt_iter_pos begin_pos
;
922 struct kprobes
*kprobe
;
923 const struct bt_ctf_event
*event
;
927 begin_pos
.type
= BT_SEEK_BEGIN
;
928 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
930 /* at each event, verify the status of the process table */
931 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
934 /* to handle the follow child option */
935 bt_ctf_iter_add_callback(iter
,
936 g_quark_from_static_string("sched_process_fork"),
937 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
938 /* to clean up the process table */
939 bt_ctf_iter_add_callback(iter
,
940 g_quark_from_static_string("sched_process_free"),
941 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
942 /* to get all the process from the statedumps */
943 bt_ctf_iter_add_callback(iter
,
944 g_quark_from_static_string(
945 "lttng_statedump_process_state"),
946 NULL
, 0, handle_statedump_process_state
,
948 bt_ctf_iter_add_callback(iter
,
949 g_quark_from_static_string(
950 "lttng_statedump_file_descriptor"),
951 NULL
, 0, handle_statedump_file_descriptor
,
953 bt_ctf_iter_add_callback(iter
,
954 g_quark_from_static_string("sys_open"),
955 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
956 bt_ctf_iter_add_callback(iter
,
957 g_quark_from_static_string("syscall_entry_open"),
958 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
960 bt_ctf_iter_add_callback(iter
,
961 g_quark_from_static_string("sys_socket"),
962 NULL
, 0, handle_sys_socket
, NULL
, NULL
, NULL
);
963 bt_ctf_iter_add_callback(iter
,
964 g_quark_from_static_string("syscall_entry_socket"),
965 NULL
, 0, handle_sys_socket
, NULL
, NULL
, NULL
);
967 bt_ctf_iter_add_callback(iter
,
968 g_quark_from_static_string("sys_close"),
969 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
970 bt_ctf_iter_add_callback(iter
,
971 g_quark_from_static_string("syscall_entry_close"),
972 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
974 bt_ctf_iter_add_callback(iter
,
975 g_quark_from_static_string("exit_syscall"),
976 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
977 bt_ctf_iter_add_callback(iter
,
978 g_quark_from_static_string("syscall_exit_open"),
979 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
980 bt_ctf_iter_add_callback(iter
,
981 g_quark_from_static_string("syscall_exit_socket"),
982 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
983 bt_ctf_iter_add_callback(iter
,
984 g_quark_from_static_string("syscall_exit_close"),
985 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
987 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
991 /* at each event check if we need to refresh */
992 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
995 /* to handle the scheduling events */
996 bt_ctf_iter_add_callback(iter
,
997 g_quark_from_static_string("sched_switch"),
998 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
1000 bt_ctf_iter_add_callback(iter
,
1001 g_quark_from_static_string("sys_write"),
1002 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
1003 bt_ctf_iter_add_callback(iter
,
1004 g_quark_from_static_string("syscall_entry_write"),
1005 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
1006 bt_ctf_iter_add_callback(iter
,
1007 g_quark_from_static_string("syscall_exit_write"),
1008 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
1010 bt_ctf_iter_add_callback(iter
,
1011 g_quark_from_static_string("sys_read"),
1012 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
1013 bt_ctf_iter_add_callback(iter
,
1014 g_quark_from_static_string("syscall_entry_read"),
1015 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
1016 bt_ctf_iter_add_callback(iter
,
1017 g_quark_from_static_string("syscall_exit_read"),
1018 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
1021 if (lttngtop
.kprobes_table
) {
1022 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
1023 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
1024 bt_ctf_iter_add_callback(iter
,
1025 g_quark_from_static_string(
1026 kprobe
->probe_name
),
1027 NULL
, 0, handle_kprobes
,
1033 if (opt_exec_name
) {
1038 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
1040 } else if (pid
> 0) {
1042 g_hash_table_insert(tid_filter_list
,
1051 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
1052 if (quit
|| reload_trace
)
1054 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1059 /* block until quit, we reached the end of the trace */
1060 sem_wait(&end_trace_sem
);
1063 bt_ctf_iter_destroy(iter
);
1067 * bt_context_add_traces_recursive: Open a trace recursively
1068 * (copied from BSD code in converter/babeltrace.c)
1070 * Find each trace present in the subdirectory starting from the given
1071 * path, and add them to the context. The packet_seek parameter can be
1072 * NULL: this specify to use the default format packet_seek.
1074 * Return: 0 on success, nonzero on failure.
1075 * Unable to open toplevel: failure.
1076 * Unable to open some subdirectory or file: warn and continue;
1078 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
1079 const char *format_str
,
1080 void (*packet_seek
)(struct bt_stream_pos
*pos
,
1081 size_t offset
, int whence
))
1086 char lpath
[PATH_MAX
];
1087 char * const paths
[2] = { lpath
, NULL
};
1090 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
1091 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
1092 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
1093 ret
= bt_context_add_trace(ctx
,
1094 path
, format_str
, packet_seek
, NULL
, NULL
);
1096 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
1097 "for reading.\n", path
);
1098 /* Allow to skip erroneous traces. */
1099 ret
= 1; /* partial error */
1104 * Need to copy path, because fts_open can change it.
1105 * It is the pointer array, not the strings, that are constant.
1107 strncpy(lpath
, path
, PATH_MAX
);
1108 lpath
[PATH_MAX
- 1] = '\0';
1110 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
1112 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
1117 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
1119 while ((node
= fts_read(tree
))) {
1122 if (!(node
->fts_info
& FTS_D
))
1125 dirfd
= open(node
->fts_accpath
, 0);
1127 fprintf(stderr
, "[error] [Context] Unable to open trace "
1128 "directory file descriptor.\n");
1132 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1139 ret
= close(metafd
);
1150 trace_id
= bt_context_add_trace(ctx
,
1151 node
->fts_accpath
, format_str
,
1152 packet_seek
, NULL
, NULL
);
1154 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1155 "for reading.\n", node
->fts_accpath
, path
);
1156 /* Allow to skip erroneous traces. */
1159 g_array_append_val(trace_ids
, trace_id
);
1163 g_array_free(trace_ids
, TRUE
);
1170 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1171 int field_cnt
, int *tid_check
, int *pid_check
,
1172 int *procname_check
, int *ppid_check
)
1175 struct perfcounter
*global
;
1178 for (j
= 0; j
< field_cnt
; j
++) {
1179 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1180 if (*tid_check
== 0) {
1181 if (strncmp(name
, "tid", 3) == 0)
1184 if (*pid_check
== 0) {
1185 if (strncmp(name
, "pid", 3) == 0)
1188 if (*ppid_check
== 0) {
1189 if (strncmp(name
, "ppid", 4) == 0)
1192 if (*procname_check
== 0) {
1193 if (strncmp(name
, "procname", 8) == 0)
1194 (*procname_check
)++;
1196 if (strncmp(name
, "perf_", 5) == 0) {
1197 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1199 global
= g_new0(struct perfcounter
, 1);
1200 /* by default, sort on the first perf context */
1201 if (g_hash_table_size(global_perf_liszt
) == 0)
1203 global
->visible
= 1;
1204 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1209 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1210 *procname_check
== 1)
1217 * check_requirements: check if the required context informations are available
1219 * If each mandatory context information is available for at least in one
1220 * event, return 0 otherwise return -1.
1222 int check_requirements(struct bt_context
*ctx
)
1224 unsigned int i
, evt_cnt
, field_cnt
;
1225 struct bt_ctf_event_decl
*const * evt_list
;
1226 const struct bt_ctf_field_decl
*const * field_list
;
1229 int procname_check
= 0;
1233 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1238 for (i
= 0; i
< evt_cnt
; i
++) {
1239 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1240 &field_list
, &field_cnt
);
1241 ret
= check_field_requirements(field_list
, field_cnt
,
1242 &tid_check
, &pid_check
, &procname_check
,
1245 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1246 &field_list
, &field_cnt
);
1247 ret
= check_field_requirements(field_list
, field_cnt
,
1248 &tid_check
, &pid_check
, &procname_check
,
1251 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1252 &field_list
, &field_cnt
);
1253 ret
= check_field_requirements(field_list
, field_cnt
,
1254 &tid_check
, &pid_check
, &procname_check
,
1258 if (tid_check
== 0) {
1260 fprintf(stderr
, "[error] missing tid context information\n");
1262 if (pid_check
== 0) {
1264 fprintf(stderr
, "[error] missing pid context information\n");
1266 if (ppid_check
== 0) {
1268 fprintf(stderr
, "[error] missing ppid context information\n");
1270 if (procname_check
== 0) {
1272 fprintf(stderr
, "[error] missing procname context information\n");
1282 static void handle_sigchild(int signal
)
1286 waitpid(opt_exec_pid
, &status
, 0);
1289 int main(int argc
, char **argv
, char **envp
)
1292 struct bt_context
*bt_ctx
= NULL
;
1293 char *live_session_name
= NULL
;
1296 ret
= parse_options(argc
, argv
);
1298 fprintf(stdout
, "Error parsing options.\n\n");
1301 } else if (ret
> 0) {
1305 if (opt_exec_name
) {
1306 opt_exec_env
= envp
;
1307 signal(SIGCHLD
, handle_sigchild
);
1310 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1312 ret
= create_live_local_session(&opt_relay_hostname
,
1313 &live_session_name
, 0);
1318 if (!opt_input_path
&& remote_live
) {
1320 bt_ctx
= bt_context_create();
1321 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1322 "lttng-live", NULL
);
1324 fprintf(stderr
, "[error] Opening the trace\n");
1328 bt_ctx
= bt_context_create();
1329 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1331 fprintf(stderr
, "[error] Opening the trace\n");
1335 ret
= check_requirements(bt_ctx
);
1336 if (ret
< 0 && !valid_trace
) {
1337 fprintf(stderr
, "[error] some mandatory contexts "
1338 "were missing, exiting.\n");
1342 if (!opt_textdump
) {
1343 #ifdef HAVE_LIBNCURSES
1344 pthread_create(&display_thread
, NULL
, ncurses_display
,
1346 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1349 printf("Ncurses support not compiled, please install "
1350 "the missing dependencies and recompile\n");
1359 pthread_join(display_thread
, NULL
);
1361 pthread_join(timer_thread
, NULL
);
1367 bt_context_put(bt_ctx
);
1369 if (live_session_name
) {
1370 ret
= destroy_live_local_session(live_session_name
);
1372 fprintf(stderr
, "Error destroying %s\n", live_session_name
);