Commit | Line | Data |
---|---|---|
a598bb5e ACM |
1 | /* |
2 | * builtin-trace.c | |
3 | * | |
4 | * Builtin 'trace' command: | |
5 | * | |
6 | * Display a continuously updated trace of any workload, CPU, specific PID, | |
7 | * system wide, etc. Default format is loosely strace like, but any other | |
8 | * event may be specified using --event. | |
9 | * | |
10 | * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> | |
11 | * | |
12 | * Initially based on the 'trace' prototype by Thomas Gleixner: | |
13 | * | |
14 | * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'") | |
15 | * | |
16 | * Released under the GPL v2. (and only v2, not any later version) | |
17 | */ | |
18 | ||
4e319027 | 19 | #include <traceevent/event-parse.h> |
988bdb31 | 20 | #include <api/fs/tracing_path.h> |
514f1c67 | 21 | #include "builtin.h" |
752fde44 | 22 | #include "util/color.h" |
7c304ee0 | 23 | #include "util/debug.h" |
514f1c67 | 24 | #include "util/evlist.h" |
4b6ab94e | 25 | #include <subcmd/exec-cmd.h> |
752fde44 | 26 | #include "util/machine.h" |
6810fc91 | 27 | #include "util/session.h" |
752fde44 | 28 | #include "util/thread.h" |
4b6ab94e | 29 | #include <subcmd/parse-options.h> |
2ae3a312 | 30 | #include "util/strlist.h" |
bdc89661 | 31 | #include "util/intlist.h" |
514f1c67 | 32 | #include "util/thread_map.h" |
bf2575c1 | 33 | #include "util/stat.h" |
97978b3e | 34 | #include "trace-event.h" |
9aca7f17 | 35 | #include "util/parse-events.h" |
ba504235 | 36 | #include "util/bpf-loader.h" |
566a0885 | 37 | #include "callchain.h" |
fd0db102 | 38 | #include "syscalltbl.h" |
514f1c67 | 39 | |
fd0db102 | 40 | #include <libaudit.h> /* FIXME: Still needed for audit_errno_to_name */ |
514f1c67 | 41 | #include <stdlib.h> |
f9da0b0c | 42 | #include <linux/futex.h> |
8dd2a131 | 43 | #include <linux/err.h> |
997bba8c ACM |
44 | #include <linux/seccomp.h> |
45 | #include <linux/filter.h> | |
46 | #include <linux/audit.h> | |
47 | #include <sys/ptrace.h> | |
39878d49 | 48 | #include <linux/random.h> |
c6d4a494 | 49 | #include <linux/stringify.h> |
514f1c67 | 50 | |
c188e7ac ACM |
51 | #ifndef O_CLOEXEC |
52 | # define O_CLOEXEC 02000000 | |
53 | #endif | |
54 | ||
c188e7ac ACM |
55 | #ifndef MSG_CMSG_CLOEXEC |
56 | # define MSG_CMSG_CLOEXEC 0x40000000 | |
57 | #endif | |
58 | ||
a1c2552d ACM |
59 | #ifndef PERF_FLAG_FD_NO_GROUP |
60 | # define PERF_FLAG_FD_NO_GROUP (1UL << 0) | |
61 | #endif | |
62 | ||
63 | #ifndef PERF_FLAG_FD_OUTPUT | |
64 | # define PERF_FLAG_FD_OUTPUT (1UL << 1) | |
65 | #endif | |
66 | ||
67 | #ifndef PERF_FLAG_PID_CGROUP | |
68 | # define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */ | |
69 | #endif | |
70 | ||
71 | #ifndef PERF_FLAG_FD_CLOEXEC | |
72 | # define PERF_FLAG_FD_CLOEXEC (1UL << 3) /* O_CLOEXEC */ | |
73 | #endif | |
74 | ||
d1d438a3 ACM |
75 | struct trace { |
76 | struct perf_tool tool; | |
fd0db102 | 77 | struct syscalltbl *sctbl; |
d1d438a3 ACM |
78 | struct { |
79 | int max; | |
80 | struct syscall *table; | |
81 | struct { | |
82 | struct perf_evsel *sys_enter, | |
83 | *sys_exit; | |
84 | } events; | |
85 | } syscalls; | |
86 | struct record_opts opts; | |
87 | struct perf_evlist *evlist; | |
88 | struct machine *host; | |
89 | struct thread *current; | |
90 | u64 base_time; | |
91 | FILE *output; | |
92 | unsigned long nr_events; | |
93 | struct strlist *ev_qualifier; | |
94 | struct { | |
95 | size_t nr; | |
96 | int *entries; | |
97 | } ev_qualifier_ids; | |
98 | struct intlist *tid_list; | |
99 | struct intlist *pid_list; | |
100 | struct { | |
101 | size_t nr; | |
102 | pid_t *entries; | |
103 | } filter_pids; | |
104 | double duration_filter; | |
105 | double runtime_ms; | |
106 | struct { | |
107 | u64 vfs_getname, | |
108 | proc_getname; | |
109 | } stats; | |
c6d4a494 | 110 | unsigned int max_stack; |
5cf9c84e | 111 | unsigned int min_stack; |
d1d438a3 ACM |
112 | bool not_ev_qualifier; |
113 | bool live; | |
114 | bool full_time; | |
115 | bool sched; | |
116 | bool multiple_threads; | |
117 | bool summary; | |
118 | bool summary_only; | |
119 | bool show_comm; | |
120 | bool show_tool_stats; | |
121 | bool trace_syscalls; | |
44621819 | 122 | bool kernel_syscallchains; |
d1d438a3 ACM |
123 | bool force; |
124 | bool vfs_getname; | |
125 | int trace_pgfaults; | |
fd0db102 | 126 | int open_id; |
d1d438a3 | 127 | }; |
a1c2552d | 128 | |
77170988 ACM |
129 | struct tp_field { |
130 | int offset; | |
131 | union { | |
132 | u64 (*integer)(struct tp_field *field, struct perf_sample *sample); | |
133 | void *(*pointer)(struct tp_field *field, struct perf_sample *sample); | |
134 | }; | |
135 | }; | |
136 | ||
137 | #define TP_UINT_FIELD(bits) \ | |
138 | static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \ | |
139 | { \ | |
55d43bca DA |
140 | u##bits value; \ |
141 | memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ | |
142 | return value; \ | |
77170988 ACM |
143 | } |
144 | ||
145 | TP_UINT_FIELD(8); | |
146 | TP_UINT_FIELD(16); | |
147 | TP_UINT_FIELD(32); | |
148 | TP_UINT_FIELD(64); | |
149 | ||
150 | #define TP_UINT_FIELD__SWAPPED(bits) \ | |
151 | static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \ | |
152 | { \ | |
55d43bca DA |
153 | u##bits value; \ |
154 | memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ | |
77170988 ACM |
155 | return bswap_##bits(value);\ |
156 | } | |
157 | ||
158 | TP_UINT_FIELD__SWAPPED(16); | |
159 | TP_UINT_FIELD__SWAPPED(32); | |
160 | TP_UINT_FIELD__SWAPPED(64); | |
161 | ||
162 | static int tp_field__init_uint(struct tp_field *field, | |
163 | struct format_field *format_field, | |
164 | bool needs_swap) | |
165 | { | |
166 | field->offset = format_field->offset; | |
167 | ||
168 | switch (format_field->size) { | |
169 | case 1: | |
170 | field->integer = tp_field__u8; | |
171 | break; | |
172 | case 2: | |
173 | field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16; | |
174 | break; | |
175 | case 4: | |
176 | field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32; | |
177 | break; | |
178 | case 8: | |
179 | field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64; | |
180 | break; | |
181 | default: | |
182 | return -1; | |
183 | } | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample) | |
189 | { | |
190 | return sample->raw_data + field->offset; | |
191 | } | |
192 | ||
193 | static int tp_field__init_ptr(struct tp_field *field, struct format_field *format_field) | |
194 | { | |
195 | field->offset = format_field->offset; | |
196 | field->pointer = tp_field__ptr; | |
197 | return 0; | |
198 | } | |
199 | ||
200 | struct syscall_tp { | |
201 | struct tp_field id; | |
202 | union { | |
203 | struct tp_field args, ret; | |
204 | }; | |
205 | }; | |
206 | ||
207 | static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel, | |
208 | struct tp_field *field, | |
209 | const char *name) | |
210 | { | |
211 | struct format_field *format_field = perf_evsel__field(evsel, name); | |
212 | ||
213 | if (format_field == NULL) | |
214 | return -1; | |
215 | ||
216 | return tp_field__init_uint(field, format_field, evsel->needs_swap); | |
217 | } | |
218 | ||
219 | #define perf_evsel__init_sc_tp_uint_field(evsel, name) \ | |
220 | ({ struct syscall_tp *sc = evsel->priv;\ | |
221 | perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); }) | |
222 | ||
223 | static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel, | |
224 | struct tp_field *field, | |
225 | const char *name) | |
226 | { | |
227 | struct format_field *format_field = perf_evsel__field(evsel, name); | |
228 | ||
229 | if (format_field == NULL) | |
230 | return -1; | |
231 | ||
232 | return tp_field__init_ptr(field, format_field); | |
233 | } | |
234 | ||
235 | #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \ | |
236 | ({ struct syscall_tp *sc = evsel->priv;\ | |
237 | perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); }) | |
238 | ||
239 | static void perf_evsel__delete_priv(struct perf_evsel *evsel) | |
240 | { | |
04662523 | 241 | zfree(&evsel->priv); |
77170988 ACM |
242 | perf_evsel__delete(evsel); |
243 | } | |
244 | ||
96695d44 NK |
245 | static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel, void *handler) |
246 | { | |
247 | evsel->priv = malloc(sizeof(struct syscall_tp)); | |
248 | if (evsel->priv != NULL) { | |
249 | if (perf_evsel__init_sc_tp_uint_field(evsel, id)) | |
250 | goto out_delete; | |
251 | ||
252 | evsel->handler = handler; | |
253 | return 0; | |
254 | } | |
255 | ||
256 | return -ENOMEM; | |
257 | ||
258 | out_delete: | |
04662523 | 259 | zfree(&evsel->priv); |
96695d44 NK |
260 | return -ENOENT; |
261 | } | |
262 | ||
ef503831 | 263 | static struct perf_evsel *perf_evsel__syscall_newtp(const char *direction, void *handler) |
77170988 | 264 | { |
ef503831 | 265 | struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction); |
77170988 | 266 | |
9aca7f17 | 267 | /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */ |
8dd2a131 | 268 | if (IS_ERR(evsel)) |
9aca7f17 DA |
269 | evsel = perf_evsel__newtp("syscalls", direction); |
270 | ||
8dd2a131 JO |
271 | if (IS_ERR(evsel)) |
272 | return NULL; | |
273 | ||
274 | if (perf_evsel__init_syscall_tp(evsel, handler)) | |
275 | goto out_delete; | |
77170988 ACM |
276 | |
277 | return evsel; | |
278 | ||
279 | out_delete: | |
280 | perf_evsel__delete_priv(evsel); | |
281 | return NULL; | |
282 | } | |
283 | ||
284 | #define perf_evsel__sc_tp_uint(evsel, name, sample) \ | |
285 | ({ struct syscall_tp *fields = evsel->priv; \ | |
286 | fields->name.integer(&fields->name, sample); }) | |
287 | ||
288 | #define perf_evsel__sc_tp_ptr(evsel, name, sample) \ | |
289 | ({ struct syscall_tp *fields = evsel->priv; \ | |
290 | fields->name.pointer(&fields->name, sample); }) | |
291 | ||
01533e97 ACM |
292 | struct syscall_arg { |
293 | unsigned long val; | |
75b757ca ACM |
294 | struct thread *thread; |
295 | struct trace *trace; | |
1f115cb7 | 296 | void *parm; |
01533e97 ACM |
297 | u8 idx; |
298 | u8 mask; | |
299 | }; | |
300 | ||
1f115cb7 | 301 | struct strarray { |
03e3adc9 | 302 | int offset; |
1f115cb7 ACM |
303 | int nr_entries; |
304 | const char **entries; | |
305 | }; | |
306 | ||
307 | #define DEFINE_STRARRAY(array) struct strarray strarray__##array = { \ | |
308 | .nr_entries = ARRAY_SIZE(array), \ | |
309 | .entries = array, \ | |
310 | } | |
311 | ||
03e3adc9 ACM |
312 | #define DEFINE_STRARRAY_OFFSET(array, off) struct strarray strarray__##array = { \ |
313 | .offset = off, \ | |
314 | .nr_entries = ARRAY_SIZE(array), \ | |
315 | .entries = array, \ | |
316 | } | |
317 | ||
975b7c2f ACM |
318 | static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size, |
319 | const char *intfmt, | |
320 | struct syscall_arg *arg) | |
1f115cb7 | 321 | { |
1f115cb7 | 322 | struct strarray *sa = arg->parm; |
03e3adc9 | 323 | int idx = arg->val - sa->offset; |
1f115cb7 ACM |
324 | |
325 | if (idx < 0 || idx >= sa->nr_entries) | |
975b7c2f | 326 | return scnprintf(bf, size, intfmt, arg->val); |
1f115cb7 ACM |
327 | |
328 | return scnprintf(bf, size, "%s", sa->entries[idx]); | |
329 | } | |
330 | ||
975b7c2f ACM |
331 | static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size, |
332 | struct syscall_arg *arg) | |
333 | { | |
334 | return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg); | |
335 | } | |
336 | ||
1f115cb7 ACM |
337 | #define SCA_STRARRAY syscall_arg__scnprintf_strarray |
338 | ||
844ae5b4 ACM |
339 | #if defined(__i386__) || defined(__x86_64__) |
340 | /* | |
341 | * FIXME: Make this available to all arches as soon as the ioctl beautifier | |
342 | * gets rewritten to support all arches. | |
343 | */ | |
78645cf3 ACM |
344 | static size_t syscall_arg__scnprintf_strhexarray(char *bf, size_t size, |
345 | struct syscall_arg *arg) | |
346 | { | |
347 | return __syscall_arg__scnprintf_strarray(bf, size, "%#x", arg); | |
348 | } | |
349 | ||
350 | #define SCA_STRHEXARRAY syscall_arg__scnprintf_strhexarray | |
844ae5b4 | 351 | #endif /* defined(__i386__) || defined(__x86_64__) */ |
78645cf3 | 352 | |
75b757ca ACM |
353 | static size_t syscall_arg__scnprintf_fd(char *bf, size_t size, |
354 | struct syscall_arg *arg); | |
355 | ||
356 | #define SCA_FD syscall_arg__scnprintf_fd | |
357 | ||
358 | static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size, | |
359 | struct syscall_arg *arg) | |
360 | { | |
361 | int fd = arg->val; | |
362 | ||
363 | if (fd == AT_FDCWD) | |
364 | return scnprintf(bf, size, "CWD"); | |
365 | ||
366 | return syscall_arg__scnprintf_fd(bf, size, arg); | |
367 | } | |
368 | ||
369 | #define SCA_FDAT syscall_arg__scnprintf_fd_at | |
370 | ||
371 | static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, | |
372 | struct syscall_arg *arg); | |
373 | ||
374 | #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd | |
375 | ||
6e7eeb51 | 376 | static size_t syscall_arg__scnprintf_hex(char *bf, size_t size, |
01533e97 | 377 | struct syscall_arg *arg) |
13d4ff3e | 378 | { |
01533e97 | 379 | return scnprintf(bf, size, "%#lx", arg->val); |
13d4ff3e ACM |
380 | } |
381 | ||
beccb2b5 ACM |
382 | #define SCA_HEX syscall_arg__scnprintf_hex |
383 | ||
a1c2552d ACM |
384 | static size_t syscall_arg__scnprintf_int(char *bf, size_t size, |
385 | struct syscall_arg *arg) | |
386 | { | |
387 | return scnprintf(bf, size, "%d", arg->val); | |
388 | } | |
389 | ||
390 | #define SCA_INT syscall_arg__scnprintf_int | |
391 | ||
5cea6ff2 ACM |
392 | static size_t syscall_arg__scnprintf_flock(char *bf, size_t size, |
393 | struct syscall_arg *arg) | |
394 | { | |
395 | int printed = 0, op = arg->val; | |
396 | ||
397 | if (op == 0) | |
398 | return scnprintf(bf, size, "NONE"); | |
399 | #define P_CMD(cmd) \ | |
400 | if ((op & LOCK_##cmd) == LOCK_##cmd) { \ | |
401 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #cmd); \ | |
402 | op &= ~LOCK_##cmd; \ | |
403 | } | |
404 | ||
405 | P_CMD(SH); | |
406 | P_CMD(EX); | |
407 | P_CMD(NB); | |
408 | P_CMD(UN); | |
409 | P_CMD(MAND); | |
410 | P_CMD(RW); | |
411 | P_CMD(READ); | |
412 | P_CMD(WRITE); | |
413 | #undef P_OP | |
414 | ||
415 | if (op) | |
416 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", op); | |
417 | ||
418 | return printed; | |
419 | } | |
420 | ||
421 | #define SCA_FLOCK syscall_arg__scnprintf_flock | |
422 | ||
01533e97 | 423 | static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, struct syscall_arg *arg) |
f9da0b0c ACM |
424 | { |
425 | enum syscall_futex_args { | |
426 | SCF_UADDR = (1 << 0), | |
427 | SCF_OP = (1 << 1), | |
428 | SCF_VAL = (1 << 2), | |
429 | SCF_TIMEOUT = (1 << 3), | |
430 | SCF_UADDR2 = (1 << 4), | |
431 | SCF_VAL3 = (1 << 5), | |
432 | }; | |
01533e97 | 433 | int op = arg->val; |
f9da0b0c ACM |
434 | int cmd = op & FUTEX_CMD_MASK; |
435 | size_t printed = 0; | |
436 | ||
437 | switch (cmd) { | |
438 | #define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n); | |
01533e97 ACM |
439 | P_FUTEX_OP(WAIT); arg->mask |= SCF_VAL3|SCF_UADDR2; break; |
440 | P_FUTEX_OP(WAKE); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break; | |
441 | P_FUTEX_OP(FD); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break; | |
442 | P_FUTEX_OP(REQUEUE); arg->mask |= SCF_VAL3|SCF_TIMEOUT; break; | |
443 | P_FUTEX_OP(CMP_REQUEUE); arg->mask |= SCF_TIMEOUT; break; | |
444 | P_FUTEX_OP(CMP_REQUEUE_PI); arg->mask |= SCF_TIMEOUT; break; | |
f9da0b0c | 445 | P_FUTEX_OP(WAKE_OP); break; |
01533e97 ACM |
446 | P_FUTEX_OP(LOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break; |
447 | P_FUTEX_OP(UNLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break; | |
448 | P_FUTEX_OP(TRYLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2; break; | |
449 | P_FUTEX_OP(WAIT_BITSET); arg->mask |= SCF_UADDR2; break; | |
450 | P_FUTEX_OP(WAKE_BITSET); arg->mask |= SCF_UADDR2; break; | |
f9da0b0c ACM |
451 | P_FUTEX_OP(WAIT_REQUEUE_PI); break; |
452 | default: printed = scnprintf(bf, size, "%#x", cmd); break; | |
453 | } | |
454 | ||
455 | if (op & FUTEX_PRIVATE_FLAG) | |
456 | printed += scnprintf(bf + printed, size - printed, "|PRIV"); | |
457 | ||
458 | if (op & FUTEX_CLOCK_REALTIME) | |
459 | printed += scnprintf(bf + printed, size - printed, "|CLKRT"); | |
460 | ||
461 | return printed; | |
462 | } | |
463 | ||
efe6b882 ACM |
464 | #define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op |
465 | ||
729a7841 ACM |
466 | static const char *bpf_cmd[] = { |
467 | "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM", | |
468 | "MAP_GET_NEXT_KEY", "PROG_LOAD", | |
469 | }; | |
470 | static DEFINE_STRARRAY(bpf_cmd); | |
471 | ||
03e3adc9 ACM |
472 | static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", }; |
473 | static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1); | |
eac032c5 | 474 | |
1f115cb7 ACM |
475 | static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", }; |
476 | static DEFINE_STRARRAY(itimers); | |
477 | ||
b62bee1b ACM |
478 | static const char *keyctl_options[] = { |
479 | "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN", | |
480 | "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ", | |
481 | "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT", | |
482 | "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT", | |
483 | "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT", | |
484 | }; | |
485 | static DEFINE_STRARRAY(keyctl_options); | |
486 | ||
efe6b882 ACM |
487 | static const char *whences[] = { "SET", "CUR", "END", |
488 | #ifdef SEEK_DATA | |
489 | "DATA", | |
490 | #endif | |
491 | #ifdef SEEK_HOLE | |
492 | "HOLE", | |
493 | #endif | |
494 | }; | |
495 | static DEFINE_STRARRAY(whences); | |
f9da0b0c | 496 | |
80f587d5 ACM |
497 | static const char *fcntl_cmds[] = { |
498 | "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK", | |
499 | "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "F_GETLK64", | |
500 | "F_SETLK64", "F_SETLKW64", "F_SETOWN_EX", "F_GETOWN_EX", | |
501 | "F_GETOWNER_UIDS", | |
502 | }; | |
503 | static DEFINE_STRARRAY(fcntl_cmds); | |
504 | ||
c045bf02 ACM |
505 | static const char *rlimit_resources[] = { |
506 | "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE", | |
507 | "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO", | |
508 | "RTTIME", | |
509 | }; | |
510 | static DEFINE_STRARRAY(rlimit_resources); | |
511 | ||
eb5b1b14 ACM |
512 | static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", }; |
513 | static DEFINE_STRARRAY(sighow); | |
514 | ||
4f8c1b74 DA |
515 | static const char *clockid[] = { |
516 | "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID", | |
28ebb87c ACM |
517 | "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME", |
518 | "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI" | |
4f8c1b74 DA |
519 | }; |
520 | static DEFINE_STRARRAY(clockid); | |
521 | ||
e10bce81 ACM |
522 | static const char *socket_families[] = { |
523 | "UNSPEC", "LOCAL", "INET", "AX25", "IPX", "APPLETALK", "NETROM", | |
524 | "BRIDGE", "ATMPVC", "X25", "INET6", "ROSE", "DECnet", "NETBEUI", | |
525 | "SECURITY", "KEY", "NETLINK", "PACKET", "ASH", "ECONET", "ATMSVC", | |
526 | "RDS", "SNA", "IRDA", "PPPOX", "WANPIPE", "LLC", "IB", "CAN", "TIPC", | |
527 | "BLUETOOTH", "IUCV", "RXRPC", "ISDN", "PHONET", "IEEE802154", "CAIF", | |
528 | "ALG", "NFC", "VSOCK", | |
529 | }; | |
530 | static DEFINE_STRARRAY(socket_families); | |
531 | ||
b2cc99fd ACM |
532 | #ifndef MSG_PROBE |
533 | #define MSG_PROBE 0x10 | |
534 | #endif | |
b6e8f8f4 DA |
535 | #ifndef MSG_WAITFORONE |
536 | #define MSG_WAITFORONE 0x10000 | |
537 | #endif | |
b2cc99fd ACM |
538 | #ifndef MSG_SENDPAGE_NOTLAST |
539 | #define MSG_SENDPAGE_NOTLAST 0x20000 | |
540 | #endif | |
541 | #ifndef MSG_FASTOPEN | |
542 | #define MSG_FASTOPEN 0x20000000 | |
543 | #endif | |
544 | ||
545 | static size_t syscall_arg__scnprintf_msg_flags(char *bf, size_t size, | |
546 | struct syscall_arg *arg) | |
547 | { | |
548 | int printed = 0, flags = arg->val; | |
549 | ||
550 | if (flags == 0) | |
551 | return scnprintf(bf, size, "NONE"); | |
552 | #define P_MSG_FLAG(n) \ | |
553 | if (flags & MSG_##n) { \ | |
554 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ | |
555 | flags &= ~MSG_##n; \ | |
556 | } | |
557 | ||
558 | P_MSG_FLAG(OOB); | |
559 | P_MSG_FLAG(PEEK); | |
560 | P_MSG_FLAG(DONTROUTE); | |
561 | P_MSG_FLAG(TRYHARD); | |
562 | P_MSG_FLAG(CTRUNC); | |
563 | P_MSG_FLAG(PROBE); | |
564 | P_MSG_FLAG(TRUNC); | |
565 | P_MSG_FLAG(DONTWAIT); | |
566 | P_MSG_FLAG(EOR); | |
567 | P_MSG_FLAG(WAITALL); | |
568 | P_MSG_FLAG(FIN); | |
569 | P_MSG_FLAG(SYN); | |
570 | P_MSG_FLAG(CONFIRM); | |
571 | P_MSG_FLAG(RST); | |
572 | P_MSG_FLAG(ERRQUEUE); | |
573 | P_MSG_FLAG(NOSIGNAL); | |
574 | P_MSG_FLAG(MORE); | |
575 | P_MSG_FLAG(WAITFORONE); | |
576 | P_MSG_FLAG(SENDPAGE_NOTLAST); | |
577 | P_MSG_FLAG(FASTOPEN); | |
578 | P_MSG_FLAG(CMSG_CLOEXEC); | |
579 | #undef P_MSG_FLAG | |
580 | ||
581 | if (flags) | |
582 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); | |
583 | ||
584 | return printed; | |
585 | } | |
586 | ||
587 | #define SCA_MSG_FLAGS syscall_arg__scnprintf_msg_flags | |
588 | ||
51108999 ACM |
589 | static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size, |
590 | struct syscall_arg *arg) | |
591 | { | |
592 | size_t printed = 0; | |
593 | int mode = arg->val; | |
594 | ||
595 | if (mode == F_OK) /* 0 */ | |
596 | return scnprintf(bf, size, "F"); | |
597 | #define P_MODE(n) \ | |
598 | if (mode & n##_OK) { \ | |
599 | printed += scnprintf(bf + printed, size - printed, "%s", #n); \ | |
600 | mode &= ~n##_OK; \ | |
601 | } | |
602 | ||
603 | P_MODE(R); | |
604 | P_MODE(W); | |
605 | P_MODE(X); | |
606 | #undef P_MODE | |
607 | ||
608 | if (mode) | |
609 | printed += scnprintf(bf + printed, size - printed, "|%#x", mode); | |
610 | ||
611 | return printed; | |
612 | } | |
613 | ||
614 | #define SCA_ACCMODE syscall_arg__scnprintf_access_mode | |
615 | ||
f994592d ACM |
616 | static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, |
617 | struct syscall_arg *arg); | |
618 | ||
619 | #define SCA_FILENAME syscall_arg__scnprintf_filename | |
620 | ||
be65a89a | 621 | static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size, |
01533e97 | 622 | struct syscall_arg *arg) |
be65a89a | 623 | { |
01533e97 | 624 | int printed = 0, flags = arg->val; |
be65a89a ACM |
625 | |
626 | if (!(flags & O_CREAT)) | |
01533e97 | 627 | arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */ |
be65a89a ACM |
628 | |
629 | if (flags == 0) | |
630 | return scnprintf(bf, size, "RDONLY"); | |
631 | #define P_FLAG(n) \ | |
632 | if (flags & O_##n) { \ | |
633 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ | |
634 | flags &= ~O_##n; \ | |
635 | } | |
636 | ||
637 | P_FLAG(APPEND); | |
638 | P_FLAG(ASYNC); | |
639 | P_FLAG(CLOEXEC); | |
640 | P_FLAG(CREAT); | |
641 | P_FLAG(DIRECT); | |
642 | P_FLAG(DIRECTORY); | |
643 | P_FLAG(EXCL); | |
644 | P_FLAG(LARGEFILE); | |
645 | P_FLAG(NOATIME); | |
646 | P_FLAG(NOCTTY); | |
647 | #ifdef O_NONBLOCK | |
648 | P_FLAG(NONBLOCK); | |
649 | #elif O_NDELAY | |
650 | P_FLAG(NDELAY); | |
651 | #endif | |
652 | #ifdef O_PATH | |
653 | P_FLAG(PATH); | |
654 | #endif | |
655 | P_FLAG(RDWR); | |
656 | #ifdef O_DSYNC | |
657 | if ((flags & O_SYNC) == O_SYNC) | |
658 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC"); | |
659 | else { | |
660 | P_FLAG(DSYNC); | |
661 | } | |
662 | #else | |
663 | P_FLAG(SYNC); | |
664 | #endif | |
665 | P_FLAG(TRUNC); | |
666 | P_FLAG(WRONLY); | |
667 | #undef P_FLAG | |
668 | ||
669 | if (flags) | |
670 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); | |
671 | ||
672 | return printed; | |
673 | } | |
674 | ||
675 | #define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags | |
676 | ||
a1c2552d ACM |
677 | static size_t syscall_arg__scnprintf_perf_flags(char *bf, size_t size, |
678 | struct syscall_arg *arg) | |
679 | { | |
680 | int printed = 0, flags = arg->val; | |
681 | ||
682 | if (flags == 0) | |
683 | return 0; | |
684 | ||
685 | #define P_FLAG(n) \ | |
686 | if (flags & PERF_FLAG_##n) { \ | |
687 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ | |
688 | flags &= ~PERF_FLAG_##n; \ | |
689 | } | |
690 | ||
691 | P_FLAG(FD_NO_GROUP); | |
692 | P_FLAG(FD_OUTPUT); | |
693 | P_FLAG(PID_CGROUP); | |
694 | P_FLAG(FD_CLOEXEC); | |
695 | #undef P_FLAG | |
696 | ||
697 | if (flags) | |
698 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); | |
699 | ||
700 | return printed; | |
701 | } | |
702 | ||
703 | #define SCA_PERF_FLAGS syscall_arg__scnprintf_perf_flags | |
704 | ||
46cce19b ACM |
705 | static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size, |
706 | struct syscall_arg *arg) | |
707 | { | |
708 | int printed = 0, flags = arg->val; | |
709 | ||
710 | #define P_FLAG(n) \ | |
711 | if (flags & O_##n) { \ | |
712 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ | |
713 | flags &= ~O_##n; \ | |
714 | } | |
715 | ||
716 | P_FLAG(CLOEXEC); | |
717 | P_FLAG(NONBLOCK); | |
718 | #undef P_FLAG | |
719 | ||
720 | if (flags) | |
721 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); | |
722 | ||
723 | return printed; | |
724 | } | |
725 | ||
726 | #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags | |
727 | ||
8bad5b0a ACM |
728 | static size_t syscall_arg__scnprintf_signum(char *bf, size_t size, struct syscall_arg *arg) |
729 | { | |
730 | int sig = arg->val; | |
731 | ||
732 | switch (sig) { | |
733 | #define P_SIGNUM(n) case SIG##n: return scnprintf(bf, size, #n) | |
734 | P_SIGNUM(HUP); | |
735 | P_SIGNUM(INT); | |
736 | P_SIGNUM(QUIT); | |
737 | P_SIGNUM(ILL); | |
738 | P_SIGNUM(TRAP); | |
739 | P_SIGNUM(ABRT); | |
740 | P_SIGNUM(BUS); | |
741 | P_SIGNUM(FPE); | |
742 | P_SIGNUM(KILL); | |
743 | P_SIGNUM(USR1); | |
744 | P_SIGNUM(SEGV); | |
745 | P_SIGNUM(USR2); | |
746 | P_SIGNUM(PIPE); | |
747 | P_SIGNUM(ALRM); | |
748 | P_SIGNUM(TERM); | |
8bad5b0a ACM |
749 | P_SIGNUM(CHLD); |
750 | P_SIGNUM(CONT); | |
751 | P_SIGNUM(STOP); | |
752 | P_SIGNUM(TSTP); | |
753 | P_SIGNUM(TTIN); | |
754 | P_SIGNUM(TTOU); | |
755 | P_SIGNUM(URG); | |
756 | P_SIGNUM(XCPU); | |
757 | P_SIGNUM(XFSZ); | |
758 | P_SIGNUM(VTALRM); | |
759 | P_SIGNUM(PROF); | |
760 | P_SIGNUM(WINCH); | |
761 | P_SIGNUM(IO); | |
762 | P_SIGNUM(PWR); | |
763 | P_SIGNUM(SYS); | |
02c5bb4a BH |
764 | #ifdef SIGEMT |
765 | P_SIGNUM(EMT); | |
766 | #endif | |
767 | #ifdef SIGSTKFLT | |
768 | P_SIGNUM(STKFLT); | |
769 | #endif | |
770 | #ifdef SIGSWI | |
771 | P_SIGNUM(SWI); | |
772 | #endif | |
8bad5b0a ACM |
773 | default: break; |
774 | } | |
775 | ||
776 | return scnprintf(bf, size, "%#x", sig); | |
777 | } | |
778 | ||
779 | #define SCA_SIGNUM syscall_arg__scnprintf_signum | |
780 | ||
844ae5b4 ACM |
781 | #if defined(__i386__) || defined(__x86_64__) |
782 | /* | |
783 | * FIXME: Make this available to all arches. | |
784 | */ | |
78645cf3 ACM |
785 | #define TCGETS 0x5401 |
786 | ||
787 | static const char *tioctls[] = { | |
788 | "TCGETS", "TCSETS", "TCSETSW", "TCSETSF", "TCGETA", "TCSETA", "TCSETAW", | |
789 | "TCSETAF", "TCSBRK", "TCXONC", "TCFLSH", "TIOCEXCL", "TIOCNXCL", | |
790 | "TIOCSCTTY", "TIOCGPGRP", "TIOCSPGRP", "TIOCOUTQ", "TIOCSTI", | |
791 | "TIOCGWINSZ", "TIOCSWINSZ", "TIOCMGET", "TIOCMBIS", "TIOCMBIC", | |
792 | "TIOCMSET", "TIOCGSOFTCAR", "TIOCSSOFTCAR", "FIONREAD", "TIOCLINUX", | |
793 | "TIOCCONS", "TIOCGSERIAL", "TIOCSSERIAL", "TIOCPKT", "FIONBIO", | |
794 | "TIOCNOTTY", "TIOCSETD", "TIOCGETD", "TCSBRKP", [0x27] = "TIOCSBRK", | |
795 | "TIOCCBRK", "TIOCGSID", "TCGETS2", "TCSETS2", "TCSETSW2", "TCSETSF2", | |
796 | "TIOCGRS485", "TIOCSRS485", "TIOCGPTN", "TIOCSPTLCK", | |
797 | "TIOCGDEV||TCGETX", "TCSETX", "TCSETXF", "TCSETXW", "TIOCSIG", | |
798 | "TIOCVHANGUP", "TIOCGPKT", "TIOCGPTLCK", "TIOCGEXCL", | |
799 | [0x50] = "FIONCLEX", "FIOCLEX", "FIOASYNC", "TIOCSERCONFIG", | |
800 | "TIOCSERGWILD", "TIOCSERSWILD", "TIOCGLCKTRMIOS", "TIOCSLCKTRMIOS", | |
801 | "TIOCSERGSTRUCT", "TIOCSERGETLSR", "TIOCSERGETMULTI", "TIOCSERSETMULTI", | |
802 | "TIOCMIWAIT", "TIOCGICOUNT", [0x60] = "FIOQSIZE", | |
803 | }; | |
804 | ||
805 | static DEFINE_STRARRAY_OFFSET(tioctls, 0x5401); | |
844ae5b4 | 806 | #endif /* defined(__i386__) || defined(__x86_64__) */ |
78645cf3 | 807 | |
6fb35b95 ACM |
808 | #ifndef SECCOMP_SET_MODE_STRICT |
809 | #define SECCOMP_SET_MODE_STRICT 0 | |
810 | #endif | |
811 | #ifndef SECCOMP_SET_MODE_FILTER | |
812 | #define SECCOMP_SET_MODE_FILTER 1 | |
813 | #endif | |
814 | ||
997bba8c ACM |
815 | static size_t syscall_arg__scnprintf_seccomp_op(char *bf, size_t size, struct syscall_arg *arg) |
816 | { | |
817 | int op = arg->val; | |
818 | size_t printed = 0; | |
819 | ||
820 | switch (op) { | |
821 | #define P_SECCOMP_SET_MODE_OP(n) case SECCOMP_SET_MODE_##n: printed = scnprintf(bf, size, #n); break | |
822 | P_SECCOMP_SET_MODE_OP(STRICT); | |
823 | P_SECCOMP_SET_MODE_OP(FILTER); | |
824 | #undef P_SECCOMP_SET_MODE_OP | |
825 | default: printed = scnprintf(bf, size, "%#x", op); break; | |
826 | } | |
827 | ||
828 | return printed; | |
829 | } | |
830 | ||
831 | #define SCA_SECCOMP_OP syscall_arg__scnprintf_seccomp_op | |
832 | ||
6fb35b95 ACM |
833 | #ifndef SECCOMP_FILTER_FLAG_TSYNC |
834 | #define SECCOMP_FILTER_FLAG_TSYNC 1 | |
835 | #endif | |
836 | ||
997bba8c ACM |
837 | static size_t syscall_arg__scnprintf_seccomp_flags(char *bf, size_t size, |
838 | struct syscall_arg *arg) | |
839 | { | |
840 | int printed = 0, flags = arg->val; | |
841 | ||
842 | #define P_FLAG(n) \ | |
843 | if (flags & SECCOMP_FILTER_FLAG_##n) { \ | |
844 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ | |
845 | flags &= ~SECCOMP_FILTER_FLAG_##n; \ | |
846 | } | |
847 | ||
848 | P_FLAG(TSYNC); | |
849 | #undef P_FLAG | |
850 | ||
851 | if (flags) | |
852 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); | |
853 | ||
854 | return printed; | |
855 | } | |
856 | ||
857 | #define SCA_SECCOMP_FLAGS syscall_arg__scnprintf_seccomp_flags | |
858 | ||
a355a61e ACM |
859 | #ifndef GRND_NONBLOCK |
860 | #define GRND_NONBLOCK 0x0001 | |
861 | #endif | |
862 | #ifndef GRND_RANDOM | |
863 | #define GRND_RANDOM 0x0002 | |
864 | #endif | |
865 | ||
39878d49 ACM |
866 | static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size, |
867 | struct syscall_arg *arg) | |
868 | { | |
869 | int printed = 0, flags = arg->val; | |
870 | ||
871 | #define P_FLAG(n) \ | |
872 | if (flags & GRND_##n) { \ | |
873 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ | |
874 | flags &= ~GRND_##n; \ | |
875 | } | |
876 | ||
877 | P_FLAG(RANDOM); | |
878 | P_FLAG(NONBLOCK); | |
879 | #undef P_FLAG | |
880 | ||
881 | if (flags) | |
882 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); | |
883 | ||
884 | return printed; | |
885 | } | |
886 | ||
887 | #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags | |
888 | ||
453350dd ACM |
889 | #define STRARRAY(arg, name, array) \ |
890 | .arg_scnprintf = { [arg] = SCA_STRARRAY, }, \ | |
891 | .arg_parm = { [arg] = &strarray__##array, } | |
892 | ||
ea8dc3ce | 893 | #include "trace/beauty/eventfd.c" |
d1d438a3 | 894 | #include "trace/beauty/pid.c" |
df4cb167 | 895 | #include "trace/beauty/mmap.c" |
ba2f22cf | 896 | #include "trace/beauty/mode_t.c" |
a3bca91f | 897 | #include "trace/beauty/sched_policy.c" |
bbf86c43 | 898 | #include "trace/beauty/socket_type.c" |
7206b900 | 899 | #include "trace/beauty/waitid_options.c" |
a3bca91f | 900 | |
514f1c67 ACM |
901 | static struct syscall_fmt { |
902 | const char *name; | |
aec1930b | 903 | const char *alias; |
01533e97 | 904 | size_t (*arg_scnprintf[6])(char *bf, size_t size, struct syscall_arg *arg); |
1f115cb7 | 905 | void *arg_parm[6]; |
514f1c67 | 906 | bool errmsg; |
11c8e39f | 907 | bool errpid; |
514f1c67 | 908 | bool timeout; |
04b34729 | 909 | bool hexret; |
514f1c67 | 910 | } syscall_fmts[] = { |
51108999 | 911 | { .name = "access", .errmsg = true, |
34221118 ACM |
912 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ |
913 | [1] = SCA_ACCMODE, /* mode */ }, }, | |
aec1930b | 914 | { .name = "arch_prctl", .errmsg = true, .alias = "prctl", }, |
729a7841 | 915 | { .name = "bpf", .errmsg = true, STRARRAY(0, cmd, bpf_cmd), }, |
beccb2b5 ACM |
916 | { .name = "brk", .hexret = true, |
917 | .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, }, | |
34221118 ACM |
918 | { .name = "chdir", .errmsg = true, |
919 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, }, | |
920 | { .name = "chmod", .errmsg = true, | |
921 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, }, | |
922 | { .name = "chroot", .errmsg = true, | |
923 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, }, | |
4f8c1b74 | 924 | { .name = "clock_gettime", .errmsg = true, STRARRAY(0, clk_id, clockid), }, |
11c8e39f | 925 | { .name = "clone", .errpid = true, }, |
75b757ca | 926 | { .name = "close", .errmsg = true, |
48000a1a | 927 | .arg_scnprintf = { [0] = SCA_CLOSE_FD, /* fd */ }, }, |
a14bb860 | 928 | { .name = "connect", .errmsg = true, }, |
34221118 ACM |
929 | { .name = "creat", .errmsg = true, |
930 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
75b757ca | 931 | { .name = "dup", .errmsg = true, |
48000a1a | 932 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 933 | { .name = "dup2", .errmsg = true, |
48000a1a | 934 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 935 | { .name = "dup3", .errmsg = true, |
48000a1a | 936 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
453350dd | 937 | { .name = "epoll_ctl", .errmsg = true, STRARRAY(1, op, epoll_ctl_ops), }, |
49af9e93 ACM |
938 | { .name = "eventfd2", .errmsg = true, |
939 | .arg_scnprintf = { [1] = SCA_EFD_FLAGS, /* flags */ }, }, | |
75b757ca | 940 | { .name = "faccessat", .errmsg = true, |
34221118 ACM |
941 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ |
942 | [1] = SCA_FILENAME, /* filename */ }, }, | |
75b757ca | 943 | { .name = "fadvise64", .errmsg = true, |
48000a1a | 944 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 945 | { .name = "fallocate", .errmsg = true, |
48000a1a | 946 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 947 | { .name = "fchdir", .errmsg = true, |
48000a1a | 948 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 949 | { .name = "fchmod", .errmsg = true, |
48000a1a | 950 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 951 | { .name = "fchmodat", .errmsg = true, |
090389b6 ACM |
952 | .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ |
953 | [1] = SCA_FILENAME, /* filename */ }, }, | |
75b757ca | 954 | { .name = "fchown", .errmsg = true, |
48000a1a | 955 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 956 | { .name = "fchownat", .errmsg = true, |
34221118 ACM |
957 | .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ |
958 | [1] = SCA_FILENAME, /* filename */ }, }, | |
75b757ca ACM |
959 | { .name = "fcntl", .errmsg = true, |
960 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ | |
961 | [1] = SCA_STRARRAY, /* cmd */ }, | |
962 | .arg_parm = { [1] = &strarray__fcntl_cmds, /* cmd */ }, }, | |
963 | { .name = "fdatasync", .errmsg = true, | |
48000a1a | 964 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
5cea6ff2 | 965 | { .name = "flock", .errmsg = true, |
75b757ca ACM |
966 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ |
967 | [1] = SCA_FLOCK, /* cmd */ }, }, | |
968 | { .name = "fsetxattr", .errmsg = true, | |
48000a1a | 969 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 970 | { .name = "fstat", .errmsg = true, .alias = "newfstat", |
48000a1a | 971 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 972 | { .name = "fstatat", .errmsg = true, .alias = "newfstatat", |
34221118 ACM |
973 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ |
974 | [1] = SCA_FILENAME, /* filename */ }, }, | |
75b757ca | 975 | { .name = "fstatfs", .errmsg = true, |
48000a1a | 976 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 977 | { .name = "fsync", .errmsg = true, |
48000a1a | 978 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 979 | { .name = "ftruncate", .errmsg = true, |
48000a1a | 980 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
f9da0b0c ACM |
981 | { .name = "futex", .errmsg = true, |
982 | .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, }, | |
75b757ca | 983 | { .name = "futimesat", .errmsg = true, |
090389b6 ACM |
984 | .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ |
985 | [1] = SCA_FILENAME, /* filename */ }, }, | |
75b757ca | 986 | { .name = "getdents", .errmsg = true, |
48000a1a | 987 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 988 | { .name = "getdents64", .errmsg = true, |
48000a1a | 989 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
453350dd | 990 | { .name = "getitimer", .errmsg = true, STRARRAY(0, which, itimers), }, |
c65f1070 | 991 | { .name = "getpid", .errpid = true, }, |
d1d438a3 | 992 | { .name = "getpgid", .errpid = true, }, |
c65f1070 | 993 | { .name = "getppid", .errpid = true, }, |
39878d49 ACM |
994 | { .name = "getrandom", .errmsg = true, |
995 | .arg_scnprintf = { [2] = SCA_GETRANDOM_FLAGS, /* flags */ }, }, | |
453350dd | 996 | { .name = "getrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), }, |
34221118 ACM |
997 | { .name = "getxattr", .errmsg = true, |
998 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
999 | { .name = "inotify_add_watch", .errmsg = true, | |
1000 | .arg_scnprintf = { [1] = SCA_FILENAME, /* pathname */ }, }, | |
beccb2b5 | 1001 | { .name = "ioctl", .errmsg = true, |
48000a1a | 1002 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ |
844ae5b4 ACM |
1003 | #if defined(__i386__) || defined(__x86_64__) |
1004 | /* | |
1005 | * FIXME: Make this available to all arches. | |
1006 | */ | |
78645cf3 ACM |
1007 | [1] = SCA_STRHEXARRAY, /* cmd */ |
1008 | [2] = SCA_HEX, /* arg */ }, | |
1009 | .arg_parm = { [1] = &strarray__tioctls, /* cmd */ }, }, | |
844ae5b4 ACM |
1010 | #else |
1011 | [2] = SCA_HEX, /* arg */ }, }, | |
1012 | #endif | |
b62bee1b | 1013 | { .name = "keyctl", .errmsg = true, STRARRAY(0, option, keyctl_options), }, |
8bad5b0a ACM |
1014 | { .name = "kill", .errmsg = true, |
1015 | .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, }, | |
34221118 ACM |
1016 | { .name = "lchown", .errmsg = true, |
1017 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, }, | |
1018 | { .name = "lgetxattr", .errmsg = true, | |
1019 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
75b757ca | 1020 | { .name = "linkat", .errmsg = true, |
48000a1a | 1021 | .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, }, |
34221118 ACM |
1022 | { .name = "listxattr", .errmsg = true, |
1023 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
090389b6 ACM |
1024 | { .name = "llistxattr", .errmsg = true, |
1025 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
1026 | { .name = "lremovexattr", .errmsg = true, | |
1027 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
75b757ca ACM |
1028 | { .name = "lseek", .errmsg = true, |
1029 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ | |
1030 | [2] = SCA_STRARRAY, /* whence */ }, | |
1031 | .arg_parm = { [2] = &strarray__whences, /* whence */ }, }, | |
34221118 ACM |
1032 | { .name = "lsetxattr", .errmsg = true, |
1033 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
090389b6 ACM |
1034 | { .name = "lstat", .errmsg = true, .alias = "newlstat", |
1035 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, }, | |
34221118 ACM |
1036 | { .name = "lsxattr", .errmsg = true, |
1037 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
9e9716d1 ACM |
1038 | { .name = "madvise", .errmsg = true, |
1039 | .arg_scnprintf = { [0] = SCA_HEX, /* start */ | |
1040 | [2] = SCA_MADV_BHV, /* behavior */ }, }, | |
34221118 ACM |
1041 | { .name = "mkdir", .errmsg = true, |
1042 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
75b757ca | 1043 | { .name = "mkdirat", .errmsg = true, |
34221118 ACM |
1044 | .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ |
1045 | [1] = SCA_FILENAME, /* pathname */ }, }, | |
1046 | { .name = "mknod", .errmsg = true, | |
1047 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, }, | |
75b757ca | 1048 | { .name = "mknodat", .errmsg = true, |
090389b6 ACM |
1049 | .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ |
1050 | [1] = SCA_FILENAME, /* filename */ }, }, | |
3d903aa7 ACM |
1051 | { .name = "mlock", .errmsg = true, |
1052 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, }, | |
1053 | { .name = "mlockall", .errmsg = true, | |
1054 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, }, | |
beccb2b5 | 1055 | { .name = "mmap", .hexret = true, |
ae685380 | 1056 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ |
941557e0 | 1057 | [2] = SCA_MMAP_PROT, /* prot */ |
73faab3a NK |
1058 | [3] = SCA_MMAP_FLAGS, /* flags */ |
1059 | [4] = SCA_FD, /* fd */ }, }, | |
beccb2b5 | 1060 | { .name = "mprotect", .errmsg = true, |
ae685380 ACM |
1061 | .arg_scnprintf = { [0] = SCA_HEX, /* start */ |
1062 | [2] = SCA_MMAP_PROT, /* prot */ }, }, | |
090389b6 ACM |
1063 | { .name = "mq_unlink", .errmsg = true, |
1064 | .arg_scnprintf = { [0] = SCA_FILENAME, /* u_name */ }, }, | |
ae685380 ACM |
1065 | { .name = "mremap", .hexret = true, |
1066 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ | |
86998dda | 1067 | [3] = SCA_MREMAP_FLAGS, /* flags */ |
ae685380 | 1068 | [4] = SCA_HEX, /* new_addr */ }, }, |
3d903aa7 ACM |
1069 | { .name = "munlock", .errmsg = true, |
1070 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, }, | |
beccb2b5 ACM |
1071 | { .name = "munmap", .errmsg = true, |
1072 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, }, | |
75b757ca | 1073 | { .name = "name_to_handle_at", .errmsg = true, |
48000a1a | 1074 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, }, |
75b757ca | 1075 | { .name = "newfstatat", .errmsg = true, |
34221118 ACM |
1076 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ |
1077 | [1] = SCA_FILENAME, /* filename */ }, }, | |
be65a89a | 1078 | { .name = "open", .errmsg = true, |
f994592d ACM |
1079 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ |
1080 | [1] = SCA_OPEN_FLAGS, /* flags */ }, }, | |
31cd3855 | 1081 | { .name = "open_by_handle_at", .errmsg = true, |
75b757ca ACM |
1082 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ |
1083 | [2] = SCA_OPEN_FLAGS, /* flags */ }, }, | |
31cd3855 | 1084 | { .name = "openat", .errmsg = true, |
75b757ca | 1085 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ |
34221118 | 1086 | [1] = SCA_FILENAME, /* filename */ |
75b757ca | 1087 | [2] = SCA_OPEN_FLAGS, /* flags */ }, }, |
a1c2552d ACM |
1088 | { .name = "perf_event_open", .errmsg = true, |
1089 | .arg_scnprintf = { [1] = SCA_INT, /* pid */ | |
1090 | [2] = SCA_INT, /* cpu */ | |
1091 | [3] = SCA_FD, /* group_fd */ | |
1092 | [4] = SCA_PERF_FLAGS, /* flags */ }, }, | |
46cce19b ACM |
1093 | { .name = "pipe2", .errmsg = true, |
1094 | .arg_scnprintf = { [1] = SCA_PIPE_FLAGS, /* flags */ }, }, | |
aec1930b ACM |
1095 | { .name = "poll", .errmsg = true, .timeout = true, }, |
1096 | { .name = "ppoll", .errmsg = true, .timeout = true, }, | |
75b757ca | 1097 | { .name = "pread", .errmsg = true, .alias = "pread64", |
48000a1a | 1098 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 1099 | { .name = "preadv", .errmsg = true, .alias = "pread", |
48000a1a | 1100 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
453350dd | 1101 | { .name = "prlimit64", .errmsg = true, STRARRAY(1, resource, rlimit_resources), }, |
75b757ca | 1102 | { .name = "pwrite", .errmsg = true, .alias = "pwrite64", |
48000a1a | 1103 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 1104 | { .name = "pwritev", .errmsg = true, |
48000a1a | 1105 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 1106 | { .name = "read", .errmsg = true, |
48000a1a | 1107 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
34221118 ACM |
1108 | { .name = "readlink", .errmsg = true, |
1109 | .arg_scnprintf = { [0] = SCA_FILENAME, /* path */ }, }, | |
75b757ca | 1110 | { .name = "readlinkat", .errmsg = true, |
34221118 ACM |
1111 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ |
1112 | [1] = SCA_FILENAME, /* pathname */ }, }, | |
75b757ca | 1113 | { .name = "readv", .errmsg = true, |
48000a1a | 1114 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
b2cc99fd | 1115 | { .name = "recvfrom", .errmsg = true, |
8d8c66a2 ACM |
1116 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ |
1117 | [3] = SCA_MSG_FLAGS, /* flags */ }, }, | |
b2cc99fd | 1118 | { .name = "recvmmsg", .errmsg = true, |
8d8c66a2 ACM |
1119 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ |
1120 | [3] = SCA_MSG_FLAGS, /* flags */ }, }, | |
b2cc99fd | 1121 | { .name = "recvmsg", .errmsg = true, |
8d8c66a2 ACM |
1122 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ |
1123 | [2] = SCA_MSG_FLAGS, /* flags */ }, }, | |
34221118 ACM |
1124 | { .name = "removexattr", .errmsg = true, |
1125 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
75b757ca | 1126 | { .name = "renameat", .errmsg = true, |
48000a1a | 1127 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, }, |
34221118 ACM |
1128 | { .name = "rmdir", .errmsg = true, |
1129 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
8bad5b0a ACM |
1130 | { .name = "rt_sigaction", .errmsg = true, |
1131 | .arg_scnprintf = { [0] = SCA_SIGNUM, /* sig */ }, }, | |
453350dd | 1132 | { .name = "rt_sigprocmask", .errmsg = true, STRARRAY(0, how, sighow), }, |
8bad5b0a ACM |
1133 | { .name = "rt_sigqueueinfo", .errmsg = true, |
1134 | .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, }, | |
1135 | { .name = "rt_tgsigqueueinfo", .errmsg = true, | |
1136 | .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, }, | |
a3bca91f ACM |
1137 | { .name = "sched_setscheduler", .errmsg = true, |
1138 | .arg_scnprintf = { [1] = SCA_SCHED_POLICY, /* policy */ }, }, | |
997bba8c ACM |
1139 | { .name = "seccomp", .errmsg = true, |
1140 | .arg_scnprintf = { [0] = SCA_SECCOMP_OP, /* op */ | |
1141 | [1] = SCA_SECCOMP_FLAGS, /* flags */ }, }, | |
aec1930b | 1142 | { .name = "select", .errmsg = true, .timeout = true, }, |
b2cc99fd | 1143 | { .name = "sendmmsg", .errmsg = true, |
8d8c66a2 ACM |
1144 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ |
1145 | [3] = SCA_MSG_FLAGS, /* flags */ }, }, | |
b2cc99fd | 1146 | { .name = "sendmsg", .errmsg = true, |
8d8c66a2 ACM |
1147 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ |
1148 | [2] = SCA_MSG_FLAGS, /* flags */ }, }, | |
b2cc99fd | 1149 | { .name = "sendto", .errmsg = true, |
8d8c66a2 ACM |
1150 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ |
1151 | [3] = SCA_MSG_FLAGS, /* flags */ }, }, | |
c65f1070 | 1152 | { .name = "set_tid_address", .errpid = true, }, |
453350dd | 1153 | { .name = "setitimer", .errmsg = true, STRARRAY(0, which, itimers), }, |
d1d438a3 | 1154 | { .name = "setpgid", .errmsg = true, }, |
453350dd | 1155 | { .name = "setrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), }, |
34221118 ACM |
1156 | { .name = "setxattr", .errmsg = true, |
1157 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
75b757ca | 1158 | { .name = "shutdown", .errmsg = true, |
48000a1a | 1159 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
e10bce81 | 1160 | { .name = "socket", .errmsg = true, |
a28b24b2 ACM |
1161 | .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */ |
1162 | [1] = SCA_SK_TYPE, /* type */ }, | |
07120aa5 ACM |
1163 | .arg_parm = { [0] = &strarray__socket_families, /* family */ }, }, |
1164 | { .name = "socketpair", .errmsg = true, | |
1165 | .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */ | |
1166 | [1] = SCA_SK_TYPE, /* type */ }, | |
e10bce81 | 1167 | .arg_parm = { [0] = &strarray__socket_families, /* family */ }, }, |
090389b6 ACM |
1168 | { .name = "stat", .errmsg = true, .alias = "newstat", |
1169 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
34221118 ACM |
1170 | { .name = "statfs", .errmsg = true, |
1171 | .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, }, | |
1172 | { .name = "swapoff", .errmsg = true, | |
1173 | .arg_scnprintf = { [0] = SCA_FILENAME, /* specialfile */ }, }, | |
1174 | { .name = "swapon", .errmsg = true, | |
1175 | .arg_scnprintf = { [0] = SCA_FILENAME, /* specialfile */ }, }, | |
75b757ca | 1176 | { .name = "symlinkat", .errmsg = true, |
48000a1a | 1177 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, }, |
8bad5b0a ACM |
1178 | { .name = "tgkill", .errmsg = true, |
1179 | .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, }, | |
1180 | { .name = "tkill", .errmsg = true, | |
1181 | .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, }, | |
34221118 ACM |
1182 | { .name = "truncate", .errmsg = true, |
1183 | .arg_scnprintf = { [0] = SCA_FILENAME, /* path */ }, }, | |
e5959683 | 1184 | { .name = "uname", .errmsg = true, .alias = "newuname", }, |
75b757ca | 1185 | { .name = "unlinkat", .errmsg = true, |
34221118 ACM |
1186 | .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ |
1187 | [1] = SCA_FILENAME, /* pathname */ }, }, | |
1188 | { .name = "utime", .errmsg = true, | |
1189 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, }, | |
75b757ca | 1190 | { .name = "utimensat", .errmsg = true, |
34221118 ACM |
1191 | .arg_scnprintf = { [0] = SCA_FDAT, /* dirfd */ |
1192 | [1] = SCA_FILENAME, /* filename */ }, }, | |
1193 | { .name = "utimes", .errmsg = true, | |
1194 | .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, }, | |
090389b6 ACM |
1195 | { .name = "vmsplice", .errmsg = true, |
1196 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, | |
11c8e39f | 1197 | { .name = "wait4", .errpid = true, |
7206b900 | 1198 | .arg_scnprintf = { [2] = SCA_WAITID_OPTIONS, /* options */ }, }, |
11c8e39f | 1199 | { .name = "waitid", .errpid = true, |
7206b900 | 1200 | .arg_scnprintf = { [3] = SCA_WAITID_OPTIONS, /* options */ }, }, |
75b757ca | 1201 | { .name = "write", .errmsg = true, |
48000a1a | 1202 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
75b757ca | 1203 | { .name = "writev", .errmsg = true, |
48000a1a | 1204 | .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, }, |
514f1c67 ACM |
1205 | }; |
1206 | ||
1207 | static int syscall_fmt__cmp(const void *name, const void *fmtp) | |
1208 | { | |
1209 | const struct syscall_fmt *fmt = fmtp; | |
1210 | return strcmp(name, fmt->name); | |
1211 | } | |
1212 | ||
1213 | static struct syscall_fmt *syscall_fmt__find(const char *name) | |
1214 | { | |
1215 | const int nmemb = ARRAY_SIZE(syscall_fmts); | |
1216 | return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); | |
1217 | } | |
1218 | ||
1219 | struct syscall { | |
1220 | struct event_format *tp_format; | |
f208bd8d ACM |
1221 | int nr_args; |
1222 | struct format_field *args; | |
514f1c67 | 1223 | const char *name; |
5089f20e | 1224 | bool is_exit; |
514f1c67 | 1225 | struct syscall_fmt *fmt; |
01533e97 | 1226 | size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg); |
1f115cb7 | 1227 | void **arg_parm; |
514f1c67 ACM |
1228 | }; |
1229 | ||
60c907ab ACM |
1230 | static size_t fprintf_duration(unsigned long t, FILE *fp) |
1231 | { | |
1232 | double duration = (double)t / NSEC_PER_MSEC; | |
1233 | size_t printed = fprintf(fp, "("); | |
1234 | ||
1235 | if (duration >= 1.0) | |
1236 | printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); | |
1237 | else if (duration >= 0.01) | |
1238 | printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); | |
1239 | else | |
1240 | printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); | |
c24ff998 | 1241 | return printed + fprintf(fp, "): "); |
60c907ab ACM |
1242 | } |
1243 | ||
f994592d ACM |
1244 | /** |
1245 | * filename.ptr: The filename char pointer that will be vfs_getname'd | |
1246 | * filename.entry_str_pos: Where to insert the string translated from | |
1247 | * filename.ptr by the vfs_getname tracepoint/kprobe. | |
1248 | */ | |
752fde44 ACM |
1249 | struct thread_trace { |
1250 | u64 entry_time; | |
1251 | u64 exit_time; | |
1252 | bool entry_pending; | |
efd5745e | 1253 | unsigned long nr_events; |
a2ea67d7 | 1254 | unsigned long pfmaj, pfmin; |
752fde44 | 1255 | char *entry_str; |
1302d88e | 1256 | double runtime_ms; |
f994592d ACM |
1257 | struct { |
1258 | unsigned long ptr; | |
7f4f8001 ACM |
1259 | short int entry_str_pos; |
1260 | bool pending_open; | |
1261 | unsigned int namelen; | |
1262 | char *name; | |
f994592d | 1263 | } filename; |
75b757ca ACM |
1264 | struct { |
1265 | int max; | |
1266 | char **table; | |
1267 | } paths; | |
bf2575c1 DA |
1268 | |
1269 | struct intlist *syscall_stats; | |
752fde44 ACM |
1270 | }; |
1271 | ||
1272 | static struct thread_trace *thread_trace__new(void) | |
1273 | { | |
75b757ca ACM |
1274 | struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace)); |
1275 | ||
1276 | if (ttrace) | |
1277 | ttrace->paths.max = -1; | |
1278 | ||
bf2575c1 DA |
1279 | ttrace->syscall_stats = intlist__new(NULL); |
1280 | ||
75b757ca | 1281 | return ttrace; |
752fde44 ACM |
1282 | } |
1283 | ||
c24ff998 | 1284 | static struct thread_trace *thread__trace(struct thread *thread, FILE *fp) |
752fde44 | 1285 | { |
efd5745e ACM |
1286 | struct thread_trace *ttrace; |
1287 | ||
752fde44 ACM |
1288 | if (thread == NULL) |
1289 | goto fail; | |
1290 | ||
89dceb22 NK |
1291 | if (thread__priv(thread) == NULL) |
1292 | thread__set_priv(thread, thread_trace__new()); | |
48000a1a | 1293 | |
89dceb22 | 1294 | if (thread__priv(thread) == NULL) |
752fde44 ACM |
1295 | goto fail; |
1296 | ||
89dceb22 | 1297 | ttrace = thread__priv(thread); |
efd5745e ACM |
1298 | ++ttrace->nr_events; |
1299 | ||
1300 | return ttrace; | |
752fde44 | 1301 | fail: |
c24ff998 | 1302 | color_fprintf(fp, PERF_COLOR_RED, |
752fde44 ACM |
1303 | "WARNING: not enough memory, dropping samples!\n"); |
1304 | return NULL; | |
1305 | } | |
1306 | ||
598d02c5 SF |
1307 | #define TRACE_PFMAJ (1 << 0) |
1308 | #define TRACE_PFMIN (1 << 1) | |
1309 | ||
e4d44e83 ACM |
1310 | static const size_t trace__entry_str_size = 2048; |
1311 | ||
97119f37 | 1312 | static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname) |
75b757ca | 1313 | { |
89dceb22 | 1314 | struct thread_trace *ttrace = thread__priv(thread); |
75b757ca ACM |
1315 | |
1316 | if (fd > ttrace->paths.max) { | |
1317 | char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *)); | |
1318 | ||
1319 | if (npath == NULL) | |
1320 | return -1; | |
1321 | ||
1322 | if (ttrace->paths.max != -1) { | |
1323 | memset(npath + ttrace->paths.max + 1, 0, | |
1324 | (fd - ttrace->paths.max) * sizeof(char *)); | |
1325 | } else { | |
1326 | memset(npath, 0, (fd + 1) * sizeof(char *)); | |
1327 | } | |
1328 | ||
1329 | ttrace->paths.table = npath; | |
1330 | ttrace->paths.max = fd; | |
1331 | } | |
1332 | ||
1333 | ttrace->paths.table[fd] = strdup(pathname); | |
1334 | ||
1335 | return ttrace->paths.table[fd] != NULL ? 0 : -1; | |
1336 | } | |
1337 | ||
97119f37 ACM |
1338 | static int thread__read_fd_path(struct thread *thread, int fd) |
1339 | { | |
1340 | char linkname[PATH_MAX], pathname[PATH_MAX]; | |
1341 | struct stat st; | |
1342 | int ret; | |
1343 | ||
1344 | if (thread->pid_ == thread->tid) { | |
1345 | scnprintf(linkname, sizeof(linkname), | |
1346 | "/proc/%d/fd/%d", thread->pid_, fd); | |
1347 | } else { | |
1348 | scnprintf(linkname, sizeof(linkname), | |
1349 | "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd); | |
1350 | } | |
1351 | ||
1352 | if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname)) | |
1353 | return -1; | |
1354 | ||
1355 | ret = readlink(linkname, pathname, sizeof(pathname)); | |
1356 | ||
1357 | if (ret < 0 || ret > st.st_size) | |
1358 | return -1; | |
1359 | ||
1360 | pathname[ret] = '\0'; | |
1361 | return trace__set_fd_pathname(thread, fd, pathname); | |
1362 | } | |
1363 | ||
c522739d ACM |
1364 | static const char *thread__fd_path(struct thread *thread, int fd, |
1365 | struct trace *trace) | |
75b757ca | 1366 | { |
89dceb22 | 1367 | struct thread_trace *ttrace = thread__priv(thread); |
75b757ca ACM |
1368 | |
1369 | if (ttrace == NULL) | |
1370 | return NULL; | |
1371 | ||
1372 | if (fd < 0) | |
1373 | return NULL; | |
1374 | ||
cdcd1e6b | 1375 | if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) { |
c522739d ACM |
1376 | if (!trace->live) |
1377 | return NULL; | |
1378 | ++trace->stats.proc_getname; | |
cdcd1e6b | 1379 | if (thread__read_fd_path(thread, fd)) |
c522739d ACM |
1380 | return NULL; |
1381 | } | |
75b757ca ACM |
1382 | |
1383 | return ttrace->paths.table[fd]; | |
1384 | } | |
1385 | ||
1386 | static size_t syscall_arg__scnprintf_fd(char *bf, size_t size, | |
1387 | struct syscall_arg *arg) | |
1388 | { | |
1389 | int fd = arg->val; | |
1390 | size_t printed = scnprintf(bf, size, "%d", fd); | |
c522739d | 1391 | const char *path = thread__fd_path(arg->thread, fd, arg->trace); |
75b757ca ACM |
1392 | |
1393 | if (path) | |
1394 | printed += scnprintf(bf + printed, size - printed, "<%s>", path); | |
1395 | ||
1396 | return printed; | |
1397 | } | |
1398 | ||
1399 | static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, | |
1400 | struct syscall_arg *arg) | |
1401 | { | |
1402 | int fd = arg->val; | |
1403 | size_t printed = syscall_arg__scnprintf_fd(bf, size, arg); | |
89dceb22 | 1404 | struct thread_trace *ttrace = thread__priv(arg->thread); |
75b757ca | 1405 | |
04662523 ACM |
1406 | if (ttrace && fd >= 0 && fd <= ttrace->paths.max) |
1407 | zfree(&ttrace->paths.table[fd]); | |
75b757ca ACM |
1408 | |
1409 | return printed; | |
1410 | } | |
1411 | ||
f994592d ACM |
1412 | static void thread__set_filename_pos(struct thread *thread, const char *bf, |
1413 | unsigned long ptr) | |
1414 | { | |
1415 | struct thread_trace *ttrace = thread__priv(thread); | |
1416 | ||
1417 | ttrace->filename.ptr = ptr; | |
1418 | ttrace->filename.entry_str_pos = bf - ttrace->entry_str; | |
1419 | } | |
1420 | ||
1421 | static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, | |
1422 | struct syscall_arg *arg) | |
1423 | { | |
1424 | unsigned long ptr = arg->val; | |
1425 | ||
1426 | if (!arg->trace->vfs_getname) | |
1427 | return scnprintf(bf, size, "%#x", ptr); | |
1428 | ||
1429 | thread__set_filename_pos(arg->thread, bf, ptr); | |
1430 | return 0; | |
1431 | } | |
1432 | ||
ae9ed035 ACM |
1433 | static bool trace__filter_duration(struct trace *trace, double t) |
1434 | { | |
1435 | return t < (trace->duration_filter * NSEC_PER_MSEC); | |
1436 | } | |
1437 | ||
752fde44 ACM |
1438 | static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) |
1439 | { | |
1440 | double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; | |
1441 | ||
60c907ab | 1442 | return fprintf(fp, "%10.3f ", ts); |
752fde44 ACM |
1443 | } |
1444 | ||
f15eb531 | 1445 | static bool done = false; |
ba209f85 | 1446 | static bool interrupted = false; |
f15eb531 | 1447 | |
ba209f85 | 1448 | static void sig_handler(int sig) |
f15eb531 NK |
1449 | { |
1450 | done = true; | |
ba209f85 | 1451 | interrupted = sig == SIGINT; |
f15eb531 NK |
1452 | } |
1453 | ||
752fde44 | 1454 | static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread, |
60c907ab | 1455 | u64 duration, u64 tstamp, FILE *fp) |
752fde44 ACM |
1456 | { |
1457 | size_t printed = trace__fprintf_tstamp(trace, tstamp, fp); | |
60c907ab | 1458 | printed += fprintf_duration(duration, fp); |
752fde44 | 1459 | |
50c95cbd ACM |
1460 | if (trace->multiple_threads) { |
1461 | if (trace->show_comm) | |
1902efe7 | 1462 | printed += fprintf(fp, "%.14s/", thread__comm_str(thread)); |
38051234 | 1463 | printed += fprintf(fp, "%d ", thread->tid); |
50c95cbd | 1464 | } |
752fde44 ACM |
1465 | |
1466 | return printed; | |
1467 | } | |
1468 | ||
c24ff998 | 1469 | static int trace__process_event(struct trace *trace, struct machine *machine, |
162f0bef | 1470 | union perf_event *event, struct perf_sample *sample) |
752fde44 ACM |
1471 | { |
1472 | int ret = 0; | |
1473 | ||
1474 | switch (event->header.type) { | |
1475 | case PERF_RECORD_LOST: | |
c24ff998 | 1476 | color_fprintf(trace->output, PERF_COLOR_RED, |
752fde44 | 1477 | "LOST %" PRIu64 " events!\n", event->lost.lost); |
162f0bef | 1478 | ret = machine__process_lost_event(machine, event, sample); |
3ed5ca2e | 1479 | break; |
752fde44 | 1480 | default: |
162f0bef | 1481 | ret = machine__process_event(machine, event, sample); |
752fde44 ACM |
1482 | break; |
1483 | } | |
1484 | ||
1485 | return ret; | |
1486 | } | |
1487 | ||
c24ff998 | 1488 | static int trace__tool_process(struct perf_tool *tool, |
752fde44 | 1489 | union perf_event *event, |
162f0bef | 1490 | struct perf_sample *sample, |
752fde44 ACM |
1491 | struct machine *machine) |
1492 | { | |
c24ff998 | 1493 | struct trace *trace = container_of(tool, struct trace, tool); |
162f0bef | 1494 | return trace__process_event(trace, machine, event, sample); |
752fde44 ACM |
1495 | } |
1496 | ||
1497 | static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) | |
1498 | { | |
0a7e6d1b | 1499 | int err = symbol__init(NULL); |
752fde44 ACM |
1500 | |
1501 | if (err) | |
1502 | return err; | |
1503 | ||
8fb598e5 DA |
1504 | trace->host = machine__new_host(); |
1505 | if (trace->host == NULL) | |
1506 | return -ENOMEM; | |
752fde44 | 1507 | |
959c2199 | 1508 | if (trace_event__register_resolver(trace->host, machine__resolve_kernel_addr) < 0) |
706c3da4 ACM |
1509 | return -errno; |
1510 | ||
a33fbd56 | 1511 | err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target, |
9d9cad76 KL |
1512 | evlist->threads, trace__tool_process, false, |
1513 | trace->opts.proc_map_timeout); | |
752fde44 ACM |
1514 | if (err) |
1515 | symbol__exit(); | |
1516 | ||
1517 | return err; | |
1518 | } | |
1519 | ||
13d4ff3e ACM |
1520 | static int syscall__set_arg_fmts(struct syscall *sc) |
1521 | { | |
1522 | struct format_field *field; | |
1523 | int idx = 0; | |
1524 | ||
f208bd8d | 1525 | sc->arg_scnprintf = calloc(sc->nr_args, sizeof(void *)); |
13d4ff3e ACM |
1526 | if (sc->arg_scnprintf == NULL) |
1527 | return -1; | |
1528 | ||
1f115cb7 ACM |
1529 | if (sc->fmt) |
1530 | sc->arg_parm = sc->fmt->arg_parm; | |
1531 | ||
f208bd8d | 1532 | for (field = sc->args; field; field = field->next) { |
beccb2b5 ACM |
1533 | if (sc->fmt && sc->fmt->arg_scnprintf[idx]) |
1534 | sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx]; | |
1535 | else if (field->flags & FIELD_IS_POINTER) | |
13d4ff3e | 1536 | sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex; |
d1d438a3 ACM |
1537 | else if (strcmp(field->type, "pid_t") == 0) |
1538 | sc->arg_scnprintf[idx] = SCA_PID; | |
ba2f22cf ACM |
1539 | else if (strcmp(field->type, "umode_t") == 0) |
1540 | sc->arg_scnprintf[idx] = SCA_MODE_T; | |
13d4ff3e ACM |
1541 | ++idx; |
1542 | } | |
1543 | ||
1544 | return 0; | |
1545 | } | |
1546 | ||
514f1c67 ACM |
1547 | static int trace__read_syscall_info(struct trace *trace, int id) |
1548 | { | |
1549 | char tp_name[128]; | |
1550 | struct syscall *sc; | |
fd0db102 | 1551 | const char *name = syscalltbl__name(trace->sctbl, id); |
3a531260 ACM |
1552 | |
1553 | if (name == NULL) | |
1554 | return -1; | |
514f1c67 ACM |
1555 | |
1556 | if (id > trace->syscalls.max) { | |
1557 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); | |
1558 | ||
1559 | if (nsyscalls == NULL) | |
1560 | return -1; | |
1561 | ||
1562 | if (trace->syscalls.max != -1) { | |
1563 | memset(nsyscalls + trace->syscalls.max + 1, 0, | |
1564 | (id - trace->syscalls.max) * sizeof(*sc)); | |
1565 | } else { | |
1566 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); | |
1567 | } | |
1568 | ||
1569 | trace->syscalls.table = nsyscalls; | |
1570 | trace->syscalls.max = id; | |
1571 | } | |
1572 | ||
1573 | sc = trace->syscalls.table + id; | |
3a531260 | 1574 | sc->name = name; |
2ae3a312 | 1575 | |
3a531260 | 1576 | sc->fmt = syscall_fmt__find(sc->name); |
514f1c67 | 1577 | |
aec1930b | 1578 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
97978b3e | 1579 | sc->tp_format = trace_event__tp_format("syscalls", tp_name); |
aec1930b | 1580 | |
8dd2a131 | 1581 | if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) { |
aec1930b | 1582 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); |
97978b3e | 1583 | sc->tp_format = trace_event__tp_format("syscalls", tp_name); |
aec1930b | 1584 | } |
514f1c67 | 1585 | |
8dd2a131 | 1586 | if (IS_ERR(sc->tp_format)) |
13d4ff3e ACM |
1587 | return -1; |
1588 | ||
f208bd8d ACM |
1589 | sc->args = sc->tp_format->format.fields; |
1590 | sc->nr_args = sc->tp_format->format.nr_fields; | |
c42de706 TS |
1591 | /* |
1592 | * We need to check and discard the first variable '__syscall_nr' | |
1593 | * or 'nr' that mean the syscall number. It is needless here. | |
1594 | * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. | |
1595 | */ | |
1596 | if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) { | |
f208bd8d ACM |
1597 | sc->args = sc->args->next; |
1598 | --sc->nr_args; | |
1599 | } | |
1600 | ||
5089f20e ACM |
1601 | sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit"); |
1602 | ||
13d4ff3e | 1603 | return syscall__set_arg_fmts(sc); |
514f1c67 ACM |
1604 | } |
1605 | ||
d0cc439b ACM |
1606 | static int trace__validate_ev_qualifier(struct trace *trace) |
1607 | { | |
8b3ce757 | 1608 | int err = 0, i; |
d0cc439b ACM |
1609 | struct str_node *pos; |
1610 | ||
8b3ce757 ACM |
1611 | trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier); |
1612 | trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr * | |
1613 | sizeof(trace->ev_qualifier_ids.entries[0])); | |
1614 | ||
1615 | if (trace->ev_qualifier_ids.entries == NULL) { | |
1616 | fputs("Error:\tNot enough memory for allocating events qualifier ids\n", | |
1617 | trace->output); | |
1618 | err = -EINVAL; | |
1619 | goto out; | |
1620 | } | |
1621 | ||
1622 | i = 0; | |
1623 | ||
d0cc439b ACM |
1624 | strlist__for_each(pos, trace->ev_qualifier) { |
1625 | const char *sc = pos->s; | |
fd0db102 | 1626 | int id = syscalltbl__id(trace->sctbl, sc); |
d0cc439b | 1627 | |
8b3ce757 | 1628 | if (id < 0) { |
d0cc439b ACM |
1629 | if (err == 0) { |
1630 | fputs("Error:\tInvalid syscall ", trace->output); | |
1631 | err = -EINVAL; | |
1632 | } else { | |
1633 | fputs(", ", trace->output); | |
1634 | } | |
1635 | ||
1636 | fputs(sc, trace->output); | |
1637 | } | |
8b3ce757 ACM |
1638 | |
1639 | trace->ev_qualifier_ids.entries[i++] = id; | |
d0cc439b ACM |
1640 | } |
1641 | ||
1642 | if (err < 0) { | |
1643 | fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'" | |
1644 | "\nHint:\tand: 'man syscalls'\n", trace->output); | |
8b3ce757 ACM |
1645 | zfree(&trace->ev_qualifier_ids.entries); |
1646 | trace->ev_qualifier_ids.nr = 0; | |
d0cc439b | 1647 | } |
8b3ce757 | 1648 | out: |
d0cc439b ACM |
1649 | return err; |
1650 | } | |
1651 | ||
55d43bca DA |
1652 | /* |
1653 | * args is to be interpreted as a series of longs but we need to handle | |
1654 | * 8-byte unaligned accesses. args points to raw_data within the event | |
1655 | * and raw_data is guaranteed to be 8-byte unaligned because it is | |
1656 | * preceded by raw_size which is a u32. So we need to copy args to a temp | |
1657 | * variable to read it. Most notably this avoids extended load instructions | |
1658 | * on unaligned addresses | |
1659 | */ | |
1660 | ||
752fde44 | 1661 | static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, |
55d43bca | 1662 | unsigned char *args, struct trace *trace, |
75b757ca | 1663 | struct thread *thread) |
514f1c67 | 1664 | { |
514f1c67 | 1665 | size_t printed = 0; |
55d43bca DA |
1666 | unsigned char *p; |
1667 | unsigned long val; | |
514f1c67 | 1668 | |
f208bd8d | 1669 | if (sc->args != NULL) { |
514f1c67 | 1670 | struct format_field *field; |
01533e97 ACM |
1671 | u8 bit = 1; |
1672 | struct syscall_arg arg = { | |
75b757ca ACM |
1673 | .idx = 0, |
1674 | .mask = 0, | |
1675 | .trace = trace, | |
1676 | .thread = thread, | |
01533e97 | 1677 | }; |
6e7eeb51 | 1678 | |
f208bd8d | 1679 | for (field = sc->args; field; |
01533e97 ACM |
1680 | field = field->next, ++arg.idx, bit <<= 1) { |
1681 | if (arg.mask & bit) | |
6e7eeb51 | 1682 | continue; |
55d43bca DA |
1683 | |
1684 | /* special care for unaligned accesses */ | |
1685 | p = args + sizeof(unsigned long) * arg.idx; | |
1686 | memcpy(&val, p, sizeof(val)); | |
1687 | ||
4aa58232 ACM |
1688 | /* |
1689 | * Suppress this argument if its value is zero and | |
1690 | * and we don't have a string associated in an | |
1691 | * strarray for it. | |
1692 | */ | |
55d43bca | 1693 | if (val == 0 && |
4aa58232 ACM |
1694 | !(sc->arg_scnprintf && |
1695 | sc->arg_scnprintf[arg.idx] == SCA_STRARRAY && | |
1696 | sc->arg_parm[arg.idx])) | |
22ae5cf1 ACM |
1697 | continue; |
1698 | ||
752fde44 | 1699 | printed += scnprintf(bf + printed, size - printed, |
13d4ff3e | 1700 | "%s%s: ", printed ? ", " : "", field->name); |
01533e97 | 1701 | if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) { |
55d43bca | 1702 | arg.val = val; |
1f115cb7 ACM |
1703 | if (sc->arg_parm) |
1704 | arg.parm = sc->arg_parm[arg.idx]; | |
01533e97 ACM |
1705 | printed += sc->arg_scnprintf[arg.idx](bf + printed, |
1706 | size - printed, &arg); | |
6e7eeb51 | 1707 | } else { |
13d4ff3e | 1708 | printed += scnprintf(bf + printed, size - printed, |
55d43bca | 1709 | "%ld", val); |
6e7eeb51 | 1710 | } |
514f1c67 ACM |
1711 | } |
1712 | } else { | |
01533e97 ACM |
1713 | int i = 0; |
1714 | ||
514f1c67 | 1715 | while (i < 6) { |
55d43bca DA |
1716 | /* special care for unaligned accesses */ |
1717 | p = args + sizeof(unsigned long) * i; | |
1718 | memcpy(&val, p, sizeof(val)); | |
752fde44 ACM |
1719 | printed += scnprintf(bf + printed, size - printed, |
1720 | "%sarg%d: %ld", | |
55d43bca | 1721 | printed ? ", " : "", i, val); |
514f1c67 ACM |
1722 | ++i; |
1723 | } | |
1724 | } | |
1725 | ||
1726 | return printed; | |
1727 | } | |
1728 | ||
ba3d7dee | 1729 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 1730 | union perf_event *event, |
ba3d7dee ACM |
1731 | struct perf_sample *sample); |
1732 | ||
1733 | static struct syscall *trace__syscall_info(struct trace *trace, | |
bf2575c1 | 1734 | struct perf_evsel *evsel, int id) |
ba3d7dee | 1735 | { |
ba3d7dee ACM |
1736 | |
1737 | if (id < 0) { | |
adaa18bf ACM |
1738 | |
1739 | /* | |
1740 | * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried | |
1741 | * before that, leaving at a higher verbosity level till that is | |
1742 | * explained. Reproduced with plain ftrace with: | |
1743 | * | |
1744 | * echo 1 > /t/events/raw_syscalls/sys_exit/enable | |
1745 | * grep "NR -1 " /t/trace_pipe | |
1746 | * | |
1747 | * After generating some load on the machine. | |
1748 | */ | |
1749 | if (verbose > 1) { | |
1750 | static u64 n; | |
1751 | fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n", | |
1752 | id, perf_evsel__name(evsel), ++n); | |
1753 | } | |
ba3d7dee ACM |
1754 | return NULL; |
1755 | } | |
1756 | ||
1757 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && | |
1758 | trace__read_syscall_info(trace, id)) | |
1759 | goto out_cant_read; | |
1760 | ||
1761 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) | |
1762 | goto out_cant_read; | |
1763 | ||
1764 | return &trace->syscalls.table[id]; | |
1765 | ||
1766 | out_cant_read: | |
7c304ee0 ACM |
1767 | if (verbose) { |
1768 | fprintf(trace->output, "Problems reading syscall %d", id); | |
1769 | if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL) | |
1770 | fprintf(trace->output, "(%s)", trace->syscalls.table[id].name); | |
1771 | fputs(" information\n", trace->output); | |
1772 | } | |
ba3d7dee ACM |
1773 | return NULL; |
1774 | } | |
1775 | ||
bf2575c1 DA |
1776 | static void thread__update_stats(struct thread_trace *ttrace, |
1777 | int id, struct perf_sample *sample) | |
1778 | { | |
1779 | struct int_node *inode; | |
1780 | struct stats *stats; | |
1781 | u64 duration = 0; | |
1782 | ||
1783 | inode = intlist__findnew(ttrace->syscall_stats, id); | |
1784 | if (inode == NULL) | |
1785 | return; | |
1786 | ||
1787 | stats = inode->priv; | |
1788 | if (stats == NULL) { | |
1789 | stats = malloc(sizeof(struct stats)); | |
1790 | if (stats == NULL) | |
1791 | return; | |
1792 | init_stats(stats); | |
1793 | inode->priv = stats; | |
1794 | } | |
1795 | ||
1796 | if (ttrace->entry_time && sample->time > ttrace->entry_time) | |
1797 | duration = sample->time - ttrace->entry_time; | |
1798 | ||
1799 | update_stats(stats, duration); | |
1800 | } | |
1801 | ||
e596663e ACM |
1802 | static int trace__printf_interrupted_entry(struct trace *trace, struct perf_sample *sample) |
1803 | { | |
1804 | struct thread_trace *ttrace; | |
1805 | u64 duration; | |
1806 | size_t printed; | |
1807 | ||
1808 | if (trace->current == NULL) | |
1809 | return 0; | |
1810 | ||
1811 | ttrace = thread__priv(trace->current); | |
1812 | ||
1813 | if (!ttrace->entry_pending) | |
1814 | return 0; | |
1815 | ||
1816 | duration = sample->time - ttrace->entry_time; | |
1817 | ||
1818 | printed = trace__fprintf_entry_head(trace, trace->current, duration, sample->time, trace->output); | |
1819 | printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str); | |
1820 | ttrace->entry_pending = false; | |
1821 | ||
1822 | return printed; | |
1823 | } | |
1824 | ||
ba3d7dee | 1825 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 1826 | union perf_event *event __maybe_unused, |
ba3d7dee ACM |
1827 | struct perf_sample *sample) |
1828 | { | |
752fde44 | 1829 | char *msg; |
ba3d7dee | 1830 | void *args; |
752fde44 | 1831 | size_t printed = 0; |
2ae3a312 | 1832 | struct thread *thread; |
b91fc39f | 1833 | int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1; |
bf2575c1 | 1834 | struct syscall *sc = trace__syscall_info(trace, evsel, id); |
2ae3a312 ACM |
1835 | struct thread_trace *ttrace; |
1836 | ||
1837 | if (sc == NULL) | |
1838 | return -1; | |
ba3d7dee | 1839 | |
8fb598e5 | 1840 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
c24ff998 | 1841 | ttrace = thread__trace(thread, trace->output); |
2ae3a312 | 1842 | if (ttrace == NULL) |
b91fc39f | 1843 | goto out_put; |
ba3d7dee | 1844 | |
77170988 | 1845 | args = perf_evsel__sc_tp_ptr(evsel, args, sample); |
752fde44 ACM |
1846 | |
1847 | if (ttrace->entry_str == NULL) { | |
e4d44e83 | 1848 | ttrace->entry_str = malloc(trace__entry_str_size); |
752fde44 | 1849 | if (!ttrace->entry_str) |
b91fc39f | 1850 | goto out_put; |
752fde44 ACM |
1851 | } |
1852 | ||
5cf9c84e | 1853 | if (!(trace->duration_filter || trace->summary_only || trace->min_stack)) |
6ebad5c1 | 1854 | trace__printf_interrupted_entry(trace, sample); |
e596663e | 1855 | |
752fde44 ACM |
1856 | ttrace->entry_time = sample->time; |
1857 | msg = ttrace->entry_str; | |
e4d44e83 | 1858 | printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name); |
752fde44 | 1859 | |
e4d44e83 | 1860 | printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed, |
75b757ca | 1861 | args, trace, thread); |
752fde44 | 1862 | |
5089f20e | 1863 | if (sc->is_exit) { |
5cf9c84e | 1864 | if (!(trace->duration_filter || trace->summary_only || trace->min_stack)) { |
c24ff998 ACM |
1865 | trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output); |
1866 | fprintf(trace->output, "%-70s\n", ttrace->entry_str); | |
ae9ed035 | 1867 | } |
7f4f8001 | 1868 | } else { |
752fde44 | 1869 | ttrace->entry_pending = true; |
7f4f8001 ACM |
1870 | /* See trace__vfs_getname & trace__sys_exit */ |
1871 | ttrace->filename.pending_open = false; | |
1872 | } | |
ba3d7dee | 1873 | |
f3b623b8 ACM |
1874 | if (trace->current != thread) { |
1875 | thread__put(trace->current); | |
1876 | trace->current = thread__get(thread); | |
1877 | } | |
b91fc39f ACM |
1878 | err = 0; |
1879 | out_put: | |
1880 | thread__put(thread); | |
1881 | return err; | |
ba3d7dee ACM |
1882 | } |
1883 | ||
5cf9c84e ACM |
1884 | static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel, |
1885 | struct perf_sample *sample, | |
1886 | struct callchain_cursor *cursor) | |
202ff968 ACM |
1887 | { |
1888 | struct addr_location al; | |
5cf9c84e ACM |
1889 | |
1890 | if (machine__resolve(trace->host, &al, sample) < 0 || | |
1891 | thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, trace->max_stack)) | |
1892 | return -1; | |
1893 | ||
1894 | return 0; | |
1895 | } | |
1896 | ||
1897 | static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample) | |
1898 | { | |
202ff968 | 1899 | /* TODO: user-configurable print_opts */ |
e20ab86e ACM |
1900 | const unsigned int print_opts = EVSEL__PRINT_SYM | |
1901 | EVSEL__PRINT_DSO | | |
1902 | EVSEL__PRINT_UNKNOWN_AS_ADDR; | |
202ff968 | 1903 | |
d327e60c | 1904 | return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output); |
202ff968 ACM |
1905 | } |
1906 | ||
ba3d7dee | 1907 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 1908 | union perf_event *event __maybe_unused, |
ba3d7dee ACM |
1909 | struct perf_sample *sample) |
1910 | { | |
2c82c3ad | 1911 | long ret; |
60c907ab | 1912 | u64 duration = 0; |
2ae3a312 | 1913 | struct thread *thread; |
5cf9c84e | 1914 | int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0; |
bf2575c1 | 1915 | struct syscall *sc = trace__syscall_info(trace, evsel, id); |
2ae3a312 ACM |
1916 | struct thread_trace *ttrace; |
1917 | ||
1918 | if (sc == NULL) | |
1919 | return -1; | |
ba3d7dee | 1920 | |
8fb598e5 | 1921 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
c24ff998 | 1922 | ttrace = thread__trace(thread, trace->output); |
2ae3a312 | 1923 | if (ttrace == NULL) |
b91fc39f | 1924 | goto out_put; |
ba3d7dee | 1925 | |
bf2575c1 DA |
1926 | if (trace->summary) |
1927 | thread__update_stats(ttrace, id, sample); | |
1928 | ||
77170988 | 1929 | ret = perf_evsel__sc_tp_uint(evsel, ret, sample); |
ba3d7dee | 1930 | |
fd0db102 | 1931 | if (id == trace->open_id && ret >= 0 && ttrace->filename.pending_open) { |
7f4f8001 ACM |
1932 | trace__set_fd_pathname(thread, ret, ttrace->filename.name); |
1933 | ttrace->filename.pending_open = false; | |
c522739d ACM |
1934 | ++trace->stats.vfs_getname; |
1935 | } | |
1936 | ||
752fde44 ACM |
1937 | ttrace->exit_time = sample->time; |
1938 | ||
ae9ed035 | 1939 | if (ttrace->entry_time) { |
60c907ab | 1940 | duration = sample->time - ttrace->entry_time; |
ae9ed035 ACM |
1941 | if (trace__filter_duration(trace, duration)) |
1942 | goto out; | |
1943 | } else if (trace->duration_filter) | |
1944 | goto out; | |
60c907ab | 1945 | |
5cf9c84e ACM |
1946 | if (sample->callchain) { |
1947 | callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); | |
1948 | if (callchain_ret == 0) { | |
1949 | if (callchain_cursor.nr < trace->min_stack) | |
1950 | goto out; | |
1951 | callchain_ret = 1; | |
1952 | } | |
1953 | } | |
1954 | ||
fd2eabaf DA |
1955 | if (trace->summary_only) |
1956 | goto out; | |
1957 | ||
c24ff998 | 1958 | trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output); |
752fde44 ACM |
1959 | |
1960 | if (ttrace->entry_pending) { | |
c24ff998 | 1961 | fprintf(trace->output, "%-70s", ttrace->entry_str); |
752fde44 | 1962 | } else { |
c24ff998 ACM |
1963 | fprintf(trace->output, " ... ["); |
1964 | color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued"); | |
1965 | fprintf(trace->output, "]: %s()", sc->name); | |
752fde44 ACM |
1966 | } |
1967 | ||
da3c9a44 ACM |
1968 | if (sc->fmt == NULL) { |
1969 | signed_print: | |
2c82c3ad | 1970 | fprintf(trace->output, ") = %ld", ret); |
11c8e39f | 1971 | } else if (ret < 0 && (sc->fmt->errmsg || sc->fmt->errpid)) { |
942a91ed | 1972 | char bf[STRERR_BUFSIZE]; |
ba3d7dee ACM |
1973 | const char *emsg = strerror_r(-ret, bf, sizeof(bf)), |
1974 | *e = audit_errno_to_name(-ret); | |
1975 | ||
c24ff998 | 1976 | fprintf(trace->output, ") = -1 %s %s", e, emsg); |
da3c9a44 | 1977 | } else if (ret == 0 && sc->fmt->timeout) |
c24ff998 | 1978 | fprintf(trace->output, ") = 0 Timeout"); |
04b34729 | 1979 | else if (sc->fmt->hexret) |
2c82c3ad | 1980 | fprintf(trace->output, ") = %#lx", ret); |
11c8e39f ACM |
1981 | else if (sc->fmt->errpid) { |
1982 | struct thread *child = machine__find_thread(trace->host, ret, ret); | |
1983 | ||
1984 | if (child != NULL) { | |
1985 | fprintf(trace->output, ") = %ld", ret); | |
1986 | if (child->comm_set) | |
1987 | fprintf(trace->output, " (%s)", thread__comm_str(child)); | |
1988 | thread__put(child); | |
1989 | } | |
1990 | } else | |
da3c9a44 | 1991 | goto signed_print; |
ba3d7dee | 1992 | |
c24ff998 | 1993 | fputc('\n', trace->output); |
566a0885 | 1994 | |
5cf9c84e ACM |
1995 | if (callchain_ret > 0) |
1996 | trace__fprintf_callchain(trace, sample); | |
1997 | else if (callchain_ret < 0) | |
1998 | pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); | |
ae9ed035 | 1999 | out: |
752fde44 | 2000 | ttrace->entry_pending = false; |
b91fc39f ACM |
2001 | err = 0; |
2002 | out_put: | |
2003 | thread__put(thread); | |
2004 | return err; | |
ba3d7dee ACM |
2005 | } |
2006 | ||
c522739d | 2007 | static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 2008 | union perf_event *event __maybe_unused, |
c522739d ACM |
2009 | struct perf_sample *sample) |
2010 | { | |
f994592d ACM |
2011 | struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
2012 | struct thread_trace *ttrace; | |
2013 | size_t filename_len, entry_str_len, to_move; | |
2014 | ssize_t remaining_space; | |
2015 | char *pos; | |
7f4f8001 | 2016 | const char *filename = perf_evsel__rawptr(evsel, sample, "pathname"); |
f994592d ACM |
2017 | |
2018 | if (!thread) | |
2019 | goto out; | |
2020 | ||
2021 | ttrace = thread__priv(thread); | |
2022 | if (!ttrace) | |
2023 | goto out; | |
2024 | ||
7f4f8001 ACM |
2025 | filename_len = strlen(filename); |
2026 | ||
2027 | if (ttrace->filename.namelen < filename_len) { | |
2028 | char *f = realloc(ttrace->filename.name, filename_len + 1); | |
2029 | ||
2030 | if (f == NULL) | |
2031 | goto out; | |
2032 | ||
2033 | ttrace->filename.namelen = filename_len; | |
2034 | ttrace->filename.name = f; | |
2035 | } | |
2036 | ||
2037 | strcpy(ttrace->filename.name, filename); | |
2038 | ttrace->filename.pending_open = true; | |
2039 | ||
f994592d ACM |
2040 | if (!ttrace->filename.ptr) |
2041 | goto out; | |
2042 | ||
2043 | entry_str_len = strlen(ttrace->entry_str); | |
2044 | remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */ | |
2045 | if (remaining_space <= 0) | |
2046 | goto out; | |
2047 | ||
f994592d ACM |
2048 | if (filename_len > (size_t)remaining_space) { |
2049 | filename += filename_len - remaining_space; | |
2050 | filename_len = remaining_space; | |
2051 | } | |
2052 | ||
2053 | to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */ | |
2054 | pos = ttrace->entry_str + ttrace->filename.entry_str_pos; | |
2055 | memmove(pos + filename_len, pos, to_move); | |
2056 | memcpy(pos, filename, filename_len); | |
2057 | ||
2058 | ttrace->filename.ptr = 0; | |
2059 | ttrace->filename.entry_str_pos = 0; | |
2060 | out: | |
c522739d ACM |
2061 | return 0; |
2062 | } | |
2063 | ||
1302d88e | 2064 | static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 2065 | union perf_event *event __maybe_unused, |
1302d88e ACM |
2066 | struct perf_sample *sample) |
2067 | { | |
2068 | u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); | |
2069 | double runtime_ms = (double)runtime / NSEC_PER_MSEC; | |
8fb598e5 | 2070 | struct thread *thread = machine__findnew_thread(trace->host, |
314add6b AH |
2071 | sample->pid, |
2072 | sample->tid); | |
c24ff998 | 2073 | struct thread_trace *ttrace = thread__trace(thread, trace->output); |
1302d88e ACM |
2074 | |
2075 | if (ttrace == NULL) | |
2076 | goto out_dump; | |
2077 | ||
2078 | ttrace->runtime_ms += runtime_ms; | |
2079 | trace->runtime_ms += runtime_ms; | |
b91fc39f | 2080 | thread__put(thread); |
1302d88e ACM |
2081 | return 0; |
2082 | ||
2083 | out_dump: | |
c24ff998 | 2084 | fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n", |
1302d88e ACM |
2085 | evsel->name, |
2086 | perf_evsel__strval(evsel, sample, "comm"), | |
2087 | (pid_t)perf_evsel__intval(evsel, sample, "pid"), | |
2088 | runtime, | |
2089 | perf_evsel__intval(evsel, sample, "vruntime")); | |
b91fc39f | 2090 | thread__put(thread); |
1302d88e ACM |
2091 | return 0; |
2092 | } | |
2093 | ||
1d6c9407 WN |
2094 | static void bpf_output__printer(enum binary_printer_ops op, |
2095 | unsigned int val, void *extra) | |
2096 | { | |
2097 | FILE *output = extra; | |
2098 | unsigned char ch = (unsigned char)val; | |
2099 | ||
2100 | switch (op) { | |
2101 | case BINARY_PRINT_CHAR_DATA: | |
2102 | fprintf(output, "%c", isprint(ch) ? ch : '.'); | |
2103 | break; | |
2104 | case BINARY_PRINT_DATA_BEGIN: | |
2105 | case BINARY_PRINT_LINE_BEGIN: | |
2106 | case BINARY_PRINT_ADDR: | |
2107 | case BINARY_PRINT_NUM_DATA: | |
2108 | case BINARY_PRINT_NUM_PAD: | |
2109 | case BINARY_PRINT_SEP: | |
2110 | case BINARY_PRINT_CHAR_PAD: | |
2111 | case BINARY_PRINT_LINE_END: | |
2112 | case BINARY_PRINT_DATA_END: | |
2113 | default: | |
2114 | break; | |
2115 | } | |
2116 | } | |
2117 | ||
2118 | static void bpf_output__fprintf(struct trace *trace, | |
2119 | struct perf_sample *sample) | |
2120 | { | |
2121 | print_binary(sample->raw_data, sample->raw_size, 8, | |
2122 | bpf_output__printer, trace->output); | |
2123 | } | |
2124 | ||
14a052df ACM |
2125 | static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel, |
2126 | union perf_event *event __maybe_unused, | |
2127 | struct perf_sample *sample) | |
2128 | { | |
2129 | trace__printf_interrupted_entry(trace, sample); | |
2130 | trace__fprintf_tstamp(trace, sample->time, trace->output); | |
0808921a ACM |
2131 | |
2132 | if (trace->trace_syscalls) | |
2133 | fprintf(trace->output, "( ): "); | |
2134 | ||
2135 | fprintf(trace->output, "%s:", evsel->name); | |
14a052df | 2136 | |
1d6c9407 WN |
2137 | if (perf_evsel__is_bpf_output(evsel)) { |
2138 | bpf_output__fprintf(trace, sample); | |
2139 | } else if (evsel->tp_format) { | |
14a052df ACM |
2140 | event_format__fprintf(evsel->tp_format, sample->cpu, |
2141 | sample->raw_data, sample->raw_size, | |
2142 | trace->output); | |
2143 | } | |
2144 | ||
2145 | fprintf(trace->output, ")\n"); | |
202ff968 | 2146 | |
5cf9c84e ACM |
2147 | if (sample->callchain) { |
2148 | if (trace__resolve_callchain(trace, evsel, sample, &callchain_cursor) == 0) | |
2149 | trace__fprintf_callchain(trace, sample); | |
2150 | } | |
202ff968 | 2151 | |
14a052df ACM |
2152 | return 0; |
2153 | } | |
2154 | ||
598d02c5 SF |
2155 | static void print_location(FILE *f, struct perf_sample *sample, |
2156 | struct addr_location *al, | |
2157 | bool print_dso, bool print_sym) | |
2158 | { | |
2159 | ||
2160 | if ((verbose || print_dso) && al->map) | |
2161 | fprintf(f, "%s@", al->map->dso->long_name); | |
2162 | ||
2163 | if ((verbose || print_sym) && al->sym) | |
4414a3c5 | 2164 | fprintf(f, "%s+0x%" PRIx64, al->sym->name, |
598d02c5 SF |
2165 | al->addr - al->sym->start); |
2166 | else if (al->map) | |
4414a3c5 | 2167 | fprintf(f, "0x%" PRIx64, al->addr); |
598d02c5 | 2168 | else |
4414a3c5 | 2169 | fprintf(f, "0x%" PRIx64, sample->addr); |
598d02c5 SF |
2170 | } |
2171 | ||
2172 | static int trace__pgfault(struct trace *trace, | |
2173 | struct perf_evsel *evsel, | |
473398a2 | 2174 | union perf_event *event __maybe_unused, |
598d02c5 SF |
2175 | struct perf_sample *sample) |
2176 | { | |
2177 | struct thread *thread; | |
598d02c5 SF |
2178 | struct addr_location al; |
2179 | char map_type = 'd'; | |
a2ea67d7 | 2180 | struct thread_trace *ttrace; |
b91fc39f | 2181 | int err = -1; |
598d02c5 SF |
2182 | |
2183 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); | |
a2ea67d7 SF |
2184 | ttrace = thread__trace(thread, trace->output); |
2185 | if (ttrace == NULL) | |
b91fc39f | 2186 | goto out_put; |
a2ea67d7 SF |
2187 | |
2188 | if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ) | |
2189 | ttrace->pfmaj++; | |
2190 | else | |
2191 | ttrace->pfmin++; | |
2192 | ||
2193 | if (trace->summary_only) | |
b91fc39f | 2194 | goto out; |
598d02c5 | 2195 | |
473398a2 | 2196 | thread__find_addr_location(thread, sample->cpumode, MAP__FUNCTION, |
598d02c5 SF |
2197 | sample->ip, &al); |
2198 | ||
2199 | trace__fprintf_entry_head(trace, thread, 0, sample->time, trace->output); | |
2200 | ||
2201 | fprintf(trace->output, "%sfault [", | |
2202 | evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ? | |
2203 | "maj" : "min"); | |
2204 | ||
2205 | print_location(trace->output, sample, &al, false, true); | |
2206 | ||
2207 | fprintf(trace->output, "] => "); | |
2208 | ||
473398a2 | 2209 | thread__find_addr_location(thread, sample->cpumode, MAP__VARIABLE, |
598d02c5 SF |
2210 | sample->addr, &al); |
2211 | ||
2212 | if (!al.map) { | |
473398a2 | 2213 | thread__find_addr_location(thread, sample->cpumode, |
598d02c5 SF |
2214 | MAP__FUNCTION, sample->addr, &al); |
2215 | ||
2216 | if (al.map) | |
2217 | map_type = 'x'; | |
2218 | else | |
2219 | map_type = '?'; | |
2220 | } | |
2221 | ||
2222 | print_location(trace->output, sample, &al, true, false); | |
2223 | ||
2224 | fprintf(trace->output, " (%c%c)\n", map_type, al.level); | |
0c3a6ef4 ACM |
2225 | |
2226 | if (sample->callchain) { | |
2227 | if (trace__resolve_callchain(trace, evsel, sample, &callchain_cursor) == 0) | |
2228 | trace__fprintf_callchain(trace, sample); | |
2229 | } | |
b91fc39f ACM |
2230 | out: |
2231 | err = 0; | |
2232 | out_put: | |
2233 | thread__put(thread); | |
2234 | return err; | |
598d02c5 SF |
2235 | } |
2236 | ||
bdc89661 DA |
2237 | static bool skip_sample(struct trace *trace, struct perf_sample *sample) |
2238 | { | |
2239 | if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) || | |
2240 | (trace->tid_list && intlist__find(trace->tid_list, sample->tid))) | |
2241 | return false; | |
2242 | ||
2243 | if (trace->pid_list || trace->tid_list) | |
2244 | return true; | |
2245 | ||
2246 | return false; | |
2247 | } | |
2248 | ||
e6001980 | 2249 | static void trace__set_base_time(struct trace *trace, |
8a07a809 | 2250 | struct perf_evsel *evsel, |
e6001980 ACM |
2251 | struct perf_sample *sample) |
2252 | { | |
8a07a809 ACM |
2253 | /* |
2254 | * BPF events were not setting PERF_SAMPLE_TIME, so be more robust | |
2255 | * and don't use sample->time unconditionally, we may end up having | |
2256 | * some other event in the future without PERF_SAMPLE_TIME for good | |
2257 | * reason, i.e. we may not be interested in its timestamps, just in | |
2258 | * it taking place, picking some piece of information when it | |
2259 | * appears in our event stream (vfs_getname comes to mind). | |
2260 | */ | |
2261 | if (trace->base_time == 0 && !trace->full_time && | |
2262 | (evsel->attr.sample_type & PERF_SAMPLE_TIME)) | |
e6001980 ACM |
2263 | trace->base_time = sample->time; |
2264 | } | |
2265 | ||
6810fc91 | 2266 | static int trace__process_sample(struct perf_tool *tool, |
0c82adcf | 2267 | union perf_event *event, |
6810fc91 DA |
2268 | struct perf_sample *sample, |
2269 | struct perf_evsel *evsel, | |
2270 | struct machine *machine __maybe_unused) | |
2271 | { | |
2272 | struct trace *trace = container_of(tool, struct trace, tool); | |
2273 | int err = 0; | |
2274 | ||
744a9719 | 2275 | tracepoint_handler handler = evsel->handler; |
6810fc91 | 2276 | |
bdc89661 DA |
2277 | if (skip_sample(trace, sample)) |
2278 | return 0; | |
2279 | ||
e6001980 | 2280 | trace__set_base_time(trace, evsel, sample); |
6810fc91 | 2281 | |
3160565f DA |
2282 | if (handler) { |
2283 | ++trace->nr_events; | |
0c82adcf | 2284 | handler(trace, evsel, event, sample); |
3160565f | 2285 | } |
6810fc91 DA |
2286 | |
2287 | return err; | |
2288 | } | |
2289 | ||
bdc89661 DA |
2290 | static int parse_target_str(struct trace *trace) |
2291 | { | |
2292 | if (trace->opts.target.pid) { | |
2293 | trace->pid_list = intlist__new(trace->opts.target.pid); | |
2294 | if (trace->pid_list == NULL) { | |
2295 | pr_err("Error parsing process id string\n"); | |
2296 | return -EINVAL; | |
2297 | } | |
2298 | } | |
2299 | ||
2300 | if (trace->opts.target.tid) { | |
2301 | trace->tid_list = intlist__new(trace->opts.target.tid); | |
2302 | if (trace->tid_list == NULL) { | |
2303 | pr_err("Error parsing thread id string\n"); | |
2304 | return -EINVAL; | |
2305 | } | |
2306 | } | |
2307 | ||
2308 | return 0; | |
2309 | } | |
2310 | ||
1e28fe0a | 2311 | static int trace__record(struct trace *trace, int argc, const char **argv) |
5e2485b1 DA |
2312 | { |
2313 | unsigned int rec_argc, i, j; | |
2314 | const char **rec_argv; | |
2315 | const char * const record_args[] = { | |
2316 | "record", | |
2317 | "-R", | |
2318 | "-m", "1024", | |
2319 | "-c", "1", | |
5e2485b1 DA |
2320 | }; |
2321 | ||
1e28fe0a SF |
2322 | const char * const sc_args[] = { "-e", }; |
2323 | unsigned int sc_args_nr = ARRAY_SIZE(sc_args); | |
2324 | const char * const majpf_args[] = { "-e", "major-faults" }; | |
2325 | unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args); | |
2326 | const char * const minpf_args[] = { "-e", "minor-faults" }; | |
2327 | unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args); | |
2328 | ||
9aca7f17 | 2329 | /* +1 is for the event string below */ |
1e28fe0a SF |
2330 | rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 + |
2331 | majpf_args_nr + minpf_args_nr + argc; | |
5e2485b1 DA |
2332 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
2333 | ||
2334 | if (rec_argv == NULL) | |
2335 | return -ENOMEM; | |
2336 | ||
1e28fe0a | 2337 | j = 0; |
5e2485b1 | 2338 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
1e28fe0a SF |
2339 | rec_argv[j++] = record_args[i]; |
2340 | ||
e281a960 SF |
2341 | if (trace->trace_syscalls) { |
2342 | for (i = 0; i < sc_args_nr; i++) | |
2343 | rec_argv[j++] = sc_args[i]; | |
2344 | ||
2345 | /* event string may be different for older kernels - e.g., RHEL6 */ | |
2346 | if (is_valid_tracepoint("raw_syscalls:sys_enter")) | |
2347 | rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit"; | |
2348 | else if (is_valid_tracepoint("syscalls:sys_enter")) | |
2349 | rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit"; | |
2350 | else { | |
2351 | pr_err("Neither raw_syscalls nor syscalls events exist.\n"); | |
2352 | return -1; | |
2353 | } | |
9aca7f17 | 2354 | } |
9aca7f17 | 2355 | |
1e28fe0a SF |
2356 | if (trace->trace_pgfaults & TRACE_PFMAJ) |
2357 | for (i = 0; i < majpf_args_nr; i++) | |
2358 | rec_argv[j++] = majpf_args[i]; | |
2359 | ||
2360 | if (trace->trace_pgfaults & TRACE_PFMIN) | |
2361 | for (i = 0; i < minpf_args_nr; i++) | |
2362 | rec_argv[j++] = minpf_args[i]; | |
2363 | ||
2364 | for (i = 0; i < (unsigned int)argc; i++) | |
2365 | rec_argv[j++] = argv[i]; | |
5e2485b1 | 2366 | |
1e28fe0a | 2367 | return cmd_record(j, rec_argv, NULL); |
5e2485b1 DA |
2368 | } |
2369 | ||
bf2575c1 DA |
2370 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp); |
2371 | ||
08c98776 | 2372 | static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist) |
c522739d | 2373 | { |
ef503831 | 2374 | struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname"); |
8dd2a131 JO |
2375 | |
2376 | if (IS_ERR(evsel)) | |
08c98776 | 2377 | return false; |
c522739d ACM |
2378 | |
2379 | if (perf_evsel__field(evsel, "pathname") == NULL) { | |
2380 | perf_evsel__delete(evsel); | |
08c98776 | 2381 | return false; |
c522739d ACM |
2382 | } |
2383 | ||
744a9719 | 2384 | evsel->handler = trace__vfs_getname; |
c522739d | 2385 | perf_evlist__add(evlist, evsel); |
08c98776 | 2386 | return true; |
c522739d ACM |
2387 | } |
2388 | ||
0ae537cb | 2389 | static struct perf_evsel *perf_evsel__new_pgfault(u64 config) |
598d02c5 SF |
2390 | { |
2391 | struct perf_evsel *evsel; | |
2392 | struct perf_event_attr attr = { | |
2393 | .type = PERF_TYPE_SOFTWARE, | |
2394 | .mmap_data = 1, | |
598d02c5 SF |
2395 | }; |
2396 | ||
2397 | attr.config = config; | |
0524798c | 2398 | attr.sample_period = 1; |
598d02c5 SF |
2399 | |
2400 | event_attr_init(&attr); | |
2401 | ||
2402 | evsel = perf_evsel__new(&attr); | |
0ae537cb ACM |
2403 | if (evsel) |
2404 | evsel->handler = trace__pgfault; | |
598d02c5 | 2405 | |
0ae537cb | 2406 | return evsel; |
598d02c5 SF |
2407 | } |
2408 | ||
ddbb1b13 ACM |
2409 | static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample) |
2410 | { | |
2411 | const u32 type = event->header.type; | |
2412 | struct perf_evsel *evsel; | |
2413 | ||
ddbb1b13 ACM |
2414 | if (type != PERF_RECORD_SAMPLE) { |
2415 | trace__process_event(trace, trace->host, event, sample); | |
2416 | return; | |
2417 | } | |
2418 | ||
2419 | evsel = perf_evlist__id2evsel(trace->evlist, sample->id); | |
2420 | if (evsel == NULL) { | |
2421 | fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id); | |
2422 | return; | |
2423 | } | |
2424 | ||
e6001980 ACM |
2425 | trace__set_base_time(trace, evsel, sample); |
2426 | ||
ddbb1b13 ACM |
2427 | if (evsel->attr.type == PERF_TYPE_TRACEPOINT && |
2428 | sample->raw_data == NULL) { | |
2429 | fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", | |
2430 | perf_evsel__name(evsel), sample->tid, | |
2431 | sample->cpu, sample->raw_size); | |
2432 | } else { | |
2433 | tracepoint_handler handler = evsel->handler; | |
2434 | handler(trace, evsel, event, sample); | |
2435 | } | |
2436 | } | |
2437 | ||
c27366f0 ACM |
2438 | static int trace__add_syscall_newtp(struct trace *trace) |
2439 | { | |
2440 | int ret = -1; | |
2441 | struct perf_evlist *evlist = trace->evlist; | |
2442 | struct perf_evsel *sys_enter, *sys_exit; | |
2443 | ||
2444 | sys_enter = perf_evsel__syscall_newtp("sys_enter", trace__sys_enter); | |
2445 | if (sys_enter == NULL) | |
2446 | goto out; | |
2447 | ||
2448 | if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args)) | |
2449 | goto out_delete_sys_enter; | |
2450 | ||
2451 | sys_exit = perf_evsel__syscall_newtp("sys_exit", trace__sys_exit); | |
2452 | if (sys_exit == NULL) | |
2453 | goto out_delete_sys_enter; | |
2454 | ||
2455 | if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret)) | |
2456 | goto out_delete_sys_exit; | |
2457 | ||
2458 | perf_evlist__add(evlist, sys_enter); | |
2459 | perf_evlist__add(evlist, sys_exit); | |
2460 | ||
2ddd5c04 | 2461 | if (callchain_param.enabled && !trace->kernel_syscallchains) { |
44621819 ACM |
2462 | /* |
2463 | * We're interested only in the user space callchain | |
2464 | * leading to the syscall, allow overriding that for | |
2465 | * debugging reasons using --kernel_syscall_callchains | |
2466 | */ | |
2467 | sys_exit->attr.exclude_callchain_kernel = 1; | |
2468 | } | |
2469 | ||
8b3ce757 ACM |
2470 | trace->syscalls.events.sys_enter = sys_enter; |
2471 | trace->syscalls.events.sys_exit = sys_exit; | |
c27366f0 ACM |
2472 | |
2473 | ret = 0; | |
2474 | out: | |
2475 | return ret; | |
2476 | ||
2477 | out_delete_sys_exit: | |
2478 | perf_evsel__delete_priv(sys_exit); | |
2479 | out_delete_sys_enter: | |
2480 | perf_evsel__delete_priv(sys_enter); | |
2481 | goto out; | |
2482 | } | |
2483 | ||
19867b61 ACM |
2484 | static int trace__set_ev_qualifier_filter(struct trace *trace) |
2485 | { | |
2486 | int err = -1; | |
2487 | char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier, | |
2488 | trace->ev_qualifier_ids.nr, | |
2489 | trace->ev_qualifier_ids.entries); | |
2490 | ||
2491 | if (filter == NULL) | |
2492 | goto out_enomem; | |
2493 | ||
2494 | if (!perf_evsel__append_filter(trace->syscalls.events.sys_enter, "&&", filter)) | |
2495 | err = perf_evsel__append_filter(trace->syscalls.events.sys_exit, "&&", filter); | |
2496 | ||
2497 | free(filter); | |
2498 | out: | |
2499 | return err; | |
2500 | out_enomem: | |
2501 | errno = ENOMEM; | |
2502 | goto out; | |
2503 | } | |
c27366f0 | 2504 | |
f15eb531 | 2505 | static int trace__run(struct trace *trace, int argc, const char **argv) |
514f1c67 | 2506 | { |
14a052df | 2507 | struct perf_evlist *evlist = trace->evlist; |
0ae537cb | 2508 | struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL; |
efd5745e ACM |
2509 | int err = -1, i; |
2510 | unsigned long before; | |
f15eb531 | 2511 | const bool forks = argc > 0; |
46fb3c21 | 2512 | bool draining = false; |
514f1c67 | 2513 | |
75b757ca ACM |
2514 | trace->live = true; |
2515 | ||
c27366f0 | 2516 | if (trace->trace_syscalls && trace__add_syscall_newtp(trace)) |
801c67b0 | 2517 | goto out_error_raw_syscalls; |
514f1c67 | 2518 | |
e281a960 | 2519 | if (trace->trace_syscalls) |
08c98776 | 2520 | trace->vfs_getname = perf_evlist__add_vfs_getname(evlist); |
c522739d | 2521 | |
0ae537cb ACM |
2522 | if ((trace->trace_pgfaults & TRACE_PFMAJ)) { |
2523 | pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ); | |
2524 | if (pgfault_maj == NULL) | |
2525 | goto out_error_mem; | |
2526 | perf_evlist__add(evlist, pgfault_maj); | |
e2726d99 | 2527 | } |
598d02c5 | 2528 | |
0ae537cb ACM |
2529 | if ((trace->trace_pgfaults & TRACE_PFMIN)) { |
2530 | pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN); | |
2531 | if (pgfault_min == NULL) | |
2532 | goto out_error_mem; | |
2533 | perf_evlist__add(evlist, pgfault_min); | |
2534 | } | |
598d02c5 | 2535 | |
1302d88e | 2536 | if (trace->sched && |
2cc990ba ACM |
2537 | perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime", |
2538 | trace__sched_stat_runtime)) | |
2539 | goto out_error_sched_stat_runtime; | |
1302d88e | 2540 | |
514f1c67 ACM |
2541 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
2542 | if (err < 0) { | |
c24ff998 | 2543 | fprintf(trace->output, "Problems parsing the target to trace, check your options!\n"); |
514f1c67 ACM |
2544 | goto out_delete_evlist; |
2545 | } | |
2546 | ||
752fde44 ACM |
2547 | err = trace__symbols_init(trace, evlist); |
2548 | if (err < 0) { | |
c24ff998 | 2549 | fprintf(trace->output, "Problems initializing symbol libraries!\n"); |
03ad9747 | 2550 | goto out_delete_evlist; |
752fde44 ACM |
2551 | } |
2552 | ||
fde54b78 ACM |
2553 | perf_evlist__config(evlist, &trace->opts, NULL); |
2554 | ||
0c3a6ef4 ACM |
2555 | if (callchain_param.enabled) { |
2556 | bool use_identifier = false; | |
2557 | ||
2558 | if (trace->syscalls.events.sys_exit) { | |
2559 | perf_evsel__config_callchain(trace->syscalls.events.sys_exit, | |
2560 | &trace->opts, &callchain_param); | |
2561 | use_identifier = true; | |
2562 | } | |
2563 | ||
2564 | if (pgfault_maj) { | |
2565 | perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param); | |
2566 | use_identifier = true; | |
2567 | } | |
2568 | ||
2569 | if (pgfault_min) { | |
2570 | perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param); | |
2571 | use_identifier = true; | |
2572 | } | |
2573 | ||
2574 | if (use_identifier) { | |
2575 | /* | |
2576 | * Now we have evsels with different sample_ids, use | |
2577 | * PERF_SAMPLE_IDENTIFIER to map from sample to evsel | |
2578 | * from a fixed position in each ring buffer record. | |
2579 | * | |
2580 | * As of this the changeset introducing this comment, this | |
2581 | * isn't strictly needed, as the fields that can come before | |
2582 | * PERF_SAMPLE_ID are all used, but we'll probably disable | |
2583 | * some of those for things like copying the payload of | |
2584 | * pointer syscall arguments, and for vfs_getname we don't | |
2585 | * need PERF_SAMPLE_ADDR and PERF_SAMPLE_IP, so do this | |
2586 | * here as a warning we need to use PERF_SAMPLE_IDENTIFIER. | |
2587 | */ | |
2588 | perf_evlist__set_sample_bit(evlist, IDENTIFIER); | |
2589 | perf_evlist__reset_sample_bit(evlist, ID); | |
2590 | } | |
fde54b78 | 2591 | } |
514f1c67 | 2592 | |
f15eb531 NK |
2593 | signal(SIGCHLD, sig_handler); |
2594 | signal(SIGINT, sig_handler); | |
2595 | ||
2596 | if (forks) { | |
6ef73ec4 | 2597 | err = perf_evlist__prepare_workload(evlist, &trace->opts.target, |
735f7e0b | 2598 | argv, false, NULL); |
f15eb531 | 2599 | if (err < 0) { |
c24ff998 | 2600 | fprintf(trace->output, "Couldn't run the workload!\n"); |
03ad9747 | 2601 | goto out_delete_evlist; |
f15eb531 NK |
2602 | } |
2603 | } | |
2604 | ||
514f1c67 | 2605 | err = perf_evlist__open(evlist); |
a8f23d8f ACM |
2606 | if (err < 0) |
2607 | goto out_error_open; | |
514f1c67 | 2608 | |
ba504235 WN |
2609 | err = bpf__apply_obj_config(); |
2610 | if (err) { | |
2611 | char errbuf[BUFSIZ]; | |
2612 | ||
2613 | bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); | |
2614 | pr_err("ERROR: Apply config to BPF failed: %s\n", | |
2615 | errbuf); | |
2616 | goto out_error_open; | |
2617 | } | |
2618 | ||
241b057c ACM |
2619 | /* |
2620 | * Better not use !target__has_task() here because we need to cover the | |
2621 | * case where no threads were specified in the command line, but a | |
2622 | * workload was, and in that case we will fill in the thread_map when | |
2623 | * we fork the workload in perf_evlist__prepare_workload. | |
2624 | */ | |
f078c385 ACM |
2625 | if (trace->filter_pids.nr > 0) |
2626 | err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries); | |
e13798c7 | 2627 | else if (thread_map__pid(evlist->threads, 0) == -1) |
f078c385 ACM |
2628 | err = perf_evlist__set_filter_pid(evlist, getpid()); |
2629 | ||
94ad89bc ACM |
2630 | if (err < 0) |
2631 | goto out_error_mem; | |
2632 | ||
19867b61 ACM |
2633 | if (trace->ev_qualifier_ids.nr > 0) { |
2634 | err = trace__set_ev_qualifier_filter(trace); | |
2635 | if (err < 0) | |
2636 | goto out_errno; | |
19867b61 | 2637 | |
2e5e5f87 ACM |
2638 | pr_debug("event qualifier tracepoint filter: %s\n", |
2639 | trace->syscalls.events.sys_exit->filter); | |
2640 | } | |
19867b61 | 2641 | |
94ad89bc ACM |
2642 | err = perf_evlist__apply_filters(evlist, &evsel); |
2643 | if (err < 0) | |
2644 | goto out_error_apply_filters; | |
241b057c | 2645 | |
f885037e | 2646 | err = perf_evlist__mmap(evlist, trace->opts.mmap_pages, false); |
e09b18d4 ACM |
2647 | if (err < 0) |
2648 | goto out_error_mmap; | |
514f1c67 | 2649 | |
cb24d01d ACM |
2650 | if (!target__none(&trace->opts.target)) |
2651 | perf_evlist__enable(evlist); | |
2652 | ||
f15eb531 NK |
2653 | if (forks) |
2654 | perf_evlist__start_workload(evlist); | |
2655 | ||
e13798c7 | 2656 | trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 || |
42052bea ACM |
2657 | evlist->threads->nr > 1 || |
2658 | perf_evlist__first(evlist)->attr.inherit; | |
514f1c67 | 2659 | again: |
efd5745e | 2660 | before = trace->nr_events; |
514f1c67 ACM |
2661 | |
2662 | for (i = 0; i < evlist->nr_mmaps; i++) { | |
2663 | union perf_event *event; | |
2664 | ||
2665 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { | |
514f1c67 | 2666 | struct perf_sample sample; |
514f1c67 | 2667 | |
efd5745e | 2668 | ++trace->nr_events; |
514f1c67 | 2669 | |
514f1c67 ACM |
2670 | err = perf_evlist__parse_sample(evlist, event, &sample); |
2671 | if (err) { | |
c24ff998 | 2672 | fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err); |
8e50d384 | 2673 | goto next_event; |
514f1c67 ACM |
2674 | } |
2675 | ||
ddbb1b13 | 2676 | trace__handle_event(trace, event, &sample); |
8e50d384 ZZ |
2677 | next_event: |
2678 | perf_evlist__mmap_consume(evlist, i); | |
20c5f10e | 2679 | |
ba209f85 ACM |
2680 | if (interrupted) |
2681 | goto out_disable; | |
02ac5421 ACM |
2682 | |
2683 | if (done && !draining) { | |
2684 | perf_evlist__disable(evlist); | |
2685 | draining = true; | |
2686 | } | |
514f1c67 ACM |
2687 | } |
2688 | } | |
2689 | ||
efd5745e | 2690 | if (trace->nr_events == before) { |
ba209f85 | 2691 | int timeout = done ? 100 : -1; |
f15eb531 | 2692 | |
46fb3c21 ACM |
2693 | if (!draining && perf_evlist__poll(evlist, timeout) > 0) { |
2694 | if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP) == 0) | |
2695 | draining = true; | |
2696 | ||
ba209f85 | 2697 | goto again; |
46fb3c21 | 2698 | } |
ba209f85 ACM |
2699 | } else { |
2700 | goto again; | |
f15eb531 NK |
2701 | } |
2702 | ||
ba209f85 | 2703 | out_disable: |
f3b623b8 ACM |
2704 | thread__zput(trace->current); |
2705 | ||
ba209f85 | 2706 | perf_evlist__disable(evlist); |
514f1c67 | 2707 | |
c522739d ACM |
2708 | if (!err) { |
2709 | if (trace->summary) | |
2710 | trace__fprintf_thread_summary(trace, trace->output); | |
2711 | ||
2712 | if (trace->show_tool_stats) { | |
2713 | fprintf(trace->output, "Stats:\n " | |
2714 | " vfs_getname : %" PRIu64 "\n" | |
2715 | " proc_getname: %" PRIu64 "\n", | |
2716 | trace->stats.vfs_getname, | |
2717 | trace->stats.proc_getname); | |
2718 | } | |
2719 | } | |
bf2575c1 | 2720 | |
514f1c67 ACM |
2721 | out_delete_evlist: |
2722 | perf_evlist__delete(evlist); | |
14a052df | 2723 | trace->evlist = NULL; |
75b757ca | 2724 | trace->live = false; |
514f1c67 | 2725 | return err; |
6ef068cb ACM |
2726 | { |
2727 | char errbuf[BUFSIZ]; | |
a8f23d8f | 2728 | |
2cc990ba | 2729 | out_error_sched_stat_runtime: |
988bdb31 | 2730 | tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime"); |
2cc990ba ACM |
2731 | goto out_error; |
2732 | ||
801c67b0 | 2733 | out_error_raw_syscalls: |
988bdb31 | 2734 | tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)"); |
a8f23d8f ACM |
2735 | goto out_error; |
2736 | ||
e09b18d4 ACM |
2737 | out_error_mmap: |
2738 | perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf)); | |
2739 | goto out_error; | |
2740 | ||
a8f23d8f ACM |
2741 | out_error_open: |
2742 | perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf)); | |
2743 | ||
2744 | out_error: | |
6ef068cb | 2745 | fprintf(trace->output, "%s\n", errbuf); |
87f91868 | 2746 | goto out_delete_evlist; |
94ad89bc ACM |
2747 | |
2748 | out_error_apply_filters: | |
2749 | fprintf(trace->output, | |
2750 | "Failed to set filter \"%s\" on event %s with %d (%s)\n", | |
2751 | evsel->filter, perf_evsel__name(evsel), errno, | |
2752 | strerror_r(errno, errbuf, sizeof(errbuf))); | |
2753 | goto out_delete_evlist; | |
514f1c67 | 2754 | } |
5ed08dae ACM |
2755 | out_error_mem: |
2756 | fprintf(trace->output, "Not enough memory to run!\n"); | |
2757 | goto out_delete_evlist; | |
19867b61 ACM |
2758 | |
2759 | out_errno: | |
2760 | fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno)); | |
2761 | goto out_delete_evlist; | |
a8f23d8f | 2762 | } |
514f1c67 | 2763 | |
6810fc91 DA |
2764 | static int trace__replay(struct trace *trace) |
2765 | { | |
2766 | const struct perf_evsel_str_handler handlers[] = { | |
c522739d | 2767 | { "probe:vfs_getname", trace__vfs_getname, }, |
6810fc91 | 2768 | }; |
f5fc1412 JO |
2769 | struct perf_data_file file = { |
2770 | .path = input_name, | |
2771 | .mode = PERF_DATA_MODE_READ, | |
e366a6d8 | 2772 | .force = trace->force, |
f5fc1412 | 2773 | }; |
6810fc91 | 2774 | struct perf_session *session; |
003824e8 | 2775 | struct perf_evsel *evsel; |
6810fc91 DA |
2776 | int err = -1; |
2777 | ||
2778 | trace->tool.sample = trace__process_sample; | |
2779 | trace->tool.mmap = perf_event__process_mmap; | |
384c671e | 2780 | trace->tool.mmap2 = perf_event__process_mmap2; |
6810fc91 DA |
2781 | trace->tool.comm = perf_event__process_comm; |
2782 | trace->tool.exit = perf_event__process_exit; | |
2783 | trace->tool.fork = perf_event__process_fork; | |
2784 | trace->tool.attr = perf_event__process_attr; | |
2785 | trace->tool.tracing_data = perf_event__process_tracing_data; | |
2786 | trace->tool.build_id = perf_event__process_build_id; | |
2787 | ||
0a8cb85c | 2788 | trace->tool.ordered_events = true; |
6810fc91 DA |
2789 | trace->tool.ordering_requires_timestamps = true; |
2790 | ||
2791 | /* add tid to output */ | |
2792 | trace->multiple_threads = true; | |
2793 | ||
f5fc1412 | 2794 | session = perf_session__new(&file, false, &trace->tool); |
6810fc91 | 2795 | if (session == NULL) |
52e02834 | 2796 | return -1; |
6810fc91 | 2797 | |
0a7e6d1b | 2798 | if (symbol__init(&session->header.env) < 0) |
cb2ffae2 NK |
2799 | goto out; |
2800 | ||
8fb598e5 DA |
2801 | trace->host = &session->machines.host; |
2802 | ||
6810fc91 DA |
2803 | err = perf_session__set_tracepoints_handlers(session, handlers); |
2804 | if (err) | |
2805 | goto out; | |
2806 | ||
003824e8 NK |
2807 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, |
2808 | "raw_syscalls:sys_enter"); | |
9aca7f17 DA |
2809 | /* older kernels have syscalls tp versus raw_syscalls */ |
2810 | if (evsel == NULL) | |
2811 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, | |
2812 | "syscalls:sys_enter"); | |
003824e8 | 2813 | |
e281a960 SF |
2814 | if (evsel && |
2815 | (perf_evsel__init_syscall_tp(evsel, trace__sys_enter) < 0 || | |
2816 | perf_evsel__init_sc_tp_ptr_field(evsel, args))) { | |
003824e8 NK |
2817 | pr_err("Error during initialize raw_syscalls:sys_enter event\n"); |
2818 | goto out; | |
2819 | } | |
2820 | ||
2821 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, | |
2822 | "raw_syscalls:sys_exit"); | |
9aca7f17 DA |
2823 | if (evsel == NULL) |
2824 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, | |
2825 | "syscalls:sys_exit"); | |
e281a960 SF |
2826 | if (evsel && |
2827 | (perf_evsel__init_syscall_tp(evsel, trace__sys_exit) < 0 || | |
2828 | perf_evsel__init_sc_tp_uint_field(evsel, ret))) { | |
003824e8 | 2829 | pr_err("Error during initialize raw_syscalls:sys_exit event\n"); |
6810fc91 DA |
2830 | goto out; |
2831 | } | |
2832 | ||
1e28fe0a SF |
2833 | evlist__for_each(session->evlist, evsel) { |
2834 | if (evsel->attr.type == PERF_TYPE_SOFTWARE && | |
2835 | (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ || | |
2836 | evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN || | |
2837 | evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS)) | |
2838 | evsel->handler = trace__pgfault; | |
2839 | } | |
2840 | ||
bdc89661 DA |
2841 | err = parse_target_str(trace); |
2842 | if (err != 0) | |
2843 | goto out; | |
2844 | ||
6810fc91 DA |
2845 | setup_pager(); |
2846 | ||
b7b61cbe | 2847 | err = perf_session__process_events(session); |
6810fc91 DA |
2848 | if (err) |
2849 | pr_err("Failed to process events, error %d", err); | |
2850 | ||
bf2575c1 DA |
2851 | else if (trace->summary) |
2852 | trace__fprintf_thread_summary(trace, trace->output); | |
2853 | ||
6810fc91 DA |
2854 | out: |
2855 | perf_session__delete(session); | |
2856 | ||
2857 | return err; | |
2858 | } | |
2859 | ||
1302d88e ACM |
2860 | static size_t trace__fprintf_threads_header(FILE *fp) |
2861 | { | |
2862 | size_t printed; | |
2863 | ||
99ff7150 | 2864 | printed = fprintf(fp, "\n Summary of events:\n\n"); |
bf2575c1 DA |
2865 | |
2866 | return printed; | |
2867 | } | |
2868 | ||
2869 | static size_t thread__dump_stats(struct thread_trace *ttrace, | |
2870 | struct trace *trace, FILE *fp) | |
2871 | { | |
2872 | struct stats *stats; | |
2873 | size_t printed = 0; | |
2874 | struct syscall *sc; | |
2875 | struct int_node *inode = intlist__first(ttrace->syscall_stats); | |
2876 | ||
2877 | if (inode == NULL) | |
2878 | return 0; | |
2879 | ||
2880 | printed += fprintf(fp, "\n"); | |
2881 | ||
834fd46d MW |
2882 | printed += fprintf(fp, " syscall calls total min avg max stddev\n"); |
2883 | printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n"); | |
2884 | printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n"); | |
99ff7150 | 2885 | |
bf2575c1 DA |
2886 | /* each int_node is a syscall */ |
2887 | while (inode) { | |
2888 | stats = inode->priv; | |
2889 | if (stats) { | |
2890 | double min = (double)(stats->min) / NSEC_PER_MSEC; | |
2891 | double max = (double)(stats->max) / NSEC_PER_MSEC; | |
2892 | double avg = avg_stats(stats); | |
2893 | double pct; | |
2894 | u64 n = (u64) stats->n; | |
2895 | ||
2896 | pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0; | |
2897 | avg /= NSEC_PER_MSEC; | |
2898 | ||
2899 | sc = &trace->syscalls.table[inode->i]; | |
99ff7150 | 2900 | printed += fprintf(fp, " %-15s", sc->name); |
834fd46d MW |
2901 | printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f", |
2902 | n, avg * n, min, avg); | |
27a778b5 | 2903 | printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct); |
bf2575c1 DA |
2904 | } |
2905 | ||
2906 | inode = intlist__next(inode); | |
2907 | } | |
2908 | ||
2909 | printed += fprintf(fp, "\n\n"); | |
1302d88e ACM |
2910 | |
2911 | return printed; | |
2912 | } | |
2913 | ||
896cbb56 DA |
2914 | /* struct used to pass data to per-thread function */ |
2915 | struct summary_data { | |
2916 | FILE *fp; | |
2917 | struct trace *trace; | |
2918 | size_t printed; | |
2919 | }; | |
2920 | ||
2921 | static int trace__fprintf_one_thread(struct thread *thread, void *priv) | |
2922 | { | |
2923 | struct summary_data *data = priv; | |
2924 | FILE *fp = data->fp; | |
2925 | size_t printed = data->printed; | |
2926 | struct trace *trace = data->trace; | |
89dceb22 | 2927 | struct thread_trace *ttrace = thread__priv(thread); |
896cbb56 DA |
2928 | double ratio; |
2929 | ||
2930 | if (ttrace == NULL) | |
2931 | return 0; | |
2932 | ||
2933 | ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; | |
2934 | ||
15e65c69 | 2935 | printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid); |
99ff7150 | 2936 | printed += fprintf(fp, "%lu events, ", ttrace->nr_events); |
15e65c69 | 2937 | printed += fprintf(fp, "%.1f%%", ratio); |
a2ea67d7 SF |
2938 | if (ttrace->pfmaj) |
2939 | printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj); | |
2940 | if (ttrace->pfmin) | |
2941 | printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin); | |
99ff7150 | 2942 | printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms); |
bf2575c1 | 2943 | printed += thread__dump_stats(ttrace, trace, fp); |
896cbb56 DA |
2944 | |
2945 | data->printed += printed; | |
2946 | ||
2947 | return 0; | |
2948 | } | |
2949 | ||
1302d88e ACM |
2950 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) |
2951 | { | |
896cbb56 DA |
2952 | struct summary_data data = { |
2953 | .fp = fp, | |
2954 | .trace = trace | |
2955 | }; | |
2956 | data.printed = trace__fprintf_threads_header(fp); | |
1302d88e | 2957 | |
896cbb56 DA |
2958 | machine__for_each_thread(trace->host, trace__fprintf_one_thread, &data); |
2959 | ||
2960 | return data.printed; | |
1302d88e ACM |
2961 | } |
2962 | ||
ae9ed035 ACM |
2963 | static int trace__set_duration(const struct option *opt, const char *str, |
2964 | int unset __maybe_unused) | |
2965 | { | |
2966 | struct trace *trace = opt->value; | |
2967 | ||
2968 | trace->duration_filter = atof(str); | |
2969 | return 0; | |
2970 | } | |
2971 | ||
f078c385 ACM |
2972 | static int trace__set_filter_pids(const struct option *opt, const char *str, |
2973 | int unset __maybe_unused) | |
2974 | { | |
2975 | int ret = -1; | |
2976 | size_t i; | |
2977 | struct trace *trace = opt->value; | |
2978 | /* | |
2979 | * FIXME: introduce a intarray class, plain parse csv and create a | |
2980 | * { int nr, int entries[] } struct... | |
2981 | */ | |
2982 | struct intlist *list = intlist__new(str); | |
2983 | ||
2984 | if (list == NULL) | |
2985 | return -1; | |
2986 | ||
2987 | i = trace->filter_pids.nr = intlist__nr_entries(list) + 1; | |
2988 | trace->filter_pids.entries = calloc(i, sizeof(pid_t)); | |
2989 | ||
2990 | if (trace->filter_pids.entries == NULL) | |
2991 | goto out; | |
2992 | ||
2993 | trace->filter_pids.entries[0] = getpid(); | |
2994 | ||
2995 | for (i = 1; i < trace->filter_pids.nr; ++i) | |
2996 | trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i; | |
2997 | ||
2998 | intlist__delete(list); | |
2999 | ret = 0; | |
3000 | out: | |
3001 | return ret; | |
3002 | } | |
3003 | ||
c24ff998 ACM |
3004 | static int trace__open_output(struct trace *trace, const char *filename) |
3005 | { | |
3006 | struct stat st; | |
3007 | ||
3008 | if (!stat(filename, &st) && st.st_size) { | |
3009 | char oldname[PATH_MAX]; | |
3010 | ||
3011 | scnprintf(oldname, sizeof(oldname), "%s.old", filename); | |
3012 | unlink(oldname); | |
3013 | rename(filename, oldname); | |
3014 | } | |
3015 | ||
3016 | trace->output = fopen(filename, "w"); | |
3017 | ||
3018 | return trace->output == NULL ? -errno : 0; | |
3019 | } | |
3020 | ||
598d02c5 SF |
3021 | static int parse_pagefaults(const struct option *opt, const char *str, |
3022 | int unset __maybe_unused) | |
3023 | { | |
3024 | int *trace_pgfaults = opt->value; | |
3025 | ||
3026 | if (strcmp(str, "all") == 0) | |
3027 | *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN; | |
3028 | else if (strcmp(str, "maj") == 0) | |
3029 | *trace_pgfaults |= TRACE_PFMAJ; | |
3030 | else if (strcmp(str, "min") == 0) | |
3031 | *trace_pgfaults |= TRACE_PFMIN; | |
3032 | else | |
3033 | return -1; | |
3034 | ||
3035 | return 0; | |
3036 | } | |
3037 | ||
14a052df ACM |
3038 | static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler) |
3039 | { | |
3040 | struct perf_evsel *evsel; | |
3041 | ||
3042 | evlist__for_each(evlist, evsel) | |
3043 | evsel->handler = handler; | |
3044 | } | |
3045 | ||
514f1c67 ACM |
3046 | int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) |
3047 | { | |
6fdd9cb7 | 3048 | const char *trace_usage[] = { |
f15eb531 NK |
3049 | "perf trace [<options>] [<command>]", |
3050 | "perf trace [<options>] -- <command> [<options>]", | |
5e2485b1 DA |
3051 | "perf trace record [<options>] [<command>]", |
3052 | "perf trace record [<options>] -- <command> [<options>]", | |
514f1c67 ACM |
3053 | NULL |
3054 | }; | |
3055 | struct trace trace = { | |
514f1c67 ACM |
3056 | .syscalls = { |
3057 | . max = -1, | |
3058 | }, | |
3059 | .opts = { | |
3060 | .target = { | |
3061 | .uid = UINT_MAX, | |
3062 | .uses_mmap = true, | |
3063 | }, | |
3064 | .user_freq = UINT_MAX, | |
3065 | .user_interval = ULLONG_MAX, | |
509051ea | 3066 | .no_buffering = true, |
38d5447d | 3067 | .mmap_pages = UINT_MAX, |
9d9cad76 | 3068 | .proc_map_timeout = 500, |
514f1c67 | 3069 | }, |
007d66a0 | 3070 | .output = stderr, |
50c95cbd | 3071 | .show_comm = true, |
e281a960 | 3072 | .trace_syscalls = true, |
44621819 | 3073 | .kernel_syscallchains = false, |
05614993 | 3074 | .max_stack = UINT_MAX, |
514f1c67 | 3075 | }; |
c24ff998 | 3076 | const char *output_name = NULL; |
2ae3a312 | 3077 | const char *ev_qualifier_str = NULL; |
514f1c67 | 3078 | const struct option trace_options[] = { |
14a052df ACM |
3079 | OPT_CALLBACK(0, "event", &trace.evlist, "event", |
3080 | "event selector. use 'perf list' to list available events", | |
3081 | parse_events_option), | |
50c95cbd ACM |
3082 | OPT_BOOLEAN(0, "comm", &trace.show_comm, |
3083 | "show the thread COMM next to its id"), | |
c522739d | 3084 | OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"), |
d303e85a | 3085 | OPT_STRING('e', "expr", &ev_qualifier_str, "expr", "list of syscalls to trace"), |
c24ff998 | 3086 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
6810fc91 | 3087 | OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"), |
514f1c67 ACM |
3088 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", |
3089 | "trace events on existing process id"), | |
ac9be8ee | 3090 | OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", |
514f1c67 | 3091 | "trace events on existing thread id"), |
fa0e4ffe ACM |
3092 | OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids", |
3093 | "pids to filter (by the kernel)", trace__set_filter_pids), | |
ac9be8ee | 3094 | OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, |
514f1c67 | 3095 | "system-wide collection from all CPUs"), |
ac9be8ee | 3096 | OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", |
514f1c67 | 3097 | "list of cpus to monitor"), |
6810fc91 | 3098 | OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, |
514f1c67 | 3099 | "child tasks do not inherit counters"), |
994a1f78 JO |
3100 | OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages", |
3101 | "number of mmap data pages", | |
3102 | perf_evlist__parse_mmap_pages), | |
ac9be8ee | 3103 | OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user", |
514f1c67 | 3104 | "user to profile"), |
ae9ed035 ACM |
3105 | OPT_CALLBACK(0, "duration", &trace, "float", |
3106 | "show only events with duration > N.M ms", | |
3107 | trace__set_duration), | |
1302d88e | 3108 | OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), |
7c304ee0 | 3109 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
4bb09192 DA |
3110 | OPT_BOOLEAN('T', "time", &trace.full_time, |
3111 | "Show full timestamp, not time relative to first start"), | |
fd2eabaf DA |
3112 | OPT_BOOLEAN('s', "summary", &trace.summary_only, |
3113 | "Show only syscall summary with statistics"), | |
3114 | OPT_BOOLEAN('S', "with-summary", &trace.summary, | |
3115 | "Show all syscalls and summary with statistics"), | |
598d02c5 SF |
3116 | OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min", |
3117 | "Trace pagefaults", parse_pagefaults, "maj"), | |
e281a960 | 3118 | OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"), |
e366a6d8 | 3119 | OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"), |
566a0885 MW |
3120 | OPT_CALLBACK(0, "call-graph", &trace.opts, |
3121 | "record_mode[,record_size]", record_callchain_help, | |
3122 | &record_parse_callchain_opt), | |
44621819 ACM |
3123 | OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains, |
3124 | "Show the kernel callchains on the syscall exit path"), | |
5cf9c84e ACM |
3125 | OPT_UINTEGER(0, "min-stack", &trace.min_stack, |
3126 | "Set the minimum stack depth when parsing the callchain, " | |
3127 | "anything below the specified depth will be ignored."), | |
c6d4a494 ACM |
3128 | OPT_UINTEGER(0, "max-stack", &trace.max_stack, |
3129 | "Set the maximum stack depth when parsing the callchain, " | |
3130 | "anything beyond the specified depth will be ignored. " | |
3131 | "Default: " __stringify(PERF_MAX_STACK_DEPTH)), | |
9d9cad76 KL |
3132 | OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout, |
3133 | "per thread proc mmap processing timeout in ms"), | |
514f1c67 ACM |
3134 | OPT_END() |
3135 | }; | |
ccd62a89 | 3136 | bool __maybe_unused max_stack_user_set = true; |
f3e459d1 | 3137 | bool mmap_pages_user_set = true; |
6fdd9cb7 | 3138 | const char * const trace_subcommands[] = { "record", NULL }; |
514f1c67 | 3139 | int err; |
32caf0d1 | 3140 | char bf[BUFSIZ]; |
514f1c67 | 3141 | |
4d08cb80 ACM |
3142 | signal(SIGSEGV, sighandler_dump_stack); |
3143 | signal(SIGFPE, sighandler_dump_stack); | |
3144 | ||
14a052df | 3145 | trace.evlist = perf_evlist__new(); |
fd0db102 | 3146 | trace.sctbl = syscalltbl__new(); |
14a052df | 3147 | |
fd0db102 | 3148 | if (trace.evlist == NULL || trace.sctbl == NULL) { |
14a052df | 3149 | pr_err("Not enough memory to run!\n"); |
ff8f695c | 3150 | err = -ENOMEM; |
14a052df ACM |
3151 | goto out; |
3152 | } | |
3153 | ||
6fdd9cb7 YS |
3154 | argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands, |
3155 | trace_usage, PARSE_OPT_STOP_AT_NON_OPTION); | |
fd2eabaf | 3156 | |
d7888573 WN |
3157 | err = bpf__setup_stdout(trace.evlist); |
3158 | if (err) { | |
3159 | bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf)); | |
3160 | pr_err("ERROR: Setup BPF stdout failed: %s\n", bf); | |
3161 | goto out; | |
3162 | } | |
3163 | ||
59247e33 ACM |
3164 | err = -1; |
3165 | ||
598d02c5 SF |
3166 | if (trace.trace_pgfaults) { |
3167 | trace.opts.sample_address = true; | |
3168 | trace.opts.sample_time = true; | |
3169 | } | |
3170 | ||
f3e459d1 ACM |
3171 | if (trace.opts.mmap_pages == UINT_MAX) |
3172 | mmap_pages_user_set = false; | |
3173 | ||
05614993 ACM |
3174 | if (trace.max_stack == UINT_MAX) { |
3175 | trace.max_stack = PERF_MAX_STACK_DEPTH; | |
3176 | max_stack_user_set = false; | |
3177 | } | |
3178 | ||
3179 | #ifdef HAVE_DWARF_UNWIND_SUPPORT | |
2ddd5c04 | 3180 | if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) |
05614993 ACM |
3181 | record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false); |
3182 | #endif | |
3183 | ||
2ddd5c04 | 3184 | if (callchain_param.enabled) { |
f3e459d1 ACM |
3185 | if (!mmap_pages_user_set && geteuid() == 0) |
3186 | trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4; | |
3187 | ||
566a0885 | 3188 | symbol_conf.use_callchain = true; |
f3e459d1 | 3189 | } |
566a0885 | 3190 | |
14a052df ACM |
3191 | if (trace.evlist->nr_entries > 0) |
3192 | evlist__set_evsel_handler(trace.evlist, trace__event_handler); | |
3193 | ||
1e28fe0a SF |
3194 | if ((argc >= 1) && (strcmp(argv[0], "record") == 0)) |
3195 | return trace__record(&trace, argc-1, &argv[1]); | |
3196 | ||
3197 | /* summary_only implies summary option, but don't overwrite summary if set */ | |
3198 | if (trace.summary_only) | |
3199 | trace.summary = trace.summary_only; | |
3200 | ||
726f3234 ACM |
3201 | if (!trace.trace_syscalls && !trace.trace_pgfaults && |
3202 | trace.evlist->nr_entries == 0 /* Was --events used? */) { | |
e281a960 SF |
3203 | pr_err("Please specify something to trace.\n"); |
3204 | return -1; | |
3205 | } | |
3206 | ||
59247e33 ACM |
3207 | if (!trace.trace_syscalls && ev_qualifier_str) { |
3208 | pr_err("The -e option can't be used with --no-syscalls.\n"); | |
3209 | goto out; | |
3210 | } | |
3211 | ||
c24ff998 ACM |
3212 | if (output_name != NULL) { |
3213 | err = trace__open_output(&trace, output_name); | |
3214 | if (err < 0) { | |
3215 | perror("failed to create output file"); | |
3216 | goto out; | |
3217 | } | |
3218 | } | |
3219 | ||
fd0db102 ACM |
3220 | trace.open_id = syscalltbl__id(trace.sctbl, "open"); |
3221 | ||
2ae3a312 | 3222 | if (ev_qualifier_str != NULL) { |
b059efdf | 3223 | const char *s = ev_qualifier_str; |
005438a8 ACM |
3224 | struct strlist_config slist_config = { |
3225 | .dirname = system_path(STRACE_GROUPS_DIR), | |
3226 | }; | |
b059efdf ACM |
3227 | |
3228 | trace.not_ev_qualifier = *s == '!'; | |
3229 | if (trace.not_ev_qualifier) | |
3230 | ++s; | |
005438a8 | 3231 | trace.ev_qualifier = strlist__new(s, &slist_config); |
2ae3a312 | 3232 | if (trace.ev_qualifier == NULL) { |
c24ff998 ACM |
3233 | fputs("Not enough memory to parse event qualifier", |
3234 | trace.output); | |
3235 | err = -ENOMEM; | |
3236 | goto out_close; | |
2ae3a312 | 3237 | } |
d0cc439b ACM |
3238 | |
3239 | err = trace__validate_ev_qualifier(&trace); | |
3240 | if (err) | |
3241 | goto out_close; | |
2ae3a312 ACM |
3242 | } |
3243 | ||
602ad878 | 3244 | err = target__validate(&trace.opts.target); |
32caf0d1 | 3245 | if (err) { |
602ad878 | 3246 | target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
c24ff998 ACM |
3247 | fprintf(trace.output, "%s", bf); |
3248 | goto out_close; | |
32caf0d1 NK |
3249 | } |
3250 | ||
602ad878 | 3251 | err = target__parse_uid(&trace.opts.target); |
514f1c67 | 3252 | if (err) { |
602ad878 | 3253 | target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
c24ff998 ACM |
3254 | fprintf(trace.output, "%s", bf); |
3255 | goto out_close; | |
514f1c67 ACM |
3256 | } |
3257 | ||
602ad878 | 3258 | if (!argc && target__none(&trace.opts.target)) |
ee76120e NK |
3259 | trace.opts.target.system_wide = true; |
3260 | ||
6810fc91 DA |
3261 | if (input_name) |
3262 | err = trace__replay(&trace); | |
3263 | else | |
3264 | err = trace__run(&trace, argc, argv); | |
1302d88e | 3265 | |
c24ff998 ACM |
3266 | out_close: |
3267 | if (output_name != NULL) | |
3268 | fclose(trace.output); | |
3269 | out: | |
1302d88e | 3270 | return err; |
514f1c67 | 3271 | } |