4 * Builtin 'trace' command:
6 * Display a continuously updated trace of any workload, CPU, specific PID,
7 * system wide, etc. Default format is loosely strace like, but any other
8 * event may be specified using --event.
10 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
12 * Initially based on the 'trace' prototype by Thomas Gleixner:
14 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
16 * Released under the GPL v2. (and only v2, not any later version)
19 #include <traceevent/event-parse.h>
20 #include <api/fs/tracing_path.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"
32 #include "util/path.h"
33 #include "util/session.h"
34 #include "util/thread.h"
35 #include <subcmd/parse-options.h>
36 #include "util/strlist.h"
37 #include "util/intlist.h"
38 #include "util/thread_map.h"
39 #include "util/stat.h"
40 #include "trace/beauty/beauty.h"
41 #include "trace-event.h"
42 #include "util/parse-events.h"
43 #include "util/bpf-loader.h"
44 #include "callchain.h"
45 #include "print_binary.h"
47 #include "syscalltbl.h"
48 #include "rb_resort.h"
56 #include <linux/err.h>
57 #include <linux/filter.h>
58 #include <linux/kernel.h>
59 #include <linux/random.h>
60 #include <linux/stringify.h>
61 #include <linux/time64.h>
64 #include "sane_ctype.h"
67 # define O_CLOEXEC 02000000
70 #ifndef F_LINUX_SPECIFIC_BASE
71 # define F_LINUX_SPECIFIC_BASE 1024
75 struct perf_tool tool;
76 struct syscalltbl *sctbl;
79 struct syscall *table;
82 struct perf_evsel *sys_enter,
87 struct record_opts opts;
88 struct perf_evlist *evlist;
90 struct thread *current;
91 struct cgroup *cgroup;
94 unsigned long nr_events;
95 unsigned long nr_events_printed;
96 unsigned long max_events;
97 struct strlist *ev_qualifier;
107 double duration_filter;
113 unsigned int max_stack;
114 unsigned int min_stack;
116 bool raw_augmented_syscalls;
117 bool not_ev_qualifier;
121 bool multiple_threads;
127 bool show_tool_stats;
129 bool kernel_syscallchains;
135 bool show_string_prefix;
140 struct ordered_events data;
148 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
149 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
153 #define TP_UINT_FIELD(bits) \
154 static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
157 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
166 #define TP_UINT_FIELD__SWAPPED(bits) \
167 static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
170 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
171 return bswap_##bits(value);\
174 TP_UINT_FIELD__SWAPPED(16);
175 TP_UINT_FIELD__SWAPPED(32);
176 TP_UINT_FIELD__SWAPPED(64);
178 static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap)
180 field->offset = offset;
184 field->integer = tp_field__u8;
187 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
190 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
193 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
202 static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap)
204 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
207 static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
209 return sample->raw_data + field->offset;
212 static int __tp_field__init_ptr(struct tp_field *field, int offset)
214 field->offset = offset;
215 field->pointer = tp_field__ptr;
219 static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
221 return __tp_field__init_ptr(field, format_field->offset);
227 struct tp_field args, ret;
231 static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
232 struct tp_field *field,
235 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
237 if (format_field == NULL)
240 return tp_field__init_uint(field, format_field, evsel->needs_swap);
243 #define perf_evsel__init_sc_tp_uint_field(evsel, name) \
244 ({ struct syscall_tp *sc = evsel->priv;\
245 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
247 static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
248 struct tp_field *field,
251 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
253 if (format_field == NULL)
256 return tp_field__init_ptr(field, format_field);
259 #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
260 ({ struct syscall_tp *sc = evsel->priv;\
261 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
263 static void perf_evsel__delete_priv(struct perf_evsel *evsel)
266 perf_evsel__delete(evsel);
269 static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
271 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
273 if (evsel->priv != NULL) {
274 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
275 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
286 static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel)
288 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
290 if (evsel->priv != NULL) { /* field, sizeof_field, offsetof_field */
291 if (__tp_field__init_uint(&sc->id, sizeof(long), sizeof(long long), evsel->needs_swap))
303 static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
305 struct syscall_tp *sc = evsel->priv;
307 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
310 static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
312 struct syscall_tp *sc = evsel->priv;
314 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
317 static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
319 evsel->priv = malloc(sizeof(struct syscall_tp));
320 if (evsel->priv != NULL) {
321 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
324 evsel->handler = handler;
335 static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
337 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
339 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
341 evsel = perf_evsel__newtp("syscalls", direction);
346 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
352 perf_evsel__delete_priv(evsel);
356 #define perf_evsel__sc_tp_uint(evsel, name, sample) \
357 ({ struct syscall_tp *fields = evsel->priv; \
358 fields->name.integer(&fields->name, sample); })
360 #define perf_evsel__sc_tp_ptr(evsel, name, sample) \
361 ({ struct syscall_tp *fields = evsel->priv; \
362 fields->name.pointer(&fields->name, sample); })
364 size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
366 int idx = val - sa->offset;
368 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
369 size_t printed = scnprintf(bf, size, intfmt, val);
371 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
375 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
378 static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
380 struct syscall_arg *arg)
382 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
385 static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
386 struct syscall_arg *arg)
388 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
391 #define SCA_STRARRAY syscall_arg__scnprintf_strarray
393 size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
398 for (i = 0; i < sas->nr_entries; ++i) {
399 struct strarray *sa = sas->entries[i];
400 int idx = val - sa->offset;
402 if (idx >= 0 && idx < sa->nr_entries) {
403 if (sa->entries[idx] == NULL)
405 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
409 printed = scnprintf(bf, size, intfmt, val);
411 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
415 size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
416 struct syscall_arg *arg)
418 return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
422 #define AT_FDCWD -100
425 static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
426 struct syscall_arg *arg)
429 const char *prefix = "AT_FD";
432 return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
434 return syscall_arg__scnprintf_fd(bf, size, arg);
437 #define SCA_FDAT syscall_arg__scnprintf_fd_at
439 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
440 struct syscall_arg *arg);
442 #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
444 size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
446 return scnprintf(bf, size, "%#lx", arg->val);
449 size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
452 return scnprintf(bf, size, "NULL");
453 return syscall_arg__scnprintf_hex(bf, size, arg);
456 size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
458 return scnprintf(bf, size, "%d", arg->val);
461 size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
463 return scnprintf(bf, size, "%ld", arg->val);
466 static const char *bpf_cmd[] = {
467 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
468 "MAP_GET_NEXT_KEY", "PROG_LOAD",
470 static DEFINE_STRARRAY(bpf_cmd, "BPF_");
472 static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
473 static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
475 static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
476 static DEFINE_STRARRAY(itimers, "ITIMER_");
478 static const char *keyctl_options[] = {
479 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
480 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
481 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
482 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
483 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
485 static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
487 static const char *whences[] = { "SET", "CUR", "END",
495 static DEFINE_STRARRAY(whences, "SEEK_");
497 static const char *fcntl_cmds[] = {
498 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
499 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
500 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
503 static DEFINE_STRARRAY(fcntl_cmds, "F_");
505 static const char *fcntl_linux_specific_cmds[] = {
506 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
507 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
508 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
511 static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
513 static struct strarray *fcntl_cmds_arrays[] = {
514 &strarray__fcntl_cmds,
515 &strarray__fcntl_linux_specific_cmds,
518 static DEFINE_STRARRAYS(fcntl_cmds_arrays);
520 static const char *rlimit_resources[] = {
521 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
522 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
525 static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
527 static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
528 static DEFINE_STRARRAY(sighow, "SIG_");
530 static const char *clockid[] = {
531 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
532 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
533 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
535 static DEFINE_STRARRAY(clockid, "CLOCK_");
537 static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
538 struct syscall_arg *arg)
540 bool show_prefix = arg->show_string_prefix;
541 const char *suffix = "_OK";
545 if (mode == F_OK) /* 0 */
546 return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
548 if (mode & n##_OK) { \
549 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
559 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
564 #define SCA_ACCMODE syscall_arg__scnprintf_access_mode
566 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
567 struct syscall_arg *arg);
569 #define SCA_FILENAME syscall_arg__scnprintf_filename
571 static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
572 struct syscall_arg *arg)
574 bool show_prefix = arg->show_string_prefix;
575 const char *prefix = "O_";
576 int printed = 0, flags = arg->val;
579 if (flags & O_##n) { \
580 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
589 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
594 #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
596 #ifndef GRND_NONBLOCK
597 #define GRND_NONBLOCK 0x0001
600 #define GRND_RANDOM 0x0002
603 static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
604 struct syscall_arg *arg)
606 bool show_prefix = arg->show_string_prefix;
607 const char *prefix = "GRND_";
608 int printed = 0, flags = arg->val;
611 if (flags & GRND_##n) { \
612 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
613 flags &= ~GRND_##n; \
621 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
626 #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
628 #define STRARRAY(name, array) \
629 { .scnprintf = SCA_STRARRAY, \
630 .parm = &strarray__##array, }
632 #include "trace/beauty/arch_errno_names.c"
633 #include "trace/beauty/eventfd.c"
634 #include "trace/beauty/futex_op.c"
635 #include "trace/beauty/futex_val3.c"
636 #include "trace/beauty/mmap.c"
637 #include "trace/beauty/mode_t.c"
638 #include "trace/beauty/msg_flags.c"
639 #include "trace/beauty/open_flags.c"
640 #include "trace/beauty/perf_event_open.c"
641 #include "trace/beauty/pid.c"
642 #include "trace/beauty/sched_policy.c"
643 #include "trace/beauty/seccomp.c"
644 #include "trace/beauty/signum.c"
645 #include "trace/beauty/socket_type.c"
646 #include "trace/beauty/waitid_options.c"
648 struct syscall_arg_fmt {
649 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
650 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
656 static struct syscall_fmt {
659 struct syscall_arg_fmt arg[6];
666 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
667 { .name = "arch_prctl",
668 .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
669 [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
671 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
673 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
674 { .name = "brk", .hexret = true,
675 .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
676 { .name = "clock_gettime",
677 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
678 { .name = "clone", .errpid = true, .nr_args = 5,
679 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
680 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
681 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
682 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
683 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
685 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
687 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
688 { .name = "epoll_ctl",
689 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
690 { .name = "eventfd2",
691 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
692 { .name = "fchmodat",
693 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
694 { .name = "fchownat",
695 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
697 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
698 .parm = &strarrays__fcntl_cmds_arrays,
699 .show_zero = true, },
700 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
702 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
703 { .name = "fstat", .alias = "newfstat", },
704 { .name = "fstatat", .alias = "newfstatat", },
706 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
707 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
708 { .name = "futimesat",
709 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
710 { .name = "getitimer",
711 .arg = { [0] = STRARRAY(which, itimers), }, },
712 { .name = "getpid", .errpid = true, },
713 { .name = "getpgid", .errpid = true, },
714 { .name = "getppid", .errpid = true, },
715 { .name = "getrandom",
716 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
717 { .name = "getrlimit",
718 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
719 { .name = "gettid", .errpid = true, },
722 #if defined(__i386__) || defined(__x86_64__)
724 * FIXME: Make this available to all arches.
726 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
727 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
729 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
731 { .name = "kcmp", .nr_args = 5,
732 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
733 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
734 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
735 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
736 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
738 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
740 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
742 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
744 .arg = { [2] = STRARRAY(whence, whences), }, },
745 { .name = "lstat", .alias = "newlstat", },
747 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
748 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
750 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
752 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
753 { .name = "mmap", .hexret = true,
754 /* The standard mmap maps to old_mmap on s390x */
755 #if defined(__s390x__)
758 .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
759 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ },
760 [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, },
762 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
763 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
764 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
765 { .name = "mprotect",
766 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
767 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
768 { .name = "mq_unlink",
769 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
770 { .name = "mremap", .hexret = true,
771 .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
772 { .name = "name_to_handle_at",
773 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
774 { .name = "newfstatat",
775 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
777 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
778 { .name = "open_by_handle_at",
779 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
780 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
782 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
783 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
784 { .name = "perf_event_open",
785 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
786 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
787 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
789 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
790 { .name = "pkey_alloc",
791 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
792 { .name = "pkey_free",
793 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
794 { .name = "pkey_mprotect",
795 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
796 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
797 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
798 { .name = "poll", .timeout = true, },
799 { .name = "ppoll", .timeout = true, },
801 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
802 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
803 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
804 { .name = "pread", .alias = "pread64", },
805 { .name = "preadv", .alias = "pread", },
806 { .name = "prlimit64",
807 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
808 { .name = "pwrite", .alias = "pwrite64", },
809 { .name = "readlinkat",
810 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
811 { .name = "recvfrom",
812 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
813 { .name = "recvmmsg",
814 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
816 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
817 { .name = "renameat",
818 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
819 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
820 { .name = "renameat2",
821 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
822 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
823 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
824 { .name = "rt_sigaction",
825 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
826 { .name = "rt_sigprocmask",
827 .arg = { [0] = STRARRAY(how, sighow), }, },
828 { .name = "rt_sigqueueinfo",
829 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
830 { .name = "rt_tgsigqueueinfo",
831 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
832 { .name = "sched_setscheduler",
833 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
835 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
836 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
837 { .name = "select", .timeout = true, },
838 { .name = "sendmmsg",
839 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
841 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
843 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
844 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
845 { .name = "set_tid_address", .errpid = true, },
846 { .name = "setitimer",
847 .arg = { [0] = STRARRAY(which, itimers), }, },
848 { .name = "setrlimit",
849 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
851 .arg = { [0] = STRARRAY(family, socket_families),
852 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
853 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
854 { .name = "socketpair",
855 .arg = { [0] = STRARRAY(family, socket_families),
856 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
857 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
858 { .name = "stat", .alias = "newstat", },
860 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
861 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
862 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
864 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
866 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
867 { .name = "symlinkat",
868 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
870 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
872 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
873 { .name = "umount2", .alias = "umount",
874 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
875 { .name = "uname", .alias = "newuname", },
876 { .name = "unlinkat",
877 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
878 { .name = "utimensat",
879 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
880 { .name = "wait4", .errpid = true,
881 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
882 { .name = "waitid", .errpid = true,
883 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
886 static int syscall_fmt__cmp(const void *name, const void *fmtp)
888 const struct syscall_fmt *fmt = fmtp;
889 return strcmp(name, fmt->name);
892 static struct syscall_fmt *syscall_fmt__find(const char *name)
894 const int nmemb = ARRAY_SIZE(syscall_fmts);
895 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
898 static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
900 int i, nmemb = ARRAY_SIZE(syscall_fmts);
902 for (i = 0; i < nmemb; ++i) {
903 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
904 return &syscall_fmts[i];
911 * is_exit: is this "exit" or "exit_group"?
912 * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter.
913 * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc.
916 struct tep_event *tp_format;
921 struct tep_format_field *args;
923 struct syscall_fmt *fmt;
924 struct syscall_arg_fmt *arg_fmt;
927 struct bpf_map_syscall_entry {
932 * We need to have this 'calculated' boolean because in some cases we really
933 * don't know what is the duration of a syscall, for instance, when we start
934 * a session and some threads are waiting for a syscall to finish, say 'poll',
935 * in which case all we can do is to print "( ? ) for duration and for the
938 static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
940 double duration = (double)t / NSEC_PER_MSEC;
941 size_t printed = fprintf(fp, "(");
944 printed += fprintf(fp, " ");
945 else if (duration >= 1.0)
946 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
947 else if (duration >= 0.01)
948 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
950 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
951 return printed + fprintf(fp, "): ");
955 * filename.ptr: The filename char pointer that will be vfs_getname'd
956 * filename.entry_str_pos: Where to insert the string translated from
957 * filename.ptr by the vfs_getname tracepoint/kprobe.
958 * ret_scnprintf: syscall args may set this to a different syscall return
959 * formatter, for instance, fcntl may return fds, file flags, etc.
961 struct thread_trace {
964 unsigned long nr_events;
965 unsigned long pfmaj, pfmin;
968 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
971 short int entry_str_pos;
973 unsigned int namelen;
981 struct intlist *syscall_stats;
984 static struct thread_trace *thread_trace__new(void)
986 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
989 ttrace->paths.max = -1;
991 ttrace->syscall_stats = intlist__new(NULL);
996 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
998 struct thread_trace *ttrace;
1003 if (thread__priv(thread) == NULL)
1004 thread__set_priv(thread, thread_trace__new());
1006 if (thread__priv(thread) == NULL)
1009 ttrace = thread__priv(thread);
1010 ++ttrace->nr_events;
1014 color_fprintf(fp, PERF_COLOR_RED,
1015 "WARNING: not enough memory, dropping samples!\n");
1020 void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
1021 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
1023 struct thread_trace *ttrace = thread__priv(arg->thread);
1025 ttrace->ret_scnprintf = ret_scnprintf;
1028 #define TRACE_PFMAJ (1 << 0)
1029 #define TRACE_PFMIN (1 << 1)
1031 static const size_t trace__entry_str_size = 2048;
1033 static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
1035 struct thread_trace *ttrace = thread__priv(thread);
1037 if (fd > ttrace->paths.max) {
1038 char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
1043 if (ttrace->paths.max != -1) {
1044 memset(npath + ttrace->paths.max + 1, 0,
1045 (fd - ttrace->paths.max) * sizeof(char *));
1047 memset(npath, 0, (fd + 1) * sizeof(char *));
1050 ttrace->paths.table = npath;
1051 ttrace->paths.max = fd;
1054 ttrace->paths.table[fd] = strdup(pathname);
1056 return ttrace->paths.table[fd] != NULL ? 0 : -1;
1059 static int thread__read_fd_path(struct thread *thread, int fd)
1061 char linkname[PATH_MAX], pathname[PATH_MAX];
1065 if (thread->pid_ == thread->tid) {
1066 scnprintf(linkname, sizeof(linkname),
1067 "/proc/%d/fd/%d", thread->pid_, fd);
1069 scnprintf(linkname, sizeof(linkname),
1070 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1073 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1076 ret = readlink(linkname, pathname, sizeof(pathname));
1078 if (ret < 0 || ret > st.st_size)
1081 pathname[ret] = '\0';
1082 return trace__set_fd_pathname(thread, fd, pathname);
1085 static const char *thread__fd_path(struct thread *thread, int fd,
1086 struct trace *trace)
1088 struct thread_trace *ttrace = thread__priv(thread);
1096 if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
1099 ++trace->stats.proc_getname;
1100 if (thread__read_fd_path(thread, fd))
1104 return ttrace->paths.table[fd];
1107 size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
1110 size_t printed = scnprintf(bf, size, "%d", fd);
1111 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
1114 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1119 size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1121 size_t printed = scnprintf(bf, size, "%d", fd);
1122 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1125 const char *path = thread__fd_path(thread, fd, trace);
1128 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1130 thread__put(thread);
1136 static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1137 struct syscall_arg *arg)
1140 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
1141 struct thread_trace *ttrace = thread__priv(arg->thread);
1143 if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
1144 zfree(&ttrace->paths.table[fd]);
1149 static void thread__set_filename_pos(struct thread *thread, const char *bf,
1152 struct thread_trace *ttrace = thread__priv(thread);
1154 ttrace->filename.ptr = ptr;
1155 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1158 static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1160 struct augmented_arg *augmented_arg = arg->augmented.args;
1162 return scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
1165 static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1166 struct syscall_arg *arg)
1168 unsigned long ptr = arg->val;
1170 if (arg->augmented.args)
1171 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1173 if (!arg->trace->vfs_getname)
1174 return scnprintf(bf, size, "%#x", ptr);
1176 thread__set_filename_pos(arg->thread, bf, ptr);
1180 static bool trace__filter_duration(struct trace *trace, double t)
1182 return t < (trace->duration_filter * NSEC_PER_MSEC);
1185 static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1187 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1189 return fprintf(fp, "%10.3f ", ts);
1193 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1194 * using ttrace->entry_time for a thread that receives a sys_exit without
1195 * first having received a sys_enter ("poll" issued before tracing session
1196 * starts, lost sys_enter exit due to ring buffer overflow).
1198 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1201 return __trace__fprintf_tstamp(trace, tstamp, fp);
1203 return fprintf(fp, " ? ");
1206 static bool done = false;
1207 static bool interrupted = false;
1209 static void sig_handler(int sig)
1212 interrupted = sig == SIGINT;
1215 static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
1219 if (trace->multiple_threads) {
1220 if (trace->show_comm)
1221 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
1222 printed += fprintf(fp, "%d ", thread->tid);
1228 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1229 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1233 if (trace->show_tstamp)
1234 printed = trace__fprintf_tstamp(trace, tstamp, fp);
1235 if (trace->show_duration)
1236 printed += fprintf_duration(duration, duration_calculated, fp);
1237 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1240 static int trace__process_event(struct trace *trace, struct machine *machine,
1241 union perf_event *event, struct perf_sample *sample)
1245 switch (event->header.type) {
1246 case PERF_RECORD_LOST:
1247 color_fprintf(trace->output, PERF_COLOR_RED,
1248 "LOST %" PRIu64 " events!\n", event->lost.lost);
1249 ret = machine__process_lost_event(machine, event, sample);
1252 ret = machine__process_event(machine, event, sample);
1259 static int trace__tool_process(struct perf_tool *tool,
1260 union perf_event *event,
1261 struct perf_sample *sample,
1262 struct machine *machine)
1264 struct trace *trace = container_of(tool, struct trace, tool);
1265 return trace__process_event(trace, machine, event, sample);
1268 static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1270 struct machine *machine = vmachine;
1272 if (machine->kptr_restrict_warned)
1275 if (symbol_conf.kptr_restrict) {
1276 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1277 "Check /proc/sys/kernel/kptr_restrict.\n\n"
1278 "Kernel samples will not be resolved.\n");
1279 machine->kptr_restrict_warned = true;
1283 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1286 static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1288 int err = symbol__init(NULL);
1293 trace->host = machine__new_host();
1294 if (trace->host == NULL)
1297 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1301 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
1302 evlist->threads, trace__tool_process, false,
1311 static void trace__symbols__exit(struct trace *trace)
1313 machine__exit(trace->host);
1319 static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1323 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1324 nr_args = sc->fmt->nr_args;
1326 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1327 if (sc->arg_fmt == NULL)
1330 for (idx = 0; idx < nr_args; ++idx) {
1332 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1335 sc->nr_args = nr_args;
1339 static int syscall__set_arg_fmts(struct syscall *sc)
1341 struct tep_format_field *field, *last_field = NULL;
1344 for (field = sc->args; field; field = field->next, ++idx) {
1347 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1350 if (strcmp(field->type, "const char *") == 0 &&
1351 (strcmp(field->name, "filename") == 0 ||
1352 strcmp(field->name, "path") == 0 ||
1353 strcmp(field->name, "pathname") == 0))
1354 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
1355 else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
1356 sc->arg_fmt[idx].scnprintf = SCA_PTR;
1357 else if (strcmp(field->type, "pid_t") == 0)
1358 sc->arg_fmt[idx].scnprintf = SCA_PID;
1359 else if (strcmp(field->type, "umode_t") == 0)
1360 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
1361 else if ((strcmp(field->type, "int") == 0 ||
1362 strcmp(field->type, "unsigned int") == 0 ||
1363 strcmp(field->type, "long") == 0) &&
1364 (len = strlen(field->name)) >= 2 &&
1365 strcmp(field->name + len - 2, "fd") == 0) {
1367 * /sys/kernel/tracing/events/syscalls/sys_enter*
1368 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1373 sc->arg_fmt[idx].scnprintf = SCA_FD;
1378 sc->args_size = last_field->offset + last_field->size;
1383 static int trace__read_syscall_info(struct trace *trace, int id)
1387 const char *name = syscalltbl__name(trace->sctbl, id);
1392 if (id > trace->syscalls.max) {
1393 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1395 if (nsyscalls == NULL)
1398 if (trace->syscalls.max != -1) {
1399 memset(nsyscalls + trace->syscalls.max + 1, 0,
1400 (id - trace->syscalls.max) * sizeof(*sc));
1402 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1405 trace->syscalls.table = nsyscalls;
1406 trace->syscalls.max = id;
1409 sc = trace->syscalls.table + id;
1412 sc->fmt = syscall_fmt__find(sc->name);
1414 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
1415 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
1417 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
1418 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
1419 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
1422 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1425 if (IS_ERR(sc->tp_format))
1428 sc->args = sc->tp_format->format.fields;
1430 * We need to check and discard the first variable '__syscall_nr'
1431 * or 'nr' that mean the syscall number. It is needless here.
1432 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1434 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
1435 sc->args = sc->args->next;
1439 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
1440 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
1442 return syscall__set_arg_fmts(sc);
1445 static int trace__validate_ev_qualifier(struct trace *trace)
1448 size_t nr_allocated;
1449 struct str_node *pos;
1451 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1452 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1453 sizeof(trace->ev_qualifier_ids.entries[0]));
1455 if (trace->ev_qualifier_ids.entries == NULL) {
1456 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1462 nr_allocated = trace->ev_qualifier_ids.nr;
1465 strlist__for_each_entry(pos, trace->ev_qualifier) {
1466 const char *sc = pos->s;
1467 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
1470 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1475 fputs("Error:\tInvalid syscall ", trace->output);
1478 fputs(", ", trace->output);
1481 fputs(sc, trace->output);
1484 trace->ev_qualifier_ids.entries[i++] = id;
1485 if (match_next == -1)
1489 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1492 if (nr_allocated == trace->ev_qualifier_ids.nr) {
1496 entries = realloc(trace->ev_qualifier_ids.entries,
1497 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1498 if (entries == NULL) {
1500 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1503 trace->ev_qualifier_ids.entries = entries;
1505 trace->ev_qualifier_ids.nr++;
1506 trace->ev_qualifier_ids.entries[i++] = id;
1511 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1512 "\nHint:\tand: 'man syscalls'\n", trace->output);
1514 zfree(&trace->ev_qualifier_ids.entries);
1515 trace->ev_qualifier_ids.nr = 0;
1522 * args is to be interpreted as a series of longs but we need to handle
1523 * 8-byte unaligned accesses. args points to raw_data within the event
1524 * and raw_data is guaranteed to be 8-byte unaligned because it is
1525 * preceded by raw_size which is a u32. So we need to copy args to a temp
1526 * variable to read it. Most notably this avoids extended load instructions
1527 * on unaligned addresses
1529 unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
1532 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
1534 memcpy(&val, p, sizeof(val));
1538 static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1539 struct syscall_arg *arg)
1541 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1542 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1544 return scnprintf(bf, size, "arg%d: ", arg->idx);
1548 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1549 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1550 * in tools/perf/trace/beauty/mount_flags.c
1552 static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1554 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1555 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1560 static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1561 struct syscall_arg *arg, unsigned long val)
1563 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1565 if (sc->arg_fmt[arg->idx].parm)
1566 arg->parm = sc->arg_fmt[arg->idx].parm;
1567 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1569 return scnprintf(bf, size, "%ld", val);
1572 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
1573 unsigned char *args, void *augmented_args, int augmented_args_size,
1574 struct trace *trace, struct thread *thread)
1579 struct syscall_arg arg = {
1582 .size = augmented_args_size,
1583 .args = augmented_args,
1589 .show_string_prefix = trace->show_string_prefix,
1591 struct thread_trace *ttrace = thread__priv(thread);
1594 * Things like fcntl will set this in its 'cmd' formatter to pick the
1595 * right formatter for the return value (an fd? file flags?), which is
1596 * not needed for syscalls that always return a given type, say an fd.
1598 ttrace->ret_scnprintf = NULL;
1600 if (sc->args != NULL) {
1601 struct tep_format_field *field;
1603 for (field = sc->args; field;
1604 field = field->next, ++arg.idx, bit <<= 1) {
1608 val = syscall_arg__val(&arg, arg.idx);
1610 * Some syscall args need some mask, most don't and
1611 * return val untouched.
1613 val = syscall__mask_val(sc, &arg, val);
1616 * Suppress this argument if its value is zero and
1617 * and we don't have a string associated in an
1621 !trace->show_zeros &&
1623 (sc->arg_fmt[arg.idx].show_zero ||
1624 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
1625 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1626 sc->arg_fmt[arg.idx].parm))
1629 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
1631 if (trace->show_arg_names)
1632 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
1634 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1636 } else if (IS_ERR(sc->tp_format)) {
1638 * If we managed to read the tracepoint /format file, then we
1639 * may end up not having any args, like with gettid(), so only
1640 * print the raw args when we didn't manage to read it.
1642 while (arg.idx < sc->nr_args) {
1645 val = syscall_arg__val(&arg, arg.idx);
1647 printed += scnprintf(bf + printed, size - printed, ", ");
1648 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
1649 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1659 typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
1660 union perf_event *event,
1661 struct perf_sample *sample);
1663 static struct syscall *trace__syscall_info(struct trace *trace,
1664 struct perf_evsel *evsel, int id)
1670 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1671 * before that, leaving at a higher verbosity level till that is
1672 * explained. Reproduced with plain ftrace with:
1674 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1675 * grep "NR -1 " /t/trace_pipe
1677 * After generating some load on the machine.
1681 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1682 id, perf_evsel__name(evsel), ++n);
1687 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1688 trace__read_syscall_info(trace, id))
1691 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1694 return &trace->syscalls.table[id];
1698 fprintf(trace->output, "Problems reading syscall %d", id);
1699 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1700 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1701 fputs(" information\n", trace->output);
1706 static void thread__update_stats(struct thread_trace *ttrace,
1707 int id, struct perf_sample *sample)
1709 struct int_node *inode;
1710 struct stats *stats;
1713 inode = intlist__findnew(ttrace->syscall_stats, id);
1717 stats = inode->priv;
1718 if (stats == NULL) {
1719 stats = malloc(sizeof(struct stats));
1723 inode->priv = stats;
1726 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1727 duration = sample->time - ttrace->entry_time;
1729 update_stats(stats, duration);
1732 static int trace__printf_interrupted_entry(struct trace *trace)
1734 struct thread_trace *ttrace;
1737 if (trace->failure_only || trace->current == NULL)
1740 ttrace = thread__priv(trace->current);
1742 if (!ttrace->entry_pending)
1745 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
1746 printed += fprintf(trace->output, ")%-*s ...\n", trace->args_alignment, ttrace->entry_str);
1747 ttrace->entry_pending = false;
1749 ++trace->nr_events_printed;
1754 static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1755 struct perf_sample *sample, struct thread *thread)
1759 if (trace->print_sample) {
1760 double ts = (double)sample->time / NSEC_PER_MSEC;
1762 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1763 perf_evsel__name(evsel), ts,
1764 thread__comm_str(thread),
1765 sample->pid, sample->tid, sample->cpu);
1771 static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, bool raw_augmented)
1773 void *augmented_args = NULL;
1775 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
1776 * and there we get all 6 syscall args plus the tracepoint common
1777 * fields (sizeof(long)) and the syscall_nr (another long). So we check
1778 * if that is the case and if so don't look after the sc->args_size,
1779 * but always after the full raw_syscalls:sys_enter payload, which is
1782 * We'll revisit this later to pass s->args_size to the BPF augmenter
1783 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1784 * copies only what we need for each syscall, like what happens when we
1785 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1786 * traffic to just what is needed for each syscall.
1788 int args_size = raw_augmented ? (8 * (int)sizeof(long)) : sc->args_size;
1790 *augmented_args_size = sample->raw_size - args_size;
1791 if (*augmented_args_size > 0)
1792 augmented_args = sample->raw_data + args_size;
1794 return augmented_args;
1797 static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
1798 union perf_event *event __maybe_unused,
1799 struct perf_sample *sample)
1804 struct thread *thread;
1805 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1806 int augmented_args_size = 0;
1807 void *augmented_args = NULL;
1808 struct syscall *sc = trace__syscall_info(trace, evsel, id);
1809 struct thread_trace *ttrace;
1814 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1815 ttrace = thread__trace(thread, trace->output);
1819 trace__fprintf_sample(trace, evsel, sample, thread);
1821 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
1823 if (ttrace->entry_str == NULL) {
1824 ttrace->entry_str = malloc(trace__entry_str_size);
1825 if (!ttrace->entry_str)
1829 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
1830 trace__printf_interrupted_entry(trace);
1832 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1833 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1834 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1835 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1836 * so when handling, say the openat syscall, we end up getting 6 args for the
1837 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1838 * thinking that the extra 2 u64 args are the augmented filename, so just check
1839 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1841 if (evsel != trace->syscalls.events.sys_enter)
1842 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
1843 ttrace->entry_time = sample->time;
1844 msg = ttrace->entry_str;
1845 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
1847 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
1848 args, augmented_args, augmented_args_size, trace, thread);
1851 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
1854 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
1855 printed = fprintf(trace->output, "%s)", ttrace->entry_str);
1856 if (trace->args_alignment > printed)
1857 alignment = trace->args_alignment - printed;
1858 fprintf(trace->output, "%*s= ?\n", alignment, " ");
1861 ttrace->entry_pending = true;
1862 /* See trace__vfs_getname & trace__sys_exit */
1863 ttrace->filename.pending_open = false;
1866 if (trace->current != thread) {
1867 thread__put(trace->current);
1868 trace->current = thread__get(thread);
1872 thread__put(thread);
1876 static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1877 struct perf_sample *sample)
1879 struct thread_trace *ttrace;
1880 struct thread *thread;
1881 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1882 struct syscall *sc = trace__syscall_info(trace, evsel, id);
1884 void *args, *augmented_args = NULL;
1885 int augmented_args_size;
1890 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1891 ttrace = thread__trace(thread, trace->output);
1893 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1894 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1899 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
1900 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
1901 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
1902 fprintf(trace->output, "%s", msg);
1905 thread__put(thread);
1909 static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1910 struct perf_sample *sample,
1911 struct callchain_cursor *cursor)
1913 struct addr_location al;
1914 int max_stack = evsel->attr.sample_max_stack ?
1915 evsel->attr.sample_max_stack :
1919 if (machine__resolve(trace->host, &al, sample) < 0)
1922 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
1923 addr_location__put(&al);
1927 static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
1929 /* TODO: user-configurable print_opts */
1930 const unsigned int print_opts = EVSEL__PRINT_SYM |
1932 EVSEL__PRINT_UNKNOWN_AS_ADDR;
1934 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
1937 static const char *errno_to_name(struct perf_evsel *evsel, int err)
1939 struct perf_env *env = perf_evsel__env(evsel);
1940 const char *arch_name = perf_env__arch(env);
1942 return arch_syscalls__strerrno(arch_name, err);
1945 static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
1946 union perf_event *event __maybe_unused,
1947 struct perf_sample *sample)
1951 bool duration_calculated = false;
1952 struct thread *thread;
1953 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
1954 int alignment = trace->args_alignment;
1955 struct syscall *sc = trace__syscall_info(trace, evsel, id);
1956 struct thread_trace *ttrace;
1961 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1962 ttrace = thread__trace(thread, trace->output);
1966 trace__fprintf_sample(trace, evsel, sample, thread);
1969 thread__update_stats(ttrace, id, sample);
1971 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
1973 if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
1974 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
1975 ttrace->filename.pending_open = false;
1976 ++trace->stats.vfs_getname;
1979 if (ttrace->entry_time) {
1980 duration = sample->time - ttrace->entry_time;
1981 if (trace__filter_duration(trace, duration))
1983 duration_calculated = true;
1984 } else if (trace->duration_filter)
1987 if (sample->callchain) {
1988 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
1989 if (callchain_ret == 0) {
1990 if (callchain_cursor.nr < trace->min_stack)
1996 if (trace->summary_only || (ret >= 0 && trace->failure_only))
1999 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
2001 if (ttrace->entry_pending) {
2002 printed = fprintf(trace->output, "%s", ttrace->entry_str);
2004 fprintf(trace->output, " ... [");
2005 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
2006 fprintf(trace->output, "]: %s()", sc->name);
2009 printed++; /* the closing ')' */
2011 if (alignment > printed)
2012 alignment -= printed;
2016 fprintf(trace->output, ")%*s= ", alignment, " ");
2018 if (sc->fmt == NULL) {
2022 fprintf(trace->output, "%ld", ret);
2023 } else if (ret < 0) {
2025 char bf[STRERR_BUFSIZE];
2026 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
2027 *e = errno_to_name(evsel, -ret);
2029 fprintf(trace->output, "-1 %s (%s)", e, emsg);
2031 } else if (ret == 0 && sc->fmt->timeout)
2032 fprintf(trace->output, "0 (Timeout)");
2033 else if (ttrace->ret_scnprintf) {
2035 struct syscall_arg arg = {
2040 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
2041 ttrace->ret_scnprintf = NULL;
2042 fprintf(trace->output, "%s", bf);
2043 } else if (sc->fmt->hexret)
2044 fprintf(trace->output, "%#lx", ret);
2045 else if (sc->fmt->errpid) {
2046 struct thread *child = machine__find_thread(trace->host, ret, ret);
2048 if (child != NULL) {
2049 fprintf(trace->output, "%ld", ret);
2050 if (child->comm_set)
2051 fprintf(trace->output, " (%s)", thread__comm_str(child));
2057 fputc('\n', trace->output);
2060 * We only consider an 'event' for the sake of --max-events a non-filtered
2061 * sys_enter + sys_exit and other tracepoint events.
2063 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2066 if (callchain_ret > 0)
2067 trace__fprintf_callchain(trace, sample);
2068 else if (callchain_ret < 0)
2069 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2071 ttrace->entry_pending = false;
2074 thread__put(thread);
2078 static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
2079 union perf_event *event __maybe_unused,
2080 struct perf_sample *sample)
2082 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2083 struct thread_trace *ttrace;
2084 size_t filename_len, entry_str_len, to_move;
2085 ssize_t remaining_space;
2087 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
2092 ttrace = thread__priv(thread);
2096 filename_len = strlen(filename);
2097 if (filename_len == 0)
2100 if (ttrace->filename.namelen < filename_len) {
2101 char *f = realloc(ttrace->filename.name, filename_len + 1);
2106 ttrace->filename.namelen = filename_len;
2107 ttrace->filename.name = f;
2110 strcpy(ttrace->filename.name, filename);
2111 ttrace->filename.pending_open = true;
2113 if (!ttrace->filename.ptr)
2116 entry_str_len = strlen(ttrace->entry_str);
2117 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2118 if (remaining_space <= 0)
2121 if (filename_len > (size_t)remaining_space) {
2122 filename += filename_len - remaining_space;
2123 filename_len = remaining_space;
2126 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2127 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2128 memmove(pos + filename_len, pos, to_move);
2129 memcpy(pos, filename, filename_len);
2131 ttrace->filename.ptr = 0;
2132 ttrace->filename.entry_str_pos = 0;
2134 thread__put(thread);
2139 static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
2140 union perf_event *event __maybe_unused,
2141 struct perf_sample *sample)
2143 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2144 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
2145 struct thread *thread = machine__findnew_thread(trace->host,
2148 struct thread_trace *ttrace = thread__trace(thread, trace->output);
2153 ttrace->runtime_ms += runtime_ms;
2154 trace->runtime_ms += runtime_ms;
2156 thread__put(thread);
2160 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
2162 perf_evsel__strval(evsel, sample, "comm"),
2163 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2165 perf_evsel__intval(evsel, sample, "vruntime"));
2169 static int bpf_output__printer(enum binary_printer_ops op,
2170 unsigned int val, void *extra __maybe_unused, FILE *fp)
2172 unsigned char ch = (unsigned char)val;
2175 case BINARY_PRINT_CHAR_DATA:
2176 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
2177 case BINARY_PRINT_DATA_BEGIN:
2178 case BINARY_PRINT_LINE_BEGIN:
2179 case BINARY_PRINT_ADDR:
2180 case BINARY_PRINT_NUM_DATA:
2181 case BINARY_PRINT_NUM_PAD:
2182 case BINARY_PRINT_SEP:
2183 case BINARY_PRINT_CHAR_PAD:
2184 case BINARY_PRINT_LINE_END:
2185 case BINARY_PRINT_DATA_END:
2193 static void bpf_output__fprintf(struct trace *trace,
2194 struct perf_sample *sample)
2196 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2197 bpf_output__printer, NULL, trace->output);
2198 ++trace->nr_events_printed;
2201 static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2202 union perf_event *event __maybe_unused,
2203 struct perf_sample *sample)
2205 struct thread *thread;
2206 int callchain_ret = 0;
2208 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2209 * this event's max_events having been hit and this is an entry coming
2210 * from the ring buffer that we should discard, since the max events
2211 * have already been considered/printed.
2213 if (evsel->disabled)
2216 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2218 if (sample->callchain) {
2219 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2220 if (callchain_ret == 0) {
2221 if (callchain_cursor.nr < trace->min_stack)
2227 trace__printf_interrupted_entry(trace);
2228 trace__fprintf_tstamp(trace, sample->time, trace->output);
2230 if (trace->trace_syscalls && trace->show_duration)
2231 fprintf(trace->output, "( ): ");
2234 trace__fprintf_comm_tid(trace, thread, trace->output);
2236 if (evsel == trace->syscalls.events.augmented) {
2237 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2238 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2241 fprintf(trace->output, "%s(", sc->name);
2242 trace__fprintf_sys_enter(trace, evsel, sample);
2243 fputc(')', trace->output);
2248 * XXX: Not having the associated syscall info or not finding/adding
2249 * the thread should never happen, but if it does...
2250 * fall thru and print it as a bpf_output event.
2254 fprintf(trace->output, "%s:", evsel->name);
2256 if (perf_evsel__is_bpf_output(evsel)) {
2257 bpf_output__fprintf(trace, sample);
2258 } else if (evsel->tp_format) {
2259 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2260 trace__fprintf_sys_enter(trace, evsel, sample)) {
2261 event_format__fprintf(evsel->tp_format, sample->cpu,
2262 sample->raw_data, sample->raw_size,
2264 ++trace->nr_events_printed;
2266 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2267 perf_evsel__disable(evsel);
2268 perf_evsel__close(evsel);
2274 fprintf(trace->output, "\n");
2276 if (callchain_ret > 0)
2277 trace__fprintf_callchain(trace, sample);
2278 else if (callchain_ret < 0)
2279 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2281 thread__put(thread);
2285 static void print_location(FILE *f, struct perf_sample *sample,
2286 struct addr_location *al,
2287 bool print_dso, bool print_sym)
2290 if ((verbose > 0 || print_dso) && al->map)
2291 fprintf(f, "%s@", al->map->dso->long_name);
2293 if ((verbose > 0 || print_sym) && al->sym)
2294 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
2295 al->addr - al->sym->start);
2297 fprintf(f, "0x%" PRIx64, al->addr);
2299 fprintf(f, "0x%" PRIx64, sample->addr);
2302 static int trace__pgfault(struct trace *trace,
2303 struct perf_evsel *evsel,
2304 union perf_event *event __maybe_unused,
2305 struct perf_sample *sample)
2307 struct thread *thread;
2308 struct addr_location al;
2309 char map_type = 'd';
2310 struct thread_trace *ttrace;
2312 int callchain_ret = 0;
2314 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2316 if (sample->callchain) {
2317 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2318 if (callchain_ret == 0) {
2319 if (callchain_cursor.nr < trace->min_stack)
2325 ttrace = thread__trace(thread, trace->output);
2329 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2334 if (trace->summary_only)
2337 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
2339 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
2341 fprintf(trace->output, "%sfault [",
2342 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2345 print_location(trace->output, sample, &al, false, true);
2347 fprintf(trace->output, "] => ");
2349 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
2352 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
2360 print_location(trace->output, sample, &al, true, false);
2362 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
2364 if (callchain_ret > 0)
2365 trace__fprintf_callchain(trace, sample);
2366 else if (callchain_ret < 0)
2367 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2369 ++trace->nr_events_printed;
2373 thread__put(thread);
2377 static void trace__set_base_time(struct trace *trace,
2378 struct perf_evsel *evsel,
2379 struct perf_sample *sample)
2382 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2383 * and don't use sample->time unconditionally, we may end up having
2384 * some other event in the future without PERF_SAMPLE_TIME for good
2385 * reason, i.e. we may not be interested in its timestamps, just in
2386 * it taking place, picking some piece of information when it
2387 * appears in our event stream (vfs_getname comes to mind).
2389 if (trace->base_time == 0 && !trace->full_time &&
2390 (evsel->attr.sample_type & PERF_SAMPLE_TIME))
2391 trace->base_time = sample->time;
2394 static int trace__process_sample(struct perf_tool *tool,
2395 union perf_event *event,
2396 struct perf_sample *sample,
2397 struct perf_evsel *evsel,
2398 struct machine *machine __maybe_unused)
2400 struct trace *trace = container_of(tool, struct trace, tool);
2401 struct thread *thread;
2404 tracepoint_handler handler = evsel->handler;
2406 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2407 if (thread && thread__is_filtered(thread))
2410 trace__set_base_time(trace, evsel, sample);
2414 handler(trace, evsel, event, sample);
2417 thread__put(thread);
2421 static int trace__record(struct trace *trace, int argc, const char **argv)
2423 unsigned int rec_argc, i, j;
2424 const char **rec_argv;
2425 const char * const record_args[] = {
2432 const char * const sc_args[] = { "-e", };
2433 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2434 const char * const majpf_args[] = { "-e", "major-faults" };
2435 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2436 const char * const minpf_args[] = { "-e", "minor-faults" };
2437 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2439 /* +1 is for the event string below */
2440 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2441 majpf_args_nr + minpf_args_nr + argc;
2442 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2444 if (rec_argv == NULL)
2448 for (i = 0; i < ARRAY_SIZE(record_args); i++)
2449 rec_argv[j++] = record_args[i];
2451 if (trace->trace_syscalls) {
2452 for (i = 0; i < sc_args_nr; i++)
2453 rec_argv[j++] = sc_args[i];
2455 /* event string may be different for older kernels - e.g., RHEL6 */
2456 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2457 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2458 else if (is_valid_tracepoint("syscalls:sys_enter"))
2459 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2461 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
2467 if (trace->trace_pgfaults & TRACE_PFMAJ)
2468 for (i = 0; i < majpf_args_nr; i++)
2469 rec_argv[j++] = majpf_args[i];
2471 if (trace->trace_pgfaults & TRACE_PFMIN)
2472 for (i = 0; i < minpf_args_nr; i++)
2473 rec_argv[j++] = minpf_args[i];
2475 for (i = 0; i < (unsigned int)argc; i++)
2476 rec_argv[j++] = argv[i];
2478 return cmd_record(j, rec_argv);
2481 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2483 static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
2485 struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
2490 if (perf_evsel__field(evsel, "pathname") == NULL) {
2491 perf_evsel__delete(evsel);
2495 evsel->handler = trace__vfs_getname;
2496 perf_evlist__add(evlist, evsel);
2500 static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
2502 struct perf_evsel *evsel;
2503 struct perf_event_attr attr = {
2504 .type = PERF_TYPE_SOFTWARE,
2508 attr.config = config;
2509 attr.sample_period = 1;
2511 event_attr_init(&attr);
2513 evsel = perf_evsel__new(&attr);
2515 evsel->handler = trace__pgfault;
2520 static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2522 const u32 type = event->header.type;
2523 struct perf_evsel *evsel;
2525 if (type != PERF_RECORD_SAMPLE) {
2526 trace__process_event(trace, trace->host, event, sample);
2530 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2531 if (evsel == NULL) {
2532 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2536 trace__set_base_time(trace, evsel, sample);
2538 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2539 sample->raw_data == NULL) {
2540 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2541 perf_evsel__name(evsel), sample->tid,
2542 sample->cpu, sample->raw_size);
2544 tracepoint_handler handler = evsel->handler;
2545 handler(trace, evsel, event, sample);
2548 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2552 static int trace__add_syscall_newtp(struct trace *trace)
2555 struct perf_evlist *evlist = trace->evlist;
2556 struct perf_evsel *sys_enter, *sys_exit;
2558 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
2559 if (sys_enter == NULL)
2562 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2563 goto out_delete_sys_enter;
2565 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
2566 if (sys_exit == NULL)
2567 goto out_delete_sys_enter;
2569 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2570 goto out_delete_sys_exit;
2572 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2573 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2575 perf_evlist__add(evlist, sys_enter);
2576 perf_evlist__add(evlist, sys_exit);
2578 if (callchain_param.enabled && !trace->kernel_syscallchains) {
2580 * We're interested only in the user space callchain
2581 * leading to the syscall, allow overriding that for
2582 * debugging reasons using --kernel_syscall_callchains
2584 sys_exit->attr.exclude_callchain_kernel = 1;
2587 trace->syscalls.events.sys_enter = sys_enter;
2588 trace->syscalls.events.sys_exit = sys_exit;
2594 out_delete_sys_exit:
2595 perf_evsel__delete_priv(sys_exit);
2596 out_delete_sys_enter:
2597 perf_evsel__delete_priv(sys_enter);
2601 static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
2604 struct perf_evsel *sys_exit;
2605 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2606 trace->ev_qualifier_ids.nr,
2607 trace->ev_qualifier_ids.entries);
2612 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2614 sys_exit = trace->syscalls.events.sys_exit;
2615 err = perf_evsel__append_tp_filter(sys_exit, filter);
2626 #ifdef HAVE_LIBBPF_SUPPORT
2627 static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2629 int fd = bpf_map__fd(trace->syscalls.map);
2630 struct bpf_map_syscall_entry value = {
2631 .enabled = !trace->not_ev_qualifier,
2636 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2637 int key = trace->ev_qualifier_ids.entries[i];
2639 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2647 static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2649 int fd = bpf_map__fd(trace->syscalls.map);
2650 struct bpf_map_syscall_entry value = {
2655 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
2656 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
2664 static int trace__init_syscalls_bpf_map(struct trace *trace)
2666 bool enabled = true;
2668 if (trace->ev_qualifier_ids.nr)
2669 enabled = trace->not_ev_qualifier;
2671 return __trace__init_syscalls_bpf_map(trace, enabled);
2674 static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
2679 static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
2683 #endif // HAVE_LIBBPF_SUPPORT
2685 static int trace__set_ev_qualifier_filter(struct trace *trace)
2687 if (trace->syscalls.map)
2688 return trace__set_ev_qualifier_bpf_filter(trace);
2689 return trace__set_ev_qualifier_tp_filter(trace);
2692 static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
2693 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
2696 #ifdef HAVE_LIBBPF_SUPPORT
2698 int map_fd = bpf_map__fd(map);
2701 for (i = 0; i < npids; ++i) {
2702 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
2710 static int trace__set_filter_loop_pids(struct trace *trace)
2712 unsigned int nr = 1, err;
2716 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2718 while (thread && nr < ARRAY_SIZE(pids)) {
2719 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2724 if (!strcmp(thread__comm_str(parent), "sshd")) {
2725 pids[nr++] = parent->tid;
2731 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
2732 if (!err && trace->filter_pids.map)
2733 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
2738 static int trace__set_filter_pids(struct trace *trace)
2742 * Better not use !target__has_task() here because we need to cover the
2743 * case where no threads were specified in the command line, but a
2744 * workload was, and in that case we will fill in the thread_map when
2745 * we fork the workload in perf_evlist__prepare_workload.
2747 if (trace->filter_pids.nr > 0) {
2748 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
2749 trace->filter_pids.entries);
2750 if (!err && trace->filter_pids.map) {
2751 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
2752 trace->filter_pids.entries);
2754 } else if (thread_map__pid(trace->evlist->threads, 0) == -1) {
2755 err = trace__set_filter_loop_pids(trace);
2761 static int __trace__deliver_event(struct trace *trace, union perf_event *event)
2763 struct perf_evlist *evlist = trace->evlist;
2764 struct perf_sample sample;
2767 err = perf_evlist__parse_sample(evlist, event, &sample);
2769 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
2771 trace__handle_event(trace, event, &sample);
2776 static int __trace__flush_events(struct trace *trace)
2778 u64 first = ordered_events__first_time(&trace->oe.data);
2779 u64 flush = trace->oe.last - NSEC_PER_SEC;
2781 /* Is there some thing to flush.. */
2782 if (first && first < flush)
2783 return ordered_events__flush_time(&trace->oe.data, flush);
2788 static int trace__flush_events(struct trace *trace)
2790 return !trace->sort_events ? 0 : __trace__flush_events(trace);
2793 static int trace__deliver_event(struct trace *trace, union perf_event *event)
2797 if (!trace->sort_events)
2798 return __trace__deliver_event(trace, event);
2800 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
2801 if (err && err != -1)
2804 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
2808 return trace__flush_events(trace);
2811 static int ordered_events__deliver_event(struct ordered_events *oe,
2812 struct ordered_event *event)
2814 struct trace *trace = container_of(oe, struct trace, oe.data);
2816 return __trace__deliver_event(trace, event->event);
2819 static int trace__run(struct trace *trace, int argc, const char **argv)
2821 struct perf_evlist *evlist = trace->evlist;
2822 struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
2824 unsigned long before;
2825 const bool forks = argc > 0;
2826 bool draining = false;
2830 if (!trace->raw_augmented_syscalls) {
2831 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
2832 goto out_error_raw_syscalls;
2834 if (trace->trace_syscalls)
2835 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
2838 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2839 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2840 if (pgfault_maj == NULL)
2842 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
2843 perf_evlist__add(evlist, pgfault_maj);
2846 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2847 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2848 if (pgfault_min == NULL)
2850 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
2851 perf_evlist__add(evlist, pgfault_min);
2855 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2856 trace__sched_stat_runtime))
2857 goto out_error_sched_stat_runtime;
2860 * If a global cgroup was set, apply it to all the events without an
2861 * explicit cgroup. I.e.:
2863 * trace -G A -e sched:*switch
2865 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2866 * _and_ sched:sched_switch to the 'A' cgroup, while:
2868 * trace -e sched:*switch -G A
2870 * will only set the sched:sched_switch event to the 'A' cgroup, all the
2871 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2872 * a cgroup (on the root cgroup, sys wide, etc).
2876 * trace -G A -e sched:*switch -G B
2878 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
2879 * to the 'B' cgroup.
2881 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
2882 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
2885 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
2887 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2889 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
2890 goto out_delete_evlist;
2893 err = trace__symbols_init(trace, evlist);
2895 fprintf(trace->output, "Problems initializing symbol libraries!\n");
2896 goto out_delete_evlist;
2899 perf_evlist__config(evlist, &trace->opts, &callchain_param);
2901 signal(SIGCHLD, sig_handler);
2902 signal(SIGINT, sig_handler);
2905 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
2908 fprintf(trace->output, "Couldn't run the workload!\n");
2909 goto out_delete_evlist;
2913 err = perf_evlist__open(evlist);
2915 goto out_error_open;
2917 err = bpf__apply_obj_config();
2919 char errbuf[BUFSIZ];
2921 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2922 pr_err("ERROR: Apply config to BPF failed: %s\n",
2924 goto out_error_open;
2927 err = trace__set_filter_pids(trace);
2931 if (trace->syscalls.map)
2932 trace__init_syscalls_bpf_map(trace);
2934 if (trace->ev_qualifier_ids.nr > 0) {
2935 err = trace__set_ev_qualifier_filter(trace);
2939 if (trace->syscalls.events.sys_exit) {
2940 pr_debug("event qualifier tracepoint filter: %s\n",
2941 trace->syscalls.events.sys_exit->filter);
2945 err = perf_evlist__apply_filters(evlist, &evsel);
2947 goto out_error_apply_filters;
2949 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
2951 goto out_error_mmap;
2953 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
2954 perf_evlist__enable(evlist);
2957 perf_evlist__start_workload(evlist);
2959 if (trace->opts.initial_delay) {
2960 usleep(trace->opts.initial_delay * 1000);
2961 perf_evlist__enable(evlist);
2964 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
2965 evlist->threads->nr > 1 ||
2966 perf_evlist__first(evlist)->attr.inherit;
2969 * Now that we already used evsel->attr to ask the kernel to setup the
2970 * events, lets reuse evsel->attr.sample_max_stack as the limit in
2971 * trace__resolve_callchain(), allowing per-event max-stack settings
2972 * to override an explicitly set --max-stack global setting.
2974 evlist__for_each_entry(evlist, evsel) {
2975 if (evsel__has_callchain(evsel) &&
2976 evsel->attr.sample_max_stack == 0)
2977 evsel->attr.sample_max_stack = trace->max_stack;
2980 before = trace->nr_events;
2982 for (i = 0; i < evlist->nr_mmaps; i++) {
2983 union perf_event *event;
2984 struct perf_mmap *md;
2986 md = &evlist->mmap[i];
2987 if (perf_mmap__read_init(md) < 0)
2990 while ((event = perf_mmap__read_event(md)) != NULL) {
2993 err = trace__deliver_event(trace, event);
2997 perf_mmap__consume(md);
3002 if (done && !draining) {
3003 perf_evlist__disable(evlist);
3007 perf_mmap__read_done(md);
3010 if (trace->nr_events == before) {
3011 int timeout = done ? 100 : -1;
3013 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
3014 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
3019 if (trace__flush_events(trace))
3027 thread__zput(trace->current);
3029 perf_evlist__disable(evlist);
3031 if (trace->sort_events)
3032 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
3036 trace__fprintf_thread_summary(trace, trace->output);
3038 if (trace->show_tool_stats) {
3039 fprintf(trace->output, "Stats:\n "
3040 " vfs_getname : %" PRIu64 "\n"
3041 " proc_getname: %" PRIu64 "\n",
3042 trace->stats.vfs_getname,
3043 trace->stats.proc_getname);
3048 trace__symbols__exit(trace);
3050 perf_evlist__delete(evlist);
3051 cgroup__put(trace->cgroup);
3052 trace->evlist = NULL;
3053 trace->live = false;
3056 char errbuf[BUFSIZ];
3058 out_error_sched_stat_runtime:
3059 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
3062 out_error_raw_syscalls:
3063 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
3067 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3071 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3074 fprintf(trace->output, "%s\n", errbuf);
3075 goto out_delete_evlist;
3077 out_error_apply_filters:
3078 fprintf(trace->output,
3079 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3080 evsel->filter, perf_evsel__name(evsel), errno,
3081 str_error_r(errno, errbuf, sizeof(errbuf)));
3082 goto out_delete_evlist;
3085 fprintf(trace->output, "Not enough memory to run!\n");
3086 goto out_delete_evlist;
3089 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3090 goto out_delete_evlist;
3093 static int trace__replay(struct trace *trace)
3095 const struct perf_evsel_str_handler handlers[] = {
3096 { "probe:vfs_getname", trace__vfs_getname, },
3098 struct perf_data data = {
3102 .mode = PERF_DATA_MODE_READ,
3103 .force = trace->force,
3105 struct perf_session *session;
3106 struct perf_evsel *evsel;
3109 trace->tool.sample = trace__process_sample;
3110 trace->tool.mmap = perf_event__process_mmap;
3111 trace->tool.mmap2 = perf_event__process_mmap2;
3112 trace->tool.comm = perf_event__process_comm;
3113 trace->tool.exit = perf_event__process_exit;
3114 trace->tool.fork = perf_event__process_fork;
3115 trace->tool.attr = perf_event__process_attr;
3116 trace->tool.tracing_data = perf_event__process_tracing_data;
3117 trace->tool.build_id = perf_event__process_build_id;
3118 trace->tool.namespaces = perf_event__process_namespaces;
3120 trace->tool.ordered_events = true;
3121 trace->tool.ordering_requires_timestamps = true;
3123 /* add tid to output */
3124 trace->multiple_threads = true;
3126 session = perf_session__new(&data, false, &trace->tool);
3127 if (session == NULL)
3130 if (trace->opts.target.pid)
3131 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3133 if (trace->opts.target.tid)
3134 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3136 if (symbol__init(&session->header.env) < 0)
3139 trace->host = &session->machines.host;
3141 err = perf_session__set_tracepoints_handlers(session, handlers);
3145 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3146 "raw_syscalls:sys_enter");
3147 /* older kernels have syscalls tp versus raw_syscalls */
3149 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3150 "syscalls:sys_enter");
3153 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
3154 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
3155 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3159 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3160 "raw_syscalls:sys_exit");
3162 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3163 "syscalls:sys_exit");
3165 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
3166 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
3167 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
3171 evlist__for_each_entry(session->evlist, evsel) {
3172 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
3173 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3174 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3175 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
3176 evsel->handler = trace__pgfault;
3181 err = perf_session__process_events(session);
3183 pr_err("Failed to process events, error %d", err);
3185 else if (trace->summary)
3186 trace__fprintf_thread_summary(trace, trace->output);
3189 perf_session__delete(session);
3194 static size_t trace__fprintf_threads_header(FILE *fp)
3198 printed = fprintf(fp, "\n Summary of events:\n\n");
3203 DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3204 struct stats *stats;
3209 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3210 struct stats *stats = source->priv;
3212 entry->syscall = source->i;
3213 entry->stats = stats;
3214 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3217 static size_t thread__dump_stats(struct thread_trace *ttrace,
3218 struct trace *trace, FILE *fp)
3223 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
3225 if (syscall_stats == NULL)
3228 printed += fprintf(fp, "\n");
3230 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3231 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3232 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
3234 resort_rb__for_each_entry(nd, syscall_stats) {
3235 struct stats *stats = syscall_stats_entry->stats;
3237 double min = (double)(stats->min) / NSEC_PER_MSEC;
3238 double max = (double)(stats->max) / NSEC_PER_MSEC;
3239 double avg = avg_stats(stats);
3241 u64 n = (u64) stats->n;
3243 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3244 avg /= NSEC_PER_MSEC;
3246 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
3247 printed += fprintf(fp, " %-15s", sc->name);
3248 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
3249 n, syscall_stats_entry->msecs, min, avg);
3250 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
3254 resort_rb__delete(syscall_stats);
3255 printed += fprintf(fp, "\n\n");
3260 static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
3263 struct thread_trace *ttrace = thread__priv(thread);
3269 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3271 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
3272 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
3273 printed += fprintf(fp, "%.1f%%", ratio);
3275 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3277 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
3279 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3280 else if (fputc('\n', fp) != EOF)
3283 printed += thread__dump_stats(ttrace, trace, fp);
3288 static unsigned long thread__nr_events(struct thread_trace *ttrace)
3290 return ttrace ? ttrace->nr_events : 0;
3293 DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3294 struct thread *thread;
3297 entry->thread = rb_entry(nd, struct thread, rb_node);
3300 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3302 size_t printed = trace__fprintf_threads_header(fp);
3306 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3307 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3309 if (threads == NULL) {
3310 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3314 resort_rb__for_each_entry(nd, threads)
3315 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3317 resort_rb__delete(threads);
3322 static int trace__set_duration(const struct option *opt, const char *str,
3323 int unset __maybe_unused)
3325 struct trace *trace = opt->value;
3327 trace->duration_filter = atof(str);
3331 static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3332 int unset __maybe_unused)
3336 struct trace *trace = opt->value;
3338 * FIXME: introduce a intarray class, plain parse csv and create a
3339 * { int nr, int entries[] } struct...
3341 struct intlist *list = intlist__new(str);
3346 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3347 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3349 if (trace->filter_pids.entries == NULL)
3352 trace->filter_pids.entries[0] = getpid();
3354 for (i = 1; i < trace->filter_pids.nr; ++i)
3355 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3357 intlist__delete(list);
3363 static int trace__open_output(struct trace *trace, const char *filename)
3367 if (!stat(filename, &st) && st.st_size) {
3368 char oldname[PATH_MAX];
3370 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3372 rename(filename, oldname);
3375 trace->output = fopen(filename, "w");
3377 return trace->output == NULL ? -errno : 0;
3380 static int parse_pagefaults(const struct option *opt, const char *str,
3381 int unset __maybe_unused)
3383 int *trace_pgfaults = opt->value;
3385 if (strcmp(str, "all") == 0)
3386 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3387 else if (strcmp(str, "maj") == 0)
3388 *trace_pgfaults |= TRACE_PFMAJ;
3389 else if (strcmp(str, "min") == 0)
3390 *trace_pgfaults |= TRACE_PFMIN;
3397 static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3399 struct perf_evsel *evsel;
3401 evlist__for_each_entry(evlist, evsel)
3402 evsel->handler = handler;
3405 static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3407 struct perf_evsel *evsel;
3409 evlist__for_each_entry(evlist, evsel) {
3410 if (evsel->priv || !evsel->tp_format)
3413 if (strcmp(evsel->tp_format->system, "syscalls"))
3416 if (perf_evsel__init_syscall_tp(evsel))
3419 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3420 struct syscall_tp *sc = evsel->priv;
3422 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3424 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3425 struct syscall_tp *sc = evsel->priv;
3427 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3436 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3437 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3438 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3440 * It'd be better to introduce a parse_options() variant that would return a
3441 * list with the terms it didn't match to an event...
3443 static int trace__parse_events_option(const struct option *opt, const char *str,
3444 int unset __maybe_unused)
3446 struct trace *trace = (struct trace *)opt->value;
3447 const char *s = str;
3448 char *sep = NULL, *lists[2] = { NULL, NULL, };
3449 int len = strlen(str) + 1, err = -1, list, idx;
3450 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3451 char group_name[PATH_MAX];
3452 struct syscall_fmt *fmt;
3454 if (strace_groups_dir == NULL)
3459 trace->not_ev_qualifier = true;
3463 if ((sep = strchr(s, ',')) != NULL)
3467 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3468 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
3473 fmt = syscall_fmt__find_by_alias(s);
3478 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3479 if (access(group_name, R_OK) == 0)
3484 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3486 lists[list] = malloc(len);
3487 if (lists[list] == NULL)
3489 strcpy(lists[list], s);
3499 if (lists[1] != NULL) {
3500 struct strlist_config slist_config = {
3501 .dirname = strace_groups_dir,
3504 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3505 if (trace->ev_qualifier == NULL) {
3506 fputs("Not enough memory to parse event qualifier", trace->output);
3510 if (trace__validate_ev_qualifier(trace))
3512 trace->trace_syscalls = true;
3518 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3519 "event selector. use 'perf list' to list available events",
3520 parse_events_option);
3521 err = parse_events_option(&o, lists[0], 0);
3530 static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3532 struct trace *trace = opt->value;
3534 if (!list_empty(&trace->evlist->entries))
3535 return parse_cgroups(opt, str, unset);
3537 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3542 static struct bpf_map *bpf__find_map_by_name(const char *name)
3544 struct bpf_object *obj, *tmp;
3546 bpf_object__for_each_safe(obj, tmp) {
3547 struct bpf_map *map = bpf_object__find_map_by_name(obj, name);
3556 static void trace__set_bpf_map_filtered_pids(struct trace *trace)
3558 trace->filter_pids.map = bpf__find_map_by_name("pids_filtered");
3561 static void trace__set_bpf_map_syscalls(struct trace *trace)
3563 trace->syscalls.map = bpf__find_map_by_name("syscalls");
3566 static int trace__config(const char *var, const char *value, void *arg)
3568 struct trace *trace = arg;
3571 if (!strcmp(var, "trace.add_events")) {
3572 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3573 "event selector. use 'perf list' to list available events",
3574 parse_events_option);
3575 err = parse_events_option(&o, value, 0);
3576 } else if (!strcmp(var, "trace.show_timestamp")) {
3577 trace->show_tstamp = perf_config_bool(var, value);
3578 } else if (!strcmp(var, "trace.show_duration")) {
3579 trace->show_duration = perf_config_bool(var, value);
3580 } else if (!strcmp(var, "trace.show_arg_names")) {
3581 trace->show_arg_names = perf_config_bool(var, value);
3582 if (!trace->show_arg_names)
3583 trace->show_zeros = true;
3584 } else if (!strcmp(var, "trace.show_zeros")) {
3585 bool new_show_zeros = perf_config_bool(var, value);
3586 if (!trace->show_arg_names && !new_show_zeros) {
3587 pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
3590 trace->show_zeros = new_show_zeros;
3591 } else if (!strcmp(var, "trace.show_prefix")) {
3592 trace->show_string_prefix = perf_config_bool(var, value);
3593 } else if (!strcmp(var, "trace.no_inherit")) {
3594 trace->opts.no_inherit = perf_config_bool(var, value);
3595 } else if (!strcmp(var, "trace.args_alignment")) {
3596 int args_alignment = 0;
3597 if (perf_config_int(&args_alignment, var, value) == 0)
3598 trace->args_alignment = args_alignment;
3604 int cmd_trace(int argc, const char **argv)
3606 const char *trace_usage[] = {
3607 "perf trace [<options>] [<command>]",
3608 "perf trace [<options>] -- <command> [<options>]",
3609 "perf trace record [<options>] [<command>]",
3610 "perf trace record [<options>] -- <command> [<options>]",
3613 struct trace trace = {
3622 .user_freq = UINT_MAX,
3623 .user_interval = ULLONG_MAX,
3624 .no_buffering = true,
3625 .mmap_pages = UINT_MAX,
3629 .show_tstamp = true,
3630 .show_duration = true,
3631 .show_arg_names = true,
3632 .args_alignment = 70,
3633 .trace_syscalls = false,
3634 .kernel_syscallchains = false,
3635 .max_stack = UINT_MAX,
3636 .max_events = ULONG_MAX,
3638 const char *output_name = NULL;
3639 const struct option trace_options[] = {
3640 OPT_CALLBACK('e', "event", &trace, "event",
3641 "event/syscall selector. use 'perf list' to list available events",
3642 trace__parse_events_option),
3643 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3644 "show the thread COMM next to its id"),
3645 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
3646 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3647 trace__parse_events_option),
3648 OPT_STRING('o', "output", &output_name, "file", "output file name"),
3649 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
3650 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3651 "trace events on existing process id"),
3652 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
3653 "trace events on existing thread id"),
3654 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
3655 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
3656 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
3657 "system-wide collection from all CPUs"),
3658 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
3659 "list of cpus to monitor"),
3660 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
3661 "child tasks do not inherit counters"),
3662 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3663 "number of mmap data pages",
3664 perf_evlist__parse_mmap_pages),
3665 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
3667 OPT_CALLBACK(0, "duration", &trace, "float",
3668 "show only events with duration > N.M ms",
3669 trace__set_duration),
3670 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
3671 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
3672 OPT_BOOLEAN('T', "time", &trace.full_time,
3673 "Show full timestamp, not time relative to first start"),
3674 OPT_BOOLEAN(0, "failure", &trace.failure_only,
3675 "Show only syscalls that failed"),
3676 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3677 "Show only syscall summary with statistics"),
3678 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3679 "Show all syscalls and summary with statistics"),
3680 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3681 "Trace pagefaults", parse_pagefaults, "maj"),
3682 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
3683 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
3684 OPT_CALLBACK(0, "call-graph", &trace.opts,
3685 "record_mode[,record_size]", record_callchain_help,
3686 &record_parse_callchain_opt),
3687 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3688 "Show the kernel callchains on the syscall exit path"),
3689 OPT_ULONG(0, "max-events", &trace.max_events,
3690 "Set the maximum number of events to print, exit after that is reached. "),
3691 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3692 "Set the minimum stack depth when parsing the callchain, "
3693 "anything below the specified depth will be ignored."),
3694 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3695 "Set the maximum stack depth when parsing the callchain, "
3696 "anything beyond the specified depth will be ignored. "
3697 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3698 OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
3699 "Sort batch of events before processing, use if getting out of order events"),
3700 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3701 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
3702 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
3703 "per thread proc mmap processing timeout in ms"),
3704 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3705 trace__parse_cgroups),
3706 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3707 "ms to wait before starting measurement after program "
3711 bool __maybe_unused max_stack_user_set = true;
3712 bool mmap_pages_user_set = true;
3713 struct perf_evsel *evsel;
3714 const char * const trace_subcommands[] = { "record", NULL };
3718 signal(SIGSEGV, sighandler_dump_stack);
3719 signal(SIGFPE, sighandler_dump_stack);
3721 trace.evlist = perf_evlist__new();
3722 trace.sctbl = syscalltbl__new();
3724 if (trace.evlist == NULL || trace.sctbl == NULL) {
3725 pr_err("Not enough memory to run!\n");
3730 err = perf_config(trace__config, &trace);
3734 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3735 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
3737 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3738 usage_with_options_msg(trace_usage, trace_options,
3739 "cgroup monitoring only available in system-wide mode");
3742 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3743 if (IS_ERR(evsel)) {
3744 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
3745 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3750 trace.syscalls.events.augmented = evsel;
3751 trace__set_bpf_map_filtered_pids(&trace);
3752 trace__set_bpf_map_syscalls(&trace);
3755 err = bpf__setup_stdout(trace.evlist);
3757 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3758 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3764 if (trace.trace_pgfaults) {
3765 trace.opts.sample_address = true;
3766 trace.opts.sample_time = true;
3769 if (trace.opts.mmap_pages == UINT_MAX)
3770 mmap_pages_user_set = false;
3772 if (trace.max_stack == UINT_MAX) {
3773 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
3774 max_stack_user_set = false;
3777 #ifdef HAVE_DWARF_UNWIND_SUPPORT
3778 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
3779 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
3783 if (callchain_param.enabled) {
3784 if (!mmap_pages_user_set && geteuid() == 0)
3785 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3787 symbol_conf.use_callchain = true;
3790 if (trace.evlist->nr_entries > 0) {
3791 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
3792 if (evlist__set_syscall_tp_fields(trace.evlist)) {
3793 perror("failed to set syscalls:* tracepoint fields");
3798 if (trace.sort_events) {
3799 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
3800 ordered_events__set_copy_on_queue(&trace.oe.data, true);
3804 * If we are augmenting syscalls, then combine what we put in the
3805 * __augmented_syscalls__ BPF map with what is in the
3806 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3807 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3809 * We'll switch to look at two BPF maps, one for sys_enter and the
3810 * other for sys_exit when we start augmenting the sys_exit paths with
3811 * buffers that are being copied from kernel to userspace, think 'read'
3814 if (trace.syscalls.events.augmented) {
3815 evsel = trace.syscalls.events.augmented;
3817 if (perf_evsel__init_augmented_syscall_tp(evsel) ||
3818 perf_evsel__init_augmented_syscall_tp_args(evsel))
3820 evsel->handler = trace__sys_enter;
3822 evlist__for_each_entry(trace.evlist, evsel) {
3823 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
3825 if (raw_syscalls_sys_exit) {
3826 trace.raw_augmented_syscalls = true;
3827 goto init_augmented_syscall_tp;
3830 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
3831 init_augmented_syscall_tp:
3832 perf_evsel__init_augmented_syscall_tp(evsel);
3833 perf_evsel__init_augmented_syscall_tp_ret(evsel);
3834 evsel->handler = trace__sys_exit;
3839 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3840 return trace__record(&trace, argc-1, &argv[1]);
3842 /* summary_only implies summary option, but don't overwrite summary if set */
3843 if (trace.summary_only)
3844 trace.summary = trace.summary_only;
3846 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3847 trace.evlist->nr_entries == 0 /* Was --events used? */) {
3848 trace.trace_syscalls = true;
3851 if (output_name != NULL) {
3852 err = trace__open_output(&trace, output_name);
3854 perror("failed to create output file");
3859 err = target__validate(&trace.opts.target);
3861 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
3862 fprintf(trace.output, "%s", bf);
3866 err = target__parse_uid(&trace.opts.target);
3868 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
3869 fprintf(trace.output, "%s", bf);
3873 if (!argc && target__none(&trace.opts.target))
3874 trace.opts.target.system_wide = true;
3877 err = trace__replay(&trace);
3879 err = trace__run(&trace, argc, argv);
3882 if (output_name != NULL)
3883 fclose(trace.output);