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