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
;
70 struct lttngtop
*copy
;
71 pthread_t display_thread
;
72 pthread_t timer_thread
;
74 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
75 unsigned long last_display_update
= 0;
76 unsigned long last_event_ts
= 0;
77 struct syscall
*last_syscall
;
79 /* list of FDs available for being read with snapshots */
80 struct bt_mmap_stream_list mmap_list
;
81 GPtrArray
*lttng_consumer_stream_array
;
82 int sessiond_metadata
, consumerd_metadata
;
83 struct lttng_consumer_local_data
*ctx
= NULL
;
84 /* list of snapshots currently not consumed */
85 GPtrArray
*available_snapshots
;
86 sem_t metadata_available
;
104 static struct poptOption long_options
[] = {
105 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
106 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
107 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
108 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
109 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
110 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
111 { "hostname", 'n', POPT_ARG_STRING
, &opt_hostname
, OPT_HOSTNAME
, NULL
, NULL
},
112 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
113 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
114 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
115 { "all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
116 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
119 #ifdef LTTNGTOP_MMAP_LIVE
120 static void handle_textdump_sigterm(int signal
)
123 lttng_destroy_session("test");
127 void *refresh_thread(void *p
)
131 sem_post(&pause_sem
);
133 sem_post(&end_trace_sem
);
134 sem_post(&goodtodisplay
);
135 sem_post(&goodtoupdate
);
138 if (!opt_input_path
) {
139 #ifdef LTTNGTOP_MMAP_LIVE
140 mmap_live_flush(mmap_list
);
143 sem_wait(&pause_sem
);
144 sem_post(&pause_sem
);
146 sleep(refresh_display
/NSEC_PER_SEC
);
150 void *ncurses_display(void *p
)
152 unsigned int current_display_index
= 0;
154 sem_wait(&bootstrap
);
156 * Prevent the 1 second delay when we hit ESC
163 sem_wait(&goodtodisplay
);
164 sem_wait(&pause_sem
);
167 sem_post(&pause_sem
);
173 copy
= g_ptr_array_index(copies
, current_display_index
);
175 display(current_display_index
++);
177 sem_post(&goodtoupdate
);
178 sem_post(&pause_sem
);
182 void print_fields(struct bt_ctf_event
*event
)
185 const struct bt_definition
*const * list
;
186 const struct bt_declaration
*l
;
187 const struct bt_definition
*scope
;
188 enum ctf_type_id type
;
191 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
193 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
194 for (i
= 0; i
< cnt
; i
++) {
197 printf("%s = ", bt_ctf_field_name(list
[i
]));
198 l
= bt_ctf_get_decl_from_def(list
[i
]);
199 type
= bt_ctf_field_type(l
);
200 if (type
== CTF_TYPE_INTEGER
) {
201 if (bt_ctf_get_int_signedness(l
) == 0)
202 printf("%" PRIu64
"", bt_ctf_get_uint64(list
[i
]));
204 printf("%" PRId64
"", bt_ctf_get_int64(list
[i
]));
205 } else if (type
== CTF_TYPE_STRING
) {
206 printf("%s", bt_ctf_get_string(list
[i
]));
207 } else if (type
== CTF_TYPE_ARRAY
) {
208 str
= bt_ctf_get_char_array(list
[i
]);
209 if (!bt_ctf_field_get_error() && str
)
216 * hook on each event to check the timestamp and refresh the display if
219 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
221 unsigned long timestamp
;
224 uint64_t ts_nsec_start
;
225 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
226 const struct bt_definition
*scope
;
227 const char *hostname
, *procname
;
229 char *from_syscall
= NULL
;
231 timestamp
= bt_ctf_get_timestamp(call_data
);
233 /* can happen in network live when tracing is idle */
234 if (timestamp
< last_event_ts
)
237 last_event_ts
= timestamp
;
239 start
= format_timestamp(timestamp
);
240 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
242 pid
= get_context_pid(call_data
);
243 if (pid
== -1ULL && opt_tid
) {
247 tid
= get_context_tid(call_data
);
249 hostname
= get_context_hostname(call_data
);
254 if (opt_tid
|| opt_hostname
|| opt_exec_name
) {
255 if (!lookup_filter_tid_list(lookup
)) {
256 /* To display when a process of ours in getting scheduled in */
257 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
260 scope
= bt_ctf_get_top_level_scope(call_data
,
262 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
263 scope
, "_next_tid"));
264 if (bt_ctf_field_get_error()) {
265 fprintf(stderr
, "Missing next_tid field\n");
268 if (!lookup_filter_tid_list(next_tid
)) {
275 } else if (!opt_all
) {
284 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
285 "exit_syscall", 12)) != 0) {
287 printf(" ...interrupted...\n");
290 cpu_id
= get_cpu_id(call_data
);
291 procname
= get_context_comm(call_data
);
292 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
293 cpu
= get_cpu(cpu_id
);
294 cpu
->current_syscall
= g_new0(struct syscall
, 1);
295 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
296 cpu
->current_syscall
->ts_start
= timestamp
;
297 cpu
->current_syscall
->cpu_id
= cpu_id
;
298 last_syscall
= cpu
->current_syscall
;
300 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
303 /* Return code of a syscall if it was the last displayed event. */
304 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
305 if (last_syscall
->cpu_id
== cpu_id
) {
308 delta
= timestamp
- last_syscall
->ts_start
;
309 scope
= bt_ctf_get_top_level_scope(call_data
,
311 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
314 printf(" = %" PRId64
" (+%" PRIu64
".%09" PRIu64
")\n",
315 syscall_ret
, delta
/ NSEC_PER_SEC
,
316 delta
% NSEC_PER_SEC
);
321 printf(" ...interrupted...\n");
325 cpu
= get_cpu(cpu_id
);
326 if (cpu
->current_syscall
) {
327 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
328 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
329 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
330 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
331 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
332 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
333 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
334 cpu_id
, cpu
->current_syscall
->name
);
338 free(cpu
->current_syscall
->name
);
339 g_free(cpu
->current_syscall
);
340 cpu
->current_syscall
= NULL
;
347 delta
= timestamp
- prev_ts
;
350 printf("%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
351 "(cpu %d) [%s (%d/%d)] %s (",
352 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
353 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
354 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
355 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
356 bt_ctf_event_name(call_data
));
357 print_fields(call_data
);
358 printf(")%s%c", (from_syscall
) ? from_syscall
: "",
359 (current_syscall
) ? '\0' : '\n');
367 return BT_CB_ERROR_STOP
;
369 return BT_CB_OK_STOP
;
372 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
375 struct kprobes
*kprobe
;
378 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
379 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
380 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
389 * hook on each event to check the timestamp and refresh the display if
392 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
394 unsigned long timestamp
;
396 timestamp
= bt_ctf_get_timestamp(call_data
);
397 if (timestamp
== -1ULL)
400 /* can happen in network live when tracing is idle */
401 if (timestamp
< last_event_ts
)
404 last_event_ts
= timestamp
;
406 if (last_display_update
== 0)
407 last_display_update
= timestamp
;
409 if (timestamp
- last_display_update
>= refresh_display
) {
410 sem_wait(&goodtoupdate
);
411 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
413 sem_post(&goodtodisplay
);
414 sem_post(&bootstrap
);
415 last_display_update
= timestamp
;
420 fprintf(stderr
, "check_timestamp callback error\n");
421 return BT_CB_ERROR_STOP
;
424 return BT_CB_OK_STOP
;
428 * get_perf_counter : get or create and return a perf_counter struct for
429 * either a process or a cpu (only one of the 2 parameters mandatory)
431 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
434 struct perfcounter
*ret
;
444 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
448 ret
= g_new0(struct perfcounter
, 1);
449 /* by default, make it visible in the UI */
451 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
460 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
461 const char *name
, int value
)
463 struct perfcounter
*cpu_perf
, *process_perf
;
465 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
466 if (cpu_perf
->count
< value
) {
467 process_perf
= get_perf_counter(name
, proc
, NULL
);
468 process_perf
->count
+= value
- cpu_perf
->count
;
469 cpu_perf
->count
= value
;
473 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
474 const struct bt_definition
*scope
,
475 struct processtop
*proc
,
478 struct bt_definition
const * const *list
= NULL
;
479 const struct bt_definition
*field
;
481 struct perfcounter
*perfcounter
;
489 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
496 g_hash_table_iter_init(&iter
, global_perf_liszt
);
497 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
498 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
500 int value
= bt_ctf_get_uint64(field
);
501 if (bt_ctf_field_get_error())
503 update_perf_value(proc
, cpu
, (char *) key
, value
);
511 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
514 const struct bt_definition
*scope
;
516 cpu
= get_cpu(get_cpu_id(event
));
518 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
519 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
521 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
522 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
524 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
525 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
528 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
531 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
532 char *comm
, *hostname
;
533 struct processtop
*parent
, *child
;
534 unsigned long timestamp
;
536 timestamp
= bt_ctf_get_timestamp(call_data
);
537 if (timestamp
== -1ULL)
540 pid
= get_context_pid(call_data
);
545 tid
= get_context_tid(call_data
);
549 ppid
= get_context_ppid(call_data
);
553 vpid
= get_context_vpid(call_data
);
557 vtid
= get_context_vtid(call_data
);
561 vppid
= get_context_vppid(call_data
);
565 comm
= get_context_comm(call_data
);
570 hostname
= get_context_hostname(call_data
);
572 /* find or create the current process */
573 child
= find_process_tid(<tngtop
, tid
, comm
);
575 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
578 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
581 /* find or create the parent */
582 parent
= find_process_tid(<tngtop
, pid
, comm
);
584 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
589 /* attach the parent to the current process */
590 child
->threadparent
= parent
;
591 add_thread(parent
, child
);
594 update_perf_counter(child
, call_data
);
600 return BT_CB_ERROR_STOP
;
605 copies
= g_ptr_array_new();
606 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
607 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
608 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
609 tid_filter_list
= g_hash_table_new(g_str_hash
,
612 sem_init(&goodtodisplay
, 0, 0);
613 sem_init(&goodtoupdate
, 0, 1);
614 sem_init(&timer
, 0, 1);
615 sem_init(&bootstrap
, 0, 0);
616 sem_init(&pause_sem
, 0, 1);
617 sem_init(&end_trace_sem
, 0, 0);
619 reset_global_counters();
621 lttngtop
.nbthreads
= 0;
622 lttngtop
.nbfiles
= 0;
624 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
626 lttngtop
.process_table
= g_ptr_array_new();
627 lttngtop
.files_table
= g_ptr_array_new();
628 lttngtop
.cpu_table
= g_ptr_array_new();
635 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
636 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
637 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
638 fprintf(fp
, " -h, --help This help message\n");
639 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
640 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
641 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
642 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
643 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");
644 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
645 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
646 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
650 * Parse probe options.
651 * Shamelessly stolen from lttng-tools :
652 * src/bin/lttng/commands/enable_events.c
654 static struct kprobes
*parse_probe_opts(char *opt
)
657 char name
[LTTNG_SYMBOL_NAME_LEN
];
658 struct kprobes
*kprobe
;
662 kprobe->probe_addr = 0;
663 kprobe->probe_offset = 0;
664 asprintf(&kprobe->probe_name, "probe_sys_open");
665 asprintf(&kprobe->symbol_name, "sys_open");
673 kprobe
= g_new0(struct kprobes
, 1);
675 /* Check for symbol+offset */
676 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
678 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
679 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
681 if (strlen(s_hex
) == 0) {
682 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
686 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
687 kprobe
->probe_addr
= 0;
691 /* Check for symbol */
692 if (isalpha(name
[0])) {
693 ret
= sscanf(opt
, "%s", name
);
695 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
696 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
697 kprobe
->probe_offset
= 0;
698 kprobe
->probe_addr
= 0;
703 /* Check for address */
704 ret
= sscanf(opt
, "%s", s_hex
);
706 if (strlen(s_hex
) == 0) {
707 fprintf(stderr
, "Invalid probe address %s", s_hex
);
711 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
712 kprobe
->probe_offset
= 0;
713 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
725 * Return 0 if caller should continue, < 0 if caller should return
726 * error, > 0 if caller should exit without reporting error.
728 static int parse_options(int argc
, char **argv
)
738 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
739 poptReadDefaultConfig(pc
, 0);
741 while ((opt
= poptGetNextOpt(pc
)) != -1) {
745 ret
= 1; /* exit cleanly */
758 tmp_str
= strtok(opt_tid
, ",");
760 tid
= malloc(sizeof(int));
761 *tid
= atoi(tmp_str
);
762 g_hash_table_insert(tid_filter_list
,
763 (gpointer
) tid
, tid
);
764 tmp_str
= strtok(NULL
, ",");
768 /* start reading the live trace from the beginning */
773 tmp_str
= strtok(opt_hostname
, ",");
775 add_hostname_list(tmp_str
, 1);
776 tmp_str
= strtok(NULL
, ",");
779 case OPT_RELAY_HOSTNAME
:
783 lttngtop
.kprobes_table
= g_ptr_array_new();
784 tmp_str
= strtok(opt_kprobes
, ",");
786 struct kprobes
*kprobe
;
788 kprobe
= parse_probe_opts(tmp_str
);
791 lttngtop
.kprobes_table
,
797 tmp_str
= strtok(NULL
, ",");
806 opt_exec_name
= NULL
;
807 opt_exec_argv
= NULL
;
808 for (i
= 0; i
< argc
; i
++) {
809 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
810 opt_exec_name
= argv
[i
+ 1];
811 opt_exec_argv
= &argv
[i
+ 1];
815 if (!opt_exec_name
) {
816 opt_input_path
= poptGetArg(pc
);
826 void iter_trace(struct bt_context
*bt_ctx
)
828 struct bt_ctf_iter
*iter
;
829 struct bt_iter_pos begin_pos
;
830 struct kprobes
*kprobe
;
831 const struct bt_ctf_event
*event
;
835 begin_pos
.type
= BT_SEEK_BEGIN
;
836 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
838 /* at each event, verify the status of the process table */
839 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
842 /* to handle the follow child option */
843 bt_ctf_iter_add_callback(iter
,
844 g_quark_from_static_string("sched_process_fork"),
845 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
846 /* to clean up the process table */
847 bt_ctf_iter_add_callback(iter
,
848 g_quark_from_static_string("sched_process_free"),
849 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
851 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
855 /* at each event check if we need to refresh */
856 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
859 /* to handle the scheduling events */
860 bt_ctf_iter_add_callback(iter
,
861 g_quark_from_static_string("sched_switch"),
862 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
863 /* to get all the process from the statedumps */
864 bt_ctf_iter_add_callback(iter
,
865 g_quark_from_static_string(
866 "lttng_statedump_process_state"),
867 NULL
, 0, handle_statedump_process_state
,
871 bt_ctf_iter_add_callback(iter
,
872 g_quark_from_static_string("exit_syscall"),
873 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
874 bt_ctf_iter_add_callback(iter
,
875 g_quark_from_static_string("sys_write"),
876 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
877 bt_ctf_iter_add_callback(iter
,
878 g_quark_from_static_string("sys_read"),
879 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
880 bt_ctf_iter_add_callback(iter
,
881 g_quark_from_static_string("sys_open"),
882 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
883 bt_ctf_iter_add_callback(iter
,
884 g_quark_from_static_string("sys_close"),
885 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
886 bt_ctf_iter_add_callback(iter
,
887 g_quark_from_static_string(
888 "lttng_statedump_file_descriptor"),
889 NULL
, 0, handle_statedump_file_descriptor
,
893 if (lttngtop
.kprobes_table
) {
894 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
895 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
896 bt_ctf_iter_add_callback(iter
,
897 g_quark_from_static_string(
899 NULL
, 0, handle_kprobes
,
910 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
912 } else if (pid
> 0) {
914 g_hash_table_insert(tid_filter_list
,
923 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
924 if (quit
|| reload_trace
)
926 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
931 /* block until quit, we reached the end of the trace */
932 sem_wait(&end_trace_sem
);
935 bt_ctf_iter_destroy(iter
);
939 * bt_context_add_traces_recursive: Open a trace recursively
940 * (copied from BSD code in converter/babeltrace.c)
942 * Find each trace present in the subdirectory starting from the given
943 * path, and add them to the context. The packet_seek parameter can be
944 * NULL: this specify to use the default format packet_seek.
946 * Return: 0 on success, nonzero on failure.
947 * Unable to open toplevel: failure.
948 * Unable to open some subdirectory or file: warn and continue;
950 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
951 const char *format_str
,
952 void (*packet_seek
)(struct bt_stream_pos
*pos
,
953 size_t offset
, int whence
))
958 char lpath
[PATH_MAX
];
959 char * const paths
[2] = { lpath
, NULL
};
962 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
963 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
964 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
965 ret
= bt_context_add_trace(ctx
,
966 path
, format_str
, packet_seek
, NULL
, NULL
);
968 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
969 "for reading.\n", path
);
970 /* Allow to skip erroneous traces. */
971 ret
= 1; /* partial error */
976 * Need to copy path, because fts_open can change it.
977 * It is the pointer array, not the strings, that are constant.
979 strncpy(lpath
, path
, PATH_MAX
);
980 lpath
[PATH_MAX
- 1] = '\0';
982 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
984 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
989 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
991 while ((node
= fts_read(tree
))) {
994 if (!(node
->fts_info
& FTS_D
))
997 dirfd
= open(node
->fts_accpath
, 0);
999 fprintf(stderr
, "[error] [Context] Unable to open trace "
1000 "directory file descriptor.\n");
1004 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1011 ret
= close(metafd
);
1022 trace_id
= bt_context_add_trace(ctx
,
1023 node
->fts_accpath
, format_str
,
1024 packet_seek
, NULL
, NULL
);
1026 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1027 "for reading.\n", node
->fts_accpath
, path
);
1028 /* Allow to skip erroneous traces. */
1031 g_array_append_val(trace_ids
, trace_id
);
1035 g_array_free(trace_ids
, TRUE
);
1042 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1043 int field_cnt
, int *tid_check
, int *pid_check
,
1044 int *procname_check
, int *ppid_check
)
1047 struct perfcounter
*global
;
1050 for (j
= 0; j
< field_cnt
; j
++) {
1051 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1052 if (*tid_check
== 0) {
1053 if (strncmp(name
, "tid", 3) == 0)
1056 if (*pid_check
== 0) {
1057 if (strncmp(name
, "pid", 3) == 0)
1060 if (*ppid_check
== 0) {
1061 if (strncmp(name
, "ppid", 4) == 0)
1064 if (*procname_check
== 0) {
1065 if (strncmp(name
, "procname", 8) == 0)
1066 (*procname_check
)++;
1068 if (strncmp(name
, "perf_", 5) == 0) {
1069 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1071 global
= g_new0(struct perfcounter
, 1);
1072 /* by default, sort on the first perf context */
1073 if (g_hash_table_size(global_perf_liszt
) == 0)
1075 global
->visible
= 1;
1076 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1081 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1082 *procname_check
== 1)
1089 * check_requirements: check if the required context informations are available
1091 * If each mandatory context information is available for at least in one
1092 * event, return 0 otherwise return -1.
1094 int check_requirements(struct bt_context
*ctx
)
1096 unsigned int i
, evt_cnt
, field_cnt
;
1097 struct bt_ctf_event_decl
*const * evt_list
;
1098 const struct bt_ctf_field_decl
*const * field_list
;
1101 int procname_check
= 0;
1105 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1110 for (i
= 0; i
< evt_cnt
; i
++) {
1111 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1112 &field_list
, &field_cnt
);
1113 ret
= check_field_requirements(field_list
, field_cnt
,
1114 &tid_check
, &pid_check
, &procname_check
,
1117 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1118 &field_list
, &field_cnt
);
1119 ret
= check_field_requirements(field_list
, field_cnt
,
1120 &tid_check
, &pid_check
, &procname_check
,
1123 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1124 &field_list
, &field_cnt
);
1125 ret
= check_field_requirements(field_list
, field_cnt
,
1126 &tid_check
, &pid_check
, &procname_check
,
1130 if (tid_check
== 0) {
1132 fprintf(stderr
, "[error] missing tid context information\n");
1134 if (pid_check
== 0) {
1136 fprintf(stderr
, "[error] missing pid context information\n");
1138 if (ppid_check
== 0) {
1140 fprintf(stderr
, "[error] missing ppid context information\n");
1142 if (procname_check
== 0) {
1144 fprintf(stderr
, "[error] missing procname context information\n");
1151 static void handle_sigchild(int signal
)
1155 waitpid(opt_exec_pid
, &status
, 0);
1158 int main(int argc
, char **argv
, char **envp
)
1161 struct bt_context
*bt_ctx
= NULL
;
1163 //babeltrace_verbose = 1;
1165 ret
= parse_options(argc
, argv
);
1167 fprintf(stdout
, "Error parsing options.\n\n");
1170 } else if (ret
> 0) {
1174 if (opt_exec_name
) {
1175 opt_exec_env
= envp
;
1176 signal(SIGCHLD
, handle_sigchild
);
1179 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1181 #ifdef LTTNGTOP_MMAP_LIVE
1183 signal(SIGTERM
, handle_textdump_sigterm
);
1184 signal(SIGINT
, handle_textdump_sigterm
);
1186 mmap_live_loop(bt_ctx
);
1187 pthread_join(timer_thread
, NULL
);
1189 pthread_join(display_thread
, NULL
);
1191 lttng_stop_tracing("test");
1192 lttng_destroy_session("test");
1196 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1197 "trace directory or -r <relayd hostname/IP>\n");
1201 #endif /* LTTNGTOP_MMAP_LIVE */
1202 } else if (!opt_input_path
&& remote_live
) {
1204 bt_ctx
= bt_context_create();
1205 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1206 "lttng-live", NULL
);
1208 fprintf(stderr
, "[error] Opening the trace\n");
1214 bt_ctx
= bt_context_create();
1215 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1217 fprintf(stderr
, "[error] Opening the trace\n");
1221 ret
= check_requirements(bt_ctx
);
1223 fprintf(stderr
, "[error] some mandatory contexts "
1224 "were missing, exiting.\n");
1228 if (!opt_textdump
) {
1229 pthread_create(&display_thread
, NULL
, ncurses_display
,
1231 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1239 pthread_join(display_thread
, NULL
);
1241 pthread_join(timer_thread
, NULL
);
1247 bt_context_put(bt_ctx
);