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;
77 /* list of FDs available for being read with snapshots */
78 struct bt_mmap_stream_list mmap_list
;
79 GPtrArray
*lttng_consumer_stream_array
;
80 int sessiond_metadata
, consumerd_metadata
;
81 struct lttng_consumer_local_data
*ctx
= NULL
;
82 /* list of snapshots currently not consumed */
83 GPtrArray
*available_snapshots
;
84 sem_t metadata_available
;
101 static struct poptOption long_options
[] = {
102 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
103 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
104 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
105 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
106 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
107 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
108 { "hostname", 'n', POPT_ARG_STRING
, &opt_hostname
, OPT_HOSTNAME
, NULL
, NULL
},
109 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
110 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
111 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
112 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
115 #ifdef LTTNGTOP_MMAP_LIVE
116 static void handle_textdump_sigterm(int signal
)
119 lttng_destroy_session("test");
123 void *refresh_thread(void *p
)
127 sem_post(&pause_sem
);
129 sem_post(&end_trace_sem
);
130 sem_post(&goodtodisplay
);
131 sem_post(&goodtoupdate
);
134 if (!opt_input_path
) {
135 #ifdef LTTNGTOP_MMAP_LIVE
136 mmap_live_flush(mmap_list
);
139 sem_wait(&pause_sem
);
140 sem_post(&pause_sem
);
142 sleep(refresh_display
/NSEC_PER_SEC
);
146 void *ncurses_display(void *p
)
148 unsigned int current_display_index
= 0;
150 sem_wait(&bootstrap
);
152 * Prevent the 1 second delay when we hit ESC
159 sem_wait(&goodtodisplay
);
160 sem_wait(&pause_sem
);
163 sem_post(&pause_sem
);
169 copy
= g_ptr_array_index(copies
, current_display_index
);
171 display(current_display_index
++);
173 sem_post(&goodtoupdate
);
174 sem_post(&pause_sem
);
178 void print_fields(struct bt_ctf_event
*event
)
181 const struct bt_definition
*const * list
;
182 const struct bt_declaration
*l
;
183 const struct bt_definition
*scope
;
184 enum ctf_type_id type
;
187 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
189 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
190 for (i
= 0; i
< cnt
; i
++) {
193 printf("%s = ", bt_ctf_field_name(list
[i
]));
194 l
= bt_ctf_get_decl_from_def(list
[i
]);
195 type
= bt_ctf_field_type(l
);
196 if (type
== CTF_TYPE_INTEGER
) {
197 if (bt_ctf_get_int_signedness(l
) == 0)
198 printf("%" PRIu64
"", bt_ctf_get_uint64(list
[i
]));
200 printf("%" PRId64
"", bt_ctf_get_int64(list
[i
]));
201 } else if (type
== CTF_TYPE_STRING
) {
202 printf("%s", bt_ctf_get_string(list
[i
]));
203 } else if (type
== CTF_TYPE_ARRAY
) {
204 str
= bt_ctf_get_char_array(list
[i
]);
205 if (!bt_ctf_field_get_error() && str
)
212 * hook on each event to check the timestamp and refresh the display if
215 enum bt_cb_ret
print_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
217 unsigned long timestamp
;
220 uint64_t ts_nsec_start
;
221 int pid
, cpu_id
, tid
, ret
;
222 const struct bt_definition
*scope
;
223 const char *hostname
, *procname
;
225 char *from_syscall
= NULL
;
227 timestamp
= bt_ctf_get_timestamp(call_data
);
229 /* can happen in network live when tracing is idle */
230 if (timestamp
< last_event_ts
)
233 last_event_ts
= timestamp
;
235 start
= format_timestamp(timestamp
);
236 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
238 pid
= get_context_pid(call_data
);
239 if (pid
== -1ULL && opt_tid
) {
243 tid
= get_context_tid(call_data
);
245 hostname
= get_context_hostname(call_data
);
246 if (opt_tid
|| opt_hostname
|| opt_exec_name
) {
247 if (!lookup_filter_tid_list(pid
)) {
248 /* To display when a process of ours in getting scheduled in */
249 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
252 scope
= bt_ctf_get_top_level_scope(call_data
,
254 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
255 scope
, "_next_tid"));
256 if (bt_ctf_field_get_error()) {
257 fprintf(stderr
, "Missing next_tid field\n");
260 if (!lookup_filter_tid_list(next_tid
)) {
269 cpu_id
= get_cpu_id(call_data
);
270 procname
= get_context_comm(call_data
);
271 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
272 cpu
= get_cpu(cpu_id
);
273 cpu
->current_syscall
= g_new0(struct syscall
, 1);
274 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
275 cpu
->current_syscall
->ts_start
= timestamp
;
276 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
279 cpu
= get_cpu(cpu_id
);
280 if (cpu
->current_syscall
) {
281 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
282 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
283 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
284 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]\n",
285 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
286 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
287 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
288 cpu_id
, cpu
->current_syscall
->name
);
292 free(cpu
->current_syscall
->name
);
293 g_free(cpu
->current_syscall
);
294 cpu
->current_syscall
= NULL
;
300 delta
= timestamp
- prev_ts
;
303 printf("%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
304 "(cpu %d) [%s (%d/%d)] %s (",
305 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
306 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
307 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
308 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
309 bt_ctf_event_name(call_data
));
310 print_fields(call_data
);
311 printf(")%s", (from_syscall
) ? from_syscall
: "\n");
318 return BT_CB_ERROR_STOP
;
320 return BT_CB_OK_STOP
;
323 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
326 struct kprobes
*kprobe
;
329 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
330 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
331 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
340 * hook on each event to check the timestamp and refresh the display if
343 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
345 unsigned long timestamp
;
347 timestamp
= bt_ctf_get_timestamp(call_data
);
348 if (timestamp
== -1ULL)
351 /* can happen in network live when tracing is idle */
352 if (timestamp
< last_event_ts
)
355 last_event_ts
= timestamp
;
357 if (last_display_update
== 0)
358 last_display_update
= timestamp
;
360 if (timestamp
- last_display_update
>= refresh_display
) {
361 sem_wait(&goodtoupdate
);
362 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
364 sem_post(&goodtodisplay
);
365 sem_post(&bootstrap
);
366 last_display_update
= timestamp
;
371 fprintf(stderr
, "check_timestamp callback error\n");
372 return BT_CB_ERROR_STOP
;
375 return BT_CB_OK_STOP
;
379 * get_perf_counter : get or create and return a perf_counter struct for
380 * either a process or a cpu (only one of the 2 parameters mandatory)
382 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
385 struct perfcounter
*ret
;
395 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
399 ret
= g_new0(struct perfcounter
, 1);
400 /* by default, make it visible in the UI */
402 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
411 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
412 const char *name
, int value
)
414 struct perfcounter
*cpu_perf
, *process_perf
;
416 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
417 if (cpu_perf
->count
< value
) {
418 process_perf
= get_perf_counter(name
, proc
, NULL
);
419 process_perf
->count
+= value
- cpu_perf
->count
;
420 cpu_perf
->count
= value
;
424 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
425 const struct bt_definition
*scope
,
426 struct processtop
*proc
,
429 struct bt_definition
const * const *list
= NULL
;
430 const struct bt_definition
*field
;
432 struct perfcounter
*perfcounter
;
440 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
447 g_hash_table_iter_init(&iter
, global_perf_liszt
);
448 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
449 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
451 int value
= bt_ctf_get_uint64(field
);
452 if (bt_ctf_field_get_error())
454 update_perf_value(proc
, cpu
, (char *) key
, value
);
462 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
465 const struct bt_definition
*scope
;
467 cpu
= get_cpu(get_cpu_id(event
));
469 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
470 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
472 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
473 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
475 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
476 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
479 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
482 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
483 char *comm
, *hostname
;
484 struct processtop
*parent
, *child
;
485 unsigned long timestamp
;
487 timestamp
= bt_ctf_get_timestamp(call_data
);
488 if (timestamp
== -1ULL)
491 pid
= get_context_pid(call_data
);
496 tid
= get_context_tid(call_data
);
500 ppid
= get_context_ppid(call_data
);
504 vpid
= get_context_vpid(call_data
);
508 vtid
= get_context_vtid(call_data
);
512 vppid
= get_context_vppid(call_data
);
516 comm
= get_context_comm(call_data
);
521 hostname
= get_context_hostname(call_data
);
523 /* find or create the current process */
524 child
= find_process_tid(<tngtop
, tid
, comm
);
526 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
529 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
532 /* find or create the parent */
533 parent
= find_process_tid(<tngtop
, pid
, comm
);
535 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
540 /* attach the parent to the current process */
541 child
->threadparent
= parent
;
542 add_thread(parent
, child
);
545 update_perf_counter(child
, call_data
);
551 return BT_CB_ERROR_STOP
;
556 copies
= g_ptr_array_new();
557 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
558 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
559 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
560 tid_filter_list
= g_hash_table_new(g_str_hash
,
563 sem_init(&goodtodisplay
, 0, 0);
564 sem_init(&goodtoupdate
, 0, 1);
565 sem_init(&timer
, 0, 1);
566 sem_init(&bootstrap
, 0, 0);
567 sem_init(&pause_sem
, 0, 1);
568 sem_init(&end_trace_sem
, 0, 0);
570 reset_global_counters();
572 lttngtop
.nbthreads
= 0;
573 lttngtop
.nbfiles
= 0;
575 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
577 lttngtop
.process_table
= g_ptr_array_new();
578 lttngtop
.files_table
= g_ptr_array_new();
579 lttngtop
.cpu_table
= g_ptr_array_new();
586 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
587 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
588 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
589 fprintf(fp
, " -h, --help This help message\n");
590 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
591 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
592 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
593 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
594 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
595 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
596 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
600 * Parse probe options.
601 * Shamelessly stolen from lttng-tools :
602 * src/bin/lttng/commands/enable_events.c
604 static struct kprobes
*parse_probe_opts(char *opt
)
607 char name
[LTTNG_SYMBOL_NAME_LEN
];
608 struct kprobes
*kprobe
;
612 kprobe->probe_addr = 0;
613 kprobe->probe_offset = 0;
614 asprintf(&kprobe->probe_name, "probe_sys_open");
615 asprintf(&kprobe->symbol_name, "sys_open");
623 kprobe
= g_new0(struct kprobes
, 1);
625 /* Check for symbol+offset */
626 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
628 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
629 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
631 if (strlen(s_hex
) == 0) {
632 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
636 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
637 kprobe
->probe_addr
= 0;
641 /* Check for symbol */
642 if (isalpha(name
[0])) {
643 ret
= sscanf(opt
, "%s", name
);
645 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
646 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
647 kprobe
->probe_offset
= 0;
648 kprobe
->probe_addr
= 0;
653 /* Check for address */
654 ret
= sscanf(opt
, "%s", s_hex
);
656 if (strlen(s_hex
) == 0) {
657 fprintf(stderr
, "Invalid probe address %s", s_hex
);
661 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
662 kprobe
->probe_offset
= 0;
663 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
675 * Return 0 if caller should continue, < 0 if caller should return
676 * error, > 0 if caller should exit without reporting error.
678 static int parse_options(int argc
, char **argv
)
688 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
689 poptReadDefaultConfig(pc
, 0);
691 while ((opt
= poptGetNextOpt(pc
)) != -1) {
695 ret
= 1; /* exit cleanly */
705 tmp_str
= strtok(opt_tid
, ",");
707 tid
= malloc(sizeof(int));
708 *tid
= atoi(tmp_str
);
709 g_hash_table_insert(tid_filter_list
,
710 (gpointer
) tid
, tid
);
711 tmp_str
= strtok(NULL
, ",");
715 /* start reading the live trace from the beginning */
720 tmp_str
= strtok(opt_hostname
, ",");
722 add_hostname_list(tmp_str
, 1);
723 tmp_str
= strtok(NULL
, ",");
726 case OPT_RELAY_HOSTNAME
:
730 lttngtop
.kprobes_table
= g_ptr_array_new();
731 tmp_str
= strtok(opt_kprobes
, ",");
733 struct kprobes
*kprobe
;
735 kprobe
= parse_probe_opts(tmp_str
);
738 lttngtop
.kprobes_table
,
744 tmp_str
= strtok(NULL
, ",");
753 opt_exec_name
= NULL
;
754 opt_exec_argv
= NULL
;
755 for (i
= 0; i
< argc
; i
++) {
756 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
757 opt_exec_name
= argv
[i
+ 1];
758 opt_exec_argv
= &argv
[i
+ 1];
762 if (!opt_exec_name
) {
763 opt_input_path
= poptGetArg(pc
);
773 void iter_trace(struct bt_context
*bt_ctx
)
775 struct bt_ctf_iter
*iter
;
776 struct bt_iter_pos begin_pos
;
777 struct kprobes
*kprobe
;
778 const struct bt_ctf_event
*event
;
782 begin_pos
.type
= BT_SEEK_BEGIN
;
783 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
785 /* at each event, verify the status of the process table */
786 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
789 /* to handle the follow child option */
790 bt_ctf_iter_add_callback(iter
,
791 g_quark_from_static_string("sched_process_fork"),
792 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
793 /* to clean up the process table */
794 bt_ctf_iter_add_callback(iter
,
795 g_quark_from_static_string("sched_process_free"),
796 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
798 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
802 /* at each event check if we need to refresh */
803 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
806 /* to handle the scheduling events */
807 bt_ctf_iter_add_callback(iter
,
808 g_quark_from_static_string("sched_switch"),
809 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
810 /* to get all the process from the statedumps */
811 bt_ctf_iter_add_callback(iter
,
812 g_quark_from_static_string(
813 "lttng_statedump_process_state"),
814 NULL
, 0, handle_statedump_process_state
,
818 bt_ctf_iter_add_callback(iter
,
819 g_quark_from_static_string("exit_syscall"),
820 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
821 bt_ctf_iter_add_callback(iter
,
822 g_quark_from_static_string("sys_write"),
823 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
824 bt_ctf_iter_add_callback(iter
,
825 g_quark_from_static_string("sys_read"),
826 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
827 bt_ctf_iter_add_callback(iter
,
828 g_quark_from_static_string("sys_open"),
829 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
830 bt_ctf_iter_add_callback(iter
,
831 g_quark_from_static_string("sys_close"),
832 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
833 bt_ctf_iter_add_callback(iter
,
834 g_quark_from_static_string(
835 "lttng_statedump_file_descriptor"),
836 NULL
, 0, handle_statedump_file_descriptor
,
840 if (lttngtop
.kprobes_table
) {
841 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
842 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
843 bt_ctf_iter_add_callback(iter
,
844 g_quark_from_static_string(
846 NULL
, 0, handle_kprobes
,
857 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
859 } else if (pid
> 0) {
861 g_hash_table_insert(tid_filter_list
,
870 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
871 if (quit
|| reload_trace
)
873 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
878 /* block until quit, we reached the end of the trace */
879 sem_wait(&end_trace_sem
);
882 bt_ctf_iter_destroy(iter
);
886 * bt_context_add_traces_recursive: Open a trace recursively
887 * (copied from BSD code in converter/babeltrace.c)
889 * Find each trace present in the subdirectory starting from the given
890 * path, and add them to the context. The packet_seek parameter can be
891 * NULL: this specify to use the default format packet_seek.
893 * Return: 0 on success, nonzero on failure.
894 * Unable to open toplevel: failure.
895 * Unable to open some subdirectory or file: warn and continue;
897 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
898 const char *format_str
,
899 void (*packet_seek
)(struct bt_stream_pos
*pos
,
900 size_t offset
, int whence
))
905 char lpath
[PATH_MAX
];
906 char * const paths
[2] = { lpath
, NULL
};
909 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
910 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
911 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
912 ret
= bt_context_add_trace(ctx
,
913 path
, format_str
, packet_seek
, NULL
, NULL
);
915 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
916 "for reading.\n", path
);
917 /* Allow to skip erroneous traces. */
918 ret
= 1; /* partial error */
923 * Need to copy path, because fts_open can change it.
924 * It is the pointer array, not the strings, that are constant.
926 strncpy(lpath
, path
, PATH_MAX
);
927 lpath
[PATH_MAX
- 1] = '\0';
929 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
931 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
936 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
938 while ((node
= fts_read(tree
))) {
941 if (!(node
->fts_info
& FTS_D
))
944 dirfd
= open(node
->fts_accpath
, 0);
946 fprintf(stderr
, "[error] [Context] Unable to open trace "
947 "directory file descriptor.\n");
951 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
969 trace_id
= bt_context_add_trace(ctx
,
970 node
->fts_accpath
, format_str
,
971 packet_seek
, NULL
, NULL
);
973 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
974 "for reading.\n", node
->fts_accpath
, path
);
975 /* Allow to skip erroneous traces. */
978 g_array_append_val(trace_ids
, trace_id
);
982 g_array_free(trace_ids
, TRUE
);
989 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
990 int field_cnt
, int *tid_check
, int *pid_check
,
991 int *procname_check
, int *ppid_check
)
994 struct perfcounter
*global
;
997 for (j
= 0; j
< field_cnt
; j
++) {
998 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
999 if (*tid_check
== 0) {
1000 if (strncmp(name
, "tid", 3) == 0)
1003 if (*pid_check
== 0) {
1004 if (strncmp(name
, "pid", 3) == 0)
1007 if (*ppid_check
== 0) {
1008 if (strncmp(name
, "ppid", 4) == 0)
1011 if (*procname_check
== 0) {
1012 if (strncmp(name
, "procname", 8) == 0)
1013 (*procname_check
)++;
1015 if (strncmp(name
, "perf_", 5) == 0) {
1016 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1018 global
= g_new0(struct perfcounter
, 1);
1019 /* by default, sort on the first perf context */
1020 if (g_hash_table_size(global_perf_liszt
) == 0)
1022 global
->visible
= 1;
1023 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1028 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1029 *procname_check
== 1)
1036 * check_requirements: check if the required context informations are available
1038 * If each mandatory context information is available for at least in one
1039 * event, return 0 otherwise return -1.
1041 int check_requirements(struct bt_context
*ctx
)
1043 unsigned int i
, evt_cnt
, field_cnt
;
1044 struct bt_ctf_event_decl
*const * evt_list
;
1045 const struct bt_ctf_field_decl
*const * field_list
;
1048 int procname_check
= 0;
1052 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1057 for (i
= 0; i
< evt_cnt
; i
++) {
1058 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1059 &field_list
, &field_cnt
);
1060 ret
= check_field_requirements(field_list
, field_cnt
,
1061 &tid_check
, &pid_check
, &procname_check
,
1064 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1065 &field_list
, &field_cnt
);
1066 ret
= check_field_requirements(field_list
, field_cnt
,
1067 &tid_check
, &pid_check
, &procname_check
,
1070 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1071 &field_list
, &field_cnt
);
1072 ret
= check_field_requirements(field_list
, field_cnt
,
1073 &tid_check
, &pid_check
, &procname_check
,
1077 if (tid_check
== 0) {
1079 fprintf(stderr
, "[error] missing tid context information\n");
1081 if (pid_check
== 0) {
1083 fprintf(stderr
, "[error] missing pid context information\n");
1085 if (ppid_check
== 0) {
1087 fprintf(stderr
, "[error] missing ppid context information\n");
1089 if (procname_check
== 0) {
1091 fprintf(stderr
, "[error] missing procname context information\n");
1098 static void handle_sigchild(int signal
)
1102 waitpid(opt_exec_pid
, &status
, 0);
1105 int main(int argc
, char **argv
, char **envp
)
1108 struct bt_context
*bt_ctx
= NULL
;
1110 //babeltrace_verbose = 1;
1112 ret
= parse_options(argc
, argv
);
1114 fprintf(stdout
, "Error parsing options.\n\n");
1117 } else if (ret
> 0) {
1121 if (opt_exec_name
) {
1122 opt_exec_env
= envp
;
1123 signal(SIGCHLD
, handle_sigchild
);
1126 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1128 #ifdef LTTNGTOP_MMAP_LIVE
1130 signal(SIGTERM
, handle_textdump_sigterm
);
1131 signal(SIGINT
, handle_textdump_sigterm
);
1133 mmap_live_loop(bt_ctx
);
1134 pthread_join(timer_thread
, NULL
);
1136 pthread_join(display_thread
, NULL
);
1138 lttng_stop_tracing("test");
1139 lttng_destroy_session("test");
1143 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1144 "trace directory or -r <relayd hostname/IP>\n");
1148 #endif /* LTTNGTOP_MMAP_LIVE */
1149 } else if (!opt_input_path
&& remote_live
) {
1151 bt_ctx
= bt_context_create();
1152 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1153 "lttng-live", NULL
);
1155 fprintf(stderr
, "[error] Opening the trace\n");
1161 bt_ctx
= bt_context_create();
1162 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1164 fprintf(stderr
, "[error] Opening the trace\n");
1168 ret
= check_requirements(bt_ctx
);
1170 fprintf(stderr
, "[error] some mandatory contexts "
1171 "were missing, exiting.\n");
1175 if (!opt_textdump
) {
1176 pthread_create(&display_thread
, NULL
, ncurses_display
,
1178 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1186 pthread_join(display_thread
, NULL
);
1188 pthread_join(timer_thread
, NULL
);
1194 bt_context_put(bt_ctx
);