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