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