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