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