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