attach to a process in gui or textdump + fix get_proc returning NULL 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
1fc22eb4
JD
76enum {
77 OPT_NONE = 0,
78 OPT_HELP,
6777529d 79 OPT_TEXTDUMP,
32b70e07
JD
80 OPT_PID,
81 OPT_CHILD,
1fc22eb4
JD
82};
83
84static struct poptOption long_options[] = {
85 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
86 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
6777529d 87 { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL },
32b70e07
JD
88 { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL },
89 { "pid", 'p', POPT_ARG_INT, &opt_pid, OPT_PID, NULL, NULL },
1fc22eb4
JD
90 { NULL, 0, 0, NULL, 0, NULL, NULL },
91};
92
93void *refresh_thread(void *p)
94{
cade3418
JD
95 struct mmap_stream *mmap_info;
96
1fc22eb4 97 while (1) {
ae9e85c7
JD
98 if (quit) {
99 sem_post(&pause_sem);
100 sem_post(&timer);
1402044a 101 sem_post(&end_trace_sem);
ae9e85c7 102 sem_post(&goodtodisplay);
1402044a 103 sem_post(&goodtoupdate);
ae9e85c7
JD
104 pthread_exit(0);
105 }
1402044a 106 if (!opt_input_path) {
d2d680e0
JD
107 bt_list_for_each_entry(mmap_info, &mmap_list.head, list)
108 helper_kernctl_buffer_flush(mmap_info->fd);
1402044a 109 }
1fc22eb4
JD
110 sem_wait(&pause_sem);
111 sem_post(&pause_sem);
112 sem_post(&timer);
113 sleep(refresh_display/NSEC_PER_SEC);
114 }
115}
116
117void *ncurses_display(void *p)
118{
119 unsigned int current_display_index = 0;
120
121 sem_wait(&bootstrap);
b332d28f
JD
122 /*
123 * Prevent the 1 second delay when we hit ESC
124 */
125 ESCDELAY = 0;
1fc22eb4
JD
126 init_ncurses();
127
128 while (1) {
129 sem_wait(&timer);
130 sem_wait(&goodtodisplay);
131 sem_wait(&pause_sem);
132
ae9e85c7 133 if (quit) {
33572a17
JD
134 sem_post(&pause_sem);
135 sem_post(&timer);
ae9e85c7
JD
136 reset_ncurses();
137 pthread_exit(0);
138 }
139
1fc22eb4 140 copy = g_ptr_array_index(copies, current_display_index);
85db4618
JD
141 assert(copy);
142 display(current_display_index++);
1fc22eb4
JD
143
144 sem_post(&goodtoupdate);
145 sem_post(&pause_sem);
1fc22eb4
JD
146 }
147}
148
6112d0d4
JD
149/*
150 * hook on each event to check the timestamp and refresh the display if
151 * necessary
152 */
153enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
154{
155 unsigned long timestamp;
156 struct tm start;
b520ab45 157 uint64_t ts_nsec_start;
32b70e07 158 int pid;
d2d680e0
JD
159 int64_t syscall_ret;
160 const struct definition *scope;
6112d0d4 161
6112d0d4
JD
162 timestamp = bt_ctf_get_timestamp(call_data);
163
b520ab45 164 start = format_timestamp(timestamp);
6112d0d4
JD
165 ts_nsec_start = timestamp % NSEC_PER_SEC;
166
32b70e07
JD
167 pid = get_context_pid(call_data);
168 if (pid == -1ULL && opt_pid) {
169 goto error;
170 }
171
172 if (opt_pid && opt_pid != pid)
173 goto end;
174
d2d680e0
JD
175 if (strcmp(bt_ctf_event_name(call_data), "exit_syscall") == 0) {
176 scope = bt_ctf_get_top_level_scope(call_data,
177 BT_EVENT_FIELDS);
178 syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data,
179 scope, "_ret"));
180 printf("= %ld\n", syscall_ret);
181 } else {
182 printf("%02d:%02d:%02d.%09" PRIu64 " %d : %s ", start.tm_hour,
183 start.tm_min, start.tm_sec, ts_nsec_start,
184 pid, bt_ctf_event_name(call_data));
185 }
6112d0d4 186
32b70e07 187end:
6112d0d4 188 return BT_CB_OK;
32b70e07
JD
189error:
190 return BT_CB_ERROR_STOP;
6112d0d4
JD
191}
192
1fc22eb4
JD
193/*
194 * hook on each event to check the timestamp and refresh the display if
195 * necessary
196 */
4adc8274 197enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
1fc22eb4
JD
198{
199 unsigned long timestamp;
200
c78f2cdc 201 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
202 if (timestamp == -1ULL)
203 goto error;
204
205 if (last_display_update == 0)
206 last_display_update = timestamp;
207
208 if (timestamp - last_display_update >= refresh_display) {
209 sem_wait(&goodtoupdate);
210 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
211 timestamp));
212 sem_post(&goodtodisplay);
213 sem_post(&bootstrap);
214 last_display_update = timestamp;
215 }
216 return BT_CB_OK;
217
218error:
219 fprintf(stderr, "check_timestamp callback error\n");
220 return BT_CB_ERROR_STOP;
221}
222
223/*
224 * get_perf_counter : get or create and return a perf_counter struct for
225 * either a process or a cpu (only one of the 2 parameters mandatory)
226 */
227struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
228 struct cputime *cpu)
229{
bb6d72a2 230 struct perfcounter *ret;
1fc22eb4
JD
231 GHashTable *table;
232
233 if (proc)
234 table = proc->perf;
235 else if (cpu)
236 table = cpu->perf;
237 else
238 goto error;
239
240 ret = g_hash_table_lookup(table, (gpointer) name);
241 if (ret)
242 goto end;
243
559c9f86 244 ret = g_new0(struct perfcounter, 1);
1fc22eb4
JD
245 /* by default, make it visible in the UI */
246 ret->visible = 1;
85db4618 247 g_hash_table_insert(table, (gpointer) strdup(name), ret);
1fc22eb4 248
1fc22eb4
JD
249end:
250 return ret;
251
252error:
253 return NULL;
254}
255
256void update_perf_value(struct processtop *proc, struct cputime *cpu,
257 const char *name, int value)
258{
259 struct perfcounter *cpu_perf, *process_perf;
260
261 cpu_perf = get_perf_counter(name, NULL, cpu);
262 if (cpu_perf->count < value) {
263 process_perf = get_perf_counter(name, proc, NULL);
264 process_perf->count += value - cpu_perf->count;
265 cpu_perf->count = value;
266 }
267}
268
4adc8274 269void extract_perf_counter_scope(const struct bt_ctf_event *event,
2e0a1190 270 const struct bt_definition *scope,
1fc22eb4
JD
271 struct processtop *proc,
272 struct cputime *cpu)
273{
2e0a1190
JD
274 struct bt_definition const * const *list = NULL;
275 const struct bt_definition *field;
1fc22eb4 276 unsigned int count;
bb6d72a2
JD
277 struct perfcounter *perfcounter;
278 GHashTableIter iter;
279 gpointer key;
280 int ret;
1fc22eb4
JD
281
282 if (!scope)
283 goto end;
284
285 ret = bt_ctf_get_field_list(event, scope, &list, &count);
286 if (ret < 0)
287 goto end;
288
bb6d72a2
JD
289 if (count == 0)
290 goto end;
291
292 g_hash_table_iter_init(&iter, global_perf_liszt);
293 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) {
294 field = bt_ctf_get_field(event, scope, (char *) key);
295 if (field) {
296 int value = bt_ctf_get_uint64(field);
1fc22eb4
JD
297 if (bt_ctf_field_get_error())
298 continue;
bb6d72a2 299 update_perf_value(proc, cpu, (char *) key, value);
1fc22eb4
JD
300 }
301 }
302
303end:
304 return;
305}
306
4adc8274 307void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
1fc22eb4 308{
1fc22eb4 309 struct cputime *cpu;
2e0a1190 310 const struct bt_definition *scope;
1fc22eb4 311
d67167cd 312 cpu = get_cpu(get_cpu_id(event));
1fc22eb4
JD
313
314 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
315 extract_perf_counter_scope(event, scope, proc, cpu);
316
317 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
318 extract_perf_counter_scope(event, scope, proc, cpu);
319
320 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
321 extract_perf_counter_scope(event, scope, proc, cpu);
1fc22eb4
JD
322}
323
4adc8274 324enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
1fc22eb4
JD
325 void *private_data)
326{
1402044a 327 int pid, tid, ppid, vpid, vtid, vppid;
1fc22eb4
JD
328 char *comm;
329 struct processtop *parent, *child;
1fc22eb4
JD
330 unsigned long timestamp;
331
c78f2cdc 332 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
333 if (timestamp == -1ULL)
334 goto error;
335
1dec520a
JD
336 pid = get_context_pid(call_data);
337 if (pid == -1ULL) {
1fc22eb4
JD
338 goto error;
339 }
96aa77de 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);
96aa77de
JD
370 if (!child)
371 goto end;
1402044a 372 update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm);
1fc22eb4
JD
373
374 if (pid != tid) {
375 /* find or create the parent */
376 parent = find_process_tid(&lttngtop, pid, comm);
377 if (!parent) {
378 parent = add_proc(&lttngtop, pid, comm, timestamp);
96aa77de
JD
379 if (parent)
380 parent->pid = pid;
1fc22eb4
JD
381 }
382
383 /* attach the parent to the current process */
384 child->threadparent = parent;
385 add_thread(parent, child);
386 }
387
388 update_perf_counter(child, call_data);
389
96aa77de 390end:
1fc22eb4
JD
391 return BT_CB_OK;
392
393error:
394 return BT_CB_ERROR_STOP;
395}
396
397void init_lttngtop()
398{
399 copies = g_ptr_array_new();
0d91c12a 400 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
401
402 sem_init(&goodtodisplay, 0, 0);
403 sem_init(&goodtoupdate, 0, 1);
404 sem_init(&timer, 0, 1);
405 sem_init(&bootstrap, 0, 0);
406 sem_init(&pause_sem, 0, 1);
407 sem_init(&end_trace_sem, 0, 0);
408
e05a35a6
JD
409 reset_global_counters();
410 lttngtop.nbproc = 0;
411 lttngtop.nbthreads = 0;
412 lttngtop.nbfiles = 0;
413
30b646c4
JD
414 lttngtop.process_hash_table = g_hash_table_new(g_direct_hash,
415 g_direct_equal);
1fc22eb4
JD
416 lttngtop.process_table = g_ptr_array_new();
417 lttngtop.files_table = g_ptr_array_new();
418 lttngtop.cpu_table = g_ptr_array_new();
419}
420
c263c4eb 421void usage(FILE *fp)
1fc22eb4 422{
c263c4eb
JD
423 fprintf(fp, "LTTngTop %s\n\n", VERSION);
424 fprintf(fp, "Usage : lttngtop /path/to/trace\n");
1fc22eb4
JD
425}
426
427/*
428 * Return 0 if caller should continue, < 0 if caller should return
429 * error, > 0 if caller should exit without reporting error.
430 */
431static int parse_options(int argc, char **argv)
432{
433 poptContext pc;
434 int opt, ret = 0;
435
1fc22eb4
JD
436 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
437 poptReadDefaultConfig(pc, 0);
438
439 while ((opt = poptGetNextOpt(pc)) != -1) {
440 switch (opt) {
441 case OPT_HELP:
442 usage(stdout);
443 ret = 1; /* exit cleanly */
444 goto end;
6777529d
JD
445 case OPT_TEXTDUMP:
446 opt_textdump = 1;
32b70e07
JD
447 break;
448 case OPT_CHILD:
449 opt_textdump = 1;
450 opt_child = 1;
451 break;
452 case OPT_PID:
96aa77de 453 //opt_textdump = 1;
32b70e07 454 break;
1fc22eb4
JD
455 default:
456 ret = -EINVAL;
457 goto end;
458 }
459 }
460
461 opt_input_path = poptGetArg(pc);
16b22a0f 462
1fc22eb4
JD
463end:
464 if (pc) {
465 poptFreeContext(pc);
466 }
467 return ret;
468}
469
470void iter_trace(struct bt_context *bt_ctx)
471{
472 struct bt_ctf_iter *iter;
473 struct bt_iter_pos begin_pos;
4adc8274 474 const struct bt_ctf_event *event;
1fc22eb4
JD
475 int ret = 0;
476
477 begin_pos.type = BT_SEEK_BEGIN;
478 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
479
6777529d
JD
480 if (opt_textdump) {
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;
96aa77de 951 if (opt_pid && opt_textdump) {
d2d680e0
JD
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");
96aa77de 970// if (!opt_pid) {
d2d680e0
JD
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 }
96aa77de 977// }
16b22a0f 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);
96aa77de 990// if (!opt_pid) {
d2d680e0
JD
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);
96aa77de 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.14009 seconds and 4 git commands to generate.