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