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