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