4 * Builtin 'trace' command:
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.
10 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
12 * Initially based on the 'trace' prototype by Thomas Gleixner:
14 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
17 #include "util/record.h"
18 #include <traceevent/event-parse.h>
19 #include <api/fs/tracing_path.h>
21 #include "util/bpf_map.h"
22 #include "util/rlimit.h"
24 #include "util/cgroup.h"
25 #include "util/color.h"
26 #include "util/config.h"
27 #include "util/debug.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"
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"
51 #include "syscalltbl.h"
52 #include "rb_resort.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>
68 #include <sys/sysmacros.h>
70 #include <linux/ctype.h>
73 # define O_CLOEXEC 02000000
76 #ifndef F_LINUX_SPECIFIC_BASE
77 # define F_LINUX_SPECIFIC_BASE 1024
81 struct perf_tool tool
;
82 struct syscalltbl
*sctbl
;
84 struct syscall
*table
;
86 struct { // per syscall BPF_MAP_TYPE_PROG_ARRAY
87 struct bpf_map
*sys_enter
,
91 struct evsel
*sys_enter
,
95 struct bpf_program
*unaugmented_prog
;
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
;
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
;
122 double duration_filter
;
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
;
134 bool not_ev_qualifier
;
138 bool multiple_threads
;
144 bool show_tool_stats
;
146 bool kernel_syscallchains
;
152 bool show_string_prefix
;
157 struct ordered_events data
;
165 u64 (*integer
)(struct tp_field
*field
, struct perf_sample
*sample
);
166 void *(*pointer
)(struct tp_field
*field
, struct perf_sample
*sample
);
170 #define TP_UINT_FIELD(bits) \
171 static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
174 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
183 #define TP_UINT_FIELD__SWAPPED(bits) \
184 static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
187 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
188 return bswap_##bits(value);\
191 TP_UINT_FIELD__SWAPPED(16);
192 TP_UINT_FIELD__SWAPPED(32);
193 TP_UINT_FIELD__SWAPPED(64);
195 static int __tp_field__init_uint(struct tp_field
*field
, int size
, int offset
, bool needs_swap
)
197 field
->offset
= offset
;
201 field
->integer
= tp_field__u8
;
204 field
->integer
= needs_swap
? tp_field__swapped_u16
: tp_field__u16
;
207 field
->integer
= needs_swap
? tp_field__swapped_u32
: tp_field__u32
;
210 field
->integer
= needs_swap
? tp_field__swapped_u64
: tp_field__u64
;
219 static int tp_field__init_uint(struct tp_field
*field
, struct tep_format_field
*format_field
, bool needs_swap
)
221 return __tp_field__init_uint(field
, format_field
->size
, format_field
->offset
, needs_swap
);
224 static void *tp_field__ptr(struct tp_field
*field
, struct perf_sample
*sample
)
226 return sample
->raw_data
+ field
->offset
;
229 static int __tp_field__init_ptr(struct tp_field
*field
, int offset
)
231 field
->offset
= offset
;
232 field
->pointer
= tp_field__ptr
;
236 static int tp_field__init_ptr(struct tp_field
*field
, struct tep_format_field
*format_field
)
238 return __tp_field__init_ptr(field
, format_field
->offset
);
244 struct tp_field args
, ret
;
248 static int perf_evsel__init_tp_uint_field(struct evsel
*evsel
,
249 struct tp_field
*field
,
252 struct tep_format_field
*format_field
= perf_evsel__field(evsel
, name
);
254 if (format_field
== NULL
)
257 return tp_field__init_uint(field
, format_field
, evsel
->needs_swap
);
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); })
264 static int perf_evsel__init_tp_ptr_field(struct evsel
*evsel
,
265 struct tp_field
*field
,
268 struct tep_format_field
*format_field
= perf_evsel__field(evsel
, name
);
270 if (format_field
== NULL
)
273 return tp_field__init_ptr(field
, format_field
);
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); })
280 static void evsel__delete_priv(struct evsel
*evsel
)
283 evsel__delete(evsel
);
286 static int perf_evsel__init_syscall_tp(struct evsel
*evsel
)
288 struct syscall_tp
*sc
= evsel
->priv
= malloc(sizeof(struct syscall_tp
));
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"))
303 static int perf_evsel__init_augmented_syscall_tp(struct evsel
*evsel
, struct evsel
*tp
)
305 struct syscall_tp
*sc
= evsel
->priv
= malloc(sizeof(struct syscall_tp
));
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
)
313 if (__tp_field__init_uint(&sc
->id
, syscall_id
->size
, syscall_id
->offset
, evsel
->needs_swap
))
325 static int perf_evsel__init_augmented_syscall_tp_args(struct evsel
*evsel
)
327 struct syscall_tp
*sc
= evsel
->priv
;
329 return __tp_field__init_ptr(&sc
->args
, sc
->id
.offset
+ sizeof(u64
));
332 static int perf_evsel__init_augmented_syscall_tp_ret(struct evsel
*evsel
)
334 struct syscall_tp
*sc
= evsel
->priv
;
336 return __tp_field__init_uint(&sc
->ret
, sizeof(u64
), sc
->id
.offset
+ sizeof(u64
), evsel
->needs_swap
);
339 static int perf_evsel__init_raw_syscall_tp(struct evsel
*evsel
, void *handler
)
341 evsel
->priv
= malloc(sizeof(struct syscall_tp
));
342 if (evsel
->priv
!= NULL
) {
343 if (perf_evsel__init_sc_tp_uint_field(evsel
, id
))
346 evsel
->handler
= handler
;
357 static struct evsel
*perf_evsel__raw_syscall_newtp(const char *direction
, void *handler
)
359 struct evsel
*evsel
= perf_evsel__newtp("raw_syscalls", direction
);
361 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
363 evsel
= perf_evsel__newtp("syscalls", direction
);
368 if (perf_evsel__init_raw_syscall_tp(evsel
, handler
))
374 evsel__delete_priv(evsel
);
378 #define perf_evsel__sc_tp_uint(evsel, name, sample) \
379 ({ struct syscall_tp *fields = evsel->priv; \
380 fields->name.integer(&fields->name, sample); })
382 #define perf_evsel__sc_tp_ptr(evsel, name, sample) \
383 ({ struct syscall_tp *fields = evsel->priv; \
384 fields->name.pointer(&fields->name, sample); })
386 size_t strarray__scnprintf(struct strarray
*sa
, char *bf
, size_t size
, const char *intfmt
, bool show_prefix
, int val
)
388 int idx
= val
- sa
->offset
;
390 if (idx
< 0 || idx
>= sa
->nr_entries
|| sa
->entries
[idx
] == NULL
) {
391 size_t printed
= scnprintf(bf
, size
, intfmt
, val
);
393 printed
+= scnprintf(bf
+ printed
, size
- printed
, " /* %s??? */", sa
->prefix
);
397 return scnprintf(bf
, size
, "%s%s", show_prefix
? sa
->prefix
: "", sa
->entries
[idx
]);
400 static size_t __syscall_arg__scnprintf_strarray(char *bf
, size_t size
,
402 struct syscall_arg
*arg
)
404 return strarray__scnprintf(arg
->parm
, bf
, size
, intfmt
, arg
->show_string_prefix
, arg
->val
);
407 static size_t syscall_arg__scnprintf_strarray(char *bf
, size_t size
,
408 struct syscall_arg
*arg
)
410 return __syscall_arg__scnprintf_strarray(bf
, size
, "%d", arg
);
413 #define SCA_STRARRAY syscall_arg__scnprintf_strarray
415 size_t syscall_arg__scnprintf_strarray_flags(char *bf
, size_t size
, struct syscall_arg
*arg
)
417 return strarray__scnprintf_flags(arg
->parm
, bf
, size
, arg
->show_string_prefix
, arg
->val
);
420 size_t strarrays__scnprintf(struct strarrays
*sas
, char *bf
, size_t size
, const char *intfmt
, bool show_prefix
, int val
)
425 for (i
= 0; i
< sas
->nr_entries
; ++i
) {
426 struct strarray
*sa
= sas
->entries
[i
];
427 int idx
= val
- sa
->offset
;
429 if (idx
>= 0 && idx
< sa
->nr_entries
) {
430 if (sa
->entries
[idx
] == NULL
)
432 return scnprintf(bf
, size
, "%s%s", show_prefix
? sa
->prefix
: "", sa
->entries
[idx
]);
436 printed
= scnprintf(bf
, size
, intfmt
, val
);
438 printed
+= scnprintf(bf
+ printed
, size
- printed
, " /* %s??? */", sas
->entries
[0]->prefix
);
442 size_t syscall_arg__scnprintf_strarrays(char *bf
, size_t size
,
443 struct syscall_arg
*arg
)
445 return strarrays__scnprintf(arg
->parm
, bf
, size
, "%d", arg
->show_string_prefix
, arg
->val
);
449 #define AT_FDCWD -100
452 static size_t syscall_arg__scnprintf_fd_at(char *bf
, size_t size
,
453 struct syscall_arg
*arg
)
456 const char *prefix
= "AT_FD";
459 return scnprintf(bf
, size
, "%s%s", arg
->show_string_prefix
? prefix
: "", "CWD");
461 return syscall_arg__scnprintf_fd(bf
, size
, arg
);
464 #define SCA_FDAT syscall_arg__scnprintf_fd_at
466 static size_t syscall_arg__scnprintf_close_fd(char *bf
, size_t size
,
467 struct syscall_arg
*arg
);
469 #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
471 size_t syscall_arg__scnprintf_hex(char *bf
, size_t size
, struct syscall_arg
*arg
)
473 return scnprintf(bf
, size
, "%#lx", arg
->val
);
476 size_t syscall_arg__scnprintf_ptr(char *bf
, size_t size
, struct syscall_arg
*arg
)
479 return scnprintf(bf
, size
, "NULL");
480 return syscall_arg__scnprintf_hex(bf
, size
, arg
);
483 size_t syscall_arg__scnprintf_int(char *bf
, size_t size
, struct syscall_arg
*arg
)
485 return scnprintf(bf
, size
, "%d", arg
->val
);
488 size_t syscall_arg__scnprintf_long(char *bf
, size_t size
, struct syscall_arg
*arg
)
490 return scnprintf(bf
, size
, "%ld", arg
->val
);
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",
497 static DEFINE_STRARRAY(bpf_cmd
, "BPF_");
499 static const char *fsmount_flags
[] = {
502 static DEFINE_STRARRAY(fsmount_flags
, "FSMOUNT_");
504 #include "trace/beauty/generated/fsconfig_arrays.c"
506 static DEFINE_STRARRAY(fsconfig_cmds
, "FSCONFIG_");
508 static const char *epoll_ctl_ops
[] = { "ADD", "DEL", "MOD", };
509 static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops
, "EPOLL_CTL_", 1);
511 static const char *itimers
[] = { "REAL", "VIRTUAL", "PROF", };
512 static DEFINE_STRARRAY(itimers
, "ITIMER_");
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",
521 static DEFINE_STRARRAY(keyctl_options
, "KEYCTL_");
523 static const char *whences
[] = { "SET", "CUR", "END",
531 static DEFINE_STRARRAY(whences
, "SEEK_");
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",
539 static DEFINE_STRARRAY(fcntl_cmds
, "F_");
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",
547 static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds
, "F_", F_LINUX_SPECIFIC_BASE
);
549 static struct strarray
*fcntl_cmds_arrays
[] = {
550 &strarray__fcntl_cmds
,
551 &strarray__fcntl_linux_specific_cmds
,
554 static DEFINE_STRARRAYS(fcntl_cmds_arrays
);
556 static const char *rlimit_resources
[] = {
557 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
558 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
561 static DEFINE_STRARRAY(rlimit_resources
, "RLIMIT_");
563 static const char *sighow
[] = { "BLOCK", "UNBLOCK", "SETMASK", };
564 static DEFINE_STRARRAY(sighow
, "SIG_");
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"
571 static DEFINE_STRARRAY(clockid
, "CLOCK_");
573 static size_t syscall_arg__scnprintf_access_mode(char *bf
, size_t size
,
574 struct syscall_arg
*arg
)
576 bool show_prefix
= arg
->show_string_prefix
;
577 const char *suffix
= "_OK";
581 if (mode
== F_OK
) /* 0 */
582 return scnprintf(bf
, size
, "F%s", show_prefix
? suffix
: "");
584 if (mode & n##_OK) { \
585 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
595 printed
+= scnprintf(bf
+ printed
, size
- printed
, "|%#x", mode
);
600 #define SCA_ACCMODE syscall_arg__scnprintf_access_mode
602 static size_t syscall_arg__scnprintf_filename(char *bf
, size_t size
,
603 struct syscall_arg
*arg
);
605 #define SCA_FILENAME syscall_arg__scnprintf_filename
607 static size_t syscall_arg__scnprintf_pipe_flags(char *bf
, size_t size
,
608 struct syscall_arg
*arg
)
610 bool show_prefix
= arg
->show_string_prefix
;
611 const char *prefix
= "O_";
612 int printed
= 0, flags
= arg
->val
;
615 if (flags & O_##n) { \
616 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
625 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", flags
);
630 #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
632 #ifndef GRND_NONBLOCK
633 #define GRND_NONBLOCK 0x0001
636 #define GRND_RANDOM 0x0002
639 static size_t syscall_arg__scnprintf_getrandom_flags(char *bf
, size_t size
,
640 struct syscall_arg
*arg
)
642 bool show_prefix
= arg
->show_string_prefix
;
643 const char *prefix
= "GRND_";
644 int printed
= 0, flags
= arg
->val
;
647 if (flags & GRND_##n) { \
648 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
649 flags &= ~GRND_##n; \
657 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", flags
);
662 #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
664 #define STRARRAY(name, array) \
665 { .scnprintf = SCA_STRARRAY, \
666 .parm = &strarray__##array, }
668 #define STRARRAY_FLAGS(name, array) \
669 { .scnprintf = SCA_STRARRAY_FLAGS, \
670 .parm = &strarray__##array, }
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"
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
);
696 static struct syscall_fmt
{
700 const char *sys_enter
,
703 struct syscall_arg_fmt arg
[6];
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 */ }, }, },
715 .arg
= { [0] = { .scnprintf
= SCA_INT
, /* fd */ },
716 [1] = { .scnprintf
= SCA_SOCKADDR
, /* umyaddr */ },
717 [2] = { .scnprintf
= SCA_INT
, /* addrlen */ }, }, },
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
, }, }, },
731 .arg
= { [0] = { .scnprintf
= SCA_CLOSE_FD
, /* fd */ }, }, },
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 */ }, }, },
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 */ }, }, },
750 .arg
= { [1] = { .scnprintf
= SCA_FLOCK
, /* cmd */ }, }, },
751 { .name
= "fsconfig",
752 .arg
= { [1] = STRARRAY(cmd
, fsconfig_cmds
), }, },
754 .arg
= { [1] = STRARRAY_FLAGS(flags
, fsmount_flags
),
755 [2] = { .scnprintf
= SCA_FSMOUNT_ATTR_FLAGS
, /* attr_flags */ }, }, },
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", },
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, },
779 #if defined(__i386__) || defined(__x86_64__)
781 * FIXME: Make this available to all arches.
783 [1] = { .scnprintf
= SCA_IOCTL_CMD
, /* cmd */ },
784 [2] = { .scnprintf
= SCA_HEX
, /* arg */ }, }, },
786 [2] = { .scnprintf
= SCA_HEX
, /* arg */ }, }, },
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
, }, }, },
795 .arg
= { [0] = STRARRAY(option
, keyctl_options
), }, },
797 .arg
= { [1] = { .scnprintf
= SCA_SIGNUM
, /* sig */ }, }, },
799 .arg
= { [0] = { .scnprintf
= SCA_FDAT
, /* fd */ }, }, },
801 .arg
= { [2] = STRARRAY(whence
, whences
), }, },
802 { .name
= "lstat", .alias
= "newlstat", },
804 .arg
= { [0] = { .scnprintf
= SCA_HEX
, /* start */ },
805 [2] = { .scnprintf
= SCA_MADV_BHV
, /* behavior */ }, }, },
807 .arg
= { [0] = { .scnprintf
= SCA_FDAT
, /* fd */ }, }, },
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__)
815 .arg
= { [2] = { .scnprintf
= SCA_MMAP_PROT
, /* prot */ },
816 [3] = { .scnprintf
= SCA_MMAP_FLAGS
, /* flags */ },
817 [5] = { .scnprintf
= SCA_HEX
, /* offset */ }, }, },
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 */ }, }, },
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 */ }, }, },
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 */ }, }, },
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, },
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 */ }, }, },
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 */ }, }, },
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 */ }, }, },
905 .arg
= { [2] = { .scnprintf
= SCA_MSG_FLAGS
, /* flags */ }, }, },
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
), }, },
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", },
924 .arg
= { [0] = { .scnprintf
= SCA_FDAT
, /* fdat */ },
925 [2] = { .scnprintf
= SCA_STATX_FLAGS
, /* flags */ } ,
926 [3] = { .scnprintf
= SCA_STATX_MASK
, /* mask */ }, }, },
928 .arg
= { [0] = { .scnprintf
= SCA_FILENAME
, /* specialfile */ }, }, },
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 */ }, }, },
936 .arg
= { [2] = { .scnprintf
= SCA_SIGNUM
, /* sig */ }, }, },
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 */ }, }, },
952 static int syscall_fmt__cmp(const void *name
, const void *fmtp
)
954 const struct syscall_fmt
*fmt
= fmtp
;
955 return strcmp(name
, fmt
->name
);
958 static struct syscall_fmt
*syscall_fmt__find(const char *name
)
960 const int nmemb
= ARRAY_SIZE(syscall_fmts
);
961 return bsearch(name
, syscall_fmts
, nmemb
, sizeof(struct syscall_fmt
), syscall_fmt__cmp
);
964 static struct syscall_fmt
*syscall_fmt__find_by_alias(const char *alias
)
966 int i
, nmemb
= ARRAY_SIZE(syscall_fmts
);
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
];
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
983 struct tep_event
*tp_format
;
987 struct bpf_program
*sys_enter
,
993 struct tep_format_field
*args
;
995 struct syscall_fmt
*fmt
;
996 struct syscall_arg_fmt
*arg_fmt
;
1000 * Must match what is in the BPF program:
1002 * tools/perf/examples/bpf/augmented_raw_syscalls.c
1004 struct bpf_map_syscall_entry
{
1006 u16 string_args_len
[6];
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
1016 static size_t fprintf_duration(unsigned long t
, bool calculated
, FILE *fp
)
1018 double duration
= (double)t
/ NSEC_PER_MSEC
;
1019 size_t printed
= fprintf(fp
, "(");
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
);
1028 printed
+= color_fprintf(fp
, PERF_COLOR_NORMAL
, "%6.3f ms", duration
);
1029 return printed
+ fprintf(fp
, "): ");
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.
1039 struct thread_trace
{
1042 unsigned long nr_events
;
1043 unsigned long pfmaj
, pfmin
;
1046 size_t (*ret_scnprintf
)(char *bf
, size_t size
, struct syscall_arg
*arg
);
1049 short int entry_str_pos
;
1051 unsigned int namelen
;
1059 struct intlist
*syscall_stats
;
1062 static struct thread_trace
*thread_trace__new(void)
1064 struct thread_trace
*ttrace
= zalloc(sizeof(struct thread_trace
));
1067 ttrace
->files
.max
= -1;
1068 ttrace
->syscall_stats
= intlist__new(NULL
);
1074 static struct thread_trace
*thread__trace(struct thread
*thread
, FILE *fp
)
1076 struct thread_trace
*ttrace
;
1081 if (thread__priv(thread
) == NULL
)
1082 thread__set_priv(thread
, thread_trace__new());
1084 if (thread__priv(thread
) == NULL
)
1087 ttrace
= thread__priv(thread
);
1088 ++ttrace
->nr_events
;
1092 color_fprintf(fp
, PERF_COLOR_RED
,
1093 "WARNING: not enough memory, dropping samples!\n");
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
))
1101 struct thread_trace
*ttrace
= thread__priv(arg
->thread
);
1103 ttrace
->ret_scnprintf
= ret_scnprintf
;
1106 #define TRACE_PFMAJ (1 << 0)
1107 #define TRACE_PFMIN (1 << 1)
1109 static const size_t trace__entry_str_size
= 2048;
1111 static struct file
*thread_trace__files_entry(struct thread_trace
*ttrace
, int fd
)
1116 if (fd
> ttrace
->files
.max
) {
1117 struct file
*nfiles
= realloc(ttrace
->files
.table
, (fd
+ 1) * sizeof(struct file
));
1122 if (ttrace
->files
.max
!= -1) {
1123 memset(nfiles
+ ttrace
->files
.max
+ 1, 0,
1124 (fd
- ttrace
->files
.max
) * sizeof(struct file
));
1126 memset(nfiles
, 0, (fd
+ 1) * sizeof(struct file
));
1129 ttrace
->files
.table
= nfiles
;
1130 ttrace
->files
.max
= fd
;
1133 return ttrace
->files
.table
+ fd
;
1136 struct file
*thread__files_entry(struct thread
*thread
, int fd
)
1138 return thread_trace__files_entry(thread__priv(thread
), fd
);
1141 static int trace__set_fd_pathname(struct thread
*thread
, int fd
, const char *pathname
)
1143 struct thread_trace
*ttrace
= thread__priv(thread
);
1144 struct file
*file
= thread_trace__files_entry(ttrace
, fd
);
1148 if (stat(pathname
, &st
) == 0)
1149 file
->dev_maj
= major(st
.st_rdev
);
1150 file
->pathname
= strdup(pathname
);
1158 static int thread__read_fd_path(struct thread
*thread
, int fd
)
1160 char linkname
[PATH_MAX
], pathname
[PATH_MAX
];
1164 if (thread
->pid_
== thread
->tid
) {
1165 scnprintf(linkname
, sizeof(linkname
),
1166 "/proc/%d/fd/%d", thread
->pid_
, fd
);
1168 scnprintf(linkname
, sizeof(linkname
),
1169 "/proc/%d/task/%d/fd/%d", thread
->pid_
, thread
->tid
, fd
);
1172 if (lstat(linkname
, &st
) < 0 || st
.st_size
+ 1 > (off_t
)sizeof(pathname
))
1175 ret
= readlink(linkname
, pathname
, sizeof(pathname
));
1177 if (ret
< 0 || ret
> st
.st_size
)
1180 pathname
[ret
] = '\0';
1181 return trace__set_fd_pathname(thread
, fd
, pathname
);
1184 static const char *thread__fd_path(struct thread
*thread
, int fd
,
1185 struct trace
*trace
)
1187 struct thread_trace
*ttrace
= thread__priv(thread
);
1189 if (ttrace
== NULL
|| trace
->fd_path_disabled
)
1195 if ((fd
> ttrace
->files
.max
|| ttrace
->files
.table
[fd
].pathname
== NULL
)) {
1198 ++trace
->stats
.proc_getname
;
1199 if (thread__read_fd_path(thread
, fd
))
1203 return ttrace
->files
.table
[fd
].pathname
;
1206 size_t syscall_arg__scnprintf_fd(char *bf
, size_t size
, struct syscall_arg
*arg
)
1209 size_t printed
= scnprintf(bf
, size
, "%d", fd
);
1210 const char *path
= thread__fd_path(arg
->thread
, fd
, arg
->trace
);
1213 printed
+= scnprintf(bf
+ printed
, size
- printed
, "<%s>", path
);
1218 size_t pid__scnprintf_fd(struct trace
*trace
, pid_t pid
, int fd
, char *bf
, size_t size
)
1220 size_t printed
= scnprintf(bf
, size
, "%d", fd
);
1221 struct thread
*thread
= machine__find_thread(trace
->host
, pid
, pid
);
1224 const char *path
= thread__fd_path(thread
, fd
, trace
);
1227 printed
+= scnprintf(bf
+ printed
, size
- printed
, "<%s>", path
);
1229 thread__put(thread
);
1235 static size_t syscall_arg__scnprintf_close_fd(char *bf
, size_t size
,
1236 struct syscall_arg
*arg
)
1239 size_t printed
= syscall_arg__scnprintf_fd(bf
, size
, arg
);
1240 struct thread_trace
*ttrace
= thread__priv(arg
->thread
);
1242 if (ttrace
&& fd
>= 0 && fd
<= ttrace
->files
.max
)
1243 zfree(&ttrace
->files
.table
[fd
].pathname
);
1248 static void thread__set_filename_pos(struct thread
*thread
, const char *bf
,
1251 struct thread_trace
*ttrace
= thread__priv(thread
);
1253 ttrace
->filename
.ptr
= ptr
;
1254 ttrace
->filename
.entry_str_pos
= bf
- ttrace
->entry_str
;
1257 static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg
*arg
, char *bf
, size_t size
)
1259 struct augmented_arg
*augmented_arg
= arg
->augmented
.args
;
1260 size_t printed
= scnprintf(bf
, size
, "\"%.*s\"", augmented_arg
->size
, augmented_arg
->value
);
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.
1265 int consumed
= sizeof(*augmented_arg
) + augmented_arg
->size
;
1267 arg
->augmented
.args
= ((void *)arg
->augmented
.args
) + consumed
;
1268 arg
->augmented
.size
-= consumed
;
1273 static size_t syscall_arg__scnprintf_filename(char *bf
, size_t size
,
1274 struct syscall_arg
*arg
)
1276 unsigned long ptr
= arg
->val
;
1278 if (arg
->augmented
.args
)
1279 return syscall_arg__scnprintf_augmented_string(arg
, bf
, size
);
1281 if (!arg
->trace
->vfs_getname
)
1282 return scnprintf(bf
, size
, "%#x", ptr
);
1284 thread__set_filename_pos(arg
->thread
, bf
, ptr
);
1288 static bool trace__filter_duration(struct trace
*trace
, double t
)
1290 return t
< (trace
->duration_filter
* NSEC_PER_MSEC
);
1293 static size_t __trace__fprintf_tstamp(struct trace
*trace
, u64 tstamp
, FILE *fp
)
1295 double ts
= (double)(tstamp
- trace
->base_time
) / NSEC_PER_MSEC
;
1297 return fprintf(fp
, "%10.3f ", ts
);
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).
1306 static size_t trace__fprintf_tstamp(struct trace
*trace
, u64 tstamp
, FILE *fp
)
1309 return __trace__fprintf_tstamp(trace
, tstamp
, fp
);
1311 return fprintf(fp
, " ? ");
1314 static bool done
= false;
1315 static bool interrupted
= false;
1317 static void sig_handler(int sig
)
1320 interrupted
= sig
== SIGINT
;
1323 static size_t trace__fprintf_comm_tid(struct trace
*trace
, struct thread
*thread
, FILE *fp
)
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
);
1336 static size_t trace__fprintf_entry_head(struct trace
*trace
, struct thread
*thread
,
1337 u64 duration
, bool duration_calculated
, u64 tstamp
, FILE *fp
)
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
);
1348 static int trace__process_event(struct trace
*trace
, struct machine
*machine
,
1349 union perf_event
*event
, struct perf_sample
*sample
)
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
);
1360 ret
= machine__process_event(machine
, event
, sample
);
1367 static int trace__tool_process(struct perf_tool
*tool
,
1368 union perf_event
*event
,
1369 struct perf_sample
*sample
,
1370 struct machine
*machine
)
1372 struct trace
*trace
= container_of(tool
, struct trace
, tool
);
1373 return trace__process_event(trace
, machine
, event
, sample
);
1376 static char *trace__machine__resolve_kernel_addr(void *vmachine
, unsigned long long *addrp
, char **modp
)
1378 struct machine
*machine
= vmachine
;
1380 if (machine
->kptr_restrict_warned
)
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;
1391 return machine__resolve_kernel_addr(vmachine
, addrp
, modp
);
1394 static int trace__symbols_init(struct trace
*trace
, struct evlist
*evlist
)
1396 int err
= symbol__init(NULL
);
1401 trace
->host
= machine__new_host();
1402 if (trace
->host
== NULL
)
1405 err
= trace_event__register_resolver(trace
->host
, trace__machine__resolve_kernel_addr
);
1409 err
= __machine__synthesize_threads(trace
->host
, &trace
->tool
, &trace
->opts
.target
,
1410 evlist
->core
.threads
, trace__tool_process
, false,
1419 static void trace__symbols__exit(struct trace
*trace
)
1421 machine__exit(trace
->host
);
1427 static int syscall__alloc_arg_fmts(struct syscall
*sc
, int nr_args
)
1431 if (nr_args
== 6 && sc
->fmt
&& sc
->fmt
->nr_args
!= 0)
1432 nr_args
= sc
->fmt
->nr_args
;
1434 sc
->arg_fmt
= calloc(nr_args
, sizeof(*sc
->arg_fmt
));
1435 if (sc
->arg_fmt
== NULL
)
1438 for (idx
= 0; idx
< nr_args
; ++idx
) {
1440 sc
->arg_fmt
[idx
] = sc
->fmt
->arg
[idx
];
1443 sc
->nr_args
= nr_args
;
1447 static int syscall__set_arg_fmts(struct syscall
*sc
)
1449 struct tep_format_field
*field
, *last_field
= NULL
;
1452 for (field
= sc
->args
; field
; field
= field
->next
, ++idx
) {
1455 if (sc
->fmt
&& sc
->fmt
->arg
[idx
].scnprintf
)
1458 len
= strlen(field
->name
);
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) {
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
1481 sc
->arg_fmt
[idx
].scnprintf
= SCA_FD
;
1486 sc
->args_size
= last_field
->offset
+ last_field
->size
;
1491 static int trace__read_syscall_info(struct trace
*trace
, int id
)
1495 const char *name
= syscalltbl__name(trace
->sctbl
, id
);
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
)
1503 sc
= trace
->syscalls
.table
+ id
;
1504 if (sc
->nonexistent
)
1508 sc
->nonexistent
= true;
1513 sc
->fmt
= syscall_fmt__find(sc
->name
);
1515 snprintf(tp_name
, sizeof(tp_name
), "sys_enter_%s", sc
->name
);
1516 sc
->tp_format
= trace_event__tp_format("syscalls", tp_name
);
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
);
1523 if (syscall__alloc_arg_fmts(sc
, IS_ERR(sc
->tp_format
) ? 6 : sc
->tp_format
->format
.nr_fields
))
1526 if (IS_ERR(sc
->tp_format
))
1527 return PTR_ERR(sc
->tp_format
);
1529 sc
->args
= sc
->tp_format
->format
.fields
;
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.
1535 if (sc
->args
&& (!strcmp(sc
->args
->name
, "__syscall_nr") || !strcmp(sc
->args
->name
, "nr"))) {
1536 sc
->args
= sc
->args
->next
;
1540 sc
->is_exit
= !strcmp(name
, "exit_group") || !strcmp(name
, "exit");
1541 sc
->is_open
= !strcmp(name
, "open") || !strcmp(name
, "openat");
1543 return syscall__set_arg_fmts(sc
);
1546 static int intcmp(const void *a
, const void *b
)
1548 const int *one
= a
, *another
= b
;
1550 return *one
- *another
;
1553 static int trace__validate_ev_qualifier(struct trace
*trace
)
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
);
1560 trace
->ev_qualifier_ids
.entries
= malloc(nr_allocated
*
1561 sizeof(trace
->ev_qualifier_ids
.entries
[0]));
1563 if (trace
->ev_qualifier_ids
.entries
== NULL
) {
1564 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
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;
1575 id
= syscalltbl__strglobmatch_first(trace
->sctbl
, sc
, &match_next
);
1579 if (!printed_invalid_prefix
) {
1580 pr_debug("Skipping unknown syscalls: ");
1581 printed_invalid_prefix
= true;
1590 trace
->ev_qualifier_ids
.entries
[nr_used
++] = id
;
1591 if (match_next
== -1)
1595 id
= syscalltbl__strglobmatch_next(trace
->sctbl
, sc
, &match_next
);
1598 if (nr_allocated
== nr_used
) {
1602 entries
= realloc(trace
->ev_qualifier_ids
.entries
,
1603 nr_allocated
* sizeof(trace
->ev_qualifier_ids
.entries
[0]));
1604 if (entries
== NULL
) {
1606 fputs("\nError:\t Not enough memory for parsing\n", trace
->output
);
1609 trace
->ev_qualifier_ids
.entries
= entries
;
1611 trace
->ev_qualifier_ids
.entries
[nr_used
++] = id
;
1615 trace
->ev_qualifier_ids
.nr
= nr_used
;
1616 qsort(trace
->ev_qualifier_ids
.entries
, nr_used
, sizeof(int), intcmp
);
1618 if (printed_invalid_prefix
)
1622 zfree(&trace
->ev_qualifier_ids
.entries
);
1623 trace
->ev_qualifier_ids
.nr
= 0;
1627 static __maybe_unused
bool trace__syscall_enabled(struct trace
*trace
, int id
)
1629 bool in_ev_qualifier
;
1631 if (trace
->ev_qualifier_ids
.nr
== 0)
1634 in_ev_qualifier
= bsearch(&id
, trace
->ev_qualifier_ids
.entries
,
1635 trace
->ev_qualifier_ids
.nr
, sizeof(int), intcmp
) != NULL
;
1637 if (in_ev_qualifier
)
1638 return !trace
->not_ev_qualifier
;
1640 return trace
->not_ev_qualifier
;
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
1651 unsigned long syscall_arg__val(struct syscall_arg
*arg
, u8 idx
)
1654 unsigned char *p
= arg
->args
+ sizeof(unsigned long) * idx
;
1656 memcpy(&val
, p
, sizeof(val
));
1660 static size_t syscall__scnprintf_name(struct syscall
*sc
, char *bf
, size_t size
,
1661 struct syscall_arg
*arg
)
1663 if (sc
->arg_fmt
&& sc
->arg_fmt
[arg
->idx
].name
)
1664 return scnprintf(bf
, size
, "%s: ", sc
->arg_fmt
[arg
->idx
].name
);
1666 return scnprintf(bf
, size
, "arg%d: ", arg
->idx
);
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
1674 static unsigned long syscall__mask_val(struct syscall
*sc
, struct syscall_arg
*arg
, unsigned long val
)
1676 if (sc
->arg_fmt
&& sc
->arg_fmt
[arg
->idx
].mask_val
)
1677 return sc
->arg_fmt
[arg
->idx
].mask_val(arg
, val
);
1682 static size_t syscall__scnprintf_val(struct syscall
*sc
, char *bf
, size_t size
,
1683 struct syscall_arg
*arg
, unsigned long val
)
1685 if (sc
->arg_fmt
&& sc
->arg_fmt
[arg
->idx
].scnprintf
) {
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
);
1691 return scnprintf(bf
, size
, "%ld", val
);
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
)
1701 struct syscall_arg arg
= {
1704 .size
= augmented_args_size
,
1705 .args
= augmented_args
,
1711 .show_string_prefix
= trace
->show_string_prefix
,
1713 struct thread_trace
*ttrace
= thread__priv(thread
);
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.
1720 ttrace
->ret_scnprintf
= NULL
;
1722 if (sc
->args
!= NULL
) {
1723 struct tep_format_field
*field
;
1725 for (field
= sc
->args
; field
;
1726 field
= field
->next
, ++arg
.idx
, bit
<<= 1) {
1730 val
= syscall_arg__val(&arg
, arg
.idx
);
1732 * Some syscall args need some mask, most don't and
1733 * return val untouched.
1735 val
= syscall__mask_val(sc
, &arg
, val
);
1738 * Suppress this argument if its value is zero and
1739 * and we don't have a string associated in an
1743 !trace
->show_zeros
&&
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
))
1751 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s", printed
? ", " : "");
1753 if (trace
->show_arg_names
)
1754 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s: ", field
->name
);
1756 printed
+= syscall__scnprintf_val(sc
, bf
+ printed
, size
- printed
, &arg
, val
);
1758 } else if (IS_ERR(sc
->tp_format
)) {
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.
1764 while (arg
.idx
< sc
->nr_args
) {
1767 val
= syscall_arg__val(&arg
, arg
.idx
);
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
);
1781 typedef int (*tracepoint_handler
)(struct trace
*trace
, struct evsel
*evsel
,
1782 union perf_event
*event
,
1783 struct perf_sample
*sample
);
1785 static struct syscall
*trace__syscall_info(struct trace
*trace
,
1786 struct evsel
*evsel
, int id
)
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:
1797 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1798 * grep "NR -1 " /t/trace_pipe
1800 * After generating some load on the machine.
1804 fprintf(trace
->output
, "Invalid syscall %d id, skipping (%s, %" PRIu64
") ...\n",
1805 id
, perf_evsel__name(evsel
), ++n
);
1812 if (id
> trace
->sctbl
->syscalls
.max_id
)
1815 if ((trace
->syscalls
.table
== NULL
|| trace
->syscalls
.table
[id
].name
== NULL
) &&
1816 (err
= trace__read_syscall_info(trace
, id
)) != 0)
1819 if (trace
->syscalls
.table
[id
].name
== NULL
) {
1820 if (trace
->syscalls
.table
[id
].nonexistent
)
1825 return &trace
->syscalls
.table
[id
];
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
);
1838 static void thread__update_stats(struct thread_trace
*ttrace
,
1839 int id
, struct perf_sample
*sample
)
1841 struct int_node
*inode
;
1842 struct stats
*stats
;
1845 inode
= intlist__findnew(ttrace
->syscall_stats
, id
);
1849 stats
= inode
->priv
;
1850 if (stats
== NULL
) {
1851 stats
= malloc(sizeof(struct stats
));
1855 inode
->priv
= stats
;
1858 if (ttrace
->entry_time
&& sample
->time
> ttrace
->entry_time
)
1859 duration
= sample
->time
- ttrace
->entry_time
;
1861 update_stats(stats
, duration
);
1864 static int trace__printf_interrupted_entry(struct trace
*trace
)
1866 struct thread_trace
*ttrace
;
1870 if (trace
->failure_only
|| trace
->current
== NULL
)
1873 ttrace
= thread__priv(trace
->current
);
1875 if (!ttrace
->entry_pending
)
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
);
1881 if (len
< trace
->args_alignment
- 4)
1882 printed
+= fprintf(trace
->output
, "%-*s", trace
->args_alignment
- 4 - len
, " ");
1884 printed
+= fprintf(trace
->output
, " ...\n");
1886 ttrace
->entry_pending
= false;
1887 ++trace
->nr_events_printed
;
1892 static int trace__fprintf_sample(struct trace
*trace
, struct evsel
*evsel
,
1893 struct perf_sample
*sample
, struct thread
*thread
)
1897 if (trace
->print_sample
) {
1898 double ts
= (double)sample
->time
/ NSEC_PER_MSEC
;
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
);
1909 static void *syscall__augmented_args(struct syscall
*sc
, struct perf_sample
*sample
, int *augmented_args_size
, int raw_augmented_args_size
)
1911 void *augmented_args
= NULL
;
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,
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.
1926 int args_size
= raw_augmented_args_size
?: sc
->args_size
;
1928 *augmented_args_size
= sample
->raw_size
- args_size
;
1929 if (*augmented_args_size
> 0)
1930 augmented_args
= sample
->raw_data
+ args_size
;
1932 return augmented_args
;
1935 static int trace__sys_enter(struct trace
*trace
, struct evsel
*evsel
,
1936 union perf_event
*event __maybe_unused
,
1937 struct perf_sample
*sample
)
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
;
1952 thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
1953 ttrace
= thread__trace(thread
, trace
->output
);
1957 trace__fprintf_sample(trace
, evsel
, sample
, thread
);
1959 args
= perf_evsel__sc_tp_ptr(evsel
, args
, sample
);
1961 if (ttrace
->entry_str
== NULL
) {
1962 ttrace
->entry_str
= malloc(trace__entry_str_size
);
1963 if (!ttrace
->entry_str
)
1967 if (!(trace
->duration_filter
|| trace
->summary_only
|| trace
->min_stack
))
1968 trace__printf_interrupted_entry(trace
);
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.
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
);
1985 printed
+= syscall__scnprintf_args(sc
, msg
+ printed
, trace__entry_str_size
- printed
,
1986 args
, augmented_args
, augmented_args_size
, trace
, thread
);
1989 if (!(trace
->duration_filter
|| trace
->summary_only
|| trace
->failure_only
|| trace
->min_stack
)) {
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
, " ");
1999 ttrace
->entry_pending
= true;
2000 /* See trace__vfs_getname & trace__sys_exit */
2001 ttrace
->filename
.pending_open
= false;
2004 if (trace
->current
!= thread
) {
2005 thread__put(trace
->current
);
2006 trace
->current
= thread__get(thread
);
2010 thread__put(thread
);
2014 static int trace__fprintf_sys_enter(struct trace
*trace
, struct evsel
*evsel
,
2015 struct perf_sample
*sample
)
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
);
2022 void *args
, *augmented_args
= NULL
;
2023 int augmented_args_size
;
2028 thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
2029 ttrace
= thread__trace(thread
, trace
->output
);
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.
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
);
2043 thread__put(thread
);
2047 static int trace__resolve_callchain(struct trace
*trace
, struct evsel
*evsel
,
2048 struct perf_sample
*sample
,
2049 struct callchain_cursor
*cursor
)
2051 struct addr_location al
;
2052 int max_stack
= evsel
->core
.attr
.sample_max_stack
?
2053 evsel
->core
.attr
.sample_max_stack
:
2057 if (machine__resolve(trace
->host
, &al
, sample
) < 0)
2060 err
= thread__resolve_callchain(al
.thread
, cursor
, evsel
, sample
, NULL
, NULL
, max_stack
);
2061 addr_location__put(&al
);
2065 static int trace__fprintf_callchain(struct trace
*trace
, struct perf_sample
*sample
)
2067 /* TODO: user-configurable print_opts */
2068 const unsigned int print_opts
= EVSEL__PRINT_SYM
|
2070 EVSEL__PRINT_UNKNOWN_AS_ADDR
;
2072 return sample__fprintf_callchain(sample
, 38, print_opts
, &callchain_cursor
, trace
->output
);
2075 static const char *errno_to_name(struct evsel
*evsel
, int err
)
2077 struct perf_env
*env
= perf_evsel__env(evsel
);
2078 const char *arch_name
= perf_env__arch(env
);
2080 return arch_syscalls__strerrno(arch_name
, err
);
2083 static int trace__sys_exit(struct trace
*trace
, struct evsel
*evsel
,
2084 union perf_event
*event __maybe_unused
,
2085 struct perf_sample
*sample
)
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
;
2099 thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
2100 ttrace
= thread__trace(thread
, trace
->output
);
2104 trace__fprintf_sample(trace
, evsel
, sample
, thread
);
2107 thread__update_stats(ttrace
, id
, sample
);
2109 ret
= perf_evsel__sc_tp_uint(evsel
, ret
, sample
);
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
;
2117 if (ttrace
->entry_time
) {
2118 duration
= sample
->time
- ttrace
->entry_time
;
2119 if (trace__filter_duration(trace
, duration
))
2121 duration_calculated
= true;
2122 } else if (trace
->duration_filter
)
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
)
2134 if (trace
->summary_only
|| (ret
>= 0 && trace
->failure_only
))
2137 trace__fprintf_entry_head(trace
, thread
, duration
, duration_calculated
, ttrace
->entry_time
, trace
->output
);
2139 if (ttrace
->entry_pending
) {
2140 printed
= fprintf(trace
->output
, "%s", ttrace
->entry_str
);
2142 printed
+= fprintf(trace
->output
, " ... [");
2143 color_fprintf(trace
->output
, PERF_COLOR_YELLOW
, "continued");
2145 printed
+= fprintf(trace
->output
, "]: %s()", sc
->name
);
2148 printed
++; /* the closing ')' */
2150 if (alignment
> printed
)
2151 alignment
-= printed
;
2155 fprintf(trace
->output
, ")%*s= ", alignment
, " ");
2157 if (sc
->fmt
== NULL
) {
2161 fprintf(trace
->output
, "%ld", ret
);
2162 } else if (ret
< 0) {
2164 char bf
[STRERR_BUFSIZE
];
2165 const char *emsg
= str_error_r(-ret
, bf
, sizeof(bf
)),
2166 *e
= errno_to_name(evsel
, -ret
);
2168 fprintf(trace
->output
, "-1 %s (%s)", e
, emsg
);
2170 } else if (ret
== 0 && sc
->fmt
->timeout
)
2171 fprintf(trace
->output
, "0 (Timeout)");
2172 else if (ttrace
->ret_scnprintf
) {
2174 struct syscall_arg arg
= {
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
);
2187 if (child
!= NULL
) {
2188 fprintf(trace
->output
, "%ld", ret
);
2189 if (child
->comm_set
)
2190 fprintf(trace
->output
, " (%s)", thread__comm_str(child
));
2196 fputc('\n', trace
->output
);
2199 * We only consider an 'event' for the sake of --max-events a non-filtered
2200 * sys_enter + sys_exit and other tracepoint events.
2202 if (++trace
->nr_events_printed
== trace
->max_events
&& trace
->max_events
!= ULONG_MAX
)
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
));
2210 ttrace
->entry_pending
= false;
2213 thread__put(thread
);
2217 static int trace__vfs_getname(struct trace
*trace
, struct evsel
*evsel
,
2218 union perf_event
*event __maybe_unused
,
2219 struct perf_sample
*sample
)
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
;
2226 const char *filename
= perf_evsel__rawptr(evsel
, sample
, "pathname");
2231 ttrace
= thread__priv(thread
);
2235 filename_len
= strlen(filename
);
2236 if (filename_len
== 0)
2239 if (ttrace
->filename
.namelen
< filename_len
) {
2240 char *f
= realloc(ttrace
->filename
.name
, filename_len
+ 1);
2245 ttrace
->filename
.namelen
= filename_len
;
2246 ttrace
->filename
.name
= f
;
2249 strcpy(ttrace
->filename
.name
, filename
);
2250 ttrace
->filename
.pending_open
= true;
2252 if (!ttrace
->filename
.ptr
)
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)
2260 if (filename_len
> (size_t)remaining_space
) {
2261 filename
+= filename_len
- remaining_space
;
2262 filename_len
= remaining_space
;
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
);
2270 ttrace
->filename
.ptr
= 0;
2271 ttrace
->filename
.entry_str_pos
= 0;
2273 thread__put(thread
);
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
)
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
,
2287 struct thread_trace
*ttrace
= thread__trace(thread
, trace
->output
);
2292 ttrace
->runtime_ms
+= runtime_ms
;
2293 trace
->runtime_ms
+= runtime_ms
;
2295 thread__put(thread
);
2299 fprintf(trace
->output
, "%s: comm=%s,pid=%u,runtime=%" PRIu64
",vruntime=%" PRIu64
")\n",
2301 perf_evsel__strval(evsel
, sample
, "comm"),
2302 (pid_t
)perf_evsel__intval(evsel
, sample
, "pid"),
2304 perf_evsel__intval(evsel
, sample
, "vruntime"));
2308 static int bpf_output__printer(enum binary_printer_ops op
,
2309 unsigned int val
, void *extra __maybe_unused
, FILE *fp
)
2311 unsigned char ch
= (unsigned char)val
;
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
:
2332 static void bpf_output__fprintf(struct trace
*trace
,
2333 struct perf_sample
*sample
)
2335 binary__fprintf(sample
->raw_data
, sample
->raw_size
, 8,
2336 bpf_output__printer
, NULL
, trace
->output
);
2337 ++trace
->nr_events_printed
;
2340 static int trace__event_handler(struct trace
*trace
, struct evsel
*evsel
,
2341 union perf_event
*event __maybe_unused
,
2342 struct perf_sample
*sample
)
2344 struct thread
*thread
;
2345 int callchain_ret
= 0;
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.
2352 if (evsel
->disabled
)
2355 thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
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
)
2366 trace__printf_interrupted_entry(trace
);
2367 trace__fprintf_tstamp(trace
, sample
->time
, trace
->output
);
2369 if (trace
->trace_syscalls
&& trace
->show_duration
)
2370 fprintf(trace
->output
, "( ): ");
2373 trace__fprintf_comm_tid(trace
, thread
, trace
->output
);
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
);
2380 fprintf(trace
->output
, "%s(", sc
->name
);
2381 trace__fprintf_sys_enter(trace
, evsel
, sample
);
2382 fputc(')', trace
->output
);
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.
2393 fprintf(trace
->output
, "%s:", evsel
->name
);
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
,
2403 ++trace
->nr_events_printed
;
2405 if (evsel
->max_events
!= ULONG_MAX
&& ++evsel
->nr_events_printed
== evsel
->max_events
) {
2406 evsel__disable(evsel
);
2407 evsel__close(evsel
);
2413 fprintf(trace
->output
, "\n");
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
));
2420 thread__put(thread
);
2424 static void print_location(FILE *f
, struct perf_sample
*sample
,
2425 struct addr_location
*al
,
2426 bool print_dso
, bool print_sym
)
2429 if ((verbose
> 0 || print_dso
) && al
->map
)
2430 fprintf(f
, "%s@", al
->map
->dso
->long_name
);
2432 if ((verbose
> 0 || print_sym
) && al
->sym
)
2433 fprintf(f
, "%s+0x%" PRIx64
, al
->sym
->name
,
2434 al
->addr
- al
->sym
->start
);
2436 fprintf(f
, "0x%" PRIx64
, al
->addr
);
2438 fprintf(f
, "0x%" PRIx64
, sample
->addr
);
2441 static int trace__pgfault(struct trace
*trace
,
2442 struct evsel
*evsel
,
2443 union perf_event
*event __maybe_unused
,
2444 struct perf_sample
*sample
)
2446 struct thread
*thread
;
2447 struct addr_location al
;
2448 char map_type
= 'd';
2449 struct thread_trace
*ttrace
;
2451 int callchain_ret
= 0;
2453 thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
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
)
2464 ttrace
= thread__trace(thread
, trace
->output
);
2468 if (evsel
->core
.attr
.config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
)
2473 if (trace
->summary_only
)
2476 thread__find_symbol(thread
, sample
->cpumode
, sample
->ip
, &al
);
2478 trace__fprintf_entry_head(trace
, thread
, 0, true, sample
->time
, trace
->output
);
2480 fprintf(trace
->output
, "%sfault [",
2481 evsel
->core
.attr
.config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
?
2484 print_location(trace
->output
, sample
, &al
, false, true);
2486 fprintf(trace
->output
, "] => ");
2488 thread__find_symbol(thread
, sample
->cpumode
, sample
->addr
, &al
);
2491 thread__find_symbol(thread
, sample
->cpumode
, sample
->addr
, &al
);
2499 print_location(trace
->output
, sample
, &al
, true, false);
2501 fprintf(trace
->output
, " (%c%c)\n", map_type
, al
.level
);
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
));
2508 ++trace
->nr_events_printed
;
2512 thread__put(thread
);
2516 static void trace__set_base_time(struct trace
*trace
,
2517 struct evsel
*evsel
,
2518 struct perf_sample
*sample
)
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).
2528 if (trace
->base_time
== 0 && !trace
->full_time
&&
2529 (evsel
->core
.attr
.sample_type
& PERF_SAMPLE_TIME
))
2530 trace
->base_time
= sample
->time
;
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
)
2539 struct trace
*trace
= container_of(tool
, struct trace
, tool
);
2540 struct thread
*thread
;
2543 tracepoint_handler handler
= evsel
->handler
;
2545 thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
2546 if (thread
&& thread__is_filtered(thread
))
2549 trace__set_base_time(trace
, evsel
, sample
);
2553 handler(trace
, evsel
, event
, sample
);
2556 thread__put(thread
);
2560 static int trace__record(struct trace
*trace
, int argc
, const char **argv
)
2562 unsigned int rec_argc
, i
, j
;
2563 const char **rec_argv
;
2564 const char * const record_args
[] = {
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
);
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 *));
2583 if (rec_argv
== NULL
)
2587 for (i
= 0; i
< ARRAY_SIZE(record_args
); i
++)
2588 rec_argv
[j
++] = record_args
[i
];
2590 if (trace
->trace_syscalls
) {
2591 for (i
= 0; i
< sc_args_nr
; i
++)
2592 rec_argv
[j
++] = sc_args
[i
];
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";
2600 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
2606 if (trace
->trace_pgfaults
& TRACE_PFMAJ
)
2607 for (i
= 0; i
< majpf_args_nr
; i
++)
2608 rec_argv
[j
++] = majpf_args
[i
];
2610 if (trace
->trace_pgfaults
& TRACE_PFMIN
)
2611 for (i
= 0; i
< minpf_args_nr
; i
++)
2612 rec_argv
[j
++] = minpf_args
[i
];
2614 for (i
= 0; i
< (unsigned int)argc
; i
++)
2615 rec_argv
[j
++] = argv
[i
];
2617 return cmd_record(j
, rec_argv
);
2620 static size_t trace__fprintf_thread_summary(struct trace
*trace
, FILE *fp
);
2622 static bool evlist__add_vfs_getname(struct evlist
*evlist
)
2625 struct evsel
*evsel
, *tmp
;
2626 struct parse_events_error err
= { .idx
= 0, };
2627 int ret
= parse_events(evlist
, "probe:vfs_getname*", &err
);
2632 evlist__for_each_entry_safe(evlist
, evsel
, tmp
) {
2633 if (!strstarts(perf_evsel__name(evsel
), "probe:vfs_getname"))
2636 if (perf_evsel__field(evsel
, "pathname")) {
2637 evsel
->handler
= trace__vfs_getname
;
2642 list_del_init(&evsel
->core
.node
);
2643 evsel
->evlist
= NULL
;
2644 evsel__delete(evsel
);
2650 static struct evsel
*perf_evsel__new_pgfault(u64 config
)
2652 struct evsel
*evsel
;
2653 struct perf_event_attr attr
= {
2654 .type
= PERF_TYPE_SOFTWARE
,
2658 attr
.config
= config
;
2659 attr
.sample_period
= 1;
2661 event_attr_init(&attr
);
2663 evsel
= evsel__new(&attr
);
2665 evsel
->handler
= trace__pgfault
;
2670 static void trace__handle_event(struct trace
*trace
, union perf_event
*event
, struct perf_sample
*sample
)
2672 const u32 type
= event
->header
.type
;
2673 struct evsel
*evsel
;
2675 if (type
!= PERF_RECORD_SAMPLE
) {
2676 trace__process_event(trace
, trace
->host
, event
, sample
);
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
);
2686 if (evswitch__discard(&trace
->evswitch
, evsel
))
2689 trace__set_base_time(trace
, evsel
, sample
);
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
);
2697 tracepoint_handler handler
= evsel
->handler
;
2698 handler(trace
, evsel
, event
, sample
);
2701 if (trace
->nr_events_printed
>= trace
->max_events
&& trace
->max_events
!= ULONG_MAX
)
2705 static int trace__add_syscall_newtp(struct trace
*trace
)
2708 struct evlist
*evlist
= trace
->evlist
;
2709 struct evsel
*sys_enter
, *sys_exit
;
2711 sys_enter
= perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter
);
2712 if (sys_enter
== NULL
)
2715 if (perf_evsel__init_sc_tp_ptr_field(sys_enter
, args
))
2716 goto out_delete_sys_enter
;
2718 sys_exit
= perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit
);
2719 if (sys_exit
== NULL
)
2720 goto out_delete_sys_enter
;
2722 if (perf_evsel__init_sc_tp_uint_field(sys_exit
, ret
))
2723 goto out_delete_sys_exit
;
2725 perf_evsel__config_callchain(sys_enter
, &trace
->opts
, &callchain_param
);
2726 perf_evsel__config_callchain(sys_exit
, &trace
->opts
, &callchain_param
);
2728 evlist__add(evlist
, sys_enter
);
2729 evlist__add(evlist
, sys_exit
);
2731 if (callchain_param
.enabled
&& !trace
->kernel_syscallchains
) {
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
2737 sys_exit
->core
.attr
.exclude_callchain_kernel
= 1;
2740 trace
->syscalls
.events
.sys_enter
= sys_enter
;
2741 trace
->syscalls
.events
.sys_exit
= sys_exit
;
2747 out_delete_sys_exit
:
2748 evsel__delete_priv(sys_exit
);
2749 out_delete_sys_enter
:
2750 evsel__delete_priv(sys_enter
);
2754 static int trace__set_ev_qualifier_tp_filter(struct trace
*trace
)
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
);
2765 if (!perf_evsel__append_tp_filter(trace
->syscalls
.events
.sys_enter
,
2767 sys_exit
= trace
->syscalls
.events
.sys_exit
;
2768 err
= perf_evsel__append_tp_filter(sys_exit
, filter
);
2779 #ifdef HAVE_LIBBPF_SUPPORT
2780 static struct bpf_program
*trace__find_bpf_program_by_title(struct trace
*trace
, const char *name
)
2782 if (trace
->bpf_obj
== NULL
)
2785 return bpf_object__find_program_by_title(trace
->bpf_obj
, name
);
2788 static struct bpf_program
*trace__find_syscall_bpf_prog(struct trace
*trace
, struct syscall
*sc
,
2789 const char *prog_name
, const char *type
)
2791 struct bpf_program
*prog
;
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
);
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
);
2805 goto out_unaugmented
;
2808 prog
= trace__find_bpf_program_by_title(trace
, prog_name
);
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
);
2818 return trace
->syscalls
.unaugmented_prog
;
2821 static void trace__init_syscall_bpf_progs(struct trace
*trace
, int id
)
2823 struct syscall
*sc
= trace__syscall_info(trace
, NULL
, id
);
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");
2832 static int trace__bpf_prog_sys_enter_fd(struct trace
*trace
, int id
)
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
);
2838 static int trace__bpf_prog_sys_exit_fd(struct trace
*trace
, int id
)
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
);
2844 static void trace__init_bpf_map_syscall_args(struct trace
*trace
, int id
, struct bpf_map_syscall_entry
*entry
)
2846 struct syscall
*sc
= trace__syscall_info(trace
, NULL
, id
);
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
;
2860 for (; arg
< 6; ++arg
)
2861 entry
->string_args_len
[arg
] = 0;
2863 static int trace__set_ev_qualifier_bpf_filter(struct trace
*trace
)
2865 int fd
= bpf_map__fd(trace
->syscalls
.map
);
2866 struct bpf_map_syscall_entry value
= {
2867 .enabled
= !trace
->not_ev_qualifier
,
2872 for (i
= 0; i
< trace
->ev_qualifier_ids
.nr
; ++i
) {
2873 int key
= trace
->ev_qualifier_ids
.entries
[i
];
2875 if (value
.enabled
) {
2876 trace__init_bpf_map_syscall_args(trace
, key
, &value
);
2877 trace__init_syscall_bpf_progs(trace
, key
);
2880 err
= bpf_map_update_elem(fd
, &key
, &value
, BPF_EXIST
);
2888 static int __trace__init_syscalls_bpf_map(struct trace
*trace
, bool enabled
)
2890 int fd
= bpf_map__fd(trace
->syscalls
.map
);
2891 struct bpf_map_syscall_entry value
= {
2896 for (key
= 0; key
< trace
->sctbl
->syscalls
.nr_entries
; ++key
) {
2898 trace__init_bpf_map_syscall_args(trace
, key
, &value
);
2900 err
= bpf_map_update_elem(fd
, &key
, &value
, BPF_ANY
);
2908 static int trace__init_syscalls_bpf_map(struct trace
*trace
)
2910 bool enabled
= true;
2912 if (trace
->ev_qualifier_ids
.nr
)
2913 enabled
= trace
->not_ev_qualifier
;
2915 return __trace__init_syscalls_bpf_map(trace
, enabled
);
2918 static struct bpf_program
*trace__find_usable_bpf_prog_entry(struct trace
*trace
, struct syscall
*sc
)
2920 struct tep_format_field
*field
, *candidate_field
;
2924 * We're only interested in syscalls that have a pointer:
2926 for (field
= sc
->args
; field
; field
= field
->next
) {
2927 if (field
->flags
& TEP_FIELD_IS_POINTER
)
2928 goto 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;
2939 if (pair
== NULL
|| pair
== sc
||
2940 pair
->bpf_prog
.sys_enter
== trace
->syscalls
.unaugmented_prog
)
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
;
2949 if (!candidate_is_pointer
) {
2950 // The candidate just doesn't copies our pointer arg, might copy other pointers we want.
2954 if (candidate_is_pointer
) {
2955 // The candidate might copy a pointer we don't have, skip it.
2956 goto next_candidate
;
2961 if (strcmp(field
->type
, candidate_field
->type
))
2962 goto next_candidate
;
2964 is_candidate
= true;
2968 goto next_candidate
;
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.
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
;
2981 pair_prog
= pair
->bpf_prog
.sys_enter
;
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.
2988 * For instance, we have "!syscalls:sys_enter_renameat" and that is
2989 * useful for "renameat2".
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
;
2997 pr_debug("Reusing \"%s\" BPF sys_enter augmenter for \"%s\"\n", pair
->name
, sc
->name
);
3006 static int trace__init_syscalls_bpf_prog_array_maps(struct trace
*trace
)
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
);
3012 for (key
= 0; key
< trace
->sctbl
->syscalls
.nr_entries
; ++key
) {
3015 if (!trace__syscall_enabled(trace
, key
))
3018 trace__init_syscall_bpf_progs(trace
, key
);
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
);
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
);
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.
3036 * I.e. if we have an augmenter for the "open" syscall that has
3039 * int open(const char *pathname, int flags, mode_t mode);
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:
3044 * int creat(const char *pathname, mode_t mode);
3048 * int stat(const char *pathname, struct stat *statbuf);
3049 * int lstat(const char *pathname, struct stat *statbuf);
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.
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.
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
;
3064 if (sc
== NULL
|| sc
->bpf_prog
.sys_enter
== NULL
)
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.
3071 if (sc
->bpf_prog
.sys_enter
!= trace
->syscalls
.unaugmented_prog
)
3075 * Look at all the other syscalls for one that has a signature
3076 * that is close enough that we can share:
3078 pair_prog
= trace__find_usable_bpf_prog_entry(trace
, sc
);
3079 if (pair_prog
== NULL
)
3082 sc
->bpf_prog
.sys_enter
= pair_prog
;
3085 * Update the BPF_MAP_TYPE_PROG_SHARED for raw_syscalls:sys_enter
3086 * with the fd for the program we're reusing:
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
);
3098 static int trace__set_ev_qualifier_bpf_filter(struct trace
*trace __maybe_unused
)
3103 static int trace__init_syscalls_bpf_map(struct trace
*trace __maybe_unused
)
3108 static struct bpf_program
*trace__find_bpf_program_by_title(struct trace
*trace __maybe_unused
,
3109 const char *name __maybe_unused
)
3114 static int trace__init_syscalls_bpf_prog_array_maps(struct trace
*trace __maybe_unused
)
3118 #endif // HAVE_LIBBPF_SUPPORT
3120 static int trace__set_ev_qualifier_filter(struct trace
*trace
)
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
);
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
)
3133 #ifdef HAVE_LIBBPF_SUPPORT
3135 int map_fd
= bpf_map__fd(map
);
3138 for (i
= 0; i
< npids
; ++i
) {
3139 err
= bpf_map_update_elem(map_fd
, &pids
[i
], &value
, BPF_ANY
);
3147 static int trace__set_filter_loop_pids(struct trace
*trace
)
3149 unsigned int nr
= 1, err
;
3153 struct thread
*thread
= machine__find_thread(trace
->host
, pids
[0], pids
[0]);
3155 while (thread
&& nr
< ARRAY_SIZE(pids
)) {
3156 struct thread
*parent
= machine__find_thread(trace
->host
, thread
->ppid
, thread
->ppid
);
3161 if (!strcmp(thread__comm_str(parent
), "sshd") ||
3162 strstarts(thread__comm_str(parent
), "gnome-terminal")) {
3163 pids
[nr
++] = parent
->tid
;
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
);
3176 static int trace__set_filter_pids(struct trace
*trace
)
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.
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
);
3192 } else if (perf_thread_map__pid(trace
->evlist
->core
.threads
, 0) == -1) {
3193 err
= trace__set_filter_loop_pids(trace
);
3199 static int __trace__deliver_event(struct trace
*trace
, union perf_event
*event
)
3201 struct evlist
*evlist
= trace
->evlist
;
3202 struct perf_sample sample
;
3205 err
= perf_evlist__parse_sample(evlist
, event
, &sample
);
3207 fprintf(trace
->output
, "Can't parse sample, err = %d, skipping...\n", err
);
3209 trace__handle_event(trace
, event
, &sample
);
3214 static int __trace__flush_events(struct trace
*trace
)
3216 u64 first
= ordered_events__first_time(&trace
->oe
.data
);
3217 u64 flush
= trace
->oe
.last
- NSEC_PER_SEC
;
3219 /* Is there some thing to flush.. */
3220 if (first
&& first
< flush
)
3221 return ordered_events__flush_time(&trace
->oe
.data
, flush
);
3226 static int trace__flush_events(struct trace
*trace
)
3228 return !trace
->sort_events
? 0 : __trace__flush_events(trace
);
3231 static int trace__deliver_event(struct trace
*trace
, union perf_event
*event
)
3235 if (!trace
->sort_events
)
3236 return __trace__deliver_event(trace
, event
);
3238 err
= perf_evlist__parse_sample_timestamp(trace
->evlist
, event
, &trace
->oe
.last
);
3239 if (err
&& err
!= -1)
3242 err
= ordered_events__queue(&trace
->oe
.data
, event
, trace
->oe
.last
, 0);
3246 return trace__flush_events(trace
);
3249 static int ordered_events__deliver_event(struct ordered_events
*oe
,
3250 struct ordered_event
*event
)
3252 struct trace
*trace
= container_of(oe
, struct trace
, oe
.data
);
3254 return __trace__deliver_event(trace
, event
->event
);
3257 static int trace__run(struct trace
*trace
, int argc
, const char **argv
)
3259 struct evlist
*evlist
= trace
->evlist
;
3260 struct evsel
*evsel
, *pgfault_maj
= NULL
, *pgfault_min
= NULL
;
3262 unsigned long before
;
3263 const bool forks
= argc
> 0;
3264 bool draining
= false;
3268 if (!trace
->raw_augmented_syscalls
) {
3269 if (trace
->trace_syscalls
&& trace__add_syscall_newtp(trace
))
3270 goto out_error_raw_syscalls
;
3272 if (trace
->trace_syscalls
)
3273 trace
->vfs_getname
= evlist__add_vfs_getname(evlist
);
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
)
3280 perf_evsel__config_callchain(pgfault_maj
, &trace
->opts
, &callchain_param
);
3281 evlist__add(evlist
, pgfault_maj
);
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
)
3288 perf_evsel__config_callchain(pgfault_min
, &trace
->opts
, &callchain_param
);
3289 evlist__add(evlist
, pgfault_min
);
3293 perf_evlist__add_newtp(evlist
, "sched", "sched_stat_runtime",
3294 trace__sched_stat_runtime
))
3295 goto out_error_sched_stat_runtime
;
3298 * If a global cgroup was set, apply it to all the events without an
3299 * explicit cgroup. I.e.:
3301 * trace -G A -e sched:*switch
3303 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
3304 * _and_ sched:sched_switch to the 'A' cgroup, while:
3306 * trace -e sched:*switch -G A
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).
3314 * trace -G A -e sched:*switch -G B
3316 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
3317 * to the 'B' cgroup.
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.
3323 evlist__set_default_cgroup(trace
->evlist
, trace
->cgroup
);
3325 err
= perf_evlist__create_maps(evlist
, &trace
->opts
.target
);
3327 fprintf(trace
->output
, "Problems parsing the target to trace, check your options!\n");
3328 goto out_delete_evlist
;
3331 err
= trace__symbols_init(trace
, evlist
);
3333 fprintf(trace
->output
, "Problems initializing symbol libraries!\n");
3334 goto out_delete_evlist
;
3337 perf_evlist__config(evlist
, &trace
->opts
, &callchain_param
);
3339 signal(SIGCHLD
, sig_handler
);
3340 signal(SIGINT
, sig_handler
);
3343 err
= perf_evlist__prepare_workload(evlist
, &trace
->opts
.target
,
3346 fprintf(trace
->output
, "Couldn't run the workload!\n");
3347 goto out_delete_evlist
;
3351 err
= evlist__open(evlist
);
3353 goto out_error_open
;
3355 err
= bpf__apply_obj_config();
3357 char errbuf
[BUFSIZ
];
3359 bpf__strerror_apply_obj_config(err
, errbuf
, sizeof(errbuf
));
3360 pr_err("ERROR: Apply config to BPF failed: %s\n",
3362 goto out_error_open
;
3365 err
= trace__set_filter_pids(trace
);
3369 if (trace
->syscalls
.map
)
3370 trace__init_syscalls_bpf_map(trace
);
3372 if (trace
->syscalls
.prog_array
.sys_enter
)
3373 trace__init_syscalls_bpf_prog_array_maps(trace
);
3375 if (trace
->ev_qualifier_ids
.nr
> 0) {
3376 err
= trace__set_ev_qualifier_filter(trace
);
3380 if (trace
->syscalls
.events
.sys_exit
) {
3381 pr_debug("event qualifier tracepoint filter: %s\n",
3382 trace
->syscalls
.events
.sys_exit
->filter
);
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
3394 * So just disable this beautifier (SCA_FD, SCA_FDAT) when 'close' is
3397 trace
->fd_path_disabled
= !trace__syscall_enabled(trace
, syscalltbl__id(trace
->sctbl
, "close"));
3399 err
= perf_evlist__apply_filters(evlist
, &evsel
);
3401 goto out_error_apply_filters
;
3403 if (trace
->dump
.map
)
3404 bpf_map__fprintf(trace
->dump
.map
, trace
->output
);
3406 err
= perf_evlist__mmap(evlist
, trace
->opts
.mmap_pages
);
3408 goto out_error_mmap
;
3410 if (!target__none(&trace
->opts
.target
) && !trace
->opts
.initial_delay
)
3411 evlist__enable(evlist
);
3414 perf_evlist__start_workload(evlist
);
3416 if (trace
->opts
.initial_delay
) {
3417 usleep(trace
->opts
.initial_delay
* 1000);
3418 evlist__enable(evlist
);
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
;
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.
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
;
3437 before
= trace
->nr_events
;
3439 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
3440 union perf_event
*event
;
3441 struct perf_mmap
*md
;
3443 md
= &evlist
->mmap
[i
];
3444 if (perf_mmap__read_init(md
) < 0)
3447 while ((event
= perf_mmap__read_event(md
)) != NULL
) {
3450 err
= trace__deliver_event(trace
, event
);
3454 perf_mmap__consume(md
);
3459 if (done
&& !draining
) {
3460 evlist__disable(evlist
);
3464 perf_mmap__read_done(md
);
3467 if (trace
->nr_events
== before
) {
3468 int timeout
= done
? 100 : -1;
3470 if (!draining
&& perf_evlist__poll(evlist
, timeout
) > 0) {
3471 if (perf_evlist__filter_pollfd(evlist
, POLLERR
| POLLHUP
| POLLNVAL
) == 0)
3476 if (trace__flush_events(trace
))
3484 thread__zput(trace
->current
);
3486 evlist__disable(evlist
);
3488 if (trace
->sort_events
)
3489 ordered_events__flush(&trace
->oe
.data
, OE_FLUSH__FINAL
);
3493 trace__fprintf_thread_summary(trace
, trace
->output
);
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
);
3505 trace__symbols__exit(trace
);
3507 evlist__delete(evlist
);
3508 cgroup__put(trace
->cgroup
);
3509 trace
->evlist
= NULL
;
3510 trace
->live
= false;
3513 char errbuf
[BUFSIZ
];
3515 out_error_sched_stat_runtime
:
3516 tracing_path__strerror_open_tp(errno
, errbuf
, sizeof(errbuf
), "sched", "sched_stat_runtime");
3519 out_error_raw_syscalls
:
3520 tracing_path__strerror_open_tp(errno
, errbuf
, sizeof(errbuf
), "raw_syscalls", "sys_(enter|exit)");
3524 perf_evlist__strerror_mmap(evlist
, errno
, errbuf
, sizeof(errbuf
));
3528 perf_evlist__strerror_open(evlist
, errno
, errbuf
, sizeof(errbuf
));
3531 fprintf(trace
->output
, "%s\n", errbuf
);
3532 goto out_delete_evlist
;
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
;
3542 fprintf(trace
->output
, "Not enough memory to run!\n");
3543 goto out_delete_evlist
;
3546 fprintf(trace
->output
, "errno=%d,%s\n", errno
, strerror(errno
));
3547 goto out_delete_evlist
;
3550 static int trace__replay(struct trace
*trace
)
3552 const struct evsel_str_handler handlers
[] = {
3553 { "probe:vfs_getname", trace__vfs_getname
, },
3555 struct perf_data data
= {
3557 .mode
= PERF_DATA_MODE_READ
,
3558 .force
= trace
->force
,
3560 struct perf_session
*session
;
3561 struct evsel
*evsel
;
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
;
3575 trace
->tool
.ordered_events
= true;
3576 trace
->tool
.ordering_requires_timestamps
= true;
3578 /* add tid to output */
3579 trace
->multiple_threads
= true;
3581 session
= perf_session__new(&data
, false, &trace
->tool
);
3582 if (session
== NULL
)
3585 if (trace
->opts
.target
.pid
)
3586 symbol_conf
.pid_list_str
= strdup(trace
->opts
.target
.pid
);
3588 if (trace
->opts
.target
.tid
)
3589 symbol_conf
.tid_list_str
= strdup(trace
->opts
.target
.tid
);
3591 if (symbol__init(&session
->header
.env
) < 0)
3594 trace
->host
= &session
->machines
.host
;
3596 err
= perf_session__set_tracepoints_handlers(session
, handlers
);
3600 evsel
= perf_evlist__find_tracepoint_by_name(session
->evlist
,
3601 "raw_syscalls:sys_enter");
3602 /* older kernels have syscalls tp versus raw_syscalls */
3604 evsel
= perf_evlist__find_tracepoint_by_name(session
->evlist
,
3605 "syscalls:sys_enter");
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");
3614 evsel
= perf_evlist__find_tracepoint_by_name(session
->evlist
,
3615 "raw_syscalls:sys_exit");
3617 evsel
= perf_evlist__find_tracepoint_by_name(session
->evlist
,
3618 "syscalls:sys_exit");
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");
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
;
3636 err
= perf_session__process_events(session
);
3638 pr_err("Failed to process events, error %d", err
);
3640 else if (trace
->summary
)
3641 trace__fprintf_thread_summary(trace
, trace
->output
);
3644 perf_session__delete(session
);
3649 static size_t trace__fprintf_threads_header(FILE *fp
)
3653 printed
= fprintf(fp
, "\n Summary of events:\n\n");
3658 DEFINE_RESORT_RB(syscall_stats
, a
->msecs
> b
->msecs
,
3659 struct stats
*stats
;
3664 struct int_node
*source
= rb_entry(nd
, struct int_node
, rb_node
);
3665 struct stats
*stats
= source
->priv
;
3667 entry
->syscall
= source
->i
;
3668 entry
->stats
= stats
;
3669 entry
->msecs
= stats
? (u64
)stats
->n
* (avg_stats(stats
) / NSEC_PER_MSEC
) : 0;
3672 static size_t thread__dump_stats(struct thread_trace
*ttrace
,
3673 struct trace
*trace
, FILE *fp
)
3678 DECLARE_RESORT_RB_INTLIST(syscall_stats
, ttrace
->syscall_stats
);
3680 if (syscall_stats
== NULL
)
3683 printed
+= fprintf(fp
, "\n");
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");
3689 resort_rb__for_each_entry(nd
, syscall_stats
) {
3690 struct stats
*stats
= syscall_stats_entry
->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
);
3696 u64 n
= (u64
) stats
->n
;
3698 pct
= avg
? 100.0 * stddev_stats(stats
)/avg
: 0.0;
3699 avg
/= NSEC_PER_MSEC
;
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
);
3709 resort_rb__delete(syscall_stats
);
3710 printed
+= fprintf(fp
, "\n\n");
3715 static size_t trace__fprintf_thread(FILE *fp
, struct thread
*thread
, struct trace
*trace
)
3718 struct thread_trace
*ttrace
= thread__priv(thread
);
3724 ratio
= (double)ttrace
->nr_events
/ trace
->nr_events
* 100.0;
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
);
3730 printed
+= fprintf(fp
, ", %lu majfaults", ttrace
->pfmaj
);
3732 printed
+= fprintf(fp
, ", %lu minfaults", ttrace
->pfmin
);
3734 printed
+= fprintf(fp
, ", %.3f msec\n", ttrace
->runtime_ms
);
3735 else if (fputc('\n', fp
) != EOF
)
3738 printed
+= thread__dump_stats(ttrace
, trace
, fp
);
3743 static unsigned long thread__nr_events(struct thread_trace
*ttrace
)
3745 return ttrace
? ttrace
->nr_events
: 0;
3748 DEFINE_RESORT_RB(threads
, (thread__nr_events(a
->thread
->priv
) < thread__nr_events(b
->thread
->priv
)),
3749 struct thread
*thread
;
3752 entry
->thread
= rb_entry(nd
, struct thread
, rb_node
);
3755 static size_t trace__fprintf_thread_summary(struct trace
*trace
, FILE *fp
)
3757 size_t printed
= trace__fprintf_threads_header(fp
);
3761 for (i
= 0; i
< THREADS__TABLE_SIZE
; i
++) {
3762 DECLARE_RESORT_RB_MACHINE_THREADS(threads
, trace
->host
, i
);
3764 if (threads
== NULL
) {
3765 fprintf(fp
, "%s", "Error sorting output by nr_events!\n");
3769 resort_rb__for_each_entry(nd
, threads
)
3770 printed
+= trace__fprintf_thread(fp
, threads_entry
->thread
, trace
);
3772 resort_rb__delete(threads
);
3777 static int trace__set_duration(const struct option
*opt
, const char *str
,
3778 int unset __maybe_unused
)
3780 struct trace
*trace
= opt
->value
;
3782 trace
->duration_filter
= atof(str
);
3786 static int trace__set_filter_pids_from_option(const struct option
*opt
, const char *str
,
3787 int unset __maybe_unused
)
3791 struct trace
*trace
= opt
->value
;
3793 * FIXME: introduce a intarray class, plain parse csv and create a
3794 * { int nr, int entries[] } struct...
3796 struct intlist
*list
= intlist__new(str
);
3801 i
= trace
->filter_pids
.nr
= intlist__nr_entries(list
) + 1;
3802 trace
->filter_pids
.entries
= calloc(i
, sizeof(pid_t
));
3804 if (trace
->filter_pids
.entries
== NULL
)
3807 trace
->filter_pids
.entries
[0] = getpid();
3809 for (i
= 1; i
< trace
->filter_pids
.nr
; ++i
)
3810 trace
->filter_pids
.entries
[i
] = intlist__entry(list
, i
- 1)->i
;
3812 intlist__delete(list
);
3818 static int trace__open_output(struct trace
*trace
, const char *filename
)
3822 if (!stat(filename
, &st
) && st
.st_size
) {
3823 char oldname
[PATH_MAX
];
3825 scnprintf(oldname
, sizeof(oldname
), "%s.old", filename
);
3827 rename(filename
, oldname
);
3830 trace
->output
= fopen(filename
, "w");
3832 return trace
->output
== NULL
? -errno
: 0;
3835 static int parse_pagefaults(const struct option
*opt
, const char *str
,
3836 int unset __maybe_unused
)
3838 int *trace_pgfaults
= opt
->value
;
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
;
3852 static void evlist__set_evsel_handler(struct evlist
*evlist
, void *handler
)
3854 struct evsel
*evsel
;
3856 evlist__for_each_entry(evlist
, evsel
)
3857 evsel
->handler
= handler
;
3860 static int evlist__set_syscall_tp_fields(struct evlist
*evlist
)
3862 struct evsel
*evsel
;
3864 evlist__for_each_entry(evlist
, evsel
) {
3865 if (evsel
->priv
|| !evsel
->tp_format
)
3868 if (strcmp(evsel
->tp_format
->system
, "syscalls"))
3871 if (perf_evsel__init_syscall_tp(evsel
))
3874 if (!strncmp(evsel
->tp_format
->name
, "sys_enter_", 10)) {
3875 struct syscall_tp
*sc
= evsel
->priv
;
3877 if (__tp_field__init_ptr(&sc
->args
, sc
->id
.offset
+ sizeof(u64
)))
3879 } else if (!strncmp(evsel
->tp_format
->name
, "sys_exit_", 9)) {
3880 struct syscall_tp
*sc
= evsel
->priv
;
3882 if (__tp_field__init_uint(&sc
->ret
, sizeof(u64
), sc
->id
.offset
+ sizeof(u64
), evsel
->needs_swap
))
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()).
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...
3898 static int trace__parse_events_option(const struct option
*opt
, const char *str
,
3899 int unset __maybe_unused
)
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
;
3909 if (strace_groups_dir
== NULL
)
3914 trace
->not_ev_qualifier
= true;
3918 if ((sep
= strchr(s
, ',')) != NULL
)
3922 if (syscalltbl__id(trace
->sctbl
, s
) >= 0 ||
3923 syscalltbl__strglobmatch_first(trace
->sctbl
, s
, &idx
) >= 0) {
3928 fmt
= syscall_fmt__find_by_alias(s
);
3933 path__join(group_name
, sizeof(group_name
), strace_groups_dir
, s
);
3934 if (access(group_name
, R_OK
) == 0)
3939 sprintf(lists
[list
] + strlen(lists
[list
]), ",%s", s
);
3941 lists
[list
] = malloc(len
);
3942 if (lists
[list
] == NULL
)
3944 strcpy(lists
[list
], s
);
3954 if (lists
[1] != NULL
) {
3955 struct strlist_config slist_config
= {
3956 .dirname
= strace_groups_dir
,
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
);
3965 if (trace__validate_ev_qualifier(trace
))
3967 trace
->trace_syscalls
= true;
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);
3985 static int trace__parse_cgroups(const struct option
*opt
, const char *str
, int unset
)
3987 struct trace
*trace
= opt
->value
;
3989 if (!list_empty(&trace
->evlist
->core
.entries
))
3990 return parse_cgroups(opt
, str
, unset
);
3992 trace
->cgroup
= evlist__findnew_cgroup(trace
->evlist
, str
);
3997 static struct bpf_map
*trace__find_bpf_map_by_name(struct trace
*trace
, const char *name
)
3999 if (trace
->bpf_obj
== NULL
)
4002 return bpf_object__find_map_by_name(trace
->bpf_obj
, name
);
4005 static void trace__set_bpf_map_filtered_pids(struct trace
*trace
)
4007 trace
->filter_pids
.map
= trace__find_bpf_map_by_name(trace
, "pids_filtered");
4010 static void trace__set_bpf_map_syscalls(struct trace
*trace
)
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");
4017 static int trace__config(const char *var
, const char *value
, void *arg
)
4019 struct trace
*trace
= arg
;
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
);
4027 * We can't propagate parse_event_option() return, as it is 1
4028 * for failure while perf_config() expects -1.
4030 if (parse_events_option(&o
, value
, 0))
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");
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
;
4060 int cmd_trace(int argc
, const char **argv
)
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>]",
4069 struct trace trace
= {
4075 .user_freq
= UINT_MAX
,
4076 .user_interval
= ULLONG_MAX
,
4077 .no_buffering
= true,
4078 .mmap_pages
= UINT_MAX
,
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
,
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",
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"),
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 "
4166 OPTS_EVSWITCH(&trace
.evswitch
),
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
};
4176 signal(SIGSEGV
, sighandler_dump_stack
);
4177 signal(SIGFPE
, sighandler_dump_stack
);
4179 trace
.evlist
= evlist__new();
4180 trace
.sctbl
= syscalltbl__new();
4182 if (trace
.evlist
== NULL
|| trace
.sctbl
== NULL
) {
4183 pr_err("Not enough memory to run!\n");
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.
4195 rlimit__bump_memlock();
4197 err
= perf_config(trace__config
, &trace
);
4201 argc
= parse_options_subcommand(argc
, argv
, trace_options
, trace_subcommands
,
4202 trace_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
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");
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
);
4217 trace
.syscalls
.events
.augmented
= evsel
;
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");
4225 if (evsel
->bpf_obj
== NULL
) {
4226 pr_err("ERROR: raw_syscalls:sys_enter not associated to a BPF object\n");
4230 trace
.bpf_obj
= evsel
->bpf_obj
;
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");
4237 err
= bpf__setup_stdout(trace
.evlist
);
4239 bpf__strerror_setup_stdout(trace
.evlist
, err
, bf
, sizeof(bf
));
4240 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf
);
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
);
4254 if (trace
.trace_pgfaults
) {
4255 trace
.opts
.sample_address
= true;
4256 trace
.opts
.sample_time
= true;
4259 if (trace
.opts
.mmap_pages
== UINT_MAX
)
4260 mmap_pages_user_set
= false;
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;
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);
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;
4277 symbol_conf
.use_callchain
= true;
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");
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);
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.
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'
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;
4308 if (raw_syscalls_sys_exit
) {
4309 trace
.raw_augmented_syscalls
= true;
4310 goto init_augmented_syscall_tp
;
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
))
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.
4324 augmented
->handler
= trace__sys_enter
;
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:
4331 if (perf_evsel__init_augmented_syscall_tp(evsel
, evsel
) ||
4332 perf_evsel__init_augmented_syscall_tp_args(evsel
))
4334 evsel
->handler
= trace__sys_enter
;
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
))
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.
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.
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
;
4370 if ((argc
>= 1) && (strcmp(argv
[0], "record") == 0))
4371 return trace__record(&trace
, argc
-1, &argv
[1]);
4373 /* summary_only implies summary option, but don't overwrite summary if set */
4374 if (trace
.summary_only
)
4375 trace
.summary
= trace
.summary_only
;
4377 if (!trace
.trace_syscalls
&& !trace
.trace_pgfaults
&&
4378 trace
.evlist
->core
.nr_entries
== 0 /* Was --events used? */) {
4379 trace
.trace_syscalls
= true;
4382 if (output_name
!= NULL
) {
4383 err
= trace__open_output(&trace
, output_name
);
4385 perror("failed to create output file");
4390 err
= evswitch__init(&trace
.evswitch
, trace
.evlist
, stderr
);
4394 err
= target__validate(&trace
.opts
.target
);
4396 target__strerror(&trace
.opts
.target
, err
, bf
, sizeof(bf
));
4397 fprintf(trace
.output
, "%s", bf
);
4401 err
= target__parse_uid(&trace
.opts
.target
);
4403 target__strerror(&trace
.opts
.target
, err
, bf
, sizeof(bf
));
4404 fprintf(trace
.output
, "%s", bf
);
4408 if (!argc
&& target__none(&trace
.opts
.target
))
4409 trace
.opts
.target
.system_wide
= true;
4412 err
= trace__replay(&trace
);
4414 err
= trace__run(&trace
, argc
, argv
);
4417 if (output_name
!= NULL
)
4418 fclose(trace
.output
);