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