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;
87 static struct poptOption long_options
[] = {
88 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
89 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
90 { "textdump", 't', POPT_ARG_NONE
, NULL
, OPT_TEXTDUMP
, NULL
, NULL
},
91 { "child", 'f', POPT_ARG_NONE
, NULL
, OPT_CHILD
, NULL
, NULL
},
92 { "pid", 'p', POPT_ARG_STRING
, &opt_tid
, OPT_PID
, NULL
, NULL
},
93 { "hostname", 'n', POPT_ARG_STRING
, &opt_hostname
, OPT_HOSTNAME
, NULL
, NULL
},
94 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
97 static void handle_textdump_sigterm(int signal
)
100 lttng_destroy_session("test");
103 void *refresh_thread(void *p
)
105 struct mmap_stream
*mmap_info
;
109 sem_post(&pause_sem
);
111 sem_post(&end_trace_sem
);
112 sem_post(&goodtodisplay
);
113 sem_post(&goodtoupdate
);
116 if (!opt_input_path
) {
117 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
)
118 helper_kernctl_buffer_flush(mmap_info
->fd
);
120 sem_wait(&pause_sem
);
121 sem_post(&pause_sem
);
123 sleep(refresh_display
/NSEC_PER_SEC
);
127 void *ncurses_display(void *p
)
129 unsigned int current_display_index
= 0;
131 sem_wait(&bootstrap
);
133 * Prevent the 1 second delay when we hit ESC
140 sem_wait(&goodtodisplay
);
141 sem_wait(&pause_sem
);
144 sem_post(&pause_sem
);
150 copy
= g_ptr_array_index(copies
, current_display_index
);
152 display(current_display_index
++);
154 sem_post(&goodtoupdate
);
155 sem_post(&pause_sem
);
159 void print_fields(struct bt_ctf_event
*event
)
162 const struct definition
*const * list
;
163 const struct definition
*scope
;
164 enum ctf_type_id type
;
167 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_FIELDS
);
169 bt_ctf_get_field_list(event
, scope
, &list
, &cnt
);
170 for (i
= 0; i
< cnt
; i
++) {
173 printf("%s = ", bt_ctf_field_name(list
[i
]));
174 type
= bt_ctf_field_type(list
[i
]);
175 if (type
== CTF_TYPE_INTEGER
) {
176 if (bt_ctf_get_int_signedness(list
[i
]) == 0)
177 printf("%" PRIu64
"", bt_ctf_get_uint64(list
[i
]));
179 printf("%" PRId64
"", bt_ctf_get_int64(list
[i
]));
180 } else if (type
== CTF_TYPE_STRING
) {
181 printf("%s", bt_ctf_get_string(list
[i
]));
182 } else if (type
== CTF_TYPE_ARRAY
) {
183 str
= bt_ctf_get_char_array(list
[i
]);
184 if (!bt_ctf_field_get_error() && str
)
191 * hook on each event to check the timestamp and refresh the display if
194 enum bt_cb_ret
print_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
196 unsigned long timestamp
;
198 uint64_t ts_nsec_start
;
201 const struct definition
*scope
;
202 const char *hostname
, *procname
;
204 timestamp
= bt_ctf_get_timestamp(call_data
);
206 start
= format_timestamp(timestamp
);
207 ts_nsec_start
= timestamp
% NSEC_PER_SEC
;
209 pid
= get_context_pid(call_data
);
210 if (pid
== -1ULL && opt_tid
) {
214 hostname
= get_context_hostname(call_data
);
215 if (!opt_tid
&& (opt_hostname
&& !lookup_hostname_list(hostname
)))
217 if (!opt_hostname
&& (opt_tid
&& !lookup_tid_list(pid
)))
219 if ((opt_tid
&& !lookup_tid_list(pid
)) &&
220 (opt_hostname
&& !lookup_hostname_list(hostname
)))
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) && !last_textdump_print_newline
) {
227 scope
= bt_ctf_get_top_level_scope(call_data
,
229 syscall_ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
231 printf("= %ld\n", syscall_ret
);
232 last_textdump_print_newline
= 1;
234 /* we might have lost the exit_syscall event, so need to
235 * print the newline in this case */
236 if (last_textdump_print_newline
== 0)
238 printf("%02d:%02d:%02d.%09" PRIu64
" (%s) (cpu %d) [%s (%d)] %s (",
239 start
.tm_hour
, start
.tm_min
, start
.tm_sec
,
240 ts_nsec_start
, hostname
, cpu_id
, procname
, pid
,
241 bt_ctf_event_name(call_data
));
242 print_fields(call_data
);
244 if (strncmp(bt_ctf_event_name(call_data
), "sys_", 4) != 0) {
246 last_textdump_print_newline
= 1;
248 last_textdump_print_newline
= 0;
255 return BT_CB_ERROR_STOP
;
258 enum bt_cb_ret
handle_kprobes(struct bt_ctf_event
*call_data
, void *private_data
)
261 struct kprobes
*kprobe
;
264 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
265 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
266 if (strcmp(bt_ctf_event_name(call_data
), kprobe
->probe_name
) == 0) {
275 * hook on each event to check the timestamp and refresh the display if
278 enum bt_cb_ret
check_timestamp(struct bt_ctf_event
*call_data
, void *private_data
)
280 unsigned long timestamp
;
282 timestamp
= bt_ctf_get_timestamp(call_data
);
283 if (timestamp
== -1ULL)
286 if (last_display_update
== 0)
287 last_display_update
= timestamp
;
289 if (timestamp
- last_display_update
>= refresh_display
) {
290 sem_wait(&goodtoupdate
);
291 g_ptr_array_add(copies
, get_copy_lttngtop(last_display_update
,
293 sem_post(&goodtodisplay
);
294 sem_post(&bootstrap
);
295 last_display_update
= timestamp
;
300 fprintf(stderr
, "check_timestamp callback error\n");
301 return BT_CB_ERROR_STOP
;
305 * get_perf_counter : get or create and return a perf_counter struct for
306 * either a process or a cpu (only one of the 2 parameters mandatory)
308 struct perfcounter
*get_perf_counter(const char *name
, struct processtop
*proc
,
311 struct perfcounter
*ret
;
321 ret
= g_hash_table_lookup(table
, (gpointer
) name
);
325 ret
= g_new0(struct perfcounter
, 1);
326 /* by default, make it visible in the UI */
328 g_hash_table_insert(table
, (gpointer
) strdup(name
), ret
);
337 void update_perf_value(struct processtop
*proc
, struct cputime
*cpu
,
338 const char *name
, int value
)
340 struct perfcounter
*cpu_perf
, *process_perf
;
342 cpu_perf
= get_perf_counter(name
, NULL
, cpu
);
343 if (cpu_perf
->count
< value
) {
344 process_perf
= get_perf_counter(name
, proc
, NULL
);
345 process_perf
->count
+= value
- cpu_perf
->count
;
346 cpu_perf
->count
= value
;
350 void extract_perf_counter_scope(const struct bt_ctf_event
*event
,
351 const struct bt_definition
*scope
,
352 struct processtop
*proc
,
355 struct bt_definition
const * const *list
= NULL
;
356 const struct bt_definition
*field
;
358 struct perfcounter
*perfcounter
;
366 ret
= bt_ctf_get_field_list(event
, scope
, &list
, &count
);
373 g_hash_table_iter_init(&iter
, global_perf_liszt
);
374 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfcounter
)) {
375 field
= bt_ctf_get_field(event
, scope
, (char *) key
);
377 int value
= bt_ctf_get_uint64(field
);
378 if (bt_ctf_field_get_error())
380 update_perf_value(proc
, cpu
, (char *) key
, value
);
388 void update_perf_counter(struct processtop
*proc
, const struct bt_ctf_event
*event
)
391 const struct bt_definition
*scope
;
393 cpu
= get_cpu(get_cpu_id(event
));
395 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
396 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
398 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_PACKET_CONTEXT
);
399 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
401 scope
= bt_ctf_get_top_level_scope(event
, BT_EVENT_CONTEXT
);
402 extract_perf_counter_scope(event
, scope
, proc
, cpu
);
405 enum bt_cb_ret
fix_process_table(struct bt_ctf_event
*call_data
,
408 int pid
, tid
, ppid
, vpid
, vtid
, vppid
;
409 char *comm
, *hostname
;
410 struct processtop
*parent
, *child
;
411 unsigned long timestamp
;
413 timestamp
= bt_ctf_get_timestamp(call_data
);
414 if (timestamp
== -1ULL)
417 pid
= get_context_pid(call_data
);
422 tid
= get_context_tid(call_data
);
426 ppid
= get_context_ppid(call_data
);
430 vpid
= get_context_vpid(call_data
);
434 vtid
= get_context_vtid(call_data
);
438 vppid
= get_context_vppid(call_data
);
442 comm
= get_context_comm(call_data
);
447 hostname
= get_context_hostname(call_data
);
449 /* find or create the current process */
450 child
= find_process_tid(<tngtop
, tid
, comm
);
452 child
= add_proc(<tngtop
, tid
, comm
, timestamp
);
455 update_proc(child
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, comm
, hostname
);
458 /* find or create the parent */
459 parent
= find_process_tid(<tngtop
, pid
, comm
);
461 parent
= add_proc(<tngtop
, pid
, comm
, timestamp
);
466 /* attach the parent to the current process */
467 child
->threadparent
= parent
;
468 add_thread(parent
, child
);
471 update_perf_counter(child
, call_data
);
477 return BT_CB_ERROR_STOP
;
482 copies
= g_ptr_array_new();
483 global_perf_liszt
= g_hash_table_new(g_str_hash
, g_str_equal
);
485 sem_init(&goodtodisplay
, 0, 0);
486 sem_init(&goodtoupdate
, 0, 1);
487 sem_init(&timer
, 0, 1);
488 sem_init(&bootstrap
, 0, 0);
489 sem_init(&pause_sem
, 0, 1);
490 sem_init(&end_trace_sem
, 0, 0);
492 reset_global_counters();
494 lttngtop
.nbthreads
= 0;
495 lttngtop
.nbfiles
= 0;
497 lttngtop
.process_hash_table
= g_hash_table_new(g_direct_hash
,
499 lttngtop
.process_table
= g_ptr_array_new();
500 lttngtop
.files_table
= g_ptr_array_new();
501 lttngtop
.cpu_table
= g_ptr_array_new();
502 lttngtop
.kprobes_table
= g_ptr_array_new();
507 fprintf(fp
, "LTTngTop %s\n\n", VERSION
);
508 fprintf(fp
, "Usage : lttngtop [OPTIONS] [TRACE]\n");
509 fprintf(fp
, " TRACE Path to the trace to analyse (no trace path for live tracing)\n");
510 fprintf(fp
, " -h, --help This help message\n");
511 fprintf(fp
, " -t, --textdump Display live events in text-only\n");
512 fprintf(fp
, " -p, --pid Comma-separated list of PIDs to display\n");
513 fprintf(fp
, " -f, --child Follow threads associated with selected PIDs\n");
514 fprintf(fp
, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
518 * Return 0 if caller should continue, < 0 if caller should return
519 * error, > 0 if caller should exit without reporting error.
521 static int parse_options(int argc
, char **argv
)
528 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
529 poptReadDefaultConfig(pc
, 0);
531 while ((opt
= poptGetNextOpt(pc
)) != -1) {
535 ret
= 1; /* exit cleanly */
545 tid_list
= g_hash_table_new(g_str_hash
,
547 tmp_str
= strtok(opt_tid
, ",");
549 tid
= malloc(sizeof(int));
550 *tid
= atoi(tmp_str
);
551 g_hash_table_insert(tid_list
,
552 (gpointer
) tid
, tid
);
553 tmp_str
= strtok(NULL
, ",");
557 hostname_list
= g_hash_table_new(g_str_hash
,
559 tmp_str
= strtok(opt_hostname
, ",");
561 char *new_str
= strdup(tmp_str
);
562 g_hash_table_insert(hostname_list
,
565 tmp_str
= strtok(NULL
, ",");
574 opt_input_path
= poptGetArg(pc
);
583 void iter_trace(struct bt_context
*bt_ctx
)
585 struct bt_ctf_iter
*iter
;
586 struct bt_iter_pos begin_pos
;
587 struct kprobes
*kprobe
;
588 const struct bt_ctf_event
*event
;
592 begin_pos
.type
= BT_SEEK_BEGIN
;
593 iter
= bt_ctf_iter_create(bt_ctx
, &begin_pos
, NULL
);
596 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
600 /* at each event check if we need to refresh */
601 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
604 /* at each event, verify the status of the process table */
605 bt_ctf_iter_add_callback(iter
, 0, NULL
, 0,
608 /* to handle the scheduling events */
609 bt_ctf_iter_add_callback(iter
,
610 g_quark_from_static_string("sched_switch"),
611 NULL
, 0, handle_sched_switch
, NULL
, NULL
, NULL
);
612 /* to clean up the process table */
613 bt_ctf_iter_add_callback(iter
,
614 g_quark_from_static_string("sched_process_free"),
615 NULL
, 0, handle_sched_process_free
, NULL
, NULL
, NULL
);
616 /* to get all the process from the statedumps */
617 bt_ctf_iter_add_callback(iter
,
618 g_quark_from_static_string(
619 "lttng_statedump_process_state"),
620 NULL
, 0, handle_statedump_process_state
,
624 bt_ctf_iter_add_callback(iter
,
625 g_quark_from_static_string("exit_syscall"),
626 NULL
, 0, handle_exit_syscall
, NULL
, NULL
, NULL
);
627 bt_ctf_iter_add_callback(iter
,
628 g_quark_from_static_string("sys_write"),
629 NULL
, 0, handle_sys_write
, NULL
, NULL
, NULL
);
630 bt_ctf_iter_add_callback(iter
,
631 g_quark_from_static_string("sys_read"),
632 NULL
, 0, handle_sys_read
, NULL
, NULL
, NULL
);
633 bt_ctf_iter_add_callback(iter
,
634 g_quark_from_static_string("sys_open"),
635 NULL
, 0, handle_sys_open
, NULL
, NULL
, NULL
);
636 bt_ctf_iter_add_callback(iter
,
637 g_quark_from_static_string("sys_close"),
638 NULL
, 0, handle_sys_close
, NULL
, NULL
, NULL
);
639 bt_ctf_iter_add_callback(iter
,
640 g_quark_from_static_string(
641 "lttng_statedump_file_descriptor"),
642 NULL
, 0, handle_statedump_file_descriptor
,
646 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
647 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
648 bt_ctf_iter_add_callback(iter
,
649 g_quark_from_static_string(
651 NULL
, 0, handle_kprobes
,
656 while ((event
= bt_ctf_iter_read_event(iter
)) != NULL
) {
657 if (quit
|| reload_trace
)
659 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
664 /* block until quit, we reached the end of the trace */
665 sem_wait(&end_trace_sem
);
668 bt_ctf_iter_destroy(iter
);
672 * bt_context_add_traces_recursive: Open a trace recursively
673 * (copied from BSD code in converter/babeltrace.c)
675 * Find each trace present in the subdirectory starting from the given
676 * path, and add them to the context. The packet_seek parameter can be
677 * NULL: this specify to use the default format packet_seek.
679 * Return: 0 on success, nonzero on failure.
680 * Unable to open toplevel: failure.
681 * Unable to open some subdirectory or file: warn and continue;
683 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
684 const char *format_str
,
685 void (*packet_seek
)(struct bt_stream_pos
*pos
,
686 size_t offset
, int whence
))
691 char lpath
[PATH_MAX
];
692 char * const paths
[2] = { lpath
, NULL
};
696 * Need to copy path, because fts_open can change it.
697 * It is the pointer array, not the strings, that are constant.
699 strncpy(lpath
, path
, PATH_MAX
);
700 lpath
[PATH_MAX
- 1] = '\0';
702 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
704 fprintf(stderr
, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
709 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
711 while ((node
= fts_read(tree
))) {
714 if (!(node
->fts_info
& FTS_D
))
717 dirfd
= open(node
->fts_accpath
, 0);
719 fprintf(stderr
, "[error] [Context] Unable to open trace "
720 "directory file descriptor.\n");
724 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
743 trace_id
= bt_context_add_trace(ctx
,
744 node
->fts_accpath
, format_str
,
745 packet_seek
, NULL
, NULL
);
747 fprintf(stderr
, "[warning] [Context] opening trace \"%s\" from %s "
748 "for reading.\n", node
->fts_accpath
, path
);
749 /* Allow to skip erroneous traces. */
752 g_array_append_val(trace_ids
, trace_id
);
756 g_array_free(trace_ids
, TRUE
);
763 static int check_field_requirements(const struct bt_ctf_field_decl
*const * field_list
,
764 int field_cnt
, int *tid_check
, int *pid_check
,
765 int *procname_check
, int *ppid_check
)
768 struct perfcounter
*global
;
771 for (j
= 0; j
< field_cnt
; j
++) {
772 name
= bt_ctf_get_decl_field_name(field_list
[j
]);
773 if (*tid_check
== 0) {
774 if (strncmp(name
, "tid", 3) == 0)
777 if (*pid_check
== 0) {
778 if (strncmp(name
, "pid", 3) == 0)
781 if (*ppid_check
== 0) {
782 if (strncmp(name
, "ppid", 4) == 0)
785 if (*procname_check
== 0) {
786 if (strncmp(name
, "procname", 8) == 0)
789 if (strncmp(name
, "perf_", 5) == 0) {
790 global
= g_hash_table_lookup(global_perf_liszt
, (gpointer
) name
);
792 global
= g_new0(struct perfcounter
, 1);
793 /* by default, sort on the first perf context */
794 if (g_hash_table_size(global_perf_liszt
) == 0)
797 g_hash_table_insert(global_perf_liszt
, (gpointer
) strdup(name
), global
);
802 if (*tid_check
== 1 && *pid_check
== 1 && *ppid_check
== 1 &&
803 *procname_check
== 1)
810 * check_requirements: check if the required context informations are available
812 * If each mandatory context information is available for at least in one
813 * event, return 0 otherwise return -1.
815 int check_requirements(struct bt_context
*ctx
)
817 unsigned int i
, evt_cnt
, field_cnt
;
818 struct bt_ctf_event_decl
*const * evt_list
;
819 const struct bt_ctf_field_decl
*const * field_list
;
822 int procname_check
= 0;
826 bt_ctf_get_event_decl_list(0, ctx
, &evt_list
, &evt_cnt
);
827 for (i
= 0; i
< evt_cnt
; i
++) {
828 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_EVENT_CONTEXT
,
829 &field_list
, &field_cnt
);
830 ret
= check_field_requirements(field_list
, field_cnt
,
831 &tid_check
, &pid_check
, &procname_check
,
834 bt_ctf_get_decl_fields(evt_list
[i
], BT_EVENT_CONTEXT
,
835 &field_list
, &field_cnt
);
836 ret
= check_field_requirements(field_list
, field_cnt
,
837 &tid_check
, &pid_check
, &procname_check
,
840 bt_ctf_get_decl_fields(evt_list
[i
], BT_STREAM_PACKET_CONTEXT
,
841 &field_list
, &field_cnt
);
842 ret
= check_field_requirements(field_list
, field_cnt
,
843 &tid_check
, &pid_check
, &procname_check
,
847 if (tid_check
== 0) {
849 fprintf(stderr
, "[error] missing tid context information\n");
851 if (pid_check
== 0) {
853 fprintf(stderr
, "[error] missing pid context information\n");
855 if (ppid_check
== 0) {
857 fprintf(stderr
, "[error] missing ppid context information\n");
859 if (procname_check
== 0) {
861 fprintf(stderr
, "[error] missing procname context information\n");
867 ssize_t
read_subbuffer(struct lttng_consumer_stream
*kconsumerd_fd
,
868 struct lttng_consumer_local_data
*ctx
)
873 int infd
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
875 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_SPLICE
) {
876 /* Get the next subbuffer */
877 err
= helper_kernctl_get_next_subbuf(infd
);
880 perror("Reserving sub buffer failed (everything is normal, "
881 "it is due to concurrency)");
884 /* read the whole subbuffer */
885 err
= helper_kernctl_get_padded_subbuf_size(infd
, &len
);
888 perror("Getting sub-buffer len failed.");
892 /* splice the subbuffer to the tracefile */
893 ret
= helper_lttng_consumer_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
896 * display the error but continue processing to try
897 * to release the subbuffer
899 fprintf(stderr
,"Error splicing to tracefile\n");
901 err
= helper_kernctl_put_next_subbuf(infd
);
904 perror("Reserving sub buffer failed (everything is normal, "
905 "it is due to concurrency)");
908 sem_post(&metadata_available
);
915 int on_update_fd(int key
, uint32_t state
)
917 /* let the lib handle the metadata FD */
918 if (key
== sessiond_metadata
)
923 int on_recv_fd(struct lttng_consumer_stream
*kconsumerd_fd
)
926 struct mmap_stream
*new_mmap_stream
;
928 /* Opening the tracefile in write mode */
929 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
) != NULL
) {
930 ret
= open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd
),
931 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
936 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd
, ret
);
939 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd
) == LTTNG_EVENT_MMAP
) {
940 new_mmap_stream
= malloc(sizeof(struct mmap_stream
));
941 new_mmap_stream
->fd
= helper_get_lttng_consumer_stream_wait_fd(
943 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
945 g_ptr_array_add(lttng_consumer_stream_array
, kconsumerd_fd
);
946 /* keep mmap FDs internally */
949 consumerd_metadata
= helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd
);
950 sessiond_metadata
= helper_get_lttng_consumer_stream_key(kconsumerd_fd
);
960 void live_consume(struct bt_context
**bt_ctx
)
965 sem_wait(&metadata_available
);
966 if (access("/tmp/livesession/kernel/metadata", F_OK
) != 0) {
967 fprintf(stderr
,"no metadata\n");
970 metadata_fp
= fopen("/tmp/livesession/kernel/metadata", "r");
972 *bt_ctx
= bt_context_create();
973 ret
= bt_context_add_trace(*bt_ctx
, NULL
, "ctf",
974 lttngtop_ctf_packet_seek
, &mmap_list
, metadata_fp
);
976 printf("Error adding trace\n");
984 int setup_consumer(char *command_sock_path
, pthread_t
*threads
,
985 struct lttng_consumer_local_data
*ctx
)
989 ctx
= helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL
,
990 read_subbuffer
, NULL
, on_recv_fd
, on_update_fd
);
994 unlink(command_sock_path
);
995 helper_lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
996 helper_lttng_consumer_init();
998 /* Create the thread to manage the receive of fd */
999 ret
= pthread_create(&threads
[0], NULL
, helper_lttng_consumer_thread_receive_fds
,
1002 perror("pthread_create receive fd");
1005 /* Create thread to manage the polling/writing of traces */
1006 ret
= pthread_create(&threads
[1], NULL
, helper_lttng_consumer_thread_poll_fds
,
1009 perror("pthread_create poll fd");
1017 int enable_kprobes(struct lttng_handle
*handle
, char *channel_name
)
1019 struct lttng_event ev
;
1020 struct kprobes
*kprobe
;
1025 kprobe = g_new0(struct kprobes, 1);
1026 kprobe->probe_addr = 0;
1027 kprobe->probe_offset = 0;
1028 asprintf(&kprobe->probe_name, "probe_sys_open");
1029 asprintf(&kprobe->symbol_name, "sys_open");
1030 g_ptr_array_add(lttngtop.kprobes_table, kprobe);
1032 kprobe = g_new0(struct kprobes, 1);
1033 kprobe->probe_addr = 0;
1034 kprobe->probe_offset = 0;
1035 asprintf(&kprobe->probe_name, "probe_sys_close");
1036 asprintf(&kprobe->symbol_name, "sys_close");
1037 g_ptr_array_add(lttngtop.kprobes_table, kprobe);
1040 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
1041 kprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
1043 memset(&ev
, '\0', sizeof(struct lttng_event
));
1044 ev
.type
= LTTNG_EVENT_PROBE
;
1045 sprintf(ev
.attr
.probe
.symbol_name
, "%s", kprobe
->symbol_name
);
1046 sprintf(ev
.name
, "%s", kprobe
->probe_name
);
1047 ev
.attr
.probe
.addr
= kprobe
->probe_addr
;
1048 ev
.attr
.probe
.offset
= kprobe
->probe_offset
;
1049 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1050 fprintf(stderr
,"error enabling kprobes : %s\n",
1051 helper_lttcomm_get_readable_code(ret
));
1060 int setup_live_tracing()
1062 struct lttng_domain dom
;
1063 struct lttng_channel chan
;
1064 char *channel_name
= "mmapchan";
1065 struct lttng_event ev
;
1067 char *command_sock_path
= "/tmp/consumerd_sock";
1068 static pthread_t threads
[2]; /* recv_fd, poll */
1069 struct lttng_event_context kctxpid
, kctxcomm
, kctxppid
, kctxtid
;
1071 struct lttng_handle
*handle
;
1073 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1075 lttng_consumer_stream_array
= g_ptr_array_new();
1077 if ((ret
= setup_consumer(command_sock_path
, threads
, ctx
)) < 0) {
1078 fprintf(stderr
,"error setting up consumer\n");
1082 available_snapshots
= g_ptr_array_new();
1084 /* setup the session */
1085 dom
.type
= LTTNG_DOMAIN_KERNEL
;
1087 ret
= unlink("/tmp/livesession");
1089 lttng_destroy_session("test");
1090 if ((ret
= lttng_create_session("test", "/tmp/livesession")) < 0) {
1091 fprintf(stderr
,"error creating the session : %s\n",
1092 helper_lttcomm_get_readable_code(ret
));
1096 if ((handle
= lttng_create_handle("test", &dom
)) == NULL
) {
1097 fprintf(stderr
,"error creating handle\n");
1102 * FIXME : need to let the
1103 * helper_lttng_consumer_thread_receive_fds create the
1105 * Cleaner solution ?
1107 while (access(command_sock_path
, F_OK
)) {
1111 if ((ret
= lttng_register_consumer(handle
, command_sock_path
)) < 0) {
1112 fprintf(stderr
,"error registering consumer : %s\n",
1113 helper_lttcomm_get_readable_code(ret
));
1117 strcpy(chan
.name
, channel_name
);
1118 chan
.attr
.overwrite
= 0;
1119 if (opt_tid
&& opt_textdump
) {
1120 chan
.attr
.subbuf_size
= 32768;
1121 chan
.attr
.num_subbuf
= 8;
1123 chan
.attr
.subbuf_size
= 1048576; /* 1MB */
1124 chan
.attr
.num_subbuf
= 4;
1126 chan
.attr
.switch_timer_interval
= 0;
1127 chan
.attr
.read_timer_interval
= 200;
1128 chan
.attr
.output
= LTTNG_EVENT_MMAP
;
1130 if ((ret
= lttng_enable_channel(handle
, &chan
)) < 0) {
1131 fprintf(stderr
,"error creating channel : %s\n",
1132 helper_lttcomm_get_readable_code(ret
));
1136 memset(&ev
, '\0', sizeof(struct lttng_event
));
1137 //sprintf(ev.name, "sched_switch");
1138 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1139 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1140 fprintf(stderr
,"error enabling event : %s\n",
1141 helper_lttcomm_get_readable_code(ret
));
1145 memset(&ev
, '\0', sizeof(struct lttng_event
));
1146 ev
.type
= LTTNG_EVENT_SYSCALL
;
1147 if ((ret
= lttng_enable_event(handle
, &ev
, channel_name
)) < 0) {
1148 fprintf(stderr
,"error enabling syscalls : %s\n",
1149 helper_lttcomm_get_readable_code(ret
));
1153 ret
= enable_kprobes(handle
, channel_name
);
1158 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_PID
;
1159 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1160 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_TID
;
1161 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1162 kctxppid
.ctx
= LTTNG_EVENT_CONTEXT_PPID
;
1163 lttng_add_context(handle
, &kctxppid
, NULL
, NULL
);
1164 kctxcomm
.ctx
= LTTNG_EVENT_CONTEXT_PROCNAME
;
1165 lttng_add_context(handle
, &kctxcomm
, NULL
, NULL
);
1166 kctxpid
.ctx
= LTTNG_EVENT_CONTEXT_VPID
;
1167 lttng_add_context(handle
, &kctxpid
, NULL
, NULL
);
1168 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_VTID
;
1169 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1170 kctxtid
.ctx
= LTTNG_EVENT_CONTEXT_HOSTNAME
;
1171 lttng_add_context(handle
, &kctxtid
, NULL
, NULL
);
1173 if ((ret
= lttng_start_tracing("test")) < 0) {
1174 fprintf(stderr
,"error starting tracing : %s\n",
1175 helper_lttcomm_get_readable_code(ret
));
1179 helper_kernctl_buffer_flush(consumerd_metadata
);
1181 /* block until metadata is ready */
1182 sem_init(&metadata_available
, 0, 0);
1187 lttng_destroy_session("test");
1192 int main(int argc
, char **argv
)
1195 struct bt_context
*bt_ctx
= NULL
;
1196 struct mmap_stream
*mmap_info
;
1197 unsigned long mmap_len
;
1199 ret
= parse_options(argc
, argv
);
1201 fprintf(stdout
, "Error parsing options.\n\n");
1204 } else if (ret
> 0) {
1208 if (!opt_input_path
) {
1210 signal(SIGTERM
, handle_textdump_sigterm
);
1211 signal(SIGINT
, handle_textdump_sigterm
);
1214 ret
= setup_live_tracing();
1218 if (!opt_textdump
) {
1219 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1220 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1224 live_consume(&bt_ctx
);
1226 ret
= bt_context_remove_trace(bt_ctx
, 0);
1228 fprintf(stderr
, "error removing trace\n");
1230 bt_context_put(bt_ctx
);
1234 * since we receive all FDs every time there is an
1235 * update and the FD number is different every time,
1236 * we don't know which one are valid.
1237 * so we check if all FDs are usable with a simple
1240 bt_list_for_each_entry(mmap_info
, &mmap_list
.head
, list
) {
1241 ret
= helper_kernctl_get_mmap_len(mmap_info
->fd
, &mmap_len
);
1243 bt_list_del(&mmap_info
->list
);
1246 sem_post(&metadata_available
);
1249 pthread_join(timer_thread
, NULL
);
1251 pthread_join(display_thread
, NULL
);
1253 lttng_stop_tracing("test");
1254 lttng_destroy_session("test");
1260 bt_ctx
= bt_context_create();
1261 ret
= bt_context_add_traces_recursive(bt_ctx
, opt_input_path
, "ctf", NULL
);
1263 fprintf(stderr
, "[error] Opening the trace\n");
1267 ret
= check_requirements(bt_ctx
);
1269 fprintf(stderr
, "[error] some mandatory contexts were missing, exiting.\n");
1272 pthread_create(&display_thread
, NULL
, ncurses_display
, (void *) NULL
);
1273 pthread_create(&timer_thread
, NULL
, refresh_thread
, (void *) NULL
);
1277 pthread_join(display_thread
, NULL
);
1279 pthread_join(timer_thread
, NULL
);
1284 bt_context_put(bt_ctx
);