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