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