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