kprobe support
[lttngtop.git] / src / lttngtop.c
1 /*
2 * Copyright (C) 2011-2012 Julien Desfossez
3 *
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;
7 *
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.
12 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <babeltrace/babeltrace.h>
23 #include <babeltrace/ctf/events.h>
24 #include <babeltrace/ctf/callbacks.h>
25 #include <babeltrace/ctf/iterator.h>
26 #include <fcntl.h>
27 #include <pthread.h>
28 #include <popt.h>
29 #include <stdlib.h>
30 #include <ftw.h>
31 #include <dirent.h>
32 #include <ctype.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <fts.h>
39 #include <assert.h>
40 #include <sys/mman.h>
41 #include <lttng/lttng.h>
42 #include <lttng/lttngtop-helper.h>
43 #include <babeltrace/lttngtopmmappacketseek.h>
44
45 #include "lttngtoptypes.h"
46 #include "cputop.h"
47 #include "iostreamtop.h"
48 #include "cursesdisplay.h"
49 #include "common.h"
50
51 #define DEFAULT_FILE_ARRAY_SIZE 1
52
53 const char *opt_input_path;
54 static int opt_textdump;
55 static int opt_child;
56
57 int quit = 0;
58
59 struct lttngtop *copy;
60 pthread_t display_thread;
61 pthread_t timer_thread;
62
63 unsigned long refresh_display = 1 * NSEC_PER_SEC;
64 unsigned long last_display_update = 0;
65
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;
74 int reload_trace = 0;
75
76 int last_textdump_print_newline = 1;
77
78 enum {
79 OPT_NONE = 0,
80 OPT_HELP,
81 OPT_TEXTDUMP,
82 OPT_PID,
83 OPT_CHILD,
84 OPT_HOSTNAME,
85 };
86
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 },
95 };
96
97 static void handle_textdump_sigterm(int signal)
98 {
99 quit = 1;
100 lttng_destroy_session("test");
101 }
102
103 void *refresh_thread(void *p)
104 {
105 struct mmap_stream *mmap_info;
106
107 while (1) {
108 if (quit) {
109 sem_post(&pause_sem);
110 sem_post(&timer);
111 sem_post(&end_trace_sem);
112 sem_post(&goodtodisplay);
113 sem_post(&goodtoupdate);
114 pthread_exit(0);
115 }
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);
119 }
120 sem_wait(&pause_sem);
121 sem_post(&pause_sem);
122 sem_post(&timer);
123 sleep(refresh_display/NSEC_PER_SEC);
124 }
125 }
126
127 void *ncurses_display(void *p)
128 {
129 unsigned int current_display_index = 0;
130
131 sem_wait(&bootstrap);
132 /*
133 * Prevent the 1 second delay when we hit ESC
134 */
135 ESCDELAY = 0;
136 init_ncurses();
137
138 while (1) {
139 sem_wait(&timer);
140 sem_wait(&goodtodisplay);
141 sem_wait(&pause_sem);
142
143 if (quit) {
144 sem_post(&pause_sem);
145 sem_post(&timer);
146 reset_ncurses();
147 pthread_exit(0);
148 }
149
150 copy = g_ptr_array_index(copies, current_display_index);
151 assert(copy);
152 display(current_display_index++);
153
154 sem_post(&goodtoupdate);
155 sem_post(&pause_sem);
156 }
157 }
158
159 void print_fields(struct bt_ctf_event *event)
160 {
161 unsigned int cnt, i;
162 const struct definition *const * list;
163 const struct definition *scope;
164 enum ctf_type_id type;
165 const char *str;
166
167 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_FIELDS);
168
169 bt_ctf_get_field_list(event, scope, &list, &cnt);
170 for (i = 0; i < cnt; i++) {
171 if (i != 0)
172 printf(", ");
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]));
178 else
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)
185 printf("%s", str);
186 }
187 }
188 }
189
190 /*
191 * hook on each event to check the timestamp and refresh the display if
192 * necessary
193 */
194 enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
195 {
196 unsigned long timestamp;
197 struct tm start;
198 uint64_t ts_nsec_start;
199 int pid, cpu_id;
200 int64_t syscall_ret;
201 const struct definition *scope;
202 const char *hostname, *procname;
203
204 timestamp = bt_ctf_get_timestamp(call_data);
205
206 start = format_timestamp(timestamp);
207 ts_nsec_start = timestamp % NSEC_PER_SEC;
208
209 pid = get_context_pid(call_data);
210 if (pid == -1ULL && opt_tid) {
211 goto error;
212 }
213
214 hostname = get_context_hostname(call_data);
215 if (!opt_tid && (opt_hostname && !lookup_hostname_list(hostname)))
216 goto end;
217 if (!opt_hostname && (opt_tid && !lookup_tid_list(pid)))
218 goto end;
219 if ((opt_tid && !lookup_tid_list(pid)) &&
220 (opt_hostname && !lookup_hostname_list(hostname)))
221 goto end;
222
223 cpu_id = get_cpu_id(call_data);
224 procname = get_context_comm(call_data);
225
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,
228 BT_EVENT_FIELDS);
229 syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data,
230 scope, "_ret"));
231 printf("= %ld\n", syscall_ret);
232 last_textdump_print_newline = 1;
233 } else {
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)
237 printf("\n");
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);
243 printf(") ");
244 if (strncmp(bt_ctf_event_name(call_data), "sys_", 4) != 0) {
245 printf("\n");
246 last_textdump_print_newline = 1;
247 } else {
248 last_textdump_print_newline = 0;
249 }
250 }
251
252 end:
253 return BT_CB_OK;
254 error:
255 return BT_CB_ERROR_STOP;
256 }
257
258 enum bt_cb_ret handle_kprobes(struct bt_ctf_event *call_data, void *private_data)
259 {
260 int i;
261 struct kprobes *kprobe;
262
263 /* for kprobes */
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) {
267 kprobe->count++;
268 }
269 }
270
271 return BT_CB_OK;
272 }
273
274 /*
275 * hook on each event to check the timestamp and refresh the display if
276 * necessary
277 */
278 enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
279 {
280 unsigned long timestamp;
281
282 timestamp = bt_ctf_get_timestamp(call_data);
283 if (timestamp == -1ULL)
284 goto error;
285
286 if (last_display_update == 0)
287 last_display_update = timestamp;
288
289 if (timestamp - last_display_update >= refresh_display) {
290 sem_wait(&goodtoupdate);
291 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
292 timestamp));
293 sem_post(&goodtodisplay);
294 sem_post(&bootstrap);
295 last_display_update = timestamp;
296 }
297 return BT_CB_OK;
298
299 error:
300 fprintf(stderr, "check_timestamp callback error\n");
301 return BT_CB_ERROR_STOP;
302 }
303
304 /*
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)
307 */
308 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
309 struct cputime *cpu)
310 {
311 struct perfcounter *ret;
312 GHashTable *table;
313
314 if (proc)
315 table = proc->perf;
316 else if (cpu)
317 table = cpu->perf;
318 else
319 goto error;
320
321 ret = g_hash_table_lookup(table, (gpointer) name);
322 if (ret)
323 goto end;
324
325 ret = g_new0(struct perfcounter, 1);
326 /* by default, make it visible in the UI */
327 ret->visible = 1;
328 g_hash_table_insert(table, (gpointer) strdup(name), ret);
329
330 end:
331 return ret;
332
333 error:
334 return NULL;
335 }
336
337 void update_perf_value(struct processtop *proc, struct cputime *cpu,
338 const char *name, int value)
339 {
340 struct perfcounter *cpu_perf, *process_perf;
341
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;
347 }
348 }
349
350 void extract_perf_counter_scope(const struct bt_ctf_event *event,
351 const struct bt_definition *scope,
352 struct processtop *proc,
353 struct cputime *cpu)
354 {
355 struct bt_definition const * const *list = NULL;
356 const struct bt_definition *field;
357 unsigned int count;
358 struct perfcounter *perfcounter;
359 GHashTableIter iter;
360 gpointer key;
361 int ret;
362
363 if (!scope)
364 goto end;
365
366 ret = bt_ctf_get_field_list(event, scope, &list, &count);
367 if (ret < 0)
368 goto end;
369
370 if (count == 0)
371 goto end;
372
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);
376 if (field) {
377 int value = bt_ctf_get_uint64(field);
378 if (bt_ctf_field_get_error())
379 continue;
380 update_perf_value(proc, cpu, (char *) key, value);
381 }
382 }
383
384 end:
385 return;
386 }
387
388 void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
389 {
390 struct cputime *cpu;
391 const struct bt_definition *scope;
392
393 cpu = get_cpu(get_cpu_id(event));
394
395 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
396 extract_perf_counter_scope(event, scope, proc, cpu);
397
398 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
399 extract_perf_counter_scope(event, scope, proc, cpu);
400
401 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
402 extract_perf_counter_scope(event, scope, proc, cpu);
403 }
404
405 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
406 void *private_data)
407 {
408 int pid, tid, ppid, vpid, vtid, vppid;
409 char *comm, *hostname;
410 struct processtop *parent, *child;
411 unsigned long timestamp;
412
413 timestamp = bt_ctf_get_timestamp(call_data);
414 if (timestamp == -1ULL)
415 goto error;
416
417 pid = get_context_pid(call_data);
418 if (pid == -1ULL) {
419 goto error;
420 }
421
422 tid = get_context_tid(call_data);
423 if (tid == -1ULL) {
424 goto error;
425 }
426 ppid = get_context_ppid(call_data);
427 if (ppid == -1ULL) {
428 goto error;
429 }
430 vpid = get_context_vpid(call_data);
431 if (pid == -1ULL) {
432 vpid = -1;
433 }
434 vtid = get_context_vtid(call_data);
435 if (tid == -1ULL) {
436 vtid = -1;
437 }
438 vppid = get_context_vppid(call_data);
439 if (ppid == -1ULL) {
440 vppid = -1;
441 }
442 comm = get_context_comm(call_data);
443 if (!comm) {
444 goto error;
445 }
446 /* optional */
447 hostname = get_context_hostname(call_data);
448
449 /* find or create the current process */
450 child = find_process_tid(&lttngtop, tid, comm);
451 if (!child)
452 child = add_proc(&lttngtop, tid, comm, timestamp);
453 if (!child)
454 goto end;
455 update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm, hostname);
456
457 if (pid != tid) {
458 /* find or create the parent */
459 parent = find_process_tid(&lttngtop, pid, comm);
460 if (!parent) {
461 parent = add_proc(&lttngtop, pid, comm, timestamp);
462 if (parent)
463 parent->pid = pid;
464 }
465
466 /* attach the parent to the current process */
467 child->threadparent = parent;
468 add_thread(parent, child);
469 }
470
471 update_perf_counter(child, call_data);
472
473 end:
474 return BT_CB_OK;
475
476 error:
477 return BT_CB_ERROR_STOP;
478 }
479
480 void init_lttngtop()
481 {
482 copies = g_ptr_array_new();
483 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
484
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);
491
492 reset_global_counters();
493 lttngtop.nbproc = 0;
494 lttngtop.nbthreads = 0;
495 lttngtop.nbfiles = 0;
496
497 lttngtop.process_hash_table = g_hash_table_new(g_direct_hash,
498 g_direct_equal);
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();
503 }
504
505 void usage(FILE *fp)
506 {
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");
515 }
516
517 /*
518 * Return 0 if caller should continue, < 0 if caller should return
519 * error, > 0 if caller should exit without reporting error.
520 */
521 static int parse_options(int argc, char **argv)
522 {
523 poptContext pc;
524 int opt, ret = 0;
525 char *tmp_str;
526 int *tid;
527
528 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
529 poptReadDefaultConfig(pc, 0);
530
531 while ((opt = poptGetNextOpt(pc)) != -1) {
532 switch (opt) {
533 case OPT_HELP:
534 usage(stdout);
535 ret = 1; /* exit cleanly */
536 goto end;
537 case OPT_TEXTDUMP:
538 opt_textdump = 1;
539 break;
540 case OPT_CHILD:
541 opt_textdump = 1;
542 opt_child = 1;
543 break;
544 case OPT_PID:
545 tid_list = g_hash_table_new(g_str_hash,
546 g_str_equal);
547 tmp_str = strtok(opt_tid, ",");
548 while (tmp_str) {
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, ",");
554 }
555 break;
556 case OPT_HOSTNAME:
557 hostname_list = g_hash_table_new(g_str_hash,
558 g_str_equal);
559 tmp_str = strtok(opt_hostname, ",");
560 while (tmp_str) {
561 char *new_str = strdup(tmp_str);
562 g_hash_table_insert(hostname_list,
563 (gpointer) new_str,
564 (gpointer) new_str);
565 tmp_str = strtok(NULL, ",");
566 }
567 break;
568 default:
569 ret = -EINVAL;
570 goto end;
571 }
572 }
573
574 opt_input_path = poptGetArg(pc);
575
576 end:
577 if (pc) {
578 poptFreeContext(pc);
579 }
580 return ret;
581 }
582
583 void iter_trace(struct bt_context *bt_ctx)
584 {
585 struct bt_ctf_iter *iter;
586 struct bt_iter_pos begin_pos;
587 struct kprobes *kprobe;
588 const struct bt_ctf_event *event;
589 int i;
590 int ret = 0;
591
592 begin_pos.type = BT_SEEK_BEGIN;
593 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
594
595 if (opt_textdump) {
596 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
597 print_timestamp,
598 NULL, NULL, NULL);
599 } else {
600 /* at each event check if we need to refresh */
601 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
602 check_timestamp,
603 NULL, NULL, NULL);
604 /* at each event, verify the status of the process table */
605 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
606 fix_process_table,
607 NULL, NULL, NULL);
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,
621 NULL, NULL, NULL);
622
623 /* for IO top */
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,
643 NULL, NULL, NULL);
644
645 /* for kprobes */
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(
650 kprobe->probe_name),
651 NULL, 0, handle_kprobes,
652 NULL, NULL, NULL);
653 }
654 }
655
656 while ((event = bt_ctf_iter_read_event(iter)) != NULL) {
657 if (quit || reload_trace)
658 goto end_iter;
659 ret = bt_iter_next(bt_ctf_get_iter(iter));
660 if (ret < 0)
661 goto end_iter;
662 }
663
664 /* block until quit, we reached the end of the trace */
665 sem_wait(&end_trace_sem);
666
667 end_iter:
668 bt_ctf_iter_destroy(iter);
669 }
670
671 /*
672 * bt_context_add_traces_recursive: Open a trace recursively
673 * (copied from BSD code in converter/babeltrace.c)
674 *
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.
678 *
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;
682 */
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))
687 {
688 FTS *tree;
689 FTSENT *node;
690 GArray *trace_ids;
691 char lpath[PATH_MAX];
692 char * const paths[2] = { lpath, NULL };
693 int ret = -1;
694
695 /*
696 * Need to copy path, because fts_open can change it.
697 * It is the pointer array, not the strings, that are constant.
698 */
699 strncpy(lpath, path, PATH_MAX);
700 lpath[PATH_MAX - 1] = '\0';
701
702 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
703 if (tree == NULL) {
704 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
705 path);
706 return -EINVAL;
707 }
708
709 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
710
711 while ((node = fts_read(tree))) {
712 int dirfd, metafd;
713
714 if (!(node->fts_info & FTS_D))
715 continue;
716
717 dirfd = open(node->fts_accpath, 0);
718 if (dirfd < 0) {
719 fprintf(stderr, "[error] [Context] Unable to open trace "
720 "directory file descriptor.\n");
721 ret = dirfd;
722 goto error;
723 }
724 metafd = openat(dirfd, "metadata", O_RDONLY);
725 if (metafd < 0) {
726 close(dirfd);
727 ret = -1;
728 continue;
729 } else {
730 int trace_id;
731
732 ret = close(metafd);
733 if (ret < 0) {
734 perror("close");
735 goto error;
736 }
737 ret = close(dirfd);
738 if (ret < 0) {
739 perror("close");
740 goto error;
741 }
742
743 trace_id = bt_context_add_trace(ctx,
744 node->fts_accpath, format_str,
745 packet_seek, NULL, NULL);
746 if (trace_id < 0) {
747 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
748 "for reading.\n", node->fts_accpath, path);
749 /* Allow to skip erroneous traces. */
750 continue;
751 }
752 g_array_append_val(trace_ids, trace_id);
753 }
754 }
755
756 g_array_free(trace_ids, TRUE);
757 return ret;
758
759 error:
760 return ret;
761 }
762
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)
766 {
767 int j;
768 struct perfcounter *global;
769 const char *name;
770
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)
775 (*tid_check)++;
776 }
777 if (*pid_check == 0) {
778 if (strncmp(name, "pid", 3) == 0)
779 (*pid_check)++;
780 }
781 if (*ppid_check == 0) {
782 if (strncmp(name, "ppid", 4) == 0)
783 (*ppid_check)++;
784 }
785 if (*procname_check == 0) {
786 if (strncmp(name, "procname", 8) == 0)
787 (*procname_check)++;
788 }
789 if (strncmp(name, "perf_", 5) == 0) {
790 global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
791 if (!global) {
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)
795 global->sort = 1;
796 global->visible = 1;
797 g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
798 }
799 }
800 }
801
802 if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
803 *procname_check == 1)
804 return 0;
805
806 return -1;
807 }
808
809 /*
810 * check_requirements: check if the required context informations are available
811 *
812 * If each mandatory context information is available for at least in one
813 * event, return 0 otherwise return -1.
814 */
815 int check_requirements(struct bt_context *ctx)
816 {
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;
820 int tid_check = 0;
821 int pid_check = 0;
822 int procname_check = 0;
823 int ppid_check = 0;
824 int ret = 0;
825
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,
832 &ppid_check);
833
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,
838 &ppid_check);
839
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,
844 &ppid_check);
845 }
846
847 if (tid_check == 0) {
848 ret = -1;
849 fprintf(stderr, "[error] missing tid context information\n");
850 }
851 if (pid_check == 0) {
852 ret = -1;
853 fprintf(stderr, "[error] missing pid context information\n");
854 }
855 if (ppid_check == 0) {
856 ret = -1;
857 fprintf(stderr, "[error] missing ppid context information\n");
858 }
859 if (procname_check == 0) {
860 ret = -1;
861 fprintf(stderr, "[error] missing procname context information\n");
862 }
863
864 return ret;
865 }
866
867 ssize_t read_subbuffer(struct lttng_consumer_stream *kconsumerd_fd,
868 struct lttng_consumer_local_data *ctx)
869 {
870 unsigned long len;
871 int err;
872 long ret = 0;
873 int infd = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
874
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);
878 if (err != 0) {
879 ret = errno;
880 perror("Reserving sub buffer failed (everything is normal, "
881 "it is due to concurrency)");
882 goto end;
883 }
884 /* read the whole subbuffer */
885 err = helper_kernctl_get_padded_subbuf_size(infd, &len);
886 if (err != 0) {
887 ret = errno;
888 perror("Getting sub-buffer len failed.");
889 goto end;
890 }
891
892 /* splice the subbuffer to the tracefile */
893 ret = helper_lttng_consumer_on_read_subbuffer_splice(ctx, kconsumerd_fd, len);
894 if (ret < 0) {
895 /*
896 * display the error but continue processing to try
897 * to release the subbuffer
898 */
899 fprintf(stderr,"Error splicing to tracefile\n");
900 }
901 err = helper_kernctl_put_next_subbuf(infd);
902 if (err != 0) {
903 ret = errno;
904 perror("Reserving sub buffer failed (everything is normal, "
905 "it is due to concurrency)");
906 goto end;
907 }
908 sem_post(&metadata_available);
909 }
910
911 end:
912 return 0;
913 }
914
915 int on_update_fd(int key, uint32_t state)
916 {
917 /* let the lib handle the metadata FD */
918 if (key == sessiond_metadata)
919 return 0;
920 return 1;
921 }
922
923 int on_recv_fd(struct lttng_consumer_stream *kconsumerd_fd)
924 {
925 int ret;
926 struct mmap_stream *new_mmap_stream;
927
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);
932 if (ret < 0) {
933 perror("open");
934 goto end;
935 }
936 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd, ret);
937 }
938
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(
942 kconsumerd_fd);
943 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
944
945 g_ptr_array_add(lttng_consumer_stream_array, kconsumerd_fd);
946 /* keep mmap FDs internally */
947 ret = 1;
948 } else {
949 consumerd_metadata = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
950 sessiond_metadata = helper_get_lttng_consumer_stream_key(kconsumerd_fd);
951 ret = 0;
952 }
953
954 reload_trace = 1;
955
956 end:
957 return ret;
958 }
959
960 void live_consume(struct bt_context **bt_ctx)
961 {
962 int ret;
963 FILE *metadata_fp;
964
965 sem_wait(&metadata_available);
966 if (access("/tmp/livesession/kernel/metadata", F_OK) != 0) {
967 fprintf(stderr,"no metadata\n");
968 goto end;
969 }
970 metadata_fp = fopen("/tmp/livesession/kernel/metadata", "r");
971
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);
975 if (ret < 0) {
976 printf("Error adding trace\n");
977 goto end;
978 }
979
980 end:
981 return;
982 }
983
984 int setup_consumer(char *command_sock_path, pthread_t *threads,
985 struct lttng_consumer_local_data *ctx)
986 {
987 int ret = 0;
988
989 ctx = helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL,
990 read_subbuffer, NULL, on_recv_fd, on_update_fd);
991 if (!ctx)
992 goto end;
993
994 unlink(command_sock_path);
995 helper_lttng_consumer_set_command_sock_path(ctx, command_sock_path);
996 helper_lttng_consumer_init();
997
998 /* Create the thread to manage the receive of fd */
999 ret = pthread_create(&threads[0], NULL, helper_lttng_consumer_thread_receive_fds,
1000 (void *) ctx);
1001 if (ret != 0) {
1002 perror("pthread_create receive fd");
1003 goto end;
1004 }
1005 /* Create thread to manage the polling/writing of traces */
1006 ret = pthread_create(&threads[1], NULL, helper_lttng_consumer_thread_poll_fds,
1007 (void *) ctx);
1008 if (ret != 0) {
1009 perror("pthread_create poll fd");
1010 goto end;
1011 }
1012
1013 end:
1014 return ret;
1015 }
1016
1017 int enable_kprobes(struct lttng_handle *handle, char *channel_name)
1018 {
1019 struct lttng_event ev;
1020 struct kprobes *kprobe;
1021 int ret = 0;
1022 int i;
1023
1024 /*
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);
1031
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);
1038 */
1039
1040 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
1041 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
1042
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));
1052 goto end;
1053 }
1054 }
1055
1056 end:
1057 return ret;
1058 }
1059
1060 int setup_live_tracing()
1061 {
1062 struct lttng_domain dom;
1063 struct lttng_channel chan;
1064 char *channel_name = "mmapchan";
1065 struct lttng_event ev;
1066 int ret = 0;
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;
1070
1071 struct lttng_handle *handle;
1072
1073 BT_INIT_LIST_HEAD(&mmap_list.head);
1074
1075 lttng_consumer_stream_array = g_ptr_array_new();
1076
1077 if ((ret = setup_consumer(command_sock_path, threads, ctx)) < 0) {
1078 fprintf(stderr,"error setting up consumer\n");
1079 goto error;
1080 }
1081
1082 available_snapshots = g_ptr_array_new();
1083
1084 /* setup the session */
1085 dom.type = LTTNG_DOMAIN_KERNEL;
1086
1087 ret = unlink("/tmp/livesession");
1088
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));
1093 goto error;
1094 }
1095
1096 if ((handle = lttng_create_handle("test", &dom)) == NULL) {
1097 fprintf(stderr,"error creating handle\n");
1098 goto error_session;
1099 }
1100
1101 /*
1102 * FIXME : need to let the
1103 * helper_lttng_consumer_thread_receive_fds create the
1104 * socket.
1105 * Cleaner solution ?
1106 */
1107 while (access(command_sock_path, F_OK)) {
1108 sleep(0.1);
1109 }
1110
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));
1114 goto error_session;
1115 }
1116
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;
1122 } else {
1123 chan.attr.subbuf_size = 1048576; /* 1MB */
1124 chan.attr.num_subbuf = 4;
1125 }
1126 chan.attr.switch_timer_interval = 0;
1127 chan.attr.read_timer_interval = 200;
1128 chan.attr.output = LTTNG_EVENT_MMAP;
1129
1130 if ((ret = lttng_enable_channel(handle, &chan)) < 0) {
1131 fprintf(stderr,"error creating channel : %s\n",
1132 helper_lttcomm_get_readable_code(ret));
1133 goto error_session;
1134 }
1135
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));
1142 goto error_session;
1143 }
1144
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));
1150 goto error_session;
1151 }
1152
1153 ret = enable_kprobes(handle, channel_name);
1154 if (ret < 0) {
1155 goto error_session;
1156 }
1157
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);
1172
1173 if ((ret = lttng_start_tracing("test")) < 0) {
1174 fprintf(stderr,"error starting tracing : %s\n",
1175 helper_lttcomm_get_readable_code(ret));
1176 goto error_session;
1177 }
1178
1179 helper_kernctl_buffer_flush(consumerd_metadata);
1180
1181 /* block until metadata is ready */
1182 sem_init(&metadata_available, 0, 0);
1183
1184 return 0;
1185
1186 error_session:
1187 lttng_destroy_session("test");
1188 error:
1189 return -1;
1190 }
1191
1192 int main(int argc, char **argv)
1193 {
1194 int ret;
1195 struct bt_context *bt_ctx = NULL;
1196 struct mmap_stream *mmap_info;
1197 unsigned long mmap_len;
1198
1199 ret = parse_options(argc, argv);
1200 if (ret < 0) {
1201 fprintf(stdout, "Error parsing options.\n\n");
1202 usage(stdout);
1203 exit(EXIT_FAILURE);
1204 } else if (ret > 0) {
1205 exit(EXIT_SUCCESS);
1206 }
1207
1208 if (!opt_input_path) {
1209 if (opt_textdump) {
1210 signal(SIGTERM, handle_textdump_sigterm);
1211 signal(SIGINT, handle_textdump_sigterm);
1212 }
1213 init_lttngtop();
1214 ret = setup_live_tracing();
1215 if (ret < 0) {
1216 goto end;
1217 }
1218 if (!opt_textdump) {
1219 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1220 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1221 }
1222 while (!quit) {
1223 reload_trace = 0;
1224 live_consume(&bt_ctx);
1225 iter_trace(bt_ctx);
1226 ret = bt_context_remove_trace(bt_ctx, 0);
1227 if (ret != 0)
1228 fprintf(stderr, "error removing trace\n");
1229 if (bt_ctx) {
1230 bt_context_put(bt_ctx);
1231 }
1232
1233 /*
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
1238 * ioctl call.
1239 */
1240 bt_list_for_each_entry(mmap_info, &mmap_list.head, list) {
1241 ret = helper_kernctl_get_mmap_len(mmap_info->fd, &mmap_len);
1242 if (ret != 0) {
1243 bt_list_del(&mmap_info->list);
1244 }
1245 }
1246 sem_post(&metadata_available);
1247 }
1248
1249 pthread_join(timer_thread, NULL);
1250 quit = 1;
1251 pthread_join(display_thread, NULL);
1252
1253 lttng_stop_tracing("test");
1254 lttng_destroy_session("test");
1255
1256 goto end;
1257 } else {
1258 init_lttngtop();
1259
1260 bt_ctx = bt_context_create();
1261 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
1262 if (ret < 0) {
1263 fprintf(stderr, "[error] Opening the trace\n");
1264 goto end;
1265 }
1266
1267 ret = check_requirements(bt_ctx);
1268 if (ret < 0) {
1269 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
1270 goto end;
1271 }
1272 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1273 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1274
1275 iter_trace(bt_ctx);
1276
1277 pthread_join(display_thread, NULL);
1278 quit = 1;
1279 pthread_join(timer_thread, NULL);
1280 }
1281
1282 end:
1283 if (bt_ctx)
1284 bt_context_put(bt_ctx);
1285
1286 return 0;
1287 }
This page took 0.091141 seconds and 4 git commands to generate.