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