2 * Copyright (C) 2013 Julien Desfossez
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/babeltrace.h>
23 #include <babeltrace/ctf/events.h>
24 #include <babeltrace/ctf/callbacks.h>
25 #include <babeltrace/ctf/iterator.h>
37 #include <sys/types.h>
42 #include <lttng/lttng.h>
43 #ifdef LTTNGTOP_MMAP_LIVE
44 #include <lttng/lttngtop-helper.h>
45 #include <babeltrace/lttngtopmmappacketseek.h>
46 #include "mmap-live.h"
47 #endif /* LTTNGTOP_MMAP_LIVE */
49 #include "lttngtoptypes.h"
51 #include "iostreamtop.h"
52 #include "cursesdisplay.h"
54 #include "network-live.h"
56 #define NET_URL_PREFIX "net://"
57 #define NET4_URL_PREFIX "net4://"
58 #define NET6_URL_PREFIX "net6://"
60 #define DEFAULT_FILE_ARRAY_SIZE 1
62 const char *opt_input_path
;
69 /* We need at least one valid trace to start processing. */
72 struct lttngtop
*copy
;
73 pthread_t display_thread
;
74 pthread_t timer_thread
;
76 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
77 unsigned long last_display_update
= 0;
78 unsigned long last_event_ts
= 0;
79 struct syscall
*last_syscall
;
81 /* list of FDs available for being read with snapshots */
82 struct bt_mmap_stream_list mmap_list
;
83 GPtrArray
*lttng_consumer_stream_array
;
84 int sessiond_metadata
, consumerd_metadata
;
85 struct lttng_consumer_local_data
*ctx
= NULL
;
86 /* list of snapshots currently not consumed */
87 GPtrArray
*available_snapshots
;
88 sem_t metadata_available
;
106 static struct poptOption long_options
[] = {
107 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
108 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
109 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
110 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
111 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
112 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
113 { "hostname", 'n', POPT_ARG_STRING
, &opt_hostname
, OPT_HOSTNAME
, NULL
, NULL
},
114 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
115 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
116 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
117 { "all", 'a', POPT_ARG_NONE
, NULL
, OPT_ALL
, NULL
, NULL
},
118 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
121 #ifdef LTTNGTOP_MMAP_LIVE
122 static void handle_textdump_sigterm(int signal
)
125 lttng_destroy_session("test");
129 void *refresh_thread(void *p
)
133 sem_post(&pause_sem
);
135 sem_post(&end_trace_sem
);
136 sem_post(&goodtodisplay
);
137 sem_post(&goodtoupdate
);
140 if (!opt_input_path
) {
141 #ifdef LTTNGTOP_MMAP_LIVE
142 mmap_live_flush(mmap_list
);
145 sem_wait(&pause_sem
);
146 sem_post(&pause_sem
);
148 sleep(refresh_display
/NSEC_PER_SEC
);
152 void *ncurses_display(void *p
)
154 unsigned int current_display_index
= 0;
156 sem_wait(&bootstrap
);
158 * Prevent the 1 second delay when we hit ESC
165 sem_wait(&goodtodisplay
);
166 sem_wait(&pause_sem
);
169 sem_post(&pause_sem
);
175 copy
= g_ptr_array_index(copies
, current_display_index
);
177 display(current_display_index
++);
179 sem_post(&goodtoupdate
);
180 sem_post(&pause_sem
);
184 void print_fields(struct bt_ctf_event
*event
, const char *procname
,
188 const struct bt_definition
*const * list
;
189 const struct bt_declaration
*l
;
190 const struct bt_definition
*scope
;
191 enum ctf_type_id type
;
193 struct processtop
*current_proc
;
194 struct files
*current_file
;
195 int fd
, fd_value
= -1;
197 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
199 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
200 for (i
= 0; i
< cnt
; i
++) {
203 printf("%s = ", bt_ctf_field_name(list
[i
]));
204 l
= bt_ctf_get_decl_from_def(list
[i
]);
205 if (strncmp(bt_ctf_field_name(list
[i
]), "fd", 2) == 0)
209 type
= bt_ctf_field_type(l
);
210 if (type
== CTF_TYPE_INTEGER
) {
211 if (bt_ctf_get_int_signedness(l
) == 0) {
212 fd_value
= bt_ctf_get_uint64(list
[i
]);
213 printf("%" PRIu64
, bt_ctf_get_uint64(list
[i
]));
215 fd_value
= bt_ctf_get_int64(list
[i
]);
216 printf("%" PRId64
, bt_ctf_get_int64(list
[i
]));
218 } else if (type
== CTF_TYPE_STRING
) {
219 printf("%s", bt_ctf_get_string(list
[i
]));
220 } else if (type
== CTF_TYPE_ARRAY
) {
221 str
= bt_ctf_get_char_array(list
[i
]);
222 if (!bt_ctf_field_get_error() && str
)
226 current_proc
= find_process_tid(<tngtop
, pid
, procname
);
229 current_file
= get_file(current_proc
, fd_value
);
230 if (!current_file
|| !current_file
->name
)
232 printf("<%s>", current_file
->name
);
238 * hook on each event to check the timestamp and refresh the display if
241 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
243 unsigned long timestamp
;
246 uint64_t ts_nsec_start
;
247 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
248 const struct bt_definition
*scope
;
249 const char *hostname
, *procname
;
251 char *from_syscall
= NULL
;
253 timestamp
= bt_ctf_get_timestamp(call_data
);
255 /* can happen in network live when tracing is idle */
256 if (timestamp
< last_event_ts
)
259 last_event_ts
= timestamp
;
261 start
= format_timestamp(timestamp
);
262 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
264 pid
= get_context_pid(call_data
);
265 if (pid
== -1ULL && opt_tid
) {
269 tid
= get_context_tid(call_data
);
271 hostname
= get_context_hostname(call_data
);
276 if (opt_tid
|| opt_hostname
|| opt_exec_name
) {
277 if (!lookup_filter_tid_list(lookup
)) {
278 /* To display when a process of ours in getting scheduled in */
279 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
282 scope
= bt_ctf_get_top_level_scope(call_data
,
284 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
285 scope
, "_next_tid"));
286 if (bt_ctf_field_get_error()) {
287 fprintf(stderr
, "Missing next_tid field\n");
290 if (!lookup_filter_tid_list(next_tid
)) {
297 } else if (!opt_all
) {
306 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
307 "exit_syscall", 12)) != 0) {
309 printf(" ...interrupted...\n");
312 cpu_id
= get_cpu_id(call_data
);
313 procname
= get_context_comm(call_data
);
314 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
315 cpu
= get_cpu(cpu_id
);
316 cpu
->current_syscall
= g_new0(struct syscall
, 1);
317 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
318 cpu
->current_syscall
->ts_start
= timestamp
;
319 cpu
->current_syscall
->cpu_id
= cpu_id
;
320 last_syscall
= cpu
->current_syscall
;
322 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
325 /* Return code of a syscall if it was the last displayed event. */
326 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
327 if (last_syscall
->cpu_id
== cpu_id
) {
330 delta
= timestamp
- last_syscall
->ts_start
;
331 scope
= bt_ctf_get_top_level_scope(call_data
,
333 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
336 printf("= %" PRId64
" (%" PRIu64
".%09" PRIu64
"s)\n",
337 syscall_ret
, delta
/ NSEC_PER_SEC
,
338 delta
% NSEC_PER_SEC
);
343 printf(" ...interrupted...\n");
347 cpu
= get_cpu(cpu_id
);
348 if (cpu
->current_syscall
) {
349 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
350 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
351 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
352 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
353 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
354 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
355 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
356 cpu_id
, cpu
->current_syscall
->name
);
360 free(cpu
->current_syscall
->name
);
361 g_free(cpu
->current_syscall
);
362 cpu
->current_syscall
= NULL
;
369 delta
= timestamp
- prev_ts
;
372 printf("%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
373 "(cpu %d) [%s (%d/%d)] %s (",
374 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
375 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
376 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
377 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
378 bt_ctf_event_name(call_data
));
379 print_fields(call_data
, procname
, pid
);
380 printf(")%s%c", (from_syscall
) ? from_syscall
: "",
381 (!current_syscall
) ? '\n' : ' ');
384 if (opt_all
&& (opt_tid
|| opt_hostname
|| opt_exec_name
))
390 return BT_CB_ERROR_STOP
;
392 return BT_CB_OK_STOP
;
395 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
398 struct kprobes
*kprobe
;
401 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
402 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
403 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
412 * hook on each event to check the timestamp and refresh the display if
415 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
417 unsigned long timestamp
;
419 timestamp
= bt_ctf_get_timestamp(call_data
);
420 if (timestamp
== -1ULL)
423 /* can happen in network live when tracing is idle */
424 if (timestamp
< last_event_ts
)
427 last_event_ts
= timestamp
;
429 if (last_display_update
== 0)
430 last_display_update
= timestamp
;
432 if (timestamp
- last_display_update
>= refresh_display
) {
433 sem_wait(&goodtoupdate
);
434 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
436 sem_post(&goodtodisplay
);
437 sem_post(&bootstrap
);
438 last_display_update
= timestamp
;
443 fprintf(stderr
, "check_timestamp callback error\n");
444 return BT_CB_ERROR_STOP
;
447 return BT_CB_OK_STOP
;
451 * get_perf_counter : get or create and return a perf_counter struct for
452 * either a process or a cpu (only one of the 2 parameters mandatory)
454 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
457 struct perfcounter
*ret
;
467 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
471 ret
= g_new0(struct perfcounter
, 1);
472 /* by default, make it visible in the UI */
474 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
483 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
484 const char *name
, int value
)
486 struct perfcounter
*cpu_perf
, *process_perf
;
488 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
489 if (cpu_perf
->count
< value
) {
490 process_perf
= get_perf_counter(name
, proc
, NULL
);
491 process_perf
->count
+= value
- cpu_perf
->count
;
492 cpu_perf
->count
= value
;
496 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
497 const struct bt_definition
*scope
,
498 struct processtop
*proc
,
501 struct bt_definition
const * const *list
= NULL
;
502 const struct bt_definition
*field
;
504 struct perfcounter
*perfcounter
;
512 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
519 g_hash_table_iter_init(&iter
, global_perf_liszt
);
520 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
521 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
523 int value
= bt_ctf_get_uint64(field
);
524 if (bt_ctf_field_get_error())
526 update_perf_value(proc
, cpu
, (char *) key
, value
);
534 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
537 const struct bt_definition
*scope
;
539 cpu
= get_cpu(get_cpu_id(event
));
541 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
542 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
544 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
545 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
547 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
548 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
551 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
554 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
555 char *comm
, *hostname
;
556 struct processtop
*parent
, *child
;
557 unsigned long timestamp
;
559 timestamp
= bt_ctf_get_timestamp(call_data
);
560 if (timestamp
== -1ULL)
563 pid
= get_context_pid(call_data
);
568 tid
= get_context_tid(call_data
);
572 ppid
= get_context_ppid(call_data
);
576 vpid
= get_context_vpid(call_data
);
580 vtid
= get_context_vtid(call_data
);
584 vppid
= get_context_vppid(call_data
);
588 comm
= get_context_comm(call_data
);
593 hostname
= get_context_hostname(call_data
);
595 /* find or create the current process */
596 child
= find_process_tid(<tngtop
, tid
, comm
);
598 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
601 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
604 /* find or create the parent */
605 parent
= find_process_tid(<tngtop
, pid
, comm
);
607 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
612 /* attach the parent to the current process */
613 child
->threadparent
= parent
;
614 add_thread(parent
, child
);
617 update_perf_counter(child
, call_data
);
623 return BT_CB_ERROR_STOP
;
628 copies
= g_ptr_array_new();
629 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
630 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
631 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
632 tid_filter_list
= g_hash_table_new(g_str_hash
,
635 sem_init(&goodtodisplay
, 0, 0);
636 sem_init(&goodtoupdate
, 0, 1);
637 sem_init(&timer
, 0, 1);
638 sem_init(&bootstrap
, 0, 0);
639 sem_init(&pause_sem
, 0, 1);
640 sem_init(&end_trace_sem
, 0, 0);
642 reset_global_counters();
644 lttngtop
.nbthreads
= 0;
645 lttngtop
.nbfiles
= 0;
647 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
649 lttngtop
.process_table
= g_ptr_array_new();
650 lttngtop
.files_table
= g_ptr_array_new();
651 lttngtop
.cpu_table
= g_ptr_array_new();
658 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
659 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
660 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
661 fprintf(fp
, " -h, --help This help message\n");
662 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
663 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
664 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
665 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
666 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");
667 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
668 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
669 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
673 * Parse probe options.
674 * Shamelessly stolen from lttng-tools :
675 * src/bin/lttng/commands/enable_events.c
677 static struct kprobes
*parse_probe_opts(char *opt
)
680 char name
[LTTNG_SYMBOL_NAME_LEN
];
681 struct kprobes
*kprobe
;
685 kprobe->probe_addr = 0;
686 kprobe->probe_offset = 0;
687 asprintf(&kprobe->probe_name, "probe_sys_open");
688 asprintf(&kprobe->symbol_name, "sys_open");
696 kprobe
= g_new0(struct kprobes
, 1);
698 /* Check for symbol+offset */
699 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
701 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
702 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
704 if (strlen(s_hex
) == 0) {
705 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
709 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
710 kprobe
->probe_addr
= 0;
714 /* Check for symbol */
715 if (isalpha(name
[0])) {
716 ret
= sscanf(opt
, "%s", name
);
718 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
719 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
720 kprobe
->probe_offset
= 0;
721 kprobe
->probe_addr
= 0;
726 /* Check for address */
727 ret
= sscanf(opt
, "%s", s_hex
);
729 if (strlen(s_hex
) == 0) {
730 fprintf(stderr
, "Invalid probe address %s", s_hex
);
734 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
735 kprobe
->probe_offset
= 0;
736 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
748 * Return 0 if caller should continue, < 0 if caller should return
749 * error, > 0 if caller should exit without reporting error.
751 static int parse_options(int argc
, char **argv
)
761 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
762 poptReadDefaultConfig(pc
, 0);
764 while ((opt
= poptGetNextOpt(pc
)) != -1) {
768 ret
= 1; /* exit cleanly */
781 tmp_str
= strtok(opt_tid
, ",");
783 tid
= malloc(sizeof(int));
784 *tid
= atoi(tmp_str
);
785 g_hash_table_insert(tid_filter_list
,
786 (gpointer
) tid
, tid
);
787 tmp_str
= strtok(NULL
, ",");
791 /* start reading the live trace from the beginning */
796 tmp_str
= strtok(opt_hostname
, ",");
798 add_hostname_list(tmp_str
, 1);
799 tmp_str
= strtok(NULL
, ",");
802 case OPT_RELAY_HOSTNAME
:
806 lttngtop
.kprobes_table
= g_ptr_array_new();
807 tmp_str
= strtok(opt_kprobes
, ",");
809 struct kprobes
*kprobe
;
811 kprobe
= parse_probe_opts(tmp_str
);
814 lttngtop
.kprobes_table
,
820 tmp_str
= strtok(NULL
, ",");
829 opt_exec_name
= NULL
;
830 opt_exec_argv
= NULL
;
831 for (i
= 0; i
< argc
; i
++) {
832 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
833 opt_exec_name
= argv
[i
+ 1];
834 opt_exec_argv
= &argv
[i
+ 1];
838 if (!opt_exec_name
) {
839 opt_input_path
= poptGetArg(pc
);
849 void iter_trace(struct bt_context
*bt_ctx
)
851 struct bt_ctf_iter
*iter
;
852 struct bt_iter_pos begin_pos
;
853 struct kprobes
*kprobe
;
854 const struct bt_ctf_event
*event
;
858 begin_pos
.type
= BT_SEEK_BEGIN
;
859 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
861 /* at each event, verify the status of the process table */
862 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
865 /* to handle the follow child option */
866 bt_ctf_iter_add_callback(iter
,
867 g_quark_from_static_string("sched_process_fork"),
868 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
869 /* to clean up the process table */
870 bt_ctf_iter_add_callback(iter
,
871 g_quark_from_static_string("sched_process_free"),
872 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
873 /* to get all the process from the statedumps */
874 bt_ctf_iter_add_callback(iter
,
875 g_quark_from_static_string(
876 "lttng_statedump_process_state"),
877 NULL
, 0, handle_statedump_process_state
,
879 bt_ctf_iter_add_callback(iter
,
880 g_quark_from_static_string(
881 "lttng_statedump_file_descriptor"),
882 NULL
, 0, handle_statedump_file_descriptor
,
884 bt_ctf_iter_add_callback(iter
,
885 g_quark_from_static_string("sys_open"),
886 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
887 bt_ctf_iter_add_callback(iter
,
888 g_quark_from_static_string("sys_close"),
889 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
890 bt_ctf_iter_add_callback(iter
,
891 g_quark_from_static_string("exit_syscall"),
892 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
894 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
898 /* at each event check if we need to refresh */
899 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
902 /* to handle the scheduling events */
903 bt_ctf_iter_add_callback(iter
,
904 g_quark_from_static_string("sched_switch"),
905 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
907 bt_ctf_iter_add_callback(iter
,
908 g_quark_from_static_string("sys_write"),
909 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
910 bt_ctf_iter_add_callback(iter
,
911 g_quark_from_static_string("sys_read"),
912 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
915 if (lttngtop
.kprobes_table
) {
916 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
917 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
918 bt_ctf_iter_add_callback(iter
,
919 g_quark_from_static_string(
921 NULL
, 0, handle_kprobes
,
932 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
934 } else if (pid
> 0) {
936 g_hash_table_insert(tid_filter_list
,
945 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
946 if (quit
|| reload_trace
)
948 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
953 /* block until quit, we reached the end of the trace */
954 sem_wait(&end_trace_sem
);
957 bt_ctf_iter_destroy(iter
);
961 * bt_context_add_traces_recursive: Open a trace recursively
962 * (copied from BSD code in converter/babeltrace.c)
964 * Find each trace present in the subdirectory starting from the given
965 * path, and add them to the context. The packet_seek parameter can be
966 * NULL: this specify to use the default format packet_seek.
968 * Return: 0 on success, nonzero on failure.
969 * Unable to open toplevel: failure.
970 * Unable to open some subdirectory or file: warn and continue;
972 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
973 const char *format_str
,
974 void (*packet_seek
)(struct bt_stream_pos
*pos
,
975 size_t offset
, int whence
))
980 char lpath
[PATH_MAX
];
981 char * const paths
[2] = { lpath
, NULL
};
984 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
985 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
986 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
987 ret
= bt_context_add_trace(ctx
,
988 path
, format_str
, packet_seek
, NULL
, NULL
);
990 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
991 "for reading.\n", path
);
992 /* Allow to skip erroneous traces. */
993 ret
= 1; /* partial error */
998 * Need to copy path, because fts_open can change it.
999 * It is the pointer array, not the strings, that are constant.
1001 strncpy(lpath
, path
, PATH_MAX
);
1002 lpath
[PATH_MAX
- 1] = '\0';
1004 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
1006 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
1011 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
1013 while ((node
= fts_read(tree
))) {
1016 if (!(node
->fts_info
& FTS_D
))
1019 dirfd
= open(node
->fts_accpath
, 0);
1021 fprintf(stderr
, "[error] [Context] Unable to open trace "
1022 "directory file descriptor.\n");
1026 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1033 ret
= close(metafd
);
1044 trace_id
= bt_context_add_trace(ctx
,
1045 node
->fts_accpath
, format_str
,
1046 packet_seek
, NULL
, NULL
);
1048 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1049 "for reading.\n", node
->fts_accpath
, path
);
1050 /* Allow to skip erroneous traces. */
1053 g_array_append_val(trace_ids
, trace_id
);
1057 g_array_free(trace_ids
, TRUE
);
1064 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1065 int field_cnt
, int *tid_check
, int *pid_check
,
1066 int *procname_check
, int *ppid_check
)
1069 struct perfcounter
*global
;
1072 for (j
= 0; j
< field_cnt
; j
++) {
1073 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1074 if (*tid_check
== 0) {
1075 if (strncmp(name
, "tid", 3) == 0)
1078 if (*pid_check
== 0) {
1079 if (strncmp(name
, "pid", 3) == 0)
1082 if (*ppid_check
== 0) {
1083 if (strncmp(name
, "ppid", 4) == 0)
1086 if (*procname_check
== 0) {
1087 if (strncmp(name
, "procname", 8) == 0)
1088 (*procname_check
)++;
1090 if (strncmp(name
, "perf_", 5) == 0) {
1091 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1093 global
= g_new0(struct perfcounter
, 1);
1094 /* by default, sort on the first perf context */
1095 if (g_hash_table_size(global_perf_liszt
) == 0)
1097 global
->visible
= 1;
1098 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1103 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1104 *procname_check
== 1)
1111 * check_requirements: check if the required context informations are available
1113 * If each mandatory context information is available for at least in one
1114 * event, return 0 otherwise return -1.
1116 int check_requirements(struct bt_context
*ctx
)
1118 unsigned int i
, evt_cnt
, field_cnt
;
1119 struct bt_ctf_event_decl
*const * evt_list
;
1120 const struct bt_ctf_field_decl
*const * field_list
;
1123 int procname_check
= 0;
1127 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1132 for (i
= 0; i
< evt_cnt
; i
++) {
1133 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1134 &field_list
, &field_cnt
);
1135 ret
= check_field_requirements(field_list
, field_cnt
,
1136 &tid_check
, &pid_check
, &procname_check
,
1139 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1140 &field_list
, &field_cnt
);
1141 ret
= check_field_requirements(field_list
, field_cnt
,
1142 &tid_check
, &pid_check
, &procname_check
,
1145 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1146 &field_list
, &field_cnt
);
1147 ret
= check_field_requirements(field_list
, field_cnt
,
1148 &tid_check
, &pid_check
, &procname_check
,
1152 if (tid_check
== 0) {
1154 fprintf(stderr
, "[error] missing tid context information\n");
1156 if (pid_check
== 0) {
1158 fprintf(stderr
, "[error] missing pid context information\n");
1160 if (ppid_check
== 0) {
1162 fprintf(stderr
, "[error] missing ppid context information\n");
1164 if (procname_check
== 0) {
1166 fprintf(stderr
, "[error] missing procname context information\n");
1176 static void handle_sigchild(int signal
)
1180 waitpid(opt_exec_pid
, &status
, 0);
1183 int main(int argc
, char **argv
, char **envp
)
1186 struct bt_context
*bt_ctx
= NULL
;
1188 //babeltrace_verbose = 1;
1190 ret
= parse_options(argc
, argv
);
1192 fprintf(stdout
, "Error parsing options.\n\n");
1195 } else if (ret
> 0) {
1199 if (opt_exec_name
) {
1200 opt_exec_env
= envp
;
1201 signal(SIGCHLD
, handle_sigchild
);
1204 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1206 #ifdef LTTNGTOP_MMAP_LIVE
1208 signal(SIGTERM
, handle_textdump_sigterm
);
1209 signal(SIGINT
, handle_textdump_sigterm
);
1211 mmap_live_loop(bt_ctx
);
1212 pthread_join(timer_thread
, NULL
);
1214 pthread_join(display_thread
, NULL
);
1216 lttng_stop_tracing("test");
1217 lttng_destroy_session("test");
1221 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1222 "trace directory or -r <relayd hostname/IP>\n");
1226 #endif /* LTTNGTOP_MMAP_LIVE */
1227 } else if (!opt_input_path
&& remote_live
) {
1229 bt_ctx
= bt_context_create();
1230 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1231 "lttng-live", NULL
);
1233 fprintf(stderr
, "[error] Opening the trace\n");
1239 bt_ctx
= bt_context_create();
1240 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1242 fprintf(stderr
, "[error] Opening the trace\n");
1246 ret
= check_requirements(bt_ctx
);
1247 if (ret
< 0 && !valid_trace
) {
1248 fprintf(stderr
, "[error] some mandatory contexts "
1249 "were missing, exiting.\n");
1253 if (!opt_textdump
) {
1254 pthread_create(&display_thread
, NULL
, ncurses_display
,
1256 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1264 pthread_join(display_thread
, NULL
);
1266 pthread_join(timer_thread
, NULL
);
1272 bt_context_put(bt_ctx
);