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