2 * Copyright (C) 2011-2012 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 #include <lttng/lttngtop-helper.h>
43 #include <babeltrace/lttngtopmmappacketseek.h>
45 #include "lttngtoptypes.h"
47 #include "iostreamtop.h"
48 #include "cursesdisplay.h"
51 #define DEFAULT_FILE_ARRAY_SIZE 1
53 const char *opt_input_path
;
54 static int opt_textdump
;
59 struct lttngtop
*copy
;
60 pthread_t display_thread
;
61 pthread_t timer_thread
;
63 unsigned long refresh_display
= 1 * NSEC_PER_SEC
;
64 unsigned long last_display_update
= 0;
66 /* list of FDs available for being read with snapshots */
67 struct mmap_stream_list mmap_list
;
68 GPtrArray
*lttng_consumer_stream_array
;
69 int sessiond_metadata
, consumerd_metadata
;
70 struct lttng_consumer_local_data
*ctx
= NULL
;
71 /* list of snapshots currently not consumed */
72 GPtrArray
*available_snapshots
;
73 sem_t metadata_available
;
76 int last_textdump_print_newline
= 1;
88 static struct poptOption long_options
[] = {
89 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
90 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
91 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
92 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
93 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
94 { "hostname", 'n', POPT_ARG_STRING
, &opt_hostname
, OPT_HOSTNAME
, NULL
, NULL
},
95 { "kprobes", 'k', POPT_ARG_STRING
, &opt_kprobes
, OPT_KPROBES
, NULL
, NULL
},
96 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
99 static void handle_textdump_sigterm(int signal
)
102 lttng_destroy_session("test");
105 void *refresh_thread(void *p
)
107 struct mmap_stream
*mmap_info
;
111 sem_post(&pause_sem
);
113 sem_post(&end_trace_sem
);
114 sem_post(&goodtodisplay
);
115 sem_post(&goodtoupdate
);
118 if (!opt_input_path
) {
119 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
)
120 helper_kernctl_buffer_flush(mmap_info
->fd
);
122 sem_wait(&pause_sem
);
123 sem_post(&pause_sem
);
125 sleep(refresh_display
/NSEC_PER_SEC
);
129 void *ncurses_display(void *p
)
131 unsigned int current_display_index
= 0;
133 sem_wait(&bootstrap
);
135 * Prevent the 1 second delay when we hit ESC
142 sem_wait(&goodtodisplay
);
143 sem_wait(&pause_sem
);
146 sem_post(&pause_sem
);
152 copy
= g_ptr_array_index(copies
, current_display_index
);
154 display(current_display_index
++);
156 sem_post(&goodtoupdate
);
157 sem_post(&pause_sem
);
161 void print_fields(struct bt_ctf_event
*event
)
164 const struct definition
*const * list
;
165 const struct declaration
*l
;
166 const struct definition
*scope
;
167 enum ctf_type_id type
;
170 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
172 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
173 for (i
= 0; i
< cnt
; i
++) {
176 printf("%s = ", bt_ctf_field_name(list
[i
]));
177 l
= bt_ctf_get_decl_from_def(list
[i
]);
178 type
= bt_ctf_field_type(l
);
179 if (type
== CTF_TYPE_INTEGER
) {
180 if (bt_ctf_get_int_signedness(l
) == 0)
181 printf("%" PRIu64
"", bt_ctf_get_uint64(list
[i
]));
183 printf("%" PRId64
"", bt_ctf_get_int64(list
[i
]));
184 } else if (type
== CTF_TYPE_STRING
) {
185 printf("%s", bt_ctf_get_string(list
[i
]));
186 } else if (type
== CTF_TYPE_ARRAY
) {
187 str
= bt_ctf_get_char_array(list
[i
]);
188 if (!bt_ctf_field_get_error() && str
)
195 * hook on each event to check the timestamp and refresh the display if
198 enum bt_cb_ret
print_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
200 unsigned long timestamp
;
202 uint64_t ts_nsec_start
;
205 const struct definition
*scope
;
206 const char *hostname
, *procname
;
208 timestamp
= bt_ctf_get_timestamp(call_data
);
210 start
= format_timestamp(timestamp
);
211 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
213 pid
= get_context_pid(call_data
);
214 if (pid
== -1ULL && opt_tid
) {
218 hostname
= get_context_hostname(call_data
);
219 if (opt_tid
|| opt_hostname
)
220 if (!lookup_filter_tid_list(pid
))
223 cpu_id
= get_cpu_id(call_data
);
224 procname
= get_context_comm(call_data
);
226 if ((strcmp(bt_ctf_event_name(call_data
), "exit_syscall") == 0) &&
227 !last_textdump_print_newline
) {
228 scope
= bt_ctf_get_top_level_scope(call_data
,
230 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
232 printf("= %ld\n", syscall_ret
);
233 last_textdump_print_newline
= 1;
235 /* we might have lost the exit_syscall event, so need to
236 * print the newline in this case */
237 if (last_textdump_print_newline
== 0)
239 printf("%02d:%02d:%02d.%09" PRIu64
" (%s) (cpu %d) [%s (%d)] %s (",
240 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
241 ts_nsec_start
, hostname
, cpu_id
, procname
, pid
,
242 bt_ctf_event_name(call_data
));
243 print_fields(call_data
);
245 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) != 0) {
247 last_textdump_print_newline
= 1;
249 last_textdump_print_newline
= 0;
256 return BT_CB_ERROR_STOP
;
259 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
262 struct kprobes
*kprobe
;
265 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
266 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
267 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
276 * hook on each event to check the timestamp and refresh the display if
279 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
281 unsigned long timestamp
;
283 timestamp
= bt_ctf_get_timestamp(call_data
);
284 if (timestamp
== -1ULL)
287 if (last_display_update
== 0)
288 last_display_update
= timestamp
;
290 if (timestamp
- last_display_update
>= refresh_display
) {
291 sem_wait(&goodtoupdate
);
292 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
294 sem_post(&goodtodisplay
);
295 sem_post(&bootstrap
);
296 last_display_update
= timestamp
;
301 fprintf(stderr
, "check_timestamp callback error\n");
302 return BT_CB_ERROR_STOP
;
306 * get_perf_counter : get or create and return a perf_counter struct for
307 * either a process or a cpu (only one of the 2 parameters mandatory)
309 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
312 struct perfcounter
*ret
;
322 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
326 ret
= g_new0(struct perfcounter
, 1);
327 /* by default, make it visible in the UI */
329 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
338 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
339 const char *name
, int value
)
341 struct perfcounter
*cpu_perf
, *process_perf
;
343 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
344 if (cpu_perf
->count
< value
) {
345 process_perf
= get_perf_counter(name
, proc
, NULL
);
346 process_perf
->count
+= value
- cpu_perf
->count
;
347 cpu_perf
->count
= value
;
351 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
352 const struct bt_definition
*scope
,
353 struct processtop
*proc
,
356 struct bt_definition
const * const *list
= NULL
;
357 const struct bt_definition
*field
;
359 struct perfcounter
*perfcounter
;
367 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
374 g_hash_table_iter_init(&iter
, global_perf_liszt
);
375 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
376 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
378 int value
= bt_ctf_get_uint64(field
);
379 if (bt_ctf_field_get_error())
381 update_perf_value(proc
, cpu
, (char *) key
, value
);
389 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
392 const struct bt_definition
*scope
;
394 cpu
= get_cpu(get_cpu_id(event
));
396 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
397 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
399 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
400 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
402 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
403 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
406 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
409 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
410 char *comm
, *hostname
;
411 struct processtop
*parent
, *child
;
412 unsigned long timestamp
;
414 timestamp
= bt_ctf_get_timestamp(call_data
);
415 if (timestamp
== -1ULL)
418 pid
= get_context_pid(call_data
);
423 tid
= get_context_tid(call_data
);
427 ppid
= get_context_ppid(call_data
);
431 vpid
= get_context_vpid(call_data
);
435 vtid
= get_context_vtid(call_data
);
439 vppid
= get_context_vppid(call_data
);
443 comm
= get_context_comm(call_data
);
448 hostname
= get_context_hostname(call_data
);
450 /* find or create the current process */
451 child
= find_process_tid(<tngtop
, tid
, comm
);
453 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
456 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
459 /* find or create the parent */
460 parent
= find_process_tid(<tngtop
, pid
, comm
);
462 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
467 /* attach the parent to the current process */
468 child
->threadparent
= parent
;
469 add_thread(parent
, child
);
472 update_perf_counter(child
, call_data
);
478 return BT_CB_ERROR_STOP
;
483 copies
= g_ptr_array_new();
484 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
485 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
486 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
488 sem_init(&goodtodisplay
, 0, 0);
489 sem_init(&goodtoupdate
, 0, 1);
490 sem_init(&timer
, 0, 1);
491 sem_init(&bootstrap
, 0, 0);
492 sem_init(&pause_sem
, 0, 1);
493 sem_init(&end_trace_sem
, 0, 0);
495 reset_global_counters();
497 lttngtop
.nbthreads
= 0;
498 lttngtop
.nbfiles
= 0;
500 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
502 lttngtop
.process_table
= g_ptr_array_new();
503 lttngtop
.files_table
= g_ptr_array_new();
504 lttngtop
.cpu_table
= g_ptr_array_new();
511 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
512 fprintf(fp
, "Usage : lttngtop [OPTIONS] [TRACE]\n");
513 fprintf(fp
, " TRACE Path to the trace to analyse (no trace path for live tracing)\n");
514 fprintf(fp
, " -h, --help This help message\n");
515 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
516 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
517 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
518 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
519 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
523 * Parse probe options.
524 * Shamelessly stolen from lttng-tools :
525 * src/bin/lttng/commands/enable_events.c
527 static struct kprobes
*parse_probe_opts(char *opt
)
530 char name
[LTTNG_SYMBOL_NAME_LEN
];
531 struct kprobes
*kprobe
;
535 kprobe->probe_addr = 0;
536 kprobe->probe_offset = 0;
537 asprintf(&kprobe->probe_name, "probe_sys_open");
538 asprintf(&kprobe->symbol_name, "sys_open");
546 kprobe
= g_new0(struct kprobes
, 1);
548 /* Check for symbol+offset */
549 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
551 asprintf(&kprobe
->probe_name
, "probe_%s", name
);
552 asprintf(&kprobe
->symbol_name
, "%s", name
);
554 if (strlen(s_hex
) == 0) {
555 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
559 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
560 kprobe
->probe_addr
= 0;
564 /* Check for symbol */
565 if (isalpha(name
[0])) {
566 ret
= sscanf(opt
, "%s", name
);
568 asprintf(&kprobe
->probe_name
, "probe_%s", name
);
569 asprintf(&kprobe
->symbol_name
, "%s", name
);
570 kprobe
->probe_offset
= 0;
571 kprobe
->probe_addr
= 0;
576 /* Check for address */
577 ret
= sscanf(opt
, "%s", s_hex
);
579 if (strlen(s_hex
) == 0) {
580 fprintf(stderr
, "Invalid probe address %s", s_hex
);
584 asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
585 kprobe
->probe_offset
= 0;
586 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
598 * Return 0 if caller should continue, < 0 if caller should return
599 * error, > 0 if caller should exit without reporting error.
601 static int parse_options(int argc
, char **argv
)
608 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
609 poptReadDefaultConfig(pc
, 0);
611 while ((opt
= poptGetNextOpt(pc
)) != -1) {
615 ret
= 1; /* exit cleanly */
626 tid_filter_list
= g_hash_table_new(g_str_hash
,
628 tmp_str
= strtok(opt_tid
, ",");
630 tid
= malloc(sizeof(int));
631 *tid
= atoi(tmp_str
);
632 g_hash_table_insert(tid_filter_list
,
633 (gpointer
) tid
, tid
);
634 tmp_str
= strtok(NULL
, ",");
639 tmp_str
= strtok(opt_hostname
, ",");
641 add_hostname_list(tmp_str
, 1);
642 tmp_str
= strtok(NULL
, ",");
646 lttngtop
.kprobes_table
= g_ptr_array_new();
647 tmp_str
= strtok(opt_kprobes
, ",");
649 struct kprobes
*kprobe
;
651 kprobe
= parse_probe_opts(tmp_str
);
654 lttngtop
.kprobes_table
,
660 tmp_str
= strtok(NULL
, ",");
669 opt_input_path
= poptGetArg(pc
);
678 void iter_trace(struct bt_context
*bt_ctx
)
680 struct bt_ctf_iter
*iter
;
681 struct bt_iter_pos begin_pos
;
682 struct kprobes
*kprobe
;
683 const struct bt_ctf_event
*event
;
687 begin_pos
.type
= BT_SEEK_BEGIN
;
688 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
690 /* at each event, verify the status of the process table */
691 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
695 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
699 /* at each event check if we need to refresh */
700 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
703 /* to handle the scheduling events */
704 bt_ctf_iter_add_callback(iter
,
705 g_quark_from_static_string("sched_switch"),
706 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
707 /* to clean up the process table */
708 bt_ctf_iter_add_callback(iter
,
709 g_quark_from_static_string("sched_process_free"),
710 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
711 /* to get all the process from the statedumps */
712 bt_ctf_iter_add_callback(iter
,
713 g_quark_from_static_string(
714 "lttng_statedump_process_state"),
715 NULL
, 0, handle_statedump_process_state
,
719 bt_ctf_iter_add_callback(iter
,
720 g_quark_from_static_string("exit_syscall"),
721 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
722 bt_ctf_iter_add_callback(iter
,
723 g_quark_from_static_string("sys_write"),
724 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
725 bt_ctf_iter_add_callback(iter
,
726 g_quark_from_static_string("sys_read"),
727 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
728 bt_ctf_iter_add_callback(iter
,
729 g_quark_from_static_string("sys_open"),
730 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
731 bt_ctf_iter_add_callback(iter
,
732 g_quark_from_static_string("sys_close"),
733 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
734 bt_ctf_iter_add_callback(iter
,
735 g_quark_from_static_string(
736 "lttng_statedump_file_descriptor"),
737 NULL
, 0, handle_statedump_file_descriptor
,
741 if (lttngtop
.kprobes_table
) {
742 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
743 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
744 bt_ctf_iter_add_callback(iter
,
745 g_quark_from_static_string(
747 NULL
, 0, handle_kprobes
,
753 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
754 if (quit
|| reload_trace
)
756 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
761 /* block until quit, we reached the end of the trace */
762 sem_wait(&end_trace_sem
);
765 bt_ctf_iter_destroy(iter
);
769 * bt_context_add_traces_recursive: Open a trace recursively
770 * (copied from BSD code in converter/babeltrace.c)
772 * Find each trace present in the subdirectory starting from the given
773 * path, and add them to the context. The packet_seek parameter can be
774 * NULL: this specify to use the default format packet_seek.
776 * Return: 0 on success, nonzero on failure.
777 * Unable to open toplevel: failure.
778 * Unable to open some subdirectory or file: warn and continue;
780 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
781 const char *format_str
,
782 void (*packet_seek
)(struct bt_stream_pos
*pos
,
783 size_t offset
, int whence
))
788 char lpath
[PATH_MAX
];
789 char * const paths
[2] = { lpath
, NULL
};
793 * Need to copy path, because fts_open can change it.
794 * It is the pointer array, not the strings, that are constant.
796 strncpy(lpath
, path
, PATH_MAX
);
797 lpath
[PATH_MAX
- 1] = '\0';
799 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
801 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
806 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
808 while ((node
= fts_read(tree
))) {
811 if (!(node
->fts_info
& FTS_D
))
814 dirfd
= open(node
->fts_accpath
, 0);
816 fprintf(stderr
, "[error] [Context] Unable to open trace "
817 "directory file descriptor.\n");
821 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
840 trace_id
= bt_context_add_trace(ctx
,
841 node
->fts_accpath
, format_str
,
842 packet_seek
, NULL
, NULL
);
844 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
845 "for reading.\n", node
->fts_accpath
, path
);
846 /* Allow to skip erroneous traces. */
849 g_array_append_val(trace_ids
, trace_id
);
853 g_array_free(trace_ids
, TRUE
);
860 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
861 int field_cnt
, int *tid_check
, int *pid_check
,
862 int *procname_check
, int *ppid_check
)
865 struct perfcounter
*global
;
868 for (j
= 0; j
< field_cnt
; j
++) {
869 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
870 if (*tid_check
== 0) {
871 if (strncmp(name
, "tid", 3) == 0)
874 if (*pid_check
== 0) {
875 if (strncmp(name
, "pid", 3) == 0)
878 if (*ppid_check
== 0) {
879 if (strncmp(name
, "ppid", 4) == 0)
882 if (*procname_check
== 0) {
883 if (strncmp(name
, "procname", 8) == 0)
886 if (strncmp(name
, "perf_", 5) == 0) {
887 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
889 global
= g_new0(struct perfcounter
, 1);
890 /* by default, sort on the first perf context */
891 if (g_hash_table_size(global_perf_liszt
) == 0)
894 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
899 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
900 *procname_check
== 1)
907 * check_requirements: check if the required context informations are available
909 * If each mandatory context information is available for at least in one
910 * event, return 0 otherwise return -1.
912 int check_requirements(struct bt_context
*ctx
)
914 unsigned int i
, evt_cnt
, field_cnt
;
915 struct bt_ctf_event_decl
*const * evt_list
;
916 const struct bt_ctf_field_decl
*const * field_list
;
919 int procname_check
= 0;
923 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
924 for (i
= 0; i
< evt_cnt
; i
++) {
925 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
926 &field_list
, &field_cnt
);
927 ret
= check_field_requirements(field_list
, field_cnt
,
928 &tid_check
, &pid_check
, &procname_check
,
931 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
932 &field_list
, &field_cnt
);
933 ret
= check_field_requirements(field_list
, field_cnt
,
934 &tid_check
, &pid_check
, &procname_check
,
937 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
938 &field_list
, &field_cnt
);
939 ret
= check_field_requirements(field_list
, field_cnt
,
940 &tid_check
, &pid_check
, &procname_check
,
944 if (tid_check
== 0) {
946 fprintf(stderr
, "[error] missing tid context information\n");
948 if (pid_check
== 0) {
950 fprintf(stderr
, "[error] missing pid context information\n");
952 if (ppid_check
== 0) {
954 fprintf(stderr
, "[error] missing ppid context information\n");
956 if (procname_check
== 0) {
958 fprintf(stderr
, "[error] missing procname context information\n");
964 ssize_t
read_subbuffer(struct lttng_consumer_stream
*kconsumerd_fd
,
965 struct lttng_consumer_local_data
*ctx
)
970 int infd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
972 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_SPLICE
) {
973 /* Get the next subbuffer */
974 err
= helper_kernctl_get_next_subbuf(infd
);
977 perror("Reserving sub buffer failed (everything is normal, "
978 "it is due to concurrency)");
981 /* read the whole subbuffer */
982 err
= helper_kernctl_get_padded_subbuf_size(infd
, &len
);
985 perror("Getting sub-buffer len failed.");
989 /* splice the subbuffer to the tracefile */
990 ret
= helper_lttng_consumer_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
, 0);
993 * display the error but continue processing to try
994 * to release the subbuffer
996 fprintf(stderr
,"Error splicing to tracefile\n");
998 err
= helper_kernctl_put_next_subbuf(infd
);
1001 perror("Reserving sub buffer failed (everything is normal, "
1002 "it is due to concurrency)");
1005 sem_post(&metadata_available
);
1012 int on_update_fd(int key
, uint32_t state
)
1014 /* let the lib handle the metadata FD */
1015 if (key
== sessiond_metadata
)
1020 int on_recv_fd(struct lttng_consumer_stream
*kconsumerd_fd
)
1023 struct mmap_stream
*new_mmap_stream
;
1025 /* Opening the tracefile in write mode */
1026 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
) != NULL
) {
1027 ret
= open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
),
1028 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1033 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd
, ret
);
1036 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_MMAP
) {
1037 new_mmap_stream
= malloc(sizeof(struct mmap_stream
));
1038 new_mmap_stream
->fd
= helper_get_lttng_consumer_stream_wait_fd(
1040 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
1042 g_ptr_array_add(lttng_consumer_stream_array
, kconsumerd_fd
);
1043 /* keep mmap FDs internally */
1046 consumerd_metadata
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
1047 sessiond_metadata
= helper_get_lttng_consumer_stream_key(kconsumerd_fd
);
1057 void live_consume(struct bt_context
**bt_ctx
)
1062 sem_wait(&metadata_available
);
1063 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
1064 fprintf(stderr
,"no metadata\n");
1067 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
1069 *bt_ctx
= bt_context_create();
1070 ret
= bt_context_add_trace(*bt_ctx
, NULL
, "ctf",
1071 lttngtop_ctf_packet_seek
, &mmap_list
, metadata_fp
);
1073 printf("Error adding trace\n");
1081 int setup_consumer(char *command_sock_path
, pthread_t
*threads
,
1082 struct lttng_consumer_local_data
*ctx
)
1086 ctx
= helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL
,
1087 read_subbuffer
, NULL
, on_recv_fd
, on_update_fd
);
1091 unlink(command_sock_path
);
1092 helper_lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
1093 helper_lttng_consumer_init();
1095 /* Create the thread to manage the receive of fd */
1096 ret
= pthread_create(&threads
[0], NULL
, helper_lttng_consumer_thread_sessiond_poll
,
1099 perror("pthread_create receive fd");
1102 /* Create thread to manage the polling/writing of traces */
1103 ret
= pthread_create(&threads
[1], NULL
, helper_lttng_consumer_thread_metadata_poll
,
1106 perror("pthread_create poll fd");
1114 int enable_kprobes(struct lttng_handle
*handle
, char *channel_name
)
1116 struct lttng_event ev
;
1117 struct kprobes
*kprobe
;
1121 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
1122 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
1124 memset(&ev
, '\0', sizeof(struct lttng_event
));
1125 ev
.type
= LTTNG_EVENT_PROBE
;
1126 if (kprobe
->symbol_name
)
1127 sprintf(ev
.attr
.probe
.symbol_name
, "%s", kprobe
->symbol_name
);
1128 sprintf(ev
.name
, "%s", kprobe
->probe_name
);
1129 ev
.attr
.probe
.addr
= kprobe
->probe_addr
;
1130 ev
.attr
.probe
.offset
= kprobe
->probe_offset
;
1131 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1132 fprintf(stderr
,"error enabling kprobes : %s\n",
1133 helper_lttcomm_get_readable_code(ret
));
1142 int setup_live_tracing()
1144 struct lttng_domain dom
;
1145 struct lttng_channel chan
;
1146 char *channel_name
= "mmapchan";
1147 struct lttng_event ev
;
1149 char *command_sock_path
= "/tmp/consumerd_sock";
1150 static pthread_t threads
[2]; /* recv_fd, poll */
1151 struct lttng_event_context kctxpid
, kctxcomm
, kctxppid
, kctxtid
,
1152 kctxperf1
, kctxperf2
;
1154 struct lttng_handle
*handle
;
1156 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1158 lttng_consumer_stream_array
= g_ptr_array_new();
1160 if ((ret
= setup_consumer(command_sock_path
, threads
, ctx
)) < 0) {
1161 fprintf(stderr
,"error setting up consumer\n");
1165 available_snapshots
= g_ptr_array_new();
1167 /* setup the session */
1168 dom
.type
= LTTNG_DOMAIN_KERNEL
;
1170 ret
= unlink("/tmp/livesession");
1172 lttng_destroy_session("test");
1173 if ((ret
= lttng_create_session("test", "/tmp/livesession")) < 0) {
1174 fprintf(stderr
,"error creating the session : %s\n",
1175 helper_lttcomm_get_readable_code(ret
));
1179 if ((handle
= lttng_create_handle("test", &dom
)) == NULL
) {
1180 fprintf(stderr
,"error creating handle\n");
1185 * FIXME : need to let the
1186 * helper_lttng_consumer_thread_receive_fds create the
1188 * Cleaner solution ?
1190 while (access(command_sock_path
, F_OK
)) {
1194 if ((ret
= lttng_register_consumer(handle
, command_sock_path
)) < 0) {
1195 fprintf(stderr
,"error registering consumer : %s\n",
1196 helper_lttcomm_get_readable_code(ret
));
1200 strcpy(chan
.name
, channel_name
);
1201 chan
.attr
.overwrite
= 0;
1202 if (opt_tid
&& opt_textdump
) {
1203 chan
.attr
.subbuf_size
= 32768;
1204 chan
.attr
.num_subbuf
= 8;
1206 //chan.attr.subbuf_size = 1048576; /* 1MB */
1207 chan
.attr
.subbuf_size
= 2097152; /* 1MB */
1208 chan
.attr
.num_subbuf
= 4;
1210 chan
.attr
.switch_timer_interval
= 0;
1211 chan
.attr
.read_timer_interval
= 200;
1212 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
1214 if ((ret
= lttng_enable_channel(handle
, &chan
)) < 0) {
1215 fprintf(stderr
,"error creating channel : %s\n",
1216 helper_lttcomm_get_readable_code(ret
));
1220 memset(&ev
, '\0', sizeof(struct lttng_event
));
1221 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1222 sprintf(ev
.name
, "sched_switch");
1223 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1224 fprintf(stderr
,"error enabling event %s : %s\n",
1226 helper_lttcomm_get_readable_code(ret
));
1229 sprintf(ev
.name
, "sched_process_free");
1230 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1231 fprintf(stderr
,"error enabling event %s : %s\n",
1233 helper_lttcomm_get_readable_code(ret
));
1236 sprintf(ev
.name
, "lttng_statedump_process_state");
1237 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1238 fprintf(stderr
,"error enabling event %s : %s\n",
1240 helper_lttcomm_get_readable_code(ret
));
1243 sprintf(ev
.name
, "lttng_statedump_file_descriptor");
1244 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1245 fprintf(stderr
,"error enabling event %s : %s\n",
1247 helper_lttcomm_get_readable_code(ret
));
1251 memset(&ev
, '\0', sizeof(struct lttng_event
));
1252 ev
.type
= LTTNG_EVENT_SYSCALL
;
1253 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1254 fprintf(stderr
,"error enabling syscalls : %s\n",
1255 helper_lttcomm_get_readable_code(ret
));
1259 if (lttngtop
.kprobes_table
) {
1260 ret
= enable_kprobes(handle
, channel_name
);
1266 kctxperf1
.ctx
= LTTNG_EVENT_CONTEXT_PERF_COUNTER
;
1267 kctxperf1
.u
.perf_counter
.type
= 0; /* PERF_TYPE_HARDWARE */
1268 kctxperf1
.u
.perf_counter
.config
= 5; /* PERF_COUNT_HW_BRANCH_MISSES */
1269 sprintf(kctxperf1
.u
.perf_counter
.name
, "perf_branch_misses");
1270 ret
= lttng_add_context(handle
, &kctxperf1
, NULL
, NULL
);
1272 fprintf(stderr
, "error enabling context %s\n",
1273 kctxtid
.u
.perf_counter
.name
);
1276 kctxperf2
.ctx
= LTTNG_EVENT_CONTEXT_PERF_COUNTER
;
1277 kctxperf2
.u
.perf_counter
.type
= 1; /* PERF_TYPE_SOFTWARE */
1278 kctxperf2
.u
.perf_counter
.config
= 6; /* PERF_COUNT_SW_PAGE_FAULTS_MAJ */
1279 sprintf(kctxperf2
.u
.perf_counter
.name
, "perf_major_faults");
1280 ret
= lttng_add_context(handle
, &kctxperf2
, NULL
, NULL
);
1282 fprintf(stderr
, "error enabling context %s\n",
1283 kctxtid
.u
.perf_counter
.name
);
1286 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_PID
;
1287 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1288 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_TID
;
1289 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1290 kctxppid
.ctx
= LTTNG_EVENT_CONTEXT_PPID
;
1291 lttng_add_context(handle
, &kctxppid
, NULL
, NULL
);
1292 kctxcomm
.ctx
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1293 lttng_add_context(handle
, &kctxcomm
, NULL
, NULL
);
1294 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_VPID
;
1295 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1296 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
1297 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1298 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1299 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1302 if ((ret
= lttng_start_tracing("test")) < 0) {
1303 fprintf(stderr
,"error starting tracing : %s\n",
1304 helper_lttcomm_get_readable_code(ret
));
1308 helper_kernctl_buffer_flush(consumerd_metadata
);
1310 /* block until metadata is ready */
1311 sem_init(&metadata_available
, 0, 0);
1316 lttng_destroy_session("test");
1321 int main(int argc
, char **argv
)
1324 struct bt_context
*bt_ctx
= NULL
;
1325 struct mmap_stream
*mmap_info
;
1326 unsigned long mmap_len
;
1329 ret
= parse_options(argc
, argv
);
1331 fprintf(stdout
, "Error parsing options.\n\n");
1334 } else if (ret
> 0) {
1338 if (!opt_input_path
) {
1340 signal(SIGTERM
, handle_textdump_sigterm
);
1341 signal(SIGINT
, handle_textdump_sigterm
);
1343 ret
= setup_live_tracing();
1347 if (!opt_textdump
) {
1348 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1349 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1353 live_consume(&bt_ctx
);
1354 ret
= check_requirements(bt_ctx
);
1356 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1361 * FIXME : pb with cleanup in libbabeltrace
1362 ret = bt_context_remove_trace(bt_ctx, 0);
1364 fprintf(stderr, "error removing trace\n");
1369 bt_context_put(bt_ctx
);
1373 * since we receive all FDs every time there is an
1374 * update and the FD number is different every time,
1375 * we don't know which one are valid.
1376 * so we check if all FDs are usable with a simple
1379 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
) {
1380 ret
= helper_kernctl_get_mmap_len(mmap_info
->fd
, &mmap_len
);
1382 bt_list_del(&mmap_info
->list
);
1385 sem_post(&metadata_available
);
1388 pthread_join(timer_thread
, NULL
);
1390 pthread_join(display_thread
, NULL
);
1392 lttng_stop_tracing("test");
1393 lttng_destroy_session("test");
1399 bt_ctx
= bt_context_create();
1400 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1402 fprintf(stderr
, "[error] Opening the trace\n");
1406 ret
= check_requirements(bt_ctx
);
1408 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1411 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1412 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1416 pthread_join(display_thread
, NULL
);
1418 pthread_join(timer_thread
, NULL
);
1423 bt_context_put(bt_ctx
);