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