attach to a process in gui or textdump + fix get_proc returning NULL for filtering
[lttngtop.git] / src / lttngtop.c
1 /*
2 * Copyright (C) 2011-2012 Julien Desfossez
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 *
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.
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>
25 #include <babeltrace/ctf/iterator.h>
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>
39 #include <assert.h>
40 #include <sys/mman.h>
41 #include <lttng/lttng.h>
42 #include <lttng/lttngtop-helper.h>
43 #include <babeltrace/lttngtopmmappacketseek.h>
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
53 const char *opt_input_path;
54 static int opt_textdump;
55 static int opt_child;
56
57 int quit = 0;
58
59 struct lttngtop *copy;
60 pthread_t display_thread;
61 pthread_t timer_thread;
62
63 unsigned long refresh_display = 1 * NSEC_PER_SEC;
64 unsigned long last_display_update = 0;
65
66 /* list of FDs available for being read with snapshots */
67 struct mmap_stream_list mmap_list;
68 GPtrArray *lttng_consumer_stream_array;
69 int sessiond_metadata, consumerd_metadata;
70 struct lttng_consumer_local_data *ctx = NULL;
71 /* list of snapshots currently not consumed */
72 GPtrArray *available_snapshots;
73 sem_t metadata_available;
74 int reload_trace = 0;
75
76 enum {
77 OPT_NONE = 0,
78 OPT_HELP,
79 OPT_TEXTDUMP,
80 OPT_PID,
81 OPT_CHILD,
82 };
83
84 static struct poptOption long_options[] = {
85 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
86 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
87 { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL },
88 { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL },
89 { "pid", 'p', POPT_ARG_INT, &opt_pid, OPT_PID, NULL, NULL },
90 { NULL, 0, 0, NULL, 0, NULL, NULL },
91 };
92
93 void *refresh_thread(void *p)
94 {
95 struct mmap_stream *mmap_info;
96
97 while (1) {
98 if (quit) {
99 sem_post(&pause_sem);
100 sem_post(&timer);
101 sem_post(&end_trace_sem);
102 sem_post(&goodtodisplay);
103 sem_post(&goodtoupdate);
104 pthread_exit(0);
105 }
106 if (!opt_input_path) {
107 bt_list_for_each_entry(mmap_info, &mmap_list.head, list)
108 helper_kernctl_buffer_flush(mmap_info->fd);
109 }
110 sem_wait(&pause_sem);
111 sem_post(&pause_sem);
112 sem_post(&timer);
113 sleep(refresh_display/NSEC_PER_SEC);
114 }
115 }
116
117 void *ncurses_display(void *p)
118 {
119 unsigned int current_display_index = 0;
120
121 sem_wait(&bootstrap);
122 /*
123 * Prevent the 1 second delay when we hit ESC
124 */
125 ESCDELAY = 0;
126 init_ncurses();
127
128 while (1) {
129 sem_wait(&timer);
130 sem_wait(&goodtodisplay);
131 sem_wait(&pause_sem);
132
133 if (quit) {
134 sem_post(&pause_sem);
135 sem_post(&timer);
136 reset_ncurses();
137 pthread_exit(0);
138 }
139
140 copy = g_ptr_array_index(copies, current_display_index);
141 assert(copy);
142 display(current_display_index++);
143
144 sem_post(&goodtoupdate);
145 sem_post(&pause_sem);
146 }
147 }
148
149 /*
150 * hook on each event to check the timestamp and refresh the display if
151 * necessary
152 */
153 enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
154 {
155 unsigned long timestamp;
156 struct tm start;
157 uint64_t ts_nsec_start;
158 int pid;
159 int64_t syscall_ret;
160 const struct definition *scope;
161
162 timestamp = bt_ctf_get_timestamp(call_data);
163
164 start = format_timestamp(timestamp);
165 ts_nsec_start = timestamp % NSEC_PER_SEC;
166
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
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 }
186
187 end:
188 return BT_CB_OK;
189 error:
190 return BT_CB_ERROR_STOP;
191 }
192
193 /*
194 * hook on each event to check the timestamp and refresh the display if
195 * necessary
196 */
197 enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
198 {
199 unsigned long timestamp;
200
201 timestamp = bt_ctf_get_timestamp(call_data);
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
218 error:
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 */
227 struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
228 struct cputime *cpu)
229 {
230 struct perfcounter *ret;
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
244 ret = g_new0(struct perfcounter, 1);
245 /* by default, make it visible in the UI */
246 ret->visible = 1;
247 g_hash_table_insert(table, (gpointer) strdup(name), ret);
248
249 end:
250 return ret;
251
252 error:
253 return NULL;
254 }
255
256 void 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
269 void extract_perf_counter_scope(const struct bt_ctf_event *event,
270 const struct bt_definition *scope,
271 struct processtop *proc,
272 struct cputime *cpu)
273 {
274 struct bt_definition const * const *list = NULL;
275 const struct bt_definition *field;
276 unsigned int count;
277 struct perfcounter *perfcounter;
278 GHashTableIter iter;
279 gpointer key;
280 int ret;
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
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);
297 if (bt_ctf_field_get_error())
298 continue;
299 update_perf_value(proc, cpu, (char *) key, value);
300 }
301 }
302
303 end:
304 return;
305 }
306
307 void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
308 {
309 struct cputime *cpu;
310 const struct bt_definition *scope;
311
312 cpu = get_cpu(get_cpu_id(event));
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);
322 }
323
324 enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
325 void *private_data)
326 {
327 int pid, tid, ppid, vpid, vtid, vppid;
328 char *comm;
329 struct processtop *parent, *child;
330 unsigned long timestamp;
331
332 timestamp = bt_ctf_get_timestamp(call_data);
333 if (timestamp == -1ULL)
334 goto error;
335
336 pid = get_context_pid(call_data);
337 if (pid == -1ULL) {
338 goto error;
339 }
340
341 tid = get_context_tid(call_data);
342 if (tid == -1ULL) {
343 goto error;
344 }
345 ppid = get_context_ppid(call_data);
346 if (ppid == -1ULL) {
347 goto error;
348 }
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 }
361 comm = get_context_comm(call_data);
362 if (!comm) {
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);
370 if (!child)
371 goto end;
372 update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm);
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);
379 if (parent)
380 parent->pid = pid;
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
390 end:
391 return BT_CB_OK;
392
393 error:
394 return BT_CB_ERROR_STOP;
395 }
396
397 void init_lttngtop()
398 {
399 copies = g_ptr_array_new();
400 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
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
409 reset_global_counters();
410 lttngtop.nbproc = 0;
411 lttngtop.nbthreads = 0;
412 lttngtop.nbfiles = 0;
413
414 lttngtop.process_hash_table = g_hash_table_new(g_direct_hash,
415 g_direct_equal);
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
421 void usage(FILE *fp)
422 {
423 fprintf(fp, "LTTngTop %s\n\n", VERSION);
424 fprintf(fp, "Usage : lttngtop /path/to/trace\n");
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 */
431 static int parse_options(int argc, char **argv)
432 {
433 poptContext pc;
434 int opt, ret = 0;
435
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;
445 case OPT_TEXTDUMP:
446 opt_textdump = 1;
447 break;
448 case OPT_CHILD:
449 opt_textdump = 1;
450 opt_child = 1;
451 break;
452 case OPT_PID:
453 //opt_textdump = 1;
454 break;
455 default:
456 ret = -EINVAL;
457 goto end;
458 }
459 }
460
461 opt_input_path = poptGetArg(pc);
462
463 end:
464 if (pc) {
465 poptFreeContext(pc);
466 }
467 return ret;
468 }
469
470 void iter_trace(struct bt_context *bt_ctx)
471 {
472 struct bt_ctf_iter *iter;
473 struct bt_iter_pos begin_pos;
474 const struct bt_ctf_event *event;
475 int ret = 0;
476
477 begin_pos.type = BT_SEEK_BEGIN;
478 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
479
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(
526 "lttng_statedump_file_descriptor"),
527 NULL, 0, handle_statedump_file_descriptor,
528 NULL, NULL, NULL);
529 }
530
531 while ((event = bt_ctf_iter_read_event(iter, NULL)) != NULL) {
532 if (quit || reload_trace)
533 goto end_iter;
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
542 end_iter:
543 bt_ctf_iter_destroy(iter);
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 */
558 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
559 const char *format_str,
560 void (*packet_seek)(struct bt_stream_pos *pos,
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 };
568 int ret = -1;
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) {
601 close(dirfd);
602 ret = -1;
603 continue;
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) {
622 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
623 "for reading.\n", node->fts_accpath, path);
624 /* Allow to skip erroneous traces. */
625 continue;
626 }
627 g_array_append_val(trace_ids, trace_id);
628 }
629 }
630
631 g_array_free(trace_ids, TRUE);
632 return ret;
633
634 error:
635 return ret;
636 }
637
638 static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
639 int field_cnt, int *tid_check, int *pid_check,
640 int *procname_check, int *ppid_check)
641 {
642 int j;
643 struct perfcounter *global;
644 const char *name;
645
646 for (j = 0; j < field_cnt; j++) {
647 name = bt_ctf_get_decl_field_name(field_list[j]);
648 if (*tid_check == 0) {
649 if (strncmp(name, "tid", 3) == 0)
650 (*tid_check)++;
651 }
652 if (*pid_check == 0) {
653 if (strncmp(name, "pid", 3) == 0)
654 (*pid_check)++;
655 }
656 if (*ppid_check == 0) {
657 if (strncmp(name, "ppid", 4) == 0)
658 (*ppid_check)++;
659 }
660 if (*procname_check == 0) {
661 if (strncmp(name, "procname", 8) == 0)
662 (*procname_check)++;
663 }
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 }
675 }
676
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.
689 */
690 int 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);
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);
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);
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
739 return ret;
740 }
741
742 ssize_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 */
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 }
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 }
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 }
783 sem_post(&metadata_available);
784 }
785
786 end:
787 return 0;
788 }
789
790 int 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
798 int on_recv_fd(struct lttng_consumer_stream *kconsumerd_fd)
799 {
800 int ret;
801 struct mmap_stream *new_mmap_stream;
802
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) {
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);
819
820 g_ptr_array_add(lttng_consumer_stream_array, kconsumerd_fd);
821 /* keep mmap FDs internally */
822 ret = 1;
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
829 reload_trace = 1;
830
831 end:
832 return ret;
833 }
834
835 void live_consume(struct bt_context **bt_ctx)
836 {
837 int ret;
838 FILE *metadata_fp;
839
840 sem_wait(&metadata_available);
841 if (access("/tmp/livesession/kernel/metadata", F_OK) != 0) {
842 fprintf(stderr,"no metadata\n");
843 goto end;
844 }
845 metadata_fp = fopen("/tmp/livesession/kernel/metadata", "r");
846
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;
853 }
854
855 end:
856 return;
857 }
858
859 int 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) {
877 perror("pthread_create receive fd");
878 goto end;
879 }
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) {
884 perror("pthread_create poll fd");
885 goto end;
886 }
887
888 end:
889 return ret;
890 }
891
892 int setup_live_tracing()
893 {
894 struct lttng_domain dom;
895 struct lttng_channel chan;
896 char *channel_name = "mmapchan";
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;
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");
911 goto error;
912 }
913
914 available_snapshots = g_ptr_array_new();
915
916 /* setup the session */
917 dom.type = LTTNG_DOMAIN_KERNEL;
918
919 ret = unlink("/tmp/livesession");
920
921 lttng_destroy_session("test");
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));
925 goto error;
926 }
927
928 if ((handle = lttng_create_handle("test", &dom)) == NULL) {
929 fprintf(stderr,"error creating handle\n");
930 goto error_session;
931 }
932
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
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));
946 goto error_session;
947 }
948
949 strcpy(chan.name, channel_name);
950 chan.attr.overwrite = 0;
951 if (opt_pid && opt_textdump) {
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 }
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) {
963 fprintf(stderr,"error creating channel : %s\n",
964 helper_lttcomm_get_readable_code(ret));
965 goto error_session;
966 }
967
968 memset(&ev, '\0', sizeof(struct lttng_event));
969 //sprintf(ev.name, "sched_switch");
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 }
977 // }
978
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));
983 goto error_session;
984 }
985
986 kctxpid.ctx = LTTNG_EVENT_CONTEXT_PID;
987 lttng_add_context(handle, &kctxpid, NULL, NULL);
988 kctxtid.ctx = LTTNG_EVENT_CONTEXT_TID;
989 lttng_add_context(handle, &kctxtid, NULL, NULL);
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 // }
1000
1001 if ((ret = lttng_start_tracing("test")) < 0) {
1002 fprintf(stderr,"error starting tracing : %s\n",
1003 helper_lttcomm_get_readable_code(ret));
1004 goto error_session;
1005 }
1006
1007 helper_kernctl_buffer_flush(consumerd_metadata);
1008
1009 /* block until metadata is ready */
1010 sem_init(&metadata_available, 0, 0);
1011
1012 return 0;
1013
1014 error_session:
1015 lttng_destroy_session("test");
1016 error:
1017 return -1;
1018 }
1019
1020 int main(int argc, char **argv)
1021 {
1022 int ret;
1023 struct bt_context *bt_ctx = NULL;
1024 struct mmap_stream *mmap_info;
1025 unsigned long mmap_len;
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
1036 if (!opt_input_path) {
1037 ret = setup_live_tracing();
1038 if (ret < 0) {
1039 goto end;
1040 }
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 }
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) {
1067 bt_list_del(&mmap_info->list);
1068 }
1069 }
1070 sem_post(&metadata_available);
1071 }
1072
1073 pthread_join(timer_thread, NULL);
1074 quit = 1;
1075 pthread_join(display_thread, NULL);
1076
1077 lttng_stop_tracing("test");
1078 lttng_destroy_session("test");
1079
1080 goto end;
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 }
1090
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);
1098
1099 iter_trace(bt_ctx);
1100
1101 pthread_join(display_thread, NULL);
1102 quit = 1;
1103 pthread_join(timer_thread, NULL);
1104 }
1105
1106 end:
1107 if (bt_ctx)
1108 bt_context_put(bt_ctx);
1109
1110 return 0;
1111 }
This page took 0.049954 seconds and 5 git commands to generate.