1 // SPDX-License-Identifier: GPL-2.0-only
5 * Builtin 'trace' command:
7 * Display a continuously updated trace of any workload, CPU, specific PID,
8 * system wide, etc. Default format is loosely strace like, but any other
9 * event may be specified using --event.
11 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
13 * Initially based on the 'trace' prototype by Thomas Gleixner:
15 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
18 #include <traceevent/event-parse.h>
19 #include <api/fs/tracing_path.h>
21 #include "util/bpf_map.h"
23 #include "util/cgroup.h"
24 #include "util/color.h"
25 #include "util/config.h"
26 #include "util/debug.h"
28 #include "util/event.h"
29 #include "util/evlist.h"
30 #include <subcmd/exec-cmd.h>
31 #include "util/machine.h"
33 #include "util/symbol.h"
34 #include "util/path.h"
35 #include "util/session.h"
36 #include "util/thread.h"
37 #include <subcmd/parse-options.h>
38 #include "util/strlist.h"
39 #include "util/intlist.h"
40 #include "util/thread_map.h"
41 #include "util/stat.h"
42 #include "trace/beauty/beauty.h"
43 #include "trace-event.h"
44 #include "util/parse-events.h"
45 #include "util/bpf-loader.h"
46 #include "callchain.h"
47 #include "print_binary.h"
49 #include "syscalltbl.h"
50 #include "rb_resort.h"
58 #include <linux/err.h>
59 #include <linux/filter.h>
60 #include <linux/kernel.h>
61 #include <linux/random.h>
62 #include <linux/stringify.h>
63 #include <linux/time64.h>
65 #include <sys/sysmacros.h>
67 #include "sane_ctype.h"
70 # define O_CLOEXEC 02000000
73 #ifndef F_LINUX_SPECIFIC_BASE
74 # define F_LINUX_SPECIFIC_BASE 1024
78 struct perf_tool tool;
79 struct syscalltbl *sctbl;
82 struct syscall *table;
85 struct perf_evsel *sys_enter,
93 struct record_opts opts;
94 struct perf_evlist *evlist;
96 struct thread *current;
97 struct cgroup *cgroup;
100 unsigned long nr_events;
101 unsigned long nr_events_printed;
102 unsigned long max_events;
103 struct strlist *ev_qualifier;
113 double duration_filter;
119 unsigned int max_stack;
120 unsigned int min_stack;
121 int raw_augmented_syscalls_args_size;
122 bool raw_augmented_syscalls;
124 bool not_ev_qualifier;
128 bool multiple_threads;
134 bool show_tool_stats;
136 bool kernel_syscallchains;
142 bool show_string_prefix;
147 struct ordered_events data;
155 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
156 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
160 #define TP_UINT_FIELD(bits) \
161 static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
164 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
173 #define TP_UINT_FIELD__SWAPPED(bits) \
174 static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
177 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
178 return bswap_##bits(value);\
181 TP_UINT_FIELD__SWAPPED(16);
182 TP_UINT_FIELD__SWAPPED(32);
183 TP_UINT_FIELD__SWAPPED(64);
185 static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap)
187 field->offset = offset;
191 field->integer = tp_field__u8;
194 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
197 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
200 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
209 static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap)
211 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
214 static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
216 return sample->raw_data + field->offset;
219 static int __tp_field__init_ptr(struct tp_field *field, int offset)
221 field->offset = offset;
222 field->pointer = tp_field__ptr;
226 static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
228 return __tp_field__init_ptr(field, format_field->offset);
234 struct tp_field args, ret;
238 static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
239 struct tp_field *field,
242 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
244 if (format_field == NULL)
247 return tp_field__init_uint(field, format_field, evsel->needs_swap);
250 #define perf_evsel__init_sc_tp_uint_field(evsel, name) \
251 ({ struct syscall_tp *sc = evsel->priv;\
252 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
254 static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
255 struct tp_field *field,
258 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
260 if (format_field == NULL)
263 return tp_field__init_ptr(field, format_field);
266 #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
267 ({ struct syscall_tp *sc = evsel->priv;\
268 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
270 static void perf_evsel__delete_priv(struct perf_evsel *evsel)
273 perf_evsel__delete(evsel);
276 static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
278 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
280 if (evsel->priv != NULL) {
281 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
282 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
293 static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel, struct perf_evsel *tp)
295 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
297 if (evsel->priv != NULL) {
298 struct tep_format_field *syscall_id = perf_evsel__field(tp, "id");
299 if (syscall_id == NULL)
300 syscall_id = perf_evsel__field(tp, "__syscall_nr");
301 if (syscall_id == NULL)
303 if (__tp_field__init_uint(&sc->id, syscall_id->size, syscall_id->offset, evsel->needs_swap))
315 static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
317 struct syscall_tp *sc = evsel->priv;
319 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
322 static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
324 struct syscall_tp *sc = evsel->priv;
326 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
329 static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
331 evsel->priv = malloc(sizeof(struct syscall_tp));
332 if (evsel->priv != NULL) {
333 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
336 evsel->handler = handler;
347 static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
349 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
351 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
353 evsel = perf_evsel__newtp("syscalls", direction);
358 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
364 perf_evsel__delete_priv(evsel);
368 #define perf_evsel__sc_tp_uint(evsel, name, sample) \
369 ({ struct syscall_tp *fields = evsel->priv; \
370 fields->name.integer(&fields->name, sample); })
372 #define perf_evsel__sc_tp_ptr(evsel, name, sample) \
373 ({ struct syscall_tp *fields = evsel->priv; \
374 fields->name.pointer(&fields->name, sample); })
376 size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
378 int idx = val - sa->offset;
380 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
381 size_t printed = scnprintf(bf, size, intfmt, val);
383 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
387 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
390 static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
392 struct syscall_arg *arg)
394 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
397 static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
398 struct syscall_arg *arg)
400 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
403 #define SCA_STRARRAY syscall_arg__scnprintf_strarray
405 size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
410 for (i = 0; i < sas->nr_entries; ++i) {
411 struct strarray *sa = sas->entries[i];
412 int idx = val - sa->offset;
414 if (idx >= 0 && idx < sa->nr_entries) {
415 if (sa->entries[idx] == NULL)
417 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
421 printed = scnprintf(bf, size, intfmt, val);
423 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
427 size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
428 struct syscall_arg *arg)
430 return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
434 #define AT_FDCWD -100
437 static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
438 struct syscall_arg *arg)
441 const char *prefix = "AT_FD";
444 return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
446 return syscall_arg__scnprintf_fd(bf, size, arg);
449 #define SCA_FDAT syscall_arg__scnprintf_fd_at
451 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
452 struct syscall_arg *arg);
454 #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
456 size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
458 return scnprintf(bf, size, "%#lx", arg->val);
461 size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
464 return scnprintf(bf, size, "NULL");
465 return syscall_arg__scnprintf_hex(bf, size, arg);
468 size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
470 return scnprintf(bf, size, "%d", arg->val);
473 size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
475 return scnprintf(bf, size, "%ld", arg->val);
478 static const char *bpf_cmd[] = {
479 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
480 "MAP_GET_NEXT_KEY", "PROG_LOAD",
482 static DEFINE_STRARRAY(bpf_cmd, "BPF_");
484 static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
485 static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
487 static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
488 static DEFINE_STRARRAY(itimers, "ITIMER_");
490 static const char *keyctl_options[] = {
491 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
492 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
493 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
494 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
495 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
497 static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
499 static const char *whences[] = { "SET", "CUR", "END",
507 static DEFINE_STRARRAY(whences, "SEEK_");
509 static const char *fcntl_cmds[] = {
510 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
511 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
512 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
515 static DEFINE_STRARRAY(fcntl_cmds, "F_");
517 static const char *fcntl_linux_specific_cmds[] = {
518 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
519 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
520 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
523 static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
525 static struct strarray *fcntl_cmds_arrays[] = {
526 &strarray__fcntl_cmds,
527 &strarray__fcntl_linux_specific_cmds,
530 static DEFINE_STRARRAYS(fcntl_cmds_arrays);
532 static const char *rlimit_resources[] = {
533 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
534 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
537 static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
539 static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
540 static DEFINE_STRARRAY(sighow, "SIG_");
542 static const char *clockid[] = {
543 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
544 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
545 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
547 static DEFINE_STRARRAY(clockid, "CLOCK_");
549 static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
550 struct syscall_arg *arg)
552 bool show_prefix = arg->show_string_prefix;
553 const char *suffix = "_OK";
557 if (mode == F_OK) /* 0 */
558 return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
560 if (mode & n##_OK) { \
561 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
571 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
576 #define SCA_ACCMODE syscall_arg__scnprintf_access_mode
578 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
579 struct syscall_arg *arg);
581 #define SCA_FILENAME syscall_arg__scnprintf_filename
583 static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
584 struct syscall_arg *arg)
586 bool show_prefix = arg->show_string_prefix;
587 const char *prefix = "O_";
588 int printed = 0, flags = arg->val;
591 if (flags & O_##n) { \
592 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
601 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
606 #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
608 #ifndef GRND_NONBLOCK
609 #define GRND_NONBLOCK 0x0001
612 #define GRND_RANDOM 0x0002
615 static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
616 struct syscall_arg *arg)
618 bool show_prefix = arg->show_string_prefix;
619 const char *prefix = "GRND_";
620 int printed = 0, flags = arg->val;
623 if (flags & GRND_##n) { \
624 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
625 flags &= ~GRND_##n; \
633 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
638 #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
640 #define STRARRAY(name, array) \
641 { .scnprintf = SCA_STRARRAY, \
642 .parm = &strarray__##array, }
644 #include "trace/beauty/arch_errno_names.c"
645 #include "trace/beauty/eventfd.c"
646 #include "trace/beauty/futex_op.c"
647 #include "trace/beauty/futex_val3.c"
648 #include "trace/beauty/mmap.c"
649 #include "trace/beauty/mode_t.c"
650 #include "trace/beauty/msg_flags.c"
651 #include "trace/beauty/open_flags.c"
652 #include "trace/beauty/perf_event_open.c"
653 #include "trace/beauty/pid.c"
654 #include "trace/beauty/sched_policy.c"
655 #include "trace/beauty/seccomp.c"
656 #include "trace/beauty/signum.c"
657 #include "trace/beauty/socket_type.c"
658 #include "trace/beauty/waitid_options.c"
660 struct syscall_arg_fmt {
661 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
662 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
668 static struct syscall_fmt {
671 struct syscall_arg_fmt arg[6];
678 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
679 { .name = "arch_prctl",
680 .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
681 [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
683 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
685 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
686 { .name = "brk", .hexret = true,
687 .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
688 { .name = "clock_gettime",
689 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
690 { .name = "clone", .errpid = true, .nr_args = 5,
691 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
692 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
693 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
694 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
695 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
697 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
699 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
700 { .name = "epoll_ctl",
701 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
702 { .name = "eventfd2",
703 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
704 { .name = "fchmodat",
705 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
706 { .name = "fchownat",
707 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
709 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
710 .parm = &strarrays__fcntl_cmds_arrays,
711 .show_zero = true, },
712 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
714 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
715 { .name = "fstat", .alias = "newfstat", },
716 { .name = "fstatat", .alias = "newfstatat", },
718 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
719 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
720 { .name = "futimesat",
721 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
722 { .name = "getitimer",
723 .arg = { [0] = STRARRAY(which, itimers), }, },
724 { .name = "getpid", .errpid = true, },
725 { .name = "getpgid", .errpid = true, },
726 { .name = "getppid", .errpid = true, },
727 { .name = "getrandom",
728 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
729 { .name = "getrlimit",
730 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
731 { .name = "gettid", .errpid = true, },
734 #if defined(__i386__) || defined(__x86_64__)
736 * FIXME: Make this available to all arches.
738 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
739 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
741 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
743 { .name = "kcmp", .nr_args = 5,
744 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
745 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
746 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
747 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
748 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
750 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
752 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
754 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
756 .arg = { [2] = STRARRAY(whence, whences), }, },
757 { .name = "lstat", .alias = "newlstat", },
759 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
760 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
762 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
764 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
765 { .name = "mmap", .hexret = true,
766 /* The standard mmap maps to old_mmap on s390x */
767 #if defined(__s390x__)
770 .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
771 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ },
772 [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, },
774 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
775 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
776 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
777 { .name = "mprotect",
778 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
779 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
780 { .name = "mq_unlink",
781 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
782 { .name = "mremap", .hexret = true,
783 .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
784 { .name = "name_to_handle_at",
785 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
786 { .name = "newfstatat",
787 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
789 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
790 { .name = "open_by_handle_at",
791 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
792 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
794 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
795 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
796 { .name = "perf_event_open",
797 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
798 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
799 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
801 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
802 { .name = "pkey_alloc",
803 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
804 { .name = "pkey_free",
805 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
806 { .name = "pkey_mprotect",
807 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
808 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
809 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
810 { .name = "poll", .timeout = true, },
811 { .name = "ppoll", .timeout = true, },
813 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
814 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
815 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
816 { .name = "pread", .alias = "pread64", },
817 { .name = "preadv", .alias = "pread", },
818 { .name = "prlimit64",
819 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
820 { .name = "pwrite", .alias = "pwrite64", },
821 { .name = "readlinkat",
822 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
823 { .name = "recvfrom",
824 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
825 { .name = "recvmmsg",
826 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
828 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
829 { .name = "renameat",
830 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
831 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
832 { .name = "renameat2",
833 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
834 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
835 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
836 { .name = "rt_sigaction",
837 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
838 { .name = "rt_sigprocmask",
839 .arg = { [0] = STRARRAY(how, sighow), }, },
840 { .name = "rt_sigqueueinfo",
841 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
842 { .name = "rt_tgsigqueueinfo",
843 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
844 { .name = "sched_setscheduler",
845 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
847 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
848 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
849 { .name = "select", .timeout = true, },
850 { .name = "sendmmsg",
851 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
853 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
855 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
856 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
857 { .name = "set_tid_address", .errpid = true, },
858 { .name = "setitimer",
859 .arg = { [0] = STRARRAY(which, itimers), }, },
860 { .name = "setrlimit",
861 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
863 .arg = { [0] = STRARRAY(family, socket_families),
864 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
865 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
866 { .name = "socketpair",
867 .arg = { [0] = STRARRAY(family, socket_families),
868 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
869 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
870 { .name = "stat", .alias = "newstat", },
872 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
873 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
874 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
876 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
878 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
879 { .name = "symlinkat",
880 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
882 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
884 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
885 { .name = "umount2", .alias = "umount",
886 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
887 { .name = "uname", .alias = "newuname", },
888 { .name = "unlinkat",
889 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
890 { .name = "utimensat",
891 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
892 { .name = "wait4", .errpid = true,
893 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
894 { .name = "waitid", .errpid = true,
895 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
898 static int syscall_fmt__cmp(const void *name, const void *fmtp)
900 const struct syscall_fmt *fmt = fmtp;
901 return strcmp(name, fmt->name);
904 static struct syscall_fmt *syscall_fmt__find(const char *name)
906 const int nmemb = ARRAY_SIZE(syscall_fmts);
907 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
910 static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
912 int i, nmemb = ARRAY_SIZE(syscall_fmts);
914 for (i = 0; i < nmemb; ++i) {
915 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
916 return &syscall_fmts[i];
923 * is_exit: is this "exit" or "exit_group"?
924 * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter.
925 * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc.
928 struct tep_event *tp_format;
933 struct tep_format_field *args;
935 struct syscall_fmt *fmt;
936 struct syscall_arg_fmt *arg_fmt;
939 struct bpf_map_syscall_entry {
944 * We need to have this 'calculated' boolean because in some cases we really
945 * don't know what is the duration of a syscall, for instance, when we start
946 * a session and some threads are waiting for a syscall to finish, say 'poll',
947 * in which case all we can do is to print "( ? ) for duration and for the
950 static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
952 double duration = (double)t / NSEC_PER_MSEC;
953 size_t printed = fprintf(fp, "(");
956 printed += fprintf(fp, " ");
957 else if (duration >= 1.0)
958 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
959 else if (duration >= 0.01)
960 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
962 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
963 return printed + fprintf(fp, "): ");
967 * filename.ptr: The filename char pointer that will be vfs_getname'd
968 * filename.entry_str_pos: Where to insert the string translated from
969 * filename.ptr by the vfs_getname tracepoint/kprobe.
970 * ret_scnprintf: syscall args may set this to a different syscall return
971 * formatter, for instance, fcntl may return fds, file flags, etc.
973 struct thread_trace {
976 unsigned long nr_events;
977 unsigned long pfmaj, pfmin;
980 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
983 short int entry_str_pos;
985 unsigned int namelen;
993 struct intlist *syscall_stats;
996 static struct thread_trace *thread_trace__new(void)
998 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
1001 ttrace->files.max = -1;
1003 ttrace->syscall_stats = intlist__new(NULL);
1008 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
1010 struct thread_trace *ttrace;
1015 if (thread__priv(thread) == NULL)
1016 thread__set_priv(thread, thread_trace__new());
1018 if (thread__priv(thread) == NULL)
1021 ttrace = thread__priv(thread);
1022 ++ttrace->nr_events;
1026 color_fprintf(fp, PERF_COLOR_RED,
1027 "WARNING: not enough memory, dropping samples!\n");
1032 void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
1033 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
1035 struct thread_trace *ttrace = thread__priv(arg->thread);
1037 ttrace->ret_scnprintf = ret_scnprintf;
1040 #define TRACE_PFMAJ (1 << 0)
1041 #define TRACE_PFMIN (1 << 1)
1043 static const size_t trace__entry_str_size = 2048;
1045 static struct file *thread_trace__files_entry(struct thread_trace *ttrace, int fd)
1050 if (fd > ttrace->files.max) {
1051 struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file));
1056 if (ttrace->files.max != -1) {
1057 memset(nfiles + ttrace->files.max + 1, 0,
1058 (fd - ttrace->files.max) * sizeof(struct file));
1060 memset(nfiles, 0, (fd + 1) * sizeof(struct file));
1063 ttrace->files.table = nfiles;
1064 ttrace->files.max = fd;
1067 return ttrace->files.table + fd;
1070 struct file *thread__files_entry(struct thread *thread, int fd)
1072 return thread_trace__files_entry(thread__priv(thread), fd);
1075 static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
1077 struct thread_trace *ttrace = thread__priv(thread);
1078 struct file *file = thread_trace__files_entry(ttrace, fd);
1082 if (stat(pathname, &st) == 0)
1083 file->dev_maj = major(st.st_rdev);
1084 file->pathname = strdup(pathname);
1092 static int thread__read_fd_path(struct thread *thread, int fd)
1094 char linkname[PATH_MAX], pathname[PATH_MAX];
1098 if (thread->pid_ == thread->tid) {
1099 scnprintf(linkname, sizeof(linkname),
1100 "/proc/%d/fd/%d", thread->pid_, fd);
1102 scnprintf(linkname, sizeof(linkname),
1103 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1106 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1109 ret = readlink(linkname, pathname, sizeof(pathname));
1111 if (ret < 0 || ret > st.st_size)
1114 pathname[ret] = '\0';
1115 return trace__set_fd_pathname(thread, fd, pathname);
1118 static const char *thread__fd_path(struct thread *thread, int fd,
1119 struct trace *trace)
1121 struct thread_trace *ttrace = thread__priv(thread);
1129 if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) {
1132 ++trace->stats.proc_getname;
1133 if (thread__read_fd_path(thread, fd))
1137 return ttrace->files.table[fd].pathname;
1140 size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
1143 size_t printed = scnprintf(bf, size, "%d", fd);
1144 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
1147 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1152 size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1154 size_t printed = scnprintf(bf, size, "%d", fd);
1155 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1158 const char *path = thread__fd_path(thread, fd, trace);
1161 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1163 thread__put(thread);
1169 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1170 struct syscall_arg *arg)
1173 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
1174 struct thread_trace *ttrace = thread__priv(arg->thread);
1176 if (ttrace && fd >= 0 && fd <= ttrace->files.max)
1177 zfree(&ttrace->files.table[fd].pathname);
1182 static void thread__set_filename_pos(struct thread *thread, const char *bf,
1185 struct thread_trace *ttrace = thread__priv(thread);
1187 ttrace->filename.ptr = ptr;
1188 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1191 static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1193 struct augmented_arg *augmented_arg = arg->augmented.args;
1195 return scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
1198 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1199 struct syscall_arg *arg)
1201 unsigned long ptr = arg->val;
1203 if (arg->augmented.args)
1204 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1206 if (!arg->trace->vfs_getname)
1207 return scnprintf(bf, size, "%#x", ptr);
1209 thread__set_filename_pos(arg->thread, bf, ptr);
1213 static bool trace__filter_duration(struct trace *trace, double t)
1215 return t < (trace->duration_filter * NSEC_PER_MSEC);
1218 static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1220 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1222 return fprintf(fp, "%10.3f ", ts);
1226 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1227 * using ttrace->entry_time for a thread that receives a sys_exit without
1228 * first having received a sys_enter ("poll" issued before tracing session
1229 * starts, lost sys_enter exit due to ring buffer overflow).
1231 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1234 return __trace__fprintf_tstamp(trace, tstamp, fp);
1236 return fprintf(fp, " ? ");
1239 static bool done = false;
1240 static bool interrupted = false;
1242 static void sig_handler(int sig)
1245 interrupted = sig == SIGINT;
1248 static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
1252 if (trace->multiple_threads) {
1253 if (trace->show_comm)
1254 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
1255 printed += fprintf(fp, "%d ", thread->tid);
1261 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1262 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1266 if (trace->show_tstamp)
1267 printed = trace__fprintf_tstamp(trace, tstamp, fp);
1268 if (trace->show_duration)
1269 printed += fprintf_duration(duration, duration_calculated, fp);
1270 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1273 static int trace__process_event(struct trace *trace, struct machine *machine,
1274 union perf_event *event, struct perf_sample *sample)
1278 switch (event->header.type) {
1279 case PERF_RECORD_LOST:
1280 color_fprintf(trace->output, PERF_COLOR_RED,
1281 "LOST %" PRIu64 " events!\n", event->lost.lost);
1282 ret = machine__process_lost_event(machine, event, sample);
1285 ret = machine__process_event(machine, event, sample);
1292 static int trace__tool_process(struct perf_tool *tool,
1293 union perf_event *event,
1294 struct perf_sample *sample,
1295 struct machine *machine)
1297 struct trace *trace = container_of(tool, struct trace, tool);
1298 return trace__process_event(trace, machine, event, sample);
1301 static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1303 struct machine *machine = vmachine;
1305 if (machine->kptr_restrict_warned)
1308 if (symbol_conf.kptr_restrict) {
1309 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1310 "Check /proc/sys/kernel/kptr_restrict.\n\n"
1311 "Kernel samples will not be resolved.\n");
1312 machine->kptr_restrict_warned = true;
1316 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1319 static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1321 int err = symbol__init(NULL);
1326 trace->host = machine__new_host();
1327 if (trace->host == NULL)
1330 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1334 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
1335 evlist->threads, trace__tool_process, false,
1344 static void trace__symbols__exit(struct trace *trace)
1346 machine__exit(trace->host);
1352 static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1356 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1357 nr_args = sc->fmt->nr_args;
1359 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1360 if (sc->arg_fmt == NULL)
1363 for (idx = 0; idx < nr_args; ++idx) {
1365 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1368 sc->nr_args = nr_args;
1372 static int syscall__set_arg_fmts(struct syscall *sc)
1374 struct tep_format_field *field, *last_field = NULL;
1377 for (field = sc->args; field; field = field->next, ++idx) {
1380 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1383 if (strcmp(field->type, "const char *") == 0 &&
1384 (strcmp(field->name, "filename") == 0 ||
1385 strcmp(field->name, "path") == 0 ||
1386 strcmp(field->name, "pathname") == 0))
1387 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
1388 else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
1389 sc->arg_fmt[idx].scnprintf = SCA_PTR;
1390 else if (strcmp(field->type, "pid_t") == 0)
1391 sc->arg_fmt[idx].scnprintf = SCA_PID;
1392 else if (strcmp(field->type, "umode_t") == 0)
1393 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
1394 else if ((strcmp(field->type, "int") == 0 ||
1395 strcmp(field->type, "unsigned int") == 0 ||
1396 strcmp(field->type, "long") == 0) &&
1397 (len = strlen(field->name)) >= 2 &&
1398 strcmp(field->name + len - 2, "fd") == 0) {
1400 * /sys/kernel/tracing/events/syscalls/sys_enter*
1401 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1406 sc->arg_fmt[idx].scnprintf = SCA_FD;
1411 sc->args_size = last_field->offset + last_field->size;
1416 static int trace__read_syscall_info(struct trace *trace, int id)
1420 const char *name = syscalltbl__name(trace->sctbl, id);
1425 if (id > trace->syscalls.max) {
1426 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1428 if (nsyscalls == NULL)
1431 if (trace->syscalls.max != -1) {
1432 memset(nsyscalls + trace->syscalls.max + 1, 0,
1433 (id - trace->syscalls.max) * sizeof(*sc));
1435 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1438 trace->syscalls.table = nsyscalls;
1439 trace->syscalls.max = id;
1442 sc = trace->syscalls.table + id;
1445 sc->fmt = syscall_fmt__find(sc->name);
1447 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
1448 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
1450 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
1451 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
1452 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
1455 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1458 if (IS_ERR(sc->tp_format))
1461 sc->args = sc->tp_format->format.fields;
1463 * We need to check and discard the first variable '__syscall_nr'
1464 * or 'nr' that mean the syscall number. It is needless here.
1465 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1467 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
1468 sc->args = sc->args->next;
1472 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
1473 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
1475 return syscall__set_arg_fmts(sc);
1478 static int trace__validate_ev_qualifier(struct trace *trace)
1481 size_t nr_allocated;
1482 struct str_node *pos;
1484 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1485 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1486 sizeof(trace->ev_qualifier_ids.entries[0]));
1488 if (trace->ev_qualifier_ids.entries == NULL) {
1489 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1495 nr_allocated = trace->ev_qualifier_ids.nr;
1498 strlist__for_each_entry(pos, trace->ev_qualifier) {
1499 const char *sc = pos->s;
1500 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
1503 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1508 fputs("Error:\tInvalid syscall ", trace->output);
1511 fputs(", ", trace->output);
1514 fputs(sc, trace->output);
1517 trace->ev_qualifier_ids.entries[i++] = id;
1518 if (match_next == -1)
1522 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1525 if (nr_allocated == trace->ev_qualifier_ids.nr) {
1529 entries = realloc(trace->ev_qualifier_ids.entries,
1530 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1531 if (entries == NULL) {
1533 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1536 trace->ev_qualifier_ids.entries = entries;
1538 trace->ev_qualifier_ids.nr++;
1539 trace->ev_qualifier_ids.entries[i++] = id;
1544 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1545 "\nHint:\tand: 'man syscalls'\n", trace->output);
1547 zfree(&trace->ev_qualifier_ids.entries);
1548 trace->ev_qualifier_ids.nr = 0;
1555 * args is to be interpreted as a series of longs but we need to handle
1556 * 8-byte unaligned accesses. args points to raw_data within the event
1557 * and raw_data is guaranteed to be 8-byte unaligned because it is
1558 * preceded by raw_size which is a u32. So we need to copy args to a temp
1559 * variable to read it. Most notably this avoids extended load instructions
1560 * on unaligned addresses
1562 unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
1565 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
1567 memcpy(&val, p, sizeof(val));
1571 static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1572 struct syscall_arg *arg)
1574 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1575 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1577 return scnprintf(bf, size, "arg%d: ", arg->idx);
1581 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1582 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1583 * in tools/perf/trace/beauty/mount_flags.c
1585 static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1587 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1588 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1593 static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1594 struct syscall_arg *arg, unsigned long val)
1596 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1598 if (sc->arg_fmt[arg->idx].parm)
1599 arg->parm = sc->arg_fmt[arg->idx].parm;
1600 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1602 return scnprintf(bf, size, "%ld", val);
1605 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
1606 unsigned char *args, void *augmented_args, int augmented_args_size,
1607 struct trace *trace, struct thread *thread)
1612 struct syscall_arg arg = {
1615 .size = augmented_args_size,
1616 .args = augmented_args,
1622 .show_string_prefix = trace->show_string_prefix,
1624 struct thread_trace *ttrace = thread__priv(thread);
1627 * Things like fcntl will set this in its 'cmd' formatter to pick the
1628 * right formatter for the return value (an fd? file flags?), which is
1629 * not needed for syscalls that always return a given type, say an fd.
1631 ttrace->ret_scnprintf = NULL;
1633 if (sc->args != NULL) {
1634 struct tep_format_field *field;
1636 for (field = sc->args; field;
1637 field = field->next, ++arg.idx, bit <<= 1) {
1641 val = syscall_arg__val(&arg, arg.idx);
1643 * Some syscall args need some mask, most don't and
1644 * return val untouched.
1646 val = syscall__mask_val(sc, &arg, val);
1649 * Suppress this argument if its value is zero and
1650 * and we don't have a string associated in an
1654 !trace->show_zeros &&
1656 (sc->arg_fmt[arg.idx].show_zero ||
1657 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
1658 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1659 sc->arg_fmt[arg.idx].parm))
1662 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
1664 if (trace->show_arg_names)
1665 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
1667 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1669 } else if (IS_ERR(sc->tp_format)) {
1671 * If we managed to read the tracepoint /format file, then we
1672 * may end up not having any args, like with gettid(), so only
1673 * print the raw args when we didn't manage to read it.
1675 while (arg.idx < sc->nr_args) {
1678 val = syscall_arg__val(&arg, arg.idx);
1680 printed += scnprintf(bf + printed, size - printed, ", ");
1681 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
1682 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1692 typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
1693 union perf_event *event,
1694 struct perf_sample *sample);
1696 static struct syscall *trace__syscall_info(struct trace *trace,
1697 struct perf_evsel *evsel, int id)
1703 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1704 * before that, leaving at a higher verbosity level till that is
1705 * explained. Reproduced with plain ftrace with:
1707 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1708 * grep "NR -1 " /t/trace_pipe
1710 * After generating some load on the machine.
1714 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1715 id, perf_evsel__name(evsel), ++n);
1720 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1721 trace__read_syscall_info(trace, id))
1724 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1727 return &trace->syscalls.table[id];
1731 fprintf(trace->output, "Problems reading syscall %d", id);
1732 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1733 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1734 fputs(" information\n", trace->output);
1739 static void thread__update_stats(struct thread_trace *ttrace,
1740 int id, struct perf_sample *sample)
1742 struct int_node *inode;
1743 struct stats *stats;
1746 inode = intlist__findnew(ttrace->syscall_stats, id);
1750 stats = inode->priv;
1751 if (stats == NULL) {
1752 stats = malloc(sizeof(struct stats));
1756 inode->priv = stats;
1759 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1760 duration = sample->time - ttrace->entry_time;
1762 update_stats(stats, duration);
1765 static int trace__printf_interrupted_entry(struct trace *trace)
1767 struct thread_trace *ttrace;
1771 if (trace->failure_only || trace->current == NULL)
1774 ttrace = thread__priv(trace->current);
1776 if (!ttrace->entry_pending)
1779 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
1780 printed += len = fprintf(trace->output, "%s)", ttrace->entry_str);
1782 if (len < trace->args_alignment - 4)
1783 printed += fprintf(trace->output, "%-*s", trace->args_alignment - 4 - len, " ");
1785 printed += fprintf(trace->output, " ...\n");
1787 ttrace->entry_pending = false;
1788 ++trace->nr_events_printed;
1793 static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1794 struct perf_sample *sample, struct thread *thread)
1798 if (trace->print_sample) {
1799 double ts = (double)sample->time / NSEC_PER_MSEC;
1801 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1802 perf_evsel__name(evsel), ts,
1803 thread__comm_str(thread),
1804 sample->pid, sample->tid, sample->cpu);
1810 static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, int raw_augmented_args_size)
1812 void *augmented_args = NULL;
1814 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
1815 * and there we get all 6 syscall args plus the tracepoint common fields
1816 * that gets calculated at the start and the syscall_nr (another long).
1817 * So we check if that is the case and if so don't look after the
1818 * sc->args_size but always after the full raw_syscalls:sys_enter payload,
1821 * We'll revisit this later to pass s->args_size to the BPF augmenter
1822 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1823 * copies only what we need for each syscall, like what happens when we
1824 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1825 * traffic to just what is needed for each syscall.
1827 int args_size = raw_augmented_args_size ?: sc->args_size;
1829 *augmented_args_size = sample->raw_size - args_size;
1830 if (*augmented_args_size > 0)
1831 augmented_args = sample->raw_data + args_size;
1833 return augmented_args;
1836 static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
1837 union perf_event *event __maybe_unused,
1838 struct perf_sample *sample)
1843 struct thread *thread;
1844 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1845 int augmented_args_size = 0;
1846 void *augmented_args = NULL;
1847 struct syscall *sc = trace__syscall_info(trace, evsel, id);
1848 struct thread_trace *ttrace;
1853 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1854 ttrace = thread__trace(thread, trace->output);
1858 trace__fprintf_sample(trace, evsel, sample, thread);
1860 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
1862 if (ttrace->entry_str == NULL) {
1863 ttrace->entry_str = malloc(trace__entry_str_size);
1864 if (!ttrace->entry_str)
1868 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
1869 trace__printf_interrupted_entry(trace);
1871 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1872 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1873 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1874 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1875 * so when handling, say the openat syscall, we end up getting 6 args for the
1876 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1877 * thinking that the extra 2 u64 args are the augmented filename, so just check
1878 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1880 if (evsel != trace->syscalls.events.sys_enter)
1881 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
1882 ttrace->entry_time = sample->time;
1883 msg = ttrace->entry_str;
1884 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
1886 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
1887 args, augmented_args, augmented_args_size, trace, thread);
1890 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
1893 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
1894 printed = fprintf(trace->output, "%s)", ttrace->entry_str);
1895 if (trace->args_alignment > printed)
1896 alignment = trace->args_alignment - printed;
1897 fprintf(trace->output, "%*s= ?\n", alignment, " ");
1900 ttrace->entry_pending = true;
1901 /* See trace__vfs_getname & trace__sys_exit */
1902 ttrace->filename.pending_open = false;
1905 if (trace->current != thread) {
1906 thread__put(trace->current);
1907 trace->current = thread__get(thread);
1911 thread__put(thread);
1915 static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1916 struct perf_sample *sample)
1918 struct thread_trace *ttrace;
1919 struct thread *thread;
1920 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1921 struct syscall *sc = trace__syscall_info(trace, evsel, id);
1923 void *args, *augmented_args = NULL;
1924 int augmented_args_size;
1929 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1930 ttrace = thread__trace(thread, trace->output);
1932 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1933 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1938 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
1939 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
1940 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
1941 fprintf(trace->output, "%s", msg);
1944 thread__put(thread);
1948 static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1949 struct perf_sample *sample,
1950 struct callchain_cursor *cursor)
1952 struct addr_location al;
1953 int max_stack = evsel->attr.sample_max_stack ?
1954 evsel->attr.sample_max_stack :
1958 if (machine__resolve(trace->host, &al, sample) < 0)
1961 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
1962 addr_location__put(&al);
1966 static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
1968 /* TODO: user-configurable print_opts */
1969 const unsigned int print_opts = EVSEL__PRINT_SYM |
1971 EVSEL__PRINT_UNKNOWN_AS_ADDR;
1973 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
1976 static const char *errno_to_name(struct perf_evsel *evsel, int err)
1978 struct perf_env *env = perf_evsel__env(evsel);
1979 const char *arch_name = perf_env__arch(env);
1981 return arch_syscalls__strerrno(arch_name, err);
1984 static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
1985 union perf_event *event __maybe_unused,
1986 struct perf_sample *sample)
1990 bool duration_calculated = false;
1991 struct thread *thread;
1992 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
1993 int alignment = trace->args_alignment;
1994 struct syscall *sc = trace__syscall_info(trace, evsel, id);
1995 struct thread_trace *ttrace;
2000 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2001 ttrace = thread__trace(thread, trace->output);
2005 trace__fprintf_sample(trace, evsel, sample, thread);
2008 thread__update_stats(ttrace, id, sample);
2010 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
2012 if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
2013 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
2014 ttrace->filename.pending_open = false;
2015 ++trace->stats.vfs_getname;
2018 if (ttrace->entry_time) {
2019 duration = sample->time - ttrace->entry_time;
2020 if (trace__filter_duration(trace, duration))
2022 duration_calculated = true;
2023 } else if (trace->duration_filter)
2026 if (sample->callchain) {
2027 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2028 if (callchain_ret == 0) {
2029 if (callchain_cursor.nr < trace->min_stack)
2035 if (trace->summary_only || (ret >= 0 && trace->failure_only))
2038 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
2040 if (ttrace->entry_pending) {
2041 printed = fprintf(trace->output, "%s", ttrace->entry_str);
2043 printed += fprintf(trace->output, " ... [");
2044 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
2046 printed += fprintf(trace->output, "]: %s()", sc->name);
2049 printed++; /* the closing ')' */
2051 if (alignment > printed)
2052 alignment -= printed;
2056 fprintf(trace->output, ")%*s= ", alignment, " ");
2058 if (sc->fmt == NULL) {
2062 fprintf(trace->output, "%ld", ret);
2063 } else if (ret < 0) {
2065 char bf[STRERR_BUFSIZE];
2066 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
2067 *e = errno_to_name(evsel, -ret);
2069 fprintf(trace->output, "-1 %s (%s)", e, emsg);
2071 } else if (ret == 0 && sc->fmt->timeout)
2072 fprintf(trace->output, "0 (Timeout)");
2073 else if (ttrace->ret_scnprintf) {
2075 struct syscall_arg arg = {
2080 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
2081 ttrace->ret_scnprintf = NULL;
2082 fprintf(trace->output, "%s", bf);
2083 } else if (sc->fmt->hexret)
2084 fprintf(trace->output, "%#lx", ret);
2085 else if (sc->fmt->errpid) {
2086 struct thread *child = machine__find_thread(trace->host, ret, ret);
2088 if (child != NULL) {
2089 fprintf(trace->output, "%ld", ret);
2090 if (child->comm_set)
2091 fprintf(trace->output, " (%s)", thread__comm_str(child));
2097 fputc('\n', trace->output);
2100 * We only consider an 'event' for the sake of --max-events a non-filtered
2101 * sys_enter + sys_exit and other tracepoint events.
2103 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2106 if (callchain_ret > 0)
2107 trace__fprintf_callchain(trace, sample);
2108 else if (callchain_ret < 0)
2109 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2111 ttrace->entry_pending = false;
2114 thread__put(thread);
2118 static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
2119 union perf_event *event __maybe_unused,
2120 struct perf_sample *sample)
2122 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2123 struct thread_trace *ttrace;
2124 size_t filename_len, entry_str_len, to_move;
2125 ssize_t remaining_space;
2127 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
2132 ttrace = thread__priv(thread);
2136 filename_len = strlen(filename);
2137 if (filename_len == 0)
2140 if (ttrace->filename.namelen < filename_len) {
2141 char *f = realloc(ttrace->filename.name, filename_len + 1);
2146 ttrace->filename.namelen = filename_len;
2147 ttrace->filename.name = f;
2150 strcpy(ttrace->filename.name, filename);
2151 ttrace->filename.pending_open = true;
2153 if (!ttrace->filename.ptr)
2156 entry_str_len = strlen(ttrace->entry_str);
2157 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2158 if (remaining_space <= 0)
2161 if (filename_len > (size_t)remaining_space) {
2162 filename += filename_len - remaining_space;
2163 filename_len = remaining_space;
2166 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2167 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2168 memmove(pos + filename_len, pos, to_move);
2169 memcpy(pos, filename, filename_len);
2171 ttrace->filename.ptr = 0;
2172 ttrace->filename.entry_str_pos = 0;
2174 thread__put(thread);
2179 static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
2180 union perf_event *event __maybe_unused,
2181 struct perf_sample *sample)
2183 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2184 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
2185 struct thread *thread = machine__findnew_thread(trace->host,
2188 struct thread_trace *ttrace = thread__trace(thread, trace->output);
2193 ttrace->runtime_ms += runtime_ms;
2194 trace->runtime_ms += runtime_ms;
2196 thread__put(thread);
2200 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
2202 perf_evsel__strval(evsel, sample, "comm"),
2203 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2205 perf_evsel__intval(evsel, sample, "vruntime"));
2209 static int bpf_output__printer(enum binary_printer_ops op,
2210 unsigned int val, void *extra __maybe_unused, FILE *fp)
2212 unsigned char ch = (unsigned char)val;
2215 case BINARY_PRINT_CHAR_DATA:
2216 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
2217 case BINARY_PRINT_DATA_BEGIN:
2218 case BINARY_PRINT_LINE_BEGIN:
2219 case BINARY_PRINT_ADDR:
2220 case BINARY_PRINT_NUM_DATA:
2221 case BINARY_PRINT_NUM_PAD:
2222 case BINARY_PRINT_SEP:
2223 case BINARY_PRINT_CHAR_PAD:
2224 case BINARY_PRINT_LINE_END:
2225 case BINARY_PRINT_DATA_END:
2233 static void bpf_output__fprintf(struct trace *trace,
2234 struct perf_sample *sample)
2236 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2237 bpf_output__printer, NULL, trace->output);
2238 ++trace->nr_events_printed;
2241 static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2242 union perf_event *event __maybe_unused,
2243 struct perf_sample *sample)
2245 struct thread *thread;
2246 int callchain_ret = 0;
2248 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2249 * this event's max_events having been hit and this is an entry coming
2250 * from the ring buffer that we should discard, since the max events
2251 * have already been considered/printed.
2253 if (evsel->disabled)
2256 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2258 if (sample->callchain) {
2259 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2260 if (callchain_ret == 0) {
2261 if (callchain_cursor.nr < trace->min_stack)
2267 trace__printf_interrupted_entry(trace);
2268 trace__fprintf_tstamp(trace, sample->time, trace->output);
2270 if (trace->trace_syscalls && trace->show_duration)
2271 fprintf(trace->output, "( ): ");
2274 trace__fprintf_comm_tid(trace, thread, trace->output);
2276 if (evsel == trace->syscalls.events.augmented) {
2277 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2278 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2281 fprintf(trace->output, "%s(", sc->name);
2282 trace__fprintf_sys_enter(trace, evsel, sample);
2283 fputc(')', trace->output);
2288 * XXX: Not having the associated syscall info or not finding/adding
2289 * the thread should never happen, but if it does...
2290 * fall thru and print it as a bpf_output event.
2294 fprintf(trace->output, "%s:", evsel->name);
2296 if (perf_evsel__is_bpf_output(evsel)) {
2297 bpf_output__fprintf(trace, sample);
2298 } else if (evsel->tp_format) {
2299 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2300 trace__fprintf_sys_enter(trace, evsel, sample)) {
2301 event_format__fprintf(evsel->tp_format, sample->cpu,
2302 sample->raw_data, sample->raw_size,
2304 ++trace->nr_events_printed;
2306 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2307 perf_evsel__disable(evsel);
2308 perf_evsel__close(evsel);
2314 fprintf(trace->output, "\n");
2316 if (callchain_ret > 0)
2317 trace__fprintf_callchain(trace, sample);
2318 else if (callchain_ret < 0)
2319 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2321 thread__put(thread);
2325 static void print_location(FILE *f, struct perf_sample *sample,
2326 struct addr_location *al,
2327 bool print_dso, bool print_sym)
2330 if ((verbose > 0 || print_dso) && al->map)
2331 fprintf(f, "%s@", al->map->dso->long_name);
2333 if ((verbose > 0 || print_sym) && al->sym)
2334 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
2335 al->addr - al->sym->start);
2337 fprintf(f, "0x%" PRIx64, al->addr);
2339 fprintf(f, "0x%" PRIx64, sample->addr);
2342 static int trace__pgfault(struct trace *trace,
2343 struct perf_evsel *evsel,
2344 union perf_event *event __maybe_unused,
2345 struct perf_sample *sample)
2347 struct thread *thread;
2348 struct addr_location al;
2349 char map_type = 'd';
2350 struct thread_trace *ttrace;
2352 int callchain_ret = 0;
2354 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2356 if (sample->callchain) {
2357 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2358 if (callchain_ret == 0) {
2359 if (callchain_cursor.nr < trace->min_stack)
2365 ttrace = thread__trace(thread, trace->output);
2369 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2374 if (trace->summary_only)
2377 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
2379 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
2381 fprintf(trace->output, "%sfault [",
2382 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2385 print_location(trace->output, sample, &al, false, true);
2387 fprintf(trace->output, "] => ");
2389 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
2392 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
2400 print_location(trace->output, sample, &al, true, false);
2402 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
2404 if (callchain_ret > 0)
2405 trace__fprintf_callchain(trace, sample);
2406 else if (callchain_ret < 0)
2407 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2409 ++trace->nr_events_printed;
2413 thread__put(thread);
2417 static void trace__set_base_time(struct trace *trace,
2418 struct perf_evsel *evsel,
2419 struct perf_sample *sample)
2422 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2423 * and don't use sample->time unconditionally, we may end up having
2424 * some other event in the future without PERF_SAMPLE_TIME for good
2425 * reason, i.e. we may not be interested in its timestamps, just in
2426 * it taking place, picking some piece of information when it
2427 * appears in our event stream (vfs_getname comes to mind).
2429 if (trace->base_time == 0 && !trace->full_time &&
2430 (evsel->attr.sample_type & PERF_SAMPLE_TIME))
2431 trace->base_time = sample->time;
2434 static int trace__process_sample(struct perf_tool *tool,
2435 union perf_event *event,
2436 struct perf_sample *sample,
2437 struct perf_evsel *evsel,
2438 struct machine *machine __maybe_unused)
2440 struct trace *trace = container_of(tool, struct trace, tool);
2441 struct thread *thread;
2444 tracepoint_handler handler = evsel->handler;
2446 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2447 if (thread && thread__is_filtered(thread))
2450 trace__set_base_time(trace, evsel, sample);
2454 handler(trace, evsel, event, sample);
2457 thread__put(thread);
2461 static int trace__record(struct trace *trace, int argc, const char **argv)
2463 unsigned int rec_argc, i, j;
2464 const char **rec_argv;
2465 const char * const record_args[] = {
2472 const char * const sc_args[] = { "-e", };
2473 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2474 const char * const majpf_args[] = { "-e", "major-faults" };
2475 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2476 const char * const minpf_args[] = { "-e", "minor-faults" };
2477 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2479 /* +1 is for the event string below */
2480 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2481 majpf_args_nr + minpf_args_nr + argc;
2482 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2484 if (rec_argv == NULL)
2488 for (i = 0; i < ARRAY_SIZE(record_args); i++)
2489 rec_argv[j++] = record_args[i];
2491 if (trace->trace_syscalls) {
2492 for (i = 0; i < sc_args_nr; i++)
2493 rec_argv[j++] = sc_args[i];
2495 /* event string may be different for older kernels - e.g., RHEL6 */
2496 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2497 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2498 else if (is_valid_tracepoint("syscalls:sys_enter"))
2499 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2501 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
2507 if (trace->trace_pgfaults & TRACE_PFMAJ)
2508 for (i = 0; i < majpf_args_nr; i++)
2509 rec_argv[j++] = majpf_args[i];
2511 if (trace->trace_pgfaults & TRACE_PFMIN)
2512 for (i = 0; i < minpf_args_nr; i++)
2513 rec_argv[j++] = minpf_args[i];
2515 for (i = 0; i < (unsigned int)argc; i++)
2516 rec_argv[j++] = argv[i];
2518 return cmd_record(j, rec_argv);
2521 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2523 static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
2526 struct perf_evsel *evsel, *tmp;
2527 struct parse_events_error err = { .idx = 0, };
2528 int ret = parse_events(evlist, "probe:vfs_getname*", &err);
2533 evlist__for_each_entry_safe(evlist, evsel, tmp) {
2534 if (!strstarts(perf_evsel__name(evsel), "probe:vfs_getname"))
2537 if (perf_evsel__field(evsel, "pathname")) {
2538 evsel->handler = trace__vfs_getname;
2543 list_del_init(&evsel->node);
2544 evsel->evlist = NULL;
2545 perf_evsel__delete(evsel);
2551 static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
2553 struct perf_evsel *evsel;
2554 struct perf_event_attr attr = {
2555 .type = PERF_TYPE_SOFTWARE,
2559 attr.config = config;
2560 attr.sample_period = 1;
2562 event_attr_init(&attr);
2564 evsel = perf_evsel__new(&attr);
2566 evsel->handler = trace__pgfault;
2571 static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2573 const u32 type = event->header.type;
2574 struct perf_evsel *evsel;
2576 if (type != PERF_RECORD_SAMPLE) {
2577 trace__process_event(trace, trace->host, event, sample);
2581 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2582 if (evsel == NULL) {
2583 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2587 trace__set_base_time(trace, evsel, sample);
2589 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2590 sample->raw_data == NULL) {
2591 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2592 perf_evsel__name(evsel), sample->tid,
2593 sample->cpu, sample->raw_size);
2595 tracepoint_handler handler = evsel->handler;
2596 handler(trace, evsel, event, sample);
2599 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2603 static int trace__add_syscall_newtp(struct trace *trace)
2606 struct perf_evlist *evlist = trace->evlist;
2607 struct perf_evsel *sys_enter, *sys_exit;
2609 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
2610 if (sys_enter == NULL)
2613 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2614 goto out_delete_sys_enter;
2616 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
2617 if (sys_exit == NULL)
2618 goto out_delete_sys_enter;
2620 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2621 goto out_delete_sys_exit;
2623 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2624 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2626 perf_evlist__add(evlist, sys_enter);
2627 perf_evlist__add(evlist, sys_exit);
2629 if (callchain_param.enabled && !trace->kernel_syscallchains) {
2631 * We're interested only in the user space callchain
2632 * leading to the syscall, allow overriding that for
2633 * debugging reasons using --kernel_syscall_callchains
2635 sys_exit->attr.exclude_callchain_kernel = 1;
2638 trace->syscalls.events.sys_enter = sys_enter;
2639 trace->syscalls.events.sys_exit = sys_exit;
2645 out_delete_sys_exit:
2646 perf_evsel__delete_priv(sys_exit);
2647 out_delete_sys_enter:
2648 perf_evsel__delete_priv(sys_enter);
2652 static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
2655 struct perf_evsel *sys_exit;
2656 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2657 trace->ev_qualifier_ids.nr,
2658 trace->ev_qualifier_ids.entries);
2663 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2665 sys_exit = trace->syscalls.events.sys_exit;
2666 err = perf_evsel__append_tp_filter(sys_exit, filter);
2677 #ifdef HAVE_LIBBPF_SUPPORT
2678 static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2680 int fd = bpf_map__fd(trace->syscalls.map);
2681 struct bpf_map_syscall_entry value = {
2682 .enabled = !trace->not_ev_qualifier,
2687 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2688 int key = trace->ev_qualifier_ids.entries[i];
2690 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2698 static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2700 int fd = bpf_map__fd(trace->syscalls.map);
2701 struct bpf_map_syscall_entry value = {
2706 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
2707 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
2715 static int trace__init_syscalls_bpf_map(struct trace *trace)
2717 bool enabled = true;
2719 if (trace->ev_qualifier_ids.nr)
2720 enabled = trace->not_ev_qualifier;
2722 return __trace__init_syscalls_bpf_map(trace, enabled);
2725 static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
2730 static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
2734 #endif // HAVE_LIBBPF_SUPPORT
2736 static int trace__set_ev_qualifier_filter(struct trace *trace)
2738 if (trace->syscalls.map)
2739 return trace__set_ev_qualifier_bpf_filter(trace);
2740 if (trace->syscalls.events.sys_enter)
2741 return trace__set_ev_qualifier_tp_filter(trace);
2745 static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
2746 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
2749 #ifdef HAVE_LIBBPF_SUPPORT
2751 int map_fd = bpf_map__fd(map);
2754 for (i = 0; i < npids; ++i) {
2755 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
2763 static int trace__set_filter_loop_pids(struct trace *trace)
2765 unsigned int nr = 1, err;
2769 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2771 while (thread && nr < ARRAY_SIZE(pids)) {
2772 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2777 if (!strcmp(thread__comm_str(parent), "sshd") ||
2778 strstarts(thread__comm_str(parent), "gnome-terminal")) {
2779 pids[nr++] = parent->tid;
2785 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
2786 if (!err && trace->filter_pids.map)
2787 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
2792 static int trace__set_filter_pids(struct trace *trace)
2796 * Better not use !target__has_task() here because we need to cover the
2797 * case where no threads were specified in the command line, but a
2798 * workload was, and in that case we will fill in the thread_map when
2799 * we fork the workload in perf_evlist__prepare_workload.
2801 if (trace->filter_pids.nr > 0) {
2802 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
2803 trace->filter_pids.entries);
2804 if (!err && trace->filter_pids.map) {
2805 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
2806 trace->filter_pids.entries);
2808 } else if (thread_map__pid(trace->evlist->threads, 0) == -1) {
2809 err = trace__set_filter_loop_pids(trace);
2815 static int __trace__deliver_event(struct trace *trace, union perf_event *event)
2817 struct perf_evlist *evlist = trace->evlist;
2818 struct perf_sample sample;
2821 err = perf_evlist__parse_sample(evlist, event, &sample);
2823 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
2825 trace__handle_event(trace, event, &sample);
2830 static int __trace__flush_events(struct trace *trace)
2832 u64 first = ordered_events__first_time(&trace->oe.data);
2833 u64 flush = trace->oe.last - NSEC_PER_SEC;
2835 /* Is there some thing to flush.. */
2836 if (first && first < flush)
2837 return ordered_events__flush_time(&trace->oe.data, flush);
2842 static int trace__flush_events(struct trace *trace)
2844 return !trace->sort_events ? 0 : __trace__flush_events(trace);
2847 static int trace__deliver_event(struct trace *trace, union perf_event *event)
2851 if (!trace->sort_events)
2852 return __trace__deliver_event(trace, event);
2854 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
2855 if (err && err != -1)
2858 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
2862 return trace__flush_events(trace);
2865 static int ordered_events__deliver_event(struct ordered_events *oe,
2866 struct ordered_event *event)
2868 struct trace *trace = container_of(oe, struct trace, oe.data);
2870 return __trace__deliver_event(trace, event->event);
2873 static int trace__run(struct trace *trace, int argc, const char **argv)
2875 struct perf_evlist *evlist = trace->evlist;
2876 struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
2878 unsigned long before;
2879 const bool forks = argc > 0;
2880 bool draining = false;
2884 if (!trace->raw_augmented_syscalls) {
2885 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
2886 goto out_error_raw_syscalls;
2888 if (trace->trace_syscalls)
2889 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
2892 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2893 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2894 if (pgfault_maj == NULL)
2896 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
2897 perf_evlist__add(evlist, pgfault_maj);
2900 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2901 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2902 if (pgfault_min == NULL)
2904 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
2905 perf_evlist__add(evlist, pgfault_min);
2909 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2910 trace__sched_stat_runtime))
2911 goto out_error_sched_stat_runtime;
2914 * If a global cgroup was set, apply it to all the events without an
2915 * explicit cgroup. I.e.:
2917 * trace -G A -e sched:*switch
2919 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2920 * _and_ sched:sched_switch to the 'A' cgroup, while:
2922 * trace -e sched:*switch -G A
2924 * will only set the sched:sched_switch event to the 'A' cgroup, all the
2925 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2926 * a cgroup (on the root cgroup, sys wide, etc).
2930 * trace -G A -e sched:*switch -G B
2932 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
2933 * to the 'B' cgroup.
2935 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
2936 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
2939 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
2941 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2943 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
2944 goto out_delete_evlist;
2947 err = trace__symbols_init(trace, evlist);
2949 fprintf(trace->output, "Problems initializing symbol libraries!\n");
2950 goto out_delete_evlist;
2953 perf_evlist__config(evlist, &trace->opts, &callchain_param);
2955 signal(SIGCHLD, sig_handler);
2956 signal(SIGINT, sig_handler);
2959 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
2962 fprintf(trace->output, "Couldn't run the workload!\n");
2963 goto out_delete_evlist;
2967 err = perf_evlist__open(evlist);
2969 goto out_error_open;
2971 err = bpf__apply_obj_config();
2973 char errbuf[BUFSIZ];
2975 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2976 pr_err("ERROR: Apply config to BPF failed: %s\n",
2978 goto out_error_open;
2981 err = trace__set_filter_pids(trace);
2985 if (trace->syscalls.map)
2986 trace__init_syscalls_bpf_map(trace);
2988 if (trace->ev_qualifier_ids.nr > 0) {
2989 err = trace__set_ev_qualifier_filter(trace);
2993 if (trace->syscalls.events.sys_exit) {
2994 pr_debug("event qualifier tracepoint filter: %s\n",
2995 trace->syscalls.events.sys_exit->filter);
2999 err = perf_evlist__apply_filters(evlist, &evsel);
3001 goto out_error_apply_filters;
3003 if (trace->dump.map)
3004 bpf_map__fprintf(trace->dump.map, trace->output);
3006 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
3008 goto out_error_mmap;
3010 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
3011 perf_evlist__enable(evlist);
3014 perf_evlist__start_workload(evlist);
3016 if (trace->opts.initial_delay) {
3017 usleep(trace->opts.initial_delay * 1000);
3018 perf_evlist__enable(evlist);
3021 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
3022 evlist->threads->nr > 1 ||
3023 perf_evlist__first(evlist)->attr.inherit;
3026 * Now that we already used evsel->attr to ask the kernel to setup the
3027 * events, lets reuse evsel->attr.sample_max_stack as the limit in
3028 * trace__resolve_callchain(), allowing per-event max-stack settings
3029 * to override an explicitly set --max-stack global setting.
3031 evlist__for_each_entry(evlist, evsel) {
3032 if (evsel__has_callchain(evsel) &&
3033 evsel->attr.sample_max_stack == 0)
3034 evsel->attr.sample_max_stack = trace->max_stack;
3037 before = trace->nr_events;
3039 for (i = 0; i < evlist->nr_mmaps; i++) {
3040 union perf_event *event;
3041 struct perf_mmap *md;
3043 md = &evlist->mmap[i];
3044 if (perf_mmap__read_init(md) < 0)
3047 while ((event = perf_mmap__read_event(md)) != NULL) {
3050 err = trace__deliver_event(trace, event);
3054 perf_mmap__consume(md);
3059 if (done && !draining) {
3060 perf_evlist__disable(evlist);
3064 perf_mmap__read_done(md);
3067 if (trace->nr_events == before) {
3068 int timeout = done ? 100 : -1;
3070 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
3071 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
3076 if (trace__flush_events(trace))
3084 thread__zput(trace->current);
3086 perf_evlist__disable(evlist);
3088 if (trace->sort_events)
3089 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
3093 trace__fprintf_thread_summary(trace, trace->output);
3095 if (trace->show_tool_stats) {
3096 fprintf(trace->output, "Stats:\n "
3097 " vfs_getname : %" PRIu64 "\n"
3098 " proc_getname: %" PRIu64 "\n",
3099 trace->stats.vfs_getname,
3100 trace->stats.proc_getname);
3105 trace__symbols__exit(trace);
3107 perf_evlist__delete(evlist);
3108 cgroup__put(trace->cgroup);
3109 trace->evlist = NULL;
3110 trace->live = false;
3113 char errbuf[BUFSIZ];
3115 out_error_sched_stat_runtime:
3116 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
3119 out_error_raw_syscalls:
3120 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
3124 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3128 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3131 fprintf(trace->output, "%s\n", errbuf);
3132 goto out_delete_evlist;
3134 out_error_apply_filters:
3135 fprintf(trace->output,
3136 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3137 evsel->filter, perf_evsel__name(evsel), errno,
3138 str_error_r(errno, errbuf, sizeof(errbuf)));
3139 goto out_delete_evlist;
3142 fprintf(trace->output, "Not enough memory to run!\n");
3143 goto out_delete_evlist;
3146 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3147 goto out_delete_evlist;
3150 static int trace__replay(struct trace *trace)
3152 const struct perf_evsel_str_handler handlers[] = {
3153 { "probe:vfs_getname", trace__vfs_getname, },
3155 struct perf_data data = {
3157 .mode = PERF_DATA_MODE_READ,
3158 .force = trace->force,
3160 struct perf_session *session;
3161 struct perf_evsel *evsel;
3164 trace->tool.sample = trace__process_sample;
3165 trace->tool.mmap = perf_event__process_mmap;
3166 trace->tool.mmap2 = perf_event__process_mmap2;
3167 trace->tool.comm = perf_event__process_comm;
3168 trace->tool.exit = perf_event__process_exit;
3169 trace->tool.fork = perf_event__process_fork;
3170 trace->tool.attr = perf_event__process_attr;
3171 trace->tool.tracing_data = perf_event__process_tracing_data;
3172 trace->tool.build_id = perf_event__process_build_id;
3173 trace->tool.namespaces = perf_event__process_namespaces;
3175 trace->tool.ordered_events = true;
3176 trace->tool.ordering_requires_timestamps = true;
3178 /* add tid to output */
3179 trace->multiple_threads = true;
3181 session = perf_session__new(&data, false, &trace->tool);
3182 if (session == NULL)
3185 if (trace->opts.target.pid)
3186 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3188 if (trace->opts.target.tid)
3189 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3191 if (symbol__init(&session->header.env) < 0)
3194 trace->host = &session->machines.host;
3196 err = perf_session__set_tracepoints_handlers(session, handlers);
3200 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3201 "raw_syscalls:sys_enter");
3202 /* older kernels have syscalls tp versus raw_syscalls */
3204 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3205 "syscalls:sys_enter");
3208 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
3209 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
3210 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3214 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3215 "raw_syscalls:sys_exit");
3217 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3218 "syscalls:sys_exit");
3220 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
3221 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
3222 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
3226 evlist__for_each_entry(session->evlist, evsel) {
3227 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
3228 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3229 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3230 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
3231 evsel->handler = trace__pgfault;
3236 err = perf_session__process_events(session);
3238 pr_err("Failed to process events, error %d", err);
3240 else if (trace->summary)
3241 trace__fprintf_thread_summary(trace, trace->output);
3244 perf_session__delete(session);
3249 static size_t trace__fprintf_threads_header(FILE *fp)
3253 printed = fprintf(fp, "\n Summary of events:\n\n");
3258 DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3259 struct stats *stats;
3264 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3265 struct stats *stats = source->priv;
3267 entry->syscall = source->i;
3268 entry->stats = stats;
3269 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3272 static size_t thread__dump_stats(struct thread_trace *ttrace,
3273 struct trace *trace, FILE *fp)
3278 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
3280 if (syscall_stats == NULL)
3283 printed += fprintf(fp, "\n");
3285 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3286 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3287 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
3289 resort_rb__for_each_entry(nd, syscall_stats) {
3290 struct stats *stats = syscall_stats_entry->stats;
3292 double min = (double)(stats->min) / NSEC_PER_MSEC;
3293 double max = (double)(stats->max) / NSEC_PER_MSEC;
3294 double avg = avg_stats(stats);
3296 u64 n = (u64) stats->n;
3298 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3299 avg /= NSEC_PER_MSEC;
3301 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
3302 printed += fprintf(fp, " %-15s", sc->name);
3303 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
3304 n, syscall_stats_entry->msecs, min, avg);
3305 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
3309 resort_rb__delete(syscall_stats);
3310 printed += fprintf(fp, "\n\n");
3315 static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
3318 struct thread_trace *ttrace = thread__priv(thread);
3324 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3326 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
3327 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
3328 printed += fprintf(fp, "%.1f%%", ratio);
3330 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3332 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
3334 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3335 else if (fputc('\n', fp) != EOF)
3338 printed += thread__dump_stats(ttrace, trace, fp);
3343 static unsigned long thread__nr_events(struct thread_trace *ttrace)
3345 return ttrace ? ttrace->nr_events : 0;
3348 DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3349 struct thread *thread;
3352 entry->thread = rb_entry(nd, struct thread, rb_node);
3355 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3357 size_t printed = trace__fprintf_threads_header(fp);
3361 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3362 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3364 if (threads == NULL) {
3365 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3369 resort_rb__for_each_entry(nd, threads)
3370 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3372 resort_rb__delete(threads);
3377 static int trace__set_duration(const struct option *opt, const char *str,
3378 int unset __maybe_unused)
3380 struct trace *trace = opt->value;
3382 trace->duration_filter = atof(str);
3386 static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3387 int unset __maybe_unused)
3391 struct trace *trace = opt->value;
3393 * FIXME: introduce a intarray class, plain parse csv and create a
3394 * { int nr, int entries[] } struct...
3396 struct intlist *list = intlist__new(str);
3401 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3402 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3404 if (trace->filter_pids.entries == NULL)
3407 trace->filter_pids.entries[0] = getpid();
3409 for (i = 1; i < trace->filter_pids.nr; ++i)
3410 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3412 intlist__delete(list);
3418 static int trace__open_output(struct trace *trace, const char *filename)
3422 if (!stat(filename, &st) && st.st_size) {
3423 char oldname[PATH_MAX];
3425 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3427 rename(filename, oldname);
3430 trace->output = fopen(filename, "w");
3432 return trace->output == NULL ? -errno : 0;
3435 static int parse_pagefaults(const struct option *opt, const char *str,
3436 int unset __maybe_unused)
3438 int *trace_pgfaults = opt->value;
3440 if (strcmp(str, "all") == 0)
3441 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3442 else if (strcmp(str, "maj") == 0)
3443 *trace_pgfaults |= TRACE_PFMAJ;
3444 else if (strcmp(str, "min") == 0)
3445 *trace_pgfaults |= TRACE_PFMIN;
3452 static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3454 struct perf_evsel *evsel;
3456 evlist__for_each_entry(evlist, evsel)
3457 evsel->handler = handler;
3460 static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3462 struct perf_evsel *evsel;
3464 evlist__for_each_entry(evlist, evsel) {
3465 if (evsel->priv || !evsel->tp_format)
3468 if (strcmp(evsel->tp_format->system, "syscalls"))
3471 if (perf_evsel__init_syscall_tp(evsel))
3474 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3475 struct syscall_tp *sc = evsel->priv;
3477 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3479 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3480 struct syscall_tp *sc = evsel->priv;
3482 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3491 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3492 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3493 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3495 * It'd be better to introduce a parse_options() variant that would return a
3496 * list with the terms it didn't match to an event...
3498 static int trace__parse_events_option(const struct option *opt, const char *str,
3499 int unset __maybe_unused)
3501 struct trace *trace = (struct trace *)opt->value;
3502 const char *s = str;
3503 char *sep = NULL, *lists[2] = { NULL, NULL, };
3504 int len = strlen(str) + 1, err = -1, list, idx;
3505 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3506 char group_name[PATH_MAX];
3507 struct syscall_fmt *fmt;
3509 if (strace_groups_dir == NULL)
3514 trace->not_ev_qualifier = true;
3518 if ((sep = strchr(s, ',')) != NULL)
3522 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3523 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
3528 fmt = syscall_fmt__find_by_alias(s);
3533 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3534 if (access(group_name, R_OK) == 0)
3539 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3541 lists[list] = malloc(len);
3542 if (lists[list] == NULL)
3544 strcpy(lists[list], s);
3554 if (lists[1] != NULL) {
3555 struct strlist_config slist_config = {
3556 .dirname = strace_groups_dir,
3559 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3560 if (trace->ev_qualifier == NULL) {
3561 fputs("Not enough memory to parse event qualifier", trace->output);
3565 if (trace__validate_ev_qualifier(trace))
3567 trace->trace_syscalls = true;
3573 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3574 "event selector. use 'perf list' to list available events",
3575 parse_events_option);
3576 err = parse_events_option(&o, lists[0], 0);
3585 static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3587 struct trace *trace = opt->value;
3589 if (!list_empty(&trace->evlist->entries))
3590 return parse_cgroups(opt, str, unset);
3592 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3597 static struct bpf_map *bpf__find_map_by_name(const char *name)
3599 struct bpf_object *obj, *tmp;
3601 bpf_object__for_each_safe(obj, tmp) {
3602 struct bpf_map *map = bpf_object__find_map_by_name(obj, name);
3611 static void trace__set_bpf_map_filtered_pids(struct trace *trace)
3613 trace->filter_pids.map = bpf__find_map_by_name("pids_filtered");
3616 static void trace__set_bpf_map_syscalls(struct trace *trace)
3618 trace->syscalls.map = bpf__find_map_by_name("syscalls");
3621 static int trace__config(const char *var, const char *value, void *arg)
3623 struct trace *trace = arg;
3626 if (!strcmp(var, "trace.add_events")) {
3627 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3628 "event selector. use 'perf list' to list available events",
3629 parse_events_option);
3630 err = parse_events_option(&o, value, 0);
3631 } else if (!strcmp(var, "trace.show_timestamp")) {
3632 trace->show_tstamp = perf_config_bool(var, value);
3633 } else if (!strcmp(var, "trace.show_duration")) {
3634 trace->show_duration = perf_config_bool(var, value);
3635 } else if (!strcmp(var, "trace.show_arg_names")) {
3636 trace->show_arg_names = perf_config_bool(var, value);
3637 if (!trace->show_arg_names)
3638 trace->show_zeros = true;
3639 } else if (!strcmp(var, "trace.show_zeros")) {
3640 bool new_show_zeros = perf_config_bool(var, value);
3641 if (!trace->show_arg_names && !new_show_zeros) {
3642 pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
3645 trace->show_zeros = new_show_zeros;
3646 } else if (!strcmp(var, "trace.show_prefix")) {
3647 trace->show_string_prefix = perf_config_bool(var, value);
3648 } else if (!strcmp(var, "trace.no_inherit")) {
3649 trace->opts.no_inherit = perf_config_bool(var, value);
3650 } else if (!strcmp(var, "trace.args_alignment")) {
3651 int args_alignment = 0;
3652 if (perf_config_int(&args_alignment, var, value) == 0)
3653 trace->args_alignment = args_alignment;
3659 int cmd_trace(int argc, const char **argv)
3661 const char *trace_usage[] = {
3662 "perf trace [<options>] [<command>]",
3663 "perf trace [<options>] -- <command> [<options>]",
3664 "perf trace record [<options>] [<command>]",
3665 "perf trace record [<options>] -- <command> [<options>]",
3668 struct trace trace = {
3677 .user_freq = UINT_MAX,
3678 .user_interval = ULLONG_MAX,
3679 .no_buffering = true,
3680 .mmap_pages = UINT_MAX,
3684 .show_tstamp = true,
3685 .show_duration = true,
3686 .show_arg_names = true,
3687 .args_alignment = 70,
3688 .trace_syscalls = false,
3689 .kernel_syscallchains = false,
3690 .max_stack = UINT_MAX,
3691 .max_events = ULONG_MAX,
3693 const char *map_dump_str = NULL;
3694 const char *output_name = NULL;
3695 const struct option trace_options[] = {
3696 OPT_CALLBACK('e', "event", &trace, "event",
3697 "event/syscall selector. use 'perf list' to list available events",
3698 trace__parse_events_option),
3699 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3700 "show the thread COMM next to its id"),
3701 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
3702 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3703 trace__parse_events_option),
3704 OPT_STRING('o', "output", &output_name, "file", "output file name"),
3705 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
3706 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3707 "trace events on existing process id"),
3708 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
3709 "trace events on existing thread id"),
3710 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
3711 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
3712 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
3713 "system-wide collection from all CPUs"),
3714 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
3715 "list of cpus to monitor"),
3716 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
3717 "child tasks do not inherit counters"),
3718 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3719 "number of mmap data pages",
3720 perf_evlist__parse_mmap_pages),
3721 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
3723 OPT_CALLBACK(0, "duration", &trace, "float",
3724 "show only events with duration > N.M ms",
3725 trace__set_duration),
3726 #ifdef HAVE_LIBBPF_SUPPORT
3727 OPT_STRING(0, "map-dump", &map_dump_str, "BPF map", "BPF map to periodically dump"),
3729 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
3730 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
3731 OPT_BOOLEAN('T', "time", &trace.full_time,
3732 "Show full timestamp, not time relative to first start"),
3733 OPT_BOOLEAN(0, "failure", &trace.failure_only,
3734 "Show only syscalls that failed"),
3735 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3736 "Show only syscall summary with statistics"),
3737 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3738 "Show all syscalls and summary with statistics"),
3739 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3740 "Trace pagefaults", parse_pagefaults, "maj"),
3741 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
3742 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
3743 OPT_CALLBACK(0, "call-graph", &trace.opts,
3744 "record_mode[,record_size]", record_callchain_help,
3745 &record_parse_callchain_opt),
3746 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3747 "Show the kernel callchains on the syscall exit path"),
3748 OPT_ULONG(0, "max-events", &trace.max_events,
3749 "Set the maximum number of events to print, exit after that is reached. "),
3750 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3751 "Set the minimum stack depth when parsing the callchain, "
3752 "anything below the specified depth will be ignored."),
3753 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3754 "Set the maximum stack depth when parsing the callchain, "
3755 "anything beyond the specified depth will be ignored. "
3756 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3757 OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
3758 "Sort batch of events before processing, use if getting out of order events"),
3759 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3760 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
3761 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
3762 "per thread proc mmap processing timeout in ms"),
3763 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3764 trace__parse_cgroups),
3765 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3766 "ms to wait before starting measurement after program "
3770 bool __maybe_unused max_stack_user_set = true;
3771 bool mmap_pages_user_set = true;
3772 struct perf_evsel *evsel;
3773 const char * const trace_subcommands[] = { "record", NULL };
3777 signal(SIGSEGV, sighandler_dump_stack);
3778 signal(SIGFPE, sighandler_dump_stack);
3780 trace.evlist = perf_evlist__new();
3781 trace.sctbl = syscalltbl__new();
3783 if (trace.evlist == NULL || trace.sctbl == NULL) {
3784 pr_err("Not enough memory to run!\n");
3789 err = perf_config(trace__config, &trace);
3793 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3794 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
3796 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3797 usage_with_options_msg(trace_usage, trace_options,
3798 "cgroup monitoring only available in system-wide mode");
3801 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3802 if (IS_ERR(evsel)) {
3803 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
3804 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3809 trace.syscalls.events.augmented = evsel;
3810 trace__set_bpf_map_filtered_pids(&trace);
3811 trace__set_bpf_map_syscalls(&trace);
3814 err = bpf__setup_stdout(trace.evlist);
3816 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3817 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3824 trace.dump.map = bpf__find_map_by_name(map_dump_str);
3825 if (trace.dump.map == NULL) {
3826 pr_err("ERROR: BPF map \"%s\" not found\n", map_dump_str);
3831 if (trace.trace_pgfaults) {
3832 trace.opts.sample_address = true;
3833 trace.opts.sample_time = true;
3836 if (trace.opts.mmap_pages == UINT_MAX)
3837 mmap_pages_user_set = false;
3839 if (trace.max_stack == UINT_MAX) {
3840 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
3841 max_stack_user_set = false;
3844 #ifdef HAVE_DWARF_UNWIND_SUPPORT
3845 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
3846 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
3850 if (callchain_param.enabled) {
3851 if (!mmap_pages_user_set && geteuid() == 0)
3852 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3854 symbol_conf.use_callchain = true;
3857 if (trace.evlist->nr_entries > 0) {
3858 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
3859 if (evlist__set_syscall_tp_fields(trace.evlist)) {
3860 perror("failed to set syscalls:* tracepoint fields");
3865 if (trace.sort_events) {
3866 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
3867 ordered_events__set_copy_on_queue(&trace.oe.data, true);
3871 * If we are augmenting syscalls, then combine what we put in the
3872 * __augmented_syscalls__ BPF map with what is in the
3873 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3874 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3876 * We'll switch to look at two BPF maps, one for sys_enter and the
3877 * other for sys_exit when we start augmenting the sys_exit paths with
3878 * buffers that are being copied from kernel to userspace, think 'read'
3881 if (trace.syscalls.events.augmented) {
3882 evlist__for_each_entry(trace.evlist, evsel) {
3883 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
3885 if (raw_syscalls_sys_exit) {
3886 trace.raw_augmented_syscalls = true;
3887 goto init_augmented_syscall_tp;
3890 if (trace.syscalls.events.augmented->priv == NULL &&
3891 strstr(perf_evsel__name(evsel), "syscalls:sys_enter")) {
3892 struct perf_evsel *augmented = trace.syscalls.events.augmented;
3893 if (perf_evsel__init_augmented_syscall_tp(augmented, evsel) ||
3894 perf_evsel__init_augmented_syscall_tp_args(augmented))
3896 augmented->handler = trace__sys_enter;
3899 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
3900 struct syscall_tp *sc;
3901 init_augmented_syscall_tp:
3902 if (perf_evsel__init_augmented_syscall_tp(evsel, evsel))
3906 * For now with BPF raw_augmented we hook into
3907 * raw_syscalls:sys_enter and there we get all
3908 * 6 syscall args plus the tracepoint common
3909 * fields and the syscall_nr (another long).
3910 * So we check if that is the case and if so
3911 * don't look after the sc->args_size but
3912 * always after the full raw_syscalls:sys_enter
3913 * payload, which is fixed.
3915 * We'll revisit this later to pass
3916 * s->args_size to the BPF augmenter (now
3917 * tools/perf/examples/bpf/augmented_raw_syscalls.c,
3918 * so that it copies only what we need for each
3919 * syscall, like what happens when we use
3920 * syscalls:sys_enter_NAME, so that we reduce
3921 * the kernel/userspace traffic to just what is
3922 * needed for each syscall.
3924 if (trace.raw_augmented_syscalls)
3925 trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset;
3926 perf_evsel__init_augmented_syscall_tp_ret(evsel);
3927 evsel->handler = trace__sys_exit;
3932 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3933 return trace__record(&trace, argc-1, &argv[1]);
3935 /* summary_only implies summary option, but don't overwrite summary if set */
3936 if (trace.summary_only)
3937 trace.summary = trace.summary_only;
3939 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3940 trace.evlist->nr_entries == 0 /* Was --events used? */) {
3941 trace.trace_syscalls = true;
3944 if (output_name != NULL) {
3945 err = trace__open_output(&trace, output_name);
3947 perror("failed to create output file");
3952 err = target__validate(&trace.opts.target);
3954 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
3955 fprintf(trace.output, "%s", bf);
3959 err = target__parse_uid(&trace.opts.target);
3961 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
3962 fprintf(trace.output, "%s", bf);
3966 if (!argc && target__none(&trace.opts.target))
3967 trace.opts.target.system_wide = true;
3970 err = trace__replay(&trace);
3972 err = trace__run(&trace, argc, argv);
3975 if (output_name != NULL)
3976 fclose(trace.output);