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 <linux/bitmap.h>
25 #include <linux/stringify.h>
27 #include "util/mem-events.h"
29 static char const *script_name
;
30 static char const *generate_script_lang
;
31 static bool debug_mode
;
32 static u64 last_timestamp
;
33 static u64 nr_unordered
;
34 static bool no_callchain
;
35 static bool latency_format
;
36 static bool system_wide
;
37 static bool print_flags
;
39 static const char *cpu_list
;
40 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
41 static struct perf_stat_config stat_config
;
43 unsigned int scripting_max_stack
= PERF_MAX_STACK_DEPTH
;
45 enum perf_output_field
{
46 PERF_OUTPUT_COMM
= 1U << 0,
47 PERF_OUTPUT_TID
= 1U << 1,
48 PERF_OUTPUT_PID
= 1U << 2,
49 PERF_OUTPUT_TIME
= 1U << 3,
50 PERF_OUTPUT_CPU
= 1U << 4,
51 PERF_OUTPUT_EVNAME
= 1U << 5,
52 PERF_OUTPUT_TRACE
= 1U << 6,
53 PERF_OUTPUT_IP
= 1U << 7,
54 PERF_OUTPUT_SYM
= 1U << 8,
55 PERF_OUTPUT_DSO
= 1U << 9,
56 PERF_OUTPUT_ADDR
= 1U << 10,
57 PERF_OUTPUT_SYMOFFSET
= 1U << 11,
58 PERF_OUTPUT_SRCLINE
= 1U << 12,
59 PERF_OUTPUT_PERIOD
= 1U << 13,
60 PERF_OUTPUT_IREGS
= 1U << 14,
61 PERF_OUTPUT_BRSTACK
= 1U << 15,
62 PERF_OUTPUT_BRSTACKSYM
= 1U << 16,
63 PERF_OUTPUT_DATA_SRC
= 1U << 17,
64 PERF_OUTPUT_WEIGHT
= 1U << 18,
65 PERF_OUTPUT_BPF_OUTPUT
= 1U << 19,
68 struct output_option
{
70 enum perf_output_field field
;
71 } all_output_options
[] = {
72 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
73 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
74 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
75 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
76 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
77 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
78 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
79 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
80 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
81 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
82 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
83 {.str
= "symoff", .field
= PERF_OUTPUT_SYMOFFSET
},
84 {.str
= "srcline", .field
= PERF_OUTPUT_SRCLINE
},
85 {.str
= "period", .field
= PERF_OUTPUT_PERIOD
},
86 {.str
= "iregs", .field
= PERF_OUTPUT_IREGS
},
87 {.str
= "brstack", .field
= PERF_OUTPUT_BRSTACK
},
88 {.str
= "brstacksym", .field
= PERF_OUTPUT_BRSTACKSYM
},
89 {.str
= "data_src", .field
= PERF_OUTPUT_DATA_SRC
},
90 {.str
= "weight", .field
= PERF_OUTPUT_WEIGHT
},
91 {.str
= "bpf-output", .field
= PERF_OUTPUT_BPF_OUTPUT
},
94 /* default set to maintain compatibility with current format */
98 unsigned int print_ip_opts
;
101 } output
[PERF_TYPE_MAX
] = {
103 [PERF_TYPE_HARDWARE
] = {
106 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
107 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
108 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
109 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
112 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
115 [PERF_TYPE_SOFTWARE
] = {
118 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
119 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
120 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
121 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
122 PERF_OUTPUT_PERIOD
| PERF_OUTPUT_BPF_OUTPUT
,
124 .invalid_fields
= PERF_OUTPUT_TRACE
,
127 [PERF_TYPE_TRACEPOINT
] = {
130 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
131 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
132 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
138 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
139 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
140 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
141 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
142 PERF_OUTPUT_PERIOD
| PERF_OUTPUT_ADDR
|
143 PERF_OUTPUT_DATA_SRC
| PERF_OUTPUT_WEIGHT
,
145 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
148 [PERF_TYPE_BREAKPOINT
] = {
151 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
152 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
153 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
154 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
|
157 .invalid_fields
= PERF_OUTPUT_TRACE
| PERF_OUTPUT_BPF_OUTPUT
,
161 static bool output_set_by_user(void)
164 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
165 if (output
[j
].user_set
)
171 static const char *output_field2str(enum perf_output_field field
)
173 int i
, imax
= ARRAY_SIZE(all_output_options
);
174 const char *str
= "";
176 for (i
= 0; i
< imax
; ++i
) {
177 if (all_output_options
[i
].field
== field
) {
178 str
= all_output_options
[i
].str
;
185 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
187 static int perf_evsel__do_check_stype(struct perf_evsel
*evsel
,
188 u64 sample_type
, const char *sample_msg
,
189 enum perf_output_field field
,
192 struct perf_event_attr
*attr
= &evsel
->attr
;
193 int type
= attr
->type
;
196 if (attr
->sample_type
& sample_type
)
199 if (output
[type
].user_set
) {
202 evname
= perf_evsel__name(evsel
);
203 pr_err("Samples for '%s' event do not have %s attribute set. "
204 "Cannot print '%s' field.\n",
205 evname
, sample_msg
, output_field2str(field
));
209 /* user did not ask for it explicitly so remove from the default list */
210 output
[type
].fields
&= ~field
;
211 evname
= perf_evsel__name(evsel
);
212 pr_debug("Samples for '%s' event do not have %s attribute set. "
213 "Skipping '%s' field.\n",
214 evname
, sample_msg
, output_field2str(field
));
219 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
220 u64 sample_type
, const char *sample_msg
,
221 enum perf_output_field field
)
223 return perf_evsel__do_check_stype(evsel
, sample_type
, sample_msg
, field
,
227 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
228 struct perf_session
*session
)
230 struct perf_event_attr
*attr
= &evsel
->attr
;
233 if (perf_header__has_feat(&session
->header
, HEADER_STAT
))
236 allow_user_set
= perf_header__has_feat(&session
->header
,
239 if (PRINT_FIELD(TRACE
) &&
240 !perf_session__has_traces(session
, "record -R"))
243 if (PRINT_FIELD(IP
)) {
244 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_IP
, "IP",
249 if (PRINT_FIELD(ADDR
) &&
250 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_ADDR
, "ADDR",
251 PERF_OUTPUT_ADDR
, allow_user_set
))
254 if (PRINT_FIELD(DATA_SRC
) &&
255 perf_evsel__check_stype(evsel
, PERF_SAMPLE_DATA_SRC
, "DATA_SRC",
256 PERF_OUTPUT_DATA_SRC
))
259 if (PRINT_FIELD(WEIGHT
) &&
260 perf_evsel__check_stype(evsel
, PERF_SAMPLE_WEIGHT
, "WEIGHT",
264 if (PRINT_FIELD(SYM
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
265 pr_err("Display of symbols requested but neither sample IP nor "
266 "sample address\nis selected. Hence, no addresses to convert "
270 if (PRINT_FIELD(SYMOFFSET
) && !PRINT_FIELD(SYM
)) {
271 pr_err("Display of offsets requested but symbol is not"
275 if (PRINT_FIELD(DSO
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
276 pr_err("Display of DSO requested but neither sample IP nor "
277 "sample address\nis selected. Hence, no addresses to convert "
281 if (PRINT_FIELD(SRCLINE
) && !PRINT_FIELD(IP
)) {
282 pr_err("Display of source line number requested but sample IP is not\n"
283 "selected. Hence, no address to lookup the source line number.\n");
287 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
288 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID",
289 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
292 if (PRINT_FIELD(TIME
) &&
293 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TIME
, "TIME",
297 if (PRINT_FIELD(CPU
) &&
298 perf_evsel__do_check_stype(evsel
, PERF_SAMPLE_CPU
, "CPU",
299 PERF_OUTPUT_CPU
, allow_user_set
))
302 if (PRINT_FIELD(PERIOD
) &&
303 perf_evsel__check_stype(evsel
, PERF_SAMPLE_PERIOD
, "PERIOD",
307 if (PRINT_FIELD(IREGS
) &&
308 perf_evsel__check_stype(evsel
, PERF_SAMPLE_REGS_INTR
, "IREGS",
315 static void set_print_ip_opts(struct perf_event_attr
*attr
)
317 unsigned int type
= attr
->type
;
319 output
[type
].print_ip_opts
= 0;
321 output
[type
].print_ip_opts
|= EVSEL__PRINT_IP
;
323 if (PRINT_FIELD(SYM
))
324 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYM
;
326 if (PRINT_FIELD(DSO
))
327 output
[type
].print_ip_opts
|= EVSEL__PRINT_DSO
;
329 if (PRINT_FIELD(SYMOFFSET
))
330 output
[type
].print_ip_opts
|= EVSEL__PRINT_SYMOFFSET
;
332 if (PRINT_FIELD(SRCLINE
))
333 output
[type
].print_ip_opts
|= EVSEL__PRINT_SRCLINE
;
337 * verify all user requested events exist and the samples
338 * have the expected data
340 static int perf_session__check_output_opt(struct perf_session
*session
)
343 struct perf_evsel
*evsel
;
345 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
346 evsel
= perf_session__find_first_evtype(session
, j
);
349 * even if fields is set to 0 (ie., show nothing) event must
350 * exist if user explicitly includes it on the command line
352 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
) {
353 pr_err("%s events do not exist. "
354 "Remove corresponding -f option to proceed.\n",
359 if (evsel
&& output
[j
].fields
&&
360 perf_evsel__check_attr(evsel
, session
))
366 set_print_ip_opts(&evsel
->attr
);
370 bool use_callchain
= false;
372 evlist__for_each(session
->evlist
, evsel
) {
373 if (evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
374 use_callchain
= true;
379 symbol_conf
.use_callchain
= false;
383 * set default for tracepoints to print symbols only
384 * if callchains are present
386 if (symbol_conf
.use_callchain
&&
387 !output
[PERF_TYPE_TRACEPOINT
].user_set
) {
388 struct perf_event_attr
*attr
;
390 j
= PERF_TYPE_TRACEPOINT
;
391 evsel
= perf_session__find_first_evtype(session
, j
);
397 if (attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
398 output
[j
].fields
|= PERF_OUTPUT_IP
;
399 output
[j
].fields
|= PERF_OUTPUT_SYM
;
400 output
[j
].fields
|= PERF_OUTPUT_DSO
;
401 set_print_ip_opts(attr
);
409 static void print_sample_iregs(struct perf_sample
*sample
,
410 struct perf_event_attr
*attr
)
412 struct regs_dump
*regs
= &sample
->intr_regs
;
413 uint64_t mask
= attr
->sample_regs_intr
;
419 for_each_set_bit(r
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
420 u64 val
= regs
->regs
[i
++];
421 printf("%5s:0x%"PRIx64
" ", perf_reg_name(r
), val
);
425 static void print_sample_start(struct perf_sample
*sample
,
426 struct thread
*thread
,
427 struct perf_evsel
*evsel
)
429 struct perf_event_attr
*attr
= &evsel
->attr
;
432 unsigned long long nsecs
;
434 if (PRINT_FIELD(COMM
)) {
436 printf("%8.8s ", thread__comm_str(thread
));
437 else if (PRINT_FIELD(IP
) && symbol_conf
.use_callchain
)
438 printf("%s ", thread__comm_str(thread
));
440 printf("%16s ", thread__comm_str(thread
));
443 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
444 printf("%5d/%-5d ", sample
->pid
, sample
->tid
);
445 else if (PRINT_FIELD(PID
))
446 printf("%5d ", sample
->pid
);
447 else if (PRINT_FIELD(TID
))
448 printf("%5d ", sample
->tid
);
450 if (PRINT_FIELD(CPU
)) {
452 printf("%3d ", sample
->cpu
);
454 printf("[%03d] ", sample
->cpu
);
457 if (PRINT_FIELD(TIME
)) {
458 nsecs
= sample
->time
;
459 secs
= nsecs
/ NSECS_PER_SEC
;
460 nsecs
-= secs
* NSECS_PER_SEC
;
461 usecs
= nsecs
/ NSECS_PER_USEC
;
463 printf("%5lu.%09llu: ", secs
, nsecs
);
465 printf("%5lu.%06lu: ", secs
, usecs
);
470 mispred_str(struct branch_entry
*br
)
472 if (!(br
->flags
.mispred
|| br
->flags
.predicted
))
475 return br
->flags
.predicted
? 'P' : 'M';
478 static void print_sample_brstack(struct perf_sample
*sample
)
480 struct branch_stack
*br
= sample
->branch_stack
;
486 for (i
= 0; i
< br
->nr
; i
++) {
487 printf(" 0x%"PRIx64
"/0x%"PRIx64
"/%c/%c/%c/%d ",
490 mispred_str( br
->entries
+ i
),
491 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
492 br
->entries
[i
].flags
.abort
? 'A' : '-',
493 br
->entries
[i
].flags
.cycles
);
497 static void print_sample_brstacksym(struct perf_sample
*sample
,
498 struct thread
*thread
)
500 struct branch_stack
*br
= sample
->branch_stack
;
501 struct addr_location alf
, alt
;
507 for (i
= 0; i
< br
->nr
; i
++) {
509 memset(&alf
, 0, sizeof(alf
));
510 memset(&alt
, 0, sizeof(alt
));
511 from
= br
->entries
[i
].from
;
512 to
= br
->entries
[i
].to
;
514 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, from
, &alf
);
516 alf
.sym
= map__find_symbol(alf
.map
, alf
.addr
, NULL
);
518 thread__find_addr_map(thread
, sample
->cpumode
, MAP__FUNCTION
, to
, &alt
);
520 alt
.sym
= map__find_symbol(alt
.map
, alt
.addr
, NULL
);
522 symbol__fprintf_symname_offs(alf
.sym
, &alf
, stdout
);
524 symbol__fprintf_symname_offs(alt
.sym
, &alt
, stdout
);
525 printf("/%c/%c/%c/%d ",
526 mispred_str( br
->entries
+ i
),
527 br
->entries
[i
].flags
.in_tx
? 'X' : '-',
528 br
->entries
[i
].flags
.abort
? 'A' : '-',
529 br
->entries
[i
].flags
.cycles
);
534 static void print_sample_addr(struct perf_sample
*sample
,
535 struct thread
*thread
,
536 struct perf_event_attr
*attr
)
538 struct addr_location al
;
540 printf("%16" PRIx64
, sample
->addr
);
542 if (!sample_addr_correlates_sym(attr
))
545 thread__resolve(thread
, &al
, sample
);
547 if (PRINT_FIELD(SYM
)) {
549 if (PRINT_FIELD(SYMOFFSET
))
550 symbol__fprintf_symname_offs(al
.sym
, &al
, stdout
);
552 symbol__fprintf_symname(al
.sym
, stdout
);
555 if (PRINT_FIELD(DSO
)) {
557 map__fprintf_dsoname(al
.map
, stdout
);
562 static void print_sample_bts(struct perf_sample
*sample
,
563 struct perf_evsel
*evsel
,
564 struct thread
*thread
,
565 struct addr_location
*al
)
567 struct perf_event_attr
*attr
= &evsel
->attr
;
568 bool print_srcline_last
= false;
570 /* print branch_from information */
571 if (PRINT_FIELD(IP
)) {
572 unsigned int print_opts
= output
[attr
->type
].print_ip_opts
;
573 struct callchain_cursor
*cursor
= NULL
;
575 if (symbol_conf
.use_callchain
&& sample
->callchain
&&
576 thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
577 sample
, NULL
, NULL
, scripting_max_stack
) == 0)
578 cursor
= &callchain_cursor
;
580 if (cursor
== NULL
) {
582 if (print_opts
& EVSEL__PRINT_SRCLINE
) {
583 print_srcline_last
= true;
584 print_opts
&= ~EVSEL__PRINT_SRCLINE
;
589 sample__fprintf_sym(sample
, al
, 0, print_opts
, cursor
, stdout
);
592 /* print branch_to information */
593 if (PRINT_FIELD(ADDR
) ||
594 ((evsel
->attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
595 !output
[attr
->type
].user_set
)) {
597 print_sample_addr(sample
, thread
, attr
);
600 if (print_srcline_last
)
601 map__fprintf_srcline(al
->map
, al
->addr
, "\n ", stdout
);
606 static void print_sample_flags(u32 flags
)
608 const char *chars
= PERF_IP_FLAG_CHARS
;
609 const int n
= strlen(PERF_IP_FLAG_CHARS
);
613 for (i
= 0; i
< n
; i
++, flags
>>= 1) {
615 str
[pos
++] = chars
[i
];
617 for (; i
< 32; i
++, flags
>>= 1) {
622 printf(" %-4s ", str
);
625 struct printer_data
{
632 print_sample_bpf_output_printer(enum binary_printer_ops op
,
636 unsigned char ch
= (unsigned char)val
;
637 struct printer_data
*printer_data
= extra
;
640 case BINARY_PRINT_DATA_BEGIN
:
643 case BINARY_PRINT_LINE_BEGIN
:
644 printf("%17s", !printer_data
->line_no
? "BPF output:" :
647 case BINARY_PRINT_ADDR
:
648 printf(" %04x:", val
);
650 case BINARY_PRINT_NUM_DATA
:
651 printf(" %02x", val
);
653 case BINARY_PRINT_NUM_PAD
:
656 case BINARY_PRINT_SEP
:
659 case BINARY_PRINT_CHAR_DATA
:
660 if (printer_data
->hit_nul
&& ch
)
661 printer_data
->is_printable
= false;
666 if (!printer_data
->is_printable
)
670 printer_data
->hit_nul
= true;
672 printer_data
->is_printable
= false;
677 case BINARY_PRINT_CHAR_PAD
:
680 case BINARY_PRINT_LINE_END
:
682 printer_data
->line_no
++;
684 case BINARY_PRINT_DATA_END
:
690 static void print_sample_bpf_output(struct perf_sample
*sample
)
692 unsigned int nr_bytes
= sample
->raw_size
;
693 struct printer_data printer_data
= {0, false, true};
695 print_binary(sample
->raw_data
, nr_bytes
, 8,
696 print_sample_bpf_output_printer
, &printer_data
);
698 if (printer_data
.is_printable
&& printer_data
.hit_nul
)
699 printf("%17s \"%s\"\n", "BPF string:",
700 (char *)(sample
->raw_data
));
704 struct perf_tool tool
;
705 struct perf_session
*session
;
706 bool show_task_events
;
707 bool show_mmap_events
;
708 bool show_switch_events
;
710 struct cpu_map
*cpus
;
711 struct thread_map
*threads
;
715 static int perf_evlist__max_name_len(struct perf_evlist
*evlist
)
717 struct perf_evsel
*evsel
;
720 evlist__for_each(evlist
, evsel
) {
721 int len
= strlen(perf_evsel__name(evsel
));
729 static size_t data_src__printf(u64 data_src
)
731 struct mem_info mi
= { .data_src
.val
= data_src
};
737 perf_script__meminfo_scnprintf(decode
, 100, &mi
);
739 len
= scnprintf(out
, 100, "%16" PRIx64
" %s", data_src
, decode
);
743 return printf("%-*s", maxlen
, out
);
746 static void process_event(struct perf_script
*script
,
747 struct perf_sample
*sample
, struct perf_evsel
*evsel
,
748 struct addr_location
*al
)
750 struct thread
*thread
= al
->thread
;
751 struct perf_event_attr
*attr
= &evsel
->attr
;
753 if (output
[attr
->type
].fields
== 0)
756 print_sample_start(sample
, thread
, evsel
);
758 if (PRINT_FIELD(PERIOD
))
759 printf("%10" PRIu64
" ", sample
->period
);
761 if (PRINT_FIELD(EVNAME
)) {
762 const char *evname
= perf_evsel__name(evsel
);
764 if (!script
->name_width
)
765 script
->name_width
= perf_evlist__max_name_len(script
->session
->evlist
);
767 printf("%*s: ", script
->name_width
,
768 evname
? evname
: "[unknown]");
772 print_sample_flags(sample
->flags
);
774 if (is_bts_event(attr
)) {
775 print_sample_bts(sample
, evsel
, thread
, al
);
779 if (PRINT_FIELD(TRACE
))
780 event_format__print(evsel
->tp_format
, sample
->cpu
,
781 sample
->raw_data
, sample
->raw_size
);
782 if (PRINT_FIELD(ADDR
))
783 print_sample_addr(sample
, thread
, attr
);
785 if (PRINT_FIELD(DATA_SRC
))
786 data_src__printf(sample
->data_src
);
788 if (PRINT_FIELD(WEIGHT
))
789 printf("%16" PRIu64
, sample
->weight
);
791 if (PRINT_FIELD(IP
)) {
792 struct callchain_cursor
*cursor
= NULL
;
794 if (symbol_conf
.use_callchain
&& sample
->callchain
&&
795 thread__resolve_callchain(al
->thread
, &callchain_cursor
, evsel
,
796 sample
, NULL
, NULL
, scripting_max_stack
) == 0)
797 cursor
= &callchain_cursor
;
799 putchar(cursor
? '\n' : ' ');
800 sample__fprintf_sym(sample
, al
, 0, output
[attr
->type
].print_ip_opts
, cursor
, stdout
);
803 if (PRINT_FIELD(IREGS
))
804 print_sample_iregs(sample
, attr
);
806 if (PRINT_FIELD(BRSTACK
))
807 print_sample_brstack(sample
);
808 else if (PRINT_FIELD(BRSTACKSYM
))
809 print_sample_brstacksym(sample
, thread
);
811 if (perf_evsel__is_bpf_output(evsel
) && PRINT_FIELD(BPF_OUTPUT
))
812 print_sample_bpf_output(sample
);
817 static struct scripting_ops
*scripting_ops
;
819 static void __process_stat(struct perf_evsel
*counter
, u64 tstamp
)
821 int nthreads
= thread_map__nr(counter
->threads
);
822 int ncpus
= perf_evsel__nr_cpus(counter
);
824 static int header_printed
;
826 if (counter
->system_wide
)
829 if (!header_printed
) {
830 printf("%3s %8s %15s %15s %15s %15s %s\n",
831 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
835 for (thread
= 0; thread
< nthreads
; thread
++) {
836 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
837 struct perf_counts_values
*counts
;
839 counts
= perf_counts(counter
->counts
, cpu
, thread
);
841 printf("%3d %8d %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %15" PRIu64
" %s\n",
842 counter
->cpus
->map
[cpu
],
843 thread_map__pid(counter
->threads
, thread
),
848 perf_evsel__name(counter
));
853 static void process_stat(struct perf_evsel
*counter
, u64 tstamp
)
855 if (scripting_ops
&& scripting_ops
->process_stat
)
856 scripting_ops
->process_stat(&stat_config
, counter
, tstamp
);
858 __process_stat(counter
, tstamp
);
861 static void process_stat_interval(u64 tstamp
)
863 if (scripting_ops
&& scripting_ops
->process_stat_interval
)
864 scripting_ops
->process_stat_interval(tstamp
);
867 static void setup_scripting(void)
869 setup_perl_scripting();
870 setup_python_scripting();
873 static int flush_scripting(void)
875 return scripting_ops
? scripting_ops
->flush_script() : 0;
878 static int cleanup_scripting(void)
880 pr_debug("\nperf script stopped\n");
882 return scripting_ops
? scripting_ops
->stop_script() : 0;
885 static int process_sample_event(struct perf_tool
*tool
,
886 union perf_event
*event
,
887 struct perf_sample
*sample
,
888 struct perf_evsel
*evsel
,
889 struct machine
*machine
)
891 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
892 struct addr_location al
;
895 if (sample
->time
< last_timestamp
) {
896 pr_err("Samples misordered, previous: %" PRIu64
897 " this: %" PRIu64
"\n", last_timestamp
,
901 last_timestamp
= sample
->time
;
905 if (machine__resolve(machine
, &al
, sample
) < 0) {
906 pr_err("problem processing %d event, skipping it.\n",
914 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
))
918 scripting_ops
->process_event(event
, sample
, evsel
, &al
);
920 process_event(scr
, sample
, evsel
, &al
);
923 addr_location__put(&al
);
927 static int process_attr(struct perf_tool
*tool
, union perf_event
*event
,
928 struct perf_evlist
**pevlist
)
930 struct perf_script
*scr
= container_of(tool
, struct perf_script
, tool
);
931 struct perf_evlist
*evlist
;
932 struct perf_evsel
*evsel
, *pos
;
935 err
= perf_event__process_attr(tool
, event
, pevlist
);
940 evsel
= perf_evlist__last(*pevlist
);
942 if (evsel
->attr
.type
>= PERF_TYPE_MAX
)
945 evlist__for_each(evlist
, pos
) {
946 if (pos
->attr
.type
== evsel
->attr
.type
&& pos
!= evsel
)
950 set_print_ip_opts(&evsel
->attr
);
952 if (evsel
->attr
.sample_type
)
953 err
= perf_evsel__check_attr(evsel
, scr
->session
);
958 static int process_comm_event(struct perf_tool
*tool
,
959 union perf_event
*event
,
960 struct perf_sample
*sample
,
961 struct machine
*machine
)
963 struct thread
*thread
;
964 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
965 struct perf_session
*session
= script
->session
;
966 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
969 thread
= machine__findnew_thread(machine
, event
->comm
.pid
, event
->comm
.tid
);
970 if (thread
== NULL
) {
971 pr_debug("problem processing COMM event, skipping it.\n");
975 if (perf_event__process_comm(tool
, event
, sample
, machine
) < 0)
978 if (!evsel
->attr
.sample_id_all
) {
981 sample
->tid
= event
->comm
.tid
;
982 sample
->pid
= event
->comm
.pid
;
984 print_sample_start(sample
, thread
, evsel
);
985 perf_event__fprintf(event
, stdout
);
992 static int process_fork_event(struct perf_tool
*tool
,
993 union perf_event
*event
,
994 struct perf_sample
*sample
,
995 struct machine
*machine
)
997 struct thread
*thread
;
998 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
999 struct perf_session
*session
= script
->session
;
1000 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1002 if (perf_event__process_fork(tool
, event
, sample
, machine
) < 0)
1005 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
1006 if (thread
== NULL
) {
1007 pr_debug("problem processing FORK event, skipping it.\n");
1011 if (!evsel
->attr
.sample_id_all
) {
1013 sample
->time
= event
->fork
.time
;
1014 sample
->tid
= event
->fork
.tid
;
1015 sample
->pid
= event
->fork
.pid
;
1017 print_sample_start(sample
, thread
, evsel
);
1018 perf_event__fprintf(event
, stdout
);
1019 thread__put(thread
);
1023 static int process_exit_event(struct perf_tool
*tool
,
1024 union perf_event
*event
,
1025 struct perf_sample
*sample
,
1026 struct machine
*machine
)
1029 struct thread
*thread
;
1030 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1031 struct perf_session
*session
= script
->session
;
1032 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1034 thread
= machine__findnew_thread(machine
, event
->fork
.pid
, event
->fork
.tid
);
1035 if (thread
== NULL
) {
1036 pr_debug("problem processing EXIT event, skipping it.\n");
1040 if (!evsel
->attr
.sample_id_all
) {
1043 sample
->tid
= event
->fork
.tid
;
1044 sample
->pid
= event
->fork
.pid
;
1046 print_sample_start(sample
, thread
, evsel
);
1047 perf_event__fprintf(event
, stdout
);
1049 if (perf_event__process_exit(tool
, event
, sample
, machine
) < 0)
1052 thread__put(thread
);
1056 static int process_mmap_event(struct perf_tool
*tool
,
1057 union perf_event
*event
,
1058 struct perf_sample
*sample
,
1059 struct machine
*machine
)
1061 struct thread
*thread
;
1062 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1063 struct perf_session
*session
= script
->session
;
1064 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1066 if (perf_event__process_mmap(tool
, event
, sample
, machine
) < 0)
1069 thread
= machine__findnew_thread(machine
, event
->mmap
.pid
, event
->mmap
.tid
);
1070 if (thread
== NULL
) {
1071 pr_debug("problem processing MMAP event, skipping it.\n");
1075 if (!evsel
->attr
.sample_id_all
) {
1078 sample
->tid
= event
->mmap
.tid
;
1079 sample
->pid
= event
->mmap
.pid
;
1081 print_sample_start(sample
, thread
, evsel
);
1082 perf_event__fprintf(event
, stdout
);
1083 thread__put(thread
);
1087 static int process_mmap2_event(struct perf_tool
*tool
,
1088 union perf_event
*event
,
1089 struct perf_sample
*sample
,
1090 struct machine
*machine
)
1092 struct thread
*thread
;
1093 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1094 struct perf_session
*session
= script
->session
;
1095 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1097 if (perf_event__process_mmap2(tool
, event
, sample
, machine
) < 0)
1100 thread
= machine__findnew_thread(machine
, event
->mmap2
.pid
, event
->mmap2
.tid
);
1101 if (thread
== NULL
) {
1102 pr_debug("problem processing MMAP2 event, skipping it.\n");
1106 if (!evsel
->attr
.sample_id_all
) {
1109 sample
->tid
= event
->mmap2
.tid
;
1110 sample
->pid
= event
->mmap2
.pid
;
1112 print_sample_start(sample
, thread
, evsel
);
1113 perf_event__fprintf(event
, stdout
);
1114 thread__put(thread
);
1118 static int process_switch_event(struct perf_tool
*tool
,
1119 union perf_event
*event
,
1120 struct perf_sample
*sample
,
1121 struct machine
*machine
)
1123 struct thread
*thread
;
1124 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1125 struct perf_session
*session
= script
->session
;
1126 struct perf_evsel
*evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
1128 if (perf_event__process_switch(tool
, event
, sample
, machine
) < 0)
1131 thread
= machine__findnew_thread(machine
, sample
->pid
,
1133 if (thread
== NULL
) {
1134 pr_debug("problem processing SWITCH event, skipping it.\n");
1138 print_sample_start(sample
, thread
, evsel
);
1139 perf_event__fprintf(event
, stdout
);
1140 thread__put(thread
);
1144 static void sig_handler(int sig __maybe_unused
)
1149 static int __cmd_script(struct perf_script
*script
)
1153 signal(SIGINT
, sig_handler
);
1155 /* override event processing functions */
1156 if (script
->show_task_events
) {
1157 script
->tool
.comm
= process_comm_event
;
1158 script
->tool
.fork
= process_fork_event
;
1159 script
->tool
.exit
= process_exit_event
;
1161 if (script
->show_mmap_events
) {
1162 script
->tool
.mmap
= process_mmap_event
;
1163 script
->tool
.mmap2
= process_mmap2_event
;
1165 if (script
->show_switch_events
)
1166 script
->tool
.context_switch
= process_switch_event
;
1168 ret
= perf_session__process_events(script
->session
);
1171 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
1176 struct script_spec
{
1177 struct list_head node
;
1178 struct scripting_ops
*ops
;
1182 static LIST_HEAD(script_specs
);
1184 static struct script_spec
*script_spec__new(const char *spec
,
1185 struct scripting_ops
*ops
)
1187 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
1190 strcpy(s
->spec
, spec
);
1197 static void script_spec__add(struct script_spec
*s
)
1199 list_add_tail(&s
->node
, &script_specs
);
1202 static struct script_spec
*script_spec__find(const char *spec
)
1204 struct script_spec
*s
;
1206 list_for_each_entry(s
, &script_specs
, node
)
1207 if (strcasecmp(s
->spec
, spec
) == 0)
1212 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
1214 struct script_spec
*s
;
1216 s
= script_spec__find(spec
);
1220 s
= script_spec__new(spec
, ops
);
1224 script_spec__add(s
);
1229 static struct scripting_ops
*script_spec__lookup(const char *spec
)
1231 struct script_spec
*s
= script_spec__find(spec
);
1238 static void list_available_languages(void)
1240 struct script_spec
*s
;
1242 fprintf(stderr
, "\n");
1243 fprintf(stderr
, "Scripting language extensions (used in "
1244 "perf script -s [spec:]script.[spec]):\n\n");
1246 list_for_each_entry(s
, &script_specs
, node
)
1247 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
1249 fprintf(stderr
, "\n");
1252 static int parse_scriptname(const struct option
*opt __maybe_unused
,
1253 const char *str
, int unset __maybe_unused
)
1255 char spec
[PATH_MAX
];
1256 const char *script
, *ext
;
1259 if (strcmp(str
, "lang") == 0) {
1260 list_available_languages();
1264 script
= strchr(str
, ':');
1267 if (len
>= PATH_MAX
) {
1268 fprintf(stderr
, "invalid language specifier");
1271 strncpy(spec
, str
, len
);
1273 scripting_ops
= script_spec__lookup(spec
);
1274 if (!scripting_ops
) {
1275 fprintf(stderr
, "invalid language specifier");
1281 ext
= strrchr(script
, '.');
1283 fprintf(stderr
, "invalid script extension");
1286 scripting_ops
= script_spec__lookup(++ext
);
1287 if (!scripting_ops
) {
1288 fprintf(stderr
, "invalid script extension");
1293 script_name
= strdup(script
);
1298 static int parse_output_fields(const struct option
*opt __maybe_unused
,
1299 const char *arg
, int unset __maybe_unused
)
1302 int i
, imax
= ARRAY_SIZE(all_output_options
);
1305 char *str
= strdup(arg
);
1311 /* first word can state for which event type the user is specifying
1312 * the fields. If no type exists, the specified fields apply to all
1313 * event types found in the file minus the invalid fields for a type.
1315 tok
= strchr(str
, ':');
1319 if (!strcmp(str
, "hw"))
1320 type
= PERF_TYPE_HARDWARE
;
1321 else if (!strcmp(str
, "sw"))
1322 type
= PERF_TYPE_SOFTWARE
;
1323 else if (!strcmp(str
, "trace"))
1324 type
= PERF_TYPE_TRACEPOINT
;
1325 else if (!strcmp(str
, "raw"))
1326 type
= PERF_TYPE_RAW
;
1327 else if (!strcmp(str
, "break"))
1328 type
= PERF_TYPE_BREAKPOINT
;
1330 fprintf(stderr
, "Invalid event type in field string.\n");
1335 if (output
[type
].user_set
)
1336 pr_warning("Overriding previous field request for %s events.\n",
1339 output
[type
].fields
= 0;
1340 output
[type
].user_set
= true;
1341 output
[type
].wildcard_set
= false;
1345 if (strlen(str
) == 0) {
1347 "Cannot set fields to 'none' for all event types.\n");
1352 if (output_set_by_user())
1353 pr_warning("Overriding previous field request for all events.\n");
1355 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1356 output
[j
].fields
= 0;
1357 output
[j
].user_set
= true;
1358 output
[j
].wildcard_set
= true;
1362 for (tok
= strtok(tok
, ","); tok
; tok
= strtok(NULL
, ",")) {
1363 for (i
= 0; i
< imax
; ++i
) {
1364 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
1367 if (i
== imax
&& strcmp(tok
, "flags") == 0) {
1372 fprintf(stderr
, "Invalid field requested.\n");
1378 /* add user option to all events types for
1381 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
1382 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
1383 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1384 all_output_options
[i
].str
, event_type(j
));
1386 output
[j
].fields
|= all_output_options
[i
].field
;
1389 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
1390 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
1391 all_output_options
[i
].str
, event_type(type
));
1396 output
[type
].fields
|= all_output_options
[i
].field
;
1401 if (output
[type
].fields
== 0) {
1402 pr_debug("No fields requested for %s type. "
1403 "Events will not be displayed.\n", event_type(type
));
1412 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1413 static int is_directory(const char *base_path
, const struct dirent
*dent
)
1415 char path
[PATH_MAX
];
1418 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
1419 if (stat(path
, &st
))
1422 return S_ISDIR(st
.st_mode
);
1425 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1426 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1427 if ((lang_dirent->d_type == DT_DIR || \
1428 (lang_dirent->d_type == DT_UNKNOWN && \
1429 is_directory(scripts_path, lang_dirent))) && \
1430 (strcmp(lang_dirent->d_name, ".")) && \
1431 (strcmp(lang_dirent->d_name, "..")))
1433 #define for_each_script(lang_path, lang_dir, script_dirent) \
1434 while ((script_dirent = readdir(lang_dir)) != NULL) \
1435 if (script_dirent->d_type != DT_DIR && \
1436 (script_dirent->d_type != DT_UNKNOWN || \
1437 !is_directory(lang_path, script_dirent)))
1440 #define RECORD_SUFFIX "-record"
1441 #define REPORT_SUFFIX "-report"
1443 struct script_desc
{
1444 struct list_head node
;
1450 static LIST_HEAD(script_descs
);
1452 static struct script_desc
*script_desc__new(const char *name
)
1454 struct script_desc
*s
= zalloc(sizeof(*s
));
1456 if (s
!= NULL
&& name
)
1457 s
->name
= strdup(name
);
1462 static void script_desc__delete(struct script_desc
*s
)
1465 zfree(&s
->half_liner
);
1470 static void script_desc__add(struct script_desc
*s
)
1472 list_add_tail(&s
->node
, &script_descs
);
1475 static struct script_desc
*script_desc__find(const char *name
)
1477 struct script_desc
*s
;
1479 list_for_each_entry(s
, &script_descs
, node
)
1480 if (strcasecmp(s
->name
, name
) == 0)
1485 static struct script_desc
*script_desc__findnew(const char *name
)
1487 struct script_desc
*s
= script_desc__find(name
);
1492 s
= script_desc__new(name
);
1494 goto out_delete_desc
;
1496 script_desc__add(s
);
1501 script_desc__delete(s
);
1506 static const char *ends_with(const char *str
, const char *suffix
)
1508 size_t suffix_len
= strlen(suffix
);
1509 const char *p
= str
;
1511 if (strlen(str
) > suffix_len
) {
1512 p
= str
+ strlen(str
) - suffix_len
;
1513 if (!strncmp(p
, suffix
, suffix_len
))
1520 static int read_script_info(struct script_desc
*desc
, const char *filename
)
1522 char line
[BUFSIZ
], *p
;
1525 fp
= fopen(filename
, "r");
1529 while (fgets(line
, sizeof(line
), fp
)) {
1536 if (strlen(p
) && *p
== '!')
1540 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
1541 p
[strlen(p
) - 1] = '\0';
1543 if (!strncmp(p
, "description:", strlen("description:"))) {
1544 p
+= strlen("description:");
1545 desc
->half_liner
= strdup(ltrim(p
));
1549 if (!strncmp(p
, "args:", strlen("args:"))) {
1550 p
+= strlen("args:");
1551 desc
->args
= strdup(ltrim(p
));
1561 static char *get_script_root(struct dirent
*script_dirent
, const char *suffix
)
1563 char *script_root
, *str
;
1565 script_root
= strdup(script_dirent
->d_name
);
1569 str
= (char *)ends_with(script_root
, suffix
);
1579 static int list_available_scripts(const struct option
*opt __maybe_unused
,
1580 const char *s __maybe_unused
,
1581 int unset __maybe_unused
)
1583 struct dirent
*script_dirent
, *lang_dirent
;
1584 char scripts_path
[MAXPATHLEN
];
1585 DIR *scripts_dir
, *lang_dir
;
1586 char script_path
[MAXPATHLEN
];
1587 char lang_path
[MAXPATHLEN
];
1588 struct script_desc
*desc
;
1589 char first_half
[BUFSIZ
];
1592 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
1594 scripts_dir
= opendir(scripts_path
);
1598 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
1599 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1600 lang_dirent
->d_name
);
1601 lang_dir
= opendir(lang_path
);
1605 for_each_script(lang_path
, lang_dir
, script_dirent
) {
1606 script_root
= get_script_root(script_dirent
, REPORT_SUFFIX
);
1608 desc
= script_desc__findnew(script_root
);
1609 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1610 lang_path
, script_dirent
->d_name
);
1611 read_script_info(desc
, script_path
);
1617 fprintf(stdout
, "List of available trace scripts:\n");
1618 list_for_each_entry(desc
, &script_descs
, node
) {
1619 sprintf(first_half
, "%s %s", desc
->name
,
1620 desc
->args
? desc
->args
: "");
1621 fprintf(stdout
, " %-36s %s\n", first_half
,
1622 desc
->half_liner
? desc
->half_liner
: "");
1629 * Some scripts specify the required events in their "xxx-record" file,
1630 * this function will check if the events in perf.data match those
1631 * mentioned in the "xxx-record".
1633 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1634 * which is covered well now. And new parsing code should be added to
1635 * cover the future complexing formats like event groups etc.
1637 static int check_ev_match(char *dir_name
, char *scriptname
,
1638 struct perf_session
*session
)
1640 char filename
[MAXPATHLEN
], evname
[128];
1641 char line
[BUFSIZ
], *p
;
1642 struct perf_evsel
*pos
;
1646 sprintf(filename
, "%s/bin/%s-record", dir_name
, scriptname
);
1648 fp
= fopen(filename
, "r");
1652 while (fgets(line
, sizeof(line
), fp
)) {
1658 p
= strstr(p
, "-e");
1664 len
= strcspn(p
, " \t");
1668 snprintf(evname
, len
+ 1, "%s", p
);
1671 evlist__for_each(session
->evlist
, pos
) {
1672 if (!strcmp(perf_evsel__name(pos
), evname
)) {
1690 * Return -1 if none is found, otherwise the actual scripts number.
1692 * Currently the only user of this function is the script browser, which
1693 * will list all statically runnable scripts, select one, execute it and
1694 * show the output in a perf browser.
1696 int find_scripts(char **scripts_array
, char **scripts_path_array
)
1698 struct dirent
*script_dirent
, *lang_dirent
;
1699 char scripts_path
[MAXPATHLEN
], lang_path
[MAXPATHLEN
];
1700 DIR *scripts_dir
, *lang_dir
;
1701 struct perf_session
*session
;
1702 struct perf_data_file file
= {
1704 .mode
= PERF_DATA_MODE_READ
,
1709 session
= perf_session__new(&file
, false, NULL
);
1713 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
1715 scripts_dir
= opendir(scripts_path
);
1717 perf_session__delete(session
);
1721 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
1722 snprintf(lang_path
, MAXPATHLEN
, "%s/%s", scripts_path
,
1723 lang_dirent
->d_name
);
1725 if (strstr(lang_path
, "perl"))
1729 if (strstr(lang_path
, "python"))
1733 lang_dir
= opendir(lang_path
);
1737 for_each_script(lang_path
, lang_dir
, script_dirent
) {
1738 /* Skip those real time scripts: xxxtop.p[yl] */
1739 if (strstr(script_dirent
->d_name
, "top."))
1741 sprintf(scripts_path_array
[i
], "%s/%s", lang_path
,
1742 script_dirent
->d_name
);
1743 temp
= strchr(script_dirent
->d_name
, '.');
1744 snprintf(scripts_array
[i
],
1745 (temp
- script_dirent
->d_name
) + 1,
1746 "%s", script_dirent
->d_name
);
1748 if (check_ev_match(lang_path
,
1749 scripts_array
[i
], session
))
1757 closedir(scripts_dir
);
1758 perf_session__delete(session
);
1762 static char *get_script_path(const char *script_root
, const char *suffix
)
1764 struct dirent
*script_dirent
, *lang_dirent
;
1765 char scripts_path
[MAXPATHLEN
];
1766 char script_path
[MAXPATHLEN
];
1767 DIR *scripts_dir
, *lang_dir
;
1768 char lang_path
[MAXPATHLEN
];
1769 char *__script_root
;
1771 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", get_argv_exec_path());
1773 scripts_dir
= opendir(scripts_path
);
1777 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
) {
1778 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1779 lang_dirent
->d_name
);
1780 lang_dir
= opendir(lang_path
);
1784 for_each_script(lang_path
, lang_dir
, script_dirent
) {
1785 __script_root
= get_script_root(script_dirent
, suffix
);
1786 if (__script_root
&& !strcmp(script_root
, __script_root
)) {
1787 free(__script_root
);
1789 closedir(scripts_dir
);
1790 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1791 lang_path
, script_dirent
->d_name
);
1792 return strdup(script_path
);
1794 free(__script_root
);
1798 closedir(scripts_dir
);
1803 static bool is_top_script(const char *script_path
)
1805 return ends_with(script_path
, "top") == NULL
? false : true;
1808 static int has_required_arg(char *script_path
)
1810 struct script_desc
*desc
;
1814 desc
= script_desc__new(NULL
);
1816 if (read_script_info(desc
, script_path
))
1822 for (p
= desc
->args
; *p
; p
++)
1826 script_desc__delete(desc
);
1831 static int have_cmd(int argc
, const char **argv
)
1833 char **__argv
= malloc(sizeof(const char *) * argc
);
1836 pr_err("malloc failed\n");
1840 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
1841 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
1842 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
1845 system_wide
= (argc
== 0);
1850 static void script__setup_sample_type(struct perf_script
*script
)
1852 struct perf_session
*session
= script
->session
;
1853 u64 sample_type
= perf_evlist__combined_sample_type(session
->evlist
);
1855 if (symbol_conf
.use_callchain
|| symbol_conf
.cumulate_callchain
) {
1856 if ((sample_type
& PERF_SAMPLE_REGS_USER
) &&
1857 (sample_type
& PERF_SAMPLE_STACK_USER
))
1858 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
1859 else if (sample_type
& PERF_SAMPLE_BRANCH_STACK
)
1860 callchain_param
.record_mode
= CALLCHAIN_LBR
;
1862 callchain_param
.record_mode
= CALLCHAIN_FP
;
1866 static int process_stat_round_event(struct perf_tool
*tool __maybe_unused
,
1867 union perf_event
*event
,
1868 struct perf_session
*session
)
1870 struct stat_round_event
*round
= &event
->stat_round
;
1871 struct perf_evsel
*counter
;
1873 evlist__for_each(session
->evlist
, counter
) {
1874 perf_stat_process_counter(&stat_config
, counter
);
1875 process_stat(counter
, round
->time
);
1878 process_stat_interval(round
->time
);
1882 static int process_stat_config_event(struct perf_tool
*tool __maybe_unused
,
1883 union perf_event
*event
,
1884 struct perf_session
*session __maybe_unused
)
1886 perf_event__read_stat_config(&stat_config
, &event
->stat_config
);
1890 static int set_maps(struct perf_script
*script
)
1892 struct perf_evlist
*evlist
= script
->session
->evlist
;
1894 if (!script
->cpus
|| !script
->threads
)
1897 if (WARN_ONCE(script
->allocated
, "stats double allocation\n"))
1900 perf_evlist__set_maps(evlist
, script
->cpus
, script
->threads
);
1902 if (perf_evlist__alloc_stats(evlist
, true))
1905 script
->allocated
= true;
1910 int process_thread_map_event(struct perf_tool
*tool
,
1911 union perf_event
*event
,
1912 struct perf_session
*session __maybe_unused
)
1914 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1916 if (script
->threads
) {
1917 pr_warning("Extra thread map event, ignoring.\n");
1921 script
->threads
= thread_map__new_event(&event
->thread_map
);
1922 if (!script
->threads
)
1925 return set_maps(script
);
1929 int process_cpu_map_event(struct perf_tool
*tool __maybe_unused
,
1930 union perf_event
*event
,
1931 struct perf_session
*session __maybe_unused
)
1933 struct perf_script
*script
= container_of(tool
, struct perf_script
, tool
);
1936 pr_warning("Extra cpu map event, ignoring.\n");
1940 script
->cpus
= cpu_map__new_data(&event
->cpu_map
.data
);
1944 return set_maps(script
);
1947 int cmd_script(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1949 bool show_full_info
= false;
1950 bool header
= false;
1951 bool header_only
= false;
1952 bool script_started
= false;
1953 char *rec_script_path
= NULL
;
1954 char *rep_script_path
= NULL
;
1955 struct perf_session
*session
;
1956 struct itrace_synth_opts itrace_synth_opts
= { .set
= false, };
1957 char *script_path
= NULL
;
1958 const char **__argv
;
1960 struct perf_script script
= {
1962 .sample
= process_sample_event
,
1963 .mmap
= perf_event__process_mmap
,
1964 .mmap2
= perf_event__process_mmap2
,
1965 .comm
= perf_event__process_comm
,
1966 .exit
= perf_event__process_exit
,
1967 .fork
= perf_event__process_fork
,
1968 .attr
= process_attr
,
1969 .event_update
= perf_event__process_event_update
,
1970 .tracing_data
= perf_event__process_tracing_data
,
1971 .build_id
= perf_event__process_build_id
,
1972 .id_index
= perf_event__process_id_index
,
1973 .auxtrace_info
= perf_event__process_auxtrace_info
,
1974 .auxtrace
= perf_event__process_auxtrace
,
1975 .auxtrace_error
= perf_event__process_auxtrace_error
,
1976 .stat
= perf_event__process_stat_event
,
1977 .stat_round
= process_stat_round_event
,
1978 .stat_config
= process_stat_config_event
,
1979 .thread_map
= process_thread_map_event
,
1980 .cpu_map
= process_cpu_map_event
,
1981 .ordered_events
= true,
1982 .ordering_requires_timestamps
= true,
1985 struct perf_data_file file
= {
1986 .mode
= PERF_DATA_MODE_READ
,
1988 const struct option options
[] = {
1989 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1990 "dump raw trace in ASCII"),
1991 OPT_INCR('v', "verbose", &verbose
,
1992 "be more verbose (show symbol address, etc)"),
1993 OPT_BOOLEAN('L', "Latency", &latency_format
,
1994 "show latency attributes (irqs/preemption disabled, etc)"),
1995 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
1996 list_available_scripts
),
1997 OPT_CALLBACK('s', "script", NULL
, "name",
1998 "script file name (lang:script name, script name, or *)",
2000 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
2001 "generate perf-script.xx script in specified language"),
2002 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
2003 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
2004 "do various checks like samples ordering and lost events"),
2005 OPT_BOOLEAN(0, "header", &header
, "Show data header."),
2006 OPT_BOOLEAN(0, "header-only", &header_only
, "Show only data header."),
2007 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
2008 "file", "vmlinux pathname"),
2009 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
2010 "file", "kallsyms pathname"),
2011 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
2012 "When printing symbols do not display call chain"),
2013 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
2014 "Look for files with symbols relative to this directory"),
2015 OPT_CALLBACK('F', "fields", NULL
, "str",
2016 "comma separated output fields prepend with 'type:'. "
2017 "Valid types: hw,sw,trace,raw. "
2018 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2019 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields
),
2020 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
2021 "system-wide collection from all CPUs"),
2022 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
2023 "only consider these symbols"),
2024 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
2025 OPT_STRING('c', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
2026 "only display events for these comms"),
2027 OPT_STRING(0, "pid", &symbol_conf
.pid_list_str
, "pid[,pid...]",
2028 "only consider symbols in these pids"),
2029 OPT_STRING(0, "tid", &symbol_conf
.tid_list_str
, "tid[,tid...]",
2030 "only consider symbols in these tids"),
2031 OPT_UINTEGER(0, "max-stack", &scripting_max_stack
,
2032 "Set the maximum stack depth when parsing the callchain, "
2033 "anything beyond the specified depth will be ignored. "
2034 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH
)),
2035 OPT_BOOLEAN('I', "show-info", &show_full_info
,
2036 "display extended information from perf.data file"),
2037 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf
.show_kernel_path
,
2038 "Show the path of [kernel.kallsyms]"),
2039 OPT_BOOLEAN('\0', "show-task-events", &script
.show_task_events
,
2040 "Show the fork/comm/exit events"),
2041 OPT_BOOLEAN('\0', "show-mmap-events", &script
.show_mmap_events
,
2042 "Show the mmap events"),
2043 OPT_BOOLEAN('\0', "show-switch-events", &script
.show_switch_events
,
2044 "Show context switch events (if recorded)"),
2045 OPT_BOOLEAN('f', "force", &file
.force
, "don't complain, do it"),
2046 OPT_BOOLEAN(0, "ns", &nanosecs
,
2047 "Use 9 decimal places when displaying time"),
2048 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts
, NULL
, "opts",
2049 "Instruction Tracing options",
2050 itrace_parse_synth_opts
),
2051 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename
,
2052 "Show full source file name path for source lines"),
2053 OPT_BOOLEAN(0, "demangle", &symbol_conf
.demangle
,
2054 "Enable symbol demangling"),
2055 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf
.demangle_kernel
,
2056 "Enable kernel symbol demangling"),
2060 const char * const script_subcommands
[] = { "record", "report", NULL
};
2061 const char *script_usage
[] = {
2062 "perf script [<options>]",
2063 "perf script [<options>] record <script> [<record-options>] <command>",
2064 "perf script [<options>] report <script> [script-args]",
2065 "perf script [<options>] <script> [<record-options>] <command>",
2066 "perf script [<options>] <top-script> [script-args]",
2070 scripting_max_stack
= sysctl_perf_event_max_stack
;
2074 argc
= parse_options_subcommand(argc
, argv
, options
, script_subcommands
, script_usage
,
2075 PARSE_OPT_STOP_AT_NON_OPTION
);
2077 file
.path
= input_name
;
2079 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
2080 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
2081 if (!rec_script_path
)
2082 return cmd_record(argc
, argv
, NULL
);
2085 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
2086 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
2087 if (!rep_script_path
) {
2089 "Please specify a valid report script"
2090 "(see 'perf script -l' for listing)\n");
2095 if (itrace_synth_opts
.callchain
&&
2096 itrace_synth_opts
.callchain_sz
> scripting_max_stack
)
2097 scripting_max_stack
= itrace_synth_opts
.callchain_sz
;
2099 /* make sure PERF_EXEC_PATH is set for scripts */
2100 set_argv_exec_path(get_argv_exec_path());
2102 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
2107 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
2108 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
2110 if (!rec_script_path
&& !rep_script_path
) {
2111 usage_with_options_msg(script_usage
, options
,
2112 "Couldn't find script `%s'\n\n See perf"
2113 " script -l for available scripts.\n", argv
[0]);
2116 if (is_top_script(argv
[0])) {
2117 rep_args
= argc
- 1;
2121 rep_args
= has_required_arg(rep_script_path
);
2122 rec_args
= (argc
- 1) - rep_args
;
2124 usage_with_options_msg(script_usage
, options
,
2125 "`%s' script requires options."
2126 "\n\n See perf script -l for available "
2127 "scripts and options.\n", argv
[0]);
2131 if (pipe(live_pipe
) < 0) {
2132 perror("failed to create pipe");
2138 perror("failed to fork");
2145 dup2(live_pipe
[1], 1);
2146 close(live_pipe
[0]);
2148 if (is_top_script(argv
[0])) {
2150 } else if (!system_wide
) {
2151 if (have_cmd(argc
- rep_args
, &argv
[rep_args
]) != 0) {
2157 __argv
= malloc((argc
+ 6) * sizeof(const char *));
2159 pr_err("malloc failed\n");
2164 __argv
[j
++] = "/bin/sh";
2165 __argv
[j
++] = rec_script_path
;
2171 for (i
= rep_args
+ 1; i
< argc
; i
++)
2172 __argv
[j
++] = argv
[i
];
2175 execvp("/bin/sh", (char **)__argv
);
2180 dup2(live_pipe
[0], 0);
2181 close(live_pipe
[1]);
2183 __argv
= malloc((argc
+ 4) * sizeof(const char *));
2185 pr_err("malloc failed\n");
2191 __argv
[j
++] = "/bin/sh";
2192 __argv
[j
++] = rep_script_path
;
2193 for (i
= 1; i
< rep_args
+ 1; i
++)
2194 __argv
[j
++] = argv
[i
];
2199 execvp("/bin/sh", (char **)__argv
);
2204 if (rec_script_path
)
2205 script_path
= rec_script_path
;
2206 if (rep_script_path
)
2207 script_path
= rep_script_path
;
2212 if (!rec_script_path
)
2213 system_wide
= false;
2214 else if (!system_wide
) {
2215 if (have_cmd(argc
- 1, &argv
[1]) != 0) {
2221 __argv
= malloc((argc
+ 2) * sizeof(const char *));
2223 pr_err("malloc failed\n");
2228 __argv
[j
++] = "/bin/sh";
2229 __argv
[j
++] = script_path
;
2232 for (i
= 2; i
< argc
; i
++)
2233 __argv
[j
++] = argv
[i
];
2236 execvp("/bin/sh", (char **)__argv
);
2244 session
= perf_session__new(&file
, false, &script
.tool
);
2245 if (session
== NULL
)
2248 if (header
|| header_only
) {
2249 perf_session__fprintf_info(session
, stdout
, show_full_info
);
2254 if (symbol__init(&session
->header
.env
) < 0)
2257 script
.session
= session
;
2258 script__setup_sample_type(&script
);
2260 session
->itrace_synth_opts
= &itrace_synth_opts
;
2263 err
= perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
);
2269 symbol_conf
.use_callchain
= true;
2271 symbol_conf
.use_callchain
= false;
2273 if (session
->tevent
.pevent
&&
2274 pevent_set_function_resolver(session
->tevent
.pevent
,
2275 machine__resolve_kernel_addr
,
2276 &session
->machines
.host
) < 0) {
2277 pr_err("%s: failed to set libtraceevent function resolver\n", __func__
);
2281 if (generate_script_lang
) {
2282 struct stat perf_stat
;
2285 if (output_set_by_user()) {
2287 "custom fields not supported for generated scripts");
2292 input
= open(file
.path
, O_RDONLY
); /* input_name */
2295 perror("failed to open file");
2299 err
= fstat(input
, &perf_stat
);
2301 perror("failed to stat file");
2305 if (!perf_stat
.st_size
) {
2306 fprintf(stderr
, "zero-sized file, nothing to do!\n");
2310 scripting_ops
= script_spec__lookup(generate_script_lang
);
2311 if (!scripting_ops
) {
2312 fprintf(stderr
, "invalid language specifier");
2317 err
= scripting_ops
->generate_script(session
->tevent
.pevent
,
2323 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
2326 pr_debug("perf script started with script %s\n\n", script_name
);
2327 script_started
= true;
2331 err
= perf_session__check_output_opt(session
);
2335 err
= __cmd_script(&script
);
2340 perf_evlist__free_stats(session
->evlist
);
2341 perf_session__delete(session
);
2344 cleanup_scripting();