Commit | Line | Data |
---|---|---|
1fc22eb4 | 1 | /* |
aa15ac1c | 2 | * Copyright (C) 2011-2012 Julien Desfossez |
1fc22eb4 JD |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License Version 2 as | |
6 | * published by the Free Software Foundation; | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
71bd7ce1 AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
1fc22eb4 JD |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <config.h> | |
20 | #include <stdio.h> | |
21 | #include <stdint.h> | |
22 | #include <babeltrace/babeltrace.h> | |
23 | #include <babeltrace/ctf/events.h> | |
24 | #include <babeltrace/ctf/callbacks.h> | |
3ba84bed | 25 | #include <babeltrace/ctf/iterator.h> |
1fc22eb4 JD |
26 | #include <fcntl.h> |
27 | #include <pthread.h> | |
28 | #include <popt.h> | |
29 | #include <stdlib.h> | |
30 | #include <ftw.h> | |
31 | #include <dirent.h> | |
32 | #include <ctype.h> | |
33 | #include <sys/stat.h> | |
34 | #include <unistd.h> | |
35 | #include <string.h> | |
36 | #include <errno.h> | |
37 | #include <sys/types.h> | |
38 | #include <fts.h> | |
85db4618 | 39 | #include <assert.h> |
16b22a0f JD |
40 | #include <sys/mman.h> |
41 | #include <lttng/lttng.h> | |
42 | #include <lttng/lttngtop-helper.h> | |
c78f2cdc | 43 | #include <babeltrace/lttngtopmmappacketseek.h> |
1fc22eb4 JD |
44 | |
45 | #include "lttngtoptypes.h" | |
46 | #include "cputop.h" | |
47 | #include "iostreamtop.h" | |
48 | #include "cursesdisplay.h" | |
49 | #include "common.h" | |
50 | ||
51 | #define DEFAULT_FILE_ARRAY_SIZE 1 | |
52 | ||
53 | const char *opt_input_path; | |
32b70e07 | 54 | static int opt_textdump; |
32b70e07 | 55 | static int opt_child; |
1fc22eb4 | 56 | |
ae9e85c7 JD |
57 | int quit = 0; |
58 | ||
1fc22eb4 JD |
59 | struct lttngtop *copy; |
60 | pthread_t display_thread; | |
61 | pthread_t timer_thread; | |
62 | ||
63 | unsigned long refresh_display = 1 * NSEC_PER_SEC; | |
64 | unsigned long last_display_update = 0; | |
1fc22eb4 | 65 | |
16b22a0f JD |
66 | /* list of FDs available for being read with snapshots */ |
67 | struct mmap_stream_list mmap_list; | |
68 | GPtrArray *lttng_consumer_stream_array; | |
69 | int sessiond_metadata, consumerd_metadata; | |
4e6aeb3d JD |
70 | struct lttng_consumer_local_data *ctx = NULL; |
71 | /* list of snapshots currently not consumed */ | |
72 | GPtrArray *available_snapshots; | |
73 | sem_t metadata_available; | |
050f9cf9 | 74 | int reload_trace = 0; |
16b22a0f | 75 | |
57bff788 JD |
76 | int last_textdump_print_newline = 1; |
77 | ||
1fc22eb4 JD |
78 | enum { |
79 | OPT_NONE = 0, | |
80 | OPT_HELP, | |
6777529d | 81 | OPT_TEXTDUMP, |
32b70e07 JD |
82 | OPT_PID, |
83 | OPT_CHILD, | |
1fc22eb4 JD |
84 | }; |
85 | ||
86 | static struct poptOption long_options[] = { | |
87 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
88 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, | |
6777529d | 89 | { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL }, |
32b70e07 | 90 | { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL }, |
57bff788 | 91 | { "pid", 'p', POPT_ARG_STRING, &opt_tid, OPT_PID, NULL, NULL }, |
1fc22eb4 JD |
92 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
93 | }; | |
94 | ||
24797198 JD |
95 | static void handle_textdump_sigterm(int signal) |
96 | { | |
97 | quit = 1; | |
98 | lttng_destroy_session("test"); | |
99 | } | |
100 | ||
1fc22eb4 JD |
101 | void *refresh_thread(void *p) |
102 | { | |
cade3418 JD |
103 | struct mmap_stream *mmap_info; |
104 | ||
1fc22eb4 | 105 | while (1) { |
ae9e85c7 JD |
106 | if (quit) { |
107 | sem_post(&pause_sem); | |
108 | sem_post(&timer); | |
1402044a | 109 | sem_post(&end_trace_sem); |
ae9e85c7 | 110 | sem_post(&goodtodisplay); |
1402044a | 111 | sem_post(&goodtoupdate); |
ae9e85c7 JD |
112 | pthread_exit(0); |
113 | } | |
1402044a | 114 | if (!opt_input_path) { |
d2d680e0 JD |
115 | bt_list_for_each_entry(mmap_info, &mmap_list.head, list) |
116 | helper_kernctl_buffer_flush(mmap_info->fd); | |
1402044a | 117 | } |
1fc22eb4 JD |
118 | sem_wait(&pause_sem); |
119 | sem_post(&pause_sem); | |
120 | sem_post(&timer); | |
121 | sleep(refresh_display/NSEC_PER_SEC); | |
122 | } | |
123 | } | |
124 | ||
125 | void *ncurses_display(void *p) | |
126 | { | |
127 | unsigned int current_display_index = 0; | |
128 | ||
129 | sem_wait(&bootstrap); | |
b332d28f JD |
130 | /* |
131 | * Prevent the 1 second delay when we hit ESC | |
132 | */ | |
133 | ESCDELAY = 0; | |
1fc22eb4 JD |
134 | init_ncurses(); |
135 | ||
136 | while (1) { | |
137 | sem_wait(&timer); | |
138 | sem_wait(&goodtodisplay); | |
139 | sem_wait(&pause_sem); | |
140 | ||
ae9e85c7 | 141 | if (quit) { |
33572a17 JD |
142 | sem_post(&pause_sem); |
143 | sem_post(&timer); | |
ae9e85c7 JD |
144 | reset_ncurses(); |
145 | pthread_exit(0); | |
146 | } | |
147 | ||
1fc22eb4 | 148 | copy = g_ptr_array_index(copies, current_display_index); |
85db4618 JD |
149 | assert(copy); |
150 | display(current_display_index++); | |
1fc22eb4 JD |
151 | |
152 | sem_post(&goodtoupdate); | |
153 | sem_post(&pause_sem); | |
1fc22eb4 JD |
154 | } |
155 | } | |
156 | ||
6112d0d4 JD |
157 | /* |
158 | * hook on each event to check the timestamp and refresh the display if | |
159 | * necessary | |
160 | */ | |
161 | enum bt_cb_ret print_timestamp(struct bt_ctf_event *call_data, void *private_data) | |
162 | { | |
163 | unsigned long timestamp; | |
164 | struct tm start; | |
b520ab45 | 165 | uint64_t ts_nsec_start; |
32b70e07 | 166 | int pid; |
d2d680e0 JD |
167 | int64_t syscall_ret; |
168 | const struct definition *scope; | |
6112d0d4 | 169 | |
6112d0d4 JD |
170 | timestamp = bt_ctf_get_timestamp(call_data); |
171 | ||
b520ab45 | 172 | start = format_timestamp(timestamp); |
6112d0d4 JD |
173 | ts_nsec_start = timestamp % NSEC_PER_SEC; |
174 | ||
32b70e07 | 175 | pid = get_context_pid(call_data); |
57bff788 | 176 | if (pid == -1ULL && opt_tid) { |
32b70e07 JD |
177 | goto error; |
178 | } | |
179 | ||
57bff788 | 180 | if (opt_tid && !lookup_tid_list(pid)) |
32b70e07 JD |
181 | goto end; |
182 | ||
d2d680e0 JD |
183 | if (strcmp(bt_ctf_event_name(call_data), "exit_syscall") == 0) { |
184 | scope = bt_ctf_get_top_level_scope(call_data, | |
185 | BT_EVENT_FIELDS); | |
186 | syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
187 | scope, "_ret")); | |
188 | printf("= %ld\n", syscall_ret); | |
57bff788 | 189 | last_textdump_print_newline = 1; |
d2d680e0 | 190 | } else { |
57bff788 JD |
191 | /* we might have lost the exit_syscall event, so need to |
192 | * print the newline in this case */ | |
193 | if (last_textdump_print_newline == 0) | |
194 | printf("\n"); | |
d2d680e0 JD |
195 | printf("%02d:%02d:%02d.%09" PRIu64 " %d : %s ", start.tm_hour, |
196 | start.tm_min, start.tm_sec, ts_nsec_start, | |
197 | pid, bt_ctf_event_name(call_data)); | |
57bff788 | 198 | last_textdump_print_newline = 0; |
d2d680e0 | 199 | } |
6112d0d4 | 200 | |
32b70e07 | 201 | end: |
6112d0d4 | 202 | return BT_CB_OK; |
32b70e07 JD |
203 | error: |
204 | return BT_CB_ERROR_STOP; | |
6112d0d4 JD |
205 | } |
206 | ||
1fc22eb4 JD |
207 | /* |
208 | * hook on each event to check the timestamp and refresh the display if | |
209 | * necessary | |
210 | */ | |
4adc8274 | 211 | enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data) |
1fc22eb4 JD |
212 | { |
213 | unsigned long timestamp; | |
214 | ||
c78f2cdc | 215 | timestamp = bt_ctf_get_timestamp(call_data); |
1fc22eb4 JD |
216 | if (timestamp == -1ULL) |
217 | goto error; | |
218 | ||
219 | if (last_display_update == 0) | |
220 | last_display_update = timestamp; | |
221 | ||
222 | if (timestamp - last_display_update >= refresh_display) { | |
223 | sem_wait(&goodtoupdate); | |
224 | g_ptr_array_add(copies, get_copy_lttngtop(last_display_update, | |
225 | timestamp)); | |
226 | sem_post(&goodtodisplay); | |
227 | sem_post(&bootstrap); | |
228 | last_display_update = timestamp; | |
229 | } | |
230 | return BT_CB_OK; | |
231 | ||
232 | error: | |
233 | fprintf(stderr, "check_timestamp callback error\n"); | |
234 | return BT_CB_ERROR_STOP; | |
235 | } | |
236 | ||
237 | /* | |
238 | * get_perf_counter : get or create and return a perf_counter struct for | |
239 | * either a process or a cpu (only one of the 2 parameters mandatory) | |
240 | */ | |
241 | struct perfcounter *get_perf_counter(const char *name, struct processtop *proc, | |
242 | struct cputime *cpu) | |
243 | { | |
bb6d72a2 | 244 | struct perfcounter *ret; |
1fc22eb4 JD |
245 | GHashTable *table; |
246 | ||
247 | if (proc) | |
248 | table = proc->perf; | |
249 | else if (cpu) | |
250 | table = cpu->perf; | |
251 | else | |
252 | goto error; | |
253 | ||
254 | ret = g_hash_table_lookup(table, (gpointer) name); | |
255 | if (ret) | |
256 | goto end; | |
257 | ||
559c9f86 | 258 | ret = g_new0(struct perfcounter, 1); |
1fc22eb4 JD |
259 | /* by default, make it visible in the UI */ |
260 | ret->visible = 1; | |
85db4618 | 261 | g_hash_table_insert(table, (gpointer) strdup(name), ret); |
1fc22eb4 | 262 | |
1fc22eb4 JD |
263 | end: |
264 | return ret; | |
265 | ||
266 | error: | |
267 | return NULL; | |
268 | } | |
269 | ||
270 | void update_perf_value(struct processtop *proc, struct cputime *cpu, | |
271 | const char *name, int value) | |
272 | { | |
273 | struct perfcounter *cpu_perf, *process_perf; | |
274 | ||
275 | cpu_perf = get_perf_counter(name, NULL, cpu); | |
276 | if (cpu_perf->count < value) { | |
277 | process_perf = get_perf_counter(name, proc, NULL); | |
278 | process_perf->count += value - cpu_perf->count; | |
279 | cpu_perf->count = value; | |
280 | } | |
281 | } | |
282 | ||
4adc8274 | 283 | void extract_perf_counter_scope(const struct bt_ctf_event *event, |
2e0a1190 | 284 | const struct bt_definition *scope, |
1fc22eb4 JD |
285 | struct processtop *proc, |
286 | struct cputime *cpu) | |
287 | { | |
2e0a1190 JD |
288 | struct bt_definition const * const *list = NULL; |
289 | const struct bt_definition *field; | |
1fc22eb4 | 290 | unsigned int count; |
bb6d72a2 JD |
291 | struct perfcounter *perfcounter; |
292 | GHashTableIter iter; | |
293 | gpointer key; | |
294 | int ret; | |
1fc22eb4 JD |
295 | |
296 | if (!scope) | |
297 | goto end; | |
298 | ||
299 | ret = bt_ctf_get_field_list(event, scope, &list, &count); | |
300 | if (ret < 0) | |
301 | goto end; | |
302 | ||
bb6d72a2 JD |
303 | if (count == 0) |
304 | goto end; | |
305 | ||
306 | g_hash_table_iter_init(&iter, global_perf_liszt); | |
307 | while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) { | |
308 | field = bt_ctf_get_field(event, scope, (char *) key); | |
309 | if (field) { | |
310 | int value = bt_ctf_get_uint64(field); | |
1fc22eb4 JD |
311 | if (bt_ctf_field_get_error()) |
312 | continue; | |
bb6d72a2 | 313 | update_perf_value(proc, cpu, (char *) key, value); |
1fc22eb4 JD |
314 | } |
315 | } | |
316 | ||
317 | end: | |
318 | return; | |
319 | } | |
320 | ||
4adc8274 | 321 | void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event) |
1fc22eb4 | 322 | { |
1fc22eb4 | 323 | struct cputime *cpu; |
2e0a1190 | 324 | const struct bt_definition *scope; |
1fc22eb4 | 325 | |
d67167cd | 326 | cpu = get_cpu(get_cpu_id(event)); |
1fc22eb4 JD |
327 | |
328 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
329 | extract_perf_counter_scope(event, scope, proc, cpu); | |
330 | ||
331 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT); | |
332 | extract_perf_counter_scope(event, scope, proc, cpu); | |
333 | ||
334 | scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT); | |
335 | extract_perf_counter_scope(event, scope, proc, cpu); | |
1fc22eb4 JD |
336 | } |
337 | ||
4adc8274 | 338 | enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data, |
1fc22eb4 JD |
339 | void *private_data) |
340 | { | |
1402044a | 341 | int pid, tid, ppid, vpid, vtid, vppid; |
1fc22eb4 JD |
342 | char *comm; |
343 | struct processtop *parent, *child; | |
1fc22eb4 JD |
344 | unsigned long timestamp; |
345 | ||
c78f2cdc | 346 | timestamp = bt_ctf_get_timestamp(call_data); |
1fc22eb4 JD |
347 | if (timestamp == -1ULL) |
348 | goto error; | |
349 | ||
1dec520a JD |
350 | pid = get_context_pid(call_data); |
351 | if (pid == -1ULL) { | |
1fc22eb4 JD |
352 | goto error; |
353 | } | |
96aa77de | 354 | |
1dec520a JD |
355 | tid = get_context_tid(call_data); |
356 | if (tid == -1ULL) { | |
1fc22eb4 JD |
357 | goto error; |
358 | } | |
1dec520a JD |
359 | ppid = get_context_ppid(call_data); |
360 | if (ppid == -1ULL) { | |
1fc22eb4 JD |
361 | goto error; |
362 | } | |
1402044a JD |
363 | vpid = get_context_vpid(call_data); |
364 | if (pid == -1ULL) { | |
365 | vpid = -1; | |
366 | } | |
367 | vtid = get_context_vtid(call_data); | |
368 | if (tid == -1ULL) { | |
369 | vtid = -1; | |
370 | } | |
371 | vppid = get_context_vppid(call_data); | |
372 | if (ppid == -1ULL) { | |
373 | vppid = -1; | |
374 | } | |
1dec520a JD |
375 | comm = get_context_comm(call_data); |
376 | if (!comm) { | |
1fc22eb4 JD |
377 | goto error; |
378 | } | |
379 | ||
380 | /* find or create the current process */ | |
381 | child = find_process_tid(<tngtop, tid, comm); | |
382 | if (!child) | |
383 | child = add_proc(<tngtop, tid, comm, timestamp); | |
96aa77de JD |
384 | if (!child) |
385 | goto end; | |
1402044a | 386 | update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm); |
1fc22eb4 JD |
387 | |
388 | if (pid != tid) { | |
389 | /* find or create the parent */ | |
390 | parent = find_process_tid(<tngtop, pid, comm); | |
391 | if (!parent) { | |
392 | parent = add_proc(<tngtop, pid, comm, timestamp); | |
96aa77de JD |
393 | if (parent) |
394 | parent->pid = pid; | |
1fc22eb4 JD |
395 | } |
396 | ||
397 | /* attach the parent to the current process */ | |
398 | child->threadparent = parent; | |
399 | add_thread(parent, child); | |
400 | } | |
401 | ||
402 | update_perf_counter(child, call_data); | |
403 | ||
96aa77de | 404 | end: |
1fc22eb4 JD |
405 | return BT_CB_OK; |
406 | ||
407 | error: | |
408 | return BT_CB_ERROR_STOP; | |
409 | } | |
410 | ||
411 | void init_lttngtop() | |
412 | { | |
413 | copies = g_ptr_array_new(); | |
0d91c12a | 414 | global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal); |
1fc22eb4 JD |
415 | |
416 | sem_init(&goodtodisplay, 0, 0); | |
417 | sem_init(&goodtoupdate, 0, 1); | |
418 | sem_init(&timer, 0, 1); | |
419 | sem_init(&bootstrap, 0, 0); | |
420 | sem_init(&pause_sem, 0, 1); | |
421 | sem_init(&end_trace_sem, 0, 0); | |
422 | ||
e05a35a6 JD |
423 | reset_global_counters(); |
424 | lttngtop.nbproc = 0; | |
425 | lttngtop.nbthreads = 0; | |
426 | lttngtop.nbfiles = 0; | |
427 | ||
30b646c4 JD |
428 | lttngtop.process_hash_table = g_hash_table_new(g_direct_hash, |
429 | g_direct_equal); | |
1fc22eb4 JD |
430 | lttngtop.process_table = g_ptr_array_new(); |
431 | lttngtop.files_table = g_ptr_array_new(); | |
432 | lttngtop.cpu_table = g_ptr_array_new(); | |
433 | } | |
434 | ||
c263c4eb | 435 | void usage(FILE *fp) |
1fc22eb4 | 436 | { |
c263c4eb JD |
437 | fprintf(fp, "LTTngTop %s\n\n", VERSION); |
438 | fprintf(fp, "Usage : lttngtop /path/to/trace\n"); | |
1fc22eb4 JD |
439 | } |
440 | ||
441 | /* | |
442 | * Return 0 if caller should continue, < 0 if caller should return | |
443 | * error, > 0 if caller should exit without reporting error. | |
444 | */ | |
445 | static int parse_options(int argc, char **argv) | |
446 | { | |
447 | poptContext pc; | |
448 | int opt, ret = 0; | |
57bff788 JD |
449 | char *tmp_str; |
450 | int *tid; | |
1fc22eb4 | 451 | |
1fc22eb4 JD |
452 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
453 | poptReadDefaultConfig(pc, 0); | |
454 | ||
455 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
456 | switch (opt) { | |
457 | case OPT_HELP: | |
458 | usage(stdout); | |
459 | ret = 1; /* exit cleanly */ | |
460 | goto end; | |
6777529d JD |
461 | case OPT_TEXTDUMP: |
462 | opt_textdump = 1; | |
32b70e07 JD |
463 | break; |
464 | case OPT_CHILD: | |
465 | opt_textdump = 1; | |
466 | opt_child = 1; | |
467 | break; | |
468 | case OPT_PID: | |
57bff788 JD |
469 | tid_list = g_hash_table_new(g_str_hash, g_str_equal); |
470 | tmp_str = strtok(opt_tid, ","); | |
471 | while (tmp_str) { | |
472 | tid = malloc(sizeof(int)); | |
473 | *tid = atoi(tmp_str); | |
474 | g_hash_table_insert(tid_list, (gpointer) tid, tid); | |
475 | tmp_str = strtok(NULL, ","); | |
476 | } | |
32b70e07 | 477 | break; |
1fc22eb4 JD |
478 | default: |
479 | ret = -EINVAL; | |
480 | goto end; | |
481 | } | |
482 | } | |
483 | ||
484 | opt_input_path = poptGetArg(pc); | |
16b22a0f | 485 | |
1fc22eb4 JD |
486 | end: |
487 | if (pc) { | |
488 | poptFreeContext(pc); | |
489 | } | |
490 | return ret; | |
491 | } | |
492 | ||
493 | void iter_trace(struct bt_context *bt_ctx) | |
494 | { | |
495 | struct bt_ctf_iter *iter; | |
496 | struct bt_iter_pos begin_pos; | |
4adc8274 | 497 | const struct bt_ctf_event *event; |
1fc22eb4 JD |
498 | int ret = 0; |
499 | ||
500 | begin_pos.type = BT_SEEK_BEGIN; | |
501 | iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL); | |
502 | ||
6777529d JD |
503 | if (opt_textdump) { |
504 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
505 | print_timestamp, | |
506 | NULL, NULL, NULL); | |
507 | } else { | |
508 | /* at each event check if we need to refresh */ | |
509 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
510 | check_timestamp, | |
511 | NULL, NULL, NULL); | |
512 | /* at each event, verify the status of the process table */ | |
513 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
514 | fix_process_table, | |
515 | NULL, NULL, NULL); | |
516 | /* to handle the scheduling events */ | |
517 | bt_ctf_iter_add_callback(iter, | |
518 | g_quark_from_static_string("sched_switch"), | |
519 | NULL, 0, handle_sched_switch, NULL, NULL, NULL); | |
520 | /* to clean up the process table */ | |
521 | bt_ctf_iter_add_callback(iter, | |
522 | g_quark_from_static_string("sched_process_free"), | |
523 | NULL, 0, handle_sched_process_free, NULL, NULL, NULL); | |
524 | /* to get all the process from the statedumps */ | |
525 | bt_ctf_iter_add_callback(iter, | |
526 | g_quark_from_static_string( | |
527 | "lttng_statedump_process_state"), | |
528 | NULL, 0, handle_statedump_process_state, | |
529 | NULL, NULL, NULL); | |
530 | ||
531 | /* for IO top */ | |
532 | bt_ctf_iter_add_callback(iter, | |
533 | g_quark_from_static_string("exit_syscall"), | |
534 | NULL, 0, handle_exit_syscall, NULL, NULL, NULL); | |
535 | bt_ctf_iter_add_callback(iter, | |
536 | g_quark_from_static_string("sys_write"), | |
537 | NULL, 0, handle_sys_write, NULL, NULL, NULL); | |
538 | bt_ctf_iter_add_callback(iter, | |
539 | g_quark_from_static_string("sys_read"), | |
540 | NULL, 0, handle_sys_read, NULL, NULL, NULL); | |
541 | bt_ctf_iter_add_callback(iter, | |
542 | g_quark_from_static_string("sys_open"), | |
543 | NULL, 0, handle_sys_open, NULL, NULL, NULL); | |
544 | bt_ctf_iter_add_callback(iter, | |
545 | g_quark_from_static_string("sys_close"), | |
546 | NULL, 0, handle_sys_close, NULL, NULL, NULL); | |
547 | bt_ctf_iter_add_callback(iter, | |
548 | g_quark_from_static_string( | |
ceb3a221 | 549 | "lttng_statedump_file_descriptor"), |
6777529d JD |
550 | NULL, 0, handle_statedump_file_descriptor, |
551 | NULL, NULL, NULL); | |
552 | } | |
553 | ||
6db97c51 | 554 | while ((event = bt_ctf_iter_read_event(iter, NULL)) != NULL) { |
050f9cf9 | 555 | if (quit || reload_trace) |
ae9e85c7 | 556 | goto end_iter; |
1fc22eb4 JD |
557 | ret = bt_iter_next(bt_ctf_get_iter(iter)); |
558 | if (ret < 0) | |
559 | goto end_iter; | |
560 | } | |
561 | ||
562 | /* block until quit, we reached the end of the trace */ | |
563 | sem_wait(&end_trace_sem); | |
564 | ||
565 | end_iter: | |
06570214 | 566 | bt_ctf_iter_destroy(iter); |
1fc22eb4 JD |
567 | } |
568 | ||
569 | /* | |
570 | * bt_context_add_traces_recursive: Open a trace recursively | |
571 | * (copied from BSD code in converter/babeltrace.c) | |
572 | * | |
573 | * Find each trace present in the subdirectory starting from the given | |
574 | * path, and add them to the context. The packet_seek parameter can be | |
575 | * NULL: this specify to use the default format packet_seek. | |
576 | * | |
577 | * Return: 0 on success, nonzero on failure. | |
578 | * Unable to open toplevel: failure. | |
579 | * Unable to open some subdirectory or file: warn and continue; | |
580 | */ | |
581 | int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path, | |
582 | const char *format_str, | |
2e0a1190 | 583 | void (*packet_seek)(struct bt_stream_pos *pos, |
1fc22eb4 JD |
584 | size_t offset, int whence)) |
585 | { | |
586 | FTS *tree; | |
587 | FTSENT *node; | |
588 | GArray *trace_ids; | |
589 | char lpath[PATH_MAX]; | |
590 | char * const paths[2] = { lpath, NULL }; | |
0d567cf1 | 591 | int ret = -1; |
1fc22eb4 JD |
592 | |
593 | /* | |
594 | * Need to copy path, because fts_open can change it. | |
595 | * It is the pointer array, not the strings, that are constant. | |
596 | */ | |
597 | strncpy(lpath, path, PATH_MAX); | |
598 | lpath[PATH_MAX - 1] = '\0'; | |
599 | ||
600 | tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0); | |
601 | if (tree == NULL) { | |
602 | fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n", | |
603 | path); | |
604 | return -EINVAL; | |
605 | } | |
606 | ||
607 | trace_ids = g_array_new(FALSE, TRUE, sizeof(int)); | |
608 | ||
609 | while ((node = fts_read(tree))) { | |
610 | int dirfd, metafd; | |
611 | ||
612 | if (!(node->fts_info & FTS_D)) | |
613 | continue; | |
614 | ||
615 | dirfd = open(node->fts_accpath, 0); | |
616 | if (dirfd < 0) { | |
617 | fprintf(stderr, "[error] [Context] Unable to open trace " | |
618 | "directory file descriptor.\n"); | |
619 | ret = dirfd; | |
620 | goto error; | |
621 | } | |
622 | metafd = openat(dirfd, "metadata", O_RDONLY); | |
623 | if (metafd < 0) { | |
7314c855 JD |
624 | close(dirfd); |
625 | ret = -1; | |
626 | continue; | |
1fc22eb4 JD |
627 | } else { |
628 | int trace_id; | |
629 | ||
630 | ret = close(metafd); | |
631 | if (ret < 0) { | |
632 | perror("close"); | |
633 | goto error; | |
634 | } | |
635 | ret = close(dirfd); | |
636 | if (ret < 0) { | |
637 | perror("close"); | |
638 | goto error; | |
639 | } | |
640 | ||
641 | trace_id = bt_context_add_trace(ctx, | |
642 | node->fts_accpath, format_str, | |
643 | packet_seek, NULL, NULL); | |
644 | if (trace_id < 0) { | |
0d567cf1 | 645 | fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s " |
1fc22eb4 | 646 | "for reading.\n", node->fts_accpath, path); |
0d567cf1 JD |
647 | /* Allow to skip erroneous traces. */ |
648 | continue; | |
1fc22eb4 JD |
649 | } |
650 | g_array_append_val(trace_ids, trace_id); | |
651 | } | |
652 | } | |
653 | ||
654 | g_array_free(trace_ids, TRUE); | |
0d567cf1 | 655 | return ret; |
1fc22eb4 JD |
656 | |
657 | error: | |
658 | return ret; | |
659 | } | |
660 | ||
4553b4d1 | 661 | static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list, |
a54d0d2c JD |
662 | int field_cnt, int *tid_check, int *pid_check, |
663 | int *procname_check, int *ppid_check) | |
664 | { | |
665 | int j; | |
bb6d72a2 JD |
666 | struct perfcounter *global; |
667 | const char *name; | |
a54d0d2c JD |
668 | |
669 | for (j = 0; j < field_cnt; j++) { | |
bb6d72a2 | 670 | name = bt_ctf_get_decl_field_name(field_list[j]); |
a54d0d2c | 671 | if (*tid_check == 0) { |
bb6d72a2 | 672 | if (strncmp(name, "tid", 3) == 0) |
a54d0d2c | 673 | (*tid_check)++; |
a54d0d2c JD |
674 | } |
675 | if (*pid_check == 0) { | |
da401e9c | 676 | if (strncmp(name, "pid", 3) == 0) |
a54d0d2c JD |
677 | (*pid_check)++; |
678 | } | |
679 | if (*ppid_check == 0) { | |
bb6d72a2 | 680 | if (strncmp(name, "ppid", 4) == 0) |
a54d0d2c JD |
681 | (*ppid_check)++; |
682 | } | |
683 | if (*procname_check == 0) { | |
bb6d72a2 | 684 | if (strncmp(name, "procname", 8) == 0) |
a54d0d2c JD |
685 | (*procname_check)++; |
686 | } | |
bb6d72a2 JD |
687 | if (strncmp(name, "perf_", 5) == 0) { |
688 | global = g_hash_table_lookup(global_perf_liszt, (gpointer) name); | |
689 | if (!global) { | |
690 | global = g_new0(struct perfcounter, 1); | |
691 | /* by default, sort on the first perf context */ | |
692 | if (g_hash_table_size(global_perf_liszt) == 0) | |
693 | global->sort = 1; | |
694 | global->visible = 1; | |
695 | g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global); | |
696 | } | |
697 | } | |
a54d0d2c | 698 | } |
bb6d72a2 | 699 | |
a54d0d2c JD |
700 | if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 && |
701 | *procname_check == 1) | |
702 | return 0; | |
703 | ||
704 | return -1; | |
705 | } | |
706 | ||
707 | /* | |
708 | * check_requirements: check if the required context informations are available | |
709 | * | |
710 | * If each mandatory context information is available for at least in one | |
711 | * event, return 0 otherwise return -1. | |
a54d0d2c JD |
712 | */ |
713 | int check_requirements(struct bt_context *ctx) | |
714 | { | |
715 | unsigned int i, evt_cnt, field_cnt; | |
716 | struct bt_ctf_event_decl *const * evt_list; | |
717 | const struct bt_ctf_field_decl *const * field_list; | |
718 | int tid_check = 0; | |
719 | int pid_check = 0; | |
720 | int procname_check = 0; | |
721 | int ppid_check = 0; | |
722 | int ret = 0; | |
723 | ||
724 | bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt); | |
725 | for (i = 0; i < evt_cnt; i++) { | |
726 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT, | |
727 | &field_list, &field_cnt); | |
728 | ret = check_field_requirements(field_list, field_cnt, | |
729 | &tid_check, &pid_check, &procname_check, | |
730 | &ppid_check); | |
a54d0d2c JD |
731 | |
732 | bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT, | |
733 | &field_list, &field_cnt); | |
734 | ret = check_field_requirements(field_list, field_cnt, | |
735 | &tid_check, &pid_check, &procname_check, | |
736 | &ppid_check); | |
a54d0d2c JD |
737 | |
738 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT, | |
739 | &field_list, &field_cnt); | |
740 | ret = check_field_requirements(field_list, field_cnt, | |
741 | &tid_check, &pid_check, &procname_check, | |
742 | &ppid_check); | |
a54d0d2c JD |
743 | } |
744 | ||
745 | if (tid_check == 0) { | |
746 | ret = -1; | |
747 | fprintf(stderr, "[error] missing tid context information\n"); | |
748 | } | |
749 | if (pid_check == 0) { | |
750 | ret = -1; | |
751 | fprintf(stderr, "[error] missing pid context information\n"); | |
752 | } | |
753 | if (ppid_check == 0) { | |
754 | ret = -1; | |
755 | fprintf(stderr, "[error] missing ppid context information\n"); | |
756 | } | |
757 | if (procname_check == 0) { | |
758 | ret = -1; | |
759 | fprintf(stderr, "[error] missing procname context information\n"); | |
760 | } | |
761 | ||
a54d0d2c JD |
762 | return ret; |
763 | } | |
764 | ||
16b22a0f JD |
765 | ssize_t read_subbuffer(struct lttng_consumer_stream *kconsumerd_fd, |
766 | struct lttng_consumer_local_data *ctx) | |
767 | { | |
768 | unsigned long len; | |
769 | int err; | |
770 | long ret = 0; | |
771 | int infd = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd); | |
772 | ||
773 | if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_SPLICE) { | |
774 | /* Get the next subbuffer */ | |
16b22a0f JD |
775 | err = helper_kernctl_get_next_subbuf(infd); |
776 | if (err != 0) { | |
777 | ret = errno; | |
778 | perror("Reserving sub buffer failed (everything is normal, " | |
779 | "it is due to concurrency)"); | |
780 | goto end; | |
781 | } | |
782 | /* read the whole subbuffer */ | |
783 | err = helper_kernctl_get_padded_subbuf_size(infd, &len); | |
784 | if (err != 0) { | |
785 | ret = errno; | |
786 | perror("Getting sub-buffer len failed."); | |
787 | goto end; | |
788 | } | |
16b22a0f JD |
789 | |
790 | /* splice the subbuffer to the tracefile */ | |
791 | ret = helper_lttng_consumer_on_read_subbuffer_splice(ctx, kconsumerd_fd, len); | |
792 | if (ret < 0) { | |
793 | /* | |
794 | * display the error but continue processing to try | |
795 | * to release the subbuffer | |
796 | */ | |
797 | fprintf(stderr,"Error splicing to tracefile\n"); | |
798 | } | |
16b22a0f JD |
799 | err = helper_kernctl_put_next_subbuf(infd); |
800 | if (err != 0) { | |
801 | ret = errno; | |
802 | perror("Reserving sub buffer failed (everything is normal, " | |
803 | "it is due to concurrency)"); | |
804 | goto end; | |
805 | } | |
4e6aeb3d | 806 | sem_post(&metadata_available); |
16b22a0f JD |
807 | } |
808 | ||
809 | end: | |
810 | return 0; | |
811 | } | |
812 | ||
813 | int on_update_fd(int key, uint32_t state) | |
814 | { | |
815 | /* let the lib handle the metadata FD */ | |
816 | if (key == sessiond_metadata) | |
817 | return 0; | |
818 | return 1; | |
819 | } | |
820 | ||
821 | int on_recv_fd(struct lttng_consumer_stream *kconsumerd_fd) | |
822 | { | |
823 | int ret; | |
b520ab45 | 824 | struct mmap_stream *new_mmap_stream; |
16b22a0f | 825 | |
16b22a0f JD |
826 | /* Opening the tracefile in write mode */ |
827 | if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd) != NULL) { | |
828 | ret = open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd), | |
829 | O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO); | |
830 | if (ret < 0) { | |
831 | perror("open"); | |
832 | goto end; | |
833 | } | |
834 | helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd, ret); | |
835 | } | |
836 | ||
837 | if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_MMAP) { | |
b520ab45 JD |
838 | new_mmap_stream = malloc(sizeof(struct mmap_stream)); |
839 | new_mmap_stream->fd = helper_get_lttng_consumer_stream_wait_fd( | |
840 | kconsumerd_fd); | |
841 | bt_list_add(&new_mmap_stream->list, &mmap_list.head); | |
16b22a0f JD |
842 | |
843 | g_ptr_array_add(lttng_consumer_stream_array, kconsumerd_fd); | |
6112d0d4 JD |
844 | /* keep mmap FDs internally */ |
845 | ret = 1; | |
16b22a0f JD |
846 | } else { |
847 | consumerd_metadata = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd); | |
848 | sessiond_metadata = helper_get_lttng_consumer_stream_key(kconsumerd_fd); | |
849 | ret = 0; | |
850 | } | |
851 | ||
050f9cf9 JD |
852 | reload_trace = 1; |
853 | ||
16b22a0f JD |
854 | end: |
855 | return ret; | |
856 | } | |
857 | ||
eb677852 | 858 | void live_consume(struct bt_context **bt_ctx) |
e91044a8 | 859 | { |
e91044a8 | 860 | int ret; |
050f9cf9 | 861 | FILE *metadata_fp; |
e91044a8 | 862 | |
050f9cf9 JD |
863 | sem_wait(&metadata_available); |
864 | if (access("/tmp/livesession/kernel/metadata", F_OK) != 0) { | |
865 | fprintf(stderr,"no metadata\n"); | |
866 | goto end; | |
71c74ca6 | 867 | } |
050f9cf9 | 868 | metadata_fp = fopen("/tmp/livesession/kernel/metadata", "r"); |
c78f2cdc | 869 | |
050f9cf9 JD |
870 | *bt_ctx = bt_context_create(); |
871 | ret = bt_context_add_trace(*bt_ctx, NULL, "ctf", | |
872 | lttngtop_ctf_packet_seek, &mmap_list, metadata_fp); | |
873 | if (ret < 0) { | |
874 | printf("Error adding trace\n"); | |
875 | goto end; | |
e91044a8 | 876 | } |
b520ab45 | 877 | |
eb677852 JD |
878 | end: |
879 | return; | |
e91044a8 | 880 | } |
16b22a0f JD |
881 | |
882 | int setup_consumer(char *command_sock_path, pthread_t *threads, | |
883 | struct lttng_consumer_local_data *ctx) | |
884 | { | |
885 | int ret = 0; | |
886 | ||
887 | ctx = helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL, | |
888 | read_subbuffer, NULL, on_recv_fd, on_update_fd); | |
889 | if (!ctx) | |
890 | goto end; | |
891 | ||
892 | unlink(command_sock_path); | |
893 | helper_lttng_consumer_set_command_sock_path(ctx, command_sock_path); | |
894 | helper_lttng_consumer_init(); | |
895 | ||
896 | /* Create the thread to manage the receive of fd */ | |
897 | ret = pthread_create(&threads[0], NULL, helper_lttng_consumer_thread_receive_fds, | |
898 | (void *) ctx); | |
899 | if (ret != 0) { | |
c78f2cdc | 900 | perror("pthread_create receive fd"); |
16b22a0f JD |
901 | goto end; |
902 | } | |
e91044a8 JD |
903 | /* Create thread to manage the polling/writing of traces */ |
904 | ret = pthread_create(&threads[1], NULL, helper_lttng_consumer_thread_poll_fds, | |
905 | (void *) ctx); | |
906 | if (ret != 0) { | |
c78f2cdc | 907 | perror("pthread_create poll fd"); |
e91044a8 JD |
908 | goto end; |
909 | } | |
910 | ||
16b22a0f JD |
911 | end: |
912 | return ret; | |
913 | } | |
914 | ||
1a70b8ac | 915 | int setup_live_tracing() |
16b22a0f JD |
916 | { |
917 | struct lttng_domain dom; | |
918 | struct lttng_channel chan; | |
919 | char *channel_name = "mmapchan"; | |
16b22a0f JD |
920 | struct lttng_event ev; |
921 | int ret = 0; | |
922 | char *command_sock_path = "/tmp/consumerd_sock"; | |
923 | static pthread_t threads[2]; /* recv_fd, poll */ | |
924 | struct lttng_event_context kctxpid, kctxcomm, kctxppid, kctxtid; | |
16b22a0f JD |
925 | |
926 | struct lttng_handle *handle; | |
927 | ||
928 | BT_INIT_LIST_HEAD(&mmap_list.head); | |
929 | ||
930 | lttng_consumer_stream_array = g_ptr_array_new(); | |
931 | ||
932 | if ((ret = setup_consumer(command_sock_path, threads, ctx)) < 0) { | |
933 | fprintf(stderr,"error setting up consumer\n"); | |
1a70b8ac | 934 | goto error; |
16b22a0f JD |
935 | } |
936 | ||
937 | available_snapshots = g_ptr_array_new(); | |
938 | ||
939 | /* setup the session */ | |
940 | dom.type = LTTNG_DOMAIN_KERNEL; | |
941 | ||
eb677852 | 942 | ret = unlink("/tmp/livesession"); |
16b22a0f | 943 | |
71c74ca6 | 944 | lttng_destroy_session("test"); |
16b22a0f JD |
945 | if ((ret = lttng_create_session("test", "/tmp/livesession")) < 0) { |
946 | fprintf(stderr,"error creating the session : %s\n", | |
947 | helper_lttcomm_get_readable_code(ret)); | |
1a70b8ac | 948 | goto error; |
16b22a0f JD |
949 | } |
950 | ||
951 | if ((handle = lttng_create_handle("test", &dom)) == NULL) { | |
952 | fprintf(stderr,"error creating handle\n"); | |
1a70b8ac | 953 | goto error_session; |
16b22a0f JD |
954 | } |
955 | ||
050f9cf9 JD |
956 | /* |
957 | * FIXME : need to let the | |
958 | * helper_lttng_consumer_thread_receive_fds create the | |
959 | * socket. | |
960 | * Cleaner solution ? | |
961 | */ | |
962 | while (access(command_sock_path, F_OK)) { | |
963 | sleep(0.1); | |
964 | } | |
965 | ||
16b22a0f JD |
966 | if ((ret = lttng_register_consumer(handle, command_sock_path)) < 0) { |
967 | fprintf(stderr,"error registering consumer : %s\n", | |
968 | helper_lttcomm_get_readable_code(ret)); | |
1a70b8ac | 969 | goto error_session; |
16b22a0f JD |
970 | } |
971 | ||
972 | strcpy(chan.name, channel_name); | |
c78f2cdc | 973 | chan.attr.overwrite = 0; |
57bff788 | 974 | if (opt_tid && opt_textdump) { |
d2d680e0 JD |
975 | chan.attr.subbuf_size = 32768; |
976 | chan.attr.num_subbuf = 8; | |
977 | } else { | |
978 | chan.attr.subbuf_size = 1048576; /* 1MB */ | |
979 | chan.attr.num_subbuf = 4; | |
980 | } | |
16b22a0f JD |
981 | chan.attr.switch_timer_interval = 0; |
982 | chan.attr.read_timer_interval = 200; | |
983 | chan.attr.output = LTTNG_EVENT_MMAP; | |
984 | ||
985 | if ((ret = lttng_enable_channel(handle, &chan)) < 0) { | |
b872c906 JD |
986 | fprintf(stderr,"error creating channel : %s\n", |
987 | helper_lttcomm_get_readable_code(ret)); | |
1a70b8ac | 988 | goto error_session; |
16b22a0f JD |
989 | } |
990 | ||
b872c906 JD |
991 | memset(&ev, '\0', sizeof(struct lttng_event)); |
992 | //sprintf(ev.name, "sched_switch"); | |
57bff788 JD |
993 | ev.type = LTTNG_EVENT_TRACEPOINT; |
994 | if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) { | |
995 | fprintf(stderr,"error enabling event : %s\n", | |
996 | helper_lttcomm_get_readable_code(ret)); | |
997 | goto error_session; | |
998 | } | |
16b22a0f | 999 | |
6777529d JD |
1000 | ev.type = LTTNG_EVENT_SYSCALL; |
1001 | if ((ret = lttng_enable_event(handle, &ev, channel_name)) < 0) { | |
1002 | fprintf(stderr,"error enabling syscalls : %s\n", | |
1003 | helper_lttcomm_get_readable_code(ret)); | |
1a70b8ac | 1004 | goto error_session; |
6777529d JD |
1005 | } |
1006 | ||
16b22a0f JD |
1007 | kctxpid.ctx = LTTNG_EVENT_CONTEXT_PID; |
1008 | lttng_add_context(handle, &kctxpid, NULL, NULL); | |
16b22a0f JD |
1009 | kctxtid.ctx = LTTNG_EVENT_CONTEXT_TID; |
1010 | lttng_add_context(handle, &kctxtid, NULL, NULL); | |
57bff788 JD |
1011 | kctxppid.ctx = LTTNG_EVENT_CONTEXT_PPID; |
1012 | lttng_add_context(handle, &kctxppid, NULL, NULL); | |
1013 | kctxcomm.ctx = LTTNG_EVENT_CONTEXT_PROCNAME; | |
1014 | lttng_add_context(handle, &kctxcomm, NULL, NULL); | |
1015 | kctxpid.ctx = LTTNG_EVENT_CONTEXT_VPID; | |
1016 | lttng_add_context(handle, &kctxpid, NULL, NULL); | |
1017 | kctxtid.ctx = LTTNG_EVENT_CONTEXT_VTID; | |
1018 | lttng_add_context(handle, &kctxtid, NULL, NULL); | |
16b22a0f JD |
1019 | |
1020 | if ((ret = lttng_start_tracing("test")) < 0) { | |
b872c906 JD |
1021 | fprintf(stderr,"error starting tracing : %s\n", |
1022 | helper_lttcomm_get_readable_code(ret)); | |
1a70b8ac | 1023 | goto error_session; |
16b22a0f JD |
1024 | } |
1025 | ||
1026 | helper_kernctl_buffer_flush(consumerd_metadata); | |
e91044a8 | 1027 | |
16b22a0f | 1028 | /* block until metadata is ready */ |
4e6aeb3d | 1029 | sem_init(&metadata_available, 0, 0); |
16b22a0f | 1030 | |
1a70b8ac JD |
1031 | return 0; |
1032 | ||
1033 | error_session: | |
1034 | lttng_destroy_session("test"); | |
1035 | error: | |
1036 | return -1; | |
16b22a0f JD |
1037 | } |
1038 | ||
1fc22eb4 JD |
1039 | int main(int argc, char **argv) |
1040 | { | |
1041 | int ret; | |
1042 | struct bt_context *bt_ctx = NULL; | |
050f9cf9 JD |
1043 | struct mmap_stream *mmap_info; |
1044 | unsigned long mmap_len; | |
1fc22eb4 JD |
1045 | |
1046 | ret = parse_options(argc, argv); | |
1047 | if (ret < 0) { | |
1048 | fprintf(stdout, "Error parsing options.\n\n"); | |
1049 | usage(stdout); | |
1050 | exit(EXIT_FAILURE); | |
1051 | } else if (ret > 0) { | |
1052 | exit(EXIT_SUCCESS); | |
1053 | } | |
1054 | ||
16b22a0f | 1055 | if (!opt_input_path) { |
24797198 JD |
1056 | if (opt_textdump) { |
1057 | signal(SIGTERM, handle_textdump_sigterm); | |
1058 | signal(SIGINT, handle_textdump_sigterm); | |
1059 | } | |
1a70b8ac JD |
1060 | ret = setup_live_tracing(); |
1061 | if (ret < 0) { | |
1062 | goto end; | |
1063 | } | |
6777529d JD |
1064 | init_lttngtop(); |
1065 | if (!opt_textdump) { | |
1066 | pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL); | |
1067 | pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL); | |
1068 | } | |
050f9cf9 JD |
1069 | while (!quit) { |
1070 | reload_trace = 0; | |
1071 | live_consume(&bt_ctx); | |
1072 | iter_trace(bt_ctx); | |
1073 | ret = bt_context_remove_trace(bt_ctx, 0); | |
1074 | if (ret != 0) | |
1075 | fprintf(stderr, "error removing trace\n"); | |
1076 | if (bt_ctx) { | |
1077 | bt_context_put(bt_ctx); | |
1078 | } | |
1079 | ||
1080 | /* | |
1081 | * since we receive all FDs every time there is an | |
1082 | * update and the FD number is different every time, | |
1083 | * we don't know which one are valid. | |
1084 | * so we check if all FDs are usable with a simple | |
1085 | * ioctl call. | |
1086 | */ | |
1087 | bt_list_for_each_entry(mmap_info, &mmap_list.head, list) { | |
1088 | ret = helper_kernctl_get_mmap_len(mmap_info->fd, &mmap_len); | |
1089 | if (ret != 0) { | |
050f9cf9 JD |
1090 | bt_list_del(&mmap_info->list); |
1091 | } | |
1092 | } | |
1093 | sem_post(&metadata_available); | |
1094 | } | |
eb677852 | 1095 | |
ae9e85c7 | 1096 | pthread_join(timer_thread, NULL); |
eb677852 JD |
1097 | quit = 1; |
1098 | pthread_join(display_thread, NULL); | |
6777529d | 1099 | |
c78f2cdc | 1100 | lttng_stop_tracing("test"); |
c78f2cdc JD |
1101 | lttng_destroy_session("test"); |
1102 | ||
1fc22eb4 | 1103 | goto end; |
16b22a0f JD |
1104 | } else { |
1105 | init_lttngtop(); | |
1106 | ||
1107 | bt_ctx = bt_context_create(); | |
1108 | ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL); | |
1109 | if (ret < 0) { | |
1110 | fprintf(stderr, "[error] Opening the trace\n"); | |
1111 | goto end; | |
1112 | } | |
1fc22eb4 | 1113 | |
16b22a0f JD |
1114 | ret = check_requirements(bt_ctx); |
1115 | if (ret < 0) { | |
1116 | fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n"); | |
1117 | goto end; | |
1118 | } | |
1119 | pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL); | |
1120 | pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL); | |
1fc22eb4 | 1121 | |
16b22a0f | 1122 | iter_trace(bt_ctx); |
1fc22eb4 | 1123 | |
16b22a0f | 1124 | pthread_join(display_thread, NULL); |
ae9e85c7 | 1125 | quit = 1; |
16b22a0f JD |
1126 | pthread_join(timer_thread, NULL); |
1127 | } | |
1fc22eb4 JD |
1128 | |
1129 | end: | |
16b22a0f JD |
1130 | if (bt_ctx) |
1131 | bt_context_put(bt_ctx); | |
1132 | ||
1fc22eb4 JD |
1133 | return 0; |
1134 | } |