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
;
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
;
219 uint64_t ts_nsec_start
;
220 int pid
, cpu_id
, tid
;
221 const struct bt_definition
*scope
;
222 const char *hostname
, *procname
;
224 timestamp
= bt_ctf_get_timestamp(call_data
);
226 /* can happen in network live when tracing is idle */
227 if (timestamp
< last_event_ts
)
230 last_event_ts
= timestamp
;
232 start
= format_timestamp(timestamp
);
233 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
235 pid
= get_context_pid(call_data
);
236 if (pid
== -1ULL && opt_tid
) {
240 tid
= get_context_tid(call_data
);
242 hostname
= get_context_hostname(call_data
);
243 if (opt_tid
|| opt_hostname
) {
244 if (!lookup_filter_tid_list(pid
)) {
245 /* To display when a process of ours in getting scheduled in */
246 if (strcmp(bt_ctf_event_name(call_data
), "sched_switch") == 0) {
249 scope
= bt_ctf_get_top_level_scope(call_data
,
251 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
252 scope
, "_next_tid"));
253 if (bt_ctf_field_get_error()) {
254 fprintf(stderr
, "Missing next_tid field\n");
257 if (!lookup_filter_tid_list(next_tid
)) {
266 cpu_id
= get_cpu_id(call_data
);
267 procname
= get_context_comm(call_data
);
270 delta
= timestamp
- prev_ts
;
274 printf("%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") (%s) "
275 "(cpu %d) [%s (%d/%d)] %s (",
276 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
277 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
278 delta
% NSEC_PER_SEC
, hostname
, cpu_id
,
280 bt_ctf_event_name(call_data
));
282 printf("%02d:%02d:%02d.%09" PRIu64
" (+%" PRIu64
".%09" PRIu64
") "
283 "(cpu %d) [%s (%d/%d)] %s (",
284 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
285 ts_nsec_start
, delta
/ NSEC_PER_SEC
,
286 delta
% NSEC_PER_SEC
, cpu_id
, procname
, pid
,
287 tid
, bt_ctf_event_name(call_data
));
289 print_fields(call_data
);
295 return BT_CB_ERROR_STOP
;
297 return BT_CB_OK_STOP
;
300 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
303 struct kprobes
*kprobe
;
306 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
307 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
308 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
317 * hook on each event to check the timestamp and refresh the display if
320 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
322 unsigned long timestamp
;
324 timestamp
= bt_ctf_get_timestamp(call_data
);
325 if (timestamp
== -1ULL)
328 /* can happen in network live when tracing is idle */
329 if (timestamp
< last_event_ts
)
332 last_event_ts
= timestamp
;
334 if (last_display_update
== 0)
335 last_display_update
= timestamp
;
337 if (timestamp
- last_display_update
>= refresh_display
) {
338 sem_wait(&goodtoupdate
);
339 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
341 sem_post(&goodtodisplay
);
342 sem_post(&bootstrap
);
343 last_display_update
= timestamp
;
348 fprintf(stderr
, "check_timestamp callback error\n");
349 return BT_CB_ERROR_STOP
;
352 return BT_CB_OK_STOP
;
356 * get_perf_counter : get or create and return a perf_counter struct for
357 * either a process or a cpu (only one of the 2 parameters mandatory)
359 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
362 struct perfcounter
*ret
;
372 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
376 ret
= g_new0(struct perfcounter
, 1);
377 /* by default, make it visible in the UI */
379 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
388 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
389 const char *name
, int value
)
391 struct perfcounter
*cpu_perf
, *process_perf
;
393 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
394 if (cpu_perf
->count
< value
) {
395 process_perf
= get_perf_counter(name
, proc
, NULL
);
396 process_perf
->count
+= value
- cpu_perf
->count
;
397 cpu_perf
->count
= value
;
401 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
402 const struct bt_definition
*scope
,
403 struct processtop
*proc
,
406 struct bt_definition
const * const *list
= NULL
;
407 const struct bt_definition
*field
;
409 struct perfcounter
*perfcounter
;
417 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
424 g_hash_table_iter_init(&iter
, global_perf_liszt
);
425 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
426 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
428 int value
= bt_ctf_get_uint64(field
);
429 if (bt_ctf_field_get_error())
431 update_perf_value(proc
, cpu
, (char *) key
, value
);
439 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
442 const struct bt_definition
*scope
;
444 cpu
= get_cpu(get_cpu_id(event
));
446 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
447 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
449 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
450 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
452 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
453 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
456 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
459 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
460 char *comm
, *hostname
;
461 struct processtop
*parent
, *child
;
462 unsigned long timestamp
;
464 timestamp
= bt_ctf_get_timestamp(call_data
);
465 if (timestamp
== -1ULL)
468 pid
= get_context_pid(call_data
);
473 tid
= get_context_tid(call_data
);
477 ppid
= get_context_ppid(call_data
);
481 vpid
= get_context_vpid(call_data
);
485 vtid
= get_context_vtid(call_data
);
489 vppid
= get_context_vppid(call_data
);
493 comm
= get_context_comm(call_data
);
498 hostname
= get_context_hostname(call_data
);
500 /* find or create the current process */
501 child
= find_process_tid(<tngtop
, tid
, comm
);
503 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
506 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
509 /* find or create the parent */
510 parent
= find_process_tid(<tngtop
, pid
, comm
);
512 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
517 /* attach the parent to the current process */
518 child
->threadparent
= parent
;
519 add_thread(parent
, child
);
522 update_perf_counter(child
, call_data
);
528 return BT_CB_ERROR_STOP
;
533 copies
= g_ptr_array_new();
534 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
535 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
536 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
538 sem_init(&goodtodisplay
, 0, 0);
539 sem_init(&goodtoupdate
, 0, 1);
540 sem_init(&timer
, 0, 1);
541 sem_init(&bootstrap
, 0, 0);
542 sem_init(&pause_sem
, 0, 1);
543 sem_init(&end_trace_sem
, 0, 0);
545 reset_global_counters();
547 lttngtop
.nbthreads
= 0;
548 lttngtop
.nbfiles
= 0;
550 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
552 lttngtop
.process_table
= g_ptr_array_new();
553 lttngtop
.files_table
= g_ptr_array_new();
554 lttngtop
.cpu_table
= g_ptr_array_new();
561 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
562 fprintf(fp
, "Usage : lttngtop [OPTIONS] TRACE\n");
563 fprintf(fp
, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
564 fprintf(fp
, " -h, --help This help message\n");
565 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
566 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
567 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
568 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
569 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
570 fprintf(fp
, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
571 fprintf(fp
, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
575 * Parse probe options.
576 * Shamelessly stolen from lttng-tools :
577 * src/bin/lttng/commands/enable_events.c
579 static struct kprobes
*parse_probe_opts(char *opt
)
582 char name
[LTTNG_SYMBOL_NAME_LEN
];
583 struct kprobes
*kprobe
;
587 kprobe->probe_addr = 0;
588 kprobe->probe_offset = 0;
589 asprintf(&kprobe->probe_name, "probe_sys_open");
590 asprintf(&kprobe->symbol_name, "sys_open");
598 kprobe
= g_new0(struct kprobes
, 1);
600 /* Check for symbol+offset */
601 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
603 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
604 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
606 if (strlen(s_hex
) == 0) {
607 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
611 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
612 kprobe
->probe_addr
= 0;
616 /* Check for symbol */
617 if (isalpha(name
[0])) {
618 ret
= sscanf(opt
, "%s", name
);
620 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", name
);
621 ret
= asprintf(&kprobe
->symbol_name
, "%s", name
);
622 kprobe
->probe_offset
= 0;
623 kprobe
->probe_addr
= 0;
628 /* Check for address */
629 ret
= sscanf(opt
, "%s", s_hex
);
631 if (strlen(s_hex
) == 0) {
632 fprintf(stderr
, "Invalid probe address %s", s_hex
);
636 ret
= asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
637 kprobe
->probe_offset
= 0;
638 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
650 * Return 0 if caller should continue, < 0 if caller should return
651 * error, > 0 if caller should exit without reporting error.
653 static int parse_options(int argc
, char **argv
)
662 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
663 poptReadDefaultConfig(pc
, 0);
665 while ((opt
= poptGetNextOpt(pc
)) != -1) {
669 ret
= 1; /* exit cleanly */
679 tid_filter_list
= g_hash_table_new(g_str_hash
,
681 tmp_str
= strtok(opt_tid
, ",");
683 tid
= malloc(sizeof(int));
684 *tid
= atoi(tmp_str
);
685 g_hash_table_insert(tid_filter_list
,
686 (gpointer
) tid
, tid
);
687 tmp_str
= strtok(NULL
, ",");
691 /* start reading the live trace from the beginning */
696 tmp_str
= strtok(opt_hostname
, ",");
698 add_hostname_list(tmp_str
, 1);
699 tmp_str
= strtok(NULL
, ",");
702 case OPT_RELAY_HOSTNAME
:
706 lttngtop
.kprobes_table
= g_ptr_array_new();
707 tmp_str
= strtok(opt_kprobes
, ",");
709 struct kprobes
*kprobe
;
711 kprobe
= parse_probe_opts(tmp_str
);
714 lttngtop
.kprobes_table
,
720 tmp_str
= strtok(NULL
, ",");
729 opt_input_path
= poptGetArg(pc
);
738 void iter_trace(struct bt_context
*bt_ctx
)
740 struct bt_ctf_iter
*iter
;
741 struct bt_iter_pos begin_pos
;
742 struct kprobes
*kprobe
;
743 const struct bt_ctf_event
*event
;
747 begin_pos
.type
= BT_SEEK_BEGIN
;
748 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
750 /* at each event, verify the status of the process table */
751 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
754 /* to handle the follow child option */
755 bt_ctf_iter_add_callback(iter
,
756 g_quark_from_static_string("sched_process_fork"),
757 NULL
, 0, handle_sched_process_fork
, NULL
, NULL
, NULL
);
759 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
763 /* at each event check if we need to refresh */
764 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
767 /* to handle the scheduling events */
768 bt_ctf_iter_add_callback(iter
,
769 g_quark_from_static_string("sched_switch"),
770 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
771 /* to clean up the process table */
772 bt_ctf_iter_add_callback(iter
,
773 g_quark_from_static_string("sched_process_free"),
774 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
775 /* to get all the process from the statedumps */
776 bt_ctf_iter_add_callback(iter
,
777 g_quark_from_static_string(
778 "lttng_statedump_process_state"),
779 NULL
, 0, handle_statedump_process_state
,
783 bt_ctf_iter_add_callback(iter
,
784 g_quark_from_static_string("exit_syscall"),
785 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
786 bt_ctf_iter_add_callback(iter
,
787 g_quark_from_static_string("sys_write"),
788 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
789 bt_ctf_iter_add_callback(iter
,
790 g_quark_from_static_string("sys_read"),
791 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
792 bt_ctf_iter_add_callback(iter
,
793 g_quark_from_static_string("sys_open"),
794 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
795 bt_ctf_iter_add_callback(iter
,
796 g_quark_from_static_string("sys_close"),
797 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
798 bt_ctf_iter_add_callback(iter
,
799 g_quark_from_static_string(
800 "lttng_statedump_file_descriptor"),
801 NULL
, 0, handle_statedump_file_descriptor
,
805 if (lttngtop
.kprobes_table
) {
806 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
807 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
808 bt_ctf_iter_add_callback(iter
,
809 g_quark_from_static_string(
811 NULL
, 0, handle_kprobes
,
817 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
818 if (quit
|| reload_trace
)
820 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
825 /* block until quit, we reached the end of the trace */
826 sem_wait(&end_trace_sem
);
829 bt_ctf_iter_destroy(iter
);
833 * bt_context_add_traces_recursive: Open a trace recursively
834 * (copied from BSD code in converter/babeltrace.c)
836 * Find each trace present in the subdirectory starting from the given
837 * path, and add them to the context. The packet_seek parameter can be
838 * NULL: this specify to use the default format packet_seek.
840 * Return: 0 on success, nonzero on failure.
841 * Unable to open toplevel: failure.
842 * Unable to open some subdirectory or file: warn and continue;
844 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
845 const char *format_str
,
846 void (*packet_seek
)(struct bt_stream_pos
*pos
,
847 size_t offset
, int whence
))
852 char lpath
[PATH_MAX
];
853 char * const paths
[2] = { lpath
, NULL
};
856 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
857 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
858 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
859 ret
= bt_context_add_trace(ctx
,
860 path
, format_str
, packet_seek
, NULL
, NULL
);
862 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
863 "for reading.\n", path
);
864 /* Allow to skip erroneous traces. */
865 ret
= 1; /* partial error */
870 * Need to copy path, because fts_open can change it.
871 * It is the pointer array, not the strings, that are constant.
873 strncpy(lpath
, path
, PATH_MAX
);
874 lpath
[PATH_MAX
- 1] = '\0';
876 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
878 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
883 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
885 while ((node
= fts_read(tree
))) {
888 if (!(node
->fts_info
& FTS_D
))
891 dirfd
= open(node
->fts_accpath
, 0);
893 fprintf(stderr
, "[error] [Context] Unable to open trace "
894 "directory file descriptor.\n");
898 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
916 trace_id
= bt_context_add_trace(ctx
,
917 node
->fts_accpath
, format_str
,
918 packet_seek
, NULL
, NULL
);
920 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
921 "for reading.\n", node
->fts_accpath
, path
);
922 /* Allow to skip erroneous traces. */
925 g_array_append_val(trace_ids
, trace_id
);
929 g_array_free(trace_ids
, TRUE
);
936 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
937 int field_cnt
, int *tid_check
, int *pid_check
,
938 int *procname_check
, int *ppid_check
)
941 struct perfcounter
*global
;
944 for (j
= 0; j
< field_cnt
; j
++) {
945 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
946 if (*tid_check
== 0) {
947 if (strncmp(name
, "tid", 3) == 0)
950 if (*pid_check
== 0) {
951 if (strncmp(name
, "pid", 3) == 0)
954 if (*ppid_check
== 0) {
955 if (strncmp(name
, "ppid", 4) == 0)
958 if (*procname_check
== 0) {
959 if (strncmp(name
, "procname", 8) == 0)
962 if (strncmp(name
, "perf_", 5) == 0) {
963 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
965 global
= g_new0(struct perfcounter
, 1);
966 /* by default, sort on the first perf context */
967 if (g_hash_table_size(global_perf_liszt
) == 0)
970 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
975 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
976 *procname_check
== 1)
983 * check_requirements: check if the required context informations are available
985 * If each mandatory context information is available for at least in one
986 * event, return 0 otherwise return -1.
988 int check_requirements(struct bt_context
*ctx
)
990 unsigned int i
, evt_cnt
, field_cnt
;
991 struct bt_ctf_event_decl
*const * evt_list
;
992 const struct bt_ctf_field_decl
*const * field_list
;
995 int procname_check
= 0;
999 ret
= bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
1004 for (i
= 0; i
< evt_cnt
; i
++) {
1005 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
1006 &field_list
, &field_cnt
);
1007 ret
= check_field_requirements(field_list
, field_cnt
,
1008 &tid_check
, &pid_check
, &procname_check
,
1011 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
1012 &field_list
, &field_cnt
);
1013 ret
= check_field_requirements(field_list
, field_cnt
,
1014 &tid_check
, &pid_check
, &procname_check
,
1017 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
1018 &field_list
, &field_cnt
);
1019 ret
= check_field_requirements(field_list
, field_cnt
,
1020 &tid_check
, &pid_check
, &procname_check
,
1024 if (tid_check
== 0) {
1026 fprintf(stderr
, "[error] missing tid context information\n");
1028 if (pid_check
== 0) {
1030 fprintf(stderr
, "[error] missing pid context information\n");
1032 if (ppid_check
== 0) {
1034 fprintf(stderr
, "[error] missing ppid context information\n");
1036 if (procname_check
== 0) {
1038 fprintf(stderr
, "[error] missing procname context information\n");
1045 int main(int argc
, char **argv
)
1048 struct bt_context
*bt_ctx
= NULL
;
1051 ret
= parse_options(argc
, argv
);
1053 fprintf(stdout
, "Error parsing options.\n\n");
1056 } else if (ret
> 0) {
1060 if (!opt_input_path
&& !remote_live
) {
1062 #ifdef LTTNGTOP_MMAP_LIVE
1064 signal(SIGTERM
, handle_textdump_sigterm
);
1065 signal(SIGINT
, handle_textdump_sigterm
);
1067 mmap_live_loop(bt_ctx
);
1068 pthread_join(timer_thread
, NULL
);
1070 pthread_join(display_thread
, NULL
);
1072 lttng_stop_tracing("test");
1073 lttng_destroy_session("test");
1077 fprintf(stderr
, "[ERROR] Mmap live support not compiled, specify a "
1078 "trace directory or -r <relayd hostname/IP>\n");
1082 #endif /* LTTNGTOP_MMAP_LIVE */
1083 } else if (!opt_input_path
&& remote_live
) {
1086 ret
= setup_network_live(opt_relay_hostname
, opt_begin
);
1091 ret
= open_trace(&bt_ctx
);
1097 bt_ctx
= bt_context_create();
1098 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_relay_hostname
,
1099 "lttng-live", NULL
);
1101 fprintf(stderr
, "[error] Opening the trace\n");
1107 bt_ctx
= bt_context_create();
1108 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1110 fprintf(stderr
, "[error] Opening the trace\n");
1114 ret
= check_requirements(bt_ctx
);
1116 fprintf(stderr
, "[error] some mandatory contexts "
1117 "were missing, exiting.\n");
1121 if (!opt_textdump
) {
1122 pthread_create(&display_thread
, NULL
, ncurses_display
,
1124 pthread_create(&timer_thread
, NULL
, refresh_thread
,
1132 pthread_join(display_thread
, NULL
);
1134 pthread_join(timer_thread
, NULL
);
1140 bt_context_put(bt_ctx
);