fix compat with lttng-tools 2.1 rc6
[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 OPT_KPROBES,
86 };
87
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 },
97 };
98
99 static void handle_textdump_sigterm(int signal)
100 {
101 quit = 1;
102 lttng_destroy_session("test");
103 }
104
105 void *refresh_thread(void *p)
106 {
107 struct mmap_stream *mmap_info;
108
109 while (1) {
110 if (quit) {
111 sem_post(&pause_sem);
112 sem_post(&timer);
113 sem_post(&end_trace_sem);
114 sem_post(&goodtodisplay);
115 sem_post(&goodtoupdate);
116 pthread_exit(0);
117 }
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);
121 }
122 sem_wait(&pause_sem);
123 sem_post(&pause_sem);
124 sem_post(&timer);
125 sleep(refresh_display/NSEC_PER_SEC);
126 }
127 }
128
129 void *ncurses_display(void *p)
130 {
131 unsigned int current_display_index = 0;
132
133 sem_wait(&bootstrap);
134 /*
135 * Prevent the 1 second delay when we hit ESC
136 */
137 ESCDELAY = 0;
138 init_ncurses();
139
140 while (1) {
141 sem_wait(&timer);
142 sem_wait(&goodtodisplay);
143 sem_wait(&pause_sem);
144
145 if (quit) {
146 sem_post(&pause_sem);
147 sem_post(&timer);
148 reset_ncurses();
149 pthread_exit(0);
150 }
151
152 copy = g_ptr_array_index(copies, current_display_index);
153 assert(copy);
154 display(current_display_index++);
155
156 sem_post(&goodtoupdate);
157 sem_post(&pause_sem);
158 }
159 }
160
161 void print_fields(struct bt_ctf_event *event)
162 {
163 unsigned int cnt, i;
164 const struct definition *const * list;
165 const struct declaration *l;
166 const struct definition *scope;
167 enum ctf_type_id type;
168 const char *str;
169
170 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_FIELDS);
171
172 bt_ctf_get_field_list(event, scope, &list, &cnt);
173 for (i = 0; i < cnt; i++) {
174 if (i != 0)
175 printf(", ");
176 printf("%s = ", bt_ctf_field_name(list[i]));
177 l = bt_ctf_get_decl_from_def(list[i]);
178 type = bt_ctf_field_type(l);
179 if (type == CTF_TYPE_INTEGER) {
180 if (bt_ctf_get_int_signedness(l) == 0)
181 printf("%" PRIu64 "", bt_ctf_get_uint64(list[i]));
182 else
183 printf("%" PRId64 "", bt_ctf_get_int64(list[i]));
184 } else if (type == CTF_TYPE_STRING) {
185 printf("%s", bt_ctf_get_string(list[i]));
186 } else if (type == CTF_TYPE_ARRAY) {
187 str = bt_ctf_get_char_array(list[i]);
188 if (!bt_ctf_field_get_error() && str)
189 printf("%s", str);
190 }
191 }
192 }
193
194 /*
195 * hook on each event to check the timestamp and refresh the display if
196 * necessary
197 */
198 enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
199 {
200 unsigned long timestamp;
201 struct tm start;
202 uint64_t ts_nsec_start;
203 int pid, cpu_id;
204 int64_t syscall_ret;
205 const struct definition *scope;
206 const char *hostname, *procname;
207
208 timestamp = bt_ctf_get_timestamp(call_data);
209
210 start = format_timestamp(timestamp);
211 ts_nsec_start = timestamp % NSEC_PER_SEC;
212
213 pid = get_context_pid(call_data);
214 if (pid == -1ULL && opt_tid) {
215 goto error;
216 }
217
218 hostname = get_context_hostname(call_data);
219 if (opt_tid || opt_hostname)
220 if (!lookup_filter_tid_list(pid))
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) &&
227 !last_textdump_print_newline) {
228 scope = bt_ctf_get_top_level_scope(call_data,
229 BT_EVENT_FIELDS);
230 syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data,
231 scope, "_ret"));
232 printf("= %ld\n", syscall_ret);
233 last_textdump_print_newline = 1;
234 } else {
235 /* we might have lost the exit_syscall event, so need to
236 * print the newline in this case */
237 if (last_textdump_print_newline == 0)
238 printf("\n");
239 printf("%02d:%02d:%02d.%09" PRIu64 " (%s) (cpu %d) [%s (%d)] %s (",
240 start.tm_hour, start.tm_min, start.tm_sec,
241 ts_nsec_start, hostname, cpu_id, procname, pid,
242 bt_ctf_event_name(call_data));
243 print_fields(call_data);
244 printf(") ");
245 if (strncmp(bt_ctf_event_name(call_data), "sys_", 4) != 0) {
246 printf("\n");
247 last_textdump_print_newline = 1;
248 } else {
249 last_textdump_print_newline = 0;
250 }
251 }
252
253 end:
254 return BT_CB_OK;
255 error:
256 return BT_CB_ERROR_STOP;
257 }
258
259 enum bt_cb_ret handle_kprobes(struct bt_ctf_event *call_data, void *private_data)
260 {
261 int i;
262 struct kprobes *kprobe;
263
264 /* for kprobes */
265 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
266 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
267 if (strcmp(bt_ctf_event_name(call_data), kprobe->probe_name) == 0) {
268 kprobe->count++;
269 }
270 }
271
272 return BT_CB_OK;
273 }
274
275 /*
276 * hook on each event to check the timestamp and refresh the display if
277 * necessary
278 */
279 enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
280 {
281 unsigned long timestamp;
282
283 timestamp = bt_ctf_get_timestamp(call_data);
284 if (timestamp == -1ULL)
285 goto error;
286
287 if (last_display_update == 0)
288 last_display_update = timestamp;
289
290 if (timestamp - last_display_update >= refresh_display) {
291 sem_wait(&goodtoupdate);
292 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
293 timestamp));
294 sem_post(&goodtodisplay);
295 sem_post(&bootstrap);
296 last_display_update = timestamp;
297 }
298 return BT_CB_OK;
299
300 error:
301 fprintf(stderr, "check_timestamp callback error\n");
302 return BT_CB_ERROR_STOP;
303 }
304
305 /*
306 * get_perf_counter : get or create and return a perf_counter struct for
307 * either a process or a cpu (only one of the 2 parameters mandatory)
308 */
309 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
310 struct cputime *cpu)
311 {
312 struct perfcounter *ret;
313 GHashTable *table;
314
315 if (proc)
316 table = proc->perf;
317 else if (cpu)
318 table = cpu->perf;
319 else
320 goto error;
321
322 ret = g_hash_table_lookup(table, (gpointer) name);
323 if (ret)
324 goto end;
325
326 ret = g_new0(struct perfcounter, 1);
327 /* by default, make it visible in the UI */
328 ret->visible = 1;
329 g_hash_table_insert(table, (gpointer) strdup(name), ret);
330
331 end:
332 return ret;
333
334 error:
335 return NULL;
336 }
337
338 void update_perf_value(struct processtop *proc, struct cputime *cpu,
339 const char *name, int value)
340 {
341 struct perfcounter *cpu_perf, *process_perf;
342
343 cpu_perf = get_perf_counter(name, NULL, cpu);
344 if (cpu_perf->count < value) {
345 process_perf = get_perf_counter(name, proc, NULL);
346 process_perf->count += value - cpu_perf->count;
347 cpu_perf->count = value;
348 }
349 }
350
351 void extract_perf_counter_scope(const struct bt_ctf_event *event,
352 const struct bt_definition *scope,
353 struct processtop *proc,
354 struct cputime *cpu)
355 {
356 struct bt_definition const * const *list = NULL;
357 const struct bt_definition *field;
358 unsigned int count;
359 struct perfcounter *perfcounter;
360 GHashTableIter iter;
361 gpointer key;
362 int ret;
363
364 if (!scope)
365 goto end;
366
367 ret = bt_ctf_get_field_list(event, scope, &list, &count);
368 if (ret < 0)
369 goto end;
370
371 if (count == 0)
372 goto end;
373
374 g_hash_table_iter_init(&iter, global_perf_liszt);
375 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) {
376 field = bt_ctf_get_field(event, scope, (char *) key);
377 if (field) {
378 int value = bt_ctf_get_uint64(field);
379 if (bt_ctf_field_get_error())
380 continue;
381 update_perf_value(proc, cpu, (char *) key, value);
382 }
383 }
384
385 end:
386 return;
387 }
388
389 void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
390 {
391 struct cputime *cpu;
392 const struct bt_definition *scope;
393
394 cpu = get_cpu(get_cpu_id(event));
395
396 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
397 extract_perf_counter_scope(event, scope, proc, cpu);
398
399 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
400 extract_perf_counter_scope(event, scope, proc, cpu);
401
402 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
403 extract_perf_counter_scope(event, scope, proc, cpu);
404 }
405
406 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
407 void *private_data)
408 {
409 int pid, tid, ppid, vpid, vtid, vppid;
410 char *comm, *hostname;
411 struct processtop *parent, *child;
412 unsigned long timestamp;
413
414 timestamp = bt_ctf_get_timestamp(call_data);
415 if (timestamp == -1ULL)
416 goto error;
417
418 pid = get_context_pid(call_data);
419 if (pid == -1ULL) {
420 goto error;
421 }
422
423 tid = get_context_tid(call_data);
424 if (tid == -1ULL) {
425 goto error;
426 }
427 ppid = get_context_ppid(call_data);
428 if (ppid == -1ULL) {
429 goto error;
430 }
431 vpid = get_context_vpid(call_data);
432 if (pid == -1ULL) {
433 vpid = -1;
434 }
435 vtid = get_context_vtid(call_data);
436 if (tid == -1ULL) {
437 vtid = -1;
438 }
439 vppid = get_context_vppid(call_data);
440 if (ppid == -1ULL) {
441 vppid = -1;
442 }
443 comm = get_context_comm(call_data);
444 if (!comm) {
445 goto error;
446 }
447 /* optional */
448 hostname = get_context_hostname(call_data);
449
450 /* find or create the current process */
451 child = find_process_tid(&lttngtop, tid, comm);
452 if (!child)
453 child = add_proc(&lttngtop, tid, comm, timestamp, hostname);
454 if (!child)
455 goto end;
456 update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm, hostname);
457
458 if (pid != tid) {
459 /* find or create the parent */
460 parent = find_process_tid(&lttngtop, pid, comm);
461 if (!parent) {
462 parent = add_proc(&lttngtop, pid, comm, timestamp, hostname);
463 if (parent)
464 parent->pid = pid;
465 }
466
467 /* attach the parent to the current process */
468 child->threadparent = parent;
469 add_thread(parent, child);
470 }
471
472 update_perf_counter(child, call_data);
473
474 end:
475 return BT_CB_OK;
476
477 error:
478 return BT_CB_ERROR_STOP;
479 }
480
481 void init_lttngtop()
482 {
483 copies = g_ptr_array_new();
484 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
485 global_filter_list = g_hash_table_new(g_str_hash, g_str_equal);
486 global_host_list = g_hash_table_new(g_str_hash, g_str_equal);
487
488 sem_init(&goodtodisplay, 0, 0);
489 sem_init(&goodtoupdate, 0, 1);
490 sem_init(&timer, 0, 1);
491 sem_init(&bootstrap, 0, 0);
492 sem_init(&pause_sem, 0, 1);
493 sem_init(&end_trace_sem, 0, 0);
494
495 reset_global_counters();
496 lttngtop.nbproc = 0;
497 lttngtop.nbthreads = 0;
498 lttngtop.nbfiles = 0;
499
500 lttngtop.process_hash_table = g_hash_table_new(g_direct_hash,
501 g_direct_equal);
502 lttngtop.process_table = g_ptr_array_new();
503 lttngtop.files_table = g_ptr_array_new();
504 lttngtop.cpu_table = g_ptr_array_new();
505
506 toggle_filter = -1;
507 }
508
509 void usage(FILE *fp)
510 {
511 fprintf(fp, "LTTngTop %s\n\n", VERSION);
512 fprintf(fp, "Usage : lttngtop [OPTIONS] [TRACE]\n");
513 fprintf(fp, " TRACE Path to the trace to analyse (no trace path for live tracing)\n");
514 fprintf(fp, " -h, --help This help message\n");
515 fprintf(fp, " -t, --textdump Display live events in text-only\n");
516 fprintf(fp, " -p, --pid Comma-separated list of PIDs to display\n");
517 fprintf(fp, " -f, --child Follow threads associated with selected PIDs\n");
518 fprintf(fp, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
519 fprintf(fp, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
520 }
521
522 /*
523 * Parse probe options.
524 * Shamelessly stolen from lttng-tools :
525 * src/bin/lttng/commands/enable_events.c
526 */
527 static struct kprobes *parse_probe_opts(char *opt)
528 {
529 char s_hex[19];
530 char name[LTTNG_SYMBOL_NAME_LEN];
531 struct kprobes *kprobe;
532 int ret;
533
534 /*
535 kprobe->probe_addr = 0;
536 kprobe->probe_offset = 0;
537 asprintf(&kprobe->probe_name, "probe_sys_open");
538 asprintf(&kprobe->symbol_name, "sys_open");
539 */
540
541 if (opt == NULL) {
542 kprobe = NULL;
543 goto end;
544 }
545
546 kprobe = g_new0(struct kprobes, 1);
547
548 /* Check for symbol+offset */
549 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
550 if (ret == 2) {
551 asprintf(&kprobe->probe_name, "probe_%s", name);
552 asprintf(&kprobe->symbol_name, "%s", name);
553
554 if (strlen(s_hex) == 0) {
555 fprintf(stderr, "Invalid probe offset %s", s_hex);
556 ret = -1;
557 goto end;
558 }
559 kprobe->probe_offset = strtoul(s_hex, NULL, 0);
560 kprobe->probe_addr = 0;
561 goto end;
562 }
563
564 /* Check for symbol */
565 if (isalpha(name[0])) {
566 ret = sscanf(opt, "%s", name);
567 if (ret == 1) {
568 asprintf(&kprobe->probe_name, "probe_%s", name);
569 asprintf(&kprobe->symbol_name, "%s", name);
570 kprobe->probe_offset = 0;
571 kprobe->probe_addr = 0;
572 goto end;
573 }
574 }
575
576 /* Check for address */
577 ret = sscanf(opt, "%s", s_hex);
578 if (ret > 0) {
579 if (strlen(s_hex) == 0) {
580 fprintf(stderr, "Invalid probe address %s", s_hex);
581 ret = -1;
582 goto end;
583 }
584 asprintf(&kprobe->probe_name, "probe_%s", s_hex);
585 kprobe->probe_offset = 0;
586 kprobe->probe_addr = strtoul(s_hex, NULL, 0);
587 goto end;
588 }
589
590 /* No match */
591 kprobe = NULL;
592
593 end:
594 return kprobe;
595 }
596
597 /*
598 * Return 0 if caller should continue, < 0 if caller should return
599 * error, > 0 if caller should exit without reporting error.
600 */
601 static int parse_options(int argc, char **argv)
602 {
603 poptContext pc;
604 int opt, ret = 0;
605 char *tmp_str;
606 int *tid;
607
608 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
609 poptReadDefaultConfig(pc, 0);
610
611 while ((opt = poptGetNextOpt(pc)) != -1) {
612 switch (opt) {
613 case OPT_HELP:
614 usage(stdout);
615 ret = 1; /* exit cleanly */
616 goto end;
617 case OPT_TEXTDUMP:
618 opt_textdump = 1;
619 break;
620 case OPT_CHILD:
621 opt_textdump = 1;
622 opt_child = 1;
623 break;
624 case OPT_PID:
625 toggle_filter = 1;
626 tid_filter_list = g_hash_table_new(g_str_hash,
627 g_str_equal);
628 tmp_str = strtok(opt_tid, ",");
629 while (tmp_str) {
630 tid = malloc(sizeof(int));
631 *tid = atoi(tmp_str);
632 g_hash_table_insert(tid_filter_list,
633 (gpointer) tid, tid);
634 tmp_str = strtok(NULL, ",");
635 }
636 break;
637 case OPT_HOSTNAME:
638 toggle_filter = 1;
639 tmp_str = strtok(opt_hostname, ",");
640 while (tmp_str) {
641 add_hostname_list(tmp_str, 1);
642 tmp_str = strtok(NULL, ",");
643 }
644 break;
645 case OPT_KPROBES:
646 lttngtop.kprobes_table = g_ptr_array_new();
647 tmp_str = strtok(opt_kprobes, ",");
648 while (tmp_str) {
649 struct kprobes *kprobe;
650
651 kprobe = parse_probe_opts(tmp_str);
652 if (kprobe) {
653 g_ptr_array_add(
654 lttngtop.kprobes_table,
655 kprobe);
656 } else {
657 ret = -EINVAL;
658 goto end;
659 }
660 tmp_str = strtok(NULL, ",");
661 }
662 break;
663 default:
664 ret = -EINVAL;
665 goto end;
666 }
667 }
668
669 opt_input_path = poptGetArg(pc);
670
671 end:
672 if (pc) {
673 poptFreeContext(pc);
674 }
675 return ret;
676 }
677
678 void iter_trace(struct bt_context *bt_ctx)
679 {
680 struct bt_ctf_iter *iter;
681 struct bt_iter_pos begin_pos;
682 struct kprobes *kprobe;
683 const struct bt_ctf_event *event;
684 int i;
685 int ret = 0;
686
687 begin_pos.type = BT_SEEK_BEGIN;
688 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
689
690 /* at each event, verify the status of the process table */
691 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
692 fix_process_table,
693 NULL, NULL, NULL);
694 if (opt_textdump) {
695 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
696 print_timestamp,
697 NULL, NULL, NULL);
698 } else {
699 /* at each event check if we need to refresh */
700 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
701 check_timestamp,
702 NULL, NULL, NULL);
703 /* to handle the scheduling events */
704 bt_ctf_iter_add_callback(iter,
705 g_quark_from_static_string("sched_switch"),
706 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
707 /* to clean up the process table */
708 bt_ctf_iter_add_callback(iter,
709 g_quark_from_static_string("sched_process_free"),
710 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
711 /* to get all the process from the statedumps */
712 bt_ctf_iter_add_callback(iter,
713 g_quark_from_static_string(
714 "lttng_statedump_process_state"),
715 NULL, 0, handle_statedump_process_state,
716 NULL, NULL, NULL);
717
718 /* for IO top */
719 bt_ctf_iter_add_callback(iter,
720 g_quark_from_static_string("exit_syscall"),
721 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
722 bt_ctf_iter_add_callback(iter,
723 g_quark_from_static_string("sys_write"),
724 NULL, 0, handle_sys_write, NULL, NULL, NULL);
725 bt_ctf_iter_add_callback(iter,
726 g_quark_from_static_string("sys_read"),
727 NULL, 0, handle_sys_read, NULL, NULL, NULL);
728 bt_ctf_iter_add_callback(iter,
729 g_quark_from_static_string("sys_open"),
730 NULL, 0, handle_sys_open, NULL, NULL, NULL);
731 bt_ctf_iter_add_callback(iter,
732 g_quark_from_static_string("sys_close"),
733 NULL, 0, handle_sys_close, NULL, NULL, NULL);
734 bt_ctf_iter_add_callback(iter,
735 g_quark_from_static_string(
736 "lttng_statedump_file_descriptor"),
737 NULL, 0, handle_statedump_file_descriptor,
738 NULL, NULL, NULL);
739
740 /* for kprobes */
741 if (lttngtop.kprobes_table) {
742 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
743 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
744 bt_ctf_iter_add_callback(iter,
745 g_quark_from_static_string(
746 kprobe->probe_name),
747 NULL, 0, handle_kprobes,
748 NULL, NULL, NULL);
749 }
750 }
751 }
752
753 while ((event = bt_ctf_iter_read_event(iter)) != NULL) {
754 if (quit || reload_trace)
755 goto end_iter;
756 ret = bt_iter_next(bt_ctf_get_iter(iter));
757 if (ret < 0)
758 goto end_iter;
759 }
760
761 /* block until quit, we reached the end of the trace */
762 sem_wait(&end_trace_sem);
763
764 end_iter:
765 bt_ctf_iter_destroy(iter);
766 }
767
768 /*
769 * bt_context_add_traces_recursive: Open a trace recursively
770 * (copied from BSD code in converter/babeltrace.c)
771 *
772 * Find each trace present in the subdirectory starting from the given
773 * path, and add them to the context. The packet_seek parameter can be
774 * NULL: this specify to use the default format packet_seek.
775 *
776 * Return: 0 on success, nonzero on failure.
777 * Unable to open toplevel: failure.
778 * Unable to open some subdirectory or file: warn and continue;
779 */
780 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
781 const char *format_str,
782 void (*packet_seek)(struct bt_stream_pos *pos,
783 size_t offset, int whence))
784 {
785 FTS *tree;
786 FTSENT *node;
787 GArray *trace_ids;
788 char lpath[PATH_MAX];
789 char * const paths[2] = { lpath, NULL };
790 int ret = -1;
791
792 /*
793 * Need to copy path, because fts_open can change it.
794 * It is the pointer array, not the strings, that are constant.
795 */
796 strncpy(lpath, path, PATH_MAX);
797 lpath[PATH_MAX - 1] = '\0';
798
799 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
800 if (tree == NULL) {
801 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
802 path);
803 return -EINVAL;
804 }
805
806 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
807
808 while ((node = fts_read(tree))) {
809 int dirfd, metafd;
810
811 if (!(node->fts_info & FTS_D))
812 continue;
813
814 dirfd = open(node->fts_accpath, 0);
815 if (dirfd < 0) {
816 fprintf(stderr, "[error] [Context] Unable to open trace "
817 "directory file descriptor.\n");
818 ret = dirfd;
819 goto error;
820 }
821 metafd = openat(dirfd, "metadata", O_RDONLY);
822 if (metafd < 0) {
823 close(dirfd);
824 ret = -1;
825 continue;
826 } else {
827 int trace_id;
828
829 ret = close(metafd);
830 if (ret < 0) {
831 perror("close");
832 goto error;
833 }
834 ret = close(dirfd);
835 if (ret < 0) {
836 perror("close");
837 goto error;
838 }
839
840 trace_id = bt_context_add_trace(ctx,
841 node->fts_accpath, format_str,
842 packet_seek, NULL, NULL);
843 if (trace_id < 0) {
844 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
845 "for reading.\n", node->fts_accpath, path);
846 /* Allow to skip erroneous traces. */
847 continue;
848 }
849 g_array_append_val(trace_ids, trace_id);
850 }
851 }
852
853 g_array_free(trace_ids, TRUE);
854 return ret;
855
856 error:
857 return ret;
858 }
859
860 static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
861 int field_cnt, int *tid_check, int *pid_check,
862 int *procname_check, int *ppid_check)
863 {
864 int j;
865 struct perfcounter *global;
866 const char *name;
867
868 for (j = 0; j < field_cnt; j++) {
869 name = bt_ctf_get_decl_field_name(field_list[j]);
870 if (*tid_check == 0) {
871 if (strncmp(name, "tid", 3) == 0)
872 (*tid_check)++;
873 }
874 if (*pid_check == 0) {
875 if (strncmp(name, "pid", 3) == 0)
876 (*pid_check)++;
877 }
878 if (*ppid_check == 0) {
879 if (strncmp(name, "ppid", 4) == 0)
880 (*ppid_check)++;
881 }
882 if (*procname_check == 0) {
883 if (strncmp(name, "procname", 8) == 0)
884 (*procname_check)++;
885 }
886 if (strncmp(name, "perf_", 5) == 0) {
887 global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
888 if (!global) {
889 global = g_new0(struct perfcounter, 1);
890 /* by default, sort on the first perf context */
891 if (g_hash_table_size(global_perf_liszt) == 0)
892 global->sort = 1;
893 global->visible = 1;
894 g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
895 }
896 }
897 }
898
899 if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
900 *procname_check == 1)
901 return 0;
902
903 return -1;
904 }
905
906 /*
907 * check_requirements: check if the required context informations are available
908 *
909 * If each mandatory context information is available for at least in one
910 * event, return 0 otherwise return -1.
911 */
912 int check_requirements(struct bt_context *ctx)
913 {
914 unsigned int i, evt_cnt, field_cnt;
915 struct bt_ctf_event_decl *const * evt_list;
916 const struct bt_ctf_field_decl *const * field_list;
917 int tid_check = 0;
918 int pid_check = 0;
919 int procname_check = 0;
920 int ppid_check = 0;
921 int ret = 0;
922
923 bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt);
924 for (i = 0; i < evt_cnt; i++) {
925 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT,
926 &field_list, &field_cnt);
927 ret = check_field_requirements(field_list, field_cnt,
928 &tid_check, &pid_check, &procname_check,
929 &ppid_check);
930
931 bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT,
932 &field_list, &field_cnt);
933 ret = check_field_requirements(field_list, field_cnt,
934 &tid_check, &pid_check, &procname_check,
935 &ppid_check);
936
937 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT,
938 &field_list, &field_cnt);
939 ret = check_field_requirements(field_list, field_cnt,
940 &tid_check, &pid_check, &procname_check,
941 &ppid_check);
942 }
943
944 if (tid_check == 0) {
945 ret = -1;
946 fprintf(stderr, "[error] missing tid context information\n");
947 }
948 if (pid_check == 0) {
949 ret = -1;
950 fprintf(stderr, "[error] missing pid context information\n");
951 }
952 if (ppid_check == 0) {
953 ret = -1;
954 fprintf(stderr, "[error] missing ppid context information\n");
955 }
956 if (procname_check == 0) {
957 ret = -1;
958 fprintf(stderr, "[error] missing procname context information\n");
959 }
960
961 return ret;
962 }
963
964 ssize_t read_subbuffer(struct lttng_consumer_stream *kconsumerd_fd,
965 struct lttng_consumer_local_data *ctx)
966 {
967 unsigned long len;
968 int err;
969 long ret = 0;
970 int infd = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
971
972 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_SPLICE) {
973 /* Get the next subbuffer */
974 err = helper_kernctl_get_next_subbuf(infd);
975 if (err != 0) {
976 ret = errno;
977 perror("Reserving sub buffer failed (everything is normal, "
978 "it is due to concurrency)");
979 goto end;
980 }
981 /* read the whole subbuffer */
982 err = helper_kernctl_get_padded_subbuf_size(infd, &len);
983 if (err != 0) {
984 ret = errno;
985 perror("Getting sub-buffer len failed.");
986 goto end;
987 }
988
989 /* splice the subbuffer to the tracefile */
990 ret = helper_lttng_consumer_on_read_subbuffer_splice(ctx, kconsumerd_fd, len, 0);
991 if (ret < 0) {
992 /*
993 * display the error but continue processing to try
994 * to release the subbuffer
995 */
996 fprintf(stderr,"Error splicing to tracefile\n");
997 }
998 err = helper_kernctl_put_next_subbuf(infd);
999 if (err != 0) {
1000 ret = errno;
1001 perror("Reserving sub buffer failed (everything is normal, "
1002 "it is due to concurrency)");
1003 goto end;
1004 }
1005 sem_post(&metadata_available);
1006 }
1007
1008 end:
1009 return 0;
1010 }
1011
1012 int on_update_fd(int key, uint32_t state)
1013 {
1014 /* let the lib handle the metadata FD */
1015 if (key == sessiond_metadata)
1016 return 0;
1017 return 1;
1018 }
1019
1020 int on_recv_fd(struct lttng_consumer_stream *kconsumerd_fd)
1021 {
1022 int ret;
1023 struct mmap_stream *new_mmap_stream;
1024
1025 /* Opening the tracefile in write mode */
1026 if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd) != NULL) {
1027 ret = open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd),
1028 O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
1029 if (ret < 0) {
1030 perror("open");
1031 goto end;
1032 }
1033 helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd, ret);
1034 }
1035
1036 if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_MMAP) {
1037 new_mmap_stream = malloc(sizeof(struct mmap_stream));
1038 new_mmap_stream->fd = helper_get_lttng_consumer_stream_wait_fd(
1039 kconsumerd_fd);
1040 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
1041
1042 g_ptr_array_add(lttng_consumer_stream_array, kconsumerd_fd);
1043 /* keep mmap FDs internally */
1044 ret = 1;
1045 } else {
1046 consumerd_metadata = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
1047 sessiond_metadata = helper_get_lttng_consumer_stream_key(kconsumerd_fd);
1048 ret = 0;
1049 }
1050
1051 reload_trace = 1;
1052
1053 end:
1054 return ret;
1055 }
1056
1057 void live_consume(struct bt_context **bt_ctx)
1058 {
1059 int ret;
1060 FILE *metadata_fp;
1061
1062 sem_wait(&metadata_available);
1063 if (access("/tmp/livesession/kernel/metadata", F_OK) != 0) {
1064 fprintf(stderr,"no metadata\n");
1065 goto end;
1066 }
1067 metadata_fp = fopen("/tmp/livesession/kernel/metadata", "r");
1068
1069 *bt_ctx = bt_context_create();
1070 ret = bt_context_add_trace(*bt_ctx, NULL, "ctf",
1071 lttngtop_ctf_packet_seek, &mmap_list, metadata_fp);
1072 if (ret < 0) {
1073 printf("Error adding trace\n");
1074 goto end;
1075 }
1076
1077 end:
1078 return;
1079 }
1080
1081 int setup_consumer(char *command_sock_path, pthread_t *threads,
1082 struct lttng_consumer_local_data *ctx)
1083 {
1084 int ret = 0;
1085
1086 ctx = helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL,
1087 read_subbuffer, NULL, on_recv_fd, on_update_fd);
1088 if (!ctx)
1089 goto end;
1090
1091 unlink(command_sock_path);
1092 helper_lttng_consumer_set_command_sock_path(ctx, command_sock_path);
1093 helper_lttng_consumer_init();
1094
1095 /* Create the thread to manage the receive of fd */
1096 ret = pthread_create(&threads[0], NULL, helper_lttng_consumer_thread_sessiond_poll,
1097 (void *) ctx);
1098 if (ret != 0) {
1099 perror("pthread_create receive fd");
1100 goto end;
1101 }
1102 /* Create thread to manage the polling/writing of traces */
1103 ret = pthread_create(&threads[1], NULL, helper_lttng_consumer_thread_metadata_poll,
1104 (void *) ctx);
1105 if (ret != 0) {
1106 perror("pthread_create poll fd");
1107 goto end;
1108 }
1109
1110 end:
1111 return ret;
1112 }
1113
1114 int enable_kprobes(struct lttng_handle *handle, char *channel_name)
1115 {
1116 struct lttng_event ev;
1117 struct kprobes *kprobe;
1118 int ret = 0;
1119 int i;
1120
1121 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
1122 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
1123
1124 memset(&ev, '\0', sizeof(struct lttng_event));
1125 ev.type = LTTNG_EVENT_PROBE;
1126 if (kprobe->symbol_name)
1127 sprintf(ev.attr.probe.symbol_name, "%s", kprobe->symbol_name);
1128 sprintf(ev.name, "%s", kprobe->probe_name);
1129 ev.attr.probe.addr = kprobe->probe_addr;
1130 ev.attr.probe.offset = kprobe->probe_offset;
1131 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
1132 fprintf(stderr,"error enabling kprobes : %s\n",
1133 helper_lttcomm_get_readable_code(ret));
1134 goto end;
1135 }
1136 }
1137
1138 end:
1139 return ret;
1140 }
1141
1142 int setup_live_tracing()
1143 {
1144 struct lttng_domain dom;
1145 struct lttng_channel chan;
1146 char *channel_name = "mmapchan";
1147 struct lttng_event ev;
1148 int ret = 0;
1149 char *command_sock_path = "/tmp/consumerd_sock";
1150 static pthread_t threads[2]; /* recv_fd, poll */
1151 struct lttng_event_context kctxpid, kctxcomm, kctxppid, kctxtid;
1152
1153 struct lttng_handle *handle;
1154
1155 BT_INIT_LIST_HEAD(&mmap_list.head);
1156
1157 lttng_consumer_stream_array = g_ptr_array_new();
1158
1159 if ((ret = setup_consumer(command_sock_path, threads, ctx)) < 0) {
1160 fprintf(stderr,"error setting up consumer\n");
1161 goto error;
1162 }
1163
1164 available_snapshots = g_ptr_array_new();
1165
1166 /* setup the session */
1167 dom.type = LTTNG_DOMAIN_KERNEL;
1168
1169 ret = unlink("/tmp/livesession");
1170
1171 lttng_destroy_session("test");
1172 if ((ret = lttng_create_session("test", "/tmp/livesession")) < 0) {
1173 fprintf(stderr,"error creating the session : %s\n",
1174 helper_lttcomm_get_readable_code(ret));
1175 goto error;
1176 }
1177
1178 if ((handle = lttng_create_handle("test", &dom)) == NULL) {
1179 fprintf(stderr,"error creating handle\n");
1180 goto error_session;
1181 }
1182
1183 /*
1184 * FIXME : need to let the
1185 * helper_lttng_consumer_thread_receive_fds create the
1186 * socket.
1187 * Cleaner solution ?
1188 */
1189 while (access(command_sock_path, F_OK)) {
1190 sleep(0.1);
1191 }
1192
1193 if ((ret = lttng_register_consumer(handle, command_sock_path)) < 0) {
1194 fprintf(stderr,"error registering consumer : %s\n",
1195 helper_lttcomm_get_readable_code(ret));
1196 goto error_session;
1197 }
1198
1199 strcpy(chan.name, channel_name);
1200 chan.attr.overwrite = 0;
1201 if (opt_tid && opt_textdump) {
1202 chan.attr.subbuf_size = 32768;
1203 chan.attr.num_subbuf = 8;
1204 } else {
1205 //chan.attr.subbuf_size = 1048576; /* 1MB */
1206 chan.attr.subbuf_size = 2097152; /* 1MB */
1207 chan.attr.num_subbuf = 4;
1208 }
1209 chan.attr.switch_timer_interval = 0;
1210 chan.attr.read_timer_interval = 200;
1211 chan.attr.output = LTTNG_EVENT_MMAP;
1212
1213 if ((ret = lttng_enable_channel(handle, &chan)) < 0) {
1214 fprintf(stderr,"error creating channel : %s\n",
1215 helper_lttcomm_get_readable_code(ret));
1216 goto error_session;
1217 }
1218
1219 memset(&ev, '\0', sizeof(struct lttng_event));
1220 //sprintf(ev.name, "sched_switch");
1221 ev.type = LTTNG_EVENT_TRACEPOINT;
1222 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
1223 fprintf(stderr,"error enabling event : %s\n",
1224 helper_lttcomm_get_readable_code(ret));
1225 goto error_session;
1226 }
1227
1228 memset(&ev, '\0', sizeof(struct lttng_event));
1229 ev.type = LTTNG_EVENT_SYSCALL;
1230 if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) {
1231 fprintf(stderr,"error enabling syscalls : %s\n",
1232 helper_lttcomm_get_readable_code(ret));
1233 goto error_session;
1234 }
1235
1236 if (lttngtop.kprobes_table) {
1237 ret = enable_kprobes(handle, channel_name);
1238 if (ret < 0) {
1239 goto error_session;
1240 }
1241 }
1242
1243 kctxpid.ctx = LTTNG_EVENT_CONTEXT_PID;
1244 lttng_add_context(handle, &kctxpid, NULL, NULL);
1245 kctxtid.ctx = LTTNG_EVENT_CONTEXT_TID;
1246 lttng_add_context(handle, &kctxtid, NULL, NULL);
1247 kctxppid.ctx = LTTNG_EVENT_CONTEXT_PPID;
1248 lttng_add_context(handle, &kctxppid, NULL, NULL);
1249 kctxcomm.ctx = LTTNG_EVENT_CONTEXT_PROCNAME;
1250 lttng_add_context(handle, &kctxcomm, NULL, NULL);
1251 kctxpid.ctx = LTTNG_EVENT_CONTEXT_VPID;
1252 lttng_add_context(handle, &kctxpid, NULL, NULL);
1253 kctxtid.ctx = LTTNG_EVENT_CONTEXT_VTID;
1254 lttng_add_context(handle, &kctxtid, NULL, NULL);
1255 kctxtid.ctx = LTTNG_EVENT_CONTEXT_HOSTNAME;
1256 lttng_add_context(handle, &kctxtid, NULL, NULL);
1257
1258 if ((ret = lttng_start_tracing("test")) < 0) {
1259 fprintf(stderr,"error starting tracing : %s\n",
1260 helper_lttcomm_get_readable_code(ret));
1261 goto error_session;
1262 }
1263
1264 helper_kernctl_buffer_flush(consumerd_metadata);
1265
1266 /* block until metadata is ready */
1267 sem_init(&metadata_available, 0, 0);
1268
1269 return 0;
1270
1271 error_session:
1272 lttng_destroy_session("test");
1273 error:
1274 return -1;
1275 }
1276
1277 int main(int argc, char **argv)
1278 {
1279 int ret;
1280 struct bt_context *bt_ctx = NULL;
1281 struct mmap_stream *mmap_info;
1282 unsigned long mmap_len;
1283
1284 init_lttngtop();
1285 ret = parse_options(argc, argv);
1286 if (ret < 0) {
1287 fprintf(stdout, "Error parsing options.\n\n");
1288 usage(stdout);
1289 exit(EXIT_FAILURE);
1290 } else if (ret > 0) {
1291 exit(EXIT_SUCCESS);
1292 }
1293
1294 if (!opt_input_path) {
1295 if (opt_textdump) {
1296 signal(SIGTERM, handle_textdump_sigterm);
1297 signal(SIGINT, handle_textdump_sigterm);
1298 }
1299 ret = setup_live_tracing();
1300 if (ret < 0) {
1301 goto end;
1302 }
1303 if (!opt_textdump) {
1304 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1305 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1306 }
1307 while (!quit) {
1308 reload_trace = 0;
1309 live_consume(&bt_ctx);
1310 iter_trace(bt_ctx);
1311 /*
1312 * FIXME : pb with cleanup in libbabeltrace
1313 ret = bt_context_remove_trace(bt_ctx, 0);
1314 if (ret != 0) {
1315 fprintf(stderr, "error removing trace\n");
1316 goto error;
1317 }
1318 */
1319 if (bt_ctx) {
1320 bt_context_put(bt_ctx);
1321 }
1322
1323 /*
1324 * since we receive all FDs every time there is an
1325 * update and the FD number is different every time,
1326 * we don't know which one are valid.
1327 * so we check if all FDs are usable with a simple
1328 * ioctl call.
1329 */
1330 bt_list_for_each_entry(mmap_info, &mmap_list.head, list) {
1331 ret = helper_kernctl_get_mmap_len(mmap_info->fd, &mmap_len);
1332 if (ret != 0) {
1333 bt_list_del(&mmap_info->list);
1334 }
1335 }
1336 sem_post(&metadata_available);
1337 }
1338
1339 pthread_join(timer_thread, NULL);
1340 quit = 1;
1341 pthread_join(display_thread, NULL);
1342
1343 lttng_stop_tracing("test");
1344 lttng_destroy_session("test");
1345
1346 goto end;
1347 } else {
1348 //init_lttngtop();
1349
1350 bt_ctx = bt_context_create();
1351 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
1352 if (ret < 0) {
1353 fprintf(stderr, "[error] Opening the trace\n");
1354 goto end;
1355 }
1356
1357 ret = check_requirements(bt_ctx);
1358 if (ret < 0) {
1359 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
1360 goto end;
1361 }
1362 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1363 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1364
1365 iter_trace(bt_ctx);
1366
1367 pthread_join(display_thread, NULL);
1368 quit = 1;
1369 pthread_join(timer_thread, NULL);
1370 }
1371
1372 end:
1373 if (bt_ctx)
1374 bt_context_put(bt_ctx);
1375
1376 return 0;
1377
1378 error:
1379 return -1;
1380 }
This page took 0.097357 seconds and 4 git commands to generate.