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"
55 #ifdef HAVE_LIBNCURSES
56 #include "cursesdisplay.h"
59 #define NET_URL_PREFIX "net://"
60 #define NET4_URL_PREFIX "net4://"
61 #define NET6_URL_PREFIX "net6://"
63 #define DEFAULT_FILE_ARRAY_SIZE 1
65 const char *opt_input_path
;
72 /* We need at least one valid trace to start processing. */
75 struct lttngtop
*copy
;
76 pthread_t display_thread
;
77 pthread_t timer_thread
;
79 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
80 unsigned long last_display_update
= 0;
81 unsigned long last_event_ts
= 0;
82 struct syscall
*last_syscall
;
84 /* list of FDs available for being read with snapshots */
85 struct bt_mmap_stream_list mmap_list
;
86 GPtrArray
*lttng_consumer_stream_array
;
87 int sessiond_metadata
, consumerd_metadata
;
88 struct lttng_consumer_local_data
*ctx
= NULL
;
89 /* list of snapshots currently not consumed */
90 GPtrArray
*available_snapshots
;
91 sem_t metadata_available
;
112 static struct poptOption long_options
[] = {
113 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
114 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
115 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
116 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
117 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
118 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
119 { "procname", 'n', POPT_ARG_STRING
, &opt_procname
, OPT_PROCNAME
, NULL
, NULL
},
120 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
121 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
122 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
123 { "all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
124 { "output", 'o', POPT_ARG_STRING
, &opt_output
, OPT_OUTPUT_FILE
, NULL
, NULL
},
125 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
126 { "gui-test", 'g', POPT_ARG_NONE
, NULL
, OPT_GUI_TEST
, NULL
, NULL
},
127 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
130 #ifdef LTTNGTOP_MMAP_LIVE
131 static void handle_textdump_sigterm(int signal
)
134 lttng_destroy_session("test");
138 void *refresh_thread(void *p
)
142 sem_post(&pause_sem
);
144 sem_post(&end_trace_sem
);
145 sem_post(&goodtodisplay
);
146 sem_post(&goodtoupdate
);
149 if (!opt_input_path
) {
150 #ifdef LTTNGTOP_MMAP_LIVE
151 mmap_live_flush(mmap_list
);
154 sem_wait(&pause_sem
);
155 sem_post(&pause_sem
);
157 sleep(refresh_display
/NSEC_PER_SEC
);
161 #ifdef HAVE_LIBNCURSES
162 void *ncurses_display(void *p
)
164 unsigned int current_display_index
= 0;
166 sem_wait(&bootstrap
);
168 * Prevent the 1 second delay when we hit ESC
175 sem_wait(&goodtodisplay
);
176 sem_wait(&pause_sem
);
179 sem_post(&pause_sem
);
185 copy
= g_ptr_array_index(copies
, current_display_index
);
187 display(current_display_index
++);
189 sem_post(&goodtoupdate
);
190 sem_post(&pause_sem
);
193 #endif /* HAVE_LIBNCURSES */
195 void print_fields(struct bt_ctf_event
*event
, const char *procname
,
199 const struct bt_definition
*const * list
;
200 const struct bt_declaration
*l
;
201 const struct bt_definition
*scope
;
202 enum ctf_type_id type
;
204 struct processtop
*current_proc
;
205 struct files
*current_file
;
206 int fd
, fd_value
= -1;
208 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
210 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
211 for (i
= 0; i
< cnt
; i
++) {
213 fprintf(output
, ", ");
214 fprintf(output
, "%s = ", bt_ctf_field_name(list
[i
]));
215 l
= bt_ctf_get_decl_from_def(list
[i
]);
216 if (strncmp(bt_ctf_field_name(list
[i
]), "fd", 2) == 0)
220 type
= bt_ctf_field_type(l
);
221 if (type
== CTF_TYPE_INTEGER
) {
222 if (bt_ctf_get_int_signedness(l
) == 0) {
223 fd_value
= bt_ctf_get_uint64(list
[i
]);
224 fprintf(output
, "%" PRIu64
, bt_ctf_get_uint64(list
[i
]));
226 fd_value
= bt_ctf_get_int64(list
[i
]);
227 fprintf(output
, "%" PRId64
, bt_ctf_get_int64(list
[i
]));
229 } else if (type
== CTF_TYPE_STRING
) {
230 fprintf(output
, "%s", bt_ctf_get_string(list
[i
]));
231 } else if (type
== CTF_TYPE_ARRAY
) {
232 str
= bt_ctf_get_char_array(list
[i
]);
233 if (!bt_ctf_field_get_error() && str
)
234 fprintf(output
, "%s", str
);
237 current_proc
= find_process_tid(<tngtop
, pid
, procname
);
240 current_file
= get_file(current_proc
, fd_value
);
241 if (!current_file
|| !current_file
->name
)
243 fprintf(output
, "<%s>", current_file
->name
);
249 * hook on each event to check the timestamp and refresh the display if
252 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
254 unsigned long timestamp
;
257 uint64_t ts_nsec_start
;
258 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
259 const struct bt_definition
*scope
;
260 const char *hostname
, *procname
;
262 char *from_syscall
= NULL
;
264 timestamp
= bt_ctf_get_timestamp(call_data
);
266 /* can happen in network live when tracing is idle */
267 if (timestamp
< last_event_ts
)
270 last_event_ts
= timestamp
;
272 start
= format_timestamp(timestamp
);
273 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
275 pid
= get_context_pid(call_data
);
276 if (pid
== -1ULL && opt_tid
) {
280 tid
= get_context_tid(call_data
);
282 hostname
= get_context_hostname(call_data
);
287 if (opt_tid
|| opt_procname
|| opt_exec_name
) {
288 if (!lookup_filter_tid_list(lookup
)) {
289 /* To display when a process of ours in getting scheduled in */
290 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
293 scope
= bt_ctf_get_top_level_scope(call_data
,
295 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
296 scope
, "_next_tid"));
297 if (bt_ctf_field_get_error()) {
298 fprintf(stderr
, "Missing next_tid field\n");
301 if (!lookup_filter_tid_list(next_tid
)) {
306 fprintf(output
, "%c[1m", 27);
308 } else if (!opt_all
) {
313 fprintf(output
, "%c[1m", 27);
317 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
318 "exit_syscall", 12)) != 0) {
320 fprintf(output
, " ...interrupted...\n");
323 cpu_id
= get_cpu_id(call_data
);
324 procname
= get_context_comm(call_data
);
325 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
326 cpu
= get_cpu(cpu_id
);
327 cpu
->current_syscall
= g_new0(struct syscall
, 1);
328 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
329 cpu
->current_syscall
->ts_start
= timestamp
;
330 cpu
->current_syscall
->cpu_id
= cpu_id
;
331 last_syscall
= cpu
->current_syscall
;
333 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
336 /* Return code of a syscall if it was the last displayed event. */
337 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
338 if (last_syscall
->cpu_id
== cpu_id
) {
341 delta
= timestamp
- last_syscall
->ts_start
;
342 scope
= bt_ctf_get_top_level_scope(call_data
,
344 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
347 fprintf(output
, "= %" PRId64
" (%" PRIu64
".%09" PRIu64
"s)\n",
348 syscall_ret
, delta
/ NSEC_PER_SEC
,
349 delta
% NSEC_PER_SEC
);
354 fprintf(output
, " ...interrupted...\n");
358 cpu
= get_cpu(cpu_id
);
359 if (cpu
->current_syscall
) {
360 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
361 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
362 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
363 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
364 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
365 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
366 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
367 cpu_id
, cpu
->current_syscall
->name
);
371 free(cpu
->current_syscall
->name
);
372 g_free(cpu
->current_syscall
);
373 cpu
->current_syscall
= NULL
;
380 delta
= timestamp
- prev_ts
;
383 fprintf(output
, "%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
384 "(cpu %d) [%s (%d/%d)] %s (",
385 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
386 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
387 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
388 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
389 bt_ctf_event_name(call_data
));
390 print_fields(call_data
, procname
, pid
);
391 fprintf(output
, ")%s%c", (from_syscall
) ? from_syscall
: "",
392 (!current_syscall
) ? '\n' : ' ');
395 if (opt_all
&& (opt_tid
|| opt_procname
|| opt_exec_name
))
396 fprintf(output
, "%c[0m", 27);
401 return BT_CB_ERROR_STOP
;
403 return BT_CB_OK_STOP
;
406 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
409 struct kprobes
*kprobe
;
412 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
413 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
414 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
423 * hook on each event to check the timestamp and refresh the display if
426 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
428 unsigned long timestamp
;
430 timestamp
= bt_ctf_get_timestamp(call_data
);
431 if (timestamp
== -1ULL)
434 /* can happen in network live when tracing is idle */
435 if (timestamp
< last_event_ts
)
438 last_event_ts
= timestamp
;
440 if (last_display_update
== 0)
441 last_display_update
= timestamp
;
443 if (timestamp
- last_display_update
>= refresh_display
) {
444 sem_wait(&goodtoupdate
);
445 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
447 sem_post(&goodtodisplay
);
448 sem_post(&bootstrap
);
449 last_display_update
= timestamp
;
454 fprintf(stderr
, "check_timestamp callback error\n");
455 return BT_CB_ERROR_STOP
;
458 return BT_CB_OK_STOP
;
462 * get_perf_counter : get or create and return a perf_counter struct for
463 * either a process or a cpu (only one of the 2 parameters mandatory)
465 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
468 struct perfcounter
*ret
;
478 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
482 ret
= g_new0(struct perfcounter
, 1);
483 /* by default, make it visible in the UI */
485 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
494 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
495 const char *name
, int value
)
497 struct perfcounter
*cpu_perf
, *process_perf
;
499 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
500 if (cpu_perf
->count
< value
) {
501 process_perf
= get_perf_counter(name
, proc
, NULL
);
502 process_perf
->count
+= value
- cpu_perf
->count
;
503 cpu_perf
->count
= value
;
507 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
508 const struct bt_definition
*scope
,
509 struct processtop
*proc
,
512 struct bt_definition
const * const *list
= NULL
;
513 const struct bt_definition
*field
;
515 struct perfcounter
*perfcounter
;
523 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
530 g_hash_table_iter_init(&iter
, global_perf_liszt
);
531 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
532 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
534 int value
= bt_ctf_get_uint64(field
);
535 if (bt_ctf_field_get_error())
537 update_perf_value(proc
, cpu
, (char *) key
, value
);
545 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
548 const struct bt_definition
*scope
;
550 cpu
= get_cpu(get_cpu_id(event
));
552 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
553 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
555 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
556 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
558 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
559 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
562 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
565 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
566 char *comm
, *hostname
;
567 struct processtop
*parent
, *child
;
568 unsigned long timestamp
;
570 timestamp
= bt_ctf_get_timestamp(call_data
);
571 if (timestamp
== -1ULL)
574 pid
= get_context_pid(call_data
);
579 tid
= get_context_tid(call_data
);
583 ppid
= get_context_ppid(call_data
);
587 vpid
= get_context_vpid(call_data
);
591 vtid
= get_context_vtid(call_data
);
595 vppid
= get_context_vppid(call_data
);
599 comm
= get_context_comm(call_data
);
604 hostname
= get_context_hostname(call_data
);
606 /* find or create the current process */
607 child
= find_process_tid(<tngtop
, tid
, comm
);
609 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
612 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
614 if (opt_procname
&& lookup_procname(comm
) &&
615 !lookup_filter_tid_list(tid
)) {
618 tmp_tid
= malloc(sizeof(int));
620 printf("ADDING %s %d\n", comm
, tid
);
621 g_hash_table_insert(global_filter_list
,
622 (gpointer
) tmp_tid
, tmp_tid
);
626 /* find or create the parent */
627 parent
= find_process_tid(<tngtop
, pid
, comm
);
629 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
634 /* attach the parent to the current process */
635 child
->threadparent
= parent
;
636 add_thread(parent
, child
);
639 update_perf_counter(child
, call_data
);
645 return BT_CB_ERROR_STOP
;
650 copies
= g_ptr_array_new();
651 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
652 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
653 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
654 global_procname_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
655 tid_filter_list
= g_hash_table_new(g_str_hash
,
658 sem_init(&goodtodisplay
, 0, 0);
659 sem_init(&goodtoupdate
, 0, 1);
660 sem_init(&timer
, 0, 1);
661 sem_init(&bootstrap
, 0, 0);
662 sem_init(&pause_sem
, 0, 1);
663 sem_init(&end_trace_sem
, 0, 0);
665 reset_global_counters();
667 lttngtop
.nbthreads
= 0;
668 lttngtop
.nbfiles
= 0;
670 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
672 lttngtop
.process_table
= g_ptr_array_new();
673 lttngtop
.files_table
= g_ptr_array_new();
674 lttngtop
.cpu_table
= g_ptr_array_new();
681 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
682 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
683 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
684 fprintf(fp
, " -h, --help This help message\n");
685 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
686 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
687 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
688 fprintf(fp
, " -n, --procname Comma-separated list of procnames to display (require procname context in trace)\n");
689 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");
690 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
691 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
692 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
693 fprintf(fp
, " -o, --output <filename> In textdump, output the log in <filename>\n");
694 fprintf(fp
, " -g, --gui-test Test if the ncurses support is compiled in (return 0 if it is)\n");
698 * Parse probe options.
699 * Shamelessly stolen from lttng-tools :
700 * src/bin/lttng/commands/enable_events.c
702 static struct kprobes
*parse_probe_opts(char *opt
)
705 char name
[LTTNG_SYMBOL_NAME_LEN
];
706 struct kprobes
*kprobe
;
710 kprobe->probe_addr = 0;
711 kprobe->probe_offset = 0;
712 asprintf(&kprobe->probe_name, "probe_sys_open");
713 asprintf(&kprobe->symbol_name, "sys_open");
721 kprobe
= g_new0(struct kprobes
, 1);
723 /* Check for symbol+offset */
724 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
726 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
727 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
729 if (strlen(s_hex
) == 0) {
730 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
734 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
735 kprobe
->probe_addr
= 0;
739 /* Check for symbol */
740 if (isalpha(name
[0])) {
741 ret
= sscanf(opt
, "%s", name
);
743 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
744 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
745 kprobe
->probe_offset
= 0;
746 kprobe
->probe_addr
= 0;
751 /* Check for address */
752 ret
= sscanf(opt
, "%s", s_hex
);
754 if (strlen(s_hex
) == 0) {
755 fprintf(stderr
, "Invalid probe address %s", s_hex
);
759 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
760 kprobe
->probe_offset
= 0;
761 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
773 * Return 0 if caller should continue, < 0 if caller should return
774 * error, > 0 if caller should exit without reporting error.
776 static int parse_options(int argc
, char **argv
)
786 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
787 poptReadDefaultConfig(pc
, 0);
789 while ((opt
= poptGetNextOpt(pc
)) != -1) {
793 ret
= 1; /* exit cleanly */
796 #ifdef HAVE_LIBNCURSES
813 tmp_str
= strtok(opt_tid
, ",");
815 tid
= malloc(sizeof(int));
816 *tid
= atoi(tmp_str
);
817 g_hash_table_insert(tid_filter_list
,
818 (gpointer
) tid
, tid
);
819 tmp_str
= strtok(NULL
, ",");
823 /* start reading the live trace from the beginning */
828 tmp_str
= strtok(opt_procname
, ",");
830 add_procname_list(tmp_str
, 1);
831 tmp_str
= strtok(NULL
, ",");
834 case OPT_RELAY_HOSTNAME
:
838 lttngtop
.kprobes_table
= g_ptr_array_new();
839 tmp_str
= strtok(opt_kprobes
, ",");
841 struct kprobes
*kprobe
;
843 kprobe
= parse_probe_opts(tmp_str
);
846 lttngtop
.kprobes_table
,
852 tmp_str
= strtok(NULL
, ",");
855 case OPT_OUTPUT_FILE
:
858 babeltrace_verbose
= 1;
866 opt_exec_name
= NULL
;
867 opt_exec_argv
= NULL
;
868 for (i
= 0; i
< argc
; i
++) {
869 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
870 opt_exec_name
= argv
[i
+ 1];
871 opt_exec_argv
= &argv
[i
+ 1];
875 if (!opt_exec_name
) {
876 opt_input_path
= poptGetArg(pc
);
879 opt_output
= strdup("/dev/stdout");
881 output
= fopen(opt_output
, "w");
883 perror("Error opening output file");
895 void iter_trace(struct bt_context
*bt_ctx
)
897 struct bt_ctf_iter
*iter
;
898 struct bt_iter_pos begin_pos
;
899 struct kprobes
*kprobe
;
900 const struct bt_ctf_event
*event
;
904 begin_pos
.type
= BT_SEEK_BEGIN
;
905 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
907 /* at each event, verify the status of the process table */
908 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
911 /* to handle the follow child option */
912 bt_ctf_iter_add_callback(iter
,
913 g_quark_from_static_string("sched_process_fork"),
914 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
915 /* to clean up the process table */
916 bt_ctf_iter_add_callback(iter
,
917 g_quark_from_static_string("sched_process_free"),
918 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
919 /* to get all the process from the statedumps */
920 bt_ctf_iter_add_callback(iter
,
921 g_quark_from_static_string(
922 "lttng_statedump_process_state"),
923 NULL
, 0, handle_statedump_process_state
,
925 bt_ctf_iter_add_callback(iter
,
926 g_quark_from_static_string(
927 "lttng_statedump_file_descriptor"),
928 NULL
, 0, handle_statedump_file_descriptor
,
930 bt_ctf_iter_add_callback(iter
,
931 g_quark_from_static_string("sys_open"),
932 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
933 bt_ctf_iter_add_callback(iter
,
934 g_quark_from_static_string("sys_socket"),
935 NULL
, 0, handle_sys_socket
, NULL
, NULL
, NULL
);
936 bt_ctf_iter_add_callback(iter
,
937 g_quark_from_static_string("sys_close"),
938 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
939 bt_ctf_iter_add_callback(iter
,
940 g_quark_from_static_string("exit_syscall"),
941 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
943 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
947 /* at each event check if we need to refresh */
948 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
951 /* to handle the scheduling events */
952 bt_ctf_iter_add_callback(iter
,
953 g_quark_from_static_string("sched_switch"),
954 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
956 bt_ctf_iter_add_callback(iter
,
957 g_quark_from_static_string("sys_write"),
958 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
959 bt_ctf_iter_add_callback(iter
,
960 g_quark_from_static_string("sys_read"),
961 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
964 if (lttngtop
.kprobes_table
) {
965 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
966 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
967 bt_ctf_iter_add_callback(iter
,
968 g_quark_from_static_string(
970 NULL
, 0, handle_kprobes
,
981 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
983 } else if (pid
> 0) {
985 g_hash_table_insert(tid_filter_list
,
994 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
995 if (quit
|| reload_trace
)
997 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1002 /* block until quit, we reached the end of the trace */
1003 sem_wait(&end_trace_sem
);
1006 bt_ctf_iter_destroy(iter
);
1010 * bt_context_add_traces_recursive: Open a trace recursively
1011 * (copied from BSD code in converter/babeltrace.c)
1013 * Find each trace present in the subdirectory starting from the given
1014 * path, and add them to the context. The packet_seek parameter can be
1015 * NULL: this specify to use the default format packet_seek.
1017 * Return: 0 on success, nonzero on failure.
1018 * Unable to open toplevel: failure.
1019 * Unable to open some subdirectory or file: warn and continue;
1021 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
1022 const char *format_str
,
1023 void (*packet_seek
)(struct bt_stream_pos
*pos
,
1024 size_t offset
, int whence
))
1029 char lpath
[PATH_MAX
];
1030 char * const paths
[2] = { lpath
, NULL
};
1033 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
1034 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
1035 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
1036 ret
= bt_context_add_trace(ctx
,
1037 path
, format_str
, packet_seek
, NULL
, NULL
);
1039 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
1040 "for reading.\n", path
);
1041 /* Allow to skip erroneous traces. */
1042 ret
= 1; /* partial error */
1047 * Need to copy path, because fts_open can change it.
1048 * It is the pointer array, not the strings, that are constant.
1050 strncpy(lpath
, path
, PATH_MAX
);
1051 lpath
[PATH_MAX
- 1] = '\0';
1053 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
1055 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
1060 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
1062 while ((node
= fts_read(tree
))) {
1065 if (!(node
->fts_info
& FTS_D
))
1068 dirfd
= open(node
->fts_accpath
, 0);
1070 fprintf(stderr
, "[error] [Context] Unable to open trace "
1071 "directory file descriptor.\n");
1075 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1082 ret
= close(metafd
);
1093 trace_id
= bt_context_add_trace(ctx
,
1094 node
->fts_accpath
, format_str
,
1095 packet_seek
, NULL
, NULL
);
1097 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1098 "for reading.\n", node
->fts_accpath
, path
);
1099 /* Allow to skip erroneous traces. */
1102 g_array_append_val(trace_ids
, trace_id
);
1106 g_array_free(trace_ids
, TRUE
);
1113 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1114 int field_cnt
, int *tid_check
, int *pid_check
,
1115 int *procname_check
, int *ppid_check
)
1118 struct perfcounter
*global
;
1121 for (j
= 0; j
< field_cnt
; j
++) {
1122 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1123 if (*tid_check
== 0) {
1124 if (strncmp(name
, "tid", 3) == 0)
1127 if (*pid_check
== 0) {
1128 if (strncmp(name
, "pid", 3) == 0)
1131 if (*ppid_check
== 0) {
1132 if (strncmp(name
, "ppid", 4) == 0)
1135 if (*procname_check
== 0) {
1136 if (strncmp(name
, "procname", 8) == 0)
1137 (*procname_check
)++;
1139 if (strncmp(name
, "perf_", 5) == 0) {
1140 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1142 global
= g_new0(struct perfcounter
, 1);
1143 /* by default, sort on the first perf context */
1144 if (g_hash_table_size(global_perf_liszt
) == 0)
1146 global
->visible
= 1;
1147 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1152 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1153 *procname_check
== 1)
1160 * check_requirements: check if the required context informations are available
1162 * If each mandatory context information is available for at least in one
1163 * event, return 0 otherwise return -1.
1165 int check_requirements(struct bt_context
*ctx
)
1167 unsigned int i
, evt_cnt
, field_cnt
;
1168 struct bt_ctf_event_decl
*const * evt_list
;
1169 const struct bt_ctf_field_decl
*const * field_list
;
1172 int procname_check
= 0;
1176 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1181 for (i
= 0; i
< evt_cnt
; i
++) {
1182 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1183 &field_list
, &field_cnt
);
1184 ret
= check_field_requirements(field_list
, field_cnt
,
1185 &tid_check
, &pid_check
, &procname_check
,
1188 bt_ctf_get_decl_fields(evt_list
[i
], BT_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_STREAM_PACKET_CONTEXT
,
1195 &field_list
, &field_cnt
);
1196 ret
= check_field_requirements(field_list
, field_cnt
,
1197 &tid_check
, &pid_check
, &procname_check
,
1201 if (tid_check
== 0) {
1203 fprintf(stderr
, "[error] missing tid context information\n");
1205 if (pid_check
== 0) {
1207 fprintf(stderr
, "[error] missing pid context information\n");
1209 if (ppid_check
== 0) {
1211 fprintf(stderr
, "[error] missing ppid context information\n");
1213 if (procname_check
== 0) {
1215 fprintf(stderr
, "[error] missing procname context information\n");
1225 static void handle_sigchild(int signal
)
1229 waitpid(opt_exec_pid
, &status
, 0);
1232 int main(int argc
, char **argv
, char **envp
)
1235 struct bt_context
*bt_ctx
= NULL
;
1238 ret
= parse_options(argc
, argv
);
1240 fprintf(stdout
, "Error parsing options.\n\n");
1243 } else if (ret
> 0) {
1247 if (opt_exec_name
) {
1248 opt_exec_env
= envp
;
1249 signal(SIGCHLD
, handle_sigchild
);
1252 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1254 #ifdef LTTNGTOP_MMAP_LIVE
1256 signal(SIGTERM
, handle_textdump_sigterm
);
1257 signal(SIGINT
, handle_textdump_sigterm
);
1259 mmap_live_loop(bt_ctx
);
1260 pthread_join(timer_thread
, NULL
);
1262 pthread_join(display_thread
, NULL
);
1264 lttng_stop_tracing("test");
1265 lttng_destroy_session("test");
1269 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1270 "trace directory or -r <relayd hostname/IP>\n");
1274 #endif /* LTTNGTOP_MMAP_LIVE */
1275 } else 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");
1287 bt_ctx
= bt_context_create();
1288 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1290 fprintf(stderr
, "[error] Opening the trace\n");
1294 ret
= check_requirements(bt_ctx
);
1295 if (ret
< 0 && !valid_trace
) {
1296 fprintf(stderr
, "[error] some mandatory contexts "
1297 "were missing, exiting.\n");
1301 if (!opt_textdump
) {
1302 #ifdef HAVE_LIBNCURSES
1303 pthread_create(&display_thread
, NULL
, ncurses_display
,
1305 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1308 printf("Ncurses support not compiled, please install "
1309 "the missing dependencies and recompile\n");
1318 pthread_join(display_thread
, NULL
);
1320 pthread_join(timer_thread
, NULL
);
1326 bt_context_put(bt_ctx
);