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