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"
52 #include "cursesdisplay.h"
54 #include "network-live.h"
56 #define NET_URL_PREFIX "net://"
57 #define NET4_URL_PREFIX "net4://"
58 #define NET6_URL_PREFIX "net6://"
60 #define DEFAULT_FILE_ARRAY_SIZE 1
62 const char *opt_input_path
;
69 /* We need at least one valid trace to start processing. */
72 struct lttngtop
*copy
;
73 pthread_t display_thread
;
74 pthread_t timer_thread
;
76 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
77 unsigned long last_display_update
= 0;
78 unsigned long last_event_ts
= 0;
79 struct syscall
*last_syscall
;
81 /* list of FDs available for being read with snapshots */
82 struct bt_mmap_stream_list mmap_list
;
83 GPtrArray
*lttng_consumer_stream_array
;
84 int sessiond_metadata
, consumerd_metadata
;
85 struct lttng_consumer_local_data
*ctx
= NULL
;
86 /* list of snapshots currently not consumed */
87 GPtrArray
*available_snapshots
;
88 sem_t metadata_available
;
108 static struct poptOption long_options
[] = {
109 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
110 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
111 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
112 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
113 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
114 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
115 { "hostname", 'n', POPT_ARG_STRING
, &opt_hostname
, OPT_HOSTNAME
, NULL
, NULL
},
116 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
117 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
118 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
119 { "all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
120 { "output", 'o', POPT_ARG_STRING
, &opt_output
, OPT_OUTPUT_FILE
, NULL
, NULL
},
121 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
122 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
125 #ifdef LTTNGTOP_MMAP_LIVE
126 static void handle_textdump_sigterm(int signal
)
129 lttng_destroy_session("test");
133 void *refresh_thread(void *p
)
137 sem_post(&pause_sem
);
139 sem_post(&end_trace_sem
);
140 sem_post(&goodtodisplay
);
141 sem_post(&goodtoupdate
);
144 if (!opt_input_path
) {
145 #ifdef LTTNGTOP_MMAP_LIVE
146 mmap_live_flush(mmap_list
);
149 sem_wait(&pause_sem
);
150 sem_post(&pause_sem
);
152 sleep(refresh_display
/NSEC_PER_SEC
);
156 void *ncurses_display(void *p
)
158 unsigned int current_display_index
= 0;
160 sem_wait(&bootstrap
);
162 * Prevent the 1 second delay when we hit ESC
169 sem_wait(&goodtodisplay
);
170 sem_wait(&pause_sem
);
173 sem_post(&pause_sem
);
179 copy
= g_ptr_array_index(copies
, current_display_index
);
181 display(current_display_index
++);
183 sem_post(&goodtoupdate
);
184 sem_post(&pause_sem
);
188 void print_fields(struct bt_ctf_event
*event
, const char *procname
,
192 const struct bt_definition
*const * list
;
193 const struct bt_declaration
*l
;
194 const struct bt_definition
*scope
;
195 enum ctf_type_id type
;
197 struct processtop
*current_proc
;
198 struct files
*current_file
;
199 int fd
, fd_value
= -1;
201 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
203 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
204 for (i
= 0; i
< cnt
; i
++) {
206 fprintf(output
, ", ");
207 fprintf(output
, "%s = ", bt_ctf_field_name(list
[i
]));
208 l
= bt_ctf_get_decl_from_def(list
[i
]);
209 if (strncmp(bt_ctf_field_name(list
[i
]), "fd", 2) == 0)
213 type
= bt_ctf_field_type(l
);
214 if (type
== CTF_TYPE_INTEGER
) {
215 if (bt_ctf_get_int_signedness(l
) == 0) {
216 fd_value
= bt_ctf_get_uint64(list
[i
]);
217 fprintf(output
, "%" PRIu64
, bt_ctf_get_uint64(list
[i
]));
219 fd_value
= bt_ctf_get_int64(list
[i
]);
220 fprintf(output
, "%" PRId64
, bt_ctf_get_int64(list
[i
]));
222 } else if (type
== CTF_TYPE_STRING
) {
223 fprintf(output
, "%s", bt_ctf_get_string(list
[i
]));
224 } else if (type
== CTF_TYPE_ARRAY
) {
225 str
= bt_ctf_get_char_array(list
[i
]);
226 if (!bt_ctf_field_get_error() && str
)
227 fprintf(output
, "%s", str
);
230 current_proc
= find_process_tid(<tngtop
, pid
, procname
);
233 current_file
= get_file(current_proc
, fd_value
);
234 if (!current_file
|| !current_file
->name
)
236 fprintf(output
, "<%s>", current_file
->name
);
242 * hook on each event to check the timestamp and refresh the display if
245 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
247 unsigned long timestamp
;
250 uint64_t ts_nsec_start
;
251 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
252 const struct bt_definition
*scope
;
253 const char *hostname
, *procname
;
255 char *from_syscall
= NULL
;
257 timestamp
= bt_ctf_get_timestamp(call_data
);
259 /* can happen in network live when tracing is idle */
260 if (timestamp
< last_event_ts
)
263 last_event_ts
= timestamp
;
265 start
= format_timestamp(timestamp
);
266 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
268 pid
= get_context_pid(call_data
);
269 if (pid
== -1ULL && opt_tid
) {
273 tid
= get_context_tid(call_data
);
275 hostname
= get_context_hostname(call_data
);
280 if (opt_tid
|| opt_hostname
|| opt_exec_name
) {
281 if (!lookup_filter_tid_list(lookup
)) {
282 /* To display when a process of ours in getting scheduled in */
283 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
286 scope
= bt_ctf_get_top_level_scope(call_data
,
288 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
289 scope
, "_next_tid"));
290 if (bt_ctf_field_get_error()) {
291 fprintf(stderr
, "Missing next_tid field\n");
294 if (!lookup_filter_tid_list(next_tid
)) {
299 fprintf(output
, "%c[1m", 27);
301 } else if (!opt_all
) {
306 fprintf(output
, "%c[1m", 27);
310 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
311 "exit_syscall", 12)) != 0) {
313 fprintf(output
, " ...interrupted...\n");
316 cpu_id
= get_cpu_id(call_data
);
317 procname
= get_context_comm(call_data
);
318 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
319 cpu
= get_cpu(cpu_id
);
320 cpu
->current_syscall
= g_new0(struct syscall
, 1);
321 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
322 cpu
->current_syscall
->ts_start
= timestamp
;
323 cpu
->current_syscall
->cpu_id
= cpu_id
;
324 last_syscall
= cpu
->current_syscall
;
326 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
329 /* Return code of a syscall if it was the last displayed event. */
330 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
331 if (last_syscall
->cpu_id
== cpu_id
) {
334 delta
= timestamp
- last_syscall
->ts_start
;
335 scope
= bt_ctf_get_top_level_scope(call_data
,
337 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
340 fprintf(output
, "= %" PRId64
" (%" PRIu64
".%09" PRIu64
"s)\n",
341 syscall_ret
, delta
/ NSEC_PER_SEC
,
342 delta
% NSEC_PER_SEC
);
347 fprintf(output
, " ...interrupted...\n");
351 cpu
= get_cpu(cpu_id
);
352 if (cpu
->current_syscall
) {
353 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
354 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
355 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
356 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
357 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
358 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
359 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
360 cpu_id
, cpu
->current_syscall
->name
);
364 free(cpu
->current_syscall
->name
);
365 g_free(cpu
->current_syscall
);
366 cpu
->current_syscall
= NULL
;
373 delta
= timestamp
- prev_ts
;
376 fprintf(output
, "%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
377 "(cpu %d) [%s (%d/%d)] %s (",
378 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
379 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
380 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
381 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
382 bt_ctf_event_name(call_data
));
383 print_fields(call_data
, procname
, pid
);
384 fprintf(output
, ")%s%c", (from_syscall
) ? from_syscall
: "",
385 (!current_syscall
) ? '\n' : ' ');
388 if (opt_all
&& (opt_tid
|| opt_hostname
|| opt_exec_name
))
389 fprintf(output
, "%c[0m", 27);
394 return BT_CB_ERROR_STOP
;
396 return BT_CB_OK_STOP
;
399 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
402 struct kprobes
*kprobe
;
405 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
406 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
407 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
416 * hook on each event to check the timestamp and refresh the display if
419 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
421 unsigned long timestamp
;
423 timestamp
= bt_ctf_get_timestamp(call_data
);
424 if (timestamp
== -1ULL)
427 /* can happen in network live when tracing is idle */
428 if (timestamp
< last_event_ts
)
431 last_event_ts
= timestamp
;
433 if (last_display_update
== 0)
434 last_display_update
= timestamp
;
436 if (timestamp
- last_display_update
>= refresh_display
) {
437 sem_wait(&goodtoupdate
);
438 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
440 sem_post(&goodtodisplay
);
441 sem_post(&bootstrap
);
442 last_display_update
= timestamp
;
447 fprintf(stderr
, "check_timestamp callback error\n");
448 return BT_CB_ERROR_STOP
;
451 return BT_CB_OK_STOP
;
455 * get_perf_counter : get or create and return a perf_counter struct for
456 * either a process or a cpu (only one of the 2 parameters mandatory)
458 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
461 struct perfcounter
*ret
;
471 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
475 ret
= g_new0(struct perfcounter
, 1);
476 /* by default, make it visible in the UI */
478 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
487 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
488 const char *name
, int value
)
490 struct perfcounter
*cpu_perf
, *process_perf
;
492 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
493 if (cpu_perf
->count
< value
) {
494 process_perf
= get_perf_counter(name
, proc
, NULL
);
495 process_perf
->count
+= value
- cpu_perf
->count
;
496 cpu_perf
->count
= value
;
500 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
501 const struct bt_definition
*scope
,
502 struct processtop
*proc
,
505 struct bt_definition
const * const *list
= NULL
;
506 const struct bt_definition
*field
;
508 struct perfcounter
*perfcounter
;
516 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
523 g_hash_table_iter_init(&iter
, global_perf_liszt
);
524 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
525 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
527 int value
= bt_ctf_get_uint64(field
);
528 if (bt_ctf_field_get_error())
530 update_perf_value(proc
, cpu
, (char *) key
, value
);
538 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
541 const struct bt_definition
*scope
;
543 cpu
= get_cpu(get_cpu_id(event
));
545 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
546 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
548 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
549 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
551 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
552 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
555 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
558 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
559 char *comm
, *hostname
;
560 struct processtop
*parent
, *child
;
561 unsigned long timestamp
;
563 timestamp
= bt_ctf_get_timestamp(call_data
);
564 if (timestamp
== -1ULL)
567 pid
= get_context_pid(call_data
);
572 tid
= get_context_tid(call_data
);
576 ppid
= get_context_ppid(call_data
);
580 vpid
= get_context_vpid(call_data
);
584 vtid
= get_context_vtid(call_data
);
588 vppid
= get_context_vppid(call_data
);
592 comm
= get_context_comm(call_data
);
597 hostname
= get_context_hostname(call_data
);
599 /* find or create the current process */
600 child
= find_process_tid(<tngtop
, tid
, comm
);
602 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
605 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
608 /* find or create the parent */
609 parent
= find_process_tid(<tngtop
, pid
, comm
);
611 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
616 /* attach the parent to the current process */
617 child
->threadparent
= parent
;
618 add_thread(parent
, child
);
621 update_perf_counter(child
, call_data
);
627 return BT_CB_ERROR_STOP
;
632 copies
= g_ptr_array_new();
633 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
634 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
635 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
636 tid_filter_list
= g_hash_table_new(g_str_hash
,
639 sem_init(&goodtodisplay
, 0, 0);
640 sem_init(&goodtoupdate
, 0, 1);
641 sem_init(&timer
, 0, 1);
642 sem_init(&bootstrap
, 0, 0);
643 sem_init(&pause_sem
, 0, 1);
644 sem_init(&end_trace_sem
, 0, 0);
646 reset_global_counters();
648 lttngtop
.nbthreads
= 0;
649 lttngtop
.nbfiles
= 0;
651 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
653 lttngtop
.process_table
= g_ptr_array_new();
654 lttngtop
.files_table
= g_ptr_array_new();
655 lttngtop
.cpu_table
= g_ptr_array_new();
662 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
663 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
664 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
665 fprintf(fp
, " -h, --help This help message\n");
666 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
667 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
668 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
669 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
670 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");
671 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
672 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
673 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
674 fprintf(fp
, " -o, --output <filename> In textdump, output the log in <filename>\n");
678 * Parse probe options.
679 * Shamelessly stolen from lttng-tools :
680 * src/bin/lttng/commands/enable_events.c
682 static struct kprobes
*parse_probe_opts(char *opt
)
685 char name
[LTTNG_SYMBOL_NAME_LEN
];
686 struct kprobes
*kprobe
;
690 kprobe->probe_addr = 0;
691 kprobe->probe_offset = 0;
692 asprintf(&kprobe->probe_name, "probe_sys_open");
693 asprintf(&kprobe->symbol_name, "sys_open");
701 kprobe
= g_new0(struct kprobes
, 1);
703 /* Check for symbol+offset */
704 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
706 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
707 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
709 if (strlen(s_hex
) == 0) {
710 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
714 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
715 kprobe
->probe_addr
= 0;
719 /* Check for symbol */
720 if (isalpha(name
[0])) {
721 ret
= sscanf(opt
, "%s", name
);
723 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
724 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
725 kprobe
->probe_offset
= 0;
726 kprobe
->probe_addr
= 0;
731 /* Check for address */
732 ret
= sscanf(opt
, "%s", s_hex
);
734 if (strlen(s_hex
) == 0) {
735 fprintf(stderr
, "Invalid probe address %s", s_hex
);
739 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
740 kprobe
->probe_offset
= 0;
741 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
753 * Return 0 if caller should continue, < 0 if caller should return
754 * error, > 0 if caller should exit without reporting error.
756 static int parse_options(int argc
, char **argv
)
766 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
767 poptReadDefaultConfig(pc
, 0);
769 while ((opt
= poptGetNextOpt(pc
)) != -1) {
773 ret
= 1; /* exit cleanly */
786 tmp_str
= strtok(opt_tid
, ",");
788 tid
= malloc(sizeof(int));
789 *tid
= atoi(tmp_str
);
790 g_hash_table_insert(tid_filter_list
,
791 (gpointer
) tid
, tid
);
792 tmp_str
= strtok(NULL
, ",");
796 /* start reading the live trace from the beginning */
801 tmp_str
= strtok(opt_hostname
, ",");
803 add_hostname_list(tmp_str
, 1);
804 tmp_str
= strtok(NULL
, ",");
807 case OPT_RELAY_HOSTNAME
:
811 lttngtop
.kprobes_table
= g_ptr_array_new();
812 tmp_str
= strtok(opt_kprobes
, ",");
814 struct kprobes
*kprobe
;
816 kprobe
= parse_probe_opts(tmp_str
);
819 lttngtop
.kprobes_table
,
825 tmp_str
= strtok(NULL
, ",");
828 case OPT_OUTPUT_FILE
:
831 babeltrace_verbose
= 1;
839 opt_exec_name
= NULL
;
840 opt_exec_argv
= NULL
;
841 for (i
= 0; i
< argc
; i
++) {
842 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
843 opt_exec_name
= argv
[i
+ 1];
844 opt_exec_argv
= &argv
[i
+ 1];
848 if (!opt_exec_name
) {
849 opt_input_path
= poptGetArg(pc
);
852 opt_output
= strdup("/dev/stdout");
854 output
= fopen(opt_output
, "w");
856 perror("Error opening output file");
868 void iter_trace(struct bt_context
*bt_ctx
)
870 struct bt_ctf_iter
*iter
;
871 struct bt_iter_pos begin_pos
;
872 struct kprobes
*kprobe
;
873 const struct bt_ctf_event
*event
;
877 begin_pos
.type
= BT_SEEK_BEGIN
;
878 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
880 /* at each event, verify the status of the process table */
881 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
884 /* to handle the follow child option */
885 bt_ctf_iter_add_callback(iter
,
886 g_quark_from_static_string("sched_process_fork"),
887 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
888 /* to clean up the process table */
889 bt_ctf_iter_add_callback(iter
,
890 g_quark_from_static_string("sched_process_free"),
891 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
892 /* to get all the process from the statedumps */
893 bt_ctf_iter_add_callback(iter
,
894 g_quark_from_static_string(
895 "lttng_statedump_process_state"),
896 NULL
, 0, handle_statedump_process_state
,
898 bt_ctf_iter_add_callback(iter
,
899 g_quark_from_static_string(
900 "lttng_statedump_file_descriptor"),
901 NULL
, 0, handle_statedump_file_descriptor
,
903 bt_ctf_iter_add_callback(iter
,
904 g_quark_from_static_string("sys_open"),
905 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
906 bt_ctf_iter_add_callback(iter
,
907 g_quark_from_static_string("sys_close"),
908 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
909 bt_ctf_iter_add_callback(iter
,
910 g_quark_from_static_string("exit_syscall"),
911 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
913 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
917 /* at each event check if we need to refresh */
918 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
921 /* to handle the scheduling events */
922 bt_ctf_iter_add_callback(iter
,
923 g_quark_from_static_string("sched_switch"),
924 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
926 bt_ctf_iter_add_callback(iter
,
927 g_quark_from_static_string("sys_write"),
928 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
929 bt_ctf_iter_add_callback(iter
,
930 g_quark_from_static_string("sys_read"),
931 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
934 if (lttngtop
.kprobes_table
) {
935 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
936 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
937 bt_ctf_iter_add_callback(iter
,
938 g_quark_from_static_string(
940 NULL
, 0, handle_kprobes
,
951 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
953 } else if (pid
> 0) {
955 g_hash_table_insert(tid_filter_list
,
964 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
965 if (quit
|| reload_trace
)
967 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
972 /* block until quit, we reached the end of the trace */
973 sem_wait(&end_trace_sem
);
976 bt_ctf_iter_destroy(iter
);
980 * bt_context_add_traces_recursive: Open a trace recursively
981 * (copied from BSD code in converter/babeltrace.c)
983 * Find each trace present in the subdirectory starting from the given
984 * path, and add them to the context. The packet_seek parameter can be
985 * NULL: this specify to use the default format packet_seek.
987 * Return: 0 on success, nonzero on failure.
988 * Unable to open toplevel: failure.
989 * Unable to open some subdirectory or file: warn and continue;
991 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
992 const char *format_str
,
993 void (*packet_seek
)(struct bt_stream_pos
*pos
,
994 size_t offset
, int whence
))
999 char lpath
[PATH_MAX
];
1000 char * const paths
[2] = { lpath
, NULL
};
1003 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
1004 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
1005 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
1006 ret
= bt_context_add_trace(ctx
,
1007 path
, format_str
, packet_seek
, NULL
, NULL
);
1009 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
1010 "for reading.\n", path
);
1011 /* Allow to skip erroneous traces. */
1012 ret
= 1; /* partial error */
1017 * Need to copy path, because fts_open can change it.
1018 * It is the pointer array, not the strings, that are constant.
1020 strncpy(lpath
, path
, PATH_MAX
);
1021 lpath
[PATH_MAX
- 1] = '\0';
1023 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
1025 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
1030 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
1032 while ((node
= fts_read(tree
))) {
1035 if (!(node
->fts_info
& FTS_D
))
1038 dirfd
= open(node
->fts_accpath
, 0);
1040 fprintf(stderr
, "[error] [Context] Unable to open trace "
1041 "directory file descriptor.\n");
1045 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1052 ret
= close(metafd
);
1063 trace_id
= bt_context_add_trace(ctx
,
1064 node
->fts_accpath
, format_str
,
1065 packet_seek
, NULL
, NULL
);
1067 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1068 "for reading.\n", node
->fts_accpath
, path
);
1069 /* Allow to skip erroneous traces. */
1072 g_array_append_val(trace_ids
, trace_id
);
1076 g_array_free(trace_ids
, TRUE
);
1083 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1084 int field_cnt
, int *tid_check
, int *pid_check
,
1085 int *procname_check
, int *ppid_check
)
1088 struct perfcounter
*global
;
1091 for (j
= 0; j
< field_cnt
; j
++) {
1092 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1093 if (*tid_check
== 0) {
1094 if (strncmp(name
, "tid", 3) == 0)
1097 if (*pid_check
== 0) {
1098 if (strncmp(name
, "pid", 3) == 0)
1101 if (*ppid_check
== 0) {
1102 if (strncmp(name
, "ppid", 4) == 0)
1105 if (*procname_check
== 0) {
1106 if (strncmp(name
, "procname", 8) == 0)
1107 (*procname_check
)++;
1109 if (strncmp(name
, "perf_", 5) == 0) {
1110 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1112 global
= g_new0(struct perfcounter
, 1);
1113 /* by default, sort on the first perf context */
1114 if (g_hash_table_size(global_perf_liszt
) == 0)
1116 global
->visible
= 1;
1117 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1122 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1123 *procname_check
== 1)
1130 * check_requirements: check if the required context informations are available
1132 * If each mandatory context information is available for at least in one
1133 * event, return 0 otherwise return -1.
1135 int check_requirements(struct bt_context
*ctx
)
1137 unsigned int i
, evt_cnt
, field_cnt
;
1138 struct bt_ctf_event_decl
*const * evt_list
;
1139 const struct bt_ctf_field_decl
*const * field_list
;
1142 int procname_check
= 0;
1146 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1151 for (i
= 0; i
< evt_cnt
; i
++) {
1152 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1153 &field_list
, &field_cnt
);
1154 ret
= check_field_requirements(field_list
, field_cnt
,
1155 &tid_check
, &pid_check
, &procname_check
,
1158 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1159 &field_list
, &field_cnt
);
1160 ret
= check_field_requirements(field_list
, field_cnt
,
1161 &tid_check
, &pid_check
, &procname_check
,
1164 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1165 &field_list
, &field_cnt
);
1166 ret
= check_field_requirements(field_list
, field_cnt
,
1167 &tid_check
, &pid_check
, &procname_check
,
1171 if (tid_check
== 0) {
1173 fprintf(stderr
, "[error] missing tid context information\n");
1175 if (pid_check
== 0) {
1177 fprintf(stderr
, "[error] missing pid context information\n");
1179 if (ppid_check
== 0) {
1181 fprintf(stderr
, "[error] missing ppid context information\n");
1183 if (procname_check
== 0) {
1185 fprintf(stderr
, "[error] missing procname context information\n");
1195 static void handle_sigchild(int signal
)
1199 waitpid(opt_exec_pid
, &status
, 0);
1202 int main(int argc
, char **argv
, char **envp
)
1205 struct bt_context
*bt_ctx
= NULL
;
1208 ret
= parse_options(argc
, argv
);
1210 fprintf(stdout
, "Error parsing options.\n\n");
1213 } else if (ret
> 0) {
1217 if (opt_exec_name
) {
1218 opt_exec_env
= envp
;
1219 signal(SIGCHLD
, handle_sigchild
);
1222 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1224 #ifdef LTTNGTOP_MMAP_LIVE
1226 signal(SIGTERM
, handle_textdump_sigterm
);
1227 signal(SIGINT
, handle_textdump_sigterm
);
1229 mmap_live_loop(bt_ctx
);
1230 pthread_join(timer_thread
, NULL
);
1232 pthread_join(display_thread
, NULL
);
1234 lttng_stop_tracing("test");
1235 lttng_destroy_session("test");
1239 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1240 "trace directory or -r <relayd hostname/IP>\n");
1244 #endif /* LTTNGTOP_MMAP_LIVE */
1245 } else if (!opt_input_path
&& remote_live
) {
1247 bt_ctx
= bt_context_create();
1248 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1249 "lttng-live", NULL
);
1251 fprintf(stderr
, "[error] Opening the trace\n");
1257 bt_ctx
= bt_context_create();
1258 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1260 fprintf(stderr
, "[error] Opening the trace\n");
1264 ret
= check_requirements(bt_ctx
);
1265 if (ret
< 0 && !valid_trace
) {
1266 fprintf(stderr
, "[error] some mandatory contexts "
1267 "were missing, exiting.\n");
1271 if (!opt_textdump
) {
1272 pthread_create(&display_thread
, NULL
, ncurses_display
,
1274 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1282 pthread_join(display_thread
, NULL
);
1284 pthread_join(timer_thread
, NULL
);
1290 bt_context_put(bt_ctx
);