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
;
71 struct lttngtop
*copy
;
72 pthread_t display_thread
;
73 pthread_t timer_thread
;
75 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
76 unsigned long last_display_update
= 0;
77 unsigned long last_event_ts
= 0;
78 struct syscall
*last_syscall
;
80 /* list of FDs available for being read with snapshots */
81 struct bt_mmap_stream_list mmap_list
;
82 GPtrArray
*lttng_consumer_stream_array
;
83 int sessiond_metadata
, consumerd_metadata
;
84 struct lttng_consumer_local_data
*ctx
= NULL
;
85 /* list of snapshots currently not consumed */
86 GPtrArray
*available_snapshots
;
87 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
, 'y', POPT_ARG_NONE
, NULL
, OPT_FD_PATH
, NULL
, NULL
},
119 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
122 #ifdef LTTNGTOP_MMAP_LIVE
123 static void handle_textdump_sigterm(int signal
)
126 lttng_destroy_session("test");
130 void *refresh_thread(void *p
)
134 sem_post(&pause_sem
);
136 sem_post(&end_trace_sem
);
137 sem_post(&goodtodisplay
);
138 sem_post(&goodtoupdate
);
141 if (!opt_input_path
) {
142 #ifdef LTTNGTOP_MMAP_LIVE
143 mmap_live_flush(mmap_list
);
146 sem_wait(&pause_sem
);
147 sem_post(&pause_sem
);
149 sleep(refresh_display
/NSEC_PER_SEC
);
153 void *ncurses_display(void *p
)
155 unsigned int current_display_index
= 0;
157 sem_wait(&bootstrap
);
159 * Prevent the 1 second delay when we hit ESC
166 sem_wait(&goodtodisplay
);
167 sem_wait(&pause_sem
);
170 sem_post(&pause_sem
);
176 copy
= g_ptr_array_index(copies
, current_display_index
);
178 display(current_display_index
++);
180 sem_post(&goodtoupdate
);
181 sem_post(&pause_sem
);
185 void print_fields(struct bt_ctf_event
*event
, const char *procname
,
189 const struct bt_definition
*const * list
;
190 const struct bt_declaration
*l
;
191 const struct bt_definition
*scope
;
192 enum ctf_type_id type
;
194 struct processtop
*current_proc
;
195 struct files
*current_file
;
196 int fd
, fd_value
= -1;
198 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
200 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
201 for (i
= 0; i
< cnt
; i
++) {
204 printf("%s = ", bt_ctf_field_name(list
[i
]));
205 l
= bt_ctf_get_decl_from_def(list
[i
]);
206 if (strncmp(bt_ctf_field_name(list
[i
]), "fd", 2) == 0)
210 type
= bt_ctf_field_type(l
);
211 if (type
== CTF_TYPE_INTEGER
) {
212 if (bt_ctf_get_int_signedness(l
) == 0) {
213 fd_value
= bt_ctf_get_uint64(list
[i
]);
214 printf("%" PRIu64
, bt_ctf_get_uint64(list
[i
]));
216 fd_value
= bt_ctf_get_int64(list
[i
]);
217 printf("%" PRId64
, bt_ctf_get_int64(list
[i
]));
219 } else if (type
== CTF_TYPE_STRING
) {
220 printf("%s", bt_ctf_get_string(list
[i
]));
221 } else if (type
== CTF_TYPE_ARRAY
) {
222 str
= bt_ctf_get_char_array(list
[i
]);
223 if (!bt_ctf_field_get_error() && str
)
227 current_proc
= find_process_tid(<tngtop
, pid
, procname
);
230 current_file
= get_file(current_proc
, fd_value
);
231 if (!current_file
|| !current_file
->name
)
233 printf("<%s>", current_file
->name
);
239 * hook on each event to check the timestamp and refresh the display if
242 enum bt_cb_ret
textdump(struct bt_ctf_event
*call_data
, void *private_data
)
244 unsigned long timestamp
;
247 uint64_t ts_nsec_start
;
248 int pid
, cpu_id
, tid
, ret
, lookup
, current_syscall
= 0;
249 const struct bt_definition
*scope
;
250 const char *hostname
, *procname
;
252 char *from_syscall
= NULL
;
254 timestamp
= bt_ctf_get_timestamp(call_data
);
256 /* can happen in network live when tracing is idle */
257 if (timestamp
< last_event_ts
)
260 last_event_ts
= timestamp
;
262 start
= format_timestamp(timestamp
);
263 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
265 pid
= get_context_pid(call_data
);
266 if (pid
== -1ULL && opt_tid
) {
270 tid
= get_context_tid(call_data
);
272 hostname
= get_context_hostname(call_data
);
277 if (opt_tid
|| opt_hostname
|| opt_exec_name
) {
278 if (!lookup_filter_tid_list(lookup
)) {
279 /* To display when a process of ours in getting scheduled in */
280 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
283 scope
= bt_ctf_get_top_level_scope(call_data
,
285 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
286 scope
, "_next_tid"));
287 if (bt_ctf_field_get_error()) {
288 fprintf(stderr
, "Missing next_tid field\n");
291 if (!lookup_filter_tid_list(next_tid
)) {
298 } else if (!opt_all
) {
307 if (last_syscall
&& (strncmp(bt_ctf_event_name(call_data
),
308 "exit_syscall", 12)) != 0) {
310 printf(" ...interrupted...\n");
313 cpu_id
= get_cpu_id(call_data
);
314 procname
= get_context_comm(call_data
);
315 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) == 0) {
316 cpu
= get_cpu(cpu_id
);
317 cpu
->current_syscall
= g_new0(struct syscall
, 1);
318 cpu
->current_syscall
->name
= strdup(bt_ctf_event_name(call_data
));
319 cpu
->current_syscall
->ts_start
= timestamp
;
320 cpu
->current_syscall
->cpu_id
= cpu_id
;
321 last_syscall
= cpu
->current_syscall
;
323 } else if ((strncmp(bt_ctf_event_name(call_data
), "exit_syscall", 12)) == 0) {
326 /* Return code of a syscall if it was the last displayed event. */
327 if (last_syscall
&& last_syscall
->ts_start
== prev_ts
) {
328 if (last_syscall
->cpu_id
== cpu_id
) {
331 delta
= timestamp
- last_syscall
->ts_start
;
332 scope
= bt_ctf_get_top_level_scope(call_data
,
334 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
337 printf("= %" PRId64
" (+%" PRIu64
".%09" PRIu64
")\n",
338 syscall_ret
, delta
/ NSEC_PER_SEC
,
339 delta
% NSEC_PER_SEC
);
344 printf(" ...interrupted...\n");
348 cpu
= get_cpu(cpu_id
);
349 if (cpu
->current_syscall
) {
350 delta
= timestamp
- cpu
->current_syscall
->ts_start
;
351 start_ts
= format_timestamp(cpu
->current_syscall
->ts_start
);
352 ret
= asprintf(&from_syscall
, " [from %02d:%02d:%02d.%09" PRIu64
353 " (+%" PRIu64
".%09" PRIu64
") (cpu %d) %s]",
354 start_ts
.tm_hour
, start_ts
.tm_min
, start_ts
.tm_sec
,
355 cpu
->current_syscall
->ts_start
% NSEC_PER_SEC
,
356 delta
/ NSEC_PER_SEC
, delta
% NSEC_PER_SEC
,
357 cpu_id
, cpu
->current_syscall
->name
);
361 free(cpu
->current_syscall
->name
);
362 g_free(cpu
->current_syscall
);
363 cpu
->current_syscall
= NULL
;
370 delta
= timestamp
- prev_ts
;
373 printf("%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") %s%s"
374 "(cpu %d) [%s (%d/%d)] %s (",
375 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
376 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
377 delta
% NSEC_PER_SEC
, (hostname
) ? hostname
: "",
378 (hostname
) ? " ": "", cpu_id
, procname
, pid
, tid
,
379 bt_ctf_event_name(call_data
));
380 print_fields(call_data
, procname
, pid
);
381 printf(")%s%c", (from_syscall
) ? from_syscall
: "",
382 (!current_syscall
) ? '\n' : ' ');
385 if (opt_all
&& (opt_tid
|| opt_hostname
|| opt_exec_name
))
391 return BT_CB_ERROR_STOP
;
393 return BT_CB_OK_STOP
;
396 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
399 struct kprobes
*kprobe
;
402 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
403 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
404 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
413 * hook on each event to check the timestamp and refresh the display if
416 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
418 unsigned long timestamp
;
420 timestamp
= bt_ctf_get_timestamp(call_data
);
421 if (timestamp
== -1ULL)
424 /* can happen in network live when tracing is idle */
425 if (timestamp
< last_event_ts
)
428 last_event_ts
= timestamp
;
430 if (last_display_update
== 0)
431 last_display_update
= timestamp
;
433 if (timestamp
- last_display_update
>= refresh_display
) {
434 sem_wait(&goodtoupdate
);
435 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
437 sem_post(&goodtodisplay
);
438 sem_post(&bootstrap
);
439 last_display_update
= timestamp
;
444 fprintf(stderr
, "check_timestamp callback error\n");
445 return BT_CB_ERROR_STOP
;
448 return BT_CB_OK_STOP
;
452 * get_perf_counter : get or create and return a perf_counter struct for
453 * either a process or a cpu (only one of the 2 parameters mandatory)
455 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
458 struct perfcounter
*ret
;
468 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
472 ret
= g_new0(struct perfcounter
, 1);
473 /* by default, make it visible in the UI */
475 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
484 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
485 const char *name
, int value
)
487 struct perfcounter
*cpu_perf
, *process_perf
;
489 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
490 if (cpu_perf
->count
< value
) {
491 process_perf
= get_perf_counter(name
, proc
, NULL
);
492 process_perf
->count
+= value
- cpu_perf
->count
;
493 cpu_perf
->count
= value
;
497 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
498 const struct bt_definition
*scope
,
499 struct processtop
*proc
,
502 struct bt_definition
const * const *list
= NULL
;
503 const struct bt_definition
*field
;
505 struct perfcounter
*perfcounter
;
513 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
520 g_hash_table_iter_init(&iter
, global_perf_liszt
);
521 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
522 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
524 int value
= bt_ctf_get_uint64(field
);
525 if (bt_ctf_field_get_error())
527 update_perf_value(proc
, cpu
, (char *) key
, value
);
535 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
538 const struct bt_definition
*scope
;
540 cpu
= get_cpu(get_cpu_id(event
));
542 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
543 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
545 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
546 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
548 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
549 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
552 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
555 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
556 char *comm
, *hostname
;
557 struct processtop
*parent
, *child
;
558 unsigned long timestamp
;
560 timestamp
= bt_ctf_get_timestamp(call_data
);
561 if (timestamp
== -1ULL)
564 pid
= get_context_pid(call_data
);
569 tid
= get_context_tid(call_data
);
573 ppid
= get_context_ppid(call_data
);
577 vpid
= get_context_vpid(call_data
);
581 vtid
= get_context_vtid(call_data
);
585 vppid
= get_context_vppid(call_data
);
589 comm
= get_context_comm(call_data
);
594 hostname
= get_context_hostname(call_data
);
596 /* find or create the current process */
597 child
= find_process_tid(<tngtop
, tid
, comm
);
599 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
602 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
605 /* find or create the parent */
606 parent
= find_process_tid(<tngtop
, pid
, comm
);
608 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
613 /* attach the parent to the current process */
614 child
->threadparent
= parent
;
615 add_thread(parent
, child
);
618 update_perf_counter(child
, call_data
);
624 return BT_CB_ERROR_STOP
;
629 copies
= g_ptr_array_new();
630 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
631 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
632 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
633 tid_filter_list
= g_hash_table_new(g_str_hash
,
636 sem_init(&goodtodisplay
, 0, 0);
637 sem_init(&goodtoupdate
, 0, 1);
638 sem_init(&timer
, 0, 1);
639 sem_init(&bootstrap
, 0, 0);
640 sem_init(&pause_sem
, 0, 1);
641 sem_init(&end_trace_sem
, 0, 0);
643 reset_global_counters();
645 lttngtop
.nbthreads
= 0;
646 lttngtop
.nbfiles
= 0;
648 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
650 lttngtop
.process_table
= g_ptr_array_new();
651 lttngtop
.files_table
= g_ptr_array_new();
652 lttngtop
.cpu_table
= g_ptr_array_new();
659 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
660 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
661 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
662 fprintf(fp
, " -h, --help This help message\n");
663 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
664 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
665 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
666 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
667 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");
668 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
669 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
670 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
674 * Parse probe options.
675 * Shamelessly stolen from lttng-tools :
676 * src/bin/lttng/commands/enable_events.c
678 static struct kprobes
*parse_probe_opts(char *opt
)
681 char name
[LTTNG_SYMBOL_NAME_LEN
];
682 struct kprobes
*kprobe
;
686 kprobe->probe_addr = 0;
687 kprobe->probe_offset = 0;
688 asprintf(&kprobe->probe_name, "probe_sys_open");
689 asprintf(&kprobe->symbol_name, "sys_open");
697 kprobe
= g_new0(struct kprobes
, 1);
699 /* Check for symbol+offset */
700 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
702 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
703 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
705 if (strlen(s_hex
) == 0) {
706 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
710 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
711 kprobe
->probe_addr
= 0;
715 /* Check for symbol */
716 if (isalpha(name
[0])) {
717 ret
= sscanf(opt
, "%s", name
);
719 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
720 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
721 kprobe
->probe_offset
= 0;
722 kprobe
->probe_addr
= 0;
727 /* Check for address */
728 ret
= sscanf(opt
, "%s", s_hex
);
730 if (strlen(s_hex
) == 0) {
731 fprintf(stderr
, "Invalid probe address %s", s_hex
);
735 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
736 kprobe
->probe_offset
= 0;
737 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
749 * Return 0 if caller should continue, < 0 if caller should return
750 * error, > 0 if caller should exit without reporting error.
752 static int parse_options(int argc
, char **argv
)
762 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
763 poptReadDefaultConfig(pc
, 0);
765 while ((opt
= poptGetNextOpt(pc
)) != -1) {
769 ret
= 1; /* exit cleanly */
785 tmp_str
= strtok(opt_tid
, ",");
787 tid
= malloc(sizeof(int));
788 *tid
= atoi(tmp_str
);
789 g_hash_table_insert(tid_filter_list
,
790 (gpointer
) tid
, tid
);
791 tmp_str
= strtok(NULL
, ",");
795 /* start reading the live trace from the beginning */
800 tmp_str
= strtok(opt_hostname
, ",");
802 add_hostname_list(tmp_str
, 1);
803 tmp_str
= strtok(NULL
, ",");
806 case OPT_RELAY_HOSTNAME
:
810 lttngtop
.kprobes_table
= g_ptr_array_new();
811 tmp_str
= strtok(opt_kprobes
, ",");
813 struct kprobes
*kprobe
;
815 kprobe
= parse_probe_opts(tmp_str
);
818 lttngtop
.kprobes_table
,
824 tmp_str
= strtok(NULL
, ",");
833 opt_exec_name
= NULL
;
834 opt_exec_argv
= NULL
;
835 for (i
= 0; i
< argc
; i
++) {
836 if (argv
[i
][0] == '-' && argv
[i
][1] == '-') {
837 opt_exec_name
= argv
[i
+ 1];
838 opt_exec_argv
= &argv
[i
+ 1];
842 if (!opt_exec_name
) {
843 opt_input_path
= poptGetArg(pc
);
853 void iter_trace(struct bt_context
*bt_ctx
)
855 struct bt_ctf_iter
*iter
;
856 struct bt_iter_pos begin_pos
;
857 struct kprobes
*kprobe
;
858 const struct bt_ctf_event
*event
;
862 begin_pos
.type
= BT_SEEK_BEGIN
;
863 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
865 /* at each event, verify the status of the process table */
866 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
869 /* to handle the follow child option */
870 bt_ctf_iter_add_callback(iter
,
871 g_quark_from_static_string("sched_process_fork"),
872 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
873 /* to clean up the process table */
874 bt_ctf_iter_add_callback(iter
,
875 g_quark_from_static_string("sched_process_free"),
876 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
877 /* to get all the process from the statedumps */
878 bt_ctf_iter_add_callback(iter
,
879 g_quark_from_static_string(
880 "lttng_statedump_process_state"),
881 NULL
, 0, handle_statedump_process_state
,
883 bt_ctf_iter_add_callback(iter
,
884 g_quark_from_static_string(
885 "lttng_statedump_file_descriptor"),
886 NULL
, 0, handle_statedump_file_descriptor
,
888 bt_ctf_iter_add_callback(iter
,
889 g_quark_from_static_string("sys_open"),
890 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
891 bt_ctf_iter_add_callback(iter
,
892 g_quark_from_static_string("sys_close"),
893 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
894 bt_ctf_iter_add_callback(iter
,
895 g_quark_from_static_string("exit_syscall"),
896 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
898 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
902 /* at each event check if we need to refresh */
903 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
906 /* to handle the scheduling events */
907 bt_ctf_iter_add_callback(iter
,
908 g_quark_from_static_string("sched_switch"),
909 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
911 bt_ctf_iter_add_callback(iter
,
912 g_quark_from_static_string("sys_write"),
913 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
914 bt_ctf_iter_add_callback(iter
,
915 g_quark_from_static_string("sys_read"),
916 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
919 if (lttngtop
.kprobes_table
) {
920 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
921 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
922 bt_ctf_iter_add_callback(iter
,
923 g_quark_from_static_string(
925 NULL
, 0, handle_kprobes
,
936 execvpe(opt_exec_name
, opt_exec_argv
, opt_exec_env
);
938 } else if (pid
> 0) {
940 g_hash_table_insert(tid_filter_list
,
949 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
950 if (quit
|| reload_trace
)
952 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
957 /* block until quit, we reached the end of the trace */
958 sem_wait(&end_trace_sem
);
961 bt_ctf_iter_destroy(iter
);
965 * bt_context_add_traces_recursive: Open a trace recursively
966 * (copied from BSD code in converter/babeltrace.c)
968 * Find each trace present in the subdirectory starting from the given
969 * path, and add them to the context. The packet_seek parameter can be
970 * NULL: this specify to use the default format packet_seek.
972 * Return: 0 on success, nonzero on failure.
973 * Unable to open toplevel: failure.
974 * Unable to open some subdirectory or file: warn and continue;
976 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
977 const char *format_str
,
978 void (*packet_seek
)(struct bt_stream_pos
*pos
,
979 size_t offset
, int whence
))
984 char lpath
[PATH_MAX
];
985 char * const paths
[2] = { lpath
, NULL
};
988 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
989 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
990 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
991 ret
= bt_context_add_trace(ctx
,
992 path
, format_str
, packet_seek
, NULL
, NULL
);
994 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
995 "for reading.\n", path
);
996 /* Allow to skip erroneous traces. */
997 ret
= 1; /* partial error */
1002 * Need to copy path, because fts_open can change it.
1003 * It is the pointer array, not the strings, that are constant.
1005 strncpy(lpath
, path
, PATH_MAX
);
1006 lpath
[PATH_MAX
- 1] = '\0';
1008 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
1010 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
1015 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
1017 while ((node
= fts_read(tree
))) {
1020 if (!(node
->fts_info
& FTS_D
))
1023 dirfd
= open(node
->fts_accpath
, 0);
1025 fprintf(stderr
, "[error] [Context] Unable to open trace "
1026 "directory file descriptor.\n");
1030 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
1037 ret
= close(metafd
);
1048 trace_id
= bt_context_add_trace(ctx
,
1049 node
->fts_accpath
, format_str
,
1050 packet_seek
, NULL
, NULL
);
1052 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
1053 "for reading.\n", node
->fts_accpath
, path
);
1054 /* Allow to skip erroneous traces. */
1057 g_array_append_val(trace_ids
, trace_id
);
1061 g_array_free(trace_ids
, TRUE
);
1068 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
1069 int field_cnt
, int *tid_check
, int *pid_check
,
1070 int *procname_check
, int *ppid_check
)
1073 struct perfcounter
*global
;
1076 for (j
= 0; j
< field_cnt
; j
++) {
1077 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
1078 if (*tid_check
== 0) {
1079 if (strncmp(name
, "tid", 3) == 0)
1082 if (*pid_check
== 0) {
1083 if (strncmp(name
, "pid", 3) == 0)
1086 if (*ppid_check
== 0) {
1087 if (strncmp(name
, "ppid", 4) == 0)
1090 if (*procname_check
== 0) {
1091 if (strncmp(name
, "procname", 8) == 0)
1092 (*procname_check
)++;
1094 if (strncmp(name
, "perf_", 5) == 0) {
1095 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
1097 global
= g_new0(struct perfcounter
, 1);
1098 /* by default, sort on the first perf context */
1099 if (g_hash_table_size(global_perf_liszt
) == 0)
1101 global
->visible
= 1;
1102 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
1107 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
1108 *procname_check
== 1)
1115 * check_requirements: check if the required context informations are available
1117 * If each mandatory context information is available for at least in one
1118 * event, return 0 otherwise return -1.
1120 int check_requirements(struct bt_context
*ctx
)
1122 unsigned int i
, evt_cnt
, field_cnt
;
1123 struct bt_ctf_event_decl
*const * evt_list
;
1124 const struct bt_ctf_field_decl
*const * field_list
;
1127 int procname_check
= 0;
1131 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1136 for (i
= 0; i
< evt_cnt
; i
++) {
1137 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_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_EVENT_CONTEXT
,
1144 &field_list
, &field_cnt
);
1145 ret
= check_field_requirements(field_list
, field_cnt
,
1146 &tid_check
, &pid_check
, &procname_check
,
1149 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1150 &field_list
, &field_cnt
);
1151 ret
= check_field_requirements(field_list
, field_cnt
,
1152 &tid_check
, &pid_check
, &procname_check
,
1156 if (tid_check
== 0) {
1158 fprintf(stderr
, "[error] missing tid context information\n");
1160 if (pid_check
== 0) {
1162 fprintf(stderr
, "[error] missing pid context information\n");
1164 if (ppid_check
== 0) {
1166 fprintf(stderr
, "[error] missing ppid context information\n");
1168 if (procname_check
== 0) {
1170 fprintf(stderr
, "[error] missing procname context information\n");
1177 static void handle_sigchild(int signal
)
1181 waitpid(opt_exec_pid
, &status
, 0);
1184 int main(int argc
, char **argv
, char **envp
)
1187 struct bt_context
*bt_ctx
= NULL
;
1189 //babeltrace_verbose = 1;
1191 ret
= parse_options(argc
, argv
);
1193 fprintf(stdout
, "Error parsing options.\n\n");
1196 } else if (ret
> 0) {
1200 if (opt_exec_name
) {
1201 opt_exec_env
= envp
;
1202 signal(SIGCHLD
, handle_sigchild
);
1205 if (!opt_input_path
&& !remote_live
&& !opt_exec_name
) {
1207 #ifdef LTTNGTOP_MMAP_LIVE
1209 signal(SIGTERM
, handle_textdump_sigterm
);
1210 signal(SIGINT
, handle_textdump_sigterm
);
1212 mmap_live_loop(bt_ctx
);
1213 pthread_join(timer_thread
, NULL
);
1215 pthread_join(display_thread
, NULL
);
1217 lttng_stop_tracing("test");
1218 lttng_destroy_session("test");
1222 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1223 "trace directory or -r <relayd hostname/IP>\n");
1227 #endif /* LTTNGTOP_MMAP_LIVE */
1228 } else if (!opt_input_path
&& remote_live
) {
1230 bt_ctx
= bt_context_create();
1231 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1232 "lttng-live", NULL
);
1234 fprintf(stderr
, "[error] Opening the trace\n");
1240 bt_ctx
= bt_context_create();
1241 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1243 fprintf(stderr
, "[error] Opening the trace\n");
1247 ret
= check_requirements(bt_ctx
);
1249 fprintf(stderr
, "[error] some mandatory contexts "
1250 "were missing, exiting.\n");
1254 if (!opt_textdump
) {
1255 pthread_create(&display_thread
, NULL
, ncurses_display
,
1257 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1265 pthread_join(display_thread
, NULL
);
1267 pthread_join(timer_thread
, NULL
);
1273 bt_context_put(bt_ctx
);