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