4 #include "util/cache.h"
5 #include "util/debug.h"
6 #include <subcmd/exec-cmd.h>
7 #include "util/header.h"
8 #include <subcmd/parse-options.h>
9 #include "util/perf_regs.h"
10 #include "util/session.h"
11 #include "util/tool.h"
12 #include "util/symbol.h"
13 #include "util/thread.h"
14 #include "util/trace-event.h"
15 #include "util/util.h"
16 #include "util/evlist.h"
17 #include "util/evsel.h"
18 #include "util/sort.h"
19 #include "util/data.h"
20 #include "util/auxtrace.h"
21 #include "util/cpumap.h"
22 #include "util/thread_map.h"
23 #include "util/stat.h"
24 #include "util/string2.h"
25 #include "util/thread-stack.h"
26 #include "util/time-utils.h"
27 #include "print_binary.h"
28 #include <linux/bitmap.h>
29 #include <linux/kernel.h>
30 #include <linux/stringify.h>
31 #include <linux/time64.h>
33 #include "util/mem-events.h"
34 #include "util/dump-insn.h"
39 #include <sys/param.h>
40 #include <sys/types.h>
44 #include "sane_ctype.h"
46 static char const *script_name
;
47 static char const *generate_script_lang
;
48 static bool debug_mode
;
49 static u64 last_timestamp
;
50 static u64 nr_unordered
;
51 static bool no_callchain
;
52 static bool latency_format
;
53 static bool system_wide
;
54 static bool print_flags
;
56 static const char *cpu_list
;
57 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
58 static struct perf_stat_config stat_config
;
59 static int max_blocks
;
61 unsigned int scripting_max_stack
= PERF_MAX_STACK_DEPTH
;
63 enum perf_output_field
{
64 PERF_OUTPUT_COMM
= 1U << 0,
65 PERF_OUTPUT_TID
= 1U << 1,
66 PERF_OUTPUT_PID
= 1U << 2,
67 PERF_OUTPUT_TIME
= 1U << 3,
68 PERF_OUTPUT_CPU
= 1U << 4,
69 PERF_OUTPUT_EVNAME
= 1U << 5,
70 PERF_OUTPUT_TRACE
= 1U << 6,
71 PERF_OUTPUT_IP
= 1U << 7,
72 PERF_OUTPUT_SYM
= 1U << 8,
73 PERF_OUTPUT_DSO
= 1U << 9,
74 PERF_OUTPUT_ADDR
= 1U << 10,
75 PERF_OUTPUT_SYMOFFSET
= 1U << 11,
76 PERF_OUTPUT_SRCLINE
= 1U << 12,
77 PERF_OUTPUT_PERIOD
= 1U << 13,
78 PERF_OUTPUT_IREGS
= 1U << 14,
79 PERF_OUTPUT_BRSTACK
= 1U << 15,
80 PERF_OUTPUT_BRSTACKSYM
= 1U << 16,
81 PERF_OUTPUT_DATA_SRC
= 1U << 17,
82 PERF_OUTPUT_WEIGHT
= 1U << 18,
83 PERF_OUTPUT_BPF_OUTPUT
= 1U << 19,
84 PERF_OUTPUT_CALLINDENT
= 1U << 20,
85 PERF_OUTPUT_INSN
= 1U << 21,
86 PERF_OUTPUT_INSNLEN
= 1U << 22,
87 PERF_OUTPUT_BRSTACKINSN
= 1U << 23,
88 PERF_OUTPUT_BRSTACKOFF
= 1U << 24,
89 PERF_OUTPUT_SYNTH
= 1U << 25,
90 PERF_OUTPUT_PHYS_ADDR
= 1U << 26,
93 struct output_option
{
95 enum perf_output_field field
;
96 } all_output_options
[] = {
97 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
98 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
99 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
100 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
101 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
102 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
103 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
104 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
105 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
106 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
107 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
108 {.str
= "symoff", .field
= PERF_OUTPUT_SYMOFFSET
},
109 {.str
= "srcline", .field
= PERF_OUTPUT_SRCLINE
},
110 {.str
= "period", .field
= PERF_OUTPUT_PERIOD
},
111 {.str
= "iregs", .field
= PERF_OUTPUT_IREGS
},
112 {.str
= "brstack", .field
= PERF_OUTPUT_BRSTACK
},
113 {.str
= "brstacksym", .field
= PERF_OUTPUT_BRSTACKSYM
},
114 {.str
= "data_src", .field
= PERF_OUTPUT_DATA_SRC
},
115 {.str
= "weight", .field
= PERF_OUTPUT_WEIGHT
},
116 {.str
= "bpf-output", .field
= PERF_OUTPUT_BPF_OUTPUT
},
117 {.str
= "callindent", .field
= PERF_OUTPUT_CALLINDENT
},
118 {.str
= "insn", .field
= PERF_OUTPUT_INSN
},
119 {.str
= "insnlen", .field
= PERF_OUTPUT_INSNLEN
},
120 {.str
= "brstackinsn", .field
= PERF_OUTPUT_BRSTACKINSN
},
121 {.str
= "brstackoff", .field
= PERF_OUTPUT_BRSTACKOFF
},
122 {.str
= "synth", .field
= PERF_OUTPUT_SYNTH
},
123 {.str
= "phys_addr", .field
= PERF_OUTPUT_PHYS_ADDR
},
127 OUTPUT_TYPE_SYNTH
= PERF_TYPE_MAX
,
131 /* default set to maintain compatibility with current format */
135 unsigned int print_ip_opts
;
138 } output
[OUTPUT_TYPE_MAX
] = {
140 [PERF_TYPE_HARDWARE
] = {
143 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
144 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
145 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
146 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
149 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
152 [PERF_TYPE_SOFTWARE
] = {
155 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
156 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
157 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
158 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
159 PERF_OUTPUT_PERIOD
| PERF_OUTPUT_BPF_OUTPUT
,
161 .invalid_fields
= PERF_OUTPUT_TRACE
,
164 [PERF_TYPE_TRACEPOINT
] = {
167 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
168 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
169 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
175 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
176 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
177 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
178 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
179 PERF_OUTPUT_PERIOD
| PERF_OUTPUT_ADDR
|
180 PERF_OUTPUT_DATA_SRC
| PERF_OUTPUT_WEIGHT
|
181 PERF_OUTPUT_PHYS_ADDR
,
183 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
186 [PERF_TYPE_BREAKPOINT
] = {
189 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
190 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
191 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
192 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
195 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
198 [OUTPUT_TYPE_SYNTH
] = {
201 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
202 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
203 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
204 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
207 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
211 static inline int output_type(unsigned int type
)
214 case PERF_TYPE_SYNTH
:
215 return OUTPUT_TYPE_SYNTH
;
221 static inline unsigned int attr_type(unsigned int type
)
224 case OUTPUT_TYPE_SYNTH
:
225 return PERF_TYPE_SYNTH
;
231 static bool output_set_by_user(void)
234 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
235 if (output
[j
].user_set
)
241 static const char *output_field2str(enum perf_output_field field
)
243 int i
, imax
= ARRAY_SIZE(all_output_options
);
244 const char *str
= "";
246 for (i
= 0; i
< imax
; ++i
) {
247 if (all_output_options
[i
].field
== field
) {
248 str
= all_output_options
[i
].str
;
255 #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
257 static int perf_evsel__do_check_stype(struct perf_evsel
*evsel
,
258 u64 sample_type
, const char *sample_msg
,
259 enum perf_output_field field
,
262 struct perf_event_attr
*attr
= &evsel
->attr
;
263 int type
= output_type(attr
->type
);
266 if (attr
->sample_type
& sample_type
)
269 if (output
[type
].user_set
) {
272 evname
= perf_evsel__name(evsel
);
273 pr_err("Samples for '%s' event do not have %s attribute set. "
274 "Cannot print '%s' field.\n",
275 evname
, sample_msg
, output_field2str(field
));
279 /* user did not ask for it explicitly so remove from the default list */
280 output
[type
].fields
&= ~field
;
281 evname
= perf_evsel__name(evsel
);
282 pr_debug("Samples for '%s' event do not have %s attribute set. "
283 "Skipping '%s' field.\n",
284 evname
, sample_msg
, output_field2str(field
));
289 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
290 u64 sample_type
, const char *sample_msg
,
291 enum perf_output_field field
)
293 return perf_evsel__do_check_stype(evsel
, sample_type
, sample_msg
, field
,
297 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
298 struct perf_session
*session
)
300 struct perf_event_attr
*attr
= &evsel
->attr
;
303 if (perf_header__has_feat(&session
->header
, HEADER_STAT
))
306 allow_user_set
= perf_header__has_feat(&session
->header
,
309 if (PRINT_FIELD(TRACE
) &&
310 !perf_session__has_traces(session
, "record -R"))
313 if (PRINT_FIELD(IP
)) {
314 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_IP
, "IP",
319 if (PRINT_FIELD(ADDR
) &&
320 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_ADDR
, "ADDR",
321 PERF_OUTPUT_ADDR
, allow_user_set
))
324 if (PRINT_FIELD(DATA_SRC
) &&
325 perf_evsel__check_stype(evsel
, PERF_SAMPLE_DATA_SRC
, "DATA_SRC",
326 PERF_OUTPUT_DATA_SRC
))
329 if (PRINT_FIELD(WEIGHT
) &&
330 perf_evsel__check_stype(evsel
, PERF_SAMPLE_WEIGHT
, "WEIGHT",
334 if (PRINT_FIELD(SYM
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
335 pr_err("Display of symbols requested but neither sample IP nor "
336 "sample address\nis selected. Hence, no addresses to convert "
340 if (PRINT_FIELD(SYMOFFSET
) && !PRINT_FIELD(SYM
)) {
341 pr_err("Display of offsets requested but symbol is not"
345 if (PRINT_FIELD(DSO
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
) &&
346 !PRINT_FIELD(BRSTACK
) && !PRINT_FIELD(BRSTACKSYM
) && !PRINT_FIELD(BRSTACKOFF
)) {
347 pr_err("Display of DSO requested but no address to convert. Select\n"
348 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
351 if (PRINT_FIELD(SRCLINE
) && !PRINT_FIELD(IP
)) {
352 pr_err("Display of source line number requested but sample IP is not\n"
353 "selected. Hence, no address to lookup the source line number.\n");
356 if (PRINT_FIELD(BRSTACKINSN
) &&
357 !(perf_evlist__combined_branch_type(session
->evlist
) &
358 PERF_SAMPLE_BRANCH_ANY
)) {
359 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
360 "Hint: run 'perf record -b ...'\n");
363 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
364 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID",
365 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
368 if (PRINT_FIELD(TIME
) &&
369 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TIME
, "TIME",
373 if (PRINT_FIELD(CPU
) &&
374 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_CPU
, "CPU",
375 PERF_OUTPUT_CPU
, allow_user_set
))
378 if (PRINT_FIELD(PERIOD
) &&
379 perf_evsel__check_stype(evsel
, PERF_SAMPLE_PERIOD
, "PERIOD",
383 if (PRINT_FIELD(IREGS
) &&
384 perf_evsel__check_stype(evsel
, PERF_SAMPLE_REGS_INTR
, "IREGS",
388 if (PRINT_FIELD(PHYS_ADDR
) &&
389 perf_evsel__check_stype(evsel
, PERF_SAMPLE_PHYS_ADDR
, "PHYS_ADDR",
390 PERF_OUTPUT_PHYS_ADDR
))
396 static void set_print_ip_opts(struct perf_event_attr
*attr
)
398 unsigned int type
= output_type(attr
->type
);
400 output
[type
].print_ip_opts
= 0;
402 output
[type
].print_ip_opts
|= EVSEL__PRINT_IP
;
404 if (PRINT_FIELD(SYM
))
405 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYM
;
407 if (PRINT_FIELD(DSO
))
408 output
[type
].print_ip_opts
|= EVSEL__PRINT_DSO
;
410 if (PRINT_FIELD(SYMOFFSET
))
411 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYMOFFSET
;
413 if (PRINT_FIELD(SRCLINE
))
414 output
[type
].print_ip_opts
|= EVSEL__PRINT_SRCLINE
;
418 * verify all user requested events exist and the samples
419 * have the expected data
421 static int perf_session__check_output_opt(struct perf_session
*session
)
424 struct perf_evsel
*evsel
;
426 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
427 evsel
= perf_session__find_first_evtype(session
, attr_type(j
));
430 * even if fields is set to 0 (ie., show nothing) event must
431 * exist if user explicitly includes it on the command line
433 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
&&
434 j
!= OUTPUT_TYPE_SYNTH
) {
435 pr_err("%s events do not exist. "
436 "Remove corresponding -F option to proceed.\n",
441 if (evsel
&& output
[j
].fields
&&
442 perf_evsel__check_attr(evsel
, session
))
448 set_print_ip_opts(&evsel
->attr
);
452 bool use_callchain
= false;
453 bool not_pipe
= false;
455 evlist__for_each_entry(session
->evlist
, evsel
) {
457 if (evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
458 use_callchain
= true;
462 if (not_pipe
&& !use_callchain
)
463 symbol_conf
.use_callchain
= false;
467 * set default for tracepoints to print symbols only
468 * if callchains are present
470 if (symbol_conf
.use_callchain
&&
471 !output
[PERF_TYPE_TRACEPOINT
].user_set
) {
472 struct perf_event_attr
*attr
;
474 j
= PERF_TYPE_TRACEPOINT
;
476 evlist__for_each_entry(session
->evlist
, evsel
) {
477 if (evsel
->attr
.type
!= j
)
482 if (attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
483 output
[j
].fields
|= PERF_OUTPUT_IP
;
484 output
[j
].fields
|= PERF_OUTPUT_SYM
;
485 output
[j
].fields
|= PERF_OUTPUT_DSO
;
486 set_print_ip_opts(attr
);
496 static void print_sample_iregs(struct perf_sample
*sample
,
497 struct perf_event_attr
*attr
)
499 struct regs_dump
*regs
= &sample
->intr_regs
;
500 uint64_t mask
= attr
->sample_regs_intr
;
506 for_each_set_bit(r
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
507 u64 val
= regs
->regs
[i
++];
508 printf("%5s:0x%"PRIx64
" ", perf_reg_name(r
), val
);
512 static void print_sample_start(struct perf_sample
*sample
,
513 struct thread
*thread
,
514 struct perf_evsel
*evsel
)
516 struct perf_event_attr
*attr
= &evsel
->attr
;
518 unsigned long long nsecs
;
520 if (PRINT_FIELD(COMM
)) {
522 printf("%8.8s ", thread__comm_str(thread
));
523 else if (PRINT_FIELD(IP
) && symbol_conf
.use_callchain
)
524 printf("%s ", thread__comm_str(thread
));
526 printf("%16s ", thread__comm_str(thread
));
529 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
530 printf("%5d/%-5d ", sample
->pid
, sample
->tid
);
531 else if (PRINT_FIELD(PID
))
532 printf("%5d ", sample
->pid
);
533 else if (PRINT_FIELD(TID
))
534 printf("%5d ", sample
->tid
);
536 if (PRINT_FIELD(CPU
)) {
538 printf("%3d ", sample
->cpu
);
540 printf("[%03d] ", sample
->cpu
);
543 if (PRINT_FIELD(TIME
)) {
544 nsecs
= sample
->time
;
545 secs
= nsecs
/ NSEC_PER_SEC
;
546 nsecs
-= secs
* NSEC_PER_SEC
;
549 printf("%5lu.%09llu: ", secs
, nsecs
);
551 char sample_time
[32];
552 timestamp__scnprintf_usec(sample
->time
, sample_time
, sizeof(sample_time
));
553 printf("%12s: ", sample_time
);
559 mispred_str(struct branch_entry
*br
)
561 if (!(br
->flags
.mispred
|| br
->flags
.predicted
))
564 return br
->flags
.predicted
? 'P' : 'M';
567 static void print_sample_brstack(struct perf_sample
*sample
,
568 struct thread
*thread
,
569 struct perf_event_attr
*attr
)
571 struct branch_stack
*br
= sample
->branch_stack
;
572 struct addr_location alf
, alt
;
578 for (i
= 0; i
< br
->nr
; i
++) {
579 from
= br
->entries
[i
].from
;
580 to
= br
->entries
[i
].to
;
582 if (PRINT_FIELD(DSO
)) {
583 memset(&alf
, 0, sizeof(alf
));
584 memset(&alt
, 0, sizeof(alt
));
585 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, from
, &alf
);
586 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, to
, &alt
);
589 printf("0x%"PRIx64
, from
);
590 if (PRINT_FIELD(DSO
)) {
592 map__fprintf_dsoname(alf
.map
, stdout
);
596 printf("/0x%"PRIx64
, to
);
597 if (PRINT_FIELD(DSO
)) {
599 map__fprintf_dsoname(alt
.map
, stdout
);
603 printf("/%c/%c/%c/%d ",
604 mispred_str( br
->entries
+ i
),
605 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
606 br
->entries
[i
].flags
.abort
? 'A' : '-',
607 br
->entries
[i
].flags
.cycles
);
611 static void print_sample_brstacksym(struct perf_sample
*sample
,
612 struct thread
*thread
,
613 struct perf_event_attr
*attr
)
615 struct branch_stack
*br
= sample
->branch_stack
;
616 struct addr_location alf
, alt
;
622 for (i
= 0; i
< br
->nr
; i
++) {
624 memset(&alf
, 0, sizeof(alf
));
625 memset(&alt
, 0, sizeof(alt
));
626 from
= br
->entries
[i
].from
;
627 to
= br
->entries
[i
].to
;
629 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, from
, &alf
);
631 alf
.sym
= map__find_symbol(alf
.map
, alf
.addr
);
633 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, to
, &alt
);
635 alt
.sym
= map__find_symbol(alt
.map
, alt
.addr
);
637 symbol__fprintf_symname_offs(alf
.sym
, &alf
, stdout
);
638 if (PRINT_FIELD(DSO
)) {
640 map__fprintf_dsoname(alf
.map
, stdout
);
644 symbol__fprintf_symname_offs(alt
.sym
, &alt
, stdout
);
645 if (PRINT_FIELD(DSO
)) {
647 map__fprintf_dsoname(alt
.map
, stdout
);
650 printf("/%c/%c/%c/%d ",
651 mispred_str( br
->entries
+ i
),
652 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
653 br
->entries
[i
].flags
.abort
? 'A' : '-',
654 br
->entries
[i
].flags
.cycles
);
658 static void print_sample_brstackoff(struct perf_sample
*sample
,
659 struct thread
*thread
,
660 struct perf_event_attr
*attr
)
662 struct branch_stack
*br
= sample
->branch_stack
;
663 struct addr_location alf
, alt
;
669 for (i
= 0; i
< br
->nr
; i
++) {
671 memset(&alf
, 0, sizeof(alf
));
672 memset(&alt
, 0, sizeof(alt
));
673 from
= br
->entries
[i
].from
;
674 to
= br
->entries
[i
].to
;
676 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, from
, &alf
);
677 if (alf
.map
&& !alf
.map
->dso
->adjust_symbols
)
678 from
= map__map_ip(alf
.map
, from
);
680 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, to
, &alt
);
681 if (alt
.map
&& !alt
.map
->dso
->adjust_symbols
)
682 to
= map__map_ip(alt
.map
, to
);
684 printf("0x%"PRIx64
, from
);
685 if (PRINT_FIELD(DSO
)) {
687 map__fprintf_dsoname(alf
.map
, stdout
);
690 printf("/0x%"PRIx64
, to
);
691 if (PRINT_FIELD(DSO
)) {
693 map__fprintf_dsoname(alt
.map
, stdout
);
696 printf("/%c/%c/%c/%d ",
697 mispred_str(br
->entries
+ i
),
698 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
699 br
->entries
[i
].flags
.abort
? 'A' : '-',
700 br
->entries
[i
].flags
.cycles
);
703 #define MAXBB 16384UL
705 static int grab_bb(u8
*buffer
, u64 start
, u64 end
,
706 struct machine
*machine
, struct thread
*thread
,
707 bool *is64bit
, u8
*cpumode
, bool last
)
710 struct addr_location al
;
716 kernel
= machine__kernel_ip(machine
, start
);
718 *cpumode
= PERF_RECORD_MISC_KERNEL
;
720 *cpumode
= PERF_RECORD_MISC_USER
;
723 * Block overlaps between kernel and user.
724 * This can happen due to ring filtering
725 * On Intel CPUs the entry into the kernel is filtered,
726 * but the exit is not. Let the caller patch it up.
728 if (kernel
!= machine__kernel_ip(machine
, end
)) {
729 printf("\tblock %" PRIx64
"-%" PRIx64
" transfers between kernel and user\n",
734 memset(&al
, 0, sizeof(al
));
735 if (end
- start
> MAXBB
- MAXINSN
) {
737 printf("\tbrstack does not reach to final jump (%" PRIx64
"-%" PRIx64
")\n", start
, end
);
739 printf("\tblock %" PRIx64
"-%" PRIx64
" (%" PRIu64
") too long to dump\n", start
, end
, end
- start
);
743 thread__find_addr_map(thread
, *cpumode
, MAP__FUNCTION
, start
, &al
);
744 if (!al
.map
|| !al
.map
->dso
) {
745 printf("\tcannot resolve %" PRIx64
"-%" PRIx64
"\n", start
, end
);
748 if (al
.map
->dso
->data
.status
== DSO_DATA_STATUS_ERROR
) {
749 printf("\tcannot resolve %" PRIx64
"-%" PRIx64
"\n", start
, end
);
753 /* Load maps to ensure dso->is_64_bit has been updated */
756 offset
= al
.map
->map_ip(al
.map
, start
);
757 len
= dso__data_read_offset(al
.map
->dso
, machine
, offset
, (u8
*)buffer
,
758 end
- start
+ MAXINSN
);
760 *is64bit
= al
.map
->dso
->is_64_bit
;
762 printf("\tcannot fetch code for block at %" PRIx64
"-%" PRIx64
"\n",
767 static void print_jump(uint64_t ip
, struct branch_entry
*en
,
768 struct perf_insn
*x
, u8
*inbuf
, int len
,
771 printf("\t%016" PRIx64
"\t%-30s\t#%s%s%s%s",
773 dump_insn(x
, ip
, inbuf
, len
, NULL
),
774 en
->flags
.predicted
? " PRED" : "",
775 en
->flags
.mispred
? " MISPRED" : "",
776 en
->flags
.in_tx
? " INTX" : "",
777 en
->flags
.abort
? " ABORT" : "");
778 if (en
->flags
.cycles
) {
779 printf(" %d cycles", en
->flags
.cycles
);
781 printf(" %.2f IPC", (float)insn
/ en
->flags
.cycles
);
786 static void print_ip_sym(struct thread
*thread
, u8 cpumode
, int cpu
,
787 uint64_t addr
, struct symbol
**lastsym
,
788 struct perf_event_attr
*attr
)
790 struct addr_location al
;
793 memset(&al
, 0, sizeof(al
));
795 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, addr
, &al
);
797 thread__find_addr_map(thread
, cpumode
, MAP__VARIABLE
,
799 if ((*lastsym
) && al
.addr
>= (*lastsym
)->start
&& al
.addr
< (*lastsym
)->end
)
805 al
.sym
= map__find_symbol(al
.map
, al
.addr
);
810 if (al
.addr
< al
.sym
->end
)
811 off
= al
.addr
- al
.sym
->start
;
813 off
= al
.addr
- al
.map
->start
- al
.sym
->start
;
814 printf("\t%s", al
.sym
->name
);
818 if (PRINT_FIELD(SRCLINE
))
819 map__fprintf_srcline(al
.map
, al
.addr
, "\t", stdout
);
824 static void print_sample_brstackinsn(struct perf_sample
*sample
,
825 struct thread
*thread
,
826 struct perf_event_attr
*attr
,
827 struct machine
*machine
)
829 struct branch_stack
*br
= sample
->branch_stack
;
831 int i
, insn
, len
, nr
, ilen
;
835 struct symbol
*lastsym
= NULL
;
840 if (max_blocks
&& nr
> max_blocks
+ 1)
848 /* Handle first from jump, of which we don't know the entry. */
849 len
= grab_bb(buffer
, br
->entries
[nr
-1].from
,
850 br
->entries
[nr
-1].from
,
851 machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
853 print_ip_sym(thread
, x
.cpumode
, x
.cpu
,
854 br
->entries
[nr
- 1].from
, &lastsym
, attr
);
855 print_jump(br
->entries
[nr
- 1].from
, &br
->entries
[nr
- 1],
859 /* Print all blocks */
860 for (i
= nr
- 2; i
>= 0; i
--) {
861 if (br
->entries
[i
].from
|| br
->entries
[i
].to
)
862 pr_debug("%d: %" PRIx64
"-%" PRIx64
"\n", i
,
865 start
= br
->entries
[i
+ 1].to
;
866 end
= br
->entries
[i
].from
;
868 len
= grab_bb(buffer
, start
, end
, machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
869 /* Patch up missing kernel transfers due to ring filters */
870 if (len
== -ENXIO
&& i
> 0) {
871 end
= br
->entries
[--i
].from
;
872 pr_debug("\tpatching up to %" PRIx64
"-%" PRIx64
"\n", start
, end
);
873 len
= grab_bb(buffer
, start
, end
, machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
879 for (off
= 0;; off
+= ilen
) {
880 uint64_t ip
= start
+ off
;
882 print_ip_sym(thread
, x
.cpumode
, x
.cpu
, ip
, &lastsym
, attr
);
884 print_jump(ip
, &br
->entries
[i
], &x
, buffer
+ off
, len
- off
, insn
);
887 printf("\t%016" PRIx64
"\t%s\n", ip
,
888 dump_insn(&x
, ip
, buffer
+ off
, len
- off
, &ilen
));
897 * Hit the branch? In this case we are already done, and the target
898 * has not been executed yet.
900 if (br
->entries
[0].from
== sample
->ip
)
902 if (br
->entries
[0].flags
.abort
)
906 * Print final block upto sample
908 start
= br
->entries
[0].to
;
910 len
= grab_bb(buffer
, start
, end
, machine
, thread
, &x
.is64bit
, &x
.cpumode
, true);
911 print_ip_sym(thread
, x
.cpumode
, x
.cpu
, start
, &lastsym
, attr
);
913 /* Print at least last IP if basic block did not work */
914 len
= grab_bb(buffer
, sample
->ip
, sample
->ip
,
915 machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
919 printf("\t%016" PRIx64
"\t%s\n", sample
->ip
,
920 dump_insn(&x
, sample
->ip
, buffer
, len
, NULL
));
923 for (off
= 0; off
<= end
- start
; off
+= ilen
) {
924 printf("\t%016" PRIx64
"\t%s\n", start
+ off
,
925 dump_insn(&x
, start
+ off
, buffer
+ off
, len
- off
, &ilen
));
931 static void print_sample_addr(struct perf_sample
*sample
,
932 struct thread
*thread
,
933 struct perf_event_attr
*attr
)
935 struct addr_location al
;
937 printf("%16" PRIx64
, sample
->addr
);
939 if (!sample_addr_correlates_sym(attr
))
942 thread__resolve(thread
, &al
, sample
);
944 if (PRINT_FIELD(SYM
)) {
946 if (PRINT_FIELD(SYMOFFSET
))
947 symbol__fprintf_symname_offs(al
.sym
, &al
, stdout
);
949 symbol__fprintf_symname(al
.sym
, stdout
);
952 if (PRINT_FIELD(DSO
)) {
954 map__fprintf_dsoname(al
.map
, stdout
);
959 static void print_sample_callindent(struct perf_sample
*sample
,
960 struct perf_evsel
*evsel
,
961 struct thread
*thread
,
962 struct addr_location
*al
)
964 struct perf_event_attr
*attr
= &evsel
->attr
;
965 size_t depth
= thread_stack__depth(thread
);
966 struct addr_location addr_al
;
967 const char *name
= NULL
;
973 * The 'return' has already been popped off the stack so the depth has
974 * to be adjusted to match the 'call'.
976 if (thread
->ts
&& sample
->flags
& PERF_IP_FLAG_RETURN
)
979 if (sample
->flags
& (PERF_IP_FLAG_CALL
| PERF_IP_FLAG_TRACE_BEGIN
)) {
980 if (sample_addr_correlates_sym(attr
)) {
981 thread__resolve(thread
, &addr_al
, sample
);
983 name
= addr_al
.sym
->name
;
989 } else if (sample
->flags
& (PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_TRACE_END
)) {
991 name
= al
->sym
->name
;
997 len
= printf("%*s%s", (int)depth
* 4, "", name
);
999 len
= printf("%*s%16" PRIx64
, (int)depth
* 4, "", ip
);
1005 * Try to keep the output length from changing frequently so that the
1006 * output lines up more nicely.
1008 if (len
> spacing
|| (len
&& len
< spacing
- 52))
1009 spacing
= round_up(len
+ 4, 32);
1012 printf("%*s", spacing
- len
, "");
1015 static void print_insn(struct perf_sample
*sample
,
1016 struct perf_event_attr
*attr
,
1017 struct thread
*thread
,
1018 struct machine
*machine
)
1020 if (PRINT_FIELD(INSNLEN
))
1021 printf(" ilen: %d", sample
->insn_len
);
1022 if (PRINT_FIELD(INSN
)) {
1026 for (i
= 0; i
< sample
->insn_len
; i
++)
1027 printf(" %02x", (unsigned char)sample
->insn
[i
]);
1029 if (PRINT_FIELD(BRSTACKINSN
))
1030 print_sample_brstackinsn(sample
, thread
, attr
, machine
);
1033 static void print_sample_bts(struct perf_sample
*sample
,
1034 struct perf_evsel
*evsel
,
1035 struct thread
*thread
,
1036 struct addr_location
*al
,
1037 struct machine
*machine
)
1039 struct perf_event_attr
*attr
= &evsel
->attr
;
1040 unsigned int type
= output_type(attr
->type
);
1041 bool print_srcline_last
= false;
1043 if (PRINT_FIELD(CALLINDENT
))
1044 print_sample_callindent(sample
, evsel
, thread
, al
);
1046 /* print branch_from information */
1047 if (PRINT_FIELD(IP
)) {
1048 unsigned int print_opts
= output
[type
].print_ip_opts
;
1049 struct callchain_cursor
*cursor
= NULL
;
1051 if (symbol_conf
.use_callchain
&& sample
->callchain
&&
1052 thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
1053 sample
, NULL
, NULL
, scripting_max_stack
) == 0)
1054 cursor
= &callchain_cursor
;
1056 if (cursor
== NULL
) {
1058 if (print_opts
& EVSEL__PRINT_SRCLINE
) {
1059 print_srcline_last
= true;
1060 print_opts
&= ~EVSEL__PRINT_SRCLINE
;
1065 sample__fprintf_sym(sample
, al
, 0, print_opts
, cursor
, stdout
);
1068 /* print branch_to information */
1069 if (PRINT_FIELD(ADDR
) ||
1070 ((evsel
->attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
1071 !output
[type
].user_set
)) {
1073 print_sample_addr(sample
, thread
, attr
);
1076 if (print_srcline_last
)
1077 map__fprintf_srcline(al
->map
, al
->addr
, "\n ", stdout
);
1079 print_insn(sample
, attr
, thread
, machine
);
1087 } sample_flags
[] = {
1088 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
, "call"},
1089 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
, "return"},
1090 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CONDITIONAL
, "jcc"},
1091 {PERF_IP_FLAG_BRANCH
, "jmp"},
1092 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_INTERRUPT
, "int"},
1093 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_INTERRUPT
, "iret"},
1094 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_SYSCALLRET
, "syscall"},
1095 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_SYSCALLRET
, "sysret"},
1096 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_ASYNC
, "async"},
1097 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_ASYNC
| PERF_IP_FLAG_INTERRUPT
, "hw int"},
1098 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TX_ABORT
, "tx abrt"},
1099 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TRACE_BEGIN
, "tr strt"},
1100 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TRACE_END
, "tr end"},
1104 static void print_sample_flags(u32 flags
)
1106 const char *chars
= PERF_IP_FLAG_CHARS
;
1107 const int n
= strlen(PERF_IP_FLAG_CHARS
);
1108 bool in_tx
= flags
& PERF_IP_FLAG_IN_TX
;
1109 const char *name
= NULL
;
1113 for (i
= 0; sample_flags
[i
].name
; i
++) {
1114 if (sample_flags
[i
].flags
== (flags
& ~PERF_IP_FLAG_IN_TX
)) {
1115 name
= sample_flags
[i
].name
;
1120 for (i
= 0; i
< n
; i
++, flags
>>= 1) {
1122 str
[pos
++] = chars
[i
];
1124 for (; i
< 32; i
++, flags
>>= 1) {
1131 printf(" %-7s%4s ", name
, in_tx
? "(x)" : "");
1133 printf(" %-11s ", str
);
1136 struct printer_data
{
1143 print_sample_bpf_output_printer(enum binary_printer_ops op
,
1147 unsigned char ch
= (unsigned char)val
;
1148 struct printer_data
*printer_data
= extra
;
1151 case BINARY_PRINT_DATA_BEGIN
:
1154 case BINARY_PRINT_LINE_BEGIN
:
1155 printf("%17s", !printer_data
->line_no
? "BPF output:" :
1158 case BINARY_PRINT_ADDR
:
1159 printf(" %04x:", val
);
1161 case BINARY_PRINT_NUM_DATA
:
1162 printf(" %02x", val
);
1164 case BINARY_PRINT_NUM_PAD
:
1167 case BINARY_PRINT_SEP
:
1170 case BINARY_PRINT_CHAR_DATA
:
1171 if (printer_data
->hit_nul
&& ch
)
1172 printer_data
->is_printable
= false;
1177 if (!printer_data
->is_printable
)
1181 printer_data
->hit_nul
= true;
1183 printer_data
->is_printable
= false;
1188 case BINARY_PRINT_CHAR_PAD
:
1191 case BINARY_PRINT_LINE_END
:
1193 printer_data
->line_no
++;
1195 case BINARY_PRINT_DATA_END
:
1201 static void print_sample_bpf_output(struct perf_sample
*sample
)
1203 unsigned int nr_bytes
= sample
->raw_size
;
1204 struct printer_data printer_data
= {0, false, true};
1206 print_binary(sample
->raw_data
, nr_bytes
, 8,
1207 print_sample_bpf_output_printer
, &printer_data
);
1209 if (printer_data
.is_printable
&& printer_data
.hit_nul
)
1210 printf("%17s \"%s\"\n", "BPF string:",
1211 (char *)(sample
->raw_data
));
1214 static void print_sample_spacing(int len
, int spacing
)
1216 if (len
> 0 && len
< spacing
)
1217 printf("%*s", spacing
- len
, "");
1220 static void print_sample_pt_spacing(int len
)
1222 print_sample_spacing(len
, 34);
1225 static void print_sample_synth_ptwrite(struct perf_sample
*sample
)
1227 struct perf_synth_intel_ptwrite
*data
= perf_sample__synth_ptr(sample
);
1230 if (perf_sample__bad_synth_size(sample
, *data
))
1233 len
= printf(" IP: %u payload: %#" PRIx64
" ",
1234 data
->ip
, le64_to_cpu(data
->payload
));
1235 print_sample_pt_spacing(len
);
1238 static void print_sample_synth_mwait(struct perf_sample
*sample
)
1240 struct perf_synth_intel_mwait
*data
= perf_sample__synth_ptr(sample
);
1243 if (perf_sample__bad_synth_size(sample
, *data
))
1246 len
= printf(" hints: %#x extensions: %#x ",
1247 data
->hints
, data
->extensions
);
1248 print_sample_pt_spacing(len
);
1251 static void print_sample_synth_pwre(struct perf_sample
*sample
)
1253 struct perf_synth_intel_pwre
*data
= perf_sample__synth_ptr(sample
);
1256 if (perf_sample__bad_synth_size(sample
, *data
))
1259 len
= printf(" hw: %u cstate: %u sub-cstate: %u ",
1260 data
->hw
, data
->cstate
, data
->subcstate
);
1261 print_sample_pt_spacing(len
);
1264 static void print_sample_synth_exstop(struct perf_sample
*sample
)
1266 struct perf_synth_intel_exstop
*data
= perf_sample__synth_ptr(sample
);
1269 if (perf_sample__bad_synth_size(sample
, *data
))
1272 len
= printf(" IP: %u ", data
->ip
);
1273 print_sample_pt_spacing(len
);
1276 static void print_sample_synth_pwrx(struct perf_sample
*sample
)
1278 struct perf_synth_intel_pwrx
*data
= perf_sample__synth_ptr(sample
);
1281 if (perf_sample__bad_synth_size(sample
, *data
))
1284 len
= printf(" deepest cstate: %u last cstate: %u wake reason: %#x ",
1285 data
->deepest_cstate
, data
->last_cstate
,
1287 print_sample_pt_spacing(len
);
1290 static void print_sample_synth_cbr(struct perf_sample
*sample
)
1292 struct perf_synth_intel_cbr
*data
= perf_sample__synth_ptr(sample
);
1293 unsigned int percent
, freq
;
1296 if (perf_sample__bad_synth_size(sample
, *data
))
1299 freq
= (le32_to_cpu(data
->freq
) + 500) / 1000;
1300 len
= printf(" cbr: %2u freq: %4u MHz ", data
->cbr
, freq
);
1301 if (data
->max_nonturbo
) {
1302 percent
= (5 + (1000 * data
->cbr
) / data
->max_nonturbo
) / 10;
1303 len
+= printf("(%3u%%) ", percent
);
1305 print_sample_pt_spacing(len
);
1308 static void print_sample_synth(struct perf_sample
*sample
,
1309 struct perf_evsel
*evsel
)
1311 switch (evsel
->attr
.config
) {
1312 case PERF_SYNTH_INTEL_PTWRITE
:
1313 print_sample_synth_ptwrite(sample
);
1315 case PERF_SYNTH_INTEL_MWAIT
:
1316 print_sample_synth_mwait(sample
);
1318 case PERF_SYNTH_INTEL_PWRE
:
1319 print_sample_synth_pwre(sample
);
1321 case PERF_SYNTH_INTEL_EXSTOP
:
1322 print_sample_synth_exstop(sample
);
1324 case PERF_SYNTH_INTEL_PWRX
:
1325 print_sample_synth_pwrx(sample
);
1327 case PERF_SYNTH_INTEL_CBR
:
1328 print_sample_synth_cbr(sample
);
1335 struct perf_script
{
1336 struct perf_tool tool
;
1337 struct perf_session
*session
;
1338 bool show_task_events
;
1339 bool show_mmap_events
;
1340 bool show_switch_events
;
1341 bool show_namespace_events
;
1343 struct cpu_map
*cpus
;
1344 struct thread_map
*threads
;
1346 const char *time_str
;
1347 struct perf_time_interval ptime
;
1350 static int perf_evlist__max_name_len(struct perf_evlist
*evlist
)
1352 struct perf_evsel
*evsel
;
1355 evlist__for_each_entry(evlist
, evsel
) {
1356 int len
= strlen(perf_evsel__name(evsel
));
1358 max
= MAX(len
, max
);
1364 static size_t data_src__printf(u64 data_src
)
1366 struct mem_info mi
= { .data_src
.val
= data_src
};
1372 perf_script__meminfo_scnprintf(decode
, 100, &mi
);
1374 len
= scnprintf(out
, 100, "%16" PRIx64
" %s", data_src
, decode
);
1378 return printf("%-*s", maxlen
, out
);
1381 static void process_event(struct perf_script
*script
,
1382 struct perf_sample
*sample
, struct perf_evsel
*evsel
,
1383 struct addr_location
*al
,
1384 struct machine
*machine
)
1386 struct thread
*thread
= al
->thread
;
1387 struct perf_event_attr
*attr
= &evsel
->attr
;
1388 unsigned int type
= output_type(attr
->type
);
1390 if (output
[type
].fields
== 0)
1393 print_sample_start(sample
, thread
, evsel
);
1395 if (PRINT_FIELD(PERIOD
))
1396 printf("%10" PRIu64
" ", sample
->period
);
1398 if (PRINT_FIELD(EVNAME
)) {
1399 const char *evname
= perf_evsel__name(evsel
);
1401 if (!script
->name_width
)
1402 script
->name_width
= perf_evlist__max_name_len(script
->session
->evlist
);
1404 printf("%*s: ", script
->name_width
,
1405 evname
? evname
: "[unknown]");
1409 print_sample_flags(sample
->flags
);
1411 if (is_bts_event(attr
)) {
1412 print_sample_bts(sample
, evsel
, thread
, al
, machine
);
1416 if (PRINT_FIELD(TRACE
))
1417 event_format__print(evsel
->tp_format
, sample
->cpu
,
1418 sample
->raw_data
, sample
->raw_size
);
1420 if (attr
->type
== PERF_TYPE_SYNTH
&& PRINT_FIELD(SYNTH
))
1421 print_sample_synth(sample
, evsel
);
1423 if (PRINT_FIELD(ADDR
))
1424 print_sample_addr(sample
, thread
, attr
);
1426 if (PRINT_FIELD(DATA_SRC
))
1427 data_src__printf(sample
->data_src
);
1429 if (PRINT_FIELD(WEIGHT
))
1430 printf("%16" PRIu64
, sample
->weight
);
1432 if (PRINT_FIELD(IP
)) {
1433 struct callchain_cursor
*cursor
= NULL
;
1435 if (symbol_conf
.use_callchain
&& sample
->callchain
&&
1436 thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
1437 sample
, NULL
, NULL
, scripting_max_stack
) == 0)
1438 cursor
= &callchain_cursor
;
1440 putchar(cursor
? '\n' : ' ');
1441 sample__fprintf_sym(sample
, al
, 0, output
[type
].print_ip_opts
, cursor
, stdout
);
1444 if (PRINT_FIELD(IREGS
))
1445 print_sample_iregs(sample
, attr
);
1447 if (PRINT_FIELD(BRSTACK
))
1448 print_sample_brstack(sample
, thread
, attr
);
1449 else if (PRINT_FIELD(BRSTACKSYM
))
1450 print_sample_brstacksym(sample
, thread
, attr
);
1451 else if (PRINT_FIELD(BRSTACKOFF
))
1452 print_sample_brstackoff(sample
, thread
, attr
);
1454 if (perf_evsel__is_bpf_output(evsel
) && PRINT_FIELD(BPF_OUTPUT
))
1455 print_sample_bpf_output(sample
);
1456 print_insn(sample
, attr
, thread
, machine
);
1458 if (PRINT_FIELD(PHYS_ADDR
))
1459 printf("%16" PRIx64
, sample
->phys_addr
);
1463 static struct scripting_ops
*scripting_ops
;
1465 static void __process_stat(struct perf_evsel
*counter
, u64 tstamp
)
1467 int nthreads
= thread_map__nr(counter
->threads
);
1468 int ncpus
= perf_evsel__nr_cpus(counter
);
1470 static int header_printed
;
1472 if (counter
->system_wide
)
1475 if (!header_printed
) {
1476 printf("%3s %8s %15s %15s %15s %15s %s\n",
1477 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1481 for (thread
= 0; thread
< nthreads
; thread
++) {
1482 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
1483 struct perf_counts_values
*counts
;
1485 counts
= perf_counts(counter
->counts
, cpu
, thread
);
1487 printf("%3d %8d %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %s\n",
1488 counter
->cpus
->map
[cpu
],
1489 thread_map__pid(counter
->threads
, thread
),
1494 perf_evsel__name(counter
));
1499 static void process_stat(struct perf_evsel
*counter
, u64 tstamp
)
1501 if (scripting_ops
&& scripting_ops
->process_stat
)
1502 scripting_ops
->process_stat(&stat_config
, counter
, tstamp
);
1504 __process_stat(counter
, tstamp
);
1507 static void process_stat_interval(u64 tstamp
)
1509 if (scripting_ops
&& scripting_ops
->process_stat_interval
)
1510 scripting_ops
->process_stat_interval(tstamp
);
1513 static void setup_scripting(void)
1515 setup_perl_scripting();
1516 setup_python_scripting();
1519 static int flush_scripting(void)
1521 return scripting_ops
? scripting_ops
->flush_script() : 0;
1524 static int cleanup_scripting(void)
1526 pr_debug("\nperf script stopped\n");
1528 return scripting_ops
? scripting_ops
->stop_script() : 0;
1531 static int process_sample_event(struct perf_tool
*tool
,
1532 union perf_event
*event
,
1533 struct perf_sample
*sample
,
1534 struct perf_evsel
*evsel
,
1535 struct machine
*machine
)
1537 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
1538 struct addr_location al
;
1540 if (perf_time__skip_sample(&scr
->ptime
, sample
->time
))
1544 if (sample
->time
< last_timestamp
) {
1545 pr_err("Samples misordered, previous: %" PRIu64
1546 " this: %" PRIu64
"\n", last_timestamp
,
1550 last_timestamp
= sample
->time
;
1554 if (machine__resolve(machine
, &al
, sample
) < 0) {
1555 pr_err("problem processing %d event, skipping it.\n",
1556 event
->header
.type
);
1563 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
))
1567 scripting_ops
->process_event(event
, sample
, evsel
, &al
);
1569 process_event(scr
, sample
, evsel
, &al
, machine
);
1572 addr_location__put(&al
);
1576 static int process_attr(struct perf_tool
*tool
, union perf_event
*event
,
1577 struct perf_evlist
**pevlist
)
1579 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
1580 struct perf_evlist
*evlist
;
1581 struct perf_evsel
*evsel
, *pos
;
1584 err
= perf_event__process_attr(tool
, event
, pevlist
);
1589 evsel
= perf_evlist__last(*pevlist
);
1591 if (evsel
->attr
.type
>= PERF_TYPE_MAX
&&
1592 evsel
->attr
.type
!= PERF_TYPE_SYNTH
)
1595 evlist__for_each_entry(evlist
, pos
) {
1596 if (pos
->attr
.type
== evsel
->attr
.type
&& pos
!= evsel
)
1600 set_print_ip_opts(&evsel
->attr
);
1602 if (evsel
->attr
.sample_type
)
1603 err
= perf_evsel__check_attr(evsel
, scr
->session
);
1608 static int process_comm_event(struct perf_tool
*tool
,
1609 union perf_event
*event
,
1610 struct perf_sample
*sample
,
1611 struct machine
*machine
)
1613 struct thread
*thread
;
1614 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1615 struct perf_session
*session
= script
->session
;
1616 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1619 thread
= machine__findnew_thread(machine
, event
->comm
.pid
, event
->comm
.tid
);
1620 if (thread
== NULL
) {
1621 pr_debug("problem processing COMM event, skipping it.\n");
1625 if (perf_event__process_comm(tool
, event
, sample
, machine
) < 0)
1628 if (!evsel
->attr
.sample_id_all
) {
1631 sample
->tid
= event
->comm
.tid
;
1632 sample
->pid
= event
->comm
.pid
;
1634 print_sample_start(sample
, thread
, evsel
);
1635 perf_event__fprintf(event
, stdout
);
1638 thread__put(thread
);
1642 static int process_namespaces_event(struct perf_tool
*tool
,
1643 union perf_event
*event
,
1644 struct perf_sample
*sample
,
1645 struct machine
*machine
)
1647 struct thread
*thread
;
1648 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1649 struct perf_session
*session
= script
->session
;
1650 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1653 thread
= machine__findnew_thread(machine
, event
->namespaces
.pid
,
1654 event
->namespaces
.tid
);
1655 if (thread
== NULL
) {
1656 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1660 if (perf_event__process_namespaces(tool
, event
, sample
, machine
) < 0)
1663 if (!evsel
->attr
.sample_id_all
) {
1666 sample
->tid
= event
->namespaces
.tid
;
1667 sample
->pid
= event
->namespaces
.pid
;
1669 print_sample_start(sample
, thread
, evsel
);
1670 perf_event__fprintf(event
, stdout
);
1673 thread__put(thread
);
1677 static int process_fork_event(struct perf_tool
*tool
,
1678 union perf_event
*event
,
1679 struct perf_sample
*sample
,
1680 struct machine
*machine
)
1682 struct thread
*thread
;
1683 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1684 struct perf_session
*session
= script
->session
;
1685 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1687 if (perf_event__process_fork(tool
, event
, sample
, machine
) < 0)
1690 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
1691 if (thread
== NULL
) {
1692 pr_debug("problem processing FORK event, skipping it.\n");
1696 if (!evsel
->attr
.sample_id_all
) {
1698 sample
->time
= event
->fork
.time
;
1699 sample
->tid
= event
->fork
.tid
;
1700 sample
->pid
= event
->fork
.pid
;
1702 print_sample_start(sample
, thread
, evsel
);
1703 perf_event__fprintf(event
, stdout
);
1704 thread__put(thread
);
1708 static int process_exit_event(struct perf_tool
*tool
,
1709 union perf_event
*event
,
1710 struct perf_sample
*sample
,
1711 struct machine
*machine
)
1714 struct thread
*thread
;
1715 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1716 struct perf_session
*session
= script
->session
;
1717 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1719 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
1720 if (thread
== NULL
) {
1721 pr_debug("problem processing EXIT event, skipping it.\n");
1725 if (!evsel
->attr
.sample_id_all
) {
1728 sample
->tid
= event
->fork
.tid
;
1729 sample
->pid
= event
->fork
.pid
;
1731 print_sample_start(sample
, thread
, evsel
);
1732 perf_event__fprintf(event
, stdout
);
1734 if (perf_event__process_exit(tool
, event
, sample
, machine
) < 0)
1737 thread__put(thread
);
1741 static int process_mmap_event(struct perf_tool
*tool
,
1742 union perf_event
*event
,
1743 struct perf_sample
*sample
,
1744 struct machine
*machine
)
1746 struct thread
*thread
;
1747 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1748 struct perf_session
*session
= script
->session
;
1749 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1751 if (perf_event__process_mmap(tool
, event
, sample
, machine
) < 0)
1754 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
, event
->mmap
.tid
);
1755 if (thread
== NULL
) {
1756 pr_debug("problem processing MMAP event, skipping it.\n");
1760 if (!evsel
->attr
.sample_id_all
) {
1763 sample
->tid
= event
->mmap
.tid
;
1764 sample
->pid
= event
->mmap
.pid
;
1766 print_sample_start(sample
, thread
, evsel
);
1767 perf_event__fprintf(event
, stdout
);
1768 thread__put(thread
);
1772 static int process_mmap2_event(struct perf_tool
*tool
,
1773 union perf_event
*event
,
1774 struct perf_sample
*sample
,
1775 struct machine
*machine
)
1777 struct thread
*thread
;
1778 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1779 struct perf_session
*session
= script
->session
;
1780 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1782 if (perf_event__process_mmap2(tool
, event
, sample
, machine
) < 0)
1785 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
, event
->mmap2
.tid
);
1786 if (thread
== NULL
) {
1787 pr_debug("problem processing MMAP2 event, skipping it.\n");
1791 if (!evsel
->attr
.sample_id_all
) {
1794 sample
->tid
= event
->mmap2
.tid
;
1795 sample
->pid
= event
->mmap2
.pid
;
1797 print_sample_start(sample
, thread
, evsel
);
1798 perf_event__fprintf(event
, stdout
);
1799 thread__put(thread
);
1803 static int process_switch_event(struct perf_tool
*tool
,
1804 union perf_event
*event
,
1805 struct perf_sample
*sample
,
1806 struct machine
*machine
)
1808 struct thread
*thread
;
1809 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1810 struct perf_session
*session
= script
->session
;
1811 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1813 if (perf_event__process_switch(tool
, event
, sample
, machine
) < 0)
1816 thread
= machine__findnew_thread(machine
, sample
->pid
,
1818 if (thread
== NULL
) {
1819 pr_debug("problem processing SWITCH event, skipping it.\n");
1823 print_sample_start(sample
, thread
, evsel
);
1824 perf_event__fprintf(event
, stdout
);
1825 thread__put(thread
);
1829 static void sig_handler(int sig __maybe_unused
)
1834 static int __cmd_script(struct perf_script
*script
)
1838 signal(SIGINT
, sig_handler
);
1840 /* override event processing functions */
1841 if (script
->show_task_events
) {
1842 script
->tool
.comm
= process_comm_event
;
1843 script
->tool
.fork
= process_fork_event
;
1844 script
->tool
.exit
= process_exit_event
;
1846 if (script
->show_mmap_events
) {
1847 script
->tool
.mmap
= process_mmap_event
;
1848 script
->tool
.mmap2
= process_mmap2_event
;
1850 if (script
->show_switch_events
)
1851 script
->tool
.context_switch
= process_switch_event
;
1852 if (script
->show_namespace_events
)
1853 script
->tool
.namespaces
= process_namespaces_event
;
1855 ret
= perf_session__process_events(script
->session
);
1858 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
1863 struct script_spec
{
1864 struct list_head node
;
1865 struct scripting_ops
*ops
;
1869 static LIST_HEAD(script_specs
);
1871 static struct script_spec
*script_spec__new(const char *spec
,
1872 struct scripting_ops
*ops
)
1874 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
1877 strcpy(s
->spec
, spec
);
1884 static void script_spec__add(struct script_spec
*s
)
1886 list_add_tail(&s
->node
, &script_specs
);
1889 static struct script_spec
*script_spec__find(const char *spec
)
1891 struct script_spec
*s
;
1893 list_for_each_entry(s
, &script_specs
, node
)
1894 if (strcasecmp(s
->spec
, spec
) == 0)
1899 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
1901 struct script_spec
*s
;
1903 s
= script_spec__find(spec
);
1907 s
= script_spec__new(spec
, ops
);
1911 script_spec__add(s
);
1916 static struct scripting_ops
*script_spec__lookup(const char *spec
)
1918 struct script_spec
*s
= script_spec__find(spec
);
1925 static void list_available_languages(void)
1927 struct script_spec
*s
;
1929 fprintf(stderr
, "\n");
1930 fprintf(stderr
, "Scripting language extensions (used in "
1931 "perf script -s [spec:]script.[spec]):\n\n");
1933 list_for_each_entry(s
, &script_specs
, node
)
1934 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
1936 fprintf(stderr
, "\n");
1939 static int parse_scriptname(const struct option
*opt __maybe_unused
,
1940 const char *str
, int unset __maybe_unused
)
1942 char spec
[PATH_MAX
];
1943 const char *script
, *ext
;
1946 if (strcmp(str
, "lang") == 0) {
1947 list_available_languages();
1951 script
= strchr(str
, ':');
1954 if (len
>= PATH_MAX
) {
1955 fprintf(stderr
, "invalid language specifier");
1958 strncpy(spec
, str
, len
);
1960 scripting_ops
= script_spec__lookup(spec
);
1961 if (!scripting_ops
) {
1962 fprintf(stderr
, "invalid language specifier");
1968 ext
= strrchr(script
, '.');
1970 fprintf(stderr
, "invalid script extension");
1973 scripting_ops
= script_spec__lookup(++ext
);
1974 if (!scripting_ops
) {
1975 fprintf(stderr
, "invalid script extension");
1980 script_name
= strdup(script
);
1985 static int parse_output_fields(const struct option
*opt __maybe_unused
,
1986 const char *arg
, int unset __maybe_unused
)
1988 char *tok
, *strtok_saveptr
= NULL
;
1989 int i
, imax
= ARRAY_SIZE(all_output_options
);
1992 char *str
= strdup(arg
);
1994 enum { DEFAULT
, SET
, ADD
, REMOVE
} change
= DEFAULT
;
1999 /* first word can state for which event type the user is specifying
2000 * the fields. If no type exists, the specified fields apply to all
2001 * event types found in the file minus the invalid fields for a type.
2003 tok
= strchr(str
, ':');
2007 if (!strcmp(str
, "hw"))
2008 type
= PERF_TYPE_HARDWARE
;
2009 else if (!strcmp(str
, "sw"))
2010 type
= PERF_TYPE_SOFTWARE
;
2011 else if (!strcmp(str
, "trace"))
2012 type
= PERF_TYPE_TRACEPOINT
;
2013 else if (!strcmp(str
, "raw"))
2014 type
= PERF_TYPE_RAW
;
2015 else if (!strcmp(str
, "break"))
2016 type
= PERF_TYPE_BREAKPOINT
;
2017 else if (!strcmp(str
, "synth"))
2018 type
= OUTPUT_TYPE_SYNTH
;
2020 fprintf(stderr
, "Invalid event type in field string.\n");
2025 if (output
[type
].user_set
)
2026 pr_warning("Overriding previous field request for %s events.\n",
2029 output
[type
].fields
= 0;
2030 output
[type
].user_set
= true;
2031 output
[type
].wildcard_set
= false;
2035 if (strlen(str
) == 0) {
2037 "Cannot set fields to 'none' for all event types.\n");
2042 /* Don't override defaults for +- */
2043 if (strchr(str
, '+') || strchr(str
, '-'))
2046 if (output_set_by_user())
2047 pr_warning("Overriding previous field request for all events.\n");
2049 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
2050 output
[j
].fields
= 0;
2051 output
[j
].user_set
= true;
2052 output
[j
].wildcard_set
= true;
2057 for (tok
= strtok_r(tok
, ",", &strtok_saveptr
); tok
; tok
= strtok_r(NULL
, ",", &strtok_saveptr
)) {
2063 } else if (*tok
== '-') {
2069 if (change
!= SET
&& change
!= DEFAULT
)
2074 for (i
= 0; i
< imax
; ++i
) {
2075 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
2078 if (i
== imax
&& strcmp(tok
, "flags") == 0) {
2079 print_flags
= change
== REMOVE
? false : true;
2083 fprintf(stderr
, "Invalid field requested.\n");
2089 /* add user option to all events types for
2092 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
2093 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
2094 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2095 all_output_options
[i
].str
, event_type(j
));
2097 if (change
== REMOVE
)
2098 output
[j
].fields
&= ~all_output_options
[i
].field
;
2100 output
[j
].fields
|= all_output_options
[i
].field
;
2104 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
2105 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
2106 all_output_options
[i
].str
, event_type(type
));
2111 output
[type
].fields
|= all_output_options
[i
].field
;
2116 if (output
[type
].fields
== 0) {
2117 pr_debug("No fields requested for %s type. "
2118 "Events will not be displayed.\n", event_type(type
));
2124 fprintf(stderr
, "Cannot mix +-field with overridden fields\n");
2131 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2132 static int is_directory(const char *base_path
, const struct dirent
*dent
)
2134 char path
[PATH_MAX
];
2137 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
2138 if (stat(path
, &st
))
2141 return S_ISDIR(st
.st_mode
);
2144 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2145 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2146 if ((lang_dirent->d_type == DT_DIR || \
2147 (lang_dirent->d_type == DT_UNKNOWN && \
2148 is_directory(scripts_path, lang_dirent))) && \
2149 (strcmp(lang_dirent->d_name, ".")) && \
2150 (strcmp(lang_dirent->d_name, "..")))
2152 #define for_each_script(lang_path, lang_dir, script_dirent) \
2153 while ((script_dirent = readdir(lang_dir)) != NULL) \
2154 if (script_dirent->d_type != DT_DIR && \
2155 (script_dirent->d_type != DT_UNKNOWN || \
2156 !is_directory(lang_path, script_dirent)))
2159 #define RECORD_SUFFIX "-record"
2160 #define REPORT_SUFFIX "-report"
2162 struct script_desc
{
2163 struct list_head node
;
2169 static LIST_HEAD(script_descs
);
2171 static struct script_desc
*script_desc__new(const char *name
)
2173 struct script_desc
*s
= zalloc(sizeof(*s
));
2175 if (s
!= NULL
&& name
)
2176 s
->name
= strdup(name
);
2181 static void script_desc__delete(struct script_desc
*s
)
2184 zfree(&s
->half_liner
);
2189 static void script_desc__add(struct script_desc
*s
)
2191 list_add_tail(&s
->node
, &script_descs
);
2194 static struct script_desc
*script_desc__find(const char *name
)
2196 struct script_desc
*s
;
2198 list_for_each_entry(s
, &script_descs
, node
)
2199 if (strcasecmp(s
->name
, name
) == 0)
2204 static struct script_desc
*script_desc__findnew(const char *name
)
2206 struct script_desc
*s
= script_desc__find(name
);
2211 s
= script_desc__new(name
);
2215 script_desc__add(s
);
2220 static const char *ends_with(const char *str
, const char *suffix
)
2222 size_t suffix_len
= strlen(suffix
);
2223 const char *p
= str
;
2225 if (strlen(str
) > suffix_len
) {
2226 p
= str
+ strlen(str
) - suffix_len
;
2227 if (!strncmp(p
, suffix
, suffix_len
))
2234 static int read_script_info(struct script_desc
*desc
, const char *filename
)
2236 char line
[BUFSIZ
], *p
;
2239 fp
= fopen(filename
, "r");
2243 while (fgets(line
, sizeof(line
), fp
)) {
2250 if (strlen(p
) && *p
== '!')
2254 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
2255 p
[strlen(p
) - 1] = '\0';
2257 if (!strncmp(p
, "description:", strlen("description:"))) {
2258 p
+= strlen("description:");
2259 desc
->half_liner
= strdup(ltrim(p
));
2263 if (!strncmp(p
, "args:", strlen("args:"))) {
2264 p
+= strlen("args:");
2265 desc
->args
= strdup(ltrim(p
));
2275 static char *get_script_root(struct dirent
*script_dirent
, const char *suffix
)
2277 char *script_root
, *str
;
2279 script_root
= strdup(script_dirent
->d_name
);
2283 str
= (char *)ends_with(script_root
, suffix
);
2293 static int list_available_scripts(const struct option
*opt __maybe_unused
,
2294 const char *s __maybe_unused
,
2295 int unset __maybe_unused
)
2297 struct dirent
*script_dirent
, *lang_dirent
;
2298 char scripts_path
[MAXPATHLEN
];
2299 DIR *scripts_dir
, *lang_dir
;
2300 char script_path
[MAXPATHLEN
];
2301 char lang_path
[MAXPATHLEN
];
2302 struct script_desc
*desc
;
2303 char first_half
[BUFSIZ
];
2306 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
2308 scripts_dir
= opendir(scripts_path
);
2311 "open(%s) failed.\n"
2312 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2317 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
2318 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
2319 lang_dirent
->d_name
);
2320 lang_dir
= opendir(lang_path
);
2324 for_each_script(lang_path
, lang_dir
, script_dirent
) {
2325 script_root
= get_script_root(script_dirent
, REPORT_SUFFIX
);
2327 desc
= script_desc__findnew(script_root
);
2328 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
2329 lang_path
, script_dirent
->d_name
);
2330 read_script_info(desc
, script_path
);
2336 fprintf(stdout
, "List of available trace scripts:\n");
2337 list_for_each_entry(desc
, &script_descs
, node
) {
2338 sprintf(first_half
, "%s %s", desc
->name
,
2339 desc
->args
? desc
->args
: "");
2340 fprintf(stdout
, " %-36s %s\n", first_half
,
2341 desc
->half_liner
? desc
->half_liner
: "");
2348 * Some scripts specify the required events in their "xxx-record" file,
2349 * this function will check if the events in perf.data match those
2350 * mentioned in the "xxx-record".
2352 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2353 * which is covered well now. And new parsing code should be added to
2354 * cover the future complexing formats like event groups etc.
2356 static int check_ev_match(char *dir_name
, char *scriptname
,
2357 struct perf_session
*session
)
2359 char filename
[MAXPATHLEN
], evname
[128];
2360 char line
[BUFSIZ
], *p
;
2361 struct perf_evsel
*pos
;
2365 sprintf(filename
, "%s/bin/%s-record", dir_name
, scriptname
);
2367 fp
= fopen(filename
, "r");
2371 while (fgets(line
, sizeof(line
), fp
)) {
2377 p
= strstr(p
, "-e");
2383 len
= strcspn(p
, " \t");
2387 snprintf(evname
, len
+ 1, "%s", p
);
2390 evlist__for_each_entry(session
->evlist
, pos
) {
2391 if (!strcmp(perf_evsel__name(pos
), evname
)) {
2409 * Return -1 if none is found, otherwise the actual scripts number.
2411 * Currently the only user of this function is the script browser, which
2412 * will list all statically runnable scripts, select one, execute it and
2413 * show the output in a perf browser.
2415 int find_scripts(char **scripts_array
, char **scripts_path_array
)
2417 struct dirent
*script_dirent
, *lang_dirent
;
2418 char scripts_path
[MAXPATHLEN
], lang_path
[MAXPATHLEN
];
2419 DIR *scripts_dir
, *lang_dir
;
2420 struct perf_session
*session
;
2421 struct perf_data_file file
= {
2423 .mode
= PERF_DATA_MODE_READ
,
2428 session
= perf_session__new(&file
, false, NULL
);
2432 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
2434 scripts_dir
= opendir(scripts_path
);
2436 perf_session__delete(session
);
2440 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
2441 snprintf(lang_path
, MAXPATHLEN
, "%s/%s", scripts_path
,
2442 lang_dirent
->d_name
);
2444 if (strstr(lang_path
, "perl"))
2448 if (strstr(lang_path
, "python"))
2452 lang_dir
= opendir(lang_path
);
2456 for_each_script(lang_path
, lang_dir
, script_dirent
) {
2457 /* Skip those real time scripts: xxxtop.p[yl] */
2458 if (strstr(script_dirent
->d_name
, "top."))
2460 sprintf(scripts_path_array
[i
], "%s/%s", lang_path
,
2461 script_dirent
->d_name
);
2462 temp
= strchr(script_dirent
->d_name
, '.');
2463 snprintf(scripts_array
[i
],
2464 (temp
- script_dirent
->d_name
) + 1,
2465 "%s", script_dirent
->d_name
);
2467 if (check_ev_match(lang_path
,
2468 scripts_array
[i
], session
))
2476 closedir(scripts_dir
);
2477 perf_session__delete(session
);
2481 static char *get_script_path(const char *script_root
, const char *suffix
)
2483 struct dirent
*script_dirent
, *lang_dirent
;
2484 char scripts_path
[MAXPATHLEN
];
2485 char script_path
[MAXPATHLEN
];
2486 DIR *scripts_dir
, *lang_dir
;
2487 char lang_path
[MAXPATHLEN
];
2488 char *__script_root
;
2490 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
2492 scripts_dir
= opendir(scripts_path
);
2496 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
2497 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
2498 lang_dirent
->d_name
);
2499 lang_dir
= opendir(lang_path
);
2503 for_each_script(lang_path
, lang_dir
, script_dirent
) {
2504 __script_root
= get_script_root(script_dirent
, suffix
);
2505 if (__script_root
&& !strcmp(script_root
, __script_root
)) {
2506 free(__script_root
);
2508 closedir(scripts_dir
);
2509 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
2510 lang_path
, script_dirent
->d_name
);
2511 return strdup(script_path
);
2513 free(__script_root
);
2517 closedir(scripts_dir
);
2522 static bool is_top_script(const char *script_path
)
2524 return ends_with(script_path
, "top") == NULL
? false : true;
2527 static int has_required_arg(char *script_path
)
2529 struct script_desc
*desc
;
2533 desc
= script_desc__new(NULL
);
2535 if (read_script_info(desc
, script_path
))
2541 for (p
= desc
->args
; *p
; p
++)
2545 script_desc__delete(desc
);
2550 static int have_cmd(int argc
, const char **argv
)
2552 char **__argv
= malloc(sizeof(const char *) * argc
);
2555 pr_err("malloc failed\n");
2559 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
2560 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
2561 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
2564 system_wide
= (argc
== 0);
2569 static void script__setup_sample_type(struct perf_script
*script
)
2571 struct perf_session
*session
= script
->session
;
2572 u64 sample_type
= perf_evlist__combined_sample_type(session
->evlist
);
2574 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
) {
2575 if ((sample_type
& PERF_SAMPLE_REGS_USER
) &&
2576 (sample_type
& PERF_SAMPLE_STACK_USER
))
2577 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
2578 else if (sample_type
& PERF_SAMPLE_BRANCH_STACK
)
2579 callchain_param
.record_mode
= CALLCHAIN_LBR
;
2581 callchain_param
.record_mode
= CALLCHAIN_FP
;
2585 static int process_stat_round_event(struct perf_tool
*tool __maybe_unused
,
2586 union perf_event
*event
,
2587 struct perf_session
*session
)
2589 struct stat_round_event
*round
= &event
->stat_round
;
2590 struct perf_evsel
*counter
;
2592 evlist__for_each_entry(session
->evlist
, counter
) {
2593 perf_stat_process_counter(&stat_config
, counter
);
2594 process_stat(counter
, round
->time
);
2597 process_stat_interval(round
->time
);
2601 static int process_stat_config_event(struct perf_tool
*tool __maybe_unused
,
2602 union perf_event
*event
,
2603 struct perf_session
*session __maybe_unused
)
2605 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
2609 static int set_maps(struct perf_script
*script
)
2611 struct perf_evlist
*evlist
= script
->session
->evlist
;
2613 if (!script
->cpus
|| !script
->threads
)
2616 if (WARN_ONCE(script
->allocated
, "stats double allocation\n"))
2619 perf_evlist__set_maps(evlist
, script
->cpus
, script
->threads
);
2621 if (perf_evlist__alloc_stats(evlist
, true))
2624 script
->allocated
= true;
2629 int process_thread_map_event(struct perf_tool
*tool
,
2630 union perf_event
*event
,
2631 struct perf_session
*session __maybe_unused
)
2633 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2635 if (script
->threads
) {
2636 pr_warning("Extra thread map event, ignoring.\n");
2640 script
->threads
= thread_map__new_event(&event
->thread_map
);
2641 if (!script
->threads
)
2644 return set_maps(script
);
2648 int process_cpu_map_event(struct perf_tool
*tool __maybe_unused
,
2649 union perf_event
*event
,
2650 struct perf_session
*session __maybe_unused
)
2652 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2655 pr_warning("Extra cpu map event, ignoring.\n");
2659 script
->cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
2663 return set_maps(script
);
2666 int cmd_script(int argc
, const char **argv
)
2668 bool show_full_info
= false;
2669 bool header
= false;
2670 bool header_only
= false;
2671 bool script_started
= false;
2672 char *rec_script_path
= NULL
;
2673 char *rep_script_path
= NULL
;
2674 struct perf_session
*session
;
2675 struct itrace_synth_opts itrace_synth_opts
= { .set
= false, };
2676 char *script_path
= NULL
;
2677 const char **__argv
;
2679 struct perf_script script
= {
2681 .sample
= process_sample_event
,
2682 .mmap
= perf_event__process_mmap
,
2683 .mmap2
= perf_event__process_mmap2
,
2684 .comm
= perf_event__process_comm
,
2685 .namespaces
= perf_event__process_namespaces
,
2686 .exit
= perf_event__process_exit
,
2687 .fork
= perf_event__process_fork
,
2688 .attr
= process_attr
,
2689 .event_update
= perf_event__process_event_update
,
2690 .tracing_data
= perf_event__process_tracing_data
,
2691 .feature
= perf_event__process_feature
,
2692 .build_id
= perf_event__process_build_id
,
2693 .id_index
= perf_event__process_id_index
,
2694 .auxtrace_info
= perf_event__process_auxtrace_info
,
2695 .auxtrace
= perf_event__process_auxtrace
,
2696 .auxtrace_error
= perf_event__process_auxtrace_error
,
2697 .stat
= perf_event__process_stat_event
,
2698 .stat_round
= process_stat_round_event
,
2699 .stat_config
= process_stat_config_event
,
2700 .thread_map
= process_thread_map_event
,
2701 .cpu_map
= process_cpu_map_event
,
2702 .ordered_events
= true,
2703 .ordering_requires_timestamps
= true,
2706 struct perf_data_file file
= {
2707 .mode
= PERF_DATA_MODE_READ
,
2709 const struct option options
[] = {
2710 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
2711 "dump raw trace in ASCII"),
2712 OPT_INCR('v', "verbose", &verbose
,
2713 "be more verbose (show symbol address, etc)"),
2714 OPT_BOOLEAN('L', "Latency", &latency_format
,
2715 "show latency attributes (irqs/preemption disabled, etc)"),
2716 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
2717 list_available_scripts
),
2718 OPT_CALLBACK('s', "script", NULL
, "name",
2719 "script file name (lang:script name, script name, or *)",
2721 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
2722 "generate perf-script.xx script in specified language"),
2723 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
2724 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
2725 "do various checks like samples ordering and lost events"),
2726 OPT_BOOLEAN(0, "header", &header
, "Show data header."),
2727 OPT_BOOLEAN(0, "header-only", &header_only
, "Show only data header."),
2728 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
2729 "file", "vmlinux pathname"),
2730 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
2731 "file", "kallsyms pathname"),
2732 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
2733 "When printing symbols do not display call chain"),
2734 OPT_CALLBACK(0, "symfs", NULL
, "directory",
2735 "Look for files with symbols relative to this directory",
2736 symbol__config_symfs
),
2737 OPT_CALLBACK('F', "fields", NULL
, "str",
2738 "comma separated output fields prepend with 'type:'. "
2739 "+field to add and -field to remove."
2740 "Valid types: hw,sw,trace,raw,synth. "
2741 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2742 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
2743 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
2744 parse_output_fields
),
2745 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
2746 "system-wide collection from all CPUs"),
2747 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
2748 "only consider these symbols"),
2749 OPT_STRING(0, "stop-bt", &symbol_conf
.bt_stop_list_str
, "symbol[,symbol...]",
2750 "Stop display of callgraph at these symbols"),
2751 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
2752 OPT_STRING('c', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
2753 "only display events for these comms"),
2754 OPT_STRING(0, "pid", &symbol_conf
.pid_list_str
, "pid[,pid...]",
2755 "only consider symbols in these pids"),
2756 OPT_STRING(0, "tid", &symbol_conf
.tid_list_str
, "tid[,tid...]",
2757 "only consider symbols in these tids"),
2758 OPT_UINTEGER(0, "max-stack", &scripting_max_stack
,
2759 "Set the maximum stack depth when parsing the callchain, "
2760 "anything beyond the specified depth will be ignored. "
2761 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH
)),
2762 OPT_BOOLEAN('I', "show-info", &show_full_info
,
2763 "display extended information from perf.data file"),
2764 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf
.show_kernel_path
,
2765 "Show the path of [kernel.kallsyms]"),
2766 OPT_BOOLEAN('\0', "show-task-events", &script
.show_task_events
,
2767 "Show the fork/comm/exit events"),
2768 OPT_BOOLEAN('\0', "show-mmap-events", &script
.show_mmap_events
,
2769 "Show the mmap events"),
2770 OPT_BOOLEAN('\0', "show-switch-events", &script
.show_switch_events
,
2771 "Show context switch events (if recorded)"),
2772 OPT_BOOLEAN('\0', "show-namespace-events", &script
.show_namespace_events
,
2773 "Show namespace events (if recorded)"),
2774 OPT_BOOLEAN('f', "force", &symbol_conf
.force
, "don't complain, do it"),
2775 OPT_INTEGER(0, "max-blocks", &max_blocks
,
2776 "Maximum number of code blocks to dump with brstackinsn"),
2777 OPT_BOOLEAN(0, "ns", &nanosecs
,
2778 "Use 9 decimal places when displaying time"),
2779 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts
, NULL
, "opts",
2780 "Instruction Tracing options",
2781 itrace_parse_synth_opts
),
2782 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename
,
2783 "Show full source file name path for source lines"),
2784 OPT_BOOLEAN(0, "demangle", &symbol_conf
.demangle
,
2785 "Enable symbol demangling"),
2786 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf
.demangle_kernel
,
2787 "Enable kernel symbol demangling"),
2788 OPT_STRING(0, "time", &script
.time_str
, "str",
2789 "Time span of interest (start,stop)"),
2790 OPT_BOOLEAN(0, "inline", &symbol_conf
.inline_name
,
2791 "Show inline function"),
2794 const char * const script_subcommands
[] = { "record", "report", NULL
};
2795 const char *script_usage
[] = {
2796 "perf script [<options>]",
2797 "perf script [<options>] record <script> [<record-options>] <command>",
2798 "perf script [<options>] report <script> [script-args]",
2799 "perf script [<options>] <script> [<record-options>] <command>",
2800 "perf script [<options>] <top-script> [script-args]",
2806 argc
= parse_options_subcommand(argc
, argv
, options
, script_subcommands
, script_usage
,
2807 PARSE_OPT_STOP_AT_NON_OPTION
);
2809 file
.path
= input_name
;
2810 file
.force
= symbol_conf
.force
;
2812 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
2813 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
2814 if (!rec_script_path
)
2815 return cmd_record(argc
, argv
);
2818 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
2819 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
2820 if (!rep_script_path
) {
2822 "Please specify a valid report script"
2823 "(see 'perf script -l' for listing)\n");
2828 if (itrace_synth_opts
.callchain
&&
2829 itrace_synth_opts
.callchain_sz
> scripting_max_stack
)
2830 scripting_max_stack
= itrace_synth_opts
.callchain_sz
;
2832 /* make sure PERF_EXEC_PATH is set for scripts */
2833 set_argv_exec_path(get_argv_exec_path());
2835 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
2840 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
2841 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
2843 if (!rec_script_path
&& !rep_script_path
) {
2844 usage_with_options_msg(script_usage
, options
,
2845 "Couldn't find script `%s'\n\n See perf"
2846 " script -l for available scripts.\n", argv
[0]);
2849 if (is_top_script(argv
[0])) {
2850 rep_args
= argc
- 1;
2854 rep_args
= has_required_arg(rep_script_path
);
2855 rec_args
= (argc
- 1) - rep_args
;
2857 usage_with_options_msg(script_usage
, options
,
2858 "`%s' script requires options."
2859 "\n\n See perf script -l for available "
2860 "scripts and options.\n", argv
[0]);
2864 if (pipe(live_pipe
) < 0) {
2865 perror("failed to create pipe");
2871 perror("failed to fork");
2878 dup2(live_pipe
[1], 1);
2879 close(live_pipe
[0]);
2881 if (is_top_script(argv
[0])) {
2883 } else if (!system_wide
) {
2884 if (have_cmd(argc
- rep_args
, &argv
[rep_args
]) != 0) {
2890 __argv
= malloc((argc
+ 6) * sizeof(const char *));
2892 pr_err("malloc failed\n");
2897 __argv
[j
++] = "/bin/sh";
2898 __argv
[j
++] = rec_script_path
;
2904 for (i
= rep_args
+ 1; i
< argc
; i
++)
2905 __argv
[j
++] = argv
[i
];
2908 execvp("/bin/sh", (char **)__argv
);
2913 dup2(live_pipe
[0], 0);
2914 close(live_pipe
[1]);
2916 __argv
= malloc((argc
+ 4) * sizeof(const char *));
2918 pr_err("malloc failed\n");
2924 __argv
[j
++] = "/bin/sh";
2925 __argv
[j
++] = rep_script_path
;
2926 for (i
= 1; i
< rep_args
+ 1; i
++)
2927 __argv
[j
++] = argv
[i
];
2932 execvp("/bin/sh", (char **)__argv
);
2937 if (rec_script_path
)
2938 script_path
= rec_script_path
;
2939 if (rep_script_path
)
2940 script_path
= rep_script_path
;
2945 if (!rec_script_path
)
2946 system_wide
= false;
2947 else if (!system_wide
) {
2948 if (have_cmd(argc
- 1, &argv
[1]) != 0) {
2954 __argv
= malloc((argc
+ 2) * sizeof(const char *));
2956 pr_err("malloc failed\n");
2961 __argv
[j
++] = "/bin/sh";
2962 __argv
[j
++] = script_path
;
2965 for (i
= 2; i
< argc
; i
++)
2966 __argv
[j
++] = argv
[i
];
2969 execvp("/bin/sh", (char **)__argv
);
2977 session
= perf_session__new(&file
, false, &script
.tool
);
2978 if (session
== NULL
)
2981 if (header
|| header_only
) {
2982 script
.tool
.show_feat_hdr
= SHOW_FEAT_HEADER
;
2983 perf_session__fprintf_info(session
, stdout
, show_full_info
);
2988 script
.tool
.show_feat_hdr
= SHOW_FEAT_HEADER_FULL_INFO
;
2990 if (symbol__init(&session
->header
.env
) < 0)
2993 script
.session
= session
;
2994 script__setup_sample_type(&script
);
2996 if (output
[PERF_TYPE_HARDWARE
].fields
& PERF_OUTPUT_CALLINDENT
)
2997 itrace_synth_opts
.thread_stack
= true;
2999 session
->itrace_synth_opts
= &itrace_synth_opts
;
3002 err
= perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
);
3005 itrace_synth_opts
.cpu_bitmap
= cpu_bitmap
;
3009 symbol_conf
.use_callchain
= true;
3011 symbol_conf
.use_callchain
= false;
3013 if (session
->tevent
.pevent
&&
3014 pevent_set_function_resolver(session
->tevent
.pevent
,
3015 machine__resolve_kernel_addr
,
3016 &session
->machines
.host
) < 0) {
3017 pr_err("%s: failed to set libtraceevent function resolver\n", __func__
);
3021 if (generate_script_lang
) {
3022 struct stat perf_stat
;
3025 if (output_set_by_user()) {
3027 "custom fields not supported for generated scripts");
3032 input
= open(file
.path
, O_RDONLY
); /* input_name */
3035 perror("failed to open file");
3039 err
= fstat(input
, &perf_stat
);
3041 perror("failed to stat file");
3045 if (!perf_stat
.st_size
) {
3046 fprintf(stderr
, "zero-sized file, nothing to do!\n");
3050 scripting_ops
= script_spec__lookup(generate_script_lang
);
3051 if (!scripting_ops
) {
3052 fprintf(stderr
, "invalid language specifier");
3057 err
= scripting_ops
->generate_script(session
->tevent
.pevent
,
3063 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
3066 pr_debug("perf script started with script %s\n\n", script_name
);
3067 script_started
= true;
3071 err
= perf_session__check_output_opt(session
);
3075 /* needs to be parsed after looking up reference time */
3076 if (perf_time__parse_str(&script
.ptime
, script
.time_str
) != 0) {
3077 pr_err("Invalid time string\n");
3081 err
= __cmd_script(&script
);
3086 perf_evlist__free_stats(session
->evlist
);
3087 perf_session__delete(session
);
3090 cleanup_scripting();