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>
22 #include "util/color.h"
23 #include "util/debug.h"
24 #include "util/evlist.h"
25 #include <subcmd/exec-cmd.h>
26 #include "util/machine.h"
27 #include "util/session.h"
28 #include "util/thread.h"
29 #include <subcmd/parse-options.h>
30 #include "util/strlist.h"
31 #include "util/intlist.h"
32 #include "util/thread_map.h"
33 #include "util/stat.h"
34 #include "trace-event.h"
35 #include "util/parse-events.h"
36 #include "util/bpf-loader.h"
41 #include <linux/futex.h>
42 #include <linux/err.h>
44 /* For older distros: */
46 # define MAP_STACK 0x20000
50 # define MADV_HWPOISON 100
54 #ifndef MADV_MERGEABLE
55 # define MADV_MERGEABLE 12
58 #ifndef MADV_UNMERGEABLE
59 # define MADV_UNMERGEABLE 13
63 # define EFD_SEMAPHORE 1
67 # define EFD_NONBLOCK 00004000
71 # define EFD_CLOEXEC 02000000
75 # define O_CLOEXEC 02000000
83 # define SOCK_CLOEXEC 02000000
87 # define SOCK_NONBLOCK 00004000
90 #ifndef MSG_CMSG_CLOEXEC
91 # define MSG_CMSG_CLOEXEC 0x40000000
94 #ifndef PERF_FLAG_FD_NO_GROUP
95 # define PERF_FLAG_FD_NO_GROUP (1UL << 0)
98 #ifndef PERF_FLAG_FD_OUTPUT
99 # define PERF_FLAG_FD_OUTPUT (1UL << 1)
102 #ifndef PERF_FLAG_PID_CGROUP
103 # define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */
106 #ifndef PERF_FLAG_FD_CLOEXEC
107 # define PERF_FLAG_FD_CLOEXEC (1UL << 3) /* O_CLOEXEC */
114 u64 (*integer
)(struct tp_field
*field
, struct perf_sample
*sample
);
115 void *(*pointer
)(struct tp_field
*field
, struct perf_sample
*sample
);
119 #define TP_UINT_FIELD(bits) \
120 static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
123 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
132 #define TP_UINT_FIELD__SWAPPED(bits) \
133 static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
136 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
137 return bswap_##bits(value);\
140 TP_UINT_FIELD__SWAPPED(16);
141 TP_UINT_FIELD__SWAPPED(32);
142 TP_UINT_FIELD__SWAPPED(64);
144 static int tp_field__init_uint(struct tp_field
*field
,
145 struct format_field
*format_field
,
148 field
->offset
= format_field
->offset
;
150 switch (format_field
->size
) {
152 field
->integer
= tp_field__u8
;
155 field
->integer
= needs_swap
? tp_field__swapped_u16
: tp_field__u16
;
158 field
->integer
= needs_swap
? tp_field__swapped_u32
: tp_field__u32
;
161 field
->integer
= needs_swap
? tp_field__swapped_u64
: tp_field__u64
;
170 static void *tp_field__ptr(struct tp_field
*field
, struct perf_sample
*sample
)
172 return sample
->raw_data
+ field
->offset
;
175 static int tp_field__init_ptr(struct tp_field
*field
, struct format_field
*format_field
)
177 field
->offset
= format_field
->offset
;
178 field
->pointer
= tp_field__ptr
;
185 struct tp_field args
, ret
;
189 static int perf_evsel__init_tp_uint_field(struct perf_evsel
*evsel
,
190 struct tp_field
*field
,
193 struct format_field
*format_field
= perf_evsel__field(evsel
, name
);
195 if (format_field
== NULL
)
198 return tp_field__init_uint(field
, format_field
, evsel
->needs_swap
);
201 #define perf_evsel__init_sc_tp_uint_field(evsel, name) \
202 ({ struct syscall_tp *sc = evsel->priv;\
203 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
205 static int perf_evsel__init_tp_ptr_field(struct perf_evsel
*evsel
,
206 struct tp_field
*field
,
209 struct format_field
*format_field
= perf_evsel__field(evsel
, name
);
211 if (format_field
== NULL
)
214 return tp_field__init_ptr(field
, format_field
);
217 #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
218 ({ struct syscall_tp *sc = evsel->priv;\
219 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
221 static void perf_evsel__delete_priv(struct perf_evsel
*evsel
)
224 perf_evsel__delete(evsel
);
227 static int perf_evsel__init_syscall_tp(struct perf_evsel
*evsel
, void *handler
)
229 evsel
->priv
= malloc(sizeof(struct syscall_tp
));
230 if (evsel
->priv
!= NULL
) {
231 if (perf_evsel__init_sc_tp_uint_field(evsel
, id
))
234 evsel
->handler
= handler
;
245 static struct perf_evsel
*perf_evsel__syscall_newtp(const char *direction
, void *handler
)
247 struct perf_evsel
*evsel
= perf_evsel__newtp("raw_syscalls", direction
);
249 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
251 evsel
= perf_evsel__newtp("syscalls", direction
);
256 if (perf_evsel__init_syscall_tp(evsel
, handler
))
262 perf_evsel__delete_priv(evsel
);
266 #define perf_evsel__sc_tp_uint(evsel, name, sample) \
267 ({ struct syscall_tp *fields = evsel->priv; \
268 fields->name.integer(&fields->name, sample); })
270 #define perf_evsel__sc_tp_ptr(evsel, name, sample) \
271 ({ struct syscall_tp *fields = evsel->priv; \
272 fields->name.pointer(&fields->name, sample); })
276 struct thread
*thread
;
286 const char **entries
;
289 #define DEFINE_STRARRAY(array) struct strarray strarray__##array = { \
290 .nr_entries = ARRAY_SIZE(array), \
294 #define DEFINE_STRARRAY_OFFSET(array, off) struct strarray strarray__##array = { \
296 .nr_entries = ARRAY_SIZE(array), \
300 static size_t __syscall_arg__scnprintf_strarray(char *bf
, size_t size
,
302 struct syscall_arg
*arg
)
304 struct strarray
*sa
= arg
->parm
;
305 int idx
= arg
->val
- sa
->offset
;
307 if (idx
< 0 || idx
>= sa
->nr_entries
)
308 return scnprintf(bf
, size
, intfmt
, arg
->val
);
310 return scnprintf(bf
, size
, "%s", sa
->entries
[idx
]);
313 static size_t syscall_arg__scnprintf_strarray(char *bf
, size_t size
,
314 struct syscall_arg
*arg
)
316 return __syscall_arg__scnprintf_strarray(bf
, size
, "%d", arg
);
319 #define SCA_STRARRAY syscall_arg__scnprintf_strarray
321 #if defined(__i386__) || defined(__x86_64__)
323 * FIXME: Make this available to all arches as soon as the ioctl beautifier
324 * gets rewritten to support all arches.
326 static size_t syscall_arg__scnprintf_strhexarray(char *bf
, size_t size
,
327 struct syscall_arg
*arg
)
329 return __syscall_arg__scnprintf_strarray(bf
, size
, "%#x", arg
);
332 #define SCA_STRHEXARRAY syscall_arg__scnprintf_strhexarray
333 #endif /* defined(__i386__) || defined(__x86_64__) */
335 static size_t syscall_arg__scnprintf_fd(char *bf
, size_t size
,
336 struct syscall_arg
*arg
);
338 #define SCA_FD syscall_arg__scnprintf_fd
340 static size_t syscall_arg__scnprintf_fd_at(char *bf
, size_t size
,
341 struct syscall_arg
*arg
)
346 return scnprintf(bf
, size
, "CWD");
348 return syscall_arg__scnprintf_fd(bf
, size
, arg
);
351 #define SCA_FDAT syscall_arg__scnprintf_fd_at
353 static size_t syscall_arg__scnprintf_close_fd(char *bf
, size_t size
,
354 struct syscall_arg
*arg
);
356 #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
358 static size_t syscall_arg__scnprintf_hex(char *bf
, size_t size
,
359 struct syscall_arg
*arg
)
361 return scnprintf(bf
, size
, "%#lx", arg
->val
);
364 #define SCA_HEX syscall_arg__scnprintf_hex
366 static size_t syscall_arg__scnprintf_int(char *bf
, size_t size
,
367 struct syscall_arg
*arg
)
369 return scnprintf(bf
, size
, "%d", arg
->val
);
372 #define SCA_INT syscall_arg__scnprintf_int
374 static size_t syscall_arg__scnprintf_mmap_prot(char *bf
, size_t size
,
375 struct syscall_arg
*arg
)
377 int printed
= 0, prot
= arg
->val
;
379 if (prot
== PROT_NONE
)
380 return scnprintf(bf
, size
, "NONE");
381 #define P_MMAP_PROT(n) \
382 if (prot & PROT_##n) { \
383 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
393 P_MMAP_PROT(GROWSDOWN
);
394 P_MMAP_PROT(GROWSUP
);
398 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", prot
);
403 #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
405 static size_t syscall_arg__scnprintf_mmap_flags(char *bf
, size_t size
,
406 struct syscall_arg
*arg
)
408 int printed
= 0, flags
= arg
->val
;
410 #define P_MMAP_FLAG(n) \
411 if (flags & MAP_##n) { \
412 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
417 P_MMAP_FLAG(PRIVATE
);
421 P_MMAP_FLAG(ANONYMOUS
);
422 P_MMAP_FLAG(DENYWRITE
);
423 P_MMAP_FLAG(EXECUTABLE
);
426 P_MMAP_FLAG(GROWSDOWN
);
428 P_MMAP_FLAG(HUGETLB
);
431 P_MMAP_FLAG(NONBLOCK
);
432 P_MMAP_FLAG(NORESERVE
);
433 P_MMAP_FLAG(POPULATE
);
435 #ifdef MAP_UNINITIALIZED
436 P_MMAP_FLAG(UNINITIALIZED
);
441 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", flags
);
446 #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
448 static size_t syscall_arg__scnprintf_mremap_flags(char *bf
, size_t size
,
449 struct syscall_arg
*arg
)
451 int printed
= 0, flags
= arg
->val
;
453 #define P_MREMAP_FLAG(n) \
454 if (flags & MREMAP_##n) { \
455 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
456 flags &= ~MREMAP_##n; \
459 P_MREMAP_FLAG(MAYMOVE
);
461 P_MREMAP_FLAG(FIXED
);
466 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", flags
);
471 #define SCA_MREMAP_FLAGS syscall_arg__scnprintf_mremap_flags
473 static size_t syscall_arg__scnprintf_madvise_behavior(char *bf
, size_t size
,
474 struct syscall_arg
*arg
)
476 int behavior
= arg
->val
;
479 #define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
482 P_MADV_BHV(SEQUENTIAL
);
483 P_MADV_BHV(WILLNEED
);
484 P_MADV_BHV(DONTNEED
);
486 P_MADV_BHV(DONTFORK
);
488 P_MADV_BHV(HWPOISON
);
489 #ifdef MADV_SOFT_OFFLINE
490 P_MADV_BHV(SOFT_OFFLINE
);
492 P_MADV_BHV(MERGEABLE
);
493 P_MADV_BHV(UNMERGEABLE
);
495 P_MADV_BHV(HUGEPAGE
);
497 #ifdef MADV_NOHUGEPAGE
498 P_MADV_BHV(NOHUGEPAGE
);
501 P_MADV_BHV(DONTDUMP
);
510 return scnprintf(bf
, size
, "%#x", behavior
);
513 #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
515 static size_t syscall_arg__scnprintf_flock(char *bf
, size_t size
,
516 struct syscall_arg
*arg
)
518 int printed
= 0, op
= arg
->val
;
521 return scnprintf(bf
, size
, "NONE");
523 if ((op & LOCK_##cmd) == LOCK_##cmd) { \
524 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #cmd); \
539 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", op
);
544 #define SCA_FLOCK syscall_arg__scnprintf_flock
546 static size_t syscall_arg__scnprintf_futex_op(char *bf
, size_t size
, struct syscall_arg
*arg
)
548 enum syscall_futex_args
{
549 SCF_UADDR
= (1 << 0),
552 SCF_TIMEOUT
= (1 << 3),
553 SCF_UADDR2
= (1 << 4),
557 int cmd
= op
& FUTEX_CMD_MASK
;
561 #define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
562 P_FUTEX_OP(WAIT
); arg
->mask
|= SCF_VAL3
|SCF_UADDR2
; break;
563 P_FUTEX_OP(WAKE
); arg
->mask
|= SCF_VAL3
|SCF_UADDR2
|SCF_TIMEOUT
; break;
564 P_FUTEX_OP(FD
); arg
->mask
|= SCF_VAL3
|SCF_UADDR2
|SCF_TIMEOUT
; break;
565 P_FUTEX_OP(REQUEUE
); arg
->mask
|= SCF_VAL3
|SCF_TIMEOUT
; break;
566 P_FUTEX_OP(CMP_REQUEUE
); arg
->mask
|= SCF_TIMEOUT
; break;
567 P_FUTEX_OP(CMP_REQUEUE_PI
); arg
->mask
|= SCF_TIMEOUT
; break;
568 P_FUTEX_OP(WAKE_OP
); break;
569 P_FUTEX_OP(LOCK_PI
); arg
->mask
|= SCF_VAL3
|SCF_UADDR2
|SCF_TIMEOUT
; break;
570 P_FUTEX_OP(UNLOCK_PI
); arg
->mask
|= SCF_VAL3
|SCF_UADDR2
|SCF_TIMEOUT
; break;
571 P_FUTEX_OP(TRYLOCK_PI
); arg
->mask
|= SCF_VAL3
|SCF_UADDR2
; break;
572 P_FUTEX_OP(WAIT_BITSET
); arg
->mask
|= SCF_UADDR2
; break;
573 P_FUTEX_OP(WAKE_BITSET
); arg
->mask
|= SCF_UADDR2
; break;
574 P_FUTEX_OP(WAIT_REQUEUE_PI
); break;
575 default: printed
= scnprintf(bf
, size
, "%#x", cmd
); break;
578 if (op
& FUTEX_PRIVATE_FLAG
)
579 printed
+= scnprintf(bf
+ printed
, size
- printed
, "|PRIV");
581 if (op
& FUTEX_CLOCK_REALTIME
)
582 printed
+= scnprintf(bf
+ printed
, size
- printed
, "|CLKRT");
587 #define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
589 static const char *bpf_cmd
[] = {
590 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
591 "MAP_GET_NEXT_KEY", "PROG_LOAD",
593 static DEFINE_STRARRAY(bpf_cmd
);
595 static const char *epoll_ctl_ops
[] = { "ADD", "DEL", "MOD", };
596 static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops
, 1);
598 static const char *itimers
[] = { "REAL", "VIRTUAL", "PROF", };
599 static DEFINE_STRARRAY(itimers
);
601 static const char *keyctl_options
[] = {
602 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
603 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
604 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
605 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
606 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
608 static DEFINE_STRARRAY(keyctl_options
);
610 static const char *whences
[] = { "SET", "CUR", "END",
618 static DEFINE_STRARRAY(whences
);
620 static const char *fcntl_cmds
[] = {
621 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
622 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "F_GETLK64",
623 "F_SETLK64", "F_SETLKW64", "F_SETOWN_EX", "F_GETOWN_EX",
626 static DEFINE_STRARRAY(fcntl_cmds
);
628 static const char *rlimit_resources
[] = {
629 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
630 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
633 static DEFINE_STRARRAY(rlimit_resources
);
635 static const char *sighow
[] = { "BLOCK", "UNBLOCK", "SETMASK", };
636 static DEFINE_STRARRAY(sighow
);
638 static const char *clockid
[] = {
639 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
640 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
641 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
643 static DEFINE_STRARRAY(clockid
);
645 static const char *socket_families
[] = {
646 "UNSPEC", "LOCAL", "INET", "AX25", "IPX", "APPLETALK", "NETROM",
647 "BRIDGE", "ATMPVC", "X25", "INET6", "ROSE", "DECnet", "NETBEUI",
648 "SECURITY", "KEY", "NETLINK", "PACKET", "ASH", "ECONET", "ATMSVC",
649 "RDS", "SNA", "IRDA", "PPPOX", "WANPIPE", "LLC", "IB", "CAN", "TIPC",
650 "BLUETOOTH", "IUCV", "RXRPC", "ISDN", "PHONET", "IEEE802154", "CAIF",
651 "ALG", "NFC", "VSOCK",
653 static DEFINE_STRARRAY(socket_families
);
655 #ifndef SOCK_TYPE_MASK
656 #define SOCK_TYPE_MASK 0xf
659 static size_t syscall_arg__scnprintf_socket_type(char *bf
, size_t size
,
660 struct syscall_arg
*arg
)
664 flags
= type
& ~SOCK_TYPE_MASK
;
666 type
&= SOCK_TYPE_MASK
;
668 * Can't use a strarray, MIPS may override for ABI reasons.
671 #define P_SK_TYPE(n) case SOCK_##n: printed = scnprintf(bf, size, #n); break;
676 P_SK_TYPE(SEQPACKET
);
681 printed
= scnprintf(bf
, size
, "%#x", type
);
684 #define P_SK_FLAG(n) \
685 if (flags & SOCK_##n) { \
686 printed += scnprintf(bf + printed, size - printed, "|%s", #n); \
687 flags &= ~SOCK_##n; \
695 printed
+= scnprintf(bf
+ printed
, size
- printed
, "|%#x", flags
);
700 #define SCA_SK_TYPE syscall_arg__scnprintf_socket_type
703 #define MSG_PROBE 0x10
705 #ifndef MSG_WAITFORONE
706 #define MSG_WAITFORONE 0x10000
708 #ifndef MSG_SENDPAGE_NOTLAST
709 #define MSG_SENDPAGE_NOTLAST 0x20000
712 #define MSG_FASTOPEN 0x20000000
715 static size_t syscall_arg__scnprintf_msg_flags(char *bf
, size_t size
,
716 struct syscall_arg
*arg
)
718 int printed
= 0, flags
= arg
->val
;
721 return scnprintf(bf
, size
, "NONE");
722 #define P_MSG_FLAG(n) \
723 if (flags & MSG_##n) { \
724 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
730 P_MSG_FLAG(DONTROUTE
);
735 P_MSG_FLAG(DONTWAIT
);
742 P_MSG_FLAG(ERRQUEUE
);
743 P_MSG_FLAG(NOSIGNAL
);
745 P_MSG_FLAG(WAITFORONE
);
746 P_MSG_FLAG(SENDPAGE_NOTLAST
);
747 P_MSG_FLAG(FASTOPEN
);
748 P_MSG_FLAG(CMSG_CLOEXEC
);
752 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", flags
);
757 #define SCA_MSG_FLAGS syscall_arg__scnprintf_msg_flags
759 static size_t syscall_arg__scnprintf_access_mode(char *bf
, size_t size
,
760 struct syscall_arg
*arg
)
765 if (mode
== F_OK
) /* 0 */
766 return scnprintf(bf
, size
, "F");
768 if (mode & n##_OK) { \
769 printed += scnprintf(bf + printed, size - printed, "%s", #n); \
779 printed
+= scnprintf(bf
+ printed
, size
- printed
, "|%#x", mode
);
784 #define SCA_ACCMODE syscall_arg__scnprintf_access_mode
786 static size_t syscall_arg__scnprintf_filename(char *bf
, size_t size
,
787 struct syscall_arg
*arg
);
789 #define SCA_FILENAME syscall_arg__scnprintf_filename
791 static size_t syscall_arg__scnprintf_open_flags(char *bf
, size_t size
,
792 struct syscall_arg
*arg
)
794 int printed
= 0, flags
= arg
->val
;
796 if (!(flags
& O_CREAT
))
797 arg
->mask
|= 1 << (arg
->idx
+ 1); /* Mask the mode parm */
800 return scnprintf(bf
, size
, "RDONLY");
802 if (flags & O_##n) { \
803 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
827 if ((flags
& O_SYNC
) == O_SYNC
)
828 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%s", printed
? "|" : "", "SYNC");
840 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", flags
);
845 #define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags
847 static size_t syscall_arg__scnprintf_perf_flags(char *bf
, size_t size
,
848 struct syscall_arg
*arg
)
850 int printed
= 0, flags
= arg
->val
;
856 if (flags & PERF_FLAG_##n) { \
857 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
858 flags &= ~PERF_FLAG_##n; \
868 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", flags
);
873 #define SCA_PERF_FLAGS syscall_arg__scnprintf_perf_flags
875 static size_t syscall_arg__scnprintf_eventfd_flags(char *bf
, size_t size
,
876 struct syscall_arg
*arg
)
878 int printed
= 0, flags
= arg
->val
;
881 return scnprintf(bf
, size
, "NONE");
883 if (flags & EFD_##n) { \
884 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
894 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", flags
);
899 #define SCA_EFD_FLAGS syscall_arg__scnprintf_eventfd_flags
901 static size_t syscall_arg__scnprintf_pipe_flags(char *bf
, size_t size
,
902 struct syscall_arg
*arg
)
904 int printed
= 0, flags
= arg
->val
;
907 if (flags & O_##n) { \
908 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
917 printed
+= scnprintf(bf
+ printed
, size
- printed
, "%s%#x", printed
? "|" : "", flags
);
922 #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
924 static size_t syscall_arg__scnprintf_signum(char *bf
, size_t size
, struct syscall_arg
*arg
)
929 #define P_SIGNUM(n) case SIG##n: return scnprintf(bf, size, #n)
972 return scnprintf(bf
, size
, "%#x", sig
);
975 #define SCA_SIGNUM syscall_arg__scnprintf_signum
977 #if defined(__i386__) || defined(__x86_64__)
979 * FIXME: Make this available to all arches.
981 #define TCGETS 0x5401
983 static const char *tioctls
[] = {
984 "TCGETS", "TCSETS", "TCSETSW", "TCSETSF", "TCGETA", "TCSETA", "TCSETAW",
985 "TCSETAF", "TCSBRK", "TCXONC", "TCFLSH", "TIOCEXCL", "TIOCNXCL",
986 "TIOCSCTTY", "TIOCGPGRP", "TIOCSPGRP", "TIOCOUTQ", "TIOCSTI",
987 "TIOCGWINSZ", "TIOCSWINSZ", "TIOCMGET", "TIOCMBIS", "TIOCMBIC",
988 "TIOCMSET", "TIOCGSOFTCAR", "TIOCSSOFTCAR", "FIONREAD", "TIOCLINUX",
989 "TIOCCONS", "TIOCGSERIAL", "TIOCSSERIAL", "TIOCPKT", "FIONBIO",
990 "TIOCNOTTY", "TIOCSETD", "TIOCGETD", "TCSBRKP", [0x27] = "TIOCSBRK",
991 "TIOCCBRK", "TIOCGSID", "TCGETS2", "TCSETS2", "TCSETSW2", "TCSETSF2",
992 "TIOCGRS485", "TIOCSRS485", "TIOCGPTN", "TIOCSPTLCK",
993 "TIOCGDEV||TCGETX", "TCSETX", "TCSETXF", "TCSETXW", "TIOCSIG",
994 "TIOCVHANGUP", "TIOCGPKT", "TIOCGPTLCK", "TIOCGEXCL",
995 [0x50] = "FIONCLEX", "FIOCLEX", "FIOASYNC", "TIOCSERCONFIG",
996 "TIOCSERGWILD", "TIOCSERSWILD", "TIOCGLCKTRMIOS", "TIOCSLCKTRMIOS",
997 "TIOCSERGSTRUCT", "TIOCSERGETLSR", "TIOCSERGETMULTI", "TIOCSERSETMULTI",
998 "TIOCMIWAIT", "TIOCGICOUNT", [0x60] = "FIOQSIZE",
1001 static DEFINE_STRARRAY_OFFSET(tioctls
, 0x5401);
1002 #endif /* defined(__i386__) || defined(__x86_64__) */
1004 #define STRARRAY(arg, name, array) \
1005 .arg_scnprintf = { [arg] = SCA_STRARRAY, }, \
1006 .arg_parm = { [arg] = &strarray__##array, }
1008 static struct syscall_fmt
{
1011 size_t (*arg_scnprintf
[6])(char *bf
, size_t size
, struct syscall_arg
*arg
);
1016 } syscall_fmts
[] = {
1017 { .name
= "access", .errmsg
= true,
1018 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */
1019 [1] = SCA_ACCMODE
, /* mode */ }, },
1020 { .name
= "arch_prctl", .errmsg
= true, .alias
= "prctl", },
1021 { .name
= "bpf", .errmsg
= true, STRARRAY(0, cmd
, bpf_cmd
), },
1022 { .name
= "brk", .hexret
= true,
1023 .arg_scnprintf
= { [0] = SCA_HEX
, /* brk */ }, },
1024 { .name
= "chdir", .errmsg
= true,
1025 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */ }, },
1026 { .name
= "chmod", .errmsg
= true,
1027 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */ }, },
1028 { .name
= "chroot", .errmsg
= true,
1029 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */ }, },
1030 { .name
= "clock_gettime", .errmsg
= true, STRARRAY(0, clk_id
, clockid
), },
1031 { .name
= "close", .errmsg
= true,
1032 .arg_scnprintf
= { [0] = SCA_CLOSE_FD
, /* fd */ }, },
1033 { .name
= "connect", .errmsg
= true, },
1034 { .name
= "creat", .errmsg
= true,
1035 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1036 { .name
= "dup", .errmsg
= true,
1037 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1038 { .name
= "dup2", .errmsg
= true,
1039 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1040 { .name
= "dup3", .errmsg
= true,
1041 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1042 { .name
= "epoll_ctl", .errmsg
= true, STRARRAY(1, op
, epoll_ctl_ops
), },
1043 { .name
= "eventfd2", .errmsg
= true,
1044 .arg_scnprintf
= { [1] = SCA_EFD_FLAGS
, /* flags */ }, },
1045 { .name
= "faccessat", .errmsg
= true,
1046 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */
1047 [1] = SCA_FILENAME
, /* filename */ }, },
1048 { .name
= "fadvise64", .errmsg
= true,
1049 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1050 { .name
= "fallocate", .errmsg
= true,
1051 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1052 { .name
= "fchdir", .errmsg
= true,
1053 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1054 { .name
= "fchmod", .errmsg
= true,
1055 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1056 { .name
= "fchmodat", .errmsg
= true,
1057 .arg_scnprintf
= { [0] = SCA_FDAT
, /* fd */
1058 [1] = SCA_FILENAME
, /* filename */ }, },
1059 { .name
= "fchown", .errmsg
= true,
1060 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1061 { .name
= "fchownat", .errmsg
= true,
1062 .arg_scnprintf
= { [0] = SCA_FDAT
, /* fd */
1063 [1] = SCA_FILENAME
, /* filename */ }, },
1064 { .name
= "fcntl", .errmsg
= true,
1065 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1066 [1] = SCA_STRARRAY
, /* cmd */ },
1067 .arg_parm
= { [1] = &strarray__fcntl_cmds
, /* cmd */ }, },
1068 { .name
= "fdatasync", .errmsg
= true,
1069 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1070 { .name
= "flock", .errmsg
= true,
1071 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1072 [1] = SCA_FLOCK
, /* cmd */ }, },
1073 { .name
= "fsetxattr", .errmsg
= true,
1074 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1075 { .name
= "fstat", .errmsg
= true, .alias
= "newfstat",
1076 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1077 { .name
= "fstatat", .errmsg
= true, .alias
= "newfstatat",
1078 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */
1079 [1] = SCA_FILENAME
, /* filename */ }, },
1080 { .name
= "fstatfs", .errmsg
= true,
1081 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1082 { .name
= "fsync", .errmsg
= true,
1083 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1084 { .name
= "ftruncate", .errmsg
= true,
1085 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1086 { .name
= "futex", .errmsg
= true,
1087 .arg_scnprintf
= { [1] = SCA_FUTEX_OP
, /* op */ }, },
1088 { .name
= "futimesat", .errmsg
= true,
1089 .arg_scnprintf
= { [0] = SCA_FDAT
, /* fd */
1090 [1] = SCA_FILENAME
, /* filename */ }, },
1091 { .name
= "getdents", .errmsg
= true,
1092 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1093 { .name
= "getdents64", .errmsg
= true,
1094 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1095 { .name
= "getitimer", .errmsg
= true, STRARRAY(0, which
, itimers
), },
1096 { .name
= "getrlimit", .errmsg
= true, STRARRAY(0, resource
, rlimit_resources
), },
1097 { .name
= "getxattr", .errmsg
= true,
1098 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1099 { .name
= "inotify_add_watch", .errmsg
= true,
1100 .arg_scnprintf
= { [1] = SCA_FILENAME
, /* pathname */ }, },
1101 { .name
= "ioctl", .errmsg
= true,
1102 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1103 #if defined(__i386__) || defined(__x86_64__)
1105 * FIXME: Make this available to all arches.
1107 [1] = SCA_STRHEXARRAY
, /* cmd */
1108 [2] = SCA_HEX
, /* arg */ },
1109 .arg_parm
= { [1] = &strarray__tioctls
, /* cmd */ }, },
1111 [2] = SCA_HEX
, /* arg */ }, },
1113 { .name
= "keyctl", .errmsg
= true, STRARRAY(0, option
, keyctl_options
), },
1114 { .name
= "kill", .errmsg
= true,
1115 .arg_scnprintf
= { [1] = SCA_SIGNUM
, /* sig */ }, },
1116 { .name
= "lchown", .errmsg
= true,
1117 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */ }, },
1118 { .name
= "lgetxattr", .errmsg
= true,
1119 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1120 { .name
= "linkat", .errmsg
= true,
1121 .arg_scnprintf
= { [0] = SCA_FDAT
, /* fd */ }, },
1122 { .name
= "listxattr", .errmsg
= true,
1123 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1124 { .name
= "llistxattr", .errmsg
= true,
1125 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1126 { .name
= "lremovexattr", .errmsg
= true,
1127 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1128 { .name
= "lseek", .errmsg
= true,
1129 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1130 [2] = SCA_STRARRAY
, /* whence */ },
1131 .arg_parm
= { [2] = &strarray__whences
, /* whence */ }, },
1132 { .name
= "lsetxattr", .errmsg
= true,
1133 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1134 { .name
= "lstat", .errmsg
= true, .alias
= "newlstat",
1135 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */ }, },
1136 { .name
= "lsxattr", .errmsg
= true,
1137 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1138 { .name
= "madvise", .errmsg
= true,
1139 .arg_scnprintf
= { [0] = SCA_HEX
, /* start */
1140 [2] = SCA_MADV_BHV
, /* behavior */ }, },
1141 { .name
= "mkdir", .errmsg
= true,
1142 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1143 { .name
= "mkdirat", .errmsg
= true,
1144 .arg_scnprintf
= { [0] = SCA_FDAT
, /* fd */
1145 [1] = SCA_FILENAME
, /* pathname */ }, },
1146 { .name
= "mknod", .errmsg
= true,
1147 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */ }, },
1148 { .name
= "mknodat", .errmsg
= true,
1149 .arg_scnprintf
= { [0] = SCA_FDAT
, /* fd */
1150 [1] = SCA_FILENAME
, /* filename */ }, },
1151 { .name
= "mlock", .errmsg
= true,
1152 .arg_scnprintf
= { [0] = SCA_HEX
, /* addr */ }, },
1153 { .name
= "mlockall", .errmsg
= true,
1154 .arg_scnprintf
= { [0] = SCA_HEX
, /* addr */ }, },
1155 { .name
= "mmap", .hexret
= true,
1156 .arg_scnprintf
= { [0] = SCA_HEX
, /* addr */
1157 [2] = SCA_MMAP_PROT
, /* prot */
1158 [3] = SCA_MMAP_FLAGS
, /* flags */
1159 [4] = SCA_FD
, /* fd */ }, },
1160 { .name
= "mprotect", .errmsg
= true,
1161 .arg_scnprintf
= { [0] = SCA_HEX
, /* start */
1162 [2] = SCA_MMAP_PROT
, /* prot */ }, },
1163 { .name
= "mq_unlink", .errmsg
= true,
1164 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* u_name */ }, },
1165 { .name
= "mremap", .hexret
= true,
1166 .arg_scnprintf
= { [0] = SCA_HEX
, /* addr */
1167 [3] = SCA_MREMAP_FLAGS
, /* flags */
1168 [4] = SCA_HEX
, /* new_addr */ }, },
1169 { .name
= "munlock", .errmsg
= true,
1170 .arg_scnprintf
= { [0] = SCA_HEX
, /* addr */ }, },
1171 { .name
= "munmap", .errmsg
= true,
1172 .arg_scnprintf
= { [0] = SCA_HEX
, /* addr */ }, },
1173 { .name
= "name_to_handle_at", .errmsg
= true,
1174 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */ }, },
1175 { .name
= "newfstatat", .errmsg
= true,
1176 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */
1177 [1] = SCA_FILENAME
, /* filename */ }, },
1178 { .name
= "open", .errmsg
= true,
1179 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */
1180 [1] = SCA_OPEN_FLAGS
, /* flags */ }, },
1181 { .name
= "open_by_handle_at", .errmsg
= true,
1182 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */
1183 [2] = SCA_OPEN_FLAGS
, /* flags */ }, },
1184 { .name
= "openat", .errmsg
= true,
1185 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */
1186 [1] = SCA_FILENAME
, /* filename */
1187 [2] = SCA_OPEN_FLAGS
, /* flags */ }, },
1188 { .name
= "perf_event_open", .errmsg
= true,
1189 .arg_scnprintf
= { [1] = SCA_INT
, /* pid */
1190 [2] = SCA_INT
, /* cpu */
1191 [3] = SCA_FD
, /* group_fd */
1192 [4] = SCA_PERF_FLAGS
, /* flags */ }, },
1193 { .name
= "pipe2", .errmsg
= true,
1194 .arg_scnprintf
= { [1] = SCA_PIPE_FLAGS
, /* flags */ }, },
1195 { .name
= "poll", .errmsg
= true, .timeout
= true, },
1196 { .name
= "ppoll", .errmsg
= true, .timeout
= true, },
1197 { .name
= "pread", .errmsg
= true, .alias
= "pread64",
1198 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1199 { .name
= "preadv", .errmsg
= true, .alias
= "pread",
1200 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1201 { .name
= "prlimit64", .errmsg
= true, STRARRAY(1, resource
, rlimit_resources
), },
1202 { .name
= "pwrite", .errmsg
= true, .alias
= "pwrite64",
1203 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1204 { .name
= "pwritev", .errmsg
= true,
1205 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1206 { .name
= "read", .errmsg
= true,
1207 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1208 { .name
= "readlink", .errmsg
= true,
1209 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* path */ }, },
1210 { .name
= "readlinkat", .errmsg
= true,
1211 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */
1212 [1] = SCA_FILENAME
, /* pathname */ }, },
1213 { .name
= "readv", .errmsg
= true,
1214 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1215 { .name
= "recvfrom", .errmsg
= true,
1216 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1217 [3] = SCA_MSG_FLAGS
, /* flags */ }, },
1218 { .name
= "recvmmsg", .errmsg
= true,
1219 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1220 [3] = SCA_MSG_FLAGS
, /* flags */ }, },
1221 { .name
= "recvmsg", .errmsg
= true,
1222 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1223 [2] = SCA_MSG_FLAGS
, /* flags */ }, },
1224 { .name
= "removexattr", .errmsg
= true,
1225 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1226 { .name
= "renameat", .errmsg
= true,
1227 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */ }, },
1228 { .name
= "rmdir", .errmsg
= true,
1229 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1230 { .name
= "rt_sigaction", .errmsg
= true,
1231 .arg_scnprintf
= { [0] = SCA_SIGNUM
, /* sig */ }, },
1232 { .name
= "rt_sigprocmask", .errmsg
= true, STRARRAY(0, how
, sighow
), },
1233 { .name
= "rt_sigqueueinfo", .errmsg
= true,
1234 .arg_scnprintf
= { [1] = SCA_SIGNUM
, /* sig */ }, },
1235 { .name
= "rt_tgsigqueueinfo", .errmsg
= true,
1236 .arg_scnprintf
= { [2] = SCA_SIGNUM
, /* sig */ }, },
1237 { .name
= "select", .errmsg
= true, .timeout
= true, },
1238 { .name
= "sendmmsg", .errmsg
= true,
1239 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1240 [3] = SCA_MSG_FLAGS
, /* flags */ }, },
1241 { .name
= "sendmsg", .errmsg
= true,
1242 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1243 [2] = SCA_MSG_FLAGS
, /* flags */ }, },
1244 { .name
= "sendto", .errmsg
= true,
1245 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */
1246 [3] = SCA_MSG_FLAGS
, /* flags */ }, },
1247 { .name
= "setitimer", .errmsg
= true, STRARRAY(0, which
, itimers
), },
1248 { .name
= "setrlimit", .errmsg
= true, STRARRAY(0, resource
, rlimit_resources
), },
1249 { .name
= "setxattr", .errmsg
= true,
1250 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1251 { .name
= "shutdown", .errmsg
= true,
1252 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1253 { .name
= "socket", .errmsg
= true,
1254 .arg_scnprintf
= { [0] = SCA_STRARRAY
, /* family */
1255 [1] = SCA_SK_TYPE
, /* type */ },
1256 .arg_parm
= { [0] = &strarray__socket_families
, /* family */ }, },
1257 { .name
= "socketpair", .errmsg
= true,
1258 .arg_scnprintf
= { [0] = SCA_STRARRAY
, /* family */
1259 [1] = SCA_SK_TYPE
, /* type */ },
1260 .arg_parm
= { [0] = &strarray__socket_families
, /* family */ }, },
1261 { .name
= "stat", .errmsg
= true, .alias
= "newstat",
1262 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1263 { .name
= "statfs", .errmsg
= true,
1264 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* pathname */ }, },
1265 { .name
= "swapoff", .errmsg
= true,
1266 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* specialfile */ }, },
1267 { .name
= "swapon", .errmsg
= true,
1268 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* specialfile */ }, },
1269 { .name
= "symlinkat", .errmsg
= true,
1270 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */ }, },
1271 { .name
= "tgkill", .errmsg
= true,
1272 .arg_scnprintf
= { [2] = SCA_SIGNUM
, /* sig */ }, },
1273 { .name
= "tkill", .errmsg
= true,
1274 .arg_scnprintf
= { [1] = SCA_SIGNUM
, /* sig */ }, },
1275 { .name
= "truncate", .errmsg
= true,
1276 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* path */ }, },
1277 { .name
= "uname", .errmsg
= true, .alias
= "newuname", },
1278 { .name
= "unlinkat", .errmsg
= true,
1279 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dfd */
1280 [1] = SCA_FILENAME
, /* pathname */ }, },
1281 { .name
= "utime", .errmsg
= true,
1282 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */ }, },
1283 { .name
= "utimensat", .errmsg
= true,
1284 .arg_scnprintf
= { [0] = SCA_FDAT
, /* dirfd */
1285 [1] = SCA_FILENAME
, /* filename */ }, },
1286 { .name
= "utimes", .errmsg
= true,
1287 .arg_scnprintf
= { [0] = SCA_FILENAME
, /* filename */ }, },
1288 { .name
= "vmsplice", .errmsg
= true,
1289 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1290 { .name
= "write", .errmsg
= true,
1291 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1292 { .name
= "writev", .errmsg
= true,
1293 .arg_scnprintf
= { [0] = SCA_FD
, /* fd */ }, },
1296 static int syscall_fmt__cmp(const void *name
, const void *fmtp
)
1298 const struct syscall_fmt
*fmt
= fmtp
;
1299 return strcmp(name
, fmt
->name
);
1302 static struct syscall_fmt
*syscall_fmt__find(const char *name
)
1304 const int nmemb
= ARRAY_SIZE(syscall_fmts
);
1305 return bsearch(name
, syscall_fmts
, nmemb
, sizeof(struct syscall_fmt
), syscall_fmt__cmp
);
1309 struct event_format
*tp_format
;
1311 struct format_field
*args
;
1314 struct syscall_fmt
*fmt
;
1315 size_t (**arg_scnprintf
)(char *bf
, size_t size
, struct syscall_arg
*arg
);
1319 static size_t fprintf_duration(unsigned long t
, FILE *fp
)
1321 double duration
= (double)t
/ NSEC_PER_MSEC
;
1322 size_t printed
= fprintf(fp
, "(");
1324 if (duration
>= 1.0)
1325 printed
+= color_fprintf(fp
, PERF_COLOR_RED
, "%6.3f ms", duration
);
1326 else if (duration
>= 0.01)
1327 printed
+= color_fprintf(fp
, PERF_COLOR_YELLOW
, "%6.3f ms", duration
);
1329 printed
+= color_fprintf(fp
, PERF_COLOR_NORMAL
, "%6.3f ms", duration
);
1330 return printed
+ fprintf(fp
, "): ");
1334 * filename.ptr: The filename char pointer that will be vfs_getname'd
1335 * filename.entry_str_pos: Where to insert the string translated from
1336 * filename.ptr by the vfs_getname tracepoint/kprobe.
1338 struct thread_trace
{
1342 unsigned long nr_events
;
1343 unsigned long pfmaj
, pfmin
;
1348 short int entry_str_pos
;
1350 unsigned int namelen
;
1358 struct intlist
*syscall_stats
;
1361 static struct thread_trace
*thread_trace__new(void)
1363 struct thread_trace
*ttrace
= zalloc(sizeof(struct thread_trace
));
1366 ttrace
->paths
.max
= -1;
1368 ttrace
->syscall_stats
= intlist__new(NULL
);
1373 static struct thread_trace
*thread__trace(struct thread
*thread
, FILE *fp
)
1375 struct thread_trace
*ttrace
;
1380 if (thread__priv(thread
) == NULL
)
1381 thread__set_priv(thread
, thread_trace__new());
1383 if (thread__priv(thread
) == NULL
)
1386 ttrace
= thread__priv(thread
);
1387 ++ttrace
->nr_events
;
1391 color_fprintf(fp
, PERF_COLOR_RED
,
1392 "WARNING: not enough memory, dropping samples!\n");
1396 #define TRACE_PFMAJ (1 << 0)
1397 #define TRACE_PFMIN (1 << 1)
1399 static const size_t trace__entry_str_size
= 2048;
1402 struct perf_tool tool
;
1409 struct syscall
*table
;
1411 struct perf_evsel
*sys_enter
,
1415 struct record_opts opts
;
1416 struct perf_evlist
*evlist
;
1417 struct machine
*host
;
1418 struct thread
*current
;
1421 unsigned long nr_events
;
1422 struct strlist
*ev_qualifier
;
1427 struct intlist
*tid_list
;
1428 struct intlist
*pid_list
;
1433 double duration_filter
;
1439 bool not_ev_qualifier
;
1443 bool multiple_threads
;
1447 bool show_tool_stats
;
1448 bool trace_syscalls
;
1454 static int trace__set_fd_pathname(struct thread
*thread
, int fd
, const char *pathname
)
1456 struct thread_trace
*ttrace
= thread__priv(thread
);
1458 if (fd
> ttrace
->paths
.max
) {
1459 char **npath
= realloc(ttrace
->paths
.table
, (fd
+ 1) * sizeof(char *));
1464 if (ttrace
->paths
.max
!= -1) {
1465 memset(npath
+ ttrace
->paths
.max
+ 1, 0,
1466 (fd
- ttrace
->paths
.max
) * sizeof(char *));
1468 memset(npath
, 0, (fd
+ 1) * sizeof(char *));
1471 ttrace
->paths
.table
= npath
;
1472 ttrace
->paths
.max
= fd
;
1475 ttrace
->paths
.table
[fd
] = strdup(pathname
);
1477 return ttrace
->paths
.table
[fd
] != NULL
? 0 : -1;
1480 static int thread__read_fd_path(struct thread
*thread
, int fd
)
1482 char linkname
[PATH_MAX
], pathname
[PATH_MAX
];
1486 if (thread
->pid_
== thread
->tid
) {
1487 scnprintf(linkname
, sizeof(linkname
),
1488 "/proc/%d/fd/%d", thread
->pid_
, fd
);
1490 scnprintf(linkname
, sizeof(linkname
),
1491 "/proc/%d/task/%d/fd/%d", thread
->pid_
, thread
->tid
, fd
);
1494 if (lstat(linkname
, &st
) < 0 || st
.st_size
+ 1 > (off_t
)sizeof(pathname
))
1497 ret
= readlink(linkname
, pathname
, sizeof(pathname
));
1499 if (ret
< 0 || ret
> st
.st_size
)
1502 pathname
[ret
] = '\0';
1503 return trace__set_fd_pathname(thread
, fd
, pathname
);
1506 static const char *thread__fd_path(struct thread
*thread
, int fd
,
1507 struct trace
*trace
)
1509 struct thread_trace
*ttrace
= thread__priv(thread
);
1517 if ((fd
> ttrace
->paths
.max
|| ttrace
->paths
.table
[fd
] == NULL
)) {
1520 ++trace
->stats
.proc_getname
;
1521 if (thread__read_fd_path(thread
, fd
))
1525 return ttrace
->paths
.table
[fd
];
1528 static size_t syscall_arg__scnprintf_fd(char *bf
, size_t size
,
1529 struct syscall_arg
*arg
)
1532 size_t printed
= scnprintf(bf
, size
, "%d", fd
);
1533 const char *path
= thread__fd_path(arg
->thread
, fd
, arg
->trace
);
1536 printed
+= scnprintf(bf
+ printed
, size
- printed
, "<%s>", path
);
1541 static size_t syscall_arg__scnprintf_close_fd(char *bf
, size_t size
,
1542 struct syscall_arg
*arg
)
1545 size_t printed
= syscall_arg__scnprintf_fd(bf
, size
, arg
);
1546 struct thread_trace
*ttrace
= thread__priv(arg
->thread
);
1548 if (ttrace
&& fd
>= 0 && fd
<= ttrace
->paths
.max
)
1549 zfree(&ttrace
->paths
.table
[fd
]);
1554 static void thread__set_filename_pos(struct thread
*thread
, const char *bf
,
1557 struct thread_trace
*ttrace
= thread__priv(thread
);
1559 ttrace
->filename
.ptr
= ptr
;
1560 ttrace
->filename
.entry_str_pos
= bf
- ttrace
->entry_str
;
1563 static size_t syscall_arg__scnprintf_filename(char *bf
, size_t size
,
1564 struct syscall_arg
*arg
)
1566 unsigned long ptr
= arg
->val
;
1568 if (!arg
->trace
->vfs_getname
)
1569 return scnprintf(bf
, size
, "%#x", ptr
);
1571 thread__set_filename_pos(arg
->thread
, bf
, ptr
);
1575 static bool trace__filter_duration(struct trace
*trace
, double t
)
1577 return t
< (trace
->duration_filter
* NSEC_PER_MSEC
);
1580 static size_t trace__fprintf_tstamp(struct trace
*trace
, u64 tstamp
, FILE *fp
)
1582 double ts
= (double)(tstamp
- trace
->base_time
) / NSEC_PER_MSEC
;
1584 return fprintf(fp
, "%10.3f ", ts
);
1587 static bool done
= false;
1588 static bool interrupted
= false;
1590 static void sig_handler(int sig
)
1593 interrupted
= sig
== SIGINT
;
1596 static size_t trace__fprintf_entry_head(struct trace
*trace
, struct thread
*thread
,
1597 u64 duration
, u64 tstamp
, FILE *fp
)
1599 size_t printed
= trace__fprintf_tstamp(trace
, tstamp
, fp
);
1600 printed
+= fprintf_duration(duration
, fp
);
1602 if (trace
->multiple_threads
) {
1603 if (trace
->show_comm
)
1604 printed
+= fprintf(fp
, "%.14s/", thread__comm_str(thread
));
1605 printed
+= fprintf(fp
, "%d ", thread
->tid
);
1611 static int trace__process_event(struct trace
*trace
, struct machine
*machine
,
1612 union perf_event
*event
, struct perf_sample
*sample
)
1616 switch (event
->header
.type
) {
1617 case PERF_RECORD_LOST
:
1618 color_fprintf(trace
->output
, PERF_COLOR_RED
,
1619 "LOST %" PRIu64
" events!\n", event
->lost
.lost
);
1620 ret
= machine__process_lost_event(machine
, event
, sample
);
1622 ret
= machine__process_event(machine
, event
, sample
);
1629 static int trace__tool_process(struct perf_tool
*tool
,
1630 union perf_event
*event
,
1631 struct perf_sample
*sample
,
1632 struct machine
*machine
)
1634 struct trace
*trace
= container_of(tool
, struct trace
, tool
);
1635 return trace__process_event(trace
, machine
, event
, sample
);
1638 static int trace__symbols_init(struct trace
*trace
, struct perf_evlist
*evlist
)
1640 int err
= symbol__init(NULL
);
1645 trace
->host
= machine__new_host();
1646 if (trace
->host
== NULL
)
1649 if (trace_event__register_resolver(trace
->host
, machine__resolve_kernel_addr
) < 0)
1652 err
= __machine__synthesize_threads(trace
->host
, &trace
->tool
, &trace
->opts
.target
,
1653 evlist
->threads
, trace__tool_process
, false,
1654 trace
->opts
.proc_map_timeout
);
1661 static int syscall__set_arg_fmts(struct syscall
*sc
)
1663 struct format_field
*field
;
1666 sc
->arg_scnprintf
= calloc(sc
->nr_args
, sizeof(void *));
1667 if (sc
->arg_scnprintf
== NULL
)
1671 sc
->arg_parm
= sc
->fmt
->arg_parm
;
1673 for (field
= sc
->args
; field
; field
= field
->next
) {
1674 if (sc
->fmt
&& sc
->fmt
->arg_scnprintf
[idx
])
1675 sc
->arg_scnprintf
[idx
] = sc
->fmt
->arg_scnprintf
[idx
];
1676 else if (field
->flags
& FIELD_IS_POINTER
)
1677 sc
->arg_scnprintf
[idx
] = syscall_arg__scnprintf_hex
;
1684 static int trace__read_syscall_info(struct trace
*trace
, int id
)
1688 const char *name
= audit_syscall_to_name(id
, trace
->audit
.machine
);
1693 if (id
> trace
->syscalls
.max
) {
1694 struct syscall
*nsyscalls
= realloc(trace
->syscalls
.table
, (id
+ 1) * sizeof(*sc
));
1696 if (nsyscalls
== NULL
)
1699 if (trace
->syscalls
.max
!= -1) {
1700 memset(nsyscalls
+ trace
->syscalls
.max
+ 1, 0,
1701 (id
- trace
->syscalls
.max
) * sizeof(*sc
));
1703 memset(nsyscalls
, 0, (id
+ 1) * sizeof(*sc
));
1706 trace
->syscalls
.table
= nsyscalls
;
1707 trace
->syscalls
.max
= id
;
1710 sc
= trace
->syscalls
.table
+ id
;
1713 sc
->fmt
= syscall_fmt__find(sc
->name
);
1715 snprintf(tp_name
, sizeof(tp_name
), "sys_enter_%s", sc
->name
);
1716 sc
->tp_format
= trace_event__tp_format("syscalls", tp_name
);
1718 if (IS_ERR(sc
->tp_format
) && sc
->fmt
&& sc
->fmt
->alias
) {
1719 snprintf(tp_name
, sizeof(tp_name
), "sys_enter_%s", sc
->fmt
->alias
);
1720 sc
->tp_format
= trace_event__tp_format("syscalls", tp_name
);
1723 if (IS_ERR(sc
->tp_format
))
1726 sc
->args
= sc
->tp_format
->format
.fields
;
1727 sc
->nr_args
= sc
->tp_format
->format
.nr_fields
;
1729 * We need to check and discard the first variable '__syscall_nr'
1730 * or 'nr' that mean the syscall number. It is needless here.
1731 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1733 if (sc
->args
&& (!strcmp(sc
->args
->name
, "__syscall_nr") || !strcmp(sc
->args
->name
, "nr"))) {
1734 sc
->args
= sc
->args
->next
;
1738 sc
->is_exit
= !strcmp(name
, "exit_group") || !strcmp(name
, "exit");
1740 return syscall__set_arg_fmts(sc
);
1743 static int trace__validate_ev_qualifier(struct trace
*trace
)
1746 struct str_node
*pos
;
1748 trace
->ev_qualifier_ids
.nr
= strlist__nr_entries(trace
->ev_qualifier
);
1749 trace
->ev_qualifier_ids
.entries
= malloc(trace
->ev_qualifier_ids
.nr
*
1750 sizeof(trace
->ev_qualifier_ids
.entries
[0]));
1752 if (trace
->ev_qualifier_ids
.entries
== NULL
) {
1753 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1761 strlist__for_each(pos
, trace
->ev_qualifier
) {
1762 const char *sc
= pos
->s
;
1763 int id
= audit_name_to_syscall(sc
, trace
->audit
.machine
);
1767 fputs("Error:\tInvalid syscall ", trace
->output
);
1770 fputs(", ", trace
->output
);
1773 fputs(sc
, trace
->output
);
1776 trace
->ev_qualifier_ids
.entries
[i
++] = id
;
1780 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1781 "\nHint:\tand: 'man syscalls'\n", trace
->output
);
1782 zfree(&trace
->ev_qualifier_ids
.entries
);
1783 trace
->ev_qualifier_ids
.nr
= 0;
1790 * args is to be interpreted as a series of longs but we need to handle
1791 * 8-byte unaligned accesses. args points to raw_data within the event
1792 * and raw_data is guaranteed to be 8-byte unaligned because it is
1793 * preceded by raw_size which is a u32. So we need to copy args to a temp
1794 * variable to read it. Most notably this avoids extended load instructions
1795 * on unaligned addresses
1798 static size_t syscall__scnprintf_args(struct syscall
*sc
, char *bf
, size_t size
,
1799 unsigned char *args
, struct trace
*trace
,
1800 struct thread
*thread
)
1806 if (sc
->args
!= NULL
) {
1807 struct format_field
*field
;
1809 struct syscall_arg arg
= {
1816 for (field
= sc
->args
; field
;
1817 field
= field
->next
, ++arg
.idx
, bit
<<= 1) {
1821 /* special care for unaligned accesses */
1822 p
= args
+ sizeof(unsigned long) * arg
.idx
;
1823 memcpy(&val
, p
, sizeof(val
));
1826 * Suppress this argument if its value is zero and
1827 * and we don't have a string associated in an
1831 !(sc
->arg_scnprintf
&&
1832 sc
->arg_scnprintf
[arg
.idx
] == SCA_STRARRAY
&&
1833 sc
->arg_parm
[arg
.idx
]))
1836 printed
+= scnprintf(bf
+ printed
, size
- printed
,
1837 "%s%s: ", printed
? ", " : "", field
->name
);
1838 if (sc
->arg_scnprintf
&& sc
->arg_scnprintf
[arg
.idx
]) {
1841 arg
.parm
= sc
->arg_parm
[arg
.idx
];
1842 printed
+= sc
->arg_scnprintf
[arg
.idx
](bf
+ printed
,
1843 size
- printed
, &arg
);
1845 printed
+= scnprintf(bf
+ printed
, size
- printed
,
1853 /* special care for unaligned accesses */
1854 p
= args
+ sizeof(unsigned long) * i
;
1855 memcpy(&val
, p
, sizeof(val
));
1856 printed
+= scnprintf(bf
+ printed
, size
- printed
,
1858 printed
? ", " : "", i
, val
);
1866 typedef int (*tracepoint_handler
)(struct trace
*trace
, struct perf_evsel
*evsel
,
1867 union perf_event
*event
,
1868 struct perf_sample
*sample
);
1870 static struct syscall
*trace__syscall_info(struct trace
*trace
,
1871 struct perf_evsel
*evsel
, int id
)
1877 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1878 * before that, leaving at a higher verbosity level till that is
1879 * explained. Reproduced with plain ftrace with:
1881 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1882 * grep "NR -1 " /t/trace_pipe
1884 * After generating some load on the machine.
1888 fprintf(trace
->output
, "Invalid syscall %d id, skipping (%s, %" PRIu64
") ...\n",
1889 id
, perf_evsel__name(evsel
), ++n
);
1894 if ((id
> trace
->syscalls
.max
|| trace
->syscalls
.table
[id
].name
== NULL
) &&
1895 trace__read_syscall_info(trace
, id
))
1898 if ((id
> trace
->syscalls
.max
|| trace
->syscalls
.table
[id
].name
== NULL
))
1901 return &trace
->syscalls
.table
[id
];
1905 fprintf(trace
->output
, "Problems reading syscall %d", id
);
1906 if (id
<= trace
->syscalls
.max
&& trace
->syscalls
.table
[id
].name
!= NULL
)
1907 fprintf(trace
->output
, "(%s)", trace
->syscalls
.table
[id
].name
);
1908 fputs(" information\n", trace
->output
);
1913 static void thread__update_stats(struct thread_trace
*ttrace
,
1914 int id
, struct perf_sample
*sample
)
1916 struct int_node
*inode
;
1917 struct stats
*stats
;
1920 inode
= intlist__findnew(ttrace
->syscall_stats
, id
);
1924 stats
= inode
->priv
;
1925 if (stats
== NULL
) {
1926 stats
= malloc(sizeof(struct stats
));
1930 inode
->priv
= stats
;
1933 if (ttrace
->entry_time
&& sample
->time
> ttrace
->entry_time
)
1934 duration
= sample
->time
- ttrace
->entry_time
;
1936 update_stats(stats
, duration
);
1939 static int trace__printf_interrupted_entry(struct trace
*trace
, struct perf_sample
*sample
)
1941 struct thread_trace
*ttrace
;
1945 if (trace
->current
== NULL
)
1948 ttrace
= thread__priv(trace
->current
);
1950 if (!ttrace
->entry_pending
)
1953 duration
= sample
->time
- ttrace
->entry_time
;
1955 printed
= trace__fprintf_entry_head(trace
, trace
->current
, duration
, sample
->time
, trace
->output
);
1956 printed
+= fprintf(trace
->output
, "%-70s) ...\n", ttrace
->entry_str
);
1957 ttrace
->entry_pending
= false;
1962 static int trace__sys_enter(struct trace
*trace
, struct perf_evsel
*evsel
,
1963 union perf_event
*event __maybe_unused
,
1964 struct perf_sample
*sample
)
1969 struct thread
*thread
;
1970 int id
= perf_evsel__sc_tp_uint(evsel
, id
, sample
), err
= -1;
1971 struct syscall
*sc
= trace__syscall_info(trace
, evsel
, id
);
1972 struct thread_trace
*ttrace
;
1977 thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
1978 ttrace
= thread__trace(thread
, trace
->output
);
1982 args
= perf_evsel__sc_tp_ptr(evsel
, args
, sample
);
1984 if (ttrace
->entry_str
== NULL
) {
1985 ttrace
->entry_str
= malloc(trace__entry_str_size
);
1986 if (!ttrace
->entry_str
)
1990 if (!trace
->summary_only
)
1991 trace__printf_interrupted_entry(trace
, sample
);
1993 ttrace
->entry_time
= sample
->time
;
1994 msg
= ttrace
->entry_str
;
1995 printed
+= scnprintf(msg
+ printed
, trace__entry_str_size
- printed
, "%s(", sc
->name
);
1997 printed
+= syscall__scnprintf_args(sc
, msg
+ printed
, trace__entry_str_size
- printed
,
1998 args
, trace
, thread
);
2001 if (!trace
->duration_filter
&& !trace
->summary_only
) {
2002 trace__fprintf_entry_head(trace
, thread
, 1, sample
->time
, trace
->output
);
2003 fprintf(trace
->output
, "%-70s\n", ttrace
->entry_str
);
2006 ttrace
->entry_pending
= true;
2007 /* See trace__vfs_getname & trace__sys_exit */
2008 ttrace
->filename
.pending_open
= false;
2011 if (trace
->current
!= thread
) {
2012 thread__put(trace
->current
);
2013 trace
->current
= thread__get(thread
);
2017 thread__put(thread
);
2021 static int trace__sys_exit(struct trace
*trace
, struct perf_evsel
*evsel
,
2022 union perf_event
*event __maybe_unused
,
2023 struct perf_sample
*sample
)
2027 struct thread
*thread
;
2028 int id
= perf_evsel__sc_tp_uint(evsel
, id
, sample
), err
= -1;
2029 struct syscall
*sc
= trace__syscall_info(trace
, evsel
, id
);
2030 struct thread_trace
*ttrace
;
2035 thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
2036 ttrace
= thread__trace(thread
, trace
->output
);
2041 thread__update_stats(ttrace
, id
, sample
);
2043 ret
= perf_evsel__sc_tp_uint(evsel
, ret
, sample
);
2045 if (id
== trace
->audit
.open_id
&& ret
>= 0 && ttrace
->filename
.pending_open
) {
2046 trace__set_fd_pathname(thread
, ret
, ttrace
->filename
.name
);
2047 ttrace
->filename
.pending_open
= false;
2048 ++trace
->stats
.vfs_getname
;
2051 ttrace
->exit_time
= sample
->time
;
2053 if (ttrace
->entry_time
) {
2054 duration
= sample
->time
- ttrace
->entry_time
;
2055 if (trace__filter_duration(trace
, duration
))
2057 } else if (trace
->duration_filter
)
2060 if (trace
->summary_only
)
2063 trace__fprintf_entry_head(trace
, thread
, duration
, sample
->time
, trace
->output
);
2065 if (ttrace
->entry_pending
) {
2066 fprintf(trace
->output
, "%-70s", ttrace
->entry_str
);
2068 fprintf(trace
->output
, " ... [");
2069 color_fprintf(trace
->output
, PERF_COLOR_YELLOW
, "continued");
2070 fprintf(trace
->output
, "]: %s()", sc
->name
);
2073 if (sc
->fmt
== NULL
) {
2075 fprintf(trace
->output
, ") = %ld", ret
);
2076 } else if (ret
< 0 && sc
->fmt
->errmsg
) {
2077 char bf
[STRERR_BUFSIZE
];
2078 const char *emsg
= strerror_r(-ret
, bf
, sizeof(bf
)),
2079 *e
= audit_errno_to_name(-ret
);
2081 fprintf(trace
->output
, ") = -1 %s %s", e
, emsg
);
2082 } else if (ret
== 0 && sc
->fmt
->timeout
)
2083 fprintf(trace
->output
, ") = 0 Timeout");
2084 else if (sc
->fmt
->hexret
)
2085 fprintf(trace
->output
, ") = %#lx", ret
);
2089 fputc('\n', trace
->output
);
2091 ttrace
->entry_pending
= false;
2094 thread__put(thread
);
2098 static int trace__vfs_getname(struct trace
*trace
, struct perf_evsel
*evsel
,
2099 union perf_event
*event __maybe_unused
,
2100 struct perf_sample
*sample
)
2102 struct thread
*thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
2103 struct thread_trace
*ttrace
;
2104 size_t filename_len
, entry_str_len
, to_move
;
2105 ssize_t remaining_space
;
2107 const char *filename
= perf_evsel__rawptr(evsel
, sample
, "pathname");
2112 ttrace
= thread__priv(thread
);
2116 filename_len
= strlen(filename
);
2118 if (ttrace
->filename
.namelen
< filename_len
) {
2119 char *f
= realloc(ttrace
->filename
.name
, filename_len
+ 1);
2124 ttrace
->filename
.namelen
= filename_len
;
2125 ttrace
->filename
.name
= f
;
2128 strcpy(ttrace
->filename
.name
, filename
);
2129 ttrace
->filename
.pending_open
= true;
2131 if (!ttrace
->filename
.ptr
)
2134 entry_str_len
= strlen(ttrace
->entry_str
);
2135 remaining_space
= trace__entry_str_size
- entry_str_len
- 1; /* \0 */
2136 if (remaining_space
<= 0)
2139 if (filename_len
> (size_t)remaining_space
) {
2140 filename
+= filename_len
- remaining_space
;
2141 filename_len
= remaining_space
;
2144 to_move
= entry_str_len
- ttrace
->filename
.entry_str_pos
+ 1; /* \0 */
2145 pos
= ttrace
->entry_str
+ ttrace
->filename
.entry_str_pos
;
2146 memmove(pos
+ filename_len
, pos
, to_move
);
2147 memcpy(pos
, filename
, filename_len
);
2149 ttrace
->filename
.ptr
= 0;
2150 ttrace
->filename
.entry_str_pos
= 0;
2155 static int trace__sched_stat_runtime(struct trace
*trace
, struct perf_evsel
*evsel
,
2156 union perf_event
*event __maybe_unused
,
2157 struct perf_sample
*sample
)
2159 u64 runtime
= perf_evsel__intval(evsel
, sample
, "runtime");
2160 double runtime_ms
= (double)runtime
/ NSEC_PER_MSEC
;
2161 struct thread
*thread
= machine__findnew_thread(trace
->host
,
2164 struct thread_trace
*ttrace
= thread__trace(thread
, trace
->output
);
2169 ttrace
->runtime_ms
+= runtime_ms
;
2170 trace
->runtime_ms
+= runtime_ms
;
2171 thread__put(thread
);
2175 fprintf(trace
->output
, "%s: comm=%s,pid=%u,runtime=%" PRIu64
",vruntime=%" PRIu64
")\n",
2177 perf_evsel__strval(evsel
, sample
, "comm"),
2178 (pid_t
)perf_evsel__intval(evsel
, sample
, "pid"),
2180 perf_evsel__intval(evsel
, sample
, "vruntime"));
2181 thread__put(thread
);
2185 static void bpf_output__printer(enum binary_printer_ops op
,
2186 unsigned int val
, void *extra
)
2188 FILE *output
= extra
;
2189 unsigned char ch
= (unsigned char)val
;
2192 case BINARY_PRINT_CHAR_DATA
:
2193 fprintf(output
, "%c", isprint(ch
) ? ch
: '.');
2195 case BINARY_PRINT_DATA_BEGIN
:
2196 case BINARY_PRINT_LINE_BEGIN
:
2197 case BINARY_PRINT_ADDR
:
2198 case BINARY_PRINT_NUM_DATA
:
2199 case BINARY_PRINT_NUM_PAD
:
2200 case BINARY_PRINT_SEP
:
2201 case BINARY_PRINT_CHAR_PAD
:
2202 case BINARY_PRINT_LINE_END
:
2203 case BINARY_PRINT_DATA_END
:
2209 static void bpf_output__fprintf(struct trace
*trace
,
2210 struct perf_sample
*sample
)
2212 print_binary(sample
->raw_data
, sample
->raw_size
, 8,
2213 bpf_output__printer
, trace
->output
);
2216 static int trace__event_handler(struct trace
*trace
, struct perf_evsel
*evsel
,
2217 union perf_event
*event __maybe_unused
,
2218 struct perf_sample
*sample
)
2220 trace__printf_interrupted_entry(trace
, sample
);
2221 trace__fprintf_tstamp(trace
, sample
->time
, trace
->output
);
2223 if (trace
->trace_syscalls
)
2224 fprintf(trace
->output
, "( ): ");
2226 fprintf(trace
->output
, "%s:", evsel
->name
);
2228 if (perf_evsel__is_bpf_output(evsel
)) {
2229 bpf_output__fprintf(trace
, sample
);
2230 } else if (evsel
->tp_format
) {
2231 event_format__fprintf(evsel
->tp_format
, sample
->cpu
,
2232 sample
->raw_data
, sample
->raw_size
,
2236 fprintf(trace
->output
, ")\n");
2240 static void print_location(FILE *f
, struct perf_sample
*sample
,
2241 struct addr_location
*al
,
2242 bool print_dso
, bool print_sym
)
2245 if ((verbose
|| print_dso
) && al
->map
)
2246 fprintf(f
, "%s@", al
->map
->dso
->long_name
);
2248 if ((verbose
|| print_sym
) && al
->sym
)
2249 fprintf(f
, "%s+0x%" PRIx64
, al
->sym
->name
,
2250 al
->addr
- al
->sym
->start
);
2252 fprintf(f
, "0x%" PRIx64
, al
->addr
);
2254 fprintf(f
, "0x%" PRIx64
, sample
->addr
);
2257 static int trace__pgfault(struct trace
*trace
,
2258 struct perf_evsel
*evsel
,
2259 union perf_event
*event __maybe_unused
,
2260 struct perf_sample
*sample
)
2262 struct thread
*thread
;
2263 struct addr_location al
;
2264 char map_type
= 'd';
2265 struct thread_trace
*ttrace
;
2268 thread
= machine__findnew_thread(trace
->host
, sample
->pid
, sample
->tid
);
2269 ttrace
= thread__trace(thread
, trace
->output
);
2273 if (evsel
->attr
.config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
)
2278 if (trace
->summary_only
)
2281 thread__find_addr_location(thread
, sample
->cpumode
, MAP__FUNCTION
,
2284 trace__fprintf_entry_head(trace
, thread
, 0, sample
->time
, trace
->output
);
2286 fprintf(trace
->output
, "%sfault [",
2287 evsel
->attr
.config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
?
2290 print_location(trace
->output
, sample
, &al
, false, true);
2292 fprintf(trace
->output
, "] => ");
2294 thread__find_addr_location(thread
, sample
->cpumode
, MAP__VARIABLE
,
2298 thread__find_addr_location(thread
, sample
->cpumode
,
2299 MAP__FUNCTION
, sample
->addr
, &al
);
2307 print_location(trace
->output
, sample
, &al
, true, false);
2309 fprintf(trace
->output
, " (%c%c)\n", map_type
, al
.level
);
2313 thread__put(thread
);
2317 static bool skip_sample(struct trace
*trace
, struct perf_sample
*sample
)
2319 if ((trace
->pid_list
&& intlist__find(trace
->pid_list
, sample
->pid
)) ||
2320 (trace
->tid_list
&& intlist__find(trace
->tid_list
, sample
->tid
)))
2323 if (trace
->pid_list
|| trace
->tid_list
)
2329 static int trace__process_sample(struct perf_tool
*tool
,
2330 union perf_event
*event
,
2331 struct perf_sample
*sample
,
2332 struct perf_evsel
*evsel
,
2333 struct machine
*machine __maybe_unused
)
2335 struct trace
*trace
= container_of(tool
, struct trace
, tool
);
2338 tracepoint_handler handler
= evsel
->handler
;
2340 if (skip_sample(trace
, sample
))
2343 if (!trace
->full_time
&& trace
->base_time
== 0)
2344 trace
->base_time
= sample
->time
;
2348 handler(trace
, evsel
, event
, sample
);
2354 static int parse_target_str(struct trace
*trace
)
2356 if (trace
->opts
.target
.pid
) {
2357 trace
->pid_list
= intlist__new(trace
->opts
.target
.pid
);
2358 if (trace
->pid_list
== NULL
) {
2359 pr_err("Error parsing process id string\n");
2364 if (trace
->opts
.target
.tid
) {
2365 trace
->tid_list
= intlist__new(trace
->opts
.target
.tid
);
2366 if (trace
->tid_list
== NULL
) {
2367 pr_err("Error parsing thread id string\n");
2375 static int trace__record(struct trace
*trace
, int argc
, const char **argv
)
2377 unsigned int rec_argc
, i
, j
;
2378 const char **rec_argv
;
2379 const char * const record_args
[] = {
2386 const char * const sc_args
[] = { "-e", };
2387 unsigned int sc_args_nr
= ARRAY_SIZE(sc_args
);
2388 const char * const majpf_args
[] = { "-e", "major-faults" };
2389 unsigned int majpf_args_nr
= ARRAY_SIZE(majpf_args
);
2390 const char * const minpf_args
[] = { "-e", "minor-faults" };
2391 unsigned int minpf_args_nr
= ARRAY_SIZE(minpf_args
);
2393 /* +1 is for the event string below */
2394 rec_argc
= ARRAY_SIZE(record_args
) + sc_args_nr
+ 1 +
2395 majpf_args_nr
+ minpf_args_nr
+ argc
;
2396 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
2398 if (rec_argv
== NULL
)
2402 for (i
= 0; i
< ARRAY_SIZE(record_args
); i
++)
2403 rec_argv
[j
++] = record_args
[i
];
2405 if (trace
->trace_syscalls
) {
2406 for (i
= 0; i
< sc_args_nr
; i
++)
2407 rec_argv
[j
++] = sc_args
[i
];
2409 /* event string may be different for older kernels - e.g., RHEL6 */
2410 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2411 rec_argv
[j
++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2412 else if (is_valid_tracepoint("syscalls:sys_enter"))
2413 rec_argv
[j
++] = "syscalls:sys_enter,syscalls:sys_exit";
2415 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
2420 if (trace
->trace_pgfaults
& TRACE_PFMAJ
)
2421 for (i
= 0; i
< majpf_args_nr
; i
++)
2422 rec_argv
[j
++] = majpf_args
[i
];
2424 if (trace
->trace_pgfaults
& TRACE_PFMIN
)
2425 for (i
= 0; i
< minpf_args_nr
; i
++)
2426 rec_argv
[j
++] = minpf_args
[i
];
2428 for (i
= 0; i
< (unsigned int)argc
; i
++)
2429 rec_argv
[j
++] = argv
[i
];
2431 return cmd_record(j
, rec_argv
, NULL
);
2434 static size_t trace__fprintf_thread_summary(struct trace
*trace
, FILE *fp
);
2436 static bool perf_evlist__add_vfs_getname(struct perf_evlist
*evlist
)
2438 struct perf_evsel
*evsel
= perf_evsel__newtp("probe", "vfs_getname");
2443 if (perf_evsel__field(evsel
, "pathname") == NULL
) {
2444 perf_evsel__delete(evsel
);
2448 evsel
->handler
= trace__vfs_getname
;
2449 perf_evlist__add(evlist
, evsel
);
2453 static int perf_evlist__add_pgfault(struct perf_evlist
*evlist
,
2456 struct perf_evsel
*evsel
;
2457 struct perf_event_attr attr
= {
2458 .type
= PERF_TYPE_SOFTWARE
,
2462 attr
.config
= config
;
2463 attr
.sample_period
= 1;
2465 event_attr_init(&attr
);
2467 evsel
= perf_evsel__new(&attr
);
2471 evsel
->handler
= trace__pgfault
;
2472 perf_evlist__add(evlist
, evsel
);
2477 static void trace__handle_event(struct trace
*trace
, union perf_event
*event
, struct perf_sample
*sample
)
2479 const u32 type
= event
->header
.type
;
2480 struct perf_evsel
*evsel
;
2482 if (!trace
->full_time
&& trace
->base_time
== 0)
2483 trace
->base_time
= sample
->time
;
2485 if (type
!= PERF_RECORD_SAMPLE
) {
2486 trace__process_event(trace
, trace
->host
, event
, sample
);
2490 evsel
= perf_evlist__id2evsel(trace
->evlist
, sample
->id
);
2491 if (evsel
== NULL
) {
2492 fprintf(trace
->output
, "Unknown tp ID %" PRIu64
", skipping...\n", sample
->id
);
2496 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
2497 sample
->raw_data
== NULL
) {
2498 fprintf(trace
->output
, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2499 perf_evsel__name(evsel
), sample
->tid
,
2500 sample
->cpu
, sample
->raw_size
);
2502 tracepoint_handler handler
= evsel
->handler
;
2503 handler(trace
, evsel
, event
, sample
);
2507 static int trace__add_syscall_newtp(struct trace
*trace
)
2510 struct perf_evlist
*evlist
= trace
->evlist
;
2511 struct perf_evsel
*sys_enter
, *sys_exit
;
2513 sys_enter
= perf_evsel__syscall_newtp("sys_enter", trace__sys_enter
);
2514 if (sys_enter
== NULL
)
2517 if (perf_evsel__init_sc_tp_ptr_field(sys_enter
, args
))
2518 goto out_delete_sys_enter
;
2520 sys_exit
= perf_evsel__syscall_newtp("sys_exit", trace__sys_exit
);
2521 if (sys_exit
== NULL
)
2522 goto out_delete_sys_enter
;
2524 if (perf_evsel__init_sc_tp_uint_field(sys_exit
, ret
))
2525 goto out_delete_sys_exit
;
2527 perf_evlist__add(evlist
, sys_enter
);
2528 perf_evlist__add(evlist
, sys_exit
);
2530 trace
->syscalls
.events
.sys_enter
= sys_enter
;
2531 trace
->syscalls
.events
.sys_exit
= sys_exit
;
2537 out_delete_sys_exit
:
2538 perf_evsel__delete_priv(sys_exit
);
2539 out_delete_sys_enter
:
2540 perf_evsel__delete_priv(sys_enter
);
2544 static int trace__set_ev_qualifier_filter(struct trace
*trace
)
2547 char *filter
= asprintf_expr_inout_ints("id", !trace
->not_ev_qualifier
,
2548 trace
->ev_qualifier_ids
.nr
,
2549 trace
->ev_qualifier_ids
.entries
);
2554 if (!perf_evsel__append_filter(trace
->syscalls
.events
.sys_enter
, "&&", filter
))
2555 err
= perf_evsel__append_filter(trace
->syscalls
.events
.sys_exit
, "&&", filter
);
2565 static int trace__run(struct trace
*trace
, int argc
, const char **argv
)
2567 struct perf_evlist
*evlist
= trace
->evlist
;
2568 struct perf_evsel
*evsel
;
2570 unsigned long before
;
2571 const bool forks
= argc
> 0;
2572 bool draining
= false;
2576 if (trace
->trace_syscalls
&& trace__add_syscall_newtp(trace
))
2577 goto out_error_raw_syscalls
;
2579 if (trace
->trace_syscalls
)
2580 trace
->vfs_getname
= perf_evlist__add_vfs_getname(evlist
);
2582 if ((trace
->trace_pgfaults
& TRACE_PFMAJ
) &&
2583 perf_evlist__add_pgfault(evlist
, PERF_COUNT_SW_PAGE_FAULTS_MAJ
)) {
2587 if ((trace
->trace_pgfaults
& TRACE_PFMIN
) &&
2588 perf_evlist__add_pgfault(evlist
, PERF_COUNT_SW_PAGE_FAULTS_MIN
))
2592 perf_evlist__add_newtp(evlist
, "sched", "sched_stat_runtime",
2593 trace__sched_stat_runtime
))
2594 goto out_error_sched_stat_runtime
;
2596 err
= perf_evlist__create_maps(evlist
, &trace
->opts
.target
);
2598 fprintf(trace
->output
, "Problems parsing the target to trace, check your options!\n");
2599 goto out_delete_evlist
;
2602 err
= trace__symbols_init(trace
, evlist
);
2604 fprintf(trace
->output
, "Problems initializing symbol libraries!\n");
2605 goto out_delete_evlist
;
2608 perf_evlist__config(evlist
, &trace
->opts
);
2610 signal(SIGCHLD
, sig_handler
);
2611 signal(SIGINT
, sig_handler
);
2614 err
= perf_evlist__prepare_workload(evlist
, &trace
->opts
.target
,
2617 fprintf(trace
->output
, "Couldn't run the workload!\n");
2618 goto out_delete_evlist
;
2622 err
= perf_evlist__open(evlist
);
2624 goto out_error_open
;
2626 err
= bpf__apply_obj_config();
2628 char errbuf
[BUFSIZ
];
2630 bpf__strerror_apply_obj_config(err
, errbuf
, sizeof(errbuf
));
2631 pr_err("ERROR: Apply config to BPF failed: %s\n",
2633 goto out_error_open
;
2637 * Better not use !target__has_task() here because we need to cover the
2638 * case where no threads were specified in the command line, but a
2639 * workload was, and in that case we will fill in the thread_map when
2640 * we fork the workload in perf_evlist__prepare_workload.
2642 if (trace
->filter_pids
.nr
> 0)
2643 err
= perf_evlist__set_filter_pids(evlist
, trace
->filter_pids
.nr
, trace
->filter_pids
.entries
);
2644 else if (thread_map__pid(evlist
->threads
, 0) == -1)
2645 err
= perf_evlist__set_filter_pid(evlist
, getpid());
2650 if (trace
->ev_qualifier_ids
.nr
> 0) {
2651 err
= trace__set_ev_qualifier_filter(trace
);
2655 pr_debug("event qualifier tracepoint filter: %s\n",
2656 trace
->syscalls
.events
.sys_exit
->filter
);
2659 err
= perf_evlist__apply_filters(evlist
, &evsel
);
2661 goto out_error_apply_filters
;
2663 err
= perf_evlist__mmap(evlist
, trace
->opts
.mmap_pages
, false);
2665 goto out_error_mmap
;
2667 if (!target__none(&trace
->opts
.target
))
2668 perf_evlist__enable(evlist
);
2671 perf_evlist__start_workload(evlist
);
2673 trace
->multiple_threads
= thread_map__pid(evlist
->threads
, 0) == -1 ||
2674 evlist
->threads
->nr
> 1 ||
2675 perf_evlist__first(evlist
)->attr
.inherit
;
2677 before
= trace
->nr_events
;
2679 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
2680 union perf_event
*event
;
2682 while ((event
= perf_evlist__mmap_read(evlist
, i
)) != NULL
) {
2683 struct perf_sample sample
;
2687 err
= perf_evlist__parse_sample(evlist
, event
, &sample
);
2689 fprintf(trace
->output
, "Can't parse sample, err = %d, skipping...\n", err
);
2693 trace__handle_event(trace
, event
, &sample
);
2695 perf_evlist__mmap_consume(evlist
, i
);
2700 if (done
&& !draining
) {
2701 perf_evlist__disable(evlist
);
2707 if (trace
->nr_events
== before
) {
2708 int timeout
= done
? 100 : -1;
2710 if (!draining
&& perf_evlist__poll(evlist
, timeout
) > 0) {
2711 if (perf_evlist__filter_pollfd(evlist
, POLLERR
| POLLHUP
) == 0)
2721 thread__zput(trace
->current
);
2723 perf_evlist__disable(evlist
);
2727 trace__fprintf_thread_summary(trace
, trace
->output
);
2729 if (trace
->show_tool_stats
) {
2730 fprintf(trace
->output
, "Stats:\n "
2731 " vfs_getname : %" PRIu64
"\n"
2732 " proc_getname: %" PRIu64
"\n",
2733 trace
->stats
.vfs_getname
,
2734 trace
->stats
.proc_getname
);
2739 perf_evlist__delete(evlist
);
2740 trace
->evlist
= NULL
;
2741 trace
->live
= false;
2744 char errbuf
[BUFSIZ
];
2746 out_error_sched_stat_runtime
:
2747 tracing_path__strerror_open_tp(errno
, errbuf
, sizeof(errbuf
), "sched", "sched_stat_runtime");
2750 out_error_raw_syscalls
:
2751 tracing_path__strerror_open_tp(errno
, errbuf
, sizeof(errbuf
), "raw_syscalls", "sys_(enter|exit)");
2755 perf_evlist__strerror_mmap(evlist
, errno
, errbuf
, sizeof(errbuf
));
2759 perf_evlist__strerror_open(evlist
, errno
, errbuf
, sizeof(errbuf
));
2762 fprintf(trace
->output
, "%s\n", errbuf
);
2763 goto out_delete_evlist
;
2765 out_error_apply_filters
:
2766 fprintf(trace
->output
,
2767 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
2768 evsel
->filter
, perf_evsel__name(evsel
), errno
,
2769 strerror_r(errno
, errbuf
, sizeof(errbuf
)));
2770 goto out_delete_evlist
;
2773 fprintf(trace
->output
, "Not enough memory to run!\n");
2774 goto out_delete_evlist
;
2777 fprintf(trace
->output
, "errno=%d,%s\n", errno
, strerror(errno
));
2778 goto out_delete_evlist
;
2781 static int trace__replay(struct trace
*trace
)
2783 const struct perf_evsel_str_handler handlers
[] = {
2784 { "probe:vfs_getname", trace__vfs_getname
, },
2786 struct perf_data_file file
= {
2788 .mode
= PERF_DATA_MODE_READ
,
2789 .force
= trace
->force
,
2791 struct perf_session
*session
;
2792 struct perf_evsel
*evsel
;
2795 trace
->tool
.sample
= trace__process_sample
;
2796 trace
->tool
.mmap
= perf_event__process_mmap
;
2797 trace
->tool
.mmap2
= perf_event__process_mmap2
;
2798 trace
->tool
.comm
= perf_event__process_comm
;
2799 trace
->tool
.exit
= perf_event__process_exit
;
2800 trace
->tool
.fork
= perf_event__process_fork
;
2801 trace
->tool
.attr
= perf_event__process_attr
;
2802 trace
->tool
.tracing_data
= perf_event__process_tracing_data
;
2803 trace
->tool
.build_id
= perf_event__process_build_id
;
2805 trace
->tool
.ordered_events
= true;
2806 trace
->tool
.ordering_requires_timestamps
= true;
2808 /* add tid to output */
2809 trace
->multiple_threads
= true;
2811 session
= perf_session__new(&file
, false, &trace
->tool
);
2812 if (session
== NULL
)
2815 if (symbol__init(&session
->header
.env
) < 0)
2818 trace
->host
= &session
->machines
.host
;
2820 err
= perf_session__set_tracepoints_handlers(session
, handlers
);
2824 evsel
= perf_evlist__find_tracepoint_by_name(session
->evlist
,
2825 "raw_syscalls:sys_enter");
2826 /* older kernels have syscalls tp versus raw_syscalls */
2828 evsel
= perf_evlist__find_tracepoint_by_name(session
->evlist
,
2829 "syscalls:sys_enter");
2832 (perf_evsel__init_syscall_tp(evsel
, trace__sys_enter
) < 0 ||
2833 perf_evsel__init_sc_tp_ptr_field(evsel
, args
))) {
2834 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
2838 evsel
= perf_evlist__find_tracepoint_by_name(session
->evlist
,
2839 "raw_syscalls:sys_exit");
2841 evsel
= perf_evlist__find_tracepoint_by_name(session
->evlist
,
2842 "syscalls:sys_exit");
2844 (perf_evsel__init_syscall_tp(evsel
, trace__sys_exit
) < 0 ||
2845 perf_evsel__init_sc_tp_uint_field(evsel
, ret
))) {
2846 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
2850 evlist__for_each(session
->evlist
, evsel
) {
2851 if (evsel
->attr
.type
== PERF_TYPE_SOFTWARE
&&
2852 (evsel
->attr
.config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
||
2853 evsel
->attr
.config
== PERF_COUNT_SW_PAGE_FAULTS_MIN
||
2854 evsel
->attr
.config
== PERF_COUNT_SW_PAGE_FAULTS
))
2855 evsel
->handler
= trace__pgfault
;
2858 err
= parse_target_str(trace
);
2864 err
= perf_session__process_events(session
);
2866 pr_err("Failed to process events, error %d", err
);
2868 else if (trace
->summary
)
2869 trace__fprintf_thread_summary(trace
, trace
->output
);
2872 perf_session__delete(session
);
2877 static size_t trace__fprintf_threads_header(FILE *fp
)
2881 printed
= fprintf(fp
, "\n Summary of events:\n\n");
2886 static size_t thread__dump_stats(struct thread_trace
*ttrace
,
2887 struct trace
*trace
, FILE *fp
)
2889 struct stats
*stats
;
2892 struct int_node
*inode
= intlist__first(ttrace
->syscall_stats
);
2897 printed
+= fprintf(fp
, "\n");
2899 printed
+= fprintf(fp
, " syscall calls total min avg max stddev\n");
2900 printed
+= fprintf(fp
, " (msec) (msec) (msec) (msec) (%%)\n");
2901 printed
+= fprintf(fp
, " --------------- -------- --------- --------- --------- --------- ------\n");
2903 /* each int_node is a syscall */
2905 stats
= inode
->priv
;
2907 double min
= (double)(stats
->min
) / NSEC_PER_MSEC
;
2908 double max
= (double)(stats
->max
) / NSEC_PER_MSEC
;
2909 double avg
= avg_stats(stats
);
2911 u64 n
= (u64
) stats
->n
;
2913 pct
= avg
? 100.0 * stddev_stats(stats
)/avg
: 0.0;
2914 avg
/= NSEC_PER_MSEC
;
2916 sc
= &trace
->syscalls
.table
[inode
->i
];
2917 printed
+= fprintf(fp
, " %-15s", sc
->name
);
2918 printed
+= fprintf(fp
, " %8" PRIu64
" %9.3f %9.3f %9.3f",
2919 n
, avg
* n
, min
, avg
);
2920 printed
+= fprintf(fp
, " %9.3f %9.2f%%\n", max
, pct
);
2923 inode
= intlist__next(inode
);
2926 printed
+= fprintf(fp
, "\n\n");
2931 /* struct used to pass data to per-thread function */
2932 struct summary_data
{
2934 struct trace
*trace
;
2938 static int trace__fprintf_one_thread(struct thread
*thread
, void *priv
)
2940 struct summary_data
*data
= priv
;
2941 FILE *fp
= data
->fp
;
2942 size_t printed
= data
->printed
;
2943 struct trace
*trace
= data
->trace
;
2944 struct thread_trace
*ttrace
= thread__priv(thread
);
2950 ratio
= (double)ttrace
->nr_events
/ trace
->nr_events
* 100.0;
2952 printed
+= fprintf(fp
, " %s (%d), ", thread__comm_str(thread
), thread
->tid
);
2953 printed
+= fprintf(fp
, "%lu events, ", ttrace
->nr_events
);
2954 printed
+= fprintf(fp
, "%.1f%%", ratio
);
2956 printed
+= fprintf(fp
, ", %lu majfaults", ttrace
->pfmaj
);
2958 printed
+= fprintf(fp
, ", %lu minfaults", ttrace
->pfmin
);
2959 printed
+= fprintf(fp
, ", %.3f msec\n", ttrace
->runtime_ms
);
2960 printed
+= thread__dump_stats(ttrace
, trace
, fp
);
2962 data
->printed
+= printed
;
2967 static size_t trace__fprintf_thread_summary(struct trace
*trace
, FILE *fp
)
2969 struct summary_data data
= {
2973 data
.printed
= trace__fprintf_threads_header(fp
);
2975 machine__for_each_thread(trace
->host
, trace__fprintf_one_thread
, &data
);
2977 return data
.printed
;
2980 static int trace__set_duration(const struct option
*opt
, const char *str
,
2981 int unset __maybe_unused
)
2983 struct trace
*trace
= opt
->value
;
2985 trace
->duration_filter
= atof(str
);
2989 static int trace__set_filter_pids(const struct option
*opt
, const char *str
,
2990 int unset __maybe_unused
)
2994 struct trace
*trace
= opt
->value
;
2996 * FIXME: introduce a intarray class, plain parse csv and create a
2997 * { int nr, int entries[] } struct...
2999 struct intlist
*list
= intlist__new(str
);
3004 i
= trace
->filter_pids
.nr
= intlist__nr_entries(list
) + 1;
3005 trace
->filter_pids
.entries
= calloc(i
, sizeof(pid_t
));
3007 if (trace
->filter_pids
.entries
== NULL
)
3010 trace
->filter_pids
.entries
[0] = getpid();
3012 for (i
= 1; i
< trace
->filter_pids
.nr
; ++i
)
3013 trace
->filter_pids
.entries
[i
] = intlist__entry(list
, i
- 1)->i
;
3015 intlist__delete(list
);
3021 static int trace__open_output(struct trace
*trace
, const char *filename
)
3025 if (!stat(filename
, &st
) && st
.st_size
) {
3026 char oldname
[PATH_MAX
];
3028 scnprintf(oldname
, sizeof(oldname
), "%s.old", filename
);
3030 rename(filename
, oldname
);
3033 trace
->output
= fopen(filename
, "w");
3035 return trace
->output
== NULL
? -errno
: 0;
3038 static int parse_pagefaults(const struct option
*opt
, const char *str
,
3039 int unset __maybe_unused
)
3041 int *trace_pgfaults
= opt
->value
;
3043 if (strcmp(str
, "all") == 0)
3044 *trace_pgfaults
|= TRACE_PFMAJ
| TRACE_PFMIN
;
3045 else if (strcmp(str
, "maj") == 0)
3046 *trace_pgfaults
|= TRACE_PFMAJ
;
3047 else if (strcmp(str
, "min") == 0)
3048 *trace_pgfaults
|= TRACE_PFMIN
;
3055 static void evlist__set_evsel_handler(struct perf_evlist
*evlist
, void *handler
)
3057 struct perf_evsel
*evsel
;
3059 evlist__for_each(evlist
, evsel
)
3060 evsel
->handler
= handler
;
3063 int cmd_trace(int argc
, const char **argv
, const char *prefix __maybe_unused
)
3065 const char *trace_usage
[] = {
3066 "perf trace [<options>] [<command>]",
3067 "perf trace [<options>] -- <command> [<options>]",
3068 "perf trace record [<options>] [<command>]",
3069 "perf trace record [<options>] -- <command> [<options>]",
3072 struct trace trace
= {
3074 .machine
= audit_detect_machine(),
3075 .open_id
= audit_name_to_syscall("open", trace
.audit
.machine
),
3085 .user_freq
= UINT_MAX
,
3086 .user_interval
= ULLONG_MAX
,
3087 .no_buffering
= true,
3088 .mmap_pages
= UINT_MAX
,
3089 .proc_map_timeout
= 500,
3093 .trace_syscalls
= true,
3095 const char *output_name
= NULL
;
3096 const char *ev_qualifier_str
= NULL
;
3097 const struct option trace_options
[] = {
3098 OPT_CALLBACK(0, "event", &trace
.evlist
, "event",
3099 "event selector. use 'perf list' to list available events",
3100 parse_events_option
),
3101 OPT_BOOLEAN(0, "comm", &trace
.show_comm
,
3102 "show the thread COMM next to its id"),
3103 OPT_BOOLEAN(0, "tool_stats", &trace
.show_tool_stats
, "show tool stats"),
3104 OPT_STRING('e', "expr", &ev_qualifier_str
, "expr", "list of syscalls to trace"),
3105 OPT_STRING('o', "output", &output_name
, "file", "output file name"),
3106 OPT_STRING('i', "input", &input_name
, "file", "Analyze events in file"),
3107 OPT_STRING('p', "pid", &trace
.opts
.target
.pid
, "pid",
3108 "trace events on existing process id"),
3109 OPT_STRING('t', "tid", &trace
.opts
.target
.tid
, "tid",
3110 "trace events on existing thread id"),
3111 OPT_CALLBACK(0, "filter-pids", &trace
, "CSV list of pids",
3112 "pids to filter (by the kernel)", trace__set_filter_pids
),
3113 OPT_BOOLEAN('a', "all-cpus", &trace
.opts
.target
.system_wide
,
3114 "system-wide collection from all CPUs"),
3115 OPT_STRING('C', "cpu", &trace
.opts
.target
.cpu_list
, "cpu",
3116 "list of cpus to monitor"),
3117 OPT_BOOLEAN(0, "no-inherit", &trace
.opts
.no_inherit
,
3118 "child tasks do not inherit counters"),
3119 OPT_CALLBACK('m', "mmap-pages", &trace
.opts
.mmap_pages
, "pages",
3120 "number of mmap data pages",
3121 perf_evlist__parse_mmap_pages
),
3122 OPT_STRING('u', "uid", &trace
.opts
.target
.uid_str
, "user",
3124 OPT_CALLBACK(0, "duration", &trace
, "float",
3125 "show only events with duration > N.M ms",
3126 trace__set_duration
),
3127 OPT_BOOLEAN(0, "sched", &trace
.sched
, "show blocking scheduler events"),
3128 OPT_INCR('v', "verbose", &verbose
, "be more verbose"),
3129 OPT_BOOLEAN('T', "time", &trace
.full_time
,
3130 "Show full timestamp, not time relative to first start"),
3131 OPT_BOOLEAN('s', "summary", &trace
.summary_only
,
3132 "Show only syscall summary with statistics"),
3133 OPT_BOOLEAN('S', "with-summary", &trace
.summary
,
3134 "Show all syscalls and summary with statistics"),
3135 OPT_CALLBACK_DEFAULT('F', "pf", &trace
.trace_pgfaults
, "all|maj|min",
3136 "Trace pagefaults", parse_pagefaults
, "maj"),
3137 OPT_BOOLEAN(0, "syscalls", &trace
.trace_syscalls
, "Trace syscalls"),
3138 OPT_BOOLEAN('f', "force", &trace
.force
, "don't complain, do it"),
3139 OPT_UINTEGER(0, "proc-map-timeout", &trace
.opts
.proc_map_timeout
,
3140 "per thread proc mmap processing timeout in ms"),
3143 const char * const trace_subcommands
[] = { "record", NULL
};
3147 signal(SIGSEGV
, sighandler_dump_stack
);
3148 signal(SIGFPE
, sighandler_dump_stack
);
3150 trace
.evlist
= perf_evlist__new();
3152 if (trace
.evlist
== NULL
) {
3153 pr_err("Not enough memory to run!\n");
3158 argc
= parse_options_subcommand(argc
, argv
, trace_options
, trace_subcommands
,
3159 trace_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
3161 if (trace
.trace_pgfaults
) {
3162 trace
.opts
.sample_address
= true;
3163 trace
.opts
.sample_time
= true;
3166 if (trace
.evlist
->nr_entries
> 0)
3167 evlist__set_evsel_handler(trace
.evlist
, trace__event_handler
);
3169 if ((argc
>= 1) && (strcmp(argv
[0], "record") == 0))
3170 return trace__record(&trace
, argc
-1, &argv
[1]);
3172 /* summary_only implies summary option, but don't overwrite summary if set */
3173 if (trace
.summary_only
)
3174 trace
.summary
= trace
.summary_only
;
3176 if (!trace
.trace_syscalls
&& !trace
.trace_pgfaults
&&
3177 trace
.evlist
->nr_entries
== 0 /* Was --events used? */) {
3178 pr_err("Please specify something to trace.\n");
3182 if (output_name
!= NULL
) {
3183 err
= trace__open_output(&trace
, output_name
);
3185 perror("failed to create output file");
3190 if (ev_qualifier_str
!= NULL
) {
3191 const char *s
= ev_qualifier_str
;
3192 struct strlist_config slist_config
= {
3193 .dirname
= system_path(STRACE_GROUPS_DIR
),
3196 trace
.not_ev_qualifier
= *s
== '!';
3197 if (trace
.not_ev_qualifier
)
3199 trace
.ev_qualifier
= strlist__new(s
, &slist_config
);
3200 if (trace
.ev_qualifier
== NULL
) {
3201 fputs("Not enough memory to parse event qualifier",
3207 err
= trace__validate_ev_qualifier(&trace
);
3212 err
= target__validate(&trace
.opts
.target
);
3214 target__strerror(&trace
.opts
.target
, err
, bf
, sizeof(bf
));
3215 fprintf(trace
.output
, "%s", bf
);
3219 err
= target__parse_uid(&trace
.opts
.target
);
3221 target__strerror(&trace
.opts
.target
, err
, bf
, sizeof(bf
));
3222 fprintf(trace
.output
, "%s", bf
);
3226 if (!argc
&& target__none(&trace
.opts
.target
))
3227 trace
.opts
.target
.system_wide
= true;
3230 err
= trace__replay(&trace
);
3232 err
= trace__run(&trace
, argc
, argv
);
3235 if (output_name
!= NULL
)
3236 fclose(trace
.output
);