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