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