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