pretty-printing cleanup
[lttngtop.git] / src / lttngtop.c
CommitLineData
1fc22eb4 1/*
863dc485 2 * Copyright (C) 2013 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>
b1acd2b3 42#ifdef LTTNGTOP_MMAP_LIVE
16b22a0f 43#include <lttng/lttngtop-helper.h>
c78f2cdc 44#include <babeltrace/lttngtopmmappacketseek.h>
b1acd2b3
JD
45#include "mmap-live.h"
46#endif /* LTTNGTOP_MMAP_LIVE */
1fc22eb4
JD
47
48#include "lttngtoptypes.h"
49#include "cputop.h"
50#include "iostreamtop.h"
51#include "cursesdisplay.h"
52#include "common.h"
b1acd2b3
JD
53#include "network-live.h"
54
12a91e9d
JD
55#define NET_URL_PREFIX "net://"
56#define NET4_URL_PREFIX "net4://"
57#define NET6_URL_PREFIX "net6://"
1fc22eb4
JD
58
59#define DEFAULT_FILE_ARRAY_SIZE 1
60
61const char *opt_input_path;
12a91e9d
JD
62int opt_textdump;
63int opt_child;
64int opt_begin;
1fc22eb4 65
ae9e85c7
JD
66int quit = 0;
67
1fc22eb4
JD
68struct lttngtop *copy;
69pthread_t display_thread;
70pthread_t timer_thread;
71
72unsigned long refresh_display = 1 * NSEC_PER_SEC;
73unsigned long last_display_update = 0;
863dc485 74unsigned long last_event_ts = 0;
1fc22eb4 75
16b22a0f 76/* list of FDs available for being read with snapshots */
b1acd2b3 77struct bt_mmap_stream_list mmap_list;
16b22a0f
JD
78GPtrArray *lttng_consumer_stream_array;
79int sessiond_metadata, consumerd_metadata;
4e6aeb3d
JD
80struct lttng_consumer_local_data *ctx = NULL;
81/* list of snapshots currently not consumed */
82GPtrArray *available_snapshots;
83sem_t metadata_available;
050f9cf9 84int reload_trace = 0;
16b22a0f 85
010f485a
JD
86uint64_t prev_ts = 0;
87
1fc22eb4
JD
88enum {
89 OPT_NONE = 0,
90 OPT_HELP,
6777529d 91 OPT_TEXTDUMP,
32b70e07
JD
92 OPT_PID,
93 OPT_CHILD,
c8d75a13 94 OPT_HOSTNAME,
b1acd2b3 95 OPT_RELAY_HOSTNAME,
f16e36fc 96 OPT_KPROBES,
3160c7a9 97 OPT_BEGIN,
1fc22eb4
JD
98};
99
100static struct poptOption long_options[] = {
101 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
102 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
6777529d 103 { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL },
32b70e07 104 { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL },
3160c7a9 105 { "begin", 'b', POPT_ARG_NONE, NULL, OPT_BEGIN, NULL, NULL },
57bff788 106 { "pid", 'p', POPT_ARG_STRING, &opt_tid, OPT_PID, NULL, NULL },
c8d75a13 107 { "hostname", 'n', POPT_ARG_STRING, &opt_hostname, OPT_HOSTNAME, NULL, NULL },
b1acd2b3
JD
108 { "relay-hostname", 'r', POPT_ARG_STRING, &opt_relay_hostname,
109 OPT_RELAY_HOSTNAME, NULL, NULL },
f16e36fc 110 { "kprobes", 'k', POPT_ARG_STRING, &opt_kprobes, OPT_KPROBES, NULL, NULL },
1fc22eb4
JD
111 { NULL, 0, 0, NULL, 0, NULL, NULL },
112};
113
863dc485 114#ifdef LTTNGTOP_MMAP_LIVE
24797198
JD
115static void handle_textdump_sigterm(int signal)
116{
117 quit = 1;
118 lttng_destroy_session("test");
119}
863dc485 120#endif
24797198 121
1fc22eb4
JD
122void *refresh_thread(void *p)
123{
124 while (1) {
ae9e85c7
JD
125 if (quit) {
126 sem_post(&pause_sem);
127 sem_post(&timer);
1402044a 128 sem_post(&end_trace_sem);
ae9e85c7 129 sem_post(&goodtodisplay);
1402044a 130 sem_post(&goodtoupdate);
ae9e85c7
JD
131 pthread_exit(0);
132 }
1402044a 133 if (!opt_input_path) {
b1acd2b3
JD
134#ifdef LTTNGTOP_MMAP_LIVE
135 mmap_live_flush(mmap_list);
136#endif
1402044a 137 }
1fc22eb4
JD
138 sem_wait(&pause_sem);
139 sem_post(&pause_sem);
140 sem_post(&timer);
141 sleep(refresh_display/NSEC_PER_SEC);
142 }
143}
144
145void *ncurses_display(void *p)
146{
147 unsigned int current_display_index = 0;
148
149 sem_wait(&bootstrap);
b332d28f
JD
150 /*
151 * Prevent the 1 second delay when we hit ESC
152 */
153 ESCDELAY = 0;
1fc22eb4
JD
154 init_ncurses();
155
156 while (1) {
157 sem_wait(&timer);
158 sem_wait(&goodtodisplay);
159 sem_wait(&pause_sem);
160
ae9e85c7 161 if (quit) {
33572a17
JD
162 sem_post(&pause_sem);
163 sem_post(&timer);
ae9e85c7
JD
164 reset_ncurses();
165 pthread_exit(0);
166 }
167
1fc22eb4 168 copy = g_ptr_array_index(copies, current_display_index);
85db4618
JD
169 assert(copy);
170 display(current_display_index++);
1fc22eb4
JD
171
172 sem_post(&goodtoupdate);
173 sem_post(&pause_sem);
1fc22eb4
JD
174 }
175}
176
575cc4fd
JD
177void print_fields(struct bt_ctf_event *event)
178{
179 unsigned int cnt, i;
b1acd2b3
JD
180 const struct bt_definition *const * list;
181 const struct bt_declaration *l;
182 const struct bt_definition *scope;
575cc4fd
JD
183 enum ctf_type_id type;
184 const char *str;
185
186 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_FIELDS);
187
188 bt_ctf_get_field_list(event, scope, &list, &cnt);
189 for (i = 0; i < cnt; i++) {
190 if (i != 0)
191 printf(", ");
192 printf("%s = ", bt_ctf_field_name(list[i]));
9498e78c
JD
193 l = bt_ctf_get_decl_from_def(list[i]);
194 type = bt_ctf_field_type(l);
575cc4fd 195 if (type == CTF_TYPE_INTEGER) {
9498e78c 196 if (bt_ctf_get_int_signedness(l) == 0)
575cc4fd
JD
197 printf("%" PRIu64 "", bt_ctf_get_uint64(list[i]));
198 else
199 printf("%" PRId64 "", bt_ctf_get_int64(list[i]));
200 } else if (type == CTF_TYPE_STRING) {
201 printf("%s", bt_ctf_get_string(list[i]));
202 } else if (type == CTF_TYPE_ARRAY) {
203 str = bt_ctf_get_char_array(list[i]);
fbbda4da 204 if (!bt_ctf_field_get_error() && str)
575cc4fd
JD
205 printf("%s", str);
206 }
207 }
208}
209
6112d0d4
JD
210/*
211 * hook on each event to check the timestamp and refresh the display if
212 * necessary
213 */
214enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data)
215{
216 unsigned long timestamp;
010f485a 217 uint64_t delta;
6112d0d4 218 struct tm start;
b520ab45 219 uint64_t ts_nsec_start;
b0135e60 220 int pid, cpu_id, tid;
b1acd2b3 221 const struct bt_definition *scope;
575cc4fd 222 const char *hostname, *procname;
6112d0d4 223
6112d0d4
JD
224 timestamp = bt_ctf_get_timestamp(call_data);
225
863dc485
JD
226 /* can happen in network live when tracing is idle */
227 if (timestamp < last_event_ts)
228 goto end_stop;
229
230 last_event_ts = timestamp;
231
b520ab45 232 start = format_timestamp(timestamp);
6112d0d4
JD
233 ts_nsec_start = timestamp % NSEC_PER_SEC;
234
32b70e07 235 pid = get_context_pid(call_data);
57bff788 236 if (pid == -1ULL && opt_tid) {
32b70e07
JD
237 goto error;
238 }
b0135e60
JD
239
240 tid = get_context_tid(call_data);
32b70e07 241
c8d75a13 242 hostname = get_context_hostname(call_data);
b301de1a
JD
243 if (opt_tid || opt_hostname) {
244 if (!lookup_filter_tid_list(pid)) {
245 /* To display when a process of ours in getting scheduled in */
246 if (strcmp(bt_ctf_event_name(call_data), "sched_switch") == 0) {
247 int next_tid;
248
249 scope = bt_ctf_get_top_level_scope(call_data,
250 BT_EVENT_FIELDS);
251 next_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
252 scope, "_next_tid"));
253 if (bt_ctf_field_get_error()) {
254 fprintf(stderr, "Missing next_tid field\n");
255 goto error;
256 }
257 if (!lookup_filter_tid_list(next_tid)) {
258 goto end;
259 }
260 } else {
261 goto end;
262 }
263 }
264 }
da4353bb 265
575cc4fd
JD
266 cpu_id = get_cpu_id(call_data);
267 procname = get_context_comm(call_data);
010f485a
JD
268 if (prev_ts == 0)
269 prev_ts = timestamp;
270 delta = timestamp - prev_ts;
271 prev_ts = timestamp;
575cc4fd 272
4251cc67
JD
273 printf("%02d:%02d:%02d.%09" PRIu64 " (+%" PRIu64 ".%09" PRIu64 ") %s%s"
274 "(cpu %d) [%s (%d/%d)] %s (",
275 start.tm_hour, start.tm_min, start.tm_sec,
276 ts_nsec_start, delta / NSEC_PER_SEC,
277 delta % NSEC_PER_SEC, (hostname) ? hostname : "",
278 (hostname) ? " ": "", cpu_id, procname, pid, tid,
279 bt_ctf_event_name(call_data));
7c15d14a
JD
280 print_fields(call_data);
281 printf(")\n");
6112d0d4 282
32b70e07 283end:
6112d0d4 284 return BT_CB_OK;
32b70e07
JD
285error:
286 return BT_CB_ERROR_STOP;
863dc485
JD
287end_stop:
288 return BT_CB_OK_STOP;
6112d0d4
JD
289}
290
246d5992
JD
291enum bt_cb_ret handle_kprobes(struct bt_ctf_event *call_data, void *private_data)
292{
293 int i;
294 struct kprobes *kprobe;
295
296 /* for kprobes */
297 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
298 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
299 if (strcmp(bt_ctf_event_name(call_data), kprobe->probe_name) == 0) {
300 kprobe->count++;
301 }
302 }
303
304 return BT_CB_OK;
305}
306
1fc22eb4
JD
307/*
308 * hook on each event to check the timestamp and refresh the display if
309 * necessary
310 */
4adc8274 311enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data)
1fc22eb4
JD
312{
313 unsigned long timestamp;
314
c78f2cdc 315 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
316 if (timestamp == -1ULL)
317 goto error;
318
863dc485
JD
319 /* can happen in network live when tracing is idle */
320 if (timestamp < last_event_ts)
321 goto end_stop;
322
323 last_event_ts = timestamp;
324
1fc22eb4
JD
325 if (last_display_update == 0)
326 last_display_update = timestamp;
327
328 if (timestamp - last_display_update >= refresh_display) {
329 sem_wait(&goodtoupdate);
330 g_ptr_array_add(copies, get_copy_lttngtop(last_display_update,
331 timestamp));
332 sem_post(&goodtodisplay);
333 sem_post(&bootstrap);
334 last_display_update = timestamp;
335 }
336 return BT_CB_OK;
337
338error:
339 fprintf(stderr, "check_timestamp callback error\n");
340 return BT_CB_ERROR_STOP;
863dc485
JD
341
342end_stop:
343 return BT_CB_OK_STOP;
1fc22eb4
JD
344}
345
346/*
347 * get_perf_counter : get or create and return a perf_counter struct for
348 * either a process or a cpu (only one of the 2 parameters mandatory)
349 */
350struct perfcounter *get_perf_counter(const char *name, struct processtop *proc,
351 struct cputime *cpu)
352{
bb6d72a2 353 struct perfcounter *ret;
1fc22eb4
JD
354 GHashTable *table;
355
356 if (proc)
357 table = proc->perf;
358 else if (cpu)
359 table = cpu->perf;
360 else
361 goto error;
362
363 ret = g_hash_table_lookup(table, (gpointer) name);
364 if (ret)
365 goto end;
366
559c9f86 367 ret = g_new0(struct perfcounter, 1);
1fc22eb4
JD
368 /* by default, make it visible in the UI */
369 ret->visible = 1;
85db4618 370 g_hash_table_insert(table, (gpointer) strdup(name), ret);
1fc22eb4 371
1fc22eb4
JD
372end:
373 return ret;
374
375error:
376 return NULL;
377}
378
379void update_perf_value(struct processtop *proc, struct cputime *cpu,
380 const char *name, int value)
381{
382 struct perfcounter *cpu_perf, *process_perf;
383
384 cpu_perf = get_perf_counter(name, NULL, cpu);
385 if (cpu_perf->count < value) {
386 process_perf = get_perf_counter(name, proc, NULL);
387 process_perf->count += value - cpu_perf->count;
388 cpu_perf->count = value;
389 }
390}
391
4adc8274 392void extract_perf_counter_scope(const struct bt_ctf_event *event,
2e0a1190 393 const struct bt_definition *scope,
1fc22eb4
JD
394 struct processtop *proc,
395 struct cputime *cpu)
396{
2e0a1190
JD
397 struct bt_definition const * const *list = NULL;
398 const struct bt_definition *field;
1fc22eb4 399 unsigned int count;
bb6d72a2
JD
400 struct perfcounter *perfcounter;
401 GHashTableIter iter;
402 gpointer key;
403 int ret;
1fc22eb4
JD
404
405 if (!scope)
406 goto end;
407
408 ret = bt_ctf_get_field_list(event, scope, &list, &count);
409 if (ret < 0)
410 goto end;
411
bb6d72a2
JD
412 if (count == 0)
413 goto end;
414
415 g_hash_table_iter_init(&iter, global_perf_liszt);
416 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) {
417 field = bt_ctf_get_field(event, scope, (char *) key);
418 if (field) {
419 int value = bt_ctf_get_uint64(field);
1fc22eb4
JD
420 if (bt_ctf_field_get_error())
421 continue;
bb6d72a2 422 update_perf_value(proc, cpu, (char *) key, value);
1fc22eb4
JD
423 }
424 }
425
426end:
427 return;
428}
429
4adc8274 430void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event)
1fc22eb4 431{
1fc22eb4 432 struct cputime *cpu;
2e0a1190 433 const struct bt_definition *scope;
1fc22eb4 434
d67167cd 435 cpu = get_cpu(get_cpu_id(event));
1fc22eb4
JD
436
437 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
438 extract_perf_counter_scope(event, scope, proc, cpu);
439
440 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
441 extract_perf_counter_scope(event, scope, proc, cpu);
442
443 scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT);
444 extract_perf_counter_scope(event, scope, proc, cpu);
1fc22eb4
JD
445}
446
4adc8274 447enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data,
1fc22eb4
JD
448 void *private_data)
449{
1402044a 450 int pid, tid, ppid, vpid, vtid, vppid;
c8d75a13 451 char *comm, *hostname;
1fc22eb4 452 struct processtop *parent, *child;
1fc22eb4
JD
453 unsigned long timestamp;
454
c78f2cdc 455 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
456 if (timestamp == -1ULL)
457 goto error;
458
1dec520a
JD
459 pid = get_context_pid(call_data);
460 if (pid == -1ULL) {
1fc22eb4
JD
461 goto error;
462 }
96aa77de 463
1dec520a
JD
464 tid = get_context_tid(call_data);
465 if (tid == -1ULL) {
1fc22eb4
JD
466 goto error;
467 }
1dec520a
JD
468 ppid = get_context_ppid(call_data);
469 if (ppid == -1ULL) {
1fc22eb4
JD
470 goto error;
471 }
1402044a
JD
472 vpid = get_context_vpid(call_data);
473 if (pid == -1ULL) {
474 vpid = -1;
475 }
476 vtid = get_context_vtid(call_data);
477 if (tid == -1ULL) {
478 vtid = -1;
479 }
480 vppid = get_context_vppid(call_data);
481 if (ppid == -1ULL) {
482 vppid = -1;
483 }
1dec520a
JD
484 comm = get_context_comm(call_data);
485 if (!comm) {
1fc22eb4
JD
486 goto error;
487 }
c8d75a13
JD
488 /* optional */
489 hostname = get_context_hostname(call_data);
1fc22eb4
JD
490
491 /* find or create the current process */
492 child = find_process_tid(&lttngtop, tid, comm);
493 if (!child)
906c08f6 494 child = add_proc(&lttngtop, tid, comm, timestamp, hostname);
96aa77de
JD
495 if (!child)
496 goto end;
c8d75a13 497 update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm, hostname);
1fc22eb4
JD
498
499 if (pid != tid) {
500 /* find or create the parent */
501 parent = find_process_tid(&lttngtop, pid, comm);
502 if (!parent) {
906c08f6 503 parent = add_proc(&lttngtop, pid, comm, timestamp, hostname);
96aa77de
JD
504 if (parent)
505 parent->pid = pid;
1fc22eb4
JD
506 }
507
508 /* attach the parent to the current process */
509 child->threadparent = parent;
510 add_thread(parent, child);
511 }
512
513 update_perf_counter(child, call_data);
514
96aa77de 515end:
1fc22eb4
JD
516 return BT_CB_OK;
517
518error:
519 return BT_CB_ERROR_STOP;
520}
521
522void init_lttngtop()
523{
524 copies = g_ptr_array_new();
0d91c12a 525 global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal);
da4353bb 526 global_filter_list = g_hash_table_new(g_str_hash, g_str_equal);
114cae59 527 global_host_list = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
528
529 sem_init(&goodtodisplay, 0, 0);
530 sem_init(&goodtoupdate, 0, 1);
531 sem_init(&timer, 0, 1);
532 sem_init(&bootstrap, 0, 0);
533 sem_init(&pause_sem, 0, 1);
534 sem_init(&end_trace_sem, 0, 0);
535
e05a35a6
JD
536 reset_global_counters();
537 lttngtop.nbproc = 0;
538 lttngtop.nbthreads = 0;
539 lttngtop.nbfiles = 0;
540
30b646c4
JD
541 lttngtop.process_hash_table = g_hash_table_new(g_direct_hash,
542 g_direct_equal);
1fc22eb4
JD
543 lttngtop.process_table = g_ptr_array_new();
544 lttngtop.files_table = g_ptr_array_new();
545 lttngtop.cpu_table = g_ptr_array_new();
da4353bb
JD
546
547 toggle_filter = -1;
1fc22eb4
JD
548}
549
c263c4eb 550void usage(FILE *fp)
1fc22eb4 551{
c263c4eb 552 fprintf(fp, "LTTngTop %s\n\n", VERSION);
d2f1452b
JD
553 fprintf(fp, "Usage : lttngtop [OPTIONS] TRACE\n");
554 fprintf(fp, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n");
c8d75a13
JD
555 fprintf(fp, " -h, --help This help message\n");
556 fprintf(fp, " -t, --textdump Display live events in text-only\n");
557 fprintf(fp, " -p, --pid Comma-separated list of PIDs to display\n");
558 fprintf(fp, " -f, --child Follow threads associated with selected PIDs\n");
559 fprintf(fp, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n");
241ba90a 560 fprintf(fp, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n");
bab469fc
JD
561 fprintf(fp, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n");
562 fprintf(fp, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n");
1fc22eb4
JD
563}
564
f16e36fc
JD
565/*
566 * Parse probe options.
567 * Shamelessly stolen from lttng-tools :
568 * src/bin/lttng/commands/enable_events.c
569 */
570static struct kprobes *parse_probe_opts(char *opt)
571{
572 char s_hex[19];
573 char name[LTTNG_SYMBOL_NAME_LEN];
574 struct kprobes *kprobe;
575 int ret;
576
577 /*
578 kprobe->probe_addr = 0;
579 kprobe->probe_offset = 0;
580 asprintf(&kprobe->probe_name, "probe_sys_open");
581 asprintf(&kprobe->symbol_name, "sys_open");
582 */
583
584 if (opt == NULL) {
585 kprobe = NULL;
586 goto end;
587 }
588
589 kprobe = g_new0(struct kprobes, 1);
590
591 /* Check for symbol+offset */
592 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
593 if (ret == 2) {
b1acd2b3
JD
594 ret = asprintf(&kprobe->probe_name, "probe_%s", name);
595 ret = asprintf(&kprobe->symbol_name, "%s", name);
f16e36fc
JD
596
597 if (strlen(s_hex) == 0) {
598 fprintf(stderr, "Invalid probe offset %s", s_hex);
599 ret = -1;
600 goto end;
601 }
602 kprobe->probe_offset = strtoul(s_hex, NULL, 0);
603 kprobe->probe_addr = 0;
604 goto end;
605 }
606
607 /* Check for symbol */
608 if (isalpha(name[0])) {
609 ret = sscanf(opt, "%s", name);
610 if (ret == 1) {
b1acd2b3
JD
611 ret = asprintf(&kprobe->probe_name, "probe_%s", name);
612 ret = asprintf(&kprobe->symbol_name, "%s", name);
f16e36fc
JD
613 kprobe->probe_offset = 0;
614 kprobe->probe_addr = 0;
615 goto end;
616 }
617 }
618
619 /* Check for address */
620 ret = sscanf(opt, "%s", s_hex);
621 if (ret > 0) {
622 if (strlen(s_hex) == 0) {
623 fprintf(stderr, "Invalid probe address %s", s_hex);
624 ret = -1;
625 goto end;
626 }
b1acd2b3 627 ret = asprintf(&kprobe->probe_name, "probe_%s", s_hex);
f16e36fc
JD
628 kprobe->probe_offset = 0;
629 kprobe->probe_addr = strtoul(s_hex, NULL, 0);
630 goto end;
631 }
632
633 /* No match */
634 kprobe = NULL;
635
636end:
637 return kprobe;
638}
639
1fc22eb4
JD
640/*
641 * Return 0 if caller should continue, < 0 if caller should return
642 * error, > 0 if caller should exit without reporting error.
643 */
644static int parse_options(int argc, char **argv)
645{
646 poptContext pc;
647 int opt, ret = 0;
57bff788
JD
648 char *tmp_str;
649 int *tid;
1fc22eb4 650
b1acd2b3
JD
651 remote_live = 0;
652
1fc22eb4
JD
653 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
654 poptReadDefaultConfig(pc, 0);
655
656 while ((opt = poptGetNextOpt(pc)) != -1) {
657 switch (opt) {
658 case OPT_HELP:
659 usage(stdout);
660 ret = 1; /* exit cleanly */
661 goto end;
6777529d
JD
662 case OPT_TEXTDUMP:
663 opt_textdump = 1;
32b70e07
JD
664 break;
665 case OPT_CHILD:
32b70e07
JD
666 opt_child = 1;
667 break;
668 case OPT_PID:
da4353bb 669 toggle_filter = 1;
ea5d1dc9 670 tid_filter_list = g_hash_table_new(g_str_hash,
c8d75a13 671 g_str_equal);
57bff788
JD
672 tmp_str = strtok(opt_tid, ",");
673 while (tmp_str) {
674 tid = malloc(sizeof(int));
675 *tid = atoi(tmp_str);
ea5d1dc9 676 g_hash_table_insert(tid_filter_list,
c8d75a13
JD
677 (gpointer) tid, tid);
678 tmp_str = strtok(NULL, ",");
679 }
680 break;
3160c7a9
JD
681 case OPT_BEGIN:
682 /* start reading the live trace from the beginning */
683 opt_begin = 1;
684 break;
c8d75a13 685 case OPT_HOSTNAME:
da4353bb 686 toggle_filter = 1;
c8d75a13
JD
687 tmp_str = strtok(opt_hostname, ",");
688 while (tmp_str) {
467097ac 689 add_hostname_list(tmp_str, 1);
57bff788
JD
690 tmp_str = strtok(NULL, ",");
691 }
32b70e07 692 break;
b1acd2b3
JD
693 case OPT_RELAY_HOSTNAME:
694 remote_live = 1;
695 break;
f16e36fc
JD
696 case OPT_KPROBES:
697 lttngtop.kprobes_table = g_ptr_array_new();
698 tmp_str = strtok(opt_kprobes, ",");
699 while (tmp_str) {
700 struct kprobes *kprobe;
701
702 kprobe = parse_probe_opts(tmp_str);
703 if (kprobe) {
704 g_ptr_array_add(
705 lttngtop.kprobes_table,
706 kprobe);
707 } else {
708 ret = -EINVAL;
709 goto end;
710 }
711 tmp_str = strtok(NULL, ",");
712 }
713 break;
1fc22eb4
JD
714 default:
715 ret = -EINVAL;
716 goto end;
717 }
718 }
719
720 opt_input_path = poptGetArg(pc);
16b22a0f 721
1fc22eb4
JD
722end:
723 if (pc) {
724 poptFreeContext(pc);
725 }
726 return ret;
727}
728
729void iter_trace(struct bt_context *bt_ctx)
730{
731 struct bt_ctf_iter *iter;
732 struct bt_iter_pos begin_pos;
246d5992 733 struct kprobes *kprobe;
4adc8274 734 const struct bt_ctf_event *event;
246d5992 735 int i;
1fc22eb4
JD
736 int ret = 0;
737
738 begin_pos.type = BT_SEEK_BEGIN;
739 iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL);
740
da4353bb
JD
741 /* at each event, verify the status of the process table */
742 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
743 fix_process_table,
744 NULL, NULL, NULL);
6e11e0d0
JD
745 /* to handle the follow child option */
746 bt_ctf_iter_add_callback(iter,
747 g_quark_from_static_string("sched_process_fork"),
748 NULL, 0, handle_sched_process_fork, NULL, NULL, NULL);
6777529d
JD
749 if (opt_textdump) {
750 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
751 print_timestamp,
752 NULL, NULL, NULL);
753 } else {
754 /* at each event check if we need to refresh */
755 bt_ctf_iter_add_callback(iter, 0, NULL, 0,
756 check_timestamp,
757 NULL, NULL, NULL);
6777529d
JD
758 /* to handle the scheduling events */
759 bt_ctf_iter_add_callback(iter,
760 g_quark_from_static_string("sched_switch"),
761 NULL, 0, handle_sched_switch, NULL, NULL, NULL);
762 /* to clean up the process table */
763 bt_ctf_iter_add_callback(iter,
764 g_quark_from_static_string("sched_process_free"),
765 NULL, 0, handle_sched_process_free, NULL, NULL, NULL);
766 /* to get all the process from the statedumps */
767 bt_ctf_iter_add_callback(iter,
768 g_quark_from_static_string(
769 "lttng_statedump_process_state"),
770 NULL, 0, handle_statedump_process_state,
771 NULL, NULL, NULL);
772
773 /* for IO top */
774 bt_ctf_iter_add_callback(iter,
775 g_quark_from_static_string("exit_syscall"),
776 NULL, 0, handle_exit_syscall, NULL, NULL, NULL);
777 bt_ctf_iter_add_callback(iter,
778 g_quark_from_static_string("sys_write"),
779 NULL, 0, handle_sys_write, NULL, NULL, NULL);
780 bt_ctf_iter_add_callback(iter,
781 g_quark_from_static_string("sys_read"),
782 NULL, 0, handle_sys_read, NULL, NULL, NULL);
783 bt_ctf_iter_add_callback(iter,
784 g_quark_from_static_string("sys_open"),
785 NULL, 0, handle_sys_open, NULL, NULL, NULL);
786 bt_ctf_iter_add_callback(iter,
787 g_quark_from_static_string("sys_close"),
788 NULL, 0, handle_sys_close, NULL, NULL, NULL);
789 bt_ctf_iter_add_callback(iter,
790 g_quark_from_static_string(
ceb3a221 791 "lttng_statedump_file_descriptor"),
6777529d
JD
792 NULL, 0, handle_statedump_file_descriptor,
793 NULL, NULL, NULL);
246d5992
JD
794
795 /* for kprobes */
da4353bb
JD
796 if (lttngtop.kprobes_table) {
797 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
798 kprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
799 bt_ctf_iter_add_callback(iter,
800 g_quark_from_static_string(
801 kprobe->probe_name),
802 NULL, 0, handle_kprobes,
803 NULL, NULL, NULL);
804 }
246d5992 805 }
6777529d
JD
806 }
807
fbbda4da 808 while ((event = bt_ctf_iter_read_event(iter)) != NULL) {
050f9cf9 809 if (quit || reload_trace)
ae9e85c7 810 goto end_iter;
1fc22eb4
JD
811 ret = bt_iter_next(bt_ctf_get_iter(iter));
812 if (ret < 0)
813 goto end_iter;
814 }
815
816 /* block until quit, we reached the end of the trace */
817 sem_wait(&end_trace_sem);
818
819end_iter:
06570214 820 bt_ctf_iter_destroy(iter);
1fc22eb4
JD
821}
822
823/*
824 * bt_context_add_traces_recursive: Open a trace recursively
825 * (copied from BSD code in converter/babeltrace.c)
826 *
827 * Find each trace present in the subdirectory starting from the given
828 * path, and add them to the context. The packet_seek parameter can be
829 * NULL: this specify to use the default format packet_seek.
830 *
831 * Return: 0 on success, nonzero on failure.
832 * Unable to open toplevel: failure.
833 * Unable to open some subdirectory or file: warn and continue;
834 */
835int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
836 const char *format_str,
2e0a1190 837 void (*packet_seek)(struct bt_stream_pos *pos,
1fc22eb4
JD
838 size_t offset, int whence))
839{
840 FTS *tree;
841 FTSENT *node;
842 GArray *trace_ids;
843 char lpath[PATH_MAX];
844 char * const paths[2] = { lpath, NULL };
0d567cf1 845 int ret = -1;
1fc22eb4 846
12a91e9d
JD
847 if ((strncmp(path, NET4_URL_PREFIX, sizeof(NET4_URL_PREFIX) - 1)) == 0 ||
848 (strncmp(path, NET6_URL_PREFIX, sizeof(NET6_URL_PREFIX) - 1)) == 0 ||
849 (strncmp(path, NET_URL_PREFIX, sizeof(NET_URL_PREFIX) - 1)) == 0) {
850 ret = bt_context_add_trace(ctx,
851 path, format_str, packet_seek, NULL, NULL);
852 if (ret < 0) {
853 fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" "
854 "for reading.\n", path);
855 /* Allow to skip erroneous traces. */
856 ret = 1; /* partial error */
857 }
858 return ret;
859 }
1fc22eb4
JD
860 /*
861 * Need to copy path, because fts_open can change it.
862 * It is the pointer array, not the strings, that are constant.
863 */
864 strncpy(lpath, path, PATH_MAX);
865 lpath[PATH_MAX - 1] = '\0';
866
867 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
868 if (tree == NULL) {
869 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
870 path);
871 return -EINVAL;
872 }
873
874 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
875
876 while ((node = fts_read(tree))) {
877 int dirfd, metafd;
878
879 if (!(node->fts_info & FTS_D))
880 continue;
881
882 dirfd = open(node->fts_accpath, 0);
883 if (dirfd < 0) {
884 fprintf(stderr, "[error] [Context] Unable to open trace "
885 "directory file descriptor.\n");
886 ret = dirfd;
887 goto error;
888 }
889 metafd = openat(dirfd, "metadata", O_RDONLY);
890 if (metafd < 0) {
7314c855 891 close(dirfd);
7314c855 892 continue;
1fc22eb4
JD
893 } else {
894 int trace_id;
895
896 ret = close(metafd);
897 if (ret < 0) {
898 perror("close");
899 goto error;
900 }
901 ret = close(dirfd);
902 if (ret < 0) {
903 perror("close");
904 goto error;
905 }
906
907 trace_id = bt_context_add_trace(ctx,
908 node->fts_accpath, format_str,
909 packet_seek, NULL, NULL);
910 if (trace_id < 0) {
0d567cf1 911 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
1fc22eb4 912 "for reading.\n", node->fts_accpath, path);
0d567cf1
JD
913 /* Allow to skip erroneous traces. */
914 continue;
1fc22eb4
JD
915 }
916 g_array_append_val(trace_ids, trace_id);
917 }
918 }
919
920 g_array_free(trace_ids, TRUE);
0d567cf1 921 return ret;
1fc22eb4
JD
922
923error:
924 return ret;
925}
926
4553b4d1 927static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list,
a54d0d2c
JD
928 int field_cnt, int *tid_check, int *pid_check,
929 int *procname_check, int *ppid_check)
930{
931 int j;
bb6d72a2
JD
932 struct perfcounter *global;
933 const char *name;
a54d0d2c
JD
934
935 for (j = 0; j < field_cnt; j++) {
bb6d72a2 936 name = bt_ctf_get_decl_field_name(field_list[j]);
a54d0d2c 937 if (*tid_check == 0) {
bb6d72a2 938 if (strncmp(name, "tid", 3) == 0)
a54d0d2c 939 (*tid_check)++;
a54d0d2c
JD
940 }
941 if (*pid_check == 0) {
da401e9c 942 if (strncmp(name, "pid", 3) == 0)
a54d0d2c
JD
943 (*pid_check)++;
944 }
945 if (*ppid_check == 0) {
bb6d72a2 946 if (strncmp(name, "ppid", 4) == 0)
a54d0d2c
JD
947 (*ppid_check)++;
948 }
949 if (*procname_check == 0) {
bb6d72a2 950 if (strncmp(name, "procname", 8) == 0)
a54d0d2c
JD
951 (*procname_check)++;
952 }
bb6d72a2
JD
953 if (strncmp(name, "perf_", 5) == 0) {
954 global = g_hash_table_lookup(global_perf_liszt, (gpointer) name);
955 if (!global) {
956 global = g_new0(struct perfcounter, 1);
957 /* by default, sort on the first perf context */
958 if (g_hash_table_size(global_perf_liszt) == 0)
959 global->sort = 1;
960 global->visible = 1;
961 g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global);
962 }
963 }
a54d0d2c 964 }
bb6d72a2 965
a54d0d2c
JD
966 if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 &&
967 *procname_check == 1)
968 return 0;
969
970 return -1;
971}
972
973/*
974 * check_requirements: check if the required context informations are available
975 *
976 * If each mandatory context information is available for at least in one
977 * event, return 0 otherwise return -1.
a54d0d2c
JD
978 */
979int check_requirements(struct bt_context *ctx)
980{
981 unsigned int i, evt_cnt, field_cnt;
982 struct bt_ctf_event_decl *const * evt_list;
983 const struct bt_ctf_field_decl *const * field_list;
984 int tid_check = 0;
985 int pid_check = 0;
986 int procname_check = 0;
987 int ppid_check = 0;
988 int ret = 0;
989
b1acd2b3
JD
990 ret = bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt);
991 if (ret < 0) {
992 goto end;
993 }
994
a54d0d2c
JD
995 for (i = 0; i < evt_cnt; i++) {
996 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT,
997 &field_list, &field_cnt);
998 ret = check_field_requirements(field_list, field_cnt,
999 &tid_check, &pid_check, &procname_check,
1000 &ppid_check);
a54d0d2c
JD
1001
1002 bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT,
1003 &field_list, &field_cnt);
1004 ret = check_field_requirements(field_list, field_cnt,
1005 &tid_check, &pid_check, &procname_check,
1006 &ppid_check);
a54d0d2c
JD
1007
1008 bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT,
1009 &field_list, &field_cnt);
1010 ret = check_field_requirements(field_list, field_cnt,
1011 &tid_check, &pid_check, &procname_check,
1012 &ppid_check);
a54d0d2c
JD
1013 }
1014
1015 if (tid_check == 0) {
1016 ret = -1;
1017 fprintf(stderr, "[error] missing tid context information\n");
1018 }
1019 if (pid_check == 0) {
1020 ret = -1;
1021 fprintf(stderr, "[error] missing pid context information\n");
1022 }
1023 if (ppid_check == 0) {
1024 ret = -1;
1025 fprintf(stderr, "[error] missing ppid context information\n");
1026 }
1027 if (procname_check == 0) {
1028 ret = -1;
1029 fprintf(stderr, "[error] missing procname context information\n");
1030 }
1031
16b22a0f
JD
1032end:
1033 return ret;
1034}
1035
1fc22eb4
JD
1036int main(int argc, char **argv)
1037{
1038 int ret;
1039 struct bt_context *bt_ctx = NULL;
1040
957eed46 1041 init_lttngtop();
1fc22eb4
JD
1042 ret = parse_options(argc, argv);
1043 if (ret < 0) {
1044 fprintf(stdout, "Error parsing options.\n\n");
1045 usage(stdout);
1046 exit(EXIT_FAILURE);
1047 } else if (ret > 0) {
1048 exit(EXIT_SUCCESS);
1049 }
1050
863dc485
JD
1051 if (!opt_input_path && !remote_live) {
1052 /* mmap live */
1053#ifdef LTTNGTOP_MMAP_LIVE
24797198
JD
1054 if (opt_textdump) {
1055 signal(SIGTERM, handle_textdump_sigterm);
1056 signal(SIGINT, handle_textdump_sigterm);
1057 }
b1acd2b3 1058 mmap_live_loop(bt_ctx);
ae9e85c7 1059 pthread_join(timer_thread, NULL);
eb677852
JD
1060 quit = 1;
1061 pthread_join(display_thread, NULL);
6777529d 1062
c78f2cdc 1063 lttng_stop_tracing("test");
c78f2cdc
JD
1064 lttng_destroy_session("test");
1065
1fc22eb4 1066 goto end;
863dc485 1067#else
d2f1452b
JD
1068 fprintf(stderr, "[ERROR] Mmap live support not compiled, specify a "
1069 "trace directory or -r <relayd hostname/IP>\n");
1070 usage(stdout);
1071 ret = -1;
863dc485
JD
1072 goto end;
1073#endif /* LTTNGTOP_MMAP_LIVE */
1074 } else if (!opt_input_path && remote_live) {
1075 /* network live */
12a91e9d 1076#if 0
3160c7a9 1077 ret = setup_network_live(opt_relay_hostname, opt_begin);
863dc485
JD
1078 if (ret < 0) {
1079 goto end;
1080 }
1081
1082 ret = open_trace(&bt_ctx);
1083 if (ret < 0) {
1084 goto end;
1085 }
12a91e9d
JD
1086#endif
1087
1088 bt_ctx = bt_context_create();
1089 ret = bt_context_add_traces_recursive(bt_ctx, opt_relay_hostname,
1090 "lttng-live", NULL);
1091 if (ret < 0) {
1092 fprintf(stderr, "[error] Opening the trace\n");
1093 goto end;
1094 }
16b22a0f 1095 } else {
957eed46 1096 //init_lttngtop();
16b22a0f
JD
1097
1098 bt_ctx = bt_context_create();
1099 ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL);
1100 if (ret < 0) {
1101 fprintf(stderr, "[error] Opening the trace\n");
1102 goto end;
1103 }
1fc22eb4 1104
12a91e9d
JD
1105 ret = check_requirements(bt_ctx);
1106 if (ret < 0) {
1107 fprintf(stderr, "[error] some mandatory contexts "
1108 "were missing, exiting.\n");
1109 goto end;
1110 }
1111
1112 if (!opt_textdump) {
1113 pthread_create(&display_thread, NULL, ncurses_display,
1114 (void *) NULL);
1115 pthread_create(&timer_thread, NULL, refresh_thread,
1116 (void *) NULL);
1117 }
1118
1119 iter_trace(bt_ctx);
863dc485 1120 }
1fc22eb4 1121
1fc22eb4 1122
863dc485
JD
1123 pthread_join(display_thread, NULL);
1124 quit = 1;
1125 pthread_join(timer_thread, NULL);
1fc22eb4 1126
d2f1452b
JD
1127 ret = 0;
1128
1fc22eb4 1129end:
16b22a0f
JD
1130 if (bt_ctx)
1131 bt_context_put(bt_ctx);
1132
d2f1452b 1133 return ret;
1fc22eb4 1134}
This page took 0.081943 seconds and 4 git commands to generate.