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 definition
*scope
;
166 enum ctf_type_id type
;
169 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
171 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
172 for (i
= 0; i
< cnt
; i
++) {
175 printf("%s = ", bt_ctf_field_name(list
[i
]));
176 type
= bt_ctf_field_type(list
[i
]);
177 if (type
== CTF_TYPE_INTEGER
) {
178 if (bt_ctf_get_int_signedness(list
[i
]) == 0)
179 printf("%" PRIu64
"", bt_ctf_get_uint64(list
[i
]));
181 printf("%" PRId64
"", bt_ctf_get_int64(list
[i
]));
182 } else if (type
== CTF_TYPE_STRING
) {
183 printf("%s", bt_ctf_get_string(list
[i
]));
184 } else if (type
== CTF_TYPE_ARRAY
) {
185 str
= bt_ctf_get_char_array(list
[i
]);
186 if (!bt_ctf_field_get_error() && str
)
193 * hook on each event to check the timestamp and refresh the display if
196 enum bt_cb_ret
print_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
198 unsigned long timestamp
;
200 uint64_t ts_nsec_start
;
203 const struct definition
*scope
;
204 const char *hostname
, *procname
;
206 timestamp
= bt_ctf_get_timestamp(call_data
);
208 start
= format_timestamp(timestamp
);
209 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
211 pid
= get_context_pid(call_data
);
212 if (pid
== -1ULL && opt_tid
) {
216 hostname
= get_context_hostname(call_data
);
217 if (opt_tid
|| opt_hostname
)
218 if (!lookup_filter_tid_list(pid
))
221 cpu_id
= get_cpu_id(call_data
);
222 procname
= get_context_comm(call_data
);
224 if ((strcmp(bt_ctf_event_name(call_data
), "exit_syscall") == 0) &&
225 !last_textdump_print_newline
) {
226 scope
= bt_ctf_get_top_level_scope(call_data
,
228 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
230 printf("= %ld\n", syscall_ret
);
231 last_textdump_print_newline
= 1;
233 /* we might have lost the exit_syscall event, so need to
234 * print the newline in this case */
235 if (last_textdump_print_newline
== 0)
237 printf("%02d:%02d:%02d.%09" PRIu64
" (%s) (cpu %d) [%s (%d)] %s (",
238 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
239 ts_nsec_start
, hostname
, cpu_id
, procname
, pid
,
240 bt_ctf_event_name(call_data
));
241 print_fields(call_data
);
243 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) != 0) {
245 last_textdump_print_newline
= 1;
247 last_textdump_print_newline
= 0;
254 return BT_CB_ERROR_STOP
;
257 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
260 struct kprobes
*kprobe
;
263 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
264 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
265 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
274 * hook on each event to check the timestamp and refresh the display if
277 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
279 unsigned long timestamp
;
281 timestamp
= bt_ctf_get_timestamp(call_data
);
282 if (timestamp
== -1ULL)
285 if (last_display_update
== 0)
286 last_display_update
= timestamp
;
288 if (timestamp
- last_display_update
>= refresh_display
) {
289 sem_wait(&goodtoupdate
);
290 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
292 sem_post(&goodtodisplay
);
293 sem_post(&bootstrap
);
294 last_display_update
= timestamp
;
299 fprintf(stderr
, "check_timestamp callback error\n");
300 return BT_CB_ERROR_STOP
;
304 * get_perf_counter : get or create and return a perf_counter struct for
305 * either a process or a cpu (only one of the 2 parameters mandatory)
307 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
310 struct perfcounter
*ret
;
320 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
324 ret
= g_new0(struct perfcounter
, 1);
325 /* by default, make it visible in the UI */
327 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
336 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
337 const char *name
, int value
)
339 struct perfcounter
*cpu_perf
, *process_perf
;
341 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
342 if (cpu_perf
->count
< value
) {
343 process_perf
= get_perf_counter(name
, proc
, NULL
);
344 process_perf
->count
+= value
- cpu_perf
->count
;
345 cpu_perf
->count
= value
;
349 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
350 const struct bt_definition
*scope
,
351 struct processtop
*proc
,
354 struct bt_definition
const * const *list
= NULL
;
355 const struct bt_definition
*field
;
357 struct perfcounter
*perfcounter
;
365 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
372 g_hash_table_iter_init(&iter
, global_perf_liszt
);
373 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
374 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
376 int value
= bt_ctf_get_uint64(field
);
377 if (bt_ctf_field_get_error())
379 update_perf_value(proc
, cpu
, (char *) key
, value
);
387 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
390 const struct bt_definition
*scope
;
392 cpu
= get_cpu(get_cpu_id(event
));
394 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
395 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
397 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
398 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
400 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
401 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
404 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
407 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
408 char *comm
, *hostname
;
409 struct processtop
*parent
, *child
;
410 unsigned long timestamp
;
412 timestamp
= bt_ctf_get_timestamp(call_data
);
413 if (timestamp
== -1ULL)
416 pid
= get_context_pid(call_data
);
421 tid
= get_context_tid(call_data
);
425 ppid
= get_context_ppid(call_data
);
429 vpid
= get_context_vpid(call_data
);
433 vtid
= get_context_vtid(call_data
);
437 vppid
= get_context_vppid(call_data
);
441 comm
= get_context_comm(call_data
);
446 hostname
= get_context_hostname(call_data
);
448 /* find or create the current process */
449 child
= find_process_tid(<tngtop
, tid
, comm
);
451 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
454 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
457 /* find or create the parent */
458 parent
= find_process_tid(<tngtop
, pid
, comm
);
460 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
465 /* attach the parent to the current process */
466 child
->threadparent
= parent
;
467 add_thread(parent
, child
);
470 update_perf_counter(child
, call_data
);
476 return BT_CB_ERROR_STOP
;
481 copies
= g_ptr_array_new();
482 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
483 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
484 global_host_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
486 sem_init(&goodtodisplay
, 0, 0);
487 sem_init(&goodtoupdate
, 0, 1);
488 sem_init(&timer
, 0, 1);
489 sem_init(&bootstrap
, 0, 0);
490 sem_init(&pause_sem
, 0, 1);
491 sem_init(&end_trace_sem
, 0, 0);
493 reset_global_counters();
495 lttngtop
.nbthreads
= 0;
496 lttngtop
.nbfiles
= 0;
498 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
500 lttngtop
.process_table
= g_ptr_array_new();
501 lttngtop
.files_table
= g_ptr_array_new();
502 lttngtop
.cpu_table
= g_ptr_array_new();
509 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
510 fprintf(fp
, "Usage : lttngtop [OPTIONS] [TRACE]\n");
511 fprintf(fp
, " TRACE Path to the trace to analyse (no trace path for live tracing)\n");
512 fprintf(fp
, " -h, --help This help message\n");
513 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
514 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
515 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
516 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
517 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
521 * Parse probe options.
522 * Shamelessly stolen from lttng-tools :
523 * src/bin/lttng/commands/enable_events.c
525 static struct kprobes
*parse_probe_opts(char *opt
)
528 char name
[LTTNG_SYMBOL_NAME_LEN
];
529 struct kprobes
*kprobe
;
533 kprobe->probe_addr = 0;
534 kprobe->probe_offset = 0;
535 asprintf(&kprobe->probe_name, "probe_sys_open");
536 asprintf(&kprobe->symbol_name, "sys_open");
544 kprobe
= g_new0(struct kprobes
, 1);
546 /* Check for symbol+offset */
547 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
549 asprintf(&kprobe
->probe_name
, "probe_%s", name
);
550 asprintf(&kprobe
->symbol_name
, "%s", name
);
552 if (strlen(s_hex
) == 0) {
553 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
557 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
558 kprobe
->probe_addr
= 0;
562 /* Check for symbol */
563 if (isalpha(name
[0])) {
564 ret
= sscanf(opt
, "%s", name
);
566 asprintf(&kprobe
->probe_name
, "probe_%s", name
);
567 asprintf(&kprobe
->symbol_name
, "%s", name
);
568 kprobe
->probe_offset
= 0;
569 kprobe
->probe_addr
= 0;
574 /* Check for address */
575 ret
= sscanf(opt
, "%s", s_hex
);
577 if (strlen(s_hex
) == 0) {
578 fprintf(stderr
, "Invalid probe address %s", s_hex
);
582 asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
583 kprobe
->probe_offset
= 0;
584 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
596 * Return 0 if caller should continue, < 0 if caller should return
597 * error, > 0 if caller should exit without reporting error.
599 static int parse_options(int argc
, char **argv
)
606 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
607 poptReadDefaultConfig(pc
, 0);
609 while ((opt
= poptGetNextOpt(pc
)) != -1) {
613 ret
= 1; /* exit cleanly */
624 tid_filter_list
= g_hash_table_new(g_str_hash
,
626 tmp_str
= strtok(opt_tid
, ",");
628 tid
= malloc(sizeof(int));
629 *tid
= atoi(tmp_str
);
630 g_hash_table_insert(tid_filter_list
,
631 (gpointer
) tid
, tid
);
632 tmp_str
= strtok(NULL
, ",");
637 tmp_str
= strtok(opt_hostname
, ",");
639 // char *new_str = strdup(tmp_str);
642 host
= g_new0(struct host
, 1);
643 host
->hostname
= strdup(tmp_str
);
645 g_hash_table_insert(global_host_list
,
646 (gpointer
) host
->hostname
,
648 tmp_str
= strtok(NULL
, ",");
652 lttngtop
.kprobes_table
= g_ptr_array_new();
653 tmp_str
= strtok(opt_kprobes
, ",");
655 struct kprobes
*kprobe
;
657 kprobe
= parse_probe_opts(tmp_str
);
660 lttngtop
.kprobes_table
,
666 tmp_str
= strtok(NULL
, ",");
675 opt_input_path
= poptGetArg(pc
);
684 void iter_trace(struct bt_context
*bt_ctx
)
686 struct bt_ctf_iter
*iter
;
687 struct bt_iter_pos begin_pos
;
688 struct kprobes
*kprobe
;
689 const struct bt_ctf_event
*event
;
693 begin_pos
.type
= BT_SEEK_BEGIN
;
694 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
696 /* at each event, verify the status of the process table */
697 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
701 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
705 /* at each event check if we need to refresh */
706 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
709 /* to handle the scheduling events */
710 bt_ctf_iter_add_callback(iter
,
711 g_quark_from_static_string("sched_switch"),
712 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
713 /* to clean up the process table */
714 bt_ctf_iter_add_callback(iter
,
715 g_quark_from_static_string("sched_process_free"),
716 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
717 /* to get all the process from the statedumps */
718 bt_ctf_iter_add_callback(iter
,
719 g_quark_from_static_string(
720 "lttng_statedump_process_state"),
721 NULL
, 0, handle_statedump_process_state
,
725 bt_ctf_iter_add_callback(iter
,
726 g_quark_from_static_string("exit_syscall"),
727 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
728 bt_ctf_iter_add_callback(iter
,
729 g_quark_from_static_string("sys_write"),
730 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
731 bt_ctf_iter_add_callback(iter
,
732 g_quark_from_static_string("sys_read"),
733 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
734 bt_ctf_iter_add_callback(iter
,
735 g_quark_from_static_string("sys_open"),
736 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
737 bt_ctf_iter_add_callback(iter
,
738 g_quark_from_static_string("sys_close"),
739 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
740 bt_ctf_iter_add_callback(iter
,
741 g_quark_from_static_string(
742 "lttng_statedump_file_descriptor"),
743 NULL
, 0, handle_statedump_file_descriptor
,
747 if (lttngtop
.kprobes_table
) {
748 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
749 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
750 bt_ctf_iter_add_callback(iter
,
751 g_quark_from_static_string(
753 NULL
, 0, handle_kprobes
,
759 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
760 if (quit
|| reload_trace
)
762 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
767 /* block until quit, we reached the end of the trace */
768 sem_wait(&end_trace_sem
);
771 bt_ctf_iter_destroy(iter
);
775 * bt_context_add_traces_recursive: Open a trace recursively
776 * (copied from BSD code in converter/babeltrace.c)
778 * Find each trace present in the subdirectory starting from the given
779 * path, and add them to the context. The packet_seek parameter can be
780 * NULL: this specify to use the default format packet_seek.
782 * Return: 0 on success, nonzero on failure.
783 * Unable to open toplevel: failure.
784 * Unable to open some subdirectory or file: warn and continue;
786 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
787 const char *format_str
,
788 void (*packet_seek
)(struct bt_stream_pos
*pos
,
789 size_t offset
, int whence
))
794 char lpath
[PATH_MAX
];
795 char * const paths
[2] = { lpath
, NULL
};
799 * Need to copy path, because fts_open can change it.
800 * It is the pointer array, not the strings, that are constant.
802 strncpy(lpath
, path
, PATH_MAX
);
803 lpath
[PATH_MAX
- 1] = '\0';
805 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
807 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
812 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
814 while ((node
= fts_read(tree
))) {
817 if (!(node
->fts_info
& FTS_D
))
820 dirfd
= open(node
->fts_accpath
, 0);
822 fprintf(stderr
, "[error] [Context] Unable to open trace "
823 "directory file descriptor.\n");
827 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
846 trace_id
= bt_context_add_trace(ctx
,
847 node
->fts_accpath
, format_str
,
848 packet_seek
, NULL
, NULL
);
850 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
851 "for reading.\n", node
->fts_accpath
, path
);
852 /* Allow to skip erroneous traces. */
855 g_array_append_val(trace_ids
, trace_id
);
859 g_array_free(trace_ids
, TRUE
);
866 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
867 int field_cnt
, int *tid_check
, int *pid_check
,
868 int *procname_check
, int *ppid_check
)
871 struct perfcounter
*global
;
874 for (j
= 0; j
< field_cnt
; j
++) {
875 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
876 if (*tid_check
== 0) {
877 if (strncmp(name
, "tid", 3) == 0)
880 if (*pid_check
== 0) {
881 if (strncmp(name
, "pid", 3) == 0)
884 if (*ppid_check
== 0) {
885 if (strncmp(name
, "ppid", 4) == 0)
888 if (*procname_check
== 0) {
889 if (strncmp(name
, "procname", 8) == 0)
892 if (strncmp(name
, "perf_", 5) == 0) {
893 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
895 global
= g_new0(struct perfcounter
, 1);
896 /* by default, sort on the first perf context */
897 if (g_hash_table_size(global_perf_liszt
) == 0)
900 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
905 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
906 *procname_check
== 1)
913 * check_requirements: check if the required context informations are available
915 * If each mandatory context information is available for at least in one
916 * event, return 0 otherwise return -1.
918 int check_requirements(struct bt_context
*ctx
)
920 unsigned int i
, evt_cnt
, field_cnt
;
921 struct bt_ctf_event_decl
*const * evt_list
;
922 const struct bt_ctf_field_decl
*const * field_list
;
925 int procname_check
= 0;
929 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
930 for (i
= 0; i
< evt_cnt
; i
++) {
931 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_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_EVENT_CONTEXT
,
938 &field_list
, &field_cnt
);
939 ret
= check_field_requirements(field_list
, field_cnt
,
940 &tid_check
, &pid_check
, &procname_check
,
943 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
944 &field_list
, &field_cnt
);
945 ret
= check_field_requirements(field_list
, field_cnt
,
946 &tid_check
, &pid_check
, &procname_check
,
950 if (tid_check
== 0) {
952 fprintf(stderr
, "[error] missing tid context information\n");
954 if (pid_check
== 0) {
956 fprintf(stderr
, "[error] missing pid context information\n");
958 if (ppid_check
== 0) {
960 fprintf(stderr
, "[error] missing ppid context information\n");
962 if (procname_check
== 0) {
964 fprintf(stderr
, "[error] missing procname context information\n");
970 ssize_t
read_subbuffer(struct lttng_consumer_stream
*kconsumerd_fd
,
971 struct lttng_consumer_local_data
*ctx
)
976 int infd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
978 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_SPLICE
) {
979 /* Get the next subbuffer */
980 err
= helper_kernctl_get_next_subbuf(infd
);
983 perror("Reserving sub buffer failed (everything is normal, "
984 "it is due to concurrency)");
987 /* read the whole subbuffer */
988 err
= helper_kernctl_get_padded_subbuf_size(infd
, &len
);
991 perror("Getting sub-buffer len failed.");
995 /* splice the subbuffer to the tracefile */
996 ret
= helper_lttng_consumer_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
999 * display the error but continue processing to try
1000 * to release the subbuffer
1002 fprintf(stderr
,"Error splicing to tracefile\n");
1004 err
= helper_kernctl_put_next_subbuf(infd
);
1007 perror("Reserving sub buffer failed (everything is normal, "
1008 "it is due to concurrency)");
1011 sem_post(&metadata_available
);
1018 int on_update_fd(int key
, uint32_t state
)
1020 /* let the lib handle the metadata FD */
1021 if (key
== sessiond_metadata
)
1026 int on_recv_fd(struct lttng_consumer_stream
*kconsumerd_fd
)
1029 struct mmap_stream
*new_mmap_stream
;
1031 /* Opening the tracefile in write mode */
1032 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
) != NULL
) {
1033 ret
= open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
),
1034 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1039 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd
, ret
);
1042 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_MMAP
) {
1043 new_mmap_stream
= malloc(sizeof(struct mmap_stream
));
1044 new_mmap_stream
->fd
= helper_get_lttng_consumer_stream_wait_fd(
1046 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
1048 g_ptr_array_add(lttng_consumer_stream_array
, kconsumerd_fd
);
1049 /* keep mmap FDs internally */
1052 consumerd_metadata
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
1053 sessiond_metadata
= helper_get_lttng_consumer_stream_key(kconsumerd_fd
);
1063 void live_consume(struct bt_context
**bt_ctx
)
1068 sem_wait(&metadata_available
);
1069 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
1070 fprintf(stderr
,"no metadata\n");
1073 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
1075 *bt_ctx
= bt_context_create();
1076 ret
= bt_context_add_trace(*bt_ctx
, NULL
, "ctf",
1077 lttngtop_ctf_packet_seek
, &mmap_list
, metadata_fp
);
1079 printf("Error adding trace\n");
1087 int setup_consumer(char *command_sock_path
, pthread_t
*threads
,
1088 struct lttng_consumer_local_data
*ctx
)
1092 ctx
= helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL
,
1093 read_subbuffer
, NULL
, on_recv_fd
, on_update_fd
);
1097 unlink(command_sock_path
);
1098 helper_lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
1099 helper_lttng_consumer_init();
1101 /* Create the thread to manage the receive of fd */
1102 ret
= pthread_create(&threads
[0], NULL
, helper_lttng_consumer_thread_receive_fds
,
1105 perror("pthread_create receive fd");
1108 /* Create thread to manage the polling/writing of traces */
1109 ret
= pthread_create(&threads
[1], NULL
, helper_lttng_consumer_thread_poll_fds
,
1112 perror("pthread_create poll fd");
1120 int enable_kprobes(struct lttng_handle
*handle
, char *channel_name
)
1122 struct lttng_event ev
;
1123 struct kprobes
*kprobe
;
1127 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
1128 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
1130 memset(&ev
, '\0', sizeof(struct lttng_event
));
1131 ev
.type
= LTTNG_EVENT_PROBE
;
1132 if (kprobe
->symbol_name
)
1133 sprintf(ev
.attr
.probe
.symbol_name
, "%s", kprobe
->symbol_name
);
1134 sprintf(ev
.name
, "%s", kprobe
->probe_name
);
1135 ev
.attr
.probe
.addr
= kprobe
->probe_addr
;
1136 ev
.attr
.probe
.offset
= kprobe
->probe_offset
;
1137 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1138 fprintf(stderr
,"error enabling kprobes : %s\n",
1139 helper_lttcomm_get_readable_code(ret
));
1148 int setup_live_tracing()
1150 struct lttng_domain dom
;
1151 struct lttng_channel chan
;
1152 char *channel_name
= "mmapchan";
1153 struct lttng_event ev
;
1155 char *command_sock_path
= "/tmp/consumerd_sock";
1156 static pthread_t threads
[2]; /* recv_fd, poll */
1157 struct lttng_event_context kctxpid
, kctxcomm
, kctxppid
, kctxtid
;
1159 struct lttng_handle
*handle
;
1161 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1163 lttng_consumer_stream_array
= g_ptr_array_new();
1165 if ((ret
= setup_consumer(command_sock_path
, threads
, ctx
)) < 0) {
1166 fprintf(stderr
,"error setting up consumer\n");
1170 available_snapshots
= g_ptr_array_new();
1172 /* setup the session */
1173 dom
.type
= LTTNG_DOMAIN_KERNEL
;
1175 ret
= unlink("/tmp/livesession");
1177 lttng_destroy_session("test");
1178 if ((ret
= lttng_create_session("test", "/tmp/livesession")) < 0) {
1179 fprintf(stderr
,"error creating the session : %s\n",
1180 helper_lttcomm_get_readable_code(ret
));
1184 if ((handle
= lttng_create_handle("test", &dom
)) == NULL
) {
1185 fprintf(stderr
,"error creating handle\n");
1190 * FIXME : need to let the
1191 * helper_lttng_consumer_thread_receive_fds create the
1193 * Cleaner solution ?
1195 while (access(command_sock_path
, F_OK
)) {
1199 if ((ret
= lttng_register_consumer(handle
, command_sock_path
)) < 0) {
1200 fprintf(stderr
,"error registering consumer : %s\n",
1201 helper_lttcomm_get_readable_code(ret
));
1205 strcpy(chan
.name
, channel_name
);
1206 chan
.attr
.overwrite
= 0;
1207 if (opt_tid
&& opt_textdump
) {
1208 chan
.attr
.subbuf_size
= 32768;
1209 chan
.attr
.num_subbuf
= 8;
1211 //chan.attr.subbuf_size = 1048576; /* 1MB */
1212 chan
.attr
.subbuf_size
= 2097152; /* 1MB */
1213 chan
.attr
.num_subbuf
= 4;
1215 chan
.attr
.switch_timer_interval
= 0;
1216 chan
.attr
.read_timer_interval
= 200;
1217 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
1219 if ((ret
= lttng_enable_channel(handle
, &chan
)) < 0) {
1220 fprintf(stderr
,"error creating channel : %s\n",
1221 helper_lttcomm_get_readable_code(ret
));
1225 memset(&ev
, '\0', sizeof(struct lttng_event
));
1226 //sprintf(ev.name, "sched_switch");
1227 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1228 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1229 fprintf(stderr
,"error enabling event : %s\n",
1230 helper_lttcomm_get_readable_code(ret
));
1234 memset(&ev
, '\0', sizeof(struct lttng_event
));
1235 ev
.type
= LTTNG_EVENT_SYSCALL
;
1236 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1237 fprintf(stderr
,"error enabling syscalls : %s\n",
1238 helper_lttcomm_get_readable_code(ret
));
1242 if (lttngtop
.kprobes_table
) {
1243 ret
= enable_kprobes(handle
, channel_name
);
1249 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_PID
;
1250 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1251 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_TID
;
1252 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1253 kctxppid
.ctx
= LTTNG_EVENT_CONTEXT_PPID
;
1254 lttng_add_context(handle
, &kctxppid
, NULL
, NULL
);
1255 kctxcomm
.ctx
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1256 lttng_add_context(handle
, &kctxcomm
, NULL
, NULL
);
1257 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_VPID
;
1258 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1259 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
1260 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1261 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1262 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1264 if ((ret
= lttng_start_tracing("test")) < 0) {
1265 fprintf(stderr
,"error starting tracing : %s\n",
1266 helper_lttcomm_get_readable_code(ret
));
1270 helper_kernctl_buffer_flush(consumerd_metadata
);
1272 /* block until metadata is ready */
1273 sem_init(&metadata_available
, 0, 0);
1278 lttng_destroy_session("test");
1283 int main(int argc
, char **argv
)
1286 struct bt_context
*bt_ctx
= NULL
;
1287 struct mmap_stream
*mmap_info
;
1288 unsigned long mmap_len
;
1291 ret
= parse_options(argc
, argv
);
1293 fprintf(stdout
, "Error parsing options.\n\n");
1296 } else if (ret
> 0) {
1300 if (!opt_input_path
) {
1302 signal(SIGTERM
, handle_textdump_sigterm
);
1303 signal(SIGINT
, handle_textdump_sigterm
);
1305 ret
= setup_live_tracing();
1309 if (!opt_textdump
) {
1310 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1311 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1315 live_consume(&bt_ctx
);
1317 ret
= bt_context_remove_trace(bt_ctx
, 0);
1319 fprintf(stderr
, "error removing trace\n");
1321 bt_context_put(bt_ctx
);
1325 * since we receive all FDs every time there is an
1326 * update and the FD number is different every time,
1327 * we don't know which one are valid.
1328 * so we check if all FDs are usable with a simple
1331 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
) {
1332 ret
= helper_kernctl_get_mmap_len(mmap_info
->fd
, &mmap_len
);
1334 bt_list_del(&mmap_info
->list
);
1337 sem_post(&metadata_available
);
1340 pthread_join(timer_thread
, NULL
);
1342 pthread_join(display_thread
, NULL
);
1344 lttng_stop_tracing("test");
1345 lttng_destroy_session("test");
1351 bt_ctx
= bt_context_create();
1352 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1354 fprintf(stderr
, "[error] Opening the trace\n");
1358 ret
= check_requirements(bt_ctx
);
1360 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1363 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1364 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1368 pthread_join(display_thread
, NULL
);
1370 pthread_join(timer_thread
, NULL
);
1375 bt_context_put(bt_ctx
);