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
, const char *procname
,
186 const struct bt_definition
*const * list
;
187 const struct bt_declaration
*l
;
188 const struct bt_definition
*scope
;
189 enum ctf_type_id type
;
191 struct processtop
*current_proc
;
192 struct files
*current_file
;
193 int fd
, fd_value
= -1;
195 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
197 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
198 for (i
= 0; i
< cnt
; i
++) {
201 printf("%s = ", bt_ctf_field_name(list
[i
]));
202 l
= bt_ctf_get_decl_from_def(list
[i
]);
203 if (strncmp(bt_ctf_field_name(list
[i
]), "fd", 2) == 0)
207 type
= bt_ctf_field_type(l
);
208 if (type
== CTF_TYPE_INTEGER
) {
209 if (bt_ctf_get_int_signedness(l
) == 0) {
210 fd_value
= bt_ctf_get_uint64(list
[i
]);
211 printf("%" PRIu64
, bt_ctf_get_uint64(list
[i
]));
213 fd_value
= bt_ctf_get_int64(list
[i
]);
214 printf("%" PRId64
, bt_ctf_get_int64(list
[i
]));
216 } else if (type
== CTF_TYPE_STRING
) {
217 printf("%s", bt_ctf_get_string(list
[i
]));
218 } else if (type
== CTF_TYPE_ARRAY
) {
219 str
= bt_ctf_get_char_array(list
[i
]);
220 if (!bt_ctf_field_get_error() && str
)
224 current_proc
= find_process_tid(<tngtop
, pid
, procname
);
227 current_file
= get_file(current_proc
, fd_value
);
228 if (!current_file
|| !current_file
->name
)
230 printf("<%s>", current_file
->name
);
236 * hook on each event to check the timestamp and refresh the display if
239 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
241 unsigned long timestamp
;
244 uint64_t ts_nsec_start
;
245 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
246 const struct bt_definition
*scope
;
247 const char *hostname
, *procname
;
249 char *from_syscall
= NULL
;
251 timestamp
= bt_ctf_get_timestamp(call_data
);
253 /* can happen in network live when tracing is idle */
254 if (timestamp
< last_event_ts
)
257 last_event_ts
= timestamp
;
259 start
= format_timestamp(timestamp
);
260 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
262 pid
= get_context_pid(call_data
);
263 if (pid
== -1ULL && opt_tid
) {
267 tid
= get_context_tid(call_data
);
269 hostname
= get_context_hostname(call_data
);
274 if (opt_tid
|| opt_hostname
|| opt_exec_name
) {
275 if (!lookup_filter_tid_list(lookup
)) {
276 /* To display when a process of ours in getting scheduled in */
277 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
280 scope
= bt_ctf_get_top_level_scope(call_data
,
282 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
283 scope
, "_next_tid"));
284 if (bt_ctf_field_get_error()) {
285 fprintf(stderr
, "Missing next_tid field\n");
288 if (!lookup_filter_tid_list(next_tid
)) {
295 } else if (!opt_all
) {
304 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
305 "exit_syscall", 12)) != 0) {
307 printf(" ...interrupted...\n");
310 cpu_id
= get_cpu_id(call_data
);
311 procname
= get_context_comm(call_data
);
312 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
313 cpu
= get_cpu(cpu_id
);
314 cpu
->current_syscall
= g_new0(struct syscall
, 1);
315 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
316 cpu
->current_syscall
->ts_start
= timestamp
;
317 cpu
->current_syscall
->cpu_id
= cpu_id
;
318 last_syscall
= cpu
->current_syscall
;
320 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
323 /* Return code of a syscall if it was the last displayed event. */
324 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
325 if (last_syscall
->cpu_id
== cpu_id
) {
328 delta
= timestamp
- last_syscall
->ts_start
;
329 scope
= bt_ctf_get_top_level_scope(call_data
,
331 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
334 printf("= %" PRId64
" (%" PRIu64
".%09" PRIu64
"s)\n",
335 syscall_ret
, delta
/ NSEC_PER_SEC
,
336 delta
% NSEC_PER_SEC
);
341 printf(" ...interrupted...\n");
345 cpu
= get_cpu(cpu_id
);
346 if (cpu
->current_syscall
) {
347 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
348 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
349 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
350 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
351 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
352 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
353 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
354 cpu_id
, cpu
->current_syscall
->name
);
358 free(cpu
->current_syscall
->name
);
359 g_free(cpu
->current_syscall
);
360 cpu
->current_syscall
= NULL
;
367 delta
= timestamp
- prev_ts
;
370 printf("%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
371 "(cpu %d) [%s (%d/%d)] %s (",
372 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
373 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
374 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
375 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
376 bt_ctf_event_name(call_data
));
377 print_fields(call_data
, procname
, pid
);
378 printf(")%s%c", (from_syscall
) ? from_syscall
: "",
379 (!current_syscall
) ? '\n' : ' ');
382 if (opt_all
&& (opt_tid
|| opt_hostname
|| opt_exec_name
))
388 return BT_CB_ERROR_STOP
;
390 return BT_CB_OK_STOP
;
393 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
396 struct kprobes
*kprobe
;
399 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
400 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
401 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
410 * hook on each event to check the timestamp and refresh the display if
413 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
415 unsigned long timestamp
;
417 timestamp
= bt_ctf_get_timestamp(call_data
);
418 if (timestamp
== -1ULL)
421 /* can happen in network live when tracing is idle */
422 if (timestamp
< last_event_ts
)
425 last_event_ts
= timestamp
;
427 if (last_display_update
== 0)
428 last_display_update
= timestamp
;
430 if (timestamp
- last_display_update
>= refresh_display
) {
431 sem_wait(&goodtoupdate
);
432 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
434 sem_post(&goodtodisplay
);
435 sem_post(&bootstrap
);
436 last_display_update
= timestamp
;
441 fprintf(stderr
, "check_timestamp callback error\n");
442 return BT_CB_ERROR_STOP
;
445 return BT_CB_OK_STOP
;
449 * get_perf_counter : get or create and return a perf_counter struct for
450 * either a process or a cpu (only one of the 2 parameters mandatory)
452 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
455 struct perfcounter
*ret
;
465 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
469 ret
= g_new0(struct perfcounter
, 1);
470 /* by default, make it visible in the UI */
472 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
481 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
482 const char *name
, int value
)
484 struct perfcounter
*cpu_perf
, *process_perf
;
486 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
487 if (cpu_perf
->count
< value
) {
488 process_perf
= get_perf_counter(name
, proc
, NULL
);
489 process_perf
->count
+= value
- cpu_perf
->count
;
490 cpu_perf
->count
= value
;
494 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
495 const struct bt_definition
*scope
,
496 struct processtop
*proc
,
499 struct bt_definition
const * const *list
= NULL
;
500 const struct bt_definition
*field
;
502 struct perfcounter
*perfcounter
;
510 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
517 g_hash_table_iter_init(&iter
, global_perf_liszt
);
518 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
519 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
521 int value
= bt_ctf_get_uint64(field
);
522 if (bt_ctf_field_get_error())
524 update_perf_value(proc
, cpu
, (char *) key
, value
);
532 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
535 const struct bt_definition
*scope
;
537 cpu
= get_cpu(get_cpu_id(event
));
539 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
540 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
542 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
543 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
545 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
546 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
549 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
552 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
553 char *comm
, *hostname
;
554 struct processtop
*parent
, *child
;
555 unsigned long timestamp
;
557 timestamp
= bt_ctf_get_timestamp(call_data
);
558 if (timestamp
== -1ULL)
561 pid
= get_context_pid(call_data
);
566 tid
= get_context_tid(call_data
);
570 ppid
= get_context_ppid(call_data
);
574 vpid
= get_context_vpid(call_data
);
578 vtid
= get_context_vtid(call_data
);
582 vppid
= get_context_vppid(call_data
);
586 comm
= get_context_comm(call_data
);
591 hostname
= get_context_hostname(call_data
);
593 /* find or create the current process */
594 child
= find_process_tid(<tngtop
, tid
, comm
);
596 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
599 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
602 /* find or create the parent */
603 parent
= find_process_tid(<tngtop
, pid
, comm
);
605 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
610 /* attach the parent to the current process */
611 child
->threadparent
= parent
;
612 add_thread(parent
, child
);
615 update_perf_counter(child
, call_data
);
621 return BT_CB_ERROR_STOP
;
626 copies
= g_ptr_array_new();
627 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
628 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
629 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
630 tid_filter_list
= g_hash_table_new(g_str_hash
,
633 sem_init(&goodtodisplay
, 0, 0);
634 sem_init(&goodtoupdate
, 0, 1);
635 sem_init(&timer
, 0, 1);
636 sem_init(&bootstrap
, 0, 0);
637 sem_init(&pause_sem
, 0, 1);
638 sem_init(&end_trace_sem
, 0, 0);
640 reset_global_counters();
642 lttngtop
.nbthreads
= 0;
643 lttngtop
.nbfiles
= 0;
645 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
647 lttngtop
.process_table
= g_ptr_array_new();
648 lttngtop
.files_table
= g_ptr_array_new();
649 lttngtop
.cpu_table
= g_ptr_array_new();
656 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
657 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
658 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
659 fprintf(fp
, " -h, --help This help message\n");
660 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
661 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
662 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
663 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
664 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");
665 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
666 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
667 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
671 * Parse probe options.
672 * Shamelessly stolen from lttng-tools :
673 * src/bin/lttng/commands/enable_events.c
675 static struct kprobes
*parse_probe_opts(char *opt
)
678 char name
[LTTNG_SYMBOL_NAME_LEN
];
679 struct kprobes
*kprobe
;
683 kprobe->probe_addr = 0;
684 kprobe->probe_offset = 0;
685 asprintf(&kprobe->probe_name, "probe_sys_open");
686 asprintf(&kprobe->symbol_name, "sys_open");
694 kprobe
= g_new0(struct kprobes
, 1);
696 /* Check for symbol+offset */
697 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
699 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
700 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
702 if (strlen(s_hex
) == 0) {
703 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
707 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
708 kprobe
->probe_addr
= 0;
712 /* Check for symbol */
713 if (isalpha(name
[0])) {
714 ret
= sscanf(opt
, "%s", name
);
716 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
717 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
718 kprobe
->probe_offset
= 0;
719 kprobe
->probe_addr
= 0;
724 /* Check for address */
725 ret
= sscanf(opt
, "%s", s_hex
);
727 if (strlen(s_hex
) == 0) {
728 fprintf(stderr
, "Invalid probe address %s", s_hex
);
732 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
733 kprobe
->probe_offset
= 0;
734 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
746 * Return 0 if caller should continue, < 0 if caller should return
747 * error, > 0 if caller should exit without reporting error.
749 static int parse_options(int argc
, char **argv
)
759 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
760 poptReadDefaultConfig(pc
, 0);
762 while ((opt
= poptGetNextOpt(pc
)) != -1) {
766 ret
= 1; /* exit cleanly */
779 tmp_str
= strtok(opt_tid
, ",");
781 tid
= malloc(sizeof(int));
782 *tid
= atoi(tmp_str
);
783 g_hash_table_insert(tid_filter_list
,
784 (gpointer
) tid
, tid
);
785 tmp_str
= strtok(NULL
, ",");
789 /* start reading the live trace from the beginning */
794 tmp_str
= strtok(opt_hostname
, ",");
796 add_hostname_list(tmp_str
, 1);
797 tmp_str
= strtok(NULL
, ",");
800 case OPT_RELAY_HOSTNAME
:
804 lttngtop
.kprobes_table
= g_ptr_array_new();
805 tmp_str
= strtok(opt_kprobes
, ",");
807 struct kprobes
*kprobe
;
809 kprobe
= parse_probe_opts(tmp_str
);
812 lttngtop
.kprobes_table
,
818 tmp_str
= strtok(NULL
, ",");
827 opt_exec_name
= NULL
;
828 opt_exec_argv
= NULL
;
829 for (i
= 0; i
< argc
; i
++) {
830 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
831 opt_exec_name
= argv
[i
+ 1];
832 opt_exec_argv
= &argv
[i
+ 1];
836 if (!opt_exec_name
) {
837 opt_input_path
= poptGetArg(pc
);
847 void iter_trace(struct bt_context
*bt_ctx
)
849 struct bt_ctf_iter
*iter
;
850 struct bt_iter_pos begin_pos
;
851 struct kprobes
*kprobe
;
852 const struct bt_ctf_event
*event
;
856 begin_pos
.type
= BT_SEEK_BEGIN
;
857 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
859 /* at each event, verify the status of the process table */
860 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
863 /* to handle the follow child option */
864 bt_ctf_iter_add_callback(iter
,
865 g_quark_from_static_string("sched_process_fork"),
866 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
867 /* to clean up the process table */
868 bt_ctf_iter_add_callback(iter
,
869 g_quark_from_static_string("sched_process_free"),
870 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
871 /* to get all the process from the statedumps */
872 bt_ctf_iter_add_callback(iter
,
873 g_quark_from_static_string(
874 "lttng_statedump_process_state"),
875 NULL
, 0, handle_statedump_process_state
,
877 bt_ctf_iter_add_callback(iter
,
878 g_quark_from_static_string(
879 "lttng_statedump_file_descriptor"),
880 NULL
, 0, handle_statedump_file_descriptor
,
882 bt_ctf_iter_add_callback(iter
,
883 g_quark_from_static_string("sys_open"),
884 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
885 bt_ctf_iter_add_callback(iter
,
886 g_quark_from_static_string("sys_close"),
887 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
888 bt_ctf_iter_add_callback(iter
,
889 g_quark_from_static_string("exit_syscall"),
890 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
892 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
896 /* at each event check if we need to refresh */
897 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
900 /* to handle the scheduling events */
901 bt_ctf_iter_add_callback(iter
,
902 g_quark_from_static_string("sched_switch"),
903 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
905 bt_ctf_iter_add_callback(iter
,
906 g_quark_from_static_string("sys_write"),
907 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
908 bt_ctf_iter_add_callback(iter
,
909 g_quark_from_static_string("sys_read"),
910 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
913 if (lttngtop
.kprobes_table
) {
914 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
915 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
916 bt_ctf_iter_add_callback(iter
,
917 g_quark_from_static_string(
919 NULL
, 0, handle_kprobes
,
930 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
932 } else if (pid
> 0) {
934 g_hash_table_insert(tid_filter_list
,
943 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
944 if (quit
|| reload_trace
)
946 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
951 /* block until quit, we reached the end of the trace */
952 sem_wait(&end_trace_sem
);
955 bt_ctf_iter_destroy(iter
);
959 * bt_context_add_traces_recursive: Open a trace recursively
960 * (copied from BSD code in converter/babeltrace.c)
962 * Find each trace present in the subdirectory starting from the given
963 * path, and add them to the context. The packet_seek parameter can be
964 * NULL: this specify to use the default format packet_seek.
966 * Return: 0 on success, nonzero on failure.
967 * Unable to open toplevel: failure.
968 * Unable to open some subdirectory or file: warn and continue;
970 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
971 const char *format_str
,
972 void (*packet_seek
)(struct bt_stream_pos
*pos
,
973 size_t offset
, int whence
))
978 char lpath
[PATH_MAX
];
979 char * const paths
[2] = { lpath
, NULL
};
982 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
983 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
984 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
985 ret
= bt_context_add_trace(ctx
,
986 path
, format_str
, packet_seek
, NULL
, NULL
);
988 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
989 "for reading.\n", path
);
990 /* Allow to skip erroneous traces. */
991 ret
= 1; /* partial error */
996 * Need to copy path, because fts_open can change it.
997 * It is the pointer array, not the strings, that are constant.
999 strncpy(lpath
, path
, PATH_MAX
);
1000 lpath
[PATH_MAX
- 1] = '\0';
1002 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
1004 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
1009 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
1011 while ((node
= fts_read(tree
))) {
1014 if (!(node
->fts_info
& FTS_D
))
1017 dirfd
= open(node
->fts_accpath
, 0);
1019 fprintf(stderr
, "[error] [Context] Unable to open trace "
1020 "directory file descriptor.\n");
1024 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1031 ret
= close(metafd
);
1042 trace_id
= bt_context_add_trace(ctx
,
1043 node
->fts_accpath
, format_str
,
1044 packet_seek
, NULL
, NULL
);
1046 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1047 "for reading.\n", node
->fts_accpath
, path
);
1048 /* Allow to skip erroneous traces. */
1051 g_array_append_val(trace_ids
, trace_id
);
1055 g_array_free(trace_ids
, TRUE
);
1062 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1063 int field_cnt
, int *tid_check
, int *pid_check
,
1064 int *procname_check
, int *ppid_check
)
1067 struct perfcounter
*global
;
1070 for (j
= 0; j
< field_cnt
; j
++) {
1071 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1072 if (*tid_check
== 0) {
1073 if (strncmp(name
, "tid", 3) == 0)
1076 if (*pid_check
== 0) {
1077 if (strncmp(name
, "pid", 3) == 0)
1080 if (*ppid_check
== 0) {
1081 if (strncmp(name
, "ppid", 4) == 0)
1084 if (*procname_check
== 0) {
1085 if (strncmp(name
, "procname", 8) == 0)
1086 (*procname_check
)++;
1088 if (strncmp(name
, "perf_", 5) == 0) {
1089 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1091 global
= g_new0(struct perfcounter
, 1);
1092 /* by default, sort on the first perf context */
1093 if (g_hash_table_size(global_perf_liszt
) == 0)
1095 global
->visible
= 1;
1096 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1101 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1102 *procname_check
== 1)
1109 * check_requirements: check if the required context informations are available
1111 * If each mandatory context information is available for at least in one
1112 * event, return 0 otherwise return -1.
1114 int check_requirements(struct bt_context
*ctx
)
1116 unsigned int i
, evt_cnt
, field_cnt
;
1117 struct bt_ctf_event_decl
*const * evt_list
;
1118 const struct bt_ctf_field_decl
*const * field_list
;
1121 int procname_check
= 0;
1125 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1130 for (i
= 0; i
< evt_cnt
; i
++) {
1131 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1132 &field_list
, &field_cnt
);
1133 ret
= check_field_requirements(field_list
, field_cnt
,
1134 &tid_check
, &pid_check
, &procname_check
,
1137 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1138 &field_list
, &field_cnt
);
1139 ret
= check_field_requirements(field_list
, field_cnt
,
1140 &tid_check
, &pid_check
, &procname_check
,
1143 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1144 &field_list
, &field_cnt
);
1145 ret
= check_field_requirements(field_list
, field_cnt
,
1146 &tid_check
, &pid_check
, &procname_check
,
1150 if (tid_check
== 0) {
1152 fprintf(stderr
, "[error] missing tid context information\n");
1154 if (pid_check
== 0) {
1156 fprintf(stderr
, "[error] missing pid context information\n");
1158 if (ppid_check
== 0) {
1160 fprintf(stderr
, "[error] missing ppid context information\n");
1162 if (procname_check
== 0) {
1164 fprintf(stderr
, "[error] missing procname context information\n");
1171 static void handle_sigchild(int signal
)
1175 waitpid(opt_exec_pid
, &status
, 0);
1178 int main(int argc
, char **argv
, char **envp
)
1181 struct bt_context
*bt_ctx
= NULL
;
1183 //babeltrace_verbose = 1;
1185 ret
= parse_options(argc
, argv
);
1187 fprintf(stdout
, "Error parsing options.\n\n");
1190 } else if (ret
> 0) {
1194 if (opt_exec_name
) {
1195 opt_exec_env
= envp
;
1196 signal(SIGCHLD
, handle_sigchild
);
1199 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1201 #ifdef LTTNGTOP_MMAP_LIVE
1203 signal(SIGTERM
, handle_textdump_sigterm
);
1204 signal(SIGINT
, handle_textdump_sigterm
);
1206 mmap_live_loop(bt_ctx
);
1207 pthread_join(timer_thread
, NULL
);
1209 pthread_join(display_thread
, NULL
);
1211 lttng_stop_tracing("test");
1212 lttng_destroy_session("test");
1216 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1217 "trace directory or -r <relayd hostname/IP>\n");
1221 #endif /* LTTNGTOP_MMAP_LIVE */
1222 } else if (!opt_input_path
&& remote_live
) {
1224 bt_ctx
= bt_context_create();
1225 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1226 "lttng-live", NULL
);
1228 fprintf(stderr
, "[error] Opening the trace\n");
1234 bt_ctx
= bt_context_create();
1235 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1237 fprintf(stderr
, "[error] Opening the trace\n");
1241 ret
= check_requirements(bt_ctx
);
1243 fprintf(stderr
, "[error] some mandatory contexts "
1244 "were missing, exiting.\n");
1248 if (!opt_textdump
) {
1249 pthread_create(&display_thread
, NULL
, ncurses_display
,
1251 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1259 pthread_join(display_thread
, NULL
);
1261 pthread_join(timer_thread
, NULL
);
1267 bt_context_put(bt_ctx
);