perf trace: Add beautifier for lseek's whence 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 struct syscall_fmt {
210         const char *name;
211         const char *alias;
212         size_t     (*arg_scnprintf[6])(char *bf, size_t size, unsigned long arg, u8 *arg_mask);
213         bool       errmsg;
214         bool       timeout;
215         bool       hexret;
216 } syscall_fmts[] = {
217         { .name     = "access",     .errmsg = true, },
218         { .name     = "arch_prctl", .errmsg = true, .alias = "prctl", },
219         { .name     = "brk",        .hexret = true,
220           .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
221         { .name     = "mmap",       .hexret = true, },
222         { .name     = "connect",    .errmsg = true, },
223         { .name     = "fstat",      .errmsg = true, .alias = "newfstat", },
224         { .name     = "fstatat",    .errmsg = true, .alias = "newfstatat", },
225         { .name     = "futex",      .errmsg = true,
226           .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
227         { .name     = "ioctl",      .errmsg = true,
228           .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
229         { .name     = "lseek",      .errmsg = true,
230           .arg_scnprintf = { [2] = SCA_WHENCE, /* whence */ }, },
231         { .name     = "lstat",      .errmsg = true, .alias = "newlstat", },
232         { .name     = "madvise",    .errmsg = true,
233           .arg_scnprintf = { [0] = SCA_HEX,      /* start */
234                              [2] = SCA_MADV_BHV, /* behavior */ }, },
235         { .name     = "mmap",       .hexret = true,
236           .arg_scnprintf = { [0] = SCA_HEX,       /* addr */
237                              [2] = SCA_MMAP_PROT, /* prot */
238                              [3] = SCA_MMAP_FLAGS, /* flags */ }, },
239         { .name     = "mprotect",   .errmsg = true,
240           .arg_scnprintf = { [0] = SCA_HEX, /* start */
241                              [2] = SCA_MMAP_PROT, /* prot */ }, },
242         { .name     = "mremap",     .hexret = true,
243           .arg_scnprintf = { [0] = SCA_HEX, /* addr */
244                              [4] = SCA_HEX, /* new_addr */ }, },
245         { .name     = "munmap",     .errmsg = true,
246           .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
247         { .name     = "open",       .errmsg = true, },
248         { .name     = "poll",       .errmsg = true, .timeout = true, },
249         { .name     = "ppoll",      .errmsg = true, .timeout = true, },
250         { .name     = "pread",      .errmsg = true, .alias = "pread64", },
251         { .name     = "pwrite",     .errmsg = true, .alias = "pwrite64", },
252         { .name     = "read",       .errmsg = true, },
253         { .name     = "recvfrom",   .errmsg = true, },
254         { .name     = "select",     .errmsg = true, .timeout = true, },
255         { .name     = "socket",     .errmsg = true, },
256         { .name     = "stat",       .errmsg = true, .alias = "newstat", },
257         { .name     = "uname",      .errmsg = true, .alias = "newuname", },
258 };
259
260 static int syscall_fmt__cmp(const void *name, const void *fmtp)
261 {
262         const struct syscall_fmt *fmt = fmtp;
263         return strcmp(name, fmt->name);
264 }
265
266 static struct syscall_fmt *syscall_fmt__find(const char *name)
267 {
268         const int nmemb = ARRAY_SIZE(syscall_fmts);
269         return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
270 }
271
272 struct syscall {
273         struct event_format *tp_format;
274         const char          *name;
275         bool                filtered;
276         struct syscall_fmt  *fmt;
277         size_t              (**arg_scnprintf)(char *bf, size_t size,
278                                               unsigned long arg, u8 *args_mask);
279 };
280
281 static size_t fprintf_duration(unsigned long t, FILE *fp)
282 {
283         double duration = (double)t / NSEC_PER_MSEC;
284         size_t printed = fprintf(fp, "(");
285
286         if (duration >= 1.0)
287                 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
288         else if (duration >= 0.01)
289                 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
290         else
291                 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
292         return printed + fprintf(fp, "): ");
293 }
294
295 struct thread_trace {
296         u64               entry_time;
297         u64               exit_time;
298         bool              entry_pending;
299         unsigned long     nr_events;
300         char              *entry_str;
301         double            runtime_ms;
302 };
303
304 static struct thread_trace *thread_trace__new(void)
305 {
306         return zalloc(sizeof(struct thread_trace));
307 }
308
309 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
310 {
311         struct thread_trace *ttrace;
312
313         if (thread == NULL)
314                 goto fail;
315
316         if (thread->priv == NULL)
317                 thread->priv = thread_trace__new();
318                 
319         if (thread->priv == NULL)
320                 goto fail;
321
322         ttrace = thread->priv;
323         ++ttrace->nr_events;
324
325         return ttrace;
326 fail:
327         color_fprintf(fp, PERF_COLOR_RED,
328                       "WARNING: not enough memory, dropping samples!\n");
329         return NULL;
330 }
331
332 struct trace {
333         struct perf_tool        tool;
334         int                     audit_machine;
335         struct {
336                 int             max;
337                 struct syscall  *table;
338         } syscalls;
339         struct perf_record_opts opts;
340         struct machine          host;
341         u64                     base_time;
342         FILE                    *output;
343         unsigned long           nr_events;
344         struct strlist          *ev_qualifier;
345         bool                    not_ev_qualifier;
346         struct intlist          *tid_list;
347         struct intlist          *pid_list;
348         bool                    sched;
349         bool                    multiple_threads;
350         double                  duration_filter;
351         double                  runtime_ms;
352 };
353
354 static bool trace__filter_duration(struct trace *trace, double t)
355 {
356         return t < (trace->duration_filter * NSEC_PER_MSEC);
357 }
358
359 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
360 {
361         double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
362
363         return fprintf(fp, "%10.3f ", ts);
364 }
365
366 static bool done = false;
367
368 static void sig_handler(int sig __maybe_unused)
369 {
370         done = true;
371 }
372
373 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
374                                         u64 duration, u64 tstamp, FILE *fp)
375 {
376         size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
377         printed += fprintf_duration(duration, fp);
378
379         if (trace->multiple_threads)
380                 printed += fprintf(fp, "%d ", thread->tid);
381
382         return printed;
383 }
384
385 static int trace__process_event(struct trace *trace, struct machine *machine,
386                                 union perf_event *event)
387 {
388         int ret = 0;
389
390         switch (event->header.type) {
391         case PERF_RECORD_LOST:
392                 color_fprintf(trace->output, PERF_COLOR_RED,
393                               "LOST %" PRIu64 " events!\n", event->lost.lost);
394                 ret = machine__process_lost_event(machine, event);
395         default:
396                 ret = machine__process_event(machine, event);
397                 break;
398         }
399
400         return ret;
401 }
402
403 static int trace__tool_process(struct perf_tool *tool,
404                                union perf_event *event,
405                                struct perf_sample *sample __maybe_unused,
406                                struct machine *machine)
407 {
408         struct trace *trace = container_of(tool, struct trace, tool);
409         return trace__process_event(trace, machine, event);
410 }
411
412 static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
413 {
414         int err = symbol__init();
415
416         if (err)
417                 return err;
418
419         machine__init(&trace->host, "", HOST_KERNEL_ID);
420         machine__create_kernel_maps(&trace->host);
421
422         if (perf_target__has_task(&trace->opts.target)) {
423                 err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
424                                                         trace__tool_process,
425                                                         &trace->host);
426         } else {
427                 err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
428                                                      &trace->host);
429         }
430
431         if (err)
432                 symbol__exit();
433
434         return err;
435 }
436
437 static int syscall__set_arg_fmts(struct syscall *sc)
438 {
439         struct format_field *field;
440         int idx = 0;
441
442         sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
443         if (sc->arg_scnprintf == NULL)
444                 return -1;
445
446         for (field = sc->tp_format->format.fields->next; field; field = field->next) {
447                 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
448                         sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
449                 else if (field->flags & FIELD_IS_POINTER)
450                         sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
451                 ++idx;
452         }
453
454         return 0;
455 }
456
457 static int trace__read_syscall_info(struct trace *trace, int id)
458 {
459         char tp_name[128];
460         struct syscall *sc;
461         const char *name = audit_syscall_to_name(id, trace->audit_machine);
462
463         if (name == NULL)
464                 return -1;
465
466         if (id > trace->syscalls.max) {
467                 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
468
469                 if (nsyscalls == NULL)
470                         return -1;
471
472                 if (trace->syscalls.max != -1) {
473                         memset(nsyscalls + trace->syscalls.max + 1, 0,
474                                (id - trace->syscalls.max) * sizeof(*sc));
475                 } else {
476                         memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
477                 }
478
479                 trace->syscalls.table = nsyscalls;
480                 trace->syscalls.max   = id;
481         }
482
483         sc = trace->syscalls.table + id;
484         sc->name = name;
485
486         if (trace->ev_qualifier) {
487                 bool in = strlist__find(trace->ev_qualifier, name) != NULL;
488
489                 if (!(in ^ trace->not_ev_qualifier)) {
490                         sc->filtered = true;
491                         /*
492                          * No need to do read tracepoint information since this will be
493                          * filtered out.
494                          */
495                         return 0;
496                 }
497         }
498
499         sc->fmt  = syscall_fmt__find(sc->name);
500
501         snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
502         sc->tp_format = event_format__new("syscalls", tp_name);
503
504         if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
505                 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
506                 sc->tp_format = event_format__new("syscalls", tp_name);
507         }
508
509         if (sc->tp_format == NULL)
510                 return -1;
511
512         return syscall__set_arg_fmts(sc);
513 }
514
515 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
516                                       unsigned long *args)
517 {
518         int i = 0;
519         size_t printed = 0;
520
521         if (sc->tp_format != NULL) {
522                 struct format_field *field;
523                 u8 mask = 0, bit = 1;
524
525                 for (field = sc->tp_format->format.fields->next; field;
526                      field = field->next, ++i, bit <<= 1) {
527                         if (mask & bit)
528                                 continue;
529
530                         printed += scnprintf(bf + printed, size - printed,
531                                              "%s%s: ", printed ? ", " : "", field->name);
532
533                         if (sc->arg_scnprintf && sc->arg_scnprintf[i]) {
534                                 printed += sc->arg_scnprintf[i](bf + printed, size - printed,
535                                                                 args[i], &mask);
536                         } else {
537                                 printed += scnprintf(bf + printed, size - printed,
538                                                      "%ld", args[i]);
539                         }
540                 }
541         } else {
542                 while (i < 6) {
543                         printed += scnprintf(bf + printed, size - printed,
544                                              "%sarg%d: %ld",
545                                              printed ? ", " : "", i, args[i]);
546                         ++i;
547                 }
548         }
549
550         return printed;
551 }
552
553 typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
554                                   struct perf_sample *sample);
555
556 static struct syscall *trace__syscall_info(struct trace *trace,
557                                            struct perf_evsel *evsel,
558                                            struct perf_sample *sample)
559 {
560         int id = perf_evsel__intval(evsel, sample, "id");
561
562         if (id < 0) {
563
564                 /*
565                  * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
566                  * before that, leaving at a higher verbosity level till that is
567                  * explained. Reproduced with plain ftrace with:
568                  *
569                  * echo 1 > /t/events/raw_syscalls/sys_exit/enable
570                  * grep "NR -1 " /t/trace_pipe
571                  *
572                  * After generating some load on the machine.
573                  */
574                 if (verbose > 1) {
575                         static u64 n;
576                         fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
577                                 id, perf_evsel__name(evsel), ++n);
578                 }
579                 return NULL;
580         }
581
582         if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
583             trace__read_syscall_info(trace, id))
584                 goto out_cant_read;
585
586         if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
587                 goto out_cant_read;
588
589         return &trace->syscalls.table[id];
590
591 out_cant_read:
592         if (verbose) {
593                 fprintf(trace->output, "Problems reading syscall %d", id);
594                 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
595                         fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
596                 fputs(" information\n", trace->output);
597         }
598         return NULL;
599 }
600
601 static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
602                             struct perf_sample *sample)
603 {
604         char *msg;
605         void *args;
606         size_t printed = 0;
607         struct thread *thread;
608         struct syscall *sc = trace__syscall_info(trace, evsel, sample);
609         struct thread_trace *ttrace;
610
611         if (sc == NULL)
612                 return -1;
613
614         if (sc->filtered)
615                 return 0;
616
617         thread = machine__findnew_thread(&trace->host, sample->pid,
618                                          sample->tid);
619         ttrace = thread__trace(thread, trace->output);
620         if (ttrace == NULL)
621                 return -1;
622
623         args = perf_evsel__rawptr(evsel, sample, "args");
624         if (args == NULL) {
625                 fprintf(trace->output, "Problems reading syscall arguments\n");
626                 return -1;
627         }
628
629         ttrace = thread->priv;
630
631         if (ttrace->entry_str == NULL) {
632                 ttrace->entry_str = malloc(1024);
633                 if (!ttrace->entry_str)
634                         return -1;
635         }
636
637         ttrace->entry_time = sample->time;
638         msg = ttrace->entry_str;
639         printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
640
641         printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed,  args);
642
643         if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
644                 if (!trace->duration_filter) {
645                         trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
646                         fprintf(trace->output, "%-70s\n", ttrace->entry_str);
647                 }
648         } else
649                 ttrace->entry_pending = true;
650
651         return 0;
652 }
653
654 static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
655                            struct perf_sample *sample)
656 {
657         int ret;
658         u64 duration = 0;
659         struct thread *thread;
660         struct syscall *sc = trace__syscall_info(trace, evsel, sample);
661         struct thread_trace *ttrace;
662
663         if (sc == NULL)
664                 return -1;
665
666         if (sc->filtered)
667                 return 0;
668
669         thread = machine__findnew_thread(&trace->host, sample->pid,
670                                          sample->tid);
671         ttrace = thread__trace(thread, trace->output);
672         if (ttrace == NULL)
673                 return -1;
674
675         ret = perf_evsel__intval(evsel, sample, "ret");
676
677         ttrace = thread->priv;
678
679         ttrace->exit_time = sample->time;
680
681         if (ttrace->entry_time) {
682                 duration = sample->time - ttrace->entry_time;
683                 if (trace__filter_duration(trace, duration))
684                         goto out;
685         } else if (trace->duration_filter)
686                 goto out;
687
688         trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
689
690         if (ttrace->entry_pending) {
691                 fprintf(trace->output, "%-70s", ttrace->entry_str);
692         } else {
693                 fprintf(trace->output, " ... [");
694                 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
695                 fprintf(trace->output, "]: %s()", sc->name);
696         }
697
698         if (sc->fmt == NULL) {
699 signed_print:
700                 fprintf(trace->output, ") = %d", ret);
701         } else if (ret < 0 && sc->fmt->errmsg) {
702                 char bf[256];
703                 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
704                            *e = audit_errno_to_name(-ret);
705
706                 fprintf(trace->output, ") = -1 %s %s", e, emsg);
707         } else if (ret == 0 && sc->fmt->timeout)
708                 fprintf(trace->output, ") = 0 Timeout");
709         else if (sc->fmt->hexret)
710                 fprintf(trace->output, ") = %#x", ret);
711         else
712                 goto signed_print;
713
714         fputc('\n', trace->output);
715 out:
716         ttrace->entry_pending = false;
717
718         return 0;
719 }
720
721 static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
722                                      struct perf_sample *sample)
723 {
724         u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
725         double runtime_ms = (double)runtime / NSEC_PER_MSEC;
726         struct thread *thread = machine__findnew_thread(&trace->host,
727                                                         sample->pid,
728                                                         sample->tid);
729         struct thread_trace *ttrace = thread__trace(thread, trace->output);
730
731         if (ttrace == NULL)
732                 goto out_dump;
733
734         ttrace->runtime_ms += runtime_ms;
735         trace->runtime_ms += runtime_ms;
736         return 0;
737
738 out_dump:
739         fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
740                evsel->name,
741                perf_evsel__strval(evsel, sample, "comm"),
742                (pid_t)perf_evsel__intval(evsel, sample, "pid"),
743                runtime,
744                perf_evsel__intval(evsel, sample, "vruntime"));
745         return 0;
746 }
747
748 static bool skip_sample(struct trace *trace, struct perf_sample *sample)
749 {
750         if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
751             (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
752                 return false;
753
754         if (trace->pid_list || trace->tid_list)
755                 return true;
756
757         return false;
758 }
759
760 static int trace__process_sample(struct perf_tool *tool,
761                                  union perf_event *event __maybe_unused,
762                                  struct perf_sample *sample,
763                                  struct perf_evsel *evsel,
764                                  struct machine *machine __maybe_unused)
765 {
766         struct trace *trace = container_of(tool, struct trace, tool);
767         int err = 0;
768
769         tracepoint_handler handler = evsel->handler.func;
770
771         if (skip_sample(trace, sample))
772                 return 0;
773
774         if (trace->base_time == 0)
775                 trace->base_time = sample->time;
776
777         if (handler)
778                 handler(trace, evsel, sample);
779
780         return err;
781 }
782
783 static bool
784 perf_session__has_tp(struct perf_session *session, const char *name)
785 {
786         struct perf_evsel *evsel;
787
788         evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name);
789
790         return evsel != NULL;
791 }
792
793 static int parse_target_str(struct trace *trace)
794 {
795         if (trace->opts.target.pid) {
796                 trace->pid_list = intlist__new(trace->opts.target.pid);
797                 if (trace->pid_list == NULL) {
798                         pr_err("Error parsing process id string\n");
799                         return -EINVAL;
800                 }
801         }
802
803         if (trace->opts.target.tid) {
804                 trace->tid_list = intlist__new(trace->opts.target.tid);
805                 if (trace->tid_list == NULL) {
806                         pr_err("Error parsing thread id string\n");
807                         return -EINVAL;
808                 }
809         }
810
811         return 0;
812 }
813
814 static int trace__run(struct trace *trace, int argc, const char **argv)
815 {
816         struct perf_evlist *evlist = perf_evlist__new();
817         struct perf_evsel *evsel;
818         int err = -1, i;
819         unsigned long before;
820         const bool forks = argc > 0;
821
822         if (evlist == NULL) {
823                 fprintf(trace->output, "Not enough memory to run!\n");
824                 goto out;
825         }
826
827         if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
828             perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
829                 fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
830                 goto out_delete_evlist;
831         }
832
833         if (trace->sched &&
834             perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
835                                    trace__sched_stat_runtime)) {
836                 fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
837                 goto out_delete_evlist;
838         }
839
840         err = perf_evlist__create_maps(evlist, &trace->opts.target);
841         if (err < 0) {
842                 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
843                 goto out_delete_evlist;
844         }
845
846         err = trace__symbols_init(trace, evlist);
847         if (err < 0) {
848                 fprintf(trace->output, "Problems initializing symbol libraries!\n");
849                 goto out_delete_maps;
850         }
851
852         perf_evlist__config(evlist, &trace->opts);
853
854         signal(SIGCHLD, sig_handler);
855         signal(SIGINT, sig_handler);
856
857         if (forks) {
858                 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
859                                                     argv, false, false);
860                 if (err < 0) {
861                         fprintf(trace->output, "Couldn't run the workload!\n");
862                         goto out_delete_maps;
863                 }
864         }
865
866         err = perf_evlist__open(evlist);
867         if (err < 0) {
868                 fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
869                 goto out_delete_maps;
870         }
871
872         err = perf_evlist__mmap(evlist, UINT_MAX, false);
873         if (err < 0) {
874                 fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
875                 goto out_close_evlist;
876         }
877
878         perf_evlist__enable(evlist);
879
880         if (forks)
881                 perf_evlist__start_workload(evlist);
882
883         trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
884 again:
885         before = trace->nr_events;
886
887         for (i = 0; i < evlist->nr_mmaps; i++) {
888                 union perf_event *event;
889
890                 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
891                         const u32 type = event->header.type;
892                         tracepoint_handler handler;
893                         struct perf_sample sample;
894
895                         ++trace->nr_events;
896
897                         err = perf_evlist__parse_sample(evlist, event, &sample);
898                         if (err) {
899                                 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
900                                 continue;
901                         }
902
903                         if (trace->base_time == 0)
904                                 trace->base_time = sample.time;
905
906                         if (type != PERF_RECORD_SAMPLE) {
907                                 trace__process_event(trace, &trace->host, event);
908                                 continue;
909                         }
910
911                         evsel = perf_evlist__id2evsel(evlist, sample.id);
912                         if (evsel == NULL) {
913                                 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
914                                 continue;
915                         }
916
917                         if (sample.raw_data == NULL) {
918                                 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
919                                        perf_evsel__name(evsel), sample.tid,
920                                        sample.cpu, sample.raw_size);
921                                 continue;
922                         }
923
924                         handler = evsel->handler.func;
925                         handler(trace, evsel, &sample);
926                 }
927         }
928
929         if (trace->nr_events == before) {
930                 if (done)
931                         goto out_unmap_evlist;
932
933                 poll(evlist->pollfd, evlist->nr_fds, -1);
934         }
935
936         if (done)
937                 perf_evlist__disable(evlist);
938
939         goto again;
940
941 out_unmap_evlist:
942         perf_evlist__munmap(evlist);
943 out_close_evlist:
944         perf_evlist__close(evlist);
945 out_delete_maps:
946         perf_evlist__delete_maps(evlist);
947 out_delete_evlist:
948         perf_evlist__delete(evlist);
949 out:
950         return err;
951 }
952
953 static int trace__replay(struct trace *trace)
954 {
955         const struct perf_evsel_str_handler handlers[] = {
956                 { "raw_syscalls:sys_enter",  trace__sys_enter, },
957                 { "raw_syscalls:sys_exit",   trace__sys_exit, },
958         };
959
960         struct perf_session *session;
961         int err = -1;
962
963         trace->tool.sample        = trace__process_sample;
964         trace->tool.mmap          = perf_event__process_mmap;
965         trace->tool.comm          = perf_event__process_comm;
966         trace->tool.exit          = perf_event__process_exit;
967         trace->tool.fork          = perf_event__process_fork;
968         trace->tool.attr          = perf_event__process_attr;
969         trace->tool.tracing_data = perf_event__process_tracing_data;
970         trace->tool.build_id      = perf_event__process_build_id;
971
972         trace->tool.ordered_samples = true;
973         trace->tool.ordering_requires_timestamps = true;
974
975         /* add tid to output */
976         trace->multiple_threads = true;
977
978         if (symbol__init() < 0)
979                 return -1;
980
981         session = perf_session__new(input_name, O_RDONLY, 0, false,
982                                     &trace->tool);
983         if (session == NULL)
984                 return -ENOMEM;
985
986         err = perf_session__set_tracepoints_handlers(session, handlers);
987         if (err)
988                 goto out;
989
990         if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) {
991                 pr_err("Data file does not have raw_syscalls:sys_enter events\n");
992                 goto out;
993         }
994
995         if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) {
996                 pr_err("Data file does not have raw_syscalls:sys_exit events\n");
997                 goto out;
998         }
999
1000         err = parse_target_str(trace);
1001         if (err != 0)
1002                 goto out;
1003
1004         setup_pager();
1005
1006         err = perf_session__process_events(session, &trace->tool);
1007         if (err)
1008                 pr_err("Failed to process events, error %d", err);
1009
1010 out:
1011         perf_session__delete(session);
1012
1013         return err;
1014 }
1015
1016 static size_t trace__fprintf_threads_header(FILE *fp)
1017 {
1018         size_t printed;
1019
1020         printed  = fprintf(fp, "\n _____________________________________________________________________\n");
1021         printed += fprintf(fp," __)    Summary of events    (__\n\n");
1022         printed += fprintf(fp,"              [ task - pid ]     [ events ] [ ratio ]  [ runtime ]\n");
1023         printed += fprintf(fp," _____________________________________________________________________\n\n");
1024
1025         return printed;
1026 }
1027
1028 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
1029 {
1030         size_t printed = trace__fprintf_threads_header(fp);
1031         struct rb_node *nd;
1032
1033         for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
1034                 struct thread *thread = rb_entry(nd, struct thread, rb_node);
1035                 struct thread_trace *ttrace = thread->priv;
1036                 const char *color;
1037                 double ratio;
1038
1039                 if (ttrace == NULL)
1040                         continue;
1041
1042                 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
1043
1044                 color = PERF_COLOR_NORMAL;
1045                 if (ratio > 50.0)
1046                         color = PERF_COLOR_RED;
1047                 else if (ratio > 25.0)
1048                         color = PERF_COLOR_GREEN;
1049                 else if (ratio > 5.0)
1050                         color = PERF_COLOR_YELLOW;
1051
1052                 printed += color_fprintf(fp, color, "%20s", thread->comm);
1053                 printed += fprintf(fp, " - %-5d :%11lu   [", thread->tid, ttrace->nr_events);
1054                 printed += color_fprintf(fp, color, "%5.1f%%", ratio);
1055                 printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
1056         }
1057
1058         return printed;
1059 }
1060
1061 static int trace__set_duration(const struct option *opt, const char *str,
1062                                int unset __maybe_unused)
1063 {
1064         struct trace *trace = opt->value;
1065
1066         trace->duration_filter = atof(str);
1067         return 0;
1068 }
1069
1070 static int trace__open_output(struct trace *trace, const char *filename)
1071 {
1072         struct stat st;
1073
1074         if (!stat(filename, &st) && st.st_size) {
1075                 char oldname[PATH_MAX];
1076
1077                 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
1078                 unlink(oldname);
1079                 rename(filename, oldname);
1080         }
1081
1082         trace->output = fopen(filename, "w");
1083
1084         return trace->output == NULL ? -errno : 0;
1085 }
1086
1087 int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
1088 {
1089         const char * const trace_usage[] = {
1090                 "perf trace [<options>] [<command>]",
1091                 "perf trace [<options>] -- <command> [<options>]",
1092                 NULL
1093         };
1094         struct trace trace = {
1095                 .audit_machine = audit_detect_machine(),
1096                 .syscalls = {
1097                         . max = -1,
1098                 },
1099                 .opts = {
1100                         .target = {
1101                                 .uid       = UINT_MAX,
1102                                 .uses_mmap = true,
1103                         },
1104                         .user_freq     = UINT_MAX,
1105                         .user_interval = ULLONG_MAX,
1106                         .no_delay      = true,
1107                         .mmap_pages    = 1024,
1108                 },
1109                 .output = stdout,
1110         };
1111         const char *output_name = NULL;
1112         const char *ev_qualifier_str = NULL;
1113         const struct option trace_options[] = {
1114         OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
1115                     "list of events to trace"),
1116         OPT_STRING('o', "output", &output_name, "file", "output file name"),
1117         OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
1118         OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
1119                     "trace events on existing process id"),
1120         OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
1121                     "trace events on existing thread id"),
1122         OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
1123                     "system-wide collection from all CPUs"),
1124         OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
1125                     "list of cpus to monitor"),
1126         OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
1127                     "child tasks do not inherit counters"),
1128         OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
1129                      "number of mmap data pages"),
1130         OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
1131                    "user to profile"),
1132         OPT_CALLBACK(0, "duration", &trace, "float",
1133                      "show only events with duration > N.M ms",
1134                      trace__set_duration),
1135         OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
1136         OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1137         OPT_END()
1138         };
1139         int err;
1140         char bf[BUFSIZ];
1141
1142         argc = parse_options(argc, argv, trace_options, trace_usage, 0);
1143
1144         if (output_name != NULL) {
1145                 err = trace__open_output(&trace, output_name);
1146                 if (err < 0) {
1147                         perror("failed to create output file");
1148                         goto out;
1149                 }
1150         }
1151
1152         if (ev_qualifier_str != NULL) {
1153                 const char *s = ev_qualifier_str;
1154
1155                 trace.not_ev_qualifier = *s == '!';
1156                 if (trace.not_ev_qualifier)
1157                         ++s;
1158                 trace.ev_qualifier = strlist__new(true, s);
1159                 if (trace.ev_qualifier == NULL) {
1160                         fputs("Not enough memory to parse event qualifier",
1161                               trace.output);
1162                         err = -ENOMEM;
1163                         goto out_close;
1164                 }
1165         }
1166
1167         err = perf_target__validate(&trace.opts.target);
1168         if (err) {
1169                 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1170                 fprintf(trace.output, "%s", bf);
1171                 goto out_close;
1172         }
1173
1174         err = perf_target__parse_uid(&trace.opts.target);
1175         if (err) {
1176                 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1177                 fprintf(trace.output, "%s", bf);
1178                 goto out_close;
1179         }
1180
1181         if (!argc && perf_target__none(&trace.opts.target))
1182                 trace.opts.target.system_wide = true;
1183
1184         if (input_name)
1185                 err = trace__replay(&trace);
1186         else
1187                 err = trace__run(&trace, argc, argv);
1188
1189         if (trace.sched && !err)
1190                 trace__fprintf_thread_summary(&trace, trace.output);
1191
1192 out_close:
1193         if (output_name != NULL)
1194                 fclose(trace.output);
1195 out:
1196         return err;
1197 }