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