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