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