Commit | Line | Data |
---|---|---|
1fc22eb4 | 1 | /* |
863dc485 | 2 | * Copyright (C) 2013 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 | 40 | #include <sys/mman.h> |
54761a45 | 41 | #include <sys/wait.h> |
16b22a0f | 42 | #include <lttng/lttng.h> |
b1acd2b3 | 43 | #ifdef LTTNGTOP_MMAP_LIVE |
16b22a0f | 44 | #include <lttng/lttngtop-helper.h> |
c78f2cdc | 45 | #include <babeltrace/lttngtopmmappacketseek.h> |
b1acd2b3 JD |
46 | #include "mmap-live.h" |
47 | #endif /* LTTNGTOP_MMAP_LIVE */ | |
1fc22eb4 JD |
48 | |
49 | #include "lttngtoptypes.h" | |
50 | #include "cputop.h" | |
51 | #include "iostreamtop.h" | |
52 | #include "cursesdisplay.h" | |
53 | #include "common.h" | |
b1acd2b3 JD |
54 | #include "network-live.h" |
55 | ||
12a91e9d JD |
56 | #define NET_URL_PREFIX "net://" |
57 | #define NET4_URL_PREFIX "net4://" | |
58 | #define NET6_URL_PREFIX "net6://" | |
1fc22eb4 JD |
59 | |
60 | #define DEFAULT_FILE_ARRAY_SIZE 1 | |
61 | ||
62 | const char *opt_input_path; | |
12a91e9d JD |
63 | int opt_textdump; |
64 | int opt_child; | |
65 | int opt_begin; | |
1fc22eb4 | 66 | |
ae9e85c7 JD |
67 | int quit = 0; |
68 | ||
1fc22eb4 JD |
69 | struct lttngtop *copy; |
70 | pthread_t display_thread; | |
71 | pthread_t timer_thread; | |
72 | ||
73 | unsigned long refresh_display = 1 * NSEC_PER_SEC; | |
74 | unsigned long last_display_update = 0; | |
863dc485 | 75 | unsigned long last_event_ts = 0; |
bca0e79c | 76 | struct syscall *last_syscall; |
1fc22eb4 | 77 | |
16b22a0f | 78 | /* list of FDs available for being read with snapshots */ |
b1acd2b3 | 79 | struct bt_mmap_stream_list mmap_list; |
16b22a0f JD |
80 | GPtrArray *lttng_consumer_stream_array; |
81 | int sessiond_metadata, consumerd_metadata; | |
4e6aeb3d JD |
82 | struct lttng_consumer_local_data *ctx = NULL; |
83 | /* list of snapshots currently not consumed */ | |
84 | GPtrArray *available_snapshots; | |
85 | sem_t metadata_available; | |
050f9cf9 | 86 | int reload_trace = 0; |
16b22a0f | 87 | |
010f485a JD |
88 | uint64_t prev_ts = 0; |
89 | ||
1fc22eb4 JD |
90 | enum { |
91 | OPT_NONE = 0, | |
92 | OPT_HELP, | |
6777529d | 93 | OPT_TEXTDUMP, |
32b70e07 JD |
94 | OPT_PID, |
95 | OPT_CHILD, | |
c8d75a13 | 96 | OPT_HOSTNAME, |
b1acd2b3 | 97 | OPT_RELAY_HOSTNAME, |
f16e36fc | 98 | OPT_KPROBES, |
3160c7a9 | 99 | OPT_BEGIN, |
1fc22eb4 JD |
100 | }; |
101 | ||
102 | static struct poptOption long_options[] = { | |
103 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
104 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, | |
6777529d | 105 | { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL }, |
32b70e07 | 106 | { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL }, |
3160c7a9 | 107 | { "begin", 'b', POPT_ARG_NONE, NULL, OPT_BEGIN, NULL, NULL }, |
57bff788 | 108 | { "pid", 'p', POPT_ARG_STRING, &opt_tid, OPT_PID, NULL, NULL }, |
c8d75a13 | 109 | { "hostname", 'n', POPT_ARG_STRING, &opt_hostname, OPT_HOSTNAME, NULL, NULL }, |
b1acd2b3 JD |
110 | { "relay-hostname", 'r', POPT_ARG_STRING, &opt_relay_hostname, |
111 | OPT_RELAY_HOSTNAME, NULL, NULL }, | |
f16e36fc | 112 | { "kprobes", 'k', POPT_ARG_STRING, &opt_kprobes, OPT_KPROBES, NULL, NULL }, |
1fc22eb4 JD |
113 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
114 | }; | |
115 | ||
863dc485 | 116 | #ifdef LTTNGTOP_MMAP_LIVE |
24797198 JD |
117 | static void handle_textdump_sigterm(int signal) |
118 | { | |
119 | quit = 1; | |
120 | lttng_destroy_session("test"); | |
121 | } | |
863dc485 | 122 | #endif |
24797198 | 123 | |
1fc22eb4 JD |
124 | void *refresh_thread(void *p) |
125 | { | |
126 | while (1) { | |
ae9e85c7 JD |
127 | if (quit) { |
128 | sem_post(&pause_sem); | |
129 | sem_post(&timer); | |
1402044a | 130 | sem_post(&end_trace_sem); |
ae9e85c7 | 131 | sem_post(&goodtodisplay); |
1402044a | 132 | sem_post(&goodtoupdate); |
ae9e85c7 JD |
133 | pthread_exit(0); |
134 | } | |
1402044a | 135 | if (!opt_input_path) { |
b1acd2b3 JD |
136 | #ifdef LTTNGTOP_MMAP_LIVE |
137 | mmap_live_flush(mmap_list); | |
138 | #endif | |
1402044a | 139 | } |
1fc22eb4 JD |
140 | sem_wait(&pause_sem); |
141 | sem_post(&pause_sem); | |
142 | sem_post(&timer); | |
143 | sleep(refresh_display/NSEC_PER_SEC); | |
144 | } | |
145 | } | |
146 | ||
147 | void *ncurses_display(void *p) | |
148 | { | |
149 | unsigned int current_display_index = 0; | |
150 | ||
151 | sem_wait(&bootstrap); | |
b332d28f JD |
152 | /* |
153 | * Prevent the 1 second delay when we hit ESC | |
154 | */ | |
155 | ESCDELAY = 0; | |
1fc22eb4 JD |
156 | init_ncurses(); |
157 | ||
158 | while (1) { | |
159 | sem_wait(&timer); | |
160 | sem_wait(&goodtodisplay); | |
161 | sem_wait(&pause_sem); | |
162 | ||
ae9e85c7 | 163 | if (quit) { |
33572a17 JD |
164 | sem_post(&pause_sem); |
165 | sem_post(&timer); | |
ae9e85c7 JD |
166 | reset_ncurses(); |
167 | pthread_exit(0); | |
168 | } | |
169 | ||
1fc22eb4 | 170 | copy = g_ptr_array_index(copies, current_display_index); |
85db4618 JD |
171 | assert(copy); |
172 | display(current_display_index++); | |
1fc22eb4 JD |
173 | |
174 | sem_post(&goodtoupdate); | |
175 | sem_post(&pause_sem); | |
1fc22eb4 JD |
176 | } |
177 | } | |
178 | ||
575cc4fd JD |
179 | void print_fields(struct bt_ctf_event *event) |
180 | { | |
181 | unsigned int cnt, i; | |
b1acd2b3 JD |
182 | const struct bt_definition *const * list; |
183 | const struct bt_declaration *l; | |
184 | const struct bt_definition *scope; | |
575cc4fd JD |
185 | enum ctf_type_id type; |
186 | const char *str; | |
187 | ||
188 | scope = bt_ctf_get_top_level_scope(event, BT_EVENT_FIELDS); | |
189 | ||
190 | bt_ctf_get_field_list(event, scope, &list, &cnt); | |
191 | for (i = 0; i < cnt; i++) { | |
192 | if (i != 0) | |
193 | printf(", "); | |
194 | printf("%s = ", bt_ctf_field_name(list[i])); | |
9498e78c JD |
195 | l = bt_ctf_get_decl_from_def(list[i]); |
196 | type = bt_ctf_field_type(l); | |
575cc4fd | 197 | if (type == CTF_TYPE_INTEGER) { |
9498e78c | 198 | if (bt_ctf_get_int_signedness(l) == 0) |
575cc4fd JD |
199 | printf("%" PRIu64 "", bt_ctf_get_uint64(list[i])); |
200 | else | |
201 | printf("%" PRId64 "", bt_ctf_get_int64(list[i])); | |
202 | } else if (type == CTF_TYPE_STRING) { | |
203 | printf("%s", bt_ctf_get_string(list[i])); | |
204 | } else if (type == CTF_TYPE_ARRAY) { | |
205 | str = bt_ctf_get_char_array(list[i]); | |
fbbda4da | 206 | if (!bt_ctf_field_get_error() && str) |
575cc4fd JD |
207 | printf("%s", str); |
208 | } | |
209 | } | |
210 | } | |
211 | ||
6112d0d4 JD |
212 | /* |
213 | * hook on each event to check the timestamp and refresh the display if | |
214 | * necessary | |
215 | */ | |
bca0e79c | 216 | enum bt_cb_ret textdump(struct bt_ctf_event *call_data, void *private_data) |
6112d0d4 JD |
217 | { |
218 | unsigned long timestamp; | |
010f485a | 219 | uint64_t delta; |
6112d0d4 | 220 | struct tm start; |
b520ab45 | 221 | uint64_t ts_nsec_start; |
bca0e79c | 222 | int pid, cpu_id, tid, ret, lookup, current_syscall = 0; |
b1acd2b3 | 223 | const struct bt_definition *scope; |
575cc4fd | 224 | const char *hostname, *procname; |
1d74c4d1 JD |
225 | struct cputime *cpu; |
226 | char *from_syscall = NULL; | |
6112d0d4 | 227 | |
6112d0d4 JD |
228 | timestamp = bt_ctf_get_timestamp(call_data); |
229 | ||
863dc485 JD |
230 | /* can happen in network live when tracing is idle */ |
231 | if (timestamp < last_event_ts) | |
232 | goto end_stop; | |
233 | ||
234 | last_event_ts = timestamp; | |
235 | ||
b520ab45 | 236 | start = format_timestamp(timestamp); |
6112d0d4 JD |
237 | ts_nsec_start = timestamp % NSEC_PER_SEC; |
238 | ||
32b70e07 | 239 | pid = get_context_pid(call_data); |
57bff788 | 240 | if (pid == -1ULL && opt_tid) { |
32b70e07 JD |
241 | goto error; |
242 | } | |
b0135e60 JD |
243 | |
244 | tid = get_context_tid(call_data); | |
32b70e07 | 245 | |
c8d75a13 | 246 | hostname = get_context_hostname(call_data); |
a15e6a10 JD |
247 | if (opt_child) |
248 | lookup = pid; | |
249 | else | |
250 | lookup = tid; | |
54761a45 | 251 | if (opt_tid || opt_hostname || opt_exec_name) { |
a15e6a10 | 252 | if (!lookup_filter_tid_list(lookup)) { |
b301de1a JD |
253 | /* To display when a process of ours in getting scheduled in */ |
254 | if (strcmp(bt_ctf_event_name(call_data), "sched_switch") == 0) { | |
255 | int next_tid; | |
256 | ||
257 | scope = bt_ctf_get_top_level_scope(call_data, | |
258 | BT_EVENT_FIELDS); | |
259 | next_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
260 | scope, "_next_tid")); | |
261 | if (bt_ctf_field_get_error()) { | |
262 | fprintf(stderr, "Missing next_tid field\n"); | |
263 | goto error; | |
264 | } | |
265 | if (!lookup_filter_tid_list(next_tid)) { | |
266 | goto end; | |
267 | } | |
268 | } else { | |
269 | goto end; | |
270 | } | |
271 | } | |
272 | } | |
da4353bb | 273 | |
bca0e79c JD |
274 | if (last_syscall && (strncmp(bt_ctf_event_name(call_data), |
275 | "exit_syscall", 12)) != 0) { | |
276 | last_syscall = NULL; | |
277 | printf(" ...interrupted...\n"); | |
278 | } | |
279 | ||
575cc4fd JD |
280 | cpu_id = get_cpu_id(call_data); |
281 | procname = get_context_comm(call_data); | |
1d74c4d1 JD |
282 | if (strncmp(bt_ctf_event_name(call_data), "sys_", 4) == 0) { |
283 | cpu = get_cpu(cpu_id); | |
284 | cpu->current_syscall = g_new0(struct syscall, 1); | |
285 | cpu->current_syscall->name = strdup(bt_ctf_event_name(call_data)); | |
286 | cpu->current_syscall->ts_start = timestamp; | |
bca0e79c JD |
287 | cpu->current_syscall->cpu_id = cpu_id; |
288 | last_syscall = cpu->current_syscall; | |
289 | current_syscall = 1; | |
1d74c4d1 JD |
290 | } else if ((strncmp(bt_ctf_event_name(call_data), "exit_syscall", 12)) == 0) { |
291 | struct tm start_ts; | |
292 | ||
bca0e79c JD |
293 | /* Return code of a syscall if it was the last displayed event. */ |
294 | if (last_syscall && last_syscall->ts_start == prev_ts) { | |
295 | if (last_syscall->cpu_id == cpu_id) { | |
296 | int64_t syscall_ret; | |
297 | ||
298 | delta = timestamp - last_syscall->ts_start; | |
299 | scope = bt_ctf_get_top_level_scope(call_data, | |
300 | BT_EVENT_FIELDS); | |
301 | syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
302 | scope, "_ret")); | |
303 | ||
304 | printf(" = %" PRId64 " (+%" PRIu64 ".%09" PRIu64 ")\n", | |
305 | syscall_ret, delta / NSEC_PER_SEC, | |
306 | delta % NSEC_PER_SEC); | |
307 | last_syscall = NULL; | |
308 | goto end; | |
309 | } else { | |
310 | last_syscall = NULL; | |
311 | printf(" ...interrupted...\n"); | |
312 | } | |
313 | } | |
314 | ||
1d74c4d1 JD |
315 | cpu = get_cpu(cpu_id); |
316 | if (cpu->current_syscall) { | |
317 | delta = timestamp - cpu->current_syscall->ts_start; | |
318 | start_ts = format_timestamp(cpu->current_syscall->ts_start); | |
54761a45 | 319 | ret = asprintf(&from_syscall, " [from %02d:%02d:%02d.%09" PRIu64 |
bca0e79c | 320 | " (+%" PRIu64 ".%09" PRIu64 ") (cpu %d) %s]", |
1d74c4d1 JD |
321 | start_ts.tm_hour, start_ts.tm_min, start_ts.tm_sec, |
322 | cpu->current_syscall->ts_start % NSEC_PER_SEC, | |
323 | delta / NSEC_PER_SEC, delta % NSEC_PER_SEC, | |
324 | cpu_id, cpu->current_syscall->name); | |
54761a45 JD |
325 | if (ret < 0) { |
326 | goto error; | |
327 | } | |
1d74c4d1 JD |
328 | free(cpu->current_syscall->name); |
329 | g_free(cpu->current_syscall); | |
330 | cpu->current_syscall = NULL; | |
bca0e79c | 331 | last_syscall = NULL; |
1d74c4d1 JD |
332 | } |
333 | } | |
334 | ||
010f485a JD |
335 | if (prev_ts == 0) |
336 | prev_ts = timestamp; | |
337 | delta = timestamp - prev_ts; | |
338 | prev_ts = timestamp; | |
575cc4fd | 339 | |
4251cc67 JD |
340 | printf("%02d:%02d:%02d.%09" PRIu64 " (+%" PRIu64 ".%09" PRIu64 ") %s%s" |
341 | "(cpu %d) [%s (%d/%d)] %s (", | |
342 | start.tm_hour, start.tm_min, start.tm_sec, | |
343 | ts_nsec_start, delta / NSEC_PER_SEC, | |
344 | delta % NSEC_PER_SEC, (hostname) ? hostname : "", | |
345 | (hostname) ? " ": "", cpu_id, procname, pid, tid, | |
346 | bt_ctf_event_name(call_data)); | |
7c15d14a | 347 | print_fields(call_data); |
bca0e79c JD |
348 | printf(")%s%c", (from_syscall) ? from_syscall : "", |
349 | (current_syscall) ? '\0' : '\n'); | |
1d74c4d1 JD |
350 | |
351 | free(from_syscall); | |
6112d0d4 | 352 | |
32b70e07 | 353 | end: |
6112d0d4 | 354 | return BT_CB_OK; |
32b70e07 JD |
355 | error: |
356 | return BT_CB_ERROR_STOP; | |
863dc485 JD |
357 | end_stop: |
358 | return BT_CB_OK_STOP; | |
6112d0d4 JD |
359 | } |
360 | ||
246d5992 JD |
361 | enum bt_cb_ret handle_kprobes(struct bt_ctf_event *call_data, void *private_data) |
362 | { | |
363 | int i; | |
364 | struct kprobes *kprobe; | |
365 | ||
366 | /* for kprobes */ | |
367 | for (i = 0; i < lttngtop.kprobes_table->len; i++) { | |
368 | kprobe = g_ptr_array_index(lttngtop.kprobes_table, i); | |
369 | if (strcmp(bt_ctf_event_name(call_data), kprobe->probe_name) == 0) { | |
370 | kprobe->count++; | |
371 | } | |
372 | } | |
373 | ||
374 | return BT_CB_OK; | |
375 | } | |
376 | ||
1fc22eb4 JD |
377 | /* |
378 | * hook on each event to check the timestamp and refresh the display if | |
379 | * necessary | |
380 | */ | |
4adc8274 | 381 | enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data) |
1fc22eb4 JD |
382 | { |
383 | unsigned long timestamp; | |
384 | ||
c78f2cdc | 385 | timestamp = bt_ctf_get_timestamp(call_data); |
1fc22eb4 JD |
386 | if (timestamp == -1ULL) |
387 | goto error; | |
388 | ||
863dc485 JD |
389 | /* can happen in network live when tracing is idle */ |
390 | if (timestamp < last_event_ts) | |
391 | goto end_stop; | |
392 | ||
393 | last_event_ts = timestamp; | |
394 | ||
1fc22eb4 JD |
395 | if (last_display_update == 0) |
396 | last_display_update = timestamp; | |
397 | ||
398 | if (timestamp - last_display_update >= refresh_display) { | |
399 | sem_wait(&goodtoupdate); | |
400 | g_ptr_array_add(copies, get_copy_lttngtop(last_display_update, | |
401 | timestamp)); | |
402 | sem_post(&goodtodisplay); | |
403 | sem_post(&bootstrap); | |
404 | last_display_update = timestamp; | |
405 | } | |
406 | return BT_CB_OK; | |
407 | ||
408 | error: | |
409 | fprintf(stderr, "check_timestamp callback error\n"); | |
410 | return BT_CB_ERROR_STOP; | |
863dc485 JD |
411 | |
412 | end_stop: | |
413 | return BT_CB_OK_STOP; | |
1fc22eb4 JD |
414 | } |
415 | ||
416 | /* | |
417 | * get_perf_counter : get or create and return a perf_counter struct for | |
418 | * either a process or a cpu (only one of the 2 parameters mandatory) | |
419 | */ | |
420 | struct perfcounter *get_perf_counter(const char *name, struct processtop *proc, | |
421 | struct cputime *cpu) | |
422 | { | |
bb6d72a2 | 423 | struct perfcounter *ret; |
1fc22eb4 JD |
424 | GHashTable *table; |
425 | ||
426 | if (proc) | |
427 | table = proc->perf; | |
428 | else if (cpu) | |
429 | table = cpu->perf; | |
430 | else | |
431 | goto error; | |
432 | ||
433 | ret = g_hash_table_lookup(table, (gpointer) name); | |
434 | if (ret) | |
435 | goto end; | |
436 | ||
559c9f86 | 437 | ret = g_new0(struct perfcounter, 1); |
1fc22eb4 JD |
438 | /* by default, make it visible in the UI */ |
439 | ret->visible = 1; | |
85db4618 | 440 | g_hash_table_insert(table, (gpointer) strdup(name), ret); |
1fc22eb4 | 441 | |
1fc22eb4 JD |
442 | end: |
443 | return ret; | |
444 | ||
445 | error: | |
446 | return NULL; | |
447 | } | |
448 | ||
449 | void update_perf_value(struct processtop *proc, struct cputime *cpu, | |
450 | const char *name, int value) | |
451 | { | |
452 | struct perfcounter *cpu_perf, *process_perf; | |
453 | ||
454 | cpu_perf = get_perf_counter(name, NULL, cpu); | |
455 | if (cpu_perf->count < value) { | |
456 | process_perf = get_perf_counter(name, proc, NULL); | |
457 | process_perf->count += value - cpu_perf->count; | |
458 | cpu_perf->count = value; | |
459 | } | |
460 | } | |
461 | ||
4adc8274 | 462 | void extract_perf_counter_scope(const struct bt_ctf_event *event, |
2e0a1190 | 463 | const struct bt_definition *scope, |
1fc22eb4 JD |
464 | struct processtop *proc, |
465 | struct cputime *cpu) | |
466 | { | |
2e0a1190 JD |
467 | struct bt_definition const * const *list = NULL; |
468 | const struct bt_definition *field; | |
1fc22eb4 | 469 | unsigned int count; |
bb6d72a2 JD |
470 | struct perfcounter *perfcounter; |
471 | GHashTableIter iter; | |
472 | gpointer key; | |
473 | int ret; | |
1fc22eb4 JD |
474 | |
475 | if (!scope) | |
476 | goto end; | |
477 | ||
478 | ret = bt_ctf_get_field_list(event, scope, &list, &count); | |
479 | if (ret < 0) | |
480 | goto end; | |
481 | ||
bb6d72a2 JD |
482 | if (count == 0) |
483 | goto end; | |
484 | ||
485 | g_hash_table_iter_init(&iter, global_perf_liszt); | |
486 | while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) { | |
487 | field = bt_ctf_get_field(event, scope, (char *) key); | |
488 | if (field) { | |
489 | int value = bt_ctf_get_uint64(field); | |
1fc22eb4 JD |
490 | if (bt_ctf_field_get_error()) |
491 | continue; | |
bb6d72a2 | 492 | update_perf_value(proc, cpu, (char *) key, value); |
1fc22eb4 JD |
493 | } |
494 | } | |
495 | ||
496 | end: | |
497 | return; | |
498 | } | |
499 | ||
4adc8274 | 500 | void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event) |
1fc22eb4 | 501 | { |
1fc22eb4 | 502 | struct cputime *cpu; |
2e0a1190 | 503 | const struct bt_definition *scope; |
1fc22eb4 | 504 | |
d67167cd | 505 | cpu = get_cpu(get_cpu_id(event)); |
1fc22eb4 JD |
506 | |
507 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
508 | extract_perf_counter_scope(event, scope, proc, cpu); | |
509 | ||
510 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT); | |
511 | extract_perf_counter_scope(event, scope, proc, cpu); | |
512 | ||
513 | scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT); | |
514 | extract_perf_counter_scope(event, scope, proc, cpu); | |
1fc22eb4 JD |
515 | } |
516 | ||
4adc8274 | 517 | enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data, |
1fc22eb4 JD |
518 | void *private_data) |
519 | { | |
1402044a | 520 | int pid, tid, ppid, vpid, vtid, vppid; |
c8d75a13 | 521 | char *comm, *hostname; |
1fc22eb4 | 522 | struct processtop *parent, *child; |
1fc22eb4 JD |
523 | unsigned long timestamp; |
524 | ||
c78f2cdc | 525 | timestamp = bt_ctf_get_timestamp(call_data); |
1fc22eb4 JD |
526 | if (timestamp == -1ULL) |
527 | goto error; | |
528 | ||
1dec520a JD |
529 | pid = get_context_pid(call_data); |
530 | if (pid == -1ULL) { | |
1fc22eb4 JD |
531 | goto error; |
532 | } | |
96aa77de | 533 | |
1dec520a JD |
534 | tid = get_context_tid(call_data); |
535 | if (tid == -1ULL) { | |
1fc22eb4 JD |
536 | goto error; |
537 | } | |
1dec520a JD |
538 | ppid = get_context_ppid(call_data); |
539 | if (ppid == -1ULL) { | |
1fc22eb4 JD |
540 | goto error; |
541 | } | |
1402044a JD |
542 | vpid = get_context_vpid(call_data); |
543 | if (pid == -1ULL) { | |
544 | vpid = -1; | |
545 | } | |
546 | vtid = get_context_vtid(call_data); | |
547 | if (tid == -1ULL) { | |
548 | vtid = -1; | |
549 | } | |
550 | vppid = get_context_vppid(call_data); | |
551 | if (ppid == -1ULL) { | |
552 | vppid = -1; | |
553 | } | |
1dec520a JD |
554 | comm = get_context_comm(call_data); |
555 | if (!comm) { | |
1fc22eb4 JD |
556 | goto error; |
557 | } | |
c8d75a13 JD |
558 | /* optional */ |
559 | hostname = get_context_hostname(call_data); | |
1fc22eb4 JD |
560 | |
561 | /* find or create the current process */ | |
562 | child = find_process_tid(<tngtop, tid, comm); | |
563 | if (!child) | |
906c08f6 | 564 | child = add_proc(<tngtop, tid, comm, timestamp, hostname); |
96aa77de JD |
565 | if (!child) |
566 | goto end; | |
c8d75a13 | 567 | update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm, hostname); |
1fc22eb4 JD |
568 | |
569 | if (pid != tid) { | |
570 | /* find or create the parent */ | |
571 | parent = find_process_tid(<tngtop, pid, comm); | |
572 | if (!parent) { | |
906c08f6 | 573 | parent = add_proc(<tngtop, pid, comm, timestamp, hostname); |
96aa77de JD |
574 | if (parent) |
575 | parent->pid = pid; | |
1fc22eb4 JD |
576 | } |
577 | ||
578 | /* attach the parent to the current process */ | |
579 | child->threadparent = parent; | |
580 | add_thread(parent, child); | |
581 | } | |
582 | ||
583 | update_perf_counter(child, call_data); | |
584 | ||
96aa77de | 585 | end: |
1fc22eb4 JD |
586 | return BT_CB_OK; |
587 | ||
588 | error: | |
589 | return BT_CB_ERROR_STOP; | |
590 | } | |
591 | ||
592 | void init_lttngtop() | |
593 | { | |
594 | copies = g_ptr_array_new(); | |
0d91c12a | 595 | global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal); |
da4353bb | 596 | global_filter_list = g_hash_table_new(g_str_hash, g_str_equal); |
114cae59 | 597 | global_host_list = g_hash_table_new(g_str_hash, g_str_equal); |
54761a45 JD |
598 | tid_filter_list = g_hash_table_new(g_str_hash, |
599 | g_str_equal); | |
1fc22eb4 JD |
600 | |
601 | sem_init(&goodtodisplay, 0, 0); | |
602 | sem_init(&goodtoupdate, 0, 1); | |
603 | sem_init(&timer, 0, 1); | |
604 | sem_init(&bootstrap, 0, 0); | |
605 | sem_init(&pause_sem, 0, 1); | |
606 | sem_init(&end_trace_sem, 0, 0); | |
607 | ||
e05a35a6 JD |
608 | reset_global_counters(); |
609 | lttngtop.nbproc = 0; | |
610 | lttngtop.nbthreads = 0; | |
611 | lttngtop.nbfiles = 0; | |
612 | ||
30b646c4 JD |
613 | lttngtop.process_hash_table = g_hash_table_new(g_direct_hash, |
614 | g_direct_equal); | |
1fc22eb4 JD |
615 | lttngtop.process_table = g_ptr_array_new(); |
616 | lttngtop.files_table = g_ptr_array_new(); | |
617 | lttngtop.cpu_table = g_ptr_array_new(); | |
da4353bb JD |
618 | |
619 | toggle_filter = -1; | |
1fc22eb4 JD |
620 | } |
621 | ||
c263c4eb | 622 | void usage(FILE *fp) |
1fc22eb4 | 623 | { |
c263c4eb | 624 | fprintf(fp, "LTTngTop %s\n\n", VERSION); |
d2f1452b JD |
625 | fprintf(fp, "Usage : lttngtop [OPTIONS] TRACE\n"); |
626 | fprintf(fp, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n"); | |
c8d75a13 JD |
627 | fprintf(fp, " -h, --help This help message\n"); |
628 | fprintf(fp, " -t, --textdump Display live events in text-only\n"); | |
629 | fprintf(fp, " -p, --pid Comma-separated list of PIDs to display\n"); | |
630 | fprintf(fp, " -f, --child Follow threads associated with selected PIDs\n"); | |
631 | fprintf(fp, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n"); | |
241ba90a | 632 | fprintf(fp, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n"); |
bab469fc JD |
633 | fprintf(fp, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n"); |
634 | fprintf(fp, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n"); | |
1fc22eb4 JD |
635 | } |
636 | ||
f16e36fc JD |
637 | /* |
638 | * Parse probe options. | |
639 | * Shamelessly stolen from lttng-tools : | |
640 | * src/bin/lttng/commands/enable_events.c | |
641 | */ | |
642 | static struct kprobes *parse_probe_opts(char *opt) | |
643 | { | |
644 | char s_hex[19]; | |
645 | char name[LTTNG_SYMBOL_NAME_LEN]; | |
646 | struct kprobes *kprobe; | |
647 | int ret; | |
648 | ||
649 | /* | |
650 | kprobe->probe_addr = 0; | |
651 | kprobe->probe_offset = 0; | |
652 | asprintf(&kprobe->probe_name, "probe_sys_open"); | |
653 | asprintf(&kprobe->symbol_name, "sys_open"); | |
654 | */ | |
655 | ||
656 | if (opt == NULL) { | |
657 | kprobe = NULL; | |
658 | goto end; | |
659 | } | |
660 | ||
661 | kprobe = g_new0(struct kprobes, 1); | |
662 | ||
663 | /* Check for symbol+offset */ | |
664 | ret = sscanf(opt, "%[^'+']+%s", name, s_hex); | |
665 | if (ret == 2) { | |
b1acd2b3 JD |
666 | ret = asprintf(&kprobe->probe_name, "probe_%s", name); |
667 | ret = asprintf(&kprobe->symbol_name, "%s", name); | |
f16e36fc JD |
668 | |
669 | if (strlen(s_hex) == 0) { | |
670 | fprintf(stderr, "Invalid probe offset %s", s_hex); | |
671 | ret = -1; | |
672 | goto end; | |
673 | } | |
674 | kprobe->probe_offset = strtoul(s_hex, NULL, 0); | |
675 | kprobe->probe_addr = 0; | |
676 | goto end; | |
677 | } | |
678 | ||
679 | /* Check for symbol */ | |
680 | if (isalpha(name[0])) { | |
681 | ret = sscanf(opt, "%s", name); | |
682 | if (ret == 1) { | |
b1acd2b3 JD |
683 | ret = asprintf(&kprobe->probe_name, "probe_%s", name); |
684 | ret = asprintf(&kprobe->symbol_name, "%s", name); | |
f16e36fc JD |
685 | kprobe->probe_offset = 0; |
686 | kprobe->probe_addr = 0; | |
687 | goto end; | |
688 | } | |
689 | } | |
690 | ||
691 | /* Check for address */ | |
692 | ret = sscanf(opt, "%s", s_hex); | |
693 | if (ret > 0) { | |
694 | if (strlen(s_hex) == 0) { | |
695 | fprintf(stderr, "Invalid probe address %s", s_hex); | |
696 | ret = -1; | |
697 | goto end; | |
698 | } | |
b1acd2b3 | 699 | ret = asprintf(&kprobe->probe_name, "probe_%s", s_hex); |
f16e36fc JD |
700 | kprobe->probe_offset = 0; |
701 | kprobe->probe_addr = strtoul(s_hex, NULL, 0); | |
702 | goto end; | |
703 | } | |
704 | ||
705 | /* No match */ | |
706 | kprobe = NULL; | |
707 | ||
708 | end: | |
709 | return kprobe; | |
710 | } | |
711 | ||
1fc22eb4 JD |
712 | /* |
713 | * Return 0 if caller should continue, < 0 if caller should return | |
714 | * error, > 0 if caller should exit without reporting error. | |
715 | */ | |
716 | static int parse_options(int argc, char **argv) | |
717 | { | |
718 | poptContext pc; | |
719 | int opt, ret = 0; | |
57bff788 JD |
720 | char *tmp_str; |
721 | int *tid; | |
54761a45 | 722 | int i; |
1fc22eb4 | 723 | |
b1acd2b3 JD |
724 | remote_live = 0; |
725 | ||
1fc22eb4 JD |
726 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
727 | poptReadDefaultConfig(pc, 0); | |
728 | ||
729 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
730 | switch (opt) { | |
731 | case OPT_HELP: | |
732 | usage(stdout); | |
733 | ret = 1; /* exit cleanly */ | |
734 | goto end; | |
6777529d JD |
735 | case OPT_TEXTDUMP: |
736 | opt_textdump = 1; | |
32b70e07 JD |
737 | break; |
738 | case OPT_CHILD: | |
32b70e07 JD |
739 | opt_child = 1; |
740 | break; | |
741 | case OPT_PID: | |
da4353bb | 742 | toggle_filter = 1; |
57bff788 JD |
743 | tmp_str = strtok(opt_tid, ","); |
744 | while (tmp_str) { | |
745 | tid = malloc(sizeof(int)); | |
746 | *tid = atoi(tmp_str); | |
ea5d1dc9 | 747 | g_hash_table_insert(tid_filter_list, |
c8d75a13 JD |
748 | (gpointer) tid, tid); |
749 | tmp_str = strtok(NULL, ","); | |
750 | } | |
751 | break; | |
3160c7a9 JD |
752 | case OPT_BEGIN: |
753 | /* start reading the live trace from the beginning */ | |
754 | opt_begin = 1; | |
755 | break; | |
c8d75a13 | 756 | case OPT_HOSTNAME: |
da4353bb | 757 | toggle_filter = 1; |
c8d75a13 JD |
758 | tmp_str = strtok(opt_hostname, ","); |
759 | while (tmp_str) { | |
467097ac | 760 | add_hostname_list(tmp_str, 1); |
57bff788 JD |
761 | tmp_str = strtok(NULL, ","); |
762 | } | |
32b70e07 | 763 | break; |
b1acd2b3 JD |
764 | case OPT_RELAY_HOSTNAME: |
765 | remote_live = 1; | |
766 | break; | |
f16e36fc JD |
767 | case OPT_KPROBES: |
768 | lttngtop.kprobes_table = g_ptr_array_new(); | |
769 | tmp_str = strtok(opt_kprobes, ","); | |
770 | while (tmp_str) { | |
771 | struct kprobes *kprobe; | |
772 | ||
773 | kprobe = parse_probe_opts(tmp_str); | |
774 | if (kprobe) { | |
775 | g_ptr_array_add( | |
776 | lttngtop.kprobes_table, | |
777 | kprobe); | |
778 | } else { | |
779 | ret = -EINVAL; | |
780 | goto end; | |
781 | } | |
782 | tmp_str = strtok(NULL, ","); | |
783 | } | |
784 | break; | |
1fc22eb4 JD |
785 | default: |
786 | ret = -EINVAL; | |
787 | goto end; | |
788 | } | |
789 | } | |
790 | ||
54761a45 JD |
791 | opt_exec_name = NULL; |
792 | opt_exec_argv = NULL; | |
793 | for (i = 0; i < argc; i++) { | |
794 | if (argv[i][0] == '-' && argv[i][1] == '-') { | |
795 | opt_exec_name = argv[i + 1]; | |
796 | opt_exec_argv = &argv[i + 1]; | |
797 | break; | |
798 | } | |
799 | } | |
800 | if (!opt_exec_name) { | |
801 | opt_input_path = poptGetArg(pc); | |
802 | } | |
16b22a0f | 803 | |
1fc22eb4 JD |
804 | end: |
805 | if (pc) { | |
806 | poptFreeContext(pc); | |
807 | } | |
808 | return ret; | |
809 | } | |
810 | ||
811 | void iter_trace(struct bt_context *bt_ctx) | |
812 | { | |
813 | struct bt_ctf_iter *iter; | |
814 | struct bt_iter_pos begin_pos; | |
246d5992 | 815 | struct kprobes *kprobe; |
4adc8274 | 816 | const struct bt_ctf_event *event; |
246d5992 | 817 | int i; |
1fc22eb4 JD |
818 | int ret = 0; |
819 | ||
820 | begin_pos.type = BT_SEEK_BEGIN; | |
821 | iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL); | |
822 | ||
da4353bb JD |
823 | /* at each event, verify the status of the process table */ |
824 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
825 | fix_process_table, | |
826 | NULL, NULL, NULL); | |
6e11e0d0 JD |
827 | /* to handle the follow child option */ |
828 | bt_ctf_iter_add_callback(iter, | |
829 | g_quark_from_static_string("sched_process_fork"), | |
830 | NULL, 0, handle_sched_process_fork, NULL, NULL, NULL); | |
54761a45 JD |
831 | /* to clean up the process table */ |
832 | bt_ctf_iter_add_callback(iter, | |
833 | g_quark_from_static_string("sched_process_free"), | |
834 | NULL, 0, handle_sched_process_free, NULL, NULL, NULL); | |
6777529d JD |
835 | if (opt_textdump) { |
836 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
bca0e79c | 837 | textdump, |
6777529d JD |
838 | NULL, NULL, NULL); |
839 | } else { | |
840 | /* at each event check if we need to refresh */ | |
841 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, | |
842 | check_timestamp, | |
843 | NULL, NULL, NULL); | |
6777529d JD |
844 | /* to handle the scheduling events */ |
845 | bt_ctf_iter_add_callback(iter, | |
846 | g_quark_from_static_string("sched_switch"), | |
847 | NULL, 0, handle_sched_switch, NULL, NULL, NULL); | |
6777529d JD |
848 | /* to get all the process from the statedumps */ |
849 | bt_ctf_iter_add_callback(iter, | |
850 | g_quark_from_static_string( | |
851 | "lttng_statedump_process_state"), | |
852 | NULL, 0, handle_statedump_process_state, | |
853 | NULL, NULL, NULL); | |
854 | ||
855 | /* for IO top */ | |
856 | bt_ctf_iter_add_callback(iter, | |
857 | g_quark_from_static_string("exit_syscall"), | |
858 | NULL, 0, handle_exit_syscall, NULL, NULL, NULL); | |
859 | bt_ctf_iter_add_callback(iter, | |
860 | g_quark_from_static_string("sys_write"), | |
861 | NULL, 0, handle_sys_write, NULL, NULL, NULL); | |
862 | bt_ctf_iter_add_callback(iter, | |
863 | g_quark_from_static_string("sys_read"), | |
864 | NULL, 0, handle_sys_read, NULL, NULL, NULL); | |
865 | bt_ctf_iter_add_callback(iter, | |
866 | g_quark_from_static_string("sys_open"), | |
867 | NULL, 0, handle_sys_open, NULL, NULL, NULL); | |
868 | bt_ctf_iter_add_callback(iter, | |
869 | g_quark_from_static_string("sys_close"), | |
870 | NULL, 0, handle_sys_close, NULL, NULL, NULL); | |
871 | bt_ctf_iter_add_callback(iter, | |
872 | g_quark_from_static_string( | |
ceb3a221 | 873 | "lttng_statedump_file_descriptor"), |
6777529d JD |
874 | NULL, 0, handle_statedump_file_descriptor, |
875 | NULL, NULL, NULL); | |
246d5992 JD |
876 | |
877 | /* for kprobes */ | |
da4353bb JD |
878 | if (lttngtop.kprobes_table) { |
879 | for (i = 0; i < lttngtop.kprobes_table->len; i++) { | |
880 | kprobe = g_ptr_array_index(lttngtop.kprobes_table, i); | |
881 | bt_ctf_iter_add_callback(iter, | |
882 | g_quark_from_static_string( | |
883 | kprobe->probe_name), | |
884 | NULL, 0, handle_kprobes, | |
885 | NULL, NULL, NULL); | |
886 | } | |
246d5992 | 887 | } |
6777529d JD |
888 | } |
889 | ||
54761a45 JD |
890 | if (opt_exec_name) { |
891 | pid_t pid; | |
892 | ||
893 | pid = fork(); | |
894 | if (pid == 0) { | |
895 | execvpe(opt_exec_name, opt_exec_argv, opt_exec_env); | |
896 | exit(EXIT_SUCCESS); | |
897 | } else if (pid > 0) { | |
898 | opt_exec_pid = pid; | |
899 | g_hash_table_insert(tid_filter_list, | |
900 | (gpointer) &pid, | |
901 | &pid); | |
902 | } else { | |
903 | perror("fork"); | |
904 | exit(EXIT_FAILURE); | |
905 | } | |
906 | } | |
907 | ||
fbbda4da | 908 | while ((event = bt_ctf_iter_read_event(iter)) != NULL) { |
050f9cf9 | 909 | if (quit || reload_trace) |
ae9e85c7 | 910 | goto end_iter; |
1fc22eb4 JD |
911 | ret = bt_iter_next(bt_ctf_get_iter(iter)); |
912 | if (ret < 0) | |
913 | goto end_iter; | |
914 | } | |
915 | ||
916 | /* block until quit, we reached the end of the trace */ | |
917 | sem_wait(&end_trace_sem); | |
918 | ||
919 | end_iter: | |
06570214 | 920 | bt_ctf_iter_destroy(iter); |
1fc22eb4 JD |
921 | } |
922 | ||
923 | /* | |
924 | * bt_context_add_traces_recursive: Open a trace recursively | |
925 | * (copied from BSD code in converter/babeltrace.c) | |
926 | * | |
927 | * Find each trace present in the subdirectory starting from the given | |
928 | * path, and add them to the context. The packet_seek parameter can be | |
929 | * NULL: this specify to use the default format packet_seek. | |
930 | * | |
931 | * Return: 0 on success, nonzero on failure. | |
932 | * Unable to open toplevel: failure. | |
933 | * Unable to open some subdirectory or file: warn and continue; | |
934 | */ | |
935 | int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path, | |
936 | const char *format_str, | |
2e0a1190 | 937 | void (*packet_seek)(struct bt_stream_pos *pos, |
1fc22eb4 JD |
938 | size_t offset, int whence)) |
939 | { | |
940 | FTS *tree; | |
941 | FTSENT *node; | |
942 | GArray *trace_ids; | |
943 | char lpath[PATH_MAX]; | |
944 | char * const paths[2] = { lpath, NULL }; | |
0d567cf1 | 945 | int ret = -1; |
1fc22eb4 | 946 | |
12a91e9d JD |
947 | if ((strncmp(path, NET4_URL_PREFIX, sizeof(NET4_URL_PREFIX) - 1)) == 0 || |
948 | (strncmp(path, NET6_URL_PREFIX, sizeof(NET6_URL_PREFIX) - 1)) == 0 || | |
949 | (strncmp(path, NET_URL_PREFIX, sizeof(NET_URL_PREFIX) - 1)) == 0) { | |
950 | ret = bt_context_add_trace(ctx, | |
951 | path, format_str, packet_seek, NULL, NULL); | |
952 | if (ret < 0) { | |
953 | fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" " | |
954 | "for reading.\n", path); | |
955 | /* Allow to skip erroneous traces. */ | |
956 | ret = 1; /* partial error */ | |
957 | } | |
958 | return ret; | |
959 | } | |
1fc22eb4 JD |
960 | /* |
961 | * Need to copy path, because fts_open can change it. | |
962 | * It is the pointer array, not the strings, that are constant. | |
963 | */ | |
964 | strncpy(lpath, path, PATH_MAX); | |
965 | lpath[PATH_MAX - 1] = '\0'; | |
966 | ||
967 | tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0); | |
968 | if (tree == NULL) { | |
969 | fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n", | |
970 | path); | |
971 | return -EINVAL; | |
972 | } | |
973 | ||
974 | trace_ids = g_array_new(FALSE, TRUE, sizeof(int)); | |
975 | ||
976 | while ((node = fts_read(tree))) { | |
977 | int dirfd, metafd; | |
978 | ||
979 | if (!(node->fts_info & FTS_D)) | |
980 | continue; | |
981 | ||
982 | dirfd = open(node->fts_accpath, 0); | |
983 | if (dirfd < 0) { | |
984 | fprintf(stderr, "[error] [Context] Unable to open trace " | |
985 | "directory file descriptor.\n"); | |
986 | ret = dirfd; | |
987 | goto error; | |
988 | } | |
989 | metafd = openat(dirfd, "metadata", O_RDONLY); | |
990 | if (metafd < 0) { | |
7314c855 | 991 | close(dirfd); |
7314c855 | 992 | continue; |
1fc22eb4 JD |
993 | } else { |
994 | int trace_id; | |
995 | ||
996 | ret = close(metafd); | |
997 | if (ret < 0) { | |
998 | perror("close"); | |
999 | goto error; | |
1000 | } | |
1001 | ret = close(dirfd); | |
1002 | if (ret < 0) { | |
1003 | perror("close"); | |
1004 | goto error; | |
1005 | } | |
1006 | ||
1007 | trace_id = bt_context_add_trace(ctx, | |
1008 | node->fts_accpath, format_str, | |
1009 | packet_seek, NULL, NULL); | |
1010 | if (trace_id < 0) { | |
0d567cf1 | 1011 | fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s " |
1fc22eb4 | 1012 | "for reading.\n", node->fts_accpath, path); |
0d567cf1 JD |
1013 | /* Allow to skip erroneous traces. */ |
1014 | continue; | |
1fc22eb4 JD |
1015 | } |
1016 | g_array_append_val(trace_ids, trace_id); | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | g_array_free(trace_ids, TRUE); | |
0d567cf1 | 1021 | return ret; |
1fc22eb4 JD |
1022 | |
1023 | error: | |
1024 | return ret; | |
1025 | } | |
1026 | ||
4553b4d1 | 1027 | static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list, |
a54d0d2c JD |
1028 | int field_cnt, int *tid_check, int *pid_check, |
1029 | int *procname_check, int *ppid_check) | |
1030 | { | |
1031 | int j; | |
bb6d72a2 JD |
1032 | struct perfcounter *global; |
1033 | const char *name; | |
a54d0d2c JD |
1034 | |
1035 | for (j = 0; j < field_cnt; j++) { | |
bb6d72a2 | 1036 | name = bt_ctf_get_decl_field_name(field_list[j]); |
a54d0d2c | 1037 | if (*tid_check == 0) { |
bb6d72a2 | 1038 | if (strncmp(name, "tid", 3) == 0) |
a54d0d2c | 1039 | (*tid_check)++; |
a54d0d2c JD |
1040 | } |
1041 | if (*pid_check == 0) { | |
da401e9c | 1042 | if (strncmp(name, "pid", 3) == 0) |
a54d0d2c JD |
1043 | (*pid_check)++; |
1044 | } | |
1045 | if (*ppid_check == 0) { | |
bb6d72a2 | 1046 | if (strncmp(name, "ppid", 4) == 0) |
a54d0d2c JD |
1047 | (*ppid_check)++; |
1048 | } | |
1049 | if (*procname_check == 0) { | |
bb6d72a2 | 1050 | if (strncmp(name, "procname", 8) == 0) |
a54d0d2c JD |
1051 | (*procname_check)++; |
1052 | } | |
bb6d72a2 JD |
1053 | if (strncmp(name, "perf_", 5) == 0) { |
1054 | global = g_hash_table_lookup(global_perf_liszt, (gpointer) name); | |
1055 | if (!global) { | |
1056 | global = g_new0(struct perfcounter, 1); | |
1057 | /* by default, sort on the first perf context */ | |
1058 | if (g_hash_table_size(global_perf_liszt) == 0) | |
1059 | global->sort = 1; | |
1060 | global->visible = 1; | |
1061 | g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global); | |
1062 | } | |
1063 | } | |
a54d0d2c | 1064 | } |
bb6d72a2 | 1065 | |
a54d0d2c JD |
1066 | if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 && |
1067 | *procname_check == 1) | |
1068 | return 0; | |
1069 | ||
1070 | return -1; | |
1071 | } | |
1072 | ||
1073 | /* | |
1074 | * check_requirements: check if the required context informations are available | |
1075 | * | |
1076 | * If each mandatory context information is available for at least in one | |
1077 | * event, return 0 otherwise return -1. | |
a54d0d2c JD |
1078 | */ |
1079 | int check_requirements(struct bt_context *ctx) | |
1080 | { | |
1081 | unsigned int i, evt_cnt, field_cnt; | |
1082 | struct bt_ctf_event_decl *const * evt_list; | |
1083 | const struct bt_ctf_field_decl *const * field_list; | |
1084 | int tid_check = 0; | |
1085 | int pid_check = 0; | |
1086 | int procname_check = 0; | |
1087 | int ppid_check = 0; | |
1088 | int ret = 0; | |
1089 | ||
b1acd2b3 JD |
1090 | ret = bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt); |
1091 | if (ret < 0) { | |
1092 | goto end; | |
1093 | } | |
1094 | ||
a54d0d2c JD |
1095 | for (i = 0; i < evt_cnt; i++) { |
1096 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT, | |
1097 | &field_list, &field_cnt); | |
1098 | ret = check_field_requirements(field_list, field_cnt, | |
1099 | &tid_check, &pid_check, &procname_check, | |
1100 | &ppid_check); | |
a54d0d2c JD |
1101 | |
1102 | bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT, | |
1103 | &field_list, &field_cnt); | |
1104 | ret = check_field_requirements(field_list, field_cnt, | |
1105 | &tid_check, &pid_check, &procname_check, | |
1106 | &ppid_check); | |
a54d0d2c JD |
1107 | |
1108 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT, | |
1109 | &field_list, &field_cnt); | |
1110 | ret = check_field_requirements(field_list, field_cnt, | |
1111 | &tid_check, &pid_check, &procname_check, | |
1112 | &ppid_check); | |
a54d0d2c JD |
1113 | } |
1114 | ||
1115 | if (tid_check == 0) { | |
1116 | ret = -1; | |
1117 | fprintf(stderr, "[error] missing tid context information\n"); | |
1118 | } | |
1119 | if (pid_check == 0) { | |
1120 | ret = -1; | |
1121 | fprintf(stderr, "[error] missing pid context information\n"); | |
1122 | } | |
1123 | if (ppid_check == 0) { | |
1124 | ret = -1; | |
1125 | fprintf(stderr, "[error] missing ppid context information\n"); | |
1126 | } | |
1127 | if (procname_check == 0) { | |
1128 | ret = -1; | |
1129 | fprintf(stderr, "[error] missing procname context information\n"); | |
1130 | } | |
1131 | ||
16b22a0f JD |
1132 | end: |
1133 | return ret; | |
1134 | } | |
1135 | ||
54761a45 JD |
1136 | static void handle_sigchild(int signal) |
1137 | { | |
1138 | int status; | |
1139 | ||
1140 | waitpid(opt_exec_pid, &status, 0); | |
1141 | } | |
1142 | ||
1143 | int main(int argc, char **argv, char **envp) | |
1fc22eb4 JD |
1144 | { |
1145 | int ret; | |
1146 | struct bt_context *bt_ctx = NULL; | |
1147 | ||
71ba9228 | 1148 | //babeltrace_verbose = 1; |
957eed46 | 1149 | init_lttngtop(); |
1fc22eb4 JD |
1150 | ret = parse_options(argc, argv); |
1151 | if (ret < 0) { | |
1152 | fprintf(stdout, "Error parsing options.\n\n"); | |
1153 | usage(stdout); | |
1154 | exit(EXIT_FAILURE); | |
1155 | } else if (ret > 0) { | |
1156 | exit(EXIT_SUCCESS); | |
1157 | } | |
1158 | ||
54761a45 JD |
1159 | if (opt_exec_name) { |
1160 | opt_exec_env = envp; | |
1161 | signal(SIGCHLD, handle_sigchild); | |
1162 | } | |
1163 | ||
1164 | if (!opt_input_path && !remote_live && !opt_exec_name) { | |
863dc485 JD |
1165 | /* mmap live */ |
1166 | #ifdef LTTNGTOP_MMAP_LIVE | |
24797198 JD |
1167 | if (opt_textdump) { |
1168 | signal(SIGTERM, handle_textdump_sigterm); | |
1169 | signal(SIGINT, handle_textdump_sigterm); | |
1170 | } | |
b1acd2b3 | 1171 | mmap_live_loop(bt_ctx); |
ae9e85c7 | 1172 | pthread_join(timer_thread, NULL); |
eb677852 JD |
1173 | quit = 1; |
1174 | pthread_join(display_thread, NULL); | |
6777529d | 1175 | |
c78f2cdc | 1176 | lttng_stop_tracing("test"); |
c78f2cdc JD |
1177 | lttng_destroy_session("test"); |
1178 | ||
1fc22eb4 | 1179 | goto end; |
863dc485 | 1180 | #else |
d2f1452b JD |
1181 | fprintf(stderr, "[ERROR] Mmap live support not compiled, specify a " |
1182 | "trace directory or -r <relayd hostname/IP>\n"); | |
1183 | usage(stdout); | |
1184 | ret = -1; | |
863dc485 JD |
1185 | goto end; |
1186 | #endif /* LTTNGTOP_MMAP_LIVE */ | |
1187 | } else if (!opt_input_path && remote_live) { | |
1188 | /* network live */ | |
12a91e9d JD |
1189 | bt_ctx = bt_context_create(); |
1190 | ret = bt_context_add_traces_recursive(bt_ctx, opt_relay_hostname, | |
1191 | "lttng-live", NULL); | |
1192 | if (ret < 0) { | |
1193 | fprintf(stderr, "[error] Opening the trace\n"); | |
1194 | goto end; | |
1195 | } | |
16b22a0f | 1196 | } else { |
957eed46 | 1197 | //init_lttngtop(); |
16b22a0f JD |
1198 | |
1199 | bt_ctx = bt_context_create(); | |
1200 | ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL); | |
1201 | if (ret < 0) { | |
1202 | fprintf(stderr, "[error] Opening the trace\n"); | |
1203 | goto end; | |
1204 | } | |
1fc22eb4 | 1205 | |
12a91e9d JD |
1206 | ret = check_requirements(bt_ctx); |
1207 | if (ret < 0) { | |
1208 | fprintf(stderr, "[error] some mandatory contexts " | |
1209 | "were missing, exiting.\n"); | |
1210 | goto end; | |
1211 | } | |
1212 | ||
1213 | if (!opt_textdump) { | |
1214 | pthread_create(&display_thread, NULL, ncurses_display, | |
1215 | (void *) NULL); | |
1216 | pthread_create(&timer_thread, NULL, refresh_thread, | |
1217 | (void *) NULL); | |
1218 | } | |
1219 | ||
1220 | iter_trace(bt_ctx); | |
863dc485 | 1221 | } |
1fc22eb4 | 1222 | |
1fc22eb4 | 1223 | |
863dc485 JD |
1224 | pthread_join(display_thread, NULL); |
1225 | quit = 1; | |
1226 | pthread_join(timer_thread, NULL); | |
1fc22eb4 | 1227 | |
d2f1452b JD |
1228 | ret = 0; |
1229 | ||
1fc22eb4 | 1230 | end: |
16b22a0f JD |
1231 | if (bt_ctx) |
1232 | bt_context_put(bt_ctx); | |
1233 | ||
d2f1452b | 1234 | return ret; |
1fc22eb4 | 1235 | } |