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