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