Merge branch 'x86-spinlocks-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / tools / perf / builtin-trace.c
CommitLineData
4e319027 1#include <traceevent/event-parse.h>
514f1c67 2#include "builtin.h"
752fde44 3#include "util/color.h"
7c304ee0 4#include "util/debug.h"
514f1c67 5#include "util/evlist.h"
752fde44 6#include "util/machine.h"
6810fc91 7#include "util/session.h"
752fde44 8#include "util/thread.h"
514f1c67 9#include "util/parse-options.h"
2ae3a312 10#include "util/strlist.h"
bdc89661 11#include "util/intlist.h"
514f1c67 12#include "util/thread_map.h"
514f1c67
ACM
13
14#include <libaudit.h>
15#include <stdlib.h>
ae685380 16#include <sys/mman.h>
f9da0b0c 17#include <linux/futex.h>
514f1c67 18
6e7eeb51 19static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
31cd3855
ACM
20 unsigned long arg,
21 u8 arg_idx __maybe_unused,
22 u8 *arg_mask __maybe_unused)
13d4ff3e
ACM
23{
24 return scnprintf(bf, size, "%#lx", arg);
25}
26
beccb2b5
ACM
27#define SCA_HEX syscall_arg__scnprintf_hex
28
579e7865 29static size_t syscall_arg__scnprintf_whence(char *bf, size_t size,
31cd3855
ACM
30 unsigned long arg,
31 u8 arg_idx __maybe_unused,
32 u8 *arg_mask __maybe_unused)
579e7865
ACM
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
6e7eeb51 56static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
31cd3855
ACM
57 unsigned long arg,
58 u8 arg_idx __maybe_unused,
59 u8 *arg_mask __maybe_unused)
ae685380
ACM
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
6e7eeb51 89static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
31cd3855
ACM
90 unsigned long arg, u8 arg_idx __maybe_unused,
91 u8 *arg_mask __maybe_unused)
941557e0
ACM
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);
f2935f3e 110#ifdef MAP_HUGETLB
941557e0 111 P_MMAP_FLAG(HUGETLB);
f2935f3e 112#endif
941557e0
ACM
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
6e7eeb51 131static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
31cd3855
ACM
132 unsigned long arg, u8 arg_idx __maybe_unused,
133 u8 *arg_mask __maybe_unused)
9e9716d1
ACM
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);
f2935f3e 153#ifdef MADV_HUGEPAGE
9e9716d1 154 P_MADV_BHV(HUGEPAGE);
f2935f3e
DA
155#endif
156#ifdef MADV_NOHUGEPAGE
9e9716d1 157 P_MADV_BHV(NOHUGEPAGE);
f2935f3e 158#endif
9e9716d1
ACM
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
31cd3855
ACM
174static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, unsigned long arg,
175 u8 arg_idx __maybe_unused, u8 *arg_mask)
f9da0b0c
ACM
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
be65a89a 218static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size,
31cd3855
ACM
219 unsigned long arg,
220 u8 arg_idx, u8 *arg_mask)
be65a89a
ACM
221{
222 int printed = 0, flags = arg;
223
224 if (!(flags & O_CREAT))
31cd3855 225 *arg_mask |= 1 << (arg_idx + 1); /* Mask the mode parm */
be65a89a
ACM
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
514f1c67
ACM
275static struct syscall_fmt {
276 const char *name;
aec1930b 277 const char *alias;
31cd3855 278 size_t (*arg_scnprintf[6])(char *bf, size_t size, unsigned long arg, u8 arg_idx, u8 *arg_mask);
514f1c67
ACM
279 bool errmsg;
280 bool timeout;
04b34729 281 bool hexret;
514f1c67 282} syscall_fmts[] = {
8b745263 283 { .name = "access", .errmsg = true, },
aec1930b 284 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
beccb2b5
ACM
285 { .name = "brk", .hexret = true,
286 .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
04b34729 287 { .name = "mmap", .hexret = true, },
a14bb860 288 { .name = "connect", .errmsg = true, },
aec1930b
ACM
289 { .name = "fstat", .errmsg = true, .alias = "newfstat", },
290 { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
f9da0b0c
ACM
291 { .name = "futex", .errmsg = true,
292 .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
beccb2b5
ACM
293 { .name = "ioctl", .errmsg = true,
294 .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
579e7865
ACM
295 { .name = "lseek", .errmsg = true,
296 .arg_scnprintf = { [2] = SCA_WHENCE, /* whence */ }, },
e5959683 297 { .name = "lstat", .errmsg = true, .alias = "newlstat", },
9e9716d1
ACM
298 { .name = "madvise", .errmsg = true,
299 .arg_scnprintf = { [0] = SCA_HEX, /* start */
300 [2] = SCA_MADV_BHV, /* behavior */ }, },
beccb2b5 301 { .name = "mmap", .hexret = true,
ae685380 302 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
941557e0
ACM
303 [2] = SCA_MMAP_PROT, /* prot */
304 [3] = SCA_MMAP_FLAGS, /* flags */ }, },
beccb2b5 305 { .name = "mprotect", .errmsg = true,
ae685380
ACM
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 */ }, },
beccb2b5
ACM
311 { .name = "munmap", .errmsg = true,
312 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
be65a89a
ACM
313 { .name = "open", .errmsg = true,
314 .arg_scnprintf = { [1] = SCA_OPEN_FLAGS, /* flags */ }, },
31cd3855
ACM
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 */ }, },
aec1930b
ACM
319 { .name = "poll", .errmsg = true, .timeout = true, },
320 { .name = "ppoll", .errmsg = true, .timeout = true, },
e5959683
ACM
321 { .name = "pread", .errmsg = true, .alias = "pread64", },
322 { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
aec1930b
ACM
323 { .name = "read", .errmsg = true, },
324 { .name = "recvfrom", .errmsg = true, },
325 { .name = "select", .errmsg = true, .timeout = true, },
8b745263 326 { .name = "socket", .errmsg = true, },
aec1930b 327 { .name = "stat", .errmsg = true, .alias = "newstat", },
e5959683 328 { .name = "uname", .errmsg = true, .alias = "newuname", },
514f1c67
ACM
329};
330
331static 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
337static 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
343struct syscall {
344 struct event_format *tp_format;
345 const char *name;
2ae3a312 346 bool filtered;
514f1c67 347 struct syscall_fmt *fmt;
6e7eeb51 348 size_t (**arg_scnprintf)(char *bf, size_t size,
31cd3855 349 unsigned long arg, u8 arg_idx, u8 *args_mask);
514f1c67
ACM
350};
351
60c907ab
ACM
352static 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);
c24ff998 363 return printed + fprintf(fp, "): ");
60c907ab
ACM
364}
365
752fde44
ACM
366struct thread_trace {
367 u64 entry_time;
368 u64 exit_time;
369 bool entry_pending;
efd5745e 370 unsigned long nr_events;
752fde44 371 char *entry_str;
1302d88e 372 double runtime_ms;
752fde44
ACM
373};
374
375static struct thread_trace *thread_trace__new(void)
376{
377 return zalloc(sizeof(struct thread_trace));
378}
379
c24ff998 380static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
752fde44 381{
efd5745e
ACM
382 struct thread_trace *ttrace;
383
752fde44
ACM
384 if (thread == NULL)
385 goto fail;
386
387 if (thread->priv == NULL)
388 thread->priv = thread_trace__new();
efd5745e 389
752fde44
ACM
390 if (thread->priv == NULL)
391 goto fail;
392
efd5745e
ACM
393 ttrace = thread->priv;
394 ++ttrace->nr_events;
395
396 return ttrace;
752fde44 397fail:
c24ff998 398 color_fprintf(fp, PERF_COLOR_RED,
752fde44
ACM
399 "WARNING: not enough memory, dropping samples!\n");
400 return NULL;
401}
402
514f1c67 403struct trace {
c24ff998 404 struct perf_tool tool;
514f1c67
ACM
405 int audit_machine;
406 struct {
407 int max;
408 struct syscall *table;
409 } syscalls;
410 struct perf_record_opts opts;
752fde44
ACM
411 struct machine host;
412 u64 base_time;
c24ff998 413 FILE *output;
efd5745e 414 unsigned long nr_events;
b059efdf
ACM
415 struct strlist *ev_qualifier;
416 bool not_ev_qualifier;
bdc89661
DA
417 struct intlist *tid_list;
418 struct intlist *pid_list;
1302d88e 419 bool sched;
752fde44 420 bool multiple_threads;
ae9ed035 421 double duration_filter;
1302d88e 422 double runtime_ms;
514f1c67
ACM
423};
424
ae9ed035
ACM
425static bool trace__filter_duration(struct trace *trace, double t)
426{
427 return t < (trace->duration_filter * NSEC_PER_MSEC);
428}
429
752fde44
ACM
430static 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
60c907ab 434 return fprintf(fp, "%10.3f ", ts);
752fde44
ACM
435}
436
f15eb531
NK
437static bool done = false;
438
439static void sig_handler(int sig __maybe_unused)
440{
441 done = true;
442}
443
752fde44 444static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
60c907ab 445 u64 duration, u64 tstamp, FILE *fp)
752fde44
ACM
446{
447 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
60c907ab 448 printed += fprintf_duration(duration, fp);
752fde44
ACM
449
450 if (trace->multiple_threads)
38051234 451 printed += fprintf(fp, "%d ", thread->tid);
752fde44
ACM
452
453 return printed;
454}
455
c24ff998
ACM
456static int trace__process_event(struct trace *trace, struct machine *machine,
457 union perf_event *event)
752fde44
ACM
458{
459 int ret = 0;
460
461 switch (event->header.type) {
462 case PERF_RECORD_LOST:
c24ff998 463 color_fprintf(trace->output, PERF_COLOR_RED,
752fde44
ACM
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
c24ff998 474static int trace__tool_process(struct perf_tool *tool,
752fde44
ACM
475 union perf_event *event,
476 struct perf_sample *sample __maybe_unused,
477 struct machine *machine)
478{
c24ff998
ACM
479 struct trace *trace = container_of(tool, struct trace, tool);
480 return trace__process_event(trace, machine, event);
752fde44
ACM
481}
482
483static 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)) {
c24ff998 494 err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
752fde44
ACM
495 trace__tool_process,
496 &trace->host);
497 } else {
c24ff998 498 err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
752fde44
ACM
499 &trace->host);
500 }
501
502 if (err)
503 symbol__exit();
504
505 return err;
506}
507
13d4ff3e
ACM
508static 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) {
beccb2b5
ACM
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)
13d4ff3e
ACM
521 sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
522 ++idx;
523 }
524
525 return 0;
526}
527
514f1c67
ACM
528static int trace__read_syscall_info(struct trace *trace, int id)
529{
530 char tp_name[128];
531 struct syscall *sc;
3a531260
ACM
532 const char *name = audit_syscall_to_name(id, trace->audit_machine);
533
534 if (name == NULL)
535 return -1;
514f1c67
ACM
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;
3a531260 555 sc->name = name;
2ae3a312 556
b059efdf
ACM
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 }
2ae3a312
ACM
568 }
569
3a531260 570 sc->fmt = syscall_fmt__find(sc->name);
514f1c67 571
aec1930b 572 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
514f1c67 573 sc->tp_format = event_format__new("syscalls", tp_name);
aec1930b
ACM
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 }
514f1c67 579
13d4ff3e
ACM
580 if (sc->tp_format == NULL)
581 return -1;
582
583 return syscall__set_arg_fmts(sc);
514f1c67
ACM
584}
585
752fde44
ACM
586static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
587 unsigned long *args)
514f1c67
ACM
588{
589 int i = 0;
590 size_t printed = 0;
591
592 if (sc->tp_format != NULL) {
593 struct format_field *field;
6e7eeb51
ACM
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;
514f1c67 600
752fde44 601 printed += scnprintf(bf + printed, size - printed,
13d4ff3e
ACM
602 "%s%s: ", printed ? ", " : "", field->name);
603
6e7eeb51
ACM
604 if (sc->arg_scnprintf && sc->arg_scnprintf[i]) {
605 printed += sc->arg_scnprintf[i](bf + printed, size - printed,
31cd3855 606 args[i], i, &mask);
6e7eeb51 607 } else {
13d4ff3e
ACM
608 printed += scnprintf(bf + printed, size - printed,
609 "%ld", args[i]);
6e7eeb51 610 }
514f1c67
ACM
611 }
612 } else {
613 while (i < 6) {
752fde44
ACM
614 printed += scnprintf(bf + printed, size - printed,
615 "%sarg%d: %ld",
616 printed ? ", " : "", i, args[i]);
514f1c67
ACM
617 ++i;
618 }
619 }
620
621 return printed;
622}
623
ba3d7dee
ACM
624typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
625 struct perf_sample *sample);
626
627static 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) {
adaa18bf
ACM
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 }
ba3d7dee
ACM
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
662out_cant_read:
7c304ee0
ACM
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 }
ba3d7dee
ACM
669 return NULL;
670}
671
672static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
673 struct perf_sample *sample)
674{
752fde44 675 char *msg;
ba3d7dee 676 void *args;
752fde44 677 size_t printed = 0;
2ae3a312 678 struct thread *thread;
ba3d7dee 679 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
2ae3a312
ACM
680 struct thread_trace *ttrace;
681
682 if (sc == NULL)
683 return -1;
ba3d7dee 684
2ae3a312
ACM
685 if (sc->filtered)
686 return 0;
687
314add6b
AH
688 thread = machine__findnew_thread(&trace->host, sample->pid,
689 sample->tid);
c24ff998 690 ttrace = thread__trace(thread, trace->output);
2ae3a312 691 if (ttrace == NULL)
ba3d7dee
ACM
692 return -1;
693
694 args = perf_evsel__rawptr(evsel, sample, "args");
695 if (args == NULL) {
c24ff998 696 fprintf(trace->output, "Problems reading syscall arguments\n");
ba3d7dee
ACM
697 return -1;
698 }
699
752fde44
ACM
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")) {
ae9ed035 715 if (!trace->duration_filter) {
c24ff998
ACM
716 trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
717 fprintf(trace->output, "%-70s\n", ttrace->entry_str);
ae9ed035 718 }
752fde44
ACM
719 } else
720 ttrace->entry_pending = true;
ba3d7dee
ACM
721
722 return 0;
723}
724
725static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
726 struct perf_sample *sample)
727{
728 int ret;
60c907ab 729 u64 duration = 0;
2ae3a312 730 struct thread *thread;
ba3d7dee 731 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
2ae3a312
ACM
732 struct thread_trace *ttrace;
733
734 if (sc == NULL)
735 return -1;
ba3d7dee 736
2ae3a312
ACM
737 if (sc->filtered)
738 return 0;
739
314add6b
AH
740 thread = machine__findnew_thread(&trace->host, sample->pid,
741 sample->tid);
c24ff998 742 ttrace = thread__trace(thread, trace->output);
2ae3a312 743 if (ttrace == NULL)
ba3d7dee
ACM
744 return -1;
745
746 ret = perf_evsel__intval(evsel, sample, "ret");
747
752fde44
ACM
748 ttrace = thread->priv;
749
750 ttrace->exit_time = sample->time;
751
ae9ed035 752 if (ttrace->entry_time) {
60c907ab 753 duration = sample->time - ttrace->entry_time;
ae9ed035
ACM
754 if (trace__filter_duration(trace, duration))
755 goto out;
756 } else if (trace->duration_filter)
757 goto out;
60c907ab 758
c24ff998 759 trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
752fde44
ACM
760
761 if (ttrace->entry_pending) {
c24ff998 762 fprintf(trace->output, "%-70s", ttrace->entry_str);
752fde44 763 } else {
c24ff998
ACM
764 fprintf(trace->output, " ... [");
765 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
766 fprintf(trace->output, "]: %s()", sc->name);
752fde44
ACM
767 }
768
da3c9a44
ACM
769 if (sc->fmt == NULL) {
770signed_print:
771 fprintf(trace->output, ") = %d", ret);
772 } else if (ret < 0 && sc->fmt->errmsg) {
ba3d7dee
ACM
773 char bf[256];
774 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
775 *e = audit_errno_to_name(-ret);
776
c24ff998 777 fprintf(trace->output, ") = -1 %s %s", e, emsg);
da3c9a44 778 } else if (ret == 0 && sc->fmt->timeout)
c24ff998 779 fprintf(trace->output, ") = 0 Timeout");
04b34729
ACM
780 else if (sc->fmt->hexret)
781 fprintf(trace->output, ") = %#x", ret);
ba3d7dee 782 else
da3c9a44 783 goto signed_print;
ba3d7dee 784
c24ff998 785 fputc('\n', trace->output);
ae9ed035 786out:
752fde44
ACM
787 ttrace->entry_pending = false;
788
ba3d7dee
ACM
789 return 0;
790}
791
1302d88e
ACM
792static 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;
314add6b
AH
797 struct thread *thread = machine__findnew_thread(&trace->host,
798 sample->pid,
799 sample->tid);
c24ff998 800 struct thread_trace *ttrace = thread__trace(thread, trace->output);
1302d88e
ACM
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
809out_dump:
c24ff998 810 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
1302d88e
ACM
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
bdc89661
DA
819static 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
6810fc91
DA
831static 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
bdc89661
DA
842 if (skip_sample(trace, sample))
843 return 0;
844
6810fc91
DA
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
854static bool
855perf_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
bdc89661
DA
864static 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
f15eb531 885static int trace__run(struct trace *trace, int argc, const char **argv)
514f1c67 886{
334fe7a3 887 struct perf_evlist *evlist = perf_evlist__new();
ba3d7dee 888 struct perf_evsel *evsel;
efd5745e
ACM
889 int err = -1, i;
890 unsigned long before;
f15eb531 891 const bool forks = argc > 0;
514f1c67
ACM
892
893 if (evlist == NULL) {
c24ff998 894 fprintf(trace->output, "Not enough memory to run!\n");
514f1c67
ACM
895 goto out;
896 }
897
39876e7d
ACM
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)) {
c24ff998 900 fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
514f1c67
ACM
901 goto out_delete_evlist;
902 }
903
1302d88e
ACM
904 if (trace->sched &&
905 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
906 trace__sched_stat_runtime)) {
c24ff998 907 fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
1302d88e
ACM
908 goto out_delete_evlist;
909 }
910
514f1c67
ACM
911 err = perf_evlist__create_maps(evlist, &trace->opts.target);
912 if (err < 0) {
c24ff998 913 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
514f1c67
ACM
914 goto out_delete_evlist;
915 }
916
752fde44
ACM
917 err = trace__symbols_init(trace, evlist);
918 if (err < 0) {
c24ff998 919 fprintf(trace->output, "Problems initializing symbol libraries!\n");
3beb0861 920 goto out_delete_maps;
752fde44
ACM
921 }
922
f77a9518 923 perf_evlist__config(evlist, &trace->opts);
514f1c67 924
f15eb531
NK
925 signal(SIGCHLD, sig_handler);
926 signal(SIGINT, sig_handler);
927
928 if (forks) {
6ef73ec4 929 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
55e162ea 930 argv, false, false);
f15eb531 931 if (err < 0) {
c24ff998 932 fprintf(trace->output, "Couldn't run the workload!\n");
3beb0861 933 goto out_delete_maps;
f15eb531
NK
934 }
935 }
936
514f1c67
ACM
937 err = perf_evlist__open(evlist);
938 if (err < 0) {
c24ff998 939 fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
3beb0861 940 goto out_delete_maps;
514f1c67
ACM
941 }
942
943 err = perf_evlist__mmap(evlist, UINT_MAX, false);
944 if (err < 0) {
c24ff998 945 fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
3beb0861 946 goto out_close_evlist;
514f1c67
ACM
947 }
948
949 perf_evlist__enable(evlist);
f15eb531
NK
950
951 if (forks)
952 perf_evlist__start_workload(evlist);
953
752fde44 954 trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
514f1c67 955again:
efd5745e 956 before = trace->nr_events;
514f1c67
ACM
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;
ba3d7dee 963 tracepoint_handler handler;
514f1c67 964 struct perf_sample sample;
514f1c67 965
efd5745e 966 ++trace->nr_events;
514f1c67 967
514f1c67
ACM
968 err = perf_evlist__parse_sample(evlist, event, &sample);
969 if (err) {
c24ff998 970 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
514f1c67
ACM
971 continue;
972 }
973
752fde44
ACM
974 if (trace->base_time == 0)
975 trace->base_time = sample.time;
976
977 if (type != PERF_RECORD_SAMPLE) {
c24ff998 978 trace__process_event(trace, &trace->host, event);
752fde44
ACM
979 continue;
980 }
981
514f1c67
ACM
982 evsel = perf_evlist__id2evsel(evlist, sample.id);
983 if (evsel == NULL) {
c24ff998 984 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
514f1c67
ACM
985 continue;
986 }
987
fc551f8d 988 if (sample.raw_data == NULL) {
c24ff998 989 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
fc551f8d
ACM
990 perf_evsel__name(evsel), sample.tid,
991 sample.cpu, sample.raw_size);
992 continue;
993 }
994
ba3d7dee
ACM
995 handler = evsel->handler.func;
996 handler(trace, evsel, &sample);
514f1c67
ACM
997 }
998 }
999
efd5745e 1000 if (trace->nr_events == before) {
f15eb531 1001 if (done)
3beb0861 1002 goto out_unmap_evlist;
f15eb531 1003
514f1c67 1004 poll(evlist->pollfd, evlist->nr_fds, -1);
f15eb531
NK
1005 }
1006
1007 if (done)
1008 perf_evlist__disable(evlist);
514f1c67
ACM
1009
1010 goto again;
1011
3beb0861
NK
1012out_unmap_evlist:
1013 perf_evlist__munmap(evlist);
1014out_close_evlist:
1015 perf_evlist__close(evlist);
1016out_delete_maps:
1017 perf_evlist__delete_maps(evlist);
514f1c67
ACM
1018out_delete_evlist:
1019 perf_evlist__delete(evlist);
1020out:
1021 return err;
1022}
1023
6810fc91
DA
1024static 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
bdc89661
DA
1071 err = parse_target_str(trace);
1072 if (err != 0)
1073 goto out;
1074
6810fc91
DA
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
1081out:
1082 perf_session__delete(session);
1083
1084 return err;
1085}
1086
1302d88e
ACM
1087static 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
1099static 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);
38051234 1124 printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
1302d88e
ACM
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
ae9ed035
ACM
1132static 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
c24ff998
ACM
1141static 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
514f1c67
ACM
1158int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
1159{
1160 const char * const trace_usage[] = {
f15eb531
NK
1161 "perf trace [<options>] [<command>]",
1162 "perf trace [<options>] -- <command> [<options>]",
514f1c67
ACM
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 },
c24ff998 1180 .output = stdout,
514f1c67 1181 };
c24ff998 1182 const char *output_name = NULL;
2ae3a312 1183 const char *ev_qualifier_str = NULL;
514f1c67 1184 const struct option trace_options[] = {
2ae3a312
ACM
1185 OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
1186 "list of events to trace"),
c24ff998 1187 OPT_STRING('o', "output", &output_name, "file", "output file name"),
6810fc91 1188 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
514f1c67
ACM
1189 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
1190 "trace events on existing process id"),
ac9be8ee 1191 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
514f1c67 1192 "trace events on existing thread id"),
ac9be8ee 1193 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
514f1c67 1194 "system-wide collection from all CPUs"),
ac9be8ee 1195 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
514f1c67 1196 "list of cpus to monitor"),
6810fc91 1197 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
514f1c67 1198 "child tasks do not inherit counters"),
ac9be8ee 1199 OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
514f1c67 1200 "number of mmap data pages"),
ac9be8ee 1201 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
514f1c67 1202 "user to profile"),
ae9ed035
ACM
1203 OPT_CALLBACK(0, "duration", &trace, "float",
1204 "show only events with duration > N.M ms",
1205 trace__set_duration),
1302d88e 1206 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
7c304ee0 1207 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
514f1c67
ACM
1208 OPT_END()
1209 };
1210 int err;
32caf0d1 1211 char bf[BUFSIZ];
514f1c67
ACM
1212
1213 argc = parse_options(argc, argv, trace_options, trace_usage, 0);
514f1c67 1214
c24ff998
ACM
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
2ae3a312 1223 if (ev_qualifier_str != NULL) {
b059efdf
ACM
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);
2ae3a312 1230 if (trace.ev_qualifier == NULL) {
c24ff998
ACM
1231 fputs("Not enough memory to parse event qualifier",
1232 trace.output);
1233 err = -ENOMEM;
1234 goto out_close;
2ae3a312
ACM
1235 }
1236 }
1237
32caf0d1
NK
1238 err = perf_target__validate(&trace.opts.target);
1239 if (err) {
1240 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
c24ff998
ACM
1241 fprintf(trace.output, "%s", bf);
1242 goto out_close;
32caf0d1
NK
1243 }
1244
514f1c67
ACM
1245 err = perf_target__parse_uid(&trace.opts.target);
1246 if (err) {
514f1c67 1247 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
c24ff998
ACM
1248 fprintf(trace.output, "%s", bf);
1249 goto out_close;
514f1c67
ACM
1250 }
1251
f15eb531 1252 if (!argc && perf_target__none(&trace.opts.target))
ee76120e
NK
1253 trace.opts.target.system_wide = true;
1254
6810fc91
DA
1255 if (input_name)
1256 err = trace__replay(&trace);
1257 else
1258 err = trace__run(&trace, argc, argv);
1302d88e
ACM
1259
1260 if (trace.sched && !err)
c24ff998 1261 trace__fprintf_thread_summary(&trace, trace.output);
1302d88e 1262
c24ff998
ACM
1263out_close:
1264 if (output_name != NULL)
1265 fclose(trace.output);
1266out:
1302d88e 1267 return err;
514f1c67 1268}