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