1 // SPDX-License-Identifier: GPL-2.0
5 #include "util/cache.h"
6 #include "util/debug.h"
7 #include <subcmd/exec-cmd.h>
8 #include "util/header.h"
9 #include <subcmd/parse-options.h>
10 #include "util/perf_regs.h"
11 #include "util/session.h"
12 #include "util/tool.h"
14 #include "util/symbol.h"
15 #include "util/thread.h"
16 #include "util/trace-event.h"
17 #include "util/util.h"
18 #include "util/evlist.h"
19 #include "util/evsel.h"
20 #include "util/sort.h"
21 #include "util/data.h"
22 #include "util/auxtrace.h"
23 #include "util/cpumap.h"
24 #include "util/thread_map.h"
25 #include "util/stat.h"
26 #include "util/color.h"
27 #include "util/string2.h"
28 #include "util/thread-stack.h"
29 #include "util/time-utils.h"
30 #include "util/path.h"
31 #include "print_binary.h"
33 #include <linux/bitmap.h>
34 #include <linux/kernel.h>
35 #include <linux/stringify.h>
36 #include <linux/time64.h>
37 #include <sys/utsname.h>
39 #include "util/mem-events.h"
40 #include "util/dump-insn.h"
45 #include <sys/param.h>
46 #include <sys/types.h>
50 #include <subcmd/pager.h>
52 #include <linux/ctype.h>
54 static char const *script_name;
55 static char const *generate_script_lang;
57 static u64 initial_time;
58 static bool debug_mode;
59 static u64 last_timestamp;
60 static u64 nr_unordered;
61 static bool no_callchain;
62 static bool latency_format;
63 static bool system_wide;
64 static bool print_flags;
65 static const char *cpu_list;
66 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
67 static struct perf_stat_config stat_config;
68 static int max_blocks;
69 static bool native_arch;
71 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
73 enum perf_output_field {
74 PERF_OUTPUT_COMM = 1U << 0,
75 PERF_OUTPUT_TID = 1U << 1,
76 PERF_OUTPUT_PID = 1U << 2,
77 PERF_OUTPUT_TIME = 1U << 3,
78 PERF_OUTPUT_CPU = 1U << 4,
79 PERF_OUTPUT_EVNAME = 1U << 5,
80 PERF_OUTPUT_TRACE = 1U << 6,
81 PERF_OUTPUT_IP = 1U << 7,
82 PERF_OUTPUT_SYM = 1U << 8,
83 PERF_OUTPUT_DSO = 1U << 9,
84 PERF_OUTPUT_ADDR = 1U << 10,
85 PERF_OUTPUT_SYMOFFSET = 1U << 11,
86 PERF_OUTPUT_SRCLINE = 1U << 12,
87 PERF_OUTPUT_PERIOD = 1U << 13,
88 PERF_OUTPUT_IREGS = 1U << 14,
89 PERF_OUTPUT_BRSTACK = 1U << 15,
90 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
91 PERF_OUTPUT_DATA_SRC = 1U << 17,
92 PERF_OUTPUT_WEIGHT = 1U << 18,
93 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
94 PERF_OUTPUT_CALLINDENT = 1U << 20,
95 PERF_OUTPUT_INSN = 1U << 21,
96 PERF_OUTPUT_INSNLEN = 1U << 22,
97 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
98 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
99 PERF_OUTPUT_SYNTH = 1U << 25,
100 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
101 PERF_OUTPUT_UREGS = 1U << 27,
102 PERF_OUTPUT_METRIC = 1U << 28,
103 PERF_OUTPUT_MISC = 1U << 29,
104 PERF_OUTPUT_SRCCODE = 1U << 30,
105 PERF_OUTPUT_IPC = 1U << 31,
108 struct output_option {
110 enum perf_output_field field;
111 } all_output_options[] = {
112 {.str = "comm", .field = PERF_OUTPUT_COMM},
113 {.str = "tid", .field = PERF_OUTPUT_TID},
114 {.str = "pid", .field = PERF_OUTPUT_PID},
115 {.str = "time", .field = PERF_OUTPUT_TIME},
116 {.str = "cpu", .field = PERF_OUTPUT_CPU},
117 {.str = "event", .field = PERF_OUTPUT_EVNAME},
118 {.str = "trace", .field = PERF_OUTPUT_TRACE},
119 {.str = "ip", .field = PERF_OUTPUT_IP},
120 {.str = "sym", .field = PERF_OUTPUT_SYM},
121 {.str = "dso", .field = PERF_OUTPUT_DSO},
122 {.str = "addr", .field = PERF_OUTPUT_ADDR},
123 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
124 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
125 {.str = "period", .field = PERF_OUTPUT_PERIOD},
126 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
127 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
128 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
129 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
130 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
131 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
132 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
133 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
134 {.str = "insn", .field = PERF_OUTPUT_INSN},
135 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
136 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
137 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
138 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
139 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
140 {.str = "metric", .field = PERF_OUTPUT_METRIC},
141 {.str = "misc", .field = PERF_OUTPUT_MISC},
142 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
143 {.str = "ipc", .field = PERF_OUTPUT_IPC},
147 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
151 /* default set to maintain compatibility with current format */
155 unsigned int print_ip_opts;
159 } output[OUTPUT_TYPE_MAX] = {
161 [PERF_TYPE_HARDWARE] = {
164 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
165 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
166 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
167 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
168 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
170 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
173 [PERF_TYPE_SOFTWARE] = {
176 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
177 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
178 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
179 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
180 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
181 PERF_OUTPUT_BPF_OUTPUT,
183 .invalid_fields = PERF_OUTPUT_TRACE,
186 [PERF_TYPE_TRACEPOINT] = {
189 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
190 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
191 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
194 [PERF_TYPE_HW_CACHE] = {
197 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
198 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
199 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
200 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
201 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
203 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
209 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
210 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
211 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
212 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
213 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
214 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
215 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
217 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
220 [PERF_TYPE_BREAKPOINT] = {
223 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
224 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
225 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
226 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
227 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
229 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
232 [OUTPUT_TYPE_SYNTH] = {
235 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
236 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
237 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
238 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
239 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
241 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
245 struct perf_evsel_script {
249 /* For metric output */
254 static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
256 return (struct perf_evsel_script *)evsel->priv;
259 static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
260 struct perf_data *data)
262 struct perf_evsel_script *es = zalloc(sizeof(*es));
265 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
267 es->fp = fopen(es->filename, "w");
269 goto out_free_filename;
274 zfree(&es->filename);
280 static void perf_evsel_script__delete(struct perf_evsel_script *es)
282 zfree(&es->filename);
288 static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
292 fstat(fileno(es->fp), &st);
293 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
294 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
297 static inline int output_type(unsigned int type)
300 case PERF_TYPE_SYNTH:
301 return OUTPUT_TYPE_SYNTH;
307 static inline unsigned int attr_type(unsigned int type)
310 case OUTPUT_TYPE_SYNTH:
311 return PERF_TYPE_SYNTH;
317 static bool output_set_by_user(void)
320 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
321 if (output[j].user_set)
327 static const char *output_field2str(enum perf_output_field field)
329 int i, imax = ARRAY_SIZE(all_output_options);
330 const char *str = "";
332 for (i = 0; i < imax; ++i) {
333 if (all_output_options[i].field == field) {
334 str = all_output_options[i].str;
341 #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
343 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
344 u64 sample_type, const char *sample_msg,
345 enum perf_output_field field,
348 struct perf_event_attr *attr = &evsel->attr;
349 int type = output_type(attr->type);
352 if (attr->sample_type & sample_type)
355 if (output[type].user_set_fields & field) {
358 evname = perf_evsel__name(evsel);
359 pr_err("Samples for '%s' event do not have %s attribute set. "
360 "Cannot print '%s' field.\n",
361 evname, sample_msg, output_field2str(field));
365 /* user did not ask for it explicitly so remove from the default list */
366 output[type].fields &= ~field;
367 evname = perf_evsel__name(evsel);
368 pr_debug("Samples for '%s' event do not have %s attribute set. "
369 "Skipping '%s' field.\n",
370 evname, sample_msg, output_field2str(field));
375 static int perf_evsel__check_stype(struct perf_evsel *evsel,
376 u64 sample_type, const char *sample_msg,
377 enum perf_output_field field)
379 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
383 static int perf_evsel__check_attr(struct perf_evsel *evsel,
384 struct perf_session *session)
386 struct perf_event_attr *attr = &evsel->attr;
389 if (perf_header__has_feat(&session->header, HEADER_STAT))
392 allow_user_set = perf_header__has_feat(&session->header,
395 if (PRINT_FIELD(TRACE) &&
396 !perf_session__has_traces(session, "record -R"))
399 if (PRINT_FIELD(IP)) {
400 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
405 if (PRINT_FIELD(ADDR) &&
406 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
407 PERF_OUTPUT_ADDR, allow_user_set))
410 if (PRINT_FIELD(DATA_SRC) &&
411 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
412 PERF_OUTPUT_DATA_SRC))
415 if (PRINT_FIELD(WEIGHT) &&
416 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
420 if (PRINT_FIELD(SYM) &&
421 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
422 pr_err("Display of symbols requested but neither sample IP nor "
423 "sample address\navailable. Hence, no addresses to convert "
427 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
428 pr_err("Display of offsets requested but symbol is not"
432 if (PRINT_FIELD(DSO) &&
433 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
434 pr_err("Display of DSO requested but no address to convert.\n");
437 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
438 pr_err("Display of source line number requested but sample IP is not\n"
439 "selected. Hence, no address to lookup the source line number.\n");
442 if (PRINT_FIELD(BRSTACKINSN) &&
443 !(perf_evlist__combined_branch_type(session->evlist) &
444 PERF_SAMPLE_BRANCH_ANY)) {
445 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
446 "Hint: run 'perf record -b ...'\n");
449 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
450 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
451 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
454 if (PRINT_FIELD(TIME) &&
455 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
459 if (PRINT_FIELD(CPU) &&
460 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
461 PERF_OUTPUT_CPU, allow_user_set))
464 if (PRINT_FIELD(IREGS) &&
465 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
469 if (PRINT_FIELD(UREGS) &&
470 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
474 if (PRINT_FIELD(PHYS_ADDR) &&
475 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
476 PERF_OUTPUT_PHYS_ADDR))
482 static void set_print_ip_opts(struct perf_event_attr *attr)
484 unsigned int type = output_type(attr->type);
486 output[type].print_ip_opts = 0;
488 output[type].print_ip_opts |= EVSEL__PRINT_IP;
490 if (PRINT_FIELD(SYM))
491 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
493 if (PRINT_FIELD(DSO))
494 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
496 if (PRINT_FIELD(SYMOFFSET))
497 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
499 if (PRINT_FIELD(SRCLINE))
500 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
504 * verify all user requested events exist and the samples
505 * have the expected data
507 static int perf_session__check_output_opt(struct perf_session *session)
510 struct perf_evsel *evsel;
512 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
513 evsel = perf_session__find_first_evtype(session, attr_type(j));
516 * even if fields is set to 0 (ie., show nothing) event must
517 * exist if user explicitly includes it on the command line
519 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
520 j != OUTPUT_TYPE_SYNTH) {
521 pr_err("%s events do not exist. "
522 "Remove corresponding -F option to proceed.\n",
527 if (evsel && output[j].fields &&
528 perf_evsel__check_attr(evsel, session))
534 set_print_ip_opts(&evsel->attr);
538 bool use_callchain = false;
539 bool not_pipe = false;
541 evlist__for_each_entry(session->evlist, evsel) {
543 if (evsel__has_callchain(evsel)) {
544 use_callchain = true;
548 if (not_pipe && !use_callchain)
549 symbol_conf.use_callchain = false;
553 * set default for tracepoints to print symbols only
554 * if callchains are present
556 if (symbol_conf.use_callchain &&
557 !output[PERF_TYPE_TRACEPOINT].user_set) {
558 j = PERF_TYPE_TRACEPOINT;
560 evlist__for_each_entry(session->evlist, evsel) {
561 if (evsel->attr.type != j)
564 if (evsel__has_callchain(evsel)) {
565 output[j].fields |= PERF_OUTPUT_IP;
566 output[j].fields |= PERF_OUTPUT_SYM;
567 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
568 output[j].fields |= PERF_OUTPUT_DSO;
569 set_print_ip_opts(&evsel->attr);
579 static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
586 if (!regs || !regs->regs)
589 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
591 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
592 u64 val = regs->regs[i++];
593 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
601 static int perf_sample__fprintf_iregs(struct perf_sample *sample,
602 struct perf_event_attr *attr, FILE *fp)
604 return perf_sample__fprintf_regs(&sample->intr_regs,
605 attr->sample_regs_intr, fp);
608 static int perf_sample__fprintf_uregs(struct perf_sample *sample,
609 struct perf_event_attr *attr, FILE *fp)
611 return perf_sample__fprintf_regs(&sample->user_regs,
612 attr->sample_regs_user, fp);
615 static int perf_sample__fprintf_start(struct perf_sample *sample,
616 struct thread *thread,
617 struct perf_evsel *evsel,
620 struct perf_event_attr *attr = &evsel->attr;
622 unsigned long long nsecs;
625 if (PRINT_FIELD(COMM)) {
627 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
628 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
629 printed += fprintf(fp, "%s ", thread__comm_str(thread));
631 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
634 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
635 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
636 else if (PRINT_FIELD(PID))
637 printed += fprintf(fp, "%5d ", sample->pid);
638 else if (PRINT_FIELD(TID))
639 printed += fprintf(fp, "%5d ", sample->tid);
641 if (PRINT_FIELD(CPU)) {
643 printed += fprintf(fp, "%3d ", sample->cpu);
645 printed += fprintf(fp, "[%03d] ", sample->cpu);
648 if (PRINT_FIELD(MISC)) {
652 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
655 ret += fprintf(fp, "K");
657 ret += fprintf(fp, "U");
659 ret += fprintf(fp, "H");
660 if (has(GUEST_KERNEL))
661 ret += fprintf(fp, "G");
663 ret += fprintf(fp, "g");
666 case PERF_RECORD_MMAP:
667 case PERF_RECORD_MMAP2:
669 ret += fprintf(fp, "M");
671 case PERF_RECORD_COMM:
673 ret += fprintf(fp, "E");
675 case PERF_RECORD_SWITCH:
676 case PERF_RECORD_SWITCH_CPU_WIDE:
677 if (has(SWITCH_OUT)) {
678 ret += fprintf(fp, "S");
679 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
680 ret += fprintf(fp, "p");
688 ret += fprintf(fp, "%*s", 6 - ret, " ");
692 if (PRINT_FIELD(TIME)) {
693 u64 t = sample->time;
696 initial_time = sample->time;
697 t = sample->time - initial_time;
700 secs = nsecs / NSEC_PER_SEC;
701 nsecs -= secs * NSEC_PER_SEC;
703 if (symbol_conf.nanosecs)
704 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
706 char sample_time[32];
707 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
708 printed += fprintf(fp, "%12s: ", sample_time);
716 mispred_str(struct branch_entry *br)
718 if (!(br->flags.mispred || br->flags.predicted))
721 return br->flags.predicted ? 'P' : 'M';
724 static int perf_sample__fprintf_brstack(struct perf_sample *sample,
725 struct thread *thread,
726 struct perf_event_attr *attr, FILE *fp)
728 struct branch_stack *br = sample->branch_stack;
729 struct addr_location alf, alt;
736 for (i = 0; i < br->nr; i++) {
737 from = br->entries[i].from;
738 to = br->entries[i].to;
740 if (PRINT_FIELD(DSO)) {
741 memset(&alf, 0, sizeof(alf));
742 memset(&alt, 0, sizeof(alt));
743 thread__find_map_fb(thread, sample->cpumode, from, &alf);
744 thread__find_map_fb(thread, sample->cpumode, to, &alt);
747 printed += fprintf(fp, " 0x%"PRIx64, from);
748 if (PRINT_FIELD(DSO)) {
749 printed += fprintf(fp, "(");
750 printed += map__fprintf_dsoname(alf.map, fp);
751 printed += fprintf(fp, ")");
754 printed += fprintf(fp, "/0x%"PRIx64, to);
755 if (PRINT_FIELD(DSO)) {
756 printed += fprintf(fp, "(");
757 printed += map__fprintf_dsoname(alt.map, fp);
758 printed += fprintf(fp, ")");
761 printed += fprintf(fp, "/%c/%c/%c/%d ",
762 mispred_str( br->entries + i),
763 br->entries[i].flags.in_tx? 'X' : '-',
764 br->entries[i].flags.abort? 'A' : '-',
765 br->entries[i].flags.cycles);
771 static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
772 struct thread *thread,
773 struct perf_event_attr *attr, FILE *fp)
775 struct branch_stack *br = sample->branch_stack;
776 struct addr_location alf, alt;
783 for (i = 0; i < br->nr; i++) {
785 memset(&alf, 0, sizeof(alf));
786 memset(&alt, 0, sizeof(alt));
787 from = br->entries[i].from;
788 to = br->entries[i].to;
790 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
791 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
793 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
794 if (PRINT_FIELD(DSO)) {
795 printed += fprintf(fp, "(");
796 printed += map__fprintf_dsoname(alf.map, fp);
797 printed += fprintf(fp, ")");
799 printed += fprintf(fp, "%c", '/');
800 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
801 if (PRINT_FIELD(DSO)) {
802 printed += fprintf(fp, "(");
803 printed += map__fprintf_dsoname(alt.map, fp);
804 printed += fprintf(fp, ")");
806 printed += fprintf(fp, "/%c/%c/%c/%d ",
807 mispred_str( br->entries + i),
808 br->entries[i].flags.in_tx? 'X' : '-',
809 br->entries[i].flags.abort? 'A' : '-',
810 br->entries[i].flags.cycles);
816 static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
817 struct thread *thread,
818 struct perf_event_attr *attr, FILE *fp)
820 struct branch_stack *br = sample->branch_stack;
821 struct addr_location alf, alt;
828 for (i = 0; i < br->nr; i++) {
830 memset(&alf, 0, sizeof(alf));
831 memset(&alt, 0, sizeof(alt));
832 from = br->entries[i].from;
833 to = br->entries[i].to;
835 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
836 !alf.map->dso->adjust_symbols)
837 from = map__map_ip(alf.map, from);
839 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
840 !alt.map->dso->adjust_symbols)
841 to = map__map_ip(alt.map, to);
843 printed += fprintf(fp, " 0x%"PRIx64, from);
844 if (PRINT_FIELD(DSO)) {
845 printed += fprintf(fp, "(");
846 printed += map__fprintf_dsoname(alf.map, fp);
847 printed += fprintf(fp, ")");
849 printed += fprintf(fp, "/0x%"PRIx64, to);
850 if (PRINT_FIELD(DSO)) {
851 printed += fprintf(fp, "(");
852 printed += map__fprintf_dsoname(alt.map, fp);
853 printed += fprintf(fp, ")");
855 printed += fprintf(fp, "/%c/%c/%c/%d ",
856 mispred_str(br->entries + i),
857 br->entries[i].flags.in_tx ? 'X' : '-',
858 br->entries[i].flags.abort ? 'A' : '-',
859 br->entries[i].flags.cycles);
864 #define MAXBB 16384UL
866 static int grab_bb(u8 *buffer, u64 start, u64 end,
867 struct machine *machine, struct thread *thread,
868 bool *is64bit, u8 *cpumode, bool last)
871 struct addr_location al;
877 kernel = machine__kernel_ip(machine, start);
879 *cpumode = PERF_RECORD_MISC_KERNEL;
881 *cpumode = PERF_RECORD_MISC_USER;
884 * Block overlaps between kernel and user.
885 * This can happen due to ring filtering
886 * On Intel CPUs the entry into the kernel is filtered,
887 * but the exit is not. Let the caller patch it up.
889 if (kernel != machine__kernel_ip(machine, end)) {
890 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
894 memset(&al, 0, sizeof(al));
895 if (end - start > MAXBB - MAXINSN) {
897 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
899 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
903 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
904 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
907 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
908 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
912 /* Load maps to ensure dso->is_64_bit has been updated */
915 offset = al.map->map_ip(al.map, start);
916 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
917 end - start + MAXINSN);
919 *is64bit = al.map->dso->is_64_bit;
921 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
926 static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
928 struct addr_location al;
931 memset(&al, 0, sizeof(al));
932 thread__find_map(thread, cpumode, addr, &al);
935 ret = map__fprintf_srccode(al.map, al.addr, stdout,
936 &thread->srccode_state);
942 static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
943 struct perf_insn *x, u8 *inbuf, int len,
944 int insn, FILE *fp, int *total_cycles)
946 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
947 dump_insn(x, ip, inbuf, len, NULL),
948 en->flags.predicted ? " PRED" : "",
949 en->flags.mispred ? " MISPRED" : "",
950 en->flags.in_tx ? " INTX" : "",
951 en->flags.abort ? " ABORT" : "");
952 if (en->flags.cycles) {
953 *total_cycles += en->flags.cycles;
954 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
956 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
958 return printed + fprintf(fp, "\n");
961 static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
962 u8 cpumode, int cpu, struct symbol **lastsym,
963 struct perf_event_attr *attr, FILE *fp)
965 struct addr_location al;
966 int off, printed = 0;
968 memset(&al, 0, sizeof(al));
970 thread__find_map(thread, cpumode, addr, &al);
972 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
978 al.sym = map__find_symbol(al.map, al.addr);
983 if (al.addr < al.sym->end)
984 off = al.addr - al.sym->start;
986 off = al.addr - al.map->start - al.sym->start;
987 printed += fprintf(fp, "\t%s", al.sym->name);
989 printed += fprintf(fp, "%+d", off);
990 printed += fprintf(fp, ":");
991 if (PRINT_FIELD(SRCLINE))
992 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
993 printed += fprintf(fp, "\n");
999 static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1000 struct thread *thread,
1001 struct perf_event_attr *attr,
1002 struct machine *machine, FILE *fp)
1004 struct branch_stack *br = sample->branch_stack;
1006 int i, insn, len, nr, ilen, printed = 0;
1010 struct symbol *lastsym = NULL;
1011 int total_cycles = 0;
1013 if (!(br && br->nr))
1016 if (max_blocks && nr > max_blocks + 1)
1017 nr = max_blocks + 1;
1020 x.cpu = sample->cpu;
1022 printed += fprintf(fp, "%c", '\n');
1024 /* Handle first from jump, of which we don't know the entry. */
1025 len = grab_bb(buffer, br->entries[nr-1].from,
1026 br->entries[nr-1].from,
1027 machine, thread, &x.is64bit, &x.cpumode, false);
1029 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1030 x.cpumode, x.cpu, &lastsym, attr, fp);
1031 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
1032 &x, buffer, len, 0, fp, &total_cycles);
1033 if (PRINT_FIELD(SRCCODE))
1034 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
1037 /* Print all blocks */
1038 for (i = nr - 2; i >= 0; i--) {
1039 if (br->entries[i].from || br->entries[i].to)
1040 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1041 br->entries[i].from,
1043 start = br->entries[i + 1].to;
1044 end = br->entries[i].from;
1046 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1047 /* Patch up missing kernel transfers due to ring filters */
1048 if (len == -ENXIO && i > 0) {
1049 end = br->entries[--i].from;
1050 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1051 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1057 for (off = 0;; off += ilen) {
1058 uint64_t ip = start + off;
1060 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1062 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp,
1064 if (PRINT_FIELD(SRCCODE))
1065 printed += print_srccode(thread, x.cpumode, ip);
1068 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1069 dump_insn(&x, ip, buffer + off, len - off, &ilen));
1072 if (PRINT_FIELD(SRCCODE))
1073 print_srccode(thread, x.cpumode, ip);
1080 * Hit the branch? In this case we are already done, and the target
1081 * has not been executed yet.
1083 if (br->entries[0].from == sample->ip)
1085 if (br->entries[0].flags.abort)
1089 * Print final block upto sample
1091 * Due to pipeline delays the LBRs might be missing a branch
1092 * or two, which can result in very large or negative blocks
1093 * between final branch and sample. When this happens just
1094 * continue walking after the last TO until we hit a branch.
1096 start = br->entries[0].to;
1099 /* Missing jump. Scan 128 bytes for the next branch */
1102 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
1103 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1105 /* Print at least last IP if basic block did not work */
1106 len = grab_bb(buffer, sample->ip, sample->ip,
1107 machine, thread, &x.is64bit, &x.cpumode, false);
1110 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
1111 dump_insn(&x, sample->ip, buffer, len, NULL));
1112 if (PRINT_FIELD(SRCCODE))
1113 print_srccode(thread, x.cpumode, sample->ip);
1116 for (off = 0; off <= end - start; off += ilen) {
1117 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1118 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
1121 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1123 * Hit a missing branch. Just stop.
1125 printed += fprintf(fp, "\t... not reaching sample ...\n");
1128 if (PRINT_FIELD(SRCCODE))
1129 print_srccode(thread, x.cpumode, start + off);
1135 static int perf_sample__fprintf_addr(struct perf_sample *sample,
1136 struct thread *thread,
1137 struct perf_event_attr *attr, FILE *fp)
1139 struct addr_location al;
1140 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
1142 if (!sample_addr_correlates_sym(attr))
1145 thread__resolve(thread, &al, sample);
1147 if (PRINT_FIELD(SYM)) {
1148 printed += fprintf(fp, " ");
1149 if (PRINT_FIELD(SYMOFFSET))
1150 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
1152 printed += symbol__fprintf_symname(al.sym, fp);
1155 if (PRINT_FIELD(DSO)) {
1156 printed += fprintf(fp, " (");
1157 printed += map__fprintf_dsoname(al.map, fp);
1158 printed += fprintf(fp, ")");
1164 static const char *resolve_branch_sym(struct perf_sample *sample,
1165 struct perf_evsel *evsel,
1166 struct thread *thread,
1167 struct addr_location *al,
1170 struct addr_location addr_al;
1171 struct perf_event_attr *attr = &evsel->attr;
1172 const char *name = NULL;
1174 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1175 if (sample_addr_correlates_sym(attr)) {
1176 thread__resolve(thread, &addr_al, sample);
1178 name = addr_al.sym->name;
1184 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1186 name = al->sym->name;
1193 static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1194 struct perf_evsel *evsel,
1195 struct thread *thread,
1196 struct addr_location *al, FILE *fp)
1198 struct perf_event_attr *attr = &evsel->attr;
1199 size_t depth = thread_stack__depth(thread, sample->cpu);
1200 const char *name = NULL;
1207 * The 'return' has already been popped off the stack so the depth has
1208 * to be adjusted to match the 'call'.
1210 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1213 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
1215 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1216 dlen += fprintf(fp, "(");
1217 dlen += map__fprintf_dsoname(al->map, fp);
1218 dlen += fprintf(fp, ")\t");
1222 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
1224 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
1230 * Try to keep the output length from changing frequently so that the
1231 * output lines up more nicely.
1233 if (len > spacing || (len && len < spacing - 52))
1234 spacing = round_up(len + 4, 32);
1237 len += fprintf(fp, "%*s", spacing - len, "");
1242 __weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1243 struct thread *thread __maybe_unused,
1244 struct machine *machine __maybe_unused)
1248 static int perf_sample__fprintf_insn(struct perf_sample *sample,
1249 struct perf_event_attr *attr,
1250 struct thread *thread,
1251 struct machine *machine, FILE *fp)
1255 if (sample->insn_len == 0 && native_arch)
1256 arch_fetch_insn(sample, thread, machine);
1258 if (PRINT_FIELD(INSNLEN))
1259 printed += fprintf(fp, " ilen: %d", sample->insn_len);
1260 if (PRINT_FIELD(INSN) && sample->insn_len) {
1263 printed += fprintf(fp, " insn:");
1264 for (i = 0; i < sample->insn_len; i++)
1265 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
1267 if (PRINT_FIELD(BRSTACKINSN))
1268 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1273 static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1274 struct perf_event_attr *attr, FILE *fp)
1278 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1281 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1283 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1284 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1287 static int perf_sample__fprintf_bts(struct perf_sample *sample,
1288 struct perf_evsel *evsel,
1289 struct thread *thread,
1290 struct addr_location *al,
1291 struct machine *machine, FILE *fp)
1293 struct perf_event_attr *attr = &evsel->attr;
1294 unsigned int type = output_type(attr->type);
1295 bool print_srcline_last = false;
1298 if (PRINT_FIELD(CALLINDENT))
1299 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
1301 /* print branch_from information */
1302 if (PRINT_FIELD(IP)) {
1303 unsigned int print_opts = output[type].print_ip_opts;
1304 struct callchain_cursor *cursor = NULL;
1306 if (symbol_conf.use_callchain && sample->callchain &&
1307 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1308 sample, NULL, NULL, scripting_max_stack) == 0)
1309 cursor = &callchain_cursor;
1311 if (cursor == NULL) {
1312 printed += fprintf(fp, " ");
1313 if (print_opts & EVSEL__PRINT_SRCLINE) {
1314 print_srcline_last = true;
1315 print_opts &= ~EVSEL__PRINT_SRCLINE;
1318 printed += fprintf(fp, "\n");
1320 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
1323 /* print branch_to information */
1324 if (PRINT_FIELD(ADDR) ||
1325 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
1326 !output[type].user_set)) {
1327 printed += fprintf(fp, " => ");
1328 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
1331 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1333 if (print_srcline_last)
1334 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
1336 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1337 printed += fprintf(fp, "\n");
1338 if (PRINT_FIELD(SRCCODE)) {
1339 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1340 &thread->srccode_state);
1343 printed += printf("\n");
1352 } sample_flags[] = {
1353 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1354 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1355 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1356 {PERF_IP_FLAG_BRANCH, "jmp"},
1357 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1358 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1359 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1360 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1361 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1362 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1363 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1364 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1369 static const char *sample_flags_to_name(u32 flags)
1373 for (i = 0; sample_flags[i].name ; i++) {
1374 if (sample_flags[i].flags == flags)
1375 return sample_flags[i].name;
1381 static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1383 const char *chars = PERF_IP_FLAG_CHARS;
1384 const int n = strlen(PERF_IP_FLAG_CHARS);
1385 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1386 const char *name = NULL;
1390 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1392 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1394 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1395 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1397 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1400 if (flags & PERF_IP_FLAG_TRACE_END) {
1401 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1403 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
1406 for (i = 0; i < n; i++, flags >>= 1) {
1408 str[pos++] = chars[i];
1410 for (; i < 32; i++, flags >>= 1) {
1416 return fprintf(fp, " %-19s ", str);
1419 struct printer_data {
1425 static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1427 void *extra, FILE *fp)
1429 unsigned char ch = (unsigned char)val;
1430 struct printer_data *printer_data = extra;
1434 case BINARY_PRINT_DATA_BEGIN:
1435 printed += fprintf(fp, "\n");
1437 case BINARY_PRINT_LINE_BEGIN:
1438 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
1441 case BINARY_PRINT_ADDR:
1442 printed += fprintf(fp, " %04x:", val);
1444 case BINARY_PRINT_NUM_DATA:
1445 printed += fprintf(fp, " %02x", val);
1447 case BINARY_PRINT_NUM_PAD:
1448 printed += fprintf(fp, " ");
1450 case BINARY_PRINT_SEP:
1451 printed += fprintf(fp, " ");
1453 case BINARY_PRINT_CHAR_DATA:
1454 if (printer_data->hit_nul && ch)
1455 printer_data->is_printable = false;
1458 printed += fprintf(fp, "%c", '.');
1460 if (!printer_data->is_printable)
1464 printer_data->hit_nul = true;
1466 printer_data->is_printable = false;
1468 printed += fprintf(fp, "%c", ch);
1471 case BINARY_PRINT_CHAR_PAD:
1472 printed += fprintf(fp, " ");
1474 case BINARY_PRINT_LINE_END:
1475 printed += fprintf(fp, "\n");
1476 printer_data->line_no++;
1478 case BINARY_PRINT_DATA_END:
1486 static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
1488 unsigned int nr_bytes = sample->raw_size;
1489 struct printer_data printer_data = {0, false, true};
1490 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1491 sample__fprintf_bpf_output, &printer_data, fp);
1493 if (printer_data.is_printable && printer_data.hit_nul)
1494 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1499 static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
1501 if (len > 0 && len < spacing)
1502 return fprintf(fp, "%*s", spacing - len, "");
1507 static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
1509 return perf_sample__fprintf_spacing(len, 34, fp);
1512 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
1514 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1517 if (perf_sample__bad_synth_size(sample, *data))
1520 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
1521 data->ip, le64_to_cpu(data->payload));
1522 return len + perf_sample__fprintf_pt_spacing(len, fp);
1525 static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
1527 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1530 if (perf_sample__bad_synth_size(sample, *data))
1533 len = fprintf(fp, " hints: %#x extensions: %#x ",
1534 data->hints, data->extensions);
1535 return len + perf_sample__fprintf_pt_spacing(len, fp);
1538 static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
1540 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1543 if (perf_sample__bad_synth_size(sample, *data))
1546 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1547 data->hw, data->cstate, data->subcstate);
1548 return len + perf_sample__fprintf_pt_spacing(len, fp);
1551 static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
1553 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1556 if (perf_sample__bad_synth_size(sample, *data))
1559 len = fprintf(fp, " IP: %u ", data->ip);
1560 return len + perf_sample__fprintf_pt_spacing(len, fp);
1563 static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
1565 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1568 if (perf_sample__bad_synth_size(sample, *data))
1571 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1572 data->deepest_cstate, data->last_cstate,
1574 return len + perf_sample__fprintf_pt_spacing(len, fp);
1577 static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
1579 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1580 unsigned int percent, freq;
1583 if (perf_sample__bad_synth_size(sample, *data))
1586 freq = (le32_to_cpu(data->freq) + 500) / 1000;
1587 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
1588 if (data->max_nonturbo) {
1589 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1590 len += fprintf(fp, "(%3u%%) ", percent);
1592 return len + perf_sample__fprintf_pt_spacing(len, fp);
1595 static int perf_sample__fprintf_synth(struct perf_sample *sample,
1596 struct perf_evsel *evsel, FILE *fp)
1598 switch (evsel->attr.config) {
1599 case PERF_SYNTH_INTEL_PTWRITE:
1600 return perf_sample__fprintf_synth_ptwrite(sample, fp);
1601 case PERF_SYNTH_INTEL_MWAIT:
1602 return perf_sample__fprintf_synth_mwait(sample, fp);
1603 case PERF_SYNTH_INTEL_PWRE:
1604 return perf_sample__fprintf_synth_pwre(sample, fp);
1605 case PERF_SYNTH_INTEL_EXSTOP:
1606 return perf_sample__fprintf_synth_exstop(sample, fp);
1607 case PERF_SYNTH_INTEL_PWRX:
1608 return perf_sample__fprintf_synth_pwrx(sample, fp);
1609 case PERF_SYNTH_INTEL_CBR:
1610 return perf_sample__fprintf_synth_cbr(sample, fp);
1618 struct perf_script {
1619 struct perf_tool tool;
1620 struct perf_session *session;
1621 bool show_task_events;
1622 bool show_mmap_events;
1623 bool show_switch_events;
1624 bool show_namespace_events;
1625 bool show_lost_events;
1626 bool show_round_events;
1627 bool show_bpf_events;
1629 bool per_event_dump;
1630 struct cpu_map *cpus;
1631 struct thread_map *threads;
1633 const char *time_str;
1634 struct perf_time_interval *ptime_range;
1639 static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1641 struct perf_evsel *evsel;
1644 evlist__for_each_entry(evlist, evsel) {
1645 int len = strlen(perf_evsel__name(evsel));
1647 max = MAX(len, max);
1653 static int data_src__fprintf(u64 data_src, FILE *fp)
1655 struct mem_info mi = { .data_src.val = data_src };
1661 perf_script__meminfo_scnprintf(decode, 100, &mi);
1663 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1667 return fprintf(fp, "%-*s", maxlen, out);
1671 struct perf_sample *sample;
1672 struct thread *thread;
1673 struct perf_evsel *evsel;
1677 static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1678 void *ctx, const char *color,
1680 const char *unit, double val)
1682 struct metric_ctx *mctx = ctx;
1686 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1687 PERF_RECORD_SAMPLE, mctx->fp);
1688 fputs("\tmetric: ", mctx->fp);
1690 color_fprintf(mctx->fp, color, fmt, val);
1693 fprintf(mctx->fp, " %s\n", unit);
1696 static void script_new_line(struct perf_stat_config *config __maybe_unused,
1699 struct metric_ctx *mctx = ctx;
1701 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1702 PERF_RECORD_SAMPLE, mctx->fp);
1703 fputs("\tmetric: ", mctx->fp);
1706 static void perf_sample__fprint_metric(struct perf_script *script,
1707 struct thread *thread,
1708 struct perf_evsel *evsel,
1709 struct perf_sample *sample,
1712 struct perf_stat_output_ctx ctx = {
1713 .print_metric = script_print_metric,
1714 .new_line = script_new_line,
1715 .ctx = &(struct metric_ctx) {
1721 .force_header = false,
1723 struct perf_evsel *ev2;
1727 perf_evlist__alloc_stats(script->session->evlist, false);
1728 if (evsel_script(evsel->leader)->gnum++ == 0)
1729 perf_stat__reset_shadow_stats();
1730 val = sample->period * evsel->scale;
1731 perf_stat__update_shadow_stats(evsel,
1735 evsel_script(evsel)->val = val;
1736 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1737 for_each_group_member (ev2, evsel->leader) {
1738 perf_stat__print_shadow_stats(&stat_config, ev2,
1739 evsel_script(ev2)->val,
1745 evsel_script(evsel->leader)->gnum = 0;
1749 static bool show_event(struct perf_sample *sample,
1750 struct perf_evsel *evsel,
1751 struct thread *thread,
1752 struct addr_location *al)
1754 int depth = thread_stack__depth(thread, sample->cpu);
1756 if (!symbol_conf.graph_function)
1759 if (thread->filter) {
1760 if (depth <= thread->filter_entry_depth) {
1761 thread->filter = false;
1766 const char *s = symbol_conf.graph_function;
1768 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1774 nlen = strlen(name);
1776 unsigned len = strcspn(s, ",");
1777 if (nlen == len && !strncmp(name, s, len)) {
1778 thread->filter = true;
1779 thread->filter_entry_depth = depth;
1790 static void process_event(struct perf_script *script,
1791 struct perf_sample *sample, struct perf_evsel *evsel,
1792 struct addr_location *al,
1793 struct machine *machine)
1795 struct thread *thread = al->thread;
1796 struct perf_event_attr *attr = &evsel->attr;
1797 unsigned int type = output_type(attr->type);
1798 struct perf_evsel_script *es = evsel->priv;
1801 if (output[type].fields == 0)
1804 if (!show_event(sample, evsel, thread, al))
1809 perf_sample__fprintf_start(sample, thread, evsel,
1810 PERF_RECORD_SAMPLE, fp);
1812 if (PRINT_FIELD(PERIOD))
1813 fprintf(fp, "%10" PRIu64 " ", sample->period);
1815 if (PRINT_FIELD(EVNAME)) {
1816 const char *evname = perf_evsel__name(evsel);
1818 if (!script->name_width)
1819 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1821 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
1825 perf_sample__fprintf_flags(sample->flags, fp);
1827 if (is_bts_event(attr)) {
1828 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
1832 if (PRINT_FIELD(TRACE) && sample->raw_data) {
1833 event_format__fprintf(evsel->tp_format, sample->cpu,
1834 sample->raw_data, sample->raw_size, fp);
1837 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
1838 perf_sample__fprintf_synth(sample, evsel, fp);
1840 if (PRINT_FIELD(ADDR))
1841 perf_sample__fprintf_addr(sample, thread, attr, fp);
1843 if (PRINT_FIELD(DATA_SRC))
1844 data_src__fprintf(sample->data_src, fp);
1846 if (PRINT_FIELD(WEIGHT))
1847 fprintf(fp, "%16" PRIu64, sample->weight);
1849 if (PRINT_FIELD(IP)) {
1850 struct callchain_cursor *cursor = NULL;
1852 if (symbol_conf.use_callchain && sample->callchain &&
1853 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1854 sample, NULL, NULL, scripting_max_stack) == 0)
1855 cursor = &callchain_cursor;
1857 fputc(cursor ? '\n' : ' ', fp);
1858 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
1861 if (PRINT_FIELD(IREGS))
1862 perf_sample__fprintf_iregs(sample, attr, fp);
1864 if (PRINT_FIELD(UREGS))
1865 perf_sample__fprintf_uregs(sample, attr, fp);
1867 if (PRINT_FIELD(BRSTACK))
1868 perf_sample__fprintf_brstack(sample, thread, attr, fp);
1869 else if (PRINT_FIELD(BRSTACKSYM))
1870 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
1871 else if (PRINT_FIELD(BRSTACKOFF))
1872 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
1874 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1875 perf_sample__fprintf_bpf_output(sample, fp);
1876 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1878 if (PRINT_FIELD(PHYS_ADDR))
1879 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1881 perf_sample__fprintf_ipc(sample, attr, fp);
1885 if (PRINT_FIELD(SRCCODE)) {
1886 if (map__fprintf_srccode(al->map, al->addr, stdout,
1887 &thread->srccode_state))
1891 if (PRINT_FIELD(METRIC))
1892 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
1898 static struct scripting_ops *scripting_ops;
1900 static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1902 int nthreads = thread_map__nr(counter->threads);
1903 int ncpus = perf_evsel__nr_cpus(counter);
1905 static int header_printed;
1907 if (counter->system_wide)
1910 if (!header_printed) {
1911 printf("%3s %8s %15s %15s %15s %15s %s\n",
1912 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1916 for (thread = 0; thread < nthreads; thread++) {
1917 for (cpu = 0; cpu < ncpus; cpu++) {
1918 struct perf_counts_values *counts;
1920 counts = perf_counts(counter->counts, cpu, thread);
1922 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1923 counter->cpus->map[cpu],
1924 thread_map__pid(counter->threads, thread),
1929 perf_evsel__name(counter));
1934 static void process_stat(struct perf_evsel *counter, u64 tstamp)
1936 if (scripting_ops && scripting_ops->process_stat)
1937 scripting_ops->process_stat(&stat_config, counter, tstamp);
1939 __process_stat(counter, tstamp);
1942 static void process_stat_interval(u64 tstamp)
1944 if (scripting_ops && scripting_ops->process_stat_interval)
1945 scripting_ops->process_stat_interval(tstamp);
1948 static void setup_scripting(void)
1950 setup_perl_scripting();
1951 setup_python_scripting();
1954 static int flush_scripting(void)
1956 return scripting_ops ? scripting_ops->flush_script() : 0;
1959 static int cleanup_scripting(void)
1961 pr_debug("\nperf script stopped\n");
1963 return scripting_ops ? scripting_ops->stop_script() : 0;
1966 static bool filter_cpu(struct perf_sample *sample)
1969 return !test_bit(sample->cpu, cpu_bitmap);
1973 static int process_sample_event(struct perf_tool *tool,
1974 union perf_event *event,
1975 struct perf_sample *sample,
1976 struct perf_evsel *evsel,
1977 struct machine *machine)
1979 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1980 struct addr_location al;
1982 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1988 if (sample->time < last_timestamp) {
1989 pr_err("Samples misordered, previous: %" PRIu64
1990 " this: %" PRIu64 "\n", last_timestamp,
1994 last_timestamp = sample->time;
1998 if (machine__resolve(machine, &al, sample) < 0) {
1999 pr_err("problem processing %d event, skipping it.\n",
2000 event->header.type);
2007 if (filter_cpu(sample))
2011 scripting_ops->process_event(event, sample, evsel, &al);
2013 process_event(scr, sample, evsel, &al, machine);
2016 addr_location__put(&al);
2020 static int process_attr(struct perf_tool *tool, union perf_event *event,
2021 struct perf_evlist **pevlist)
2023 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2024 struct perf_evlist *evlist;
2025 struct perf_evsel *evsel, *pos;
2027 static struct perf_evsel_script *es;
2029 err = perf_event__process_attr(tool, event, pevlist);
2034 evsel = perf_evlist__last(*pevlist);
2037 if (scr->per_event_dump) {
2038 evsel->priv = perf_evsel_script__new(evsel,
2039 scr->session->data);
2041 es = zalloc(sizeof(*es));
2049 if (evsel->attr.type >= PERF_TYPE_MAX &&
2050 evsel->attr.type != PERF_TYPE_SYNTH)
2053 evlist__for_each_entry(evlist, pos) {
2054 if (pos->attr.type == evsel->attr.type && pos != evsel)
2058 set_print_ip_opts(&evsel->attr);
2060 if (evsel->attr.sample_type)
2061 err = perf_evsel__check_attr(evsel, scr->session);
2066 static int process_comm_event(struct perf_tool *tool,
2067 union perf_event *event,
2068 struct perf_sample *sample,
2069 struct machine *machine)
2071 struct thread *thread;
2072 struct perf_script *script = container_of(tool, struct perf_script, tool);
2073 struct perf_session *session = script->session;
2074 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2077 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2078 if (thread == NULL) {
2079 pr_debug("problem processing COMM event, skipping it.\n");
2083 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2086 if (!evsel->attr.sample_id_all) {
2089 sample->tid = event->comm.tid;
2090 sample->pid = event->comm.pid;
2092 if (!filter_cpu(sample)) {
2093 perf_sample__fprintf_start(sample, thread, evsel,
2094 PERF_RECORD_COMM, stdout);
2095 perf_event__fprintf(event, stdout);
2099 thread__put(thread);
2103 static int process_namespaces_event(struct perf_tool *tool,
2104 union perf_event *event,
2105 struct perf_sample *sample,
2106 struct machine *machine)
2108 struct thread *thread;
2109 struct perf_script *script = container_of(tool, struct perf_script, tool);
2110 struct perf_session *session = script->session;
2111 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2114 thread = machine__findnew_thread(machine, event->namespaces.pid,
2115 event->namespaces.tid);
2116 if (thread == NULL) {
2117 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2121 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2124 if (!evsel->attr.sample_id_all) {
2127 sample->tid = event->namespaces.tid;
2128 sample->pid = event->namespaces.pid;
2130 if (!filter_cpu(sample)) {
2131 perf_sample__fprintf_start(sample, thread, evsel,
2132 PERF_RECORD_NAMESPACES, stdout);
2133 perf_event__fprintf(event, stdout);
2137 thread__put(thread);
2141 static int process_fork_event(struct perf_tool *tool,
2142 union perf_event *event,
2143 struct perf_sample *sample,
2144 struct machine *machine)
2146 struct thread *thread;
2147 struct perf_script *script = container_of(tool, struct perf_script, tool);
2148 struct perf_session *session = script->session;
2149 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2151 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2154 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2155 if (thread == NULL) {
2156 pr_debug("problem processing FORK event, skipping it.\n");
2160 if (!evsel->attr.sample_id_all) {
2162 sample->time = event->fork.time;
2163 sample->tid = event->fork.tid;
2164 sample->pid = event->fork.pid;
2166 if (!filter_cpu(sample)) {
2167 perf_sample__fprintf_start(sample, thread, evsel,
2168 PERF_RECORD_FORK, stdout);
2169 perf_event__fprintf(event, stdout);
2171 thread__put(thread);
2175 static int process_exit_event(struct perf_tool *tool,
2176 union perf_event *event,
2177 struct perf_sample *sample,
2178 struct machine *machine)
2181 struct thread *thread;
2182 struct perf_script *script = container_of(tool, struct perf_script, tool);
2183 struct perf_session *session = script->session;
2184 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2186 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2187 if (thread == NULL) {
2188 pr_debug("problem processing EXIT event, skipping it.\n");
2192 if (!evsel->attr.sample_id_all) {
2195 sample->tid = event->fork.tid;
2196 sample->pid = event->fork.pid;
2198 if (!filter_cpu(sample)) {
2199 perf_sample__fprintf_start(sample, thread, evsel,
2200 PERF_RECORD_EXIT, stdout);
2201 perf_event__fprintf(event, stdout);
2204 if (perf_event__process_exit(tool, event, sample, machine) < 0)
2207 thread__put(thread);
2211 static int process_mmap_event(struct perf_tool *tool,
2212 union perf_event *event,
2213 struct perf_sample *sample,
2214 struct machine *machine)
2216 struct thread *thread;
2217 struct perf_script *script = container_of(tool, struct perf_script, tool);
2218 struct perf_session *session = script->session;
2219 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2221 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2224 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2225 if (thread == NULL) {
2226 pr_debug("problem processing MMAP event, skipping it.\n");
2230 if (!evsel->attr.sample_id_all) {
2233 sample->tid = event->mmap.tid;
2234 sample->pid = event->mmap.pid;
2236 if (!filter_cpu(sample)) {
2237 perf_sample__fprintf_start(sample, thread, evsel,
2238 PERF_RECORD_MMAP, stdout);
2239 perf_event__fprintf(event, stdout);
2241 thread__put(thread);
2245 static int process_mmap2_event(struct perf_tool *tool,
2246 union perf_event *event,
2247 struct perf_sample *sample,
2248 struct machine *machine)
2250 struct thread *thread;
2251 struct perf_script *script = container_of(tool, struct perf_script, tool);
2252 struct perf_session *session = script->session;
2253 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2255 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2258 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2259 if (thread == NULL) {
2260 pr_debug("problem processing MMAP2 event, skipping it.\n");
2264 if (!evsel->attr.sample_id_all) {
2267 sample->tid = event->mmap2.tid;
2268 sample->pid = event->mmap2.pid;
2270 if (!filter_cpu(sample)) {
2271 perf_sample__fprintf_start(sample, thread, evsel,
2272 PERF_RECORD_MMAP2, stdout);
2273 perf_event__fprintf(event, stdout);
2275 thread__put(thread);
2279 static int process_switch_event(struct perf_tool *tool,
2280 union perf_event *event,
2281 struct perf_sample *sample,
2282 struct machine *machine)
2284 struct thread *thread;
2285 struct perf_script *script = container_of(tool, struct perf_script, tool);
2286 struct perf_session *session = script->session;
2287 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2289 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2292 thread = machine__findnew_thread(machine, sample->pid,
2294 if (thread == NULL) {
2295 pr_debug("problem processing SWITCH event, skipping it.\n");
2299 if (!filter_cpu(sample)) {
2300 perf_sample__fprintf_start(sample, thread, evsel,
2301 PERF_RECORD_SWITCH, stdout);
2302 perf_event__fprintf(event, stdout);
2304 thread__put(thread);
2309 process_lost_event(struct perf_tool *tool,
2310 union perf_event *event,
2311 struct perf_sample *sample,
2312 struct machine *machine)
2314 struct perf_script *script = container_of(tool, struct perf_script, tool);
2315 struct perf_session *session = script->session;
2316 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2317 struct thread *thread;
2319 thread = machine__findnew_thread(machine, sample->pid,
2324 if (!filter_cpu(sample)) {
2325 perf_sample__fprintf_start(sample, thread, evsel,
2326 PERF_RECORD_LOST, stdout);
2327 perf_event__fprintf(event, stdout);
2329 thread__put(thread);
2334 process_finished_round_event(struct perf_tool *tool __maybe_unused,
2335 union perf_event *event,
2336 struct ordered_events *oe __maybe_unused)
2339 perf_event__fprintf(event, stdout);
2344 process_bpf_events(struct perf_tool *tool __maybe_unused,
2345 union perf_event *event,
2346 struct perf_sample *sample,
2347 struct machine *machine)
2349 struct thread *thread;
2350 struct perf_script *script = container_of(tool, struct perf_script, tool);
2351 struct perf_session *session = script->session;
2352 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2354 if (machine__process_ksymbol(machine, event, sample) < 0)
2357 if (!evsel->attr.sample_id_all) {
2358 perf_event__fprintf(event, stdout);
2362 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2363 if (thread == NULL) {
2364 pr_debug("problem processing MMAP event, skipping it.\n");
2368 if (!filter_cpu(sample)) {
2369 perf_sample__fprintf_start(sample, thread, evsel,
2370 event->header.type, stdout);
2371 perf_event__fprintf(event, stdout);
2374 thread__put(thread);
2378 static void sig_handler(int sig __maybe_unused)
2383 static void perf_script__fclose_per_event_dump(struct perf_script *script)
2385 struct perf_evlist *evlist = script->session->evlist;
2386 struct perf_evsel *evsel;
2388 evlist__for_each_entry(evlist, evsel) {
2391 perf_evsel_script__delete(evsel->priv);
2396 static int perf_script__fopen_per_event_dump(struct perf_script *script)
2398 struct perf_evsel *evsel;
2400 evlist__for_each_entry(script->session->evlist, evsel) {
2402 * Already setup? I.e. we may be called twice in cases like
2403 * Intel PT, one for the intel_pt// and dummy events, then
2404 * for the evsels syntheized from the auxtrace info.
2406 * Ses perf_script__process_auxtrace_info.
2408 if (evsel->priv != NULL)
2411 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
2412 if (evsel->priv == NULL)
2413 goto out_err_fclose;
2419 perf_script__fclose_per_event_dump(script);
2423 static int perf_script__setup_per_event_dump(struct perf_script *script)
2425 struct perf_evsel *evsel;
2426 static struct perf_evsel_script es_stdout;
2428 if (script->per_event_dump)
2429 return perf_script__fopen_per_event_dump(script);
2431 es_stdout.fp = stdout;
2433 evlist__for_each_entry(script->session->evlist, evsel)
2434 evsel->priv = &es_stdout;
2439 static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2441 struct perf_evsel *evsel;
2443 evlist__for_each_entry(script->session->evlist, evsel) {
2444 struct perf_evsel_script *es = evsel->priv;
2446 perf_evsel_script__fprintf(es, stdout);
2447 perf_evsel_script__delete(es);
2452 static int __cmd_script(struct perf_script *script)
2456 signal(SIGINT, sig_handler);
2458 perf_stat__init_shadow_stats();
2460 /* override event processing functions */
2461 if (script->show_task_events) {
2462 script->tool.comm = process_comm_event;
2463 script->tool.fork = process_fork_event;
2464 script->tool.exit = process_exit_event;
2466 if (script->show_mmap_events) {
2467 script->tool.mmap = process_mmap_event;
2468 script->tool.mmap2 = process_mmap2_event;
2470 if (script->show_switch_events)
2471 script->tool.context_switch = process_switch_event;
2472 if (script->show_namespace_events)
2473 script->tool.namespaces = process_namespaces_event;
2474 if (script->show_lost_events)
2475 script->tool.lost = process_lost_event;
2476 if (script->show_round_events) {
2477 script->tool.ordered_events = false;
2478 script->tool.finished_round = process_finished_round_event;
2480 if (script->show_bpf_events) {
2481 script->tool.ksymbol = process_bpf_events;
2482 script->tool.bpf_event = process_bpf_events;
2485 if (perf_script__setup_per_event_dump(script)) {
2486 pr_err("Couldn't create the per event dump files\n");
2490 ret = perf_session__process_events(script->session);
2492 if (script->per_event_dump)
2493 perf_script__exit_per_event_dump_stats(script);
2496 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
2501 struct script_spec {
2502 struct list_head node;
2503 struct scripting_ops *ops;
2507 static LIST_HEAD(script_specs);
2509 static struct script_spec *script_spec__new(const char *spec,
2510 struct scripting_ops *ops)
2512 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2515 strcpy(s->spec, spec);
2522 static void script_spec__add(struct script_spec *s)
2524 list_add_tail(&s->node, &script_specs);
2527 static struct script_spec *script_spec__find(const char *spec)
2529 struct script_spec *s;
2531 list_for_each_entry(s, &script_specs, node)
2532 if (strcasecmp(s->spec, spec) == 0)
2537 int script_spec_register(const char *spec, struct scripting_ops *ops)
2539 struct script_spec *s;
2541 s = script_spec__find(spec);
2545 s = script_spec__new(spec, ops);
2549 script_spec__add(s);
2554 static struct scripting_ops *script_spec__lookup(const char *spec)
2556 struct script_spec *s = script_spec__find(spec);
2563 static void list_available_languages(void)
2565 struct script_spec *s;
2567 fprintf(stderr, "\n");
2568 fprintf(stderr, "Scripting language extensions (used in "
2569 "perf script -s [spec:]script.[spec]):\n\n");
2571 list_for_each_entry(s, &script_specs, node)
2572 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2574 fprintf(stderr, "\n");
2577 static int parse_scriptname(const struct option *opt __maybe_unused,
2578 const char *str, int unset __maybe_unused)
2580 char spec[PATH_MAX];
2581 const char *script, *ext;
2584 if (strcmp(str, "lang") == 0) {
2585 list_available_languages();
2589 script = strchr(str, ':');
2592 if (len >= PATH_MAX) {
2593 fprintf(stderr, "invalid language specifier");
2596 strncpy(spec, str, len);
2598 scripting_ops = script_spec__lookup(spec);
2599 if (!scripting_ops) {
2600 fprintf(stderr, "invalid language specifier");
2606 ext = strrchr(script, '.');
2608 fprintf(stderr, "invalid script extension");
2611 scripting_ops = script_spec__lookup(++ext);
2612 if (!scripting_ops) {
2613 fprintf(stderr, "invalid script extension");
2618 script_name = strdup(script);
2623 static int parse_output_fields(const struct option *opt __maybe_unused,
2624 const char *arg, int unset __maybe_unused)
2626 char *tok, *strtok_saveptr = NULL;
2627 int i, imax = ARRAY_SIZE(all_output_options);
2630 char *str = strdup(arg);
2632 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
2637 /* first word can state for which event type the user is specifying
2638 * the fields. If no type exists, the specified fields apply to all
2639 * event types found in the file minus the invalid fields for a type.
2641 tok = strchr(str, ':');
2645 if (!strcmp(str, "hw"))
2646 type = PERF_TYPE_HARDWARE;
2647 else if (!strcmp(str, "sw"))
2648 type = PERF_TYPE_SOFTWARE;
2649 else if (!strcmp(str, "trace"))
2650 type = PERF_TYPE_TRACEPOINT;
2651 else if (!strcmp(str, "raw"))
2652 type = PERF_TYPE_RAW;
2653 else if (!strcmp(str, "break"))
2654 type = PERF_TYPE_BREAKPOINT;
2655 else if (!strcmp(str, "synth"))
2656 type = OUTPUT_TYPE_SYNTH;
2658 fprintf(stderr, "Invalid event type in field string.\n");
2663 if (output[type].user_set)
2664 pr_warning("Overriding previous field request for %s events.\n",
2667 /* Don't override defaults for +- */
2668 if (strchr(tok, '+') || strchr(tok, '-'))
2671 output[type].fields = 0;
2672 output[type].user_set = true;
2673 output[type].wildcard_set = false;
2677 if (strlen(str) == 0) {
2679 "Cannot set fields to 'none' for all event types.\n");
2684 /* Don't override defaults for +- */
2685 if (strchr(str, '+') || strchr(str, '-'))
2688 if (output_set_by_user())
2689 pr_warning("Overriding previous field request for all events.\n");
2691 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2692 output[j].fields = 0;
2693 output[j].user_set = true;
2694 output[j].wildcard_set = true;
2699 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
2705 } else if (*tok == '-') {
2711 if (change != SET && change != DEFAULT)
2716 for (i = 0; i < imax; ++i) {
2717 if (strcmp(tok, all_output_options[i].str) == 0)
2720 if (i == imax && strcmp(tok, "flags") == 0) {
2721 print_flags = change == REMOVE ? false : true;
2725 fprintf(stderr, "Invalid field requested.\n");
2731 /* add user option to all events types for
2734 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2735 if (output[j].invalid_fields & all_output_options[i].field) {
2736 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2737 all_output_options[i].str, event_type(j));
2739 if (change == REMOVE) {
2740 output[j].fields &= ~all_output_options[i].field;
2741 output[j].user_set_fields &= ~all_output_options[i].field;
2743 output[j].fields |= all_output_options[i].field;
2744 output[j].user_set_fields |= all_output_options[i].field;
2746 output[j].user_set = true;
2747 output[j].wildcard_set = true;
2751 if (output[type].invalid_fields & all_output_options[i].field) {
2752 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2753 all_output_options[i].str, event_type(type));
2758 if (change == REMOVE)
2759 output[type].fields &= ~all_output_options[i].field;
2761 output[type].fields |= all_output_options[i].field;
2762 output[type].user_set = true;
2763 output[type].wildcard_set = true;
2768 if (output[type].fields == 0) {
2769 pr_debug("No fields requested for %s type. "
2770 "Events will not be displayed.\n", event_type(type));
2776 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2783 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2784 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2785 if ((lang_dirent->d_type == DT_DIR || \
2786 (lang_dirent->d_type == DT_UNKNOWN && \
2787 is_directory(scripts_path, lang_dirent))) && \
2788 (strcmp(lang_dirent->d_name, ".")) && \
2789 (strcmp(lang_dirent->d_name, "..")))
2791 #define for_each_script(lang_path, lang_dir, script_dirent) \
2792 while ((script_dirent = readdir(lang_dir)) != NULL) \
2793 if (script_dirent->d_type != DT_DIR && \
2794 (script_dirent->d_type != DT_UNKNOWN || \
2795 !is_directory(lang_path, script_dirent)))
2798 #define RECORD_SUFFIX "-record"
2799 #define REPORT_SUFFIX "-report"
2801 struct script_desc {
2802 struct list_head node;
2808 static LIST_HEAD(script_descs);
2810 static struct script_desc *script_desc__new(const char *name)
2812 struct script_desc *s = zalloc(sizeof(*s));
2814 if (s != NULL && name)
2815 s->name = strdup(name);
2820 static void script_desc__delete(struct script_desc *s)
2823 zfree(&s->half_liner);
2828 static void script_desc__add(struct script_desc *s)
2830 list_add_tail(&s->node, &script_descs);
2833 static struct script_desc *script_desc__find(const char *name)
2835 struct script_desc *s;
2837 list_for_each_entry(s, &script_descs, node)
2838 if (strcasecmp(s->name, name) == 0)
2843 static struct script_desc *script_desc__findnew(const char *name)
2845 struct script_desc *s = script_desc__find(name);
2850 s = script_desc__new(name);
2854 script_desc__add(s);
2859 static const char *ends_with(const char *str, const char *suffix)
2861 size_t suffix_len = strlen(suffix);
2862 const char *p = str;
2864 if (strlen(str) > suffix_len) {
2865 p = str + strlen(str) - suffix_len;
2866 if (!strncmp(p, suffix, suffix_len))
2873 static int read_script_info(struct script_desc *desc, const char *filename)
2875 char line[BUFSIZ], *p;
2878 fp = fopen(filename, "r");
2882 while (fgets(line, sizeof(line), fp)) {
2889 if (strlen(p) && *p == '!')
2893 if (strlen(p) && p[strlen(p) - 1] == '\n')
2894 p[strlen(p) - 1] = '\0';
2896 if (!strncmp(p, "description:", strlen("description:"))) {
2897 p += strlen("description:");
2898 desc->half_liner = strdup(ltrim(p));
2902 if (!strncmp(p, "args:", strlen("args:"))) {
2903 p += strlen("args:");
2904 desc->args = strdup(ltrim(p));
2914 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2916 char *script_root, *str;
2918 script_root = strdup(script_dirent->d_name);
2922 str = (char *)ends_with(script_root, suffix);
2932 static int list_available_scripts(const struct option *opt __maybe_unused,
2933 const char *s __maybe_unused,
2934 int unset __maybe_unused)
2936 struct dirent *script_dirent, *lang_dirent;
2937 char scripts_path[MAXPATHLEN];
2938 DIR *scripts_dir, *lang_dir;
2939 char script_path[MAXPATHLEN];
2940 char lang_path[MAXPATHLEN];
2941 struct script_desc *desc;
2942 char first_half[BUFSIZ];
2945 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2947 scripts_dir = opendir(scripts_path);
2950 "open(%s) failed.\n"
2951 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2956 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2957 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2958 lang_dirent->d_name);
2959 lang_dir = opendir(lang_path);
2963 for_each_script(lang_path, lang_dir, script_dirent) {
2964 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
2966 desc = script_desc__findnew(script_root);
2967 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2968 lang_path, script_dirent->d_name);
2969 read_script_info(desc, script_path);
2975 fprintf(stdout, "List of available trace scripts:\n");
2976 list_for_each_entry(desc, &script_descs, node) {
2977 sprintf(first_half, "%s %s", desc->name,
2978 desc->args ? desc->args : "");
2979 fprintf(stdout, " %-36s %s\n", first_half,
2980 desc->half_liner ? desc->half_liner : "");
2987 * Some scripts specify the required events in their "xxx-record" file,
2988 * this function will check if the events in perf.data match those
2989 * mentioned in the "xxx-record".
2991 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2992 * which is covered well now. And new parsing code should be added to
2993 * cover the future complexing formats like event groups etc.
2995 static int check_ev_match(char *dir_name, char *scriptname,
2996 struct perf_session *session)
2998 char filename[MAXPATHLEN], evname[128];
2999 char line[BUFSIZ], *p;
3000 struct perf_evsel *pos;
3004 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
3006 fp = fopen(filename, "r");
3010 while (fgets(line, sizeof(line), fp)) {
3016 p = strstr(p, "-e");
3022 len = strcspn(p, " \t");
3026 snprintf(evname, len + 1, "%s", p);
3029 evlist__for_each_entry(session->evlist, pos) {
3030 if (!strcmp(perf_evsel__name(pos), evname)) {
3048 * Return -1 if none is found, otherwise the actual scripts number.
3050 * Currently the only user of this function is the script browser, which
3051 * will list all statically runnable scripts, select one, execute it and
3052 * show the output in a perf browser.
3054 int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3057 struct dirent *script_dirent, *lang_dirent;
3058 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
3059 DIR *scripts_dir, *lang_dir;
3060 struct perf_session *session;
3061 struct perf_data data = {
3063 .mode = PERF_DATA_MODE_READ,
3068 session = perf_session__new(&data, false, NULL);
3072 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3074 scripts_dir = opendir(scripts_path);
3076 perf_session__delete(session);
3080 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3081 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3082 lang_dirent->d_name);
3083 #ifndef HAVE_LIBPERL_SUPPORT
3084 if (strstr(lang_path, "perl"))
3087 #ifndef HAVE_LIBPYTHON_SUPPORT
3088 if (strstr(lang_path, "python"))
3092 lang_dir = opendir(lang_path);
3096 for_each_script(lang_path, lang_dir, script_dirent) {
3097 /* Skip those real time scripts: xxxtop.p[yl] */
3098 if (strstr(script_dirent->d_name, "top."))
3102 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3104 script_dirent->d_name);
3105 temp = strchr(script_dirent->d_name, '.');
3106 snprintf(scripts_array[i],
3107 (temp - script_dirent->d_name) + 1,
3108 "%s", script_dirent->d_name);
3110 if (check_ev_match(lang_path,
3111 scripts_array[i], session))
3119 closedir(scripts_dir);
3120 perf_session__delete(session);
3124 static char *get_script_path(const char *script_root, const char *suffix)
3126 struct dirent *script_dirent, *lang_dirent;
3127 char scripts_path[MAXPATHLEN];
3128 char script_path[MAXPATHLEN];
3129 DIR *scripts_dir, *lang_dir;
3130 char lang_path[MAXPATHLEN];
3131 char *__script_root;
3133 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3135 scripts_dir = opendir(scripts_path);
3139 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3140 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3141 lang_dirent->d_name);
3142 lang_dir = opendir(lang_path);
3146 for_each_script(lang_path, lang_dir, script_dirent) {
3147 __script_root = get_script_root(script_dirent, suffix);
3148 if (__script_root && !strcmp(script_root, __script_root)) {
3149 free(__script_root);
3151 closedir(scripts_dir);
3152 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3153 lang_path, script_dirent->d_name);
3154 return strdup(script_path);
3156 free(__script_root);
3160 closedir(scripts_dir);
3165 static bool is_top_script(const char *script_path)
3167 return ends_with(script_path, "top") == NULL ? false : true;
3170 static int has_required_arg(char *script_path)
3172 struct script_desc *desc;
3176 desc = script_desc__new(NULL);
3178 if (read_script_info(desc, script_path))
3184 for (p = desc->args; *p; p++)
3188 script_desc__delete(desc);
3193 static int have_cmd(int argc, const char **argv)
3195 char **__argv = malloc(sizeof(const char *) * argc);
3198 pr_err("malloc failed\n");
3202 memcpy(__argv, argv, sizeof(const char *) * argc);
3203 argc = parse_options(argc, (const char **)__argv, record_options,
3204 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3207 system_wide = (argc == 0);
3212 static void script__setup_sample_type(struct perf_script *script)
3214 struct perf_session *session = script->session;
3215 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3217 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3218 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
3219 (sample_type & PERF_SAMPLE_STACK_USER)) {
3220 callchain_param.record_mode = CALLCHAIN_DWARF;
3221 dwarf_callchain_users = true;
3222 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
3223 callchain_param.record_mode = CALLCHAIN_LBR;
3225 callchain_param.record_mode = CALLCHAIN_FP;
3229 static int process_stat_round_event(struct perf_session *session,
3230 union perf_event *event)
3232 struct stat_round_event *round = &event->stat_round;
3233 struct perf_evsel *counter;
3235 evlist__for_each_entry(session->evlist, counter) {
3236 perf_stat_process_counter(&stat_config, counter);
3237 process_stat(counter, round->time);
3240 process_stat_interval(round->time);
3244 static int process_stat_config_event(struct perf_session *session __maybe_unused,
3245 union perf_event *event)
3247 perf_event__read_stat_config(&stat_config, &event->stat_config);
3251 static int set_maps(struct perf_script *script)
3253 struct perf_evlist *evlist = script->session->evlist;
3255 if (!script->cpus || !script->threads)
3258 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3261 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3263 if (perf_evlist__alloc_stats(evlist, true))
3266 script->allocated = true;
3271 int process_thread_map_event(struct perf_session *session,
3272 union perf_event *event)
3274 struct perf_tool *tool = session->tool;
3275 struct perf_script *script = container_of(tool, struct perf_script, tool);
3277 if (script->threads) {
3278 pr_warning("Extra thread map event, ignoring.\n");
3282 script->threads = thread_map__new_event(&event->thread_map);
3283 if (!script->threads)
3286 return set_maps(script);
3290 int process_cpu_map_event(struct perf_session *session,
3291 union perf_event *event)
3293 struct perf_tool *tool = session->tool;
3294 struct perf_script *script = container_of(tool, struct perf_script, tool);
3297 pr_warning("Extra cpu map event, ignoring.\n");
3301 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3305 return set_maps(script);
3308 static int process_feature_event(struct perf_session *session,
3309 union perf_event *event)
3311 if (event->feat.feat_id < HEADER_LAST_FEATURE)
3312 return perf_event__process_feature(session, event);
3316 #ifdef HAVE_AUXTRACE_SUPPORT
3317 static int perf_script__process_auxtrace_info(struct perf_session *session,
3318 union perf_event *event)
3320 struct perf_tool *tool = session->tool;
3322 int ret = perf_event__process_auxtrace_info(session, event);
3325 struct perf_script *script = container_of(tool, struct perf_script, tool);
3327 ret = perf_script__setup_per_event_dump(script);
3333 #define perf_script__process_auxtrace_info 0
3336 static int parse_insn_trace(const struct option *opt __maybe_unused,
3337 const char *str __maybe_unused,
3338 int unset __maybe_unused)
3340 parse_output_fields(NULL, "+insn,-event,-period", 0);
3341 itrace_parse_synth_opts(opt, "i0ns", 0);
3342 symbol_conf.nanosecs = true;
3346 static int parse_xed(const struct option *opt __maybe_unused,
3347 const char *str __maybe_unused,
3348 int unset __maybe_unused)
3350 force_pager("xed -F insn: -A -64 | less");
3354 static int parse_call_trace(const struct option *opt __maybe_unused,
3355 const char *str __maybe_unused,
3356 int unset __maybe_unused)
3358 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3359 itrace_parse_synth_opts(opt, "cewp", 0);
3360 symbol_conf.nanosecs = true;
3361 symbol_conf.pad_output_len_dso = 50;
3365 static int parse_callret_trace(const struct option *opt __maybe_unused,
3366 const char *str __maybe_unused,
3367 int unset __maybe_unused)
3369 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3370 itrace_parse_synth_opts(opt, "crewp", 0);
3371 symbol_conf.nanosecs = true;
3375 int cmd_script(int argc, const char **argv)
3377 bool show_full_info = false;
3378 bool header = false;
3379 bool header_only = false;
3380 bool script_started = false;
3381 char *rec_script_path = NULL;
3382 char *rep_script_path = NULL;
3383 struct perf_session *session;
3384 struct itrace_synth_opts itrace_synth_opts = {
3386 .default_no_sample = true,
3389 char *script_path = NULL;
3390 const char **__argv;
3392 struct perf_script script = {
3394 .sample = process_sample_event,
3395 .mmap = perf_event__process_mmap,
3396 .mmap2 = perf_event__process_mmap2,
3397 .comm = perf_event__process_comm,
3398 .namespaces = perf_event__process_namespaces,
3399 .exit = perf_event__process_exit,
3400 .fork = perf_event__process_fork,
3401 .attr = process_attr,
3402 .event_update = perf_event__process_event_update,
3403 .tracing_data = perf_event__process_tracing_data,
3404 .feature = process_feature_event,
3405 .build_id = perf_event__process_build_id,
3406 .id_index = perf_event__process_id_index,
3407 .auxtrace_info = perf_script__process_auxtrace_info,
3408 .auxtrace = perf_event__process_auxtrace,
3409 .auxtrace_error = perf_event__process_auxtrace_error,
3410 .stat = perf_event__process_stat_event,
3411 .stat_round = process_stat_round_event,
3412 .stat_config = process_stat_config_event,
3413 .thread_map = process_thread_map_event,
3414 .cpu_map = process_cpu_map_event,
3415 .ordered_events = true,
3416 .ordering_requires_timestamps = true,
3419 struct perf_data data = {
3420 .mode = PERF_DATA_MODE_READ,
3422 const struct option options[] = {
3423 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3424 "dump raw trace in ASCII"),
3425 OPT_INCR('v', "verbose", &verbose,
3426 "be more verbose (show symbol address, etc)"),
3427 OPT_BOOLEAN('L', "Latency", &latency_format,
3428 "show latency attributes (irqs/preemption disabled, etc)"),
3429 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3430 list_available_scripts),
3431 OPT_CALLBACK('s', "script", NULL, "name",
3432 "script file name (lang:script name, script name, or *)",
3434 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3435 "generate perf-script.xx script in specified language"),
3436 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3437 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3438 "do various checks like samples ordering and lost events"),
3439 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3440 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
3441 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3442 "file", "vmlinux pathname"),
3443 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3444 "file", "kallsyms pathname"),
3445 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3446 "When printing symbols do not display call chain"),
3447 OPT_CALLBACK(0, "symfs", NULL, "directory",
3448 "Look for files with symbols relative to this directory",
3449 symbol__config_symfs),
3450 OPT_CALLBACK('F', "fields", NULL, "str",
3451 "comma separated output fields prepend with 'type:'. "
3452 "+field to add and -field to remove."
3453 "Valid types: hw,sw,trace,raw,synth. "
3454 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
3455 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3456 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3457 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
3458 parse_output_fields),
3459 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3460 "system-wide collection from all CPUs"),
3461 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3462 "only consider these symbols"),
3463 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3464 "Decode instructions from itrace", parse_insn_trace),
3465 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3466 "Run xed disassembler on output", parse_xed),
3467 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3468 "Decode calls from from itrace", parse_call_trace),
3469 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3470 "Decode calls and returns from itrace", parse_callret_trace),
3471 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3472 "Only print symbols and callees with --call-trace/--call-ret-trace"),
3473 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3474 "Stop display of callgraph at these symbols"),
3475 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3476 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3477 "only display events for these comms"),
3478 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3479 "only consider symbols in these pids"),
3480 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3481 "only consider symbols in these tids"),
3482 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3483 "Set the maximum stack depth when parsing the callchain, "
3484 "anything beyond the specified depth will be ignored. "
3485 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3486 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
3487 OPT_BOOLEAN('I', "show-info", &show_full_info,
3488 "display extended information from perf.data file"),
3489 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3490 "Show the path of [kernel.kallsyms]"),
3491 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3492 "Show the fork/comm/exit events"),
3493 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3494 "Show the mmap events"),
3495 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3496 "Show context switch events (if recorded)"),
3497 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3498 "Show namespace events (if recorded)"),
3499 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3500 "Show lost events (if recorded)"),
3501 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3502 "Show round events (if recorded)"),
3503 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3504 "Show bpf related events (if recorded)"),
3505 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3506 "Dump trace output to files named by the monitored events"),
3507 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
3508 OPT_INTEGER(0, "max-blocks", &max_blocks,
3509 "Maximum number of code blocks to dump with brstackinsn"),
3510 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
3511 "Use 9 decimal places when displaying time"),
3512 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3513 "Instruction Tracing options\n" ITRACE_HELP,
3514 itrace_parse_synth_opts),
3515 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3516 "Show full source file name path for source lines"),
3517 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3518 "Enable symbol demangling"),
3519 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3520 "Enable kernel symbol demangling"),
3521 OPT_STRING(0, "time", &script.time_str, "str",
3522 "Time span of interest (start,stop)"),
3523 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3524 "Show inline function"),
3527 const char * const script_subcommands[] = { "record", "report", NULL };
3528 const char *script_usage[] = {
3529 "perf script [<options>]",
3530 "perf script [<options>] record <script> [<record-options>] <command>",
3531 "perf script [<options>] report <script> [script-args]",
3532 "perf script [<options>] <script> [<record-options>] <command>",
3533 "perf script [<options>] <top-script> [script-args]",
3537 perf_set_singlethreaded();
3541 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
3542 PARSE_OPT_STOP_AT_NON_OPTION);
3544 data.path = input_name;
3545 data.force = symbol_conf.force;
3547 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3548 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3549 if (!rec_script_path)
3550 return cmd_record(argc, argv);
3553 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3554 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3555 if (!rep_script_path) {
3557 "Please specify a valid report script"
3558 "(see 'perf script -l' for listing)\n");
3563 if (script.time_str && reltime) {
3564 fprintf(stderr, "Don't combine --reltime with --time\n");
3568 if (itrace_synth_opts.callchain &&
3569 itrace_synth_opts.callchain_sz > scripting_max_stack)
3570 scripting_max_stack = itrace_synth_opts.callchain_sz;
3572 /* make sure PERF_EXEC_PATH is set for scripts */
3573 set_argv_exec_path(get_argv_exec_path());
3575 if (argc && !script_name && !rec_script_path && !rep_script_path) {
3580 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3581 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3583 if (!rec_script_path && !rep_script_path) {
3584 usage_with_options_msg(script_usage, options,
3585 "Couldn't find script `%s'\n\n See perf"
3586 " script -l for available scripts.\n", argv[0]);
3589 if (is_top_script(argv[0])) {
3590 rep_args = argc - 1;
3594 rep_args = has_required_arg(rep_script_path);
3595 rec_args = (argc - 1) - rep_args;
3597 usage_with_options_msg(script_usage, options,
3598 "`%s' script requires options."
3599 "\n\n See perf script -l for available "
3600 "scripts and options.\n", argv[0]);
3604 if (pipe(live_pipe) < 0) {
3605 perror("failed to create pipe");
3611 perror("failed to fork");
3618 dup2(live_pipe[1], 1);
3619 close(live_pipe[0]);
3621 if (is_top_script(argv[0])) {
3623 } else if (!system_wide) {
3624 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3630 __argv = malloc((argc + 6) * sizeof(const char *));
3632 pr_err("malloc failed\n");
3637 __argv[j++] = "/bin/sh";
3638 __argv[j++] = rec_script_path;
3644 for (i = rep_args + 1; i < argc; i++)
3645 __argv[j++] = argv[i];
3648 execvp("/bin/sh", (char **)__argv);
3653 dup2(live_pipe[0], 0);
3654 close(live_pipe[1]);
3656 __argv = malloc((argc + 4) * sizeof(const char *));
3658 pr_err("malloc failed\n");
3664 __argv[j++] = "/bin/sh";
3665 __argv[j++] = rep_script_path;
3666 for (i = 1; i < rep_args + 1; i++)
3667 __argv[j++] = argv[i];
3672 execvp("/bin/sh", (char **)__argv);
3677 if (rec_script_path)
3678 script_path = rec_script_path;
3679 if (rep_script_path)
3680 script_path = rep_script_path;
3685 if (!rec_script_path)
3686 system_wide = false;
3687 else if (!system_wide) {
3688 if (have_cmd(argc - 1, &argv[1]) != 0) {
3694 __argv = malloc((argc + 2) * sizeof(const char *));
3696 pr_err("malloc failed\n");
3701 __argv[j++] = "/bin/sh";
3702 __argv[j++] = script_path;
3705 for (i = 2; i < argc; i++)
3706 __argv[j++] = argv[i];
3709 execvp("/bin/sh", (char **)__argv);
3719 session = perf_session__new(&data, false, &script.tool);
3720 if (session == NULL)
3723 if (header || header_only) {
3724 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
3725 perf_session__fprintf_info(session, stdout, show_full_info);
3730 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
3732 if (symbol__init(&session->header.env) < 0)
3736 if (!strcmp(uts.machine, session->header.env.arch) ||
3737 (!strcmp(uts.machine, "x86_64") &&
3738 !strcmp(session->header.env.arch, "i386")))
3741 script.session = session;
3742 script__setup_sample_type(&script);
3744 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3745 symbol_conf.graph_function)
3746 itrace_synth_opts.thread_stack = true;
3748 session->itrace_synth_opts = &itrace_synth_opts;
3751 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3754 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
3758 symbol_conf.use_callchain = true;
3760 symbol_conf.use_callchain = false;
3762 if (session->tevent.pevent &&
3763 tep_set_function_resolver(session->tevent.pevent,
3764 machine__resolve_kernel_addr,
3765 &session->machines.host) < 0) {
3766 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3771 if (generate_script_lang) {
3772 struct stat perf_stat;
3775 if (output_set_by_user()) {
3777 "custom fields not supported for generated scripts");
3782 input = open(data.path, O_RDONLY); /* input_name */
3785 perror("failed to open file");
3789 err = fstat(input, &perf_stat);
3791 perror("failed to stat file");
3795 if (!perf_stat.st_size) {
3796 fprintf(stderr, "zero-sized file, nothing to do!\n");
3800 scripting_ops = script_spec__lookup(generate_script_lang);
3801 if (!scripting_ops) {
3802 fprintf(stderr, "invalid language specifier");
3807 err = scripting_ops->generate_script(session->tevent.pevent,
3813 err = scripting_ops->start_script(script_name, argc, argv);
3816 pr_debug("perf script started with script %s\n\n", script_name);
3817 script_started = true;
3821 err = perf_session__check_output_opt(session);
3825 if (script.time_str) {
3826 err = perf_time__parse_for_ranges(script.time_str, session,
3827 &script.ptime_range,
3833 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3838 err = __cmd_script(&script);
3843 if (script.ptime_range) {
3844 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
3845 zfree(&script.ptime_range);
3848 perf_evlist__free_stats(session->evlist);
3849 perf_session__delete(session);
3852 cleanup_scripting();