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