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