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