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