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