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/thread-stack.h"
25 #include <linux/bitmap.h>
26 #include <linux/stringify.h>
27 #include <linux/time64.h>
29 #include "util/mem-events.h"
31 static char const *script_name
;
32 static char const *generate_script_lang
;
33 static bool debug_mode
;
34 static u64 last_timestamp
;
35 static u64 nr_unordered
;
36 static bool no_callchain
;
37 static bool latency_format
;
38 static bool system_wide
;
39 static bool print_flags
;
41 static const char *cpu_list
;
42 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
43 static struct perf_stat_config stat_config
;
45 unsigned int scripting_max_stack
= PERF_MAX_STACK_DEPTH
;
47 enum perf_output_field
{
48 PERF_OUTPUT_COMM
= 1U << 0,
49 PERF_OUTPUT_TID
= 1U << 1,
50 PERF_OUTPUT_PID
= 1U << 2,
51 PERF_OUTPUT_TIME
= 1U << 3,
52 PERF_OUTPUT_CPU
= 1U << 4,
53 PERF_OUTPUT_EVNAME
= 1U << 5,
54 PERF_OUTPUT_TRACE
= 1U << 6,
55 PERF_OUTPUT_IP
= 1U << 7,
56 PERF_OUTPUT_SYM
= 1U << 8,
57 PERF_OUTPUT_DSO
= 1U << 9,
58 PERF_OUTPUT_ADDR
= 1U << 10,
59 PERF_OUTPUT_SYMOFFSET
= 1U << 11,
60 PERF_OUTPUT_SRCLINE
= 1U << 12,
61 PERF_OUTPUT_PERIOD
= 1U << 13,
62 PERF_OUTPUT_IREGS
= 1U << 14,
63 PERF_OUTPUT_BRSTACK
= 1U << 15,
64 PERF_OUTPUT_BRSTACKSYM
= 1U << 16,
65 PERF_OUTPUT_DATA_SRC
= 1U << 17,
66 PERF_OUTPUT_WEIGHT
= 1U << 18,
67 PERF_OUTPUT_BPF_OUTPUT
= 1U << 19,
68 PERF_OUTPUT_CALLINDENT
= 1U << 20,
71 struct output_option
{
73 enum perf_output_field field
;
74 } all_output_options
[] = {
75 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
76 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
77 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
78 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
79 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
80 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
81 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
82 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
83 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
84 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
85 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
86 {.str
= "symoff", .field
= PERF_OUTPUT_SYMOFFSET
},
87 {.str
= "srcline", .field
= PERF_OUTPUT_SRCLINE
},
88 {.str
= "period", .field
= PERF_OUTPUT_PERIOD
},
89 {.str
= "iregs", .field
= PERF_OUTPUT_IREGS
},
90 {.str
= "brstack", .field
= PERF_OUTPUT_BRSTACK
},
91 {.str
= "brstacksym", .field
= PERF_OUTPUT_BRSTACKSYM
},
92 {.str
= "data_src", .field
= PERF_OUTPUT_DATA_SRC
},
93 {.str
= "weight", .field
= PERF_OUTPUT_WEIGHT
},
94 {.str
= "bpf-output", .field
= PERF_OUTPUT_BPF_OUTPUT
},
95 {.str
= "callindent", .field
= PERF_OUTPUT_CALLINDENT
},
98 /* default set to maintain compatibility with current format */
102 unsigned int print_ip_opts
;
105 } output
[PERF_TYPE_MAX
] = {
107 [PERF_TYPE_HARDWARE
] = {
110 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
111 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
112 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
113 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
116 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
119 [PERF_TYPE_SOFTWARE
] = {
122 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
123 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
124 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
125 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
126 PERF_OUTPUT_PERIOD
| PERF_OUTPUT_BPF_OUTPUT
,
128 .invalid_fields
= PERF_OUTPUT_TRACE
,
131 [PERF_TYPE_TRACEPOINT
] = {
134 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
135 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
136 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
142 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
143 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
144 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
145 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
146 PERF_OUTPUT_PERIOD
| PERF_OUTPUT_ADDR
|
147 PERF_OUTPUT_DATA_SRC
| PERF_OUTPUT_WEIGHT
,
149 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
152 [PERF_TYPE_BREAKPOINT
] = {
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
|
161 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
165 static bool output_set_by_user(void)
168 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
169 if (output
[j
].user_set
)
175 static const char *output_field2str(enum perf_output_field field
)
177 int i
, imax
= ARRAY_SIZE(all_output_options
);
178 const char *str
= "";
180 for (i
= 0; i
< imax
; ++i
) {
181 if (all_output_options
[i
].field
== field
) {
182 str
= all_output_options
[i
].str
;
189 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
191 static int perf_evsel__do_check_stype(struct perf_evsel
*evsel
,
192 u64 sample_type
, const char *sample_msg
,
193 enum perf_output_field field
,
196 struct perf_event_attr
*attr
= &evsel
->attr
;
197 int type
= attr
->type
;
200 if (attr
->sample_type
& sample_type
)
203 if (output
[type
].user_set
) {
206 evname
= perf_evsel__name(evsel
);
207 pr_err("Samples for '%s' event do not have %s attribute set. "
208 "Cannot print '%s' field.\n",
209 evname
, sample_msg
, output_field2str(field
));
213 /* user did not ask for it explicitly so remove from the default list */
214 output
[type
].fields
&= ~field
;
215 evname
= perf_evsel__name(evsel
);
216 pr_debug("Samples for '%s' event do not have %s attribute set. "
217 "Skipping '%s' field.\n",
218 evname
, sample_msg
, output_field2str(field
));
223 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
224 u64 sample_type
, const char *sample_msg
,
225 enum perf_output_field field
)
227 return perf_evsel__do_check_stype(evsel
, sample_type
, sample_msg
, field
,
231 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
232 struct perf_session
*session
)
234 struct perf_event_attr
*attr
= &evsel
->attr
;
237 if (perf_header__has_feat(&session
->header
, HEADER_STAT
))
240 allow_user_set
= perf_header__has_feat(&session
->header
,
243 if (PRINT_FIELD(TRACE
) &&
244 !perf_session__has_traces(session
, "record -R"))
247 if (PRINT_FIELD(IP
)) {
248 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_IP
, "IP",
253 if (PRINT_FIELD(ADDR
) &&
254 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_ADDR
, "ADDR",
255 PERF_OUTPUT_ADDR
, allow_user_set
))
258 if (PRINT_FIELD(DATA_SRC
) &&
259 perf_evsel__check_stype(evsel
, PERF_SAMPLE_DATA_SRC
, "DATA_SRC",
260 PERF_OUTPUT_DATA_SRC
))
263 if (PRINT_FIELD(WEIGHT
) &&
264 perf_evsel__check_stype(evsel
, PERF_SAMPLE_WEIGHT
, "WEIGHT",
268 if (PRINT_FIELD(SYM
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
269 pr_err("Display of symbols requested but neither sample IP nor "
270 "sample address\nis selected. Hence, no addresses to convert "
274 if (PRINT_FIELD(SYMOFFSET
) && !PRINT_FIELD(SYM
)) {
275 pr_err("Display of offsets requested but symbol is not"
279 if (PRINT_FIELD(DSO
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
280 pr_err("Display of DSO requested but neither sample IP nor "
281 "sample address\nis selected. Hence, no addresses to convert "
285 if (PRINT_FIELD(SRCLINE
) && !PRINT_FIELD(IP
)) {
286 pr_err("Display of source line number requested but sample IP is not\n"
287 "selected. Hence, no address to lookup the source line number.\n");
291 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
292 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID",
293 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
296 if (PRINT_FIELD(TIME
) &&
297 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TIME
, "TIME",
301 if (PRINT_FIELD(CPU
) &&
302 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_CPU
, "CPU",
303 PERF_OUTPUT_CPU
, allow_user_set
))
306 if (PRINT_FIELD(PERIOD
) &&
307 perf_evsel__check_stype(evsel
, PERF_SAMPLE_PERIOD
, "PERIOD",
311 if (PRINT_FIELD(IREGS
) &&
312 perf_evsel__check_stype(evsel
, PERF_SAMPLE_REGS_INTR
, "IREGS",
319 static void set_print_ip_opts(struct perf_event_attr
*attr
)
321 unsigned int type
= attr
->type
;
323 output
[type
].print_ip_opts
= 0;
325 output
[type
].print_ip_opts
|= EVSEL__PRINT_IP
;
327 if (PRINT_FIELD(SYM
))
328 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYM
;
330 if (PRINT_FIELD(DSO
))
331 output
[type
].print_ip_opts
|= EVSEL__PRINT_DSO
;
333 if (PRINT_FIELD(SYMOFFSET
))
334 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYMOFFSET
;
336 if (PRINT_FIELD(SRCLINE
))
337 output
[type
].print_ip_opts
|= EVSEL__PRINT_SRCLINE
;
341 * verify all user requested events exist and the samples
342 * have the expected data
344 static int perf_session__check_output_opt(struct perf_session
*session
)
347 struct perf_evsel
*evsel
;
349 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
350 evsel
= perf_session__find_first_evtype(session
, j
);
353 * even if fields is set to 0 (ie., show nothing) event must
354 * exist if user explicitly includes it on the command line
356 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
) {
357 pr_err("%s events do not exist. "
358 "Remove corresponding -f option to proceed.\n",
363 if (evsel
&& output
[j
].fields
&&
364 perf_evsel__check_attr(evsel
, session
))
370 set_print_ip_opts(&evsel
->attr
);
374 bool use_callchain
= false;
375 bool not_pipe
= false;
377 evlist__for_each_entry(session
->evlist
, evsel
) {
379 if (evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
380 use_callchain
= true;
384 if (not_pipe
&& !use_callchain
)
385 symbol_conf
.use_callchain
= false;
389 * set default for tracepoints to print symbols only
390 * if callchains are present
392 if (symbol_conf
.use_callchain
&&
393 !output
[PERF_TYPE_TRACEPOINT
].user_set
) {
394 struct perf_event_attr
*attr
;
396 j
= PERF_TYPE_TRACEPOINT
;
398 evlist__for_each_entry(session
->evlist
, evsel
) {
399 if (evsel
->attr
.type
!= j
)
404 if (attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
405 output
[j
].fields
|= PERF_OUTPUT_IP
;
406 output
[j
].fields
|= PERF_OUTPUT_SYM
;
407 output
[j
].fields
|= PERF_OUTPUT_DSO
;
408 set_print_ip_opts(attr
);
418 static void print_sample_iregs(struct perf_sample
*sample
,
419 struct perf_event_attr
*attr
)
421 struct regs_dump
*regs
= &sample
->intr_regs
;
422 uint64_t mask
= attr
->sample_regs_intr
;
428 for_each_set_bit(r
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
429 u64 val
= regs
->regs
[i
++];
430 printf("%5s:0x%"PRIx64
" ", perf_reg_name(r
), val
);
434 static void print_sample_start(struct perf_sample
*sample
,
435 struct thread
*thread
,
436 struct perf_evsel
*evsel
)
438 struct perf_event_attr
*attr
= &evsel
->attr
;
441 unsigned long long nsecs
;
443 if (PRINT_FIELD(COMM
)) {
445 printf("%8.8s ", thread__comm_str(thread
));
446 else if (PRINT_FIELD(IP
) && symbol_conf
.use_callchain
)
447 printf("%s ", thread__comm_str(thread
));
449 printf("%16s ", thread__comm_str(thread
));
452 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
453 printf("%5d/%-5d ", sample
->pid
, sample
->tid
);
454 else if (PRINT_FIELD(PID
))
455 printf("%5d ", sample
->pid
);
456 else if (PRINT_FIELD(TID
))
457 printf("%5d ", sample
->tid
);
459 if (PRINT_FIELD(CPU
)) {
461 printf("%3d ", sample
->cpu
);
463 printf("[%03d] ", sample
->cpu
);
466 if (PRINT_FIELD(TIME
)) {
467 nsecs
= sample
->time
;
468 secs
= nsecs
/ NSEC_PER_SEC
;
469 nsecs
-= secs
* NSEC_PER_SEC
;
470 usecs
= nsecs
/ NSEC_PER_USEC
;
472 printf("%5lu.%09llu: ", secs
, nsecs
);
474 printf("%5lu.%06lu: ", secs
, usecs
);
479 mispred_str(struct branch_entry
*br
)
481 if (!(br
->flags
.mispred
|| br
->flags
.predicted
))
484 return br
->flags
.predicted
? 'P' : 'M';
487 static void print_sample_brstack(struct perf_sample
*sample
)
489 struct branch_stack
*br
= sample
->branch_stack
;
495 for (i
= 0; i
< br
->nr
; i
++) {
496 printf(" 0x%"PRIx64
"/0x%"PRIx64
"/%c/%c/%c/%d ",
499 mispred_str( br
->entries
+ i
),
500 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
501 br
->entries
[i
].flags
.abort
? 'A' : '-',
502 br
->entries
[i
].flags
.cycles
);
506 static void print_sample_brstacksym(struct perf_sample
*sample
,
507 struct thread
*thread
)
509 struct branch_stack
*br
= sample
->branch_stack
;
510 struct addr_location alf
, alt
;
516 for (i
= 0; i
< br
->nr
; i
++) {
518 memset(&alf
, 0, sizeof(alf
));
519 memset(&alt
, 0, sizeof(alt
));
520 from
= br
->entries
[i
].from
;
521 to
= br
->entries
[i
].to
;
523 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, from
, &alf
);
525 alf
.sym
= map__find_symbol(alf
.map
, alf
.addr
);
527 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, to
, &alt
);
529 alt
.sym
= map__find_symbol(alt
.map
, alt
.addr
);
531 symbol__fprintf_symname_offs(alf
.sym
, &alf
, stdout
);
533 symbol__fprintf_symname_offs(alt
.sym
, &alt
, stdout
);
534 printf("/%c/%c/%c/%d ",
535 mispred_str( br
->entries
+ i
),
536 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
537 br
->entries
[i
].flags
.abort
? 'A' : '-',
538 br
->entries
[i
].flags
.cycles
);
543 static void print_sample_addr(struct perf_sample
*sample
,
544 struct thread
*thread
,
545 struct perf_event_attr
*attr
)
547 struct addr_location al
;
549 printf("%16" PRIx64
, sample
->addr
);
551 if (!sample_addr_correlates_sym(attr
))
554 thread__resolve(thread
, &al
, sample
);
556 if (PRINT_FIELD(SYM
)) {
558 if (PRINT_FIELD(SYMOFFSET
))
559 symbol__fprintf_symname_offs(al
.sym
, &al
, stdout
);
561 symbol__fprintf_symname(al
.sym
, stdout
);
564 if (PRINT_FIELD(DSO
)) {
566 map__fprintf_dsoname(al
.map
, stdout
);
571 static void print_sample_callindent(struct perf_sample
*sample
,
572 struct perf_evsel
*evsel
,
573 struct thread
*thread
,
574 struct addr_location
*al
)
576 struct perf_event_attr
*attr
= &evsel
->attr
;
577 size_t depth
= thread_stack__depth(thread
);
578 struct addr_location addr_al
;
579 const char *name
= NULL
;
585 * The 'return' has already been popped off the stack so the depth has
586 * to be adjusted to match the 'call'.
588 if (thread
->ts
&& sample
->flags
& PERF_IP_FLAG_RETURN
)
591 if (sample
->flags
& (PERF_IP_FLAG_CALL
| PERF_IP_FLAG_TRACE_BEGIN
)) {
592 if (sample_addr_correlates_sym(attr
)) {
593 thread__resolve(thread
, &addr_al
, sample
);
595 name
= addr_al
.sym
->name
;
601 } else if (sample
->flags
& (PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_TRACE_END
)) {
603 name
= al
->sym
->name
;
609 len
= printf("%*s%s", (int)depth
* 4, "", name
);
611 len
= printf("%*s%16" PRIx64
, (int)depth
* 4, "", ip
);
617 * Try to keep the output length from changing frequently so that the
618 * output lines up more nicely.
620 if (len
> spacing
|| (len
&& len
< spacing
- 52))
621 spacing
= round_up(len
+ 4, 32);
624 printf("%*s", spacing
- len
, "");
627 static void print_sample_bts(struct perf_sample
*sample
,
628 struct perf_evsel
*evsel
,
629 struct thread
*thread
,
630 struct addr_location
*al
)
632 struct perf_event_attr
*attr
= &evsel
->attr
;
633 bool print_srcline_last
= false;
635 if (PRINT_FIELD(CALLINDENT
))
636 print_sample_callindent(sample
, evsel
, thread
, al
);
638 /* print branch_from information */
639 if (PRINT_FIELD(IP
)) {
640 unsigned int print_opts
= output
[attr
->type
].print_ip_opts
;
641 struct callchain_cursor
*cursor
= NULL
;
643 if (symbol_conf
.use_callchain
&& sample
->callchain
&&
644 thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
645 sample
, NULL
, NULL
, scripting_max_stack
) == 0)
646 cursor
= &callchain_cursor
;
648 if (cursor
== NULL
) {
650 if (print_opts
& EVSEL__PRINT_SRCLINE
) {
651 print_srcline_last
= true;
652 print_opts
&= ~EVSEL__PRINT_SRCLINE
;
657 sample__fprintf_sym(sample
, al
, 0, print_opts
, cursor
, stdout
);
660 /* print branch_to information */
661 if (PRINT_FIELD(ADDR
) ||
662 ((evsel
->attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
663 !output
[attr
->type
].user_set
)) {
665 print_sample_addr(sample
, thread
, attr
);
668 if (print_srcline_last
)
669 map__fprintf_srcline(al
->map
, al
->addr
, "\n ", stdout
);
678 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
, "call"},
679 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
, "return"},
680 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CONDITIONAL
, "jcc"},
681 {PERF_IP_FLAG_BRANCH
, "jmp"},
682 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_INTERRUPT
, "int"},
683 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_INTERRUPT
, "iret"},
684 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_SYSCALLRET
, "syscall"},
685 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_SYSCALLRET
, "sysret"},
686 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_ASYNC
, "async"},
687 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_ASYNC
| PERF_IP_FLAG_INTERRUPT
, "hw int"},
688 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TX_ABORT
, "tx abrt"},
689 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TRACE_BEGIN
, "tr strt"},
690 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TRACE_END
, "tr end"},
694 static void print_sample_flags(u32 flags
)
696 const char *chars
= PERF_IP_FLAG_CHARS
;
697 const int n
= strlen(PERF_IP_FLAG_CHARS
);
698 bool in_tx
= flags
& PERF_IP_FLAG_IN_TX
;
699 const char *name
= NULL
;
703 for (i
= 0; sample_flags
[i
].name
; i
++) {
704 if (sample_flags
[i
].flags
== (flags
& ~PERF_IP_FLAG_IN_TX
)) {
705 name
= sample_flags
[i
].name
;
710 for (i
= 0; i
< n
; i
++, flags
>>= 1) {
712 str
[pos
++] = chars
[i
];
714 for (; i
< 32; i
++, flags
>>= 1) {
721 printf(" %-7s%4s ", name
, in_tx
? "(x)" : "");
723 printf(" %-11s ", str
);
726 struct printer_data
{
733 print_sample_bpf_output_printer(enum binary_printer_ops op
,
737 unsigned char ch
= (unsigned char)val
;
738 struct printer_data
*printer_data
= extra
;
741 case BINARY_PRINT_DATA_BEGIN
:
744 case BINARY_PRINT_LINE_BEGIN
:
745 printf("%17s", !printer_data
->line_no
? "BPF output:" :
748 case BINARY_PRINT_ADDR
:
749 printf(" %04x:", val
);
751 case BINARY_PRINT_NUM_DATA
:
752 printf(" %02x", val
);
754 case BINARY_PRINT_NUM_PAD
:
757 case BINARY_PRINT_SEP
:
760 case BINARY_PRINT_CHAR_DATA
:
761 if (printer_data
->hit_nul
&& ch
)
762 printer_data
->is_printable
= false;
767 if (!printer_data
->is_printable
)
771 printer_data
->hit_nul
= true;
773 printer_data
->is_printable
= false;
778 case BINARY_PRINT_CHAR_PAD
:
781 case BINARY_PRINT_LINE_END
:
783 printer_data
->line_no
++;
785 case BINARY_PRINT_DATA_END
:
791 static void print_sample_bpf_output(struct perf_sample
*sample
)
793 unsigned int nr_bytes
= sample
->raw_size
;
794 struct printer_data printer_data
= {0, false, true};
796 print_binary(sample
->raw_data
, nr_bytes
, 8,
797 print_sample_bpf_output_printer
, &printer_data
);
799 if (printer_data
.is_printable
&& printer_data
.hit_nul
)
800 printf("%17s \"%s\"\n", "BPF string:",
801 (char *)(sample
->raw_data
));
805 struct perf_tool tool
;
806 struct perf_session
*session
;
807 bool show_task_events
;
808 bool show_mmap_events
;
809 bool show_switch_events
;
811 struct cpu_map
*cpus
;
812 struct thread_map
*threads
;
816 static int perf_evlist__max_name_len(struct perf_evlist
*evlist
)
818 struct perf_evsel
*evsel
;
821 evlist__for_each_entry(evlist
, evsel
) {
822 int len
= strlen(perf_evsel__name(evsel
));
830 static size_t data_src__printf(u64 data_src
)
832 struct mem_info mi
= { .data_src
.val
= data_src
};
838 perf_script__meminfo_scnprintf(decode
, 100, &mi
);
840 len
= scnprintf(out
, 100, "%16" PRIx64
" %s", data_src
, decode
);
844 return printf("%-*s", maxlen
, out
);
847 static void process_event(struct perf_script
*script
,
848 struct perf_sample
*sample
, struct perf_evsel
*evsel
,
849 struct addr_location
*al
)
851 struct thread
*thread
= al
->thread
;
852 struct perf_event_attr
*attr
= &evsel
->attr
;
854 if (output
[attr
->type
].fields
== 0)
857 print_sample_start(sample
, thread
, evsel
);
859 if (PRINT_FIELD(PERIOD
))
860 printf("%10" PRIu64
" ", sample
->period
);
862 if (PRINT_FIELD(EVNAME
)) {
863 const char *evname
= perf_evsel__name(evsel
);
865 if (!script
->name_width
)
866 script
->name_width
= perf_evlist__max_name_len(script
->session
->evlist
);
868 printf("%*s: ", script
->name_width
,
869 evname
? evname
: "[unknown]");
873 print_sample_flags(sample
->flags
);
875 if (is_bts_event(attr
)) {
876 print_sample_bts(sample
, evsel
, thread
, al
);
880 if (PRINT_FIELD(TRACE
))
881 event_format__print(evsel
->tp_format
, sample
->cpu
,
882 sample
->raw_data
, sample
->raw_size
);
883 if (PRINT_FIELD(ADDR
))
884 print_sample_addr(sample
, thread
, attr
);
886 if (PRINT_FIELD(DATA_SRC
))
887 data_src__printf(sample
->data_src
);
889 if (PRINT_FIELD(WEIGHT
))
890 printf("%16" PRIu64
, sample
->weight
);
892 if (PRINT_FIELD(IP
)) {
893 struct callchain_cursor
*cursor
= NULL
;
895 if (symbol_conf
.use_callchain
&& sample
->callchain
&&
896 thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
897 sample
, NULL
, NULL
, scripting_max_stack
) == 0)
898 cursor
= &callchain_cursor
;
900 putchar(cursor
? '\n' : ' ');
901 sample__fprintf_sym(sample
, al
, 0, output
[attr
->type
].print_ip_opts
, cursor
, stdout
);
904 if (PRINT_FIELD(IREGS
))
905 print_sample_iregs(sample
, attr
);
907 if (PRINT_FIELD(BRSTACK
))
908 print_sample_brstack(sample
);
909 else if (PRINT_FIELD(BRSTACKSYM
))
910 print_sample_brstacksym(sample
, thread
);
912 if (perf_evsel__is_bpf_output(evsel
) && PRINT_FIELD(BPF_OUTPUT
))
913 print_sample_bpf_output(sample
);
918 static struct scripting_ops
*scripting_ops
;
920 static void __process_stat(struct perf_evsel
*counter
, u64 tstamp
)
922 int nthreads
= thread_map__nr(counter
->threads
);
923 int ncpus
= perf_evsel__nr_cpus(counter
);
925 static int header_printed
;
927 if (counter
->system_wide
)
930 if (!header_printed
) {
931 printf("%3s %8s %15s %15s %15s %15s %s\n",
932 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
936 for (thread
= 0; thread
< nthreads
; thread
++) {
937 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
938 struct perf_counts_values
*counts
;
940 counts
= perf_counts(counter
->counts
, cpu
, thread
);
942 printf("%3d %8d %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %s\n",
943 counter
->cpus
->map
[cpu
],
944 thread_map__pid(counter
->threads
, thread
),
949 perf_evsel__name(counter
));
954 static void process_stat(struct perf_evsel
*counter
, u64 tstamp
)
956 if (scripting_ops
&& scripting_ops
->process_stat
)
957 scripting_ops
->process_stat(&stat_config
, counter
, tstamp
);
959 __process_stat(counter
, tstamp
);
962 static void process_stat_interval(u64 tstamp
)
964 if (scripting_ops
&& scripting_ops
->process_stat_interval
)
965 scripting_ops
->process_stat_interval(tstamp
);
968 static void setup_scripting(void)
970 setup_perl_scripting();
971 setup_python_scripting();
974 static int flush_scripting(void)
976 return scripting_ops
? scripting_ops
->flush_script() : 0;
979 static int cleanup_scripting(void)
981 pr_debug("\nperf script stopped\n");
983 return scripting_ops
? scripting_ops
->stop_script() : 0;
986 static int process_sample_event(struct perf_tool
*tool
,
987 union perf_event
*event
,
988 struct perf_sample
*sample
,
989 struct perf_evsel
*evsel
,
990 struct machine
*machine
)
992 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
993 struct addr_location al
;
996 if (sample
->time
< last_timestamp
) {
997 pr_err("Samples misordered, previous: %" PRIu64
998 " this: %" PRIu64
"\n", last_timestamp
,
1002 last_timestamp
= sample
->time
;
1006 if (machine__resolve(machine
, &al
, sample
) < 0) {
1007 pr_err("problem processing %d event, skipping it.\n",
1008 event
->header
.type
);
1015 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
))
1019 scripting_ops
->process_event(event
, sample
, evsel
, &al
);
1021 process_event(scr
, sample
, evsel
, &al
);
1024 addr_location__put(&al
);
1028 static int process_attr(struct perf_tool
*tool
, union perf_event
*event
,
1029 struct perf_evlist
**pevlist
)
1031 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
1032 struct perf_evlist
*evlist
;
1033 struct perf_evsel
*evsel
, *pos
;
1036 err
= perf_event__process_attr(tool
, event
, pevlist
);
1041 evsel
= perf_evlist__last(*pevlist
);
1043 if (evsel
->attr
.type
>= PERF_TYPE_MAX
)
1046 evlist__for_each_entry(evlist
, pos
) {
1047 if (pos
->attr
.type
== evsel
->attr
.type
&& pos
!= evsel
)
1051 set_print_ip_opts(&evsel
->attr
);
1053 if (evsel
->attr
.sample_type
)
1054 err
= perf_evsel__check_attr(evsel
, scr
->session
);
1059 static int process_comm_event(struct perf_tool
*tool
,
1060 union perf_event
*event
,
1061 struct perf_sample
*sample
,
1062 struct machine
*machine
)
1064 struct thread
*thread
;
1065 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1066 struct perf_session
*session
= script
->session
;
1067 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1070 thread
= machine__findnew_thread(machine
, event
->comm
.pid
, event
->comm
.tid
);
1071 if (thread
== NULL
) {
1072 pr_debug("problem processing COMM event, skipping it.\n");
1076 if (perf_event__process_comm(tool
, event
, sample
, machine
) < 0)
1079 if (!evsel
->attr
.sample_id_all
) {
1082 sample
->tid
= event
->comm
.tid
;
1083 sample
->pid
= event
->comm
.pid
;
1085 print_sample_start(sample
, thread
, evsel
);
1086 perf_event__fprintf(event
, stdout
);
1089 thread__put(thread
);
1093 static int process_fork_event(struct perf_tool
*tool
,
1094 union perf_event
*event
,
1095 struct perf_sample
*sample
,
1096 struct machine
*machine
)
1098 struct thread
*thread
;
1099 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1100 struct perf_session
*session
= script
->session
;
1101 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1103 if (perf_event__process_fork(tool
, event
, sample
, machine
) < 0)
1106 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
1107 if (thread
== NULL
) {
1108 pr_debug("problem processing FORK event, skipping it.\n");
1112 if (!evsel
->attr
.sample_id_all
) {
1114 sample
->time
= event
->fork
.time
;
1115 sample
->tid
= event
->fork
.tid
;
1116 sample
->pid
= event
->fork
.pid
;
1118 print_sample_start(sample
, thread
, evsel
);
1119 perf_event__fprintf(event
, stdout
);
1120 thread__put(thread
);
1124 static int process_exit_event(struct perf_tool
*tool
,
1125 union perf_event
*event
,
1126 struct perf_sample
*sample
,
1127 struct machine
*machine
)
1130 struct thread
*thread
;
1131 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1132 struct perf_session
*session
= script
->session
;
1133 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1135 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
1136 if (thread
== NULL
) {
1137 pr_debug("problem processing EXIT event, skipping it.\n");
1141 if (!evsel
->attr
.sample_id_all
) {
1144 sample
->tid
= event
->fork
.tid
;
1145 sample
->pid
= event
->fork
.pid
;
1147 print_sample_start(sample
, thread
, evsel
);
1148 perf_event__fprintf(event
, stdout
);
1150 if (perf_event__process_exit(tool
, event
, sample
, machine
) < 0)
1153 thread__put(thread
);
1157 static int process_mmap_event(struct perf_tool
*tool
,
1158 union perf_event
*event
,
1159 struct perf_sample
*sample
,
1160 struct machine
*machine
)
1162 struct thread
*thread
;
1163 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1164 struct perf_session
*session
= script
->session
;
1165 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1167 if (perf_event__process_mmap(tool
, event
, sample
, machine
) < 0)
1170 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
, event
->mmap
.tid
);
1171 if (thread
== NULL
) {
1172 pr_debug("problem processing MMAP event, skipping it.\n");
1176 if (!evsel
->attr
.sample_id_all
) {
1179 sample
->tid
= event
->mmap
.tid
;
1180 sample
->pid
= event
->mmap
.pid
;
1182 print_sample_start(sample
, thread
, evsel
);
1183 perf_event__fprintf(event
, stdout
);
1184 thread__put(thread
);
1188 static int process_mmap2_event(struct perf_tool
*tool
,
1189 union perf_event
*event
,
1190 struct perf_sample
*sample
,
1191 struct machine
*machine
)
1193 struct thread
*thread
;
1194 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1195 struct perf_session
*session
= script
->session
;
1196 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1198 if (perf_event__process_mmap2(tool
, event
, sample
, machine
) < 0)
1201 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
, event
->mmap2
.tid
);
1202 if (thread
== NULL
) {
1203 pr_debug("problem processing MMAP2 event, skipping it.\n");
1207 if (!evsel
->attr
.sample_id_all
) {
1210 sample
->tid
= event
->mmap2
.tid
;
1211 sample
->pid
= event
->mmap2
.pid
;
1213 print_sample_start(sample
, thread
, evsel
);
1214 perf_event__fprintf(event
, stdout
);
1215 thread__put(thread
);
1219 static int process_switch_event(struct perf_tool
*tool
,
1220 union perf_event
*event
,
1221 struct perf_sample
*sample
,
1222 struct machine
*machine
)
1224 struct thread
*thread
;
1225 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1226 struct perf_session
*session
= script
->session
;
1227 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1229 if (perf_event__process_switch(tool
, event
, sample
, machine
) < 0)
1232 thread
= machine__findnew_thread(machine
, sample
->pid
,
1234 if (thread
== NULL
) {
1235 pr_debug("problem processing SWITCH event, skipping it.\n");
1239 print_sample_start(sample
, thread
, evsel
);
1240 perf_event__fprintf(event
, stdout
);
1241 thread__put(thread
);
1245 static void sig_handler(int sig __maybe_unused
)
1250 static int __cmd_script(struct perf_script
*script
)
1254 signal(SIGINT
, sig_handler
);
1256 /* override event processing functions */
1257 if (script
->show_task_events
) {
1258 script
->tool
.comm
= process_comm_event
;
1259 script
->tool
.fork
= process_fork_event
;
1260 script
->tool
.exit
= process_exit_event
;
1262 if (script
->show_mmap_events
) {
1263 script
->tool
.mmap
= process_mmap_event
;
1264 script
->tool
.mmap2
= process_mmap2_event
;
1266 if (script
->show_switch_events
)
1267 script
->tool
.context_switch
= process_switch_event
;
1269 ret
= perf_session__process_events(script
->session
);
1272 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
1277 struct script_spec
{
1278 struct list_head node
;
1279 struct scripting_ops
*ops
;
1283 static LIST_HEAD(script_specs
);
1285 static struct script_spec
*script_spec__new(const char *spec
,
1286 struct scripting_ops
*ops
)
1288 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
1291 strcpy(s
->spec
, spec
);
1298 static void script_spec__add(struct script_spec
*s
)
1300 list_add_tail(&s
->node
, &script_specs
);
1303 static struct script_spec
*script_spec__find(const char *spec
)
1305 struct script_spec
*s
;
1307 list_for_each_entry(s
, &script_specs
, node
)
1308 if (strcasecmp(s
->spec
, spec
) == 0)
1313 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
1315 struct script_spec
*s
;
1317 s
= script_spec__find(spec
);
1321 s
= script_spec__new(spec
, ops
);
1325 script_spec__add(s
);
1330 static struct scripting_ops
*script_spec__lookup(const char *spec
)
1332 struct script_spec
*s
= script_spec__find(spec
);
1339 static void list_available_languages(void)
1341 struct script_spec
*s
;
1343 fprintf(stderr
, "\n");
1344 fprintf(stderr
, "Scripting language extensions (used in "
1345 "perf script -s [spec:]script.[spec]):\n\n");
1347 list_for_each_entry(s
, &script_specs
, node
)
1348 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
1350 fprintf(stderr
, "\n");
1353 static int parse_scriptname(const struct option
*opt __maybe_unused
,
1354 const char *str
, int unset __maybe_unused
)
1356 char spec
[PATH_MAX
];
1357 const char *script
, *ext
;
1360 if (strcmp(str
, "lang") == 0) {
1361 list_available_languages();
1365 script
= strchr(str
, ':');
1368 if (len
>= PATH_MAX
) {
1369 fprintf(stderr
, "invalid language specifier");
1372 strncpy(spec
, str
, len
);
1374 scripting_ops
= script_spec__lookup(spec
);
1375 if (!scripting_ops
) {
1376 fprintf(stderr
, "invalid language specifier");
1382 ext
= strrchr(script
, '.');
1384 fprintf(stderr
, "invalid script extension");
1387 scripting_ops
= script_spec__lookup(++ext
);
1388 if (!scripting_ops
) {
1389 fprintf(stderr
, "invalid script extension");
1394 script_name
= strdup(script
);
1399 static int parse_output_fields(const struct option
*opt __maybe_unused
,
1400 const char *arg
, int unset __maybe_unused
)
1403 int i
, imax
= ARRAY_SIZE(all_output_options
);
1406 char *str
= strdup(arg
);
1412 /* first word can state for which event type the user is specifying
1413 * the fields. If no type exists, the specified fields apply to all
1414 * event types found in the file minus the invalid fields for a type.
1416 tok
= strchr(str
, ':');
1420 if (!strcmp(str
, "hw"))
1421 type
= PERF_TYPE_HARDWARE
;
1422 else if (!strcmp(str
, "sw"))
1423 type
= PERF_TYPE_SOFTWARE
;
1424 else if (!strcmp(str
, "trace"))
1425 type
= PERF_TYPE_TRACEPOINT
;
1426 else if (!strcmp(str
, "raw"))
1427 type
= PERF_TYPE_RAW
;
1428 else if (!strcmp(str
, "break"))
1429 type
= PERF_TYPE_BREAKPOINT
;
1431 fprintf(stderr
, "Invalid event type in field string.\n");
1436 if (output
[type
].user_set
)
1437 pr_warning("Overriding previous field request for %s events.\n",
1440 output
[type
].fields
= 0;
1441 output
[type
].user_set
= true;
1442 output
[type
].wildcard_set
= false;
1446 if (strlen(str
) == 0) {
1448 "Cannot set fields to 'none' for all event types.\n");
1453 if (output_set_by_user())
1454 pr_warning("Overriding previous field request for all events.\n");
1456 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1457 output
[j
].fields
= 0;
1458 output
[j
].user_set
= true;
1459 output
[j
].wildcard_set
= true;
1463 for (tok
= strtok(tok
, ","); tok
; tok
= strtok(NULL
, ",")) {
1464 for (i
= 0; i
< imax
; ++i
) {
1465 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
1468 if (i
== imax
&& strcmp(tok
, "flags") == 0) {
1473 fprintf(stderr
, "Invalid field requested.\n");
1479 /* add user option to all events types for
1482 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1483 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
1484 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1485 all_output_options
[i
].str
, event_type(j
));
1487 output
[j
].fields
|= all_output_options
[i
].field
;
1490 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
1491 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
1492 all_output_options
[i
].str
, event_type(type
));
1497 output
[type
].fields
|= all_output_options
[i
].field
;
1502 if (output
[type
].fields
== 0) {
1503 pr_debug("No fields requested for %s type. "
1504 "Events will not be displayed.\n", event_type(type
));
1513 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1514 static int is_directory(const char *base_path
, const struct dirent
*dent
)
1516 char path
[PATH_MAX
];
1519 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
1520 if (stat(path
, &st
))
1523 return S_ISDIR(st
.st_mode
);
1526 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1527 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1528 if ((lang_dirent->d_type == DT_DIR || \
1529 (lang_dirent->d_type == DT_UNKNOWN && \
1530 is_directory(scripts_path, lang_dirent))) && \
1531 (strcmp(lang_dirent->d_name, ".")) && \
1532 (strcmp(lang_dirent->d_name, "..")))
1534 #define for_each_script(lang_path, lang_dir, script_dirent) \
1535 while ((script_dirent = readdir(lang_dir)) != NULL) \
1536 if (script_dirent->d_type != DT_DIR && \
1537 (script_dirent->d_type != DT_UNKNOWN || \
1538 !is_directory(lang_path, script_dirent)))
1541 #define RECORD_SUFFIX "-record"
1542 #define REPORT_SUFFIX "-report"
1544 struct script_desc
{
1545 struct list_head node
;
1551 static LIST_HEAD(script_descs
);
1553 static struct script_desc
*script_desc__new(const char *name
)
1555 struct script_desc
*s
= zalloc(sizeof(*s
));
1557 if (s
!= NULL
&& name
)
1558 s
->name
= strdup(name
);
1563 static void script_desc__delete(struct script_desc
*s
)
1566 zfree(&s
->half_liner
);
1571 static void script_desc__add(struct script_desc
*s
)
1573 list_add_tail(&s
->node
, &script_descs
);
1576 static struct script_desc
*script_desc__find(const char *name
)
1578 struct script_desc
*s
;
1580 list_for_each_entry(s
, &script_descs
, node
)
1581 if (strcasecmp(s
->name
, name
) == 0)
1586 static struct script_desc
*script_desc__findnew(const char *name
)
1588 struct script_desc
*s
= script_desc__find(name
);
1593 s
= script_desc__new(name
);
1595 goto out_delete_desc
;
1597 script_desc__add(s
);
1602 script_desc__delete(s
);
1607 static const char *ends_with(const char *str
, const char *suffix
)
1609 size_t suffix_len
= strlen(suffix
);
1610 const char *p
= str
;
1612 if (strlen(str
) > suffix_len
) {
1613 p
= str
+ strlen(str
) - suffix_len
;
1614 if (!strncmp(p
, suffix
, suffix_len
))
1621 static int read_script_info(struct script_desc
*desc
, const char *filename
)
1623 char line
[BUFSIZ
], *p
;
1626 fp
= fopen(filename
, "r");
1630 while (fgets(line
, sizeof(line
), fp
)) {
1637 if (strlen(p
) && *p
== '!')
1641 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
1642 p
[strlen(p
) - 1] = '\0';
1644 if (!strncmp(p
, "description:", strlen("description:"))) {
1645 p
+= strlen("description:");
1646 desc
->half_liner
= strdup(ltrim(p
));
1650 if (!strncmp(p
, "args:", strlen("args:"))) {
1651 p
+= strlen("args:");
1652 desc
->args
= strdup(ltrim(p
));
1662 static char *get_script_root(struct dirent
*script_dirent
, const char *suffix
)
1664 char *script_root
, *str
;
1666 script_root
= strdup(script_dirent
->d_name
);
1670 str
= (char *)ends_with(script_root
, suffix
);
1680 static int list_available_scripts(const struct option
*opt __maybe_unused
,
1681 const char *s __maybe_unused
,
1682 int unset __maybe_unused
)
1684 struct dirent
*script_dirent
, *lang_dirent
;
1685 char scripts_path
[MAXPATHLEN
];
1686 DIR *scripts_dir
, *lang_dir
;
1687 char script_path
[MAXPATHLEN
];
1688 char lang_path
[MAXPATHLEN
];
1689 struct script_desc
*desc
;
1690 char first_half
[BUFSIZ
];
1693 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
1695 scripts_dir
= opendir(scripts_path
);
1698 "open(%s) failed.\n"
1699 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
1704 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
1705 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1706 lang_dirent
->d_name
);
1707 lang_dir
= opendir(lang_path
);
1711 for_each_script(lang_path
, lang_dir
, script_dirent
) {
1712 script_root
= get_script_root(script_dirent
, REPORT_SUFFIX
);
1714 desc
= script_desc__findnew(script_root
);
1715 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1716 lang_path
, script_dirent
->d_name
);
1717 read_script_info(desc
, script_path
);
1723 fprintf(stdout
, "List of available trace scripts:\n");
1724 list_for_each_entry(desc
, &script_descs
, node
) {
1725 sprintf(first_half
, "%s %s", desc
->name
,
1726 desc
->args
? desc
->args
: "");
1727 fprintf(stdout
, " %-36s %s\n", first_half
,
1728 desc
->half_liner
? desc
->half_liner
: "");
1735 * Some scripts specify the required events in their "xxx-record" file,
1736 * this function will check if the events in perf.data match those
1737 * mentioned in the "xxx-record".
1739 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1740 * which is covered well now. And new parsing code should be added to
1741 * cover the future complexing formats like event groups etc.
1743 static int check_ev_match(char *dir_name
, char *scriptname
,
1744 struct perf_session
*session
)
1746 char filename
[MAXPATHLEN
], evname
[128];
1747 char line
[BUFSIZ
], *p
;
1748 struct perf_evsel
*pos
;
1752 sprintf(filename
, "%s/bin/%s-record", dir_name
, scriptname
);
1754 fp
= fopen(filename
, "r");
1758 while (fgets(line
, sizeof(line
), fp
)) {
1764 p
= strstr(p
, "-e");
1770 len
= strcspn(p
, " \t");
1774 snprintf(evname
, len
+ 1, "%s", p
);
1777 evlist__for_each_entry(session
->evlist
, pos
) {
1778 if (!strcmp(perf_evsel__name(pos
), evname
)) {
1796 * Return -1 if none is found, otherwise the actual scripts number.
1798 * Currently the only user of this function is the script browser, which
1799 * will list all statically runnable scripts, select one, execute it and
1800 * show the output in a perf browser.
1802 int find_scripts(char **scripts_array
, char **scripts_path_array
)
1804 struct dirent
*script_dirent
, *lang_dirent
;
1805 char scripts_path
[MAXPATHLEN
], lang_path
[MAXPATHLEN
];
1806 DIR *scripts_dir
, *lang_dir
;
1807 struct perf_session
*session
;
1808 struct perf_data_file file
= {
1810 .mode
= PERF_DATA_MODE_READ
,
1815 session
= perf_session__new(&file
, false, NULL
);
1819 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
1821 scripts_dir
= opendir(scripts_path
);
1823 perf_session__delete(session
);
1827 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
1828 snprintf(lang_path
, MAXPATHLEN
, "%s/%s", scripts_path
,
1829 lang_dirent
->d_name
);
1831 if (strstr(lang_path
, "perl"))
1835 if (strstr(lang_path
, "python"))
1839 lang_dir
= opendir(lang_path
);
1843 for_each_script(lang_path
, lang_dir
, script_dirent
) {
1844 /* Skip those real time scripts: xxxtop.p[yl] */
1845 if (strstr(script_dirent
->d_name
, "top."))
1847 sprintf(scripts_path_array
[i
], "%s/%s", lang_path
,
1848 script_dirent
->d_name
);
1849 temp
= strchr(script_dirent
->d_name
, '.');
1850 snprintf(scripts_array
[i
],
1851 (temp
- script_dirent
->d_name
) + 1,
1852 "%s", script_dirent
->d_name
);
1854 if (check_ev_match(lang_path
,
1855 scripts_array
[i
], session
))
1863 closedir(scripts_dir
);
1864 perf_session__delete(session
);
1868 static char *get_script_path(const char *script_root
, const char *suffix
)
1870 struct dirent
*script_dirent
, *lang_dirent
;
1871 char scripts_path
[MAXPATHLEN
];
1872 char script_path
[MAXPATHLEN
];
1873 DIR *scripts_dir
, *lang_dir
;
1874 char lang_path
[MAXPATHLEN
];
1875 char *__script_root
;
1877 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
1879 scripts_dir
= opendir(scripts_path
);
1883 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
1884 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1885 lang_dirent
->d_name
);
1886 lang_dir
= opendir(lang_path
);
1890 for_each_script(lang_path
, lang_dir
, script_dirent
) {
1891 __script_root
= get_script_root(script_dirent
, suffix
);
1892 if (__script_root
&& !strcmp(script_root
, __script_root
)) {
1893 free(__script_root
);
1895 closedir(scripts_dir
);
1896 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1897 lang_path
, script_dirent
->d_name
);
1898 return strdup(script_path
);
1900 free(__script_root
);
1904 closedir(scripts_dir
);
1909 static bool is_top_script(const char *script_path
)
1911 return ends_with(script_path
, "top") == NULL
? false : true;
1914 static int has_required_arg(char *script_path
)
1916 struct script_desc
*desc
;
1920 desc
= script_desc__new(NULL
);
1922 if (read_script_info(desc
, script_path
))
1928 for (p
= desc
->args
; *p
; p
++)
1932 script_desc__delete(desc
);
1937 static int have_cmd(int argc
, const char **argv
)
1939 char **__argv
= malloc(sizeof(const char *) * argc
);
1942 pr_err("malloc failed\n");
1946 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
1947 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
1948 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
1951 system_wide
= (argc
== 0);
1956 static void script__setup_sample_type(struct perf_script
*script
)
1958 struct perf_session
*session
= script
->session
;
1959 u64 sample_type
= perf_evlist__combined_sample_type(session
->evlist
);
1961 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
) {
1962 if ((sample_type
& PERF_SAMPLE_REGS_USER
) &&
1963 (sample_type
& PERF_SAMPLE_STACK_USER
))
1964 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
1965 else if (sample_type
& PERF_SAMPLE_BRANCH_STACK
)
1966 callchain_param
.record_mode
= CALLCHAIN_LBR
;
1968 callchain_param
.record_mode
= CALLCHAIN_FP
;
1972 static int process_stat_round_event(struct perf_tool
*tool __maybe_unused
,
1973 union perf_event
*event
,
1974 struct perf_session
*session
)
1976 struct stat_round_event
*round
= &event
->stat_round
;
1977 struct perf_evsel
*counter
;
1979 evlist__for_each_entry(session
->evlist
, counter
) {
1980 perf_stat_process_counter(&stat_config
, counter
);
1981 process_stat(counter
, round
->time
);
1984 process_stat_interval(round
->time
);
1988 static int process_stat_config_event(struct perf_tool
*tool __maybe_unused
,
1989 union perf_event
*event
,
1990 struct perf_session
*session __maybe_unused
)
1992 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
1996 static int set_maps(struct perf_script
*script
)
1998 struct perf_evlist
*evlist
= script
->session
->evlist
;
2000 if (!script
->cpus
|| !script
->threads
)
2003 if (WARN_ONCE(script
->allocated
, "stats double allocation\n"))
2006 perf_evlist__set_maps(evlist
, script
->cpus
, script
->threads
);
2008 if (perf_evlist__alloc_stats(evlist
, true))
2011 script
->allocated
= true;
2016 int process_thread_map_event(struct perf_tool
*tool
,
2017 union perf_event
*event
,
2018 struct perf_session
*session __maybe_unused
)
2020 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2022 if (script
->threads
) {
2023 pr_warning("Extra thread map event, ignoring.\n");
2027 script
->threads
= thread_map__new_event(&event
->thread_map
);
2028 if (!script
->threads
)
2031 return set_maps(script
);
2035 int process_cpu_map_event(struct perf_tool
*tool __maybe_unused
,
2036 union perf_event
*event
,
2037 struct perf_session
*session __maybe_unused
)
2039 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
2042 pr_warning("Extra cpu map event, ignoring.\n");
2046 script
->cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
2050 return set_maps(script
);
2053 int cmd_script(int argc
, const char **argv
, const char *prefix __maybe_unused
)
2055 bool show_full_info
= false;
2056 bool header
= false;
2057 bool header_only
= false;
2058 bool script_started
= false;
2059 char *rec_script_path
= NULL
;
2060 char *rep_script_path
= NULL
;
2061 struct perf_session
*session
;
2062 struct itrace_synth_opts itrace_synth_opts
= { .set
= false, };
2063 char *script_path
= NULL
;
2064 const char **__argv
;
2066 struct perf_script script
= {
2068 .sample
= process_sample_event
,
2069 .mmap
= perf_event__process_mmap
,
2070 .mmap2
= perf_event__process_mmap2
,
2071 .comm
= perf_event__process_comm
,
2072 .exit
= perf_event__process_exit
,
2073 .fork
= perf_event__process_fork
,
2074 .attr
= process_attr
,
2075 .event_update
= perf_event__process_event_update
,
2076 .tracing_data
= perf_event__process_tracing_data
,
2077 .build_id
= perf_event__process_build_id
,
2078 .id_index
= perf_event__process_id_index
,
2079 .auxtrace_info
= perf_event__process_auxtrace_info
,
2080 .auxtrace
= perf_event__process_auxtrace
,
2081 .auxtrace_error
= perf_event__process_auxtrace_error
,
2082 .stat
= perf_event__process_stat_event
,
2083 .stat_round
= process_stat_round_event
,
2084 .stat_config
= process_stat_config_event
,
2085 .thread_map
= process_thread_map_event
,
2086 .cpu_map
= process_cpu_map_event
,
2087 .ordered_events
= true,
2088 .ordering_requires_timestamps
= true,
2091 struct perf_data_file file
= {
2092 .mode
= PERF_DATA_MODE_READ
,
2094 const struct option options
[] = {
2095 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
2096 "dump raw trace in ASCII"),
2097 OPT_INCR('v', "verbose", &verbose
,
2098 "be more verbose (show symbol address, etc)"),
2099 OPT_BOOLEAN('L', "Latency", &latency_format
,
2100 "show latency attributes (irqs/preemption disabled, etc)"),
2101 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
2102 list_available_scripts
),
2103 OPT_CALLBACK('s', "script", NULL
, "name",
2104 "script file name (lang:script name, script name, or *)",
2106 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
2107 "generate perf-script.xx script in specified language"),
2108 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
2109 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
2110 "do various checks like samples ordering and lost events"),
2111 OPT_BOOLEAN(0, "header", &header
, "Show data header."),
2112 OPT_BOOLEAN(0, "header-only", &header_only
, "Show only data header."),
2113 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
2114 "file", "vmlinux pathname"),
2115 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
2116 "file", "kallsyms pathname"),
2117 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
2118 "When printing symbols do not display call chain"),
2119 OPT_CALLBACK(0, "symfs", NULL
, "directory",
2120 "Look for files with symbols relative to this directory",
2121 symbol__config_symfs
),
2122 OPT_CALLBACK('F', "fields", NULL
, "str",
2123 "comma separated output fields prepend with 'type:'. "
2124 "Valid types: hw,sw,trace,raw. "
2125 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2126 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
2127 "bpf-output,callindent", parse_output_fields
),
2128 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
2129 "system-wide collection from all CPUs"),
2130 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
2131 "only consider these symbols"),
2132 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
2133 OPT_STRING('c', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
2134 "only display events for these comms"),
2135 OPT_STRING(0, "pid", &symbol_conf
.pid_list_str
, "pid[,pid...]",
2136 "only consider symbols in these pids"),
2137 OPT_STRING(0, "tid", &symbol_conf
.tid_list_str
, "tid[,tid...]",
2138 "only consider symbols in these tids"),
2139 OPT_UINTEGER(0, "max-stack", &scripting_max_stack
,
2140 "Set the maximum stack depth when parsing the callchain, "
2141 "anything beyond the specified depth will be ignored. "
2142 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH
)),
2143 OPT_BOOLEAN('I', "show-info", &show_full_info
,
2144 "display extended information from perf.data file"),
2145 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf
.show_kernel_path
,
2146 "Show the path of [kernel.kallsyms]"),
2147 OPT_BOOLEAN('\0', "show-task-events", &script
.show_task_events
,
2148 "Show the fork/comm/exit events"),
2149 OPT_BOOLEAN('\0', "show-mmap-events", &script
.show_mmap_events
,
2150 "Show the mmap events"),
2151 OPT_BOOLEAN('\0', "show-switch-events", &script
.show_switch_events
,
2152 "Show context switch events (if recorded)"),
2153 OPT_BOOLEAN('f', "force", &file
.force
, "don't complain, do it"),
2154 OPT_BOOLEAN(0, "ns", &nanosecs
,
2155 "Use 9 decimal places when displaying time"),
2156 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts
, NULL
, "opts",
2157 "Instruction Tracing options",
2158 itrace_parse_synth_opts
),
2159 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename
,
2160 "Show full source file name path for source lines"),
2161 OPT_BOOLEAN(0, "demangle", &symbol_conf
.demangle
,
2162 "Enable symbol demangling"),
2163 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf
.demangle_kernel
,
2164 "Enable kernel symbol demangling"),
2168 const char * const script_subcommands
[] = { "record", "report", NULL
};
2169 const char *script_usage
[] = {
2170 "perf script [<options>]",
2171 "perf script [<options>] record <script> [<record-options>] <command>",
2172 "perf script [<options>] report <script> [script-args]",
2173 "perf script [<options>] <script> [<record-options>] <command>",
2174 "perf script [<options>] <top-script> [script-args]",
2180 argc
= parse_options_subcommand(argc
, argv
, options
, script_subcommands
, script_usage
,
2181 PARSE_OPT_STOP_AT_NON_OPTION
);
2183 file
.path
= input_name
;
2185 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
2186 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
2187 if (!rec_script_path
)
2188 return cmd_record(argc
, argv
, NULL
);
2191 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
2192 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
2193 if (!rep_script_path
) {
2195 "Please specify a valid report script"
2196 "(see 'perf script -l' for listing)\n");
2201 if (itrace_synth_opts
.callchain
&&
2202 itrace_synth_opts
.callchain_sz
> scripting_max_stack
)
2203 scripting_max_stack
= itrace_synth_opts
.callchain_sz
;
2205 /* make sure PERF_EXEC_PATH is set for scripts */
2206 set_argv_exec_path(get_argv_exec_path());
2208 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
2213 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
2214 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
2216 if (!rec_script_path
&& !rep_script_path
) {
2217 usage_with_options_msg(script_usage
, options
,
2218 "Couldn't find script `%s'\n\n See perf"
2219 " script -l for available scripts.\n", argv
[0]);
2222 if (is_top_script(argv
[0])) {
2223 rep_args
= argc
- 1;
2227 rep_args
= has_required_arg(rep_script_path
);
2228 rec_args
= (argc
- 1) - rep_args
;
2230 usage_with_options_msg(script_usage
, options
,
2231 "`%s' script requires options."
2232 "\n\n See perf script -l for available "
2233 "scripts and options.\n", argv
[0]);
2237 if (pipe(live_pipe
) < 0) {
2238 perror("failed to create pipe");
2244 perror("failed to fork");
2251 dup2(live_pipe
[1], 1);
2252 close(live_pipe
[0]);
2254 if (is_top_script(argv
[0])) {
2256 } else if (!system_wide
) {
2257 if (have_cmd(argc
- rep_args
, &argv
[rep_args
]) != 0) {
2263 __argv
= malloc((argc
+ 6) * sizeof(const char *));
2265 pr_err("malloc failed\n");
2270 __argv
[j
++] = "/bin/sh";
2271 __argv
[j
++] = rec_script_path
;
2277 for (i
= rep_args
+ 1; i
< argc
; i
++)
2278 __argv
[j
++] = argv
[i
];
2281 execvp("/bin/sh", (char **)__argv
);
2286 dup2(live_pipe
[0], 0);
2287 close(live_pipe
[1]);
2289 __argv
= malloc((argc
+ 4) * sizeof(const char *));
2291 pr_err("malloc failed\n");
2297 __argv
[j
++] = "/bin/sh";
2298 __argv
[j
++] = rep_script_path
;
2299 for (i
= 1; i
< rep_args
+ 1; i
++)
2300 __argv
[j
++] = argv
[i
];
2305 execvp("/bin/sh", (char **)__argv
);
2310 if (rec_script_path
)
2311 script_path
= rec_script_path
;
2312 if (rep_script_path
)
2313 script_path
= rep_script_path
;
2318 if (!rec_script_path
)
2319 system_wide
= false;
2320 else if (!system_wide
) {
2321 if (have_cmd(argc
- 1, &argv
[1]) != 0) {
2327 __argv
= malloc((argc
+ 2) * sizeof(const char *));
2329 pr_err("malloc failed\n");
2334 __argv
[j
++] = "/bin/sh";
2335 __argv
[j
++] = script_path
;
2338 for (i
= 2; i
< argc
; i
++)
2339 __argv
[j
++] = argv
[i
];
2342 execvp("/bin/sh", (char **)__argv
);
2350 session
= perf_session__new(&file
, false, &script
.tool
);
2351 if (session
== NULL
)
2354 if (header
|| header_only
) {
2355 perf_session__fprintf_info(session
, stdout
, show_full_info
);
2360 if (symbol__init(&session
->header
.env
) < 0)
2363 script
.session
= session
;
2364 script__setup_sample_type(&script
);
2366 if (output
[PERF_TYPE_HARDWARE
].fields
& PERF_OUTPUT_CALLINDENT
)
2367 itrace_synth_opts
.thread_stack
= true;
2369 session
->itrace_synth_opts
= &itrace_synth_opts
;
2372 err
= perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
);
2378 symbol_conf
.use_callchain
= true;
2380 symbol_conf
.use_callchain
= false;
2382 if (session
->tevent
.pevent
&&
2383 pevent_set_function_resolver(session
->tevent
.pevent
,
2384 machine__resolve_kernel_addr
,
2385 &session
->machines
.host
) < 0) {
2386 pr_err("%s: failed to set libtraceevent function resolver\n", __func__
);
2390 if (generate_script_lang
) {
2391 struct stat perf_stat
;
2394 if (output_set_by_user()) {
2396 "custom fields not supported for generated scripts");
2401 input
= open(file
.path
, O_RDONLY
); /* input_name */
2404 perror("failed to open file");
2408 err
= fstat(input
, &perf_stat
);
2410 perror("failed to stat file");
2414 if (!perf_stat
.st_size
) {
2415 fprintf(stderr
, "zero-sized file, nothing to do!\n");
2419 scripting_ops
= script_spec__lookup(generate_script_lang
);
2420 if (!scripting_ops
) {
2421 fprintf(stderr
, "invalid language specifier");
2426 err
= scripting_ops
->generate_script(session
->tevent
.pevent
,
2432 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
2435 pr_debug("perf script started with script %s\n\n", script_name
);
2436 script_started
= true;
2440 err
= perf_session__check_output_opt(session
);
2444 err
= __cmd_script(&script
);
2449 perf_evlist__free_stats(session
->evlist
);
2450 perf_session__delete(session
);
2453 cleanup_scripting();