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