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