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