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