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