perf evsel: Introduce set_filter method
[linux-block.git] / tools / perf / builtin-trace.c
CommitLineData
4e319027 1#include <traceevent/event-parse.h>
514f1c67 2#include "builtin.h"
752fde44 3#include "util/color.h"
7c304ee0 4#include "util/debug.h"
514f1c67 5#include "util/evlist.h"
752fde44 6#include "util/machine.h"
6810fc91 7#include "util/session.h"
752fde44 8#include "util/thread.h"
514f1c67 9#include "util/parse-options.h"
2ae3a312 10#include "util/strlist.h"
bdc89661 11#include "util/intlist.h"
514f1c67 12#include "util/thread_map.h"
bf2575c1 13#include "util/stat.h"
97978b3e 14#include "trace-event.h"
9aca7f17 15#include "util/parse-events.h"
514f1c67
ACM
16
17#include <libaudit.h>
18#include <stdlib.h>
ae685380 19#include <sys/mman.h>
f9da0b0c 20#include <linux/futex.h>
514f1c67 21
456857bd
IM
22/* For older distros: */
23#ifndef MAP_STACK
24# define MAP_STACK 0x20000
25#endif
26
27#ifndef MADV_HWPOISON
28# define MADV_HWPOISON 100
29#endif
30
31#ifndef MADV_MERGEABLE
32# define MADV_MERGEABLE 12
33#endif
34
35#ifndef MADV_UNMERGEABLE
36# define MADV_UNMERGEABLE 13
37#endif
38
79d26a6a
BH
39#ifndef EFD_SEMAPHORE
40# define EFD_SEMAPHORE 1
41#endif
42
c188e7ac
ACM
43#ifndef EFD_NONBLOCK
44# define EFD_NONBLOCK 00004000
45#endif
46
47#ifndef EFD_CLOEXEC
48# define EFD_CLOEXEC 02000000
49#endif
50
51#ifndef O_CLOEXEC
52# define O_CLOEXEC 02000000
53#endif
54
55#ifndef SOCK_DCCP
56# define SOCK_DCCP 6
57#endif
58
59#ifndef SOCK_CLOEXEC
60# define SOCK_CLOEXEC 02000000
61#endif
62
63#ifndef SOCK_NONBLOCK
64# define SOCK_NONBLOCK 00004000
65#endif
66
67#ifndef MSG_CMSG_CLOEXEC
68# define MSG_CMSG_CLOEXEC 0x40000000
69#endif
70
a1c2552d
ACM
71#ifndef PERF_FLAG_FD_NO_GROUP
72# define PERF_FLAG_FD_NO_GROUP (1UL << 0)
73#endif
74
75#ifndef PERF_FLAG_FD_OUTPUT
76# define PERF_FLAG_FD_OUTPUT (1UL << 1)
77#endif
78
79#ifndef PERF_FLAG_PID_CGROUP
80# define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */
81#endif
82
83#ifndef PERF_FLAG_FD_CLOEXEC
84# define PERF_FLAG_FD_CLOEXEC (1UL << 3) /* O_CLOEXEC */
85#endif
86
87
77170988
ACM
88struct tp_field {
89 int offset;
90 union {
91 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
92 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
93 };
94};
95
96#define TP_UINT_FIELD(bits) \
97static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
98{ \
55d43bca
DA
99 u##bits value; \
100 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
101 return value; \
77170988
ACM
102}
103
104TP_UINT_FIELD(8);
105TP_UINT_FIELD(16);
106TP_UINT_FIELD(32);
107TP_UINT_FIELD(64);
108
109#define TP_UINT_FIELD__SWAPPED(bits) \
110static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
111{ \
55d43bca
DA
112 u##bits value; \
113 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
77170988
ACM
114 return bswap_##bits(value);\
115}
116
117TP_UINT_FIELD__SWAPPED(16);
118TP_UINT_FIELD__SWAPPED(32);
119TP_UINT_FIELD__SWAPPED(64);
120
121static int tp_field__init_uint(struct tp_field *field,
122 struct format_field *format_field,
123 bool needs_swap)
124{
125 field->offset = format_field->offset;
126
127 switch (format_field->size) {
128 case 1:
129 field->integer = tp_field__u8;
130 break;
131 case 2:
132 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
133 break;
134 case 4:
135 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
136 break;
137 case 8:
138 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
139 break;
140 default:
141 return -1;
142 }
143
144 return 0;
145}
146
147static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
148{
149 return sample->raw_data + field->offset;
150}
151
152static int tp_field__init_ptr(struct tp_field *field, struct format_field *format_field)
153{
154 field->offset = format_field->offset;
155 field->pointer = tp_field__ptr;
156 return 0;
157}
158
159struct syscall_tp {
160 struct tp_field id;
161 union {
162 struct tp_field args, ret;
163 };
164};
165
166static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
167 struct tp_field *field,
168 const char *name)
169{
170 struct format_field *format_field = perf_evsel__field(evsel, name);
171
172 if (format_field == NULL)
173 return -1;
174
175 return tp_field__init_uint(field, format_field, evsel->needs_swap);
176}
177
178#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
179 ({ struct syscall_tp *sc = evsel->priv;\
180 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
181
182static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
183 struct tp_field *field,
184 const char *name)
185{
186 struct format_field *format_field = perf_evsel__field(evsel, name);
187
188 if (format_field == NULL)
189 return -1;
190
191 return tp_field__init_ptr(field, format_field);
192}
193
194#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
195 ({ struct syscall_tp *sc = evsel->priv;\
196 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
197
198static void perf_evsel__delete_priv(struct perf_evsel *evsel)
199{
04662523 200 zfree(&evsel->priv);
77170988
ACM
201 perf_evsel__delete(evsel);
202}
203
96695d44
NK
204static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel, void *handler)
205{
206 evsel->priv = malloc(sizeof(struct syscall_tp));
207 if (evsel->priv != NULL) {
208 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
209 goto out_delete;
210
211 evsel->handler = handler;
212 return 0;
213 }
214
215 return -ENOMEM;
216
217out_delete:
04662523 218 zfree(&evsel->priv);
96695d44
NK
219 return -ENOENT;
220}
221
ef503831 222static struct perf_evsel *perf_evsel__syscall_newtp(const char *direction, void *handler)
77170988 223{
ef503831 224 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
77170988 225
9aca7f17
DA
226 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
227 if (evsel == NULL)
228 evsel = perf_evsel__newtp("syscalls", direction);
229
77170988 230 if (evsel) {
96695d44 231 if (perf_evsel__init_syscall_tp(evsel, handler))
77170988 232 goto out_delete;
77170988
ACM
233 }
234
235 return evsel;
236
237out_delete:
238 perf_evsel__delete_priv(evsel);
239 return NULL;
240}
241
242#define perf_evsel__sc_tp_uint(evsel, name, sample) \
243 ({ struct syscall_tp *fields = evsel->priv; \
244 fields->name.integer(&fields->name, sample); })
245
246#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
247 ({ struct syscall_tp *fields = evsel->priv; \
248 fields->name.pointer(&fields->name, sample); })
249
01533e97
ACM
250struct syscall_arg {
251 unsigned long val;
75b757ca
ACM
252 struct thread *thread;
253 struct trace *trace;
1f115cb7 254 void *parm;
01533e97
ACM
255 u8 idx;
256 u8 mask;
257};
258
1f115cb7 259struct strarray {
03e3adc9 260 int offset;
1f115cb7
ACM
261 int nr_entries;
262 const char **entries;
263};
264
265#define DEFINE_STRARRAY(array) struct strarray strarray__##array = { \
266 .nr_entries = ARRAY_SIZE(array), \
267 .entries = array, \
268}
269
03e3adc9
ACM
270#define DEFINE_STRARRAY_OFFSET(array, off) struct strarray strarray__##array = { \
271 .offset = off, \
272 .nr_entries = ARRAY_SIZE(array), \
273 .entries = array, \
274}
275
975b7c2f
ACM
276static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
277 const char *intfmt,
278 struct syscall_arg *arg)
1f115cb7 279{
1f115cb7 280 struct strarray *sa = arg->parm;
03e3adc9 281 int idx = arg->val - sa->offset;
1f115cb7
ACM
282
283 if (idx < 0 || idx >= sa->nr_entries)
975b7c2f 284 return scnprintf(bf, size, intfmt, arg->val);
1f115cb7
ACM
285
286 return scnprintf(bf, size, "%s", sa->entries[idx]);
287}
288
975b7c2f
ACM
289static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
290 struct syscall_arg *arg)
291{
292 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
293}
294
1f115cb7
ACM
295#define SCA_STRARRAY syscall_arg__scnprintf_strarray
296
844ae5b4
ACM
297#if defined(__i386__) || defined(__x86_64__)
298/*
299 * FIXME: Make this available to all arches as soon as the ioctl beautifier
300 * gets rewritten to support all arches.
301 */
78645cf3
ACM
302static size_t syscall_arg__scnprintf_strhexarray(char *bf, size_t size,
303 struct syscall_arg *arg)
304{
305 return __syscall_arg__scnprintf_strarray(bf, size, "%#x", arg);
306}
307
308#define SCA_STRHEXARRAY syscall_arg__scnprintf_strhexarray
844ae5b4 309#endif /* defined(__i386__) || defined(__x86_64__) */
78645cf3 310
75b757ca
ACM
311static size_t syscall_arg__scnprintf_fd(char *bf, size_t size,
312 struct syscall_arg *arg);
313
314#define SCA_FD syscall_arg__scnprintf_fd
315
316static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
317 struct syscall_arg *arg)
318{
319 int fd = arg->val;
320
321 if (fd == AT_FDCWD)
322 return scnprintf(bf, size, "CWD");
323
324 return syscall_arg__scnprintf_fd(bf, size, arg);
325}
326
327#define SCA_FDAT syscall_arg__scnprintf_fd_at
328
329static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
330 struct syscall_arg *arg);
331
332#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
333
6e7eeb51 334static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
01533e97 335 struct syscall_arg *arg)
13d4ff3e 336{
01533e97 337 return scnprintf(bf, size, "%#lx", arg->val);
13d4ff3e
ACM
338}
339
beccb2b5
ACM
340#define SCA_HEX syscall_arg__scnprintf_hex
341
a1c2552d
ACM
342static size_t syscall_arg__scnprintf_int(char *bf, size_t size,
343 struct syscall_arg *arg)
344{
345 return scnprintf(bf, size, "%d", arg->val);
346}
347
348#define SCA_INT syscall_arg__scnprintf_int
349
6e7eeb51 350static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
01533e97 351 struct syscall_arg *arg)
ae685380 352{
01533e97 353 int printed = 0, prot = arg->val;
ae685380
ACM
354
355 if (prot == PROT_NONE)
356 return scnprintf(bf, size, "NONE");
357#define P_MMAP_PROT(n) \
358 if (prot & PROT_##n) { \
359 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
360 prot &= ~PROT_##n; \
361 }
362
363 P_MMAP_PROT(EXEC);
364 P_MMAP_PROT(READ);
365 P_MMAP_PROT(WRITE);
366#ifdef PROT_SEM
367 P_MMAP_PROT(SEM);
368#endif
369 P_MMAP_PROT(GROWSDOWN);
370 P_MMAP_PROT(GROWSUP);
371#undef P_MMAP_PROT
372
373 if (prot)
374 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
375
376 return printed;
377}
378
379#define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
380
6e7eeb51 381static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
01533e97 382 struct syscall_arg *arg)
941557e0 383{
01533e97 384 int printed = 0, flags = arg->val;
941557e0
ACM
385
386#define P_MMAP_FLAG(n) \
387 if (flags & MAP_##n) { \
388 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
389 flags &= ~MAP_##n; \
390 }
391
392 P_MMAP_FLAG(SHARED);
393 P_MMAP_FLAG(PRIVATE);
41817815 394#ifdef MAP_32BIT
941557e0 395 P_MMAP_FLAG(32BIT);
41817815 396#endif
941557e0
ACM
397 P_MMAP_FLAG(ANONYMOUS);
398 P_MMAP_FLAG(DENYWRITE);
399 P_MMAP_FLAG(EXECUTABLE);
400 P_MMAP_FLAG(FILE);
401 P_MMAP_FLAG(FIXED);
402 P_MMAP_FLAG(GROWSDOWN);
f2935f3e 403#ifdef MAP_HUGETLB
941557e0 404 P_MMAP_FLAG(HUGETLB);
f2935f3e 405#endif
941557e0
ACM
406 P_MMAP_FLAG(LOCKED);
407 P_MMAP_FLAG(NONBLOCK);
408 P_MMAP_FLAG(NORESERVE);
409 P_MMAP_FLAG(POPULATE);
410 P_MMAP_FLAG(STACK);
411#ifdef MAP_UNINITIALIZED
412 P_MMAP_FLAG(UNINITIALIZED);
413#endif
414#undef P_MMAP_FLAG
415
416 if (flags)
417 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
418
419 return printed;
420}
421
422#define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
423
86998dda
AS
424static size_t syscall_arg__scnprintf_mremap_flags(char *bf, size_t size,
425 struct syscall_arg *arg)
426{
427 int printed = 0, flags = arg->val;
428
429#define P_MREMAP_FLAG(n) \
430 if (flags & MREMAP_##n) { \
431 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
432 flags &= ~MREMAP_##n; \
433 }
434
435 P_MREMAP_FLAG(MAYMOVE);
436#ifdef MREMAP_FIXED
437 P_MREMAP_FLAG(FIXED);
438#endif
439#undef P_MREMAP_FLAG
440
441 if (flags)
442 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
443
444 return printed;
445}
446
447#define SCA_MREMAP_FLAGS syscall_arg__scnprintf_mremap_flags
448
6e7eeb51 449static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
01533e97 450 struct syscall_arg *arg)
9e9716d1 451{
01533e97 452 int behavior = arg->val;
9e9716d1
ACM
453
454 switch (behavior) {
455#define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
456 P_MADV_BHV(NORMAL);
457 P_MADV_BHV(RANDOM);
458 P_MADV_BHV(SEQUENTIAL);
459 P_MADV_BHV(WILLNEED);
460 P_MADV_BHV(DONTNEED);
461 P_MADV_BHV(REMOVE);
462 P_MADV_BHV(DONTFORK);
463 P_MADV_BHV(DOFORK);
464 P_MADV_BHV(HWPOISON);
465#ifdef MADV_SOFT_OFFLINE
466 P_MADV_BHV(SOFT_OFFLINE);
467#endif
468 P_MADV_BHV(MERGEABLE);
469 P_MADV_BHV(UNMERGEABLE);
f2935f3e 470#ifdef MADV_HUGEPAGE
9e9716d1 471 P_MADV_BHV(HUGEPAGE);
f2935f3e
DA
472#endif
473#ifdef MADV_NOHUGEPAGE
9e9716d1 474 P_MADV_BHV(NOHUGEPAGE);
f2935f3e 475#endif
9e9716d1
ACM
476#ifdef MADV_DONTDUMP
477 P_MADV_BHV(DONTDUMP);
478#endif
479#ifdef MADV_DODUMP
480 P_MADV_BHV(DODUMP);
481#endif
482#undef P_MADV_PHV
483 default: break;
484 }
485
486 return scnprintf(bf, size, "%#x", behavior);
487}
488
489#define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
490
5cea6ff2
ACM
491static size_t syscall_arg__scnprintf_flock(char *bf, size_t size,
492 struct syscall_arg *arg)
493{
494 int printed = 0, op = arg->val;
495
496 if (op == 0)
497 return scnprintf(bf, size, "NONE");
498#define P_CMD(cmd) \
499 if ((op & LOCK_##cmd) == LOCK_##cmd) { \
500 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #cmd); \
501 op &= ~LOCK_##cmd; \
502 }
503
504 P_CMD(SH);
505 P_CMD(EX);
506 P_CMD(NB);
507 P_CMD(UN);
508 P_CMD(MAND);
509 P_CMD(RW);
510 P_CMD(READ);
511 P_CMD(WRITE);
512#undef P_OP
513
514 if (op)
515 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", op);
516
517 return printed;
518}
519
520#define SCA_FLOCK syscall_arg__scnprintf_flock
521
01533e97 522static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, struct syscall_arg *arg)
f9da0b0c
ACM
523{
524 enum syscall_futex_args {
525 SCF_UADDR = (1 << 0),
526 SCF_OP = (1 << 1),
527 SCF_VAL = (1 << 2),
528 SCF_TIMEOUT = (1 << 3),
529 SCF_UADDR2 = (1 << 4),
530 SCF_VAL3 = (1 << 5),
531 };
01533e97 532 int op = arg->val;
f9da0b0c
ACM
533 int cmd = op & FUTEX_CMD_MASK;
534 size_t printed = 0;
535
536 switch (cmd) {
537#define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
01533e97
ACM
538 P_FUTEX_OP(WAIT); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
539 P_FUTEX_OP(WAKE); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
540 P_FUTEX_OP(FD); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
541 P_FUTEX_OP(REQUEUE); arg->mask |= SCF_VAL3|SCF_TIMEOUT; break;
542 P_FUTEX_OP(CMP_REQUEUE); arg->mask |= SCF_TIMEOUT; break;
543 P_FUTEX_OP(CMP_REQUEUE_PI); arg->mask |= SCF_TIMEOUT; break;
f9da0b0c 544 P_FUTEX_OP(WAKE_OP); break;
01533e97
ACM
545 P_FUTEX_OP(LOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
546 P_FUTEX_OP(UNLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
547 P_FUTEX_OP(TRYLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
548 P_FUTEX_OP(WAIT_BITSET); arg->mask |= SCF_UADDR2; break;
549 P_FUTEX_OP(WAKE_BITSET); arg->mask |= SCF_UADDR2; break;
f9da0b0c
ACM
550 P_FUTEX_OP(WAIT_REQUEUE_PI); break;
551 default: printed = scnprintf(bf, size, "%#x", cmd); break;
552 }
553
554 if (op & FUTEX_PRIVATE_FLAG)
555 printed += scnprintf(bf + printed, size - printed, "|PRIV");
556
557 if (op & FUTEX_CLOCK_REALTIME)
558 printed += scnprintf(bf + printed, size - printed, "|CLKRT");
559
560 return printed;
561}
562
efe6b882
ACM
563#define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
564
03e3adc9
ACM
565static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
566static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1);
eac032c5 567
1f115cb7
ACM
568static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
569static DEFINE_STRARRAY(itimers);
570
efe6b882
ACM
571static const char *whences[] = { "SET", "CUR", "END",
572#ifdef SEEK_DATA
573"DATA",
574#endif
575#ifdef SEEK_HOLE
576"HOLE",
577#endif
578};
579static DEFINE_STRARRAY(whences);
f9da0b0c 580
80f587d5
ACM
581static const char *fcntl_cmds[] = {
582 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
583 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "F_GETLK64",
584 "F_SETLK64", "F_SETLKW64", "F_SETOWN_EX", "F_GETOWN_EX",
585 "F_GETOWNER_UIDS",
586};
587static DEFINE_STRARRAY(fcntl_cmds);
588
c045bf02
ACM
589static const char *rlimit_resources[] = {
590 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
591 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
592 "RTTIME",
593};
594static DEFINE_STRARRAY(rlimit_resources);
595
eb5b1b14
ACM
596static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
597static DEFINE_STRARRAY(sighow);
598
4f8c1b74
DA
599static const char *clockid[] = {
600 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
601 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE",
602};
603static DEFINE_STRARRAY(clockid);
604
e10bce81
ACM
605static const char *socket_families[] = {
606 "UNSPEC", "LOCAL", "INET", "AX25", "IPX", "APPLETALK", "NETROM",
607 "BRIDGE", "ATMPVC", "X25", "INET6", "ROSE", "DECnet", "NETBEUI",
608 "SECURITY", "KEY", "NETLINK", "PACKET", "ASH", "ECONET", "ATMSVC",
609 "RDS", "SNA", "IRDA", "PPPOX", "WANPIPE", "LLC", "IB", "CAN", "TIPC",
610 "BLUETOOTH", "IUCV", "RXRPC", "ISDN", "PHONET", "IEEE802154", "CAIF",
611 "ALG", "NFC", "VSOCK",
612};
613static DEFINE_STRARRAY(socket_families);
614
a28b24b2
ACM
615#ifndef SOCK_TYPE_MASK
616#define SOCK_TYPE_MASK 0xf
617#endif
618
619static size_t syscall_arg__scnprintf_socket_type(char *bf, size_t size,
620 struct syscall_arg *arg)
621{
622 size_t printed;
623 int type = arg->val,
624 flags = type & ~SOCK_TYPE_MASK;
625
626 type &= SOCK_TYPE_MASK;
627 /*
628 * Can't use a strarray, MIPS may override for ABI reasons.
629 */
630 switch (type) {
631#define P_SK_TYPE(n) case SOCK_##n: printed = scnprintf(bf, size, #n); break;
632 P_SK_TYPE(STREAM);
633 P_SK_TYPE(DGRAM);
634 P_SK_TYPE(RAW);
635 P_SK_TYPE(RDM);
636 P_SK_TYPE(SEQPACKET);
637 P_SK_TYPE(DCCP);
638 P_SK_TYPE(PACKET);
639#undef P_SK_TYPE
640 default:
641 printed = scnprintf(bf, size, "%#x", type);
642 }
643
644#define P_SK_FLAG(n) \
645 if (flags & SOCK_##n) { \
646 printed += scnprintf(bf + printed, size - printed, "|%s", #n); \
647 flags &= ~SOCK_##n; \
648 }
649
650 P_SK_FLAG(CLOEXEC);
651 P_SK_FLAG(NONBLOCK);
652#undef P_SK_FLAG
653
654 if (flags)
655 printed += scnprintf(bf + printed, size - printed, "|%#x", flags);
656
657 return printed;
658}
659
660#define SCA_SK_TYPE syscall_arg__scnprintf_socket_type
661
b2cc99fd
ACM
662#ifndef MSG_PROBE
663#define MSG_PROBE 0x10
664#endif
b6e8f8f4
DA
665#ifndef MSG_WAITFORONE
666#define MSG_WAITFORONE 0x10000
667#endif
b2cc99fd
ACM
668#ifndef MSG_SENDPAGE_NOTLAST
669#define MSG_SENDPAGE_NOTLAST 0x20000
670#endif
671#ifndef MSG_FASTOPEN
672#define MSG_FASTOPEN 0x20000000
673#endif
674
675static size_t syscall_arg__scnprintf_msg_flags(char *bf, size_t size,
676 struct syscall_arg *arg)
677{
678 int printed = 0, flags = arg->val;
679
680 if (flags == 0)
681 return scnprintf(bf, size, "NONE");
682#define P_MSG_FLAG(n) \
683 if (flags & MSG_##n) { \
684 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
685 flags &= ~MSG_##n; \
686 }
687
688 P_MSG_FLAG(OOB);
689 P_MSG_FLAG(PEEK);
690 P_MSG_FLAG(DONTROUTE);
691 P_MSG_FLAG(TRYHARD);
692 P_MSG_FLAG(CTRUNC);
693 P_MSG_FLAG(PROBE);
694 P_MSG_FLAG(TRUNC);
695 P_MSG_FLAG(DONTWAIT);
696 P_MSG_FLAG(EOR);
697 P_MSG_FLAG(WAITALL);
698 P_MSG_FLAG(FIN);
699 P_MSG_FLAG(SYN);
700 P_MSG_FLAG(CONFIRM);
701 P_MSG_FLAG(RST);
702 P_MSG_FLAG(ERRQUEUE);
703 P_MSG_FLAG(NOSIGNAL);
704 P_MSG_FLAG(MORE);
705 P_MSG_FLAG(WAITFORONE);
706 P_MSG_FLAG(SENDPAGE_NOTLAST);
707 P_MSG_FLAG(FASTOPEN);
708 P_MSG_FLAG(CMSG_CLOEXEC);
709#undef P_MSG_FLAG
710
711 if (flags)
712 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
713
714 return printed;
715}
716
717#define SCA_MSG_FLAGS syscall_arg__scnprintf_msg_flags
718
51108999
ACM
719static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
720 struct syscall_arg *arg)
721{
722 size_t printed = 0;
723 int mode = arg->val;
724
725 if (mode == F_OK) /* 0 */
726 return scnprintf(bf, size, "F");
727#define P_MODE(n) \
728 if (mode & n##_OK) { \
729 printed += scnprintf(bf + printed, size - printed, "%s", #n); \
730 mode &= ~n##_OK; \
731 }
732
733 P_MODE(R);
734 P_MODE(W);
735 P_MODE(X);
736#undef P_MODE
737
738 if (mode)
739 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
740
741 return printed;
742}
743
744#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
745
be65a89a 746static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size,
01533e97 747 struct syscall_arg *arg)
be65a89a 748{
01533e97 749 int printed = 0, flags = arg->val;
be65a89a
ACM
750
751 if (!(flags & O_CREAT))
01533e97 752 arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */
be65a89a
ACM
753
754 if (flags == 0)
755 return scnprintf(bf, size, "RDONLY");
756#define P_FLAG(n) \
757 if (flags & O_##n) { \
758 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
759 flags &= ~O_##n; \
760 }
761
762 P_FLAG(APPEND);
763 P_FLAG(ASYNC);
764 P_FLAG(CLOEXEC);
765 P_FLAG(CREAT);
766 P_FLAG(DIRECT);
767 P_FLAG(DIRECTORY);
768 P_FLAG(EXCL);
769 P_FLAG(LARGEFILE);
770 P_FLAG(NOATIME);
771 P_FLAG(NOCTTY);
772#ifdef O_NONBLOCK
773 P_FLAG(NONBLOCK);
774#elif O_NDELAY
775 P_FLAG(NDELAY);
776#endif
777#ifdef O_PATH
778 P_FLAG(PATH);
779#endif
780 P_FLAG(RDWR);
781#ifdef O_DSYNC
782 if ((flags & O_SYNC) == O_SYNC)
783 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC");
784 else {
785 P_FLAG(DSYNC);
786 }
787#else
788 P_FLAG(SYNC);
789#endif
790 P_FLAG(TRUNC);
791 P_FLAG(WRONLY);
792#undef P_FLAG
793
794 if (flags)
795 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
796
797 return printed;
798}
799
800#define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags
801
a1c2552d
ACM
802static size_t syscall_arg__scnprintf_perf_flags(char *bf, size_t size,
803 struct syscall_arg *arg)
804{
805 int printed = 0, flags = arg->val;
806
807 if (flags == 0)
808 return 0;
809
810#define P_FLAG(n) \
811 if (flags & PERF_FLAG_##n) { \
812 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
813 flags &= ~PERF_FLAG_##n; \
814 }
815
816 P_FLAG(FD_NO_GROUP);
817 P_FLAG(FD_OUTPUT);
818 P_FLAG(PID_CGROUP);
819 P_FLAG(FD_CLOEXEC);
820#undef P_FLAG
821
822 if (flags)
823 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
824
825 return printed;
826}
827
828#define SCA_PERF_FLAGS syscall_arg__scnprintf_perf_flags
829
49af9e93
ACM
830static size_t syscall_arg__scnprintf_eventfd_flags(char *bf, size_t size,
831 struct syscall_arg *arg)
832{
833 int printed = 0, flags = arg->val;
834
835 if (flags == 0)
836 return scnprintf(bf, size, "NONE");
837#define P_FLAG(n) \
838 if (flags & EFD_##n) { \
839 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
840 flags &= ~EFD_##n; \
841 }
842
843 P_FLAG(SEMAPHORE);
844 P_FLAG(CLOEXEC);
845 P_FLAG(NONBLOCK);
846#undef P_FLAG
847
848 if (flags)
849 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
850
851 return printed;
852}
853
854#define SCA_EFD_FLAGS syscall_arg__scnprintf_eventfd_flags
855
46cce19b
ACM
856static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
857 struct syscall_arg *arg)
858{
859 int printed = 0, flags = arg->val;
860
861#define P_FLAG(n) \
862 if (flags & O_##n) { \
863 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
864 flags &= ~O_##n; \
865 }
866
867 P_FLAG(CLOEXEC);
868 P_FLAG(NONBLOCK);
869#undef P_FLAG
870
871 if (flags)
872 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
873
874 return printed;
875}
876
877#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
878
8bad5b0a
ACM
879static size_t syscall_arg__scnprintf_signum(char *bf, size_t size, struct syscall_arg *arg)
880{
881 int sig = arg->val;
882
883 switch (sig) {
884#define P_SIGNUM(n) case SIG##n: return scnprintf(bf, size, #n)
885 P_SIGNUM(HUP);
886 P_SIGNUM(INT);
887 P_SIGNUM(QUIT);
888 P_SIGNUM(ILL);
889 P_SIGNUM(TRAP);
890 P_SIGNUM(ABRT);
891 P_SIGNUM(BUS);
892 P_SIGNUM(FPE);
893 P_SIGNUM(KILL);
894 P_SIGNUM(USR1);
895 P_SIGNUM(SEGV);
896 P_SIGNUM(USR2);
897 P_SIGNUM(PIPE);
898 P_SIGNUM(ALRM);
899 P_SIGNUM(TERM);
8bad5b0a
ACM
900 P_SIGNUM(CHLD);
901 P_SIGNUM(CONT);
902 P_SIGNUM(STOP);
903 P_SIGNUM(TSTP);
904 P_SIGNUM(TTIN);
905 P_SIGNUM(TTOU);
906 P_SIGNUM(URG);
907 P_SIGNUM(XCPU);
908 P_SIGNUM(XFSZ);
909 P_SIGNUM(VTALRM);
910 P_SIGNUM(PROF);
911 P_SIGNUM(WINCH);
912 P_SIGNUM(IO);
913 P_SIGNUM(PWR);
914 P_SIGNUM(SYS);
02c5bb4a
BH
915#ifdef SIGEMT
916 P_SIGNUM(EMT);
917#endif
918#ifdef SIGSTKFLT
919 P_SIGNUM(STKFLT);
920#endif
921#ifdef SIGSWI
922 P_SIGNUM(SWI);
923#endif
8bad5b0a
ACM
924 default: break;
925 }
926
927 return scnprintf(bf, size, "%#x", sig);
928}
929
930#define SCA_SIGNUM syscall_arg__scnprintf_signum
931
844ae5b4
ACM
932#if defined(__i386__) || defined(__x86_64__)
933/*
934 * FIXME: Make this available to all arches.
935 */
78645cf3
ACM
936#define TCGETS 0x5401
937
938static const char *tioctls[] = {
939 "TCGETS", "TCSETS", "TCSETSW", "TCSETSF", "TCGETA", "TCSETA", "TCSETAW",
940 "TCSETAF", "TCSBRK", "TCXONC", "TCFLSH", "TIOCEXCL", "TIOCNXCL",
941 "TIOCSCTTY", "TIOCGPGRP", "TIOCSPGRP", "TIOCOUTQ", "TIOCSTI",
942 "TIOCGWINSZ", "TIOCSWINSZ", "TIOCMGET", "TIOCMBIS", "TIOCMBIC",
943 "TIOCMSET", "TIOCGSOFTCAR", "TIOCSSOFTCAR", "FIONREAD", "TIOCLINUX",
944 "TIOCCONS", "TIOCGSERIAL", "TIOCSSERIAL", "TIOCPKT", "FIONBIO",
945 "TIOCNOTTY", "TIOCSETD", "TIOCGETD", "TCSBRKP", [0x27] = "TIOCSBRK",
946 "TIOCCBRK", "TIOCGSID", "TCGETS2", "TCSETS2", "TCSETSW2", "TCSETSF2",
947 "TIOCGRS485", "TIOCSRS485", "TIOCGPTN", "TIOCSPTLCK",
948 "TIOCGDEV||TCGETX", "TCSETX", "TCSETXF", "TCSETXW", "TIOCSIG",
949 "TIOCVHANGUP", "TIOCGPKT", "TIOCGPTLCK", "TIOCGEXCL",
950 [0x50] = "FIONCLEX", "FIOCLEX", "FIOASYNC", "TIOCSERCONFIG",
951 "TIOCSERGWILD", "TIOCSERSWILD", "TIOCGLCKTRMIOS", "TIOCSLCKTRMIOS",
952 "TIOCSERGSTRUCT", "TIOCSERGETLSR", "TIOCSERGETMULTI", "TIOCSERSETMULTI",
953 "TIOCMIWAIT", "TIOCGICOUNT", [0x60] = "FIOQSIZE",
954};
955
956static DEFINE_STRARRAY_OFFSET(tioctls, 0x5401);
844ae5b4 957#endif /* defined(__i386__) || defined(__x86_64__) */
78645cf3 958
453350dd
ACM
959#define STRARRAY(arg, name, array) \
960 .arg_scnprintf = { [arg] = SCA_STRARRAY, }, \
961 .arg_parm = { [arg] = &strarray__##array, }
962
514f1c67
ACM
963static struct syscall_fmt {
964 const char *name;
aec1930b 965 const char *alias;
01533e97 966 size_t (*arg_scnprintf[6])(char *bf, size_t size, struct syscall_arg *arg);
1f115cb7 967 void *arg_parm[6];
514f1c67
ACM
968 bool errmsg;
969 bool timeout;
04b34729 970 bool hexret;
514f1c67 971} syscall_fmts[] = {
51108999
ACM
972 { .name = "access", .errmsg = true,
973 .arg_scnprintf = { [1] = SCA_ACCMODE, /* mode */ }, },
aec1930b 974 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
beccb2b5
ACM
975 { .name = "brk", .hexret = true,
976 .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
4f8c1b74 977 { .name = "clock_gettime", .errmsg = true, STRARRAY(0, clk_id, clockid), },
75b757ca 978 { .name = "close", .errmsg = true,
48000a1a 979 .arg_scnprintf = { [0] = SCA_CLOSE_FD, /* fd */ }, },
a14bb860 980 { .name = "connect", .errmsg = true, },
75b757ca 981 { .name = "dup", .errmsg = true,
48000a1a 982 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 983 { .name = "dup2", .errmsg = true,
48000a1a 984 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 985 { .name = "dup3", .errmsg = true,
48000a1a 986 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
453350dd 987 { .name = "epoll_ctl", .errmsg = true, STRARRAY(1, op, epoll_ctl_ops), },
49af9e93
ACM
988 { .name = "eventfd2", .errmsg = true,
989 .arg_scnprintf = { [1] = SCA_EFD_FLAGS, /* flags */ }, },
75b757ca
ACM
990 { .name = "faccessat", .errmsg = true,
991 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
992 { .name = "fadvise64", .errmsg = true,
48000a1a 993 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 994 { .name = "fallocate", .errmsg = true,
48000a1a 995 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 996 { .name = "fchdir", .errmsg = true,
48000a1a 997 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 998 { .name = "fchmod", .errmsg = true,
48000a1a 999 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1000 { .name = "fchmodat", .errmsg = true,
48000a1a 1001 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
75b757ca 1002 { .name = "fchown", .errmsg = true,
48000a1a 1003 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1004 { .name = "fchownat", .errmsg = true,
48000a1a 1005 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
75b757ca
ACM
1006 { .name = "fcntl", .errmsg = true,
1007 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1008 [1] = SCA_STRARRAY, /* cmd */ },
1009 .arg_parm = { [1] = &strarray__fcntl_cmds, /* cmd */ }, },
1010 { .name = "fdatasync", .errmsg = true,
48000a1a 1011 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
5cea6ff2 1012 { .name = "flock", .errmsg = true,
75b757ca
ACM
1013 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1014 [1] = SCA_FLOCK, /* cmd */ }, },
1015 { .name = "fsetxattr", .errmsg = true,
48000a1a 1016 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1017 { .name = "fstat", .errmsg = true, .alias = "newfstat",
48000a1a 1018 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1019 { .name = "fstatat", .errmsg = true, .alias = "newfstatat",
48000a1a 1020 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
75b757ca 1021 { .name = "fstatfs", .errmsg = true,
48000a1a 1022 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1023 { .name = "fsync", .errmsg = true,
48000a1a 1024 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1025 { .name = "ftruncate", .errmsg = true,
48000a1a 1026 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
f9da0b0c
ACM
1027 { .name = "futex", .errmsg = true,
1028 .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
75b757ca 1029 { .name = "futimesat", .errmsg = true,
48000a1a 1030 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
75b757ca 1031 { .name = "getdents", .errmsg = true,
48000a1a 1032 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1033 { .name = "getdents64", .errmsg = true,
48000a1a 1034 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
453350dd
ACM
1035 { .name = "getitimer", .errmsg = true, STRARRAY(0, which, itimers), },
1036 { .name = "getrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), },
beccb2b5 1037 { .name = "ioctl", .errmsg = true,
48000a1a 1038 .arg_scnprintf = { [0] = SCA_FD, /* fd */
844ae5b4
ACM
1039#if defined(__i386__) || defined(__x86_64__)
1040/*
1041 * FIXME: Make this available to all arches.
1042 */
78645cf3
ACM
1043 [1] = SCA_STRHEXARRAY, /* cmd */
1044 [2] = SCA_HEX, /* arg */ },
1045 .arg_parm = { [1] = &strarray__tioctls, /* cmd */ }, },
844ae5b4
ACM
1046#else
1047 [2] = SCA_HEX, /* arg */ }, },
1048#endif
8bad5b0a
ACM
1049 { .name = "kill", .errmsg = true,
1050 .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
75b757ca 1051 { .name = "linkat", .errmsg = true,
48000a1a 1052 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
75b757ca
ACM
1053 { .name = "lseek", .errmsg = true,
1054 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1055 [2] = SCA_STRARRAY, /* whence */ },
1056 .arg_parm = { [2] = &strarray__whences, /* whence */ }, },
e5959683 1057 { .name = "lstat", .errmsg = true, .alias = "newlstat", },
9e9716d1
ACM
1058 { .name = "madvise", .errmsg = true,
1059 .arg_scnprintf = { [0] = SCA_HEX, /* start */
1060 [2] = SCA_MADV_BHV, /* behavior */ }, },
75b757ca 1061 { .name = "mkdirat", .errmsg = true,
48000a1a 1062 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
75b757ca 1063 { .name = "mknodat", .errmsg = true,
48000a1a 1064 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
3d903aa7
ACM
1065 { .name = "mlock", .errmsg = true,
1066 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
1067 { .name = "mlockall", .errmsg = true,
1068 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
beccb2b5 1069 { .name = "mmap", .hexret = true,
ae685380 1070 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
941557e0 1071 [2] = SCA_MMAP_PROT, /* prot */
73faab3a
NK
1072 [3] = SCA_MMAP_FLAGS, /* flags */
1073 [4] = SCA_FD, /* fd */ }, },
beccb2b5 1074 { .name = "mprotect", .errmsg = true,
ae685380
ACM
1075 .arg_scnprintf = { [0] = SCA_HEX, /* start */
1076 [2] = SCA_MMAP_PROT, /* prot */ }, },
1077 { .name = "mremap", .hexret = true,
1078 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
86998dda 1079 [3] = SCA_MREMAP_FLAGS, /* flags */
ae685380 1080 [4] = SCA_HEX, /* new_addr */ }, },
3d903aa7
ACM
1081 { .name = "munlock", .errmsg = true,
1082 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
beccb2b5
ACM
1083 { .name = "munmap", .errmsg = true,
1084 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
75b757ca 1085 { .name = "name_to_handle_at", .errmsg = true,
48000a1a 1086 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
75b757ca 1087 { .name = "newfstatat", .errmsg = true,
48000a1a 1088 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
be65a89a
ACM
1089 { .name = "open", .errmsg = true,
1090 .arg_scnprintf = { [1] = SCA_OPEN_FLAGS, /* flags */ }, },
31cd3855 1091 { .name = "open_by_handle_at", .errmsg = true,
75b757ca
ACM
1092 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
1093 [2] = SCA_OPEN_FLAGS, /* flags */ }, },
31cd3855 1094 { .name = "openat", .errmsg = true,
75b757ca
ACM
1095 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
1096 [2] = SCA_OPEN_FLAGS, /* flags */ }, },
a1c2552d
ACM
1097 { .name = "perf_event_open", .errmsg = true,
1098 .arg_scnprintf = { [1] = SCA_INT, /* pid */
1099 [2] = SCA_INT, /* cpu */
1100 [3] = SCA_FD, /* group_fd */
1101 [4] = SCA_PERF_FLAGS, /* flags */ }, },
46cce19b
ACM
1102 { .name = "pipe2", .errmsg = true,
1103 .arg_scnprintf = { [1] = SCA_PIPE_FLAGS, /* flags */ }, },
aec1930b
ACM
1104 { .name = "poll", .errmsg = true, .timeout = true, },
1105 { .name = "ppoll", .errmsg = true, .timeout = true, },
75b757ca 1106 { .name = "pread", .errmsg = true, .alias = "pread64",
48000a1a 1107 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1108 { .name = "preadv", .errmsg = true, .alias = "pread",
48000a1a 1109 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
453350dd 1110 { .name = "prlimit64", .errmsg = true, STRARRAY(1, resource, rlimit_resources), },
75b757ca 1111 { .name = "pwrite", .errmsg = true, .alias = "pwrite64",
48000a1a 1112 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1113 { .name = "pwritev", .errmsg = true,
48000a1a 1114 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1115 { .name = "read", .errmsg = true,
48000a1a 1116 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1117 { .name = "readlinkat", .errmsg = true,
48000a1a 1118 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
75b757ca 1119 { .name = "readv", .errmsg = true,
48000a1a 1120 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
b2cc99fd
ACM
1121 { .name = "recvfrom", .errmsg = true,
1122 .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
1123 { .name = "recvmmsg", .errmsg = true,
1124 .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
1125 { .name = "recvmsg", .errmsg = true,
1126 .arg_scnprintf = { [2] = SCA_MSG_FLAGS, /* flags */ }, },
75b757ca 1127 { .name = "renameat", .errmsg = true,
48000a1a 1128 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
8bad5b0a
ACM
1129 { .name = "rt_sigaction", .errmsg = true,
1130 .arg_scnprintf = { [0] = SCA_SIGNUM, /* sig */ }, },
453350dd 1131 { .name = "rt_sigprocmask", .errmsg = true, STRARRAY(0, how, sighow), },
8bad5b0a
ACM
1132 { .name = "rt_sigqueueinfo", .errmsg = true,
1133 .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
1134 { .name = "rt_tgsigqueueinfo", .errmsg = true,
1135 .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
aec1930b 1136 { .name = "select", .errmsg = true, .timeout = true, },
b2cc99fd
ACM
1137 { .name = "sendmmsg", .errmsg = true,
1138 .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
1139 { .name = "sendmsg", .errmsg = true,
1140 .arg_scnprintf = { [2] = SCA_MSG_FLAGS, /* flags */ }, },
1141 { .name = "sendto", .errmsg = true,
1142 .arg_scnprintf = { [3] = SCA_MSG_FLAGS, /* flags */ }, },
453350dd
ACM
1143 { .name = "setitimer", .errmsg = true, STRARRAY(0, which, itimers), },
1144 { .name = "setrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), },
75b757ca 1145 { .name = "shutdown", .errmsg = true,
48000a1a 1146 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
e10bce81 1147 { .name = "socket", .errmsg = true,
a28b24b2
ACM
1148 .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
1149 [1] = SCA_SK_TYPE, /* type */ },
07120aa5
ACM
1150 .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
1151 { .name = "socketpair", .errmsg = true,
1152 .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
1153 [1] = SCA_SK_TYPE, /* type */ },
e10bce81 1154 .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
aec1930b 1155 { .name = "stat", .errmsg = true, .alias = "newstat", },
75b757ca 1156 { .name = "symlinkat", .errmsg = true,
48000a1a 1157 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
8bad5b0a
ACM
1158 { .name = "tgkill", .errmsg = true,
1159 .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
1160 { .name = "tkill", .errmsg = true,
1161 .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
e5959683 1162 { .name = "uname", .errmsg = true, .alias = "newuname", },
75b757ca
ACM
1163 { .name = "unlinkat", .errmsg = true,
1164 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
1165 { .name = "utimensat", .errmsg = true,
1166 .arg_scnprintf = { [0] = SCA_FDAT, /* dirfd */ }, },
1167 { .name = "write", .errmsg = true,
48000a1a 1168 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
75b757ca 1169 { .name = "writev", .errmsg = true,
48000a1a 1170 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
514f1c67
ACM
1171};
1172
1173static int syscall_fmt__cmp(const void *name, const void *fmtp)
1174{
1175 const struct syscall_fmt *fmt = fmtp;
1176 return strcmp(name, fmt->name);
1177}
1178
1179static struct syscall_fmt *syscall_fmt__find(const char *name)
1180{
1181 const int nmemb = ARRAY_SIZE(syscall_fmts);
1182 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
1183}
1184
1185struct syscall {
1186 struct event_format *tp_format;
f208bd8d
ACM
1187 int nr_args;
1188 struct format_field *args;
514f1c67 1189 const char *name;
2ae3a312 1190 bool filtered;
5089f20e 1191 bool is_exit;
514f1c67 1192 struct syscall_fmt *fmt;
01533e97 1193 size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
1f115cb7 1194 void **arg_parm;
514f1c67
ACM
1195};
1196
60c907ab
ACM
1197static size_t fprintf_duration(unsigned long t, FILE *fp)
1198{
1199 double duration = (double)t / NSEC_PER_MSEC;
1200 size_t printed = fprintf(fp, "(");
1201
1202 if (duration >= 1.0)
1203 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
1204 else if (duration >= 0.01)
1205 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
1206 else
1207 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
c24ff998 1208 return printed + fprintf(fp, "): ");
60c907ab
ACM
1209}
1210
752fde44
ACM
1211struct thread_trace {
1212 u64 entry_time;
1213 u64 exit_time;
1214 bool entry_pending;
efd5745e 1215 unsigned long nr_events;
a2ea67d7 1216 unsigned long pfmaj, pfmin;
752fde44 1217 char *entry_str;
1302d88e 1218 double runtime_ms;
75b757ca
ACM
1219 struct {
1220 int max;
1221 char **table;
1222 } paths;
bf2575c1
DA
1223
1224 struct intlist *syscall_stats;
752fde44
ACM
1225};
1226
1227static struct thread_trace *thread_trace__new(void)
1228{
75b757ca
ACM
1229 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
1230
1231 if (ttrace)
1232 ttrace->paths.max = -1;
1233
bf2575c1
DA
1234 ttrace->syscall_stats = intlist__new(NULL);
1235
75b757ca 1236 return ttrace;
752fde44
ACM
1237}
1238
c24ff998 1239static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
752fde44 1240{
efd5745e
ACM
1241 struct thread_trace *ttrace;
1242
752fde44
ACM
1243 if (thread == NULL)
1244 goto fail;
1245
89dceb22
NK
1246 if (thread__priv(thread) == NULL)
1247 thread__set_priv(thread, thread_trace__new());
48000a1a 1248
89dceb22 1249 if (thread__priv(thread) == NULL)
752fde44
ACM
1250 goto fail;
1251
89dceb22 1252 ttrace = thread__priv(thread);
efd5745e
ACM
1253 ++ttrace->nr_events;
1254
1255 return ttrace;
752fde44 1256fail:
c24ff998 1257 color_fprintf(fp, PERF_COLOR_RED,
752fde44
ACM
1258 "WARNING: not enough memory, dropping samples!\n");
1259 return NULL;
1260}
1261
598d02c5
SF
1262#define TRACE_PFMAJ (1 << 0)
1263#define TRACE_PFMIN (1 << 1)
1264
514f1c67 1265struct trace {
c24ff998 1266 struct perf_tool tool;
c522739d
ACM
1267 struct {
1268 int machine;
1269 int open_id;
1270 } audit;
514f1c67
ACM
1271 struct {
1272 int max;
1273 struct syscall *table;
c27366f0 1274 struct {
8b3ce757
ACM
1275 struct perf_evsel *sys_enter,
1276 *sys_exit;
c27366f0 1277 } events;
514f1c67 1278 } syscalls;
b4006796 1279 struct record_opts opts;
14a052df 1280 struct perf_evlist *evlist;
8fb598e5 1281 struct machine *host;
e596663e 1282 struct thread *current;
752fde44 1283 u64 base_time;
c24ff998 1284 FILE *output;
efd5745e 1285 unsigned long nr_events;
b059efdf 1286 struct strlist *ev_qualifier;
8b3ce757
ACM
1287 struct {
1288 size_t nr;
1289 int *entries;
1290 } ev_qualifier_ids;
c522739d 1291 const char *last_vfs_getname;
bdc89661
DA
1292 struct intlist *tid_list;
1293 struct intlist *pid_list;
f078c385
ACM
1294 struct {
1295 size_t nr;
1296 pid_t *entries;
1297 } filter_pids;
98eafce6
ACM
1298 double duration_filter;
1299 double runtime_ms;
1300 struct {
1301 u64 vfs_getname,
1302 proc_getname;
1303 } stats;
1304 bool not_ev_qualifier;
1305 bool live;
1306 bool full_time;
1302d88e 1307 bool sched;
752fde44 1308 bool multiple_threads;
bf2575c1 1309 bool summary;
fd2eabaf 1310 bool summary_only;
50c95cbd 1311 bool show_comm;
c522739d 1312 bool show_tool_stats;
e281a960 1313 bool trace_syscalls;
e366a6d8 1314 bool force;
598d02c5 1315 int trace_pgfaults;
514f1c67
ACM
1316};
1317
97119f37 1318static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
75b757ca 1319{
89dceb22 1320 struct thread_trace *ttrace = thread__priv(thread);
75b757ca
ACM
1321
1322 if (fd > ttrace->paths.max) {
1323 char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
1324
1325 if (npath == NULL)
1326 return -1;
1327
1328 if (ttrace->paths.max != -1) {
1329 memset(npath + ttrace->paths.max + 1, 0,
1330 (fd - ttrace->paths.max) * sizeof(char *));
1331 } else {
1332 memset(npath, 0, (fd + 1) * sizeof(char *));
1333 }
1334
1335 ttrace->paths.table = npath;
1336 ttrace->paths.max = fd;
1337 }
1338
1339 ttrace->paths.table[fd] = strdup(pathname);
1340
1341 return ttrace->paths.table[fd] != NULL ? 0 : -1;
1342}
1343
97119f37
ACM
1344static int thread__read_fd_path(struct thread *thread, int fd)
1345{
1346 char linkname[PATH_MAX], pathname[PATH_MAX];
1347 struct stat st;
1348 int ret;
1349
1350 if (thread->pid_ == thread->tid) {
1351 scnprintf(linkname, sizeof(linkname),
1352 "/proc/%d/fd/%d", thread->pid_, fd);
1353 } else {
1354 scnprintf(linkname, sizeof(linkname),
1355 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1356 }
1357
1358 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1359 return -1;
1360
1361 ret = readlink(linkname, pathname, sizeof(pathname));
1362
1363 if (ret < 0 || ret > st.st_size)
1364 return -1;
1365
1366 pathname[ret] = '\0';
1367 return trace__set_fd_pathname(thread, fd, pathname);
1368}
1369
c522739d
ACM
1370static const char *thread__fd_path(struct thread *thread, int fd,
1371 struct trace *trace)
75b757ca 1372{
89dceb22 1373 struct thread_trace *ttrace = thread__priv(thread);
75b757ca
ACM
1374
1375 if (ttrace == NULL)
1376 return NULL;
1377
1378 if (fd < 0)
1379 return NULL;
1380
cdcd1e6b 1381 if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
c522739d
ACM
1382 if (!trace->live)
1383 return NULL;
1384 ++trace->stats.proc_getname;
cdcd1e6b 1385 if (thread__read_fd_path(thread, fd))
c522739d
ACM
1386 return NULL;
1387 }
75b757ca
ACM
1388
1389 return ttrace->paths.table[fd];
1390}
1391
1392static size_t syscall_arg__scnprintf_fd(char *bf, size_t size,
1393 struct syscall_arg *arg)
1394{
1395 int fd = arg->val;
1396 size_t printed = scnprintf(bf, size, "%d", fd);
c522739d 1397 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
75b757ca
ACM
1398
1399 if (path)
1400 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1401
1402 return printed;
1403}
1404
1405static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1406 struct syscall_arg *arg)
1407{
1408 int fd = arg->val;
1409 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
89dceb22 1410 struct thread_trace *ttrace = thread__priv(arg->thread);
75b757ca 1411
04662523
ACM
1412 if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
1413 zfree(&ttrace->paths.table[fd]);
75b757ca
ACM
1414
1415 return printed;
1416}
1417
ae9ed035
ACM
1418static bool trace__filter_duration(struct trace *trace, double t)
1419{
1420 return t < (trace->duration_filter * NSEC_PER_MSEC);
1421}
1422
752fde44
ACM
1423static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1424{
1425 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1426
60c907ab 1427 return fprintf(fp, "%10.3f ", ts);
752fde44
ACM
1428}
1429
f15eb531 1430static bool done = false;
ba209f85 1431static bool interrupted = false;
f15eb531 1432
ba209f85 1433static void sig_handler(int sig)
f15eb531
NK
1434{
1435 done = true;
ba209f85 1436 interrupted = sig == SIGINT;
f15eb531
NK
1437}
1438
752fde44 1439static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
60c907ab 1440 u64 duration, u64 tstamp, FILE *fp)
752fde44
ACM
1441{
1442 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
60c907ab 1443 printed += fprintf_duration(duration, fp);
752fde44 1444
50c95cbd
ACM
1445 if (trace->multiple_threads) {
1446 if (trace->show_comm)
1902efe7 1447 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
38051234 1448 printed += fprintf(fp, "%d ", thread->tid);
50c95cbd 1449 }
752fde44
ACM
1450
1451 return printed;
1452}
1453
c24ff998 1454static int trace__process_event(struct trace *trace, struct machine *machine,
162f0bef 1455 union perf_event *event, struct perf_sample *sample)
752fde44
ACM
1456{
1457 int ret = 0;
1458
1459 switch (event->header.type) {
1460 case PERF_RECORD_LOST:
c24ff998 1461 color_fprintf(trace->output, PERF_COLOR_RED,
752fde44 1462 "LOST %" PRIu64 " events!\n", event->lost.lost);
162f0bef 1463 ret = machine__process_lost_event(machine, event, sample);
752fde44 1464 default:
162f0bef 1465 ret = machine__process_event(machine, event, sample);
752fde44
ACM
1466 break;
1467 }
1468
1469 return ret;
1470}
1471
c24ff998 1472static int trace__tool_process(struct perf_tool *tool,
752fde44 1473 union perf_event *event,
162f0bef 1474 struct perf_sample *sample,
752fde44
ACM
1475 struct machine *machine)
1476{
c24ff998 1477 struct trace *trace = container_of(tool, struct trace, tool);
162f0bef 1478 return trace__process_event(trace, machine, event, sample);
752fde44
ACM
1479}
1480
1481static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1482{
0a7e6d1b 1483 int err = symbol__init(NULL);
752fde44
ACM
1484
1485 if (err)
1486 return err;
1487
8fb598e5
DA
1488 trace->host = machine__new_host();
1489 if (trace->host == NULL)
1490 return -ENOMEM;
752fde44 1491
a33fbd56 1492 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
9d9cad76
KL
1493 evlist->threads, trace__tool_process, false,
1494 trace->opts.proc_map_timeout);
752fde44
ACM
1495 if (err)
1496 symbol__exit();
1497
1498 return err;
1499}
1500
13d4ff3e
ACM
1501static int syscall__set_arg_fmts(struct syscall *sc)
1502{
1503 struct format_field *field;
1504 int idx = 0;
1505
f208bd8d 1506 sc->arg_scnprintf = calloc(sc->nr_args, sizeof(void *));
13d4ff3e
ACM
1507 if (sc->arg_scnprintf == NULL)
1508 return -1;
1509
1f115cb7
ACM
1510 if (sc->fmt)
1511 sc->arg_parm = sc->fmt->arg_parm;
1512
f208bd8d 1513 for (field = sc->args; field; field = field->next) {
beccb2b5
ACM
1514 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
1515 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
1516 else if (field->flags & FIELD_IS_POINTER)
13d4ff3e
ACM
1517 sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
1518 ++idx;
1519 }
1520
1521 return 0;
1522}
1523
514f1c67
ACM
1524static int trace__read_syscall_info(struct trace *trace, int id)
1525{
1526 char tp_name[128];
1527 struct syscall *sc;
c522739d 1528 const char *name = audit_syscall_to_name(id, trace->audit.machine);
3a531260
ACM
1529
1530 if (name == NULL)
1531 return -1;
514f1c67
ACM
1532
1533 if (id > trace->syscalls.max) {
1534 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1535
1536 if (nsyscalls == NULL)
1537 return -1;
1538
1539 if (trace->syscalls.max != -1) {
1540 memset(nsyscalls + trace->syscalls.max + 1, 0,
1541 (id - trace->syscalls.max) * sizeof(*sc));
1542 } else {
1543 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1544 }
1545
1546 trace->syscalls.table = nsyscalls;
1547 trace->syscalls.max = id;
1548 }
1549
1550 sc = trace->syscalls.table + id;
3a531260 1551 sc->name = name;
2ae3a312 1552
b059efdf
ACM
1553 if (trace->ev_qualifier) {
1554 bool in = strlist__find(trace->ev_qualifier, name) != NULL;
1555
1556 if (!(in ^ trace->not_ev_qualifier)) {
1557 sc->filtered = true;
1558 /*
1559 * No need to do read tracepoint information since this will be
1560 * filtered out.
1561 */
1562 return 0;
1563 }
2ae3a312
ACM
1564 }
1565
3a531260 1566 sc->fmt = syscall_fmt__find(sc->name);
514f1c67 1567
aec1930b 1568 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
97978b3e 1569 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
aec1930b
ACM
1570
1571 if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
1572 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
97978b3e 1573 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
aec1930b 1574 }
514f1c67 1575
13d4ff3e
ACM
1576 if (sc->tp_format == NULL)
1577 return -1;
1578
f208bd8d
ACM
1579 sc->args = sc->tp_format->format.fields;
1580 sc->nr_args = sc->tp_format->format.nr_fields;
1581 /* drop nr field - not relevant here; does not exist on older kernels */
1582 if (sc->args && strcmp(sc->args->name, "nr") == 0) {
1583 sc->args = sc->args->next;
1584 --sc->nr_args;
1585 }
1586
5089f20e
ACM
1587 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
1588
13d4ff3e 1589 return syscall__set_arg_fmts(sc);
514f1c67
ACM
1590}
1591
d0cc439b
ACM
1592static int trace__validate_ev_qualifier(struct trace *trace)
1593{
8b3ce757 1594 int err = 0, i;
d0cc439b
ACM
1595 struct str_node *pos;
1596
8b3ce757
ACM
1597 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1598 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1599 sizeof(trace->ev_qualifier_ids.entries[0]));
1600
1601 if (trace->ev_qualifier_ids.entries == NULL) {
1602 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1603 trace->output);
1604 err = -EINVAL;
1605 goto out;
1606 }
1607
1608 i = 0;
1609
d0cc439b
ACM
1610 strlist__for_each(pos, trace->ev_qualifier) {
1611 const char *sc = pos->s;
8b3ce757 1612 int id = audit_name_to_syscall(sc, trace->audit.machine);
d0cc439b 1613
8b3ce757 1614 if (id < 0) {
d0cc439b
ACM
1615 if (err == 0) {
1616 fputs("Error:\tInvalid syscall ", trace->output);
1617 err = -EINVAL;
1618 } else {
1619 fputs(", ", trace->output);
1620 }
1621
1622 fputs(sc, trace->output);
1623 }
8b3ce757
ACM
1624
1625 trace->ev_qualifier_ids.entries[i++] = id;
d0cc439b
ACM
1626 }
1627
1628 if (err < 0) {
1629 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1630 "\nHint:\tand: 'man syscalls'\n", trace->output);
8b3ce757
ACM
1631 zfree(&trace->ev_qualifier_ids.entries);
1632 trace->ev_qualifier_ids.nr = 0;
d0cc439b 1633 }
8b3ce757 1634out:
d0cc439b
ACM
1635 return err;
1636}
1637
55d43bca
DA
1638/*
1639 * args is to be interpreted as a series of longs but we need to handle
1640 * 8-byte unaligned accesses. args points to raw_data within the event
1641 * and raw_data is guaranteed to be 8-byte unaligned because it is
1642 * preceded by raw_size which is a u32. So we need to copy args to a temp
1643 * variable to read it. Most notably this avoids extended load instructions
1644 * on unaligned addresses
1645 */
1646
752fde44 1647static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
55d43bca 1648 unsigned char *args, struct trace *trace,
75b757ca 1649 struct thread *thread)
514f1c67 1650{
514f1c67 1651 size_t printed = 0;
55d43bca
DA
1652 unsigned char *p;
1653 unsigned long val;
514f1c67 1654
f208bd8d 1655 if (sc->args != NULL) {
514f1c67 1656 struct format_field *field;
01533e97
ACM
1657 u8 bit = 1;
1658 struct syscall_arg arg = {
75b757ca
ACM
1659 .idx = 0,
1660 .mask = 0,
1661 .trace = trace,
1662 .thread = thread,
01533e97 1663 };
6e7eeb51 1664
f208bd8d 1665 for (field = sc->args; field;
01533e97
ACM
1666 field = field->next, ++arg.idx, bit <<= 1) {
1667 if (arg.mask & bit)
6e7eeb51 1668 continue;
55d43bca
DA
1669
1670 /* special care for unaligned accesses */
1671 p = args + sizeof(unsigned long) * arg.idx;
1672 memcpy(&val, p, sizeof(val));
1673
4aa58232
ACM
1674 /*
1675 * Suppress this argument if its value is zero and
1676 * and we don't have a string associated in an
1677 * strarray for it.
1678 */
55d43bca 1679 if (val == 0 &&
4aa58232
ACM
1680 !(sc->arg_scnprintf &&
1681 sc->arg_scnprintf[arg.idx] == SCA_STRARRAY &&
1682 sc->arg_parm[arg.idx]))
22ae5cf1
ACM
1683 continue;
1684
752fde44 1685 printed += scnprintf(bf + printed, size - printed,
13d4ff3e 1686 "%s%s: ", printed ? ", " : "", field->name);
01533e97 1687 if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
55d43bca 1688 arg.val = val;
1f115cb7
ACM
1689 if (sc->arg_parm)
1690 arg.parm = sc->arg_parm[arg.idx];
01533e97
ACM
1691 printed += sc->arg_scnprintf[arg.idx](bf + printed,
1692 size - printed, &arg);
6e7eeb51 1693 } else {
13d4ff3e 1694 printed += scnprintf(bf + printed, size - printed,
55d43bca 1695 "%ld", val);
6e7eeb51 1696 }
514f1c67
ACM
1697 }
1698 } else {
01533e97
ACM
1699 int i = 0;
1700
514f1c67 1701 while (i < 6) {
55d43bca
DA
1702 /* special care for unaligned accesses */
1703 p = args + sizeof(unsigned long) * i;
1704 memcpy(&val, p, sizeof(val));
752fde44
ACM
1705 printed += scnprintf(bf + printed, size - printed,
1706 "%sarg%d: %ld",
55d43bca 1707 printed ? ", " : "", i, val);
514f1c67
ACM
1708 ++i;
1709 }
1710 }
1711
1712 return printed;
1713}
1714
ba3d7dee 1715typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 1716 union perf_event *event,
ba3d7dee
ACM
1717 struct perf_sample *sample);
1718
1719static struct syscall *trace__syscall_info(struct trace *trace,
bf2575c1 1720 struct perf_evsel *evsel, int id)
ba3d7dee 1721{
ba3d7dee
ACM
1722
1723 if (id < 0) {
adaa18bf
ACM
1724
1725 /*
1726 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1727 * before that, leaving at a higher verbosity level till that is
1728 * explained. Reproduced with plain ftrace with:
1729 *
1730 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1731 * grep "NR -1 " /t/trace_pipe
1732 *
1733 * After generating some load on the machine.
1734 */
1735 if (verbose > 1) {
1736 static u64 n;
1737 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1738 id, perf_evsel__name(evsel), ++n);
1739 }
ba3d7dee
ACM
1740 return NULL;
1741 }
1742
1743 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1744 trace__read_syscall_info(trace, id))
1745 goto out_cant_read;
1746
1747 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1748 goto out_cant_read;
1749
1750 return &trace->syscalls.table[id];
1751
1752out_cant_read:
7c304ee0
ACM
1753 if (verbose) {
1754 fprintf(trace->output, "Problems reading syscall %d", id);
1755 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1756 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1757 fputs(" information\n", trace->output);
1758 }
ba3d7dee
ACM
1759 return NULL;
1760}
1761
bf2575c1
DA
1762static void thread__update_stats(struct thread_trace *ttrace,
1763 int id, struct perf_sample *sample)
1764{
1765 struct int_node *inode;
1766 struct stats *stats;
1767 u64 duration = 0;
1768
1769 inode = intlist__findnew(ttrace->syscall_stats, id);
1770 if (inode == NULL)
1771 return;
1772
1773 stats = inode->priv;
1774 if (stats == NULL) {
1775 stats = malloc(sizeof(struct stats));
1776 if (stats == NULL)
1777 return;
1778 init_stats(stats);
1779 inode->priv = stats;
1780 }
1781
1782 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1783 duration = sample->time - ttrace->entry_time;
1784
1785 update_stats(stats, duration);
1786}
1787
e596663e
ACM
1788static int trace__printf_interrupted_entry(struct trace *trace, struct perf_sample *sample)
1789{
1790 struct thread_trace *ttrace;
1791 u64 duration;
1792 size_t printed;
1793
1794 if (trace->current == NULL)
1795 return 0;
1796
1797 ttrace = thread__priv(trace->current);
1798
1799 if (!ttrace->entry_pending)
1800 return 0;
1801
1802 duration = sample->time - ttrace->entry_time;
1803
1804 printed = trace__fprintf_entry_head(trace, trace->current, duration, sample->time, trace->output);
1805 printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
1806 ttrace->entry_pending = false;
1807
1808 return printed;
1809}
1810
ba3d7dee 1811static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 1812 union perf_event *event __maybe_unused,
ba3d7dee
ACM
1813 struct perf_sample *sample)
1814{
752fde44 1815 char *msg;
ba3d7dee 1816 void *args;
752fde44 1817 size_t printed = 0;
2ae3a312 1818 struct thread *thread;
b91fc39f 1819 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
bf2575c1 1820 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2ae3a312
ACM
1821 struct thread_trace *ttrace;
1822
1823 if (sc == NULL)
1824 return -1;
ba3d7dee 1825
2ae3a312
ACM
1826 if (sc->filtered)
1827 return 0;
1828
8fb598e5 1829 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
c24ff998 1830 ttrace = thread__trace(thread, trace->output);
2ae3a312 1831 if (ttrace == NULL)
b91fc39f 1832 goto out_put;
ba3d7dee 1833
77170988 1834 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
752fde44
ACM
1835
1836 if (ttrace->entry_str == NULL) {
1837 ttrace->entry_str = malloc(1024);
1838 if (!ttrace->entry_str)
b91fc39f 1839 goto out_put;
752fde44
ACM
1840 }
1841
13f22a2d 1842 if (!trace->summary_only)
6ebad5c1 1843 trace__printf_interrupted_entry(trace, sample);
e596663e 1844
752fde44
ACM
1845 ttrace->entry_time = sample->time;
1846 msg = ttrace->entry_str;
1847 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
1848
75b757ca
ACM
1849 printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed,
1850 args, trace, thread);
752fde44 1851
5089f20e 1852 if (sc->is_exit) {
fd2eabaf 1853 if (!trace->duration_filter && !trace->summary_only) {
c24ff998
ACM
1854 trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
1855 fprintf(trace->output, "%-70s\n", ttrace->entry_str);
ae9ed035 1856 }
752fde44
ACM
1857 } else
1858 ttrace->entry_pending = true;
ba3d7dee 1859
f3b623b8
ACM
1860 if (trace->current != thread) {
1861 thread__put(trace->current);
1862 trace->current = thread__get(thread);
1863 }
b91fc39f
ACM
1864 err = 0;
1865out_put:
1866 thread__put(thread);
1867 return err;
ba3d7dee
ACM
1868}
1869
1870static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 1871 union perf_event *event __maybe_unused,
ba3d7dee
ACM
1872 struct perf_sample *sample)
1873{
2c82c3ad 1874 long ret;
60c907ab 1875 u64 duration = 0;
2ae3a312 1876 struct thread *thread;
b91fc39f 1877 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
bf2575c1 1878 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2ae3a312
ACM
1879 struct thread_trace *ttrace;
1880
1881 if (sc == NULL)
1882 return -1;
ba3d7dee 1883
2ae3a312
ACM
1884 if (sc->filtered)
1885 return 0;
1886
8fb598e5 1887 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
c24ff998 1888 ttrace = thread__trace(thread, trace->output);
2ae3a312 1889 if (ttrace == NULL)
b91fc39f 1890 goto out_put;
ba3d7dee 1891
bf2575c1
DA
1892 if (trace->summary)
1893 thread__update_stats(ttrace, id, sample);
1894
77170988 1895 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
ba3d7dee 1896
c522739d
ACM
1897 if (id == trace->audit.open_id && ret >= 0 && trace->last_vfs_getname) {
1898 trace__set_fd_pathname(thread, ret, trace->last_vfs_getname);
1899 trace->last_vfs_getname = NULL;
1900 ++trace->stats.vfs_getname;
1901 }
1902
752fde44
ACM
1903 ttrace->exit_time = sample->time;
1904
ae9ed035 1905 if (ttrace->entry_time) {
60c907ab 1906 duration = sample->time - ttrace->entry_time;
ae9ed035
ACM
1907 if (trace__filter_duration(trace, duration))
1908 goto out;
1909 } else if (trace->duration_filter)
1910 goto out;
60c907ab 1911
fd2eabaf
DA
1912 if (trace->summary_only)
1913 goto out;
1914
c24ff998 1915 trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
752fde44
ACM
1916
1917 if (ttrace->entry_pending) {
c24ff998 1918 fprintf(trace->output, "%-70s", ttrace->entry_str);
752fde44 1919 } else {
c24ff998
ACM
1920 fprintf(trace->output, " ... [");
1921 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
1922 fprintf(trace->output, "]: %s()", sc->name);
752fde44
ACM
1923 }
1924
da3c9a44
ACM
1925 if (sc->fmt == NULL) {
1926signed_print:
2c82c3ad 1927 fprintf(trace->output, ") = %ld", ret);
da3c9a44 1928 } else if (ret < 0 && sc->fmt->errmsg) {
942a91ed 1929 char bf[STRERR_BUFSIZE];
ba3d7dee
ACM
1930 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
1931 *e = audit_errno_to_name(-ret);
1932
c24ff998 1933 fprintf(trace->output, ") = -1 %s %s", e, emsg);
da3c9a44 1934 } else if (ret == 0 && sc->fmt->timeout)
c24ff998 1935 fprintf(trace->output, ") = 0 Timeout");
04b34729 1936 else if (sc->fmt->hexret)
2c82c3ad 1937 fprintf(trace->output, ") = %#lx", ret);
ba3d7dee 1938 else
da3c9a44 1939 goto signed_print;
ba3d7dee 1940
c24ff998 1941 fputc('\n', trace->output);
ae9ed035 1942out:
752fde44 1943 ttrace->entry_pending = false;
b91fc39f
ACM
1944 err = 0;
1945out_put:
1946 thread__put(thread);
1947 return err;
ba3d7dee
ACM
1948}
1949
c522739d 1950static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 1951 union perf_event *event __maybe_unused,
c522739d
ACM
1952 struct perf_sample *sample)
1953{
1954 trace->last_vfs_getname = perf_evsel__rawptr(evsel, sample, "pathname");
1955 return 0;
1956}
1957
1302d88e 1958static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 1959 union perf_event *event __maybe_unused,
1302d88e
ACM
1960 struct perf_sample *sample)
1961{
1962 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
1963 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
8fb598e5 1964 struct thread *thread = machine__findnew_thread(trace->host,
314add6b
AH
1965 sample->pid,
1966 sample->tid);
c24ff998 1967 struct thread_trace *ttrace = thread__trace(thread, trace->output);
1302d88e
ACM
1968
1969 if (ttrace == NULL)
1970 goto out_dump;
1971
1972 ttrace->runtime_ms += runtime_ms;
1973 trace->runtime_ms += runtime_ms;
b91fc39f 1974 thread__put(thread);
1302d88e
ACM
1975 return 0;
1976
1977out_dump:
c24ff998 1978 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
1302d88e
ACM
1979 evsel->name,
1980 perf_evsel__strval(evsel, sample, "comm"),
1981 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
1982 runtime,
1983 perf_evsel__intval(evsel, sample, "vruntime"));
b91fc39f 1984 thread__put(thread);
1302d88e
ACM
1985 return 0;
1986}
1987
14a052df
ACM
1988static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
1989 union perf_event *event __maybe_unused,
1990 struct perf_sample *sample)
1991{
1992 trace__printf_interrupted_entry(trace, sample);
1993 trace__fprintf_tstamp(trace, sample->time, trace->output);
0808921a
ACM
1994
1995 if (trace->trace_syscalls)
1996 fprintf(trace->output, "( ): ");
1997
1998 fprintf(trace->output, "%s:", evsel->name);
14a052df
ACM
1999
2000 if (evsel->tp_format) {
2001 event_format__fprintf(evsel->tp_format, sample->cpu,
2002 sample->raw_data, sample->raw_size,
2003 trace->output);
2004 }
2005
2006 fprintf(trace->output, ")\n");
2007 return 0;
2008}
2009
598d02c5
SF
2010static void print_location(FILE *f, struct perf_sample *sample,
2011 struct addr_location *al,
2012 bool print_dso, bool print_sym)
2013{
2014
2015 if ((verbose || print_dso) && al->map)
2016 fprintf(f, "%s@", al->map->dso->long_name);
2017
2018 if ((verbose || print_sym) && al->sym)
4414a3c5 2019 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
598d02c5
SF
2020 al->addr - al->sym->start);
2021 else if (al->map)
4414a3c5 2022 fprintf(f, "0x%" PRIx64, al->addr);
598d02c5 2023 else
4414a3c5 2024 fprintf(f, "0x%" PRIx64, sample->addr);
598d02c5
SF
2025}
2026
2027static int trace__pgfault(struct trace *trace,
2028 struct perf_evsel *evsel,
2029 union perf_event *event,
2030 struct perf_sample *sample)
2031{
2032 struct thread *thread;
2033 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2034 struct addr_location al;
2035 char map_type = 'd';
a2ea67d7 2036 struct thread_trace *ttrace;
b91fc39f 2037 int err = -1;
598d02c5
SF
2038
2039 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
a2ea67d7
SF
2040 ttrace = thread__trace(thread, trace->output);
2041 if (ttrace == NULL)
b91fc39f 2042 goto out_put;
a2ea67d7
SF
2043
2044 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2045 ttrace->pfmaj++;
2046 else
2047 ttrace->pfmin++;
2048
2049 if (trace->summary_only)
b91fc39f 2050 goto out;
598d02c5 2051
bb871a9c 2052 thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
598d02c5
SF
2053 sample->ip, &al);
2054
2055 trace__fprintf_entry_head(trace, thread, 0, sample->time, trace->output);
2056
2057 fprintf(trace->output, "%sfault [",
2058 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2059 "maj" : "min");
2060
2061 print_location(trace->output, sample, &al, false, true);
2062
2063 fprintf(trace->output, "] => ");
2064
bb871a9c 2065 thread__find_addr_location(thread, cpumode, MAP__VARIABLE,
598d02c5
SF
2066 sample->addr, &al);
2067
2068 if (!al.map) {
bb871a9c 2069 thread__find_addr_location(thread, cpumode,
598d02c5
SF
2070 MAP__FUNCTION, sample->addr, &al);
2071
2072 if (al.map)
2073 map_type = 'x';
2074 else
2075 map_type = '?';
2076 }
2077
2078 print_location(trace->output, sample, &al, true, false);
2079
2080 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
b91fc39f
ACM
2081out:
2082 err = 0;
2083out_put:
2084 thread__put(thread);
2085 return err;
598d02c5
SF
2086}
2087
bdc89661
DA
2088static bool skip_sample(struct trace *trace, struct perf_sample *sample)
2089{
2090 if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
2091 (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
2092 return false;
2093
2094 if (trace->pid_list || trace->tid_list)
2095 return true;
2096
2097 return false;
2098}
2099
6810fc91 2100static int trace__process_sample(struct perf_tool *tool,
0c82adcf 2101 union perf_event *event,
6810fc91
DA
2102 struct perf_sample *sample,
2103 struct perf_evsel *evsel,
2104 struct machine *machine __maybe_unused)
2105{
2106 struct trace *trace = container_of(tool, struct trace, tool);
2107 int err = 0;
2108
744a9719 2109 tracepoint_handler handler = evsel->handler;
6810fc91 2110
bdc89661
DA
2111 if (skip_sample(trace, sample))
2112 return 0;
2113
4bb09192 2114 if (!trace->full_time && trace->base_time == 0)
6810fc91
DA
2115 trace->base_time = sample->time;
2116
3160565f
DA
2117 if (handler) {
2118 ++trace->nr_events;
0c82adcf 2119 handler(trace, evsel, event, sample);
3160565f 2120 }
6810fc91
DA
2121
2122 return err;
2123}
2124
bdc89661
DA
2125static int parse_target_str(struct trace *trace)
2126{
2127 if (trace->opts.target.pid) {
2128 trace->pid_list = intlist__new(trace->opts.target.pid);
2129 if (trace->pid_list == NULL) {
2130 pr_err("Error parsing process id string\n");
2131 return -EINVAL;
2132 }
2133 }
2134
2135 if (trace->opts.target.tid) {
2136 trace->tid_list = intlist__new(trace->opts.target.tid);
2137 if (trace->tid_list == NULL) {
2138 pr_err("Error parsing thread id string\n");
2139 return -EINVAL;
2140 }
2141 }
2142
2143 return 0;
2144}
2145
1e28fe0a 2146static int trace__record(struct trace *trace, int argc, const char **argv)
5e2485b1
DA
2147{
2148 unsigned int rec_argc, i, j;
2149 const char **rec_argv;
2150 const char * const record_args[] = {
2151 "record",
2152 "-R",
2153 "-m", "1024",
2154 "-c", "1",
5e2485b1
DA
2155 };
2156
1e28fe0a
SF
2157 const char * const sc_args[] = { "-e", };
2158 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2159 const char * const majpf_args[] = { "-e", "major-faults" };
2160 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2161 const char * const minpf_args[] = { "-e", "minor-faults" };
2162 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2163
9aca7f17 2164 /* +1 is for the event string below */
1e28fe0a
SF
2165 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2166 majpf_args_nr + minpf_args_nr + argc;
5e2485b1
DA
2167 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2168
2169 if (rec_argv == NULL)
2170 return -ENOMEM;
2171
1e28fe0a 2172 j = 0;
5e2485b1 2173 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1e28fe0a
SF
2174 rec_argv[j++] = record_args[i];
2175
e281a960
SF
2176 if (trace->trace_syscalls) {
2177 for (i = 0; i < sc_args_nr; i++)
2178 rec_argv[j++] = sc_args[i];
2179
2180 /* event string may be different for older kernels - e.g., RHEL6 */
2181 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2182 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2183 else if (is_valid_tracepoint("syscalls:sys_enter"))
2184 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2185 else {
2186 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
2187 return -1;
2188 }
9aca7f17 2189 }
9aca7f17 2190
1e28fe0a
SF
2191 if (trace->trace_pgfaults & TRACE_PFMAJ)
2192 for (i = 0; i < majpf_args_nr; i++)
2193 rec_argv[j++] = majpf_args[i];
2194
2195 if (trace->trace_pgfaults & TRACE_PFMIN)
2196 for (i = 0; i < minpf_args_nr; i++)
2197 rec_argv[j++] = minpf_args[i];
2198
2199 for (i = 0; i < (unsigned int)argc; i++)
2200 rec_argv[j++] = argv[i];
5e2485b1 2201
1e28fe0a 2202 return cmd_record(j, rec_argv, NULL);
5e2485b1
DA
2203}
2204
bf2575c1
DA
2205static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2206
c522739d
ACM
2207static void perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
2208{
ef503831 2209 struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
c522739d
ACM
2210 if (evsel == NULL)
2211 return;
2212
2213 if (perf_evsel__field(evsel, "pathname") == NULL) {
2214 perf_evsel__delete(evsel);
2215 return;
2216 }
2217
744a9719 2218 evsel->handler = trace__vfs_getname;
c522739d
ACM
2219 perf_evlist__add(evlist, evsel);
2220}
2221
598d02c5
SF
2222static int perf_evlist__add_pgfault(struct perf_evlist *evlist,
2223 u64 config)
2224{
2225 struct perf_evsel *evsel;
2226 struct perf_event_attr attr = {
2227 .type = PERF_TYPE_SOFTWARE,
2228 .mmap_data = 1,
598d02c5
SF
2229 };
2230
2231 attr.config = config;
0524798c 2232 attr.sample_period = 1;
598d02c5
SF
2233
2234 event_attr_init(&attr);
2235
2236 evsel = perf_evsel__new(&attr);
2237 if (!evsel)
2238 return -ENOMEM;
2239
2240 evsel->handler = trace__pgfault;
2241 perf_evlist__add(evlist, evsel);
2242
2243 return 0;
2244}
2245
ddbb1b13
ACM
2246static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2247{
2248 const u32 type = event->header.type;
2249 struct perf_evsel *evsel;
2250
2251 if (!trace->full_time && trace->base_time == 0)
2252 trace->base_time = sample->time;
2253
2254 if (type != PERF_RECORD_SAMPLE) {
2255 trace__process_event(trace, trace->host, event, sample);
2256 return;
2257 }
2258
2259 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2260 if (evsel == NULL) {
2261 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2262 return;
2263 }
2264
2265 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2266 sample->raw_data == NULL) {
2267 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2268 perf_evsel__name(evsel), sample->tid,
2269 sample->cpu, sample->raw_size);
2270 } else {
2271 tracepoint_handler handler = evsel->handler;
2272 handler(trace, evsel, event, sample);
2273 }
2274}
2275
c27366f0
ACM
2276static int trace__add_syscall_newtp(struct trace *trace)
2277{
2278 int ret = -1;
2279 struct perf_evlist *evlist = trace->evlist;
2280 struct perf_evsel *sys_enter, *sys_exit;
2281
2282 sys_enter = perf_evsel__syscall_newtp("sys_enter", trace__sys_enter);
2283 if (sys_enter == NULL)
2284 goto out;
2285
2286 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2287 goto out_delete_sys_enter;
2288
2289 sys_exit = perf_evsel__syscall_newtp("sys_exit", trace__sys_exit);
2290 if (sys_exit == NULL)
2291 goto out_delete_sys_enter;
2292
2293 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2294 goto out_delete_sys_exit;
2295
2296 perf_evlist__add(evlist, sys_enter);
2297 perf_evlist__add(evlist, sys_exit);
2298
8b3ce757
ACM
2299 trace->syscalls.events.sys_enter = sys_enter;
2300 trace->syscalls.events.sys_exit = sys_exit;
c27366f0
ACM
2301
2302 ret = 0;
2303out:
2304 return ret;
2305
2306out_delete_sys_exit:
2307 perf_evsel__delete_priv(sys_exit);
2308out_delete_sys_enter:
2309 perf_evsel__delete_priv(sys_enter);
2310 goto out;
2311}
2312
2313
f15eb531 2314static int trace__run(struct trace *trace, int argc, const char **argv)
514f1c67 2315{
14a052df 2316 struct perf_evlist *evlist = trace->evlist;
efd5745e
ACM
2317 int err = -1, i;
2318 unsigned long before;
f15eb531 2319 const bool forks = argc > 0;
46fb3c21 2320 bool draining = false;
514f1c67 2321
75b757ca
ACM
2322 trace->live = true;
2323
c27366f0 2324 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
801c67b0 2325 goto out_error_raw_syscalls;
514f1c67 2326
e281a960
SF
2327 if (trace->trace_syscalls)
2328 perf_evlist__add_vfs_getname(evlist);
c522739d 2329
598d02c5 2330 if ((trace->trace_pgfaults & TRACE_PFMAJ) &&
e2726d99 2331 perf_evlist__add_pgfault(evlist, PERF_COUNT_SW_PAGE_FAULTS_MAJ)) {
5ed08dae 2332 goto out_error_mem;
e2726d99 2333 }
598d02c5
SF
2334
2335 if ((trace->trace_pgfaults & TRACE_PFMIN) &&
2336 perf_evlist__add_pgfault(evlist, PERF_COUNT_SW_PAGE_FAULTS_MIN))
5ed08dae 2337 goto out_error_mem;
598d02c5 2338
1302d88e 2339 if (trace->sched &&
2cc990ba
ACM
2340 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2341 trace__sched_stat_runtime))
2342 goto out_error_sched_stat_runtime;
1302d88e 2343
514f1c67
ACM
2344 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2345 if (err < 0) {
c24ff998 2346 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
514f1c67
ACM
2347 goto out_delete_evlist;
2348 }
2349
752fde44
ACM
2350 err = trace__symbols_init(trace, evlist);
2351 if (err < 0) {
c24ff998 2352 fprintf(trace->output, "Problems initializing symbol libraries!\n");
03ad9747 2353 goto out_delete_evlist;
752fde44
ACM
2354 }
2355
f77a9518 2356 perf_evlist__config(evlist, &trace->opts);
514f1c67 2357
f15eb531
NK
2358 signal(SIGCHLD, sig_handler);
2359 signal(SIGINT, sig_handler);
2360
2361 if (forks) {
6ef73ec4 2362 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
735f7e0b 2363 argv, false, NULL);
f15eb531 2364 if (err < 0) {
c24ff998 2365 fprintf(trace->output, "Couldn't run the workload!\n");
03ad9747 2366 goto out_delete_evlist;
f15eb531
NK
2367 }
2368 }
2369
514f1c67 2370 err = perf_evlist__open(evlist);
a8f23d8f
ACM
2371 if (err < 0)
2372 goto out_error_open;
514f1c67 2373
241b057c
ACM
2374 /*
2375 * Better not use !target__has_task() here because we need to cover the
2376 * case where no threads were specified in the command line, but a
2377 * workload was, and in that case we will fill in the thread_map when
2378 * we fork the workload in perf_evlist__prepare_workload.
2379 */
f078c385
ACM
2380 if (trace->filter_pids.nr > 0)
2381 err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
e13798c7 2382 else if (thread_map__pid(evlist->threads, 0) == -1)
f078c385
ACM
2383 err = perf_evlist__set_filter_pid(evlist, getpid());
2384
2385 if (err < 0) {
2386 printf("err=%d,%s\n", -err, strerror(-err));
2387 exit(1);
2388 }
241b057c 2389
f885037e 2390 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages, false);
e09b18d4
ACM
2391 if (err < 0)
2392 goto out_error_mmap;
514f1c67 2393
cb24d01d
ACM
2394 if (!target__none(&trace->opts.target))
2395 perf_evlist__enable(evlist);
2396
f15eb531
NK
2397 if (forks)
2398 perf_evlist__start_workload(evlist);
2399
e13798c7 2400 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
42052bea
ACM
2401 evlist->threads->nr > 1 ||
2402 perf_evlist__first(evlist)->attr.inherit;
514f1c67 2403again:
efd5745e 2404 before = trace->nr_events;
514f1c67
ACM
2405
2406 for (i = 0; i < evlist->nr_mmaps; i++) {
2407 union perf_event *event;
2408
2409 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
514f1c67 2410 struct perf_sample sample;
514f1c67 2411
efd5745e 2412 ++trace->nr_events;
514f1c67 2413
514f1c67
ACM
2414 err = perf_evlist__parse_sample(evlist, event, &sample);
2415 if (err) {
c24ff998 2416 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
8e50d384 2417 goto next_event;
514f1c67
ACM
2418 }
2419
ddbb1b13 2420 trace__handle_event(trace, event, &sample);
8e50d384
ZZ
2421next_event:
2422 perf_evlist__mmap_consume(evlist, i);
20c5f10e 2423
ba209f85
ACM
2424 if (interrupted)
2425 goto out_disable;
02ac5421
ACM
2426
2427 if (done && !draining) {
2428 perf_evlist__disable(evlist);
2429 draining = true;
2430 }
514f1c67
ACM
2431 }
2432 }
2433
efd5745e 2434 if (trace->nr_events == before) {
ba209f85 2435 int timeout = done ? 100 : -1;
f15eb531 2436
46fb3c21
ACM
2437 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
2438 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP) == 0)
2439 draining = true;
2440
ba209f85 2441 goto again;
46fb3c21 2442 }
ba209f85
ACM
2443 } else {
2444 goto again;
f15eb531
NK
2445 }
2446
ba209f85 2447out_disable:
f3b623b8
ACM
2448 thread__zput(trace->current);
2449
ba209f85 2450 perf_evlist__disable(evlist);
514f1c67 2451
c522739d
ACM
2452 if (!err) {
2453 if (trace->summary)
2454 trace__fprintf_thread_summary(trace, trace->output);
2455
2456 if (trace->show_tool_stats) {
2457 fprintf(trace->output, "Stats:\n "
2458 " vfs_getname : %" PRIu64 "\n"
2459 " proc_getname: %" PRIu64 "\n",
2460 trace->stats.vfs_getname,
2461 trace->stats.proc_getname);
2462 }
2463 }
bf2575c1 2464
514f1c67
ACM
2465out_delete_evlist:
2466 perf_evlist__delete(evlist);
14a052df 2467 trace->evlist = NULL;
75b757ca 2468 trace->live = false;
514f1c67 2469 return err;
6ef068cb
ACM
2470{
2471 char errbuf[BUFSIZ];
a8f23d8f 2472
2cc990ba
ACM
2473out_error_sched_stat_runtime:
2474 debugfs__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
2475 goto out_error;
2476
801c67b0 2477out_error_raw_syscalls:
2cc990ba 2478 debugfs__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
a8f23d8f
ACM
2479 goto out_error;
2480
e09b18d4
ACM
2481out_error_mmap:
2482 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
2483 goto out_error;
2484
a8f23d8f
ACM
2485out_error_open:
2486 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
2487
2488out_error:
6ef068cb 2489 fprintf(trace->output, "%s\n", errbuf);
87f91868 2490 goto out_delete_evlist;
514f1c67 2491}
5ed08dae
ACM
2492out_error_mem:
2493 fprintf(trace->output, "Not enough memory to run!\n");
2494 goto out_delete_evlist;
a8f23d8f 2495}
514f1c67 2496
6810fc91
DA
2497static int trace__replay(struct trace *trace)
2498{
2499 const struct perf_evsel_str_handler handlers[] = {
c522739d 2500 { "probe:vfs_getname", trace__vfs_getname, },
6810fc91 2501 };
f5fc1412
JO
2502 struct perf_data_file file = {
2503 .path = input_name,
2504 .mode = PERF_DATA_MODE_READ,
e366a6d8 2505 .force = trace->force,
f5fc1412 2506 };
6810fc91 2507 struct perf_session *session;
003824e8 2508 struct perf_evsel *evsel;
6810fc91
DA
2509 int err = -1;
2510
2511 trace->tool.sample = trace__process_sample;
2512 trace->tool.mmap = perf_event__process_mmap;
384c671e 2513 trace->tool.mmap2 = perf_event__process_mmap2;
6810fc91
DA
2514 trace->tool.comm = perf_event__process_comm;
2515 trace->tool.exit = perf_event__process_exit;
2516 trace->tool.fork = perf_event__process_fork;
2517 trace->tool.attr = perf_event__process_attr;
2518 trace->tool.tracing_data = perf_event__process_tracing_data;
2519 trace->tool.build_id = perf_event__process_build_id;
2520
0a8cb85c 2521 trace->tool.ordered_events = true;
6810fc91
DA
2522 trace->tool.ordering_requires_timestamps = true;
2523
2524 /* add tid to output */
2525 trace->multiple_threads = true;
2526
f5fc1412 2527 session = perf_session__new(&file, false, &trace->tool);
6810fc91 2528 if (session == NULL)
52e02834 2529 return -1;
6810fc91 2530
0a7e6d1b 2531 if (symbol__init(&session->header.env) < 0)
cb2ffae2
NK
2532 goto out;
2533
8fb598e5
DA
2534 trace->host = &session->machines.host;
2535
6810fc91
DA
2536 err = perf_session__set_tracepoints_handlers(session, handlers);
2537 if (err)
2538 goto out;
2539
003824e8
NK
2540 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2541 "raw_syscalls:sys_enter");
9aca7f17
DA
2542 /* older kernels have syscalls tp versus raw_syscalls */
2543 if (evsel == NULL)
2544 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2545 "syscalls:sys_enter");
003824e8 2546
e281a960
SF
2547 if (evsel &&
2548 (perf_evsel__init_syscall_tp(evsel, trace__sys_enter) < 0 ||
2549 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
003824e8
NK
2550 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
2551 goto out;
2552 }
2553
2554 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2555 "raw_syscalls:sys_exit");
9aca7f17
DA
2556 if (evsel == NULL)
2557 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2558 "syscalls:sys_exit");
e281a960
SF
2559 if (evsel &&
2560 (perf_evsel__init_syscall_tp(evsel, trace__sys_exit) < 0 ||
2561 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
003824e8 2562 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
6810fc91
DA
2563 goto out;
2564 }
2565
1e28fe0a
SF
2566 evlist__for_each(session->evlist, evsel) {
2567 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
2568 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
2569 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
2570 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
2571 evsel->handler = trace__pgfault;
2572 }
2573
bdc89661
DA
2574 err = parse_target_str(trace);
2575 if (err != 0)
2576 goto out;
2577
6810fc91
DA
2578 setup_pager();
2579
b7b61cbe 2580 err = perf_session__process_events(session);
6810fc91
DA
2581 if (err)
2582 pr_err("Failed to process events, error %d", err);
2583
bf2575c1
DA
2584 else if (trace->summary)
2585 trace__fprintf_thread_summary(trace, trace->output);
2586
6810fc91
DA
2587out:
2588 perf_session__delete(session);
2589
2590 return err;
2591}
2592
1302d88e
ACM
2593static size_t trace__fprintf_threads_header(FILE *fp)
2594{
2595 size_t printed;
2596
99ff7150 2597 printed = fprintf(fp, "\n Summary of events:\n\n");
bf2575c1
DA
2598
2599 return printed;
2600}
2601
2602static size_t thread__dump_stats(struct thread_trace *ttrace,
2603 struct trace *trace, FILE *fp)
2604{
2605 struct stats *stats;
2606 size_t printed = 0;
2607 struct syscall *sc;
2608 struct int_node *inode = intlist__first(ttrace->syscall_stats);
2609
2610 if (inode == NULL)
2611 return 0;
2612
2613 printed += fprintf(fp, "\n");
2614
27a778b5
PE
2615 printed += fprintf(fp, " syscall calls min avg max stddev\n");
2616 printed += fprintf(fp, " (msec) (msec) (msec) (%%)\n");
2617 printed += fprintf(fp, " --------------- -------- --------- --------- --------- ------\n");
99ff7150 2618
bf2575c1
DA
2619 /* each int_node is a syscall */
2620 while (inode) {
2621 stats = inode->priv;
2622 if (stats) {
2623 double min = (double)(stats->min) / NSEC_PER_MSEC;
2624 double max = (double)(stats->max) / NSEC_PER_MSEC;
2625 double avg = avg_stats(stats);
2626 double pct;
2627 u64 n = (u64) stats->n;
2628
2629 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
2630 avg /= NSEC_PER_MSEC;
2631
2632 sc = &trace->syscalls.table[inode->i];
99ff7150 2633 printed += fprintf(fp, " %-15s", sc->name);
27a778b5 2634 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f",
7f7a4138 2635 n, min, avg);
27a778b5 2636 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
bf2575c1
DA
2637 }
2638
2639 inode = intlist__next(inode);
2640 }
2641
2642 printed += fprintf(fp, "\n\n");
1302d88e
ACM
2643
2644 return printed;
2645}
2646
896cbb56
DA
2647/* struct used to pass data to per-thread function */
2648struct summary_data {
2649 FILE *fp;
2650 struct trace *trace;
2651 size_t printed;
2652};
2653
2654static int trace__fprintf_one_thread(struct thread *thread, void *priv)
2655{
2656 struct summary_data *data = priv;
2657 FILE *fp = data->fp;
2658 size_t printed = data->printed;
2659 struct trace *trace = data->trace;
89dceb22 2660 struct thread_trace *ttrace = thread__priv(thread);
896cbb56
DA
2661 double ratio;
2662
2663 if (ttrace == NULL)
2664 return 0;
2665
2666 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
2667
15e65c69 2668 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
99ff7150 2669 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
15e65c69 2670 printed += fprintf(fp, "%.1f%%", ratio);
a2ea67d7
SF
2671 if (ttrace->pfmaj)
2672 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
2673 if (ttrace->pfmin)
2674 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
99ff7150 2675 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
bf2575c1 2676 printed += thread__dump_stats(ttrace, trace, fp);
896cbb56
DA
2677
2678 data->printed += printed;
2679
2680 return 0;
2681}
2682
1302d88e
ACM
2683static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
2684{
896cbb56
DA
2685 struct summary_data data = {
2686 .fp = fp,
2687 .trace = trace
2688 };
2689 data.printed = trace__fprintf_threads_header(fp);
1302d88e 2690
896cbb56
DA
2691 machine__for_each_thread(trace->host, trace__fprintf_one_thread, &data);
2692
2693 return data.printed;
1302d88e
ACM
2694}
2695
ae9ed035
ACM
2696static int trace__set_duration(const struct option *opt, const char *str,
2697 int unset __maybe_unused)
2698{
2699 struct trace *trace = opt->value;
2700
2701 trace->duration_filter = atof(str);
2702 return 0;
2703}
2704
f078c385
ACM
2705static int trace__set_filter_pids(const struct option *opt, const char *str,
2706 int unset __maybe_unused)
2707{
2708 int ret = -1;
2709 size_t i;
2710 struct trace *trace = opt->value;
2711 /*
2712 * FIXME: introduce a intarray class, plain parse csv and create a
2713 * { int nr, int entries[] } struct...
2714 */
2715 struct intlist *list = intlist__new(str);
2716
2717 if (list == NULL)
2718 return -1;
2719
2720 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
2721 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
2722
2723 if (trace->filter_pids.entries == NULL)
2724 goto out;
2725
2726 trace->filter_pids.entries[0] = getpid();
2727
2728 for (i = 1; i < trace->filter_pids.nr; ++i)
2729 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
2730
2731 intlist__delete(list);
2732 ret = 0;
2733out:
2734 return ret;
2735}
2736
c24ff998
ACM
2737static int trace__open_output(struct trace *trace, const char *filename)
2738{
2739 struct stat st;
2740
2741 if (!stat(filename, &st) && st.st_size) {
2742 char oldname[PATH_MAX];
2743
2744 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
2745 unlink(oldname);
2746 rename(filename, oldname);
2747 }
2748
2749 trace->output = fopen(filename, "w");
2750
2751 return trace->output == NULL ? -errno : 0;
2752}
2753
598d02c5
SF
2754static int parse_pagefaults(const struct option *opt, const char *str,
2755 int unset __maybe_unused)
2756{
2757 int *trace_pgfaults = opt->value;
2758
2759 if (strcmp(str, "all") == 0)
2760 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
2761 else if (strcmp(str, "maj") == 0)
2762 *trace_pgfaults |= TRACE_PFMAJ;
2763 else if (strcmp(str, "min") == 0)
2764 *trace_pgfaults |= TRACE_PFMIN;
2765 else
2766 return -1;
2767
2768 return 0;
2769}
2770
14a052df
ACM
2771static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
2772{
2773 struct perf_evsel *evsel;
2774
2775 evlist__for_each(evlist, evsel)
2776 evsel->handler = handler;
2777}
2778
514f1c67
ACM
2779int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
2780{
6fdd9cb7 2781 const char *trace_usage[] = {
f15eb531
NK
2782 "perf trace [<options>] [<command>]",
2783 "perf trace [<options>] -- <command> [<options>]",
5e2485b1
DA
2784 "perf trace record [<options>] [<command>]",
2785 "perf trace record [<options>] -- <command> [<options>]",
514f1c67
ACM
2786 NULL
2787 };
2788 struct trace trace = {
c522739d
ACM
2789 .audit = {
2790 .machine = audit_detect_machine(),
2791 .open_id = audit_name_to_syscall("open", trace.audit.machine),
2792 },
514f1c67
ACM
2793 .syscalls = {
2794 . max = -1,
2795 },
2796 .opts = {
2797 .target = {
2798 .uid = UINT_MAX,
2799 .uses_mmap = true,
2800 },
2801 .user_freq = UINT_MAX,
2802 .user_interval = ULLONG_MAX,
509051ea 2803 .no_buffering = true,
38d5447d 2804 .mmap_pages = UINT_MAX,
9d9cad76 2805 .proc_map_timeout = 500,
514f1c67 2806 },
c24ff998 2807 .output = stdout,
50c95cbd 2808 .show_comm = true,
e281a960 2809 .trace_syscalls = true,
514f1c67 2810 };
c24ff998 2811 const char *output_name = NULL;
2ae3a312 2812 const char *ev_qualifier_str = NULL;
514f1c67 2813 const struct option trace_options[] = {
14a052df
ACM
2814 OPT_CALLBACK(0, "event", &trace.evlist, "event",
2815 "event selector. use 'perf list' to list available events",
2816 parse_events_option),
50c95cbd
ACM
2817 OPT_BOOLEAN(0, "comm", &trace.show_comm,
2818 "show the thread COMM next to its id"),
c522739d 2819 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
d303e85a 2820 OPT_STRING('e', "expr", &ev_qualifier_str, "expr", "list of syscalls to trace"),
c24ff998 2821 OPT_STRING('o', "output", &output_name, "file", "output file name"),
6810fc91 2822 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
514f1c67
ACM
2823 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
2824 "trace events on existing process id"),
ac9be8ee 2825 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
514f1c67 2826 "trace events on existing thread id"),
fa0e4ffe
ACM
2827 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
2828 "pids to filter (by the kernel)", trace__set_filter_pids),
ac9be8ee 2829 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
514f1c67 2830 "system-wide collection from all CPUs"),
ac9be8ee 2831 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
514f1c67 2832 "list of cpus to monitor"),
6810fc91 2833 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
514f1c67 2834 "child tasks do not inherit counters"),
994a1f78
JO
2835 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
2836 "number of mmap data pages",
2837 perf_evlist__parse_mmap_pages),
ac9be8ee 2838 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
514f1c67 2839 "user to profile"),
ae9ed035
ACM
2840 OPT_CALLBACK(0, "duration", &trace, "float",
2841 "show only events with duration > N.M ms",
2842 trace__set_duration),
1302d88e 2843 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
7c304ee0 2844 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
4bb09192
DA
2845 OPT_BOOLEAN('T', "time", &trace.full_time,
2846 "Show full timestamp, not time relative to first start"),
fd2eabaf
DA
2847 OPT_BOOLEAN('s', "summary", &trace.summary_only,
2848 "Show only syscall summary with statistics"),
2849 OPT_BOOLEAN('S', "with-summary", &trace.summary,
2850 "Show all syscalls and summary with statistics"),
598d02c5
SF
2851 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
2852 "Trace pagefaults", parse_pagefaults, "maj"),
e281a960 2853 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
e366a6d8 2854 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
9d9cad76
KL
2855 OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
2856 "per thread proc mmap processing timeout in ms"),
514f1c67
ACM
2857 OPT_END()
2858 };
6fdd9cb7 2859 const char * const trace_subcommands[] = { "record", NULL };
514f1c67 2860 int err;
32caf0d1 2861 char bf[BUFSIZ];
514f1c67 2862
4d08cb80
ACM
2863 signal(SIGSEGV, sighandler_dump_stack);
2864 signal(SIGFPE, sighandler_dump_stack);
2865
14a052df 2866 trace.evlist = perf_evlist__new();
14a052df
ACM
2867
2868 if (trace.evlist == NULL) {
2869 pr_err("Not enough memory to run!\n");
ff8f695c 2870 err = -ENOMEM;
14a052df
ACM
2871 goto out;
2872 }
2873
6fdd9cb7
YS
2874 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
2875 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
fd2eabaf 2876
598d02c5
SF
2877 if (trace.trace_pgfaults) {
2878 trace.opts.sample_address = true;
2879 trace.opts.sample_time = true;
2880 }
2881
14a052df
ACM
2882 if (trace.evlist->nr_entries > 0)
2883 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
2884
1e28fe0a
SF
2885 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
2886 return trace__record(&trace, argc-1, &argv[1]);
2887
2888 /* summary_only implies summary option, but don't overwrite summary if set */
2889 if (trace.summary_only)
2890 trace.summary = trace.summary_only;
2891
726f3234
ACM
2892 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
2893 trace.evlist->nr_entries == 0 /* Was --events used? */) {
e281a960
SF
2894 pr_err("Please specify something to trace.\n");
2895 return -1;
2896 }
2897
c24ff998
ACM
2898 if (output_name != NULL) {
2899 err = trace__open_output(&trace, output_name);
2900 if (err < 0) {
2901 perror("failed to create output file");
2902 goto out;
2903 }
2904 }
2905
2ae3a312 2906 if (ev_qualifier_str != NULL) {
b059efdf
ACM
2907 const char *s = ev_qualifier_str;
2908
2909 trace.not_ev_qualifier = *s == '!';
2910 if (trace.not_ev_qualifier)
2911 ++s;
2912 trace.ev_qualifier = strlist__new(true, s);
2ae3a312 2913 if (trace.ev_qualifier == NULL) {
c24ff998
ACM
2914 fputs("Not enough memory to parse event qualifier",
2915 trace.output);
2916 err = -ENOMEM;
2917 goto out_close;
2ae3a312 2918 }
d0cc439b
ACM
2919
2920 err = trace__validate_ev_qualifier(&trace);
2921 if (err)
2922 goto out_close;
2ae3a312
ACM
2923 }
2924
602ad878 2925 err = target__validate(&trace.opts.target);
32caf0d1 2926 if (err) {
602ad878 2927 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
c24ff998
ACM
2928 fprintf(trace.output, "%s", bf);
2929 goto out_close;
32caf0d1
NK
2930 }
2931
602ad878 2932 err = target__parse_uid(&trace.opts.target);
514f1c67 2933 if (err) {
602ad878 2934 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
c24ff998
ACM
2935 fprintf(trace.output, "%s", bf);
2936 goto out_close;
514f1c67
ACM
2937 }
2938
602ad878 2939 if (!argc && target__none(&trace.opts.target))
ee76120e
NK
2940 trace.opts.target.system_wide = true;
2941
6810fc91
DA
2942 if (input_name)
2943 err = trace__replay(&trace);
2944 else
2945 err = trace__run(&trace, argc, argv);
1302d88e 2946
c24ff998
ACM
2947out_close:
2948 if (output_name != NULL)
2949 fclose(trace.output);
2950out:
1302d88e 2951 return err;
514f1c67 2952}