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