]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/perf/builtin-trace.c
perf trace: Beautify signal number arg in several syscalls
[mirror_ubuntu-bionic-kernel.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"
514f1c67
ACM
13
14#include <libaudit.h>
15#include <stdlib.h>
ae685380 16#include <sys/mman.h>
f9da0b0c 17#include <linux/futex.h>
514f1c67 18
456857bd
IM
19/* For older distros: */
20#ifndef MAP_STACK
21# define MAP_STACK 0x20000
22#endif
23
24#ifndef MADV_HWPOISON
25# define MADV_HWPOISON 100
26#endif
27
28#ifndef MADV_MERGEABLE
29# define MADV_MERGEABLE 12
30#endif
31
32#ifndef MADV_UNMERGEABLE
33# define MADV_UNMERGEABLE 13
34#endif
35
01533e97
ACM
36struct syscall_arg {
37 unsigned long val;
1f115cb7 38 void *parm;
01533e97
ACM
39 u8 idx;
40 u8 mask;
41};
42
1f115cb7
ACM
43struct strarray {
44 int nr_entries;
45 const char **entries;
46};
47
48#define DEFINE_STRARRAY(array) struct strarray strarray__##array = { \
49 .nr_entries = ARRAY_SIZE(array), \
50 .entries = array, \
51}
52
53static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
54 struct syscall_arg *arg)
55{
56 int idx = arg->val;
57 struct strarray *sa = arg->parm;
58
59 if (idx < 0 || idx >= sa->nr_entries)
60 return scnprintf(bf, size, "%d", idx);
61
62 return scnprintf(bf, size, "%s", sa->entries[idx]);
63}
64
65#define SCA_STRARRAY syscall_arg__scnprintf_strarray
66
6e7eeb51 67static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
01533e97 68 struct syscall_arg *arg)
13d4ff3e 69{
01533e97 70 return scnprintf(bf, size, "%#lx", arg->val);
13d4ff3e
ACM
71}
72
beccb2b5
ACM
73#define SCA_HEX syscall_arg__scnprintf_hex
74
6e7eeb51 75static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
01533e97 76 struct syscall_arg *arg)
ae685380 77{
01533e97 78 int printed = 0, prot = arg->val;
ae685380
ACM
79
80 if (prot == PROT_NONE)
81 return scnprintf(bf, size, "NONE");
82#define P_MMAP_PROT(n) \
83 if (prot & PROT_##n) { \
84 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
85 prot &= ~PROT_##n; \
86 }
87
88 P_MMAP_PROT(EXEC);
89 P_MMAP_PROT(READ);
90 P_MMAP_PROT(WRITE);
91#ifdef PROT_SEM
92 P_MMAP_PROT(SEM);
93#endif
94 P_MMAP_PROT(GROWSDOWN);
95 P_MMAP_PROT(GROWSUP);
96#undef P_MMAP_PROT
97
98 if (prot)
99 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
100
101 return printed;
102}
103
104#define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
105
6e7eeb51 106static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
01533e97 107 struct syscall_arg *arg)
941557e0 108{
01533e97 109 int printed = 0, flags = arg->val;
941557e0
ACM
110
111#define P_MMAP_FLAG(n) \
112 if (flags & MAP_##n) { \
113 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
114 flags &= ~MAP_##n; \
115 }
116
117 P_MMAP_FLAG(SHARED);
118 P_MMAP_FLAG(PRIVATE);
41817815 119#ifdef MAP_32BIT
941557e0 120 P_MMAP_FLAG(32BIT);
41817815 121#endif
941557e0
ACM
122 P_MMAP_FLAG(ANONYMOUS);
123 P_MMAP_FLAG(DENYWRITE);
124 P_MMAP_FLAG(EXECUTABLE);
125 P_MMAP_FLAG(FILE);
126 P_MMAP_FLAG(FIXED);
127 P_MMAP_FLAG(GROWSDOWN);
f2935f3e 128#ifdef MAP_HUGETLB
941557e0 129 P_MMAP_FLAG(HUGETLB);
f2935f3e 130#endif
941557e0
ACM
131 P_MMAP_FLAG(LOCKED);
132 P_MMAP_FLAG(NONBLOCK);
133 P_MMAP_FLAG(NORESERVE);
134 P_MMAP_FLAG(POPULATE);
135 P_MMAP_FLAG(STACK);
136#ifdef MAP_UNINITIALIZED
137 P_MMAP_FLAG(UNINITIALIZED);
138#endif
139#undef P_MMAP_FLAG
140
141 if (flags)
142 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
143
144 return printed;
145}
146
147#define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
148
6e7eeb51 149static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
01533e97 150 struct syscall_arg *arg)
9e9716d1 151{
01533e97 152 int behavior = arg->val;
9e9716d1
ACM
153
154 switch (behavior) {
155#define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
156 P_MADV_BHV(NORMAL);
157 P_MADV_BHV(RANDOM);
158 P_MADV_BHV(SEQUENTIAL);
159 P_MADV_BHV(WILLNEED);
160 P_MADV_BHV(DONTNEED);
161 P_MADV_BHV(REMOVE);
162 P_MADV_BHV(DONTFORK);
163 P_MADV_BHV(DOFORK);
164 P_MADV_BHV(HWPOISON);
165#ifdef MADV_SOFT_OFFLINE
166 P_MADV_BHV(SOFT_OFFLINE);
167#endif
168 P_MADV_BHV(MERGEABLE);
169 P_MADV_BHV(UNMERGEABLE);
f2935f3e 170#ifdef MADV_HUGEPAGE
9e9716d1 171 P_MADV_BHV(HUGEPAGE);
f2935f3e
DA
172#endif
173#ifdef MADV_NOHUGEPAGE
9e9716d1 174 P_MADV_BHV(NOHUGEPAGE);
f2935f3e 175#endif
9e9716d1
ACM
176#ifdef MADV_DONTDUMP
177 P_MADV_BHV(DONTDUMP);
178#endif
179#ifdef MADV_DODUMP
180 P_MADV_BHV(DODUMP);
181#endif
182#undef P_MADV_PHV
183 default: break;
184 }
185
186 return scnprintf(bf, size, "%#x", behavior);
187}
188
189#define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
190
01533e97 191static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, struct syscall_arg *arg)
f9da0b0c
ACM
192{
193 enum syscall_futex_args {
194 SCF_UADDR = (1 << 0),
195 SCF_OP = (1 << 1),
196 SCF_VAL = (1 << 2),
197 SCF_TIMEOUT = (1 << 3),
198 SCF_UADDR2 = (1 << 4),
199 SCF_VAL3 = (1 << 5),
200 };
01533e97 201 int op = arg->val;
f9da0b0c
ACM
202 int cmd = op & FUTEX_CMD_MASK;
203 size_t printed = 0;
204
205 switch (cmd) {
206#define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
01533e97
ACM
207 P_FUTEX_OP(WAIT); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
208 P_FUTEX_OP(WAKE); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
209 P_FUTEX_OP(FD); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
210 P_FUTEX_OP(REQUEUE); arg->mask |= SCF_VAL3|SCF_TIMEOUT; break;
211 P_FUTEX_OP(CMP_REQUEUE); arg->mask |= SCF_TIMEOUT; break;
212 P_FUTEX_OP(CMP_REQUEUE_PI); arg->mask |= SCF_TIMEOUT; break;
f9da0b0c 213 P_FUTEX_OP(WAKE_OP); break;
01533e97
ACM
214 P_FUTEX_OP(LOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
215 P_FUTEX_OP(UNLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
216 P_FUTEX_OP(TRYLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
217 P_FUTEX_OP(WAIT_BITSET); arg->mask |= SCF_UADDR2; break;
218 P_FUTEX_OP(WAKE_BITSET); arg->mask |= SCF_UADDR2; break;
f9da0b0c
ACM
219 P_FUTEX_OP(WAIT_REQUEUE_PI); break;
220 default: printed = scnprintf(bf, size, "%#x", cmd); break;
221 }
222
223 if (op & FUTEX_PRIVATE_FLAG)
224 printed += scnprintf(bf + printed, size - printed, "|PRIV");
225
226 if (op & FUTEX_CLOCK_REALTIME)
227 printed += scnprintf(bf + printed, size - printed, "|CLKRT");
228
229 return printed;
230}
231
efe6b882
ACM
232#define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
233
1f115cb7
ACM
234static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
235static DEFINE_STRARRAY(itimers);
236
efe6b882
ACM
237static const char *whences[] = { "SET", "CUR", "END",
238#ifdef SEEK_DATA
239"DATA",
240#endif
241#ifdef SEEK_HOLE
242"HOLE",
243#endif
244};
245static DEFINE_STRARRAY(whences);
f9da0b0c 246
80f587d5
ACM
247static const char *fcntl_cmds[] = {
248 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
249 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "F_GETLK64",
250 "F_SETLK64", "F_SETLKW64", "F_SETOWN_EX", "F_GETOWN_EX",
251 "F_GETOWNER_UIDS",
252};
253static DEFINE_STRARRAY(fcntl_cmds);
254
eb5b1b14
ACM
255static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
256static DEFINE_STRARRAY(sighow);
257
be65a89a 258static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size,
01533e97 259 struct syscall_arg *arg)
be65a89a 260{
01533e97 261 int printed = 0, flags = arg->val;
be65a89a
ACM
262
263 if (!(flags & O_CREAT))
01533e97 264 arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */
be65a89a
ACM
265
266 if (flags == 0)
267 return scnprintf(bf, size, "RDONLY");
268#define P_FLAG(n) \
269 if (flags & O_##n) { \
270 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
271 flags &= ~O_##n; \
272 }
273
274 P_FLAG(APPEND);
275 P_FLAG(ASYNC);
276 P_FLAG(CLOEXEC);
277 P_FLAG(CREAT);
278 P_FLAG(DIRECT);
279 P_FLAG(DIRECTORY);
280 P_FLAG(EXCL);
281 P_FLAG(LARGEFILE);
282 P_FLAG(NOATIME);
283 P_FLAG(NOCTTY);
284#ifdef O_NONBLOCK
285 P_FLAG(NONBLOCK);
286#elif O_NDELAY
287 P_FLAG(NDELAY);
288#endif
289#ifdef O_PATH
290 P_FLAG(PATH);
291#endif
292 P_FLAG(RDWR);
293#ifdef O_DSYNC
294 if ((flags & O_SYNC) == O_SYNC)
295 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC");
296 else {
297 P_FLAG(DSYNC);
298 }
299#else
300 P_FLAG(SYNC);
301#endif
302 P_FLAG(TRUNC);
303 P_FLAG(WRONLY);
304#undef P_FLAG
305
306 if (flags)
307 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
308
309 return printed;
310}
311
312#define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags
313
8bad5b0a
ACM
314static size_t syscall_arg__scnprintf_signum(char *bf, size_t size, struct syscall_arg *arg)
315{
316 int sig = arg->val;
317
318 switch (sig) {
319#define P_SIGNUM(n) case SIG##n: return scnprintf(bf, size, #n)
320 P_SIGNUM(HUP);
321 P_SIGNUM(INT);
322 P_SIGNUM(QUIT);
323 P_SIGNUM(ILL);
324 P_SIGNUM(TRAP);
325 P_SIGNUM(ABRT);
326 P_SIGNUM(BUS);
327 P_SIGNUM(FPE);
328 P_SIGNUM(KILL);
329 P_SIGNUM(USR1);
330 P_SIGNUM(SEGV);
331 P_SIGNUM(USR2);
332 P_SIGNUM(PIPE);
333 P_SIGNUM(ALRM);
334 P_SIGNUM(TERM);
335 P_SIGNUM(STKFLT);
336 P_SIGNUM(CHLD);
337 P_SIGNUM(CONT);
338 P_SIGNUM(STOP);
339 P_SIGNUM(TSTP);
340 P_SIGNUM(TTIN);
341 P_SIGNUM(TTOU);
342 P_SIGNUM(URG);
343 P_SIGNUM(XCPU);
344 P_SIGNUM(XFSZ);
345 P_SIGNUM(VTALRM);
346 P_SIGNUM(PROF);
347 P_SIGNUM(WINCH);
348 P_SIGNUM(IO);
349 P_SIGNUM(PWR);
350 P_SIGNUM(SYS);
351 default: break;
352 }
353
354 return scnprintf(bf, size, "%#x", sig);
355}
356
357#define SCA_SIGNUM syscall_arg__scnprintf_signum
358
514f1c67
ACM
359static struct syscall_fmt {
360 const char *name;
aec1930b 361 const char *alias;
01533e97 362 size_t (*arg_scnprintf[6])(char *bf, size_t size, struct syscall_arg *arg);
1f115cb7 363 void *arg_parm[6];
514f1c67
ACM
364 bool errmsg;
365 bool timeout;
04b34729 366 bool hexret;
514f1c67 367} syscall_fmts[] = {
8b745263 368 { .name = "access", .errmsg = true, },
aec1930b 369 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
beccb2b5
ACM
370 { .name = "brk", .hexret = true,
371 .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
04b34729 372 { .name = "mmap", .hexret = true, },
a14bb860 373 { .name = "connect", .errmsg = true, },
80f587d5
ACM
374 { .name = "fcntl", .errmsg = true,
375 .arg_scnprintf = { [1] = SCA_STRARRAY, /* cmd */ },
376 .arg_parm = { [1] = &strarray__fcntl_cmds, /* cmd */ }, },
aec1930b
ACM
377 { .name = "fstat", .errmsg = true, .alias = "newfstat", },
378 { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
f9da0b0c
ACM
379 { .name = "futex", .errmsg = true,
380 .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
1f115cb7
ACM
381 { .name = "getitimer", .errmsg = true,
382 .arg_scnprintf = { [0] = SCA_STRARRAY, /* which */ },
383 .arg_parm = { [0] = &strarray__itimers, /* which */ }, },
beccb2b5
ACM
384 { .name = "ioctl", .errmsg = true,
385 .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
8bad5b0a
ACM
386 { .name = "kill", .errmsg = true,
387 .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
579e7865 388 { .name = "lseek", .errmsg = true,
efe6b882
ACM
389 .arg_scnprintf = { [2] = SCA_STRARRAY, /* whence */ },
390 .arg_parm = { [2] = &strarray__whences, /* whence */ }, },
e5959683 391 { .name = "lstat", .errmsg = true, .alias = "newlstat", },
9e9716d1
ACM
392 { .name = "madvise", .errmsg = true,
393 .arg_scnprintf = { [0] = SCA_HEX, /* start */
394 [2] = SCA_MADV_BHV, /* behavior */ }, },
beccb2b5 395 { .name = "mmap", .hexret = true,
ae685380 396 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
941557e0
ACM
397 [2] = SCA_MMAP_PROT, /* prot */
398 [3] = SCA_MMAP_FLAGS, /* flags */ }, },
beccb2b5 399 { .name = "mprotect", .errmsg = true,
ae685380
ACM
400 .arg_scnprintf = { [0] = SCA_HEX, /* start */
401 [2] = SCA_MMAP_PROT, /* prot */ }, },
402 { .name = "mremap", .hexret = true,
403 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
404 [4] = SCA_HEX, /* new_addr */ }, },
beccb2b5
ACM
405 { .name = "munmap", .errmsg = true,
406 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
be65a89a
ACM
407 { .name = "open", .errmsg = true,
408 .arg_scnprintf = { [1] = SCA_OPEN_FLAGS, /* flags */ }, },
31cd3855
ACM
409 { .name = "open_by_handle_at", .errmsg = true,
410 .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
411 { .name = "openat", .errmsg = true,
412 .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
aec1930b
ACM
413 { .name = "poll", .errmsg = true, .timeout = true, },
414 { .name = "ppoll", .errmsg = true, .timeout = true, },
e5959683
ACM
415 { .name = "pread", .errmsg = true, .alias = "pread64", },
416 { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
aec1930b
ACM
417 { .name = "read", .errmsg = true, },
418 { .name = "recvfrom", .errmsg = true, },
8bad5b0a
ACM
419 { .name = "rt_sigaction", .errmsg = true,
420 .arg_scnprintf = { [0] = SCA_SIGNUM, /* sig */ }, },
eb5b1b14
ACM
421 { .name = "rt_sigprocmask", .errmsg = true,
422 .arg_scnprintf = { [0] = SCA_STRARRAY, /* how */ },
423 .arg_parm = { [0] = &strarray__sighow, /* how */ }, },
8bad5b0a
ACM
424 { .name = "rt_sigqueueinfo", .errmsg = true,
425 .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
426 { .name = "rt_tgsigqueueinfo", .errmsg = true,
427 .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
aec1930b 428 { .name = "select", .errmsg = true, .timeout = true, },
1f115cb7
ACM
429 { .name = "setitimer", .errmsg = true,
430 .arg_scnprintf = { [0] = SCA_STRARRAY, /* which */ },
431 .arg_parm = { [0] = &strarray__itimers, /* which */ }, },
8b745263 432 { .name = "socket", .errmsg = true, },
aec1930b 433 { .name = "stat", .errmsg = true, .alias = "newstat", },
8bad5b0a
ACM
434 { .name = "tgkill", .errmsg = true,
435 .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
436 { .name = "tkill", .errmsg = true,
437 .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
e5959683 438 { .name = "uname", .errmsg = true, .alias = "newuname", },
514f1c67
ACM
439};
440
441static int syscall_fmt__cmp(const void *name, const void *fmtp)
442{
443 const struct syscall_fmt *fmt = fmtp;
444 return strcmp(name, fmt->name);
445}
446
447static struct syscall_fmt *syscall_fmt__find(const char *name)
448{
449 const int nmemb = ARRAY_SIZE(syscall_fmts);
450 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
451}
452
453struct syscall {
454 struct event_format *tp_format;
455 const char *name;
2ae3a312 456 bool filtered;
514f1c67 457 struct syscall_fmt *fmt;
01533e97 458 size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
1f115cb7 459 void **arg_parm;
514f1c67
ACM
460};
461
60c907ab
ACM
462static size_t fprintf_duration(unsigned long t, FILE *fp)
463{
464 double duration = (double)t / NSEC_PER_MSEC;
465 size_t printed = fprintf(fp, "(");
466
467 if (duration >= 1.0)
468 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
469 else if (duration >= 0.01)
470 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
471 else
472 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
c24ff998 473 return printed + fprintf(fp, "): ");
60c907ab
ACM
474}
475
752fde44
ACM
476struct thread_trace {
477 u64 entry_time;
478 u64 exit_time;
479 bool entry_pending;
efd5745e 480 unsigned long nr_events;
752fde44 481 char *entry_str;
1302d88e 482 double runtime_ms;
752fde44
ACM
483};
484
485static struct thread_trace *thread_trace__new(void)
486{
487 return zalloc(sizeof(struct thread_trace));
488}
489
c24ff998 490static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
752fde44 491{
efd5745e
ACM
492 struct thread_trace *ttrace;
493
752fde44
ACM
494 if (thread == NULL)
495 goto fail;
496
497 if (thread->priv == NULL)
498 thread->priv = thread_trace__new();
efd5745e 499
752fde44
ACM
500 if (thread->priv == NULL)
501 goto fail;
502
efd5745e
ACM
503 ttrace = thread->priv;
504 ++ttrace->nr_events;
505
506 return ttrace;
752fde44 507fail:
c24ff998 508 color_fprintf(fp, PERF_COLOR_RED,
752fde44
ACM
509 "WARNING: not enough memory, dropping samples!\n");
510 return NULL;
511}
512
514f1c67 513struct trace {
c24ff998 514 struct perf_tool tool;
514f1c67
ACM
515 int audit_machine;
516 struct {
517 int max;
518 struct syscall *table;
519 } syscalls;
520 struct perf_record_opts opts;
752fde44
ACM
521 struct machine host;
522 u64 base_time;
c24ff998 523 FILE *output;
efd5745e 524 unsigned long nr_events;
b059efdf
ACM
525 struct strlist *ev_qualifier;
526 bool not_ev_qualifier;
bdc89661
DA
527 struct intlist *tid_list;
528 struct intlist *pid_list;
1302d88e 529 bool sched;
752fde44 530 bool multiple_threads;
ae9ed035 531 double duration_filter;
1302d88e 532 double runtime_ms;
514f1c67
ACM
533};
534
ae9ed035
ACM
535static bool trace__filter_duration(struct trace *trace, double t)
536{
537 return t < (trace->duration_filter * NSEC_PER_MSEC);
538}
539
752fde44
ACM
540static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
541{
542 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
543
60c907ab 544 return fprintf(fp, "%10.3f ", ts);
752fde44
ACM
545}
546
f15eb531
NK
547static bool done = false;
548
549static void sig_handler(int sig __maybe_unused)
550{
551 done = true;
552}
553
752fde44 554static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
60c907ab 555 u64 duration, u64 tstamp, FILE *fp)
752fde44
ACM
556{
557 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
60c907ab 558 printed += fprintf_duration(duration, fp);
752fde44
ACM
559
560 if (trace->multiple_threads)
38051234 561 printed += fprintf(fp, "%d ", thread->tid);
752fde44
ACM
562
563 return printed;
564}
565
c24ff998
ACM
566static int trace__process_event(struct trace *trace, struct machine *machine,
567 union perf_event *event)
752fde44
ACM
568{
569 int ret = 0;
570
571 switch (event->header.type) {
572 case PERF_RECORD_LOST:
c24ff998 573 color_fprintf(trace->output, PERF_COLOR_RED,
752fde44
ACM
574 "LOST %" PRIu64 " events!\n", event->lost.lost);
575 ret = machine__process_lost_event(machine, event);
576 default:
577 ret = machine__process_event(machine, event);
578 break;
579 }
580
581 return ret;
582}
583
c24ff998 584static int trace__tool_process(struct perf_tool *tool,
752fde44
ACM
585 union perf_event *event,
586 struct perf_sample *sample __maybe_unused,
587 struct machine *machine)
588{
c24ff998
ACM
589 struct trace *trace = container_of(tool, struct trace, tool);
590 return trace__process_event(trace, machine, event);
752fde44
ACM
591}
592
593static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
594{
595 int err = symbol__init();
596
597 if (err)
598 return err;
599
600 machine__init(&trace->host, "", HOST_KERNEL_ID);
601 machine__create_kernel_maps(&trace->host);
602
603 if (perf_target__has_task(&trace->opts.target)) {
c24ff998 604 err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
752fde44
ACM
605 trace__tool_process,
606 &trace->host);
607 } else {
c24ff998 608 err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
752fde44
ACM
609 &trace->host);
610 }
611
612 if (err)
613 symbol__exit();
614
615 return err;
616}
617
13d4ff3e
ACM
618static int syscall__set_arg_fmts(struct syscall *sc)
619{
620 struct format_field *field;
621 int idx = 0;
622
623 sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
624 if (sc->arg_scnprintf == NULL)
625 return -1;
626
1f115cb7
ACM
627 if (sc->fmt)
628 sc->arg_parm = sc->fmt->arg_parm;
629
13d4ff3e 630 for (field = sc->tp_format->format.fields->next; field; field = field->next) {
beccb2b5
ACM
631 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
632 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
633 else if (field->flags & FIELD_IS_POINTER)
13d4ff3e
ACM
634 sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
635 ++idx;
636 }
637
638 return 0;
639}
640
514f1c67
ACM
641static int trace__read_syscall_info(struct trace *trace, int id)
642{
643 char tp_name[128];
644 struct syscall *sc;
3a531260
ACM
645 const char *name = audit_syscall_to_name(id, trace->audit_machine);
646
647 if (name == NULL)
648 return -1;
514f1c67
ACM
649
650 if (id > trace->syscalls.max) {
651 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
652
653 if (nsyscalls == NULL)
654 return -1;
655
656 if (trace->syscalls.max != -1) {
657 memset(nsyscalls + trace->syscalls.max + 1, 0,
658 (id - trace->syscalls.max) * sizeof(*sc));
659 } else {
660 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
661 }
662
663 trace->syscalls.table = nsyscalls;
664 trace->syscalls.max = id;
665 }
666
667 sc = trace->syscalls.table + id;
3a531260 668 sc->name = name;
2ae3a312 669
b059efdf
ACM
670 if (trace->ev_qualifier) {
671 bool in = strlist__find(trace->ev_qualifier, name) != NULL;
672
673 if (!(in ^ trace->not_ev_qualifier)) {
674 sc->filtered = true;
675 /*
676 * No need to do read tracepoint information since this will be
677 * filtered out.
678 */
679 return 0;
680 }
2ae3a312
ACM
681 }
682
3a531260 683 sc->fmt = syscall_fmt__find(sc->name);
514f1c67 684
aec1930b 685 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
514f1c67 686 sc->tp_format = event_format__new("syscalls", tp_name);
aec1930b
ACM
687
688 if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
689 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
690 sc->tp_format = event_format__new("syscalls", tp_name);
691 }
514f1c67 692
13d4ff3e
ACM
693 if (sc->tp_format == NULL)
694 return -1;
695
696 return syscall__set_arg_fmts(sc);
514f1c67
ACM
697}
698
752fde44
ACM
699static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
700 unsigned long *args)
514f1c67 701{
514f1c67
ACM
702 size_t printed = 0;
703
704 if (sc->tp_format != NULL) {
705 struct format_field *field;
01533e97
ACM
706 u8 bit = 1;
707 struct syscall_arg arg = {
708 .idx = 0,
709 .mask = 0,
710 };
6e7eeb51
ACM
711
712 for (field = sc->tp_format->format.fields->next; field;
01533e97
ACM
713 field = field->next, ++arg.idx, bit <<= 1) {
714 if (arg.mask & bit)
6e7eeb51 715 continue;
514f1c67 716
752fde44 717 printed += scnprintf(bf + printed, size - printed,
13d4ff3e 718 "%s%s: ", printed ? ", " : "", field->name);
01533e97
ACM
719 if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
720 arg.val = args[arg.idx];
1f115cb7
ACM
721 if (sc->arg_parm)
722 arg.parm = sc->arg_parm[arg.idx];
01533e97
ACM
723 printed += sc->arg_scnprintf[arg.idx](bf + printed,
724 size - printed, &arg);
6e7eeb51 725 } else {
13d4ff3e 726 printed += scnprintf(bf + printed, size - printed,
01533e97 727 "%ld", args[arg.idx]);
6e7eeb51 728 }
514f1c67
ACM
729 }
730 } else {
01533e97
ACM
731 int i = 0;
732
514f1c67 733 while (i < 6) {
752fde44
ACM
734 printed += scnprintf(bf + printed, size - printed,
735 "%sarg%d: %ld",
736 printed ? ", " : "", i, args[i]);
514f1c67
ACM
737 ++i;
738 }
739 }
740
741 return printed;
742}
743
ba3d7dee
ACM
744typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
745 struct perf_sample *sample);
746
747static struct syscall *trace__syscall_info(struct trace *trace,
748 struct perf_evsel *evsel,
749 struct perf_sample *sample)
750{
751 int id = perf_evsel__intval(evsel, sample, "id");
752
753 if (id < 0) {
adaa18bf
ACM
754
755 /*
756 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
757 * before that, leaving at a higher verbosity level till that is
758 * explained. Reproduced with plain ftrace with:
759 *
760 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
761 * grep "NR -1 " /t/trace_pipe
762 *
763 * After generating some load on the machine.
764 */
765 if (verbose > 1) {
766 static u64 n;
767 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
768 id, perf_evsel__name(evsel), ++n);
769 }
ba3d7dee
ACM
770 return NULL;
771 }
772
773 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
774 trace__read_syscall_info(trace, id))
775 goto out_cant_read;
776
777 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
778 goto out_cant_read;
779
780 return &trace->syscalls.table[id];
781
782out_cant_read:
7c304ee0
ACM
783 if (verbose) {
784 fprintf(trace->output, "Problems reading syscall %d", id);
785 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
786 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
787 fputs(" information\n", trace->output);
788 }
ba3d7dee
ACM
789 return NULL;
790}
791
792static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
793 struct perf_sample *sample)
794{
752fde44 795 char *msg;
ba3d7dee 796 void *args;
752fde44 797 size_t printed = 0;
2ae3a312 798 struct thread *thread;
ba3d7dee 799 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
2ae3a312
ACM
800 struct thread_trace *ttrace;
801
802 if (sc == NULL)
803 return -1;
ba3d7dee 804
2ae3a312
ACM
805 if (sc->filtered)
806 return 0;
807
314add6b
AH
808 thread = machine__findnew_thread(&trace->host, sample->pid,
809 sample->tid);
c24ff998 810 ttrace = thread__trace(thread, trace->output);
2ae3a312 811 if (ttrace == NULL)
ba3d7dee
ACM
812 return -1;
813
814 args = perf_evsel__rawptr(evsel, sample, "args");
815 if (args == NULL) {
c24ff998 816 fprintf(trace->output, "Problems reading syscall arguments\n");
ba3d7dee
ACM
817 return -1;
818 }
819
752fde44
ACM
820 ttrace = thread->priv;
821
822 if (ttrace->entry_str == NULL) {
823 ttrace->entry_str = malloc(1024);
824 if (!ttrace->entry_str)
825 return -1;
826 }
827
828 ttrace->entry_time = sample->time;
829 msg = ttrace->entry_str;
830 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
831
832 printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
833
834 if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
ae9ed035 835 if (!trace->duration_filter) {
c24ff998
ACM
836 trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
837 fprintf(trace->output, "%-70s\n", ttrace->entry_str);
ae9ed035 838 }
752fde44
ACM
839 } else
840 ttrace->entry_pending = true;
ba3d7dee
ACM
841
842 return 0;
843}
844
845static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
846 struct perf_sample *sample)
847{
848 int ret;
60c907ab 849 u64 duration = 0;
2ae3a312 850 struct thread *thread;
ba3d7dee 851 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
2ae3a312
ACM
852 struct thread_trace *ttrace;
853
854 if (sc == NULL)
855 return -1;
ba3d7dee 856
2ae3a312
ACM
857 if (sc->filtered)
858 return 0;
859
314add6b
AH
860 thread = machine__findnew_thread(&trace->host, sample->pid,
861 sample->tid);
c24ff998 862 ttrace = thread__trace(thread, trace->output);
2ae3a312 863 if (ttrace == NULL)
ba3d7dee
ACM
864 return -1;
865
866 ret = perf_evsel__intval(evsel, sample, "ret");
867
752fde44
ACM
868 ttrace = thread->priv;
869
870 ttrace->exit_time = sample->time;
871
ae9ed035 872 if (ttrace->entry_time) {
60c907ab 873 duration = sample->time - ttrace->entry_time;
ae9ed035
ACM
874 if (trace__filter_duration(trace, duration))
875 goto out;
876 } else if (trace->duration_filter)
877 goto out;
60c907ab 878
c24ff998 879 trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
752fde44
ACM
880
881 if (ttrace->entry_pending) {
c24ff998 882 fprintf(trace->output, "%-70s", ttrace->entry_str);
752fde44 883 } else {
c24ff998
ACM
884 fprintf(trace->output, " ... [");
885 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
886 fprintf(trace->output, "]: %s()", sc->name);
752fde44
ACM
887 }
888
da3c9a44
ACM
889 if (sc->fmt == NULL) {
890signed_print:
891 fprintf(trace->output, ") = %d", ret);
892 } else if (ret < 0 && sc->fmt->errmsg) {
ba3d7dee
ACM
893 char bf[256];
894 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
895 *e = audit_errno_to_name(-ret);
896
c24ff998 897 fprintf(trace->output, ") = -1 %s %s", e, emsg);
da3c9a44 898 } else if (ret == 0 && sc->fmt->timeout)
c24ff998 899 fprintf(trace->output, ") = 0 Timeout");
04b34729
ACM
900 else if (sc->fmt->hexret)
901 fprintf(trace->output, ") = %#x", ret);
ba3d7dee 902 else
da3c9a44 903 goto signed_print;
ba3d7dee 904
c24ff998 905 fputc('\n', trace->output);
ae9ed035 906out:
752fde44
ACM
907 ttrace->entry_pending = false;
908
ba3d7dee
ACM
909 return 0;
910}
911
1302d88e
ACM
912static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
913 struct perf_sample *sample)
914{
915 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
916 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
314add6b
AH
917 struct thread *thread = machine__findnew_thread(&trace->host,
918 sample->pid,
919 sample->tid);
c24ff998 920 struct thread_trace *ttrace = thread__trace(thread, trace->output);
1302d88e
ACM
921
922 if (ttrace == NULL)
923 goto out_dump;
924
925 ttrace->runtime_ms += runtime_ms;
926 trace->runtime_ms += runtime_ms;
927 return 0;
928
929out_dump:
c24ff998 930 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
1302d88e
ACM
931 evsel->name,
932 perf_evsel__strval(evsel, sample, "comm"),
933 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
934 runtime,
935 perf_evsel__intval(evsel, sample, "vruntime"));
936 return 0;
937}
938
bdc89661
DA
939static bool skip_sample(struct trace *trace, struct perf_sample *sample)
940{
941 if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
942 (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
943 return false;
944
945 if (trace->pid_list || trace->tid_list)
946 return true;
947
948 return false;
949}
950
6810fc91
DA
951static int trace__process_sample(struct perf_tool *tool,
952 union perf_event *event __maybe_unused,
953 struct perf_sample *sample,
954 struct perf_evsel *evsel,
955 struct machine *machine __maybe_unused)
956{
957 struct trace *trace = container_of(tool, struct trace, tool);
958 int err = 0;
959
960 tracepoint_handler handler = evsel->handler.func;
961
bdc89661
DA
962 if (skip_sample(trace, sample))
963 return 0;
964
6810fc91
DA
965 if (trace->base_time == 0)
966 trace->base_time = sample->time;
967
968 if (handler)
969 handler(trace, evsel, sample);
970
971 return err;
972}
973
974static bool
975perf_session__has_tp(struct perf_session *session, const char *name)
976{
977 struct perf_evsel *evsel;
978
979 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name);
980
981 return evsel != NULL;
982}
983
bdc89661
DA
984static int parse_target_str(struct trace *trace)
985{
986 if (trace->opts.target.pid) {
987 trace->pid_list = intlist__new(trace->opts.target.pid);
988 if (trace->pid_list == NULL) {
989 pr_err("Error parsing process id string\n");
990 return -EINVAL;
991 }
992 }
993
994 if (trace->opts.target.tid) {
995 trace->tid_list = intlist__new(trace->opts.target.tid);
996 if (trace->tid_list == NULL) {
997 pr_err("Error parsing thread id string\n");
998 return -EINVAL;
999 }
1000 }
1001
1002 return 0;
1003}
1004
f15eb531 1005static int trace__run(struct trace *trace, int argc, const char **argv)
514f1c67 1006{
334fe7a3 1007 struct perf_evlist *evlist = perf_evlist__new();
ba3d7dee 1008 struct perf_evsel *evsel;
efd5745e
ACM
1009 int err = -1, i;
1010 unsigned long before;
f15eb531 1011 const bool forks = argc > 0;
514f1c67
ACM
1012
1013 if (evlist == NULL) {
c24ff998 1014 fprintf(trace->output, "Not enough memory to run!\n");
514f1c67
ACM
1015 goto out;
1016 }
1017
39876e7d
ACM
1018 if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
1019 perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
c24ff998 1020 fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
514f1c67
ACM
1021 goto out_delete_evlist;
1022 }
1023
1302d88e
ACM
1024 if (trace->sched &&
1025 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
1026 trace__sched_stat_runtime)) {
c24ff998 1027 fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
1302d88e
ACM
1028 goto out_delete_evlist;
1029 }
1030
514f1c67
ACM
1031 err = perf_evlist__create_maps(evlist, &trace->opts.target);
1032 if (err < 0) {
c24ff998 1033 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
514f1c67
ACM
1034 goto out_delete_evlist;
1035 }
1036
752fde44
ACM
1037 err = trace__symbols_init(trace, evlist);
1038 if (err < 0) {
c24ff998 1039 fprintf(trace->output, "Problems initializing symbol libraries!\n");
3beb0861 1040 goto out_delete_maps;
752fde44
ACM
1041 }
1042
f77a9518 1043 perf_evlist__config(evlist, &trace->opts);
514f1c67 1044
f15eb531
NK
1045 signal(SIGCHLD, sig_handler);
1046 signal(SIGINT, sig_handler);
1047
1048 if (forks) {
6ef73ec4 1049 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
55e162ea 1050 argv, false, false);
f15eb531 1051 if (err < 0) {
c24ff998 1052 fprintf(trace->output, "Couldn't run the workload!\n");
3beb0861 1053 goto out_delete_maps;
f15eb531
NK
1054 }
1055 }
1056
514f1c67
ACM
1057 err = perf_evlist__open(evlist);
1058 if (err < 0) {
c24ff998 1059 fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
3beb0861 1060 goto out_delete_maps;
514f1c67
ACM
1061 }
1062
1063 err = perf_evlist__mmap(evlist, UINT_MAX, false);
1064 if (err < 0) {
c24ff998 1065 fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
3beb0861 1066 goto out_close_evlist;
514f1c67
ACM
1067 }
1068
1069 perf_evlist__enable(evlist);
f15eb531
NK
1070
1071 if (forks)
1072 perf_evlist__start_workload(evlist);
1073
752fde44 1074 trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
514f1c67 1075again:
efd5745e 1076 before = trace->nr_events;
514f1c67
ACM
1077
1078 for (i = 0; i < evlist->nr_mmaps; i++) {
1079 union perf_event *event;
1080
1081 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
1082 const u32 type = event->header.type;
ba3d7dee 1083 tracepoint_handler handler;
514f1c67 1084 struct perf_sample sample;
514f1c67 1085
efd5745e 1086 ++trace->nr_events;
514f1c67 1087
514f1c67
ACM
1088 err = perf_evlist__parse_sample(evlist, event, &sample);
1089 if (err) {
c24ff998 1090 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
514f1c67
ACM
1091 continue;
1092 }
1093
752fde44
ACM
1094 if (trace->base_time == 0)
1095 trace->base_time = sample.time;
1096
1097 if (type != PERF_RECORD_SAMPLE) {
c24ff998 1098 trace__process_event(trace, &trace->host, event);
752fde44
ACM
1099 continue;
1100 }
1101
514f1c67
ACM
1102 evsel = perf_evlist__id2evsel(evlist, sample.id);
1103 if (evsel == NULL) {
c24ff998 1104 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
514f1c67
ACM
1105 continue;
1106 }
1107
fc551f8d 1108 if (sample.raw_data == NULL) {
c24ff998 1109 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
fc551f8d
ACM
1110 perf_evsel__name(evsel), sample.tid,
1111 sample.cpu, sample.raw_size);
1112 continue;
1113 }
1114
ba3d7dee
ACM
1115 handler = evsel->handler.func;
1116 handler(trace, evsel, &sample);
20c5f10e
ACM
1117
1118 if (done)
1119 goto out_unmap_evlist;
514f1c67
ACM
1120 }
1121 }
1122
efd5745e 1123 if (trace->nr_events == before) {
f15eb531 1124 if (done)
3beb0861 1125 goto out_unmap_evlist;
f15eb531 1126
514f1c67 1127 poll(evlist->pollfd, evlist->nr_fds, -1);
f15eb531
NK
1128 }
1129
1130 if (done)
1131 perf_evlist__disable(evlist);
514f1c67
ACM
1132
1133 goto again;
1134
3beb0861
NK
1135out_unmap_evlist:
1136 perf_evlist__munmap(evlist);
1137out_close_evlist:
1138 perf_evlist__close(evlist);
1139out_delete_maps:
1140 perf_evlist__delete_maps(evlist);
514f1c67
ACM
1141out_delete_evlist:
1142 perf_evlist__delete(evlist);
1143out:
1144 return err;
1145}
1146
6810fc91
DA
1147static int trace__replay(struct trace *trace)
1148{
1149 const struct perf_evsel_str_handler handlers[] = {
1150 { "raw_syscalls:sys_enter", trace__sys_enter, },
1151 { "raw_syscalls:sys_exit", trace__sys_exit, },
1152 };
1153
1154 struct perf_session *session;
1155 int err = -1;
1156
1157 trace->tool.sample = trace__process_sample;
1158 trace->tool.mmap = perf_event__process_mmap;
384c671e 1159 trace->tool.mmap2 = perf_event__process_mmap2;
6810fc91
DA
1160 trace->tool.comm = perf_event__process_comm;
1161 trace->tool.exit = perf_event__process_exit;
1162 trace->tool.fork = perf_event__process_fork;
1163 trace->tool.attr = perf_event__process_attr;
1164 trace->tool.tracing_data = perf_event__process_tracing_data;
1165 trace->tool.build_id = perf_event__process_build_id;
1166
1167 trace->tool.ordered_samples = true;
1168 trace->tool.ordering_requires_timestamps = true;
1169
1170 /* add tid to output */
1171 trace->multiple_threads = true;
1172
1173 if (symbol__init() < 0)
1174 return -1;
1175
1176 session = perf_session__new(input_name, O_RDONLY, 0, false,
1177 &trace->tool);
1178 if (session == NULL)
1179 return -ENOMEM;
1180
1181 err = perf_session__set_tracepoints_handlers(session, handlers);
1182 if (err)
1183 goto out;
1184
1185 if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) {
1186 pr_err("Data file does not have raw_syscalls:sys_enter events\n");
1187 goto out;
1188 }
1189
1190 if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) {
1191 pr_err("Data file does not have raw_syscalls:sys_exit events\n");
1192 goto out;
1193 }
1194
bdc89661
DA
1195 err = parse_target_str(trace);
1196 if (err != 0)
1197 goto out;
1198
6810fc91
DA
1199 setup_pager();
1200
1201 err = perf_session__process_events(session, &trace->tool);
1202 if (err)
1203 pr_err("Failed to process events, error %d", err);
1204
1205out:
1206 perf_session__delete(session);
1207
1208 return err;
1209}
1210
1302d88e
ACM
1211static size_t trace__fprintf_threads_header(FILE *fp)
1212{
1213 size_t printed;
1214
1215 printed = fprintf(fp, "\n _____________________________________________________________________\n");
1216 printed += fprintf(fp," __) Summary of events (__\n\n");
1217 printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
1218 printed += fprintf(fp," _____________________________________________________________________\n\n");
1219
1220 return printed;
1221}
1222
1223static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
1224{
1225 size_t printed = trace__fprintf_threads_header(fp);
1226 struct rb_node *nd;
1227
1228 for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
1229 struct thread *thread = rb_entry(nd, struct thread, rb_node);
1230 struct thread_trace *ttrace = thread->priv;
1231 const char *color;
1232 double ratio;
1233
1234 if (ttrace == NULL)
1235 continue;
1236
1237 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
1238
1239 color = PERF_COLOR_NORMAL;
1240 if (ratio > 50.0)
1241 color = PERF_COLOR_RED;
1242 else if (ratio > 25.0)
1243 color = PERF_COLOR_GREEN;
1244 else if (ratio > 5.0)
1245 color = PERF_COLOR_YELLOW;
1246
1247 printed += color_fprintf(fp, color, "%20s", thread->comm);
38051234 1248 printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
1302d88e
ACM
1249 printed += color_fprintf(fp, color, "%5.1f%%", ratio);
1250 printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
1251 }
1252
1253 return printed;
1254}
1255
ae9ed035
ACM
1256static int trace__set_duration(const struct option *opt, const char *str,
1257 int unset __maybe_unused)
1258{
1259 struct trace *trace = opt->value;
1260
1261 trace->duration_filter = atof(str);
1262 return 0;
1263}
1264
c24ff998
ACM
1265static int trace__open_output(struct trace *trace, const char *filename)
1266{
1267 struct stat st;
1268
1269 if (!stat(filename, &st) && st.st_size) {
1270 char oldname[PATH_MAX];
1271
1272 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
1273 unlink(oldname);
1274 rename(filename, oldname);
1275 }
1276
1277 trace->output = fopen(filename, "w");
1278
1279 return trace->output == NULL ? -errno : 0;
1280}
1281
514f1c67
ACM
1282int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
1283{
1284 const char * const trace_usage[] = {
f15eb531
NK
1285 "perf trace [<options>] [<command>]",
1286 "perf trace [<options>] -- <command> [<options>]",
514f1c67
ACM
1287 NULL
1288 };
1289 struct trace trace = {
1290 .audit_machine = audit_detect_machine(),
1291 .syscalls = {
1292 . max = -1,
1293 },
1294 .opts = {
1295 .target = {
1296 .uid = UINT_MAX,
1297 .uses_mmap = true,
1298 },
1299 .user_freq = UINT_MAX,
1300 .user_interval = ULLONG_MAX,
1301 .no_delay = true,
1302 .mmap_pages = 1024,
1303 },
c24ff998 1304 .output = stdout,
514f1c67 1305 };
c24ff998 1306 const char *output_name = NULL;
2ae3a312 1307 const char *ev_qualifier_str = NULL;
514f1c67 1308 const struct option trace_options[] = {
2ae3a312
ACM
1309 OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
1310 "list of events to trace"),
c24ff998 1311 OPT_STRING('o', "output", &output_name, "file", "output file name"),
6810fc91 1312 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
514f1c67
ACM
1313 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
1314 "trace events on existing process id"),
ac9be8ee 1315 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
514f1c67 1316 "trace events on existing thread id"),
ac9be8ee 1317 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
514f1c67 1318 "system-wide collection from all CPUs"),
ac9be8ee 1319 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
514f1c67 1320 "list of cpus to monitor"),
6810fc91 1321 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
514f1c67 1322 "child tasks do not inherit counters"),
ac9be8ee 1323 OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
514f1c67 1324 "number of mmap data pages"),
ac9be8ee 1325 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
514f1c67 1326 "user to profile"),
ae9ed035
ACM
1327 OPT_CALLBACK(0, "duration", &trace, "float",
1328 "show only events with duration > N.M ms",
1329 trace__set_duration),
1302d88e 1330 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
7c304ee0 1331 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
514f1c67
ACM
1332 OPT_END()
1333 };
1334 int err;
32caf0d1 1335 char bf[BUFSIZ];
514f1c67
ACM
1336
1337 argc = parse_options(argc, argv, trace_options, trace_usage, 0);
514f1c67 1338
c24ff998
ACM
1339 if (output_name != NULL) {
1340 err = trace__open_output(&trace, output_name);
1341 if (err < 0) {
1342 perror("failed to create output file");
1343 goto out;
1344 }
1345 }
1346
2ae3a312 1347 if (ev_qualifier_str != NULL) {
b059efdf
ACM
1348 const char *s = ev_qualifier_str;
1349
1350 trace.not_ev_qualifier = *s == '!';
1351 if (trace.not_ev_qualifier)
1352 ++s;
1353 trace.ev_qualifier = strlist__new(true, s);
2ae3a312 1354 if (trace.ev_qualifier == NULL) {
c24ff998
ACM
1355 fputs("Not enough memory to parse event qualifier",
1356 trace.output);
1357 err = -ENOMEM;
1358 goto out_close;
2ae3a312
ACM
1359 }
1360 }
1361
32caf0d1
NK
1362 err = perf_target__validate(&trace.opts.target);
1363 if (err) {
1364 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
c24ff998
ACM
1365 fprintf(trace.output, "%s", bf);
1366 goto out_close;
32caf0d1
NK
1367 }
1368
514f1c67
ACM
1369 err = perf_target__parse_uid(&trace.opts.target);
1370 if (err) {
514f1c67 1371 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
c24ff998
ACM
1372 fprintf(trace.output, "%s", bf);
1373 goto out_close;
514f1c67
ACM
1374 }
1375
f15eb531 1376 if (!argc && perf_target__none(&trace.opts.target))
ee76120e
NK
1377 trace.opts.target.system_wide = true;
1378
6810fc91
DA
1379 if (input_name)
1380 err = trace__replay(&trace);
1381 else
1382 err = trace__run(&trace, argc, argv);
1302d88e
ACM
1383
1384 if (trace.sched && !err)
c24ff998 1385 trace__fprintf_thread_summary(&trace, trace.output);
1302d88e 1386
c24ff998
ACM
1387out_close:
1388 if (output_name != NULL)
1389 fclose(trace.output);
1390out:
1302d88e 1391 return err;
514f1c67 1392}