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