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