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