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>
42 #include <lttng/lttng.h>
44 #ifdef LTTNGTOP_MMAP_LIVE
45 #include <lttng/lttngtop-helper.h>
46 #include <babeltrace/lttngtopmmappacketseek.h>
47 #include "mmap-live.h"
48 #endif /* LTTNGTOP_MMAP_LIVE */
50 #include "lttngtoptypes.h"
52 #include "iostreamtop.h"
54 #include "network-live.h"
55 #include "lttng-session.h"
57 #ifdef HAVE_LIBNCURSES
58 #include "cursesdisplay.h"
61 #define NET_URL_PREFIX "net://"
62 #define NET4_URL_PREFIX "net4://"
63 #define NET6_URL_PREFIX "net6://"
65 #define DEFAULT_FILE_ARRAY_SIZE 1
67 const char *opt_input_path
;
74 /* We need at least one valid trace to start processing. */
77 struct lttngtop
*copy
;
78 pthread_t display_thread
;
79 pthread_t timer_thread
;
81 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
82 unsigned long last_display_update
= 0;
83 unsigned long last_event_ts
= 0;
84 struct syscall
*last_syscall
;
86 /* list of FDs available for being read with snapshots */
87 struct bt_mmap_stream_list mmap_list
;
88 GPtrArray
*lttng_consumer_stream_array
;
89 int sessiond_metadata
, consumerd_metadata
;
90 struct lttng_consumer_local_data
*ctx
= NULL
;
91 /* list of snapshots currently not consumed */
92 GPtrArray
*available_snapshots
;
93 sem_t metadata_available
;
112 OPT_CREATE_LOCAL_SESSION
,
113 OPT_CREATE_LIVE_SESSION
,
116 static struct poptOption long_options
[] = {
117 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
118 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
119 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
120 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
121 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
122 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
123 { "procname", 'n', POPT_ARG_STRING
, &opt_procname
, OPT_PROCNAME
, NULL
, NULL
},
124 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
125 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
126 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
127 { "all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
128 { "output", 'o', POPT_ARG_STRING
, &opt_output
, OPT_OUTPUT_FILE
, NULL
, NULL
},
129 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
130 { "gui-test", 'g', POPT_ARG_NONE
, NULL
, OPT_GUI_TEST
, NULL
, NULL
},
131 { "create-local-session", 0, POPT_ARG_NONE
, NULL
, OPT_CREATE_LOCAL_SESSION
, NULL
, NULL
},
132 { "create-live-session", 0, POPT_ARG_NONE
, NULL
, OPT_CREATE_LIVE_SESSION
, NULL
, NULL
},
133 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
136 #ifdef LTTNGTOP_MMAP_LIVE
137 static void handle_textdump_sigterm(int signal
)
140 lttng_destroy_session("test");
144 void *refresh_thread(void *p
)
148 sem_post(&pause_sem
);
150 sem_post(&end_trace_sem
);
151 sem_post(&goodtodisplay
);
152 sem_post(&goodtoupdate
);
155 if (!opt_input_path
) {
156 #ifdef LTTNGTOP_MMAP_LIVE
157 mmap_live_flush(mmap_list
);
160 sem_wait(&pause_sem
);
161 sem_post(&pause_sem
);
163 sleep(refresh_display
/NSEC_PER_SEC
);
167 #ifdef HAVE_LIBNCURSES
168 void *ncurses_display(void *p
)
170 unsigned int current_display_index
= 0;
172 sem_wait(&bootstrap
);
174 * Prevent the 1 second delay when we hit ESC
181 sem_wait(&goodtodisplay
);
182 sem_wait(&pause_sem
);
185 sem_post(&pause_sem
);
191 copy
= g_ptr_array_index(copies
, current_display_index
);
193 display(current_display_index
++);
195 sem_post(&goodtoupdate
);
196 sem_post(&pause_sem
);
199 #endif /* HAVE_LIBNCURSES */
201 void print_fields(struct bt_ctf_event
*event
, const char *procname
,
205 const struct bt_definition
*const * list
;
206 const struct bt_declaration
*l
;
207 const struct bt_definition
*scope
;
208 enum ctf_type_id type
;
210 struct processtop
*current_proc
;
211 struct files
*current_file
;
212 int fd
, fd_value
= -1;
214 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
216 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
217 for (i
= 0; i
< cnt
; i
++) {
219 fprintf(output
, ", ");
220 fprintf(output
, "%s = ", bt_ctf_field_name(list
[i
]));
221 l
= bt_ctf_get_decl_from_def(list
[i
]);
222 if (strncmp(bt_ctf_field_name(list
[i
]), "fd", 2) == 0)
226 type
= bt_ctf_field_type(l
);
227 if (type
== CTF_TYPE_INTEGER
) {
228 if (bt_ctf_get_int_signedness(l
) == 0) {
229 fd_value
= bt_ctf_get_uint64(list
[i
]);
230 fprintf(output
, "%" PRIu64
, bt_ctf_get_uint64(list
[i
]));
232 fd_value
= bt_ctf_get_int64(list
[i
]);
233 fprintf(output
, "%" PRId64
, bt_ctf_get_int64(list
[i
]));
235 } else if (type
== CTF_TYPE_STRING
) {
236 fprintf(output
, "%s", bt_ctf_get_string(list
[i
]));
237 } else if (type
== CTF_TYPE_ARRAY
) {
238 str
= bt_ctf_get_char_array(list
[i
]);
239 if (!bt_ctf_field_get_error() && str
)
240 fprintf(output
, "%s", str
);
243 current_proc
= find_process_tid(<tngtop
, pid
, procname
);
246 current_file
= get_file(current_proc
, fd_value
);
247 if (!current_file
|| !current_file
->name
)
249 fprintf(output
, "<%s>", current_file
->name
);
255 * hook on each event to check the timestamp and refresh the display if
258 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
260 unsigned long timestamp
;
263 uint64_t ts_nsec_start
;
264 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
265 const struct bt_definition
*scope
;
266 const char *hostname
, *procname
;
268 char *from_syscall
= NULL
;
270 timestamp
= bt_ctf_get_timestamp(call_data
);
272 /* can happen in network live when tracing is idle */
273 if (timestamp
< last_event_ts
)
276 last_event_ts
= timestamp
;
278 start
= format_timestamp(timestamp
);
279 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
281 pid
= get_context_pid(call_data
);
282 if (pid
== -1ULL && opt_tid
) {
286 tid
= get_context_tid(call_data
);
288 hostname
= get_context_hostname(call_data
);
293 if (opt_tid
|| opt_procname
|| opt_exec_name
) {
294 if (!lookup_filter_tid_list(lookup
)) {
295 /* To display when a process of ours in getting scheduled in */
296 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
299 scope
= bt_ctf_get_top_level_scope(call_data
,
301 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
302 scope
, "_next_tid"));
303 if (bt_ctf_field_get_error()) {
304 fprintf(stderr
, "Missing next_tid field\n");
307 if (!lookup_filter_tid_list(next_tid
)) {
312 fprintf(output
, "%c[1m", 27);
314 } else if (!opt_all
) {
319 fprintf(output
, "%c[1m", 27);
323 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
324 "exit_syscall", 12)) != 0) {
326 fprintf(output
, " ...interrupted...\n");
329 cpu_id
= get_cpu_id(call_data
);
330 procname
= get_context_comm(call_data
);
331 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
332 cpu
= get_cpu(cpu_id
);
333 cpu
->current_syscall
= g_new0(struct syscall
, 1);
334 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
335 cpu
->current_syscall
->ts_start
= timestamp
;
336 cpu
->current_syscall
->cpu_id
= cpu_id
;
337 last_syscall
= cpu
->current_syscall
;
339 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
342 /* Return code of a syscall if it was the last displayed event. */
343 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
344 if (last_syscall
->cpu_id
== cpu_id
) {
347 delta
= timestamp
- last_syscall
->ts_start
;
348 scope
= bt_ctf_get_top_level_scope(call_data
,
350 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
353 fprintf(output
, "= %" PRId64
" (%" PRIu64
".%09" PRIu64
"s)\n",
354 syscall_ret
, delta
/ NSEC_PER_SEC
,
355 delta
% NSEC_PER_SEC
);
360 fprintf(output
, " ...interrupted...\n");
364 cpu
= get_cpu(cpu_id
);
365 if (cpu
->current_syscall
) {
366 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
367 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
368 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
369 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
370 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
371 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
372 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
373 cpu_id
, cpu
->current_syscall
->name
);
377 free(cpu
->current_syscall
->name
);
378 g_free(cpu
->current_syscall
);
379 cpu
->current_syscall
= NULL
;
386 delta
= timestamp
- prev_ts
;
389 fprintf(output
, "%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
390 "(cpu %d) [%s (%d/%d)] %s (",
391 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
392 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
393 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
394 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
395 bt_ctf_event_name(call_data
));
396 print_fields(call_data
, procname
, pid
);
397 fprintf(output
, ")%s%c", (from_syscall
) ? from_syscall
: "",
398 (!current_syscall
) ? '\n' : ' ');
401 if (opt_all
&& (opt_tid
|| opt_procname
|| opt_exec_name
))
402 fprintf(output
, "%c[0m", 27);
407 return BT_CB_ERROR_STOP
;
409 return BT_CB_OK_STOP
;
412 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
415 struct kprobes
*kprobe
;
418 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
419 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
420 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
429 * hook on each event to check the timestamp and refresh the display if
432 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
434 unsigned long timestamp
;
436 timestamp
= bt_ctf_get_timestamp(call_data
);
437 if (timestamp
== -1ULL)
440 /* can happen in network live when tracing is idle */
441 if (timestamp
< last_event_ts
)
444 last_event_ts
= timestamp
;
446 if (last_display_update
== 0)
447 last_display_update
= timestamp
;
449 if (timestamp
- last_display_update
>= refresh_display
) {
450 sem_wait(&goodtoupdate
);
451 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
453 sem_post(&goodtodisplay
);
454 sem_post(&bootstrap
);
455 last_display_update
= timestamp
;
460 fprintf(stderr
, "check_timestamp callback error\n");
461 return BT_CB_ERROR_STOP
;
464 return BT_CB_OK_STOP
;
468 * get_perf_counter : get or create and return a perf_counter struct for
469 * either a process or a cpu (only one of the 2 parameters mandatory)
471 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
474 struct perfcounter
*ret
;
484 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
488 ret
= g_new0(struct perfcounter
, 1);
489 /* by default, make it visible in the UI */
491 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
500 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
501 const char *name
, int value
)
503 struct perfcounter
*cpu_perf
, *process_perf
;
505 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
506 if (cpu_perf
->count
< value
) {
507 process_perf
= get_perf_counter(name
, proc
, NULL
);
508 process_perf
->count
+= value
- cpu_perf
->count
;
509 cpu_perf
->count
= value
;
513 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
514 const struct bt_definition
*scope
,
515 struct processtop
*proc
,
518 struct bt_definition
const * const *list
= NULL
;
519 const struct bt_definition
*field
;
521 struct perfcounter
*perfcounter
;
529 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
536 g_hash_table_iter_init(&iter
, global_perf_liszt
);
537 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
538 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
540 int value
= bt_ctf_get_uint64(field
);
541 if (bt_ctf_field_get_error())
543 update_perf_value(proc
, cpu
, (char *) key
, value
);
551 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
554 const struct bt_definition
*scope
;
556 cpu
= get_cpu(get_cpu_id(event
));
558 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
559 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
561 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
562 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
564 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
565 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
568 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
571 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
572 char *comm
, *hostname
;
573 struct processtop
*parent
, *child
;
574 unsigned long timestamp
;
576 timestamp
= bt_ctf_get_timestamp(call_data
);
577 if (timestamp
== -1ULL)
580 pid
= get_context_pid(call_data
);
585 tid
= get_context_tid(call_data
);
589 ppid
= get_context_ppid(call_data
);
593 vpid
= get_context_vpid(call_data
);
597 vtid
= get_context_vtid(call_data
);
601 vppid
= get_context_vppid(call_data
);
605 comm
= get_context_comm(call_data
);
610 hostname
= get_context_hostname(call_data
);
612 /* find or create the current process */
613 child
= find_process_tid(<tngtop
, tid
, comm
);
615 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
618 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
620 if (opt_procname
&& lookup_procname(comm
) &&
621 !lookup_filter_tid_list(tid
)) {
624 tmp_tid
= malloc(sizeof(int));
626 printf("ADDING %s %d\n", comm
, tid
);
627 g_hash_table_insert(global_filter_list
,
628 (gpointer
) tmp_tid
, tmp_tid
);
632 /* find or create the parent */
633 parent
= find_process_tid(<tngtop
, pid
, comm
);
635 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
640 /* attach the parent to the current process */
641 child
->threadparent
= parent
;
642 add_thread(parent
, child
);
645 update_perf_counter(child
, call_data
);
651 return BT_CB_ERROR_STOP
;
656 copies
= g_ptr_array_new();
657 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
658 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
659 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
660 global_procname_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
661 tid_filter_list
= g_hash_table_new(g_str_hash
,
664 sem_init(&goodtodisplay
, 0, 0);
665 sem_init(&goodtoupdate
, 0, 1);
666 sem_init(&timer
, 0, 1);
667 sem_init(&bootstrap
, 0, 0);
668 sem_init(&pause_sem
, 0, 1);
669 sem_init(&end_trace_sem
, 0, 0);
671 reset_global_counters();
673 lttngtop
.nbthreads
= 0;
674 lttngtop
.nbfiles
= 0;
676 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
678 lttngtop
.process_table
= g_ptr_array_new();
679 lttngtop
.files_table
= g_ptr_array_new();
680 lttngtop
.cpu_table
= g_ptr_array_new();
687 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
688 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
689 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
690 fprintf(fp
, " -h, --help This help message\n");
691 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
692 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
693 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
694 fprintf(fp
, " -n, --procname Comma-separated list of procnames to display (require procname context in trace)\n");
695 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");
696 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
697 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
698 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
699 fprintf(fp
, " -o, --output <filename> In textdump, output the log in <filename>\n");
700 fprintf(fp
, " -g, --gui-test Test if the ncurses support is compiled in (return 0 if it is)\n");
701 fprintf(fp
, " --create-local-session Setup a LTTng local session with all the right parameters\n");
702 fprintf(fp
, " --create-live-session Setup a LTTng live session on localhost with all the right parameters\n");
706 * Parse probe options.
707 * Shamelessly stolen from lttng-tools :
708 * src/bin/lttng/commands/enable_events.c
710 static struct kprobes
*parse_probe_opts(char *opt
)
713 char name
[LTTNG_SYMBOL_NAME_LEN
];
714 struct kprobes
*kprobe
;
718 kprobe->probe_addr = 0;
719 kprobe->probe_offset = 0;
720 asprintf(&kprobe->probe_name, "probe_sys_open");
721 asprintf(&kprobe->symbol_name, "sys_open");
729 kprobe
= g_new0(struct kprobes
, 1);
731 /* Check for symbol+offset */
732 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
734 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
735 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
737 if (strlen(s_hex
) == 0) {
738 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
742 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
743 kprobe
->probe_addr
= 0;
747 /* Check for symbol */
748 if (isalpha(name
[0])) {
749 ret
= sscanf(opt
, "%s", name
);
751 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
752 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
753 kprobe
->probe_offset
= 0;
754 kprobe
->probe_addr
= 0;
759 /* Check for address */
760 ret
= sscanf(opt
, "%s", s_hex
);
762 if (strlen(s_hex
) == 0) {
763 fprintf(stderr
, "Invalid probe address %s", s_hex
);
767 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
768 kprobe
->probe_offset
= 0;
769 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
781 * Return 0 if caller should continue, < 0 if caller should return
782 * error, > 0 if caller should exit without reporting error.
784 static int parse_options(int argc
, char **argv
)
794 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
795 poptReadDefaultConfig(pc
, 0);
797 while ((opt
= poptGetNextOpt(pc
)) != -1) {
801 ret
= 1; /* exit cleanly */
804 #ifdef HAVE_LIBNCURSES
810 case OPT_CREATE_LOCAL_SESSION
:
811 ret
= create_local_session();
813 case OPT_CREATE_LIVE_SESSION
:
814 ret
= create_live_local_session(NULL
, NULL
, 1);
827 tmp_str
= strtok(opt_tid
, ",");
829 tid
= malloc(sizeof(int));
830 *tid
= atoi(tmp_str
);
831 g_hash_table_insert(tid_filter_list
,
832 (gpointer
) tid
, tid
);
833 tmp_str
= strtok(NULL
, ",");
837 /* start reading the live trace from the beginning */
842 tmp_str
= strtok(opt_procname
, ",");
844 add_procname_list(tmp_str
, 1);
845 tmp_str
= strtok(NULL
, ",");
848 case OPT_RELAY_HOSTNAME
:
852 lttngtop
.kprobes_table
= g_ptr_array_new();
853 tmp_str
= strtok(opt_kprobes
, ",");
855 struct kprobes
*kprobe
;
857 kprobe
= parse_probe_opts(tmp_str
);
860 lttngtop
.kprobes_table
,
866 tmp_str
= strtok(NULL
, ",");
869 case OPT_OUTPUT_FILE
:
872 babeltrace_verbose
= 1;
880 opt_exec_name
= NULL
;
881 opt_exec_argv
= NULL
;
882 for (i
= 0; i
< argc
; i
++) {
883 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
884 opt_exec_name
= argv
[i
+ 1];
885 opt_exec_argv
= &argv
[i
+ 1];
889 if (!opt_exec_name
) {
890 opt_input_path
= poptGetArg(pc
);
893 opt_output
= strdup("/dev/stdout");
895 output
= fopen(opt_output
, "w");
897 perror("Error opening output file");
909 void iter_trace(struct bt_context
*bt_ctx
)
911 struct bt_ctf_iter
*iter
;
912 struct bt_iter_pos begin_pos
;
913 struct kprobes
*kprobe
;
914 const struct bt_ctf_event
*event
;
918 begin_pos
.type
= BT_SEEK_BEGIN
;
919 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
921 /* at each event, verify the status of the process table */
922 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
925 /* to handle the follow child option */
926 bt_ctf_iter_add_callback(iter
,
927 g_quark_from_static_string("sched_process_fork"),
928 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
929 /* to clean up the process table */
930 bt_ctf_iter_add_callback(iter
,
931 g_quark_from_static_string("sched_process_free"),
932 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
933 /* to get all the process from the statedumps */
934 bt_ctf_iter_add_callback(iter
,
935 g_quark_from_static_string(
936 "lttng_statedump_process_state"),
937 NULL
, 0, handle_statedump_process_state
,
939 bt_ctf_iter_add_callback(iter
,
940 g_quark_from_static_string(
941 "lttng_statedump_file_descriptor"),
942 NULL
, 0, handle_statedump_file_descriptor
,
944 bt_ctf_iter_add_callback(iter
,
945 g_quark_from_static_string("sys_open"),
946 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
947 bt_ctf_iter_add_callback(iter
,
948 g_quark_from_static_string("sys_socket"),
949 NULL
, 0, handle_sys_socket
, NULL
, NULL
, NULL
);
950 bt_ctf_iter_add_callback(iter
,
951 g_quark_from_static_string("sys_close"),
952 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
953 bt_ctf_iter_add_callback(iter
,
954 g_quark_from_static_string("exit_syscall"),
955 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
957 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
961 /* at each event check if we need to refresh */
962 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
965 /* to handle the scheduling events */
966 bt_ctf_iter_add_callback(iter
,
967 g_quark_from_static_string("sched_switch"),
968 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
970 bt_ctf_iter_add_callback(iter
,
971 g_quark_from_static_string("sys_write"),
972 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
973 bt_ctf_iter_add_callback(iter
,
974 g_quark_from_static_string("sys_read"),
975 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
978 if (lttngtop
.kprobes_table
) {
979 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
980 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
981 bt_ctf_iter_add_callback(iter
,
982 g_quark_from_static_string(
984 NULL
, 0, handle_kprobes
,
995 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
997 } else if (pid
> 0) {
999 g_hash_table_insert(tid_filter_list
,
1008 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
1009 if (quit
|| reload_trace
)
1011 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1016 /* block until quit, we reached the end of the trace */
1017 sem_wait(&end_trace_sem
);
1020 bt_ctf_iter_destroy(iter
);
1024 * bt_context_add_traces_recursive: Open a trace recursively
1025 * (copied from BSD code in converter/babeltrace.c)
1027 * Find each trace present in the subdirectory starting from the given
1028 * path, and add them to the context. The packet_seek parameter can be
1029 * NULL: this specify to use the default format packet_seek.
1031 * Return: 0 on success, nonzero on failure.
1032 * Unable to open toplevel: failure.
1033 * Unable to open some subdirectory or file: warn and continue;
1035 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
1036 const char *format_str
,
1037 void (*packet_seek
)(struct bt_stream_pos
*pos
,
1038 size_t offset
, int whence
))
1043 char lpath
[PATH_MAX
];
1044 char * const paths
[2] = { lpath
, NULL
};
1047 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
1048 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
1049 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
1050 ret
= bt_context_add_trace(ctx
,
1051 path
, format_str
, packet_seek
, NULL
, NULL
);
1053 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
1054 "for reading.\n", path
);
1055 /* Allow to skip erroneous traces. */
1056 ret
= 1; /* partial error */
1061 * Need to copy path, because fts_open can change it.
1062 * It is the pointer array, not the strings, that are constant.
1064 strncpy(lpath
, path
, PATH_MAX
);
1065 lpath
[PATH_MAX
- 1] = '\0';
1067 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
1069 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
1074 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
1076 while ((node
= fts_read(tree
))) {
1079 if (!(node
->fts_info
& FTS_D
))
1082 dirfd
= open(node
->fts_accpath
, 0);
1084 fprintf(stderr
, "[error] [Context] Unable to open trace "
1085 "directory file descriptor.\n");
1089 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1096 ret
= close(metafd
);
1107 trace_id
= bt_context_add_trace(ctx
,
1108 node
->fts_accpath
, format_str
,
1109 packet_seek
, NULL
, NULL
);
1111 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1112 "for reading.\n", node
->fts_accpath
, path
);
1113 /* Allow to skip erroneous traces. */
1116 g_array_append_val(trace_ids
, trace_id
);
1120 g_array_free(trace_ids
, TRUE
);
1127 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1128 int field_cnt
, int *tid_check
, int *pid_check
,
1129 int *procname_check
, int *ppid_check
)
1132 struct perfcounter
*global
;
1135 for (j
= 0; j
< field_cnt
; j
++) {
1136 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1137 if (*tid_check
== 0) {
1138 if (strncmp(name
, "tid", 3) == 0)
1141 if (*pid_check
== 0) {
1142 if (strncmp(name
, "pid", 3) == 0)
1145 if (*ppid_check
== 0) {
1146 if (strncmp(name
, "ppid", 4) == 0)
1149 if (*procname_check
== 0) {
1150 if (strncmp(name
, "procname", 8) == 0)
1151 (*procname_check
)++;
1153 if (strncmp(name
, "perf_", 5) == 0) {
1154 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1156 global
= g_new0(struct perfcounter
, 1);
1157 /* by default, sort on the first perf context */
1158 if (g_hash_table_size(global_perf_liszt
) == 0)
1160 global
->visible
= 1;
1161 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1166 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1167 *procname_check
== 1)
1174 * check_requirements: check if the required context informations are available
1176 * If each mandatory context information is available for at least in one
1177 * event, return 0 otherwise return -1.
1179 int check_requirements(struct bt_context
*ctx
)
1181 unsigned int i
, evt_cnt
, field_cnt
;
1182 struct bt_ctf_event_decl
*const * evt_list
;
1183 const struct bt_ctf_field_decl
*const * field_list
;
1186 int procname_check
= 0;
1190 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1195 for (i
= 0; i
< evt_cnt
; i
++) {
1196 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1197 &field_list
, &field_cnt
);
1198 ret
= check_field_requirements(field_list
, field_cnt
,
1199 &tid_check
, &pid_check
, &procname_check
,
1202 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1203 &field_list
, &field_cnt
);
1204 ret
= check_field_requirements(field_list
, field_cnt
,
1205 &tid_check
, &pid_check
, &procname_check
,
1208 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1209 &field_list
, &field_cnt
);
1210 ret
= check_field_requirements(field_list
, field_cnt
,
1211 &tid_check
, &pid_check
, &procname_check
,
1215 if (tid_check
== 0) {
1217 fprintf(stderr
, "[error] missing tid context information\n");
1219 if (pid_check
== 0) {
1221 fprintf(stderr
, "[error] missing pid context information\n");
1223 if (ppid_check
== 0) {
1225 fprintf(stderr
, "[error] missing ppid context information\n");
1227 if (procname_check
== 0) {
1229 fprintf(stderr
, "[error] missing procname context information\n");
1239 static void handle_sigchild(int signal
)
1243 waitpid(opt_exec_pid
, &status
, 0);
1246 int main(int argc
, char **argv
, char **envp
)
1249 struct bt_context
*bt_ctx
= NULL
;
1250 char *live_session_name
= NULL
;
1253 ret
= parse_options(argc
, argv
);
1255 fprintf(stdout
, "Error parsing options.\n\n");
1258 } else if (ret
> 0) {
1262 if (opt_exec_name
) {
1263 opt_exec_env
= envp
;
1264 signal(SIGCHLD
, handle_sigchild
);
1267 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1269 ret
= create_live_local_session(&opt_relay_hostname
,
1270 &live_session_name
, 0);
1275 if (!opt_input_path
&& remote_live
) {
1277 bt_ctx
= bt_context_create();
1278 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1279 "lttng-live", NULL
);
1281 fprintf(stderr
, "[error] Opening the trace\n");
1285 bt_ctx
= bt_context_create();
1286 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1288 fprintf(stderr
, "[error] Opening the trace\n");
1292 ret
= check_requirements(bt_ctx
);
1293 if (ret
< 0 && !valid_trace
) {
1294 fprintf(stderr
, "[error] some mandatory contexts "
1295 "were missing, exiting.\n");
1299 if (!opt_textdump
) {
1300 #ifdef HAVE_LIBNCURSES
1301 pthread_create(&display_thread
, NULL
, ncurses_display
,
1303 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1306 printf("Ncurses support not compiled, please install "
1307 "the missing dependencies and recompile\n");
1316 pthread_join(display_thread
, NULL
);
1318 pthread_join(timer_thread
, NULL
);
1324 bt_context_put(bt_ctx
);
1326 if (live_session_name
) {
1327 ret
= destroy_live_local_session(live_session_name
);
1329 fprintf(stderr
, "Error destroying %s\n", live_session_name
);