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