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"
30 #include "util/event.h"
31 #include "util/synthetic-events.h"
32 #include "util/evlist.h"
33 #include "util/evswitch.h"
34 #include "util/mmap.h"
35 #include <subcmd/pager.h>
36 #include <subcmd/exec-cmd.h>
37 #include "util/machine.h"
39 #include "util/symbol.h"
40 #include "util/path.h"
41 #include "util/session.h"
42 #include "util/thread.h"
43 #include <subcmd/parse-options.h>
44 #include "util/strlist.h"
45 #include "util/intlist.h"
46 #include "util/thread_map.h"
47 #include "util/stat.h"
48 #include "util/tool.h"
49 #include "util/util.h"
50 #include "trace/beauty/beauty.h"
51 #include "trace-event.h"
52 #include "util/parse-events.h"
53 #include "util/bpf-loader.h"
54 #include "callchain.h"
55 #include "print_binary.h"
57 #include "syscalltbl.h"
58 #include "rb_resort.h"
67 #include <linux/err.h>
68 #include <linux/filter.h>
69 #include <linux/kernel.h>
70 #include <linux/random.h>
71 #include <linux/stringify.h>
72 #include <linux/time64.h>
73 #include <linux/zalloc.h>
75 #include <sys/sysmacros.h>
77 #include <linux/ctype.h>
80 # define O_CLOEXEC 02000000
83 #ifndef F_LINUX_SPECIFIC_BASE
84 # define F_LINUX_SPECIFIC_BASE 1024
88 struct perf_tool tool;
89 struct syscalltbl *sctbl;
91 struct syscall *table;
93 struct { // per syscall BPF_MAP_TYPE_PROG_ARRAY
94 struct bpf_map *sys_enter,
98 struct evsel *sys_enter,
102 struct bpf_program *unaugmented_prog;
107 struct record_opts opts;
108 struct evlist *evlist;
109 struct machine *host;
110 struct thread *current;
111 struct bpf_object *bpf_obj;
112 struct cgroup *cgroup;
115 unsigned long nr_events;
116 unsigned long nr_events_printed;
117 unsigned long max_events;
118 struct evswitch evswitch;
119 struct strlist *ev_qualifier;
129 double duration_filter;
135 unsigned int max_stack;
136 unsigned int min_stack;
137 int raw_augmented_syscalls_args_size;
138 bool raw_augmented_syscalls;
139 bool fd_path_disabled;
141 bool not_ev_qualifier;
145 bool multiple_threads;
151 bool show_tool_stats;
153 bool kernel_syscallchains;
159 bool show_string_prefix;
164 struct ordered_events data;
172 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
173 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
177 #define TP_UINT_FIELD(bits) \
178 static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
181 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
190 #define TP_UINT_FIELD__SWAPPED(bits) \
191 static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
194 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
195 return bswap_##bits(value);\
198 TP_UINT_FIELD__SWAPPED(16);
199 TP_UINT_FIELD__SWAPPED(32);
200 TP_UINT_FIELD__SWAPPED(64);
202 static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap)
204 field->offset = offset;
208 field->integer = tp_field__u8;
211 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
214 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
217 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
226 static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap)
228 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
231 static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
233 return sample->raw_data + field->offset;
236 static int __tp_field__init_ptr(struct tp_field *field, int offset)
238 field->offset = offset;
239 field->pointer = tp_field__ptr;
243 static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
245 return __tp_field__init_ptr(field, format_field->offset);
251 struct tp_field args, ret;
255 static int perf_evsel__init_tp_uint_field(struct evsel *evsel,
256 struct tp_field *field,
259 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
261 if (format_field == NULL)
264 return tp_field__init_uint(field, format_field, evsel->needs_swap);
267 #define perf_evsel__init_sc_tp_uint_field(evsel, name) \
268 ({ struct syscall_tp *sc = evsel->priv;\
269 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
271 static int perf_evsel__init_tp_ptr_field(struct evsel *evsel,
272 struct tp_field *field,
275 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
277 if (format_field == NULL)
280 return tp_field__init_ptr(field, format_field);
283 #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
284 ({ struct syscall_tp *sc = evsel->priv;\
285 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
287 static void evsel__delete_priv(struct evsel *evsel)
290 evsel__delete(evsel);
293 static int perf_evsel__init_syscall_tp(struct evsel *evsel)
295 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
297 if (evsel->priv != NULL) {
298 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
299 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
310 static int perf_evsel__init_augmented_syscall_tp(struct evsel *evsel, struct evsel *tp)
312 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
314 if (evsel->priv != NULL) {
315 struct tep_format_field *syscall_id = perf_evsel__field(tp, "id");
316 if (syscall_id == NULL)
317 syscall_id = perf_evsel__field(tp, "__syscall_nr");
318 if (syscall_id == NULL)
320 if (__tp_field__init_uint(&sc->id, syscall_id->size, syscall_id->offset, evsel->needs_swap))
332 static int perf_evsel__init_augmented_syscall_tp_args(struct evsel *evsel)
334 struct syscall_tp *sc = evsel->priv;
336 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
339 static int perf_evsel__init_augmented_syscall_tp_ret(struct evsel *evsel)
341 struct syscall_tp *sc = evsel->priv;
343 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
346 static int perf_evsel__init_raw_syscall_tp(struct evsel *evsel, void *handler)
348 evsel->priv = malloc(sizeof(struct syscall_tp));
349 if (evsel->priv != NULL) {
350 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
353 evsel->handler = handler;
364 static struct evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
366 struct evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
368 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
370 evsel = perf_evsel__newtp("syscalls", direction);
375 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
381 evsel__delete_priv(evsel);
385 #define perf_evsel__sc_tp_uint(evsel, name, sample) \
386 ({ struct syscall_tp *fields = evsel->priv; \
387 fields->name.integer(&fields->name, sample); })
389 #define perf_evsel__sc_tp_ptr(evsel, name, sample) \
390 ({ struct syscall_tp *fields = evsel->priv; \
391 fields->name.pointer(&fields->name, sample); })
393 size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
395 int idx = val - sa->offset;
397 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
398 size_t printed = scnprintf(bf, size, intfmt, val);
400 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
404 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
407 static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
409 struct syscall_arg *arg)
411 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
414 static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
415 struct syscall_arg *arg)
417 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
420 #define SCA_STRARRAY syscall_arg__scnprintf_strarray
422 size_t syscall_arg__scnprintf_strarray_flags(char *bf, size_t size, struct syscall_arg *arg)
424 return strarray__scnprintf_flags(arg->parm, bf, size, arg->show_string_prefix, arg->val);
427 size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
432 for (i = 0; i < sas->nr_entries; ++i) {
433 struct strarray *sa = sas->entries[i];
434 int idx = val - sa->offset;
436 if (idx >= 0 && idx < sa->nr_entries) {
437 if (sa->entries[idx] == NULL)
439 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
443 printed = scnprintf(bf, size, intfmt, val);
445 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
449 size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
450 struct syscall_arg *arg)
452 return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
456 #define AT_FDCWD -100
459 static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
460 struct syscall_arg *arg)
463 const char *prefix = "AT_FD";
466 return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
468 return syscall_arg__scnprintf_fd(bf, size, arg);
471 #define SCA_FDAT syscall_arg__scnprintf_fd_at
473 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
474 struct syscall_arg *arg);
476 #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
478 size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
480 return scnprintf(bf, size, "%#lx", arg->val);
483 size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
486 return scnprintf(bf, size, "NULL");
487 return syscall_arg__scnprintf_hex(bf, size, arg);
490 size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
492 return scnprintf(bf, size, "%d", arg->val);
495 size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
497 return scnprintf(bf, size, "%ld", arg->val);
500 static const char *bpf_cmd[] = {
501 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
502 "MAP_GET_NEXT_KEY", "PROG_LOAD",
504 static DEFINE_STRARRAY(bpf_cmd, "BPF_");
506 static const char *fsmount_flags[] = {
509 static DEFINE_STRARRAY(fsmount_flags, "FSMOUNT_");
511 #include "trace/beauty/generated/fsconfig_arrays.c"
513 static DEFINE_STRARRAY(fsconfig_cmds, "FSCONFIG_");
515 static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
516 static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
518 static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
519 static DEFINE_STRARRAY(itimers, "ITIMER_");
521 static const char *keyctl_options[] = {
522 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
523 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
524 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
525 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
526 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
528 static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
530 static const char *whences[] = { "SET", "CUR", "END",
538 static DEFINE_STRARRAY(whences, "SEEK_");
540 static const char *fcntl_cmds[] = {
541 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
542 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
543 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
546 static DEFINE_STRARRAY(fcntl_cmds, "F_");
548 static const char *fcntl_linux_specific_cmds[] = {
549 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
550 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
551 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
554 static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
556 static struct strarray *fcntl_cmds_arrays[] = {
557 &strarray__fcntl_cmds,
558 &strarray__fcntl_linux_specific_cmds,
561 static DEFINE_STRARRAYS(fcntl_cmds_arrays);
563 static const char *rlimit_resources[] = {
564 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
565 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
568 static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
570 static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
571 static DEFINE_STRARRAY(sighow, "SIG_");
573 static const char *clockid[] = {
574 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
575 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
576 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
578 static DEFINE_STRARRAY(clockid, "CLOCK_");
580 static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
581 struct syscall_arg *arg)
583 bool show_prefix = arg->show_string_prefix;
584 const char *suffix = "_OK";
588 if (mode == F_OK) /* 0 */
589 return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
591 if (mode & n##_OK) { \
592 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
602 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
607 #define SCA_ACCMODE syscall_arg__scnprintf_access_mode
609 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
610 struct syscall_arg *arg);
612 #define SCA_FILENAME syscall_arg__scnprintf_filename
614 static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
615 struct syscall_arg *arg)
617 bool show_prefix = arg->show_string_prefix;
618 const char *prefix = "O_";
619 int printed = 0, flags = arg->val;
622 if (flags & O_##n) { \
623 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
632 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
637 #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
639 #ifndef GRND_NONBLOCK
640 #define GRND_NONBLOCK 0x0001
643 #define GRND_RANDOM 0x0002
646 static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
647 struct syscall_arg *arg)
649 bool show_prefix = arg->show_string_prefix;
650 const char *prefix = "GRND_";
651 int printed = 0, flags = arg->val;
654 if (flags & GRND_##n) { \
655 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
656 flags &= ~GRND_##n; \
664 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
669 #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
671 #define STRARRAY(name, array) \
672 { .scnprintf = SCA_STRARRAY, \
673 .parm = &strarray__##array, }
675 #define STRARRAY_FLAGS(name, array) \
676 { .scnprintf = SCA_STRARRAY_FLAGS, \
677 .parm = &strarray__##array, }
679 #include "trace/beauty/arch_errno_names.c"
680 #include "trace/beauty/eventfd.c"
681 #include "trace/beauty/futex_op.c"
682 #include "trace/beauty/futex_val3.c"
683 #include "trace/beauty/mmap.c"
684 #include "trace/beauty/mode_t.c"
685 #include "trace/beauty/msg_flags.c"
686 #include "trace/beauty/open_flags.c"
687 #include "trace/beauty/perf_event_open.c"
688 #include "trace/beauty/pid.c"
689 #include "trace/beauty/sched_policy.c"
690 #include "trace/beauty/seccomp.c"
691 #include "trace/beauty/signum.c"
692 #include "trace/beauty/socket_type.c"
693 #include "trace/beauty/waitid_options.c"
695 struct syscall_arg_fmt {
696 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
697 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
703 static struct syscall_fmt {
707 const char *sys_enter,
710 struct syscall_arg_fmt arg[6];
717 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
718 { .name = "arch_prctl",
719 .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
720 [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
722 .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ },
723 [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ },
724 [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, },
726 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
727 { .name = "brk", .hexret = true,
728 .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
729 { .name = "clock_gettime",
730 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
731 { .name = "clone", .errpid = true, .nr_args = 5,
732 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
733 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
734 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
735 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
736 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
738 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
740 .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ },
741 [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ },
742 [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, },
743 { .name = "epoll_ctl",
744 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
745 { .name = "eventfd2",
746 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
747 { .name = "fchmodat",
748 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
749 { .name = "fchownat",
750 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
752 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
753 .parm = &strarrays__fcntl_cmds_arrays,
754 .show_zero = true, },
755 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
757 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
758 { .name = "fsconfig",
759 .arg = { [1] = STRARRAY(cmd, fsconfig_cmds), }, },
761 .arg = { [1] = STRARRAY_FLAGS(flags, fsmount_flags),
762 [2] = { .scnprintf = SCA_FSMOUNT_ATTR_FLAGS, /* attr_flags */ }, }, },
764 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
765 [1] = { .scnprintf = SCA_FILENAME, /* path */ },
766 [2] = { .scnprintf = SCA_FSPICK_FLAGS, /* flags */ }, }, },
767 { .name = "fstat", .alias = "newfstat", },
768 { .name = "fstatat", .alias = "newfstatat", },
770 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
771 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
772 { .name = "futimesat",
773 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
774 { .name = "getitimer",
775 .arg = { [0] = STRARRAY(which, itimers), }, },
776 { .name = "getpid", .errpid = true, },
777 { .name = "getpgid", .errpid = true, },
778 { .name = "getppid", .errpid = true, },
779 { .name = "getrandom",
780 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
781 { .name = "getrlimit",
782 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
783 { .name = "gettid", .errpid = true, },
786 #if defined(__i386__) || defined(__x86_64__)
788 * FIXME: Make this available to all arches.
790 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
791 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
793 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
795 { .name = "kcmp", .nr_args = 5,
796 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
797 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
798 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
799 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
800 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
802 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
804 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
806 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
808 .arg = { [2] = STRARRAY(whence, whences), }, },
809 { .name = "lstat", .alias = "newlstat", },
811 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
812 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
814 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
816 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
817 { .name = "mmap", .hexret = true,
818 /* The standard mmap maps to old_mmap on s390x */
819 #if defined(__s390x__)
822 .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
823 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ },
824 [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, },
826 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
827 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
828 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
829 { .name = "move_mount",
830 .arg = { [0] = { .scnprintf = SCA_FDAT, /* from_dfd */ },
831 [1] = { .scnprintf = SCA_FILENAME, /* from_pathname */ },
832 [2] = { .scnprintf = SCA_FDAT, /* to_dfd */ },
833 [3] = { .scnprintf = SCA_FILENAME, /* to_pathname */ },
834 [4] = { .scnprintf = SCA_MOVE_MOUNT_FLAGS, /* flags */ }, }, },
835 { .name = "mprotect",
836 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
837 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
838 { .name = "mq_unlink",
839 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
840 { .name = "mremap", .hexret = true,
841 .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
842 { .name = "name_to_handle_at",
843 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
844 { .name = "newfstatat",
845 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
847 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
848 { .name = "open_by_handle_at",
849 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
850 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
852 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
853 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
854 { .name = "perf_event_open",
855 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
856 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
857 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
859 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
860 { .name = "pkey_alloc",
861 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
862 { .name = "pkey_free",
863 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
864 { .name = "pkey_mprotect",
865 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
866 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
867 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
868 { .name = "poll", .timeout = true, },
869 { .name = "ppoll", .timeout = true, },
871 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
872 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
873 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
874 { .name = "pread", .alias = "pread64", },
875 { .name = "preadv", .alias = "pread", },
876 { .name = "prlimit64",
877 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
878 { .name = "pwrite", .alias = "pwrite64", },
879 { .name = "readlinkat",
880 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
881 { .name = "recvfrom",
882 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
883 { .name = "recvmmsg",
884 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
886 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
887 { .name = "renameat",
888 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
889 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
890 { .name = "renameat2",
891 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
892 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
893 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
894 { .name = "rt_sigaction",
895 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
896 { .name = "rt_sigprocmask",
897 .arg = { [0] = STRARRAY(how, sighow), }, },
898 { .name = "rt_sigqueueinfo",
899 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
900 { .name = "rt_tgsigqueueinfo",
901 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
902 { .name = "sched_setscheduler",
903 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
905 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
906 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
907 { .name = "select", .timeout = true, },
908 { .name = "sendfile", .alias = "sendfile64", },
909 { .name = "sendmmsg",
910 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
912 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
914 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
915 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
916 { .name = "set_tid_address", .errpid = true, },
917 { .name = "setitimer",
918 .arg = { [0] = STRARRAY(which, itimers), }, },
919 { .name = "setrlimit",
920 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
922 .arg = { [0] = STRARRAY(family, socket_families),
923 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
924 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
925 { .name = "socketpair",
926 .arg = { [0] = STRARRAY(family, socket_families),
927 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
928 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
929 { .name = "stat", .alias = "newstat", },
931 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
932 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
933 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
935 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
937 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
938 { .name = "symlinkat",
939 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
940 { .name = "sync_file_range",
941 .arg = { [3] = { .scnprintf = SCA_SYNC_FILE_RANGE_FLAGS, /* flags */ }, }, },
943 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
945 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
946 { .name = "umount2", .alias = "umount",
947 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
948 { .name = "uname", .alias = "newuname", },
949 { .name = "unlinkat",
950 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
951 { .name = "utimensat",
952 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
953 { .name = "wait4", .errpid = true,
954 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
955 { .name = "waitid", .errpid = true,
956 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
959 static int syscall_fmt__cmp(const void *name, const void *fmtp)
961 const struct syscall_fmt *fmt = fmtp;
962 return strcmp(name, fmt->name);
965 static struct syscall_fmt *syscall_fmt__find(const char *name)
967 const int nmemb = ARRAY_SIZE(syscall_fmts);
968 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
971 static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
973 int i, nmemb = ARRAY_SIZE(syscall_fmts);
975 for (i = 0; i < nmemb; ++i) {
976 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
977 return &syscall_fmts[i];
984 * is_exit: is this "exit" or "exit_group"?
985 * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter.
986 * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc.
987 * nonexistent: Just a hole in the syscall table, syscall id not allocated
990 struct tep_event *tp_format;
994 struct bpf_program *sys_enter,
1000 struct tep_format_field *args;
1002 struct syscall_fmt *fmt;
1003 struct syscall_arg_fmt *arg_fmt;
1007 * Must match what is in the BPF program:
1009 * tools/perf/examples/bpf/augmented_raw_syscalls.c
1011 struct bpf_map_syscall_entry {
1013 u16 string_args_len[6];
1017 * We need to have this 'calculated' boolean because in some cases we really
1018 * don't know what is the duration of a syscall, for instance, when we start
1019 * a session and some threads are waiting for a syscall to finish, say 'poll',
1020 * in which case all we can do is to print "( ? ) for duration and for the
1023 static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
1025 double duration = (double)t / NSEC_PER_MSEC;
1026 size_t printed = fprintf(fp, "(");
1029 printed += fprintf(fp, " ");
1030 else if (duration >= 1.0)
1031 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
1032 else if (duration >= 0.01)
1033 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
1035 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
1036 return printed + fprintf(fp, "): ");
1040 * filename.ptr: The filename char pointer that will be vfs_getname'd
1041 * filename.entry_str_pos: Where to insert the string translated from
1042 * filename.ptr by the vfs_getname tracepoint/kprobe.
1043 * ret_scnprintf: syscall args may set this to a different syscall return
1044 * formatter, for instance, fcntl may return fds, file flags, etc.
1046 struct thread_trace {
1049 unsigned long nr_events;
1050 unsigned long pfmaj, pfmin;
1053 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
1056 short int entry_str_pos;
1058 unsigned int namelen;
1066 struct intlist *syscall_stats;
1069 static struct thread_trace *thread_trace__new(void)
1071 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
1074 ttrace->files.max = -1;
1075 ttrace->syscall_stats = intlist__new(NULL);
1081 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
1083 struct thread_trace *ttrace;
1088 if (thread__priv(thread) == NULL)
1089 thread__set_priv(thread, thread_trace__new());
1091 if (thread__priv(thread) == NULL)
1094 ttrace = thread__priv(thread);
1095 ++ttrace->nr_events;
1099 color_fprintf(fp, PERF_COLOR_RED,
1100 "WARNING: not enough memory, dropping samples!\n");
1105 void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
1106 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
1108 struct thread_trace *ttrace = thread__priv(arg->thread);
1110 ttrace->ret_scnprintf = ret_scnprintf;
1113 #define TRACE_PFMAJ (1 << 0)
1114 #define TRACE_PFMIN (1 << 1)
1116 static const size_t trace__entry_str_size = 2048;
1118 static struct file *thread_trace__files_entry(struct thread_trace *ttrace, int fd)
1123 if (fd > ttrace->files.max) {
1124 struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file));
1129 if (ttrace->files.max != -1) {
1130 memset(nfiles + ttrace->files.max + 1, 0,
1131 (fd - ttrace->files.max) * sizeof(struct file));
1133 memset(nfiles, 0, (fd + 1) * sizeof(struct file));
1136 ttrace->files.table = nfiles;
1137 ttrace->files.max = fd;
1140 return ttrace->files.table + fd;
1143 struct file *thread__files_entry(struct thread *thread, int fd)
1145 return thread_trace__files_entry(thread__priv(thread), fd);
1148 static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
1150 struct thread_trace *ttrace = thread__priv(thread);
1151 struct file *file = thread_trace__files_entry(ttrace, fd);
1155 if (stat(pathname, &st) == 0)
1156 file->dev_maj = major(st.st_rdev);
1157 file->pathname = strdup(pathname);
1165 static int thread__read_fd_path(struct thread *thread, int fd)
1167 char linkname[PATH_MAX], pathname[PATH_MAX];
1171 if (thread->pid_ == thread->tid) {
1172 scnprintf(linkname, sizeof(linkname),
1173 "/proc/%d/fd/%d", thread->pid_, fd);
1175 scnprintf(linkname, sizeof(linkname),
1176 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1179 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1182 ret = readlink(linkname, pathname, sizeof(pathname));
1184 if (ret < 0 || ret > st.st_size)
1187 pathname[ret] = '\0';
1188 return trace__set_fd_pathname(thread, fd, pathname);
1191 static const char *thread__fd_path(struct thread *thread, int fd,
1192 struct trace *trace)
1194 struct thread_trace *ttrace = thread__priv(thread);
1196 if (ttrace == NULL || trace->fd_path_disabled)
1202 if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) {
1205 ++trace->stats.proc_getname;
1206 if (thread__read_fd_path(thread, fd))
1210 return ttrace->files.table[fd].pathname;
1213 size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
1216 size_t printed = scnprintf(bf, size, "%d", fd);
1217 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
1220 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1225 size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1227 size_t printed = scnprintf(bf, size, "%d", fd);
1228 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1231 const char *path = thread__fd_path(thread, fd, trace);
1234 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1236 thread__put(thread);
1242 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1243 struct syscall_arg *arg)
1246 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
1247 struct thread_trace *ttrace = thread__priv(arg->thread);
1249 if (ttrace && fd >= 0 && fd <= ttrace->files.max)
1250 zfree(&ttrace->files.table[fd].pathname);
1255 static void thread__set_filename_pos(struct thread *thread, const char *bf,
1258 struct thread_trace *ttrace = thread__priv(thread);
1260 ttrace->filename.ptr = ptr;
1261 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1264 static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1266 struct augmented_arg *augmented_arg = arg->augmented.args;
1267 size_t printed = scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
1269 * So that the next arg with a payload can consume its augmented arg, i.e. for rename* syscalls
1270 * we would have two strings, each prefixed by its size.
1272 int consumed = sizeof(*augmented_arg) + augmented_arg->size;
1274 arg->augmented.args = ((void *)arg->augmented.args) + consumed;
1275 arg->augmented.size -= consumed;
1280 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1281 struct syscall_arg *arg)
1283 unsigned long ptr = arg->val;
1285 if (arg->augmented.args)
1286 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1288 if (!arg->trace->vfs_getname)
1289 return scnprintf(bf, size, "%#x", ptr);
1291 thread__set_filename_pos(arg->thread, bf, ptr);
1295 static bool trace__filter_duration(struct trace *trace, double t)
1297 return t < (trace->duration_filter * NSEC_PER_MSEC);
1300 static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1302 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1304 return fprintf(fp, "%10.3f ", ts);
1308 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1309 * using ttrace->entry_time for a thread that receives a sys_exit without
1310 * first having received a sys_enter ("poll" issued before tracing session
1311 * starts, lost sys_enter exit due to ring buffer overflow).
1313 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1316 return __trace__fprintf_tstamp(trace, tstamp, fp);
1318 return fprintf(fp, " ? ");
1321 static bool done = false;
1322 static bool interrupted = false;
1324 static void sig_handler(int sig)
1327 interrupted = sig == SIGINT;
1330 static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
1334 if (trace->multiple_threads) {
1335 if (trace->show_comm)
1336 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
1337 printed += fprintf(fp, "%d ", thread->tid);
1343 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1344 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1348 if (trace->show_tstamp)
1349 printed = trace__fprintf_tstamp(trace, tstamp, fp);
1350 if (trace->show_duration)
1351 printed += fprintf_duration(duration, duration_calculated, fp);
1352 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1355 static int trace__process_event(struct trace *trace, struct machine *machine,
1356 union perf_event *event, struct perf_sample *sample)
1360 switch (event->header.type) {
1361 case PERF_RECORD_LOST:
1362 color_fprintf(trace->output, PERF_COLOR_RED,
1363 "LOST %" PRIu64 " events!\n", event->lost.lost);
1364 ret = machine__process_lost_event(machine, event, sample);
1367 ret = machine__process_event(machine, event, sample);
1374 static int trace__tool_process(struct perf_tool *tool,
1375 union perf_event *event,
1376 struct perf_sample *sample,
1377 struct machine *machine)
1379 struct trace *trace = container_of(tool, struct trace, tool);
1380 return trace__process_event(trace, machine, event, sample);
1383 static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1385 struct machine *machine = vmachine;
1387 if (machine->kptr_restrict_warned)
1390 if (symbol_conf.kptr_restrict) {
1391 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1392 "Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
1393 "Kernel samples will not be resolved.\n");
1394 machine->kptr_restrict_warned = true;
1398 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1401 static int trace__symbols_init(struct trace *trace, struct evlist *evlist)
1403 int err = symbol__init(NULL);
1408 trace->host = machine__new_host();
1409 if (trace->host == NULL)
1412 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1416 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
1417 evlist->core.threads, trace__tool_process, false,
1426 static void trace__symbols__exit(struct trace *trace)
1428 machine__exit(trace->host);
1434 static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1438 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1439 nr_args = sc->fmt->nr_args;
1441 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1442 if (sc->arg_fmt == NULL)
1445 for (idx = 0; idx < nr_args; ++idx) {
1447 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1450 sc->nr_args = nr_args;
1454 static int syscall__set_arg_fmts(struct syscall *sc)
1456 struct tep_format_field *field, *last_field = NULL;
1459 for (field = sc->args; field; field = field->next, ++idx) {
1462 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1465 len = strlen(field->name);
1467 if (strcmp(field->type, "const char *") == 0 &&
1468 ((len >= 4 && strcmp(field->name + len - 4, "name") == 0) ||
1469 strstr(field->name, "path") != NULL))
1470 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
1471 else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
1472 sc->arg_fmt[idx].scnprintf = SCA_PTR;
1473 else if (strcmp(field->type, "pid_t") == 0)
1474 sc->arg_fmt[idx].scnprintf = SCA_PID;
1475 else if (strcmp(field->type, "umode_t") == 0)
1476 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
1477 else if ((strcmp(field->type, "int") == 0 ||
1478 strcmp(field->type, "unsigned int") == 0 ||
1479 strcmp(field->type, "long") == 0) &&
1480 len >= 2 && strcmp(field->name + len - 2, "fd") == 0) {
1482 * /sys/kernel/tracing/events/syscalls/sys_enter*
1483 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1488 sc->arg_fmt[idx].scnprintf = SCA_FD;
1493 sc->args_size = last_field->offset + last_field->size;
1498 static int trace__read_syscall_info(struct trace *trace, int id)
1502 const char *name = syscalltbl__name(trace->sctbl, id);
1504 if (trace->syscalls.table == NULL) {
1505 trace->syscalls.table = calloc(trace->sctbl->syscalls.max_id + 1, sizeof(*sc));
1506 if (trace->syscalls.table == NULL)
1510 sc = trace->syscalls.table + id;
1511 if (sc->nonexistent)
1515 sc->nonexistent = true;
1520 sc->fmt = syscall_fmt__find(sc->name);
1522 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
1523 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
1525 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
1526 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
1527 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
1530 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1533 if (IS_ERR(sc->tp_format))
1534 return PTR_ERR(sc->tp_format);
1536 sc->args = sc->tp_format->format.fields;
1538 * We need to check and discard the first variable '__syscall_nr'
1539 * or 'nr' that mean the syscall number. It is needless here.
1540 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1542 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
1543 sc->args = sc->args->next;
1547 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
1548 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
1550 return syscall__set_arg_fmts(sc);
1553 static int intcmp(const void *a, const void *b)
1555 const int *one = a, *another = b;
1557 return *one - *another;
1560 static int trace__validate_ev_qualifier(struct trace *trace)
1563 bool printed_invalid_prefix = false;
1564 struct str_node *pos;
1565 size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier);
1567 trace->ev_qualifier_ids.entries = malloc(nr_allocated *
1568 sizeof(trace->ev_qualifier_ids.entries[0]));
1570 if (trace->ev_qualifier_ids.entries == NULL) {
1571 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1577 strlist__for_each_entry(pos, trace->ev_qualifier) {
1578 const char *sc = pos->s;
1579 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
1582 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1586 if (!printed_invalid_prefix) {
1587 pr_debug("Skipping unknown syscalls: ");
1588 printed_invalid_prefix = true;
1597 trace->ev_qualifier_ids.entries[nr_used++] = id;
1598 if (match_next == -1)
1602 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1605 if (nr_allocated == nr_used) {
1609 entries = realloc(trace->ev_qualifier_ids.entries,
1610 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1611 if (entries == NULL) {
1613 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1616 trace->ev_qualifier_ids.entries = entries;
1618 trace->ev_qualifier_ids.entries[nr_used++] = id;
1622 trace->ev_qualifier_ids.nr = nr_used;
1623 qsort(trace->ev_qualifier_ids.entries, nr_used, sizeof(int), intcmp);
1625 if (printed_invalid_prefix)
1629 zfree(&trace->ev_qualifier_ids.entries);
1630 trace->ev_qualifier_ids.nr = 0;
1634 static __maybe_unused bool trace__syscall_enabled(struct trace *trace, int id)
1636 bool in_ev_qualifier;
1638 if (trace->ev_qualifier_ids.nr == 0)
1641 in_ev_qualifier = bsearch(&id, trace->ev_qualifier_ids.entries,
1642 trace->ev_qualifier_ids.nr, sizeof(int), intcmp) != NULL;
1644 if (in_ev_qualifier)
1645 return !trace->not_ev_qualifier;
1647 return trace->not_ev_qualifier;
1651 * args is to be interpreted as a series of longs but we need to handle
1652 * 8-byte unaligned accesses. args points to raw_data within the event
1653 * and raw_data is guaranteed to be 8-byte unaligned because it is
1654 * preceded by raw_size which is a u32. So we need to copy args to a temp
1655 * variable to read it. Most notably this avoids extended load instructions
1656 * on unaligned addresses
1658 unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
1661 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
1663 memcpy(&val, p, sizeof(val));
1667 static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1668 struct syscall_arg *arg)
1670 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1671 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1673 return scnprintf(bf, size, "arg%d: ", arg->idx);
1677 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1678 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1679 * in tools/perf/trace/beauty/mount_flags.c
1681 static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1683 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1684 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1689 static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1690 struct syscall_arg *arg, unsigned long val)
1692 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1694 if (sc->arg_fmt[arg->idx].parm)
1695 arg->parm = sc->arg_fmt[arg->idx].parm;
1696 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1698 return scnprintf(bf, size, "%ld", val);
1701 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
1702 unsigned char *args, void *augmented_args, int augmented_args_size,
1703 struct trace *trace, struct thread *thread)
1708 struct syscall_arg arg = {
1711 .size = augmented_args_size,
1712 .args = augmented_args,
1718 .show_string_prefix = trace->show_string_prefix,
1720 struct thread_trace *ttrace = thread__priv(thread);
1723 * Things like fcntl will set this in its 'cmd' formatter to pick the
1724 * right formatter for the return value (an fd? file flags?), which is
1725 * not needed for syscalls that always return a given type, say an fd.
1727 ttrace->ret_scnprintf = NULL;
1729 if (sc->args != NULL) {
1730 struct tep_format_field *field;
1732 for (field = sc->args; field;
1733 field = field->next, ++arg.idx, bit <<= 1) {
1737 val = syscall_arg__val(&arg, arg.idx);
1739 * Some syscall args need some mask, most don't and
1740 * return val untouched.
1742 val = syscall__mask_val(sc, &arg, val);
1745 * Suppress this argument if its value is zero and
1746 * and we don't have a string associated in an
1750 !trace->show_zeros &&
1752 (sc->arg_fmt[arg.idx].show_zero ||
1753 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
1754 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1755 sc->arg_fmt[arg.idx].parm))
1758 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
1760 if (trace->show_arg_names)
1761 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
1763 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1765 } else if (IS_ERR(sc->tp_format)) {
1767 * If we managed to read the tracepoint /format file, then we
1768 * may end up not having any args, like with gettid(), so only
1769 * print the raw args when we didn't manage to read it.
1771 while (arg.idx < sc->nr_args) {
1774 val = syscall_arg__val(&arg, arg.idx);
1776 printed += scnprintf(bf + printed, size - printed, ", ");
1777 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
1778 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1788 typedef int (*tracepoint_handler)(struct trace *trace, struct evsel *evsel,
1789 union perf_event *event,
1790 struct perf_sample *sample);
1792 static struct syscall *trace__syscall_info(struct trace *trace,
1793 struct evsel *evsel, int id)
1800 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1801 * before that, leaving at a higher verbosity level till that is
1802 * explained. Reproduced with plain ftrace with:
1804 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1805 * grep "NR -1 " /t/trace_pipe
1807 * After generating some load on the machine.
1811 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1812 id, perf_evsel__name(evsel), ++n);
1819 if (id > trace->sctbl->syscalls.max_id)
1822 if ((trace->syscalls.table == NULL || trace->syscalls.table[id].name == NULL) &&
1823 (err = trace__read_syscall_info(trace, id)) != 0)
1826 if (trace->syscalls.table[id].name == NULL) {
1827 if (trace->syscalls.table[id].nonexistent)
1832 return &trace->syscalls.table[id];
1836 char sbuf[STRERR_BUFSIZE];
1837 fprintf(trace->output, "Problems reading syscall %d: %d (%s)", id, -err, str_error_r(-err, sbuf, sizeof(sbuf)));
1838 if (id <= trace->sctbl->syscalls.max_id && trace->syscalls.table[id].name != NULL)
1839 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1840 fputs(" information\n", trace->output);
1845 static void thread__update_stats(struct thread_trace *ttrace,
1846 int id, struct perf_sample *sample)
1848 struct int_node *inode;
1849 struct stats *stats;
1852 inode = intlist__findnew(ttrace->syscall_stats, id);
1856 stats = inode->priv;
1857 if (stats == NULL) {
1858 stats = malloc(sizeof(struct stats));
1862 inode->priv = stats;
1865 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1866 duration = sample->time - ttrace->entry_time;
1868 update_stats(stats, duration);
1871 static int trace__printf_interrupted_entry(struct trace *trace)
1873 struct thread_trace *ttrace;
1877 if (trace->failure_only || trace->current == NULL)
1880 ttrace = thread__priv(trace->current);
1882 if (!ttrace->entry_pending)
1885 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
1886 printed += len = fprintf(trace->output, "%s)", ttrace->entry_str);
1888 if (len < trace->args_alignment - 4)
1889 printed += fprintf(trace->output, "%-*s", trace->args_alignment - 4 - len, " ");
1891 printed += fprintf(trace->output, " ...\n");
1893 ttrace->entry_pending = false;
1894 ++trace->nr_events_printed;
1899 static int trace__fprintf_sample(struct trace *trace, struct evsel *evsel,
1900 struct perf_sample *sample, struct thread *thread)
1904 if (trace->print_sample) {
1905 double ts = (double)sample->time / NSEC_PER_MSEC;
1907 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1908 perf_evsel__name(evsel), ts,
1909 thread__comm_str(thread),
1910 sample->pid, sample->tid, sample->cpu);
1916 static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, int raw_augmented_args_size)
1918 void *augmented_args = NULL;
1920 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
1921 * and there we get all 6 syscall args plus the tracepoint common fields
1922 * that gets calculated at the start and the syscall_nr (another long).
1923 * So we check if that is the case and if so don't look after the
1924 * sc->args_size but always after the full raw_syscalls:sys_enter payload,
1927 * We'll revisit this later to pass s->args_size to the BPF augmenter
1928 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1929 * copies only what we need for each syscall, like what happens when we
1930 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1931 * traffic to just what is needed for each syscall.
1933 int args_size = raw_augmented_args_size ?: sc->args_size;
1935 *augmented_args_size = sample->raw_size - args_size;
1936 if (*augmented_args_size > 0)
1937 augmented_args = sample->raw_data + args_size;
1939 return augmented_args;
1942 static int trace__sys_enter(struct trace *trace, struct evsel *evsel,
1943 union perf_event *event __maybe_unused,
1944 struct perf_sample *sample)
1949 struct thread *thread;
1950 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1951 int augmented_args_size = 0;
1952 void *augmented_args = NULL;
1953 struct syscall *sc = trace__syscall_info(trace, evsel, id);
1954 struct thread_trace *ttrace;
1959 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1960 ttrace = thread__trace(thread, trace->output);
1964 trace__fprintf_sample(trace, evsel, sample, thread);
1966 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
1968 if (ttrace->entry_str == NULL) {
1969 ttrace->entry_str = malloc(trace__entry_str_size);
1970 if (!ttrace->entry_str)
1974 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
1975 trace__printf_interrupted_entry(trace);
1977 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1978 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1979 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1980 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1981 * so when handling, say the openat syscall, we end up getting 6 args for the
1982 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1983 * thinking that the extra 2 u64 args are the augmented filename, so just check
1984 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1986 if (evsel != trace->syscalls.events.sys_enter)
1987 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
1988 ttrace->entry_time = sample->time;
1989 msg = ttrace->entry_str;
1990 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
1992 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
1993 args, augmented_args, augmented_args_size, trace, thread);
1996 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
1999 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
2000 printed = fprintf(trace->output, "%s)", ttrace->entry_str);
2001 if (trace->args_alignment > printed)
2002 alignment = trace->args_alignment - printed;
2003 fprintf(trace->output, "%*s= ?\n", alignment, " ");
2006 ttrace->entry_pending = true;
2007 /* See trace__vfs_getname & trace__sys_exit */
2008 ttrace->filename.pending_open = false;
2011 if (trace->current != thread) {
2012 thread__put(trace->current);
2013 trace->current = thread__get(thread);
2017 thread__put(thread);
2021 static int trace__fprintf_sys_enter(struct trace *trace, struct evsel *evsel,
2022 struct perf_sample *sample)
2024 struct thread_trace *ttrace;
2025 struct thread *thread;
2026 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
2027 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2029 void *args, *augmented_args = NULL;
2030 int augmented_args_size;
2035 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2036 ttrace = thread__trace(thread, trace->output);
2038 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
2039 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
2044 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
2045 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
2046 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
2047 fprintf(trace->output, "%s", msg);
2050 thread__put(thread);
2054 static int trace__resolve_callchain(struct trace *trace, struct evsel *evsel,
2055 struct perf_sample *sample,
2056 struct callchain_cursor *cursor)
2058 struct addr_location al;
2059 int max_stack = evsel->core.attr.sample_max_stack ?
2060 evsel->core.attr.sample_max_stack :
2064 if (machine__resolve(trace->host, &al, sample) < 0)
2067 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
2068 addr_location__put(&al);
2072 static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
2074 /* TODO: user-configurable print_opts */
2075 const unsigned int print_opts = EVSEL__PRINT_SYM |
2077 EVSEL__PRINT_UNKNOWN_AS_ADDR;
2079 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
2082 static const char *errno_to_name(struct evsel *evsel, int err)
2084 struct perf_env *env = perf_evsel__env(evsel);
2085 const char *arch_name = perf_env__arch(env);
2087 return arch_syscalls__strerrno(arch_name, err);
2090 static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
2091 union perf_event *event __maybe_unused,
2092 struct perf_sample *sample)
2096 bool duration_calculated = false;
2097 struct thread *thread;
2098 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
2099 int alignment = trace->args_alignment;
2100 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2101 struct thread_trace *ttrace;
2106 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2107 ttrace = thread__trace(thread, trace->output);
2111 trace__fprintf_sample(trace, evsel, sample, thread);
2114 thread__update_stats(ttrace, id, sample);
2116 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
2118 if (!trace->fd_path_disabled && sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
2119 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
2120 ttrace->filename.pending_open = false;
2121 ++trace->stats.vfs_getname;
2124 if (ttrace->entry_time) {
2125 duration = sample->time - ttrace->entry_time;
2126 if (trace__filter_duration(trace, duration))
2128 duration_calculated = true;
2129 } else if (trace->duration_filter)
2132 if (sample->callchain) {
2133 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2134 if (callchain_ret == 0) {
2135 if (callchain_cursor.nr < trace->min_stack)
2141 if (trace->summary_only || (ret >= 0 && trace->failure_only))
2144 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
2146 if (ttrace->entry_pending) {
2147 printed = fprintf(trace->output, "%s", ttrace->entry_str);
2149 printed += fprintf(trace->output, " ... [");
2150 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
2152 printed += fprintf(trace->output, "]: %s()", sc->name);
2155 printed++; /* the closing ')' */
2157 if (alignment > printed)
2158 alignment -= printed;
2162 fprintf(trace->output, ")%*s= ", alignment, " ");
2164 if (sc->fmt == NULL) {
2168 fprintf(trace->output, "%ld", ret);
2169 } else if (ret < 0) {
2171 char bf[STRERR_BUFSIZE];
2172 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
2173 *e = errno_to_name(evsel, -ret);
2175 fprintf(trace->output, "-1 %s (%s)", e, emsg);
2177 } else if (ret == 0 && sc->fmt->timeout)
2178 fprintf(trace->output, "0 (Timeout)");
2179 else if (ttrace->ret_scnprintf) {
2181 struct syscall_arg arg = {
2186 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
2187 ttrace->ret_scnprintf = NULL;
2188 fprintf(trace->output, "%s", bf);
2189 } else if (sc->fmt->hexret)
2190 fprintf(trace->output, "%#lx", ret);
2191 else if (sc->fmt->errpid) {
2192 struct thread *child = machine__find_thread(trace->host, ret, ret);
2194 if (child != NULL) {
2195 fprintf(trace->output, "%ld", ret);
2196 if (child->comm_set)
2197 fprintf(trace->output, " (%s)", thread__comm_str(child));
2203 fputc('\n', trace->output);
2206 * We only consider an 'event' for the sake of --max-events a non-filtered
2207 * sys_enter + sys_exit and other tracepoint events.
2209 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2212 if (callchain_ret > 0)
2213 trace__fprintf_callchain(trace, sample);
2214 else if (callchain_ret < 0)
2215 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2217 ttrace->entry_pending = false;
2220 thread__put(thread);
2224 static int trace__vfs_getname(struct trace *trace, struct evsel *evsel,
2225 union perf_event *event __maybe_unused,
2226 struct perf_sample *sample)
2228 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2229 struct thread_trace *ttrace;
2230 size_t filename_len, entry_str_len, to_move;
2231 ssize_t remaining_space;
2233 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
2238 ttrace = thread__priv(thread);
2242 filename_len = strlen(filename);
2243 if (filename_len == 0)
2246 if (ttrace->filename.namelen < filename_len) {
2247 char *f = realloc(ttrace->filename.name, filename_len + 1);
2252 ttrace->filename.namelen = filename_len;
2253 ttrace->filename.name = f;
2256 strcpy(ttrace->filename.name, filename);
2257 ttrace->filename.pending_open = true;
2259 if (!ttrace->filename.ptr)
2262 entry_str_len = strlen(ttrace->entry_str);
2263 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2264 if (remaining_space <= 0)
2267 if (filename_len > (size_t)remaining_space) {
2268 filename += filename_len - remaining_space;
2269 filename_len = remaining_space;
2272 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2273 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2274 memmove(pos + filename_len, pos, to_move);
2275 memcpy(pos, filename, filename_len);
2277 ttrace->filename.ptr = 0;
2278 ttrace->filename.entry_str_pos = 0;
2280 thread__put(thread);
2285 static int trace__sched_stat_runtime(struct trace *trace, struct evsel *evsel,
2286 union perf_event *event __maybe_unused,
2287 struct perf_sample *sample)
2289 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2290 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
2291 struct thread *thread = machine__findnew_thread(trace->host,
2294 struct thread_trace *ttrace = thread__trace(thread, trace->output);
2299 ttrace->runtime_ms += runtime_ms;
2300 trace->runtime_ms += runtime_ms;
2302 thread__put(thread);
2306 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
2308 perf_evsel__strval(evsel, sample, "comm"),
2309 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2311 perf_evsel__intval(evsel, sample, "vruntime"));
2315 static int bpf_output__printer(enum binary_printer_ops op,
2316 unsigned int val, void *extra __maybe_unused, FILE *fp)
2318 unsigned char ch = (unsigned char)val;
2321 case BINARY_PRINT_CHAR_DATA:
2322 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
2323 case BINARY_PRINT_DATA_BEGIN:
2324 case BINARY_PRINT_LINE_BEGIN:
2325 case BINARY_PRINT_ADDR:
2326 case BINARY_PRINT_NUM_DATA:
2327 case BINARY_PRINT_NUM_PAD:
2328 case BINARY_PRINT_SEP:
2329 case BINARY_PRINT_CHAR_PAD:
2330 case BINARY_PRINT_LINE_END:
2331 case BINARY_PRINT_DATA_END:
2339 static void bpf_output__fprintf(struct trace *trace,
2340 struct perf_sample *sample)
2342 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2343 bpf_output__printer, NULL, trace->output);
2344 ++trace->nr_events_printed;
2347 static int trace__event_handler(struct trace *trace, struct evsel *evsel,
2348 union perf_event *event __maybe_unused,
2349 struct perf_sample *sample)
2351 struct thread *thread;
2352 int callchain_ret = 0;
2354 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2355 * this event's max_events having been hit and this is an entry coming
2356 * from the ring buffer that we should discard, since the max events
2357 * have already been considered/printed.
2359 if (evsel->disabled)
2362 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2364 if (sample->callchain) {
2365 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2366 if (callchain_ret == 0) {
2367 if (callchain_cursor.nr < trace->min_stack)
2373 trace__printf_interrupted_entry(trace);
2374 trace__fprintf_tstamp(trace, sample->time, trace->output);
2376 if (trace->trace_syscalls && trace->show_duration)
2377 fprintf(trace->output, "( ): ");
2380 trace__fprintf_comm_tid(trace, thread, trace->output);
2382 if (evsel == trace->syscalls.events.augmented) {
2383 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2384 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2387 fprintf(trace->output, "%s(", sc->name);
2388 trace__fprintf_sys_enter(trace, evsel, sample);
2389 fputc(')', trace->output);
2394 * XXX: Not having the associated syscall info or not finding/adding
2395 * the thread should never happen, but if it does...
2396 * fall thru and print it as a bpf_output event.
2400 fprintf(trace->output, "%s:", evsel->name);
2402 if (perf_evsel__is_bpf_output(evsel)) {
2403 bpf_output__fprintf(trace, sample);
2404 } else if (evsel->tp_format) {
2405 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2406 trace__fprintf_sys_enter(trace, evsel, sample)) {
2407 event_format__fprintf(evsel->tp_format, sample->cpu,
2408 sample->raw_data, sample->raw_size,
2410 ++trace->nr_events_printed;
2412 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2413 evsel__disable(evsel);
2414 evsel__close(evsel);
2420 fprintf(trace->output, "\n");
2422 if (callchain_ret > 0)
2423 trace__fprintf_callchain(trace, sample);
2424 else if (callchain_ret < 0)
2425 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2427 thread__put(thread);
2431 static void print_location(FILE *f, struct perf_sample *sample,
2432 struct addr_location *al,
2433 bool print_dso, bool print_sym)
2436 if ((verbose > 0 || print_dso) && al->map)
2437 fprintf(f, "%s@", al->map->dso->long_name);
2439 if ((verbose > 0 || print_sym) && al->sym)
2440 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
2441 al->addr - al->sym->start);
2443 fprintf(f, "0x%" PRIx64, al->addr);
2445 fprintf(f, "0x%" PRIx64, sample->addr);
2448 static int trace__pgfault(struct trace *trace,
2449 struct evsel *evsel,
2450 union perf_event *event __maybe_unused,
2451 struct perf_sample *sample)
2453 struct thread *thread;
2454 struct addr_location al;
2455 char map_type = 'd';
2456 struct thread_trace *ttrace;
2458 int callchain_ret = 0;
2460 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2462 if (sample->callchain) {
2463 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2464 if (callchain_ret == 0) {
2465 if (callchain_cursor.nr < trace->min_stack)
2471 ttrace = thread__trace(thread, trace->output);
2475 if (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2480 if (trace->summary_only)
2483 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
2485 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
2487 fprintf(trace->output, "%sfault [",
2488 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2491 print_location(trace->output, sample, &al, false, true);
2493 fprintf(trace->output, "] => ");
2495 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
2498 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
2506 print_location(trace->output, sample, &al, true, false);
2508 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
2510 if (callchain_ret > 0)
2511 trace__fprintf_callchain(trace, sample);
2512 else if (callchain_ret < 0)
2513 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2515 ++trace->nr_events_printed;
2519 thread__put(thread);
2523 static void trace__set_base_time(struct trace *trace,
2524 struct evsel *evsel,
2525 struct perf_sample *sample)
2528 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2529 * and don't use sample->time unconditionally, we may end up having
2530 * some other event in the future without PERF_SAMPLE_TIME for good
2531 * reason, i.e. we may not be interested in its timestamps, just in
2532 * it taking place, picking some piece of information when it
2533 * appears in our event stream (vfs_getname comes to mind).
2535 if (trace->base_time == 0 && !trace->full_time &&
2536 (evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
2537 trace->base_time = sample->time;
2540 static int trace__process_sample(struct perf_tool *tool,
2541 union perf_event *event,
2542 struct perf_sample *sample,
2543 struct evsel *evsel,
2544 struct machine *machine __maybe_unused)
2546 struct trace *trace = container_of(tool, struct trace, tool);
2547 struct thread *thread;
2550 tracepoint_handler handler = evsel->handler;
2552 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2553 if (thread && thread__is_filtered(thread))
2556 trace__set_base_time(trace, evsel, sample);
2560 handler(trace, evsel, event, sample);
2563 thread__put(thread);
2567 static int trace__record(struct trace *trace, int argc, const char **argv)
2569 unsigned int rec_argc, i, j;
2570 const char **rec_argv;
2571 const char * const record_args[] = {
2578 const char * const sc_args[] = { "-e", };
2579 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2580 const char * const majpf_args[] = { "-e", "major-faults" };
2581 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2582 const char * const minpf_args[] = { "-e", "minor-faults" };
2583 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2585 /* +1 is for the event string below */
2586 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2587 majpf_args_nr + minpf_args_nr + argc;
2588 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2590 if (rec_argv == NULL)
2594 for (i = 0; i < ARRAY_SIZE(record_args); i++)
2595 rec_argv[j++] = record_args[i];
2597 if (trace->trace_syscalls) {
2598 for (i = 0; i < sc_args_nr; i++)
2599 rec_argv[j++] = sc_args[i];
2601 /* event string may be different for older kernels - e.g., RHEL6 */
2602 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2603 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2604 else if (is_valid_tracepoint("syscalls:sys_enter"))
2605 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2607 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
2613 if (trace->trace_pgfaults & TRACE_PFMAJ)
2614 for (i = 0; i < majpf_args_nr; i++)
2615 rec_argv[j++] = majpf_args[i];
2617 if (trace->trace_pgfaults & TRACE_PFMIN)
2618 for (i = 0; i < minpf_args_nr; i++)
2619 rec_argv[j++] = minpf_args[i];
2621 for (i = 0; i < (unsigned int)argc; i++)
2622 rec_argv[j++] = argv[i];
2624 return cmd_record(j, rec_argv);
2627 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2629 static bool evlist__add_vfs_getname(struct evlist *evlist)
2632 struct evsel *evsel, *tmp;
2633 struct parse_events_error err = { .idx = 0, };
2634 int ret = parse_events(evlist, "probe:vfs_getname*", &err);
2639 evlist__for_each_entry_safe(evlist, evsel, tmp) {
2640 if (!strstarts(perf_evsel__name(evsel), "probe:vfs_getname"))
2643 if (perf_evsel__field(evsel, "pathname")) {
2644 evsel->handler = trace__vfs_getname;
2649 list_del_init(&evsel->core.node);
2650 evsel->evlist = NULL;
2651 evsel__delete(evsel);
2657 static struct evsel *perf_evsel__new_pgfault(u64 config)
2659 struct evsel *evsel;
2660 struct perf_event_attr attr = {
2661 .type = PERF_TYPE_SOFTWARE,
2665 attr.config = config;
2666 attr.sample_period = 1;
2668 event_attr_init(&attr);
2670 evsel = evsel__new(&attr);
2672 evsel->handler = trace__pgfault;
2677 static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2679 const u32 type = event->header.type;
2680 struct evsel *evsel;
2682 if (type != PERF_RECORD_SAMPLE) {
2683 trace__process_event(trace, trace->host, event, sample);
2687 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2688 if (evsel == NULL) {
2689 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2693 if (evswitch__discard(&trace->evswitch, evsel))
2696 trace__set_base_time(trace, evsel, sample);
2698 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
2699 sample->raw_data == NULL) {
2700 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2701 perf_evsel__name(evsel), sample->tid,
2702 sample->cpu, sample->raw_size);
2704 tracepoint_handler handler = evsel->handler;
2705 handler(trace, evsel, event, sample);
2708 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2712 static int trace__add_syscall_newtp(struct trace *trace)
2715 struct evlist *evlist = trace->evlist;
2716 struct evsel *sys_enter, *sys_exit;
2718 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
2719 if (sys_enter == NULL)
2722 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2723 goto out_delete_sys_enter;
2725 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
2726 if (sys_exit == NULL)
2727 goto out_delete_sys_enter;
2729 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2730 goto out_delete_sys_exit;
2732 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2733 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2735 evlist__add(evlist, sys_enter);
2736 evlist__add(evlist, sys_exit);
2738 if (callchain_param.enabled && !trace->kernel_syscallchains) {
2740 * We're interested only in the user space callchain
2741 * leading to the syscall, allow overriding that for
2742 * debugging reasons using --kernel_syscall_callchains
2744 sys_exit->core.attr.exclude_callchain_kernel = 1;
2747 trace->syscalls.events.sys_enter = sys_enter;
2748 trace->syscalls.events.sys_exit = sys_exit;
2754 out_delete_sys_exit:
2755 evsel__delete_priv(sys_exit);
2756 out_delete_sys_enter:
2757 evsel__delete_priv(sys_enter);
2761 static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
2764 struct evsel *sys_exit;
2765 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2766 trace->ev_qualifier_ids.nr,
2767 trace->ev_qualifier_ids.entries);
2772 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2774 sys_exit = trace->syscalls.events.sys_exit;
2775 err = perf_evsel__append_tp_filter(sys_exit, filter);
2786 #ifdef HAVE_LIBBPF_SUPPORT
2787 static struct bpf_program *trace__find_bpf_program_by_title(struct trace *trace, const char *name)
2789 if (trace->bpf_obj == NULL)
2792 return bpf_object__find_program_by_title(trace->bpf_obj, name);
2795 static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace, struct syscall *sc,
2796 const char *prog_name, const char *type)
2798 struct bpf_program *prog;
2800 if (prog_name == NULL) {
2801 char default_prog_name[256];
2802 scnprintf(default_prog_name, sizeof(default_prog_name), "!syscalls:sys_%s_%s", type, sc->name);
2803 prog = trace__find_bpf_program_by_title(trace, default_prog_name);
2806 if (sc->fmt && sc->fmt->alias) {
2807 scnprintf(default_prog_name, sizeof(default_prog_name), "!syscalls:sys_%s_%s", type, sc->fmt->alias);
2808 prog = trace__find_bpf_program_by_title(trace, default_prog_name);
2812 goto out_unaugmented;
2815 prog = trace__find_bpf_program_by_title(trace, prog_name);
2822 pr_debug("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n",
2823 prog_name, type, sc->name);
2825 return trace->syscalls.unaugmented_prog;
2828 static void trace__init_syscall_bpf_progs(struct trace *trace, int id)
2830 struct syscall *sc = trace__syscall_info(trace, NULL, id);
2835 sc->bpf_prog.sys_enter = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_enter : NULL, "enter");
2836 sc->bpf_prog.sys_exit = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_exit : NULL, "exit");
2839 static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int id)
2841 struct syscall *sc = trace__syscall_info(trace, NULL, id);
2842 return sc ? bpf_program__fd(sc->bpf_prog.sys_enter) : bpf_program__fd(trace->syscalls.unaugmented_prog);
2845 static int trace__bpf_prog_sys_exit_fd(struct trace *trace, int id)
2847 struct syscall *sc = trace__syscall_info(trace, NULL, id);
2848 return sc ? bpf_program__fd(sc->bpf_prog.sys_exit) : bpf_program__fd(trace->syscalls.unaugmented_prog);
2851 static void trace__init_bpf_map_syscall_args(struct trace *trace, int id, struct bpf_map_syscall_entry *entry)
2853 struct syscall *sc = trace__syscall_info(trace, NULL, id);
2859 for (; arg < sc->nr_args; ++arg) {
2860 entry->string_args_len[arg] = 0;
2861 if (sc->arg_fmt[arg].scnprintf == SCA_FILENAME) {
2862 /* Should be set like strace -s strsize */
2863 entry->string_args_len[arg] = PATH_MAX;
2867 for (; arg < 6; ++arg)
2868 entry->string_args_len[arg] = 0;
2870 static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2872 int fd = bpf_map__fd(trace->syscalls.map);
2873 struct bpf_map_syscall_entry value = {
2874 .enabled = !trace->not_ev_qualifier,
2879 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2880 int key = trace->ev_qualifier_ids.entries[i];
2882 if (value.enabled) {
2883 trace__init_bpf_map_syscall_args(trace, key, &value);
2884 trace__init_syscall_bpf_progs(trace, key);
2887 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2895 static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2897 int fd = bpf_map__fd(trace->syscalls.map);
2898 struct bpf_map_syscall_entry value = {
2903 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
2905 trace__init_bpf_map_syscall_args(trace, key, &value);
2907 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
2915 static int trace__init_syscalls_bpf_map(struct trace *trace)
2917 bool enabled = true;
2919 if (trace->ev_qualifier_ids.nr)
2920 enabled = trace->not_ev_qualifier;
2922 return __trace__init_syscalls_bpf_map(trace, enabled);
2925 static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace, struct syscall *sc)
2927 struct tep_format_field *field, *candidate_field;
2931 * We're only interested in syscalls that have a pointer:
2933 for (field = sc->args; field; field = field->next) {
2934 if (field->flags & TEP_FIELD_IS_POINTER)
2935 goto try_to_find_pair;
2941 for (id = 0; id < trace->sctbl->syscalls.nr_entries; ++id) {
2942 struct syscall *pair = trace__syscall_info(trace, NULL, id);
2943 struct bpf_program *pair_prog;
2944 bool is_candidate = false;
2946 if (pair == NULL || pair == sc ||
2947 pair->bpf_prog.sys_enter == trace->syscalls.unaugmented_prog)
2950 for (field = sc->args, candidate_field = pair->args;
2951 field && candidate_field; field = field->next, candidate_field = candidate_field->next) {
2952 bool is_pointer = field->flags & TEP_FIELD_IS_POINTER,
2953 candidate_is_pointer = candidate_field->flags & TEP_FIELD_IS_POINTER;
2956 if (!candidate_is_pointer) {
2957 // The candidate just doesn't copies our pointer arg, might copy other pointers we want.
2961 if (candidate_is_pointer) {
2962 // The candidate might copy a pointer we don't have, skip it.
2963 goto next_candidate;
2968 if (strcmp(field->type, candidate_field->type))
2969 goto next_candidate;
2971 is_candidate = true;
2975 goto next_candidate;
2978 * Check if the tentative pair syscall augmenter has more pointers, if it has,
2979 * then it may be collecting that and we then can't use it, as it would collect
2980 * more than what is common to the two syscalls.
2982 if (candidate_field) {
2983 for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next)
2984 if (candidate_field->flags & TEP_FIELD_IS_POINTER)
2985 goto next_candidate;
2988 pair_prog = pair->bpf_prog.sys_enter;
2990 * If the pair isn't enabled, then its bpf_prog.sys_enter will not
2991 * have been searched for, so search it here and if it returns the
2992 * unaugmented one, then ignore it, otherwise we'll reuse that BPF
2993 * program for a filtered syscall on a non-filtered one.
2995 * For instance, we have "!syscalls:sys_enter_renameat" and that is
2996 * useful for "renameat2".
2998 if (pair_prog == NULL) {
2999 pair_prog = trace__find_syscall_bpf_prog(trace, pair, pair->fmt ? pair->fmt->bpf_prog_name.sys_enter : NULL, "enter");
3000 if (pair_prog == trace->syscalls.unaugmented_prog)
3001 goto next_candidate;
3004 pr_debug("Reusing \"%s\" BPF sys_enter augmenter for \"%s\"\n", pair->name, sc->name);
3013 static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace)
3015 int map_enter_fd = bpf_map__fd(trace->syscalls.prog_array.sys_enter),
3016 map_exit_fd = bpf_map__fd(trace->syscalls.prog_array.sys_exit);
3019 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
3022 if (!trace__syscall_enabled(trace, key))
3025 trace__init_syscall_bpf_progs(trace, key);
3027 // It'll get at least the "!raw_syscalls:unaugmented"
3028 prog_fd = trace__bpf_prog_sys_enter_fd(trace, key);
3029 err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY);
3032 prog_fd = trace__bpf_prog_sys_exit_fd(trace, key);
3033 err = bpf_map_update_elem(map_exit_fd, &key, &prog_fd, BPF_ANY);
3039 * Now lets do a second pass looking for enabled syscalls without
3040 * an augmenter that have a signature that is a superset of another
3041 * syscall with an augmenter so that we can auto-reuse it.
3043 * I.e. if we have an augmenter for the "open" syscall that has
3046 * int open(const char *pathname, int flags, mode_t mode);
3048 * I.e. that will collect just the first string argument, then we
3049 * can reuse it for the 'creat' syscall, that has this signature:
3051 * int creat(const char *pathname, mode_t mode);
3055 * int stat(const char *pathname, struct stat *statbuf);
3056 * int lstat(const char *pathname, struct stat *statbuf);
3058 * Because the 'open' augmenter will collect the first arg as a string,
3059 * and leave alone all the other args, which already helps with
3060 * beautifying 'stat' and 'lstat''s pathname arg.
3062 * Then, in time, when 'stat' gets an augmenter that collects both
3063 * first and second arg (this one on the raw_syscalls:sys_exit prog
3064 * array tail call, then that one will be used.
3066 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
3067 struct syscall *sc = trace__syscall_info(trace, NULL, key);
3068 struct bpf_program *pair_prog;
3071 if (sc == NULL || sc->bpf_prog.sys_enter == NULL)
3075 * For now we're just reusing the sys_enter prog, and if it
3076 * already has an augmenter, we don't need to find one.
3078 if (sc->bpf_prog.sys_enter != trace->syscalls.unaugmented_prog)
3082 * Look at all the other syscalls for one that has a signature
3083 * that is close enough that we can share:
3085 pair_prog = trace__find_usable_bpf_prog_entry(trace, sc);
3086 if (pair_prog == NULL)
3089 sc->bpf_prog.sys_enter = pair_prog;
3092 * Update the BPF_MAP_TYPE_PROG_SHARED for raw_syscalls:sys_enter
3093 * with the fd for the program we're reusing:
3095 prog_fd = bpf_program__fd(sc->bpf_prog.sys_enter);
3096 err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY);
3105 static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
3110 static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
3115 static struct bpf_program *trace__find_bpf_program_by_title(struct trace *trace __maybe_unused,
3116 const char *name __maybe_unused)
3121 static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace __maybe_unused)
3125 #endif // HAVE_LIBBPF_SUPPORT
3127 static int trace__set_ev_qualifier_filter(struct trace *trace)
3129 if (trace->syscalls.map)
3130 return trace__set_ev_qualifier_bpf_filter(trace);
3131 if (trace->syscalls.events.sys_enter)
3132 return trace__set_ev_qualifier_tp_filter(trace);
3136 static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
3137 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
3140 #ifdef HAVE_LIBBPF_SUPPORT
3142 int map_fd = bpf_map__fd(map);
3145 for (i = 0; i < npids; ++i) {
3146 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
3154 static int trace__set_filter_loop_pids(struct trace *trace)
3156 unsigned int nr = 1, err;
3160 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
3162 while (thread && nr < ARRAY_SIZE(pids)) {
3163 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
3168 if (!strcmp(thread__comm_str(parent), "sshd") ||
3169 strstarts(thread__comm_str(parent), "gnome-terminal")) {
3170 pids[nr++] = parent->tid;
3176 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
3177 if (!err && trace->filter_pids.map)
3178 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
3183 static int trace__set_filter_pids(struct trace *trace)
3187 * Better not use !target__has_task() here because we need to cover the
3188 * case where no threads were specified in the command line, but a
3189 * workload was, and in that case we will fill in the thread_map when
3190 * we fork the workload in perf_evlist__prepare_workload.
3192 if (trace->filter_pids.nr > 0) {
3193 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
3194 trace->filter_pids.entries);
3195 if (!err && trace->filter_pids.map) {
3196 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
3197 trace->filter_pids.entries);
3199 } else if (perf_thread_map__pid(trace->evlist->core.threads, 0) == -1) {
3200 err = trace__set_filter_loop_pids(trace);
3206 static int __trace__deliver_event(struct trace *trace, union perf_event *event)
3208 struct evlist *evlist = trace->evlist;
3209 struct perf_sample sample;
3212 err = perf_evlist__parse_sample(evlist, event, &sample);
3214 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
3216 trace__handle_event(trace, event, &sample);
3221 static int __trace__flush_events(struct trace *trace)
3223 u64 first = ordered_events__first_time(&trace->oe.data);
3224 u64 flush = trace->oe.last - NSEC_PER_SEC;
3226 /* Is there some thing to flush.. */
3227 if (first && first < flush)
3228 return ordered_events__flush_time(&trace->oe.data, flush);
3233 static int trace__flush_events(struct trace *trace)
3235 return !trace->sort_events ? 0 : __trace__flush_events(trace);
3238 static int trace__deliver_event(struct trace *trace, union perf_event *event)
3242 if (!trace->sort_events)
3243 return __trace__deliver_event(trace, event);
3245 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
3246 if (err && err != -1)
3249 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
3253 return trace__flush_events(trace);
3256 static int ordered_events__deliver_event(struct ordered_events *oe,
3257 struct ordered_event *event)
3259 struct trace *trace = container_of(oe, struct trace, oe.data);
3261 return __trace__deliver_event(trace, event->event);
3264 static int trace__run(struct trace *trace, int argc, const char **argv)
3266 struct evlist *evlist = trace->evlist;
3267 struct evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
3269 unsigned long before;
3270 const bool forks = argc > 0;
3271 bool draining = false;
3275 if (!trace->raw_augmented_syscalls) {
3276 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
3277 goto out_error_raw_syscalls;
3279 if (trace->trace_syscalls)
3280 trace->vfs_getname = evlist__add_vfs_getname(evlist);
3283 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
3284 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
3285 if (pgfault_maj == NULL)
3287 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
3288 evlist__add(evlist, pgfault_maj);
3291 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
3292 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
3293 if (pgfault_min == NULL)
3295 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
3296 evlist__add(evlist, pgfault_min);
3300 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
3301 trace__sched_stat_runtime))
3302 goto out_error_sched_stat_runtime;
3305 * If a global cgroup was set, apply it to all the events without an
3306 * explicit cgroup. I.e.:
3308 * trace -G A -e sched:*switch
3310 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
3311 * _and_ sched:sched_switch to the 'A' cgroup, while:
3313 * trace -e sched:*switch -G A
3315 * will only set the sched:sched_switch event to the 'A' cgroup, all the
3316 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
3317 * a cgroup (on the root cgroup, sys wide, etc).
3321 * trace -G A -e sched:*switch -G B
3323 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
3324 * to the 'B' cgroup.
3326 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
3327 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
3330 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
3332 err = perf_evlist__create_maps(evlist, &trace->opts.target);
3334 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
3335 goto out_delete_evlist;
3338 err = trace__symbols_init(trace, evlist);
3340 fprintf(trace->output, "Problems initializing symbol libraries!\n");
3341 goto out_delete_evlist;
3344 perf_evlist__config(evlist, &trace->opts, &callchain_param);
3346 signal(SIGCHLD, sig_handler);
3347 signal(SIGINT, sig_handler);
3350 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
3353 fprintf(trace->output, "Couldn't run the workload!\n");
3354 goto out_delete_evlist;
3358 err = evlist__open(evlist);
3360 goto out_error_open;
3362 err = bpf__apply_obj_config();
3364 char errbuf[BUFSIZ];
3366 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
3367 pr_err("ERROR: Apply config to BPF failed: %s\n",
3369 goto out_error_open;
3372 err = trace__set_filter_pids(trace);
3376 if (trace->syscalls.map)
3377 trace__init_syscalls_bpf_map(trace);
3379 if (trace->syscalls.prog_array.sys_enter)
3380 trace__init_syscalls_bpf_prog_array_maps(trace);
3382 if (trace->ev_qualifier_ids.nr > 0) {
3383 err = trace__set_ev_qualifier_filter(trace);
3387 if (trace->syscalls.events.sys_exit) {
3388 pr_debug("event qualifier tracepoint filter: %s\n",
3389 trace->syscalls.events.sys_exit->filter);
3394 * If the "close" syscall is not traced, then we will not have the
3395 * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
3396 * fd->pathname table and were ending up showing the last value set by
3397 * syscalls opening a pathname and associating it with a descriptor or
3398 * reading it from /proc/pid/fd/ in cases where that doesn't make
3401 * So just disable this beautifier (SCA_FD, SCA_FDAT) when 'close' is
3404 trace->fd_path_disabled = !trace__syscall_enabled(trace, syscalltbl__id(trace->sctbl, "close"));
3406 err = perf_evlist__apply_filters(evlist, &evsel);
3408 goto out_error_apply_filters;
3410 if (trace->dump.map)
3411 bpf_map__fprintf(trace->dump.map, trace->output);
3413 err = evlist__mmap(evlist, trace->opts.mmap_pages);
3415 goto out_error_mmap;
3417 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
3418 evlist__enable(evlist);
3421 perf_evlist__start_workload(evlist);
3423 if (trace->opts.initial_delay) {
3424 usleep(trace->opts.initial_delay * 1000);
3425 evlist__enable(evlist);
3428 trace->multiple_threads = perf_thread_map__pid(evlist->core.threads, 0) == -1 ||
3429 evlist->core.threads->nr > 1 ||
3430 evlist__first(evlist)->core.attr.inherit;
3433 * Now that we already used evsel->core.attr to ask the kernel to setup the
3434 * events, lets reuse evsel->core.attr.sample_max_stack as the limit in
3435 * trace__resolve_callchain(), allowing per-event max-stack settings
3436 * to override an explicitly set --max-stack global setting.
3438 evlist__for_each_entry(evlist, evsel) {
3439 if (evsel__has_callchain(evsel) &&
3440 evsel->core.attr.sample_max_stack == 0)
3441 evsel->core.attr.sample_max_stack = trace->max_stack;
3444 before = trace->nr_events;
3446 for (i = 0; i < evlist->core.nr_mmaps; i++) {
3447 union perf_event *event;
3450 md = &evlist->mmap[i];
3451 if (perf_mmap__read_init(md) < 0)
3454 while ((event = perf_mmap__read_event(md)) != NULL) {
3457 err = trace__deliver_event(trace, event);
3461 perf_mmap__consume(md);
3466 if (done && !draining) {
3467 evlist__disable(evlist);
3471 perf_mmap__read_done(md);
3474 if (trace->nr_events == before) {
3475 int timeout = done ? 100 : -1;
3477 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
3478 if (evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
3483 if (trace__flush_events(trace))
3491 thread__zput(trace->current);
3493 evlist__disable(evlist);
3495 if (trace->sort_events)
3496 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
3500 trace__fprintf_thread_summary(trace, trace->output);
3502 if (trace->show_tool_stats) {
3503 fprintf(trace->output, "Stats:\n "
3504 " vfs_getname : %" PRIu64 "\n"
3505 " proc_getname: %" PRIu64 "\n",
3506 trace->stats.vfs_getname,
3507 trace->stats.proc_getname);
3512 trace__symbols__exit(trace);
3514 evlist__delete(evlist);
3515 cgroup__put(trace->cgroup);
3516 trace->evlist = NULL;
3517 trace->live = false;
3520 char errbuf[BUFSIZ];
3522 out_error_sched_stat_runtime:
3523 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
3526 out_error_raw_syscalls:
3527 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
3531 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3535 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3538 fprintf(trace->output, "%s\n", errbuf);
3539 goto out_delete_evlist;
3541 out_error_apply_filters:
3542 fprintf(trace->output,
3543 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3544 evsel->filter, perf_evsel__name(evsel), errno,
3545 str_error_r(errno, errbuf, sizeof(errbuf)));
3546 goto out_delete_evlist;
3549 fprintf(trace->output, "Not enough memory to run!\n");
3550 goto out_delete_evlist;
3553 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3554 goto out_delete_evlist;
3557 static int trace__replay(struct trace *trace)
3559 const struct evsel_str_handler handlers[] = {
3560 { "probe:vfs_getname", trace__vfs_getname, },
3562 struct perf_data data = {
3564 .mode = PERF_DATA_MODE_READ,
3565 .force = trace->force,
3567 struct perf_session *session;
3568 struct evsel *evsel;
3571 trace->tool.sample = trace__process_sample;
3572 trace->tool.mmap = perf_event__process_mmap;
3573 trace->tool.mmap2 = perf_event__process_mmap2;
3574 trace->tool.comm = perf_event__process_comm;
3575 trace->tool.exit = perf_event__process_exit;
3576 trace->tool.fork = perf_event__process_fork;
3577 trace->tool.attr = perf_event__process_attr;
3578 trace->tool.tracing_data = perf_event__process_tracing_data;
3579 trace->tool.build_id = perf_event__process_build_id;
3580 trace->tool.namespaces = perf_event__process_namespaces;
3582 trace->tool.ordered_events = true;
3583 trace->tool.ordering_requires_timestamps = true;
3585 /* add tid to output */
3586 trace->multiple_threads = true;
3588 session = perf_session__new(&data, false, &trace->tool);
3589 if (IS_ERR(session))
3590 return PTR_ERR(session);
3592 if (trace->opts.target.pid)
3593 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3595 if (trace->opts.target.tid)
3596 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3598 if (symbol__init(&session->header.env) < 0)
3601 trace->host = &session->machines.host;
3603 err = perf_session__set_tracepoints_handlers(session, handlers);
3607 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3608 "raw_syscalls:sys_enter");
3609 /* older kernels have syscalls tp versus raw_syscalls */
3611 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3612 "syscalls:sys_enter");
3615 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
3616 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
3617 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3621 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3622 "raw_syscalls:sys_exit");
3624 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3625 "syscalls:sys_exit");
3627 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
3628 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
3629 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
3633 evlist__for_each_entry(session->evlist, evsel) {
3634 if (evsel->core.attr.type == PERF_TYPE_SOFTWARE &&
3635 (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3636 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3637 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS))
3638 evsel->handler = trace__pgfault;
3643 err = perf_session__process_events(session);
3645 pr_err("Failed to process events, error %d", err);
3647 else if (trace->summary)
3648 trace__fprintf_thread_summary(trace, trace->output);
3651 perf_session__delete(session);
3656 static size_t trace__fprintf_threads_header(FILE *fp)
3660 printed = fprintf(fp, "\n Summary of events:\n\n");
3665 DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3666 struct stats *stats;
3671 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3672 struct stats *stats = source->priv;
3674 entry->syscall = source->i;
3675 entry->stats = stats;
3676 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3679 static size_t thread__dump_stats(struct thread_trace *ttrace,
3680 struct trace *trace, FILE *fp)
3685 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
3687 if (syscall_stats == NULL)
3690 printed += fprintf(fp, "\n");
3692 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3693 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3694 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
3696 resort_rb__for_each_entry(nd, syscall_stats) {
3697 struct stats *stats = syscall_stats_entry->stats;
3699 double min = (double)(stats->min) / NSEC_PER_MSEC;
3700 double max = (double)(stats->max) / NSEC_PER_MSEC;
3701 double avg = avg_stats(stats);
3703 u64 n = (u64) stats->n;
3705 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3706 avg /= NSEC_PER_MSEC;
3708 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
3709 printed += fprintf(fp, " %-15s", sc->name);
3710 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
3711 n, syscall_stats_entry->msecs, min, avg);
3712 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
3716 resort_rb__delete(syscall_stats);
3717 printed += fprintf(fp, "\n\n");
3722 static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
3725 struct thread_trace *ttrace = thread__priv(thread);
3731 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3733 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
3734 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
3735 printed += fprintf(fp, "%.1f%%", ratio);
3737 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3739 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
3741 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3742 else if (fputc('\n', fp) != EOF)
3745 printed += thread__dump_stats(ttrace, trace, fp);
3750 static unsigned long thread__nr_events(struct thread_trace *ttrace)
3752 return ttrace ? ttrace->nr_events : 0;
3755 DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3756 struct thread *thread;
3759 entry->thread = rb_entry(nd, struct thread, rb_node);
3762 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3764 size_t printed = trace__fprintf_threads_header(fp);
3768 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3769 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3771 if (threads == NULL) {
3772 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3776 resort_rb__for_each_entry(nd, threads)
3777 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3779 resort_rb__delete(threads);
3784 static int trace__set_duration(const struct option *opt, const char *str,
3785 int unset __maybe_unused)
3787 struct trace *trace = opt->value;
3789 trace->duration_filter = atof(str);
3793 static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3794 int unset __maybe_unused)
3798 struct trace *trace = opt->value;
3800 * FIXME: introduce a intarray class, plain parse csv and create a
3801 * { int nr, int entries[] } struct...
3803 struct intlist *list = intlist__new(str);
3808 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3809 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3811 if (trace->filter_pids.entries == NULL)
3814 trace->filter_pids.entries[0] = getpid();
3816 for (i = 1; i < trace->filter_pids.nr; ++i)
3817 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3819 intlist__delete(list);
3825 static int trace__open_output(struct trace *trace, const char *filename)
3829 if (!stat(filename, &st) && st.st_size) {
3830 char oldname[PATH_MAX];
3832 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3834 rename(filename, oldname);
3837 trace->output = fopen(filename, "w");
3839 return trace->output == NULL ? -errno : 0;
3842 static int parse_pagefaults(const struct option *opt, const char *str,
3843 int unset __maybe_unused)
3845 int *trace_pgfaults = opt->value;
3847 if (strcmp(str, "all") == 0)
3848 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3849 else if (strcmp(str, "maj") == 0)
3850 *trace_pgfaults |= TRACE_PFMAJ;
3851 else if (strcmp(str, "min") == 0)
3852 *trace_pgfaults |= TRACE_PFMIN;
3859 static void evlist__set_evsel_handler(struct evlist *evlist, void *handler)
3861 struct evsel *evsel;
3863 evlist__for_each_entry(evlist, evsel)
3864 evsel->handler = handler;
3867 static int evlist__set_syscall_tp_fields(struct evlist *evlist)
3869 struct evsel *evsel;
3871 evlist__for_each_entry(evlist, evsel) {
3872 if (evsel->priv || !evsel->tp_format)
3875 if (strcmp(evsel->tp_format->system, "syscalls"))
3878 if (perf_evsel__init_syscall_tp(evsel))
3881 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3882 struct syscall_tp *sc = evsel->priv;
3884 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3886 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3887 struct syscall_tp *sc = evsel->priv;
3889 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3898 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3899 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3900 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3902 * It'd be better to introduce a parse_options() variant that would return a
3903 * list with the terms it didn't match to an event...
3905 static int trace__parse_events_option(const struct option *opt, const char *str,
3906 int unset __maybe_unused)
3908 struct trace *trace = (struct trace *)opt->value;
3909 const char *s = str;
3910 char *sep = NULL, *lists[2] = { NULL, NULL, };
3911 int len = strlen(str) + 1, err = -1, list, idx;
3912 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3913 char group_name[PATH_MAX];
3914 struct syscall_fmt *fmt;
3916 if (strace_groups_dir == NULL)
3921 trace->not_ev_qualifier = true;
3925 if ((sep = strchr(s, ',')) != NULL)
3929 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3930 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
3935 fmt = syscall_fmt__find_by_alias(s);
3940 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3941 if (access(group_name, R_OK) == 0)
3946 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3948 lists[list] = malloc(len);
3949 if (lists[list] == NULL)
3951 strcpy(lists[list], s);
3961 if (lists[1] != NULL) {
3962 struct strlist_config slist_config = {
3963 .dirname = strace_groups_dir,
3966 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3967 if (trace->ev_qualifier == NULL) {
3968 fputs("Not enough memory to parse event qualifier", trace->output);
3972 if (trace__validate_ev_qualifier(trace))
3974 trace->trace_syscalls = true;
3980 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3981 "event selector. use 'perf list' to list available events",
3982 parse_events_option);
3983 err = parse_events_option(&o, lists[0], 0);
3992 static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3994 struct trace *trace = opt->value;
3996 if (!list_empty(&trace->evlist->core.entries))
3997 return parse_cgroups(opt, str, unset);
3999 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
4004 static struct bpf_map *trace__find_bpf_map_by_name(struct trace *trace, const char *name)
4006 if (trace->bpf_obj == NULL)
4009 return bpf_object__find_map_by_name(trace->bpf_obj, name);
4012 static void trace__set_bpf_map_filtered_pids(struct trace *trace)
4014 trace->filter_pids.map = trace__find_bpf_map_by_name(trace, "pids_filtered");
4017 static void trace__set_bpf_map_syscalls(struct trace *trace)
4019 trace->syscalls.map = trace__find_bpf_map_by_name(trace, "syscalls");
4020 trace->syscalls.prog_array.sys_enter = trace__find_bpf_map_by_name(trace, "syscalls_sys_enter");
4021 trace->syscalls.prog_array.sys_exit = trace__find_bpf_map_by_name(trace, "syscalls_sys_exit");
4024 static int trace__config(const char *var, const char *value, void *arg)
4026 struct trace *trace = arg;
4029 if (!strcmp(var, "trace.add_events")) {
4030 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
4031 "event selector. use 'perf list' to list available events",
4032 parse_events_option);
4034 * We can't propagate parse_event_option() return, as it is 1
4035 * for failure while perf_config() expects -1.
4037 if (parse_events_option(&o, value, 0))
4039 } else if (!strcmp(var, "trace.show_timestamp")) {
4040 trace->show_tstamp = perf_config_bool(var, value);
4041 } else if (!strcmp(var, "trace.show_duration")) {
4042 trace->show_duration = perf_config_bool(var, value);
4043 } else if (!strcmp(var, "trace.show_arg_names")) {
4044 trace->show_arg_names = perf_config_bool(var, value);
4045 if (!trace->show_arg_names)
4046 trace->show_zeros = true;
4047 } else if (!strcmp(var, "trace.show_zeros")) {
4048 bool new_show_zeros = perf_config_bool(var, value);
4049 if (!trace->show_arg_names && !new_show_zeros) {
4050 pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
4053 trace->show_zeros = new_show_zeros;
4054 } else if (!strcmp(var, "trace.show_prefix")) {
4055 trace->show_string_prefix = perf_config_bool(var, value);
4056 } else if (!strcmp(var, "trace.no_inherit")) {
4057 trace->opts.no_inherit = perf_config_bool(var, value);
4058 } else if (!strcmp(var, "trace.args_alignment")) {
4059 int args_alignment = 0;
4060 if (perf_config_int(&args_alignment, var, value) == 0)
4061 trace->args_alignment = args_alignment;
4067 int cmd_trace(int argc, const char **argv)
4069 const char *trace_usage[] = {
4070 "perf trace [<options>] [<command>]",
4071 "perf trace [<options>] -- <command> [<options>]",
4072 "perf trace record [<options>] [<command>]",
4073 "perf trace record [<options>] -- <command> [<options>]",
4076 struct trace trace = {
4082 .user_freq = UINT_MAX,
4083 .user_interval = ULLONG_MAX,
4084 .no_buffering = true,
4085 .mmap_pages = UINT_MAX,
4089 .show_tstamp = true,
4090 .show_duration = true,
4091 .show_arg_names = true,
4092 .args_alignment = 70,
4093 .trace_syscalls = false,
4094 .kernel_syscallchains = false,
4095 .max_stack = UINT_MAX,
4096 .max_events = ULONG_MAX,
4098 const char *map_dump_str = NULL;
4099 const char *output_name = NULL;
4100 const struct option trace_options[] = {
4101 OPT_CALLBACK('e', "event", &trace, "event",
4102 "event/syscall selector. use 'perf list' to list available events",
4103 trace__parse_events_option),
4104 OPT_BOOLEAN(0, "comm", &trace.show_comm,
4105 "show the thread COMM next to its id"),
4106 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
4107 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
4108 trace__parse_events_option),
4109 OPT_STRING('o', "output", &output_name, "file", "output file name"),
4110 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
4111 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
4112 "trace events on existing process id"),
4113 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
4114 "trace events on existing thread id"),
4115 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
4116 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
4117 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
4118 "system-wide collection from all CPUs"),
4119 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
4120 "list of cpus to monitor"),
4121 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
4122 "child tasks do not inherit counters"),
4123 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
4124 "number of mmap data pages",
4125 perf_evlist__parse_mmap_pages),
4126 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
4128 OPT_CALLBACK(0, "duration", &trace, "float",
4129 "show only events with duration > N.M ms",
4130 trace__set_duration),
4131 #ifdef HAVE_LIBBPF_SUPPORT
4132 OPT_STRING(0, "map-dump", &map_dump_str, "BPF map", "BPF map to periodically dump"),
4134 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
4135 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
4136 OPT_BOOLEAN('T', "time", &trace.full_time,
4137 "Show full timestamp, not time relative to first start"),
4138 OPT_BOOLEAN(0, "failure", &trace.failure_only,
4139 "Show only syscalls that failed"),
4140 OPT_BOOLEAN('s', "summary", &trace.summary_only,
4141 "Show only syscall summary with statistics"),
4142 OPT_BOOLEAN('S', "with-summary", &trace.summary,
4143 "Show all syscalls and summary with statistics"),
4144 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
4145 "Trace pagefaults", parse_pagefaults, "maj"),
4146 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
4147 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
4148 OPT_CALLBACK(0, "call-graph", &trace.opts,
4149 "record_mode[,record_size]", record_callchain_help,
4150 &record_parse_callchain_opt),
4151 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
4152 "Show the kernel callchains on the syscall exit path"),
4153 OPT_ULONG(0, "max-events", &trace.max_events,
4154 "Set the maximum number of events to print, exit after that is reached. "),
4155 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
4156 "Set the minimum stack depth when parsing the callchain, "
4157 "anything below the specified depth will be ignored."),
4158 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
4159 "Set the maximum stack depth when parsing the callchain, "
4160 "anything beyond the specified depth will be ignored. "
4161 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
4162 OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
4163 "Sort batch of events before processing, use if getting out of order events"),
4164 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
4165 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
4166 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
4167 "per thread proc mmap processing timeout in ms"),
4168 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
4169 trace__parse_cgroups),
4170 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
4171 "ms to wait before starting measurement after program "
4173 OPTS_EVSWITCH(&trace.evswitch),
4176 bool __maybe_unused max_stack_user_set = true;
4177 bool mmap_pages_user_set = true;
4178 struct evsel *evsel;
4179 const char * const trace_subcommands[] = { "record", NULL };
4183 signal(SIGSEGV, sighandler_dump_stack);
4184 signal(SIGFPE, sighandler_dump_stack);
4186 trace.evlist = evlist__new();
4187 trace.sctbl = syscalltbl__new();
4189 if (trace.evlist == NULL || trace.sctbl == NULL) {
4190 pr_err("Not enough memory to run!\n");
4196 * Parsing .perfconfig may entail creating a BPF event, that may need
4197 * to create BPF maps, so bump RLIM_MEMLOCK as the default 64K setting
4198 * is too small. This affects just this process, not touching the
4199 * global setting. If it fails we'll get something in 'perf trace -v'
4200 * to help diagnose the problem.
4202 rlimit__bump_memlock();
4204 err = perf_config(trace__config, &trace);
4208 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
4209 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
4211 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
4212 usage_with_options_msg(trace_usage, trace_options,
4213 "cgroup monitoring only available in system-wide mode");
4216 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
4217 if (IS_ERR(evsel)) {
4218 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
4219 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
4224 trace.syscalls.events.augmented = evsel;
4226 evsel = perf_evlist__find_tracepoint_by_name(trace.evlist, "raw_syscalls:sys_enter");
4227 if (evsel == NULL) {
4228 pr_err("ERROR: raw_syscalls:sys_enter not found in the augmented BPF object\n");
4232 if (evsel->bpf_obj == NULL) {
4233 pr_err("ERROR: raw_syscalls:sys_enter not associated to a BPF object\n");
4237 trace.bpf_obj = evsel->bpf_obj;
4239 trace__set_bpf_map_filtered_pids(&trace);
4240 trace__set_bpf_map_syscalls(&trace);
4241 trace.syscalls.unaugmented_prog = trace__find_bpf_program_by_title(&trace, "!raw_syscalls:unaugmented");
4244 err = bpf__setup_stdout(trace.evlist);
4246 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
4247 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
4254 trace.dump.map = trace__find_bpf_map_by_name(&trace, map_dump_str);
4255 if (trace.dump.map == NULL) {
4256 pr_err("ERROR: BPF map \"%s\" not found\n", map_dump_str);
4261 if (trace.trace_pgfaults) {
4262 trace.opts.sample_address = true;
4263 trace.opts.sample_time = true;
4266 if (trace.opts.mmap_pages == UINT_MAX)
4267 mmap_pages_user_set = false;
4269 if (trace.max_stack == UINT_MAX) {
4270 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
4271 max_stack_user_set = false;
4274 #ifdef HAVE_DWARF_UNWIND_SUPPORT
4275 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
4276 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
4280 if (callchain_param.enabled) {
4281 if (!mmap_pages_user_set && geteuid() == 0)
4282 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
4284 symbol_conf.use_callchain = true;
4287 if (trace.evlist->core.nr_entries > 0) {
4288 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
4289 if (evlist__set_syscall_tp_fields(trace.evlist)) {
4290 perror("failed to set syscalls:* tracepoint fields");
4295 if (trace.sort_events) {
4296 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
4297 ordered_events__set_copy_on_queue(&trace.oe.data, true);
4301 * If we are augmenting syscalls, then combine what we put in the
4302 * __augmented_syscalls__ BPF map with what is in the
4303 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
4304 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
4306 * We'll switch to look at two BPF maps, one for sys_enter and the
4307 * other for sys_exit when we start augmenting the sys_exit paths with
4308 * buffers that are being copied from kernel to userspace, think 'read'
4311 if (trace.syscalls.events.augmented) {
4312 evlist__for_each_entry(trace.evlist, evsel) {
4313 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
4315 if (raw_syscalls_sys_exit) {
4316 trace.raw_augmented_syscalls = true;
4317 goto init_augmented_syscall_tp;
4320 if (trace.syscalls.events.augmented->priv == NULL &&
4321 strstr(perf_evsel__name(evsel), "syscalls:sys_enter")) {
4322 struct evsel *augmented = trace.syscalls.events.augmented;
4323 if (perf_evsel__init_augmented_syscall_tp(augmented, evsel) ||
4324 perf_evsel__init_augmented_syscall_tp_args(augmented))
4327 * Augmented is __augmented_syscalls__ BPF_OUTPUT event
4328 * Above we made sure we can get from the payload the tp fields
4329 * that we get from syscalls:sys_enter tracefs format file.
4331 augmented->handler = trace__sys_enter;
4333 * Now we do the same for the *syscalls:sys_enter event so that
4334 * if we handle it directly, i.e. if the BPF prog returns 0 so
4335 * as not to filter it, then we'll handle it just like we would
4336 * for the BPF_OUTPUT one:
4338 if (perf_evsel__init_augmented_syscall_tp(evsel, evsel) ||
4339 perf_evsel__init_augmented_syscall_tp_args(evsel))
4341 evsel->handler = trace__sys_enter;
4344 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
4345 struct syscall_tp *sc;
4346 init_augmented_syscall_tp:
4347 if (perf_evsel__init_augmented_syscall_tp(evsel, evsel))
4351 * For now with BPF raw_augmented we hook into
4352 * raw_syscalls:sys_enter and there we get all
4353 * 6 syscall args plus the tracepoint common
4354 * fields and the syscall_nr (another long).
4355 * So we check if that is the case and if so
4356 * don't look after the sc->args_size but
4357 * always after the full raw_syscalls:sys_enter
4358 * payload, which is fixed.
4360 * We'll revisit this later to pass
4361 * s->args_size to the BPF augmenter (now
4362 * tools/perf/examples/bpf/augmented_raw_syscalls.c,
4363 * so that it copies only what we need for each
4364 * syscall, like what happens when we use
4365 * syscalls:sys_enter_NAME, so that we reduce
4366 * the kernel/userspace traffic to just what is
4367 * needed for each syscall.
4369 if (trace.raw_augmented_syscalls)
4370 trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset;
4371 perf_evsel__init_augmented_syscall_tp_ret(evsel);
4372 evsel->handler = trace__sys_exit;
4377 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
4378 return trace__record(&trace, argc-1, &argv[1]);
4380 /* summary_only implies summary option, but don't overwrite summary if set */
4381 if (trace.summary_only)
4382 trace.summary = trace.summary_only;
4384 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
4385 trace.evlist->core.nr_entries == 0 /* Was --events used? */) {
4386 trace.trace_syscalls = true;
4389 if (output_name != NULL) {
4390 err = trace__open_output(&trace, output_name);
4392 perror("failed to create output file");
4397 err = evswitch__init(&trace.evswitch, trace.evlist, stderr);
4401 err = target__validate(&trace.opts.target);
4403 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
4404 fprintf(trace.output, "%s", bf);
4408 err = target__parse_uid(&trace.opts.target);
4410 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
4411 fprintf(trace.output, "%s", bf);
4415 if (!argc && target__none(&trace.opts.target))
4416 trace.opts.target.system_wide = true;
4419 err = trace__replay(&trace);
4421 err = trace__run(&trace, argc, argv);
4424 if (output_name != NULL)
4425 fclose(trace.output);