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
,
114 static struct poptOption long_options
[] = {
115 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
116 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
117 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
118 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
119 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
120 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
121 { "procname", 'n', POPT_ARG_STRING
, &opt_procname
, OPT_PROCNAME
, NULL
, NULL
},
122 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
123 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
124 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
125 { "all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
126 { "output", 'o', POPT_ARG_STRING
, &opt_output
, OPT_OUTPUT_FILE
, NULL
, NULL
},
127 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
128 { "gui-test", 'g', POPT_ARG_NONE
, NULL
, OPT_GUI_TEST
, NULL
, NULL
},
129 { "create-local-session", 0, POPT_ARG_NONE
, NULL
, OPT_CREATE_LOCAL_SESSION
, NULL
, NULL
},
130 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
133 #ifdef LTTNGTOP_MMAP_LIVE
134 static void handle_textdump_sigterm(int signal
)
137 lttng_destroy_session("test");
141 void *refresh_thread(void *p
)
145 sem_post(&pause_sem
);
147 sem_post(&end_trace_sem
);
148 sem_post(&goodtodisplay
);
149 sem_post(&goodtoupdate
);
152 if (!opt_input_path
) {
153 #ifdef LTTNGTOP_MMAP_LIVE
154 mmap_live_flush(mmap_list
);
157 sem_wait(&pause_sem
);
158 sem_post(&pause_sem
);
160 sleep(refresh_display
/NSEC_PER_SEC
);
164 #ifdef HAVE_LIBNCURSES
165 void *ncurses_display(void *p
)
167 unsigned int current_display_index
= 0;
169 sem_wait(&bootstrap
);
171 * Prevent the 1 second delay when we hit ESC
178 sem_wait(&goodtodisplay
);
179 sem_wait(&pause_sem
);
182 sem_post(&pause_sem
);
188 copy
= g_ptr_array_index(copies
, current_display_index
);
190 display(current_display_index
++);
192 sem_post(&goodtoupdate
);
193 sem_post(&pause_sem
);
196 #endif /* HAVE_LIBNCURSES */
198 void print_fields(struct bt_ctf_event
*event
, const char *procname
,
202 const struct bt_definition
*const * list
;
203 const struct bt_declaration
*l
;
204 const struct bt_definition
*scope
;
205 enum ctf_type_id type
;
207 struct processtop
*current_proc
;
208 struct files
*current_file
;
209 int fd
, fd_value
= -1;
211 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
213 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
214 for (i
= 0; i
< cnt
; i
++) {
216 fprintf(output
, ", ");
217 fprintf(output
, "%s = ", bt_ctf_field_name(list
[i
]));
218 l
= bt_ctf_get_decl_from_def(list
[i
]);
219 if (strncmp(bt_ctf_field_name(list
[i
]), "fd", 2) == 0)
223 type
= bt_ctf_field_type(l
);
224 if (type
== CTF_TYPE_INTEGER
) {
225 if (bt_ctf_get_int_signedness(l
) == 0) {
226 fd_value
= bt_ctf_get_uint64(list
[i
]);
227 fprintf(output
, "%" PRIu64
, bt_ctf_get_uint64(list
[i
]));
229 fd_value
= bt_ctf_get_int64(list
[i
]);
230 fprintf(output
, "%" PRId64
, bt_ctf_get_int64(list
[i
]));
232 } else if (type
== CTF_TYPE_STRING
) {
233 fprintf(output
, "%s", bt_ctf_get_string(list
[i
]));
234 } else if (type
== CTF_TYPE_ARRAY
) {
235 str
= bt_ctf_get_char_array(list
[i
]);
236 if (!bt_ctf_field_get_error() && str
)
237 fprintf(output
, "%s", str
);
240 current_proc
= find_process_tid(<tngtop
, pid
, procname
);
243 current_file
= get_file(current_proc
, fd_value
);
244 if (!current_file
|| !current_file
->name
)
246 fprintf(output
, "<%s>", current_file
->name
);
252 * hook on each event to check the timestamp and refresh the display if
255 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
257 unsigned long timestamp
;
260 uint64_t ts_nsec_start
;
261 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
262 const struct bt_definition
*scope
;
263 const char *hostname
, *procname
;
265 char *from_syscall
= NULL
;
267 timestamp
= bt_ctf_get_timestamp(call_data
);
269 /* can happen in network live when tracing is idle */
270 if (timestamp
< last_event_ts
)
273 last_event_ts
= timestamp
;
275 start
= format_timestamp(timestamp
);
276 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
278 pid
= get_context_pid(call_data
);
279 if (pid
== -1ULL && opt_tid
) {
283 tid
= get_context_tid(call_data
);
285 hostname
= get_context_hostname(call_data
);
290 if (opt_tid
|| opt_procname
|| opt_exec_name
) {
291 if (!lookup_filter_tid_list(lookup
)) {
292 /* To display when a process of ours in getting scheduled in */
293 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
296 scope
= bt_ctf_get_top_level_scope(call_data
,
298 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
299 scope
, "_next_tid"));
300 if (bt_ctf_field_get_error()) {
301 fprintf(stderr
, "Missing next_tid field\n");
304 if (!lookup_filter_tid_list(next_tid
)) {
309 fprintf(output
, "%c[1m", 27);
311 } else if (!opt_all
) {
316 fprintf(output
, "%c[1m", 27);
320 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
321 "exit_syscall", 12)) != 0) {
323 fprintf(output
, " ...interrupted...\n");
326 cpu_id
= get_cpu_id(call_data
);
327 procname
= get_context_comm(call_data
);
328 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
329 cpu
= get_cpu(cpu_id
);
330 cpu
->current_syscall
= g_new0(struct syscall
, 1);
331 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
332 cpu
->current_syscall
->ts_start
= timestamp
;
333 cpu
->current_syscall
->cpu_id
= cpu_id
;
334 last_syscall
= cpu
->current_syscall
;
336 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
339 /* Return code of a syscall if it was the last displayed event. */
340 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
341 if (last_syscall
->cpu_id
== cpu_id
) {
344 delta
= timestamp
- last_syscall
->ts_start
;
345 scope
= bt_ctf_get_top_level_scope(call_data
,
347 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
350 fprintf(output
, "= %" PRId64
" (%" PRIu64
".%09" PRIu64
"s)\n",
351 syscall_ret
, delta
/ NSEC_PER_SEC
,
352 delta
% NSEC_PER_SEC
);
357 fprintf(output
, " ...interrupted...\n");
361 cpu
= get_cpu(cpu_id
);
362 if (cpu
->current_syscall
) {
363 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
364 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
365 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
366 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
367 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
368 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
369 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
370 cpu_id
, cpu
->current_syscall
->name
);
374 free(cpu
->current_syscall
->name
);
375 g_free(cpu
->current_syscall
);
376 cpu
->current_syscall
= NULL
;
383 delta
= timestamp
- prev_ts
;
386 fprintf(output
, "%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
387 "(cpu %d) [%s (%d/%d)] %s (",
388 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
389 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
390 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
391 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
392 bt_ctf_event_name(call_data
));
393 print_fields(call_data
, procname
, pid
);
394 fprintf(output
, ")%s%c", (from_syscall
) ? from_syscall
: "",
395 (!current_syscall
) ? '\n' : ' ');
398 if (opt_all
&& (opt_tid
|| opt_procname
|| opt_exec_name
))
399 fprintf(output
, "%c[0m", 27);
404 return BT_CB_ERROR_STOP
;
406 return BT_CB_OK_STOP
;
409 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
412 struct kprobes
*kprobe
;
415 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
416 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
417 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
426 * hook on each event to check the timestamp and refresh the display if
429 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
431 unsigned long timestamp
;
433 timestamp
= bt_ctf_get_timestamp(call_data
);
434 if (timestamp
== -1ULL)
437 /* can happen in network live when tracing is idle */
438 if (timestamp
< last_event_ts
)
441 last_event_ts
= timestamp
;
443 if (last_display_update
== 0)
444 last_display_update
= timestamp
;
446 if (timestamp
- last_display_update
>= refresh_display
) {
447 sem_wait(&goodtoupdate
);
448 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
450 sem_post(&goodtodisplay
);
451 sem_post(&bootstrap
);
452 last_display_update
= timestamp
;
457 fprintf(stderr
, "check_timestamp callback error\n");
458 return BT_CB_ERROR_STOP
;
461 return BT_CB_OK_STOP
;
465 * get_perf_counter : get or create and return a perf_counter struct for
466 * either a process or a cpu (only one of the 2 parameters mandatory)
468 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
471 struct perfcounter
*ret
;
481 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
485 ret
= g_new0(struct perfcounter
, 1);
486 /* by default, make it visible in the UI */
488 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
497 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
498 const char *name
, int value
)
500 struct perfcounter
*cpu_perf
, *process_perf
;
502 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
503 if (cpu_perf
->count
< value
) {
504 process_perf
= get_perf_counter(name
, proc
, NULL
);
505 process_perf
->count
+= value
- cpu_perf
->count
;
506 cpu_perf
->count
= value
;
510 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
511 const struct bt_definition
*scope
,
512 struct processtop
*proc
,
515 struct bt_definition
const * const *list
= NULL
;
516 const struct bt_definition
*field
;
518 struct perfcounter
*perfcounter
;
526 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
533 g_hash_table_iter_init(&iter
, global_perf_liszt
);
534 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
535 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
537 int value
= bt_ctf_get_uint64(field
);
538 if (bt_ctf_field_get_error())
540 update_perf_value(proc
, cpu
, (char *) key
, value
);
548 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
551 const struct bt_definition
*scope
;
553 cpu
= get_cpu(get_cpu_id(event
));
555 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
556 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
558 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
559 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
561 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
562 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
565 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
568 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
569 char *comm
, *hostname
;
570 struct processtop
*parent
, *child
;
571 unsigned long timestamp
;
573 timestamp
= bt_ctf_get_timestamp(call_data
);
574 if (timestamp
== -1ULL)
577 pid
= get_context_pid(call_data
);
582 tid
= get_context_tid(call_data
);
586 ppid
= get_context_ppid(call_data
);
590 vpid
= get_context_vpid(call_data
);
594 vtid
= get_context_vtid(call_data
);
598 vppid
= get_context_vppid(call_data
);
602 comm
= get_context_comm(call_data
);
607 hostname
= get_context_hostname(call_data
);
609 /* find or create the current process */
610 child
= find_process_tid(<tngtop
, tid
, comm
);
612 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
615 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
617 if (opt_procname
&& lookup_procname(comm
) &&
618 !lookup_filter_tid_list(tid
)) {
621 tmp_tid
= malloc(sizeof(int));
623 printf("ADDING %s %d\n", comm
, tid
);
624 g_hash_table_insert(global_filter_list
,
625 (gpointer
) tmp_tid
, tmp_tid
);
629 /* find or create the parent */
630 parent
= find_process_tid(<tngtop
, pid
, comm
);
632 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
637 /* attach the parent to the current process */
638 child
->threadparent
= parent
;
639 add_thread(parent
, child
);
642 update_perf_counter(child
, call_data
);
648 return BT_CB_ERROR_STOP
;
653 copies
= g_ptr_array_new();
654 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
655 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
656 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
657 global_procname_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
658 tid_filter_list
= g_hash_table_new(g_str_hash
,
661 sem_init(&goodtodisplay
, 0, 0);
662 sem_init(&goodtoupdate
, 0, 1);
663 sem_init(&timer
, 0, 1);
664 sem_init(&bootstrap
, 0, 0);
665 sem_init(&pause_sem
, 0, 1);
666 sem_init(&end_trace_sem
, 0, 0);
668 reset_global_counters();
670 lttngtop
.nbthreads
= 0;
671 lttngtop
.nbfiles
= 0;
673 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
675 lttngtop
.process_table
= g_ptr_array_new();
676 lttngtop
.files_table
= g_ptr_array_new();
677 lttngtop
.cpu_table
= g_ptr_array_new();
684 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
685 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
686 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
687 fprintf(fp
, " -h, --help This help message\n");
688 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
689 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
690 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
691 fprintf(fp
, " -n, --procname Comma-separated list of procnames to display (require procname context in trace)\n");
692 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");
693 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
694 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
695 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
696 fprintf(fp
, " -o, --output <filename> In textdump, output the log in <filename>\n");
697 fprintf(fp
, " -g, --gui-test Test if the ncurses support is compiled in (return 0 if it is)\n");
701 * Parse probe options.
702 * Shamelessly stolen from lttng-tools :
703 * src/bin/lttng/commands/enable_events.c
705 static struct kprobes
*parse_probe_opts(char *opt
)
708 char name
[LTTNG_SYMBOL_NAME_LEN
];
709 struct kprobes
*kprobe
;
713 kprobe->probe_addr = 0;
714 kprobe->probe_offset = 0;
715 asprintf(&kprobe->probe_name, "probe_sys_open");
716 asprintf(&kprobe->symbol_name, "sys_open");
724 kprobe
= g_new0(struct kprobes
, 1);
726 /* Check for symbol+offset */
727 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
729 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
730 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
732 if (strlen(s_hex
) == 0) {
733 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
737 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
738 kprobe
->probe_addr
= 0;
742 /* Check for symbol */
743 if (isalpha(name
[0])) {
744 ret
= sscanf(opt
, "%s", name
);
746 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
747 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
748 kprobe
->probe_offset
= 0;
749 kprobe
->probe_addr
= 0;
754 /* Check for address */
755 ret
= sscanf(opt
, "%s", s_hex
);
757 if (strlen(s_hex
) == 0) {
758 fprintf(stderr
, "Invalid probe address %s", s_hex
);
762 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
763 kprobe
->probe_offset
= 0;
764 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
776 * Return 0 if caller should continue, < 0 if caller should return
777 * error, > 0 if caller should exit without reporting error.
779 static int parse_options(int argc
, char **argv
)
789 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
790 poptReadDefaultConfig(pc
, 0);
792 while ((opt
= poptGetNextOpt(pc
)) != -1) {
796 ret
= 1; /* exit cleanly */
799 #ifdef HAVE_LIBNCURSES
805 case OPT_CREATE_LOCAL_SESSION
:
806 ret
= create_local_session();
819 tmp_str
= strtok(opt_tid
, ",");
821 tid
= malloc(sizeof(int));
822 *tid
= atoi(tmp_str
);
823 g_hash_table_insert(tid_filter_list
,
824 (gpointer
) tid
, tid
);
825 tmp_str
= strtok(NULL
, ",");
829 /* start reading the live trace from the beginning */
834 tmp_str
= strtok(opt_procname
, ",");
836 add_procname_list(tmp_str
, 1);
837 tmp_str
= strtok(NULL
, ",");
840 case OPT_RELAY_HOSTNAME
:
844 lttngtop
.kprobes_table
= g_ptr_array_new();
845 tmp_str
= strtok(opt_kprobes
, ",");
847 struct kprobes
*kprobe
;
849 kprobe
= parse_probe_opts(tmp_str
);
852 lttngtop
.kprobes_table
,
858 tmp_str
= strtok(NULL
, ",");
861 case OPT_OUTPUT_FILE
:
864 babeltrace_verbose
= 1;
872 opt_exec_name
= NULL
;
873 opt_exec_argv
= NULL
;
874 for (i
= 0; i
< argc
; i
++) {
875 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
876 opt_exec_name
= argv
[i
+ 1];
877 opt_exec_argv
= &argv
[i
+ 1];
881 if (!opt_exec_name
) {
882 opt_input_path
= poptGetArg(pc
);
885 opt_output
= strdup("/dev/stdout");
887 output
= fopen(opt_output
, "w");
889 perror("Error opening output file");
901 void iter_trace(struct bt_context
*bt_ctx
)
903 struct bt_ctf_iter
*iter
;
904 struct bt_iter_pos begin_pos
;
905 struct kprobes
*kprobe
;
906 const struct bt_ctf_event
*event
;
910 begin_pos
.type
= BT_SEEK_BEGIN
;
911 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
913 /* at each event, verify the status of the process table */
914 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
917 /* to handle the follow child option */
918 bt_ctf_iter_add_callback(iter
,
919 g_quark_from_static_string("sched_process_fork"),
920 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
921 /* to clean up the process table */
922 bt_ctf_iter_add_callback(iter
,
923 g_quark_from_static_string("sched_process_free"),
924 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
925 /* to get all the process from the statedumps */
926 bt_ctf_iter_add_callback(iter
,
927 g_quark_from_static_string(
928 "lttng_statedump_process_state"),
929 NULL
, 0, handle_statedump_process_state
,
931 bt_ctf_iter_add_callback(iter
,
932 g_quark_from_static_string(
933 "lttng_statedump_file_descriptor"),
934 NULL
, 0, handle_statedump_file_descriptor
,
936 bt_ctf_iter_add_callback(iter
,
937 g_quark_from_static_string("sys_open"),
938 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
939 bt_ctf_iter_add_callback(iter
,
940 g_quark_from_static_string("sys_socket"),
941 NULL
, 0, handle_sys_socket
, NULL
, NULL
, NULL
);
942 bt_ctf_iter_add_callback(iter
,
943 g_quark_from_static_string("sys_close"),
944 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
945 bt_ctf_iter_add_callback(iter
,
946 g_quark_from_static_string("exit_syscall"),
947 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
949 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
953 /* at each event check if we need to refresh */
954 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
957 /* to handle the scheduling events */
958 bt_ctf_iter_add_callback(iter
,
959 g_quark_from_static_string("sched_switch"),
960 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
962 bt_ctf_iter_add_callback(iter
,
963 g_quark_from_static_string("sys_write"),
964 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
965 bt_ctf_iter_add_callback(iter
,
966 g_quark_from_static_string("sys_read"),
967 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
970 if (lttngtop
.kprobes_table
) {
971 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
972 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
973 bt_ctf_iter_add_callback(iter
,
974 g_quark_from_static_string(
976 NULL
, 0, handle_kprobes
,
987 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
989 } else if (pid
> 0) {
991 g_hash_table_insert(tid_filter_list
,
1000 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
1001 if (quit
|| reload_trace
)
1003 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1008 /* block until quit, we reached the end of the trace */
1009 sem_wait(&end_trace_sem
);
1012 bt_ctf_iter_destroy(iter
);
1016 * bt_context_add_traces_recursive: Open a trace recursively
1017 * (copied from BSD code in converter/babeltrace.c)
1019 * Find each trace present in the subdirectory starting from the given
1020 * path, and add them to the context. The packet_seek parameter can be
1021 * NULL: this specify to use the default format packet_seek.
1023 * Return: 0 on success, nonzero on failure.
1024 * Unable to open toplevel: failure.
1025 * Unable to open some subdirectory or file: warn and continue;
1027 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
1028 const char *format_str
,
1029 void (*packet_seek
)(struct bt_stream_pos
*pos
,
1030 size_t offset
, int whence
))
1035 char lpath
[PATH_MAX
];
1036 char * const paths
[2] = { lpath
, NULL
};
1039 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
1040 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
1041 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
1042 ret
= bt_context_add_trace(ctx
,
1043 path
, format_str
, packet_seek
, NULL
, NULL
);
1045 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
1046 "for reading.\n", path
);
1047 /* Allow to skip erroneous traces. */
1048 ret
= 1; /* partial error */
1053 * Need to copy path, because fts_open can change it.
1054 * It is the pointer array, not the strings, that are constant.
1056 strncpy(lpath
, path
, PATH_MAX
);
1057 lpath
[PATH_MAX
- 1] = '\0';
1059 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
1061 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
1066 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
1068 while ((node
= fts_read(tree
))) {
1071 if (!(node
->fts_info
& FTS_D
))
1074 dirfd
= open(node
->fts_accpath
, 0);
1076 fprintf(stderr
, "[error] [Context] Unable to open trace "
1077 "directory file descriptor.\n");
1081 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1088 ret
= close(metafd
);
1099 trace_id
= bt_context_add_trace(ctx
,
1100 node
->fts_accpath
, format_str
,
1101 packet_seek
, NULL
, NULL
);
1103 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1104 "for reading.\n", node
->fts_accpath
, path
);
1105 /* Allow to skip erroneous traces. */
1108 g_array_append_val(trace_ids
, trace_id
);
1112 g_array_free(trace_ids
, TRUE
);
1119 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1120 int field_cnt
, int *tid_check
, int *pid_check
,
1121 int *procname_check
, int *ppid_check
)
1124 struct perfcounter
*global
;
1127 for (j
= 0; j
< field_cnt
; j
++) {
1128 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1129 if (*tid_check
== 0) {
1130 if (strncmp(name
, "tid", 3) == 0)
1133 if (*pid_check
== 0) {
1134 if (strncmp(name
, "pid", 3) == 0)
1137 if (*ppid_check
== 0) {
1138 if (strncmp(name
, "ppid", 4) == 0)
1141 if (*procname_check
== 0) {
1142 if (strncmp(name
, "procname", 8) == 0)
1143 (*procname_check
)++;
1145 if (strncmp(name
, "perf_", 5) == 0) {
1146 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1148 global
= g_new0(struct perfcounter
, 1);
1149 /* by default, sort on the first perf context */
1150 if (g_hash_table_size(global_perf_liszt
) == 0)
1152 global
->visible
= 1;
1153 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1158 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1159 *procname_check
== 1)
1166 * check_requirements: check if the required context informations are available
1168 * If each mandatory context information is available for at least in one
1169 * event, return 0 otherwise return -1.
1171 int check_requirements(struct bt_context
*ctx
)
1173 unsigned int i
, evt_cnt
, field_cnt
;
1174 struct bt_ctf_event_decl
*const * evt_list
;
1175 const struct bt_ctf_field_decl
*const * field_list
;
1178 int procname_check
= 0;
1182 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1187 for (i
= 0; i
< evt_cnt
; i
++) {
1188 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1189 &field_list
, &field_cnt
);
1190 ret
= check_field_requirements(field_list
, field_cnt
,
1191 &tid_check
, &pid_check
, &procname_check
,
1194 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1195 &field_list
, &field_cnt
);
1196 ret
= check_field_requirements(field_list
, field_cnt
,
1197 &tid_check
, &pid_check
, &procname_check
,
1200 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1201 &field_list
, &field_cnt
);
1202 ret
= check_field_requirements(field_list
, field_cnt
,
1203 &tid_check
, &pid_check
, &procname_check
,
1207 if (tid_check
== 0) {
1209 fprintf(stderr
, "[error] missing tid context information\n");
1211 if (pid_check
== 0) {
1213 fprintf(stderr
, "[error] missing pid context information\n");
1215 if (ppid_check
== 0) {
1217 fprintf(stderr
, "[error] missing ppid context information\n");
1219 if (procname_check
== 0) {
1221 fprintf(stderr
, "[error] missing procname context information\n");
1231 static void handle_sigchild(int signal
)
1235 waitpid(opt_exec_pid
, &status
, 0);
1238 int main(int argc
, char **argv
, char **envp
)
1241 struct bt_context
*bt_ctx
= NULL
;
1244 ret
= parse_options(argc
, argv
);
1246 fprintf(stdout
, "Error parsing options.\n\n");
1249 } else if (ret
> 0) {
1253 if (opt_exec_name
) {
1254 opt_exec_env
= envp
;
1255 signal(SIGCHLD
, handle_sigchild
);
1258 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1260 #ifdef LTTNGTOP_MMAP_LIVE
1262 signal(SIGTERM
, handle_textdump_sigterm
);
1263 signal(SIGINT
, handle_textdump_sigterm
);
1265 mmap_live_loop(bt_ctx
);
1266 pthread_join(timer_thread
, NULL
);
1268 pthread_join(display_thread
, NULL
);
1270 lttng_stop_tracing("test");
1271 lttng_destroy_session("test");
1275 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1276 "trace directory or -r <relayd hostname/IP>\n");
1280 #endif /* LTTNGTOP_MMAP_LIVE */
1281 } else if (!opt_input_path
&& remote_live
) {
1283 bt_ctx
= bt_context_create();
1284 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1285 "lttng-live", NULL
);
1287 fprintf(stderr
, "[error] Opening the trace\n");
1293 bt_ctx
= bt_context_create();
1294 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1296 fprintf(stderr
, "[error] Opening the trace\n");
1300 ret
= check_requirements(bt_ctx
);
1301 if (ret
< 0 && !valid_trace
) {
1302 fprintf(stderr
, "[error] some mandatory contexts "
1303 "were missing, exiting.\n");
1307 if (!opt_textdump
) {
1308 #ifdef HAVE_LIBNCURSES
1309 pthread_create(&display_thread
, NULL
, ncurses_display
,
1311 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1314 printf("Ncurses support not compiled, please install "
1315 "the missing dependencies and recompile\n");
1324 pthread_join(display_thread
, NULL
);
1326 pthread_join(timer_thread
, NULL
);
1332 bt_context_put(bt_ctx
);