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>
41 #include <lttng/lttng.h>
42 #ifdef LTTNGTOP_MMAP_LIVE
43 #include <lttng/lttngtop-helper.h>
44 #include <babeltrace/lttngtopmmappacketseek.h>
45 #include "mmap-live.h"
46 #endif /* LTTNGTOP_MMAP_LIVE */
48 #include "lttngtoptypes.h"
50 #include "iostreamtop.h"
51 #include "cursesdisplay.h"
53 #include "network-live.h"
55 #define NET_URL_PREFIX "net://"
56 #define NET4_URL_PREFIX "net4://"
57 #define NET6_URL_PREFIX "net6://"
59 #define DEFAULT_FILE_ARRAY_SIZE 1
61 const char *opt_input_path
;
68 struct lttngtop
*copy
;
69 pthread_t display_thread
;
70 pthread_t timer_thread
;
72 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
73 unsigned long last_display_update
= 0;
74 unsigned long last_event_ts
= 0;
76 /* list of FDs available for being read with snapshots */
77 struct bt_mmap_stream_list mmap_list
;
78 GPtrArray
*lttng_consumer_stream_array
;
79 int sessiond_metadata
, consumerd_metadata
;
80 struct lttng_consumer_local_data
*ctx
= NULL
;
81 /* list of snapshots currently not consumed */
82 GPtrArray
*available_snapshots
;
83 sem_t metadata_available
;
86 int last_textdump_print_newline
= 1;
100 static struct poptOption long_options
[] = {
101 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
102 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
103 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
104 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
105 { "begin", 'b', POPT_ARG_NONE
, NULL
, OPT_BEGIN
, NULL
, NULL
},
106 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
107 { "hostname", 'n', POPT_ARG_STRING
, &opt_hostname
, OPT_HOSTNAME
, NULL
, NULL
},
108 { "relay-hostname", 'r', POPT_ARG_STRING
, &opt_relay_hostname
,
109 OPT_RELAY_HOSTNAME
, NULL
, NULL
},
110 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
111 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
114 #ifdef LTTNGTOP_MMAP_LIVE
115 static void handle_textdump_sigterm(int signal
)
118 lttng_destroy_session("test");
122 void *refresh_thread(void *p
)
126 sem_post(&pause_sem
);
128 sem_post(&end_trace_sem
);
129 sem_post(&goodtodisplay
);
130 sem_post(&goodtoupdate
);
133 if (!opt_input_path
) {
134 #ifdef LTTNGTOP_MMAP_LIVE
135 mmap_live_flush(mmap_list
);
138 sem_wait(&pause_sem
);
139 sem_post(&pause_sem
);
141 sleep(refresh_display
/NSEC_PER_SEC
);
145 void *ncurses_display(void *p
)
147 unsigned int current_display_index
= 0;
149 sem_wait(&bootstrap
);
151 * Prevent the 1 second delay when we hit ESC
158 sem_wait(&goodtodisplay
);
159 sem_wait(&pause_sem
);
162 sem_post(&pause_sem
);
168 copy
= g_ptr_array_index(copies
, current_display_index
);
170 display(current_display_index
++);
172 sem_post(&goodtoupdate
);
173 sem_post(&pause_sem
);
177 void print_fields(struct bt_ctf_event
*event
)
180 const struct bt_definition
*const * list
;
181 const struct bt_declaration
*l
;
182 const struct bt_definition
*scope
;
183 enum ctf_type_id type
;
186 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
188 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
189 for (i
= 0; i
< cnt
; i
++) {
192 printf("%s = ", bt_ctf_field_name(list
[i
]));
193 l
= bt_ctf_get_decl_from_def(list
[i
]);
194 type
= bt_ctf_field_type(l
);
195 if (type
== CTF_TYPE_INTEGER
) {
196 if (bt_ctf_get_int_signedness(l
) == 0)
197 printf("%" PRIu64
"", bt_ctf_get_uint64(list
[i
]));
199 printf("%" PRId64
"", bt_ctf_get_int64(list
[i
]));
200 } else if (type
== CTF_TYPE_STRING
) {
201 printf("%s", bt_ctf_get_string(list
[i
]));
202 } else if (type
== CTF_TYPE_ARRAY
) {
203 str
= bt_ctf_get_char_array(list
[i
]);
204 if (!bt_ctf_field_get_error() && str
)
211 * hook on each event to check the timestamp and refresh the display if
214 enum bt_cb_ret
print_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
216 unsigned long timestamp
;
218 uint64_t ts_nsec_start
;
220 const struct bt_definition
*scope
;
221 const char *hostname
, *procname
;
223 timestamp
= bt_ctf_get_timestamp(call_data
);
225 /* can happen in network live when tracing is idle */
226 if (timestamp
< last_event_ts
)
229 last_event_ts
= timestamp
;
231 start
= format_timestamp(timestamp
);
232 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
234 pid
= get_context_pid(call_data
);
235 if (pid
== -1ULL && opt_tid
) {
239 hostname
= get_context_hostname(call_data
);
240 if (opt_tid
|| opt_hostname
) {
241 if (!lookup_filter_tid_list(pid
)) {
242 /* To display when a process of ours in getting scheduled in */
243 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
246 scope
= bt_ctf_get_top_level_scope(call_data
,
248 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
249 scope
, "_next_tid"));
250 if (bt_ctf_field_get_error()) {
251 fprintf(stderr
, "Missing next_tid field\n");
254 if (!lookup_filter_tid_list(next_tid
)) {
263 cpu_id
= get_cpu_id(call_data
);
264 procname
= get_context_comm(call_data
);
267 printf("%02d:%02d:%02d.%09" PRIu64
" (%s) (cpu %d) [%s (%d)] %s (",
268 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
269 ts_nsec_start
, hostname
, cpu_id
, procname
, pid
,
270 bt_ctf_event_name(call_data
));
272 printf("%02d:%02d:%02d.%09" PRIu64
" (cpu %d) [%s (%d)] %s (",
273 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
274 ts_nsec_start
, cpu_id
, procname
, pid
,
275 bt_ctf_event_name(call_data
));
277 print_fields(call_data
);
283 return BT_CB_ERROR_STOP
;
285 return BT_CB_OK_STOP
;
288 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
291 struct kprobes
*kprobe
;
294 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
295 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
296 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
305 * hook on each event to check the timestamp and refresh the display if
308 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
310 unsigned long timestamp
;
312 timestamp
= bt_ctf_get_timestamp(call_data
);
313 if (timestamp
== -1ULL)
316 /* can happen in network live when tracing is idle */
317 if (timestamp
< last_event_ts
)
320 last_event_ts
= timestamp
;
322 if (last_display_update
== 0)
323 last_display_update
= timestamp
;
325 if (timestamp
- last_display_update
>= refresh_display
) {
326 sem_wait(&goodtoupdate
);
327 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
329 sem_post(&goodtodisplay
);
330 sem_post(&bootstrap
);
331 last_display_update
= timestamp
;
336 fprintf(stderr
, "check_timestamp callback error\n");
337 return BT_CB_ERROR_STOP
;
340 return BT_CB_OK_STOP
;
344 * get_perf_counter : get or create and return a perf_counter struct for
345 * either a process or a cpu (only one of the 2 parameters mandatory)
347 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
350 struct perfcounter
*ret
;
360 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
364 ret
= g_new0(struct perfcounter
, 1);
365 /* by default, make it visible in the UI */
367 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
376 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
377 const char *name
, int value
)
379 struct perfcounter
*cpu_perf
, *process_perf
;
381 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
382 if (cpu_perf
->count
< value
) {
383 process_perf
= get_perf_counter(name
, proc
, NULL
);
384 process_perf
->count
+= value
- cpu_perf
->count
;
385 cpu_perf
->count
= value
;
389 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
390 const struct bt_definition
*scope
,
391 struct processtop
*proc
,
394 struct bt_definition
const * const *list
= NULL
;
395 const struct bt_definition
*field
;
397 struct perfcounter
*perfcounter
;
405 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
412 g_hash_table_iter_init(&iter
, global_perf_liszt
);
413 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
414 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
416 int value
= bt_ctf_get_uint64(field
);
417 if (bt_ctf_field_get_error())
419 update_perf_value(proc
, cpu
, (char *) key
, value
);
427 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
430 const struct bt_definition
*scope
;
432 cpu
= get_cpu(get_cpu_id(event
));
434 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
435 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
437 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
438 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
440 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
441 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
444 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
447 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
448 char *comm
, *hostname
;
449 struct processtop
*parent
, *child
;
450 unsigned long timestamp
;
452 timestamp
= bt_ctf_get_timestamp(call_data
);
453 if (timestamp
== -1ULL)
456 pid
= get_context_pid(call_data
);
461 tid
= get_context_tid(call_data
);
465 ppid
= get_context_ppid(call_data
);
469 vpid
= get_context_vpid(call_data
);
473 vtid
= get_context_vtid(call_data
);
477 vppid
= get_context_vppid(call_data
);
481 comm
= get_context_comm(call_data
);
486 hostname
= get_context_hostname(call_data
);
488 /* find or create the current process */
489 child
= find_process_tid(<tngtop
, tid
, comm
);
491 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
494 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
497 /* find or create the parent */
498 parent
= find_process_tid(<tngtop
, pid
, comm
);
500 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
505 /* attach the parent to the current process */
506 child
->threadparent
= parent
;
507 add_thread(parent
, child
);
510 update_perf_counter(child
, call_data
);
516 return BT_CB_ERROR_STOP
;
521 copies
= g_ptr_array_new();
522 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
523 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
524 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
526 sem_init(&goodtodisplay
, 0, 0);
527 sem_init(&goodtoupdate
, 0, 1);
528 sem_init(&timer
, 0, 1);
529 sem_init(&bootstrap
, 0, 0);
530 sem_init(&pause_sem
, 0, 1);
531 sem_init(&end_trace_sem
, 0, 0);
533 reset_global_counters();
535 lttngtop
.nbthreads
= 0;
536 lttngtop
.nbfiles
= 0;
538 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
540 lttngtop
.process_table
= g_ptr_array_new();
541 lttngtop
.files_table
= g_ptr_array_new();
542 lttngtop
.cpu_table
= g_ptr_array_new();
549 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
550 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
551 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
552 fprintf(fp
, " -h, --help This help message\n");
553 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
554 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
555 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
556 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
557 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
558 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
559 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
563 * Parse probe options.
564 * Shamelessly stolen from lttng-tools :
565 * src/bin/lttng/commands/enable_events.c
567 static struct kprobes
*parse_probe_opts(char *opt
)
570 char name
[LTTNG_SYMBOL_NAME_LEN
];
571 struct kprobes
*kprobe
;
575 kprobe->probe_addr = 0;
576 kprobe->probe_offset = 0;
577 asprintf(&kprobe->probe_name, "probe_sys_open");
578 asprintf(&kprobe->symbol_name, "sys_open");
586 kprobe
= g_new0(struct kprobes
, 1);
588 /* Check for symbol+offset */
589 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
591 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
592 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
594 if (strlen(s_hex
) == 0) {
595 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
599 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
600 kprobe
->probe_addr
= 0;
604 /* Check for symbol */
605 if (isalpha(name
[0])) {
606 ret
= sscanf(opt
, "%s", name
);
608 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
609 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
610 kprobe
->probe_offset
= 0;
611 kprobe
->probe_addr
= 0;
616 /* Check for address */
617 ret
= sscanf(opt
, "%s", s_hex
);
619 if (strlen(s_hex
) == 0) {
620 fprintf(stderr
, "Invalid probe address %s", s_hex
);
624 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
625 kprobe
->probe_offset
= 0;
626 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
638 * Return 0 if caller should continue, < 0 if caller should return
639 * error, > 0 if caller should exit without reporting error.
641 static int parse_options(int argc
, char **argv
)
650 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
651 poptReadDefaultConfig(pc
, 0);
653 while ((opt
= poptGetNextOpt(pc
)) != -1) {
657 ret
= 1; /* exit cleanly */
667 tid_filter_list
= g_hash_table_new(g_str_hash
,
669 tmp_str
= strtok(opt_tid
, ",");
671 tid
= malloc(sizeof(int));
672 *tid
= atoi(tmp_str
);
673 g_hash_table_insert(tid_filter_list
,
674 (gpointer
) tid
, tid
);
675 tmp_str
= strtok(NULL
, ",");
679 /* start reading the live trace from the beginning */
684 tmp_str
= strtok(opt_hostname
, ",");
686 add_hostname_list(tmp_str
, 1);
687 tmp_str
= strtok(NULL
, ",");
690 case OPT_RELAY_HOSTNAME
:
694 lttngtop
.kprobes_table
= g_ptr_array_new();
695 tmp_str
= strtok(opt_kprobes
, ",");
697 struct kprobes
*kprobe
;
699 kprobe
= parse_probe_opts(tmp_str
);
702 lttngtop
.kprobes_table
,
708 tmp_str
= strtok(NULL
, ",");
717 opt_input_path
= poptGetArg(pc
);
726 void iter_trace(struct bt_context
*bt_ctx
)
728 struct bt_ctf_iter
*iter
;
729 struct bt_iter_pos begin_pos
;
730 struct kprobes
*kprobe
;
731 const struct bt_ctf_event
*event
;
735 begin_pos
.type
= BT_SEEK_BEGIN
;
736 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
738 /* at each event, verify the status of the process table */
739 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
742 /* to handle the follow child option */
743 bt_ctf_iter_add_callback(iter
,
744 g_quark_from_static_string("sched_process_fork"),
745 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
747 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
751 /* at each event check if we need to refresh */
752 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
755 /* to handle the scheduling events */
756 bt_ctf_iter_add_callback(iter
,
757 g_quark_from_static_string("sched_switch"),
758 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
759 /* to clean up the process table */
760 bt_ctf_iter_add_callback(iter
,
761 g_quark_from_static_string("sched_process_free"),
762 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
763 /* to get all the process from the statedumps */
764 bt_ctf_iter_add_callback(iter
,
765 g_quark_from_static_string(
766 "lttng_statedump_process_state"),
767 NULL
, 0, handle_statedump_process_state
,
771 bt_ctf_iter_add_callback(iter
,
772 g_quark_from_static_string("exit_syscall"),
773 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
774 bt_ctf_iter_add_callback(iter
,
775 g_quark_from_static_string("sys_write"),
776 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
777 bt_ctf_iter_add_callback(iter
,
778 g_quark_from_static_string("sys_read"),
779 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
780 bt_ctf_iter_add_callback(iter
,
781 g_quark_from_static_string("sys_open"),
782 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
783 bt_ctf_iter_add_callback(iter
,
784 g_quark_from_static_string("sys_close"),
785 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
786 bt_ctf_iter_add_callback(iter
,
787 g_quark_from_static_string(
788 "lttng_statedump_file_descriptor"),
789 NULL
, 0, handle_statedump_file_descriptor
,
793 if (lttngtop
.kprobes_table
) {
794 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
795 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
796 bt_ctf_iter_add_callback(iter
,
797 g_quark_from_static_string(
799 NULL
, 0, handle_kprobes
,
805 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
806 if (quit
|| reload_trace
)
808 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
813 /* block until quit, we reached the end of the trace */
814 sem_wait(&end_trace_sem
);
817 bt_ctf_iter_destroy(iter
);
821 * bt_context_add_traces_recursive: Open a trace recursively
822 * (copied from BSD code in converter/babeltrace.c)
824 * Find each trace present in the subdirectory starting from the given
825 * path, and add them to the context. The packet_seek parameter can be
826 * NULL: this specify to use the default format packet_seek.
828 * Return: 0 on success, nonzero on failure.
829 * Unable to open toplevel: failure.
830 * Unable to open some subdirectory or file: warn and continue;
832 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
833 const char *format_str
,
834 void (*packet_seek
)(struct bt_stream_pos
*pos
,
835 size_t offset
, int whence
))
840 char lpath
[PATH_MAX
];
841 char * const paths
[2] = { lpath
, NULL
};
844 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
845 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
846 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
847 ret
= bt_context_add_trace(ctx
,
848 path
, format_str
, packet_seek
, NULL
, NULL
);
850 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
851 "for reading.\n", path
);
852 /* Allow to skip erroneous traces. */
853 ret
= 1; /* partial error */
858 * Need to copy path, because fts_open can change it.
859 * It is the pointer array, not the strings, that are constant.
861 strncpy(lpath
, path
, PATH_MAX
);
862 lpath
[PATH_MAX
- 1] = '\0';
864 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
866 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
871 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
873 while ((node
= fts_read(tree
))) {
876 if (!(node
->fts_info
& FTS_D
))
879 dirfd
= open(node
->fts_accpath
, 0);
881 fprintf(stderr
, "[error] [Context] Unable to open trace "
882 "directory file descriptor.\n");
886 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
904 trace_id
= bt_context_add_trace(ctx
,
905 node
->fts_accpath
, format_str
,
906 packet_seek
, NULL
, NULL
);
908 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
909 "for reading.\n", node
->fts_accpath
, path
);
910 /* Allow to skip erroneous traces. */
913 g_array_append_val(trace_ids
, trace_id
);
917 g_array_free(trace_ids
, TRUE
);
924 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
925 int field_cnt
, int *tid_check
, int *pid_check
,
926 int *procname_check
, int *ppid_check
)
929 struct perfcounter
*global
;
932 for (j
= 0; j
< field_cnt
; j
++) {
933 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
934 if (*tid_check
== 0) {
935 if (strncmp(name
, "tid", 3) == 0)
938 if (*pid_check
== 0) {
939 if (strncmp(name
, "pid", 3) == 0)
942 if (*ppid_check
== 0) {
943 if (strncmp(name
, "ppid", 4) == 0)
946 if (*procname_check
== 0) {
947 if (strncmp(name
, "procname", 8) == 0)
950 if (strncmp(name
, "perf_", 5) == 0) {
951 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
953 global
= g_new0(struct perfcounter
, 1);
954 /* by default, sort on the first perf context */
955 if (g_hash_table_size(global_perf_liszt
) == 0)
958 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
963 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
964 *procname_check
== 1)
971 * check_requirements: check if the required context informations are available
973 * If each mandatory context information is available for at least in one
974 * event, return 0 otherwise return -1.
976 int check_requirements(struct bt_context
*ctx
)
978 unsigned int i
, evt_cnt
, field_cnt
;
979 struct bt_ctf_event_decl
*const * evt_list
;
980 const struct bt_ctf_field_decl
*const * field_list
;
983 int procname_check
= 0;
987 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
992 for (i
= 0; i
< evt_cnt
; i
++) {
993 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
994 &field_list
, &field_cnt
);
995 ret
= check_field_requirements(field_list
, field_cnt
,
996 &tid_check
, &pid_check
, &procname_check
,
999 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1000 &field_list
, &field_cnt
);
1001 ret
= check_field_requirements(field_list
, field_cnt
,
1002 &tid_check
, &pid_check
, &procname_check
,
1005 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1006 &field_list
, &field_cnt
);
1007 ret
= check_field_requirements(field_list
, field_cnt
,
1008 &tid_check
, &pid_check
, &procname_check
,
1012 if (tid_check
== 0) {
1014 fprintf(stderr
, "[error] missing tid context information\n");
1016 if (pid_check
== 0) {
1018 fprintf(stderr
, "[error] missing pid context information\n");
1020 if (ppid_check
== 0) {
1022 fprintf(stderr
, "[error] missing ppid context information\n");
1024 if (procname_check
== 0) {
1026 fprintf(stderr
, "[error] missing procname context information\n");
1033 int main(int argc
, char **argv
)
1036 struct bt_context
*bt_ctx
= NULL
;
1039 ret
= parse_options(argc
, argv
);
1041 fprintf(stdout
, "Error parsing options.\n\n");
1044 } else if (ret
> 0) {
1048 if (!opt_input_path
&& !remote_live
) {
1050 #ifdef LTTNGTOP_MMAP_LIVE
1052 signal(SIGTERM
, handle_textdump_sigterm
);
1053 signal(SIGINT
, handle_textdump_sigterm
);
1055 mmap_live_loop(bt_ctx
);
1056 pthread_join(timer_thread
, NULL
);
1058 pthread_join(display_thread
, NULL
);
1060 lttng_stop_tracing("test");
1061 lttng_destroy_session("test");
1065 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1066 "trace directory or -r <relayd hostname/IP>\n");
1070 #endif /* LTTNGTOP_MMAP_LIVE */
1071 } else if (!opt_input_path
&& remote_live
) {
1074 ret
= setup_network_live(opt_relay_hostname
, opt_begin
);
1079 ret
= open_trace(&bt_ctx
);
1085 bt_ctx
= bt_context_create();
1086 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1087 "lttng-live", NULL
);
1089 fprintf(stderr
, "[error] Opening the trace\n");
1095 bt_ctx
= bt_context_create();
1096 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1098 fprintf(stderr
, "[error] Opening the trace\n");
1102 ret
= check_requirements(bt_ctx
);
1104 fprintf(stderr
, "[error] some mandatory contexts "
1105 "were missing, exiting.\n");
1109 if (!opt_textdump
) {
1110 pthread_create(&display_thread
, NULL
, ncurses_display
,
1112 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1120 pthread_join(display_thread
, NULL
);
1122 pthread_join(timer_thread
, NULL
);
1128 bt_context_put(bt_ctx
);