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