1 // SPDX-License-Identifier: GPL-2.0
4 #include "util/counts.h"
5 #include "util/debug.h"
7 #include <subcmd/exec-cmd.h>
8 #include "util/header.h"
9 #include <subcmd/parse-options.h>
10 #include "util/perf_regs.h"
11 #include "util/session.h"
12 #include "util/tool.h"
14 #include "util/srcline.h"
15 #include "util/symbol.h"
16 #include "util/thread.h"
17 #include "util/trace-event.h"
19 #include "util/evlist.h"
20 #include "util/evsel.h"
21 #include "util/evsel_fprintf.h"
22 #include "util/evswitch.h"
23 #include "util/sort.h"
24 #include "util/data.h"
25 #include "util/auxtrace.h"
26 #include "util/cpumap.h"
27 #include "util/thread_map.h"
28 #include "util/stat.h"
29 #include "util/color.h"
30 #include "util/string2.h"
31 #include "util/thread-stack.h"
32 #include "util/time-utils.h"
33 #include "util/path.h"
34 #include "util/event.h"
35 #include "util/mem-info.h"
37 #include "print_binary.h"
38 #include "print_insn.h"
40 #include <linux/bitmap.h>
41 #include <linux/kernel.h>
42 #include <linux/stringify.h>
43 #include <linux/time64.h>
44 #include <linux/zalloc.h>
45 #include <sys/utsname.h>
47 #include "util/mem-events.h"
48 #include "util/dump-insn.h"
53 #include <sys/param.h>
54 #include <sys/types.h>
58 #include <subcmd/pager.h>
59 #include <perf/evlist.h>
60 #include <linux/err.h>
61 #include "util/dlfilter.h"
62 #include "util/record.h"
63 #include "util/util.h"
64 #include "util/cgroup.h"
65 #include "util/annotate.h"
68 #include <linux/ctype.h>
69 #ifdef HAVE_LIBTRACEEVENT
70 #include <event-parse.h>
73 static char const *script_name
;
74 static char const *generate_script_lang
;
76 static bool deltatime
;
77 static u64 initial_time
;
78 static u64 previous_time
;
79 static bool debug_mode
;
80 static u64 last_timestamp
;
81 static u64 nr_unordered
;
82 static bool no_callchain
;
83 static bool latency_format
;
84 static bool system_wide
;
85 static bool print_flags
;
86 static const char *cpu_list
;
87 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
88 static int max_blocks
;
89 static bool native_arch
;
90 static struct dlfilter
*dlfilter
;
94 enum perf_output_field
{
95 PERF_OUTPUT_COMM
= 1ULL << 0,
96 PERF_OUTPUT_TID
= 1ULL << 1,
97 PERF_OUTPUT_PID
= 1ULL << 2,
98 PERF_OUTPUT_TIME
= 1ULL << 3,
99 PERF_OUTPUT_CPU
= 1ULL << 4,
100 PERF_OUTPUT_EVNAME
= 1ULL << 5,
101 PERF_OUTPUT_TRACE
= 1ULL << 6,
102 PERF_OUTPUT_IP
= 1ULL << 7,
103 PERF_OUTPUT_SYM
= 1ULL << 8,
104 PERF_OUTPUT_DSO
= 1ULL << 9,
105 PERF_OUTPUT_ADDR
= 1ULL << 10,
106 PERF_OUTPUT_SYMOFFSET
= 1ULL << 11,
107 PERF_OUTPUT_SRCLINE
= 1ULL << 12,
108 PERF_OUTPUT_PERIOD
= 1ULL << 13,
109 PERF_OUTPUT_IREGS
= 1ULL << 14,
110 PERF_OUTPUT_BRSTACK
= 1ULL << 15,
111 PERF_OUTPUT_BRSTACKSYM
= 1ULL << 16,
112 PERF_OUTPUT_DATA_SRC
= 1ULL << 17,
113 PERF_OUTPUT_WEIGHT
= 1ULL << 18,
114 PERF_OUTPUT_BPF_OUTPUT
= 1ULL << 19,
115 PERF_OUTPUT_CALLINDENT
= 1ULL << 20,
116 PERF_OUTPUT_INSN
= 1ULL << 21,
117 PERF_OUTPUT_INSNLEN
= 1ULL << 22,
118 PERF_OUTPUT_BRSTACKINSN
= 1ULL << 23,
119 PERF_OUTPUT_BRSTACKOFF
= 1ULL << 24,
120 PERF_OUTPUT_SYNTH
= 1ULL << 25,
121 PERF_OUTPUT_PHYS_ADDR
= 1ULL << 26,
122 PERF_OUTPUT_UREGS
= 1ULL << 27,
123 PERF_OUTPUT_METRIC
= 1ULL << 28,
124 PERF_OUTPUT_MISC
= 1ULL << 29,
125 PERF_OUTPUT_SRCCODE
= 1ULL << 30,
126 PERF_OUTPUT_IPC
= 1ULL << 31,
127 PERF_OUTPUT_TOD
= 1ULL << 32,
128 PERF_OUTPUT_DATA_PAGE_SIZE
= 1ULL << 33,
129 PERF_OUTPUT_CODE_PAGE_SIZE
= 1ULL << 34,
130 PERF_OUTPUT_INS_LAT
= 1ULL << 35,
131 PERF_OUTPUT_BRSTACKINSNLEN
= 1ULL << 36,
132 PERF_OUTPUT_MACHINE_PID
= 1ULL << 37,
133 PERF_OUTPUT_VCPU
= 1ULL << 38,
134 PERF_OUTPUT_CGROUP
= 1ULL << 39,
135 PERF_OUTPUT_RETIRE_LAT
= 1ULL << 40,
136 PERF_OUTPUT_DSOFF
= 1ULL << 41,
137 PERF_OUTPUT_DISASM
= 1ULL << 42,
138 PERF_OUTPUT_BRSTACKDISASM
= 1ULL << 43,
139 PERF_OUTPUT_BRCNTR
= 1ULL << 44,
143 struct perf_tool tool
;
144 struct perf_session
*session
;
145 bool show_task_events
;
146 bool show_mmap_events
;
147 bool show_switch_events
;
148 bool show_namespace_events
;
149 bool show_lost_events
;
150 bool show_round_events
;
151 bool show_bpf_events
;
152 bool show_cgroup_events
;
153 bool show_text_poke_events
;
157 struct evswitch evswitch
;
158 struct perf_cpu_map
*cpus
;
159 struct perf_thread_map
*threads
;
161 const char *time_str
;
162 struct perf_time_interval
*ptime_range
;
167 struct output_option
{
169 enum perf_output_field field
;
170 } all_output_options
[] = {
171 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
172 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
173 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
174 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
175 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
176 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
177 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
178 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
179 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
180 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
181 {.str
= "dsoff", .field
= PERF_OUTPUT_DSOFF
},
182 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
183 {.str
= "symoff", .field
= PERF_OUTPUT_SYMOFFSET
},
184 {.str
= "srcline", .field
= PERF_OUTPUT_SRCLINE
},
185 {.str
= "period", .field
= PERF_OUTPUT_PERIOD
},
186 {.str
= "iregs", .field
= PERF_OUTPUT_IREGS
},
187 {.str
= "uregs", .field
= PERF_OUTPUT_UREGS
},
188 {.str
= "brstack", .field
= PERF_OUTPUT_BRSTACK
},
189 {.str
= "brstacksym", .field
= PERF_OUTPUT_BRSTACKSYM
},
190 {.str
= "data_src", .field
= PERF_OUTPUT_DATA_SRC
},
191 {.str
= "weight", .field
= PERF_OUTPUT_WEIGHT
},
192 {.str
= "bpf-output", .field
= PERF_OUTPUT_BPF_OUTPUT
},
193 {.str
= "callindent", .field
= PERF_OUTPUT_CALLINDENT
},
194 {.str
= "insn", .field
= PERF_OUTPUT_INSN
},
195 {.str
= "disasm", .field
= PERF_OUTPUT_DISASM
},
196 {.str
= "insnlen", .field
= PERF_OUTPUT_INSNLEN
},
197 {.str
= "brstackinsn", .field
= PERF_OUTPUT_BRSTACKINSN
},
198 {.str
= "brstackoff", .field
= PERF_OUTPUT_BRSTACKOFF
},
199 {.str
= "synth", .field
= PERF_OUTPUT_SYNTH
},
200 {.str
= "phys_addr", .field
= PERF_OUTPUT_PHYS_ADDR
},
201 {.str
= "metric", .field
= PERF_OUTPUT_METRIC
},
202 {.str
= "misc", .field
= PERF_OUTPUT_MISC
},
203 {.str
= "srccode", .field
= PERF_OUTPUT_SRCCODE
},
204 {.str
= "ipc", .field
= PERF_OUTPUT_IPC
},
205 {.str
= "tod", .field
= PERF_OUTPUT_TOD
},
206 {.str
= "data_page_size", .field
= PERF_OUTPUT_DATA_PAGE_SIZE
},
207 {.str
= "code_page_size", .field
= PERF_OUTPUT_CODE_PAGE_SIZE
},
208 {.str
= "ins_lat", .field
= PERF_OUTPUT_INS_LAT
},
209 {.str
= "brstackinsnlen", .field
= PERF_OUTPUT_BRSTACKINSNLEN
},
210 {.str
= "machine_pid", .field
= PERF_OUTPUT_MACHINE_PID
},
211 {.str
= "vcpu", .field
= PERF_OUTPUT_VCPU
},
212 {.str
= "cgroup", .field
= PERF_OUTPUT_CGROUP
},
213 {.str
= "retire_lat", .field
= PERF_OUTPUT_RETIRE_LAT
},
214 {.str
= "brstackdisasm", .field
= PERF_OUTPUT_BRSTACKDISASM
},
215 {.str
= "brcntr", .field
= PERF_OUTPUT_BRCNTR
},
219 OUTPUT_TYPE_SYNTH
= PERF_TYPE_MAX
,
224 // We need to refactor the evsel->priv use in in 'perf script' to allow for
225 // using that area, that is being used only in some cases.
226 #define OUTPUT_TYPE_UNSET -1
228 /* default set to maintain compatibility with current format */
232 unsigned int print_ip_opts
;
236 u64 user_unset_fields
;
237 } output
[OUTPUT_TYPE_MAX
] = {
239 [PERF_TYPE_HARDWARE
] = {
242 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
243 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
244 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
245 PERF_OUTPUT_SYM
| PERF_OUTPUT_SYMOFFSET
|
246 PERF_OUTPUT_DSO
| PERF_OUTPUT_PERIOD
,
248 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
251 [PERF_TYPE_SOFTWARE
] = {
254 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
255 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
256 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
257 PERF_OUTPUT_SYM
| PERF_OUTPUT_SYMOFFSET
|
258 PERF_OUTPUT_DSO
| PERF_OUTPUT_PERIOD
|
259 PERF_OUTPUT_BPF_OUTPUT
,
261 .invalid_fields
= PERF_OUTPUT_TRACE
,
264 [PERF_TYPE_TRACEPOINT
] = {
267 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
268 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
269 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
272 [PERF_TYPE_HW_CACHE
] = {
275 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
276 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
277 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
278 PERF_OUTPUT_SYM
| PERF_OUTPUT_SYMOFFSET
|
279 PERF_OUTPUT_DSO
| PERF_OUTPUT_PERIOD
,
281 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
287 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
288 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
289 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
290 PERF_OUTPUT_SYM
| PERF_OUTPUT_SYMOFFSET
|
291 PERF_OUTPUT_DSO
| PERF_OUTPUT_PERIOD
|
292 PERF_OUTPUT_ADDR
| PERF_OUTPUT_DATA_SRC
|
293 PERF_OUTPUT_WEIGHT
| PERF_OUTPUT_PHYS_ADDR
|
294 PERF_OUTPUT_DATA_PAGE_SIZE
| PERF_OUTPUT_CODE_PAGE_SIZE
|
295 PERF_OUTPUT_INS_LAT
| PERF_OUTPUT_RETIRE_LAT
,
297 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
300 [PERF_TYPE_BREAKPOINT
] = {
303 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
304 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
305 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
306 PERF_OUTPUT_SYM
| PERF_OUTPUT_SYMOFFSET
|
307 PERF_OUTPUT_DSO
| PERF_OUTPUT_PERIOD
,
309 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
312 [OUTPUT_TYPE_SYNTH
] = {
315 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
316 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
317 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
318 PERF_OUTPUT_SYM
| PERF_OUTPUT_SYMOFFSET
|
319 PERF_OUTPUT_DSO
| PERF_OUTPUT_SYNTH
,
321 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
324 [OUTPUT_TYPE_OTHER
] = {
327 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
328 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
329 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
330 PERF_OUTPUT_SYM
| PERF_OUTPUT_SYMOFFSET
|
331 PERF_OUTPUT_DSO
| PERF_OUTPUT_PERIOD
,
333 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
337 struct evsel_script
{
341 /* For metric output */
346 static inline struct evsel_script
*evsel_script(struct evsel
*evsel
)
348 return (struct evsel_script
*)evsel
->priv
;
351 static struct evsel_script
*evsel_script__new(struct evsel
*evsel
, struct perf_data
*data
)
353 struct evsel_script
*es
= zalloc(sizeof(*es
));
356 if (asprintf(&es
->filename
, "%s.%s.dump", data
->file
.path
, evsel__name(evsel
)) < 0)
358 es
->fp
= fopen(es
->filename
, "w");
360 goto out_free_filename
;
365 zfree(&es
->filename
);
371 static void evsel_script__delete(struct evsel_script
*es
)
373 zfree(&es
->filename
);
379 static int evsel_script__fprintf(struct evsel_script
*es
, FILE *fp
)
383 fstat(fileno(es
->fp
), &st
);
384 return fprintf(fp
, "[ perf script: Wrote %.3f MB %s (%" PRIu64
" samples) ]\n",
385 st
.st_size
/ 1024.0 / 1024.0, es
->filename
, es
->samples
);
388 static inline int output_type(unsigned int type
)
391 case PERF_TYPE_SYNTH
:
392 return OUTPUT_TYPE_SYNTH
;
394 if (type
< PERF_TYPE_MAX
)
398 return OUTPUT_TYPE_OTHER
;
401 static inline int evsel__output_type(struct evsel
*evsel
)
403 int type
= evsel
->script_output_type
;
405 if (type
== OUTPUT_TYPE_UNSET
) {
406 type
= output_type(evsel
->core
.attr
.type
);
407 if (type
== OUTPUT_TYPE_OTHER
) {
408 struct perf_pmu
*pmu
= evsel__find_pmu(evsel
);
410 if (pmu
&& pmu
->is_core
)
411 type
= PERF_TYPE_RAW
;
413 evsel
->script_output_type
= type
;
419 static bool output_set_by_user(void)
422 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
423 if (output
[j
].user_set
)
429 static const char *output_field2str(enum perf_output_field field
)
431 int i
, imax
= ARRAY_SIZE(all_output_options
);
432 const char *str
= "";
434 for (i
= 0; i
< imax
; ++i
) {
435 if (all_output_options
[i
].field
== field
) {
436 str
= all_output_options
[i
].str
;
443 #define PRINT_FIELD(x) (output[evsel__output_type(evsel)].fields & PERF_OUTPUT_##x)
445 static int evsel__do_check_stype(struct evsel
*evsel
, u64 sample_type
, const char *sample_msg
,
446 enum perf_output_field field
, bool allow_user_set
)
448 struct perf_event_attr
*attr
= &evsel
->core
.attr
;
449 int type
= evsel__output_type(evsel
);
452 if (attr
->sample_type
& sample_type
)
455 if (output
[type
].user_set_fields
& field
) {
458 evname
= evsel__name(evsel
);
459 pr_err("Samples for '%s' event do not have %s attribute set. "
460 "Cannot print '%s' field.\n",
461 evname
, sample_msg
, output_field2str(field
));
465 /* user did not ask for it explicitly so remove from the default list */
466 output
[type
].fields
&= ~field
;
467 evname
= evsel__name(evsel
);
468 pr_debug("Samples for '%s' event do not have %s attribute set. "
469 "Skipping '%s' field.\n",
470 evname
, sample_msg
, output_field2str(field
));
475 static int evsel__check_stype(struct evsel
*evsel
, u64 sample_type
, const char *sample_msg
,
476 enum perf_output_field field
)
478 return evsel__do_check_stype(evsel
, sample_type
, sample_msg
, field
, false);
481 static int evsel__check_attr(struct evsel
*evsel
, struct perf_session
*session
)
485 if (evsel__is_dummy_event(evsel
))
488 if (perf_header__has_feat(&session
->header
, HEADER_STAT
))
491 allow_user_set
= perf_header__has_feat(&session
->header
,
494 if (PRINT_FIELD(TRACE
) &&
495 !perf_session__has_traces(session
, "record -R"))
498 if (PRINT_FIELD(IP
)) {
499 if (evsel__check_stype(evsel
, PERF_SAMPLE_IP
, "IP", PERF_OUTPUT_IP
))
503 if (PRINT_FIELD(ADDR
) &&
504 evsel__do_check_stype(evsel
, PERF_SAMPLE_ADDR
, "ADDR", PERF_OUTPUT_ADDR
, allow_user_set
))
507 if (PRINT_FIELD(DATA_SRC
) &&
508 evsel__do_check_stype(evsel
, PERF_SAMPLE_DATA_SRC
, "DATA_SRC", PERF_OUTPUT_DATA_SRC
, allow_user_set
))
511 if (PRINT_FIELD(WEIGHT
) &&
512 evsel__do_check_stype(evsel
, PERF_SAMPLE_WEIGHT_TYPE
, "WEIGHT", PERF_OUTPUT_WEIGHT
, allow_user_set
))
515 if (PRINT_FIELD(SYM
) &&
516 !(evsel
->core
.attr
.sample_type
& (PERF_SAMPLE_IP
|PERF_SAMPLE_ADDR
))) {
517 pr_err("Display of symbols requested but neither sample IP nor "
518 "sample address\navailable. Hence, no addresses to convert "
522 if (PRINT_FIELD(SYMOFFSET
) && !PRINT_FIELD(SYM
)) {
523 pr_err("Display of offsets requested but symbol is not"
527 if (PRINT_FIELD(DSO
) &&
528 !(evsel
->core
.attr
.sample_type
& (PERF_SAMPLE_IP
|PERF_SAMPLE_ADDR
))) {
529 pr_err("Display of DSO requested but no address to convert.\n");
532 if ((PRINT_FIELD(SRCLINE
) || PRINT_FIELD(SRCCODE
)) && !PRINT_FIELD(IP
)) {
533 pr_err("Display of source line number requested but sample IP is not\n"
534 "selected. Hence, no address to lookup the source line number.\n");
537 if ((PRINT_FIELD(BRSTACKINSN
) || PRINT_FIELD(BRSTACKINSNLEN
) || PRINT_FIELD(BRSTACKDISASM
))
538 && !allow_user_set
&&
539 !(evlist__combined_branch_type(session
->evlist
) & PERF_SAMPLE_BRANCH_ANY
)) {
540 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
541 "Hint: run 'perf record -b ...'\n");
544 if (PRINT_FIELD(BRCNTR
) &&
545 !(evlist__combined_branch_type(session
->evlist
) & PERF_SAMPLE_BRANCH_COUNTERS
)) {
546 pr_err("Display of branch counter requested but it's not enabled\n"
547 "Hint: run 'perf record -j any,counter ...'\n");
550 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
551 evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID", PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
554 if (PRINT_FIELD(TIME
) &&
555 evsel__check_stype(evsel
, PERF_SAMPLE_TIME
, "TIME", PERF_OUTPUT_TIME
))
558 if (PRINT_FIELD(CPU
) &&
559 evsel__do_check_stype(evsel
, PERF_SAMPLE_CPU
, "CPU", PERF_OUTPUT_CPU
, allow_user_set
))
562 if (PRINT_FIELD(IREGS
) &&
563 evsel__do_check_stype(evsel
, PERF_SAMPLE_REGS_INTR
, "IREGS", PERF_OUTPUT_IREGS
, allow_user_set
))
566 if (PRINT_FIELD(UREGS
) &&
567 evsel__check_stype(evsel
, PERF_SAMPLE_REGS_USER
, "UREGS", PERF_OUTPUT_UREGS
))
570 if (PRINT_FIELD(PHYS_ADDR
) &&
571 evsel__do_check_stype(evsel
, PERF_SAMPLE_PHYS_ADDR
, "PHYS_ADDR", PERF_OUTPUT_PHYS_ADDR
, allow_user_set
))
574 if (PRINT_FIELD(DATA_PAGE_SIZE
) &&
575 evsel__check_stype(evsel
, PERF_SAMPLE_DATA_PAGE_SIZE
, "DATA_PAGE_SIZE", PERF_OUTPUT_DATA_PAGE_SIZE
))
578 if (PRINT_FIELD(CODE_PAGE_SIZE
) &&
579 evsel__check_stype(evsel
, PERF_SAMPLE_CODE_PAGE_SIZE
, "CODE_PAGE_SIZE", PERF_OUTPUT_CODE_PAGE_SIZE
))
582 if (PRINT_FIELD(INS_LAT
) &&
583 evsel__check_stype(evsel
, PERF_SAMPLE_WEIGHT_STRUCT
, "WEIGHT_STRUCT", PERF_OUTPUT_INS_LAT
))
586 if (PRINT_FIELD(CGROUP
) &&
587 evsel__check_stype(evsel
, PERF_SAMPLE_CGROUP
, "CGROUP", PERF_OUTPUT_CGROUP
)) {
588 pr_err("Hint: run 'perf record --all-cgroups ...'\n");
592 if (PRINT_FIELD(RETIRE_LAT
) &&
593 evsel__check_stype(evsel
, PERF_SAMPLE_WEIGHT_STRUCT
, "WEIGHT_STRUCT", PERF_OUTPUT_RETIRE_LAT
))
599 static void evsel__set_print_ip_opts(struct evsel
*evsel
)
601 unsigned int type
= evsel__output_type(evsel
);
603 output
[type
].print_ip_opts
= 0;
605 output
[type
].print_ip_opts
|= EVSEL__PRINT_IP
;
607 if (PRINT_FIELD(SYM
))
608 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYM
;
610 if (PRINT_FIELD(DSO
))
611 output
[type
].print_ip_opts
|= EVSEL__PRINT_DSO
;
613 if (PRINT_FIELD(DSOFF
))
614 output
[type
].print_ip_opts
|= EVSEL__PRINT_DSOFF
;
616 if (PRINT_FIELD(SYMOFFSET
))
617 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYMOFFSET
;
619 if (PRINT_FIELD(SRCLINE
))
620 output
[type
].print_ip_opts
|= EVSEL__PRINT_SRCLINE
;
623 static struct evsel
*find_first_output_type(struct evlist
*evlist
,
628 evlist__for_each_entry(evlist
, evsel
) {
629 if (evsel__is_dummy_event(evsel
))
631 if (evsel__output_type(evsel
) == (int)type
)
638 * verify all user requested events exist and the samples
639 * have the expected data
641 static int perf_session__check_output_opt(struct perf_session
*session
)
647 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
648 evsel
= find_first_output_type(session
->evlist
, j
);
651 * even if fields is set to 0 (ie., show nothing) event must
652 * exist if user explicitly includes it on the command line
654 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
&&
655 j
!= OUTPUT_TYPE_SYNTH
) {
656 pr_err("%s events do not exist. "
657 "Remove corresponding -F option to proceed.\n",
662 if (evsel
&& output
[j
].fields
&&
663 evsel__check_attr(evsel
, session
))
669 /* 'dsoff' implys 'dso' field */
670 if (output
[j
].fields
& PERF_OUTPUT_DSOFF
)
671 output
[j
].fields
|= PERF_OUTPUT_DSO
;
673 evsel__set_print_ip_opts(evsel
);
674 tod
|= output
[j
].fields
& PERF_OUTPUT_TOD
;
678 bool use_callchain
= false;
679 bool not_pipe
= false;
681 evlist__for_each_entry(session
->evlist
, evsel
) {
683 if (evsel__has_callchain(evsel
) || evsel__is_offcpu_event(evsel
)) {
684 use_callchain
= true;
688 if (not_pipe
&& !use_callchain
)
689 symbol_conf
.use_callchain
= false;
693 * set default for tracepoints to print symbols only
694 * if callchains are present
696 if (symbol_conf
.use_callchain
&&
697 !output
[PERF_TYPE_TRACEPOINT
].user_set
) {
698 j
= PERF_TYPE_TRACEPOINT
;
700 evlist__for_each_entry(session
->evlist
, evsel
) {
701 if (evsel
->core
.attr
.type
!= j
)
704 if (evsel__has_callchain(evsel
)) {
705 output
[j
].fields
|= PERF_OUTPUT_IP
;
706 output
[j
].fields
|= PERF_OUTPUT_SYM
;
707 output
[j
].fields
|= PERF_OUTPUT_SYMOFFSET
;
708 output
[j
].fields
|= PERF_OUTPUT_DSO
;
709 evsel__set_print_ip_opts(evsel
);
715 if (tod
&& !session
->header
.env
.clock
.enabled
) {
716 pr_err("Can't provide 'tod' time, missing clock data. "
717 "Please record with -k/--clockid option.\n");
724 static int perf_sample__fprintf_regs(struct regs_dump
*regs
, uint64_t mask
, const char *arch
,
730 if (!regs
|| !regs
->regs
)
733 printed
+= fprintf(fp
, " ABI:%" PRIu64
" ", regs
->abi
);
735 for_each_set_bit(r
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
736 u64 val
= regs
->regs
[i
++];
737 printed
+= fprintf(fp
, "%5s:0x%"PRIx64
" ", perf_reg_name(r
, arch
), val
);
743 #define DEFAULT_TOD_FMT "%F %H:%M:%S"
746 tod_scnprintf(struct perf_script
*script
, char *buf
, int buflen
,
749 u64 tod_ns
, clockid_ns
;
750 struct perf_env
*env
;
757 if (buflen
< 64 || !script
)
760 env
= &script
->session
->header
.env
;
761 if (!env
->clock
.enabled
) {
762 scnprintf(buf
, buflen
, "disabled");
766 clockid_ns
= env
->clock
.clockid_ns
;
767 tod_ns
= env
->clock
.tod_ns
;
769 if (timestamp
> clockid_ns
)
770 tod_ns
+= timestamp
- clockid_ns
;
772 tod_ns
-= clockid_ns
- timestamp
;
774 sec
= (time_t) (tod_ns
/ NSEC_PER_SEC
);
775 nsec
= tod_ns
- sec
* NSEC_PER_SEC
;
777 if (localtime_r(&sec
, <ime
) == NULL
) {
778 scnprintf(buf
, buflen
, "failed");
780 strftime(date
, sizeof(date
), DEFAULT_TOD_FMT
, <ime
);
782 if (symbol_conf
.nanosecs
) {
783 snprintf(buf
, buflen
, "%s.%09lu", date
, nsec
);
785 snprintf(buf
, buflen
, "%s.%06lu",
786 date
, nsec
/ NSEC_PER_USEC
);
793 static int perf_sample__fprintf_iregs(struct perf_sample
*sample
,
794 struct perf_event_attr
*attr
, const char *arch
, FILE *fp
)
796 if (!sample
->intr_regs
)
799 return perf_sample__fprintf_regs(perf_sample__intr_regs(sample
),
800 attr
->sample_regs_intr
, arch
, fp
);
803 static int perf_sample__fprintf_uregs(struct perf_sample
*sample
,
804 struct perf_event_attr
*attr
, const char *arch
, FILE *fp
)
806 if (!sample
->user_regs
)
809 return perf_sample__fprintf_regs(perf_sample__user_regs(sample
),
810 attr
->sample_regs_user
, arch
, fp
);
813 static int perf_sample__fprintf_start(struct perf_script
*script
,
814 struct perf_sample
*sample
,
815 struct thread
*thread
,
820 unsigned long long nsecs
;
825 * Print the branch counter's abbreviation list,
826 * if the branch counter is available.
828 if (PRINT_FIELD(BRCNTR
) && !verbose
) {
831 if (!annotation_br_cntr_abbr_list(&buf
, evsel
, true)) {
832 printed
+= fprintf(stdout
, "%s", buf
);
837 if (PRINT_FIELD(MACHINE_PID
) && sample
->machine_pid
)
838 printed
+= fprintf(fp
, "VM:%5d ", sample
->machine_pid
);
840 /* Print VCPU only for guest events i.e. with machine_pid */
841 if (PRINT_FIELD(VCPU
) && sample
->machine_pid
)
842 printed
+= fprintf(fp
, "VCPU:%03d ", sample
->vcpu
);
844 if (PRINT_FIELD(COMM
)) {
845 const char *comm
= thread
? thread__comm_str(thread
) : ":-1";
848 printed
+= fprintf(fp
, "%8.8s ", comm
);
849 else if (PRINT_FIELD(IP
) && evsel__has_callchain(evsel
) && symbol_conf
.use_callchain
)
850 printed
+= fprintf(fp
, "%s ", comm
);
852 printed
+= fprintf(fp
, "%16s ", comm
);
855 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
856 printed
+= fprintf(fp
, "%7d/%-7d ", sample
->pid
, sample
->tid
);
857 else if (PRINT_FIELD(PID
))
858 printed
+= fprintf(fp
, "%7d ", sample
->pid
);
859 else if (PRINT_FIELD(TID
))
860 printed
+= fprintf(fp
, "%7d ", sample
->tid
);
862 if (PRINT_FIELD(CPU
)) {
864 printed
+= fprintf(fp
, "%3d ", sample
->cpu
);
866 printed
+= fprintf(fp
, "[%03d] ", sample
->cpu
);
869 if (PRINT_FIELD(MISC
)) {
873 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
876 ret
+= fprintf(fp
, "K");
878 ret
+= fprintf(fp
, "U");
880 ret
+= fprintf(fp
, "H");
881 if (has(GUEST_KERNEL
))
882 ret
+= fprintf(fp
, "G");
884 ret
+= fprintf(fp
, "g");
887 case PERF_RECORD_MMAP
:
888 case PERF_RECORD_MMAP2
:
890 ret
+= fprintf(fp
, "M");
892 case PERF_RECORD_COMM
:
894 ret
+= fprintf(fp
, "E");
896 case PERF_RECORD_SWITCH
:
897 case PERF_RECORD_SWITCH_CPU_WIDE
:
898 if (has(SWITCH_OUT
)) {
899 ret
+= fprintf(fp
, "S");
900 if (sample
->misc
& PERF_RECORD_MISC_SWITCH_OUT_PREEMPT
)
901 ret
+= fprintf(fp
, "p");
909 ret
+= fprintf(fp
, "%*s", 6 - ret
, " ");
913 if (PRINT_FIELD(TOD
)) {
914 tod_scnprintf(script
, tstr
, sizeof(tstr
), sample
->time
);
915 printed
+= fprintf(fp
, "%s ", tstr
);
918 if (PRINT_FIELD(TIME
)) {
919 u64 t
= sample
->time
;
922 initial_time
= sample
->time
;
923 t
= sample
->time
- initial_time
;
924 } else if (deltatime
) {
926 t
= sample
->time
- previous_time
;
930 previous_time
= sample
->time
;
933 secs
= nsecs
/ NSEC_PER_SEC
;
934 nsecs
-= secs
* NSEC_PER_SEC
;
936 if (symbol_conf
.nanosecs
)
937 printed
+= fprintf(fp
, "%5lu.%09llu: ", secs
, nsecs
);
939 char sample_time
[32];
940 timestamp__scnprintf_usec(t
, sample_time
, sizeof(sample_time
));
941 printed
+= fprintf(fp
, "%12s: ", sample_time
);
949 bstack_event_str(struct branch_entry
*br
, char *buf
, size_t sz
)
951 if (!(br
->flags
.mispred
|| br
->flags
.predicted
|| br
->flags
.not_taken
))
952 return snprintf(buf
, sz
, "-");
954 return snprintf(buf
, sz
, "%s%s",
955 br
->flags
.predicted
? "P" : "M",
956 br
->flags
.not_taken
? "N" : "");
959 static int print_bstack_flags(FILE *fp
, struct branch_entry
*br
)
961 char events
[16] = { 0 };
964 pos
= bstack_event_str(br
, events
, sizeof(events
));
965 return fprintf(fp
, "/%s/%c/%c/%d/%s/%s ",
966 pos
< 0 ? "-" : events
,
967 br
->flags
.in_tx
? 'X' : '-',
968 br
->flags
.abort
? 'A' : '-',
971 br
->flags
.spec
? branch_spec_desc(br
->flags
.spec
) : "-");
974 static int perf_sample__fprintf_brstack(struct perf_sample
*sample
,
975 struct thread
*thread
,
976 struct evsel
*evsel
, FILE *fp
)
978 struct branch_stack
*br
= sample
->branch_stack
;
979 struct branch_entry
*entries
= perf_sample__branch_entries(sample
);
986 for (i
= 0; i
< br
->nr
; i
++) {
987 from
= entries
[i
].from
;
990 printed
+= fprintf(fp
, " 0x%"PRIx64
, from
);
991 if (PRINT_FIELD(DSO
)) {
992 struct addr_location alf
, alt
;
994 addr_location__init(&alf
);
995 addr_location__init(&alt
);
996 thread__find_map_fb(thread
, sample
->cpumode
, from
, &alf
);
997 thread__find_map_fb(thread
, sample
->cpumode
, to
, &alt
);
999 printed
+= map__fprintf_dsoname_dsoff(alf
.map
, PRINT_FIELD(DSOFF
), alf
.addr
, fp
);
1000 printed
+= fprintf(fp
, "/0x%"PRIx64
, to
);
1001 printed
+= map__fprintf_dsoname_dsoff(alt
.map
, PRINT_FIELD(DSOFF
), alt
.addr
, fp
);
1002 addr_location__exit(&alt
);
1003 addr_location__exit(&alf
);
1005 printed
+= fprintf(fp
, "/0x%"PRIx64
, to
);
1007 printed
+= print_bstack_flags(fp
, entries
+ i
);
1013 static int perf_sample__fprintf_brstacksym(struct perf_sample
*sample
,
1014 struct thread
*thread
,
1015 struct evsel
*evsel
, FILE *fp
)
1017 struct branch_stack
*br
= sample
->branch_stack
;
1018 struct branch_entry
*entries
= perf_sample__branch_entries(sample
);
1022 if (!(br
&& br
->nr
))
1025 for (i
= 0; i
< br
->nr
; i
++) {
1026 struct addr_location alf
, alt
;
1028 addr_location__init(&alf
);
1029 addr_location__init(&alt
);
1030 from
= entries
[i
].from
;
1033 thread__find_symbol_fb(thread
, sample
->cpumode
, from
, &alf
);
1034 thread__find_symbol_fb(thread
, sample
->cpumode
, to
, &alt
);
1036 printed
+= symbol__fprintf_symname_offs(alf
.sym
, &alf
, fp
);
1037 if (PRINT_FIELD(DSO
))
1038 printed
+= map__fprintf_dsoname_dsoff(alf
.map
, PRINT_FIELD(DSOFF
), alf
.addr
, fp
);
1039 printed
+= fprintf(fp
, "%c", '/');
1040 printed
+= symbol__fprintf_symname_offs(alt
.sym
, &alt
, fp
);
1041 if (PRINT_FIELD(DSO
))
1042 printed
+= map__fprintf_dsoname_dsoff(alt
.map
, PRINT_FIELD(DSOFF
), alt
.addr
, fp
);
1043 printed
+= print_bstack_flags(fp
, entries
+ i
);
1044 addr_location__exit(&alt
);
1045 addr_location__exit(&alf
);
1051 static int perf_sample__fprintf_brstackoff(struct perf_sample
*sample
,
1052 struct thread
*thread
,
1053 struct evsel
*evsel
, FILE *fp
)
1055 struct branch_stack
*br
= sample
->branch_stack
;
1056 struct branch_entry
*entries
= perf_sample__branch_entries(sample
);
1060 if (!(br
&& br
->nr
))
1063 for (i
= 0; i
< br
->nr
; i
++) {
1064 struct addr_location alf
, alt
;
1066 addr_location__init(&alf
);
1067 addr_location__init(&alt
);
1068 from
= entries
[i
].from
;
1071 if (thread__find_map_fb(thread
, sample
->cpumode
, from
, &alf
) &&
1072 !dso__adjust_symbols(map__dso(alf
.map
)))
1073 from
= map__dso_map_ip(alf
.map
, from
);
1075 if (thread__find_map_fb(thread
, sample
->cpumode
, to
, &alt
) &&
1076 !dso__adjust_symbols(map__dso(alt
.map
)))
1077 to
= map__dso_map_ip(alt
.map
, to
);
1079 printed
+= fprintf(fp
, " 0x%"PRIx64
, from
);
1080 if (PRINT_FIELD(DSO
))
1081 printed
+= map__fprintf_dsoname_dsoff(alf
.map
, PRINT_FIELD(DSOFF
), alf
.addr
, fp
);
1082 printed
+= fprintf(fp
, "/0x%"PRIx64
, to
);
1083 if (PRINT_FIELD(DSO
))
1084 printed
+= map__fprintf_dsoname_dsoff(alt
.map
, PRINT_FIELD(DSOFF
), alt
.addr
, fp
);
1085 printed
+= print_bstack_flags(fp
, entries
+ i
);
1086 addr_location__exit(&alt
);
1087 addr_location__exit(&alf
);
1092 #define MAXBB 16384UL
1094 static int grab_bb(u8
*buffer
, u64 start
, u64 end
,
1095 struct machine
*machine
, struct thread
*thread
,
1096 bool *is64bit
, u8
*cpumode
, bool last
)
1099 struct addr_location al
;
1107 kernel
= machine__kernel_ip(machine
, start
);
1109 *cpumode
= PERF_RECORD_MISC_KERNEL
;
1111 *cpumode
= PERF_RECORD_MISC_USER
;
1114 * Block overlaps between kernel and user.
1115 * This can happen due to ring filtering
1116 * On Intel CPUs the entry into the kernel is filtered,
1117 * but the exit is not. Let the caller patch it up.
1119 if (kernel
!= machine__kernel_ip(machine
, end
)) {
1120 pr_debug("\tblock %" PRIx64
"-%" PRIx64
" transfers between kernel and user\n", start
, end
);
1124 if (end
- start
> MAXBB
- MAXINSN
) {
1126 pr_debug("\tbrstack does not reach to final jump (%" PRIx64
"-%" PRIx64
")\n", start
, end
);
1128 pr_debug("\tblock %" PRIx64
"-%" PRIx64
" (%" PRIu64
") too long to dump\n", start
, end
, end
- start
);
1132 addr_location__init(&al
);
1133 if (!thread__find_map(thread
, *cpumode
, start
, &al
) || (dso
= map__dso(al
.map
)) == NULL
) {
1134 pr_debug("\tcannot resolve %" PRIx64
"-%" PRIx64
"\n", start
, end
);
1137 if (dso__data(dso
)->status
== DSO_DATA_STATUS_ERROR
) {
1138 pr_debug("\tcannot resolve %" PRIx64
"-%" PRIx64
"\n", start
, end
);
1142 /* Load maps to ensure dso->is_64_bit has been updated */
1145 offset
= map__map_ip(al
.map
, start
);
1146 len
= dso__data_read_offset(dso
, machine
, offset
, (u8
*)buffer
,
1147 end
- start
+ MAXINSN
);
1149 *is64bit
= dso__is_64_bit(dso
);
1151 pr_debug("\tcannot fetch code for block at %" PRIx64
"-%" PRIx64
"\n",
1155 addr_location__exit(&al
);
1159 static int map__fprintf_srccode(struct map
*map
, u64 addr
, FILE *fp
, struct srccode_state
*state
)
1168 if (!map
|| (dso
= map__dso(map
)) == NULL
)
1170 srcfile
= get_srcline_split(dso
,
1171 map__rip_2objdump(map
, addr
),
1176 /* Avoid redundant printing */
1179 !strcmp(state
->srcfile
, srcfile
) &&
1180 state
->line
== line
) {
1185 srccode
= find_sourceline(srcfile
, line
, &len
);
1189 ret
= fprintf(fp
, "|%-8d %.*s", line
, len
, srccode
);
1192 state
->srcfile
= srcfile
;
1202 static int print_srccode(struct thread
*thread
, u8 cpumode
, uint64_t addr
)
1204 struct addr_location al
;
1207 addr_location__init(&al
);
1208 thread__find_map(thread
, cpumode
, addr
, &al
);
1211 ret
= map__fprintf_srccode(al
.map
, al
.addr
, stdout
,
1212 thread__srccode_state(thread
));
1214 ret
+= printf("\n");
1216 addr_location__exit(&al
);
1220 static int any_dump_insn(struct evsel
*evsel __maybe_unused
,
1221 struct perf_insn
*x
, uint64_t ip
,
1222 u8
*inbuf
, int inlen
, int *lenp
,
1225 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1226 if (PRINT_FIELD(BRSTACKDISASM
)) {
1227 int printed
= fprintf_insn_asm(x
->machine
, x
->thread
, x
->cpumode
, x
->is64bit
,
1228 (uint8_t *)inbuf
, inlen
, ip
, lenp
,
1229 PRINT_INSN_IMM_HEX
, fp
);
1235 return fprintf(fp
, "%s", dump_insn(x
, ip
, inbuf
, inlen
, lenp
));
1238 static int add_padding(FILE *fp
, int printed
, int padding
)
1240 if (printed
>= 0 && printed
< padding
)
1241 printed
+= fprintf(fp
, "%*s", padding
- printed
, "");
1245 static int ip__fprintf_jump(uint64_t ip
, struct branch_entry
*en
,
1246 struct perf_insn
*x
, u8
*inbuf
, int len
,
1247 int insn
, FILE *fp
, int *total_cycles
,
1248 struct evsel
*evsel
,
1249 struct thread
*thread
,
1253 int printed
= fprintf(fp
, "\t%016" PRIx64
"\t", ip
);
1255 printed
+= add_padding(fp
, any_dump_insn(evsel
, x
, ip
, inbuf
, len
, &ilen
, fp
), 30);
1256 printed
+= fprintf(fp
, "\t");
1258 if (PRINT_FIELD(BRSTACKINSNLEN
))
1259 printed
+= fprintf(fp
, "ilen: %d\t", ilen
);
1261 if (PRINT_FIELD(SRCLINE
)) {
1262 struct addr_location al
;
1264 addr_location__init(&al
);
1265 thread__find_map(thread
, x
->cpumode
, ip
, &al
);
1266 printed
+= map__fprintf_srcline(al
.map
, al
.addr
, " srcline: ", fp
);
1267 printed
+= fprintf(fp
, "\t");
1268 addr_location__exit(&al
);
1271 if (PRINT_FIELD(BRCNTR
)) {
1272 struct evsel
*pos
= evsel__leader(evsel
);
1273 unsigned int i
= 0, j
, num
, mask
, width
;
1275 perf_env__find_br_cntr_info(evsel__env(evsel
), NULL
, &width
);
1276 mask
= (1L << width
) - 1;
1277 printed
+= fprintf(fp
, "br_cntr: ");
1278 evlist__for_each_entry_from(evsel
->evlist
, pos
) {
1279 if (!(pos
->core
.attr
.branch_sample_type
& PERF_SAMPLE_BRANCH_COUNTERS
))
1281 if (evsel__leader(pos
) != evsel__leader(evsel
))
1284 num
= (br_cntr
>> (i
++ * width
)) & mask
;
1286 for (j
= 0; j
< num
; j
++)
1287 printed
+= fprintf(fp
, "%s", pos
->abbr_name
);
1289 printed
+= fprintf(fp
, "%s %d ", pos
->name
, num
);
1291 printed
+= fprintf(fp
, "\t");
1294 printed
+= fprintf(fp
, "#%s%s%s%s",
1295 en
->flags
.predicted
? " PRED" : "",
1296 en
->flags
.mispred
? " MISPRED" : "",
1297 en
->flags
.in_tx
? " INTX" : "",
1298 en
->flags
.abort
? " ABORT" : "");
1299 if (en
->flags
.cycles
) {
1300 *total_cycles
+= en
->flags
.cycles
;
1301 printed
+= fprintf(fp
, " %d cycles [%d]", en
->flags
.cycles
, *total_cycles
);
1303 printed
+= fprintf(fp
, " %.2f IPC", (float)insn
/ en
->flags
.cycles
);
1306 return printed
+ fprintf(fp
, "\n");
1309 static int ip__fprintf_sym(uint64_t addr
, struct thread
*thread
,
1310 u8 cpumode
, int cpu
, struct symbol
**lastsym
,
1311 struct evsel
*evsel
, FILE *fp
)
1313 struct addr_location al
;
1314 int off
, printed
= 0, ret
= 0;
1316 addr_location__init(&al
);
1317 thread__find_map(thread
, cpumode
, addr
, &al
);
1319 if ((*lastsym
) && al
.addr
>= (*lastsym
)->start
&& al
.addr
< (*lastsym
)->end
)
1325 al
.sym
= map__find_symbol(al
.map
, al
.addr
);
1330 if (al
.addr
< al
.sym
->end
)
1331 off
= al
.addr
- al
.sym
->start
;
1333 off
= al
.addr
- map__start(al
.map
) - al
.sym
->start
;
1334 printed
+= fprintf(fp
, "\t%s", al
.sym
->name
);
1336 printed
+= fprintf(fp
, "%+d", off
);
1337 printed
+= fprintf(fp
, ":");
1338 if (PRINT_FIELD(SRCLINE
))
1339 printed
+= map__fprintf_srcline(al
.map
, al
.addr
, "\t", fp
);
1340 printed
+= fprintf(fp
, "\n");
1345 addr_location__exit(&al
);
1349 static int perf_sample__fprintf_brstackinsn(struct perf_sample
*sample
,
1350 struct evsel
*evsel
,
1351 struct thread
*thread
,
1352 struct perf_event_attr
*attr
,
1353 struct machine
*machine
, FILE *fp
)
1355 struct branch_stack
*br
= sample
->branch_stack
;
1356 struct branch_entry
*entries
= perf_sample__branch_entries(sample
);
1358 int i
, insn
, len
, nr
, ilen
, printed
= 0;
1362 struct symbol
*lastsym
= NULL
;
1363 int total_cycles
= 0;
1366 if (!(br
&& br
->nr
))
1369 if (max_blocks
&& nr
> max_blocks
+ 1)
1370 nr
= max_blocks
+ 1;
1373 x
.machine
= machine
;
1374 x
.cpu
= sample
->cpu
;
1376 if (PRINT_FIELD(BRCNTR
) && sample
->branch_stack_cntr
)
1377 br_cntr
= sample
->branch_stack_cntr
[nr
- 1];
1379 printed
+= fprintf(fp
, "%c", '\n');
1381 /* Handle first from jump, of which we don't know the entry. */
1382 len
= grab_bb(buffer
, entries
[nr
-1].from
,
1384 machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
1386 printed
+= ip__fprintf_sym(entries
[nr
- 1].from
, thread
,
1387 x
.cpumode
, x
.cpu
, &lastsym
, evsel
, fp
);
1388 printed
+= ip__fprintf_jump(entries
[nr
- 1].from
, &entries
[nr
- 1],
1389 &x
, buffer
, len
, 0, fp
, &total_cycles
,
1390 evsel
, thread
, br_cntr
);
1391 if (PRINT_FIELD(SRCCODE
))
1392 printed
+= print_srccode(thread
, x
.cpumode
, entries
[nr
- 1].from
);
1395 /* Print all blocks */
1396 for (i
= nr
- 2; i
>= 0; i
--) {
1397 if (entries
[i
].from
|| entries
[i
].to
)
1398 pr_debug("%d: %" PRIx64
"-%" PRIx64
"\n", i
,
1401 start
= entries
[i
+ 1].to
;
1402 end
= entries
[i
].from
;
1404 len
= grab_bb(buffer
, start
, end
, machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
1405 /* Patch up missing kernel transfers due to ring filters */
1406 if (len
== -ENXIO
&& i
> 0) {
1407 end
= entries
[--i
].from
;
1408 pr_debug("\tpatching up to %" PRIx64
"-%" PRIx64
"\n", start
, end
);
1409 len
= grab_bb(buffer
, start
, end
, machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
1415 for (off
= 0; off
< (unsigned)len
; off
+= ilen
) {
1416 uint64_t ip
= start
+ off
;
1418 printed
+= ip__fprintf_sym(ip
, thread
, x
.cpumode
, x
.cpu
, &lastsym
, evsel
, fp
);
1420 if (PRINT_FIELD(BRCNTR
) && sample
->branch_stack_cntr
)
1421 br_cntr
= sample
->branch_stack_cntr
[i
];
1422 printed
+= ip__fprintf_jump(ip
, &entries
[i
], &x
, buffer
+ off
, len
- off
, ++insn
, fp
,
1423 &total_cycles
, evsel
, thread
, br_cntr
);
1424 if (PRINT_FIELD(SRCCODE
))
1425 printed
+= print_srccode(thread
, x
.cpumode
, ip
);
1429 printed
+= fprintf(fp
, "\t%016" PRIx64
"\t", ip
);
1430 printed
+= any_dump_insn(evsel
, &x
, ip
, buffer
+ off
, len
- off
, &ilen
, fp
);
1431 if (PRINT_FIELD(BRSTACKINSNLEN
))
1432 printed
+= fprintf(fp
, "\tilen: %d", ilen
);
1433 printed
+= fprintf(fp
, "\n");
1436 if (PRINT_FIELD(SRCCODE
))
1437 print_srccode(thread
, x
.cpumode
, ip
);
1441 if (off
!= end
- start
)
1442 printed
+= fprintf(fp
, "\tmismatch of LBR data and executable\n");
1446 * Hit the branch? In this case we are already done, and the target
1447 * has not been executed yet.
1449 if (entries
[0].from
== sample
->ip
)
1451 if (entries
[0].flags
.abort
)
1455 * Print final block up to sample
1457 * Due to pipeline delays the LBRs might be missing a branch
1458 * or two, which can result in very large or negative blocks
1459 * between final branch and sample. When this happens just
1460 * continue walking after the last TO.
1462 start
= entries
[0].to
;
1465 /* Missing jump. Scan 128 bytes for the next branch */
1468 len
= grab_bb(buffer
, start
, end
, machine
, thread
, &x
.is64bit
, &x
.cpumode
, true);
1469 printed
+= ip__fprintf_sym(start
, thread
, x
.cpumode
, x
.cpu
, &lastsym
, evsel
, fp
);
1471 /* Print at least last IP if basic block did not work */
1472 len
= grab_bb(buffer
, sample
->ip
, sample
->ip
,
1473 machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
1477 printed
+= fprintf(fp
, "\t%016" PRIx64
"\t", sample
->ip
);
1478 printed
+= any_dump_insn(evsel
, &x
, sample
->ip
, buffer
, len
, &ilen
, fp
);
1479 if (PRINT_FIELD(BRSTACKINSNLEN
))
1480 printed
+= fprintf(fp
, "\tilen: %d", ilen
);
1481 printed
+= fprintf(fp
, "\n");
1482 if (PRINT_FIELD(SRCCODE
))
1483 print_srccode(thread
, x
.cpumode
, sample
->ip
);
1486 for (off
= 0; off
<= end
- start
; off
+= ilen
) {
1488 printed
+= fprintf(fp
, "\t%016" PRIx64
"\t", start
+ off
);
1489 printed
+= any_dump_insn(evsel
, &x
, start
+ off
, buffer
+ off
, len
- off
, &ilen
, fp
);
1490 if (PRINT_FIELD(BRSTACKINSNLEN
))
1491 printed
+= fprintf(fp
, "\tilen: %d", ilen
);
1492 printed
+= fprintf(fp
, "\n");
1495 if ((attr
->branch_sample_type
== 0 || attr
->branch_sample_type
& PERF_SAMPLE_BRANCH_ANY
)
1496 && arch_is_uncond_branch(buffer
+ off
, len
- off
, x
.is64bit
)
1497 && start
+ off
!= sample
->ip
) {
1499 * Hit a missing branch. Just stop.
1501 printed
+= fprintf(fp
, "\t... not reaching sample ...\n");
1504 if (PRINT_FIELD(SRCCODE
))
1505 print_srccode(thread
, x
.cpumode
, start
+ off
);
1511 static int perf_sample__fprintf_addr(struct perf_sample
*sample
,
1512 struct thread
*thread
,
1513 struct evsel
*evsel
, FILE *fp
)
1515 struct addr_location al
;
1516 int printed
= fprintf(fp
, "%16" PRIx64
, sample
->addr
);
1518 addr_location__init(&al
);
1519 if (!sample_addr_correlates_sym(&evsel
->core
.attr
))
1522 thread__resolve(thread
, &al
, sample
);
1524 if (PRINT_FIELD(SYM
)) {
1525 printed
+= fprintf(fp
, " ");
1526 if (PRINT_FIELD(SYMOFFSET
))
1527 printed
+= symbol__fprintf_symname_offs(al
.sym
, &al
, fp
);
1529 printed
+= symbol__fprintf_symname(al
.sym
, fp
);
1532 if (PRINT_FIELD(DSO
))
1533 printed
+= map__fprintf_dsoname_dsoff(al
.map
, PRINT_FIELD(DSOFF
), al
.addr
, fp
);
1535 addr_location__exit(&al
);
1539 static const char *resolve_branch_sym(struct perf_sample
*sample
,
1540 struct evsel
*evsel
,
1541 struct thread
*thread
,
1542 struct addr_location
*al
,
1543 struct addr_location
*addr_al
,
1546 const char *name
= NULL
;
1548 if (sample
->flags
& (PERF_IP_FLAG_CALL
| PERF_IP_FLAG_TRACE_BEGIN
)) {
1549 if (sample_addr_correlates_sym(&evsel
->core
.attr
)) {
1550 if (!addr_al
->thread
)
1551 thread__resolve(thread
, addr_al
, sample
);
1553 name
= addr_al
->sym
->name
;
1559 } else if (sample
->flags
& (PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_TRACE_END
)) {
1561 name
= al
->sym
->name
;
1568 static int perf_sample__fprintf_callindent(struct perf_sample
*sample
,
1569 struct evsel
*evsel
,
1570 struct thread
*thread
,
1571 struct addr_location
*al
,
1572 struct addr_location
*addr_al
,
1575 size_t depth
= thread_stack__depth(thread
, sample
->cpu
);
1576 const char *name
= NULL
;
1583 * The 'return' has already been popped off the stack so the depth has
1584 * to be adjusted to match the 'call'.
1586 if (thread__ts(thread
) && sample
->flags
& PERF_IP_FLAG_RETURN
)
1589 name
= resolve_branch_sym(sample
, evsel
, thread
, al
, addr_al
, &ip
);
1591 if (PRINT_FIELD(DSO
) && !(PRINT_FIELD(IP
) || PRINT_FIELD(ADDR
))) {
1592 dlen
+= fprintf(fp
, "(");
1593 dlen
+= map__fprintf_dsoname(al
->map
, fp
);
1594 dlen
+= fprintf(fp
, ")\t");
1598 len
= fprintf(fp
, "%*s%s", (int)depth
* 4, "", name
);
1600 len
= fprintf(fp
, "%*s%16" PRIx64
, (int)depth
* 4, "", ip
);
1606 * Try to keep the output length from changing frequently so that the
1607 * output lines up more nicely.
1609 if (len
> spacing
|| (len
&& len
< spacing
- 52))
1610 spacing
= round_up(len
+ 4, 32);
1613 len
+= fprintf(fp
, "%*s", spacing
- len
, "");
1618 static int perf_sample__fprintf_insn(struct perf_sample
*sample
,
1619 struct evsel
*evsel
,
1620 struct perf_event_attr
*attr
,
1621 struct thread
*thread
,
1622 struct machine
*machine
, FILE *fp
,
1623 struct addr_location
*al
)
1627 script_fetch_insn(sample
, thread
, machine
, native_arch
);
1629 if (PRINT_FIELD(INSNLEN
))
1630 printed
+= fprintf(fp
, " ilen: %d", sample
->insn_len
);
1631 if (PRINT_FIELD(INSN
) && sample
->insn_len
) {
1632 printed
+= fprintf(fp
, " insn: ");
1633 printed
+= sample__fprintf_insn_raw(sample
, fp
);
1635 if (PRINT_FIELD(DISASM
) && sample
->insn_len
) {
1636 printed
+= fprintf(fp
, "\t\t");
1637 printed
+= sample__fprintf_insn_asm(sample
, thread
, machine
, fp
, al
);
1639 if (PRINT_FIELD(BRSTACKINSN
) || PRINT_FIELD(BRSTACKINSNLEN
) || PRINT_FIELD(BRSTACKDISASM
))
1640 printed
+= perf_sample__fprintf_brstackinsn(sample
, evsel
, thread
, attr
, machine
, fp
);
1645 static int perf_sample__fprintf_ipc(struct perf_sample
*sample
,
1646 struct evsel
*evsel
, FILE *fp
)
1650 if (!PRINT_FIELD(IPC
) || !sample
->cyc_cnt
|| !sample
->insn_cnt
)
1653 ipc
= (sample
->insn_cnt
* 100) / sample
->cyc_cnt
;
1655 return fprintf(fp
, " \t IPC: %u.%02u (%" PRIu64
"/%" PRIu64
") ",
1656 ipc
/ 100, ipc
% 100, sample
->insn_cnt
, sample
->cyc_cnt
);
1659 static int perf_sample__fprintf_bts(struct perf_sample
*sample
,
1660 struct evsel
*evsel
,
1661 struct thread
*thread
,
1662 struct addr_location
*al
,
1663 struct addr_location
*addr_al
,
1664 struct machine
*machine
, FILE *fp
)
1666 struct perf_event_attr
*attr
= &evsel
->core
.attr
;
1667 unsigned int type
= evsel__output_type(evsel
);
1668 bool print_srcline_last
= false;
1671 if (PRINT_FIELD(CALLINDENT
))
1672 printed
+= perf_sample__fprintf_callindent(sample
, evsel
, thread
, al
, addr_al
, fp
);
1674 /* print branch_from information */
1675 if (PRINT_FIELD(IP
)) {
1676 unsigned int print_opts
= output
[type
].print_ip_opts
;
1677 struct callchain_cursor
*cursor
= NULL
;
1679 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
1680 cursor
= get_tls_callchain_cursor();
1681 if (thread__resolve_callchain(al
->thread
, cursor
, evsel
,
1683 scripting_max_stack
))
1686 if (cursor
== NULL
) {
1687 printed
+= fprintf(fp
, " ");
1688 if (print_opts
& EVSEL__PRINT_SRCLINE
) {
1689 print_srcline_last
= true;
1690 print_opts
&= ~EVSEL__PRINT_SRCLINE
;
1693 printed
+= fprintf(fp
, "\n");
1695 printed
+= sample__fprintf_sym(sample
, al
, 0, print_opts
, cursor
,
1696 symbol_conf
.bt_stop_list
, fp
);
1699 /* print branch_to information */
1700 if (PRINT_FIELD(ADDR
) ||
1701 ((evsel
->core
.attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
1702 !output
[type
].user_set
)) {
1703 printed
+= fprintf(fp
, " => ");
1704 printed
+= perf_sample__fprintf_addr(sample
, thread
, evsel
, fp
);
1707 printed
+= perf_sample__fprintf_ipc(sample
, evsel
, fp
);
1709 if (print_srcline_last
)
1710 printed
+= map__fprintf_srcline(al
->map
, al
->addr
, "\n ", fp
);
1712 printed
+= perf_sample__fprintf_insn(sample
, evsel
, attr
, thread
, machine
, fp
, al
);
1713 printed
+= fprintf(fp
, "\n");
1714 if (PRINT_FIELD(SRCCODE
)) {
1715 int ret
= map__fprintf_srccode(al
->map
, al
->addr
, stdout
,
1716 thread__srccode_state(thread
));
1719 printed
+= printf("\n");
1725 static int perf_sample__fprintf_flags(u32 flags
, FILE *fp
)
1727 char str
[SAMPLE_FLAGS_BUF_SIZE
];
1730 ret
= perf_sample__sprintf_flags(flags
, str
, sizeof(str
));
1732 return fprintf(fp
, " raw flags:0x%-*x ",
1733 SAMPLE_FLAGS_STR_ALIGNED_SIZE
- 12, flags
);
1735 return fprintf(fp
, " %-*s ", SAMPLE_FLAGS_STR_ALIGNED_SIZE
, str
);
1738 struct printer_data
{
1744 static int sample__fprintf_bpf_output(enum binary_printer_ops op
,
1746 void *extra
, FILE *fp
)
1748 unsigned char ch
= (unsigned char)val
;
1749 struct printer_data
*printer_data
= extra
;
1753 case BINARY_PRINT_DATA_BEGIN
:
1754 printed
+= fprintf(fp
, "\n");
1756 case BINARY_PRINT_LINE_BEGIN
:
1757 printed
+= fprintf(fp
, "%17s", !printer_data
->line_no
? "BPF output:" :
1760 case BINARY_PRINT_ADDR
:
1761 printed
+= fprintf(fp
, " %04x:", val
);
1763 case BINARY_PRINT_NUM_DATA
:
1764 printed
+= fprintf(fp
, " %02x", val
);
1766 case BINARY_PRINT_NUM_PAD
:
1767 printed
+= fprintf(fp
, " ");
1769 case BINARY_PRINT_SEP
:
1770 printed
+= fprintf(fp
, " ");
1772 case BINARY_PRINT_CHAR_DATA
:
1773 if (printer_data
->hit_nul
&& ch
)
1774 printer_data
->is_printable
= false;
1777 printed
+= fprintf(fp
, "%c", '.');
1779 if (!printer_data
->is_printable
)
1783 printer_data
->hit_nul
= true;
1785 printer_data
->is_printable
= false;
1787 printed
+= fprintf(fp
, "%c", ch
);
1790 case BINARY_PRINT_CHAR_PAD
:
1791 printed
+= fprintf(fp
, " ");
1793 case BINARY_PRINT_LINE_END
:
1794 printed
+= fprintf(fp
, "\n");
1795 printer_data
->line_no
++;
1797 case BINARY_PRINT_DATA_END
:
1805 static int perf_sample__fprintf_bpf_output(struct perf_sample
*sample
, FILE *fp
)
1807 unsigned int nr_bytes
= sample
->raw_size
;
1808 struct printer_data printer_data
= {0, false, true};
1809 int printed
= binary__fprintf(sample
->raw_data
, nr_bytes
, 8,
1810 sample__fprintf_bpf_output
, &printer_data
, fp
);
1812 if (printer_data
.is_printable
&& printer_data
.hit_nul
)
1813 printed
+= fprintf(fp
, "%17s \"%s\"\n", "BPF string:", (char *)(sample
->raw_data
));
1818 static int perf_sample__fprintf_spacing(int len
, int spacing
, FILE *fp
)
1820 if (len
> 0 && len
< spacing
)
1821 return fprintf(fp
, "%*s", spacing
- len
, "");
1826 static int perf_sample__fprintf_pt_spacing(int len
, FILE *fp
)
1828 return perf_sample__fprintf_spacing(len
, 34, fp
);
1831 /* If a value contains only printable ASCII characters padded with NULLs */
1832 static bool ptw_is_prt(u64 val
)
1837 for (i
= 0; i
< sizeof(val
); i
++) {
1838 c
= ((char *)&val
)[i
];
1841 if (!isprint(c
) || !isascii(c
))
1844 for (; i
< sizeof(val
); i
++) {
1845 c
= ((char *)&val
)[i
];
1852 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample
*sample
, FILE *fp
)
1854 struct perf_synth_intel_ptwrite
*data
= perf_sample__synth_ptr(sample
);
1855 char str
[sizeof(u64
) + 1] = "";
1859 if (perf_sample__bad_synth_size(sample
, *data
))
1862 val
= le64_to_cpu(data
->payload
);
1863 if (ptw_is_prt(val
)) {
1864 memcpy(str
, &val
, sizeof(val
));
1865 str
[sizeof(val
)] = 0;
1867 len
= fprintf(fp
, " IP: %u payload: %#" PRIx64
" %s ",
1868 data
->ip
, val
, str
);
1869 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1872 static int perf_sample__fprintf_synth_mwait(struct perf_sample
*sample
, FILE *fp
)
1874 struct perf_synth_intel_mwait
*data
= perf_sample__synth_ptr(sample
);
1877 if (perf_sample__bad_synth_size(sample
, *data
))
1880 len
= fprintf(fp
, " hints: %#x extensions: %#x ",
1881 data
->hints
, data
->extensions
);
1882 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1885 static int perf_sample__fprintf_synth_pwre(struct perf_sample
*sample
, FILE *fp
)
1887 struct perf_synth_intel_pwre
*data
= perf_sample__synth_ptr(sample
);
1890 if (perf_sample__bad_synth_size(sample
, *data
))
1893 len
= fprintf(fp
, " hw: %u cstate: %u sub-cstate: %u ",
1894 data
->hw
, data
->cstate
, data
->subcstate
);
1895 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1898 static int perf_sample__fprintf_synth_exstop(struct perf_sample
*sample
, FILE *fp
)
1900 struct perf_synth_intel_exstop
*data
= perf_sample__synth_ptr(sample
);
1903 if (perf_sample__bad_synth_size(sample
, *data
))
1906 len
= fprintf(fp
, " IP: %u ", data
->ip
);
1907 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1910 static int perf_sample__fprintf_synth_pwrx(struct perf_sample
*sample
, FILE *fp
)
1912 struct perf_synth_intel_pwrx
*data
= perf_sample__synth_ptr(sample
);
1915 if (perf_sample__bad_synth_size(sample
, *data
))
1918 len
= fprintf(fp
, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1919 data
->deepest_cstate
, data
->last_cstate
,
1921 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1924 static int perf_sample__fprintf_synth_cbr(struct perf_sample
*sample
, FILE *fp
)
1926 struct perf_synth_intel_cbr
*data
= perf_sample__synth_ptr(sample
);
1927 unsigned int percent
, freq
;
1930 if (perf_sample__bad_synth_size(sample
, *data
))
1933 freq
= (le32_to_cpu(data
->freq
) + 500) / 1000;
1934 len
= fprintf(fp
, " cbr: %2u freq: %4u MHz ", data
->cbr
, freq
);
1935 if (data
->max_nonturbo
) {
1936 percent
= (5 + (1000 * data
->cbr
) / data
->max_nonturbo
) / 10;
1937 len
+= fprintf(fp
, "(%3u%%) ", percent
);
1939 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1942 static int perf_sample__fprintf_synth_psb(struct perf_sample
*sample
, FILE *fp
)
1944 struct perf_synth_intel_psb
*data
= perf_sample__synth_ptr(sample
);
1947 if (perf_sample__bad_synth_size(sample
, *data
))
1950 len
= fprintf(fp
, " psb offs: %#" PRIx64
, data
->offset
);
1951 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1954 /* Intel PT Event Trace */
1955 static int perf_sample__fprintf_synth_evt(struct perf_sample
*sample
, FILE *fp
)
1957 struct perf_synth_intel_evt
*data
= perf_sample__synth_ptr(sample
);
1958 const char *cfe
[32] = {NULL
, "INTR", "IRET", "SMI", "RSM", "SIPI",
1959 "INIT", "VMENTRY", "VMEXIT", "VMEXIT_INTR",
1960 "SHUTDOWN", NULL
, "UINTR", "UIRET"};
1961 const char *evd
[64] = {"PFA", "VMXQ", "VMXR"};
1965 if (perf_sample__bad_synth_size(sample
, *data
))
1968 s
= cfe
[data
->type
];
1970 len
= fprintf(fp
, " cfe: %s IP: %d vector: %u",
1971 s
, data
->ip
, data
->vector
);
1973 len
= fprintf(fp
, " cfe: %u IP: %d vector: %u",
1974 data
->type
, data
->ip
, data
->vector
);
1976 for (i
= 0; i
< data
->evd_cnt
; i
++) {
1977 unsigned int et
= data
->evd
[i
].evd_type
& 0x3f;
1981 len
+= fprintf(fp
, " %s: %#" PRIx64
,
1982 s
, data
->evd
[i
].payload
);
1984 len
+= fprintf(fp
, " EVD_%u: %#" PRIx64
,
1985 et
, data
->evd
[i
].payload
);
1988 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1991 static int perf_sample__fprintf_synth_iflag_chg(struct perf_sample
*sample
, FILE *fp
)
1993 struct perf_synth_intel_iflag_chg
*data
= perf_sample__synth_ptr(sample
);
1996 if (perf_sample__bad_synth_size(sample
, *data
))
1999 len
= fprintf(fp
, " IFLAG: %d->%d %s branch", !data
->iflag
, data
->iflag
,
2000 data
->via_branch
? "via" : "non");
2001 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
2004 static int perf_sample__fprintf_synth(struct perf_sample
*sample
,
2005 struct evsel
*evsel
, FILE *fp
)
2007 switch (evsel
->core
.attr
.config
) {
2008 case PERF_SYNTH_INTEL_PTWRITE
:
2009 return perf_sample__fprintf_synth_ptwrite(sample
, fp
);
2010 case PERF_SYNTH_INTEL_MWAIT
:
2011 return perf_sample__fprintf_synth_mwait(sample
, fp
);
2012 case PERF_SYNTH_INTEL_PWRE
:
2013 return perf_sample__fprintf_synth_pwre(sample
, fp
);
2014 case PERF_SYNTH_INTEL_EXSTOP
:
2015 return perf_sample__fprintf_synth_exstop(sample
, fp
);
2016 case PERF_SYNTH_INTEL_PWRX
:
2017 return perf_sample__fprintf_synth_pwrx(sample
, fp
);
2018 case PERF_SYNTH_INTEL_CBR
:
2019 return perf_sample__fprintf_synth_cbr(sample
, fp
);
2020 case PERF_SYNTH_INTEL_PSB
:
2021 return perf_sample__fprintf_synth_psb(sample
, fp
);
2022 case PERF_SYNTH_INTEL_EVT
:
2023 return perf_sample__fprintf_synth_evt(sample
, fp
);
2024 case PERF_SYNTH_INTEL_IFLAG_CHG
:
2025 return perf_sample__fprintf_synth_iflag_chg(sample
, fp
);
2033 static int evlist__max_name_len(struct evlist
*evlist
)
2035 struct evsel
*evsel
;
2038 evlist__for_each_entry(evlist
, evsel
) {
2039 int len
= strlen(evsel__name(evsel
));
2041 max
= MAX(len
, max
);
2047 static int data_src__fprintf(u64 data_src
, FILE *fp
)
2049 struct mem_info
*mi
= mem_info__new();
2058 mem_info__data_src(mi
)->val
= data_src
;
2059 perf_script__meminfo_scnprintf(decode
, 100, mi
);
2062 len
= scnprintf(out
, 100, "%16" PRIx64
" %s", data_src
, decode
);
2066 return fprintf(fp
, "%-*s", maxlen
, out
);
2070 struct perf_sample
*sample
;
2071 struct thread
*thread
;
2072 struct evsel
*evsel
;
2076 static void script_print_metric(struct perf_stat_config
*config __maybe_unused
,
2077 void *ctx
, enum metric_threshold_classify thresh
,
2078 const char *fmt
, const char *unit
, double val
)
2080 struct metric_ctx
*mctx
= ctx
;
2081 const char *color
= metric_threshold_classify__color(thresh
);
2085 perf_sample__fprintf_start(NULL
, mctx
->sample
, mctx
->thread
, mctx
->evsel
,
2086 PERF_RECORD_SAMPLE
, mctx
->fp
);
2087 fputs("\tmetric: ", mctx
->fp
);
2089 color_fprintf(mctx
->fp
, color
, fmt
, val
);
2092 fprintf(mctx
->fp
, " %s\n", unit
);
2095 static void script_new_line(struct perf_stat_config
*config __maybe_unused
,
2098 struct metric_ctx
*mctx
= ctx
;
2100 perf_sample__fprintf_start(NULL
, mctx
->sample
, mctx
->thread
, mctx
->evsel
,
2101 PERF_RECORD_SAMPLE
, mctx
->fp
);
2102 fputs("\tmetric: ", mctx
->fp
);
2105 static void perf_sample__fprint_metric(struct perf_script
*script
,
2106 struct thread
*thread
,
2107 struct evsel
*evsel
,
2108 struct perf_sample
*sample
,
2111 struct evsel
*leader
= evsel__leader(evsel
);
2112 struct perf_stat_output_ctx ctx
= {
2113 .print_metric
= script_print_metric
,
2114 .new_line
= script_new_line
,
2115 .ctx
= &(struct metric_ctx
) {
2121 .force_header
= false,
2127 evlist__alloc_stats(&stat_config
, script
->session
->evlist
, /*alloc_raw=*/false);
2128 if (evsel_script(leader
)->gnum
++ == 0)
2129 perf_stat__reset_shadow_stats();
2130 val
= sample
->period
* evsel
->scale
;
2131 evsel_script(evsel
)->val
= val
;
2132 if (evsel_script(leader
)->gnum
== leader
->core
.nr_members
) {
2133 for_each_group_member (ev2
, leader
) {
2134 perf_stat__print_shadow_stats(&stat_config
, ev2
,
2135 evsel_script(ev2
)->val
,
2140 evsel_script(leader
)->gnum
= 0;
2144 static bool show_event(struct perf_sample
*sample
,
2145 struct evsel
*evsel
,
2146 struct thread
*thread
,
2147 struct addr_location
*al
,
2148 struct addr_location
*addr_al
)
2150 int depth
= thread_stack__depth(thread
, sample
->cpu
);
2152 if (!symbol_conf
.graph_function
)
2155 if (thread__filter(thread
)) {
2156 if (depth
<= thread__filter_entry_depth(thread
)) {
2157 thread__set_filter(thread
, false);
2162 const char *s
= symbol_conf
.graph_function
;
2164 const char *name
= resolve_branch_sym(sample
, evsel
, thread
, al
, addr_al
,
2170 nlen
= strlen(name
);
2172 unsigned len
= strcspn(s
, ",");
2173 if (nlen
== len
&& !strncmp(name
, s
, len
)) {
2174 thread__set_filter(thread
, true);
2175 thread__set_filter_entry_depth(thread
, depth
);
2186 static void process_event(struct perf_script
*script
,
2187 struct perf_sample
*sample
, struct evsel
*evsel
,
2188 struct addr_location
*al
,
2189 struct addr_location
*addr_al
,
2190 struct machine
*machine
)
2192 struct thread
*thread
= al
->thread
;
2193 struct perf_event_attr
*attr
= &evsel
->core
.attr
;
2194 unsigned int type
= evsel__output_type(evsel
);
2195 struct evsel_script
*es
= evsel
->priv
;
2197 char str
[PAGE_SIZE_NAME_LEN
];
2198 const char *arch
= perf_env__arch(machine
->env
);
2200 if (output
[type
].fields
== 0)
2205 perf_sample__fprintf_start(script
, sample
, thread
, evsel
,
2206 PERF_RECORD_SAMPLE
, fp
);
2208 if (PRINT_FIELD(PERIOD
))
2209 fprintf(fp
, "%10" PRIu64
" ", sample
->period
);
2211 if (PRINT_FIELD(EVNAME
)) {
2212 const char *evname
= evsel__name(evsel
);
2214 if (!script
->name_width
)
2215 script
->name_width
= evlist__max_name_len(script
->session
->evlist
);
2217 fprintf(fp
, "%*s: ", script
->name_width
, evname
?: "[unknown]");
2221 perf_sample__fprintf_flags(sample
->flags
, fp
);
2223 if (is_bts_event(attr
)) {
2224 perf_sample__fprintf_bts(sample
, evsel
, thread
, al
, addr_al
, machine
, fp
);
2227 #ifdef HAVE_LIBTRACEEVENT
2228 if (PRINT_FIELD(TRACE
) && sample
->raw_data
) {
2229 const struct tep_event
*tp_format
= evsel__tp_format(evsel
);
2232 event_format__fprintf(tp_format
, sample
->cpu
,
2233 sample
->raw_data
, sample
->raw_size
,
2238 if (attr
->type
== PERF_TYPE_SYNTH
&& PRINT_FIELD(SYNTH
))
2239 perf_sample__fprintf_synth(sample
, evsel
, fp
);
2241 if (PRINT_FIELD(ADDR
))
2242 perf_sample__fprintf_addr(sample
, thread
, evsel
, fp
);
2244 if (PRINT_FIELD(DATA_SRC
))
2245 data_src__fprintf(sample
->data_src
, fp
);
2247 if (PRINT_FIELD(WEIGHT
))
2248 fprintf(fp
, "%16" PRIu64
, sample
->weight
);
2250 if (PRINT_FIELD(INS_LAT
))
2251 fprintf(fp
, "%16" PRIu16
, sample
->ins_lat
);
2253 if (PRINT_FIELD(RETIRE_LAT
))
2254 fprintf(fp
, "%16" PRIu16
, sample
->retire_lat
);
2256 if (PRINT_FIELD(CGROUP
)) {
2257 const char *cgrp_name
;
2258 struct cgroup
*cgrp
= cgroup__find(machine
->env
,
2261 cgrp_name
= cgrp
->name
;
2263 cgrp_name
= "unknown";
2264 fprintf(fp
, " %s", cgrp_name
);
2267 if (PRINT_FIELD(IP
)) {
2268 struct callchain_cursor
*cursor
= NULL
;
2270 if (script
->stitch_lbr
)
2271 thread__set_lbr_stitch_enable(al
->thread
, true);
2273 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
2274 cursor
= get_tls_callchain_cursor();
2275 if (thread__resolve_callchain(al
->thread
, cursor
, evsel
,
2277 scripting_max_stack
))
2280 fputc(cursor
? '\n' : ' ', fp
);
2281 sample__fprintf_sym(sample
, al
, 0, output
[type
].print_ip_opts
, cursor
,
2282 symbol_conf
.bt_stop_list
, fp
);
2285 if (PRINT_FIELD(IREGS
))
2286 perf_sample__fprintf_iregs(sample
, attr
, arch
, fp
);
2288 if (PRINT_FIELD(UREGS
))
2289 perf_sample__fprintf_uregs(sample
, attr
, arch
, fp
);
2291 if (PRINT_FIELD(BRSTACK
))
2292 perf_sample__fprintf_brstack(sample
, thread
, evsel
, fp
);
2293 else if (PRINT_FIELD(BRSTACKSYM
))
2294 perf_sample__fprintf_brstacksym(sample
, thread
, evsel
, fp
);
2295 else if (PRINT_FIELD(BRSTACKOFF
))
2296 perf_sample__fprintf_brstackoff(sample
, thread
, evsel
, fp
);
2298 if (evsel__is_bpf_output(evsel
) && !evsel__is_offcpu_event(evsel
) && PRINT_FIELD(BPF_OUTPUT
))
2299 perf_sample__fprintf_bpf_output(sample
, fp
);
2300 perf_sample__fprintf_insn(sample
, evsel
, attr
, thread
, machine
, fp
, al
);
2302 if (PRINT_FIELD(PHYS_ADDR
))
2303 fprintf(fp
, "%16" PRIx64
, sample
->phys_addr
);
2305 if (PRINT_FIELD(DATA_PAGE_SIZE
))
2306 fprintf(fp
, " %s", get_page_size_name(sample
->data_page_size
, str
));
2308 if (PRINT_FIELD(CODE_PAGE_SIZE
))
2309 fprintf(fp
, " %s", get_page_size_name(sample
->code_page_size
, str
));
2311 perf_sample__fprintf_ipc(sample
, evsel
, fp
);
2315 if (PRINT_FIELD(SRCCODE
)) {
2316 if (map__fprintf_srccode(al
->map
, al
->addr
, stdout
,
2317 thread__srccode_state(thread
)))
2321 if (PRINT_FIELD(METRIC
))
2322 perf_sample__fprint_metric(script
, thread
, evsel
, sample
, fp
);
2328 static struct scripting_ops
*scripting_ops
;
2330 static void __process_stat(struct evsel
*counter
, u64 tstamp
)
2332 int nthreads
= perf_thread_map__nr(counter
->core
.threads
);
2334 struct perf_cpu cpu
;
2335 static int header_printed
;
2337 if (!header_printed
) {
2338 printf("%3s %8s %15s %15s %15s %15s %s\n",
2339 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
2343 for (thread
= 0; thread
< nthreads
; thread
++) {
2344 perf_cpu_map__for_each_cpu(cpu
, idx
, evsel__cpus(counter
)) {
2345 struct perf_counts_values
*counts
;
2347 counts
= perf_counts(counter
->counts
, idx
, thread
);
2349 printf("%3d %8d %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %s\n",
2351 perf_thread_map__pid(counter
->core
.threads
, thread
),
2356 evsel__name(counter
));
2361 static void process_stat(struct evsel
*counter
, u64 tstamp
)
2363 if (scripting_ops
&& scripting_ops
->process_stat
)
2364 scripting_ops
->process_stat(&stat_config
, counter
, tstamp
);
2366 __process_stat(counter
, tstamp
);
2369 static void process_stat_interval(u64 tstamp
)
2371 if (scripting_ops
&& scripting_ops
->process_stat_interval
)
2372 scripting_ops
->process_stat_interval(tstamp
);
2375 static void setup_scripting(void)
2377 #ifdef HAVE_LIBTRACEEVENT
2378 setup_perl_scripting();
2380 setup_python_scripting();
2383 static int flush_scripting(void)
2385 return scripting_ops
? scripting_ops
->flush_script() : 0;
2388 static int cleanup_scripting(void)
2390 pr_debug("\nperf script stopped\n");
2392 return scripting_ops
? scripting_ops
->stop_script() : 0;
2395 static bool filter_cpu(struct perf_sample
*sample
)
2397 if (cpu_list
&& sample
->cpu
!= (u32
)-1)
2398 return !test_bit(sample
->cpu
, cpu_bitmap
);
2402 static int process_sample_event(const struct perf_tool
*tool
,
2403 union perf_event
*event
,
2404 struct perf_sample
*sample
,
2405 struct evsel
*evsel
,
2406 struct machine
*machine
)
2408 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
2409 struct addr_location al
;
2410 struct addr_location addr_al
;
2413 /* Set thread to NULL to indicate addr_al and al are not initialized */
2414 addr_location__init(&al
);
2415 addr_location__init(&addr_al
);
2417 ret
= dlfilter__filter_event_early(dlfilter
, event
, sample
, evsel
, machine
, &al
, &addr_al
);
2424 if (perf_time__ranges_skip_sample(scr
->ptime_range
, scr
->range_num
,
2430 if (sample
->time
< last_timestamp
) {
2431 pr_err("Samples misordered, previous: %" PRIu64
2432 " this: %" PRIu64
"\n", last_timestamp
,
2436 last_timestamp
= sample
->time
;
2440 if (filter_cpu(sample
))
2443 if (!al
.thread
&& machine__resolve(machine
, &al
, sample
) < 0) {
2444 pr_err("problem processing %d event, skipping it.\n",
2445 event
->header
.type
);
2453 if (!show_event(sample
, evsel
, al
.thread
, &al
, &addr_al
))
2456 if (evswitch__discard(&scr
->evswitch
, evsel
))
2459 ret
= dlfilter__filter_event(dlfilter
, event
, sample
, evsel
, machine
, &al
, &addr_al
);
2466 if (scripting_ops
) {
2467 struct addr_location
*addr_al_ptr
= NULL
;
2469 if ((evsel
->core
.attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
2470 sample_addr_correlates_sym(&evsel
->core
.attr
)) {
2471 if (!addr_al
.thread
)
2472 thread__resolve(al
.thread
, &addr_al
, sample
);
2473 addr_al_ptr
= &addr_al
;
2475 scripting_ops
->process_event(event
, sample
, evsel
, &al
, addr_al_ptr
);
2477 process_event(scr
, sample
, evsel
, &al
, &addr_al
, machine
);
2481 addr_location__exit(&addr_al
);
2482 addr_location__exit(&al
);
2486 // Used when scr->per_event_dump is not set
2487 static struct evsel_script es_stdout
;
2489 static int process_attr(const struct perf_tool
*tool
, union perf_event
*event
,
2490 struct evlist
**pevlist
)
2492 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
2493 struct evlist
*evlist
;
2494 struct evsel
*evsel
, *pos
;
2498 err
= perf_event__process_attr(tool
, event
, pevlist
);
2503 evsel
= evlist__last(*pevlist
);
2506 if (scr
->per_event_dump
) {
2507 evsel
->priv
= evsel_script__new(evsel
, scr
->session
->data
);
2510 } else { // Replicate what is done in perf_script__setup_per_event_dump()
2511 es_stdout
.fp
= stdout
;
2512 evsel
->priv
= &es_stdout
;
2516 if (evsel
->core
.attr
.type
>= PERF_TYPE_MAX
&&
2517 evsel
->core
.attr
.type
!= PERF_TYPE_SYNTH
)
2520 evlist__for_each_entry(evlist
, pos
) {
2521 if (pos
->core
.attr
.type
== evsel
->core
.attr
.type
&& pos
!= evsel
)
2525 if (evsel
->core
.attr
.sample_type
) {
2526 err
= evsel__check_attr(evsel
, scr
->session
);
2532 * Check if we need to enable callchains based
2533 * on events sample_type.
2535 sample_type
= evlist__combined_sample_type(evlist
);
2536 callchain_param_setup(sample_type
, perf_env__arch((*pevlist
)->env
));
2538 /* Enable fields for callchain entries */
2539 if (symbol_conf
.use_callchain
&&
2540 (sample_type
& PERF_SAMPLE_CALLCHAIN
||
2541 sample_type
& PERF_SAMPLE_BRANCH_STACK
||
2542 (sample_type
& PERF_SAMPLE_REGS_USER
&&
2543 sample_type
& PERF_SAMPLE_STACK_USER
))) {
2544 int type
= evsel__output_type(evsel
);
2546 if (!(output
[type
].user_unset_fields
& PERF_OUTPUT_IP
))
2547 output
[type
].fields
|= PERF_OUTPUT_IP
;
2548 if (!(output
[type
].user_unset_fields
& PERF_OUTPUT_SYM
))
2549 output
[type
].fields
|= PERF_OUTPUT_SYM
;
2551 evsel__set_print_ip_opts(evsel
);
2555 static int print_event_with_time(const struct perf_tool
*tool
,
2556 union perf_event
*event
,
2557 struct perf_sample
*sample
,
2558 struct machine
*machine
,
2559 pid_t pid
, pid_t tid
, u64 timestamp
)
2561 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2562 struct perf_session
*session
= script
->session
;
2563 struct evsel
*evsel
= evlist__id2evsel(session
->evlist
, sample
->id
);
2564 struct thread
*thread
= NULL
;
2566 if (evsel
&& !evsel
->core
.attr
.sample_id_all
) {
2568 sample
->time
= timestamp
;
2573 if (filter_cpu(sample
))
2577 thread
= machine__findnew_thread(machine
, pid
, tid
);
2580 perf_sample__fprintf_start(script
, sample
, thread
, evsel
,
2581 event
->header
.type
, stdout
);
2584 perf_event__fprintf(event
, machine
, stdout
);
2586 thread__put(thread
);
2591 static int print_event(const struct perf_tool
*tool
, union perf_event
*event
,
2592 struct perf_sample
*sample
, struct machine
*machine
,
2593 pid_t pid
, pid_t tid
)
2595 return print_event_with_time(tool
, event
, sample
, machine
, pid
, tid
, 0);
2598 static int process_comm_event(const struct perf_tool
*tool
,
2599 union perf_event
*event
,
2600 struct perf_sample
*sample
,
2601 struct machine
*machine
)
2603 if (perf_event__process_comm(tool
, event
, sample
, machine
) < 0)
2606 return print_event(tool
, event
, sample
, machine
, event
->comm
.pid
,
2610 static int process_namespaces_event(const struct perf_tool
*tool
,
2611 union perf_event
*event
,
2612 struct perf_sample
*sample
,
2613 struct machine
*machine
)
2615 if (perf_event__process_namespaces(tool
, event
, sample
, machine
) < 0)
2618 return print_event(tool
, event
, sample
, machine
, event
->namespaces
.pid
,
2619 event
->namespaces
.tid
);
2622 static int process_cgroup_event(const struct perf_tool
*tool
,
2623 union perf_event
*event
,
2624 struct perf_sample
*sample
,
2625 struct machine
*machine
)
2627 if (perf_event__process_cgroup(tool
, event
, sample
, machine
) < 0)
2630 return print_event(tool
, event
, sample
, machine
, sample
->pid
,
2634 static int process_fork_event(const struct perf_tool
*tool
,
2635 union perf_event
*event
,
2636 struct perf_sample
*sample
,
2637 struct machine
*machine
)
2639 if (perf_event__process_fork(tool
, event
, sample
, machine
) < 0)
2642 return print_event_with_time(tool
, event
, sample
, machine
,
2643 event
->fork
.pid
, event
->fork
.tid
,
2646 static int process_exit_event(const struct perf_tool
*tool
,
2647 union perf_event
*event
,
2648 struct perf_sample
*sample
,
2649 struct machine
*machine
)
2651 /* Print before 'exit' deletes anything */
2652 if (print_event_with_time(tool
, event
, sample
, machine
, event
->fork
.pid
,
2653 event
->fork
.tid
, event
->fork
.time
))
2656 return perf_event__process_exit(tool
, event
, sample
, machine
);
2659 static int process_mmap_event(const struct perf_tool
*tool
,
2660 union perf_event
*event
,
2661 struct perf_sample
*sample
,
2662 struct machine
*machine
)
2664 if (perf_event__process_mmap(tool
, event
, sample
, machine
) < 0)
2667 return print_event(tool
, event
, sample
, machine
, event
->mmap
.pid
,
2671 static int process_mmap2_event(const struct perf_tool
*tool
,
2672 union perf_event
*event
,
2673 struct perf_sample
*sample
,
2674 struct machine
*machine
)
2676 if (perf_event__process_mmap2(tool
, event
, sample
, machine
) < 0)
2679 return print_event(tool
, event
, sample
, machine
, event
->mmap2
.pid
,
2683 static int process_switch_event(const struct perf_tool
*tool
,
2684 union perf_event
*event
,
2685 struct perf_sample
*sample
,
2686 struct machine
*machine
)
2688 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2690 if (perf_event__process_switch(tool
, event
, sample
, machine
) < 0)
2693 if (scripting_ops
&& scripting_ops
->process_switch
&& !filter_cpu(sample
))
2694 scripting_ops
->process_switch(event
, sample
, machine
);
2696 if (!script
->show_switch_events
)
2699 return print_event(tool
, event
, sample
, machine
, sample
->pid
,
2703 static int process_auxtrace_error(struct perf_session
*session
,
2704 union perf_event
*event
)
2706 if (scripting_ops
&& scripting_ops
->process_auxtrace_error
) {
2707 scripting_ops
->process_auxtrace_error(session
, event
);
2711 return perf_event__process_auxtrace_error(session
, event
);
2715 process_lost_event(const struct perf_tool
*tool
,
2716 union perf_event
*event
,
2717 struct perf_sample
*sample
,
2718 struct machine
*machine
)
2720 return print_event(tool
, event
, sample
, machine
, sample
->pid
,
2725 process_throttle_event(const struct perf_tool
*tool __maybe_unused
,
2726 union perf_event
*event
,
2727 struct perf_sample
*sample
,
2728 struct machine
*machine
)
2730 if (scripting_ops
&& scripting_ops
->process_throttle
)
2731 scripting_ops
->process_throttle(event
, sample
, machine
);
2736 process_finished_round_event(const struct perf_tool
*tool __maybe_unused
,
2737 union perf_event
*event
,
2738 struct ordered_events
*oe __maybe_unused
)
2741 perf_event__fprintf(event
, NULL
, stdout
);
2746 process_bpf_events(const struct perf_tool
*tool __maybe_unused
,
2747 union perf_event
*event
,
2748 struct perf_sample
*sample
,
2749 struct machine
*machine
)
2751 if (machine__process_ksymbol(machine
, event
, sample
) < 0)
2754 return print_event(tool
, event
, sample
, machine
, sample
->pid
,
2758 static int process_text_poke_events(const struct perf_tool
*tool
,
2759 union perf_event
*event
,
2760 struct perf_sample
*sample
,
2761 struct machine
*machine
)
2763 if (perf_event__process_text_poke(tool
, event
, sample
, machine
) < 0)
2766 return print_event(tool
, event
, sample
, machine
, sample
->pid
,
2770 static void sig_handler(int sig __maybe_unused
)
2775 static void perf_script__fclose_per_event_dump(struct perf_script
*script
)
2777 struct evlist
*evlist
= script
->session
->evlist
;
2778 struct evsel
*evsel
;
2780 evlist__for_each_entry(evlist
, evsel
) {
2783 evsel_script__delete(evsel
->priv
);
2788 static int perf_script__fopen_per_event_dump(struct perf_script
*script
)
2790 struct evsel
*evsel
;
2792 evlist__for_each_entry(script
->session
->evlist
, evsel
) {
2794 * Already setup? I.e. we may be called twice in cases like
2795 * Intel PT, one for the intel_pt// and dummy events, then
2796 * for the evsels synthesized from the auxtrace info.
2798 * Ses perf_script__process_auxtrace_info.
2800 if (evsel
->priv
!= NULL
)
2803 evsel
->priv
= evsel_script__new(evsel
, script
->session
->data
);
2804 if (evsel
->priv
== NULL
)
2805 goto out_err_fclose
;
2811 perf_script__fclose_per_event_dump(script
);
2815 static int perf_script__setup_per_event_dump(struct perf_script
*script
)
2817 struct evsel
*evsel
;
2819 if (script
->per_event_dump
)
2820 return perf_script__fopen_per_event_dump(script
);
2822 es_stdout
.fp
= stdout
;
2824 evlist__for_each_entry(script
->session
->evlist
, evsel
)
2825 evsel
->priv
= &es_stdout
;
2830 static void perf_script__exit_per_event_dump_stats(struct perf_script
*script
)
2832 struct evsel
*evsel
;
2834 evlist__for_each_entry(script
->session
->evlist
, evsel
) {
2835 struct evsel_script
*es
= evsel
->priv
;
2837 evsel_script__fprintf(es
, stdout
);
2838 evsel_script__delete(es
);
2843 static void perf_script__exit(struct perf_script
*script
)
2845 perf_thread_map__put(script
->threads
);
2846 perf_cpu_map__put(script
->cpus
);
2849 static int __cmd_script(struct perf_script
*script
)
2853 signal(SIGINT
, sig_handler
);
2855 /* override event processing functions */
2856 if (script
->show_task_events
) {
2857 script
->tool
.comm
= process_comm_event
;
2858 script
->tool
.fork
= process_fork_event
;
2859 script
->tool
.exit
= process_exit_event
;
2861 if (script
->show_mmap_events
) {
2862 script
->tool
.mmap
= process_mmap_event
;
2863 script
->tool
.mmap2
= process_mmap2_event
;
2865 if (script
->show_switch_events
|| (scripting_ops
&& scripting_ops
->process_switch
))
2866 script
->tool
.context_switch
= process_switch_event
;
2867 if (scripting_ops
&& scripting_ops
->process_auxtrace_error
)
2868 script
->tool
.auxtrace_error
= process_auxtrace_error
;
2869 if (script
->show_namespace_events
)
2870 script
->tool
.namespaces
= process_namespaces_event
;
2871 if (script
->show_cgroup_events
)
2872 script
->tool
.cgroup
= process_cgroup_event
;
2873 if (script
->show_lost_events
)
2874 script
->tool
.lost
= process_lost_event
;
2875 if (script
->show_round_events
) {
2876 script
->tool
.ordered_events
= false;
2877 script
->tool
.finished_round
= process_finished_round_event
;
2879 if (script
->show_bpf_events
) {
2880 script
->tool
.ksymbol
= process_bpf_events
;
2881 script
->tool
.bpf
= process_bpf_events
;
2883 if (script
->show_text_poke_events
) {
2884 script
->tool
.ksymbol
= process_bpf_events
;
2885 script
->tool
.text_poke
= process_text_poke_events
;
2888 if (perf_script__setup_per_event_dump(script
)) {
2889 pr_err("Couldn't create the per event dump files\n");
2893 ret
= perf_session__process_events(script
->session
);
2895 if (script
->per_event_dump
)
2896 perf_script__exit_per_event_dump_stats(script
);
2899 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
2904 static int list_available_languages_cb(struct scripting_ops
*ops
, const char *spec
)
2906 fprintf(stderr
, " %-42s [%s]\n", spec
, ops
->name
);
2910 static void list_available_languages(void)
2912 fprintf(stderr
, "\n");
2913 fprintf(stderr
, "Scripting language extensions (used in "
2914 "perf script -s [spec:]script.[spec]):\n\n");
2915 script_spec__for_each(&list_available_languages_cb
);
2916 fprintf(stderr
, "\n");
2919 /* Find script file relative to current directory or exec path */
2920 static char *find_script(const char *script
)
2922 char path
[PATH_MAX
];
2924 if (!scripting_ops
) {
2925 const char *ext
= strrchr(script
, '.');
2930 scripting_ops
= script_spec__lookup(++ext
);
2935 if (access(script
, R_OK
)) {
2936 char *exec_path
= get_argv_exec_path();
2940 snprintf(path
, sizeof(path
), "%s/scripts/%s/%s",
2941 exec_path
, scripting_ops
->dirname
, script
);
2944 if (access(script
, R_OK
))
2947 return strdup(script
);
2950 static int parse_scriptname(const struct option
*opt __maybe_unused
,
2951 const char *str
, int unset __maybe_unused
)
2953 char spec
[PATH_MAX
];
2954 const char *script
, *ext
;
2957 if (strcmp(str
, "lang") == 0) {
2958 list_available_languages();
2962 script
= strchr(str
, ':');
2965 if (len
>= PATH_MAX
) {
2966 fprintf(stderr
, "invalid language specifier");
2969 strncpy(spec
, str
, len
);
2971 scripting_ops
= script_spec__lookup(spec
);
2972 if (!scripting_ops
) {
2973 fprintf(stderr
, "invalid language specifier");
2979 ext
= strrchr(script
, '.');
2981 fprintf(stderr
, "invalid script extension");
2984 scripting_ops
= script_spec__lookup(++ext
);
2985 if (!scripting_ops
) {
2986 fprintf(stderr
, "invalid script extension");
2991 script_name
= find_script(script
);
2993 script_name
= strdup(script
);
2998 static int parse_output_fields(const struct option
*opt __maybe_unused
,
2999 const char *arg
, int unset __maybe_unused
)
3001 char *tok
, *strtok_saveptr
= NULL
;
3002 int i
, imax
= ARRAY_SIZE(all_output_options
);
3005 char *str
= strdup(arg
);
3007 enum { DEFAULT
, SET
, ADD
, REMOVE
} change
= DEFAULT
;
3012 /* first word can state for which event type the user is specifying
3013 * the fields. If no type exists, the specified fields apply to all
3014 * event types found in the file minus the invalid fields for a type.
3016 tok
= strchr(str
, ':');
3020 if (!strcmp(str
, "hw"))
3021 type
= PERF_TYPE_HARDWARE
;
3022 else if (!strcmp(str
, "sw"))
3023 type
= PERF_TYPE_SOFTWARE
;
3024 else if (!strcmp(str
, "trace"))
3025 type
= PERF_TYPE_TRACEPOINT
;
3026 else if (!strcmp(str
, "raw"))
3027 type
= PERF_TYPE_RAW
;
3028 else if (!strcmp(str
, "break"))
3029 type
= PERF_TYPE_BREAKPOINT
;
3030 else if (!strcmp(str
, "synth"))
3031 type
= OUTPUT_TYPE_SYNTH
;
3033 fprintf(stderr
, "Invalid event type in field string.\n");
3038 if (output
[type
].user_set
)
3039 pr_warning("Overriding previous field request for %s events.\n",
3042 /* Don't override defaults for +- */
3043 if (strchr(tok
, '+') || strchr(tok
, '-'))
3046 output
[type
].fields
= 0;
3047 output
[type
].user_set
= true;
3048 output
[type
].wildcard_set
= false;
3052 if (strlen(str
) == 0) {
3054 "Cannot set fields to 'none' for all event types.\n");
3059 /* Don't override defaults for +- */
3060 if (strchr(str
, '+') || strchr(str
, '-'))
3063 if (output_set_by_user())
3064 pr_warning("Overriding previous field request for all events.\n");
3066 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
3067 output
[j
].fields
= 0;
3068 output
[j
].user_set
= true;
3069 output
[j
].wildcard_set
= true;
3074 for (tok
= strtok_r(tok
, ",", &strtok_saveptr
); tok
; tok
= strtok_r(NULL
, ",", &strtok_saveptr
)) {
3080 } else if (*tok
== '-') {
3086 if (change
!= SET
&& change
!= DEFAULT
)
3091 for (i
= 0; i
< imax
; ++i
) {
3092 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
3095 if (i
== imax
&& strcmp(tok
, "flags") == 0) {
3096 print_flags
= change
!= REMOVE
;
3100 fprintf(stderr
, "Invalid field requested.\n");
3104 #ifndef HAVE_LIBCAPSTONE_SUPPORT
3105 if (change
!= REMOVE
&& strcmp(tok
, "disasm") == 0) {
3106 fprintf(stderr
, "Field \"disasm\" requires perf to be built with libcapstone support.\n");
3113 /* add user option to all events types for
3116 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
3117 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
3118 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
3119 all_output_options
[i
].str
, event_type(j
));
3121 if (change
== REMOVE
) {
3122 output
[j
].fields
&= ~all_output_options
[i
].field
;
3123 output
[j
].user_set_fields
&= ~all_output_options
[i
].field
;
3124 output
[j
].user_unset_fields
|= all_output_options
[i
].field
;
3126 output
[j
].fields
|= all_output_options
[i
].field
;
3127 output
[j
].user_set_fields
|= all_output_options
[i
].field
;
3128 output
[j
].user_unset_fields
&= ~all_output_options
[i
].field
;
3130 output
[j
].user_set
= true;
3131 output
[j
].wildcard_set
= true;
3135 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
3136 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
3137 all_output_options
[i
].str
, event_type(type
));
3142 if (change
== REMOVE
)
3143 output
[type
].fields
&= ~all_output_options
[i
].field
;
3145 output
[type
].fields
|= all_output_options
[i
].field
;
3146 output
[type
].user_set
= true;
3147 output
[type
].wildcard_set
= true;
3152 if (output
[type
].fields
== 0) {
3153 pr_debug("No fields requested for %s type. "
3154 "Events will not be displayed.\n", event_type(type
));
3160 fprintf(stderr
, "Cannot mix +-field with overridden fields\n");
3167 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
3168 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
3169 if ((lang_dirent->d_type == DT_DIR || \
3170 (lang_dirent->d_type == DT_UNKNOWN && \
3171 is_directory(scripts_path, lang_dirent))) && \
3172 (strcmp(lang_dirent->d_name, ".")) && \
3173 (strcmp(lang_dirent->d_name, "..")))
3175 #define for_each_script(lang_path, lang_dir, script_dirent) \
3176 while ((script_dirent = readdir(lang_dir)) != NULL) \
3177 if (script_dirent->d_type != DT_DIR && \
3178 (script_dirent->d_type != DT_UNKNOWN || \
3179 !is_directory(lang_path, script_dirent)))
3182 #define RECORD_SUFFIX "-record"
3183 #define REPORT_SUFFIX "-report"
3185 struct script_desc
{
3186 struct list_head node
;
3192 static LIST_HEAD(script_descs
);
3194 static struct script_desc
*script_desc__new(const char *name
)
3196 struct script_desc
*s
= zalloc(sizeof(*s
));
3198 if (s
!= NULL
&& name
)
3199 s
->name
= strdup(name
);
3204 static void script_desc__delete(struct script_desc
*s
)
3207 zfree(&s
->half_liner
);
3212 static void script_desc__add(struct script_desc
*s
)
3214 list_add_tail(&s
->node
, &script_descs
);
3217 static struct script_desc
*script_desc__find(const char *name
)
3219 struct script_desc
*s
;
3221 list_for_each_entry(s
, &script_descs
, node
)
3222 if (strcasecmp(s
->name
, name
) == 0)
3227 static struct script_desc
*script_desc__findnew(const char *name
)
3229 struct script_desc
*s
= script_desc__find(name
);
3234 s
= script_desc__new(name
);
3238 script_desc__add(s
);
3243 static const char *ends_with(const char *str
, const char *suffix
)
3245 size_t suffix_len
= strlen(suffix
);
3246 const char *p
= str
;
3248 if (strlen(str
) > suffix_len
) {
3249 p
= str
+ strlen(str
) - suffix_len
;
3250 if (!strncmp(p
, suffix
, suffix_len
))
3257 static int read_script_info(struct script_desc
*desc
, const char *filename
)
3259 char line
[BUFSIZ
], *p
;
3262 fp
= fopen(filename
, "r");
3266 while (fgets(line
, sizeof(line
), fp
)) {
3267 p
= skip_spaces(line
);
3273 if (strlen(p
) && *p
== '!')
3277 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
3278 p
[strlen(p
) - 1] = '\0';
3280 if (!strncmp(p
, "description:", strlen("description:"))) {
3281 p
+= strlen("description:");
3282 desc
->half_liner
= strdup(skip_spaces(p
));
3286 if (!strncmp(p
, "args:", strlen("args:"))) {
3287 p
+= strlen("args:");
3288 desc
->args
= strdup(skip_spaces(p
));
3298 static char *get_script_root(struct dirent
*script_dirent
, const char *suffix
)
3300 char *script_root
, *str
;
3302 script_root
= strdup(script_dirent
->d_name
);
3306 str
= (char *)ends_with(script_root
, suffix
);
3316 static int list_available_scripts(const struct option
*opt __maybe_unused
,
3317 const char *s __maybe_unused
,
3318 int unset __maybe_unused
)
3320 struct dirent
*script_dirent
, *lang_dirent
;
3321 char *buf
, *scripts_path
, *script_path
, *lang_path
, *first_half
;
3322 DIR *scripts_dir
, *lang_dir
;
3323 struct script_desc
*desc
;
3326 buf
= malloc(3 * MAXPATHLEN
+ BUFSIZ
);
3328 pr_err("malloc failed\n");
3332 script_path
= buf
+ MAXPATHLEN
;
3333 lang_path
= buf
+ 2 * MAXPATHLEN
;
3334 first_half
= buf
+ 3 * MAXPATHLEN
;
3336 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
3338 scripts_dir
= opendir(scripts_path
);
3341 "open(%s) failed.\n"
3342 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
3348 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
3349 scnprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
3350 lang_dirent
->d_name
);
3351 lang_dir
= opendir(lang_path
);
3355 for_each_script(lang_path
, lang_dir
, script_dirent
) {
3356 script_root
= get_script_root(script_dirent
, REPORT_SUFFIX
);
3358 desc
= script_desc__findnew(script_root
);
3359 scnprintf(script_path
, MAXPATHLEN
, "%s/%s",
3360 lang_path
, script_dirent
->d_name
);
3361 read_script_info(desc
, script_path
);
3367 fprintf(stdout
, "List of available trace scripts:\n");
3368 list_for_each_entry(desc
, &script_descs
, node
) {
3369 sprintf(first_half
, "%s %s", desc
->name
,
3370 desc
->args
? desc
->args
: "");
3371 fprintf(stdout
, " %-36s %s\n", first_half
,
3372 desc
->half_liner
? desc
->half_liner
: "");
3379 static int add_dlarg(const struct option
*opt __maybe_unused
,
3380 const char *s
, int unset __maybe_unused
)
3382 char *arg
= strdup(s
);
3388 a
= realloc(dlargv
, sizeof(dlargv
[0]) * (dlargc
+ 1));
3395 dlargv
[dlargc
++] = arg
;
3400 static void free_dlarg(void)
3403 free(dlargv
[dlargc
]);
3407 static char *get_script_path(const char *script_root
, const char *suffix
)
3409 struct dirent
*script_dirent
, *lang_dirent
;
3410 char scripts_path
[MAXPATHLEN
];
3411 char script_path
[MAXPATHLEN
];
3412 DIR *scripts_dir
, *lang_dir
;
3413 char lang_path
[MAXPATHLEN
];
3414 char *__script_root
;
3416 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
3418 scripts_dir
= opendir(scripts_path
);
3422 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
3423 scnprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
3424 lang_dirent
->d_name
);
3425 lang_dir
= opendir(lang_path
);
3429 for_each_script(lang_path
, lang_dir
, script_dirent
) {
3430 __script_root
= get_script_root(script_dirent
, suffix
);
3431 if (__script_root
&& !strcmp(script_root
, __script_root
)) {
3432 free(__script_root
);
3433 closedir(scripts_dir
);
3434 scnprintf(script_path
, MAXPATHLEN
, "%s/%s",
3435 lang_path
, script_dirent
->d_name
);
3437 return strdup(script_path
);
3439 free(__script_root
);
3443 closedir(scripts_dir
);
3448 static bool is_top_script(const char *script_path
)
3450 return ends_with(script_path
, "top") != NULL
;
3453 static int has_required_arg(char *script_path
)
3455 struct script_desc
*desc
;
3459 desc
= script_desc__new(NULL
);
3461 if (read_script_info(desc
, script_path
))
3467 for (p
= desc
->args
; *p
; p
++)
3471 script_desc__delete(desc
);
3476 static int have_cmd(int argc
, const char **argv
)
3478 char **__argv
= malloc(sizeof(const char *) * argc
);
3481 pr_err("malloc failed\n");
3485 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
3486 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
3487 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
3490 system_wide
= (argc
== 0);
3495 static void script__setup_sample_type(struct perf_script
*script
)
3497 struct perf_session
*session
= script
->session
;
3498 u64 sample_type
= evlist__combined_sample_type(session
->evlist
);
3500 callchain_param_setup(sample_type
, perf_env__arch(session
->machines
.host
.env
));
3502 if (script
->stitch_lbr
&& (callchain_param
.record_mode
!= CALLCHAIN_LBR
)) {
3503 pr_warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
3504 "Please apply --call-graph lbr when recording.\n");
3505 script
->stitch_lbr
= false;
3509 static int process_stat_round_event(struct perf_session
*session
,
3510 union perf_event
*event
)
3512 struct perf_record_stat_round
*round
= &event
->stat_round
;
3513 struct evsel
*counter
;
3515 evlist__for_each_entry(session
->evlist
, counter
) {
3516 perf_stat_process_counter(&stat_config
, counter
);
3517 process_stat(counter
, round
->time
);
3520 process_stat_interval(round
->time
);
3524 static int process_stat_config_event(struct perf_session
*session __maybe_unused
,
3525 union perf_event
*event
)
3527 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
3530 * Aggregation modes are not used since post-processing scripts are
3531 * supposed to take care of such requirements
3533 stat_config
.aggr_mode
= AGGR_NONE
;
3538 static int set_maps(struct perf_script
*script
)
3540 struct evlist
*evlist
= script
->session
->evlist
;
3542 if (!script
->cpus
|| !script
->threads
)
3545 if (WARN_ONCE(script
->allocated
, "stats double allocation\n"))
3548 perf_evlist__set_maps(&evlist
->core
, script
->cpus
, script
->threads
);
3550 if (evlist__alloc_stats(&stat_config
, evlist
, /*alloc_raw=*/true))
3553 script
->allocated
= true;
3558 int process_thread_map_event(struct perf_session
*session
,
3559 union perf_event
*event
)
3561 const struct perf_tool
*tool
= session
->tool
;
3562 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
3565 perf_event__fprintf_thread_map(event
, stdout
);
3567 if (script
->threads
) {
3568 pr_warning("Extra thread map event, ignoring.\n");
3572 script
->threads
= thread_map__new_event(&event
->thread_map
);
3573 if (!script
->threads
)
3576 return set_maps(script
);
3580 int process_cpu_map_event(struct perf_session
*session
,
3581 union perf_event
*event
)
3583 const struct perf_tool
*tool
= session
->tool
;
3584 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
3587 perf_event__fprintf_cpu_map(event
, stdout
);
3590 pr_warning("Extra cpu map event, ignoring.\n");
3594 script
->cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
3598 return set_maps(script
);
3601 static int process_feature_event(struct perf_session
*session
,
3602 union perf_event
*event
)
3604 if (event
->feat
.feat_id
< HEADER_LAST_FEATURE
)
3605 return perf_event__process_feature(session
, event
);
3609 #ifdef HAVE_AUXTRACE_SUPPORT
3610 static int perf_script__process_auxtrace_info(struct perf_session
*session
,
3611 union perf_event
*event
)
3613 int ret
= perf_event__process_auxtrace_info(session
, event
);
3616 const struct perf_tool
*tool
= session
->tool
;
3617 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
3619 ret
= perf_script__setup_per_event_dump(script
);
3625 #define perf_script__process_auxtrace_info 0
3628 static int parse_insn_trace(const struct option
*opt __maybe_unused
,
3629 const char *str
, int unset __maybe_unused
)
3631 const char *fields
= "+insn,-event,-period";
3635 if (strcmp(str
, "disasm") == 0)
3636 fields
= "+disasm,-event,-period";
3637 else if (strlen(str
) != 0 && strcmp(str
, "raw") != 0) {
3638 fprintf(stderr
, "Only accept raw|disasm\n");
3643 ret
= parse_output_fields(NULL
, fields
, 0);
3647 itrace_parse_synth_opts(opt
, "i0nse", 0);
3648 symbol_conf
.nanosecs
= true;
3652 static int parse_xed(const struct option
*opt __maybe_unused
,
3653 const char *str __maybe_unused
,
3654 int unset __maybe_unused
)
3657 force_pager("xed -F insn: -A -64 | less");
3659 force_pager("xed -F insn: -A -64");
3663 static int parse_call_trace(const struct option
*opt __maybe_unused
,
3664 const char *str __maybe_unused
,
3665 int unset __maybe_unused
)
3667 parse_output_fields(NULL
, "-ip,-addr,-event,-period,+callindent", 0);
3668 itrace_parse_synth_opts(opt
, "cewp", 0);
3669 symbol_conf
.nanosecs
= true;
3670 symbol_conf
.pad_output_len_dso
= 50;
3674 static int parse_callret_trace(const struct option
*opt __maybe_unused
,
3675 const char *str __maybe_unused
,
3676 int unset __maybe_unused
)
3678 parse_output_fields(NULL
, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3679 itrace_parse_synth_opts(opt
, "crewp", 0);
3680 symbol_conf
.nanosecs
= true;
3684 int cmd_script(int argc
, const char **argv
)
3686 bool show_full_info
= false;
3687 bool header
= false;
3688 bool header_only
= false;
3689 bool script_started
= false;
3690 bool unsorted_dump
= false;
3691 char *rec_script_path
= NULL
;
3692 char *rep_script_path
= NULL
;
3693 struct perf_session
*session
;
3694 struct itrace_synth_opts itrace_synth_opts
= {
3696 .default_no_sample
= true,
3699 char *script_path
= NULL
;
3700 const char *dlfilter_file
= NULL
;
3701 const char **__argv
;
3703 struct perf_script script
= {};
3704 struct perf_data data
= {
3705 .mode
= PERF_DATA_MODE_READ
,
3707 const struct option options
[] = {
3708 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
3709 "dump raw trace in ASCII"),
3710 OPT_BOOLEAN(0, "dump-unsorted-raw-trace", &unsorted_dump
,
3711 "dump unsorted raw trace in ASCII"),
3712 OPT_INCR('v', "verbose", &verbose
,
3713 "be more verbose (show symbol address, etc)"),
3714 OPT_BOOLEAN('L', "Latency", &latency_format
,
3715 "show latency attributes (irqs/preemption disabled, etc)"),
3716 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
3717 list_available_scripts
),
3718 OPT_CALLBACK_NOOPT(0, "list-dlfilters", NULL
, NULL
, "list available dlfilters",
3719 list_available_dlfilters
),
3720 OPT_CALLBACK('s', "script", NULL
, "name",
3721 "script file name (lang:script name, script name, or *)",
3723 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
3724 "generate perf-script.xx script in specified language"),
3725 OPT_STRING(0, "dlfilter", &dlfilter_file
, "file", "filter .so file name"),
3726 OPT_CALLBACK(0, "dlarg", NULL
, "argument", "filter argument",
3728 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
3729 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
3730 "do various checks like samples ordering and lost events"),
3731 OPT_BOOLEAN(0, "header", &header
, "Show data header."),
3732 OPT_BOOLEAN(0, "header-only", &header_only
, "Show only data header."),
3733 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
3734 "file", "vmlinux pathname"),
3735 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
3736 "file", "kallsyms pathname"),
3737 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
3738 "When printing symbols do not display call chain"),
3739 OPT_CALLBACK(0, "symfs", NULL
, "directory",
3740 "Look for files with symbols relative to this directory",
3741 symbol__config_symfs
),
3742 OPT_CALLBACK('F', "fields", NULL
, "str",
3743 "comma separated output fields prepend with 'type:'. "
3744 "+field to add and -field to remove."
3745 "Valid types: hw,sw,trace,raw,synth. "
3746 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,dsoff,"
3747 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3748 "brstacksym,flags,data_src,weight,bpf-output,brstackinsn,"
3749 "brstackinsnlen,brstackdisasm,brstackoff,callindent,insn,disasm,insnlen,synth,"
3750 "phys_addr,metric,misc,srccode,ipc,tod,data_page_size,"
3751 "code_page_size,ins_lat,machine_pid,vcpu,cgroup,retire_lat,"
3753 parse_output_fields
),
3754 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
3755 "system-wide collection from all CPUs"),
3756 OPT_STRING(0, "dsos", &symbol_conf
.dso_list_str
, "dso[,dso...]",
3757 "only consider symbols in these DSOs"),
3758 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
3759 "only consider these symbols"),
3760 OPT_INTEGER(0, "addr-range", &symbol_conf
.addr_range
,
3761 "Use with -S to list traced records within address range"),
3762 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts
, NULL
, "raw|disasm",
3763 "Decode instructions from itrace", parse_insn_trace
),
3764 OPT_CALLBACK_OPTARG(0, "xed", NULL
, NULL
, NULL
,
3765 "Run xed disassembler on output", parse_xed
),
3766 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts
, NULL
, NULL
,
3767 "Decode calls from itrace", parse_call_trace
),
3768 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts
, NULL
, NULL
,
3769 "Decode calls and returns from itrace", parse_callret_trace
),
3770 OPT_STRING(0, "graph-function", &symbol_conf
.graph_function
, "symbol[,symbol...]",
3771 "Only print symbols and callees with --call-trace/--call-ret-trace"),
3772 OPT_STRING(0, "stop-bt", &symbol_conf
.bt_stop_list_str
, "symbol[,symbol...]",
3773 "Stop display of callgraph at these symbols"),
3774 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
3775 OPT_STRING('c', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
3776 "only display events for these comms"),
3777 OPT_STRING(0, "pid", &symbol_conf
.pid_list_str
, "pid[,pid...]",
3778 "only consider symbols in these pids"),
3779 OPT_STRING(0, "tid", &symbol_conf
.tid_list_str
, "tid[,tid...]",
3780 "only consider symbols in these tids"),
3781 OPT_UINTEGER(0, "max-stack", &scripting_max_stack
,
3782 "Set the maximum stack depth when parsing the callchain, "
3783 "anything beyond the specified depth will be ignored. "
3784 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH
)),
3785 OPT_BOOLEAN(0, "reltime", &reltime
, "Show time stamps relative to start"),
3786 OPT_BOOLEAN(0, "deltatime", &deltatime
, "Show time stamps relative to previous event"),
3787 OPT_BOOLEAN('I', "show-info", &show_full_info
,
3788 "display extended information from perf.data file"),
3789 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf
.show_kernel_path
,
3790 "Show the path of [kernel.kallsyms]"),
3791 OPT_BOOLEAN('\0', "show-task-events", &script
.show_task_events
,
3792 "Show the fork/comm/exit events"),
3793 OPT_BOOLEAN('\0', "show-mmap-events", &script
.show_mmap_events
,
3794 "Show the mmap events"),
3795 OPT_BOOLEAN('\0', "show-switch-events", &script
.show_switch_events
,
3796 "Show context switch events (if recorded)"),
3797 OPT_BOOLEAN('\0', "show-namespace-events", &script
.show_namespace_events
,
3798 "Show namespace events (if recorded)"),
3799 OPT_BOOLEAN('\0', "show-cgroup-events", &script
.show_cgroup_events
,
3800 "Show cgroup events (if recorded)"),
3801 OPT_BOOLEAN('\0', "show-lost-events", &script
.show_lost_events
,
3802 "Show lost events (if recorded)"),
3803 OPT_BOOLEAN('\0', "show-round-events", &script
.show_round_events
,
3804 "Show round events (if recorded)"),
3805 OPT_BOOLEAN('\0', "show-bpf-events", &script
.show_bpf_events
,
3806 "Show bpf related events (if recorded)"),
3807 OPT_BOOLEAN('\0', "show-text-poke-events", &script
.show_text_poke_events
,
3808 "Show text poke related events (if recorded)"),
3809 OPT_BOOLEAN('\0', "per-event-dump", &script
.per_event_dump
,
3810 "Dump trace output to files named by the monitored events"),
3811 OPT_BOOLEAN('f', "force", &symbol_conf
.force
, "don't complain, do it"),
3812 OPT_INTEGER(0, "max-blocks", &max_blocks
,
3813 "Maximum number of code blocks to dump with brstackinsn"),
3814 OPT_BOOLEAN(0, "ns", &symbol_conf
.nanosecs
,
3815 "Use 9 decimal places when displaying time"),
3816 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts
, NULL
, "opts",
3817 "Instruction Tracing options\n" ITRACE_HELP
,
3818 itrace_parse_synth_opts
),
3819 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename
,
3820 "Show full source file name path for source lines"),
3821 OPT_BOOLEAN(0, "demangle", &symbol_conf
.demangle
,
3822 "Enable symbol demangling"),
3823 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf
.demangle_kernel
,
3824 "Enable kernel symbol demangling"),
3825 OPT_STRING(0, "addr2line", &symbol_conf
.addr2line_path
, "path",
3826 "addr2line binary to use for line numbers"),
3827 OPT_STRING(0, "time", &script
.time_str
, "str",
3828 "Time span of interest (start,stop)"),
3829 OPT_BOOLEAN(0, "inline", &symbol_conf
.inline_name
,
3830 "Show inline function"),
3831 OPT_STRING(0, "guestmount", &symbol_conf
.guestmount
, "directory",
3832 "guest mount directory under which every guest os"
3833 " instance has a subdir"),
3834 OPT_STRING(0, "guestvmlinux", &symbol_conf
.default_guest_vmlinux_name
,
3835 "file", "file saving guest os vmlinux"),
3836 OPT_STRING(0, "guestkallsyms", &symbol_conf
.default_guest_kallsyms
,
3837 "file", "file saving guest os /proc/kallsyms"),
3838 OPT_STRING(0, "guestmodules", &symbol_conf
.default_guest_modules
,
3839 "file", "file saving guest os /proc/modules"),
3840 OPT_BOOLEAN(0, "guest-code", &symbol_conf
.guest_code
,
3841 "Guest code can be found in hypervisor process"),
3842 OPT_BOOLEAN('\0', "stitch-lbr", &script
.stitch_lbr
,
3843 "Enable LBR callgraph stitching approach"),
3844 OPTS_EVSWITCH(&script
.evswitch
),
3847 const char * const script_subcommands
[] = { "record", "report", NULL
};
3848 const char *script_usage
[] = {
3849 "perf script [<options>]",
3850 "perf script [<options>] record <script> [<record-options>] <command>",
3851 "perf script [<options>] report <script> [script-args]",
3852 "perf script [<options>] <script> [<record-options>] <command>",
3853 "perf script [<options>] <top-script> [script-args]",
3857 perf_set_singlethreaded();
3861 argc
= parse_options_subcommand(argc
, argv
, options
, script_subcommands
, script_usage
,
3862 PARSE_OPT_STOP_AT_NON_OPTION
);
3864 if (symbol_conf
.guestmount
||
3865 symbol_conf
.default_guest_vmlinux_name
||
3866 symbol_conf
.default_guest_kallsyms
||
3867 symbol_conf
.default_guest_modules
||
3868 symbol_conf
.guest_code
) {
3870 * Enable guest sample processing.
3875 data
.path
= input_name
;
3876 data
.force
= symbol_conf
.force
;
3881 if (symbol__validate_sym_arguments())
3884 if (argc
> 1 && strlen(argv
[0]) > 2 && strstarts("record", argv
[0])) {
3885 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
3886 if (!rec_script_path
)
3887 return cmd_record(argc
, argv
);
3890 if (argc
> 1 && strlen(argv
[0]) > 2 && strstarts("report", argv
[0])) {
3891 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
3892 if (!rep_script_path
) {
3894 "Please specify a valid report script"
3895 "(see 'perf script -l' for listing)\n");
3900 if (reltime
&& deltatime
) {
3902 "reltime and deltatime - the two don't get along well. "
3903 "Please limit to --reltime or --deltatime.\n");
3907 if ((itrace_synth_opts
.callchain
|| itrace_synth_opts
.add_callchain
) &&
3908 itrace_synth_opts
.callchain_sz
> scripting_max_stack
)
3909 scripting_max_stack
= itrace_synth_opts
.callchain_sz
;
3911 /* make sure PERF_EXEC_PATH is set for scripts */
3912 set_argv_exec_path(get_argv_exec_path());
3914 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
3919 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
3920 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
3922 if (!rec_script_path
&& !rep_script_path
) {
3923 script_name
= find_script(argv
[0]);
3929 usage_with_options_msg(script_usage
, options
,
3930 "Couldn't find script `%s'\n\n See perf"
3931 " script -l for available scripts.\n", argv
[0]);
3934 if (is_top_script(argv
[0])) {
3935 rep_args
= argc
- 1;
3939 rep_args
= has_required_arg(rep_script_path
);
3940 rec_args
= (argc
- 1) - rep_args
;
3942 usage_with_options_msg(script_usage
, options
,
3943 "`%s' script requires options."
3944 "\n\n See perf script -l for available "
3945 "scripts and options.\n", argv
[0]);
3949 if (pipe(live_pipe
) < 0) {
3950 perror("failed to create pipe");
3956 perror("failed to fork");
3963 dup2(live_pipe
[1], 1);
3964 close(live_pipe
[0]);
3966 if (is_top_script(argv
[0])) {
3968 } else if (!system_wide
) {
3969 if (have_cmd(argc
- rep_args
, &argv
[rep_args
]) != 0) {
3975 __argv
= malloc((argc
+ 6) * sizeof(const char *));
3977 pr_err("malloc failed\n");
3982 __argv
[j
++] = "/bin/sh";
3983 __argv
[j
++] = rec_script_path
;
3989 for (i
= rep_args
+ 1; i
< argc
; i
++)
3990 __argv
[j
++] = argv
[i
];
3993 execvp("/bin/sh", (char **)__argv
);
3998 dup2(live_pipe
[0], 0);
3999 close(live_pipe
[1]);
4001 __argv
= malloc((argc
+ 4) * sizeof(const char *));
4003 pr_err("malloc failed\n");
4009 __argv
[j
++] = "/bin/sh";
4010 __argv
[j
++] = rep_script_path
;
4011 for (i
= 1; i
< rep_args
+ 1; i
++)
4012 __argv
[j
++] = argv
[i
];
4017 execvp("/bin/sh", (char **)__argv
);
4022 if (rec_script_path
)
4023 script_path
= rec_script_path
;
4024 if (rep_script_path
)
4025 script_path
= rep_script_path
;
4030 if (!rec_script_path
)
4031 system_wide
= false;
4032 else if (!system_wide
) {
4033 if (have_cmd(argc
- 1, &argv
[1]) != 0) {
4039 __argv
= malloc((argc
+ 2) * sizeof(const char *));
4041 pr_err("malloc failed\n");
4046 __argv
[j
++] = "/bin/sh";
4047 __argv
[j
++] = script_path
;
4050 for (i
= 2; i
< argc
; i
++)
4051 __argv
[j
++] = argv
[i
];
4054 execvp("/bin/sh", (char **)__argv
);
4059 if (dlfilter_file
) {
4060 dlfilter
= dlfilter__new(dlfilter_file
, dlargc
, dlargv
);
4070 perf_tool__init(&script
.tool
, !unsorted_dump
);
4071 script
.tool
.sample
= process_sample_event
;
4072 script
.tool
.mmap
= perf_event__process_mmap
;
4073 script
.tool
.mmap2
= perf_event__process_mmap2
;
4074 script
.tool
.comm
= perf_event__process_comm
;
4075 script
.tool
.namespaces
= perf_event__process_namespaces
;
4076 script
.tool
.cgroup
= perf_event__process_cgroup
;
4077 script
.tool
.exit
= perf_event__process_exit
;
4078 script
.tool
.fork
= perf_event__process_fork
;
4079 script
.tool
.attr
= process_attr
;
4080 script
.tool
.event_update
= perf_event__process_event_update
;
4081 #ifdef HAVE_LIBTRACEEVENT
4082 script
.tool
.tracing_data
= perf_event__process_tracing_data
;
4084 script
.tool
.feature
= process_feature_event
;
4085 script
.tool
.build_id
= perf_event__process_build_id
;
4086 script
.tool
.id_index
= perf_event__process_id_index
;
4087 script
.tool
.auxtrace_info
= perf_script__process_auxtrace_info
;
4088 script
.tool
.auxtrace
= perf_event__process_auxtrace
;
4089 script
.tool
.auxtrace_error
= perf_event__process_auxtrace_error
;
4090 script
.tool
.stat
= perf_event__process_stat_event
;
4091 script
.tool
.stat_round
= process_stat_round_event
;
4092 script
.tool
.stat_config
= process_stat_config_event
;
4093 script
.tool
.thread_map
= process_thread_map_event
;
4094 script
.tool
.cpu_map
= process_cpu_map_event
;
4095 script
.tool
.throttle
= process_throttle_event
;
4096 script
.tool
.unthrottle
= process_throttle_event
;
4097 script
.tool
.ordering_requires_timestamps
= true;
4098 session
= perf_session__new(&data
, &script
.tool
);
4099 if (IS_ERR(session
))
4100 return PTR_ERR(session
);
4102 if (header
|| header_only
) {
4103 script
.tool
.show_feat_hdr
= SHOW_FEAT_HEADER
;
4104 perf_session__fprintf_info(session
, stdout
, show_full_info
);
4109 script
.tool
.show_feat_hdr
= SHOW_FEAT_HEADER_FULL_INFO
;
4111 if (symbol__init(&session
->header
.env
) < 0)
4115 if (data
.is_pipe
) { /* Assume pipe_mode indicates native_arch */
4117 } else if (session
->header
.env
.arch
) {
4118 if (!strcmp(uts
.machine
, session
->header
.env
.arch
))
4120 else if (!strcmp(uts
.machine
, "x86_64") &&
4121 !strcmp(session
->header
.env
.arch
, "i386"))
4125 script
.session
= session
;
4126 script__setup_sample_type(&script
);
4128 if ((output
[PERF_TYPE_HARDWARE
].fields
& PERF_OUTPUT_CALLINDENT
) ||
4129 symbol_conf
.graph_function
)
4130 itrace_synth_opts
.thread_stack
= true;
4132 session
->itrace_synth_opts
= &itrace_synth_opts
;
4135 err
= perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
);
4138 itrace_synth_opts
.cpu_bitmap
= cpu_bitmap
;
4142 symbol_conf
.use_callchain
= true;
4144 symbol_conf
.use_callchain
= false;
4146 #ifdef HAVE_LIBTRACEEVENT
4147 if (session
->tevent
.pevent
&&
4148 tep_set_function_resolver(session
->tevent
.pevent
,
4149 machine__resolve_kernel_addr
,
4150 &session
->machines
.host
) < 0) {
4151 pr_err("%s: failed to set libtraceevent function resolver\n", __func__
);
4156 if (generate_script_lang
) {
4157 struct stat perf_stat
;
4160 if (output_set_by_user()) {
4162 "custom fields not supported for generated scripts");
4167 input
= open(data
.path
, O_RDONLY
); /* input_name */
4170 perror("failed to open file");
4174 err
= fstat(input
, &perf_stat
);
4176 perror("failed to stat file");
4180 if (!perf_stat
.st_size
) {
4181 fprintf(stderr
, "zero-sized file, nothing to do!\n");
4185 scripting_ops
= script_spec__lookup(generate_script_lang
);
4186 if (!scripting_ops
) {
4187 fprintf(stderr
, "invalid language specifier");
4191 #ifdef HAVE_LIBTRACEEVENT
4192 err
= scripting_ops
->generate_script(session
->tevent
.pevent
,
4195 err
= scripting_ops
->generate_script(NULL
, "perf-script");
4200 err
= dlfilter__start(dlfilter
, session
);
4205 err
= scripting_ops
->start_script(script_name
, argc
, argv
, session
);
4208 pr_debug("perf script started with script %s\n\n", script_name
);
4209 script_started
= true;
4213 err
= perf_session__check_output_opt(session
);
4217 if (script
.time_str
) {
4218 err
= perf_time__parse_for_ranges_reltime(script
.time_str
, session
,
4219 &script
.ptime_range
,
4226 itrace_synth_opts__set_time_range(&itrace_synth_opts
,
4231 err
= evswitch__init(&script
.evswitch
, session
->evlist
, stderr
);
4235 if (zstd_init(&(session
->zstd_data
), 0) < 0)
4236 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
4238 err
= __cmd_script(&script
);
4242 if (verbose
> 2 || debug_kmaps
)
4243 perf_session__dump_kmaps(session
);
4246 if (script
.ptime_range
) {
4247 itrace_synth_opts__clear_time_range(&itrace_synth_opts
);
4248 zfree(&script
.ptime_range
);
4251 zstd_fini(&(session
->zstd_data
));
4252 evlist__free_stats(session
->evlist
);
4253 perf_session__delete(session
);
4254 perf_script__exit(&script
);
4257 cleanup_scripting();
4258 dlfilter__cleanup(dlfilter
);