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