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