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