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
))
222 if (!opt_tid && (opt_hostname && !lookup_hostname_list(hostname)))
224 if (!opt_hostname && (opt_tid && !lookup_tid_list(pid)))
226 if ((opt_tid && !lookup_tid_list(pid)) &&
227 (opt_hostname && !lookup_hostname_list(hostname)))
231 cpu_id
= get_cpu_id(call_data
);
232 procname
= get_context_comm(call_data
);
234 if ((strcmp(bt_ctf_event_name(call_data
), "exit_syscall") == 0) &&
235 !last_textdump_print_newline
) {
236 scope
= bt_ctf_get_top_level_scope(call_data
,
238 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
240 printf("= %ld\n", syscall_ret
);
241 last_textdump_print_newline
= 1;
243 /* we might have lost the exit_syscall event, so need to
244 * print the newline in this case */
245 if (last_textdump_print_newline
== 0)
247 printf("%02d:%02d:%02d.%09" PRIu64
" (%s) (cpu %d) [%s (%d)] %s (",
248 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
249 ts_nsec_start
, hostname
, cpu_id
, procname
, pid
,
250 bt_ctf_event_name(call_data
));
251 print_fields(call_data
);
253 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) != 0) {
255 last_textdump_print_newline
= 1;
257 last_textdump_print_newline
= 0;
264 return BT_CB_ERROR_STOP
;
267 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
270 struct kprobes
*kprobe
;
273 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
274 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
275 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
284 * hook on each event to check the timestamp and refresh the display if
287 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
289 unsigned long timestamp
;
291 timestamp
= bt_ctf_get_timestamp(call_data
);
292 if (timestamp
== -1ULL)
295 if (last_display_update
== 0)
296 last_display_update
= timestamp
;
298 if (timestamp
- last_display_update
>= refresh_display
) {
299 sem_wait(&goodtoupdate
);
300 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
302 sem_post(&goodtodisplay
);
303 sem_post(&bootstrap
);
304 last_display_update
= timestamp
;
309 fprintf(stderr
, "check_timestamp callback error\n");
310 return BT_CB_ERROR_STOP
;
314 * get_perf_counter : get or create and return a perf_counter struct for
315 * either a process or a cpu (only one of the 2 parameters mandatory)
317 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
320 struct perfcounter
*ret
;
330 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
334 ret
= g_new0(struct perfcounter
, 1);
335 /* by default, make it visible in the UI */
337 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
346 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
347 const char *name
, int value
)
349 struct perfcounter
*cpu_perf
, *process_perf
;
351 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
352 if (cpu_perf
->count
< value
) {
353 process_perf
= get_perf_counter(name
, proc
, NULL
);
354 process_perf
->count
+= value
- cpu_perf
->count
;
355 cpu_perf
->count
= value
;
359 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
360 const struct bt_definition
*scope
,
361 struct processtop
*proc
,
364 struct bt_definition
const * const *list
= NULL
;
365 const struct bt_definition
*field
;
367 struct perfcounter
*perfcounter
;
375 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
382 g_hash_table_iter_init(&iter
, global_perf_liszt
);
383 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
384 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
386 int value
= bt_ctf_get_uint64(field
);
387 if (bt_ctf_field_get_error())
389 update_perf_value(proc
, cpu
, (char *) key
, value
);
397 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
400 const struct bt_definition
*scope
;
402 cpu
= get_cpu(get_cpu_id(event
));
404 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
405 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
407 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
408 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
410 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
411 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
414 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
417 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
418 char *comm
, *hostname
;
419 struct processtop
*parent
, *child
;
420 unsigned long timestamp
;
422 timestamp
= bt_ctf_get_timestamp(call_data
);
423 if (timestamp
== -1ULL)
426 pid
= get_context_pid(call_data
);
431 tid
= get_context_tid(call_data
);
435 ppid
= get_context_ppid(call_data
);
439 vpid
= get_context_vpid(call_data
);
443 vtid
= get_context_vtid(call_data
);
447 vppid
= get_context_vppid(call_data
);
451 comm
= get_context_comm(call_data
);
456 hostname
= get_context_hostname(call_data
);
458 /* find or create the current process */
459 child
= find_process_tid(<tngtop
, tid
, comm
);
461 child
= add_proc(<tngtop
, tid
, comm
, timestamp
, hostname
);
464 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
467 /* find or create the parent */
468 parent
= find_process_tid(<tngtop
, pid
, comm
);
470 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
, hostname
);
475 /* attach the parent to the current process */
476 child
->threadparent
= parent
;
477 add_thread(parent
, child
);
480 update_perf_counter(child
, call_data
);
486 return BT_CB_ERROR_STOP
;
491 copies
= g_ptr_array_new();
492 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
493 global_filter_list
= g_hash_table_new(g_str_hash
, g_str_equal
);
495 sem_init(&goodtodisplay
, 0, 0);
496 sem_init(&goodtoupdate
, 0, 1);
497 sem_init(&timer
, 0, 1);
498 sem_init(&bootstrap
, 0, 0);
499 sem_init(&pause_sem
, 0, 1);
500 sem_init(&end_trace_sem
, 0, 0);
502 reset_global_counters();
504 lttngtop
.nbthreads
= 0;
505 lttngtop
.nbfiles
= 0;
507 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
509 lttngtop
.process_table
= g_ptr_array_new();
510 lttngtop
.files_table
= g_ptr_array_new();
511 lttngtop
.cpu_table
= g_ptr_array_new();
518 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
519 fprintf(fp
, "Usage : lttngtop [OPTIONS] [TRACE]\n");
520 fprintf(fp
, " TRACE Path to the trace to analyse (no trace path for live tracing)\n");
521 fprintf(fp
, " -h, --help This help message\n");
522 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
523 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
524 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
525 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
526 fprintf(fp
, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
530 * Parse probe options.
531 * Shamelessly stolen from lttng-tools :
532 * src/bin/lttng/commands/enable_events.c
534 static struct kprobes
*parse_probe_opts(char *opt
)
537 char name
[LTTNG_SYMBOL_NAME_LEN
];
538 struct kprobes
*kprobe
;
542 kprobe->probe_addr = 0;
543 kprobe->probe_offset = 0;
544 asprintf(&kprobe->probe_name, "probe_sys_open");
545 asprintf(&kprobe->symbol_name, "sys_open");
553 kprobe
= g_new0(struct kprobes
, 1);
555 /* Check for symbol+offset */
556 ret
= sscanf(opt
, "%[^'+']+%s", name
, s_hex
);
558 asprintf(&kprobe
->probe_name
, "probe_%s", name
);
559 asprintf(&kprobe
->symbol_name
, "%s", name
);
561 if (strlen(s_hex
) == 0) {
562 fprintf(stderr
, "Invalid probe offset %s", s_hex
);
566 kprobe
->probe_offset
= strtoul(s_hex
, NULL
, 0);
567 kprobe
->probe_addr
= 0;
571 /* Check for symbol */
572 if (isalpha(name
[0])) {
573 ret
= sscanf(opt
, "%s", name
);
575 asprintf(&kprobe
->probe_name
, "probe_%s", name
);
576 asprintf(&kprobe
->symbol_name
, "%s", name
);
577 kprobe
->probe_offset
= 0;
578 kprobe
->probe_addr
= 0;
583 /* Check for address */
584 ret
= sscanf(opt
, "%s", s_hex
);
586 if (strlen(s_hex
) == 0) {
587 fprintf(stderr
, "Invalid probe address %s", s_hex
);
591 asprintf(&kprobe
->probe_name
, "probe_%s", s_hex
);
592 kprobe
->probe_offset
= 0;
593 kprobe
->probe_addr
= strtoul(s_hex
, NULL
, 0);
605 * Return 0 if caller should continue, < 0 if caller should return
606 * error, > 0 if caller should exit without reporting error.
608 static int parse_options(int argc
, char **argv
)
615 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
616 poptReadDefaultConfig(pc
, 0);
618 while ((opt
= poptGetNextOpt(pc
)) != -1) {
622 ret
= 1; /* exit cleanly */
633 tid_list
= g_hash_table_new(g_str_hash
,
635 tmp_str
= strtok(opt_tid
, ",");
637 tid
= malloc(sizeof(int));
638 *tid
= atoi(tmp_str
);
639 g_hash_table_insert(tid_list
,
640 (gpointer
) tid
, tid
);
641 tmp_str
= strtok(NULL
, ",");
646 hostname_list
= g_hash_table_new(g_str_hash
,
648 tmp_str
= strtok(opt_hostname
, ",");
650 char *new_str
= strdup(tmp_str
);
651 g_hash_table_insert(hostname_list
,
654 tmp_str
= strtok(NULL
, ",");
658 lttngtop
.kprobes_table
= g_ptr_array_new();
659 tmp_str
= strtok(opt_kprobes
, ",");
661 struct kprobes
*kprobe
;
663 kprobe
= parse_probe_opts(tmp_str
);
666 lttngtop
.kprobes_table
,
672 tmp_str
= strtok(NULL
, ",");
681 opt_input_path
= poptGetArg(pc
);
690 void iter_trace(struct bt_context
*bt_ctx
)
692 struct bt_ctf_iter
*iter
;
693 struct bt_iter_pos begin_pos
;
694 struct kprobes
*kprobe
;
695 const struct bt_ctf_event
*event
;
699 begin_pos
.type
= BT_SEEK_BEGIN
;
700 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
702 /* at each event, verify the status of the process table */
703 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
707 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
711 /* at each event check if we need to refresh */
712 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
715 /* to handle the scheduling events */
716 bt_ctf_iter_add_callback(iter
,
717 g_quark_from_static_string("sched_switch"),
718 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
719 /* to clean up the process table */
720 bt_ctf_iter_add_callback(iter
,
721 g_quark_from_static_string("sched_process_free"),
722 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
723 /* to get all the process from the statedumps */
724 bt_ctf_iter_add_callback(iter
,
725 g_quark_from_static_string(
726 "lttng_statedump_process_state"),
727 NULL
, 0, handle_statedump_process_state
,
731 bt_ctf_iter_add_callback(iter
,
732 g_quark_from_static_string("exit_syscall"),
733 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
734 bt_ctf_iter_add_callback(iter
,
735 g_quark_from_static_string("sys_write"),
736 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
737 bt_ctf_iter_add_callback(iter
,
738 g_quark_from_static_string("sys_read"),
739 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
740 bt_ctf_iter_add_callback(iter
,
741 g_quark_from_static_string("sys_open"),
742 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
743 bt_ctf_iter_add_callback(iter
,
744 g_quark_from_static_string("sys_close"),
745 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
746 bt_ctf_iter_add_callback(iter
,
747 g_quark_from_static_string(
748 "lttng_statedump_file_descriptor"),
749 NULL
, 0, handle_statedump_file_descriptor
,
753 if (lttngtop
.kprobes_table
) {
754 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
755 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
756 bt_ctf_iter_add_callback(iter
,
757 g_quark_from_static_string(
759 NULL
, 0, handle_kprobes
,
765 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
766 if (quit
|| reload_trace
)
768 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
773 /* block until quit, we reached the end of the trace */
774 sem_wait(&end_trace_sem
);
777 bt_ctf_iter_destroy(iter
);
781 * bt_context_add_traces_recursive: Open a trace recursively
782 * (copied from BSD code in converter/babeltrace.c)
784 * Find each trace present in the subdirectory starting from the given
785 * path, and add them to the context. The packet_seek parameter can be
786 * NULL: this specify to use the default format packet_seek.
788 * Return: 0 on success, nonzero on failure.
789 * Unable to open toplevel: failure.
790 * Unable to open some subdirectory or file: warn and continue;
792 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
793 const char *format_str
,
794 void (*packet_seek
)(struct bt_stream_pos
*pos
,
795 size_t offset
, int whence
))
800 char lpath
[PATH_MAX
];
801 char * const paths
[2] = { lpath
, NULL
};
805 * Need to copy path, because fts_open can change it.
806 * It is the pointer array, not the strings, that are constant.
808 strncpy(lpath
, path
, PATH_MAX
);
809 lpath
[PATH_MAX
- 1] = '\0';
811 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
813 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
818 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
820 while ((node
= fts_read(tree
))) {
823 if (!(node
->fts_info
& FTS_D
))
826 dirfd
= open(node
->fts_accpath
, 0);
828 fprintf(stderr
, "[error] [Context] Unable to open trace "
829 "directory file descriptor.\n");
833 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
852 trace_id
= bt_context_add_trace(ctx
,
853 node
->fts_accpath
, format_str
,
854 packet_seek
, NULL
, NULL
);
856 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
857 "for reading.\n", node
->fts_accpath
, path
);
858 /* Allow to skip erroneous traces. */
861 g_array_append_val(trace_ids
, trace_id
);
865 g_array_free(trace_ids
, TRUE
);
872 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
873 int field_cnt
, int *tid_check
, int *pid_check
,
874 int *procname_check
, int *ppid_check
)
877 struct perfcounter
*global
;
880 for (j
= 0; j
< field_cnt
; j
++) {
881 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
882 if (*tid_check
== 0) {
883 if (strncmp(name
, "tid", 3) == 0)
886 if (*pid_check
== 0) {
887 if (strncmp(name
, "pid", 3) == 0)
890 if (*ppid_check
== 0) {
891 if (strncmp(name
, "ppid", 4) == 0)
894 if (*procname_check
== 0) {
895 if (strncmp(name
, "procname", 8) == 0)
898 if (strncmp(name
, "perf_", 5) == 0) {
899 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
901 global
= g_new0(struct perfcounter
, 1);
902 /* by default, sort on the first perf context */
903 if (g_hash_table_size(global_perf_liszt
) == 0)
906 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
911 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
912 *procname_check
== 1)
919 * check_requirements: check if the required context informations are available
921 * If each mandatory context information is available for at least in one
922 * event, return 0 otherwise return -1.
924 int check_requirements(struct bt_context
*ctx
)
926 unsigned int i
, evt_cnt
, field_cnt
;
927 struct bt_ctf_event_decl
*const * evt_list
;
928 const struct bt_ctf_field_decl
*const * field_list
;
931 int procname_check
= 0;
935 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
936 for (i
= 0; i
< evt_cnt
; i
++) {
937 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_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_EVENT_CONTEXT
,
944 &field_list
, &field_cnt
);
945 ret
= check_field_requirements(field_list
, field_cnt
,
946 &tid_check
, &pid_check
, &procname_check
,
949 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
950 &field_list
, &field_cnt
);
951 ret
= check_field_requirements(field_list
, field_cnt
,
952 &tid_check
, &pid_check
, &procname_check
,
956 if (tid_check
== 0) {
958 fprintf(stderr
, "[error] missing tid context information\n");
960 if (pid_check
== 0) {
962 fprintf(stderr
, "[error] missing pid context information\n");
964 if (ppid_check
== 0) {
966 fprintf(stderr
, "[error] missing ppid context information\n");
968 if (procname_check
== 0) {
970 fprintf(stderr
, "[error] missing procname context information\n");
976 ssize_t
read_subbuffer(struct lttng_consumer_stream
*kconsumerd_fd
,
977 struct lttng_consumer_local_data
*ctx
)
982 int infd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
984 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_SPLICE
) {
985 /* Get the next subbuffer */
986 err
= helper_kernctl_get_next_subbuf(infd
);
989 perror("Reserving sub buffer failed (everything is normal, "
990 "it is due to concurrency)");
993 /* read the whole subbuffer */
994 err
= helper_kernctl_get_padded_subbuf_size(infd
, &len
);
997 perror("Getting sub-buffer len failed.");
1001 /* splice the subbuffer to the tracefile */
1002 ret
= helper_lttng_consumer_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
1005 * display the error but continue processing to try
1006 * to release the subbuffer
1008 fprintf(stderr
,"Error splicing to tracefile\n");
1010 err
= helper_kernctl_put_next_subbuf(infd
);
1013 perror("Reserving sub buffer failed (everything is normal, "
1014 "it is due to concurrency)");
1017 sem_post(&metadata_available
);
1024 int on_update_fd(int key
, uint32_t state
)
1026 /* let the lib handle the metadata FD */
1027 if (key
== sessiond_metadata
)
1032 int on_recv_fd(struct lttng_consumer_stream
*kconsumerd_fd
)
1035 struct mmap_stream
*new_mmap_stream
;
1037 /* Opening the tracefile in write mode */
1038 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
) != NULL
) {
1039 ret
= open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
),
1040 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1045 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd
, ret
);
1048 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_MMAP
) {
1049 new_mmap_stream
= malloc(sizeof(struct mmap_stream
));
1050 new_mmap_stream
->fd
= helper_get_lttng_consumer_stream_wait_fd(
1052 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
1054 g_ptr_array_add(lttng_consumer_stream_array
, kconsumerd_fd
);
1055 /* keep mmap FDs internally */
1058 consumerd_metadata
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
1059 sessiond_metadata
= helper_get_lttng_consumer_stream_key(kconsumerd_fd
);
1069 void live_consume(struct bt_context
**bt_ctx
)
1074 sem_wait(&metadata_available
);
1075 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
1076 fprintf(stderr
,"no metadata\n");
1079 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
1081 *bt_ctx
= bt_context_create();
1082 ret
= bt_context_add_trace(*bt_ctx
, NULL
, "ctf",
1083 lttngtop_ctf_packet_seek
, &mmap_list
, metadata_fp
);
1085 printf("Error adding trace\n");
1093 int setup_consumer(char *command_sock_path
, pthread_t
*threads
,
1094 struct lttng_consumer_local_data
*ctx
)
1098 ctx
= helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL
,
1099 read_subbuffer
, NULL
, on_recv_fd
, on_update_fd
);
1103 unlink(command_sock_path
);
1104 helper_lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
1105 helper_lttng_consumer_init();
1107 /* Create the thread to manage the receive of fd */
1108 ret
= pthread_create(&threads
[0], NULL
, helper_lttng_consumer_thread_receive_fds
,
1111 perror("pthread_create receive fd");
1114 /* Create thread to manage the polling/writing of traces */
1115 ret
= pthread_create(&threads
[1], NULL
, helper_lttng_consumer_thread_poll_fds
,
1118 perror("pthread_create poll fd");
1126 int enable_kprobes(struct lttng_handle
*handle
, char *channel_name
)
1128 struct lttng_event ev
;
1129 struct kprobes
*kprobe
;
1133 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
1134 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
1136 memset(&ev
, '\0', sizeof(struct lttng_event
));
1137 ev
.type
= LTTNG_EVENT_PROBE
;
1138 if (kprobe
->symbol_name
)
1139 sprintf(ev
.attr
.probe
.symbol_name
, "%s", kprobe
->symbol_name
);
1140 sprintf(ev
.name
, "%s", kprobe
->probe_name
);
1141 ev
.attr
.probe
.addr
= kprobe
->probe_addr
;
1142 ev
.attr
.probe
.offset
= kprobe
->probe_offset
;
1143 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1144 fprintf(stderr
,"error enabling kprobes : %s\n",
1145 helper_lttcomm_get_readable_code(ret
));
1154 int setup_live_tracing()
1156 struct lttng_domain dom
;
1157 struct lttng_channel chan
;
1158 char *channel_name
= "mmapchan";
1159 struct lttng_event ev
;
1161 char *command_sock_path
= "/tmp/consumerd_sock";
1162 static pthread_t threads
[2]; /* recv_fd, poll */
1163 struct lttng_event_context kctxpid
, kctxcomm
, kctxppid
, kctxtid
;
1165 struct lttng_handle
*handle
;
1167 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1169 lttng_consumer_stream_array
= g_ptr_array_new();
1171 if ((ret
= setup_consumer(command_sock_path
, threads
, ctx
)) < 0) {
1172 fprintf(stderr
,"error setting up consumer\n");
1176 available_snapshots
= g_ptr_array_new();
1178 /* setup the session */
1179 dom
.type
= LTTNG_DOMAIN_KERNEL
;
1181 ret
= unlink("/tmp/livesession");
1183 lttng_destroy_session("test");
1184 if ((ret
= lttng_create_session("test", "/tmp/livesession")) < 0) {
1185 fprintf(stderr
,"error creating the session : %s\n",
1186 helper_lttcomm_get_readable_code(ret
));
1190 if ((handle
= lttng_create_handle("test", &dom
)) == NULL
) {
1191 fprintf(stderr
,"error creating handle\n");
1196 * FIXME : need to let the
1197 * helper_lttng_consumer_thread_receive_fds create the
1199 * Cleaner solution ?
1201 while (access(command_sock_path
, F_OK
)) {
1205 if ((ret
= lttng_register_consumer(handle
, command_sock_path
)) < 0) {
1206 fprintf(stderr
,"error registering consumer : %s\n",
1207 helper_lttcomm_get_readable_code(ret
));
1211 strcpy(chan
.name
, channel_name
);
1212 chan
.attr
.overwrite
= 0;
1213 if (opt_tid
&& opt_textdump
) {
1214 chan
.attr
.subbuf_size
= 32768;
1215 chan
.attr
.num_subbuf
= 8;
1217 //chan.attr.subbuf_size = 1048576; /* 1MB */
1218 chan
.attr
.subbuf_size
= 2097152; /* 1MB */
1219 chan
.attr
.num_subbuf
= 4;
1221 chan
.attr
.switch_timer_interval
= 0;
1222 chan
.attr
.read_timer_interval
= 200;
1223 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
1225 if ((ret
= lttng_enable_channel(handle
, &chan
)) < 0) {
1226 fprintf(stderr
,"error creating channel : %s\n",
1227 helper_lttcomm_get_readable_code(ret
));
1231 memset(&ev
, '\0', sizeof(struct lttng_event
));
1232 //sprintf(ev.name, "sched_switch");
1233 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1234 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1235 fprintf(stderr
,"error enabling event : %s\n",
1236 helper_lttcomm_get_readable_code(ret
));
1240 memset(&ev
, '\0', sizeof(struct lttng_event
));
1241 ev
.type
= LTTNG_EVENT_SYSCALL
;
1242 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1243 fprintf(stderr
,"error enabling syscalls : %s\n",
1244 helper_lttcomm_get_readable_code(ret
));
1248 if (lttngtop
.kprobes_table
) {
1249 ret
= enable_kprobes(handle
, channel_name
);
1255 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_PID
;
1256 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1257 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_TID
;
1258 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1259 kctxppid
.ctx
= LTTNG_EVENT_CONTEXT_PPID
;
1260 lttng_add_context(handle
, &kctxppid
, NULL
, NULL
);
1261 kctxcomm
.ctx
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1262 lttng_add_context(handle
, &kctxcomm
, NULL
, NULL
);
1263 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_VPID
;
1264 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1265 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
1266 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1267 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1268 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1270 if ((ret
= lttng_start_tracing("test")) < 0) {
1271 fprintf(stderr
,"error starting tracing : %s\n",
1272 helper_lttcomm_get_readable_code(ret
));
1276 helper_kernctl_buffer_flush(consumerd_metadata
);
1278 /* block until metadata is ready */
1279 sem_init(&metadata_available
, 0, 0);
1284 lttng_destroy_session("test");
1289 int main(int argc
, char **argv
)
1292 struct bt_context
*bt_ctx
= NULL
;
1293 struct mmap_stream
*mmap_info
;
1294 unsigned long mmap_len
;
1297 ret
= parse_options(argc
, argv
);
1299 fprintf(stdout
, "Error parsing options.\n\n");
1302 } else if (ret
> 0) {
1306 if (!opt_input_path
) {
1308 signal(SIGTERM
, handle_textdump_sigterm
);
1309 signal(SIGINT
, handle_textdump_sigterm
);
1311 ret
= setup_live_tracing();
1315 if (!opt_textdump
) {
1316 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1317 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1321 live_consume(&bt_ctx
);
1323 ret
= bt_context_remove_trace(bt_ctx
, 0);
1325 fprintf(stderr
, "error removing trace\n");
1327 bt_context_put(bt_ctx
);
1331 * since we receive all FDs every time there is an
1332 * update and the FD number is different every time,
1333 * we don't know which one are valid.
1334 * so we check if all FDs are usable with a simple
1337 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
) {
1338 ret
= helper_kernctl_get_mmap_len(mmap_info
->fd
, &mmap_len
);
1340 bt_list_del(&mmap_info
->list
);
1343 sem_post(&metadata_available
);
1346 pthread_join(timer_thread
, NULL
);
1348 pthread_join(display_thread
, NULL
);
1350 lttng_stop_tracing("test");
1351 lttng_destroy_session("test");
1357 bt_ctx
= bt_context_create();
1358 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1360 fprintf(stderr
, "[error] Opening the trace\n");
1364 ret
= check_requirements(bt_ctx
);
1366 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1369 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1370 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1374 pthread_join(display_thread
, NULL
);
1376 pthread_join(timer_thread
, NULL
);
1381 bt_context_put(bt_ctx
);