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