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>
43 #ifdef LTTNGTOP_MMAP_LIVE
44 #include <lttng/lttngtop-helper.h>
45 #include <babeltrace/lttngtopmmappacketseek.h>
46 #include "mmap-live.h"
47 #endif /* LTTNGTOP_MMAP_LIVE */
49 #include "lttngtoptypes.h"
51 #include "iostreamtop.h"
53 #include "network-live.h"
54 #include "lttng-session.h"
56 #ifdef HAVE_LIBNCURSES
57 #include "cursesdisplay.h"
60 #define NET_URL_PREFIX "net://"
61 #define NET4_URL_PREFIX "net4://"
62 #define NET6_URL_PREFIX "net6://"
64 #define DEFAULT_FILE_ARRAY_SIZE 1
66 const char *opt_input_path
;
73 /* We need at least one valid trace to start processing. */
76 struct lttngtop
*copy
;
77 pthread_t display_thread
;
78 pthread_t timer_thread
;
80 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
81 unsigned long last_display_update
= 0;
82 unsigned long last_event_ts
= 0;
83 struct syscall
*last_syscall
;
85 /* list of FDs available for being read with snapshots */
86 struct bt_mmap_stream_list mmap_list
;
87 GPtrArray
*lttng_consumer_stream_array
;
88 int sessiond_metadata
, consumerd_metadata
;
89 struct lttng_consumer_local_data
*ctx
= NULL
;
90 /* list of snapshots currently not consumed */
91 GPtrArray
*available_snapshots
;
92 sem_t metadata_available
;
111 OPT_CREATE_LOCAL_SESSION
,
112 OPT_CREATE_LIVE_SESSION
,
115 static struct poptOption long_options
[] = {
116 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
117 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
118 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
119 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
120 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
121 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
122 { "procname", 'n', POPT_ARG_STRING
, &opt_procname
, OPT_PROCNAME
, NULL
, NULL
},
123 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
124 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
125 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
126 { "all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
127 { "output", 'o', POPT_ARG_STRING
, &opt_output
, OPT_OUTPUT_FILE
, NULL
, NULL
},
128 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
129 { "gui-test", 'g', POPT_ARG_NONE
, NULL
, OPT_GUI_TEST
, NULL
, NULL
},
130 { "create-local-session", 0, POPT_ARG_NONE
, NULL
, OPT_CREATE_LOCAL_SESSION
, NULL
, NULL
},
131 { "create-live-session", 0, POPT_ARG_NONE
, NULL
, OPT_CREATE_LIVE_SESSION
, NULL
, NULL
},
132 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
135 #ifdef LTTNGTOP_MMAP_LIVE
136 static void handle_textdump_sigterm(int signal
)
139 lttng_destroy_session("test");
143 void *refresh_thread(void *p
)
147 sem_post(&pause_sem
);
149 sem_post(&end_trace_sem
);
150 sem_post(&goodtodisplay
);
151 sem_post(&goodtoupdate
);
154 if (!opt_input_path
) {
155 #ifdef LTTNGTOP_MMAP_LIVE
156 mmap_live_flush(mmap_list
);
159 sem_wait(&pause_sem
);
160 sem_post(&pause_sem
);
162 sleep(refresh_display
/NSEC_PER_SEC
);
166 #ifdef HAVE_LIBNCURSES
167 void *ncurses_display(void *p
)
169 unsigned int current_display_index
= 0;
171 sem_wait(&bootstrap
);
173 * Prevent the 1 second delay when we hit ESC
180 sem_wait(&goodtodisplay
);
181 sem_wait(&pause_sem
);
184 sem_post(&pause_sem
);
190 copy
= g_ptr_array_index(copies
, current_display_index
);
192 display(current_display_index
++);
194 sem_post(&goodtoupdate
);
195 sem_post(&pause_sem
);
198 #endif /* HAVE_LIBNCURSES */
200 void print_fields(struct bt_ctf_event
*event
, const char *procname
,
204 const struct bt_definition
*const * list
;
205 const struct bt_declaration
*l
;
206 const struct bt_definition
*scope
;
207 enum ctf_type_id type
;
209 struct processtop
*current_proc
;
210 struct files
*current_file
;
211 int fd
, fd_value
= -1;
213 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
215 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
216 for (i
= 0; i
< cnt
; i
++) {
218 fprintf(output
, ", ");
219 fprintf(output
, "%s = ", bt_ctf_field_name(list
[i
]));
220 l
= bt_ctf_get_decl_from_def(list
[i
]);
221 if (strncmp(bt_ctf_field_name(list
[i
]), "fd", 2) == 0)
225 type
= bt_ctf_field_type(l
);
226 if (type
== CTF_TYPE_INTEGER
) {
227 if (bt_ctf_get_int_signedness(l
) == 0) {
228 fd_value
= bt_ctf_get_uint64(list
[i
]);
229 fprintf(output
, "%" PRIu64
, bt_ctf_get_uint64(list
[i
]));
231 fd_value
= bt_ctf_get_int64(list
[i
]);
232 fprintf(output
, "%" PRId64
, bt_ctf_get_int64(list
[i
]));
234 } else if (type
== CTF_TYPE_STRING
) {
235 fprintf(output
, "%s", bt_ctf_get_string(list
[i
]));
236 } else if (type
== CTF_TYPE_ARRAY
) {
237 str
= bt_ctf_get_char_array(list
[i
]);
238 if (!bt_ctf_field_get_error() && str
)
239 fprintf(output
, "%s", str
);
242 current_proc
= find_process_tid(<tngtop
, pid
, procname
);
245 current_file
= get_file(current_proc
, fd_value
);
246 if (!current_file
|| !current_file
->name
)
248 fprintf(output
, "<%s>", current_file
->name
);
254 * hook on each event to check the timestamp and refresh the display if
257 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
259 unsigned long timestamp
;
262 uint64_t ts_nsec_start
;
263 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
264 const struct bt_definition
*scope
;
265 const char *hostname
, *procname
;
267 char *from_syscall
= NULL
;
269 timestamp
= bt_ctf_get_timestamp(call_data
);
271 /* can happen in network live when tracing is idle */
272 if (timestamp
< last_event_ts
)
275 last_event_ts
= timestamp
;
277 start
= format_timestamp(timestamp
);
278 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
280 pid
= get_context_pid(call_data
);
281 if (pid
== -1ULL && opt_tid
) {
285 tid
= get_context_tid(call_data
);
287 hostname
= get_context_hostname(call_data
);
292 if (opt_tid
|| opt_procname
|| opt_exec_name
) {
293 if (!lookup_filter_tid_list(lookup
)) {
294 /* To display when a process of ours in getting scheduled in */
295 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
298 scope
= bt_ctf_get_top_level_scope(call_data
,
300 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
301 scope
, "_next_tid"));
302 if (bt_ctf_field_get_error()) {
303 fprintf(stderr
, "Missing next_tid field\n");
306 if (!lookup_filter_tid_list(next_tid
)) {
311 fprintf(output
, "%c[1m", 27);
313 } else if (!opt_all
) {
318 fprintf(output
, "%c[1m", 27);
322 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
323 "exit_syscall", 12)) != 0) {
325 fprintf(output
, " ...interrupted...\n");
328 cpu_id
= get_cpu_id(call_data
);
329 procname
= get_context_comm(call_data
);
330 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
331 cpu
= get_cpu(cpu_id
);
332 cpu
->current_syscall
= g_new0(struct syscall
, 1);
333 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
334 cpu
->current_syscall
->ts_start
= timestamp
;
335 cpu
->current_syscall
->cpu_id
= cpu_id
;
336 last_syscall
= cpu
->current_syscall
;
338 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
341 /* Return code of a syscall if it was the last displayed event. */
342 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
343 if (last_syscall
->cpu_id
== cpu_id
) {
346 delta
= timestamp
- last_syscall
->ts_start
;
347 scope
= bt_ctf_get_top_level_scope(call_data
,
349 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
352 fprintf(output
, "= %" PRId64
" (%" PRIu64
".%09" PRIu64
"s)\n",
353 syscall_ret
, delta
/ NSEC_PER_SEC
,
354 delta
% NSEC_PER_SEC
);
359 fprintf(output
, " ...interrupted...\n");
363 cpu
= get_cpu(cpu_id
);
364 if (cpu
->current_syscall
) {
365 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
366 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
367 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
368 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
369 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
370 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
371 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
372 cpu_id
, cpu
->current_syscall
->name
);
376 free(cpu
->current_syscall
->name
);
377 g_free(cpu
->current_syscall
);
378 cpu
->current_syscall
= NULL
;
385 delta
= timestamp
- prev_ts
;
388 fprintf(output
, "%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
389 "(cpu %d) [%s (%d/%d)] %s (",
390 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
391 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
392 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
393 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
394 bt_ctf_event_name(call_data
));
395 print_fields(call_data
, procname
, pid
);
396 fprintf(output
, ")%s%c", (from_syscall
) ? from_syscall
: "",
397 (!current_syscall
) ? '\n' : ' ');
400 if (opt_all
&& (opt_tid
|| opt_procname
|| opt_exec_name
))
401 fprintf(output
, "%c[0m", 27);
406 return BT_CB_ERROR_STOP
;
408 return BT_CB_OK_STOP
;
411 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
414 struct kprobes
*kprobe
;
417 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
418 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
419 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
428 * hook on each event to check the timestamp and refresh the display if
431 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
433 unsigned long timestamp
;
435 timestamp
= bt_ctf_get_timestamp(call_data
);
436 if (timestamp
== -1ULL)
439 /* can happen in network live when tracing is idle */
440 if (timestamp
< last_event_ts
)
443 last_event_ts
= timestamp
;
445 if (last_display_update
== 0)
446 last_display_update
= timestamp
;
448 if (timestamp
- last_display_update
>= refresh_display
) {
449 sem_wait(&goodtoupdate
);
450 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
452 sem_post(&goodtodisplay
);
453 sem_post(&bootstrap
);
454 last_display_update
= timestamp
;
459 fprintf(stderr
, "check_timestamp callback error\n");
460 return BT_CB_ERROR_STOP
;
463 return BT_CB_OK_STOP
;
467 * get_perf_counter : get or create and return a perf_counter struct for
468 * either a process or a cpu (only one of the 2 parameters mandatory)
470 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
473 struct perfcounter
*ret
;
483 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
487 ret
= g_new0(struct perfcounter
, 1);
488 /* by default, make it visible in the UI */
490 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
499 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
500 const char *name
, int value
)
502 struct perfcounter
*cpu_perf
, *process_perf
;
504 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
505 if (cpu_perf
->count
< value
) {
506 process_perf
= get_perf_counter(name
, proc
, NULL
);
507 process_perf
->count
+= value
- cpu_perf
->count
;
508 cpu_perf
->count
= value
;
512 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
513 const struct bt_definition
*scope
,
514 struct processtop
*proc
,
517 struct bt_definition
const * const *list
= NULL
;
518 const struct bt_definition
*field
;
520 struct perfcounter
*perfcounter
;
528 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
535 g_hash_table_iter_init(&iter
, global_perf_liszt
);
536 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
537 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
539 int value
= bt_ctf_get_uint64(field
);
540 if (bt_ctf_field_get_error())
542 update_perf_value(proc
, cpu
, (char *) key
, value
);
550 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
553 const struct bt_definition
*scope
;
555 cpu
= get_cpu(get_cpu_id(event
));
557 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
558 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
560 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
561 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
563 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
564 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
567 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
570 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
571 char *comm
, *hostname
;
572 struct processtop
*parent
, *child
;
573 unsigned long timestamp
;
575 timestamp
= bt_ctf_get_timestamp(call_data
);
576 if (timestamp
== -1ULL)
579 pid
= get_context_pid(call_data
);
584 tid
= get_context_tid(call_data
);
588 ppid
= get_context_ppid(call_data
);
592 vpid
= get_context_vpid(call_data
);
596 vtid
= get_context_vtid(call_data
);
600 vppid
= get_context_vppid(call_data
);
604 comm
= get_context_comm(call_data
);
609 hostname
= get_context_hostname(call_data
);
611 /* find or create the current process */
612 child
= find_process_tid(<tngtop
, tid
, comm
);
614 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
617 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
619 if (opt_procname
&& lookup_procname(comm
) &&
620 !lookup_filter_tid_list(tid
)) {
623 tmp_tid
= malloc(sizeof(int));
625 printf("ADDING %s %d\n", comm
, tid
);
626 g_hash_table_insert(global_filter_list
,
627 (gpointer
) tmp_tid
, tmp_tid
);
631 /* find or create the parent */
632 parent
= find_process_tid(<tngtop
, pid
, comm
);
634 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
639 /* attach the parent to the current process */
640 child
->threadparent
= parent
;
641 add_thread(parent
, child
);
644 update_perf_counter(child
, call_data
);
650 return BT_CB_ERROR_STOP
;
655 copies
= g_ptr_array_new();
656 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
657 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
658 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
659 global_procname_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
660 tid_filter_list
= g_hash_table_new(g_str_hash
,
663 sem_init(&goodtodisplay
, 0, 0);
664 sem_init(&goodtoupdate
, 0, 1);
665 sem_init(&timer
, 0, 1);
666 sem_init(&bootstrap
, 0, 0);
667 sem_init(&pause_sem
, 0, 1);
668 sem_init(&end_trace_sem
, 0, 0);
670 reset_global_counters();
672 lttngtop
.nbthreads
= 0;
673 lttngtop
.nbfiles
= 0;
675 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
677 lttngtop
.process_table
= g_ptr_array_new();
678 lttngtop
.files_table
= g_ptr_array_new();
679 lttngtop
.cpu_table
= g_ptr_array_new();
686 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
687 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
688 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
689 fprintf(fp
, " -h, --help This help message\n");
690 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
691 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
692 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
693 fprintf(fp
, " -n, --procname Comma-separated list of procnames to display (require procname context in trace)\n");
694 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");
695 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
696 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
697 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
698 fprintf(fp
, " -o, --output <filename> In textdump, output the log in <filename>\n");
699 fprintf(fp
, " -g, --gui-test Test if the ncurses support is compiled in (return 0 if it is)\n");
700 fprintf(fp
, " --create-local-session Setup a LTTng local session with all the right parameters\n");
701 fprintf(fp
, " --create-live-session Setup a LTTng live session on localhost with all the right parameters\n");
705 * Parse probe options.
706 * Shamelessly stolen from lttng-tools :
707 * src/bin/lttng/commands/enable_events.c
709 static struct kprobes
*parse_probe_opts(char *opt
)
712 char name
[LTTNG_SYMBOL_NAME_LEN
];
713 struct kprobes
*kprobe
;
717 kprobe->probe_addr = 0;
718 kprobe->probe_offset = 0;
719 asprintf(&kprobe->probe_name, "probe_sys_open");
720 asprintf(&kprobe->symbol_name, "sys_open");
728 kprobe
= g_new0(struct kprobes
, 1);
730 /* Check for symbol+offset */
731 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
733 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
734 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
736 if (strlen(s_hex
) == 0) {
737 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
741 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
742 kprobe
->probe_addr
= 0;
746 /* Check for symbol */
747 if (isalpha(name
[0])) {
748 ret
= sscanf(opt
, "%s", name
);
750 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
751 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
752 kprobe
->probe_offset
= 0;
753 kprobe
->probe_addr
= 0;
758 /* Check for address */
759 ret
= sscanf(opt
, "%s", s_hex
);
761 if (strlen(s_hex
) == 0) {
762 fprintf(stderr
, "Invalid probe address %s", s_hex
);
766 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
767 kprobe
->probe_offset
= 0;
768 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
780 * Return 0 if caller should continue, < 0 if caller should return
781 * error, > 0 if caller should exit without reporting error.
783 static int parse_options(int argc
, char **argv
)
793 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
794 poptReadDefaultConfig(pc
, 0);
796 while ((opt
= poptGetNextOpt(pc
)) != -1) {
800 ret
= 1; /* exit cleanly */
803 #ifdef HAVE_LIBNCURSES
809 case OPT_CREATE_LOCAL_SESSION
:
810 ret
= create_local_session();
812 case OPT_CREATE_LIVE_SESSION
:
813 ret
= create_live_local_session(NULL
, NULL
, 1);
826 tmp_str
= strtok(opt_tid
, ",");
828 tid
= malloc(sizeof(int));
829 *tid
= atoi(tmp_str
);
830 g_hash_table_insert(tid_filter_list
,
831 (gpointer
) tid
, tid
);
832 tmp_str
= strtok(NULL
, ",");
836 /* start reading the live trace from the beginning */
841 tmp_str
= strtok(opt_procname
, ",");
843 add_procname_list(tmp_str
, 1);
844 tmp_str
= strtok(NULL
, ",");
847 case OPT_RELAY_HOSTNAME
:
851 lttngtop
.kprobes_table
= g_ptr_array_new();
852 tmp_str
= strtok(opt_kprobes
, ",");
854 struct kprobes
*kprobe
;
856 kprobe
= parse_probe_opts(tmp_str
);
859 lttngtop
.kprobes_table
,
865 tmp_str
= strtok(NULL
, ",");
868 case OPT_OUTPUT_FILE
:
871 babeltrace_verbose
= 1;
879 opt_exec_name
= NULL
;
880 opt_exec_argv
= NULL
;
881 for (i
= 0; i
< argc
; i
++) {
882 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
883 opt_exec_name
= argv
[i
+ 1];
884 opt_exec_argv
= &argv
[i
+ 1];
888 if (!opt_exec_name
) {
889 opt_input_path
= poptGetArg(pc
);
892 opt_output
= strdup("/dev/stdout");
894 output
= fopen(opt_output
, "w");
896 perror("Error opening output file");
908 void iter_trace(struct bt_context
*bt_ctx
)
910 struct bt_ctf_iter
*iter
;
911 struct bt_iter_pos begin_pos
;
912 struct kprobes
*kprobe
;
913 const struct bt_ctf_event
*event
;
917 begin_pos
.type
= BT_SEEK_BEGIN
;
918 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
920 /* at each event, verify the status of the process table */
921 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
924 /* to handle the follow child option */
925 bt_ctf_iter_add_callback(iter
,
926 g_quark_from_static_string("sched_process_fork"),
927 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
928 /* to clean up the process table */
929 bt_ctf_iter_add_callback(iter
,
930 g_quark_from_static_string("sched_process_free"),
931 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
932 /* to get all the process from the statedumps */
933 bt_ctf_iter_add_callback(iter
,
934 g_quark_from_static_string(
935 "lttng_statedump_process_state"),
936 NULL
, 0, handle_statedump_process_state
,
938 bt_ctf_iter_add_callback(iter
,
939 g_quark_from_static_string(
940 "lttng_statedump_file_descriptor"),
941 NULL
, 0, handle_statedump_file_descriptor
,
943 bt_ctf_iter_add_callback(iter
,
944 g_quark_from_static_string("sys_open"),
945 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
946 bt_ctf_iter_add_callback(iter
,
947 g_quark_from_static_string("sys_socket"),
948 NULL
, 0, handle_sys_socket
, NULL
, NULL
, NULL
);
949 bt_ctf_iter_add_callback(iter
,
950 g_quark_from_static_string("sys_close"),
951 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
952 bt_ctf_iter_add_callback(iter
,
953 g_quark_from_static_string("exit_syscall"),
954 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
956 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
960 /* at each event check if we need to refresh */
961 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
964 /* to handle the scheduling events */
965 bt_ctf_iter_add_callback(iter
,
966 g_quark_from_static_string("sched_switch"),
967 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
969 bt_ctf_iter_add_callback(iter
,
970 g_quark_from_static_string("sys_write"),
971 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
972 bt_ctf_iter_add_callback(iter
,
973 g_quark_from_static_string("sys_read"),
974 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
977 if (lttngtop
.kprobes_table
) {
978 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
979 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
980 bt_ctf_iter_add_callback(iter
,
981 g_quark_from_static_string(
983 NULL
, 0, handle_kprobes
,
994 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
996 } else if (pid
> 0) {
998 g_hash_table_insert(tid_filter_list
,
1007 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
1008 if (quit
|| reload_trace
)
1010 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1015 /* block until quit, we reached the end of the trace */
1016 sem_wait(&end_trace_sem
);
1019 bt_ctf_iter_destroy(iter
);
1023 * bt_context_add_traces_recursive: Open a trace recursively
1024 * (copied from BSD code in converter/babeltrace.c)
1026 * Find each trace present in the subdirectory starting from the given
1027 * path, and add them to the context. The packet_seek parameter can be
1028 * NULL: this specify to use the default format packet_seek.
1030 * Return: 0 on success, nonzero on failure.
1031 * Unable to open toplevel: failure.
1032 * Unable to open some subdirectory or file: warn and continue;
1034 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
1035 const char *format_str
,
1036 void (*packet_seek
)(struct bt_stream_pos
*pos
,
1037 size_t offset
, int whence
))
1042 char lpath
[PATH_MAX
];
1043 char * const paths
[2] = { lpath
, NULL
};
1046 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
1047 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
1048 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
1049 ret
= bt_context_add_trace(ctx
,
1050 path
, format_str
, packet_seek
, NULL
, NULL
);
1052 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
1053 "for reading.\n", path
);
1054 /* Allow to skip erroneous traces. */
1055 ret
= 1; /* partial error */
1060 * Need to copy path, because fts_open can change it.
1061 * It is the pointer array, not the strings, that are constant.
1063 strncpy(lpath
, path
, PATH_MAX
);
1064 lpath
[PATH_MAX
- 1] = '\0';
1066 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
1068 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
1073 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
1075 while ((node
= fts_read(tree
))) {
1078 if (!(node
->fts_info
& FTS_D
))
1081 dirfd
= open(node
->fts_accpath
, 0);
1083 fprintf(stderr
, "[error] [Context] Unable to open trace "
1084 "directory file descriptor.\n");
1088 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1095 ret
= close(metafd
);
1106 trace_id
= bt_context_add_trace(ctx
,
1107 node
->fts_accpath
, format_str
,
1108 packet_seek
, NULL
, NULL
);
1110 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1111 "for reading.\n", node
->fts_accpath
, path
);
1112 /* Allow to skip erroneous traces. */
1115 g_array_append_val(trace_ids
, trace_id
);
1119 g_array_free(trace_ids
, TRUE
);
1126 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1127 int field_cnt
, int *tid_check
, int *pid_check
,
1128 int *procname_check
, int *ppid_check
)
1131 struct perfcounter
*global
;
1134 for (j
= 0; j
< field_cnt
; j
++) {
1135 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1136 if (*tid_check
== 0) {
1137 if (strncmp(name
, "tid", 3) == 0)
1140 if (*pid_check
== 0) {
1141 if (strncmp(name
, "pid", 3) == 0)
1144 if (*ppid_check
== 0) {
1145 if (strncmp(name
, "ppid", 4) == 0)
1148 if (*procname_check
== 0) {
1149 if (strncmp(name
, "procname", 8) == 0)
1150 (*procname_check
)++;
1152 if (strncmp(name
, "perf_", 5) == 0) {
1153 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1155 global
= g_new0(struct perfcounter
, 1);
1156 /* by default, sort on the first perf context */
1157 if (g_hash_table_size(global_perf_liszt
) == 0)
1159 global
->visible
= 1;
1160 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1165 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1166 *procname_check
== 1)
1173 * check_requirements: check if the required context informations are available
1175 * If each mandatory context information is available for at least in one
1176 * event, return 0 otherwise return -1.
1178 int check_requirements(struct bt_context
*ctx
)
1180 unsigned int i
, evt_cnt
, field_cnt
;
1181 struct bt_ctf_event_decl
*const * evt_list
;
1182 const struct bt_ctf_field_decl
*const * field_list
;
1185 int procname_check
= 0;
1189 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1194 for (i
= 0; i
< evt_cnt
; i
++) {
1195 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1196 &field_list
, &field_cnt
);
1197 ret
= check_field_requirements(field_list
, field_cnt
,
1198 &tid_check
, &pid_check
, &procname_check
,
1201 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1202 &field_list
, &field_cnt
);
1203 ret
= check_field_requirements(field_list
, field_cnt
,
1204 &tid_check
, &pid_check
, &procname_check
,
1207 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1208 &field_list
, &field_cnt
);
1209 ret
= check_field_requirements(field_list
, field_cnt
,
1210 &tid_check
, &pid_check
, &procname_check
,
1214 if (tid_check
== 0) {
1216 fprintf(stderr
, "[error] missing tid context information\n");
1218 if (pid_check
== 0) {
1220 fprintf(stderr
, "[error] missing pid context information\n");
1222 if (ppid_check
== 0) {
1224 fprintf(stderr
, "[error] missing ppid context information\n");
1226 if (procname_check
== 0) {
1228 fprintf(stderr
, "[error] missing procname context information\n");
1238 static void handle_sigchild(int signal
)
1242 waitpid(opt_exec_pid
, &status
, 0);
1245 int main(int argc
, char **argv
, char **envp
)
1248 struct bt_context
*bt_ctx
= NULL
;
1249 char *live_session_name
= NULL
;
1252 ret
= parse_options(argc
, argv
);
1254 fprintf(stdout
, "Error parsing options.\n\n");
1257 } else if (ret
> 0) {
1261 if (opt_exec_name
) {
1262 opt_exec_env
= envp
;
1263 signal(SIGCHLD
, handle_sigchild
);
1266 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1268 ret
= create_live_local_session(&opt_relay_hostname
,
1269 &live_session_name
, 0);
1274 if (!opt_input_path
&& remote_live
) {
1276 bt_ctx
= bt_context_create();
1277 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1278 "lttng-live", NULL
);
1280 fprintf(stderr
, "[error] Opening the trace\n");
1284 bt_ctx
= bt_context_create();
1285 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1287 fprintf(stderr
, "[error] Opening the trace\n");
1291 ret
= check_requirements(bt_ctx
);
1292 if (ret
< 0 && !valid_trace
) {
1293 fprintf(stderr
, "[error] some mandatory contexts "
1294 "were missing, exiting.\n");
1298 if (!opt_textdump
) {
1299 #ifdef HAVE_LIBNCURSES
1300 pthread_create(&display_thread
, NULL
, ncurses_display
,
1302 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1305 printf("Ncurses support not compiled, please install "
1306 "the missing dependencies and recompile\n");
1315 pthread_join(display_thread
, NULL
);
1317 pthread_join(timer_thread
, NULL
);
1323 bt_context_put(bt_ctx
);
1325 if (live_session_name
) {
1326 ret
= destroy_live_local_session(live_session_name
);
1328 fprintf(stderr
, "Error destroying %s\n", live_session_name
);