2800cfac124a208698f47396e89659e8342850d5
[lttngtop.git] / src / lttngtop.c
1 /*
2 * Copyright (C) 2013 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 #ifdef LTTNGTOP_MMAP_LIVE
43 #include <lttng/lttngtop-helper.h>
44 #include <babeltrace/lttngtopmmappacketseek.h>
45 #include "mmap-live.h"
46 #endif /* LTTNGTOP_MMAP_LIVE */
47
48 #include "lttngtoptypes.h"
49 #include "cputop.h"
50 #include "iostreamtop.h"
51 #include "cursesdisplay.h"
52 #include "common.h"
53 #include "network-live.h"
54
55 #include "lttng-index.h"
56
57 #define DEFAULT_FILE_ARRAY_SIZE 1
58
59 const char *opt_input_path;
60 static int opt_textdump;
61 static int opt_child;
62 static int opt_begin;
63
64 int quit = 0;
65
66 struct lttngtop *copy;
67 pthread_t display_thread;
68 pthread_t timer_thread;
69
70 unsigned long refresh_display = 1 * NSEC_PER_SEC;
71 unsigned long last_display_update = 0;
72 unsigned long last_event_ts = 0;
73
74 /* list of FDs available for being read with snapshots */
75 struct bt_mmap_stream_list mmap_list;
76 GPtrArray *lttng_consumer_stream_array;
77 int sessiond_metadata, consumerd_metadata;
78 struct lttng_consumer_local_data *ctx = NULL;
79 /* list of snapshots currently not consumed */
80 GPtrArray *available_snapshots;
81 sem_t metadata_available;
82 int reload_trace = 0;
83
84 int last_textdump_print_newline = 1;
85
86 enum {
87 OPT_NONE = 0,
88 OPT_HELP,
89 OPT_TEXTDUMP,
90 OPT_PID,
91 OPT_CHILD,
92 OPT_HOSTNAME,
93 OPT_RELAY_HOSTNAME,
94 OPT_KPROBES,
95 OPT_BEGIN,
96 };
97
98 static struct poptOption long_options[] = {
99 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
100 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
101 { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL },
102 { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL },
103 { "begin", 'b', POPT_ARG_NONE, NULL, OPT_BEGIN, NULL, NULL },
104 { "pid", 'p', POPT_ARG_STRING, &opt_tid, OPT_PID, NULL, NULL },
105 { "hostname", 'n', POPT_ARG_STRING, &opt_hostname, OPT_HOSTNAME, NULL, NULL },
106 { "relay-hostname", 'r', POPT_ARG_STRING, &opt_relay_hostname,
107 OPT_RELAY_HOSTNAME, NULL, NULL },
108 { "kprobes", 'k', POPT_ARG_STRING, &opt_kprobes, OPT_KPROBES, NULL, NULL },
109 { NULL, 0, 0, NULL, 0, NULL, NULL },
110 };
111
112 #ifdef LTTNGTOP_MMAP_LIVE
113 static void handle_textdump_sigterm(int signal)
114 {
115 quit = 1;
116 lttng_destroy_session("test");
117 }
118 #endif
119
120 void *refresh_thread(void *p)
121 {
122 while (1) {
123 if (quit) {
124 sem_post(&pause_sem);
125 sem_post(&timer);
126 sem_post(&end_trace_sem);
127 sem_post(&goodtodisplay);
128 sem_post(&goodtoupdate);
129 pthread_exit(0);
130 }
131 if (!opt_input_path) {
132 #ifdef LTTNGTOP_MMAP_LIVE
133 mmap_live_flush(mmap_list);
134 #endif
135 }
136 sem_wait(&pause_sem);
137 sem_post(&pause_sem);
138 sem_post(&timer);
139 sleep(refresh_display/NSEC_PER_SEC);
140 }
141 }
142
143 void *ncurses_display(void *p)
144 {
145 unsigned int current_display_index = 0;
146
147 sem_wait(&bootstrap);
148 /*
149 * Prevent the 1 second delay when we hit ESC
150 */
151 ESCDELAY = 0;
152 init_ncurses();
153
154 while (1) {
155 sem_wait(&timer);
156 sem_wait(&goodtodisplay);
157 sem_wait(&pause_sem);
158
159 if (quit) {
160 sem_post(&pause_sem);
161 sem_post(&timer);
162 reset_ncurses();
163 pthread_exit(0);
164 }
165
166 copy = g_ptr_array_index(copies, current_display_index);
167 assert(copy);
168 display(current_display_index++);
169
170 sem_post(&goodtoupdate);
171 sem_post(&pause_sem);
172 }
173 }
174
175 void print_fields(struct bt_ctf_event *event)
176 {
177 unsigned int cnt, i;
178 const struct bt_definition *const * list;
179 const struct bt_declaration *l;
180 const struct bt_definition *scope;
181 enum ctf_type_id type;
182 const char *str;
183
184 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_FIELDS);
185
186 bt_ctf_get_field_list(event, scope, &list, &cnt);
187 for (i = 0; i < cnt; i++) {
188 if (i != 0)
189 printf(", ");
190 printf("%s = ", bt_ctf_field_name(list[i]));
191 l = bt_ctf_get_decl_from_def(list[i]);
192 type = bt_ctf_field_type(l);
193 if (type == CTF_TYPE_INTEGER) {
194 if (bt_ctf_get_int_signedness(l) == 0)
195 printf("%" PRIu64 "", bt_ctf_get_uint64(list[i]));
196 else
197 printf("%" PRId64 "", bt_ctf_get_int64(list[i]));
198 } else if (type == CTF_TYPE_STRING) {
199 printf("%s", bt_ctf_get_string(list[i]));
200 } else if (type == CTF_TYPE_ARRAY) {
201 str = bt_ctf_get_char_array(list[i]);
202 if (!bt_ctf_field_get_error() && str)
203 printf("%s", str);
204 }
205 }
206 }
207
208 /*
209 * hook on each event to check the timestamp and refresh the display if
210 * necessary
211 */
212 enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
213 {
214 unsigned long timestamp;
215 struct tm start;
216 uint64_t ts_nsec_start;
217 int pid, cpu_id;
218 int64_t syscall_ret;
219 const struct bt_definition *scope;
220 const char *hostname, *procname;
221
222 timestamp = bt_ctf_get_timestamp(call_data);
223
224 /* can happen in network live when tracing is idle */
225 if (timestamp < last_event_ts)
226 goto end_stop;
227
228 last_event_ts = timestamp;
229
230 start = format_timestamp(timestamp);
231 ts_nsec_start = timestamp % NSEC_PER_SEC;
232
233 pid = get_context_pid(call_data);
234 if (pid == -1ULL && opt_tid) {
235 goto error;
236 }
237
238 hostname = get_context_hostname(call_data);
239 if (opt_tid || opt_hostname)
240 if (!lookup_filter_tid_list(pid))
241 goto end;
242
243 cpu_id = get_cpu_id(call_data);
244 procname = get_context_comm(call_data);
245
246 if ((strcmp(bt_ctf_event_name(call_data), "exit_syscall") == 0) &&
247 !last_textdump_print_newline) {
248 scope = bt_ctf_get_top_level_scope(call_data,
249 BT_EVENT_FIELDS);
250 syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data,
251 scope, "_ret"));
252 printf("= %ld\n", syscall_ret);
253 last_textdump_print_newline = 1;
254 } else {
255 /* we might have lost the exit_syscall event, so need to
256 * print the newline in this case */
257 if (last_textdump_print_newline == 0)
258 printf("\n");
259 printf("%02d:%02d:%02d.%09" PRIu64 " (%s) (cpu %d) [%s (%d)] %s (",
260 start.tm_hour, start.tm_min, start.tm_sec,
261 ts_nsec_start, hostname, cpu_id, procname, pid,
262 bt_ctf_event_name(call_data));
263 print_fields(call_data);
264 printf(") ");
265 if (strncmp(bt_ctf_event_name(call_data), "sys_", 4) != 0) {
266 printf("\n");
267 last_textdump_print_newline = 1;
268 } else {
269 last_textdump_print_newline = 0;
270 }
271 }
272
273 end:
274 return BT_CB_OK;
275 error:
276 return BT_CB_ERROR_STOP;
277 end_stop:
278 return BT_CB_OK_STOP;
279 }
280
281 enum bt_cb_ret handle_kprobes(struct bt_ctf_event *call_data, void *private_data)
282 {
283 int i;
284 struct kprobes *kprobe;
285
286 /* for kprobes */
287 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
288 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
289 if (strcmp(bt_ctf_event_name(call_data), kprobe->probe_name) == 0) {
290 kprobe->count++;
291 }
292 }
293
294 return BT_CB_OK;
295 }
296
297 /*
298 * hook on each event to check the timestamp and refresh the display if
299 * necessary
300 */
301 enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
302 {
303 unsigned long timestamp;
304
305 timestamp = bt_ctf_get_timestamp(call_data);
306 if (timestamp == -1ULL)
307 goto error;
308
309 /* can happen in network live when tracing is idle */
310 if (timestamp < last_event_ts)
311 goto end_stop;
312
313 last_event_ts = timestamp;
314
315 if (last_display_update == 0)
316 last_display_update = timestamp;
317
318 if (timestamp - last_display_update >= refresh_display) {
319 sem_wait(&goodtoupdate);
320 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
321 timestamp));
322 sem_post(&goodtodisplay);
323 sem_post(&bootstrap);
324 last_display_update = timestamp;
325 }
326 return BT_CB_OK;
327
328 error:
329 fprintf(stderr, "check_timestamp callback error\n");
330 return BT_CB_ERROR_STOP;
331
332 end_stop:
333 return BT_CB_OK_STOP;
334 }
335
336 /*
337 * get_perf_counter : get or create and return a perf_counter struct for
338 * either a process or a cpu (only one of the 2 parameters mandatory)
339 */
340 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
341 struct cputime *cpu)
342 {
343 struct perfcounter *ret;
344 GHashTable *table;
345
346 if (proc)
347 table = proc->perf;
348 else if (cpu)
349 table = cpu->perf;
350 else
351 goto error;
352
353 ret = g_hash_table_lookup(table, (gpointer) name);
354 if (ret)
355 goto end;
356
357 ret = g_new0(struct perfcounter, 1);
358 /* by default, make it visible in the UI */
359 ret->visible = 1;
360 g_hash_table_insert(table, (gpointer) strdup(name), ret);
361
362 end:
363 return ret;
364
365 error:
366 return NULL;
367 }
368
369 void update_perf_value(struct processtop *proc, struct cputime *cpu,
370 const char *name, int value)
371 {
372 struct perfcounter *cpu_perf, *process_perf;
373
374 cpu_perf = get_perf_counter(name, NULL, cpu);
375 if (cpu_perf->count < value) {
376 process_perf = get_perf_counter(name, proc, NULL);
377 process_perf->count += value - cpu_perf->count;
378 cpu_perf->count = value;
379 }
380 }
381
382 void extract_perf_counter_scope(const struct bt_ctf_event *event,
383 const struct bt_definition *scope,
384 struct processtop *proc,
385 struct cputime *cpu)
386 {
387 struct bt_definition const * const *list = NULL;
388 const struct bt_definition *field;
389 unsigned int count;
390 struct perfcounter *perfcounter;
391 GHashTableIter iter;
392 gpointer key;
393 int ret;
394
395 if (!scope)
396 goto end;
397
398 ret = bt_ctf_get_field_list(event, scope, &list, &count);
399 if (ret < 0)
400 goto end;
401
402 if (count == 0)
403 goto end;
404
405 g_hash_table_iter_init(&iter, global_perf_liszt);
406 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) {
407 field = bt_ctf_get_field(event, scope, (char *) key);
408 if (field) {
409 int value = bt_ctf_get_uint64(field);
410 if (bt_ctf_field_get_error())
411 continue;
412 update_perf_value(proc, cpu, (char *) key, value);
413 }
414 }
415
416 end:
417 return;
418 }
419
420 void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
421 {
422 struct cputime *cpu;
423 const struct bt_definition *scope;
424
425 cpu = get_cpu(get_cpu_id(event));
426
427 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
428 extract_perf_counter_scope(event, scope, proc, cpu);
429
430 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
431 extract_perf_counter_scope(event, scope, proc, cpu);
432
433 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
434 extract_perf_counter_scope(event, scope, proc, cpu);
435 }
436
437 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
438 void *private_data)
439 {
440 int pid, tid, ppid, vpid, vtid, vppid;
441 char *comm, *hostname;
442 struct processtop *parent, *child;
443 unsigned long timestamp;
444
445 timestamp = bt_ctf_get_timestamp(call_data);
446 if (timestamp == -1ULL)
447 goto error;
448
449 pid = get_context_pid(call_data);
450 if (pid == -1ULL) {
451 goto error;
452 }
453
454 tid = get_context_tid(call_data);
455 if (tid == -1ULL) {
456 goto error;
457 }
458 ppid = get_context_ppid(call_data);
459 if (ppid == -1ULL) {
460 goto error;
461 }
462 vpid = get_context_vpid(call_data);
463 if (pid == -1ULL) {
464 vpid = -1;
465 }
466 vtid = get_context_vtid(call_data);
467 if (tid == -1ULL) {
468 vtid = -1;
469 }
470 vppid = get_context_vppid(call_data);
471 if (ppid == -1ULL) {
472 vppid = -1;
473 }
474 comm = get_context_comm(call_data);
475 if (!comm) {
476 goto error;
477 }
478 /* optional */
479 hostname = get_context_hostname(call_data);
480
481 /* find or create the current process */
482 child = find_process_tid(&lttngtop, tid, comm);
483 if (!child)
484 child = add_proc(&lttngtop, tid, comm, timestamp, hostname);
485 if (!child)
486 goto end;
487 update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm, hostname);
488
489 if (pid != tid) {
490 /* find or create the parent */
491 parent = find_process_tid(&lttngtop, pid, comm);
492 if (!parent) {
493 parent = add_proc(&lttngtop, pid, comm, timestamp, hostname);
494 if (parent)
495 parent->pid = pid;
496 }
497
498 /* attach the parent to the current process */
499 child->threadparent = parent;
500 add_thread(parent, child);
501 }
502
503 update_perf_counter(child, call_data);
504
505 end:
506 return BT_CB_OK;
507
508 error:
509 return BT_CB_ERROR_STOP;
510 }
511
512 void init_lttngtop()
513 {
514 copies = g_ptr_array_new();
515 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
516 global_filter_list = g_hash_table_new(g_str_hash, g_str_equal);
517 global_host_list = g_hash_table_new(g_str_hash, g_str_equal);
518
519 sem_init(&goodtodisplay, 0, 0);
520 sem_init(&goodtoupdate, 0, 1);
521 sem_init(&timer, 0, 1);
522 sem_init(&bootstrap, 0, 0);
523 sem_init(&pause_sem, 0, 1);
524 sem_init(&end_trace_sem, 0, 0);
525
526 reset_global_counters();
527 lttngtop.nbproc = 0;
528 lttngtop.nbthreads = 0;
529 lttngtop.nbfiles = 0;
530
531 lttngtop.process_hash_table = g_hash_table_new(g_direct_hash,
532 g_direct_equal);
533 lttngtop.process_table = g_ptr_array_new();
534 lttngtop.files_table = g_ptr_array_new();
535 lttngtop.cpu_table = g_ptr_array_new();
536
537 toggle_filter = -1;
538 }
539
540 void usage(FILE *fp)
541 {
542 fprintf(fp, "LTTngTop %s\n\n", VERSION);
543 fprintf(fp, "Usage : lttngtop [OPTIONS] [TRACE]\n");
544 fprintf(fp, " TRACE Path to the trace to analyse (no trace path for live tracing)\n");
545 fprintf(fp, " -h, --help This help message\n");
546 fprintf(fp, " -t, --textdump Display live events in text-only\n");
547 fprintf(fp, " -p, --pid Comma-separated list of PIDs to display\n");
548 fprintf(fp, " -f, --child Follow threads associated with selected PIDs\n");
549 fprintf(fp, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
550 fprintf(fp, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
551 }
552
553 /*
554 * Parse probe options.
555 * Shamelessly stolen from lttng-tools :
556 * src/bin/lttng/commands/enable_events.c
557 */
558 static struct kprobes *parse_probe_opts(char *opt)
559 {
560 char s_hex[19];
561 char name[LTTNG_SYMBOL_NAME_LEN];
562 struct kprobes *kprobe;
563 int ret;
564
565 /*
566 kprobe->probe_addr = 0;
567 kprobe->probe_offset = 0;
568 asprintf(&kprobe->probe_name, "probe_sys_open");
569 asprintf(&kprobe->symbol_name, "sys_open");
570 */
571
572 if (opt == NULL) {
573 kprobe = NULL;
574 goto end;
575 }
576
577 kprobe = g_new0(struct kprobes, 1);
578
579 /* Check for symbol+offset */
580 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
581 if (ret == 2) {
582 ret = asprintf(&kprobe->probe_name, "probe_%s", name);
583 ret = asprintf(&kprobe->symbol_name, "%s", name);
584
585 if (strlen(s_hex) == 0) {
586 fprintf(stderr, "Invalid probe offset %s", s_hex);
587 ret = -1;
588 goto end;
589 }
590 kprobe->probe_offset = strtoul(s_hex, NULL, 0);
591 kprobe->probe_addr = 0;
592 goto end;
593 }
594
595 /* Check for symbol */
596 if (isalpha(name[0])) {
597 ret = sscanf(opt, "%s", name);
598 if (ret == 1) {
599 ret = asprintf(&kprobe->probe_name, "probe_%s", name);
600 ret = asprintf(&kprobe->symbol_name, "%s", name);
601 kprobe->probe_offset = 0;
602 kprobe->probe_addr = 0;
603 goto end;
604 }
605 }
606
607 /* Check for address */
608 ret = sscanf(opt, "%s", s_hex);
609 if (ret > 0) {
610 if (strlen(s_hex) == 0) {
611 fprintf(stderr, "Invalid probe address %s", s_hex);
612 ret = -1;
613 goto end;
614 }
615 ret = asprintf(&kprobe->probe_name, "probe_%s", s_hex);
616 kprobe->probe_offset = 0;
617 kprobe->probe_addr = strtoul(s_hex, NULL, 0);
618 goto end;
619 }
620
621 /* No match */
622 kprobe = NULL;
623
624 end:
625 return kprobe;
626 }
627
628 /*
629 * Return 0 if caller should continue, < 0 if caller should return
630 * error, > 0 if caller should exit without reporting error.
631 */
632 static int parse_options(int argc, char **argv)
633 {
634 poptContext pc;
635 int opt, ret = 0;
636 char *tmp_str;
637 int *tid;
638
639 remote_live = 0;
640
641 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
642 poptReadDefaultConfig(pc, 0);
643
644 while ((opt = poptGetNextOpt(pc)) != -1) {
645 switch (opt) {
646 case OPT_HELP:
647 usage(stdout);
648 ret = 1; /* exit cleanly */
649 goto end;
650 case OPT_TEXTDUMP:
651 opt_textdump = 1;
652 break;
653 case OPT_CHILD:
654 opt_textdump = 1;
655 opt_child = 1;
656 break;
657 case OPT_PID:
658 toggle_filter = 1;
659 tid_filter_list = g_hash_table_new(g_str_hash,
660 g_str_equal);
661 tmp_str = strtok(opt_tid, ",");
662 while (tmp_str) {
663 tid = malloc(sizeof(int));
664 *tid = atoi(tmp_str);
665 g_hash_table_insert(tid_filter_list,
666 (gpointer) tid, tid);
667 tmp_str = strtok(NULL, ",");
668 }
669 break;
670 case OPT_BEGIN:
671 /* start reading the live trace from the beginning */
672 opt_begin = 1;
673 break;
674 case OPT_HOSTNAME:
675 toggle_filter = 1;
676 tmp_str = strtok(opt_hostname, ",");
677 while (tmp_str) {
678 add_hostname_list(tmp_str, 1);
679 tmp_str = strtok(NULL, ",");
680 }
681 break;
682 case OPT_RELAY_HOSTNAME:
683 remote_live = 1;
684 break;
685 case OPT_KPROBES:
686 lttngtop.kprobes_table = g_ptr_array_new();
687 tmp_str = strtok(opt_kprobes, ",");
688 while (tmp_str) {
689 struct kprobes *kprobe;
690
691 kprobe = parse_probe_opts(tmp_str);
692 if (kprobe) {
693 g_ptr_array_add(
694 lttngtop.kprobes_table,
695 kprobe);
696 } else {
697 ret = -EINVAL;
698 goto end;
699 }
700 tmp_str = strtok(NULL, ",");
701 }
702 break;
703 default:
704 ret = -EINVAL;
705 goto end;
706 }
707 }
708
709 opt_input_path = poptGetArg(pc);
710
711 end:
712 if (pc) {
713 poptFreeContext(pc);
714 }
715 return ret;
716 }
717
718 void iter_trace(struct bt_context *bt_ctx)
719 {
720 struct bt_ctf_iter *iter;
721 struct bt_iter_pos begin_pos;
722 struct kprobes *kprobe;
723 const struct bt_ctf_event *event;
724 int i;
725 int ret = 0;
726
727 begin_pos.type = BT_SEEK_BEGIN;
728 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
729
730 /* at each event, verify the status of the process table */
731 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
732 fix_process_table,
733 NULL, NULL, NULL);
734 if (opt_textdump) {
735 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
736 print_timestamp,
737 NULL, NULL, NULL);
738 } else {
739 /* at each event check if we need to refresh */
740 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
741 check_timestamp,
742 NULL, NULL, NULL);
743 /* to handle the scheduling events */
744 bt_ctf_iter_add_callback(iter,
745 g_quark_from_static_string("sched_switch"),
746 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
747 /* to clean up the process table */
748 bt_ctf_iter_add_callback(iter,
749 g_quark_from_static_string("sched_process_free"),
750 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
751 /* to get all the process from the statedumps */
752 bt_ctf_iter_add_callback(iter,
753 g_quark_from_static_string(
754 "lttng_statedump_process_state"),
755 NULL, 0, handle_statedump_process_state,
756 NULL, NULL, NULL);
757
758 /* for IO top */
759 bt_ctf_iter_add_callback(iter,
760 g_quark_from_static_string("exit_syscall"),
761 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
762 bt_ctf_iter_add_callback(iter,
763 g_quark_from_static_string("sys_write"),
764 NULL, 0, handle_sys_write, NULL, NULL, NULL);
765 bt_ctf_iter_add_callback(iter,
766 g_quark_from_static_string("sys_read"),
767 NULL, 0, handle_sys_read, NULL, NULL, NULL);
768 bt_ctf_iter_add_callback(iter,
769 g_quark_from_static_string("sys_open"),
770 NULL, 0, handle_sys_open, NULL, NULL, NULL);
771 bt_ctf_iter_add_callback(iter,
772 g_quark_from_static_string("sys_close"),
773 NULL, 0, handle_sys_close, NULL, NULL, NULL);
774 bt_ctf_iter_add_callback(iter,
775 g_quark_from_static_string(
776 "lttng_statedump_file_descriptor"),
777 NULL, 0, handle_statedump_file_descriptor,
778 NULL, NULL, NULL);
779
780 /* for kprobes */
781 if (lttngtop.kprobes_table) {
782 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
783 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
784 bt_ctf_iter_add_callback(iter,
785 g_quark_from_static_string(
786 kprobe->probe_name),
787 NULL, 0, handle_kprobes,
788 NULL, NULL, NULL);
789 }
790 }
791 }
792
793 while ((event = bt_ctf_iter_read_event(iter)) != NULL) {
794 if (quit || reload_trace)
795 goto end_iter;
796 ret = bt_iter_next(bt_ctf_get_iter(iter));
797 if (ret < 0)
798 goto end_iter;
799 }
800
801 /* block until quit, we reached the end of the trace */
802 sem_wait(&end_trace_sem);
803
804 end_iter:
805 bt_ctf_iter_destroy(iter);
806 }
807
808 /*
809 * bt_context_add_traces_recursive: Open a trace recursively
810 * (copied from BSD code in converter/babeltrace.c)
811 *
812 * Find each trace present in the subdirectory starting from the given
813 * path, and add them to the context. The packet_seek parameter can be
814 * NULL: this specify to use the default format packet_seek.
815 *
816 * Return: 0 on success, nonzero on failure.
817 * Unable to open toplevel: failure.
818 * Unable to open some subdirectory or file: warn and continue;
819 */
820 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
821 const char *format_str,
822 void (*packet_seek)(struct bt_stream_pos *pos,
823 size_t offset, int whence))
824 {
825 FTS *tree;
826 FTSENT *node;
827 GArray *trace_ids;
828 char lpath[PATH_MAX];
829 char * const paths[2] = { lpath, NULL };
830 int ret = -1;
831
832 /*
833 * Need to copy path, because fts_open can change it.
834 * It is the pointer array, not the strings, that are constant.
835 */
836 strncpy(lpath, path, PATH_MAX);
837 lpath[PATH_MAX - 1] = '\0';
838
839 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
840 if (tree == NULL) {
841 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
842 path);
843 return -EINVAL;
844 }
845
846 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
847
848 while ((node = fts_read(tree))) {
849 int dirfd, metafd;
850
851 if (!(node->fts_info & FTS_D))
852 continue;
853
854 dirfd = open(node->fts_accpath, 0);
855 if (dirfd < 0) {
856 fprintf(stderr, "[error] [Context] Unable to open trace "
857 "directory file descriptor.\n");
858 ret = dirfd;
859 goto error;
860 }
861 metafd = openat(dirfd, "metadata", O_RDONLY);
862 if (metafd < 0) {
863 close(dirfd);
864 ret = -1;
865 continue;
866 } else {
867 int trace_id;
868
869 ret = close(metafd);
870 if (ret < 0) {
871 perror("close");
872 goto error;
873 }
874 ret = close(dirfd);
875 if (ret < 0) {
876 perror("close");
877 goto error;
878 }
879
880 trace_id = bt_context_add_trace(ctx,
881 node->fts_accpath, format_str,
882 packet_seek, NULL, NULL);
883 if (trace_id < 0) {
884 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
885 "for reading.\n", node->fts_accpath, path);
886 /* Allow to skip erroneous traces. */
887 continue;
888 }
889 g_array_append_val(trace_ids, trace_id);
890 }
891 }
892
893 g_array_free(trace_ids, TRUE);
894 return ret;
895
896 error:
897 return ret;
898 }
899
900 static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
901 int field_cnt, int *tid_check, int *pid_check,
902 int *procname_check, int *ppid_check)
903 {
904 int j;
905 struct perfcounter *global;
906 const char *name;
907
908 for (j = 0; j < field_cnt; j++) {
909 name = bt_ctf_get_decl_field_name(field_list[j]);
910 if (*tid_check == 0) {
911 if (strncmp(name, "tid", 3) == 0)
912 (*tid_check)++;
913 }
914 if (*pid_check == 0) {
915 if (strncmp(name, "pid", 3) == 0)
916 (*pid_check)++;
917 }
918 if (*ppid_check == 0) {
919 if (strncmp(name, "ppid", 4) == 0)
920 (*ppid_check)++;
921 }
922 if (*procname_check == 0) {
923 if (strncmp(name, "procname", 8) == 0)
924 (*procname_check)++;
925 }
926 if (strncmp(name, "perf_", 5) == 0) {
927 global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
928 if (!global) {
929 global = g_new0(struct perfcounter, 1);
930 /* by default, sort on the first perf context */
931 if (g_hash_table_size(global_perf_liszt) == 0)
932 global->sort = 1;
933 global->visible = 1;
934 g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
935 }
936 }
937 }
938
939 if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
940 *procname_check == 1)
941 return 0;
942
943 return -1;
944 }
945
946 /*
947 * check_requirements: check if the required context informations are available
948 *
949 * If each mandatory context information is available for at least in one
950 * event, return 0 otherwise return -1.
951 */
952 int check_requirements(struct bt_context *ctx)
953 {
954 unsigned int i, evt_cnt, field_cnt;
955 struct bt_ctf_event_decl *const * evt_list;
956 const struct bt_ctf_field_decl *const * field_list;
957 int tid_check = 0;
958 int pid_check = 0;
959 int procname_check = 0;
960 int ppid_check = 0;
961 int ret = 0;
962
963 ret = bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt);
964 if (ret < 0) {
965 goto end;
966 }
967
968 for (i = 0; i < evt_cnt; i++) {
969 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT,
970 &field_list, &field_cnt);
971 ret = check_field_requirements(field_list, field_cnt,
972 &tid_check, &pid_check, &procname_check,
973 &ppid_check);
974
975 bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT,
976 &field_list, &field_cnt);
977 ret = check_field_requirements(field_list, field_cnt,
978 &tid_check, &pid_check, &procname_check,
979 &ppid_check);
980
981 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT,
982 &field_list, &field_cnt);
983 ret = check_field_requirements(field_list, field_cnt,
984 &tid_check, &pid_check, &procname_check,
985 &ppid_check);
986 }
987
988 if (tid_check == 0) {
989 ret = -1;
990 fprintf(stderr, "[error] missing tid context information\n");
991 }
992 if (pid_check == 0) {
993 ret = -1;
994 fprintf(stderr, "[error] missing pid context information\n");
995 }
996 if (ppid_check == 0) {
997 ret = -1;
998 fprintf(stderr, "[error] missing ppid context information\n");
999 }
1000 if (procname_check == 0) {
1001 ret = -1;
1002 fprintf(stderr, "[error] missing procname context information\n");
1003 }
1004
1005 end:
1006 return ret;
1007 }
1008
1009 int main(int argc, char **argv)
1010 {
1011 int ret;
1012 struct bt_context *bt_ctx = NULL;
1013
1014 init_lttngtop();
1015 ret = parse_options(argc, argv);
1016 if (ret < 0) {
1017 fprintf(stdout, "Error parsing options.\n\n");
1018 usage(stdout);
1019 exit(EXIT_FAILURE);
1020 } else if (ret > 0) {
1021 exit(EXIT_SUCCESS);
1022 }
1023
1024 if (!opt_input_path && !remote_live) {
1025 /* mmap live */
1026 #ifdef LTTNGTOP_MMAP_LIVE
1027 if (opt_textdump) {
1028 signal(SIGTERM, handle_textdump_sigterm);
1029 signal(SIGINT, handle_textdump_sigterm);
1030 }
1031 mmap_live_loop(bt_ctx);
1032 pthread_join(timer_thread, NULL);
1033 quit = 1;
1034 pthread_join(display_thread, NULL);
1035
1036 lttng_stop_tracing("test");
1037 lttng_destroy_session("test");
1038
1039 goto end;
1040 #else
1041 fprintf(stderr, "Mmap live support not compiled\n");
1042 goto end;
1043 #endif /* LTTNGTOP_MMAP_LIVE */
1044 } else if (!opt_input_path && remote_live) {
1045 /* network live */
1046 ret = setup_network_live(opt_relay_hostname, opt_begin);
1047 if (ret < 0) {
1048 goto end;
1049 }
1050
1051 ret = open_trace(&bt_ctx);
1052 if (ret < 0) {
1053 goto end;
1054 }
1055 } else {
1056 //init_lttngtop();
1057
1058 bt_ctx = bt_context_create();
1059 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
1060 if (ret < 0) {
1061 fprintf(stderr, "[error] Opening the trace\n");
1062 goto end;
1063 }
1064 }
1065
1066 ret = check_requirements(bt_ctx);
1067 if (ret < 0) {
1068 fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n");
1069 goto end;
1070 }
1071 if (!opt_textdump) {
1072 pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL);
1073 pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL);
1074 }
1075
1076 iter_trace(bt_ctx);
1077
1078 pthread_join(display_thread, NULL);
1079 quit = 1;
1080 pthread_join(timer_thread, NULL);
1081
1082 end:
1083 if (bt_ctx)
1084 bt_context_put(bt_ctx);
1085
1086 return 0;
1087 }
This page took 0.079044 seconds and 3 git commands to generate.