]>
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> |
744fafc7 | 21 | #include <bpf/bpf.h> |
514f1c67 | 22 | #include "builtin.h" |
9ea42ba4 | 23 | #include "util/cgroup.h" |
752fde44 | 24 | #include "util/color.h" |
ac96287c | 25 | #include "util/config.h" |
7c304ee0 | 26 | #include "util/debug.h" |
092bd3cd | 27 | #include "util/env.h" |
5ab8c689 | 28 | #include "util/event.h" |
514f1c67 | 29 | #include "util/evlist.h" |
4b6ab94e | 30 | #include <subcmd/exec-cmd.h> |
752fde44 | 31 | #include "util/machine.h" |
9a3993d4 | 32 | #include "util/path.h" |
6810fc91 | 33 | #include "util/session.h" |
752fde44 | 34 | #include "util/thread.h" |
4b6ab94e | 35 | #include <subcmd/parse-options.h> |
2ae3a312 | 36 | #include "util/strlist.h" |
bdc89661 | 37 | #include "util/intlist.h" |
514f1c67 | 38 | #include "util/thread_map.h" |
bf2575c1 | 39 | #include "util/stat.h" |
fd5cead2 | 40 | #include "trace/beauty/beauty.h" |
97978b3e | 41 | #include "trace-event.h" |
9aca7f17 | 42 | #include "util/parse-events.h" |
ba504235 | 43 | #include "util/bpf-loader.h" |
566a0885 | 44 | #include "callchain.h" |
fea01392 | 45 | #include "print_binary.h" |
a067558e | 46 | #include "string2.h" |
fd0db102 | 47 | #include "syscalltbl.h" |
96c14451 | 48 | #include "rb_resort.h" |
514f1c67 | 49 | |
a43783ae | 50 | #include <errno.h> |
fd20e811 | 51 | #include <inttypes.h> |
4208735d | 52 | #include <poll.h> |
9607ad3a | 53 | #include <signal.h> |
514f1c67 | 54 | #include <stdlib.h> |
017037ff | 55 | #include <string.h> |
8dd2a131 | 56 | #include <linux/err.h> |
997bba8c | 57 | #include <linux/filter.h> |
877a7a11 | 58 | #include <linux/kernel.h> |
39878d49 | 59 | #include <linux/random.h> |
c6d4a494 | 60 | #include <linux/stringify.h> |
bd48c63e | 61 | #include <linux/time64.h> |
bafae98e | 62 | #include <fcntl.h> |
514f1c67 | 63 | |
3d689ed6 ACM |
64 | #include "sane_ctype.h" |
65 | ||
c188e7ac ACM |
66 | #ifndef O_CLOEXEC |
67 | # define O_CLOEXEC 02000000 | |
68 | #endif | |
69 | ||
83a51694 ACM |
70 | #ifndef F_LINUX_SPECIFIC_BASE |
71 | # define F_LINUX_SPECIFIC_BASE 1024 | |
72 | #endif | |
73 | ||
d1d438a3 ACM |
74 | struct trace { |
75 | struct perf_tool tool; | |
fd0db102 | 76 | struct syscalltbl *sctbl; |
d1d438a3 ACM |
77 | struct { |
78 | int max; | |
79 | struct syscall *table; | |
b27b38ed | 80 | struct bpf_map *map; |
d1d438a3 ACM |
81 | struct { |
82 | struct perf_evsel *sys_enter, | |
d3d1c4bd ACM |
83 | *sys_exit, |
84 | *augmented; | |
d1d438a3 ACM |
85 | } events; |
86 | } syscalls; | |
87 | struct record_opts opts; | |
88 | struct perf_evlist *evlist; | |
89 | struct machine *host; | |
90 | struct thread *current; | |
9ea42ba4 | 91 | struct cgroup *cgroup; |
d1d438a3 ACM |
92 | u64 base_time; |
93 | FILE *output; | |
94 | unsigned long nr_events; | |
5067a8cd ACM |
95 | unsigned long nr_events_printed; |
96 | unsigned long max_events; | |
d1d438a3 ACM |
97 | struct strlist *ev_qualifier; |
98 | struct { | |
99 | size_t nr; | |
100 | int *entries; | |
101 | } ev_qualifier_ids; | |
d1d438a3 ACM |
102 | struct { |
103 | size_t nr; | |
104 | pid_t *entries; | |
744fafc7 | 105 | struct bpf_map *map; |
d1d438a3 ACM |
106 | } filter_pids; |
107 | double duration_filter; | |
108 | double runtime_ms; | |
109 | struct { | |
110 | u64 vfs_getname, | |
111 | proc_getname; | |
112 | } stats; | |
c6d4a494 | 113 | unsigned int max_stack; |
5cf9c84e | 114 | unsigned int min_stack; |
a761a8d1 | 115 | bool sort_events; |
cd26ea6d | 116 | bool raw_augmented_syscalls; |
d1d438a3 ACM |
117 | bool not_ev_qualifier; |
118 | bool live; | |
119 | bool full_time; | |
120 | bool sched; | |
121 | bool multiple_threads; | |
122 | bool summary; | |
123 | bool summary_only; | |
0a6545bd | 124 | bool failure_only; |
d1d438a3 | 125 | bool show_comm; |
591421e1 | 126 | bool print_sample; |
d1d438a3 ACM |
127 | bool show_tool_stats; |
128 | bool trace_syscalls; | |
44621819 | 129 | bool kernel_syscallchains; |
9ed45d59 | 130 | s16 args_alignment; |
b036146f | 131 | bool show_tstamp; |
42e4a52d | 132 | bool show_duration; |
e7c634fc | 133 | bool show_zeros; |
9d6dc178 | 134 | bool show_arg_names; |
c65c83ff | 135 | bool show_string_prefix; |
d1d438a3 ACM |
136 | bool force; |
137 | bool vfs_getname; | |
138 | int trace_pgfaults; | |
028713aa JO |
139 | struct { |
140 | struct ordered_events data; | |
141 | u64 last; | |
142 | } oe; | |
d1d438a3 | 143 | }; |
a1c2552d | 144 | |
77170988 ACM |
145 | struct tp_field { |
146 | int offset; | |
147 | union { | |
148 | u64 (*integer)(struct tp_field *field, struct perf_sample *sample); | |
149 | void *(*pointer)(struct tp_field *field, struct perf_sample *sample); | |
150 | }; | |
151 | }; | |
152 | ||
153 | #define TP_UINT_FIELD(bits) \ | |
154 | static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \ | |
155 | { \ | |
55d43bca DA |
156 | u##bits value; \ |
157 | memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ | |
158 | return value; \ | |
77170988 ACM |
159 | } |
160 | ||
161 | TP_UINT_FIELD(8); | |
162 | TP_UINT_FIELD(16); | |
163 | TP_UINT_FIELD(32); | |
164 | TP_UINT_FIELD(64); | |
165 | ||
166 | #define TP_UINT_FIELD__SWAPPED(bits) \ | |
167 | static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \ | |
168 | { \ | |
55d43bca DA |
169 | u##bits value; \ |
170 | memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ | |
77170988 ACM |
171 | return bswap_##bits(value);\ |
172 | } | |
173 | ||
174 | TP_UINT_FIELD__SWAPPED(16); | |
175 | TP_UINT_FIELD__SWAPPED(32); | |
176 | TP_UINT_FIELD__SWAPPED(64); | |
177 | ||
aa823f58 | 178 | static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap) |
77170988 | 179 | { |
aa823f58 | 180 | field->offset = offset; |
77170988 | 181 | |
aa823f58 | 182 | switch (size) { |
77170988 ACM |
183 | case 1: |
184 | field->integer = tp_field__u8; | |
185 | break; | |
186 | case 2: | |
187 | field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16; | |
188 | break; | |
189 | case 4: | |
190 | field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32; | |
191 | break; | |
192 | case 8: | |
193 | field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64; | |
194 | break; | |
195 | default: | |
196 | return -1; | |
197 | } | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
2c92f982 | 202 | static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap) |
aa823f58 ACM |
203 | { |
204 | return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap); | |
205 | } | |
206 | ||
77170988 ACM |
207 | static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample) |
208 | { | |
209 | return sample->raw_data + field->offset; | |
210 | } | |
211 | ||
aa823f58 | 212 | static int __tp_field__init_ptr(struct tp_field *field, int offset) |
77170988 | 213 | { |
aa823f58 | 214 | field->offset = offset; |
77170988 ACM |
215 | field->pointer = tp_field__ptr; |
216 | return 0; | |
217 | } | |
218 | ||
2c92f982 | 219 | static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field) |
aa823f58 ACM |
220 | { |
221 | return __tp_field__init_ptr(field, format_field->offset); | |
222 | } | |
223 | ||
77170988 ACM |
224 | struct syscall_tp { |
225 | struct tp_field id; | |
226 | union { | |
227 | struct tp_field args, ret; | |
228 | }; | |
229 | }; | |
230 | ||
231 | static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel, | |
232 | struct tp_field *field, | |
233 | const char *name) | |
234 | { | |
2c92f982 | 235 | struct tep_format_field *format_field = perf_evsel__field(evsel, name); |
77170988 ACM |
236 | |
237 | if (format_field == NULL) | |
238 | return -1; | |
239 | ||
240 | return tp_field__init_uint(field, format_field, evsel->needs_swap); | |
241 | } | |
242 | ||
243 | #define perf_evsel__init_sc_tp_uint_field(evsel, name) \ | |
244 | ({ struct syscall_tp *sc = evsel->priv;\ | |
245 | perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); }) | |
246 | ||
247 | static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel, | |
248 | struct tp_field *field, | |
249 | const char *name) | |
250 | { | |
2c92f982 | 251 | struct tep_format_field *format_field = perf_evsel__field(evsel, name); |
77170988 ACM |
252 | |
253 | if (format_field == NULL) | |
254 | return -1; | |
255 | ||
256 | return tp_field__init_ptr(field, format_field); | |
257 | } | |
258 | ||
259 | #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \ | |
260 | ({ struct syscall_tp *sc = evsel->priv;\ | |
261 | perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); }) | |
262 | ||
263 | static void perf_evsel__delete_priv(struct perf_evsel *evsel) | |
264 | { | |
04662523 | 265 | zfree(&evsel->priv); |
77170988 ACM |
266 | perf_evsel__delete(evsel); |
267 | } | |
268 | ||
d32855fa ACM |
269 | static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel) |
270 | { | |
271 | struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp)); | |
272 | ||
273 | if (evsel->priv != NULL) { | |
42da438c ACM |
274 | if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") && |
275 | perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr")) | |
d32855fa ACM |
276 | goto out_delete; |
277 | return 0; | |
278 | } | |
279 | ||
280 | return -ENOMEM; | |
281 | out_delete: | |
282 | zfree(&evsel->priv); | |
283 | return -ENOENT; | |
284 | } | |
285 | ||
d3d1c4bd ACM |
286 | static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel) |
287 | { | |
288 | struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp)); | |
289 | ||
290 | if (evsel->priv != NULL) { /* field, sizeof_field, offsetof_field */ | |
291 | if (__tp_field__init_uint(&sc->id, sizeof(long), sizeof(long long), evsel->needs_swap)) | |
292 | goto out_delete; | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | return -ENOMEM; | |
298 | out_delete: | |
299 | zfree(&evsel->priv); | |
300 | return -EINVAL; | |
301 | } | |
302 | ||
303 | static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel) | |
304 | { | |
305 | struct syscall_tp *sc = evsel->priv; | |
306 | ||
307 | return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)); | |
308 | } | |
309 | ||
b1a9e253 ACM |
310 | static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel) |
311 | { | |
312 | struct syscall_tp *sc = evsel->priv; | |
313 | ||
314 | return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap); | |
315 | } | |
316 | ||
63f11c80 | 317 | static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler) |
96695d44 NK |
318 | { |
319 | evsel->priv = malloc(sizeof(struct syscall_tp)); | |
320 | if (evsel->priv != NULL) { | |
321 | if (perf_evsel__init_sc_tp_uint_field(evsel, id)) | |
322 | goto out_delete; | |
323 | ||
324 | evsel->handler = handler; | |
325 | return 0; | |
326 | } | |
327 | ||
328 | return -ENOMEM; | |
329 | ||
330 | out_delete: | |
04662523 | 331 | zfree(&evsel->priv); |
96695d44 NK |
332 | return -ENOENT; |
333 | } | |
334 | ||
63f11c80 | 335 | static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler) |
77170988 | 336 | { |
ef503831 | 337 | struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction); |
77170988 | 338 | |
9aca7f17 | 339 | /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */ |
8dd2a131 | 340 | if (IS_ERR(evsel)) |
9aca7f17 DA |
341 | evsel = perf_evsel__newtp("syscalls", direction); |
342 | ||
8dd2a131 JO |
343 | if (IS_ERR(evsel)) |
344 | return NULL; | |
345 | ||
63f11c80 | 346 | if (perf_evsel__init_raw_syscall_tp(evsel, handler)) |
8dd2a131 | 347 | goto out_delete; |
77170988 ACM |
348 | |
349 | return evsel; | |
350 | ||
351 | out_delete: | |
352 | perf_evsel__delete_priv(evsel); | |
353 | return NULL; | |
354 | } | |
355 | ||
356 | #define perf_evsel__sc_tp_uint(evsel, name, sample) \ | |
357 | ({ struct syscall_tp *fields = evsel->priv; \ | |
358 | fields->name.integer(&fields->name, sample); }) | |
359 | ||
360 | #define perf_evsel__sc_tp_ptr(evsel, name, sample) \ | |
361 | ({ struct syscall_tp *fields = evsel->priv; \ | |
362 | fields->name.pointer(&fields->name, sample); }) | |
363 | ||
c65c83ff | 364 | size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val) |
0ae79636 ACM |
365 | { |
366 | int idx = val - sa->offset; | |
1f115cb7 | 367 | |
9614b8d6 ACM |
368 | if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) { |
369 | size_t printed = scnprintf(bf, size, intfmt, val); | |
370 | if (show_prefix) | |
371 | printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix); | |
372 | return printed; | |
373 | } | |
1f115cb7 | 374 | |
c65c83ff | 375 | return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]); |
03e3adc9 ACM |
376 | } |
377 | ||
975b7c2f ACM |
378 | static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size, |
379 | const char *intfmt, | |
380 | struct syscall_arg *arg) | |
1f115cb7 | 381 | { |
c65c83ff | 382 | return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val); |
1f115cb7 ACM |
383 | } |
384 | ||
975b7c2f ACM |
385 | static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size, |
386 | struct syscall_arg *arg) | |
387 | { | |
388 | return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg); | |
389 | } | |
390 | ||
1f115cb7 ACM |
391 | #define SCA_STRARRAY syscall_arg__scnprintf_strarray |
392 | ||
1f2d085e | 393 | size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val) |
83a51694 | 394 | { |
9614b8d6 | 395 | size_t printed; |
83a51694 ACM |
396 | int i; |
397 | ||
398 | for (i = 0; i < sas->nr_entries; ++i) { | |
399 | struct strarray *sa = sas->entries[i]; | |
1f2d085e | 400 | int idx = val - sa->offset; |
83a51694 ACM |
401 | |
402 | if (idx >= 0 && idx < sa->nr_entries) { | |
403 | if (sa->entries[idx] == NULL) | |
404 | break; | |
1f2d085e | 405 | return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]); |
83a51694 ACM |
406 | } |
407 | } | |
408 | ||
9614b8d6 ACM |
409 | printed = scnprintf(bf, size, intfmt, val); |
410 | if (show_prefix) | |
411 | printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix); | |
412 | return printed; | |
1f2d085e ACM |
413 | } |
414 | ||
415 | size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size, | |
416 | struct syscall_arg *arg) | |
417 | { | |
418 | return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val); | |
83a51694 ACM |
419 | } |
420 | ||
48e1f91a ACM |
421 | #ifndef AT_FDCWD |
422 | #define AT_FDCWD -100 | |
423 | #endif | |
424 | ||
75b757ca ACM |
425 | static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size, |
426 | struct syscall_arg *arg) | |
427 | { | |
428 | int fd = arg->val; | |
c65c83ff | 429 | const char *prefix = "AT_FD"; |
75b757ca ACM |
430 | |
431 | if (fd == AT_FDCWD) | |
c65c83ff | 432 | return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD"); |
75b757ca ACM |
433 | |
434 | return syscall_arg__scnprintf_fd(bf, size, arg); | |
435 | } | |
436 | ||
437 | #define SCA_FDAT syscall_arg__scnprintf_fd_at | |
438 | ||
439 | static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, | |
440 | struct syscall_arg *arg); | |
441 | ||
442 | #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd | |
443 | ||
2c2b1623 | 444 | size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg) |
13d4ff3e | 445 | { |
01533e97 | 446 | return scnprintf(bf, size, "%#lx", arg->val); |
13d4ff3e ACM |
447 | } |
448 | ||
ce05539f ACM |
449 | size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg) |
450 | { | |
451 | if (arg->val == 0) | |
452 | return scnprintf(bf, size, "NULL"); | |
453 | return syscall_arg__scnprintf_hex(bf, size, arg); | |
454 | } | |
455 | ||
2c2b1623 | 456 | size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg) |
a1c2552d ACM |
457 | { |
458 | return scnprintf(bf, size, "%d", arg->val); | |
459 | } | |
460 | ||
5dde91ed ACM |
461 | size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg) |
462 | { | |
463 | return scnprintf(bf, size, "%ld", arg->val); | |
464 | } | |
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 | }; | |
2e3d7fac | 470 | static DEFINE_STRARRAY(bpf_cmd, "BPF_"); |
729a7841 | 471 | |
03e3adc9 | 472 | static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", }; |
2e3d7fac | 473 | static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1); |
eac032c5 | 474 | |
1f115cb7 | 475 | static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", }; |
2e3d7fac | 476 | static DEFINE_STRARRAY(itimers, "ITIMER_"); |
1f115cb7 | 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 | }; | |
2e3d7fac | 485 | static DEFINE_STRARRAY(keyctl_options, "KEYCTL_"); |
b62bee1b | 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 | }; | |
2e3d7fac | 495 | static DEFINE_STRARRAY(whences, "SEEK_"); |
f9da0b0c | 496 | |
80f587d5 ACM |
497 | static const char *fcntl_cmds[] = { |
498 | "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK", | |
e000e5e3 ACM |
499 | "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64", |
500 | "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX", | |
501 | "GETOWNER_UIDS", | |
80f587d5 | 502 | }; |
2e3d7fac | 503 | static DEFINE_STRARRAY(fcntl_cmds, "F_"); |
80f587d5 | 504 | |
83a51694 ACM |
505 | static const char *fcntl_linux_specific_cmds[] = { |
506 | "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC", | |
507 | "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS", | |
64e4561d | 508 | "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT", |
83a51694 ACM |
509 | }; |
510 | ||
2e3d7fac | 511 | static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE); |
83a51694 ACM |
512 | |
513 | static struct strarray *fcntl_cmds_arrays[] = { | |
514 | &strarray__fcntl_cmds, | |
515 | &strarray__fcntl_linux_specific_cmds, | |
516 | }; | |
517 | ||
518 | static DEFINE_STRARRAYS(fcntl_cmds_arrays); | |
519 | ||
c045bf02 ACM |
520 | static const char *rlimit_resources[] = { |
521 | "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE", | |
522 | "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO", | |
523 | "RTTIME", | |
524 | }; | |
2e3d7fac | 525 | static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_"); |
c045bf02 | 526 | |
eb5b1b14 | 527 | static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", }; |
2e3d7fac | 528 | static DEFINE_STRARRAY(sighow, "SIG_"); |
eb5b1b14 | 529 | |
4f8c1b74 DA |
530 | static const char *clockid[] = { |
531 | "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID", | |
28ebb87c ACM |
532 | "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME", |
533 | "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI" | |
4f8c1b74 | 534 | }; |
2e3d7fac | 535 | static DEFINE_STRARRAY(clockid, "CLOCK_"); |
4f8c1b74 | 536 | |
51108999 ACM |
537 | static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size, |
538 | struct syscall_arg *arg) | |
539 | { | |
c65c83ff ACM |
540 | bool show_prefix = arg->show_string_prefix; |
541 | const char *suffix = "_OK"; | |
51108999 ACM |
542 | size_t printed = 0; |
543 | int mode = arg->val; | |
544 | ||
545 | if (mode == F_OK) /* 0 */ | |
c65c83ff | 546 | return scnprintf(bf, size, "F%s", show_prefix ? suffix : ""); |
51108999 ACM |
547 | #define P_MODE(n) \ |
548 | if (mode & n##_OK) { \ | |
c65c83ff | 549 | printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \ |
51108999 ACM |
550 | mode &= ~n##_OK; \ |
551 | } | |
552 | ||
553 | P_MODE(R); | |
554 | P_MODE(W); | |
555 | P_MODE(X); | |
556 | #undef P_MODE | |
557 | ||
558 | if (mode) | |
559 | printed += scnprintf(bf + printed, size - printed, "|%#x", mode); | |
560 | ||
561 | return printed; | |
562 | } | |
563 | ||
564 | #define SCA_ACCMODE syscall_arg__scnprintf_access_mode | |
565 | ||
f994592d ACM |
566 | static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, |
567 | struct syscall_arg *arg); | |
568 | ||
569 | #define SCA_FILENAME syscall_arg__scnprintf_filename | |
570 | ||
46cce19b ACM |
571 | static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size, |
572 | struct syscall_arg *arg) | |
573 | { | |
c65c83ff ACM |
574 | bool show_prefix = arg->show_string_prefix; |
575 | const char *prefix = "O_"; | |
46cce19b ACM |
576 | int printed = 0, flags = arg->val; |
577 | ||
578 | #define P_FLAG(n) \ | |
579 | if (flags & O_##n) { \ | |
c65c83ff | 580 | printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \ |
46cce19b ACM |
581 | flags &= ~O_##n; \ |
582 | } | |
583 | ||
584 | P_FLAG(CLOEXEC); | |
585 | P_FLAG(NONBLOCK); | |
586 | #undef P_FLAG | |
587 | ||
588 | if (flags) | |
589 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); | |
590 | ||
591 | return printed; | |
592 | } | |
593 | ||
594 | #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags | |
595 | ||
a355a61e ACM |
596 | #ifndef GRND_NONBLOCK |
597 | #define GRND_NONBLOCK 0x0001 | |
598 | #endif | |
599 | #ifndef GRND_RANDOM | |
600 | #define GRND_RANDOM 0x0002 | |
601 | #endif | |
602 | ||
39878d49 ACM |
603 | static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size, |
604 | struct syscall_arg *arg) | |
605 | { | |
c65c83ff ACM |
606 | bool show_prefix = arg->show_string_prefix; |
607 | const char *prefix = "GRND_"; | |
39878d49 ACM |
608 | int printed = 0, flags = arg->val; |
609 | ||
610 | #define P_FLAG(n) \ | |
611 | if (flags & GRND_##n) { \ | |
c65c83ff | 612 | printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \ |
39878d49 ACM |
613 | flags &= ~GRND_##n; \ |
614 | } | |
615 | ||
616 | P_FLAG(RANDOM); | |
617 | P_FLAG(NONBLOCK); | |
618 | #undef P_FLAG | |
619 | ||
620 | if (flags) | |
621 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); | |
622 | ||
623 | return printed; | |
624 | } | |
625 | ||
626 | #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags | |
627 | ||
82d4a110 ACM |
628 | #define STRARRAY(name, array) \ |
629 | { .scnprintf = SCA_STRARRAY, \ | |
630 | .parm = &strarray__##array, } | |
453350dd | 631 | |
092bd3cd | 632 | #include "trace/beauty/arch_errno_names.c" |
ea8dc3ce | 633 | #include "trace/beauty/eventfd.c" |
d5d71e86 | 634 | #include "trace/beauty/futex_op.c" |
3258abe0 | 635 | #include "trace/beauty/futex_val3.c" |
df4cb167 | 636 | #include "trace/beauty/mmap.c" |
ba2f22cf | 637 | #include "trace/beauty/mode_t.c" |
a30e6259 | 638 | #include "trace/beauty/msg_flags.c" |
8f48df69 | 639 | #include "trace/beauty/open_flags.c" |
62de344e | 640 | #include "trace/beauty/perf_event_open.c" |
d5d71e86 | 641 | #include "trace/beauty/pid.c" |
a3bca91f | 642 | #include "trace/beauty/sched_policy.c" |
f5cd95ea | 643 | #include "trace/beauty/seccomp.c" |
12199d8e | 644 | #include "trace/beauty/signum.c" |
bbf86c43 | 645 | #include "trace/beauty/socket_type.c" |
7206b900 | 646 | #include "trace/beauty/waitid_options.c" |
a3bca91f | 647 | |
82d4a110 ACM |
648 | struct syscall_arg_fmt { |
649 | size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg); | |
496fd346 | 650 | unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val); |
82d4a110 | 651 | void *parm; |
c51bdfec | 652 | const char *name; |
d47737d5 | 653 | bool show_zero; |
82d4a110 ACM |
654 | }; |
655 | ||
514f1c67 ACM |
656 | static struct syscall_fmt { |
657 | const char *name; | |
aec1930b | 658 | const char *alias; |
82d4a110 | 659 | struct syscall_arg_fmt arg[6]; |
332337da | 660 | u8 nr_args; |
11c8e39f | 661 | bool errpid; |
514f1c67 | 662 | bool timeout; |
04b34729 | 663 | bool hexret; |
514f1c67 | 664 | } syscall_fmts[] = { |
1f63139c | 665 | { .name = "access", |
82d4a110 | 666 | .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, }, |
02ef2884 ACM |
667 | { .name = "bind", |
668 | .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, }, | |
1f63139c | 669 | { .name = "bpf", |
82d4a110 | 670 | .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, }, |
beccb2b5 | 671 | { .name = "brk", .hexret = true, |
ce05539f | 672 | .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, }, |
1f63139c | 673 | { .name = "clock_gettime", |
82d4a110 | 674 | .arg = { [0] = STRARRAY(clk_id, clockid), }, }, |
33396a3a ACM |
675 | { .name = "clone", .errpid = true, .nr_args = 5, |
676 | .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, }, | |
677 | [1] = { .name = "child_stack", .scnprintf = SCA_HEX, }, | |
678 | [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, }, | |
679 | [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, }, | |
680 | [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, }, | |
1f63139c | 681 | { .name = "close", |
82d4a110 | 682 | .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, }, |
d5a7e661 ACM |
683 | { .name = "connect", |
684 | .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, }, | |
1f63139c | 685 | { .name = "epoll_ctl", |
82d4a110 | 686 | .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, }, |
1f63139c | 687 | { .name = "eventfd2", |
82d4a110 | 688 | .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, }, |
1f63139c | 689 | { .name = "fchmodat", |
82d4a110 | 690 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
1f63139c | 691 | { .name = "fchownat", |
82d4a110 | 692 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
1f63139c | 693 | { .name = "fcntl", |
82d4a110 | 694 | .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */ |
39cc355b ACM |
695 | .parm = &strarrays__fcntl_cmds_arrays, |
696 | .show_zero = true, }, | |
82d4a110 | 697 | [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, }, |
1f63139c | 698 | { .name = "flock", |
82d4a110 | 699 | .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, }, |
1f63139c ACM |
700 | { .name = "fstat", .alias = "newfstat", }, |
701 | { .name = "fstatat", .alias = "newfstatat", }, | |
702 | { .name = "futex", | |
3258abe0 ACM |
703 | .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ }, |
704 | [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, }, | |
1f63139c | 705 | { .name = "futimesat", |
82d4a110 | 706 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
1f63139c | 707 | { .name = "getitimer", |
82d4a110 | 708 | .arg = { [0] = STRARRAY(which, itimers), }, }, |
c65f1070 | 709 | { .name = "getpid", .errpid = true, }, |
d1d438a3 | 710 | { .name = "getpgid", .errpid = true, }, |
c65f1070 | 711 | { .name = "getppid", .errpid = true, }, |
1f63139c | 712 | { .name = "getrandom", |
82d4a110 | 713 | .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, }, |
1f63139c | 714 | { .name = "getrlimit", |
82d4a110 | 715 | .arg = { [0] = STRARRAY(resource, rlimit_resources), }, }, |
2d1073de | 716 | { .name = "gettid", .errpid = true, }, |
1f63139c | 717 | { .name = "ioctl", |
82d4a110 | 718 | .arg = { |
844ae5b4 ACM |
719 | #if defined(__i386__) || defined(__x86_64__) |
720 | /* | |
721 | * FIXME: Make this available to all arches. | |
722 | */ | |
1cc47f2d | 723 | [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ }, |
82d4a110 | 724 | [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, }, |
844ae5b4 | 725 | #else |
82d4a110 | 726 | [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, }, |
844ae5b4 | 727 | #endif |
1de3038d ACM |
728 | { .name = "kcmp", .nr_args = 5, |
729 | .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, }, | |
730 | [1] = { .name = "pid2", .scnprintf = SCA_PID, }, | |
731 | [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, }, | |
732 | [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, }, | |
733 | [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, }, | |
1f63139c | 734 | { .name = "keyctl", |
82d4a110 | 735 | .arg = { [0] = STRARRAY(option, keyctl_options), }, }, |
1f63139c | 736 | { .name = "kill", |
82d4a110 | 737 | .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
1f63139c | 738 | { .name = "linkat", |
82d4a110 | 739 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
1f63139c | 740 | { .name = "lseek", |
82d4a110 | 741 | .arg = { [2] = STRARRAY(whence, whences), }, }, |
1f63139c ACM |
742 | { .name = "lstat", .alias = "newlstat", }, |
743 | { .name = "madvise", | |
82d4a110 ACM |
744 | .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, |
745 | [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, }, | |
1f63139c | 746 | { .name = "mkdirat", |
82d4a110 | 747 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
1f63139c | 748 | { .name = "mknodat", |
82d4a110 | 749 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
beccb2b5 | 750 | { .name = "mmap", .hexret = true, |
54265664 JO |
751 | /* The standard mmap maps to old_mmap on s390x */ |
752 | #if defined(__s390x__) | |
753 | .alias = "old_mmap", | |
754 | #endif | |
ce05539f | 755 | .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, |
82d4a110 | 756 | [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ }, }, }, |
73d141ad | 757 | { .name = "mount", |
23c07a23 ACM |
758 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ }, |
759 | [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */ | |
73d141ad | 760 | .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, }, |
1f63139c | 761 | { .name = "mprotect", |
82d4a110 ACM |
762 | .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, |
763 | [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, }, | |
1f63139c | 764 | { .name = "mq_unlink", |
82d4a110 | 765 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, }, |
ae685380 | 766 | { .name = "mremap", .hexret = true, |
ce05539f | 767 | .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, }, |
1f63139c | 768 | { .name = "name_to_handle_at", |
82d4a110 | 769 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
1f63139c | 770 | { .name = "newfstatat", |
82d4a110 | 771 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
1f63139c | 772 | { .name = "open", |
82d4a110 | 773 | .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, |
1f63139c | 774 | { .name = "open_by_handle_at", |
82d4a110 ACM |
775 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, |
776 | [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, | |
1f63139c | 777 | { .name = "openat", |
82d4a110 ACM |
778 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, |
779 | [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, | |
1f63139c | 780 | { .name = "perf_event_open", |
82d4a110 ACM |
781 | .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ }, |
782 | [3] = { .scnprintf = SCA_FD, /* group_fd */ }, | |
783 | [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, }, | |
1f63139c | 784 | { .name = "pipe2", |
82d4a110 | 785 | .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, }, |
83bc9c37 ACM |
786 | { .name = "pkey_alloc", |
787 | .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, }, | |
788 | { .name = "pkey_free", | |
789 | .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, }, | |
790 | { .name = "pkey_mprotect", | |
791 | .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, | |
792 | [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, | |
793 | [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, }, | |
1f63139c ACM |
794 | { .name = "poll", .timeout = true, }, |
795 | { .name = "ppoll", .timeout = true, }, | |
d688d037 ACM |
796 | { .name = "prctl", .alias = "arch_prctl", |
797 | .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ }, | |
798 | [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ }, | |
799 | [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, }, | |
1f63139c ACM |
800 | { .name = "pread", .alias = "pread64", }, |
801 | { .name = "preadv", .alias = "pread", }, | |
802 | { .name = "prlimit64", | |
82d4a110 | 803 | .arg = { [1] = STRARRAY(resource, rlimit_resources), }, }, |
1f63139c ACM |
804 | { .name = "pwrite", .alias = "pwrite64", }, |
805 | { .name = "readlinkat", | |
82d4a110 | 806 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
1f63139c | 807 | { .name = "recvfrom", |
82d4a110 | 808 | .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
1f63139c | 809 | { .name = "recvmmsg", |
82d4a110 | 810 | .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
1f63139c | 811 | { .name = "recvmsg", |
82d4a110 | 812 | .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
1f63139c | 813 | { .name = "renameat", |
436651ca ACM |
814 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ }, |
815 | [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, }, | |
84a83541 ACM |
816 | { .name = "renameat2", |
817 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ }, | |
ca7ff2c8 ACM |
818 | [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, |
819 | [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, }, | |
1f63139c | 820 | { .name = "rt_sigaction", |
82d4a110 | 821 | .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
1f63139c | 822 | { .name = "rt_sigprocmask", |
82d4a110 | 823 | .arg = { [0] = STRARRAY(how, sighow), }, }, |
1f63139c | 824 | { .name = "rt_sigqueueinfo", |
82d4a110 | 825 | .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
1f63139c | 826 | { .name = "rt_tgsigqueueinfo", |
82d4a110 | 827 | .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
1f63139c | 828 | { .name = "sched_setscheduler", |
82d4a110 | 829 | .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, }, |
1f63139c | 830 | { .name = "seccomp", |
82d4a110 ACM |
831 | .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ }, |
832 | [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, }, | |
1f63139c ACM |
833 | { .name = "select", .timeout = true, }, |
834 | { .name = "sendmmsg", | |
82d4a110 | 835 | .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
1f63139c | 836 | { .name = "sendmsg", |
82d4a110 | 837 | .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
1f63139c | 838 | { .name = "sendto", |
6ebb6862 ACM |
839 | .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, |
840 | [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, }, | |
c65f1070 | 841 | { .name = "set_tid_address", .errpid = true, }, |
1f63139c | 842 | { .name = "setitimer", |
82d4a110 | 843 | .arg = { [0] = STRARRAY(which, itimers), }, }, |
1f63139c | 844 | { .name = "setrlimit", |
82d4a110 | 845 | .arg = { [0] = STRARRAY(resource, rlimit_resources), }, }, |
1f63139c | 846 | { .name = "socket", |
82d4a110 | 847 | .arg = { [0] = STRARRAY(family, socket_families), |
162d3edb ACM |
848 | [1] = { .scnprintf = SCA_SK_TYPE, /* type */ }, |
849 | [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, }, | |
1f63139c | 850 | { .name = "socketpair", |
82d4a110 | 851 | .arg = { [0] = STRARRAY(family, socket_families), |
162d3edb ACM |
852 | [1] = { .scnprintf = SCA_SK_TYPE, /* type */ }, |
853 | [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, }, | |
1f63139c ACM |
854 | { .name = "stat", .alias = "newstat", }, |
855 | { .name = "statx", | |
82d4a110 ACM |
856 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ }, |
857 | [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } , | |
858 | [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, }, | |
1f63139c | 859 | { .name = "swapoff", |
82d4a110 | 860 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, }, |
1f63139c | 861 | { .name = "swapon", |
82d4a110 | 862 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, }, |
1f63139c | 863 | { .name = "symlinkat", |
82d4a110 | 864 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
1f63139c | 865 | { .name = "tgkill", |
82d4a110 | 866 | .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
1f63139c | 867 | { .name = "tkill", |
82d4a110 | 868 | .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
476c92ca ACM |
869 | { .name = "umount2", .alias = "umount", |
870 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, }, | |
1f63139c ACM |
871 | { .name = "uname", .alias = "newuname", }, |
872 | { .name = "unlinkat", | |
82d4a110 | 873 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
1f63139c | 874 | { .name = "utimensat", |
82d4a110 | 875 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, }, |
11c8e39f | 876 | { .name = "wait4", .errpid = true, |
82d4a110 | 877 | .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, }, |
11c8e39f | 878 | { .name = "waitid", .errpid = true, |
82d4a110 | 879 | .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, }, |
514f1c67 ACM |
880 | }; |
881 | ||
882 | static int syscall_fmt__cmp(const void *name, const void *fmtp) | |
883 | { | |
884 | const struct syscall_fmt *fmt = fmtp; | |
885 | return strcmp(name, fmt->name); | |
886 | } | |
887 | ||
888 | static struct syscall_fmt *syscall_fmt__find(const char *name) | |
889 | { | |
890 | const int nmemb = ARRAY_SIZE(syscall_fmts); | |
891 | return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); | |
892 | } | |
893 | ||
f932184e ACM |
894 | static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias) |
895 | { | |
896 | int i, nmemb = ARRAY_SIZE(syscall_fmts); | |
897 | ||
898 | for (i = 0; i < nmemb; ++i) { | |
899 | if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0) | |
900 | return &syscall_fmts[i]; | |
901 | } | |
902 | ||
903 | return NULL; | |
904 | } | |
905 | ||
6a648b53 ACM |
906 | /* |
907 | * is_exit: is this "exit" or "exit_group"? | |
908 | * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter. | |
7a983a0f | 909 | * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc. |
6a648b53 | 910 | */ |
514f1c67 | 911 | struct syscall { |
97fbf3f0 | 912 | struct tep_event *tp_format; |
f208bd8d | 913 | int nr_args; |
7a983a0f | 914 | int args_size; |
6a648b53 ACM |
915 | bool is_exit; |
916 | bool is_open; | |
2c92f982 | 917 | struct tep_format_field *args; |
514f1c67 ACM |
918 | const char *name; |
919 | struct syscall_fmt *fmt; | |
82d4a110 | 920 | struct syscall_arg_fmt *arg_fmt; |
514f1c67 ACM |
921 | }; |
922 | ||
bbab50dd ACM |
923 | struct bpf_map_syscall_entry { |
924 | bool enabled; | |
925 | }; | |
926 | ||
fd2b2975 ACM |
927 | /* |
928 | * We need to have this 'calculated' boolean because in some cases we really | |
929 | * don't know what is the duration of a syscall, for instance, when we start | |
930 | * a session and some threads are waiting for a syscall to finish, say 'poll', | |
931 | * in which case all we can do is to print "( ? ) for duration and for the | |
932 | * start timestamp. | |
933 | */ | |
934 | static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp) | |
60c907ab ACM |
935 | { |
936 | double duration = (double)t / NSEC_PER_MSEC; | |
937 | size_t printed = fprintf(fp, "("); | |
938 | ||
fd2b2975 | 939 | if (!calculated) |
522283fe | 940 | printed += fprintf(fp, " "); |
fd2b2975 | 941 | else if (duration >= 1.0) |
60c907ab ACM |
942 | printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); |
943 | else if (duration >= 0.01) | |
944 | printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); | |
945 | else | |
946 | printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); | |
c24ff998 | 947 | return printed + fprintf(fp, "): "); |
60c907ab ACM |
948 | } |
949 | ||
f994592d ACM |
950 | /** |
951 | * filename.ptr: The filename char pointer that will be vfs_getname'd | |
952 | * filename.entry_str_pos: Where to insert the string translated from | |
953 | * filename.ptr by the vfs_getname tracepoint/kprobe. | |
84486caa ACM |
954 | * ret_scnprintf: syscall args may set this to a different syscall return |
955 | * formatter, for instance, fcntl may return fds, file flags, etc. | |
f994592d | 956 | */ |
752fde44 ACM |
957 | struct thread_trace { |
958 | u64 entry_time; | |
752fde44 | 959 | bool entry_pending; |
efd5745e | 960 | unsigned long nr_events; |
a2ea67d7 | 961 | unsigned long pfmaj, pfmin; |
752fde44 | 962 | char *entry_str; |
1302d88e | 963 | double runtime_ms; |
7ee57434 | 964 | size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg); |
f994592d ACM |
965 | struct { |
966 | unsigned long ptr; | |
7f4f8001 ACM |
967 | short int entry_str_pos; |
968 | bool pending_open; | |
969 | unsigned int namelen; | |
970 | char *name; | |
f994592d | 971 | } filename; |
75b757ca ACM |
972 | struct { |
973 | int max; | |
974 | char **table; | |
975 | } paths; | |
bf2575c1 DA |
976 | |
977 | struct intlist *syscall_stats; | |
752fde44 ACM |
978 | }; |
979 | ||
980 | static struct thread_trace *thread_trace__new(void) | |
981 | { | |
75b757ca ACM |
982 | struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace)); |
983 | ||
984 | if (ttrace) | |
985 | ttrace->paths.max = -1; | |
986 | ||
bf2575c1 DA |
987 | ttrace->syscall_stats = intlist__new(NULL); |
988 | ||
75b757ca | 989 | return ttrace; |
752fde44 ACM |
990 | } |
991 | ||
c24ff998 | 992 | static struct thread_trace *thread__trace(struct thread *thread, FILE *fp) |
752fde44 | 993 | { |
efd5745e ACM |
994 | struct thread_trace *ttrace; |
995 | ||
752fde44 ACM |
996 | if (thread == NULL) |
997 | goto fail; | |
998 | ||
89dceb22 NK |
999 | if (thread__priv(thread) == NULL) |
1000 | thread__set_priv(thread, thread_trace__new()); | |
48000a1a | 1001 | |
89dceb22 | 1002 | if (thread__priv(thread) == NULL) |
752fde44 ACM |
1003 | goto fail; |
1004 | ||
89dceb22 | 1005 | ttrace = thread__priv(thread); |
efd5745e ACM |
1006 | ++ttrace->nr_events; |
1007 | ||
1008 | return ttrace; | |
752fde44 | 1009 | fail: |
c24ff998 | 1010 | color_fprintf(fp, PERF_COLOR_RED, |
752fde44 ACM |
1011 | "WARNING: not enough memory, dropping samples!\n"); |
1012 | return NULL; | |
1013 | } | |
1014 | ||
84486caa ACM |
1015 | |
1016 | void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg, | |
7ee57434 | 1017 | size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg)) |
84486caa ACM |
1018 | { |
1019 | struct thread_trace *ttrace = thread__priv(arg->thread); | |
1020 | ||
1021 | ttrace->ret_scnprintf = ret_scnprintf; | |
1022 | } | |
1023 | ||
598d02c5 SF |
1024 | #define TRACE_PFMAJ (1 << 0) |
1025 | #define TRACE_PFMIN (1 << 1) | |
1026 | ||
e4d44e83 ACM |
1027 | static const size_t trace__entry_str_size = 2048; |
1028 | ||
97119f37 | 1029 | static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname) |
75b757ca | 1030 | { |
89dceb22 | 1031 | struct thread_trace *ttrace = thread__priv(thread); |
75b757ca ACM |
1032 | |
1033 | if (fd > ttrace->paths.max) { | |
1034 | char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *)); | |
1035 | ||
1036 | if (npath == NULL) | |
1037 | return -1; | |
1038 | ||
1039 | if (ttrace->paths.max != -1) { | |
1040 | memset(npath + ttrace->paths.max + 1, 0, | |
1041 | (fd - ttrace->paths.max) * sizeof(char *)); | |
1042 | } else { | |
1043 | memset(npath, 0, (fd + 1) * sizeof(char *)); | |
1044 | } | |
1045 | ||
1046 | ttrace->paths.table = npath; | |
1047 | ttrace->paths.max = fd; | |
1048 | } | |
1049 | ||
1050 | ttrace->paths.table[fd] = strdup(pathname); | |
1051 | ||
1052 | return ttrace->paths.table[fd] != NULL ? 0 : -1; | |
1053 | } | |
1054 | ||
97119f37 ACM |
1055 | static int thread__read_fd_path(struct thread *thread, int fd) |
1056 | { | |
1057 | char linkname[PATH_MAX], pathname[PATH_MAX]; | |
1058 | struct stat st; | |
1059 | int ret; | |
1060 | ||
1061 | if (thread->pid_ == thread->tid) { | |
1062 | scnprintf(linkname, sizeof(linkname), | |
1063 | "/proc/%d/fd/%d", thread->pid_, fd); | |
1064 | } else { | |
1065 | scnprintf(linkname, sizeof(linkname), | |
1066 | "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd); | |
1067 | } | |
1068 | ||
1069 | if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname)) | |
1070 | return -1; | |
1071 | ||
1072 | ret = readlink(linkname, pathname, sizeof(pathname)); | |
1073 | ||
1074 | if (ret < 0 || ret > st.st_size) | |
1075 | return -1; | |
1076 | ||
1077 | pathname[ret] = '\0'; | |
1078 | return trace__set_fd_pathname(thread, fd, pathname); | |
1079 | } | |
1080 | ||
c522739d ACM |
1081 | static const char *thread__fd_path(struct thread *thread, int fd, |
1082 | struct trace *trace) | |
75b757ca | 1083 | { |
89dceb22 | 1084 | struct thread_trace *ttrace = thread__priv(thread); |
75b757ca ACM |
1085 | |
1086 | if (ttrace == NULL) | |
1087 | return NULL; | |
1088 | ||
1089 | if (fd < 0) | |
1090 | return NULL; | |
1091 | ||
cdcd1e6b | 1092 | if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) { |
c522739d ACM |
1093 | if (!trace->live) |
1094 | return NULL; | |
1095 | ++trace->stats.proc_getname; | |
cdcd1e6b | 1096 | if (thread__read_fd_path(thread, fd)) |
c522739d ACM |
1097 | return NULL; |
1098 | } | |
75b757ca ACM |
1099 | |
1100 | return ttrace->paths.table[fd]; | |
1101 | } | |
1102 | ||
fc65eb82 | 1103 | size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg) |
75b757ca ACM |
1104 | { |
1105 | int fd = arg->val; | |
1106 | size_t printed = scnprintf(bf, size, "%d", fd); | |
c522739d | 1107 | const char *path = thread__fd_path(arg->thread, fd, arg->trace); |
75b757ca ACM |
1108 | |
1109 | if (path) | |
1110 | printed += scnprintf(bf + printed, size - printed, "<%s>", path); | |
1111 | ||
1112 | return printed; | |
1113 | } | |
1114 | ||
0a2f7540 ACM |
1115 | size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size) |
1116 | { | |
1117 | size_t printed = scnprintf(bf, size, "%d", fd); | |
1118 | struct thread *thread = machine__find_thread(trace->host, pid, pid); | |
1119 | ||
1120 | if (thread) { | |
1121 | const char *path = thread__fd_path(thread, fd, trace); | |
1122 | ||
1123 | if (path) | |
1124 | printed += scnprintf(bf + printed, size - printed, "<%s>", path); | |
1125 | ||
1126 | thread__put(thread); | |
1127 | } | |
1128 | ||
1129 | return printed; | |
1130 | } | |
1131 | ||
75b757ca ACM |
1132 | static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, |
1133 | struct syscall_arg *arg) | |
1134 | { | |
1135 | int fd = arg->val; | |
1136 | size_t printed = syscall_arg__scnprintf_fd(bf, size, arg); | |
89dceb22 | 1137 | struct thread_trace *ttrace = thread__priv(arg->thread); |
75b757ca | 1138 | |
04662523 ACM |
1139 | if (ttrace && fd >= 0 && fd <= ttrace->paths.max) |
1140 | zfree(&ttrace->paths.table[fd]); | |
75b757ca ACM |
1141 | |
1142 | return printed; | |
1143 | } | |
1144 | ||
f994592d ACM |
1145 | static void thread__set_filename_pos(struct thread *thread, const char *bf, |
1146 | unsigned long ptr) | |
1147 | { | |
1148 | struct thread_trace *ttrace = thread__priv(thread); | |
1149 | ||
1150 | ttrace->filename.ptr = ptr; | |
1151 | ttrace->filename.entry_str_pos = bf - ttrace->entry_str; | |
1152 | } | |
1153 | ||
75d1e306 ACM |
1154 | static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size) |
1155 | { | |
1156 | struct augmented_arg *augmented_arg = arg->augmented.args; | |
1157 | ||
721f5326 | 1158 | return scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value); |
75d1e306 ACM |
1159 | } |
1160 | ||
f994592d ACM |
1161 | static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, |
1162 | struct syscall_arg *arg) | |
1163 | { | |
1164 | unsigned long ptr = arg->val; | |
1165 | ||
75d1e306 ACM |
1166 | if (arg->augmented.args) |
1167 | return syscall_arg__scnprintf_augmented_string(arg, bf, size); | |
1168 | ||
f994592d ACM |
1169 | if (!arg->trace->vfs_getname) |
1170 | return scnprintf(bf, size, "%#x", ptr); | |
1171 | ||
1172 | thread__set_filename_pos(arg->thread, bf, ptr); | |
1173 | return 0; | |
1174 | } | |
1175 | ||
ae9ed035 ACM |
1176 | static bool trace__filter_duration(struct trace *trace, double t) |
1177 | { | |
1178 | return t < (trace->duration_filter * NSEC_PER_MSEC); | |
1179 | } | |
1180 | ||
fd2b2975 | 1181 | static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) |
752fde44 ACM |
1182 | { |
1183 | double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; | |
1184 | ||
60c907ab | 1185 | return fprintf(fp, "%10.3f ", ts); |
752fde44 ACM |
1186 | } |
1187 | ||
fd2b2975 ACM |
1188 | /* |
1189 | * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are | |
1190 | * using ttrace->entry_time for a thread that receives a sys_exit without | |
1191 | * first having received a sys_enter ("poll" issued before tracing session | |
1192 | * starts, lost sys_enter exit due to ring buffer overflow). | |
1193 | */ | |
1194 | static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) | |
1195 | { | |
1196 | if (tstamp > 0) | |
1197 | return __trace__fprintf_tstamp(trace, tstamp, fp); | |
1198 | ||
1199 | return fprintf(fp, " ? "); | |
1200 | } | |
1201 | ||
f15eb531 | 1202 | static bool done = false; |
ba209f85 | 1203 | static bool interrupted = false; |
f15eb531 | 1204 | |
ba209f85 | 1205 | static void sig_handler(int sig) |
f15eb531 NK |
1206 | { |
1207 | done = true; | |
ba209f85 | 1208 | interrupted = sig == SIGINT; |
f15eb531 NK |
1209 | } |
1210 | ||
6dcbd212 | 1211 | static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp) |
752fde44 | 1212 | { |
6dcbd212 | 1213 | size_t printed = 0; |
752fde44 | 1214 | |
50c95cbd ACM |
1215 | if (trace->multiple_threads) { |
1216 | if (trace->show_comm) | |
1902efe7 | 1217 | printed += fprintf(fp, "%.14s/", thread__comm_str(thread)); |
38051234 | 1218 | printed += fprintf(fp, "%d ", thread->tid); |
50c95cbd | 1219 | } |
752fde44 ACM |
1220 | |
1221 | return printed; | |
1222 | } | |
1223 | ||
6dcbd212 ACM |
1224 | static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread, |
1225 | u64 duration, bool duration_calculated, u64 tstamp, FILE *fp) | |
1226 | { | |
b036146f ACM |
1227 | size_t printed = 0; |
1228 | ||
1229 | if (trace->show_tstamp) | |
1230 | printed = trace__fprintf_tstamp(trace, tstamp, fp); | |
42e4a52d ACM |
1231 | if (trace->show_duration) |
1232 | printed += fprintf_duration(duration, duration_calculated, fp); | |
6dcbd212 ACM |
1233 | return printed + trace__fprintf_comm_tid(trace, thread, fp); |
1234 | } | |
1235 | ||
c24ff998 | 1236 | static int trace__process_event(struct trace *trace, struct machine *machine, |
162f0bef | 1237 | union perf_event *event, struct perf_sample *sample) |
752fde44 ACM |
1238 | { |
1239 | int ret = 0; | |
1240 | ||
1241 | switch (event->header.type) { | |
1242 | case PERF_RECORD_LOST: | |
c24ff998 | 1243 | color_fprintf(trace->output, PERF_COLOR_RED, |
752fde44 | 1244 | "LOST %" PRIu64 " events!\n", event->lost.lost); |
162f0bef | 1245 | ret = machine__process_lost_event(machine, event, sample); |
3ed5ca2e | 1246 | break; |
752fde44 | 1247 | default: |
162f0bef | 1248 | ret = machine__process_event(machine, event, sample); |
752fde44 ACM |
1249 | break; |
1250 | } | |
1251 | ||
1252 | return ret; | |
1253 | } | |
1254 | ||
c24ff998 | 1255 | static int trace__tool_process(struct perf_tool *tool, |
752fde44 | 1256 | union perf_event *event, |
162f0bef | 1257 | struct perf_sample *sample, |
752fde44 ACM |
1258 | struct machine *machine) |
1259 | { | |
c24ff998 | 1260 | struct trace *trace = container_of(tool, struct trace, tool); |
162f0bef | 1261 | return trace__process_event(trace, machine, event, sample); |
752fde44 ACM |
1262 | } |
1263 | ||
caf8a0d0 ACM |
1264 | static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp) |
1265 | { | |
1266 | struct machine *machine = vmachine; | |
1267 | ||
1268 | if (machine->kptr_restrict_warned) | |
1269 | return NULL; | |
1270 | ||
1271 | if (symbol_conf.kptr_restrict) { | |
1272 | pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n" | |
1273 | "Check /proc/sys/kernel/kptr_restrict.\n\n" | |
1274 | "Kernel samples will not be resolved.\n"); | |
1275 | machine->kptr_restrict_warned = true; | |
1276 | return NULL; | |
1277 | } | |
1278 | ||
1279 | return machine__resolve_kernel_addr(vmachine, addrp, modp); | |
1280 | } | |
1281 | ||
752fde44 ACM |
1282 | static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) |
1283 | { | |
0a7e6d1b | 1284 | int err = symbol__init(NULL); |
752fde44 ACM |
1285 | |
1286 | if (err) | |
1287 | return err; | |
1288 | ||
8fb598e5 DA |
1289 | trace->host = machine__new_host(); |
1290 | if (trace->host == NULL) | |
1291 | return -ENOMEM; | |
752fde44 | 1292 | |
cbd5c178 AV |
1293 | err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr); |
1294 | if (err < 0) | |
1295 | goto out; | |
706c3da4 | 1296 | |
a33fbd56 | 1297 | err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target, |
9d9cad76 | 1298 | evlist->threads, trace__tool_process, false, |
3fcb10e4 | 1299 | 1); |
cbd5c178 | 1300 | out: |
752fde44 ACM |
1301 | if (err) |
1302 | symbol__exit(); | |
1303 | ||
1304 | return err; | |
1305 | } | |
1306 | ||
33974a41 AV |
1307 | static void trace__symbols__exit(struct trace *trace) |
1308 | { | |
1309 | machine__exit(trace->host); | |
1310 | trace->host = NULL; | |
1311 | ||
1312 | symbol__exit(); | |
1313 | } | |
1314 | ||
5e58fcfa | 1315 | static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args) |
13d4ff3e | 1316 | { |
5e58fcfa | 1317 | int idx; |
13d4ff3e | 1318 | |
332337da ACM |
1319 | if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0) |
1320 | nr_args = sc->fmt->nr_args; | |
1321 | ||
5e58fcfa | 1322 | sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt)); |
82d4a110 | 1323 | if (sc->arg_fmt == NULL) |
13d4ff3e ACM |
1324 | return -1; |
1325 | ||
5e58fcfa ACM |
1326 | for (idx = 0; idx < nr_args; ++idx) { |
1327 | if (sc->fmt) | |
82d4a110 | 1328 | sc->arg_fmt[idx] = sc->fmt->arg[idx]; |
5e58fcfa | 1329 | } |
82d4a110 | 1330 | |
5e58fcfa ACM |
1331 | sc->nr_args = nr_args; |
1332 | return 0; | |
1333 | } | |
1334 | ||
1335 | static int syscall__set_arg_fmts(struct syscall *sc) | |
1336 | { | |
2c92f982 | 1337 | struct tep_format_field *field, *last_field = NULL; |
5e58fcfa ACM |
1338 | int idx = 0, len; |
1339 | ||
1340 | for (field = sc->args; field; field = field->next, ++idx) { | |
7a983a0f ACM |
1341 | last_field = field; |
1342 | ||
5e58fcfa ACM |
1343 | if (sc->fmt && sc->fmt->arg[idx].scnprintf) |
1344 | continue; | |
1f115cb7 | 1345 | |
82d4a110 | 1346 | if (strcmp(field->type, "const char *") == 0 && |
12f3ca4f ACM |
1347 | (strcmp(field->name, "filename") == 0 || |
1348 | strcmp(field->name, "path") == 0 || | |
1349 | strcmp(field->name, "pathname") == 0)) | |
82d4a110 | 1350 | sc->arg_fmt[idx].scnprintf = SCA_FILENAME; |
ce05539f ACM |
1351 | else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr")) |
1352 | sc->arg_fmt[idx].scnprintf = SCA_PTR; | |
d1d438a3 | 1353 | else if (strcmp(field->type, "pid_t") == 0) |
82d4a110 | 1354 | sc->arg_fmt[idx].scnprintf = SCA_PID; |
ba2f22cf | 1355 | else if (strcmp(field->type, "umode_t") == 0) |
82d4a110 | 1356 | sc->arg_fmt[idx].scnprintf = SCA_MODE_T; |
b6565c90 ACM |
1357 | else if ((strcmp(field->type, "int") == 0 || |
1358 | strcmp(field->type, "unsigned int") == 0 || | |
1359 | strcmp(field->type, "long") == 0) && | |
1360 | (len = strlen(field->name)) >= 2 && | |
1361 | strcmp(field->name + len - 2, "fd") == 0) { | |
1362 | /* | |
1363 | * /sys/kernel/tracing/events/syscalls/sys_enter* | |
1364 | * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c | |
1365 | * 65 int | |
1366 | * 23 unsigned int | |
1367 | * 7 unsigned long | |
1368 | */ | |
82d4a110 | 1369 | sc->arg_fmt[idx].scnprintf = SCA_FD; |
b6565c90 | 1370 | } |
13d4ff3e ACM |
1371 | } |
1372 | ||
7a983a0f ACM |
1373 | if (last_field) |
1374 | sc->args_size = last_field->offset + last_field->size; | |
1375 | ||
13d4ff3e ACM |
1376 | return 0; |
1377 | } | |
1378 | ||
514f1c67 ACM |
1379 | static int trace__read_syscall_info(struct trace *trace, int id) |
1380 | { | |
1381 | char tp_name[128]; | |
1382 | struct syscall *sc; | |
fd0db102 | 1383 | const char *name = syscalltbl__name(trace->sctbl, id); |
3a531260 ACM |
1384 | |
1385 | if (name == NULL) | |
1386 | return -1; | |
514f1c67 ACM |
1387 | |
1388 | if (id > trace->syscalls.max) { | |
1389 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); | |
1390 | ||
1391 | if (nsyscalls == NULL) | |
1392 | return -1; | |
1393 | ||
1394 | if (trace->syscalls.max != -1) { | |
1395 | memset(nsyscalls + trace->syscalls.max + 1, 0, | |
1396 | (id - trace->syscalls.max) * sizeof(*sc)); | |
1397 | } else { | |
1398 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); | |
1399 | } | |
1400 | ||
1401 | trace->syscalls.table = nsyscalls; | |
1402 | trace->syscalls.max = id; | |
1403 | } | |
1404 | ||
1405 | sc = trace->syscalls.table + id; | |
3a531260 | 1406 | sc->name = name; |
2ae3a312 | 1407 | |
3a531260 | 1408 | sc->fmt = syscall_fmt__find(sc->name); |
514f1c67 | 1409 | |
aec1930b | 1410 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
97978b3e | 1411 | sc->tp_format = trace_event__tp_format("syscalls", tp_name); |
aec1930b | 1412 | |
8dd2a131 | 1413 | if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) { |
aec1930b | 1414 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); |
97978b3e | 1415 | sc->tp_format = trace_event__tp_format("syscalls", tp_name); |
aec1930b | 1416 | } |
514f1c67 | 1417 | |
5e58fcfa ACM |
1418 | if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields)) |
1419 | return -1; | |
1420 | ||
8dd2a131 | 1421 | if (IS_ERR(sc->tp_format)) |
13d4ff3e ACM |
1422 | return -1; |
1423 | ||
f208bd8d | 1424 | sc->args = sc->tp_format->format.fields; |
c42de706 TS |
1425 | /* |
1426 | * We need to check and discard the first variable '__syscall_nr' | |
1427 | * or 'nr' that mean the syscall number. It is needless here. | |
1428 | * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. | |
1429 | */ | |
1430 | if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) { | |
f208bd8d ACM |
1431 | sc->args = sc->args->next; |
1432 | --sc->nr_args; | |
1433 | } | |
1434 | ||
5089f20e | 1435 | sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit"); |
6a648b53 | 1436 | sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat"); |
5089f20e | 1437 | |
13d4ff3e | 1438 | return syscall__set_arg_fmts(sc); |
514f1c67 ACM |
1439 | } |
1440 | ||
d0cc439b ACM |
1441 | static int trace__validate_ev_qualifier(struct trace *trace) |
1442 | { | |
8b3ce757 | 1443 | int err = 0, i; |
27702bcf | 1444 | size_t nr_allocated; |
d0cc439b ACM |
1445 | struct str_node *pos; |
1446 | ||
8b3ce757 ACM |
1447 | trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier); |
1448 | trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr * | |
1449 | sizeof(trace->ev_qualifier_ids.entries[0])); | |
1450 | ||
1451 | if (trace->ev_qualifier_ids.entries == NULL) { | |
1452 | fputs("Error:\tNot enough memory for allocating events qualifier ids\n", | |
1453 | trace->output); | |
1454 | err = -EINVAL; | |
1455 | goto out; | |
1456 | } | |
1457 | ||
27702bcf | 1458 | nr_allocated = trace->ev_qualifier_ids.nr; |
8b3ce757 ACM |
1459 | i = 0; |
1460 | ||
602a1f4d | 1461 | strlist__for_each_entry(pos, trace->ev_qualifier) { |
d0cc439b | 1462 | const char *sc = pos->s; |
27702bcf | 1463 | int id = syscalltbl__id(trace->sctbl, sc), match_next = -1; |
d0cc439b | 1464 | |
8b3ce757 | 1465 | if (id < 0) { |
27702bcf ACM |
1466 | id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next); |
1467 | if (id >= 0) | |
1468 | goto matches; | |
1469 | ||
d0cc439b ACM |
1470 | if (err == 0) { |
1471 | fputs("Error:\tInvalid syscall ", trace->output); | |
1472 | err = -EINVAL; | |
1473 | } else { | |
1474 | fputs(", ", trace->output); | |
1475 | } | |
1476 | ||
1477 | fputs(sc, trace->output); | |
1478 | } | |
27702bcf | 1479 | matches: |
8b3ce757 | 1480 | trace->ev_qualifier_ids.entries[i++] = id; |
27702bcf ACM |
1481 | if (match_next == -1) |
1482 | continue; | |
1483 | ||
1484 | while (1) { | |
1485 | id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next); | |
1486 | if (id < 0) | |
1487 | break; | |
1488 | if (nr_allocated == trace->ev_qualifier_ids.nr) { | |
1489 | void *entries; | |
1490 | ||
1491 | nr_allocated += 8; | |
1492 | entries = realloc(trace->ev_qualifier_ids.entries, | |
1493 | nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0])); | |
1494 | if (entries == NULL) { | |
1495 | err = -ENOMEM; | |
1496 | fputs("\nError:\t Not enough memory for parsing\n", trace->output); | |
1497 | goto out_free; | |
1498 | } | |
1499 | trace->ev_qualifier_ids.entries = entries; | |
1500 | } | |
1501 | trace->ev_qualifier_ids.nr++; | |
1502 | trace->ev_qualifier_ids.entries[i++] = id; | |
1503 | } | |
d0cc439b ACM |
1504 | } |
1505 | ||
1506 | if (err < 0) { | |
1507 | fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'" | |
1508 | "\nHint:\tand: 'man syscalls'\n", trace->output); | |
27702bcf | 1509 | out_free: |
8b3ce757 ACM |
1510 | zfree(&trace->ev_qualifier_ids.entries); |
1511 | trace->ev_qualifier_ids.nr = 0; | |
d0cc439b | 1512 | } |
8b3ce757 | 1513 | out: |
d0cc439b ACM |
1514 | return err; |
1515 | } | |
1516 | ||
55d43bca DA |
1517 | /* |
1518 | * args is to be interpreted as a series of longs but we need to handle | |
1519 | * 8-byte unaligned accesses. args points to raw_data within the event | |
1520 | * and raw_data is guaranteed to be 8-byte unaligned because it is | |
1521 | * preceded by raw_size which is a u32. So we need to copy args to a temp | |
1522 | * variable to read it. Most notably this avoids extended load instructions | |
1523 | * on unaligned addresses | |
1524 | */ | |
325f5091 | 1525 | unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx) |
f9f83b33 ACM |
1526 | { |
1527 | unsigned long val; | |
325f5091 | 1528 | unsigned char *p = arg->args + sizeof(unsigned long) * idx; |
f9f83b33 ACM |
1529 | |
1530 | memcpy(&val, p, sizeof(val)); | |
1531 | return val; | |
1532 | } | |
1533 | ||
c51bdfec ACM |
1534 | static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size, |
1535 | struct syscall_arg *arg) | |
1536 | { | |
1537 | if (sc->arg_fmt && sc->arg_fmt[arg->idx].name) | |
1538 | return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name); | |
1539 | ||
1540 | return scnprintf(bf, size, "arg%d: ", arg->idx); | |
1541 | } | |
1542 | ||
496fd346 ACM |
1543 | /* |
1544 | * Check if the value is in fact zero, i.e. mask whatever needs masking, such | |
1545 | * as mount 'flags' argument that needs ignoring some magic flag, see comment | |
1546 | * in tools/perf/trace/beauty/mount_flags.c | |
1547 | */ | |
1548 | static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val) | |
1549 | { | |
1550 | if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val) | |
1551 | return sc->arg_fmt[arg->idx].mask_val(arg, val); | |
1552 | ||
1553 | return val; | |
1554 | } | |
1555 | ||
d032d79e ACM |
1556 | static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size, |
1557 | struct syscall_arg *arg, unsigned long val) | |
1558 | { | |
1559 | if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) { | |
1560 | arg->val = val; | |
1561 | if (sc->arg_fmt[arg->idx].parm) | |
1562 | arg->parm = sc->arg_fmt[arg->idx].parm; | |
1563 | return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg); | |
1564 | } | |
1565 | return scnprintf(bf, size, "%ld", val); | |
1566 | } | |
1567 | ||
752fde44 | 1568 | static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, |
7a983a0f ACM |
1569 | unsigned char *args, void *augmented_args, int augmented_args_size, |
1570 | struct trace *trace, struct thread *thread) | |
514f1c67 | 1571 | { |
514f1c67 | 1572 | size_t printed = 0; |
55d43bca | 1573 | unsigned long val; |
d032d79e ACM |
1574 | u8 bit = 1; |
1575 | struct syscall_arg arg = { | |
1576 | .args = args, | |
7a983a0f ACM |
1577 | .augmented = { |
1578 | .size = augmented_args_size, | |
1579 | .args = augmented_args, | |
1580 | }, | |
d032d79e ACM |
1581 | .idx = 0, |
1582 | .mask = 0, | |
1583 | .trace = trace, | |
1584 | .thread = thread, | |
c65c83ff | 1585 | .show_string_prefix = trace->show_string_prefix, |
d032d79e | 1586 | }; |
84486caa ACM |
1587 | struct thread_trace *ttrace = thread__priv(thread); |
1588 | ||
1589 | /* | |
1590 | * Things like fcntl will set this in its 'cmd' formatter to pick the | |
1591 | * right formatter for the return value (an fd? file flags?), which is | |
1592 | * not needed for syscalls that always return a given type, say an fd. | |
1593 | */ | |
1594 | ttrace->ret_scnprintf = NULL; | |
514f1c67 | 1595 | |
f208bd8d | 1596 | if (sc->args != NULL) { |
2c92f982 | 1597 | struct tep_format_field *field; |
6e7eeb51 | 1598 | |
f208bd8d | 1599 | for (field = sc->args; field; |
01533e97 ACM |
1600 | field = field->next, ++arg.idx, bit <<= 1) { |
1601 | if (arg.mask & bit) | |
6e7eeb51 | 1602 | continue; |
55d43bca | 1603 | |
f9f83b33 | 1604 | val = syscall_arg__val(&arg, arg.idx); |
496fd346 ACM |
1605 | /* |
1606 | * Some syscall args need some mask, most don't and | |
1607 | * return val untouched. | |
1608 | */ | |
1609 | val = syscall__mask_val(sc, &arg, val); | |
55d43bca | 1610 | |
4aa58232 ACM |
1611 | /* |
1612 | * Suppress this argument if its value is zero and | |
1613 | * and we don't have a string associated in an | |
1614 | * strarray for it. | |
1615 | */ | |
55d43bca | 1616 | if (val == 0 && |
e7c634fc | 1617 | !trace->show_zeros && |
82d4a110 | 1618 | !(sc->arg_fmt && |
d47737d5 ACM |
1619 | (sc->arg_fmt[arg.idx].show_zero || |
1620 | sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY || | |
82d4a110 ACM |
1621 | sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) && |
1622 | sc->arg_fmt[arg.idx].parm)) | |
22ae5cf1 ACM |
1623 | continue; |
1624 | ||
9d6dc178 ACM |
1625 | printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : ""); |
1626 | ||
1627 | if (trace->show_arg_names) | |
1628 | printed += scnprintf(bf + printed, size - printed, "%s: ", field->name); | |
1629 | ||
d032d79e | 1630 | printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val); |
514f1c67 | 1631 | } |
4c4d6e51 ACM |
1632 | } else if (IS_ERR(sc->tp_format)) { |
1633 | /* | |
1634 | * If we managed to read the tracepoint /format file, then we | |
1635 | * may end up not having any args, like with gettid(), so only | |
1636 | * print the raw args when we didn't manage to read it. | |
1637 | */ | |
332337da | 1638 | while (arg.idx < sc->nr_args) { |
d032d79e ACM |
1639 | if (arg.mask & bit) |
1640 | goto next_arg; | |
1641 | val = syscall_arg__val(&arg, arg.idx); | |
c51bdfec ACM |
1642 | if (printed) |
1643 | printed += scnprintf(bf + printed, size - printed, ", "); | |
1644 | printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg); | |
d032d79e ACM |
1645 | printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val); |
1646 | next_arg: | |
1647 | ++arg.idx; | |
1648 | bit <<= 1; | |
514f1c67 ACM |
1649 | } |
1650 | } | |
1651 | ||
1652 | return printed; | |
1653 | } | |
1654 | ||
ba3d7dee | 1655 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 1656 | union perf_event *event, |
ba3d7dee ACM |
1657 | struct perf_sample *sample); |
1658 | ||
1659 | static struct syscall *trace__syscall_info(struct trace *trace, | |
bf2575c1 | 1660 | struct perf_evsel *evsel, int id) |
ba3d7dee | 1661 | { |
ba3d7dee ACM |
1662 | |
1663 | if (id < 0) { | |
adaa18bf ACM |
1664 | |
1665 | /* | |
1666 | * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried | |
1667 | * before that, leaving at a higher verbosity level till that is | |
1668 | * explained. Reproduced with plain ftrace with: | |
1669 | * | |
1670 | * echo 1 > /t/events/raw_syscalls/sys_exit/enable | |
1671 | * grep "NR -1 " /t/trace_pipe | |
1672 | * | |
1673 | * After generating some load on the machine. | |
1674 | */ | |
1675 | if (verbose > 1) { | |
1676 | static u64 n; | |
1677 | fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n", | |
1678 | id, perf_evsel__name(evsel), ++n); | |
1679 | } | |
ba3d7dee ACM |
1680 | return NULL; |
1681 | } | |
1682 | ||
1683 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && | |
1684 | trace__read_syscall_info(trace, id)) | |
1685 | goto out_cant_read; | |
1686 | ||
1687 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) | |
1688 | goto out_cant_read; | |
1689 | ||
1690 | return &trace->syscalls.table[id]; | |
1691 | ||
1692 | out_cant_read: | |
bb963e16 | 1693 | if (verbose > 0) { |
7c304ee0 ACM |
1694 | fprintf(trace->output, "Problems reading syscall %d", id); |
1695 | if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL) | |
1696 | fprintf(trace->output, "(%s)", trace->syscalls.table[id].name); | |
1697 | fputs(" information\n", trace->output); | |
1698 | } | |
ba3d7dee ACM |
1699 | return NULL; |
1700 | } | |
1701 | ||
bf2575c1 DA |
1702 | static void thread__update_stats(struct thread_trace *ttrace, |
1703 | int id, struct perf_sample *sample) | |
1704 | { | |
1705 | struct int_node *inode; | |
1706 | struct stats *stats; | |
1707 | u64 duration = 0; | |
1708 | ||
1709 | inode = intlist__findnew(ttrace->syscall_stats, id); | |
1710 | if (inode == NULL) | |
1711 | return; | |
1712 | ||
1713 | stats = inode->priv; | |
1714 | if (stats == NULL) { | |
1715 | stats = malloc(sizeof(struct stats)); | |
1716 | if (stats == NULL) | |
1717 | return; | |
1718 | init_stats(stats); | |
1719 | inode->priv = stats; | |
1720 | } | |
1721 | ||
1722 | if (ttrace->entry_time && sample->time > ttrace->entry_time) | |
1723 | duration = sample->time - ttrace->entry_time; | |
1724 | ||
1725 | update_stats(stats, duration); | |
1726 | } | |
1727 | ||
522283fe | 1728 | static int trace__printf_interrupted_entry(struct trace *trace) |
e596663e ACM |
1729 | { |
1730 | struct thread_trace *ttrace; | |
e596663e ACM |
1731 | size_t printed; |
1732 | ||
0a6545bd | 1733 | if (trace->failure_only || trace->current == NULL) |
e596663e ACM |
1734 | return 0; |
1735 | ||
1736 | ttrace = thread__priv(trace->current); | |
1737 | ||
1738 | if (!ttrace->entry_pending) | |
1739 | return 0; | |
1740 | ||
522283fe | 1741 | printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output); |
4b8a240e | 1742 | printed += fprintf(trace->output, ")%-*s ...\n", trace->args_alignment, ttrace->entry_str); |
e596663e ACM |
1743 | ttrace->entry_pending = false; |
1744 | ||
5067a8cd ACM |
1745 | ++trace->nr_events_printed; |
1746 | ||
e596663e ACM |
1747 | return printed; |
1748 | } | |
1749 | ||
591421e1 ACM |
1750 | static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel, |
1751 | struct perf_sample *sample, struct thread *thread) | |
1752 | { | |
1753 | int printed = 0; | |
1754 | ||
1755 | if (trace->print_sample) { | |
1756 | double ts = (double)sample->time / NSEC_PER_MSEC; | |
1757 | ||
1758 | printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n", | |
1759 | perf_evsel__name(evsel), ts, | |
1760 | thread__comm_str(thread), | |
1761 | sample->pid, sample->tid, sample->cpu); | |
1762 | } | |
1763 | ||
1764 | return printed; | |
1765 | } | |
1766 | ||
cd26ea6d | 1767 | static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, bool raw_augmented) |
8a041f86 ACM |
1768 | { |
1769 | void *augmented_args = NULL; | |
cd26ea6d ACM |
1770 | /* |
1771 | * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter | |
1772 | * and there we get all 6 syscall args plus the tracepoint common | |
1773 | * fields (sizeof(long)) and the syscall_nr (another long). So we check | |
1774 | * if that is the case and if so don't look after the sc->args_size, | |
1775 | * but always after the full raw_syscalls:sys_enter payload, which is | |
1776 | * fixed. | |
1777 | * | |
1778 | * We'll revisit this later to pass s->args_size to the BPF augmenter | |
1779 | * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it | |
1780 | * copies only what we need for each syscall, like what happens when we | |
1781 | * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace | |
1782 | * traffic to just what is needed for each syscall. | |
1783 | */ | |
1784 | int args_size = raw_augmented ? (8 * (int)sizeof(long)) : sc->args_size; | |
8a041f86 | 1785 | |
cd26ea6d | 1786 | *augmented_args_size = sample->raw_size - args_size; |
8a041f86 | 1787 | if (*augmented_args_size > 0) |
cd26ea6d | 1788 | augmented_args = sample->raw_data + args_size; |
8a041f86 ACM |
1789 | |
1790 | return augmented_args; | |
1791 | } | |
1792 | ||
ba3d7dee | 1793 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 1794 | union perf_event *event __maybe_unused, |
ba3d7dee ACM |
1795 | struct perf_sample *sample) |
1796 | { | |
752fde44 | 1797 | char *msg; |
ba3d7dee | 1798 | void *args; |
4b8a240e | 1799 | int printed = 0; |
2ae3a312 | 1800 | struct thread *thread; |
b91fc39f | 1801 | int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1; |
db2da3f8 ACM |
1802 | int augmented_args_size = 0; |
1803 | void *augmented_args = NULL; | |
bf2575c1 | 1804 | struct syscall *sc = trace__syscall_info(trace, evsel, id); |
2ae3a312 ACM |
1805 | struct thread_trace *ttrace; |
1806 | ||
1807 | if (sc == NULL) | |
1808 | return -1; | |
ba3d7dee | 1809 | |
8fb598e5 | 1810 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
c24ff998 | 1811 | ttrace = thread__trace(thread, trace->output); |
2ae3a312 | 1812 | if (ttrace == NULL) |
b91fc39f | 1813 | goto out_put; |
ba3d7dee | 1814 | |
591421e1 ACM |
1815 | trace__fprintf_sample(trace, evsel, sample, thread); |
1816 | ||
77170988 | 1817 | args = perf_evsel__sc_tp_ptr(evsel, args, sample); |
752fde44 ACM |
1818 | |
1819 | if (ttrace->entry_str == NULL) { | |
e4d44e83 | 1820 | ttrace->entry_str = malloc(trace__entry_str_size); |
752fde44 | 1821 | if (!ttrace->entry_str) |
b91fc39f | 1822 | goto out_put; |
752fde44 ACM |
1823 | } |
1824 | ||
5cf9c84e | 1825 | if (!(trace->duration_filter || trace->summary_only || trace->min_stack)) |
522283fe | 1826 | trace__printf_interrupted_entry(trace); |
db2da3f8 ACM |
1827 | /* |
1828 | * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible | |
1829 | * arguments, even if the syscall being handled, say "openat", uses only 4 arguments | |
1830 | * this breaks syscall__augmented_args() check for augmented args, as we calculate | |
1831 | * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file, | |
1832 | * so when handling, say the openat syscall, we end up getting 6 args for the | |
1833 | * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly | |
1834 | * thinking that the extra 2 u64 args are the augmented filename, so just check | |
1835 | * here and avoid using augmented syscalls when the evsel is the raw_syscalls one. | |
1836 | */ | |
1837 | if (evsel != trace->syscalls.events.sys_enter) | |
cd26ea6d | 1838 | augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls); |
752fde44 ACM |
1839 | ttrace->entry_time = sample->time; |
1840 | msg = ttrace->entry_str; | |
e4d44e83 | 1841 | printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name); |
752fde44 | 1842 | |
e4d44e83 | 1843 | printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed, |
db2da3f8 | 1844 | args, augmented_args, augmented_args_size, trace, thread); |
752fde44 | 1845 | |
5089f20e | 1846 | if (sc->is_exit) { |
0a6545bd | 1847 | if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) { |
4b8a240e ACM |
1848 | int alignment = 0; |
1849 | ||
fd2b2975 | 1850 | trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output); |
4b8a240e ACM |
1851 | printed = fprintf(trace->output, "%s)", ttrace->entry_str); |
1852 | if (trace->args_alignment > printed) | |
1853 | alignment = trace->args_alignment - printed; | |
1854 | fprintf(trace->output, "%*s= ?\n", alignment, " "); | |
ae9ed035 | 1855 | } |
7f4f8001 | 1856 | } else { |
752fde44 | 1857 | ttrace->entry_pending = true; |
7f4f8001 ACM |
1858 | /* See trace__vfs_getname & trace__sys_exit */ |
1859 | ttrace->filename.pending_open = false; | |
1860 | } | |
ba3d7dee | 1861 | |
f3b623b8 ACM |
1862 | if (trace->current != thread) { |
1863 | thread__put(trace->current); | |
1864 | trace->current = thread__get(thread); | |
1865 | } | |
b91fc39f ACM |
1866 | err = 0; |
1867 | out_put: | |
1868 | thread__put(thread); | |
1869 | return err; | |
ba3d7dee ACM |
1870 | } |
1871 | ||
a98392bb ACM |
1872 | static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel, |
1873 | struct perf_sample *sample) | |
1874 | { | |
a98392bb ACM |
1875 | struct thread_trace *ttrace; |
1876 | struct thread *thread; | |
f3acd886 ACM |
1877 | int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1; |
1878 | struct syscall *sc = trace__syscall_info(trace, evsel, id); | |
a98392bb | 1879 | char msg[1024]; |
7a983a0f ACM |
1880 | void *args, *augmented_args = NULL; |
1881 | int augmented_args_size; | |
a98392bb | 1882 | |
a98392bb ACM |
1883 | if (sc == NULL) |
1884 | return -1; | |
1885 | ||
1886 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); | |
1887 | ttrace = thread__trace(thread, trace->output); | |
1888 | /* | |
1889 | * We need to get ttrace just to make sure it is there when syscall__scnprintf_args() | |
1890 | * and the rest of the beautifiers accessing it via struct syscall_arg touches it. | |
1891 | */ | |
1892 | if (ttrace == NULL) | |
1893 | goto out_put; | |
1894 | ||
f3acd886 | 1895 | args = perf_evsel__sc_tp_ptr(evsel, args, sample); |
cd26ea6d | 1896 | augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls); |
7a983a0f | 1897 | syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread); |
a98392bb ACM |
1898 | fprintf(trace->output, "%s", msg); |
1899 | err = 0; | |
1900 | out_put: | |
1901 | thread__put(thread); | |
1902 | return err; | |
1903 | } | |
1904 | ||
5cf9c84e ACM |
1905 | static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel, |
1906 | struct perf_sample *sample, | |
1907 | struct callchain_cursor *cursor) | |
202ff968 ACM |
1908 | { |
1909 | struct addr_location al; | |
3a9e9a47 RB |
1910 | int max_stack = evsel->attr.sample_max_stack ? |
1911 | evsel->attr.sample_max_stack : | |
1912 | trace->max_stack; | |
4291bf5c | 1913 | int err; |
5cf9c84e | 1914 | |
4291bf5c | 1915 | if (machine__resolve(trace->host, &al, sample) < 0) |
5cf9c84e ACM |
1916 | return -1; |
1917 | ||
4291bf5c ACM |
1918 | err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack); |
1919 | addr_location__put(&al); | |
1920 | return err; | |
5cf9c84e ACM |
1921 | } |
1922 | ||
1923 | static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample) | |
1924 | { | |
202ff968 | 1925 | /* TODO: user-configurable print_opts */ |
e20ab86e ACM |
1926 | const unsigned int print_opts = EVSEL__PRINT_SYM | |
1927 | EVSEL__PRINT_DSO | | |
1928 | EVSEL__PRINT_UNKNOWN_AS_ADDR; | |
202ff968 | 1929 | |
d327e60c | 1930 | return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output); |
202ff968 ACM |
1931 | } |
1932 | ||
092bd3cd HB |
1933 | static const char *errno_to_name(struct perf_evsel *evsel, int err) |
1934 | { | |
1935 | struct perf_env *env = perf_evsel__env(evsel); | |
1936 | const char *arch_name = perf_env__arch(env); | |
1937 | ||
1938 | return arch_syscalls__strerrno(arch_name, err); | |
1939 | } | |
1940 | ||
ba3d7dee | 1941 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 1942 | union perf_event *event __maybe_unused, |
ba3d7dee ACM |
1943 | struct perf_sample *sample) |
1944 | { | |
2c82c3ad | 1945 | long ret; |
60c907ab | 1946 | u64 duration = 0; |
fd2b2975 | 1947 | bool duration_calculated = false; |
2ae3a312 | 1948 | struct thread *thread; |
4b8a240e ACM |
1949 | int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0; |
1950 | int alignment = trace->args_alignment; | |
bf2575c1 | 1951 | struct syscall *sc = trace__syscall_info(trace, evsel, id); |
2ae3a312 ACM |
1952 | struct thread_trace *ttrace; |
1953 | ||
1954 | if (sc == NULL) | |
1955 | return -1; | |
ba3d7dee | 1956 | |
8fb598e5 | 1957 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
c24ff998 | 1958 | ttrace = thread__trace(thread, trace->output); |
2ae3a312 | 1959 | if (ttrace == NULL) |
b91fc39f | 1960 | goto out_put; |
ba3d7dee | 1961 | |
591421e1 ACM |
1962 | trace__fprintf_sample(trace, evsel, sample, thread); |
1963 | ||
bf2575c1 DA |
1964 | if (trace->summary) |
1965 | thread__update_stats(ttrace, id, sample); | |
1966 | ||
77170988 | 1967 | ret = perf_evsel__sc_tp_uint(evsel, ret, sample); |
ba3d7dee | 1968 | |
6a648b53 | 1969 | if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) { |
7f4f8001 ACM |
1970 | trace__set_fd_pathname(thread, ret, ttrace->filename.name); |
1971 | ttrace->filename.pending_open = false; | |
c522739d ACM |
1972 | ++trace->stats.vfs_getname; |
1973 | } | |
1974 | ||
ae9ed035 | 1975 | if (ttrace->entry_time) { |
60c907ab | 1976 | duration = sample->time - ttrace->entry_time; |
ae9ed035 ACM |
1977 | if (trace__filter_duration(trace, duration)) |
1978 | goto out; | |
fd2b2975 | 1979 | duration_calculated = true; |
ae9ed035 ACM |
1980 | } else if (trace->duration_filter) |
1981 | goto out; | |
60c907ab | 1982 | |
5cf9c84e ACM |
1983 | if (sample->callchain) { |
1984 | callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); | |
1985 | if (callchain_ret == 0) { | |
1986 | if (callchain_cursor.nr < trace->min_stack) | |
1987 | goto out; | |
1988 | callchain_ret = 1; | |
1989 | } | |
1990 | } | |
1991 | ||
0a6545bd | 1992 | if (trace->summary_only || (ret >= 0 && trace->failure_only)) |
fd2eabaf DA |
1993 | goto out; |
1994 | ||
fd2b2975 | 1995 | trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output); |
752fde44 ACM |
1996 | |
1997 | if (ttrace->entry_pending) { | |
4b8a240e | 1998 | printed = fprintf(trace->output, "%s", ttrace->entry_str); |
752fde44 | 1999 | } else { |
c24ff998 ACM |
2000 | fprintf(trace->output, " ... ["); |
2001 | color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued"); | |
2002 | fprintf(trace->output, "]: %s()", sc->name); | |
752fde44 ACM |
2003 | } |
2004 | ||
4b8a240e ACM |
2005 | printed++; /* the closing ')' */ |
2006 | ||
2007 | if (alignment > printed) | |
2008 | alignment -= printed; | |
2009 | else | |
2010 | alignment = 0; | |
2011 | ||
2012 | fprintf(trace->output, ")%*s= ", alignment, " "); | |
2013 | ||
da3c9a44 | 2014 | if (sc->fmt == NULL) { |
1f63139c ACM |
2015 | if (ret < 0) |
2016 | goto errno_print; | |
da3c9a44 | 2017 | signed_print: |
4b8a240e | 2018 | fprintf(trace->output, "%ld", ret); |
1f63139c ACM |
2019 | } else if (ret < 0) { |
2020 | errno_print: { | |
942a91ed | 2021 | char bf[STRERR_BUFSIZE]; |
c8b5f2c9 | 2022 | const char *emsg = str_error_r(-ret, bf, sizeof(bf)), |
092bd3cd | 2023 | *e = errno_to_name(evsel, -ret); |
ba3d7dee | 2024 | |
2c83dfae | 2025 | fprintf(trace->output, "-1 %s (%s)", e, emsg); |
1f63139c | 2026 | } |
da3c9a44 | 2027 | } else if (ret == 0 && sc->fmt->timeout) |
2c83dfae | 2028 | fprintf(trace->output, "0 (Timeout)"); |
84486caa ACM |
2029 | else if (ttrace->ret_scnprintf) { |
2030 | char bf[1024]; | |
7ee57434 ACM |
2031 | struct syscall_arg arg = { |
2032 | .val = ret, | |
2033 | .thread = thread, | |
2034 | .trace = trace, | |
2035 | }; | |
2036 | ttrace->ret_scnprintf(bf, sizeof(bf), &arg); | |
84486caa | 2037 | ttrace->ret_scnprintf = NULL; |
4b8a240e | 2038 | fprintf(trace->output, "%s", bf); |
84486caa | 2039 | } else if (sc->fmt->hexret) |
4b8a240e | 2040 | fprintf(trace->output, "%#lx", ret); |
11c8e39f ACM |
2041 | else if (sc->fmt->errpid) { |
2042 | struct thread *child = machine__find_thread(trace->host, ret, ret); | |
2043 | ||
2044 | if (child != NULL) { | |
4b8a240e | 2045 | fprintf(trace->output, "%ld", ret); |
11c8e39f ACM |
2046 | if (child->comm_set) |
2047 | fprintf(trace->output, " (%s)", thread__comm_str(child)); | |
2048 | thread__put(child); | |
2049 | } | |
2050 | } else | |
da3c9a44 | 2051 | goto signed_print; |
ba3d7dee | 2052 | |
c24ff998 | 2053 | fputc('\n', trace->output); |
566a0885 | 2054 | |
5067a8cd ACM |
2055 | /* |
2056 | * We only consider an 'event' for the sake of --max-events a non-filtered | |
2057 | * sys_enter + sys_exit and other tracepoint events. | |
2058 | */ | |
2059 | if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX) | |
2060 | interrupted = true; | |
2061 | ||
5cf9c84e ACM |
2062 | if (callchain_ret > 0) |
2063 | trace__fprintf_callchain(trace, sample); | |
2064 | else if (callchain_ret < 0) | |
2065 | pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); | |
ae9ed035 | 2066 | out: |
752fde44 | 2067 | ttrace->entry_pending = false; |
b91fc39f ACM |
2068 | err = 0; |
2069 | out_put: | |
2070 | thread__put(thread); | |
2071 | return err; | |
ba3d7dee ACM |
2072 | } |
2073 | ||
c522739d | 2074 | static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 2075 | union perf_event *event __maybe_unused, |
c522739d ACM |
2076 | struct perf_sample *sample) |
2077 | { | |
f994592d ACM |
2078 | struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
2079 | struct thread_trace *ttrace; | |
2080 | size_t filename_len, entry_str_len, to_move; | |
2081 | ssize_t remaining_space; | |
2082 | char *pos; | |
7f4f8001 | 2083 | const char *filename = perf_evsel__rawptr(evsel, sample, "pathname"); |
f994592d ACM |
2084 | |
2085 | if (!thread) | |
2086 | goto out; | |
2087 | ||
2088 | ttrace = thread__priv(thread); | |
2089 | if (!ttrace) | |
ef65e96e | 2090 | goto out_put; |
f994592d | 2091 | |
7f4f8001 | 2092 | filename_len = strlen(filename); |
39f0e7a8 | 2093 | if (filename_len == 0) |
ef65e96e | 2094 | goto out_put; |
7f4f8001 ACM |
2095 | |
2096 | if (ttrace->filename.namelen < filename_len) { | |
2097 | char *f = realloc(ttrace->filename.name, filename_len + 1); | |
2098 | ||
2099 | if (f == NULL) | |
ef65e96e | 2100 | goto out_put; |
7f4f8001 ACM |
2101 | |
2102 | ttrace->filename.namelen = filename_len; | |
2103 | ttrace->filename.name = f; | |
2104 | } | |
2105 | ||
2106 | strcpy(ttrace->filename.name, filename); | |
2107 | ttrace->filename.pending_open = true; | |
2108 | ||
f994592d | 2109 | if (!ttrace->filename.ptr) |
ef65e96e | 2110 | goto out_put; |
f994592d ACM |
2111 | |
2112 | entry_str_len = strlen(ttrace->entry_str); | |
2113 | remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */ | |
2114 | if (remaining_space <= 0) | |
ef65e96e | 2115 | goto out_put; |
f994592d | 2116 | |
f994592d ACM |
2117 | if (filename_len > (size_t)remaining_space) { |
2118 | filename += filename_len - remaining_space; | |
2119 | filename_len = remaining_space; | |
2120 | } | |
2121 | ||
2122 | to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */ | |
2123 | pos = ttrace->entry_str + ttrace->filename.entry_str_pos; | |
2124 | memmove(pos + filename_len, pos, to_move); | |
2125 | memcpy(pos, filename, filename_len); | |
2126 | ||
2127 | ttrace->filename.ptr = 0; | |
2128 | ttrace->filename.entry_str_pos = 0; | |
ef65e96e ACM |
2129 | out_put: |
2130 | thread__put(thread); | |
f994592d | 2131 | out: |
c522739d ACM |
2132 | return 0; |
2133 | } | |
2134 | ||
1302d88e | 2135 | static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel, |
0c82adcf | 2136 | union perf_event *event __maybe_unused, |
1302d88e ACM |
2137 | struct perf_sample *sample) |
2138 | { | |
2139 | u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); | |
2140 | double runtime_ms = (double)runtime / NSEC_PER_MSEC; | |
8fb598e5 | 2141 | struct thread *thread = machine__findnew_thread(trace->host, |
314add6b AH |
2142 | sample->pid, |
2143 | sample->tid); | |
c24ff998 | 2144 | struct thread_trace *ttrace = thread__trace(thread, trace->output); |
1302d88e ACM |
2145 | |
2146 | if (ttrace == NULL) | |
2147 | goto out_dump; | |
2148 | ||
2149 | ttrace->runtime_ms += runtime_ms; | |
2150 | trace->runtime_ms += runtime_ms; | |
ef65e96e | 2151 | out_put: |
b91fc39f | 2152 | thread__put(thread); |
1302d88e ACM |
2153 | return 0; |
2154 | ||
2155 | out_dump: | |
c24ff998 | 2156 | fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n", |
1302d88e ACM |
2157 | evsel->name, |
2158 | perf_evsel__strval(evsel, sample, "comm"), | |
2159 | (pid_t)perf_evsel__intval(evsel, sample, "pid"), | |
2160 | runtime, | |
2161 | perf_evsel__intval(evsel, sample, "vruntime")); | |
ef65e96e | 2162 | goto out_put; |
1302d88e ACM |
2163 | } |
2164 | ||
923d0c9a ACM |
2165 | static int bpf_output__printer(enum binary_printer_ops op, |
2166 | unsigned int val, void *extra __maybe_unused, FILE *fp) | |
1d6c9407 | 2167 | { |
1d6c9407 WN |
2168 | unsigned char ch = (unsigned char)val; |
2169 | ||
2170 | switch (op) { | |
2171 | case BINARY_PRINT_CHAR_DATA: | |
923d0c9a | 2172 | return fprintf(fp, "%c", isprint(ch) ? ch : '.'); |
1d6c9407 WN |
2173 | case BINARY_PRINT_DATA_BEGIN: |
2174 | case BINARY_PRINT_LINE_BEGIN: | |
2175 | case BINARY_PRINT_ADDR: | |
2176 | case BINARY_PRINT_NUM_DATA: | |
2177 | case BINARY_PRINT_NUM_PAD: | |
2178 | case BINARY_PRINT_SEP: | |
2179 | case BINARY_PRINT_CHAR_PAD: | |
2180 | case BINARY_PRINT_LINE_END: | |
2181 | case BINARY_PRINT_DATA_END: | |
2182 | default: | |
2183 | break; | |
2184 | } | |
923d0c9a ACM |
2185 | |
2186 | return 0; | |
1d6c9407 WN |
2187 | } |
2188 | ||
2189 | static void bpf_output__fprintf(struct trace *trace, | |
2190 | struct perf_sample *sample) | |
2191 | { | |
923d0c9a ACM |
2192 | binary__fprintf(sample->raw_data, sample->raw_size, 8, |
2193 | bpf_output__printer, NULL, trace->output); | |
5067a8cd | 2194 | ++trace->nr_events_printed; |
1d6c9407 WN |
2195 | } |
2196 | ||
14a052df ACM |
2197 | static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel, |
2198 | union perf_event *event __maybe_unused, | |
2199 | struct perf_sample *sample) | |
2200 | { | |
a9c5e6c1 | 2201 | struct thread *thread; |
7ad35615 | 2202 | int callchain_ret = 0; |
a9c5e6c1 ACM |
2203 | /* |
2204 | * Check if we called perf_evsel__disable(evsel) due to, for instance, | |
2205 | * this event's max_events having been hit and this is an entry coming | |
2206 | * from the ring buffer that we should discard, since the max events | |
2207 | * have already been considered/printed. | |
2208 | */ | |
2209 | if (evsel->disabled) | |
2210 | return 0; | |
2211 | ||
2212 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); | |
7ad35615 ACM |
2213 | |
2214 | if (sample->callchain) { | |
2215 | callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); | |
2216 | if (callchain_ret == 0) { | |
2217 | if (callchain_cursor.nr < trace->min_stack) | |
2218 | goto out; | |
2219 | callchain_ret = 1; | |
2220 | } | |
2221 | } | |
2222 | ||
522283fe | 2223 | trace__printf_interrupted_entry(trace); |
14a052df | 2224 | trace__fprintf_tstamp(trace, sample->time, trace->output); |
0808921a | 2225 | |
42e4a52d | 2226 | if (trace->trace_syscalls && trace->show_duration) |
0808921a ACM |
2227 | fprintf(trace->output, "( ): "); |
2228 | ||
c4191e55 ACM |
2229 | if (thread) |
2230 | trace__fprintf_comm_tid(trace, thread, trace->output); | |
2231 | ||
1cdf618f ACM |
2232 | if (evsel == trace->syscalls.events.augmented) { |
2233 | int id = perf_evsel__sc_tp_uint(evsel, id, sample); | |
2234 | struct syscall *sc = trace__syscall_info(trace, evsel, id); | |
2235 | ||
2236 | if (sc) { | |
c4191e55 ACM |
2237 | fprintf(trace->output, "%s(", sc->name); |
2238 | trace__fprintf_sys_enter(trace, evsel, sample); | |
2239 | fputc(')', trace->output); | |
2240 | goto newline; | |
1cdf618f ACM |
2241 | } |
2242 | ||
2243 | /* | |
2244 | * XXX: Not having the associated syscall info or not finding/adding | |
2245 | * the thread should never happen, but if it does... | |
2246 | * fall thru and print it as a bpf_output event. | |
2247 | */ | |
2248 | } | |
2249 | ||
0808921a | 2250 | fprintf(trace->output, "%s:", evsel->name); |
14a052df | 2251 | |
1d6c9407 | 2252 | if (perf_evsel__is_bpf_output(evsel)) { |
1cdf618f | 2253 | bpf_output__fprintf(trace, sample); |
1d6c9407 | 2254 | } else if (evsel->tp_format) { |
a98392bb ACM |
2255 | if (strncmp(evsel->tp_format->name, "sys_enter_", 10) || |
2256 | trace__fprintf_sys_enter(trace, evsel, sample)) { | |
2257 | event_format__fprintf(evsel->tp_format, sample->cpu, | |
2258 | sample->raw_data, sample->raw_size, | |
2259 | trace->output); | |
5067a8cd | 2260 | ++trace->nr_events_printed; |
a9c5e6c1 ACM |
2261 | |
2262 | if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) { | |
2263 | perf_evsel__disable(evsel); | |
2264 | perf_evsel__close(evsel); | |
2265 | } | |
a98392bb | 2266 | } |
14a052df ACM |
2267 | } |
2268 | ||
1cdf618f | 2269 | newline: |
51125a29 | 2270 | fprintf(trace->output, "\n"); |
202ff968 | 2271 | |
7ad35615 ACM |
2272 | if (callchain_ret > 0) |
2273 | trace__fprintf_callchain(trace, sample); | |
2274 | else if (callchain_ret < 0) | |
2275 | pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); | |
2276 | out: | |
a937c665 | 2277 | thread__put(thread); |
14a052df ACM |
2278 | return 0; |
2279 | } | |
2280 | ||
598d02c5 SF |
2281 | static void print_location(FILE *f, struct perf_sample *sample, |
2282 | struct addr_location *al, | |
2283 | bool print_dso, bool print_sym) | |
2284 | { | |
2285 | ||
bb963e16 | 2286 | if ((verbose > 0 || print_dso) && al->map) |
598d02c5 SF |
2287 | fprintf(f, "%s@", al->map->dso->long_name); |
2288 | ||
bb963e16 | 2289 | if ((verbose > 0 || print_sym) && al->sym) |
4414a3c5 | 2290 | fprintf(f, "%s+0x%" PRIx64, al->sym->name, |
598d02c5 SF |
2291 | al->addr - al->sym->start); |
2292 | else if (al->map) | |
4414a3c5 | 2293 | fprintf(f, "0x%" PRIx64, al->addr); |
598d02c5 | 2294 | else |
4414a3c5 | 2295 | fprintf(f, "0x%" PRIx64, sample->addr); |
598d02c5 SF |
2296 | } |
2297 | ||
2298 | static int trace__pgfault(struct trace *trace, | |
2299 | struct perf_evsel *evsel, | |
473398a2 | 2300 | union perf_event *event __maybe_unused, |
598d02c5 SF |
2301 | struct perf_sample *sample) |
2302 | { | |
2303 | struct thread *thread; | |
598d02c5 SF |
2304 | struct addr_location al; |
2305 | char map_type = 'd'; | |
a2ea67d7 | 2306 | struct thread_trace *ttrace; |
b91fc39f | 2307 | int err = -1; |
1df54290 | 2308 | int callchain_ret = 0; |
598d02c5 SF |
2309 | |
2310 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); | |
1df54290 ACM |
2311 | |
2312 | if (sample->callchain) { | |
2313 | callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); | |
2314 | if (callchain_ret == 0) { | |
2315 | if (callchain_cursor.nr < trace->min_stack) | |
2316 | goto out_put; | |
2317 | callchain_ret = 1; | |
2318 | } | |
2319 | } | |
2320 | ||
a2ea67d7 SF |
2321 | ttrace = thread__trace(thread, trace->output); |
2322 | if (ttrace == NULL) | |
b91fc39f | 2323 | goto out_put; |
a2ea67d7 SF |
2324 | |
2325 | if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ) | |
2326 | ttrace->pfmaj++; | |
2327 | else | |
2328 | ttrace->pfmin++; | |
2329 | ||
2330 | if (trace->summary_only) | |
b91fc39f | 2331 | goto out; |
598d02c5 | 2332 | |
4546263d | 2333 | thread__find_symbol(thread, sample->cpumode, sample->ip, &al); |
598d02c5 | 2334 | |
fd2b2975 | 2335 | trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output); |
598d02c5 SF |
2336 | |
2337 | fprintf(trace->output, "%sfault [", | |
2338 | evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ? | |
2339 | "maj" : "min"); | |
2340 | ||
2341 | print_location(trace->output, sample, &al, false, true); | |
2342 | ||
2343 | fprintf(trace->output, "] => "); | |
2344 | ||
117d3c24 | 2345 | thread__find_symbol(thread, sample->cpumode, sample->addr, &al); |
598d02c5 SF |
2346 | |
2347 | if (!al.map) { | |
4546263d | 2348 | thread__find_symbol(thread, sample->cpumode, sample->addr, &al); |
598d02c5 SF |
2349 | |
2350 | if (al.map) | |
2351 | map_type = 'x'; | |
2352 | else | |
2353 | map_type = '?'; | |
2354 | } | |
2355 | ||
2356 | print_location(trace->output, sample, &al, true, false); | |
2357 | ||
2358 | fprintf(trace->output, " (%c%c)\n", map_type, al.level); | |
0c3a6ef4 | 2359 | |
1df54290 ACM |
2360 | if (callchain_ret > 0) |
2361 | trace__fprintf_callchain(trace, sample); | |
2362 | else if (callchain_ret < 0) | |
2363 | pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); | |
5067a8cd ACM |
2364 | |
2365 | ++trace->nr_events_printed; | |
b91fc39f ACM |
2366 | out: |
2367 | err = 0; | |
2368 | out_put: | |
2369 | thread__put(thread); | |
2370 | return err; | |
598d02c5 SF |
2371 | } |
2372 | ||
e6001980 | 2373 | static void trace__set_base_time(struct trace *trace, |
8a07a809 | 2374 | struct perf_evsel *evsel, |
e6001980 ACM |
2375 | struct perf_sample *sample) |
2376 | { | |
8a07a809 ACM |
2377 | /* |
2378 | * BPF events were not setting PERF_SAMPLE_TIME, so be more robust | |
2379 | * and don't use sample->time unconditionally, we may end up having | |
2380 | * some other event in the future without PERF_SAMPLE_TIME for good | |
2381 | * reason, i.e. we may not be interested in its timestamps, just in | |
2382 | * it taking place, picking some piece of information when it | |
2383 | * appears in our event stream (vfs_getname comes to mind). | |
2384 | */ | |
2385 | if (trace->base_time == 0 && !trace->full_time && | |
2386 | (evsel->attr.sample_type & PERF_SAMPLE_TIME)) | |
e6001980 ACM |
2387 | trace->base_time = sample->time; |
2388 | } | |
2389 | ||
6810fc91 | 2390 | static int trace__process_sample(struct perf_tool *tool, |
0c82adcf | 2391 | union perf_event *event, |
6810fc91 DA |
2392 | struct perf_sample *sample, |
2393 | struct perf_evsel *evsel, | |
2394 | struct machine *machine __maybe_unused) | |
2395 | { | |
2396 | struct trace *trace = container_of(tool, struct trace, tool); | |
aa07df6e | 2397 | struct thread *thread; |
6810fc91 DA |
2398 | int err = 0; |
2399 | ||
744a9719 | 2400 | tracepoint_handler handler = evsel->handler; |
6810fc91 | 2401 | |
aa07df6e DA |
2402 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
2403 | if (thread && thread__is_filtered(thread)) | |
ef65e96e | 2404 | goto out; |
bdc89661 | 2405 | |
e6001980 | 2406 | trace__set_base_time(trace, evsel, sample); |
6810fc91 | 2407 | |
3160565f DA |
2408 | if (handler) { |
2409 | ++trace->nr_events; | |
0c82adcf | 2410 | handler(trace, evsel, event, sample); |
3160565f | 2411 | } |
ef65e96e ACM |
2412 | out: |
2413 | thread__put(thread); | |
6810fc91 DA |
2414 | return err; |
2415 | } | |
2416 | ||
1e28fe0a | 2417 | static int trace__record(struct trace *trace, int argc, const char **argv) |
5e2485b1 DA |
2418 | { |
2419 | unsigned int rec_argc, i, j; | |
2420 | const char **rec_argv; | |
2421 | const char * const record_args[] = { | |
2422 | "record", | |
2423 | "-R", | |
2424 | "-m", "1024", | |
2425 | "-c", "1", | |
5e2485b1 DA |
2426 | }; |
2427 | ||
1e28fe0a SF |
2428 | const char * const sc_args[] = { "-e", }; |
2429 | unsigned int sc_args_nr = ARRAY_SIZE(sc_args); | |
2430 | const char * const majpf_args[] = { "-e", "major-faults" }; | |
2431 | unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args); | |
2432 | const char * const minpf_args[] = { "-e", "minor-faults" }; | |
2433 | unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args); | |
2434 | ||
9aca7f17 | 2435 | /* +1 is for the event string below */ |
1e28fe0a SF |
2436 | rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 + |
2437 | majpf_args_nr + minpf_args_nr + argc; | |
5e2485b1 DA |
2438 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
2439 | ||
2440 | if (rec_argv == NULL) | |
2441 | return -ENOMEM; | |
2442 | ||
1e28fe0a | 2443 | j = 0; |
5e2485b1 | 2444 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
1e28fe0a SF |
2445 | rec_argv[j++] = record_args[i]; |
2446 | ||
e281a960 SF |
2447 | if (trace->trace_syscalls) { |
2448 | for (i = 0; i < sc_args_nr; i++) | |
2449 | rec_argv[j++] = sc_args[i]; | |
2450 | ||
2451 | /* event string may be different for older kernels - e.g., RHEL6 */ | |
2452 | if (is_valid_tracepoint("raw_syscalls:sys_enter")) | |
2453 | rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit"; | |
2454 | else if (is_valid_tracepoint("syscalls:sys_enter")) | |
2455 | rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit"; | |
2456 | else { | |
2457 | pr_err("Neither raw_syscalls nor syscalls events exist.\n"); | |
c896f85a | 2458 | free(rec_argv); |
e281a960 SF |
2459 | return -1; |
2460 | } | |
9aca7f17 | 2461 | } |
9aca7f17 | 2462 | |
1e28fe0a SF |
2463 | if (trace->trace_pgfaults & TRACE_PFMAJ) |
2464 | for (i = 0; i < majpf_args_nr; i++) | |
2465 | rec_argv[j++] = majpf_args[i]; | |
2466 | ||
2467 | if (trace->trace_pgfaults & TRACE_PFMIN) | |
2468 | for (i = 0; i < minpf_args_nr; i++) | |
2469 | rec_argv[j++] = minpf_args[i]; | |
2470 | ||
2471 | for (i = 0; i < (unsigned int)argc; i++) | |
2472 | rec_argv[j++] = argv[i]; | |
5e2485b1 | 2473 | |
b0ad8ea6 | 2474 | return cmd_record(j, rec_argv); |
5e2485b1 DA |
2475 | } |
2476 | ||
bf2575c1 DA |
2477 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp); |
2478 | ||
08c98776 | 2479 | static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist) |
c522739d | 2480 | { |
ef503831 | 2481 | struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname"); |
8dd2a131 JO |
2482 | |
2483 | if (IS_ERR(evsel)) | |
08c98776 | 2484 | return false; |
c522739d ACM |
2485 | |
2486 | if (perf_evsel__field(evsel, "pathname") == NULL) { | |
2487 | perf_evsel__delete(evsel); | |
08c98776 | 2488 | return false; |
c522739d ACM |
2489 | } |
2490 | ||
744a9719 | 2491 | evsel->handler = trace__vfs_getname; |
c522739d | 2492 | perf_evlist__add(evlist, evsel); |
08c98776 | 2493 | return true; |
c522739d ACM |
2494 | } |
2495 | ||
0ae537cb | 2496 | static struct perf_evsel *perf_evsel__new_pgfault(u64 config) |
598d02c5 SF |
2497 | { |
2498 | struct perf_evsel *evsel; | |
2499 | struct perf_event_attr attr = { | |
2500 | .type = PERF_TYPE_SOFTWARE, | |
2501 | .mmap_data = 1, | |
598d02c5 SF |
2502 | }; |
2503 | ||
2504 | attr.config = config; | |
0524798c | 2505 | attr.sample_period = 1; |
598d02c5 SF |
2506 | |
2507 | event_attr_init(&attr); | |
2508 | ||
2509 | evsel = perf_evsel__new(&attr); | |
0ae537cb ACM |
2510 | if (evsel) |
2511 | evsel->handler = trace__pgfault; | |
598d02c5 | 2512 | |
0ae537cb | 2513 | return evsel; |
598d02c5 SF |
2514 | } |
2515 | ||
ddbb1b13 ACM |
2516 | static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample) |
2517 | { | |
2518 | const u32 type = event->header.type; | |
2519 | struct perf_evsel *evsel; | |
2520 | ||
ddbb1b13 ACM |
2521 | if (type != PERF_RECORD_SAMPLE) { |
2522 | trace__process_event(trace, trace->host, event, sample); | |
2523 | return; | |
2524 | } | |
2525 | ||
2526 | evsel = perf_evlist__id2evsel(trace->evlist, sample->id); | |
2527 | if (evsel == NULL) { | |
2528 | fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id); | |
2529 | return; | |
2530 | } | |
2531 | ||
e6001980 ACM |
2532 | trace__set_base_time(trace, evsel, sample); |
2533 | ||
ddbb1b13 ACM |
2534 | if (evsel->attr.type == PERF_TYPE_TRACEPOINT && |
2535 | sample->raw_data == NULL) { | |
2536 | fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", | |
2537 | perf_evsel__name(evsel), sample->tid, | |
2538 | sample->cpu, sample->raw_size); | |
2539 | } else { | |
2540 | tracepoint_handler handler = evsel->handler; | |
2541 | handler(trace, evsel, event, sample); | |
2542 | } | |
5067a8cd ACM |
2543 | |
2544 | if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX) | |
2545 | interrupted = true; | |
ddbb1b13 ACM |
2546 | } |
2547 | ||
c27366f0 ACM |
2548 | static int trace__add_syscall_newtp(struct trace *trace) |
2549 | { | |
2550 | int ret = -1; | |
2551 | struct perf_evlist *evlist = trace->evlist; | |
2552 | struct perf_evsel *sys_enter, *sys_exit; | |
2553 | ||
63f11c80 | 2554 | sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter); |
c27366f0 ACM |
2555 | if (sys_enter == NULL) |
2556 | goto out; | |
2557 | ||
2558 | if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args)) | |
2559 | goto out_delete_sys_enter; | |
2560 | ||
63f11c80 | 2561 | sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit); |
c27366f0 ACM |
2562 | if (sys_exit == NULL) |
2563 | goto out_delete_sys_enter; | |
2564 | ||
2565 | if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret)) | |
2566 | goto out_delete_sys_exit; | |
2567 | ||
08e26396 ACM |
2568 | perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param); |
2569 | perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param); | |
2570 | ||
c27366f0 ACM |
2571 | perf_evlist__add(evlist, sys_enter); |
2572 | perf_evlist__add(evlist, sys_exit); | |
2573 | ||
2ddd5c04 | 2574 | if (callchain_param.enabled && !trace->kernel_syscallchains) { |
44621819 ACM |
2575 | /* |
2576 | * We're interested only in the user space callchain | |
2577 | * leading to the syscall, allow overriding that for | |
2578 | * debugging reasons using --kernel_syscall_callchains | |
2579 | */ | |
2580 | sys_exit->attr.exclude_callchain_kernel = 1; | |
2581 | } | |
2582 | ||
8b3ce757 ACM |
2583 | trace->syscalls.events.sys_enter = sys_enter; |
2584 | trace->syscalls.events.sys_exit = sys_exit; | |
c27366f0 ACM |
2585 | |
2586 | ret = 0; | |
2587 | out: | |
2588 | return ret; | |
2589 | ||
2590 | out_delete_sys_exit: | |
2591 | perf_evsel__delete_priv(sys_exit); | |
2592 | out_delete_sys_enter: | |
2593 | perf_evsel__delete_priv(sys_enter); | |
2594 | goto out; | |
2595 | } | |
2596 | ||
246fbe03 | 2597 | static int trace__set_ev_qualifier_tp_filter(struct trace *trace) |
19867b61 ACM |
2598 | { |
2599 | int err = -1; | |
b15d0a4c | 2600 | struct perf_evsel *sys_exit; |
19867b61 ACM |
2601 | char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier, |
2602 | trace->ev_qualifier_ids.nr, | |
2603 | trace->ev_qualifier_ids.entries); | |
2604 | ||
2605 | if (filter == NULL) | |
2606 | goto out_enomem; | |
2607 | ||
3541c034 MP |
2608 | if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter, |
2609 | filter)) { | |
b15d0a4c | 2610 | sys_exit = trace->syscalls.events.sys_exit; |
3541c034 | 2611 | err = perf_evsel__append_tp_filter(sys_exit, filter); |
b15d0a4c | 2612 | } |
19867b61 ACM |
2613 | |
2614 | free(filter); | |
2615 | out: | |
2616 | return err; | |
2617 | out_enomem: | |
2618 | errno = ENOMEM; | |
2619 | goto out; | |
2620 | } | |
c27366f0 | 2621 | |
b27b38ed ACM |
2622 | #ifdef HAVE_LIBBPF_SUPPORT |
2623 | static int trace__set_ev_qualifier_bpf_filter(struct trace *trace) | |
2624 | { | |
2625 | int fd = bpf_map__fd(trace->syscalls.map); | |
bbab50dd ACM |
2626 | struct bpf_map_syscall_entry value = { |
2627 | .enabled = !trace->not_ev_qualifier, | |
2628 | }; | |
b27b38ed ACM |
2629 | int err = 0; |
2630 | size_t i; | |
2631 | ||
2632 | for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) { | |
2633 | int key = trace->ev_qualifier_ids.entries[i]; | |
2634 | ||
2635 | err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST); | |
2636 | if (err) | |
2637 | break; | |
2638 | } | |
2639 | ||
2640 | return err; | |
2641 | } | |
2642 | ||
2643 | static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled) | |
2644 | { | |
2645 | int fd = bpf_map__fd(trace->syscalls.map); | |
bbab50dd ACM |
2646 | struct bpf_map_syscall_entry value = { |
2647 | .enabled = enabled, | |
2648 | }; | |
b27b38ed ACM |
2649 | int err = 0, key; |
2650 | ||
2651 | for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) { | |
bbab50dd | 2652 | err = bpf_map_update_elem(fd, &key, &value, BPF_ANY); |
b27b38ed ACM |
2653 | if (err) |
2654 | break; | |
2655 | } | |
2656 | ||
2657 | return err; | |
2658 | } | |
2659 | ||
2660 | static int trace__init_syscalls_bpf_map(struct trace *trace) | |
2661 | { | |
2662 | bool enabled = true; | |
2663 | ||
2664 | if (trace->ev_qualifier_ids.nr) | |
2665 | enabled = trace->not_ev_qualifier; | |
2666 | ||
2667 | return __trace__init_syscalls_bpf_map(trace, enabled); | |
2668 | } | |
2669 | #else | |
2670 | static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused) | |
2671 | { | |
2672 | return 0; | |
2673 | } | |
2674 | ||
2675 | static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused) | |
2676 | { | |
2677 | return 0; | |
2678 | } | |
2679 | #endif // HAVE_LIBBPF_SUPPORT | |
2680 | ||
246fbe03 ACM |
2681 | static int trace__set_ev_qualifier_filter(struct trace *trace) |
2682 | { | |
b27b38ed ACM |
2683 | if (trace->syscalls.map) |
2684 | return trace__set_ev_qualifier_bpf_filter(trace); | |
246fbe03 ACM |
2685 | return trace__set_ev_qualifier_tp_filter(trace); |
2686 | } | |
2687 | ||
a9964c43 ACM |
2688 | static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused, |
2689 | size_t npids __maybe_unused, pid_t *pids __maybe_unused) | |
2690 | { | |
2691 | int err = 0; | |
2692 | #ifdef HAVE_LIBBPF_SUPPORT | |
2693 | bool value = true; | |
2694 | int map_fd = bpf_map__fd(map); | |
2695 | size_t i; | |
2696 | ||
2697 | for (i = 0; i < npids; ++i) { | |
2698 | err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY); | |
2699 | if (err) | |
2700 | break; | |
2701 | } | |
2702 | #endif | |
2703 | return err; | |
2704 | } | |
2705 | ||
dd1a5037 ACM |
2706 | static int trace__set_filter_loop_pids(struct trace *trace) |
2707 | { | |
a9964c43 | 2708 | unsigned int nr = 1, err; |
dd1a5037 ACM |
2709 | pid_t pids[32] = { |
2710 | getpid(), | |
2711 | }; | |
082ab9a1 ACM |
2712 | struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]); |
2713 | ||
2714 | while (thread && nr < ARRAY_SIZE(pids)) { | |
2715 | struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid); | |
2716 | ||
2717 | if (parent == NULL) | |
2718 | break; | |
2719 | ||
2720 | if (!strcmp(thread__comm_str(parent), "sshd")) { | |
2721 | pids[nr++] = parent->tid; | |
2722 | break; | |
2723 | } | |
2724 | thread = parent; | |
2725 | } | |
dd1a5037 | 2726 | |
a9964c43 ACM |
2727 | err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids); |
2728 | if (!err && trace->filter_pids.map) | |
2729 | err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids); | |
2730 | ||
2731 | return err; | |
2732 | } | |
2733 | ||
2734 | static int trace__set_filter_pids(struct trace *trace) | |
2735 | { | |
2736 | int err = 0; | |
2737 | /* | |
2738 | * Better not use !target__has_task() here because we need to cover the | |
2739 | * case where no threads were specified in the command line, but a | |
2740 | * workload was, and in that case we will fill in the thread_map when | |
2741 | * we fork the workload in perf_evlist__prepare_workload. | |
2742 | */ | |
2743 | if (trace->filter_pids.nr > 0) { | |
2744 | err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr, | |
2745 | trace->filter_pids.entries); | |
2746 | if (!err && trace->filter_pids.map) { | |
2747 | err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr, | |
2748 | trace->filter_pids.entries); | |
2749 | } | |
2750 | } else if (thread_map__pid(trace->evlist->threads, 0) == -1) { | |
2751 | err = trace__set_filter_loop_pids(trace); | |
2752 | } | |
2753 | ||
2754 | return err; | |
dd1a5037 ACM |
2755 | } |
2756 | ||
7ba61524 | 2757 | static int __trace__deliver_event(struct trace *trace, union perf_event *event) |
1f44b3e2 JO |
2758 | { |
2759 | struct perf_evlist *evlist = trace->evlist; | |
2760 | struct perf_sample sample; | |
2761 | int err; | |
2762 | ||
2763 | err = perf_evlist__parse_sample(evlist, event, &sample); | |
2764 | if (err) | |
2765 | fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err); | |
2766 | else | |
2767 | trace__handle_event(trace, event, &sample); | |
2768 | ||
2769 | return 0; | |
2770 | } | |
2771 | ||
a761a8d1 | 2772 | static int __trace__flush_events(struct trace *trace) |
028713aa JO |
2773 | { |
2774 | u64 first = ordered_events__first_time(&trace->oe.data); | |
2775 | u64 flush = trace->oe.last - NSEC_PER_SEC; | |
2776 | ||
2777 | /* Is there some thing to flush.. */ | |
2778 | if (first && first < flush) | |
2779 | return ordered_events__flush_time(&trace->oe.data, flush); | |
2780 | ||
2781 | return 0; | |
2782 | } | |
2783 | ||
a761a8d1 ACM |
2784 | static int trace__flush_events(struct trace *trace) |
2785 | { | |
2786 | return !trace->sort_events ? 0 : __trace__flush_events(trace); | |
2787 | } | |
2788 | ||
7ba61524 | 2789 | static int trace__deliver_event(struct trace *trace, union perf_event *event) |
028713aa | 2790 | { |
028713aa JO |
2791 | int err; |
2792 | ||
a761a8d1 ACM |
2793 | if (!trace->sort_events) |
2794 | return __trace__deliver_event(trace, event); | |
2795 | ||
2796 | err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last); | |
028713aa JO |
2797 | if (err && err != -1) |
2798 | return err; | |
2799 | ||
2800 | err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0); | |
2801 | if (err) | |
2802 | return err; | |
2803 | ||
7ba61524 | 2804 | return trace__flush_events(trace); |
028713aa JO |
2805 | } |
2806 | ||
2807 | static int ordered_events__deliver_event(struct ordered_events *oe, | |
2808 | struct ordered_event *event) | |
2809 | { | |
2810 | struct trace *trace = container_of(oe, struct trace, oe.data); | |
2811 | ||
7ba61524 | 2812 | return __trace__deliver_event(trace, event->event); |
028713aa JO |
2813 | } |
2814 | ||
f15eb531 | 2815 | static int trace__run(struct trace *trace, int argc, const char **argv) |
514f1c67 | 2816 | { |
14a052df | 2817 | struct perf_evlist *evlist = trace->evlist; |
0ae537cb | 2818 | struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL; |
efd5745e ACM |
2819 | int err = -1, i; |
2820 | unsigned long before; | |
f15eb531 | 2821 | const bool forks = argc > 0; |
46fb3c21 | 2822 | bool draining = false; |
514f1c67 | 2823 | |
75b757ca ACM |
2824 | trace->live = true; |
2825 | ||
0df50e0b ACM |
2826 | if (!trace->raw_augmented_syscalls) { |
2827 | if (trace->trace_syscalls && trace__add_syscall_newtp(trace)) | |
2828 | goto out_error_raw_syscalls; | |
514f1c67 | 2829 | |
0df50e0b ACM |
2830 | if (trace->trace_syscalls) |
2831 | trace->vfs_getname = perf_evlist__add_vfs_getname(evlist); | |
2832 | } | |
c522739d | 2833 | |
0ae537cb ACM |
2834 | if ((trace->trace_pgfaults & TRACE_PFMAJ)) { |
2835 | pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ); | |
2836 | if (pgfault_maj == NULL) | |
2837 | goto out_error_mem; | |
08e26396 | 2838 | perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param); |
0ae537cb | 2839 | perf_evlist__add(evlist, pgfault_maj); |
e2726d99 | 2840 | } |
598d02c5 | 2841 | |
0ae537cb ACM |
2842 | if ((trace->trace_pgfaults & TRACE_PFMIN)) { |
2843 | pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN); | |
2844 | if (pgfault_min == NULL) | |
2845 | goto out_error_mem; | |
08e26396 | 2846 | perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param); |
0ae537cb ACM |
2847 | perf_evlist__add(evlist, pgfault_min); |
2848 | } | |
598d02c5 | 2849 | |
1302d88e | 2850 | if (trace->sched && |
2cc990ba ACM |
2851 | perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime", |
2852 | trace__sched_stat_runtime)) | |
2853 | goto out_error_sched_stat_runtime; | |
1302d88e | 2854 | |
9ea42ba4 ACM |
2855 | /* |
2856 | * If a global cgroup was set, apply it to all the events without an | |
2857 | * explicit cgroup. I.e.: | |
2858 | * | |
2859 | * trace -G A -e sched:*switch | |
2860 | * | |
2861 | * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc | |
2862 | * _and_ sched:sched_switch to the 'A' cgroup, while: | |
2863 | * | |
2864 | * trace -e sched:*switch -G A | |
2865 | * | |
2866 | * will only set the sched:sched_switch event to the 'A' cgroup, all the | |
2867 | * other events (raw_syscalls:sys_{enter,exit}, etc are left "without" | |
2868 | * a cgroup (on the root cgroup, sys wide, etc). | |
2869 | * | |
2870 | * Multiple cgroups: | |
2871 | * | |
2872 | * trace -G A -e sched:*switch -G B | |
2873 | * | |
2874 | * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes | |
2875 | * to the 'B' cgroup. | |
2876 | * | |
2877 | * evlist__set_default_cgroup() grabs a reference of the passed cgroup | |
2878 | * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL. | |
2879 | */ | |
2880 | if (trace->cgroup) | |
2881 | evlist__set_default_cgroup(trace->evlist, trace->cgroup); | |
2882 | ||
514f1c67 ACM |
2883 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
2884 | if (err < 0) { | |
c24ff998 | 2885 | fprintf(trace->output, "Problems parsing the target to trace, check your options!\n"); |
514f1c67 ACM |
2886 | goto out_delete_evlist; |
2887 | } | |
2888 | ||
752fde44 ACM |
2889 | err = trace__symbols_init(trace, evlist); |
2890 | if (err < 0) { | |
c24ff998 | 2891 | fprintf(trace->output, "Problems initializing symbol libraries!\n"); |
03ad9747 | 2892 | goto out_delete_evlist; |
752fde44 ACM |
2893 | } |
2894 | ||
75d50117 | 2895 | perf_evlist__config(evlist, &trace->opts, &callchain_param); |
fde54b78 | 2896 | |
f15eb531 NK |
2897 | signal(SIGCHLD, sig_handler); |
2898 | signal(SIGINT, sig_handler); | |
2899 | ||
2900 | if (forks) { | |
6ef73ec4 | 2901 | err = perf_evlist__prepare_workload(evlist, &trace->opts.target, |
735f7e0b | 2902 | argv, false, NULL); |
f15eb531 | 2903 | if (err < 0) { |
c24ff998 | 2904 | fprintf(trace->output, "Couldn't run the workload!\n"); |
03ad9747 | 2905 | goto out_delete_evlist; |
f15eb531 NK |
2906 | } |
2907 | } | |
2908 | ||
514f1c67 | 2909 | err = perf_evlist__open(evlist); |
a8f23d8f ACM |
2910 | if (err < 0) |
2911 | goto out_error_open; | |
514f1c67 | 2912 | |
ba504235 WN |
2913 | err = bpf__apply_obj_config(); |
2914 | if (err) { | |
2915 | char errbuf[BUFSIZ]; | |
2916 | ||
2917 | bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); | |
2918 | pr_err("ERROR: Apply config to BPF failed: %s\n", | |
2919 | errbuf); | |
2920 | goto out_error_open; | |
2921 | } | |
2922 | ||
a9964c43 | 2923 | err = trace__set_filter_pids(trace); |
94ad89bc ACM |
2924 | if (err < 0) |
2925 | goto out_error_mem; | |
2926 | ||
b27b38ed ACM |
2927 | if (trace->syscalls.map) |
2928 | trace__init_syscalls_bpf_map(trace); | |
2929 | ||
19867b61 ACM |
2930 | if (trace->ev_qualifier_ids.nr > 0) { |
2931 | err = trace__set_ev_qualifier_filter(trace); | |
2932 | if (err < 0) | |
2933 | goto out_errno; | |
19867b61 | 2934 | |
0df50e0b ACM |
2935 | if (trace->syscalls.events.sys_exit) { |
2936 | pr_debug("event qualifier tracepoint filter: %s\n", | |
2937 | trace->syscalls.events.sys_exit->filter); | |
2938 | } | |
2e5e5f87 | 2939 | } |
19867b61 | 2940 | |
94ad89bc ACM |
2941 | err = perf_evlist__apply_filters(evlist, &evsel); |
2942 | if (err < 0) | |
2943 | goto out_error_apply_filters; | |
241b057c | 2944 | |
f74b9d3a | 2945 | err = perf_evlist__mmap(evlist, trace->opts.mmap_pages); |
e09b18d4 ACM |
2946 | if (err < 0) |
2947 | goto out_error_mmap; | |
514f1c67 | 2948 | |
e36b7821 | 2949 | if (!target__none(&trace->opts.target) && !trace->opts.initial_delay) |
cb24d01d ACM |
2950 | perf_evlist__enable(evlist); |
2951 | ||
f15eb531 NK |
2952 | if (forks) |
2953 | perf_evlist__start_workload(evlist); | |
2954 | ||
e36b7821 AB |
2955 | if (trace->opts.initial_delay) { |
2956 | usleep(trace->opts.initial_delay * 1000); | |
2957 | perf_evlist__enable(evlist); | |
2958 | } | |
2959 | ||
e13798c7 | 2960 | trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 || |
42052bea ACM |
2961 | evlist->threads->nr > 1 || |
2962 | perf_evlist__first(evlist)->attr.inherit; | |
bd3dda9a ACM |
2963 | |
2964 | /* | |
2965 | * Now that we already used evsel->attr to ask the kernel to setup the | |
2966 | * events, lets reuse evsel->attr.sample_max_stack as the limit in | |
2967 | * trace__resolve_callchain(), allowing per-event max-stack settings | |
adba1634 | 2968 | * to override an explicitly set --max-stack global setting. |
bd3dda9a ACM |
2969 | */ |
2970 | evlist__for_each_entry(evlist, evsel) { | |
27de9b2b | 2971 | if (evsel__has_callchain(evsel) && |
bd3dda9a ACM |
2972 | evsel->attr.sample_max_stack == 0) |
2973 | evsel->attr.sample_max_stack = trace->max_stack; | |
2974 | } | |
514f1c67 | 2975 | again: |
efd5745e | 2976 | before = trace->nr_events; |
514f1c67 ACM |
2977 | |
2978 | for (i = 0; i < evlist->nr_mmaps; i++) { | |
2979 | union perf_event *event; | |
d7f55c62 | 2980 | struct perf_mmap *md; |
514f1c67 | 2981 | |
d7f55c62 | 2982 | md = &evlist->mmap[i]; |
b9bae2c8 | 2983 | if (perf_mmap__read_init(md) < 0) |
d7f55c62 KL |
2984 | continue; |
2985 | ||
0019dc87 | 2986 | while ((event = perf_mmap__read_event(md)) != NULL) { |
efd5745e | 2987 | ++trace->nr_events; |
514f1c67 | 2988 | |
7ba61524 | 2989 | err = trace__deliver_event(trace, event); |
028713aa JO |
2990 | if (err) |
2991 | goto out_disable; | |
514f1c67 | 2992 | |
d6ace3df | 2993 | perf_mmap__consume(md); |
20c5f10e | 2994 | |
ba209f85 ACM |
2995 | if (interrupted) |
2996 | goto out_disable; | |
02ac5421 ACM |
2997 | |
2998 | if (done && !draining) { | |
2999 | perf_evlist__disable(evlist); | |
3000 | draining = true; | |
3001 | } | |
514f1c67 | 3002 | } |
d7f55c62 | 3003 | perf_mmap__read_done(md); |
514f1c67 ACM |
3004 | } |
3005 | ||
efd5745e | 3006 | if (trace->nr_events == before) { |
ba209f85 | 3007 | int timeout = done ? 100 : -1; |
f15eb531 | 3008 | |
46fb3c21 | 3009 | if (!draining && perf_evlist__poll(evlist, timeout) > 0) { |
a9c5e6c1 | 3010 | if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0) |
46fb3c21 ACM |
3011 | draining = true; |
3012 | ||
ba209f85 | 3013 | goto again; |
028713aa | 3014 | } else { |
7ba61524 | 3015 | if (trace__flush_events(trace)) |
028713aa | 3016 | goto out_disable; |
46fb3c21 | 3017 | } |
ba209f85 ACM |
3018 | } else { |
3019 | goto again; | |
f15eb531 NK |
3020 | } |
3021 | ||
ba209f85 | 3022 | out_disable: |
f3b623b8 ACM |
3023 | thread__zput(trace->current); |
3024 | ||
ba209f85 | 3025 | perf_evlist__disable(evlist); |
514f1c67 | 3026 | |
a761a8d1 ACM |
3027 | if (trace->sort_events) |
3028 | ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL); | |
028713aa | 3029 | |
c522739d ACM |
3030 | if (!err) { |
3031 | if (trace->summary) | |
3032 | trace__fprintf_thread_summary(trace, trace->output); | |
3033 | ||
3034 | if (trace->show_tool_stats) { | |
3035 | fprintf(trace->output, "Stats:\n " | |
3036 | " vfs_getname : %" PRIu64 "\n" | |
3037 | " proc_getname: %" PRIu64 "\n", | |
3038 | trace->stats.vfs_getname, | |
3039 | trace->stats.proc_getname); | |
3040 | } | |
3041 | } | |
bf2575c1 | 3042 | |
514f1c67 | 3043 | out_delete_evlist: |
33974a41 AV |
3044 | trace__symbols__exit(trace); |
3045 | ||
514f1c67 | 3046 | perf_evlist__delete(evlist); |
9ea42ba4 | 3047 | cgroup__put(trace->cgroup); |
14a052df | 3048 | trace->evlist = NULL; |
75b757ca | 3049 | trace->live = false; |
514f1c67 | 3050 | return err; |
6ef068cb ACM |
3051 | { |
3052 | char errbuf[BUFSIZ]; | |
a8f23d8f | 3053 | |
2cc990ba | 3054 | out_error_sched_stat_runtime: |
988bdb31 | 3055 | tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime"); |
2cc990ba ACM |
3056 | goto out_error; |
3057 | ||
801c67b0 | 3058 | out_error_raw_syscalls: |
988bdb31 | 3059 | tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)"); |
a8f23d8f ACM |
3060 | goto out_error; |
3061 | ||
e09b18d4 ACM |
3062 | out_error_mmap: |
3063 | perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf)); | |
3064 | goto out_error; | |
3065 | ||
a8f23d8f ACM |
3066 | out_error_open: |
3067 | perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf)); | |
3068 | ||
3069 | out_error: | |
6ef068cb | 3070 | fprintf(trace->output, "%s\n", errbuf); |
87f91868 | 3071 | goto out_delete_evlist; |
94ad89bc ACM |
3072 | |
3073 | out_error_apply_filters: | |
3074 | fprintf(trace->output, | |
3075 | "Failed to set filter \"%s\" on event %s with %d (%s)\n", | |
3076 | evsel->filter, perf_evsel__name(evsel), errno, | |
c8b5f2c9 | 3077 | str_error_r(errno, errbuf, sizeof(errbuf))); |
94ad89bc | 3078 | goto out_delete_evlist; |
514f1c67 | 3079 | } |
5ed08dae ACM |
3080 | out_error_mem: |
3081 | fprintf(trace->output, "Not enough memory to run!\n"); | |
3082 | goto out_delete_evlist; | |
19867b61 ACM |
3083 | |
3084 | out_errno: | |
3085 | fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno)); | |
3086 | goto out_delete_evlist; | |
a8f23d8f | 3087 | } |
514f1c67 | 3088 | |
6810fc91 DA |
3089 | static int trace__replay(struct trace *trace) |
3090 | { | |
3091 | const struct perf_evsel_str_handler handlers[] = { | |
c522739d | 3092 | { "probe:vfs_getname", trace__vfs_getname, }, |
6810fc91 | 3093 | }; |
8ceb41d7 | 3094 | struct perf_data data = { |
eae8ad80 JO |
3095 | .file = { |
3096 | .path = input_name, | |
3097 | }, | |
3098 | .mode = PERF_DATA_MODE_READ, | |
3099 | .force = trace->force, | |
f5fc1412 | 3100 | }; |
6810fc91 | 3101 | struct perf_session *session; |
003824e8 | 3102 | struct perf_evsel *evsel; |
6810fc91 DA |
3103 | int err = -1; |
3104 | ||
3105 | trace->tool.sample = trace__process_sample; | |
3106 | trace->tool.mmap = perf_event__process_mmap; | |
384c671e | 3107 | trace->tool.mmap2 = perf_event__process_mmap2; |
6810fc91 DA |
3108 | trace->tool.comm = perf_event__process_comm; |
3109 | trace->tool.exit = perf_event__process_exit; | |
3110 | trace->tool.fork = perf_event__process_fork; | |
3111 | trace->tool.attr = perf_event__process_attr; | |
f3b3614a | 3112 | trace->tool.tracing_data = perf_event__process_tracing_data; |
6810fc91 | 3113 | trace->tool.build_id = perf_event__process_build_id; |
f3b3614a | 3114 | trace->tool.namespaces = perf_event__process_namespaces; |
6810fc91 | 3115 | |
0a8cb85c | 3116 | trace->tool.ordered_events = true; |
6810fc91 DA |
3117 | trace->tool.ordering_requires_timestamps = true; |
3118 | ||
3119 | /* add tid to output */ | |
3120 | trace->multiple_threads = true; | |
3121 | ||
8ceb41d7 | 3122 | session = perf_session__new(&data, false, &trace->tool); |
6810fc91 | 3123 | if (session == NULL) |
52e02834 | 3124 | return -1; |
6810fc91 | 3125 | |
aa07df6e DA |
3126 | if (trace->opts.target.pid) |
3127 | symbol_conf.pid_list_str = strdup(trace->opts.target.pid); | |
3128 | ||
3129 | if (trace->opts.target.tid) | |
3130 | symbol_conf.tid_list_str = strdup(trace->opts.target.tid); | |
3131 | ||
0a7e6d1b | 3132 | if (symbol__init(&session->header.env) < 0) |
cb2ffae2 NK |
3133 | goto out; |
3134 | ||
8fb598e5 DA |
3135 | trace->host = &session->machines.host; |
3136 | ||
6810fc91 DA |
3137 | err = perf_session__set_tracepoints_handlers(session, handlers); |
3138 | if (err) | |
3139 | goto out; | |
3140 | ||
003824e8 NK |
3141 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, |
3142 | "raw_syscalls:sys_enter"); | |
9aca7f17 DA |
3143 | /* older kernels have syscalls tp versus raw_syscalls */ |
3144 | if (evsel == NULL) | |
3145 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, | |
3146 | "syscalls:sys_enter"); | |
003824e8 | 3147 | |
e281a960 | 3148 | if (evsel && |
63f11c80 | 3149 | (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 || |
e281a960 | 3150 | perf_evsel__init_sc_tp_ptr_field(evsel, args))) { |
003824e8 NK |
3151 | pr_err("Error during initialize raw_syscalls:sys_enter event\n"); |
3152 | goto out; | |
3153 | } | |
3154 | ||
3155 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, | |
3156 | "raw_syscalls:sys_exit"); | |
9aca7f17 DA |
3157 | if (evsel == NULL) |
3158 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, | |
3159 | "syscalls:sys_exit"); | |
e281a960 | 3160 | if (evsel && |
63f11c80 | 3161 | (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 || |
e281a960 | 3162 | perf_evsel__init_sc_tp_uint_field(evsel, ret))) { |
003824e8 | 3163 | pr_err("Error during initialize raw_syscalls:sys_exit event\n"); |
6810fc91 DA |
3164 | goto out; |
3165 | } | |
3166 | ||
e5cadb93 | 3167 | evlist__for_each_entry(session->evlist, evsel) { |
1e28fe0a SF |
3168 | if (evsel->attr.type == PERF_TYPE_SOFTWARE && |
3169 | (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ || | |
3170 | evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN || | |
3171 | evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS)) | |
3172 | evsel->handler = trace__pgfault; | |
3173 | } | |
3174 | ||
6810fc91 DA |
3175 | setup_pager(); |
3176 | ||
b7b61cbe | 3177 | err = perf_session__process_events(session); |
6810fc91 DA |
3178 | if (err) |
3179 | pr_err("Failed to process events, error %d", err); | |
3180 | ||
bf2575c1 DA |
3181 | else if (trace->summary) |
3182 | trace__fprintf_thread_summary(trace, trace->output); | |
3183 | ||
6810fc91 DA |
3184 | out: |
3185 | perf_session__delete(session); | |
3186 | ||
3187 | return err; | |
3188 | } | |
3189 | ||
1302d88e ACM |
3190 | static size_t trace__fprintf_threads_header(FILE *fp) |
3191 | { | |
3192 | size_t printed; | |
3193 | ||
99ff7150 | 3194 | printed = fprintf(fp, "\n Summary of events:\n\n"); |
bf2575c1 DA |
3195 | |
3196 | return printed; | |
3197 | } | |
3198 | ||
b535d523 ACM |
3199 | DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs, |
3200 | struct stats *stats; | |
3201 | double msecs; | |
3202 | int syscall; | |
3203 | ) | |
3204 | { | |
3205 | struct int_node *source = rb_entry(nd, struct int_node, rb_node); | |
3206 | struct stats *stats = source->priv; | |
3207 | ||
3208 | entry->syscall = source->i; | |
3209 | entry->stats = stats; | |
3210 | entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0; | |
3211 | } | |
3212 | ||
bf2575c1 DA |
3213 | static size_t thread__dump_stats(struct thread_trace *ttrace, |
3214 | struct trace *trace, FILE *fp) | |
3215 | { | |
bf2575c1 DA |
3216 | size_t printed = 0; |
3217 | struct syscall *sc; | |
b535d523 ACM |
3218 | struct rb_node *nd; |
3219 | DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats); | |
bf2575c1 | 3220 | |
b535d523 | 3221 | if (syscall_stats == NULL) |
bf2575c1 DA |
3222 | return 0; |
3223 | ||
3224 | printed += fprintf(fp, "\n"); | |
3225 | ||
834fd46d MW |
3226 | printed += fprintf(fp, " syscall calls total min avg max stddev\n"); |
3227 | printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n"); | |
3228 | printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n"); | |
99ff7150 | 3229 | |
98a91837 | 3230 | resort_rb__for_each_entry(nd, syscall_stats) { |
b535d523 | 3231 | struct stats *stats = syscall_stats_entry->stats; |
bf2575c1 DA |
3232 | if (stats) { |
3233 | double min = (double)(stats->min) / NSEC_PER_MSEC; | |
3234 | double max = (double)(stats->max) / NSEC_PER_MSEC; | |
3235 | double avg = avg_stats(stats); | |
3236 | double pct; | |
3237 | u64 n = (u64) stats->n; | |
3238 | ||
3239 | pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0; | |
3240 | avg /= NSEC_PER_MSEC; | |
3241 | ||
b535d523 | 3242 | sc = &trace->syscalls.table[syscall_stats_entry->syscall]; |
99ff7150 | 3243 | printed += fprintf(fp, " %-15s", sc->name); |
834fd46d | 3244 | printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f", |
b535d523 | 3245 | n, syscall_stats_entry->msecs, min, avg); |
27a778b5 | 3246 | printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct); |
bf2575c1 | 3247 | } |
bf2575c1 DA |
3248 | } |
3249 | ||
b535d523 | 3250 | resort_rb__delete(syscall_stats); |
bf2575c1 | 3251 | printed += fprintf(fp, "\n\n"); |
1302d88e ACM |
3252 | |
3253 | return printed; | |
3254 | } | |
3255 | ||
96c14451 | 3256 | static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace) |
896cbb56 | 3257 | { |
96c14451 | 3258 | size_t printed = 0; |
89dceb22 | 3259 | struct thread_trace *ttrace = thread__priv(thread); |
896cbb56 DA |
3260 | double ratio; |
3261 | ||
3262 | if (ttrace == NULL) | |
3263 | return 0; | |
3264 | ||
3265 | ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; | |
3266 | ||
15e65c69 | 3267 | printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid); |
99ff7150 | 3268 | printed += fprintf(fp, "%lu events, ", ttrace->nr_events); |
15e65c69 | 3269 | printed += fprintf(fp, "%.1f%%", ratio); |
a2ea67d7 SF |
3270 | if (ttrace->pfmaj) |
3271 | printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj); | |
3272 | if (ttrace->pfmin) | |
3273 | printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin); | |
03548ebf ACM |
3274 | if (trace->sched) |
3275 | printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms); | |
3276 | else if (fputc('\n', fp) != EOF) | |
3277 | ++printed; | |
3278 | ||
bf2575c1 | 3279 | printed += thread__dump_stats(ttrace, trace, fp); |
896cbb56 | 3280 | |
96c14451 ACM |
3281 | return printed; |
3282 | } | |
896cbb56 | 3283 | |
96c14451 ACM |
3284 | static unsigned long thread__nr_events(struct thread_trace *ttrace) |
3285 | { | |
3286 | return ttrace ? ttrace->nr_events : 0; | |
3287 | } | |
3288 | ||
3289 | DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)), | |
3290 | struct thread *thread; | |
3291 | ) | |
3292 | { | |
3293 | entry->thread = rb_entry(nd, struct thread, rb_node); | |
896cbb56 DA |
3294 | } |
3295 | ||
1302d88e ACM |
3296 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) |
3297 | { | |
96c14451 ACM |
3298 | size_t printed = trace__fprintf_threads_header(fp); |
3299 | struct rb_node *nd; | |
91e467bc | 3300 | int i; |
1302d88e | 3301 | |
91e467bc KL |
3302 | for (i = 0; i < THREADS__TABLE_SIZE; i++) { |
3303 | DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i); | |
96c14451 | 3304 | |
91e467bc KL |
3305 | if (threads == NULL) { |
3306 | fprintf(fp, "%s", "Error sorting output by nr_events!\n"); | |
3307 | return 0; | |
3308 | } | |
896cbb56 | 3309 | |
91e467bc KL |
3310 | resort_rb__for_each_entry(nd, threads) |
3311 | printed += trace__fprintf_thread(fp, threads_entry->thread, trace); | |
96c14451 | 3312 | |
91e467bc KL |
3313 | resort_rb__delete(threads); |
3314 | } | |
96c14451 | 3315 | return printed; |
1302d88e ACM |
3316 | } |
3317 | ||
ae9ed035 ACM |
3318 | static int trace__set_duration(const struct option *opt, const char *str, |
3319 | int unset __maybe_unused) | |
3320 | { | |
3321 | struct trace *trace = opt->value; | |
3322 | ||
3323 | trace->duration_filter = atof(str); | |
3324 | return 0; | |
3325 | } | |
3326 | ||
6a0b3aba ACM |
3327 | static int trace__set_filter_pids_from_option(const struct option *opt, const char *str, |
3328 | int unset __maybe_unused) | |
f078c385 ACM |
3329 | { |
3330 | int ret = -1; | |
3331 | size_t i; | |
3332 | struct trace *trace = opt->value; | |
3333 | /* | |
3334 | * FIXME: introduce a intarray class, plain parse csv and create a | |
3335 | * { int nr, int entries[] } struct... | |
3336 | */ | |
3337 | struct intlist *list = intlist__new(str); | |
3338 | ||
3339 | if (list == NULL) | |
3340 | return -1; | |
3341 | ||
3342 | i = trace->filter_pids.nr = intlist__nr_entries(list) + 1; | |
3343 | trace->filter_pids.entries = calloc(i, sizeof(pid_t)); | |
3344 | ||
3345 | if (trace->filter_pids.entries == NULL) | |
3346 | goto out; | |
3347 | ||
3348 | trace->filter_pids.entries[0] = getpid(); | |
3349 | ||
3350 | for (i = 1; i < trace->filter_pids.nr; ++i) | |
3351 | trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i; | |
3352 | ||
3353 | intlist__delete(list); | |
3354 | ret = 0; | |
3355 | out: | |
3356 | return ret; | |
3357 | } | |
3358 | ||
c24ff998 ACM |
3359 | static int trace__open_output(struct trace *trace, const char *filename) |
3360 | { | |
3361 | struct stat st; | |
3362 | ||
3363 | if (!stat(filename, &st) && st.st_size) { | |
3364 | char oldname[PATH_MAX]; | |
3365 | ||
3366 | scnprintf(oldname, sizeof(oldname), "%s.old", filename); | |
3367 | unlink(oldname); | |
3368 | rename(filename, oldname); | |
3369 | } | |
3370 | ||
3371 | trace->output = fopen(filename, "w"); | |
3372 | ||
3373 | return trace->output == NULL ? -errno : 0; | |
3374 | } | |
3375 | ||
598d02c5 SF |
3376 | static int parse_pagefaults(const struct option *opt, const char *str, |
3377 | int unset __maybe_unused) | |
3378 | { | |
3379 | int *trace_pgfaults = opt->value; | |
3380 | ||
3381 | if (strcmp(str, "all") == 0) | |
3382 | *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN; | |
3383 | else if (strcmp(str, "maj") == 0) | |
3384 | *trace_pgfaults |= TRACE_PFMAJ; | |
3385 | else if (strcmp(str, "min") == 0) | |
3386 | *trace_pgfaults |= TRACE_PFMIN; | |
3387 | else | |
3388 | return -1; | |
3389 | ||
3390 | return 0; | |
3391 | } | |
3392 | ||
14a052df ACM |
3393 | static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler) |
3394 | { | |
3395 | struct perf_evsel *evsel; | |
3396 | ||
e5cadb93 | 3397 | evlist__for_each_entry(evlist, evsel) |
14a052df ACM |
3398 | evsel->handler = handler; |
3399 | } | |
3400 | ||
d32855fa ACM |
3401 | static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist) |
3402 | { | |
3403 | struct perf_evsel *evsel; | |
3404 | ||
3405 | evlist__for_each_entry(evlist, evsel) { | |
3406 | if (evsel->priv || !evsel->tp_format) | |
3407 | continue; | |
3408 | ||
3409 | if (strcmp(evsel->tp_format->system, "syscalls")) | |
3410 | continue; | |
3411 | ||
3412 | if (perf_evsel__init_syscall_tp(evsel)) | |
3413 | return -1; | |
3414 | ||
3415 | if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) { | |
3416 | struct syscall_tp *sc = evsel->priv; | |
3417 | ||
3418 | if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64))) | |
3419 | return -1; | |
3420 | } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) { | |
3421 | struct syscall_tp *sc = evsel->priv; | |
3422 | ||
3423 | if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap)) | |
3424 | return -1; | |
3425 | } | |
3426 | } | |
3427 | ||
3428 | return 0; | |
3429 | } | |
3430 | ||
017037ff ACM |
3431 | /* |
3432 | * XXX: Hackish, just splitting the combined -e+--event (syscalls | |
3433 | * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use | |
3434 | * existing facilities unchanged (trace->ev_qualifier + parse_options()). | |
3435 | * | |
3436 | * It'd be better to introduce a parse_options() variant that would return a | |
3437 | * list with the terms it didn't match to an event... | |
3438 | */ | |
3439 | static int trace__parse_events_option(const struct option *opt, const char *str, | |
3440 | int unset __maybe_unused) | |
3441 | { | |
3442 | struct trace *trace = (struct trace *)opt->value; | |
3443 | const char *s = str; | |
3444 | char *sep = NULL, *lists[2] = { NULL, NULL, }; | |
27702bcf | 3445 | int len = strlen(str) + 1, err = -1, list, idx; |
017037ff ACM |
3446 | char *strace_groups_dir = system_path(STRACE_GROUPS_DIR); |
3447 | char group_name[PATH_MAX]; | |
f932184e | 3448 | struct syscall_fmt *fmt; |
017037ff ACM |
3449 | |
3450 | if (strace_groups_dir == NULL) | |
3451 | return -1; | |
3452 | ||
3453 | if (*s == '!') { | |
3454 | ++s; | |
3455 | trace->not_ev_qualifier = true; | |
3456 | } | |
3457 | ||
3458 | while (1) { | |
3459 | if ((sep = strchr(s, ',')) != NULL) | |
3460 | *sep = '\0'; | |
3461 | ||
3462 | list = 0; | |
27702bcf ACM |
3463 | if (syscalltbl__id(trace->sctbl, s) >= 0 || |
3464 | syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) { | |
017037ff | 3465 | list = 1; |
f932184e ACM |
3466 | goto do_concat; |
3467 | } | |
3468 | ||
3469 | fmt = syscall_fmt__find_by_alias(s); | |
3470 | if (fmt != NULL) { | |
3471 | list = 1; | |
3472 | s = fmt->name; | |
017037ff ACM |
3473 | } else { |
3474 | path__join(group_name, sizeof(group_name), strace_groups_dir, s); | |
3475 | if (access(group_name, R_OK) == 0) | |
3476 | list = 1; | |
3477 | } | |
f932184e | 3478 | do_concat: |
017037ff ACM |
3479 | if (lists[list]) { |
3480 | sprintf(lists[list] + strlen(lists[list]), ",%s", s); | |
3481 | } else { | |
3482 | lists[list] = malloc(len); | |
3483 | if (lists[list] == NULL) | |
3484 | goto out; | |
3485 | strcpy(lists[list], s); | |
3486 | } | |
3487 | ||
3488 | if (!sep) | |
3489 | break; | |
3490 | ||
3491 | *sep = ','; | |
3492 | s = sep + 1; | |
3493 | } | |
3494 | ||
3495 | if (lists[1] != NULL) { | |
3496 | struct strlist_config slist_config = { | |
3497 | .dirname = strace_groups_dir, | |
3498 | }; | |
3499 | ||
3500 | trace->ev_qualifier = strlist__new(lists[1], &slist_config); | |
3501 | if (trace->ev_qualifier == NULL) { | |
3502 | fputs("Not enough memory to parse event qualifier", trace->output); | |
3503 | goto out; | |
3504 | } | |
3505 | ||
3506 | if (trace__validate_ev_qualifier(trace)) | |
3507 | goto out; | |
b912885a | 3508 | trace->trace_syscalls = true; |
017037ff ACM |
3509 | } |
3510 | ||
3511 | err = 0; | |
3512 | ||
3513 | if (lists[0]) { | |
3514 | struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event", | |
3515 | "event selector. use 'perf list' to list available events", | |
3516 | parse_events_option); | |
3517 | err = parse_events_option(&o, lists[0], 0); | |
3518 | } | |
3519 | out: | |
3520 | if (sep) | |
3521 | *sep = ','; | |
3522 | ||
3523 | return err; | |
3524 | } | |
3525 | ||
9ea42ba4 ACM |
3526 | static int trace__parse_cgroups(const struct option *opt, const char *str, int unset) |
3527 | { | |
3528 | struct trace *trace = opt->value; | |
3529 | ||
3530 | if (!list_empty(&trace->evlist->entries)) | |
3531 | return parse_cgroups(opt, str, unset); | |
3532 | ||
3533 | trace->cgroup = evlist__findnew_cgroup(trace->evlist, str); | |
3534 | ||
3535 | return 0; | |
3536 | } | |
3537 | ||
744fafc7 ACM |
3538 | static struct bpf_map *bpf__find_map_by_name(const char *name) |
3539 | { | |
3540 | struct bpf_object *obj, *tmp; | |
3541 | ||
3542 | bpf_object__for_each_safe(obj, tmp) { | |
3543 | struct bpf_map *map = bpf_object__find_map_by_name(obj, name); | |
3544 | if (map) | |
3545 | return map; | |
3546 | ||
3547 | } | |
3548 | ||
3549 | return NULL; | |
3550 | } | |
3551 | ||
3552 | static void trace__set_bpf_map_filtered_pids(struct trace *trace) | |
3553 | { | |
3554 | trace->filter_pids.map = bpf__find_map_by_name("pids_filtered"); | |
3555 | } | |
3556 | ||
b27b38ed ACM |
3557 | static void trace__set_bpf_map_syscalls(struct trace *trace) |
3558 | { | |
3559 | trace->syscalls.map = bpf__find_map_by_name("syscalls"); | |
3560 | } | |
3561 | ||
ac96287c ACM |
3562 | static int trace__config(const char *var, const char *value, void *arg) |
3563 | { | |
e7c634fc | 3564 | struct trace *trace = arg; |
ac96287c ACM |
3565 | int err = 0; |
3566 | ||
3567 | if (!strcmp(var, "trace.add_events")) { | |
ac96287c ACM |
3568 | struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event", |
3569 | "event selector. use 'perf list' to list available events", | |
3570 | parse_events_option); | |
3571 | err = parse_events_option(&o, value, 0); | |
b036146f ACM |
3572 | } else if (!strcmp(var, "trace.show_timestamp")) { |
3573 | trace->show_tstamp = perf_config_bool(var, value); | |
42e4a52d ACM |
3574 | } else if (!strcmp(var, "trace.show_duration")) { |
3575 | trace->show_duration = perf_config_bool(var, value); | |
9d6dc178 ACM |
3576 | } else if (!strcmp(var, "trace.show_arg_names")) { |
3577 | trace->show_arg_names = perf_config_bool(var, value); | |
3578 | if (!trace->show_arg_names) | |
3579 | trace->show_zeros = true; | |
e7c634fc | 3580 | } else if (!strcmp(var, "trace.show_zeros")) { |
9d6dc178 ACM |
3581 | bool new_show_zeros = perf_config_bool(var, value); |
3582 | if (!trace->show_arg_names && !new_show_zeros) { | |
3583 | pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n"); | |
3584 | goto out; | |
3585 | } | |
3586 | trace->show_zeros = new_show_zeros; | |
c65c83ff ACM |
3587 | } else if (!strcmp(var, "trace.show_prefix")) { |
3588 | trace->show_string_prefix = perf_config_bool(var, value); | |
d32de87e ACM |
3589 | } else if (!strcmp(var, "trace.no_inherit")) { |
3590 | trace->opts.no_inherit = perf_config_bool(var, value); | |
9ed45d59 ACM |
3591 | } else if (!strcmp(var, "trace.args_alignment")) { |
3592 | int args_alignment = 0; | |
3593 | if (perf_config_int(&args_alignment, var, value) == 0) | |
3594 | trace->args_alignment = args_alignment; | |
ac96287c | 3595 | } |
9d6dc178 | 3596 | out: |
ac96287c ACM |
3597 | return err; |
3598 | } | |
3599 | ||
b0ad8ea6 | 3600 | int cmd_trace(int argc, const char **argv) |
514f1c67 | 3601 | { |
6fdd9cb7 | 3602 | const char *trace_usage[] = { |
f15eb531 NK |
3603 | "perf trace [<options>] [<command>]", |
3604 | "perf trace [<options>] -- <command> [<options>]", | |
5e2485b1 DA |
3605 | "perf trace record [<options>] [<command>]", |
3606 | "perf trace record [<options>] -- <command> [<options>]", | |
514f1c67 ACM |
3607 | NULL |
3608 | }; | |
3609 | struct trace trace = { | |
514f1c67 ACM |
3610 | .syscalls = { |
3611 | . max = -1, | |
3612 | }, | |
3613 | .opts = { | |
3614 | .target = { | |
3615 | .uid = UINT_MAX, | |
3616 | .uses_mmap = true, | |
3617 | }, | |
3618 | .user_freq = UINT_MAX, | |
3619 | .user_interval = ULLONG_MAX, | |
509051ea | 3620 | .no_buffering = true, |
38d5447d | 3621 | .mmap_pages = UINT_MAX, |
514f1c67 | 3622 | }, |
007d66a0 | 3623 | .output = stderr, |
50c95cbd | 3624 | .show_comm = true, |
b036146f | 3625 | .show_tstamp = true, |
42e4a52d | 3626 | .show_duration = true, |
9d6dc178 | 3627 | .show_arg_names = true, |
9ed45d59 | 3628 | .args_alignment = 70, |
b912885a | 3629 | .trace_syscalls = false, |
44621819 | 3630 | .kernel_syscallchains = false, |
05614993 | 3631 | .max_stack = UINT_MAX, |
5067a8cd | 3632 | .max_events = ULONG_MAX, |
514f1c67 | 3633 | }; |
c24ff998 | 3634 | const char *output_name = NULL; |
514f1c67 | 3635 | const struct option trace_options[] = { |
017037ff ACM |
3636 | OPT_CALLBACK('e', "event", &trace, "event", |
3637 | "event/syscall selector. use 'perf list' to list available events", | |
3638 | trace__parse_events_option), | |
50c95cbd ACM |
3639 | OPT_BOOLEAN(0, "comm", &trace.show_comm, |
3640 | "show the thread COMM next to its id"), | |
c522739d | 3641 | OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"), |
017037ff ACM |
3642 | OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace", |
3643 | trace__parse_events_option), | |
c24ff998 | 3644 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
6810fc91 | 3645 | OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"), |
514f1c67 ACM |
3646 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", |
3647 | "trace events on existing process id"), | |
ac9be8ee | 3648 | OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", |
514f1c67 | 3649 | "trace events on existing thread id"), |
fa0e4ffe | 3650 | OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids", |
6a0b3aba | 3651 | "pids to filter (by the kernel)", trace__set_filter_pids_from_option), |
ac9be8ee | 3652 | OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, |
514f1c67 | 3653 | "system-wide collection from all CPUs"), |
ac9be8ee | 3654 | OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", |
514f1c67 | 3655 | "list of cpus to monitor"), |
6810fc91 | 3656 | OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, |
514f1c67 | 3657 | "child tasks do not inherit counters"), |
994a1f78 JO |
3658 | OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages", |
3659 | "number of mmap data pages", | |
3660 | perf_evlist__parse_mmap_pages), | |
ac9be8ee | 3661 | OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user", |
514f1c67 | 3662 | "user to profile"), |
ae9ed035 ACM |
3663 | OPT_CALLBACK(0, "duration", &trace, "float", |
3664 | "show only events with duration > N.M ms", | |
3665 | trace__set_duration), | |
1302d88e | 3666 | OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), |
7c304ee0 | 3667 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
4bb09192 DA |
3668 | OPT_BOOLEAN('T', "time", &trace.full_time, |
3669 | "Show full timestamp, not time relative to first start"), | |
0a6545bd ACM |
3670 | OPT_BOOLEAN(0, "failure", &trace.failure_only, |
3671 | "Show only syscalls that failed"), | |
fd2eabaf DA |
3672 | OPT_BOOLEAN('s', "summary", &trace.summary_only, |
3673 | "Show only syscall summary with statistics"), | |
3674 | OPT_BOOLEAN('S', "with-summary", &trace.summary, | |
3675 | "Show all syscalls and summary with statistics"), | |
598d02c5 SF |
3676 | OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min", |
3677 | "Trace pagefaults", parse_pagefaults, "maj"), | |
e281a960 | 3678 | OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"), |
e366a6d8 | 3679 | OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"), |
566a0885 MW |
3680 | OPT_CALLBACK(0, "call-graph", &trace.opts, |
3681 | "record_mode[,record_size]", record_callchain_help, | |
3682 | &record_parse_callchain_opt), | |
44621819 ACM |
3683 | OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains, |
3684 | "Show the kernel callchains on the syscall exit path"), | |
5067a8cd ACM |
3685 | OPT_ULONG(0, "max-events", &trace.max_events, |
3686 | "Set the maximum number of events to print, exit after that is reached. "), | |
5cf9c84e ACM |
3687 | OPT_UINTEGER(0, "min-stack", &trace.min_stack, |
3688 | "Set the minimum stack depth when parsing the callchain, " | |
3689 | "anything below the specified depth will be ignored."), | |
c6d4a494 ACM |
3690 | OPT_UINTEGER(0, "max-stack", &trace.max_stack, |
3691 | "Set the maximum stack depth when parsing the callchain, " | |
3692 | "anything beyond the specified depth will be ignored. " | |
4cb93446 | 3693 | "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), |
a761a8d1 ACM |
3694 | OPT_BOOLEAN(0, "sort-events", &trace.sort_events, |
3695 | "Sort batch of events before processing, use if getting out of order events"), | |
591421e1 ACM |
3696 | OPT_BOOLEAN(0, "print-sample", &trace.print_sample, |
3697 | "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"), | |
3fcb10e4 | 3698 | OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout, |
9d9cad76 | 3699 | "per thread proc mmap processing timeout in ms"), |
9ea42ba4 ACM |
3700 | OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only", |
3701 | trace__parse_cgroups), | |
e36b7821 AB |
3702 | OPT_UINTEGER('D', "delay", &trace.opts.initial_delay, |
3703 | "ms to wait before starting measurement after program " | |
3704 | "start"), | |
514f1c67 ACM |
3705 | OPT_END() |
3706 | }; | |
ccd62a89 | 3707 | bool __maybe_unused max_stack_user_set = true; |
f3e459d1 | 3708 | bool mmap_pages_user_set = true; |
78e890ea | 3709 | struct perf_evsel *evsel; |
6fdd9cb7 | 3710 | const char * const trace_subcommands[] = { "record", NULL }; |
78e890ea | 3711 | int err = -1; |
32caf0d1 | 3712 | char bf[BUFSIZ]; |
514f1c67 | 3713 | |
4d08cb80 ACM |
3714 | signal(SIGSEGV, sighandler_dump_stack); |
3715 | signal(SIGFPE, sighandler_dump_stack); | |
3716 | ||
14a052df | 3717 | trace.evlist = perf_evlist__new(); |
fd0db102 | 3718 | trace.sctbl = syscalltbl__new(); |
14a052df | 3719 | |
fd0db102 | 3720 | if (trace.evlist == NULL || trace.sctbl == NULL) { |
14a052df | 3721 | pr_err("Not enough memory to run!\n"); |
ff8f695c | 3722 | err = -ENOMEM; |
14a052df ACM |
3723 | goto out; |
3724 | } | |
3725 | ||
ac96287c ACM |
3726 | err = perf_config(trace__config, &trace); |
3727 | if (err) | |
3728 | goto out; | |
3729 | ||
6fdd9cb7 YS |
3730 | argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands, |
3731 | trace_usage, PARSE_OPT_STOP_AT_NON_OPTION); | |
fd2eabaf | 3732 | |
9ea42ba4 ACM |
3733 | if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) { |
3734 | usage_with_options_msg(trace_usage, trace_options, | |
3735 | "cgroup monitoring only available in system-wide mode"); | |
3736 | } | |
3737 | ||
78e890ea ACM |
3738 | evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__"); |
3739 | if (IS_ERR(evsel)) { | |
3740 | bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf)); | |
e0b6d2ef ACM |
3741 | pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf); |
3742 | goto out; | |
3743 | } | |
3744 | ||
744fafc7 | 3745 | if (evsel) { |
d3d1c4bd | 3746 | trace.syscalls.events.augmented = evsel; |
744fafc7 | 3747 | trace__set_bpf_map_filtered_pids(&trace); |
b27b38ed | 3748 | trace__set_bpf_map_syscalls(&trace); |
744fafc7 | 3749 | } |
d3d1c4bd | 3750 | |
d7888573 WN |
3751 | err = bpf__setup_stdout(trace.evlist); |
3752 | if (err) { | |
3753 | bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf)); | |
3754 | pr_err("ERROR: Setup BPF stdout failed: %s\n", bf); | |
3755 | goto out; | |
3756 | } | |
3757 | ||
59247e33 ACM |
3758 | err = -1; |
3759 | ||
598d02c5 SF |
3760 | if (trace.trace_pgfaults) { |
3761 | trace.opts.sample_address = true; | |
3762 | trace.opts.sample_time = true; | |
3763 | } | |
3764 | ||
f3e459d1 ACM |
3765 | if (trace.opts.mmap_pages == UINT_MAX) |
3766 | mmap_pages_user_set = false; | |
3767 | ||
05614993 | 3768 | if (trace.max_stack == UINT_MAX) { |
029c75e5 | 3769 | trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack(); |
05614993 ACM |
3770 | max_stack_user_set = false; |
3771 | } | |
3772 | ||
3773 | #ifdef HAVE_DWARF_UNWIND_SUPPORT | |
75d50117 | 3774 | if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) { |
05614993 | 3775 | record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false); |
75d50117 | 3776 | } |
05614993 ACM |
3777 | #endif |
3778 | ||
2ddd5c04 | 3779 | if (callchain_param.enabled) { |
f3e459d1 ACM |
3780 | if (!mmap_pages_user_set && geteuid() == 0) |
3781 | trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4; | |
3782 | ||
566a0885 | 3783 | symbol_conf.use_callchain = true; |
f3e459d1 | 3784 | } |
566a0885 | 3785 | |
d32855fa | 3786 | if (trace.evlist->nr_entries > 0) { |
14a052df | 3787 | evlist__set_evsel_handler(trace.evlist, trace__event_handler); |
d32855fa ACM |
3788 | if (evlist__set_syscall_tp_fields(trace.evlist)) { |
3789 | perror("failed to set syscalls:* tracepoint fields"); | |
3790 | goto out; | |
3791 | } | |
3792 | } | |
14a052df | 3793 | |
a761a8d1 ACM |
3794 | if (trace.sort_events) { |
3795 | ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace); | |
3796 | ordered_events__set_copy_on_queue(&trace.oe.data, true); | |
3797 | } | |
028713aa | 3798 | |
b1a9e253 ACM |
3799 | /* |
3800 | * If we are augmenting syscalls, then combine what we put in the | |
3801 | * __augmented_syscalls__ BPF map with what is in the | |
3802 | * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF, | |
3803 | * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit. | |
3804 | * | |
3805 | * We'll switch to look at two BPF maps, one for sys_enter and the | |
3806 | * other for sys_exit when we start augmenting the sys_exit paths with | |
3807 | * buffers that are being copied from kernel to userspace, think 'read' | |
3808 | * syscall. | |
3809 | */ | |
3810 | if (trace.syscalls.events.augmented) { | |
3811 | evsel = trace.syscalls.events.augmented; | |
3812 | ||
3813 | if (perf_evsel__init_augmented_syscall_tp(evsel) || | |
3814 | perf_evsel__init_augmented_syscall_tp_args(evsel)) | |
3815 | goto out; | |
3816 | evsel->handler = trace__sys_enter; | |
3817 | ||
3818 | evlist__for_each_entry(trace.evlist, evsel) { | |
cd26ea6d ACM |
3819 | bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0; |
3820 | ||
3821 | if (raw_syscalls_sys_exit) { | |
3822 | trace.raw_augmented_syscalls = true; | |
3823 | goto init_augmented_syscall_tp; | |
3824 | } | |
3825 | ||
3826 | if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) { | |
3827 | init_augmented_syscall_tp: | |
b1a9e253 ACM |
3828 | perf_evsel__init_augmented_syscall_tp(evsel); |
3829 | perf_evsel__init_augmented_syscall_tp_ret(evsel); | |
3830 | evsel->handler = trace__sys_exit; | |
3831 | } | |
3832 | } | |
3833 | } | |
3834 | ||
1e28fe0a SF |
3835 | if ((argc >= 1) && (strcmp(argv[0], "record") == 0)) |
3836 | return trace__record(&trace, argc-1, &argv[1]); | |
3837 | ||
3838 | /* summary_only implies summary option, but don't overwrite summary if set */ | |
3839 | if (trace.summary_only) | |
3840 | trace.summary = trace.summary_only; | |
3841 | ||
726f3234 ACM |
3842 | if (!trace.trace_syscalls && !trace.trace_pgfaults && |
3843 | trace.evlist->nr_entries == 0 /* Was --events used? */) { | |
b912885a | 3844 | trace.trace_syscalls = true; |
59247e33 ACM |
3845 | } |
3846 | ||
c24ff998 ACM |
3847 | if (output_name != NULL) { |
3848 | err = trace__open_output(&trace, output_name); | |
3849 | if (err < 0) { | |
3850 | perror("failed to create output file"); | |
3851 | goto out; | |
3852 | } | |
3853 | } | |
3854 | ||
602ad878 | 3855 | err = target__validate(&trace.opts.target); |
32caf0d1 | 3856 | if (err) { |
602ad878 | 3857 | target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
c24ff998 ACM |
3858 | fprintf(trace.output, "%s", bf); |
3859 | goto out_close; | |
32caf0d1 NK |
3860 | } |
3861 | ||
602ad878 | 3862 | err = target__parse_uid(&trace.opts.target); |
514f1c67 | 3863 | if (err) { |
602ad878 | 3864 | target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
c24ff998 ACM |
3865 | fprintf(trace.output, "%s", bf); |
3866 | goto out_close; | |
514f1c67 ACM |
3867 | } |
3868 | ||
602ad878 | 3869 | if (!argc && target__none(&trace.opts.target)) |
ee76120e NK |
3870 | trace.opts.target.system_wide = true; |
3871 | ||
6810fc91 DA |
3872 | if (input_name) |
3873 | err = trace__replay(&trace); | |
3874 | else | |
3875 | err = trace__run(&trace, argc, argv); | |
1302d88e | 3876 | |
c24ff998 ACM |
3877 | out_close: |
3878 | if (output_name != NULL) | |
3879 | fclose(trace.output); | |
3880 | out: | |
1302d88e | 3881 | return err; |
514f1c67 | 3882 | } |