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