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 struct lttngtop
*copy
;
70 pthread_t display_thread
;
71 pthread_t timer_thread
;
73 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
74 unsigned long last_display_update
= 0;
75 unsigned long last_event_ts
= 0;
76 struct syscall
*last_syscall
;
78 /* list of FDs available for being read with snapshots */
79 struct bt_mmap_stream_list mmap_list
;
80 GPtrArray
*lttng_consumer_stream_array
;
81 int sessiond_metadata
, consumerd_metadata
;
82 struct lttng_consumer_local_data
*ctx
= NULL
;
83 /* list of snapshots currently not consumed */
84 GPtrArray
*available_snapshots
;
85 sem_t metadata_available
;
102 static struct poptOption long_options
[] = {
103 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
104 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
105 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
106 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
107 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
108 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
109 { "hostname", 'n', POPT_ARG_STRING
, &opt_hostname
, OPT_HOSTNAME
, NULL
, NULL
},
110 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
111 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
112 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
113 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
116 #ifdef LTTNGTOP_MMAP_LIVE
117 static void handle_textdump_sigterm(int signal
)
120 lttng_destroy_session("test");
124 void *refresh_thread(void *p
)
128 sem_post(&pause_sem
);
130 sem_post(&end_trace_sem
);
131 sem_post(&goodtodisplay
);
132 sem_post(&goodtoupdate
);
135 if (!opt_input_path
) {
136 #ifdef LTTNGTOP_MMAP_LIVE
137 mmap_live_flush(mmap_list
);
140 sem_wait(&pause_sem
);
141 sem_post(&pause_sem
);
143 sleep(refresh_display
/NSEC_PER_SEC
);
147 void *ncurses_display(void *p
)
149 unsigned int current_display_index
= 0;
151 sem_wait(&bootstrap
);
153 * Prevent the 1 second delay when we hit ESC
160 sem_wait(&goodtodisplay
);
161 sem_wait(&pause_sem
);
164 sem_post(&pause_sem
);
170 copy
= g_ptr_array_index(copies
, current_display_index
);
172 display(current_display_index
++);
174 sem_post(&goodtoupdate
);
175 sem_post(&pause_sem
);
179 void print_fields(struct bt_ctf_event
*event
)
182 const struct bt_definition
*const * list
;
183 const struct bt_declaration
*l
;
184 const struct bt_definition
*scope
;
185 enum ctf_type_id type
;
188 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
190 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
191 for (i
= 0; i
< cnt
; i
++) {
194 printf("%s = ", bt_ctf_field_name(list
[i
]));
195 l
= bt_ctf_get_decl_from_def(list
[i
]);
196 type
= bt_ctf_field_type(l
);
197 if (type
== CTF_TYPE_INTEGER
) {
198 if (bt_ctf_get_int_signedness(l
) == 0)
199 printf("%" PRIu64
"", bt_ctf_get_uint64(list
[i
]));
201 printf("%" PRId64
"", bt_ctf_get_int64(list
[i
]));
202 } else if (type
== CTF_TYPE_STRING
) {
203 printf("%s", bt_ctf_get_string(list
[i
]));
204 } else if (type
== CTF_TYPE_ARRAY
) {
205 str
= bt_ctf_get_char_array(list
[i
]);
206 if (!bt_ctf_field_get_error() && str
)
213 * hook on each event to check the timestamp and refresh the display if
216 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
218 unsigned long timestamp
;
221 uint64_t ts_nsec_start
;
222 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
223 const struct bt_definition
*scope
;
224 const char *hostname
, *procname
;
226 char *from_syscall
= NULL
;
228 timestamp
= bt_ctf_get_timestamp(call_data
);
230 /* can happen in network live when tracing is idle */
231 if (timestamp
< last_event_ts
)
234 last_event_ts
= timestamp
;
236 start
= format_timestamp(timestamp
);
237 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
239 pid
= get_context_pid(call_data
);
240 if (pid
== -1ULL && opt_tid
) {
244 tid
= get_context_tid(call_data
);
246 hostname
= get_context_hostname(call_data
);
251 if (opt_tid
|| opt_hostname
|| opt_exec_name
) {
252 if (!lookup_filter_tid_list(lookup
)) {
253 /* To display when a process of ours in getting scheduled in */
254 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
257 scope
= bt_ctf_get_top_level_scope(call_data
,
259 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
260 scope
, "_next_tid"));
261 if (bt_ctf_field_get_error()) {
262 fprintf(stderr
, "Missing next_tid field\n");
265 if (!lookup_filter_tid_list(next_tid
)) {
274 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
275 "exit_syscall", 12)) != 0) {
277 printf(" ...interrupted...\n");
280 cpu_id
= get_cpu_id(call_data
);
281 procname
= get_context_comm(call_data
);
282 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
283 cpu
= get_cpu(cpu_id
);
284 cpu
->current_syscall
= g_new0(struct syscall
, 1);
285 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
286 cpu
->current_syscall
->ts_start
= timestamp
;
287 cpu
->current_syscall
->cpu_id
= cpu_id
;
288 last_syscall
= cpu
->current_syscall
;
290 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
293 /* Return code of a syscall if it was the last displayed event. */
294 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
295 if (last_syscall
->cpu_id
== cpu_id
) {
298 delta
= timestamp
- last_syscall
->ts_start
;
299 scope
= bt_ctf_get_top_level_scope(call_data
,
301 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
304 printf(" = %" PRId64
" (+%" PRIu64
".%09" PRIu64
")\n",
305 syscall_ret
, delta
/ NSEC_PER_SEC
,
306 delta
% NSEC_PER_SEC
);
311 printf(" ...interrupted...\n");
315 cpu
= get_cpu(cpu_id
);
316 if (cpu
->current_syscall
) {
317 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
318 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
319 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
320 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
321 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
322 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
323 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
324 cpu_id
, cpu
->current_syscall
->name
);
328 free(cpu
->current_syscall
->name
);
329 g_free(cpu
->current_syscall
);
330 cpu
->current_syscall
= NULL
;
337 delta
= timestamp
- prev_ts
;
340 printf("%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
341 "(cpu %d) [%s (%d/%d)] %s (",
342 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
343 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
344 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
345 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
346 bt_ctf_event_name(call_data
));
347 print_fields(call_data
);
348 printf(")%s%c", (from_syscall
) ? from_syscall
: "",
349 (current_syscall
) ? '\0' : '\n');
356 return BT_CB_ERROR_STOP
;
358 return BT_CB_OK_STOP
;
361 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
364 struct kprobes
*kprobe
;
367 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
368 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
369 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
378 * hook on each event to check the timestamp and refresh the display if
381 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
383 unsigned long timestamp
;
385 timestamp
= bt_ctf_get_timestamp(call_data
);
386 if (timestamp
== -1ULL)
389 /* can happen in network live when tracing is idle */
390 if (timestamp
< last_event_ts
)
393 last_event_ts
= timestamp
;
395 if (last_display_update
== 0)
396 last_display_update
= timestamp
;
398 if (timestamp
- last_display_update
>= refresh_display
) {
399 sem_wait(&goodtoupdate
);
400 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
402 sem_post(&goodtodisplay
);
403 sem_post(&bootstrap
);
404 last_display_update
= timestamp
;
409 fprintf(stderr
, "check_timestamp callback error\n");
410 return BT_CB_ERROR_STOP
;
413 return BT_CB_OK_STOP
;
417 * get_perf_counter : get or create and return a perf_counter struct for
418 * either a process or a cpu (only one of the 2 parameters mandatory)
420 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
423 struct perfcounter
*ret
;
433 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
437 ret
= g_new0(struct perfcounter
, 1);
438 /* by default, make it visible in the UI */
440 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
449 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
450 const char *name
, int value
)
452 struct perfcounter
*cpu_perf
, *process_perf
;
454 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
455 if (cpu_perf
->count
< value
) {
456 process_perf
= get_perf_counter(name
, proc
, NULL
);
457 process_perf
->count
+= value
- cpu_perf
->count
;
458 cpu_perf
->count
= value
;
462 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
463 const struct bt_definition
*scope
,
464 struct processtop
*proc
,
467 struct bt_definition
const * const *list
= NULL
;
468 const struct bt_definition
*field
;
470 struct perfcounter
*perfcounter
;
478 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
485 g_hash_table_iter_init(&iter
, global_perf_liszt
);
486 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
487 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
489 int value
= bt_ctf_get_uint64(field
);
490 if (bt_ctf_field_get_error())
492 update_perf_value(proc
, cpu
, (char *) key
, value
);
500 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
503 const struct bt_definition
*scope
;
505 cpu
= get_cpu(get_cpu_id(event
));
507 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
508 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
510 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
511 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
513 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
514 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
517 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
520 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
521 char *comm
, *hostname
;
522 struct processtop
*parent
, *child
;
523 unsigned long timestamp
;
525 timestamp
= bt_ctf_get_timestamp(call_data
);
526 if (timestamp
== -1ULL)
529 pid
= get_context_pid(call_data
);
534 tid
= get_context_tid(call_data
);
538 ppid
= get_context_ppid(call_data
);
542 vpid
= get_context_vpid(call_data
);
546 vtid
= get_context_vtid(call_data
);
550 vppid
= get_context_vppid(call_data
);
554 comm
= get_context_comm(call_data
);
559 hostname
= get_context_hostname(call_data
);
561 /* find or create the current process */
562 child
= find_process_tid(<tngtop
, tid
, comm
);
564 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
567 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
570 /* find or create the parent */
571 parent
= find_process_tid(<tngtop
, pid
, comm
);
573 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
578 /* attach the parent to the current process */
579 child
->threadparent
= parent
;
580 add_thread(parent
, child
);
583 update_perf_counter(child
, call_data
);
589 return BT_CB_ERROR_STOP
;
594 copies
= g_ptr_array_new();
595 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
596 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
597 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
598 tid_filter_list
= g_hash_table_new(g_str_hash
,
601 sem_init(&goodtodisplay
, 0, 0);
602 sem_init(&goodtoupdate
, 0, 1);
603 sem_init(&timer
, 0, 1);
604 sem_init(&bootstrap
, 0, 0);
605 sem_init(&pause_sem
, 0, 1);
606 sem_init(&end_trace_sem
, 0, 0);
608 reset_global_counters();
610 lttngtop
.nbthreads
= 0;
611 lttngtop
.nbfiles
= 0;
613 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
615 lttngtop
.process_table
= g_ptr_array_new();
616 lttngtop
.files_table
= g_ptr_array_new();
617 lttngtop
.cpu_table
= g_ptr_array_new();
624 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
625 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
626 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
627 fprintf(fp
, " -h, --help This help message\n");
628 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
629 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
630 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
631 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
632 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
633 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
634 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
638 * Parse probe options.
639 * Shamelessly stolen from lttng-tools :
640 * src/bin/lttng/commands/enable_events.c
642 static struct kprobes
*parse_probe_opts(char *opt
)
645 char name
[LTTNG_SYMBOL_NAME_LEN
];
646 struct kprobes
*kprobe
;
650 kprobe->probe_addr = 0;
651 kprobe->probe_offset = 0;
652 asprintf(&kprobe->probe_name, "probe_sys_open");
653 asprintf(&kprobe->symbol_name, "sys_open");
661 kprobe
= g_new0(struct kprobes
, 1);
663 /* Check for symbol+offset */
664 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
666 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
667 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
669 if (strlen(s_hex
) == 0) {
670 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
674 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
675 kprobe
->probe_addr
= 0;
679 /* Check for symbol */
680 if (isalpha(name
[0])) {
681 ret
= sscanf(opt
, "%s", name
);
683 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
684 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
685 kprobe
->probe_offset
= 0;
686 kprobe
->probe_addr
= 0;
691 /* Check for address */
692 ret
= sscanf(opt
, "%s", s_hex
);
694 if (strlen(s_hex
) == 0) {
695 fprintf(stderr
, "Invalid probe address %s", s_hex
);
699 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
700 kprobe
->probe_offset
= 0;
701 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
713 * Return 0 if caller should continue, < 0 if caller should return
714 * error, > 0 if caller should exit without reporting error.
716 static int parse_options(int argc
, char **argv
)
726 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
727 poptReadDefaultConfig(pc
, 0);
729 while ((opt
= poptGetNextOpt(pc
)) != -1) {
733 ret
= 1; /* exit cleanly */
743 tmp_str
= strtok(opt_tid
, ",");
745 tid
= malloc(sizeof(int));
746 *tid
= atoi(tmp_str
);
747 g_hash_table_insert(tid_filter_list
,
748 (gpointer
) tid
, tid
);
749 tmp_str
= strtok(NULL
, ",");
753 /* start reading the live trace from the beginning */
758 tmp_str
= strtok(opt_hostname
, ",");
760 add_hostname_list(tmp_str
, 1);
761 tmp_str
= strtok(NULL
, ",");
764 case OPT_RELAY_HOSTNAME
:
768 lttngtop
.kprobes_table
= g_ptr_array_new();
769 tmp_str
= strtok(opt_kprobes
, ",");
771 struct kprobes
*kprobe
;
773 kprobe
= parse_probe_opts(tmp_str
);
776 lttngtop
.kprobes_table
,
782 tmp_str
= strtok(NULL
, ",");
791 opt_exec_name
= NULL
;
792 opt_exec_argv
= NULL
;
793 for (i
= 0; i
< argc
; i
++) {
794 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
795 opt_exec_name
= argv
[i
+ 1];
796 opt_exec_argv
= &argv
[i
+ 1];
800 if (!opt_exec_name
) {
801 opt_input_path
= poptGetArg(pc
);
811 void iter_trace(struct bt_context
*bt_ctx
)
813 struct bt_ctf_iter
*iter
;
814 struct bt_iter_pos begin_pos
;
815 struct kprobes
*kprobe
;
816 const struct bt_ctf_event
*event
;
820 begin_pos
.type
= BT_SEEK_BEGIN
;
821 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
823 /* at each event, verify the status of the process table */
824 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
827 /* to handle the follow child option */
828 bt_ctf_iter_add_callback(iter
,
829 g_quark_from_static_string("sched_process_fork"),
830 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
831 /* to clean up the process table */
832 bt_ctf_iter_add_callback(iter
,
833 g_quark_from_static_string("sched_process_free"),
834 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
836 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
840 /* at each event check if we need to refresh */
841 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
844 /* to handle the scheduling events */
845 bt_ctf_iter_add_callback(iter
,
846 g_quark_from_static_string("sched_switch"),
847 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
848 /* to get all the process from the statedumps */
849 bt_ctf_iter_add_callback(iter
,
850 g_quark_from_static_string(
851 "lttng_statedump_process_state"),
852 NULL
, 0, handle_statedump_process_state
,
856 bt_ctf_iter_add_callback(iter
,
857 g_quark_from_static_string("exit_syscall"),
858 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
859 bt_ctf_iter_add_callback(iter
,
860 g_quark_from_static_string("sys_write"),
861 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
862 bt_ctf_iter_add_callback(iter
,
863 g_quark_from_static_string("sys_read"),
864 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
865 bt_ctf_iter_add_callback(iter
,
866 g_quark_from_static_string("sys_open"),
867 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
868 bt_ctf_iter_add_callback(iter
,
869 g_quark_from_static_string("sys_close"),
870 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
871 bt_ctf_iter_add_callback(iter
,
872 g_quark_from_static_string(
873 "lttng_statedump_file_descriptor"),
874 NULL
, 0, handle_statedump_file_descriptor
,
878 if (lttngtop
.kprobes_table
) {
879 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
880 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
881 bt_ctf_iter_add_callback(iter
,
882 g_quark_from_static_string(
884 NULL
, 0, handle_kprobes
,
895 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
897 } else if (pid
> 0) {
899 g_hash_table_insert(tid_filter_list
,
908 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
909 if (quit
|| reload_trace
)
911 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
916 /* block until quit, we reached the end of the trace */
917 sem_wait(&end_trace_sem
);
920 bt_ctf_iter_destroy(iter
);
924 * bt_context_add_traces_recursive: Open a trace recursively
925 * (copied from BSD code in converter/babeltrace.c)
927 * Find each trace present in the subdirectory starting from the given
928 * path, and add them to the context. The packet_seek parameter can be
929 * NULL: this specify to use the default format packet_seek.
931 * Return: 0 on success, nonzero on failure.
932 * Unable to open toplevel: failure.
933 * Unable to open some subdirectory or file: warn and continue;
935 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
936 const char *format_str
,
937 void (*packet_seek
)(struct bt_stream_pos
*pos
,
938 size_t offset
, int whence
))
943 char lpath
[PATH_MAX
];
944 char * const paths
[2] = { lpath
, NULL
};
947 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
948 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
949 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
950 ret
= bt_context_add_trace(ctx
,
951 path
, format_str
, packet_seek
, NULL
, NULL
);
953 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
954 "for reading.\n", path
);
955 /* Allow to skip erroneous traces. */
956 ret
= 1; /* partial error */
961 * Need to copy path, because fts_open can change it.
962 * It is the pointer array, not the strings, that are constant.
964 strncpy(lpath
, path
, PATH_MAX
);
965 lpath
[PATH_MAX
- 1] = '\0';
967 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
969 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
974 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
976 while ((node
= fts_read(tree
))) {
979 if (!(node
->fts_info
& FTS_D
))
982 dirfd
= open(node
->fts_accpath
, 0);
984 fprintf(stderr
, "[error] [Context] Unable to open trace "
985 "directory file descriptor.\n");
989 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1007 trace_id
= bt_context_add_trace(ctx
,
1008 node
->fts_accpath
, format_str
,
1009 packet_seek
, NULL
, NULL
);
1011 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1012 "for reading.\n", node
->fts_accpath
, path
);
1013 /* Allow to skip erroneous traces. */
1016 g_array_append_val(trace_ids
, trace_id
);
1020 g_array_free(trace_ids
, TRUE
);
1027 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1028 int field_cnt
, int *tid_check
, int *pid_check
,
1029 int *procname_check
, int *ppid_check
)
1032 struct perfcounter
*global
;
1035 for (j
= 0; j
< field_cnt
; j
++) {
1036 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1037 if (*tid_check
== 0) {
1038 if (strncmp(name
, "tid", 3) == 0)
1041 if (*pid_check
== 0) {
1042 if (strncmp(name
, "pid", 3) == 0)
1045 if (*ppid_check
== 0) {
1046 if (strncmp(name
, "ppid", 4) == 0)
1049 if (*procname_check
== 0) {
1050 if (strncmp(name
, "procname", 8) == 0)
1051 (*procname_check
)++;
1053 if (strncmp(name
, "perf_", 5) == 0) {
1054 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1056 global
= g_new0(struct perfcounter
, 1);
1057 /* by default, sort on the first perf context */
1058 if (g_hash_table_size(global_perf_liszt
) == 0)
1060 global
->visible
= 1;
1061 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1066 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1067 *procname_check
== 1)
1074 * check_requirements: check if the required context informations are available
1076 * If each mandatory context information is available for at least in one
1077 * event, return 0 otherwise return -1.
1079 int check_requirements(struct bt_context
*ctx
)
1081 unsigned int i
, evt_cnt
, field_cnt
;
1082 struct bt_ctf_event_decl
*const * evt_list
;
1083 const struct bt_ctf_field_decl
*const * field_list
;
1086 int procname_check
= 0;
1090 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1095 for (i
= 0; i
< evt_cnt
; i
++) {
1096 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1097 &field_list
, &field_cnt
);
1098 ret
= check_field_requirements(field_list
, field_cnt
,
1099 &tid_check
, &pid_check
, &procname_check
,
1102 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1103 &field_list
, &field_cnt
);
1104 ret
= check_field_requirements(field_list
, field_cnt
,
1105 &tid_check
, &pid_check
, &procname_check
,
1108 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1109 &field_list
, &field_cnt
);
1110 ret
= check_field_requirements(field_list
, field_cnt
,
1111 &tid_check
, &pid_check
, &procname_check
,
1115 if (tid_check
== 0) {
1117 fprintf(stderr
, "[error] missing tid context information\n");
1119 if (pid_check
== 0) {
1121 fprintf(stderr
, "[error] missing pid context information\n");
1123 if (ppid_check
== 0) {
1125 fprintf(stderr
, "[error] missing ppid context information\n");
1127 if (procname_check
== 0) {
1129 fprintf(stderr
, "[error] missing procname context information\n");
1136 static void handle_sigchild(int signal
)
1140 waitpid(opt_exec_pid
, &status
, 0);
1143 int main(int argc
, char **argv
, char **envp
)
1146 struct bt_context
*bt_ctx
= NULL
;
1148 //babeltrace_verbose = 1;
1150 ret
= parse_options(argc
, argv
);
1152 fprintf(stdout
, "Error parsing options.\n\n");
1155 } else if (ret
> 0) {
1159 if (opt_exec_name
) {
1160 opt_exec_env
= envp
;
1161 signal(SIGCHLD
, handle_sigchild
);
1164 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1166 #ifdef LTTNGTOP_MMAP_LIVE
1168 signal(SIGTERM
, handle_textdump_sigterm
);
1169 signal(SIGINT
, handle_textdump_sigterm
);
1171 mmap_live_loop(bt_ctx
);
1172 pthread_join(timer_thread
, NULL
);
1174 pthread_join(display_thread
, NULL
);
1176 lttng_stop_tracing("test");
1177 lttng_destroy_session("test");
1181 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1182 "trace directory or -r <relayd hostname/IP>\n");
1186 #endif /* LTTNGTOP_MMAP_LIVE */
1187 } else if (!opt_input_path
&& remote_live
) {
1189 bt_ctx
= bt_context_create();
1190 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1191 "lttng-live", NULL
);
1193 fprintf(stderr
, "[error] Opening the trace\n");
1199 bt_ctx
= bt_context_create();
1200 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1202 fprintf(stderr
, "[error] Opening the trace\n");
1206 ret
= check_requirements(bt_ctx
);
1208 fprintf(stderr
, "[error] some mandatory contexts "
1209 "were missing, exiting.\n");
1213 if (!opt_textdump
) {
1214 pthread_create(&display_thread
, NULL
, ncurses_display
,
1216 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1224 pthread_join(display_thread
, NULL
);
1226 pthread_join(timer_thread
, NULL
);
1232 bt_context_put(bt_ctx
);