1 // SPDX-License-Identifier: GPL-2.0
5 #include "util/cache.h"
6 #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"
13 #include "util/symbol.h"
14 #include "util/thread.h"
15 #include "util/trace-event.h"
16 #include "util/util.h"
17 #include "util/evlist.h"
18 #include "util/evsel.h"
19 #include "util/sort.h"
20 #include "util/data.h"
21 #include "util/auxtrace.h"
22 #include "util/cpumap.h"
23 #include "util/thread_map.h"
24 #include "util/stat.h"
25 #include "util/color.h"
26 #include "util/string2.h"
27 #include "util/thread-stack.h"
28 #include "util/time-utils.h"
29 #include "util/path.h"
30 #include "print_binary.h"
31 #include <linux/bitmap.h>
32 #include <linux/kernel.h>
33 #include <linux/stringify.h>
34 #include <linux/time64.h>
36 #include "util/mem-events.h"
37 #include "util/dump-insn.h"
42 #include <sys/param.h>
43 #include <sys/types.h>
47 #include "sane_ctype.h"
49 static char const *script_name
;
50 static char const *generate_script_lang
;
51 static bool debug_mode
;
52 static u64 last_timestamp
;
53 static u64 nr_unordered
;
54 static bool no_callchain
;
55 static bool latency_format
;
56 static bool system_wide
;
57 static bool print_flags
;
59 static const char *cpu_list
;
60 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
61 static struct perf_stat_config stat_config
;
62 static int max_blocks
;
64 unsigned int scripting_max_stack
= PERF_MAX_STACK_DEPTH
;
66 enum perf_output_field
{
67 PERF_OUTPUT_COMM
= 1U << 0,
68 PERF_OUTPUT_TID
= 1U << 1,
69 PERF_OUTPUT_PID
= 1U << 2,
70 PERF_OUTPUT_TIME
= 1U << 3,
71 PERF_OUTPUT_CPU
= 1U << 4,
72 PERF_OUTPUT_EVNAME
= 1U << 5,
73 PERF_OUTPUT_TRACE
= 1U << 6,
74 PERF_OUTPUT_IP
= 1U << 7,
75 PERF_OUTPUT_SYM
= 1U << 8,
76 PERF_OUTPUT_DSO
= 1U << 9,
77 PERF_OUTPUT_ADDR
= 1U << 10,
78 PERF_OUTPUT_SYMOFFSET
= 1U << 11,
79 PERF_OUTPUT_SRCLINE
= 1U << 12,
80 PERF_OUTPUT_PERIOD
= 1U << 13,
81 PERF_OUTPUT_IREGS
= 1U << 14,
82 PERF_OUTPUT_BRSTACK
= 1U << 15,
83 PERF_OUTPUT_BRSTACKSYM
= 1U << 16,
84 PERF_OUTPUT_DATA_SRC
= 1U << 17,
85 PERF_OUTPUT_WEIGHT
= 1U << 18,
86 PERF_OUTPUT_BPF_OUTPUT
= 1U << 19,
87 PERF_OUTPUT_CALLINDENT
= 1U << 20,
88 PERF_OUTPUT_INSN
= 1U << 21,
89 PERF_OUTPUT_INSNLEN
= 1U << 22,
90 PERF_OUTPUT_BRSTACKINSN
= 1U << 23,
91 PERF_OUTPUT_BRSTACKOFF
= 1U << 24,
92 PERF_OUTPUT_SYNTH
= 1U << 25,
93 PERF_OUTPUT_PHYS_ADDR
= 1U << 26,
94 PERF_OUTPUT_UREGS
= 1U << 27,
95 PERF_OUTPUT_METRIC
= 1U << 28,
98 struct output_option
{
100 enum perf_output_field field
;
101 } all_output_options
[] = {
102 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
103 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
104 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
105 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
106 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
107 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
108 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
109 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
110 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
111 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
112 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
113 {.str
= "symoff", .field
= PERF_OUTPUT_SYMOFFSET
},
114 {.str
= "srcline", .field
= PERF_OUTPUT_SRCLINE
},
115 {.str
= "period", .field
= PERF_OUTPUT_PERIOD
},
116 {.str
= "iregs", .field
= PERF_OUTPUT_IREGS
},
117 {.str
= "uregs", .field
= PERF_OUTPUT_UREGS
},
118 {.str
= "brstack", .field
= PERF_OUTPUT_BRSTACK
},
119 {.str
= "brstacksym", .field
= PERF_OUTPUT_BRSTACKSYM
},
120 {.str
= "data_src", .field
= PERF_OUTPUT_DATA_SRC
},
121 {.str
= "weight", .field
= PERF_OUTPUT_WEIGHT
},
122 {.str
= "bpf-output", .field
= PERF_OUTPUT_BPF_OUTPUT
},
123 {.str
= "callindent", .field
= PERF_OUTPUT_CALLINDENT
},
124 {.str
= "insn", .field
= PERF_OUTPUT_INSN
},
125 {.str
= "insnlen", .field
= PERF_OUTPUT_INSNLEN
},
126 {.str
= "brstackinsn", .field
= PERF_OUTPUT_BRSTACKINSN
},
127 {.str
= "brstackoff", .field
= PERF_OUTPUT_BRSTACKOFF
},
128 {.str
= "synth", .field
= PERF_OUTPUT_SYNTH
},
129 {.str
= "phys_addr", .field
= PERF_OUTPUT_PHYS_ADDR
},
130 {.str
= "metric", .field
= PERF_OUTPUT_METRIC
},
134 OUTPUT_TYPE_SYNTH
= PERF_TYPE_MAX
,
138 /* default set to maintain compatibility with current format */
142 unsigned int print_ip_opts
;
145 } output
[OUTPUT_TYPE_MAX
] = {
147 [PERF_TYPE_HARDWARE
] = {
150 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
151 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
152 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
153 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
156 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
159 [PERF_TYPE_SOFTWARE
] = {
162 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
163 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
164 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
165 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
166 PERF_OUTPUT_PERIOD
| PERF_OUTPUT_BPF_OUTPUT
,
168 .invalid_fields
= PERF_OUTPUT_TRACE
,
171 [PERF_TYPE_TRACEPOINT
] = {
174 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
175 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
176 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
182 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
183 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
184 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
185 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
186 PERF_OUTPUT_PERIOD
| PERF_OUTPUT_ADDR
|
187 PERF_OUTPUT_DATA_SRC
| PERF_OUTPUT_WEIGHT
|
188 PERF_OUTPUT_PHYS_ADDR
,
190 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
193 [PERF_TYPE_BREAKPOINT
] = {
196 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
197 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
198 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
199 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
202 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
205 [OUTPUT_TYPE_SYNTH
] = {
208 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
209 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
210 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
211 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
214 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
218 struct perf_evsel_script
{
222 /* For metric output */
227 static inline struct perf_evsel_script
*evsel_script(struct perf_evsel
*evsel
)
229 return (struct perf_evsel_script
*)evsel
->priv
;
232 static struct perf_evsel_script
*perf_evsel_script__new(struct perf_evsel
*evsel
,
233 struct perf_data
*data
)
235 struct perf_evsel_script
*es
= zalloc(sizeof(*es
));
238 if (asprintf(&es
->filename
, "%s.%s.dump", data
->file
.path
, perf_evsel__name(evsel
)) < 0)
240 es
->fp
= fopen(es
->filename
, "w");
242 goto out_free_filename
;
247 zfree(&es
->filename
);
253 static void perf_evsel_script__delete(struct perf_evsel_script
*es
)
255 zfree(&es
->filename
);
261 static int perf_evsel_script__fprintf(struct perf_evsel_script
*es
, FILE *fp
)
265 fstat(fileno(es
->fp
), &st
);
266 return fprintf(fp
, "[ perf script: Wrote %.3f MB %s (%" PRIu64
" samples) ]\n",
267 st
.st_size
/ 1024.0 / 1024.0, es
->filename
, es
->samples
);
270 static inline int output_type(unsigned int type
)
273 case PERF_TYPE_SYNTH
:
274 return OUTPUT_TYPE_SYNTH
;
280 static inline unsigned int attr_type(unsigned int type
)
283 case OUTPUT_TYPE_SYNTH
:
284 return PERF_TYPE_SYNTH
;
290 static bool output_set_by_user(void)
293 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
294 if (output
[j
].user_set
)
300 static const char *output_field2str(enum perf_output_field field
)
302 int i
, imax
= ARRAY_SIZE(all_output_options
);
303 const char *str
= "";
305 for (i
= 0; i
< imax
; ++i
) {
306 if (all_output_options
[i
].field
== field
) {
307 str
= all_output_options
[i
].str
;
314 #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
316 static int perf_evsel__do_check_stype(struct perf_evsel
*evsel
,
317 u64 sample_type
, const char *sample_msg
,
318 enum perf_output_field field
,
321 struct perf_event_attr
*attr
= &evsel
->attr
;
322 int type
= output_type(attr
->type
);
325 if (attr
->sample_type
& sample_type
)
328 if (output
[type
].user_set
) {
331 evname
= perf_evsel__name(evsel
);
332 pr_err("Samples for '%s' event do not have %s attribute set. "
333 "Cannot print '%s' field.\n",
334 evname
, sample_msg
, output_field2str(field
));
338 /* user did not ask for it explicitly so remove from the default list */
339 output
[type
].fields
&= ~field
;
340 evname
= perf_evsel__name(evsel
);
341 pr_debug("Samples for '%s' event do not have %s attribute set. "
342 "Skipping '%s' field.\n",
343 evname
, sample_msg
, output_field2str(field
));
348 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
349 u64 sample_type
, const char *sample_msg
,
350 enum perf_output_field field
)
352 return perf_evsel__do_check_stype(evsel
, sample_type
, sample_msg
, field
,
356 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
357 struct perf_session
*session
)
359 struct perf_event_attr
*attr
= &evsel
->attr
;
362 if (perf_header__has_feat(&session
->header
, HEADER_STAT
))
365 allow_user_set
= perf_header__has_feat(&session
->header
,
368 if (PRINT_FIELD(TRACE
) &&
369 !perf_session__has_traces(session
, "record -R"))
372 if (PRINT_FIELD(IP
)) {
373 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_IP
, "IP",
378 if (PRINT_FIELD(ADDR
) &&
379 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_ADDR
, "ADDR",
380 PERF_OUTPUT_ADDR
, allow_user_set
))
383 if (PRINT_FIELD(DATA_SRC
) &&
384 perf_evsel__check_stype(evsel
, PERF_SAMPLE_DATA_SRC
, "DATA_SRC",
385 PERF_OUTPUT_DATA_SRC
))
388 if (PRINT_FIELD(WEIGHT
) &&
389 perf_evsel__check_stype(evsel
, PERF_SAMPLE_WEIGHT
, "WEIGHT",
393 if (PRINT_FIELD(SYM
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
394 pr_err("Display of symbols requested but neither sample IP nor "
395 "sample address\nis selected. Hence, no addresses to convert "
399 if (PRINT_FIELD(SYMOFFSET
) && !PRINT_FIELD(SYM
)) {
400 pr_err("Display of offsets requested but symbol is not"
404 if (PRINT_FIELD(DSO
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
) &&
405 !PRINT_FIELD(BRSTACK
) && !PRINT_FIELD(BRSTACKSYM
) && !PRINT_FIELD(BRSTACKOFF
)) {
406 pr_err("Display of DSO requested but no address to convert. Select\n"
407 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
410 if (PRINT_FIELD(SRCLINE
) && !PRINT_FIELD(IP
)) {
411 pr_err("Display of source line number requested but sample IP is not\n"
412 "selected. Hence, no address to lookup the source line number.\n");
415 if (PRINT_FIELD(BRSTACKINSN
) &&
416 !(perf_evlist__combined_branch_type(session
->evlist
) &
417 PERF_SAMPLE_BRANCH_ANY
)) {
418 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
419 "Hint: run 'perf record -b ...'\n");
422 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
423 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID",
424 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
427 if (PRINT_FIELD(TIME
) &&
428 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TIME
, "TIME",
432 if (PRINT_FIELD(CPU
) &&
433 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_CPU
, "CPU",
434 PERF_OUTPUT_CPU
, allow_user_set
))
437 if (PRINT_FIELD(IREGS
) &&
438 perf_evsel__check_stype(evsel
, PERF_SAMPLE_REGS_INTR
, "IREGS",
442 if (PRINT_FIELD(UREGS
) &&
443 perf_evsel__check_stype(evsel
, PERF_SAMPLE_REGS_USER
, "UREGS",
447 if (PRINT_FIELD(PHYS_ADDR
) &&
448 perf_evsel__check_stype(evsel
, PERF_SAMPLE_PHYS_ADDR
, "PHYS_ADDR",
449 PERF_OUTPUT_PHYS_ADDR
))
455 static void set_print_ip_opts(struct perf_event_attr
*attr
)
457 unsigned int type
= output_type(attr
->type
);
459 output
[type
].print_ip_opts
= 0;
461 output
[type
].print_ip_opts
|= EVSEL__PRINT_IP
;
463 if (PRINT_FIELD(SYM
))
464 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYM
;
466 if (PRINT_FIELD(DSO
))
467 output
[type
].print_ip_opts
|= EVSEL__PRINT_DSO
;
469 if (PRINT_FIELD(SYMOFFSET
))
470 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYMOFFSET
;
472 if (PRINT_FIELD(SRCLINE
))
473 output
[type
].print_ip_opts
|= EVSEL__PRINT_SRCLINE
;
477 * verify all user requested events exist and the samples
478 * have the expected data
480 static int perf_session__check_output_opt(struct perf_session
*session
)
483 struct perf_evsel
*evsel
;
485 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
486 evsel
= perf_session__find_first_evtype(session
, attr_type(j
));
489 * even if fields is set to 0 (ie., show nothing) event must
490 * exist if user explicitly includes it on the command line
492 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
&&
493 j
!= OUTPUT_TYPE_SYNTH
) {
494 pr_err("%s events do not exist. "
495 "Remove corresponding -F option to proceed.\n",
500 if (evsel
&& output
[j
].fields
&&
501 perf_evsel__check_attr(evsel
, session
))
507 set_print_ip_opts(&evsel
->attr
);
511 bool use_callchain
= false;
512 bool not_pipe
= false;
514 evlist__for_each_entry(session
->evlist
, evsel
) {
516 if (evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
517 use_callchain
= true;
521 if (not_pipe
&& !use_callchain
)
522 symbol_conf
.use_callchain
= false;
526 * set default for tracepoints to print symbols only
527 * if callchains are present
529 if (symbol_conf
.use_callchain
&&
530 !output
[PERF_TYPE_TRACEPOINT
].user_set
) {
531 struct perf_event_attr
*attr
;
533 j
= PERF_TYPE_TRACEPOINT
;
535 evlist__for_each_entry(session
->evlist
, evsel
) {
536 if (evsel
->attr
.type
!= j
)
541 if (attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
542 output
[j
].fields
|= PERF_OUTPUT_IP
;
543 output
[j
].fields
|= PERF_OUTPUT_SYM
;
544 output
[j
].fields
|= PERF_OUTPUT_DSO
;
545 set_print_ip_opts(attr
);
555 static int perf_sample__fprintf_iregs(struct perf_sample
*sample
,
556 struct perf_event_attr
*attr
, FILE *fp
)
558 struct regs_dump
*regs
= &sample
->intr_regs
;
559 uint64_t mask
= attr
->sample_regs_intr
;
566 for_each_set_bit(r
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
567 u64 val
= regs
->regs
[i
++];
568 printed
+= fprintf(fp
, "%5s:0x%"PRIx64
" ", perf_reg_name(r
), val
);
574 static int perf_sample__fprintf_uregs(struct perf_sample
*sample
,
575 struct perf_event_attr
*attr
, FILE *fp
)
577 struct regs_dump
*regs
= &sample
->user_regs
;
578 uint64_t mask
= attr
->sample_regs_user
;
582 if (!regs
|| !regs
->regs
)
585 printed
+= fprintf(fp
, " ABI:%" PRIu64
" ", regs
->abi
);
587 for_each_set_bit(r
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
588 u64 val
= regs
->regs
[i
++];
589 printed
+= fprintf(fp
, "%5s:0x%"PRIx64
" ", perf_reg_name(r
), val
);
595 static int perf_sample__fprintf_start(struct perf_sample
*sample
,
596 struct thread
*thread
,
597 struct perf_evsel
*evsel
, FILE *fp
)
599 struct perf_event_attr
*attr
= &evsel
->attr
;
601 unsigned long long nsecs
;
604 if (PRINT_FIELD(COMM
)) {
606 printed
+= fprintf(fp
, "%8.8s ", thread__comm_str(thread
));
607 else if (PRINT_FIELD(IP
) && symbol_conf
.use_callchain
)
608 printed
+= fprintf(fp
, "%s ", thread__comm_str(thread
));
610 printed
+= fprintf(fp
, "%16s ", thread__comm_str(thread
));
613 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
614 printed
+= fprintf(fp
, "%5d/%-5d ", sample
->pid
, sample
->tid
);
615 else if (PRINT_FIELD(PID
))
616 printed
+= fprintf(fp
, "%5d ", sample
->pid
);
617 else if (PRINT_FIELD(TID
))
618 printed
+= fprintf(fp
, "%5d ", sample
->tid
);
620 if (PRINT_FIELD(CPU
)) {
622 printed
+= fprintf(fp
, "%3d ", sample
->cpu
);
624 printed
+= fprintf(fp
, "[%03d] ", sample
->cpu
);
627 if (PRINT_FIELD(TIME
)) {
628 nsecs
= sample
->time
;
629 secs
= nsecs
/ NSEC_PER_SEC
;
630 nsecs
-= secs
* NSEC_PER_SEC
;
633 printed
+= fprintf(fp
, "%5lu.%09llu: ", secs
, nsecs
);
635 char sample_time
[32];
636 timestamp__scnprintf_usec(sample
->time
, sample_time
, sizeof(sample_time
));
637 printed
+= fprintf(fp
, "%12s: ", sample_time
);
645 mispred_str(struct branch_entry
*br
)
647 if (!(br
->flags
.mispred
|| br
->flags
.predicted
))
650 return br
->flags
.predicted
? 'P' : 'M';
653 static int perf_sample__fprintf_brstack(struct perf_sample
*sample
,
654 struct thread
*thread
,
655 struct perf_event_attr
*attr
, FILE *fp
)
657 struct branch_stack
*br
= sample
->branch_stack
;
658 struct addr_location alf
, alt
;
665 for (i
= 0; i
< br
->nr
; i
++) {
666 from
= br
->entries
[i
].from
;
667 to
= br
->entries
[i
].to
;
669 if (PRINT_FIELD(DSO
)) {
670 memset(&alf
, 0, sizeof(alf
));
671 memset(&alt
, 0, sizeof(alt
));
672 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, from
, &alf
);
673 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, to
, &alt
);
676 printed
+= fprintf(fp
, " 0x%"PRIx64
, from
);
677 if (PRINT_FIELD(DSO
)) {
678 printed
+= fprintf(fp
, "(");
679 printed
+= map__fprintf_dsoname(alf
.map
, fp
);
680 printed
+= fprintf(fp
, ")");
683 printed
+= fprintf(fp
, "/0x%"PRIx64
, to
);
684 if (PRINT_FIELD(DSO
)) {
685 printed
+= fprintf(fp
, "(");
686 printed
+= map__fprintf_dsoname(alt
.map
, fp
);
687 printed
+= fprintf(fp
, ")");
690 printed
+= fprintf(fp
, "/%c/%c/%c/%d ",
691 mispred_str( br
->entries
+ i
),
692 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
693 br
->entries
[i
].flags
.abort
? 'A' : '-',
694 br
->entries
[i
].flags
.cycles
);
700 static int perf_sample__fprintf_brstacksym(struct perf_sample
*sample
,
701 struct thread
*thread
,
702 struct perf_event_attr
*attr
, FILE *fp
)
704 struct branch_stack
*br
= sample
->branch_stack
;
705 struct addr_location alf
, alt
;
712 for (i
= 0; i
< br
->nr
; i
++) {
714 memset(&alf
, 0, sizeof(alf
));
715 memset(&alt
, 0, sizeof(alt
));
716 from
= br
->entries
[i
].from
;
717 to
= br
->entries
[i
].to
;
719 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, from
, &alf
);
721 alf
.sym
= map__find_symbol(alf
.map
, alf
.addr
);
723 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, to
, &alt
);
725 alt
.sym
= map__find_symbol(alt
.map
, alt
.addr
);
727 printed
+= symbol__fprintf_symname_offs(alf
.sym
, &alf
, fp
);
728 if (PRINT_FIELD(DSO
)) {
729 printed
+= fprintf(fp
, "(");
730 printed
+= map__fprintf_dsoname(alf
.map
, fp
);
731 printed
+= fprintf(fp
, ")");
733 printed
+= fprintf(fp
, "%c", '/');
734 printed
+= symbol__fprintf_symname_offs(alt
.sym
, &alt
, fp
);
735 if (PRINT_FIELD(DSO
)) {
736 printed
+= fprintf(fp
, "(");
737 printed
+= map__fprintf_dsoname(alt
.map
, fp
);
738 printed
+= fprintf(fp
, ")");
740 printed
+= fprintf(fp
, "/%c/%c/%c/%d ",
741 mispred_str( br
->entries
+ i
),
742 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
743 br
->entries
[i
].flags
.abort
? 'A' : '-',
744 br
->entries
[i
].flags
.cycles
);
750 static int perf_sample__fprintf_brstackoff(struct perf_sample
*sample
,
751 struct thread
*thread
,
752 struct perf_event_attr
*attr
, FILE *fp
)
754 struct branch_stack
*br
= sample
->branch_stack
;
755 struct addr_location alf
, alt
;
762 for (i
= 0; i
< br
->nr
; i
++) {
764 memset(&alf
, 0, sizeof(alf
));
765 memset(&alt
, 0, sizeof(alt
));
766 from
= br
->entries
[i
].from
;
767 to
= br
->entries
[i
].to
;
769 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, from
, &alf
);
770 if (alf
.map
&& !alf
.map
->dso
->adjust_symbols
)
771 from
= map__map_ip(alf
.map
, from
);
773 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, to
, &alt
);
774 if (alt
.map
&& !alt
.map
->dso
->adjust_symbols
)
775 to
= map__map_ip(alt
.map
, to
);
777 printed
+= fprintf(fp
, " 0x%"PRIx64
, from
);
778 if (PRINT_FIELD(DSO
)) {
779 printed
+= fprintf(fp
, "(");
780 printed
+= map__fprintf_dsoname(alf
.map
, fp
);
781 printed
+= fprintf(fp
, ")");
783 printed
+= fprintf(fp
, "/0x%"PRIx64
, to
);
784 if (PRINT_FIELD(DSO
)) {
785 printed
+= fprintf(fp
, "(");
786 printed
+= map__fprintf_dsoname(alt
.map
, fp
);
787 printed
+= fprintf(fp
, ")");
789 printed
+= fprintf(fp
, "/%c/%c/%c/%d ",
790 mispred_str(br
->entries
+ i
),
791 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
792 br
->entries
[i
].flags
.abort
? 'A' : '-',
793 br
->entries
[i
].flags
.cycles
);
798 #define MAXBB 16384UL
800 static int grab_bb(u8
*buffer
, u64 start
, u64 end
,
801 struct machine
*machine
, struct thread
*thread
,
802 bool *is64bit
, u8
*cpumode
, bool last
)
805 struct addr_location al
;
811 kernel
= machine__kernel_ip(machine
, start
);
813 *cpumode
= PERF_RECORD_MISC_KERNEL
;
815 *cpumode
= PERF_RECORD_MISC_USER
;
818 * Block overlaps between kernel and user.
819 * This can happen due to ring filtering
820 * On Intel CPUs the entry into the kernel is filtered,
821 * but the exit is not. Let the caller patch it up.
823 if (kernel
!= machine__kernel_ip(machine
, end
)) {
824 pr_debug("\tblock %" PRIx64
"-%" PRIx64
" transfers between kernel and user\n", start
, end
);
828 memset(&al
, 0, sizeof(al
));
829 if (end
- start
> MAXBB
- MAXINSN
) {
831 pr_debug("\tbrstack does not reach to final jump (%" PRIx64
"-%" PRIx64
")\n", start
, end
);
833 pr_debug("\tblock %" PRIx64
"-%" PRIx64
" (%" PRIu64
") too long to dump\n", start
, end
, end
- start
);
837 thread__find_addr_map(thread
, *cpumode
, MAP__FUNCTION
, start
, &al
);
838 if (!al
.map
|| !al
.map
->dso
) {
839 pr_debug("\tcannot resolve %" PRIx64
"-%" PRIx64
"\n", start
, end
);
842 if (al
.map
->dso
->data
.status
== DSO_DATA_STATUS_ERROR
) {
843 pr_debug("\tcannot resolve %" PRIx64
"-%" PRIx64
"\n", start
, end
);
847 /* Load maps to ensure dso->is_64_bit has been updated */
850 offset
= al
.map
->map_ip(al
.map
, start
);
851 len
= dso__data_read_offset(al
.map
->dso
, machine
, offset
, (u8
*)buffer
,
852 end
- start
+ MAXINSN
);
854 *is64bit
= al
.map
->dso
->is_64_bit
;
856 pr_debug("\tcannot fetch code for block at %" PRIx64
"-%" PRIx64
"\n",
861 static int ip__fprintf_jump(uint64_t ip
, struct branch_entry
*en
,
862 struct perf_insn
*x
, u8
*inbuf
, int len
,
865 int printed
= fprintf(fp
, "\t%016" PRIx64
"\t%-30s\t#%s%s%s%s", ip
,
866 dump_insn(x
, ip
, inbuf
, len
, NULL
),
867 en
->flags
.predicted
? " PRED" : "",
868 en
->flags
.mispred
? " MISPRED" : "",
869 en
->flags
.in_tx
? " INTX" : "",
870 en
->flags
.abort
? " ABORT" : "");
871 if (en
->flags
.cycles
) {
872 printed
+= fprintf(fp
, " %d cycles", en
->flags
.cycles
);
874 printed
+= fprintf(fp
, " %.2f IPC", (float)insn
/ en
->flags
.cycles
);
876 return printed
+ fprintf(fp
, "\n");
879 static int ip__fprintf_sym(uint64_t addr
, struct thread
*thread
,
880 u8 cpumode
, int cpu
, struct symbol
**lastsym
,
881 struct perf_event_attr
*attr
, FILE *fp
)
883 struct addr_location al
;
884 int off
, printed
= 0;
886 memset(&al
, 0, sizeof(al
));
888 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, addr
, &al
);
890 thread__find_addr_map(thread
, cpumode
, MAP__VARIABLE
,
892 if ((*lastsym
) && al
.addr
>= (*lastsym
)->start
&& al
.addr
< (*lastsym
)->end
)
898 al
.sym
= map__find_symbol(al
.map
, al
.addr
);
903 if (al
.addr
< al
.sym
->end
)
904 off
= al
.addr
- al
.sym
->start
;
906 off
= al
.addr
- al
.map
->start
- al
.sym
->start
;
907 printed
+= fprintf(fp
, "\t%s", al
.sym
->name
);
909 printed
+= fprintf(fp
, "%+d", off
);
910 printed
+= fprintf(fp
, ":");
911 if (PRINT_FIELD(SRCLINE
))
912 printed
+= map__fprintf_srcline(al
.map
, al
.addr
, "\t", fp
);
913 printed
+= fprintf(fp
, "\n");
919 static int perf_sample__fprintf_brstackinsn(struct perf_sample
*sample
,
920 struct thread
*thread
,
921 struct perf_event_attr
*attr
,
922 struct machine
*machine
, FILE *fp
)
924 struct branch_stack
*br
= sample
->branch_stack
;
926 int i
, insn
, len
, nr
, ilen
, printed
= 0;
930 struct symbol
*lastsym
= NULL
;
935 if (max_blocks
&& nr
> max_blocks
+ 1)
941 printed
+= fprintf(fp
, "%c", '\n');
943 /* Handle first from jump, of which we don't know the entry. */
944 len
= grab_bb(buffer
, br
->entries
[nr
-1].from
,
945 br
->entries
[nr
-1].from
,
946 machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
948 printed
+= ip__fprintf_sym(br
->entries
[nr
- 1].from
, thread
,
949 x
.cpumode
, x
.cpu
, &lastsym
, attr
, fp
);
950 printed
+= ip__fprintf_jump(br
->entries
[nr
- 1].from
, &br
->entries
[nr
- 1],
951 &x
, buffer
, len
, 0, fp
);
954 /* Print all blocks */
955 for (i
= nr
- 2; i
>= 0; i
--) {
956 if (br
->entries
[i
].from
|| br
->entries
[i
].to
)
957 pr_debug("%d: %" PRIx64
"-%" PRIx64
"\n", i
,
960 start
= br
->entries
[i
+ 1].to
;
961 end
= br
->entries
[i
].from
;
963 len
= grab_bb(buffer
, start
, end
, machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
964 /* Patch up missing kernel transfers due to ring filters */
965 if (len
== -ENXIO
&& i
> 0) {
966 end
= br
->entries
[--i
].from
;
967 pr_debug("\tpatching up to %" PRIx64
"-%" PRIx64
"\n", start
, end
);
968 len
= grab_bb(buffer
, start
, end
, machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
974 for (off
= 0;; off
+= ilen
) {
975 uint64_t ip
= start
+ off
;
977 printed
+= ip__fprintf_sym(ip
, thread
, x
.cpumode
, x
.cpu
, &lastsym
, attr
, fp
);
979 printed
+= ip__fprintf_jump(ip
, &br
->entries
[i
], &x
, buffer
+ off
, len
- off
, insn
, fp
);
982 printed
+= fprintf(fp
, "\t%016" PRIx64
"\t%s\n", ip
,
983 dump_insn(&x
, ip
, buffer
+ off
, len
- off
, &ilen
));
992 * Hit the branch? In this case we are already done, and the target
993 * has not been executed yet.
995 if (br
->entries
[0].from
== sample
->ip
)
997 if (br
->entries
[0].flags
.abort
)
1001 * Print final block upto sample
1003 start
= br
->entries
[0].to
;
1005 len
= grab_bb(buffer
, start
, end
, machine
, thread
, &x
.is64bit
, &x
.cpumode
, true);
1006 printed
+= ip__fprintf_sym(start
, thread
, x
.cpumode
, x
.cpu
, &lastsym
, attr
, fp
);
1008 /* Print at least last IP if basic block did not work */
1009 len
= grab_bb(buffer
, sample
->ip
, sample
->ip
,
1010 machine
, thread
, &x
.is64bit
, &x
.cpumode
, false);
1014 printed
+= fprintf(fp
, "\t%016" PRIx64
"\t%s\n", sample
->ip
,
1015 dump_insn(&x
, sample
->ip
, buffer
, len
, NULL
));
1018 for (off
= 0; off
<= end
- start
; off
+= ilen
) {
1019 printed
+= fprintf(fp
, "\t%016" PRIx64
"\t%s\n", start
+ off
,
1020 dump_insn(&x
, start
+ off
, buffer
+ off
, len
- off
, &ilen
));
1028 static int perf_sample__fprintf_addr(struct perf_sample
*sample
,
1029 struct thread
*thread
,
1030 struct perf_event_attr
*attr
, FILE *fp
)
1032 struct addr_location al
;
1033 int printed
= fprintf(fp
, "%16" PRIx64
, sample
->addr
);
1035 if (!sample_addr_correlates_sym(attr
))
1038 thread__resolve(thread
, &al
, sample
);
1040 if (PRINT_FIELD(SYM
)) {
1041 printed
+= fprintf(fp
, " ");
1042 if (PRINT_FIELD(SYMOFFSET
))
1043 printed
+= symbol__fprintf_symname_offs(al
.sym
, &al
, fp
);
1045 printed
+= symbol__fprintf_symname(al
.sym
, fp
);
1048 if (PRINT_FIELD(DSO
)) {
1049 printed
+= fprintf(fp
, " (");
1050 printed
+= map__fprintf_dsoname(al
.map
, fp
);
1051 printed
+= fprintf(fp
, ")");
1057 static int perf_sample__fprintf_callindent(struct perf_sample
*sample
,
1058 struct perf_evsel
*evsel
,
1059 struct thread
*thread
,
1060 struct addr_location
*al
, FILE *fp
)
1062 struct perf_event_attr
*attr
= &evsel
->attr
;
1063 size_t depth
= thread_stack__depth(thread
);
1064 struct addr_location addr_al
;
1065 const char *name
= NULL
;
1071 * The 'return' has already been popped off the stack so the depth has
1072 * to be adjusted to match the 'call'.
1074 if (thread
->ts
&& sample
->flags
& PERF_IP_FLAG_RETURN
)
1077 if (sample
->flags
& (PERF_IP_FLAG_CALL
| PERF_IP_FLAG_TRACE_BEGIN
)) {
1078 if (sample_addr_correlates_sym(attr
)) {
1079 thread__resolve(thread
, &addr_al
, sample
);
1081 name
= addr_al
.sym
->name
;
1087 } else if (sample
->flags
& (PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_TRACE_END
)) {
1089 name
= al
->sym
->name
;
1095 len
= fprintf(fp
, "%*s%s", (int)depth
* 4, "", name
);
1097 len
= fprintf(fp
, "%*s%16" PRIx64
, (int)depth
* 4, "", ip
);
1103 * Try to keep the output length from changing frequently so that the
1104 * output lines up more nicely.
1106 if (len
> spacing
|| (len
&& len
< spacing
- 52))
1107 spacing
= round_up(len
+ 4, 32);
1110 len
+= fprintf(fp
, "%*s", spacing
- len
, "");
1115 static int perf_sample__fprintf_insn(struct perf_sample
*sample
,
1116 struct perf_event_attr
*attr
,
1117 struct thread
*thread
,
1118 struct machine
*machine
, FILE *fp
)
1122 if (PRINT_FIELD(INSNLEN
))
1123 printed
+= fprintf(fp
, " ilen: %d", sample
->insn_len
);
1124 if (PRINT_FIELD(INSN
)) {
1127 printed
+= fprintf(fp
, " insn:");
1128 for (i
= 0; i
< sample
->insn_len
; i
++)
1129 printed
+= fprintf(fp
, " %02x", (unsigned char)sample
->insn
[i
]);
1131 if (PRINT_FIELD(BRSTACKINSN
))
1132 printed
+= perf_sample__fprintf_brstackinsn(sample
, thread
, attr
, machine
, fp
);
1137 static int perf_sample__fprintf_bts(struct perf_sample
*sample
,
1138 struct perf_evsel
*evsel
,
1139 struct thread
*thread
,
1140 struct addr_location
*al
,
1141 struct machine
*machine
, FILE *fp
)
1143 struct perf_event_attr
*attr
= &evsel
->attr
;
1144 unsigned int type
= output_type(attr
->type
);
1145 bool print_srcline_last
= false;
1148 if (PRINT_FIELD(CALLINDENT
))
1149 printed
+= perf_sample__fprintf_callindent(sample
, evsel
, thread
, al
, fp
);
1151 /* print branch_from information */
1152 if (PRINT_FIELD(IP
)) {
1153 unsigned int print_opts
= output
[type
].print_ip_opts
;
1154 struct callchain_cursor
*cursor
= NULL
;
1156 if (symbol_conf
.use_callchain
&& sample
->callchain
&&
1157 thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
1158 sample
, NULL
, NULL
, scripting_max_stack
) == 0)
1159 cursor
= &callchain_cursor
;
1161 if (cursor
== NULL
) {
1162 printed
+= fprintf(fp
, " ");
1163 if (print_opts
& EVSEL__PRINT_SRCLINE
) {
1164 print_srcline_last
= true;
1165 print_opts
&= ~EVSEL__PRINT_SRCLINE
;
1168 printed
+= fprintf(fp
, "\n");
1170 printed
+= sample__fprintf_sym(sample
, al
, 0, print_opts
, cursor
, fp
);
1173 /* print branch_to information */
1174 if (PRINT_FIELD(ADDR
) ||
1175 ((evsel
->attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
1176 !output
[type
].user_set
)) {
1177 printed
+= fprintf(fp
, " => ");
1178 printed
+= perf_sample__fprintf_addr(sample
, thread
, attr
, fp
);
1181 if (print_srcline_last
)
1182 printed
+= map__fprintf_srcline(al
->map
, al
->addr
, "\n ", fp
);
1184 printed
+= perf_sample__fprintf_insn(sample
, attr
, thread
, machine
, fp
);
1185 return printed
+ fprintf(fp
, "\n");
1191 } sample_flags
[] = {
1192 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
, "call"},
1193 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
, "return"},
1194 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CONDITIONAL
, "jcc"},
1195 {PERF_IP_FLAG_BRANCH
, "jmp"},
1196 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_INTERRUPT
, "int"},
1197 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_INTERRUPT
, "iret"},
1198 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_SYSCALLRET
, "syscall"},
1199 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_SYSCALLRET
, "sysret"},
1200 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_ASYNC
, "async"},
1201 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_ASYNC
| PERF_IP_FLAG_INTERRUPT
, "hw int"},
1202 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TX_ABORT
, "tx abrt"},
1203 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TRACE_BEGIN
, "tr strt"},
1204 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TRACE_END
, "tr end"},
1208 static int perf_sample__fprintf_flags(u32 flags
, FILE *fp
)
1210 const char *chars
= PERF_IP_FLAG_CHARS
;
1211 const int n
= strlen(PERF_IP_FLAG_CHARS
);
1212 bool in_tx
= flags
& PERF_IP_FLAG_IN_TX
;
1213 const char *name
= NULL
;
1217 for (i
= 0; sample_flags
[i
].name
; i
++) {
1218 if (sample_flags
[i
].flags
== (flags
& ~PERF_IP_FLAG_IN_TX
)) {
1219 name
= sample_flags
[i
].name
;
1224 for (i
= 0; i
< n
; i
++, flags
>>= 1) {
1226 str
[pos
++] = chars
[i
];
1228 for (; i
< 32; i
++, flags
>>= 1) {
1235 return fprintf(fp
, " %-7s%4s ", name
, in_tx
? "(x)" : "");
1237 return fprintf(fp
, " %-11s ", str
);
1240 struct printer_data
{
1246 static int sample__fprintf_bpf_output(enum binary_printer_ops op
,
1248 void *extra
, FILE *fp
)
1250 unsigned char ch
= (unsigned char)val
;
1251 struct printer_data
*printer_data
= extra
;
1255 case BINARY_PRINT_DATA_BEGIN
:
1256 printed
+= fprintf(fp
, "\n");
1258 case BINARY_PRINT_LINE_BEGIN
:
1259 printed
+= fprintf(fp
, "%17s", !printer_data
->line_no
? "BPF output:" :
1262 case BINARY_PRINT_ADDR
:
1263 printed
+= fprintf(fp
, " %04x:", val
);
1265 case BINARY_PRINT_NUM_DATA
:
1266 printed
+= fprintf(fp
, " %02x", val
);
1268 case BINARY_PRINT_NUM_PAD
:
1269 printed
+= fprintf(fp
, " ");
1271 case BINARY_PRINT_SEP
:
1272 printed
+= fprintf(fp
, " ");
1274 case BINARY_PRINT_CHAR_DATA
:
1275 if (printer_data
->hit_nul
&& ch
)
1276 printer_data
->is_printable
= false;
1279 printed
+= fprintf(fp
, "%c", '.');
1281 if (!printer_data
->is_printable
)
1285 printer_data
->hit_nul
= true;
1287 printer_data
->is_printable
= false;
1289 printed
+= fprintf(fp
, "%c", ch
);
1292 case BINARY_PRINT_CHAR_PAD
:
1293 printed
+= fprintf(fp
, " ");
1295 case BINARY_PRINT_LINE_END
:
1296 printed
+= fprintf(fp
, "\n");
1297 printer_data
->line_no
++;
1299 case BINARY_PRINT_DATA_END
:
1307 static int perf_sample__fprintf_bpf_output(struct perf_sample
*sample
, FILE *fp
)
1309 unsigned int nr_bytes
= sample
->raw_size
;
1310 struct printer_data printer_data
= {0, false, true};
1311 int printed
= binary__fprintf(sample
->raw_data
, nr_bytes
, 8,
1312 sample__fprintf_bpf_output
, &printer_data
, fp
);
1314 if (printer_data
.is_printable
&& printer_data
.hit_nul
)
1315 printed
+= fprintf(fp
, "%17s \"%s\"\n", "BPF string:", (char *)(sample
->raw_data
));
1320 static int perf_sample__fprintf_spacing(int len
, int spacing
, FILE *fp
)
1322 if (len
> 0 && len
< spacing
)
1323 return fprintf(fp
, "%*s", spacing
- len
, "");
1328 static int perf_sample__fprintf_pt_spacing(int len
, FILE *fp
)
1330 return perf_sample__fprintf_spacing(len
, 34, fp
);
1333 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample
*sample
, FILE *fp
)
1335 struct perf_synth_intel_ptwrite
*data
= perf_sample__synth_ptr(sample
);
1338 if (perf_sample__bad_synth_size(sample
, *data
))
1341 len
= fprintf(fp
, " IP: %u payload: %#" PRIx64
" ",
1342 data
->ip
, le64_to_cpu(data
->payload
));
1343 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1346 static int perf_sample__fprintf_synth_mwait(struct perf_sample
*sample
, FILE *fp
)
1348 struct perf_synth_intel_mwait
*data
= perf_sample__synth_ptr(sample
);
1351 if (perf_sample__bad_synth_size(sample
, *data
))
1354 len
= fprintf(fp
, " hints: %#x extensions: %#x ",
1355 data
->hints
, data
->extensions
);
1356 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1359 static int perf_sample__fprintf_synth_pwre(struct perf_sample
*sample
, FILE *fp
)
1361 struct perf_synth_intel_pwre
*data
= perf_sample__synth_ptr(sample
);
1364 if (perf_sample__bad_synth_size(sample
, *data
))
1367 len
= fprintf(fp
, " hw: %u cstate: %u sub-cstate: %u ",
1368 data
->hw
, data
->cstate
, data
->subcstate
);
1369 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1372 static int perf_sample__fprintf_synth_exstop(struct perf_sample
*sample
, FILE *fp
)
1374 struct perf_synth_intel_exstop
*data
= perf_sample__synth_ptr(sample
);
1377 if (perf_sample__bad_synth_size(sample
, *data
))
1380 len
= fprintf(fp
, " IP: %u ", data
->ip
);
1381 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1384 static int perf_sample__fprintf_synth_pwrx(struct perf_sample
*sample
, FILE *fp
)
1386 struct perf_synth_intel_pwrx
*data
= perf_sample__synth_ptr(sample
);
1389 if (perf_sample__bad_synth_size(sample
, *data
))
1392 len
= fprintf(fp
, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1393 data
->deepest_cstate
, data
->last_cstate
,
1395 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1398 static int perf_sample__fprintf_synth_cbr(struct perf_sample
*sample
, FILE *fp
)
1400 struct perf_synth_intel_cbr
*data
= perf_sample__synth_ptr(sample
);
1401 unsigned int percent
, freq
;
1404 if (perf_sample__bad_synth_size(sample
, *data
))
1407 freq
= (le32_to_cpu(data
->freq
) + 500) / 1000;
1408 len
= fprintf(fp
, " cbr: %2u freq: %4u MHz ", data
->cbr
, freq
);
1409 if (data
->max_nonturbo
) {
1410 percent
= (5 + (1000 * data
->cbr
) / data
->max_nonturbo
) / 10;
1411 len
+= fprintf(fp
, "(%3u%%) ", percent
);
1413 return len
+ perf_sample__fprintf_pt_spacing(len
, fp
);
1416 static int perf_sample__fprintf_synth(struct perf_sample
*sample
,
1417 struct perf_evsel
*evsel
, FILE *fp
)
1419 switch (evsel
->attr
.config
) {
1420 case PERF_SYNTH_INTEL_PTWRITE
:
1421 return perf_sample__fprintf_synth_ptwrite(sample
, fp
);
1422 case PERF_SYNTH_INTEL_MWAIT
:
1423 return perf_sample__fprintf_synth_mwait(sample
, fp
);
1424 case PERF_SYNTH_INTEL_PWRE
:
1425 return perf_sample__fprintf_synth_pwre(sample
, fp
);
1426 case PERF_SYNTH_INTEL_EXSTOP
:
1427 return perf_sample__fprintf_synth_exstop(sample
, fp
);
1428 case PERF_SYNTH_INTEL_PWRX
:
1429 return perf_sample__fprintf_synth_pwrx(sample
, fp
);
1430 case PERF_SYNTH_INTEL_CBR
:
1431 return perf_sample__fprintf_synth_cbr(sample
, fp
);
1439 struct perf_script
{
1440 struct perf_tool tool
;
1441 struct perf_session
*session
;
1442 bool show_task_events
;
1443 bool show_mmap_events
;
1444 bool show_switch_events
;
1445 bool show_namespace_events
;
1447 bool per_event_dump
;
1448 struct cpu_map
*cpus
;
1449 struct thread_map
*threads
;
1451 const char *time_str
;
1452 struct perf_time_interval ptime
;
1455 static int perf_evlist__max_name_len(struct perf_evlist
*evlist
)
1457 struct perf_evsel
*evsel
;
1460 evlist__for_each_entry(evlist
, evsel
) {
1461 int len
= strlen(perf_evsel__name(evsel
));
1463 max
= MAX(len
, max
);
1469 static int data_src__fprintf(u64 data_src
, FILE *fp
)
1471 struct mem_info mi
= { .data_src
.val
= data_src
};
1477 perf_script__meminfo_scnprintf(decode
, 100, &mi
);
1479 len
= scnprintf(out
, 100, "%16" PRIx64
" %s", data_src
, decode
);
1483 return fprintf(fp
, "%-*s", maxlen
, out
);
1487 struct perf_sample
*sample
;
1488 struct thread
*thread
;
1489 struct perf_evsel
*evsel
;
1493 static void script_print_metric(void *ctx
, const char *color
,
1495 const char *unit
, double val
)
1497 struct metric_ctx
*mctx
= ctx
;
1501 perf_sample__fprintf_start(mctx
->sample
, mctx
->thread
, mctx
->evsel
,
1503 fputs("\tmetric: ", mctx
->fp
);
1505 color_fprintf(mctx
->fp
, color
, fmt
, val
);
1508 fprintf(mctx
->fp
, " %s\n", unit
);
1511 static void script_new_line(void *ctx
)
1513 struct metric_ctx
*mctx
= ctx
;
1515 perf_sample__fprintf_start(mctx
->sample
, mctx
->thread
, mctx
->evsel
,
1517 fputs("\tmetric: ", mctx
->fp
);
1520 static void perf_sample__fprint_metric(struct perf_script
*script
,
1521 struct thread
*thread
,
1522 struct perf_evsel
*evsel
,
1523 struct perf_sample
*sample
,
1526 struct perf_stat_output_ctx ctx
= {
1527 .print_metric
= script_print_metric
,
1528 .new_line
= script_new_line
,
1529 .ctx
= &(struct metric_ctx
) {
1535 .force_header
= false,
1537 struct perf_evsel
*ev2
;
1542 perf_stat__init_shadow_stats();
1546 perf_evlist__alloc_stats(script
->session
->evlist
, false);
1547 if (evsel_script(evsel
->leader
)->gnum
++ == 0)
1548 perf_stat__reset_shadow_stats();
1549 val
= sample
->period
* evsel
->scale
;
1550 perf_stat__update_shadow_stats(evsel
,
1554 evsel_script(evsel
)->val
= val
;
1555 if (evsel_script(evsel
->leader
)->gnum
== evsel
->leader
->nr_members
) {
1556 for_each_group_member (ev2
, evsel
->leader
) {
1557 perf_stat__print_shadow_stats(ev2
,
1558 evsel_script(ev2
)->val
,
1564 evsel_script(evsel
->leader
)->gnum
= 0;
1568 static void process_event(struct perf_script
*script
,
1569 struct perf_sample
*sample
, struct perf_evsel
*evsel
,
1570 struct addr_location
*al
,
1571 struct machine
*machine
)
1573 struct thread
*thread
= al
->thread
;
1574 struct perf_event_attr
*attr
= &evsel
->attr
;
1575 unsigned int type
= output_type(attr
->type
);
1576 struct perf_evsel_script
*es
= evsel
->priv
;
1579 if (output
[type
].fields
== 0)
1584 perf_sample__fprintf_start(sample
, thread
, evsel
, fp
);
1586 if (PRINT_FIELD(PERIOD
))
1587 fprintf(fp
, "%10" PRIu64
" ", sample
->period
);
1589 if (PRINT_FIELD(EVNAME
)) {
1590 const char *evname
= perf_evsel__name(evsel
);
1592 if (!script
->name_width
)
1593 script
->name_width
= perf_evlist__max_name_len(script
->session
->evlist
);
1595 fprintf(fp
, "%*s: ", script
->name_width
, evname
?: "[unknown]");
1599 perf_sample__fprintf_flags(sample
->flags
, fp
);
1601 if (is_bts_event(attr
)) {
1602 perf_sample__fprintf_bts(sample
, evsel
, thread
, al
, machine
, fp
);
1606 if (PRINT_FIELD(TRACE
)) {
1607 event_format__fprintf(evsel
->tp_format
, sample
->cpu
,
1608 sample
->raw_data
, sample
->raw_size
, fp
);
1611 if (attr
->type
== PERF_TYPE_SYNTH
&& PRINT_FIELD(SYNTH
))
1612 perf_sample__fprintf_synth(sample
, evsel
, fp
);
1614 if (PRINT_FIELD(ADDR
))
1615 perf_sample__fprintf_addr(sample
, thread
, attr
, fp
);
1617 if (PRINT_FIELD(DATA_SRC
))
1618 data_src__fprintf(sample
->data_src
, fp
);
1620 if (PRINT_FIELD(WEIGHT
))
1621 fprintf(fp
, "%16" PRIu64
, sample
->weight
);
1623 if (PRINT_FIELD(IP
)) {
1624 struct callchain_cursor
*cursor
= NULL
;
1626 if (symbol_conf
.use_callchain
&& sample
->callchain
&&
1627 thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
1628 sample
, NULL
, NULL
, scripting_max_stack
) == 0)
1629 cursor
= &callchain_cursor
;
1631 fputc(cursor
? '\n' : ' ', fp
);
1632 sample__fprintf_sym(sample
, al
, 0, output
[type
].print_ip_opts
, cursor
, fp
);
1635 if (PRINT_FIELD(IREGS
))
1636 perf_sample__fprintf_iregs(sample
, attr
, fp
);
1638 if (PRINT_FIELD(UREGS
))
1639 perf_sample__fprintf_uregs(sample
, attr
, fp
);
1641 if (PRINT_FIELD(BRSTACK
))
1642 perf_sample__fprintf_brstack(sample
, thread
, attr
, fp
);
1643 else if (PRINT_FIELD(BRSTACKSYM
))
1644 perf_sample__fprintf_brstacksym(sample
, thread
, attr
, fp
);
1645 else if (PRINT_FIELD(BRSTACKOFF
))
1646 perf_sample__fprintf_brstackoff(sample
, thread
, attr
, fp
);
1648 if (perf_evsel__is_bpf_output(evsel
) && PRINT_FIELD(BPF_OUTPUT
))
1649 perf_sample__fprintf_bpf_output(sample
, fp
);
1650 perf_sample__fprintf_insn(sample
, attr
, thread
, machine
, fp
);
1652 if (PRINT_FIELD(PHYS_ADDR
))
1653 fprintf(fp
, "%16" PRIx64
, sample
->phys_addr
);
1656 if (PRINT_FIELD(METRIC
))
1657 perf_sample__fprint_metric(script
, thread
, evsel
, sample
, fp
);
1660 static struct scripting_ops
*scripting_ops
;
1662 static void __process_stat(struct perf_evsel
*counter
, u64 tstamp
)
1664 int nthreads
= thread_map__nr(counter
->threads
);
1665 int ncpus
= perf_evsel__nr_cpus(counter
);
1667 static int header_printed
;
1669 if (counter
->system_wide
)
1672 if (!header_printed
) {
1673 printf("%3s %8s %15s %15s %15s %15s %s\n",
1674 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1678 for (thread
= 0; thread
< nthreads
; thread
++) {
1679 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
1680 struct perf_counts_values
*counts
;
1682 counts
= perf_counts(counter
->counts
, cpu
, thread
);
1684 printf("%3d %8d %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %s\n",
1685 counter
->cpus
->map
[cpu
],
1686 thread_map__pid(counter
->threads
, thread
),
1691 perf_evsel__name(counter
));
1696 static void process_stat(struct perf_evsel
*counter
, u64 tstamp
)
1698 if (scripting_ops
&& scripting_ops
->process_stat
)
1699 scripting_ops
->process_stat(&stat_config
, counter
, tstamp
);
1701 __process_stat(counter
, tstamp
);
1704 static void process_stat_interval(u64 tstamp
)
1706 if (scripting_ops
&& scripting_ops
->process_stat_interval
)
1707 scripting_ops
->process_stat_interval(tstamp
);
1710 static void setup_scripting(void)
1712 setup_perl_scripting();
1713 setup_python_scripting();
1716 static int flush_scripting(void)
1718 return scripting_ops
? scripting_ops
->flush_script() : 0;
1721 static int cleanup_scripting(void)
1723 pr_debug("\nperf script stopped\n");
1725 return scripting_ops
? scripting_ops
->stop_script() : 0;
1728 static int process_sample_event(struct perf_tool
*tool
,
1729 union perf_event
*event
,
1730 struct perf_sample
*sample
,
1731 struct perf_evsel
*evsel
,
1732 struct machine
*machine
)
1734 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
1735 struct addr_location al
;
1737 if (perf_time__skip_sample(&scr
->ptime
, sample
->time
))
1741 if (sample
->time
< last_timestamp
) {
1742 pr_err("Samples misordered, previous: %" PRIu64
1743 " this: %" PRIu64
"\n", last_timestamp
,
1747 last_timestamp
= sample
->time
;
1751 if (machine__resolve(machine
, &al
, sample
) < 0) {
1752 pr_err("problem processing %d event, skipping it.\n",
1753 event
->header
.type
);
1760 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
))
1764 scripting_ops
->process_event(event
, sample
, evsel
, &al
);
1766 process_event(scr
, sample
, evsel
, &al
, machine
);
1769 addr_location__put(&al
);
1773 static int process_attr(struct perf_tool
*tool
, union perf_event
*event
,
1774 struct perf_evlist
**pevlist
)
1776 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
1777 struct perf_evlist
*evlist
;
1778 struct perf_evsel
*evsel
, *pos
;
1781 err
= perf_event__process_attr(tool
, event
, pevlist
);
1786 evsel
= perf_evlist__last(*pevlist
);
1788 if (evsel
->attr
.type
>= PERF_TYPE_MAX
&&
1789 evsel
->attr
.type
!= PERF_TYPE_SYNTH
)
1792 evlist__for_each_entry(evlist
, pos
) {
1793 if (pos
->attr
.type
== evsel
->attr
.type
&& pos
!= evsel
)
1797 set_print_ip_opts(&evsel
->attr
);
1799 if (evsel
->attr
.sample_type
)
1800 err
= perf_evsel__check_attr(evsel
, scr
->session
);
1805 static int process_comm_event(struct perf_tool
*tool
,
1806 union perf_event
*event
,
1807 struct perf_sample
*sample
,
1808 struct machine
*machine
)
1810 struct thread
*thread
;
1811 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1812 struct perf_session
*session
= script
->session
;
1813 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1816 thread
= machine__findnew_thread(machine
, event
->comm
.pid
, event
->comm
.tid
);
1817 if (thread
== NULL
) {
1818 pr_debug("problem processing COMM event, skipping it.\n");
1822 if (perf_event__process_comm(tool
, event
, sample
, machine
) < 0)
1825 if (!evsel
->attr
.sample_id_all
) {
1828 sample
->tid
= event
->comm
.tid
;
1829 sample
->pid
= event
->comm
.pid
;
1831 perf_sample__fprintf_start(sample
, thread
, evsel
, stdout
);
1832 perf_event__fprintf(event
, stdout
);
1835 thread__put(thread
);
1839 static int process_namespaces_event(struct perf_tool
*tool
,
1840 union perf_event
*event
,
1841 struct perf_sample
*sample
,
1842 struct machine
*machine
)
1844 struct thread
*thread
;
1845 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1846 struct perf_session
*session
= script
->session
;
1847 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1850 thread
= machine__findnew_thread(machine
, event
->namespaces
.pid
,
1851 event
->namespaces
.tid
);
1852 if (thread
== NULL
) {
1853 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1857 if (perf_event__process_namespaces(tool
, event
, sample
, machine
) < 0)
1860 if (!evsel
->attr
.sample_id_all
) {
1863 sample
->tid
= event
->namespaces
.tid
;
1864 sample
->pid
= event
->namespaces
.pid
;
1866 perf_sample__fprintf_start(sample
, thread
, evsel
, stdout
);
1867 perf_event__fprintf(event
, stdout
);
1870 thread__put(thread
);
1874 static int process_fork_event(struct perf_tool
*tool
,
1875 union perf_event
*event
,
1876 struct perf_sample
*sample
,
1877 struct machine
*machine
)
1879 struct thread
*thread
;
1880 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1881 struct perf_session
*session
= script
->session
;
1882 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1884 if (perf_event__process_fork(tool
, event
, sample
, machine
) < 0)
1887 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
1888 if (thread
== NULL
) {
1889 pr_debug("problem processing FORK event, skipping it.\n");
1893 if (!evsel
->attr
.sample_id_all
) {
1895 sample
->time
= event
->fork
.time
;
1896 sample
->tid
= event
->fork
.tid
;
1897 sample
->pid
= event
->fork
.pid
;
1899 perf_sample__fprintf_start(sample
, thread
, evsel
, stdout
);
1900 perf_event__fprintf(event
, stdout
);
1901 thread__put(thread
);
1905 static int process_exit_event(struct perf_tool
*tool
,
1906 union perf_event
*event
,
1907 struct perf_sample
*sample
,
1908 struct machine
*machine
)
1911 struct thread
*thread
;
1912 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1913 struct perf_session
*session
= script
->session
;
1914 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1916 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
1917 if (thread
== NULL
) {
1918 pr_debug("problem processing EXIT event, skipping it.\n");
1922 if (!evsel
->attr
.sample_id_all
) {
1925 sample
->tid
= event
->fork
.tid
;
1926 sample
->pid
= event
->fork
.pid
;
1928 perf_sample__fprintf_start(sample
, thread
, evsel
, stdout
);
1929 perf_event__fprintf(event
, stdout
);
1931 if (perf_event__process_exit(tool
, event
, sample
, machine
) < 0)
1934 thread__put(thread
);
1938 static int process_mmap_event(struct perf_tool
*tool
,
1939 union perf_event
*event
,
1940 struct perf_sample
*sample
,
1941 struct machine
*machine
)
1943 struct thread
*thread
;
1944 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1945 struct perf_session
*session
= script
->session
;
1946 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1948 if (perf_event__process_mmap(tool
, event
, sample
, machine
) < 0)
1951 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
, event
->mmap
.tid
);
1952 if (thread
== NULL
) {
1953 pr_debug("problem processing MMAP event, skipping it.\n");
1957 if (!evsel
->attr
.sample_id_all
) {
1960 sample
->tid
= event
->mmap
.tid
;
1961 sample
->pid
= event
->mmap
.pid
;
1963 perf_sample__fprintf_start(sample
, thread
, evsel
, stdout
);
1964 perf_event__fprintf(event
, stdout
);
1965 thread__put(thread
);
1969 static int process_mmap2_event(struct perf_tool
*tool
,
1970 union perf_event
*event
,
1971 struct perf_sample
*sample
,
1972 struct machine
*machine
)
1974 struct thread
*thread
;
1975 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1976 struct perf_session
*session
= script
->session
;
1977 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1979 if (perf_event__process_mmap2(tool
, event
, sample
, machine
) < 0)
1982 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
, event
->mmap2
.tid
);
1983 if (thread
== NULL
) {
1984 pr_debug("problem processing MMAP2 event, skipping it.\n");
1988 if (!evsel
->attr
.sample_id_all
) {
1991 sample
->tid
= event
->mmap2
.tid
;
1992 sample
->pid
= event
->mmap2
.pid
;
1994 perf_sample__fprintf_start(sample
, thread
, evsel
, stdout
);
1995 perf_event__fprintf(event
, stdout
);
1996 thread__put(thread
);
2000 static int process_switch_event(struct perf_tool
*tool
,
2001 union perf_event
*event
,
2002 struct perf_sample
*sample
,
2003 struct machine
*machine
)
2005 struct thread
*thread
;
2006 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2007 struct perf_session
*session
= script
->session
;
2008 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
2010 if (perf_event__process_switch(tool
, event
, sample
, machine
) < 0)
2013 thread
= machine__findnew_thread(machine
, sample
->pid
,
2015 if (thread
== NULL
) {
2016 pr_debug("problem processing SWITCH event, skipping it.\n");
2020 perf_sample__fprintf_start(sample
, thread
, evsel
, stdout
);
2021 perf_event__fprintf(event
, stdout
);
2022 thread__put(thread
);
2026 static void sig_handler(int sig __maybe_unused
)
2031 static void perf_script__fclose_per_event_dump(struct perf_script
*script
)
2033 struct perf_evlist
*evlist
= script
->session
->evlist
;
2034 struct perf_evsel
*evsel
;
2036 evlist__for_each_entry(evlist
, evsel
) {
2039 perf_evsel_script__delete(evsel
->priv
);
2044 static int perf_script__fopen_per_event_dump(struct perf_script
*script
)
2046 struct perf_evsel
*evsel
;
2048 evlist__for_each_entry(script
->session
->evlist
, evsel
) {
2050 * Already setup? I.e. we may be called twice in cases like
2051 * Intel PT, one for the intel_pt// and dummy events, then
2052 * for the evsels syntheized from the auxtrace info.
2054 * Ses perf_script__process_auxtrace_info.
2056 if (evsel
->priv
!= NULL
)
2059 evsel
->priv
= perf_evsel_script__new(evsel
, script
->session
->data
);
2060 if (evsel
->priv
== NULL
)
2061 goto out_err_fclose
;
2067 perf_script__fclose_per_event_dump(script
);
2071 static int perf_script__setup_per_event_dump(struct perf_script
*script
)
2073 struct perf_evsel
*evsel
;
2074 static struct perf_evsel_script es_stdout
;
2076 if (script
->per_event_dump
)
2077 return perf_script__fopen_per_event_dump(script
);
2079 es_stdout
.fp
= stdout
;
2081 evlist__for_each_entry(script
->session
->evlist
, evsel
)
2082 evsel
->priv
= &es_stdout
;
2087 static void perf_script__exit_per_event_dump_stats(struct perf_script
*script
)
2089 struct perf_evsel
*evsel
;
2091 evlist__for_each_entry(script
->session
->evlist
, evsel
) {
2092 struct perf_evsel_script
*es
= evsel
->priv
;
2094 perf_evsel_script__fprintf(es
, stdout
);
2095 perf_evsel_script__delete(es
);
2100 static int __cmd_script(struct perf_script
*script
)
2104 signal(SIGINT
, sig_handler
);
2106 /* override event processing functions */
2107 if (script
->show_task_events
) {
2108 script
->tool
.comm
= process_comm_event
;
2109 script
->tool
.fork
= process_fork_event
;
2110 script
->tool
.exit
= process_exit_event
;
2112 if (script
->show_mmap_events
) {
2113 script
->tool
.mmap
= process_mmap_event
;
2114 script
->tool
.mmap2
= process_mmap2_event
;
2116 if (script
->show_switch_events
)
2117 script
->tool
.context_switch
= process_switch_event
;
2118 if (script
->show_namespace_events
)
2119 script
->tool
.namespaces
= process_namespaces_event
;
2121 if (perf_script__setup_per_event_dump(script
)) {
2122 pr_err("Couldn't create the per event dump files\n");
2126 ret
= perf_session__process_events(script
->session
);
2128 if (script
->per_event_dump
)
2129 perf_script__exit_per_event_dump_stats(script
);
2132 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
2137 struct script_spec
{
2138 struct list_head node
;
2139 struct scripting_ops
*ops
;
2143 static LIST_HEAD(script_specs
);
2145 static struct script_spec
*script_spec__new(const char *spec
,
2146 struct scripting_ops
*ops
)
2148 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
2151 strcpy(s
->spec
, spec
);
2158 static void script_spec__add(struct script_spec
*s
)
2160 list_add_tail(&s
->node
, &script_specs
);
2163 static struct script_spec
*script_spec__find(const char *spec
)
2165 struct script_spec
*s
;
2167 list_for_each_entry(s
, &script_specs
, node
)
2168 if (strcasecmp(s
->spec
, spec
) == 0)
2173 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
2175 struct script_spec
*s
;
2177 s
= script_spec__find(spec
);
2181 s
= script_spec__new(spec
, ops
);
2185 script_spec__add(s
);
2190 static struct scripting_ops
*script_spec__lookup(const char *spec
)
2192 struct script_spec
*s
= script_spec__find(spec
);
2199 static void list_available_languages(void)
2201 struct script_spec
*s
;
2203 fprintf(stderr
, "\n");
2204 fprintf(stderr
, "Scripting language extensions (used in "
2205 "perf script -s [spec:]script.[spec]):\n\n");
2207 list_for_each_entry(s
, &script_specs
, node
)
2208 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
2210 fprintf(stderr
, "\n");
2213 static int parse_scriptname(const struct option
*opt __maybe_unused
,
2214 const char *str
, int unset __maybe_unused
)
2216 char spec
[PATH_MAX
];
2217 const char *script
, *ext
;
2220 if (strcmp(str
, "lang") == 0) {
2221 list_available_languages();
2225 script
= strchr(str
, ':');
2228 if (len
>= PATH_MAX
) {
2229 fprintf(stderr
, "invalid language specifier");
2232 strncpy(spec
, str
, len
);
2234 scripting_ops
= script_spec__lookup(spec
);
2235 if (!scripting_ops
) {
2236 fprintf(stderr
, "invalid language specifier");
2242 ext
= strrchr(script
, '.');
2244 fprintf(stderr
, "invalid script extension");
2247 scripting_ops
= script_spec__lookup(++ext
);
2248 if (!scripting_ops
) {
2249 fprintf(stderr
, "invalid script extension");
2254 script_name
= strdup(script
);
2259 static int parse_output_fields(const struct option
*opt __maybe_unused
,
2260 const char *arg
, int unset __maybe_unused
)
2262 char *tok
, *strtok_saveptr
= NULL
;
2263 int i
, imax
= ARRAY_SIZE(all_output_options
);
2266 char *str
= strdup(arg
);
2268 enum { DEFAULT
, SET
, ADD
, REMOVE
} change
= DEFAULT
;
2273 /* first word can state for which event type the user is specifying
2274 * the fields. If no type exists, the specified fields apply to all
2275 * event types found in the file minus the invalid fields for a type.
2277 tok
= strchr(str
, ':');
2281 if (!strcmp(str
, "hw"))
2282 type
= PERF_TYPE_HARDWARE
;
2283 else if (!strcmp(str
, "sw"))
2284 type
= PERF_TYPE_SOFTWARE
;
2285 else if (!strcmp(str
, "trace"))
2286 type
= PERF_TYPE_TRACEPOINT
;
2287 else if (!strcmp(str
, "raw"))
2288 type
= PERF_TYPE_RAW
;
2289 else if (!strcmp(str
, "break"))
2290 type
= PERF_TYPE_BREAKPOINT
;
2291 else if (!strcmp(str
, "synth"))
2292 type
= OUTPUT_TYPE_SYNTH
;
2294 fprintf(stderr
, "Invalid event type in field string.\n");
2299 if (output
[type
].user_set
)
2300 pr_warning("Overriding previous field request for %s events.\n",
2303 output
[type
].fields
= 0;
2304 output
[type
].user_set
= true;
2305 output
[type
].wildcard_set
= false;
2309 if (strlen(str
) == 0) {
2311 "Cannot set fields to 'none' for all event types.\n");
2316 /* Don't override defaults for +- */
2317 if (strchr(str
, '+') || strchr(str
, '-'))
2320 if (output_set_by_user())
2321 pr_warning("Overriding previous field request for all events.\n");
2323 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
2324 output
[j
].fields
= 0;
2325 output
[j
].user_set
= true;
2326 output
[j
].wildcard_set
= true;
2331 for (tok
= strtok_r(tok
, ",", &strtok_saveptr
); tok
; tok
= strtok_r(NULL
, ",", &strtok_saveptr
)) {
2337 } else if (*tok
== '-') {
2343 if (change
!= SET
&& change
!= DEFAULT
)
2348 for (i
= 0; i
< imax
; ++i
) {
2349 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
2352 if (i
== imax
&& strcmp(tok
, "flags") == 0) {
2353 print_flags
= change
== REMOVE
? false : true;
2357 fprintf(stderr
, "Invalid field requested.\n");
2363 /* add user option to all events types for
2366 for (j
= 0; j
< OUTPUT_TYPE_MAX
; ++j
) {
2367 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
2368 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2369 all_output_options
[i
].str
, event_type(j
));
2371 if (change
== REMOVE
)
2372 output
[j
].fields
&= ~all_output_options
[i
].field
;
2374 output
[j
].fields
|= all_output_options
[i
].field
;
2378 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
2379 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
2380 all_output_options
[i
].str
, event_type(type
));
2385 output
[type
].fields
|= all_output_options
[i
].field
;
2390 if (output
[type
].fields
== 0) {
2391 pr_debug("No fields requested for %s type. "
2392 "Events will not be displayed.\n", event_type(type
));
2398 fprintf(stderr
, "Cannot mix +-field with overridden fields\n");
2405 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2406 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2407 if ((lang_dirent->d_type == DT_DIR || \
2408 (lang_dirent->d_type == DT_UNKNOWN && \
2409 is_directory(scripts_path, lang_dirent))) && \
2410 (strcmp(lang_dirent->d_name, ".")) && \
2411 (strcmp(lang_dirent->d_name, "..")))
2413 #define for_each_script(lang_path, lang_dir, script_dirent) \
2414 while ((script_dirent = readdir(lang_dir)) != NULL) \
2415 if (script_dirent->d_type != DT_DIR && \
2416 (script_dirent->d_type != DT_UNKNOWN || \
2417 !is_directory(lang_path, script_dirent)))
2420 #define RECORD_SUFFIX "-record"
2421 #define REPORT_SUFFIX "-report"
2423 struct script_desc
{
2424 struct list_head node
;
2430 static LIST_HEAD(script_descs
);
2432 static struct script_desc
*script_desc__new(const char *name
)
2434 struct script_desc
*s
= zalloc(sizeof(*s
));
2436 if (s
!= NULL
&& name
)
2437 s
->name
= strdup(name
);
2442 static void script_desc__delete(struct script_desc
*s
)
2445 zfree(&s
->half_liner
);
2450 static void script_desc__add(struct script_desc
*s
)
2452 list_add_tail(&s
->node
, &script_descs
);
2455 static struct script_desc
*script_desc__find(const char *name
)
2457 struct script_desc
*s
;
2459 list_for_each_entry(s
, &script_descs
, node
)
2460 if (strcasecmp(s
->name
, name
) == 0)
2465 static struct script_desc
*script_desc__findnew(const char *name
)
2467 struct script_desc
*s
= script_desc__find(name
);
2472 s
= script_desc__new(name
);
2476 script_desc__add(s
);
2481 static const char *ends_with(const char *str
, const char *suffix
)
2483 size_t suffix_len
= strlen(suffix
);
2484 const char *p
= str
;
2486 if (strlen(str
) > suffix_len
) {
2487 p
= str
+ strlen(str
) - suffix_len
;
2488 if (!strncmp(p
, suffix
, suffix_len
))
2495 static int read_script_info(struct script_desc
*desc
, const char *filename
)
2497 char line
[BUFSIZ
], *p
;
2500 fp
= fopen(filename
, "r");
2504 while (fgets(line
, sizeof(line
), fp
)) {
2511 if (strlen(p
) && *p
== '!')
2515 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
2516 p
[strlen(p
) - 1] = '\0';
2518 if (!strncmp(p
, "description:", strlen("description:"))) {
2519 p
+= strlen("description:");
2520 desc
->half_liner
= strdup(ltrim(p
));
2524 if (!strncmp(p
, "args:", strlen("args:"))) {
2525 p
+= strlen("args:");
2526 desc
->args
= strdup(ltrim(p
));
2536 static char *get_script_root(struct dirent
*script_dirent
, const char *suffix
)
2538 char *script_root
, *str
;
2540 script_root
= strdup(script_dirent
->d_name
);
2544 str
= (char *)ends_with(script_root
, suffix
);
2554 static int list_available_scripts(const struct option
*opt __maybe_unused
,
2555 const char *s __maybe_unused
,
2556 int unset __maybe_unused
)
2558 struct dirent
*script_dirent
, *lang_dirent
;
2559 char scripts_path
[MAXPATHLEN
];
2560 DIR *scripts_dir
, *lang_dir
;
2561 char script_path
[MAXPATHLEN
];
2562 char lang_path
[MAXPATHLEN
];
2563 struct script_desc
*desc
;
2564 char first_half
[BUFSIZ
];
2567 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
2569 scripts_dir
= opendir(scripts_path
);
2572 "open(%s) failed.\n"
2573 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2578 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
2579 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
2580 lang_dirent
->d_name
);
2581 lang_dir
= opendir(lang_path
);
2585 for_each_script(lang_path
, lang_dir
, script_dirent
) {
2586 script_root
= get_script_root(script_dirent
, REPORT_SUFFIX
);
2588 desc
= script_desc__findnew(script_root
);
2589 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
2590 lang_path
, script_dirent
->d_name
);
2591 read_script_info(desc
, script_path
);
2597 fprintf(stdout
, "List of available trace scripts:\n");
2598 list_for_each_entry(desc
, &script_descs
, node
) {
2599 sprintf(first_half
, "%s %s", desc
->name
,
2600 desc
->args
? desc
->args
: "");
2601 fprintf(stdout
, " %-36s %s\n", first_half
,
2602 desc
->half_liner
? desc
->half_liner
: "");
2609 * Some scripts specify the required events in their "xxx-record" file,
2610 * this function will check if the events in perf.data match those
2611 * mentioned in the "xxx-record".
2613 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2614 * which is covered well now. And new parsing code should be added to
2615 * cover the future complexing formats like event groups etc.
2617 static int check_ev_match(char *dir_name
, char *scriptname
,
2618 struct perf_session
*session
)
2620 char filename
[MAXPATHLEN
], evname
[128];
2621 char line
[BUFSIZ
], *p
;
2622 struct perf_evsel
*pos
;
2626 sprintf(filename
, "%s/bin/%s-record", dir_name
, scriptname
);
2628 fp
= fopen(filename
, "r");
2632 while (fgets(line
, sizeof(line
), fp
)) {
2638 p
= strstr(p
, "-e");
2644 len
= strcspn(p
, " \t");
2648 snprintf(evname
, len
+ 1, "%s", p
);
2651 evlist__for_each_entry(session
->evlist
, pos
) {
2652 if (!strcmp(perf_evsel__name(pos
), evname
)) {
2670 * Return -1 if none is found, otherwise the actual scripts number.
2672 * Currently the only user of this function is the script browser, which
2673 * will list all statically runnable scripts, select one, execute it and
2674 * show the output in a perf browser.
2676 int find_scripts(char **scripts_array
, char **scripts_path_array
)
2678 struct dirent
*script_dirent
, *lang_dirent
;
2679 char scripts_path
[MAXPATHLEN
], lang_path
[MAXPATHLEN
];
2680 DIR *scripts_dir
, *lang_dir
;
2681 struct perf_session
*session
;
2682 struct perf_data data
= {
2686 .mode
= PERF_DATA_MODE_READ
,
2691 session
= perf_session__new(&data
, false, NULL
);
2695 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
2697 scripts_dir
= opendir(scripts_path
);
2699 perf_session__delete(session
);
2703 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
2704 snprintf(lang_path
, MAXPATHLEN
, "%s/%s", scripts_path
,
2705 lang_dirent
->d_name
);
2707 if (strstr(lang_path
, "perl"))
2711 if (strstr(lang_path
, "python"))
2715 lang_dir
= opendir(lang_path
);
2719 for_each_script(lang_path
, lang_dir
, script_dirent
) {
2720 /* Skip those real time scripts: xxxtop.p[yl] */
2721 if (strstr(script_dirent
->d_name
, "top."))
2723 sprintf(scripts_path_array
[i
], "%s/%s", lang_path
,
2724 script_dirent
->d_name
);
2725 temp
= strchr(script_dirent
->d_name
, '.');
2726 snprintf(scripts_array
[i
],
2727 (temp
- script_dirent
->d_name
) + 1,
2728 "%s", script_dirent
->d_name
);
2730 if (check_ev_match(lang_path
,
2731 scripts_array
[i
], session
))
2739 closedir(scripts_dir
);
2740 perf_session__delete(session
);
2744 static char *get_script_path(const char *script_root
, const char *suffix
)
2746 struct dirent
*script_dirent
, *lang_dirent
;
2747 char scripts_path
[MAXPATHLEN
];
2748 char script_path
[MAXPATHLEN
];
2749 DIR *scripts_dir
, *lang_dir
;
2750 char lang_path
[MAXPATHLEN
];
2751 char *__script_root
;
2753 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
2755 scripts_dir
= opendir(scripts_path
);
2759 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
2760 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
2761 lang_dirent
->d_name
);
2762 lang_dir
= opendir(lang_path
);
2766 for_each_script(lang_path
, lang_dir
, script_dirent
) {
2767 __script_root
= get_script_root(script_dirent
, suffix
);
2768 if (__script_root
&& !strcmp(script_root
, __script_root
)) {
2769 free(__script_root
);
2771 closedir(scripts_dir
);
2772 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
2773 lang_path
, script_dirent
->d_name
);
2774 return strdup(script_path
);
2776 free(__script_root
);
2780 closedir(scripts_dir
);
2785 static bool is_top_script(const char *script_path
)
2787 return ends_with(script_path
, "top") == NULL
? false : true;
2790 static int has_required_arg(char *script_path
)
2792 struct script_desc
*desc
;
2796 desc
= script_desc__new(NULL
);
2798 if (read_script_info(desc
, script_path
))
2804 for (p
= desc
->args
; *p
; p
++)
2808 script_desc__delete(desc
);
2813 static int have_cmd(int argc
, const char **argv
)
2815 char **__argv
= malloc(sizeof(const char *) * argc
);
2818 pr_err("malloc failed\n");
2822 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
2823 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
2824 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
2827 system_wide
= (argc
== 0);
2832 static void script__setup_sample_type(struct perf_script
*script
)
2834 struct perf_session
*session
= script
->session
;
2835 u64 sample_type
= perf_evlist__combined_sample_type(session
->evlist
);
2837 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
) {
2838 if ((sample_type
& PERF_SAMPLE_REGS_USER
) &&
2839 (sample_type
& PERF_SAMPLE_STACK_USER
))
2840 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
2841 else if (sample_type
& PERF_SAMPLE_BRANCH_STACK
)
2842 callchain_param
.record_mode
= CALLCHAIN_LBR
;
2844 callchain_param
.record_mode
= CALLCHAIN_FP
;
2848 static int process_stat_round_event(struct perf_tool
*tool __maybe_unused
,
2849 union perf_event
*event
,
2850 struct perf_session
*session
)
2852 struct stat_round_event
*round
= &event
->stat_round
;
2853 struct perf_evsel
*counter
;
2855 evlist__for_each_entry(session
->evlist
, counter
) {
2856 perf_stat_process_counter(&stat_config
, counter
);
2857 process_stat(counter
, round
->time
);
2860 process_stat_interval(round
->time
);
2864 static int process_stat_config_event(struct perf_tool
*tool __maybe_unused
,
2865 union perf_event
*event
,
2866 struct perf_session
*session __maybe_unused
)
2868 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
2872 static int set_maps(struct perf_script
*script
)
2874 struct perf_evlist
*evlist
= script
->session
->evlist
;
2876 if (!script
->cpus
|| !script
->threads
)
2879 if (WARN_ONCE(script
->allocated
, "stats double allocation\n"))
2882 perf_evlist__set_maps(evlist
, script
->cpus
, script
->threads
);
2884 if (perf_evlist__alloc_stats(evlist
, true))
2887 script
->allocated
= true;
2892 int process_thread_map_event(struct perf_tool
*tool
,
2893 union perf_event
*event
,
2894 struct perf_session
*session __maybe_unused
)
2896 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2898 if (script
->threads
) {
2899 pr_warning("Extra thread map event, ignoring.\n");
2903 script
->threads
= thread_map__new_event(&event
->thread_map
);
2904 if (!script
->threads
)
2907 return set_maps(script
);
2911 int process_cpu_map_event(struct perf_tool
*tool __maybe_unused
,
2912 union perf_event
*event
,
2913 struct perf_session
*session __maybe_unused
)
2915 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2918 pr_warning("Extra cpu map event, ignoring.\n");
2922 script
->cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
2926 return set_maps(script
);
2929 #ifdef HAVE_AUXTRACE_SUPPORT
2930 static int perf_script__process_auxtrace_info(struct perf_tool
*tool
,
2931 union perf_event
*event
,
2932 struct perf_session
*session
)
2934 int ret
= perf_event__process_auxtrace_info(tool
, event
, session
);
2937 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2939 ret
= perf_script__setup_per_event_dump(script
);
2945 #define perf_script__process_auxtrace_info 0
2948 int cmd_script(int argc
, const char **argv
)
2950 bool show_full_info
= false;
2951 bool header
= false;
2952 bool header_only
= false;
2953 bool script_started
= false;
2954 char *rec_script_path
= NULL
;
2955 char *rep_script_path
= NULL
;
2956 struct perf_session
*session
;
2957 struct itrace_synth_opts itrace_synth_opts
= { .set
= false, };
2958 char *script_path
= NULL
;
2959 const char **__argv
;
2961 struct perf_script script
= {
2963 .sample
= process_sample_event
,
2964 .mmap
= perf_event__process_mmap
,
2965 .mmap2
= perf_event__process_mmap2
,
2966 .comm
= perf_event__process_comm
,
2967 .namespaces
= perf_event__process_namespaces
,
2968 .exit
= perf_event__process_exit
,
2969 .fork
= perf_event__process_fork
,
2970 .attr
= process_attr
,
2971 .event_update
= perf_event__process_event_update
,
2972 .tracing_data
= perf_event__process_tracing_data
,
2973 .feature
= perf_event__process_feature
,
2974 .build_id
= perf_event__process_build_id
,
2975 .id_index
= perf_event__process_id_index
,
2976 .auxtrace_info
= perf_script__process_auxtrace_info
,
2977 .auxtrace
= perf_event__process_auxtrace
,
2978 .auxtrace_error
= perf_event__process_auxtrace_error
,
2979 .stat
= perf_event__process_stat_event
,
2980 .stat_round
= process_stat_round_event
,
2981 .stat_config
= process_stat_config_event
,
2982 .thread_map
= process_thread_map_event
,
2983 .cpu_map
= process_cpu_map_event
,
2984 .ordered_events
= true,
2985 .ordering_requires_timestamps
= true,
2988 struct perf_data data
= {
2989 .mode
= PERF_DATA_MODE_READ
,
2991 const struct option options
[] = {
2992 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
2993 "dump raw trace in ASCII"),
2994 OPT_INCR('v', "verbose", &verbose
,
2995 "be more verbose (show symbol address, etc)"),
2996 OPT_BOOLEAN('L', "Latency", &latency_format
,
2997 "show latency attributes (irqs/preemption disabled, etc)"),
2998 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
2999 list_available_scripts
),
3000 OPT_CALLBACK('s', "script", NULL
, "name",
3001 "script file name (lang:script name, script name, or *)",
3003 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
3004 "generate perf-script.xx script in specified language"),
3005 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
3006 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
3007 "do various checks like samples ordering and lost events"),
3008 OPT_BOOLEAN(0, "header", &header
, "Show data header."),
3009 OPT_BOOLEAN(0, "header-only", &header_only
, "Show only data header."),
3010 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
3011 "file", "vmlinux pathname"),
3012 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
3013 "file", "kallsyms pathname"),
3014 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
3015 "When printing symbols do not display call chain"),
3016 OPT_CALLBACK(0, "symfs", NULL
, "directory",
3017 "Look for files with symbols relative to this directory",
3018 symbol__config_symfs
),
3019 OPT_CALLBACK('F', "fields", NULL
, "str",
3020 "comma separated output fields prepend with 'type:'. "
3021 "+field to add and -field to remove."
3022 "Valid types: hw,sw,trace,raw,synth. "
3023 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
3024 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
3025 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
3026 parse_output_fields
),
3027 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
3028 "system-wide collection from all CPUs"),
3029 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
3030 "only consider these symbols"),
3031 OPT_STRING(0, "stop-bt", &symbol_conf
.bt_stop_list_str
, "symbol[,symbol...]",
3032 "Stop display of callgraph at these symbols"),
3033 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
3034 OPT_STRING('c', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
3035 "only display events for these comms"),
3036 OPT_STRING(0, "pid", &symbol_conf
.pid_list_str
, "pid[,pid...]",
3037 "only consider symbols in these pids"),
3038 OPT_STRING(0, "tid", &symbol_conf
.tid_list_str
, "tid[,tid...]",
3039 "only consider symbols in these tids"),
3040 OPT_UINTEGER(0, "max-stack", &scripting_max_stack
,
3041 "Set the maximum stack depth when parsing the callchain, "
3042 "anything beyond the specified depth will be ignored. "
3043 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH
)),
3044 OPT_BOOLEAN('I', "show-info", &show_full_info
,
3045 "display extended information from perf.data file"),
3046 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf
.show_kernel_path
,
3047 "Show the path of [kernel.kallsyms]"),
3048 OPT_BOOLEAN('\0', "show-task-events", &script
.show_task_events
,
3049 "Show the fork/comm/exit events"),
3050 OPT_BOOLEAN('\0', "show-mmap-events", &script
.show_mmap_events
,
3051 "Show the mmap events"),
3052 OPT_BOOLEAN('\0', "show-switch-events", &script
.show_switch_events
,
3053 "Show context switch events (if recorded)"),
3054 OPT_BOOLEAN('\0', "show-namespace-events", &script
.show_namespace_events
,
3055 "Show namespace events (if recorded)"),
3056 OPT_BOOLEAN('\0', "per-event-dump", &script
.per_event_dump
,
3057 "Dump trace output to files named by the monitored events"),
3058 OPT_BOOLEAN('f', "force", &symbol_conf
.force
, "don't complain, do it"),
3059 OPT_INTEGER(0, "max-blocks", &max_blocks
,
3060 "Maximum number of code blocks to dump with brstackinsn"),
3061 OPT_BOOLEAN(0, "ns", &nanosecs
,
3062 "Use 9 decimal places when displaying time"),
3063 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts
, NULL
, "opts",
3064 "Instruction Tracing options",
3065 itrace_parse_synth_opts
),
3066 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename
,
3067 "Show full source file name path for source lines"),
3068 OPT_BOOLEAN(0, "demangle", &symbol_conf
.demangle
,
3069 "Enable symbol demangling"),
3070 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf
.demangle_kernel
,
3071 "Enable kernel symbol demangling"),
3072 OPT_STRING(0, "time", &script
.time_str
, "str",
3073 "Time span of interest (start,stop)"),
3074 OPT_BOOLEAN(0, "inline", &symbol_conf
.inline_name
,
3075 "Show inline function"),
3078 const char * const script_subcommands
[] = { "record", "report", NULL
};
3079 const char *script_usage
[] = {
3080 "perf script [<options>]",
3081 "perf script [<options>] record <script> [<record-options>] <command>",
3082 "perf script [<options>] report <script> [script-args]",
3083 "perf script [<options>] <script> [<record-options>] <command>",
3084 "perf script [<options>] <top-script> [script-args]",
3088 perf_set_singlethreaded();
3092 argc
= parse_options_subcommand(argc
, argv
, options
, script_subcommands
, script_usage
,
3093 PARSE_OPT_STOP_AT_NON_OPTION
);
3095 data
.file
.path
= input_name
;
3096 data
.force
= symbol_conf
.force
;
3098 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
3099 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
3100 if (!rec_script_path
)
3101 return cmd_record(argc
, argv
);
3104 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
3105 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
3106 if (!rep_script_path
) {
3108 "Please specify a valid report script"
3109 "(see 'perf script -l' for listing)\n");
3114 if (itrace_synth_opts
.callchain
&&
3115 itrace_synth_opts
.callchain_sz
> scripting_max_stack
)
3116 scripting_max_stack
= itrace_synth_opts
.callchain_sz
;
3118 /* make sure PERF_EXEC_PATH is set for scripts */
3119 set_argv_exec_path(get_argv_exec_path());
3121 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
3126 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
3127 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
3129 if (!rec_script_path
&& !rep_script_path
) {
3130 usage_with_options_msg(script_usage
, options
,
3131 "Couldn't find script `%s'\n\n See perf"
3132 " script -l for available scripts.\n", argv
[0]);
3135 if (is_top_script(argv
[0])) {
3136 rep_args
= argc
- 1;
3140 rep_args
= has_required_arg(rep_script_path
);
3141 rec_args
= (argc
- 1) - rep_args
;
3143 usage_with_options_msg(script_usage
, options
,
3144 "`%s' script requires options."
3145 "\n\n See perf script -l for available "
3146 "scripts and options.\n", argv
[0]);
3150 if (pipe(live_pipe
) < 0) {
3151 perror("failed to create pipe");
3157 perror("failed to fork");
3164 dup2(live_pipe
[1], 1);
3165 close(live_pipe
[0]);
3167 if (is_top_script(argv
[0])) {
3169 } else if (!system_wide
) {
3170 if (have_cmd(argc
- rep_args
, &argv
[rep_args
]) != 0) {
3176 __argv
= malloc((argc
+ 6) * sizeof(const char *));
3178 pr_err("malloc failed\n");
3183 __argv
[j
++] = "/bin/sh";
3184 __argv
[j
++] = rec_script_path
;
3190 for (i
= rep_args
+ 1; i
< argc
; i
++)
3191 __argv
[j
++] = argv
[i
];
3194 execvp("/bin/sh", (char **)__argv
);
3199 dup2(live_pipe
[0], 0);
3200 close(live_pipe
[1]);
3202 __argv
= malloc((argc
+ 4) * sizeof(const char *));
3204 pr_err("malloc failed\n");
3210 __argv
[j
++] = "/bin/sh";
3211 __argv
[j
++] = rep_script_path
;
3212 for (i
= 1; i
< rep_args
+ 1; i
++)
3213 __argv
[j
++] = argv
[i
];
3218 execvp("/bin/sh", (char **)__argv
);
3223 if (rec_script_path
)
3224 script_path
= rec_script_path
;
3225 if (rep_script_path
)
3226 script_path
= rep_script_path
;
3231 if (!rec_script_path
)
3232 system_wide
= false;
3233 else if (!system_wide
) {
3234 if (have_cmd(argc
- 1, &argv
[1]) != 0) {
3240 __argv
= malloc((argc
+ 2) * sizeof(const char *));
3242 pr_err("malloc failed\n");
3247 __argv
[j
++] = "/bin/sh";
3248 __argv
[j
++] = script_path
;
3251 for (i
= 2; i
< argc
; i
++)
3252 __argv
[j
++] = argv
[i
];
3255 execvp("/bin/sh", (char **)__argv
);
3263 session
= perf_session__new(&data
, false, &script
.tool
);
3264 if (session
== NULL
)
3267 if (header
|| header_only
) {
3268 script
.tool
.show_feat_hdr
= SHOW_FEAT_HEADER
;
3269 perf_session__fprintf_info(session
, stdout
, show_full_info
);
3274 script
.tool
.show_feat_hdr
= SHOW_FEAT_HEADER_FULL_INFO
;
3276 if (symbol__init(&session
->header
.env
) < 0)
3279 script
.session
= session
;
3280 script__setup_sample_type(&script
);
3282 if (output
[PERF_TYPE_HARDWARE
].fields
& PERF_OUTPUT_CALLINDENT
)
3283 itrace_synth_opts
.thread_stack
= true;
3285 session
->itrace_synth_opts
= &itrace_synth_opts
;
3288 err
= perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
);
3291 itrace_synth_opts
.cpu_bitmap
= cpu_bitmap
;
3295 symbol_conf
.use_callchain
= true;
3297 symbol_conf
.use_callchain
= false;
3299 if (session
->tevent
.pevent
&&
3300 pevent_set_function_resolver(session
->tevent
.pevent
,
3301 machine__resolve_kernel_addr
,
3302 &session
->machines
.host
) < 0) {
3303 pr_err("%s: failed to set libtraceevent function resolver\n", __func__
);
3308 if (generate_script_lang
) {
3309 struct stat perf_stat
;
3312 if (output_set_by_user()) {
3314 "custom fields not supported for generated scripts");
3319 input
= open(data
.file
.path
, O_RDONLY
); /* input_name */
3322 perror("failed to open file");
3326 err
= fstat(input
, &perf_stat
);
3328 perror("failed to stat file");
3332 if (!perf_stat
.st_size
) {
3333 fprintf(stderr
, "zero-sized file, nothing to do!\n");
3337 scripting_ops
= script_spec__lookup(generate_script_lang
);
3338 if (!scripting_ops
) {
3339 fprintf(stderr
, "invalid language specifier");
3344 err
= scripting_ops
->generate_script(session
->tevent
.pevent
,
3350 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
3353 pr_debug("perf script started with script %s\n\n", script_name
);
3354 script_started
= true;
3358 err
= perf_session__check_output_opt(session
);
3362 /* needs to be parsed after looking up reference time */
3363 if (perf_time__parse_str(&script
.ptime
, script
.time_str
) != 0) {
3364 pr_err("Invalid time string\n");
3369 err
= __cmd_script(&script
);
3374 perf_evlist__free_stats(session
->evlist
);
3375 perf_session__delete(session
);
3378 cleanup_scripting();