1 // SPDX-License-Identifier: GPL-2.0
4 #include "util/counts.h"
5 #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/srcline.h"
15 #include "util/symbol.h"
16 #include "util/thread.h"
17 #include "util/trace-event.h"
18 #include "util/evlist.h"
19 #include "util/evsel.h"
20 #include "util/evswitch.h"
21 #include "util/sort.h"
22 #include "util/data.h"
23 #include "util/auxtrace.h"
24 #include "util/cpumap.h"
25 #include "util/thread_map.h"
26 #include "util/stat.h"
27 #include "util/color.h"
28 #include "util/string2.h"
29 #include "util/thread-stack.h"
30 #include "util/time-utils.h"
31 #include "util/path.h"
33 #include "print_binary.h"
35 #include <linux/bitmap.h>
36 #include <linux/kernel.h>
37 #include <linux/stringify.h>
38 #include <linux/time64.h>
39 #include <linux/zalloc.h>
40 #include <sys/utsname.h>
42 #include "util/mem-events.h"
43 #include "util/dump-insn.h"
48 #include <sys/param.h>
49 #include <sys/types.h>
53 #include <subcmd/pager.h>
54 #include <perf/evlist.h>
55 #include <linux/err.h>
56 #include "util/record.h"
57 #include "util/util.h"
60 #include <linux/ctype.h>
62 static char const *script_name;
63 static char const *generate_script_lang;
65 static u64 initial_time;
66 static bool debug_mode;
67 static u64 last_timestamp;
68 static u64 nr_unordered;
69 static bool no_callchain;
70 static bool latency_format;
71 static bool system_wide;
72 static bool print_flags;
73 static const char *cpu_list;
74 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
75 static struct perf_stat_config stat_config;
76 static int max_blocks;
77 static bool native_arch;
79 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
81 enum perf_output_field {
82 PERF_OUTPUT_COMM = 1U << 0,
83 PERF_OUTPUT_TID = 1U << 1,
84 PERF_OUTPUT_PID = 1U << 2,
85 PERF_OUTPUT_TIME = 1U << 3,
86 PERF_OUTPUT_CPU = 1U << 4,
87 PERF_OUTPUT_EVNAME = 1U << 5,
88 PERF_OUTPUT_TRACE = 1U << 6,
89 PERF_OUTPUT_IP = 1U << 7,
90 PERF_OUTPUT_SYM = 1U << 8,
91 PERF_OUTPUT_DSO = 1U << 9,
92 PERF_OUTPUT_ADDR = 1U << 10,
93 PERF_OUTPUT_SYMOFFSET = 1U << 11,
94 PERF_OUTPUT_SRCLINE = 1U << 12,
95 PERF_OUTPUT_PERIOD = 1U << 13,
96 PERF_OUTPUT_IREGS = 1U << 14,
97 PERF_OUTPUT_BRSTACK = 1U << 15,
98 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
99 PERF_OUTPUT_DATA_SRC = 1U << 17,
100 PERF_OUTPUT_WEIGHT = 1U << 18,
101 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
102 PERF_OUTPUT_CALLINDENT = 1U << 20,
103 PERF_OUTPUT_INSN = 1U << 21,
104 PERF_OUTPUT_INSNLEN = 1U << 22,
105 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
106 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
107 PERF_OUTPUT_SYNTH = 1U << 25,
108 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
109 PERF_OUTPUT_UREGS = 1U << 27,
110 PERF_OUTPUT_METRIC = 1U << 28,
111 PERF_OUTPUT_MISC = 1U << 29,
112 PERF_OUTPUT_SRCCODE = 1U << 30,
113 PERF_OUTPUT_IPC = 1U << 31,
116 struct output_option {
118 enum perf_output_field field;
119 } all_output_options[] = {
120 {.str = "comm", .field = PERF_OUTPUT_COMM},
121 {.str = "tid", .field = PERF_OUTPUT_TID},
122 {.str = "pid", .field = PERF_OUTPUT_PID},
123 {.str = "time", .field = PERF_OUTPUT_TIME},
124 {.str = "cpu", .field = PERF_OUTPUT_CPU},
125 {.str = "event", .field = PERF_OUTPUT_EVNAME},
126 {.str = "trace", .field = PERF_OUTPUT_TRACE},
127 {.str = "ip", .field = PERF_OUTPUT_IP},
128 {.str = "sym", .field = PERF_OUTPUT_SYM},
129 {.str = "dso", .field = PERF_OUTPUT_DSO},
130 {.str = "addr", .field = PERF_OUTPUT_ADDR},
131 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
132 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
133 {.str = "period", .field = PERF_OUTPUT_PERIOD},
134 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
135 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
136 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
137 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
138 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
139 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
140 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
141 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
142 {.str = "insn", .field = PERF_OUTPUT_INSN},
143 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
144 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
145 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
146 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
147 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
148 {.str = "metric", .field = PERF_OUTPUT_METRIC},
149 {.str = "misc", .field = PERF_OUTPUT_MISC},
150 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
151 {.str = "ipc", .field = PERF_OUTPUT_IPC},
155 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
159 /* default set to maintain compatibility with current format */
163 unsigned int print_ip_opts;
167 } output[OUTPUT_TYPE_MAX] = {
169 [PERF_TYPE_HARDWARE] = {
172 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
173 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
174 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
175 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
176 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
178 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
181 [PERF_TYPE_SOFTWARE] = {
184 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
185 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
186 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
187 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
188 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
189 PERF_OUTPUT_BPF_OUTPUT,
191 .invalid_fields = PERF_OUTPUT_TRACE,
194 [PERF_TYPE_TRACEPOINT] = {
197 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
198 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
199 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
202 [PERF_TYPE_HW_CACHE] = {
205 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
206 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
207 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
208 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
209 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
211 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
217 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
218 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
219 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
220 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
221 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
222 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
223 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
225 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
228 [PERF_TYPE_BREAKPOINT] = {
231 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
232 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
233 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
234 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
235 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
237 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
240 [OUTPUT_TYPE_SYNTH] = {
243 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
244 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
245 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
246 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
247 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
249 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
253 struct evsel_script {
257 /* For metric output */
262 static inline struct evsel_script *evsel_script(struct evsel *evsel)
264 return (struct evsel_script *)evsel->priv;
267 static struct evsel_script *perf_evsel_script__new(struct evsel *evsel,
268 struct perf_data *data)
270 struct evsel_script *es = zalloc(sizeof(*es));
273 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
275 es->fp = fopen(es->filename, "w");
277 goto out_free_filename;
282 zfree(&es->filename);
288 static void perf_evsel_script__delete(struct evsel_script *es)
290 zfree(&es->filename);
296 static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp)
300 fstat(fileno(es->fp), &st);
301 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
302 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
305 static inline int output_type(unsigned int type)
308 case PERF_TYPE_SYNTH:
309 return OUTPUT_TYPE_SYNTH;
315 static inline unsigned int attr_type(unsigned int type)
318 case OUTPUT_TYPE_SYNTH:
319 return PERF_TYPE_SYNTH;
325 static bool output_set_by_user(void)
328 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
329 if (output[j].user_set)
335 static const char *output_field2str(enum perf_output_field field)
337 int i, imax = ARRAY_SIZE(all_output_options);
338 const char *str = "";
340 for (i = 0; i < imax; ++i) {
341 if (all_output_options[i].field == field) {
342 str = all_output_options[i].str;
349 #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
351 static int perf_evsel__do_check_stype(struct evsel *evsel,
352 u64 sample_type, const char *sample_msg,
353 enum perf_output_field field,
356 struct perf_event_attr *attr = &evsel->core.attr;
357 int type = output_type(attr->type);
360 if (attr->sample_type & sample_type)
363 if (output[type].user_set_fields & field) {
366 evname = perf_evsel__name(evsel);
367 pr_err("Samples for '%s' event do not have %s attribute set. "
368 "Cannot print '%s' field.\n",
369 evname, sample_msg, output_field2str(field));
373 /* user did not ask for it explicitly so remove from the default list */
374 output[type].fields &= ~field;
375 evname = perf_evsel__name(evsel);
376 pr_debug("Samples for '%s' event do not have %s attribute set. "
377 "Skipping '%s' field.\n",
378 evname, sample_msg, output_field2str(field));
383 static int perf_evsel__check_stype(struct evsel *evsel,
384 u64 sample_type, const char *sample_msg,
385 enum perf_output_field field)
387 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
391 static int perf_evsel__check_attr(struct evsel *evsel,
392 struct perf_session *session)
394 struct perf_event_attr *attr = &evsel->core.attr;
397 if (perf_header__has_feat(&session->header, HEADER_STAT))
400 allow_user_set = perf_header__has_feat(&session->header,
403 if (PRINT_FIELD(TRACE) &&
404 !perf_session__has_traces(session, "record -R"))
407 if (PRINT_FIELD(IP)) {
408 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
413 if (PRINT_FIELD(ADDR) &&
414 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
415 PERF_OUTPUT_ADDR, allow_user_set))
418 if (PRINT_FIELD(DATA_SRC) &&
419 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
420 PERF_OUTPUT_DATA_SRC))
423 if (PRINT_FIELD(WEIGHT) &&
424 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
428 if (PRINT_FIELD(SYM) &&
429 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
430 pr_err("Display of symbols requested but neither sample IP nor "
431 "sample address\navailable. Hence, no addresses to convert "
435 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
436 pr_err("Display of offsets requested but symbol is not"
440 if (PRINT_FIELD(DSO) &&
441 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
442 pr_err("Display of DSO requested but no address to convert.\n");
445 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
446 pr_err("Display of source line number requested but sample IP is not\n"
447 "selected. Hence, no address to lookup the source line number.\n");
450 if (PRINT_FIELD(BRSTACKINSN) &&
451 !(perf_evlist__combined_branch_type(session->evlist) &
452 PERF_SAMPLE_BRANCH_ANY)) {
453 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
454 "Hint: run 'perf record -b ...'\n");
457 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
458 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
459 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
462 if (PRINT_FIELD(TIME) &&
463 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
467 if (PRINT_FIELD(CPU) &&
468 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
469 PERF_OUTPUT_CPU, allow_user_set))
472 if (PRINT_FIELD(IREGS) &&
473 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
477 if (PRINT_FIELD(UREGS) &&
478 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
482 if (PRINT_FIELD(PHYS_ADDR) &&
483 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
484 PERF_OUTPUT_PHYS_ADDR))
490 static void set_print_ip_opts(struct perf_event_attr *attr)
492 unsigned int type = output_type(attr->type);
494 output[type].print_ip_opts = 0;
496 output[type].print_ip_opts |= EVSEL__PRINT_IP;
498 if (PRINT_FIELD(SYM))
499 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
501 if (PRINT_FIELD(DSO))
502 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
504 if (PRINT_FIELD(SYMOFFSET))
505 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
507 if (PRINT_FIELD(SRCLINE))
508 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
512 * verify all user requested events exist and the samples
513 * have the expected data
515 static int perf_session__check_output_opt(struct perf_session *session)
520 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
521 evsel = perf_session__find_first_evtype(session, attr_type(j));
524 * even if fields is set to 0 (ie., show nothing) event must
525 * exist if user explicitly includes it on the command line
527 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
528 j != OUTPUT_TYPE_SYNTH) {
529 pr_err("%s events do not exist. "
530 "Remove corresponding -F option to proceed.\n",
535 if (evsel && output[j].fields &&
536 perf_evsel__check_attr(evsel, session))
542 set_print_ip_opts(&evsel->core.attr);
546 bool use_callchain = false;
547 bool not_pipe = false;
549 evlist__for_each_entry(session->evlist, evsel) {
551 if (evsel__has_callchain(evsel)) {
552 use_callchain = true;
556 if (not_pipe && !use_callchain)
557 symbol_conf.use_callchain = false;
561 * set default for tracepoints to print symbols only
562 * if callchains are present
564 if (symbol_conf.use_callchain &&
565 !output[PERF_TYPE_TRACEPOINT].user_set) {
566 j = PERF_TYPE_TRACEPOINT;
568 evlist__for_each_entry(session->evlist, evsel) {
569 if (evsel->core.attr.type != j)
572 if (evsel__has_callchain(evsel)) {
573 output[j].fields |= PERF_OUTPUT_IP;
574 output[j].fields |= PERF_OUTPUT_SYM;
575 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
576 output[j].fields |= PERF_OUTPUT_DSO;
577 set_print_ip_opts(&evsel->core.attr);
587 static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
594 if (!regs || !regs->regs)
597 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
599 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
600 u64 val = regs->regs[i++];
601 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
609 static int perf_sample__fprintf_iregs(struct perf_sample *sample,
610 struct perf_event_attr *attr, FILE *fp)
612 return perf_sample__fprintf_regs(&sample->intr_regs,
613 attr->sample_regs_intr, fp);
616 static int perf_sample__fprintf_uregs(struct perf_sample *sample,
617 struct perf_event_attr *attr, FILE *fp)
619 return perf_sample__fprintf_regs(&sample->user_regs,
620 attr->sample_regs_user, fp);
623 static int perf_sample__fprintf_start(struct perf_sample *sample,
624 struct thread *thread,
628 struct perf_event_attr *attr = &evsel->core.attr;
630 unsigned long long nsecs;
633 if (PRINT_FIELD(COMM)) {
635 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
636 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
637 printed += fprintf(fp, "%s ", thread__comm_str(thread));
639 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
642 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
643 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
644 else if (PRINT_FIELD(PID))
645 printed += fprintf(fp, "%5d ", sample->pid);
646 else if (PRINT_FIELD(TID))
647 printed += fprintf(fp, "%5d ", sample->tid);
649 if (PRINT_FIELD(CPU)) {
651 printed += fprintf(fp, "%3d ", sample->cpu);
653 printed += fprintf(fp, "[%03d] ", sample->cpu);
656 if (PRINT_FIELD(MISC)) {
660 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
663 ret += fprintf(fp, "K");
665 ret += fprintf(fp, "U");
667 ret += fprintf(fp, "H");
668 if (has(GUEST_KERNEL))
669 ret += fprintf(fp, "G");
671 ret += fprintf(fp, "g");
674 case PERF_RECORD_MMAP:
675 case PERF_RECORD_MMAP2:
677 ret += fprintf(fp, "M");
679 case PERF_RECORD_COMM:
681 ret += fprintf(fp, "E");
683 case PERF_RECORD_SWITCH:
684 case PERF_RECORD_SWITCH_CPU_WIDE:
685 if (has(SWITCH_OUT)) {
686 ret += fprintf(fp, "S");
687 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
688 ret += fprintf(fp, "p");
696 ret += fprintf(fp, "%*s", 6 - ret, " ");
700 if (PRINT_FIELD(TIME)) {
701 u64 t = sample->time;
704 initial_time = sample->time;
705 t = sample->time - initial_time;
708 secs = nsecs / NSEC_PER_SEC;
709 nsecs -= secs * NSEC_PER_SEC;
711 if (symbol_conf.nanosecs)
712 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
714 char sample_time[32];
715 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
716 printed += fprintf(fp, "%12s: ", sample_time);
724 mispred_str(struct branch_entry *br)
726 if (!(br->flags.mispred || br->flags.predicted))
729 return br->flags.predicted ? 'P' : 'M';
732 static int perf_sample__fprintf_brstack(struct perf_sample *sample,
733 struct thread *thread,
734 struct perf_event_attr *attr, FILE *fp)
736 struct branch_stack *br = sample->branch_stack;
737 struct addr_location alf, alt;
744 for (i = 0; i < br->nr; i++) {
745 from = br->entries[i].from;
746 to = br->entries[i].to;
748 if (PRINT_FIELD(DSO)) {
749 memset(&alf, 0, sizeof(alf));
750 memset(&alt, 0, sizeof(alt));
751 thread__find_map_fb(thread, sample->cpumode, from, &alf);
752 thread__find_map_fb(thread, sample->cpumode, to, &alt);
755 printed += fprintf(fp, " 0x%"PRIx64, from);
756 if (PRINT_FIELD(DSO)) {
757 printed += fprintf(fp, "(");
758 printed += map__fprintf_dsoname(alf.map, fp);
759 printed += fprintf(fp, ")");
762 printed += fprintf(fp, "/0x%"PRIx64, to);
763 if (PRINT_FIELD(DSO)) {
764 printed += fprintf(fp, "(");
765 printed += map__fprintf_dsoname(alt.map, fp);
766 printed += fprintf(fp, ")");
769 printed += fprintf(fp, "/%c/%c/%c/%d ",
770 mispred_str( br->entries + i),
771 br->entries[i].flags.in_tx? 'X' : '-',
772 br->entries[i].flags.abort? 'A' : '-',
773 br->entries[i].flags.cycles);
779 static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
780 struct thread *thread,
781 struct perf_event_attr *attr, FILE *fp)
783 struct branch_stack *br = sample->branch_stack;
784 struct addr_location alf, alt;
791 for (i = 0; i < br->nr; i++) {
793 memset(&alf, 0, sizeof(alf));
794 memset(&alt, 0, sizeof(alt));
795 from = br->entries[i].from;
796 to = br->entries[i].to;
798 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
799 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
801 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
802 if (PRINT_FIELD(DSO)) {
803 printed += fprintf(fp, "(");
804 printed += map__fprintf_dsoname(alf.map, fp);
805 printed += fprintf(fp, ")");
807 printed += fprintf(fp, "%c", '/');
808 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
809 if (PRINT_FIELD(DSO)) {
810 printed += fprintf(fp, "(");
811 printed += map__fprintf_dsoname(alt.map, fp);
812 printed += fprintf(fp, ")");
814 printed += fprintf(fp, "/%c/%c/%c/%d ",
815 mispred_str( br->entries + i),
816 br->entries[i].flags.in_tx? 'X' : '-',
817 br->entries[i].flags.abort? 'A' : '-',
818 br->entries[i].flags.cycles);
824 static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
825 struct thread *thread,
826 struct perf_event_attr *attr, FILE *fp)
828 struct branch_stack *br = sample->branch_stack;
829 struct addr_location alf, alt;
836 for (i = 0; i < br->nr; i++) {
838 memset(&alf, 0, sizeof(alf));
839 memset(&alt, 0, sizeof(alt));
840 from = br->entries[i].from;
841 to = br->entries[i].to;
843 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
844 !alf.map->dso->adjust_symbols)
845 from = map__map_ip(alf.map, from);
847 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
848 !alt.map->dso->adjust_symbols)
849 to = map__map_ip(alt.map, to);
851 printed += fprintf(fp, " 0x%"PRIx64, from);
852 if (PRINT_FIELD(DSO)) {
853 printed += fprintf(fp, "(");
854 printed += map__fprintf_dsoname(alf.map, fp);
855 printed += fprintf(fp, ")");
857 printed += fprintf(fp, "/0x%"PRIx64, to);
858 if (PRINT_FIELD(DSO)) {
859 printed += fprintf(fp, "(");
860 printed += map__fprintf_dsoname(alt.map, fp);
861 printed += fprintf(fp, ")");
863 printed += fprintf(fp, "/%c/%c/%c/%d ",
864 mispred_str(br->entries + i),
865 br->entries[i].flags.in_tx ? 'X' : '-',
866 br->entries[i].flags.abort ? 'A' : '-',
867 br->entries[i].flags.cycles);
872 #define MAXBB 16384UL
874 static int grab_bb(u8 *buffer, u64 start, u64 end,
875 struct machine *machine, struct thread *thread,
876 bool *is64bit, u8 *cpumode, bool last)
879 struct addr_location al;
885 kernel = machine__kernel_ip(machine, start);
887 *cpumode = PERF_RECORD_MISC_KERNEL;
889 *cpumode = PERF_RECORD_MISC_USER;
892 * Block overlaps between kernel and user.
893 * This can happen due to ring filtering
894 * On Intel CPUs the entry into the kernel is filtered,
895 * but the exit is not. Let the caller patch it up.
897 if (kernel != machine__kernel_ip(machine, end)) {
898 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
902 memset(&al, 0, sizeof(al));
903 if (end - start > MAXBB - MAXINSN) {
905 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
907 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
911 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
912 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
915 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
916 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
920 /* Load maps to ensure dso->is_64_bit has been updated */
923 offset = al.map->map_ip(al.map, start);
924 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
925 end - start + MAXINSN);
927 *is64bit = al.map->dso->is_64_bit;
929 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
934 static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
936 struct addr_location al;
939 memset(&al, 0, sizeof(al));
940 thread__find_map(thread, cpumode, addr, &al);
943 ret = map__fprintf_srccode(al.map, al.addr, stdout,
944 &thread->srccode_state);
950 static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
951 struct perf_insn *x, u8 *inbuf, int len,
952 int insn, FILE *fp, int *total_cycles)
954 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
955 dump_insn(x, ip, inbuf, len, NULL),
956 en->flags.predicted ? " PRED" : "",
957 en->flags.mispred ? " MISPRED" : "",
958 en->flags.in_tx ? " INTX" : "",
959 en->flags.abort ? " ABORT" : "");
960 if (en->flags.cycles) {
961 *total_cycles += en->flags.cycles;
962 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
964 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
966 return printed + fprintf(fp, "\n");
969 static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
970 u8 cpumode, int cpu, struct symbol **lastsym,
971 struct perf_event_attr *attr, FILE *fp)
973 struct addr_location al;
974 int off, printed = 0;
976 memset(&al, 0, sizeof(al));
978 thread__find_map(thread, cpumode, addr, &al);
980 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
986 al.sym = map__find_symbol(al.map, al.addr);
991 if (al.addr < al.sym->end)
992 off = al.addr - al.sym->start;
994 off = al.addr - al.map->start - al.sym->start;
995 printed += fprintf(fp, "\t%s", al.sym->name);
997 printed += fprintf(fp, "%+d", off);
998 printed += fprintf(fp, ":");
999 if (PRINT_FIELD(SRCLINE))
1000 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
1001 printed += fprintf(fp, "\n");
1007 static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1008 struct thread *thread,
1009 struct perf_event_attr *attr,
1010 struct machine *machine, FILE *fp)
1012 struct branch_stack *br = sample->branch_stack;
1014 int i, insn, len, nr, ilen, printed = 0;
1018 struct symbol *lastsym = NULL;
1019 int total_cycles = 0;
1021 if (!(br && br->nr))
1024 if (max_blocks && nr > max_blocks + 1)
1025 nr = max_blocks + 1;
1028 x.cpu = sample->cpu;
1030 printed += fprintf(fp, "%c", '\n');
1032 /* Handle first from jump, of which we don't know the entry. */
1033 len = grab_bb(buffer, br->entries[nr-1].from,
1034 br->entries[nr-1].from,
1035 machine, thread, &x.is64bit, &x.cpumode, false);
1037 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1038 x.cpumode, x.cpu, &lastsym, attr, fp);
1039 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
1040 &x, buffer, len, 0, fp, &total_cycles);
1041 if (PRINT_FIELD(SRCCODE))
1042 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
1045 /* Print all blocks */
1046 for (i = nr - 2; i >= 0; i--) {
1047 if (br->entries[i].from || br->entries[i].to)
1048 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1049 br->entries[i].from,
1051 start = br->entries[i + 1].to;
1052 end = br->entries[i].from;
1054 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1055 /* Patch up missing kernel transfers due to ring filters */
1056 if (len == -ENXIO && i > 0) {
1057 end = br->entries[--i].from;
1058 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1059 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1065 for (off = 0;; off += ilen) {
1066 uint64_t ip = start + off;
1068 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1070 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, ++insn, fp,
1072 if (PRINT_FIELD(SRCCODE))
1073 printed += print_srccode(thread, x.cpumode, ip);
1076 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1077 dump_insn(&x, ip, buffer + off, len - off, &ilen));
1080 if (PRINT_FIELD(SRCCODE))
1081 print_srccode(thread, x.cpumode, ip);
1088 * Hit the branch? In this case we are already done, and the target
1089 * has not been executed yet.
1091 if (br->entries[0].from == sample->ip)
1093 if (br->entries[0].flags.abort)
1097 * Print final block upto sample
1099 * Due to pipeline delays the LBRs might be missing a branch
1100 * or two, which can result in very large or negative blocks
1101 * between final branch and sample. When this happens just
1102 * continue walking after the last TO until we hit a branch.
1104 start = br->entries[0].to;
1107 /* Missing jump. Scan 128 bytes for the next branch */
1110 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
1111 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1113 /* Print at least last IP if basic block did not work */
1114 len = grab_bb(buffer, sample->ip, sample->ip,
1115 machine, thread, &x.is64bit, &x.cpumode, false);
1118 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
1119 dump_insn(&x, sample->ip, buffer, len, NULL));
1120 if (PRINT_FIELD(SRCCODE))
1121 print_srccode(thread, x.cpumode, sample->ip);
1124 for (off = 0; off <= end - start; off += ilen) {
1125 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1126 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
1129 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1131 * Hit a missing branch. Just stop.
1133 printed += fprintf(fp, "\t... not reaching sample ...\n");
1136 if (PRINT_FIELD(SRCCODE))
1137 print_srccode(thread, x.cpumode, start + off);
1143 static int perf_sample__fprintf_addr(struct perf_sample *sample,
1144 struct thread *thread,
1145 struct perf_event_attr *attr, FILE *fp)
1147 struct addr_location al;
1148 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
1150 if (!sample_addr_correlates_sym(attr))
1153 thread__resolve(thread, &al, sample);
1155 if (PRINT_FIELD(SYM)) {
1156 printed += fprintf(fp, " ");
1157 if (PRINT_FIELD(SYMOFFSET))
1158 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
1160 printed += symbol__fprintf_symname(al.sym, fp);
1163 if (PRINT_FIELD(DSO)) {
1164 printed += fprintf(fp, " (");
1165 printed += map__fprintf_dsoname(al.map, fp);
1166 printed += fprintf(fp, ")");
1172 static const char *resolve_branch_sym(struct perf_sample *sample,
1173 struct evsel *evsel,
1174 struct thread *thread,
1175 struct addr_location *al,
1178 struct addr_location addr_al;
1179 struct perf_event_attr *attr = &evsel->core.attr;
1180 const char *name = NULL;
1182 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1183 if (sample_addr_correlates_sym(attr)) {
1184 thread__resolve(thread, &addr_al, sample);
1186 name = addr_al.sym->name;
1192 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1194 name = al->sym->name;
1201 static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1202 struct evsel *evsel,
1203 struct thread *thread,
1204 struct addr_location *al, FILE *fp)
1206 struct perf_event_attr *attr = &evsel->core.attr;
1207 size_t depth = thread_stack__depth(thread, sample->cpu);
1208 const char *name = NULL;
1215 * The 'return' has already been popped off the stack so the depth has
1216 * to be adjusted to match the 'call'.
1218 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1221 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
1223 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1224 dlen += fprintf(fp, "(");
1225 dlen += map__fprintf_dsoname(al->map, fp);
1226 dlen += fprintf(fp, ")\t");
1230 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
1232 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
1238 * Try to keep the output length from changing frequently so that the
1239 * output lines up more nicely.
1241 if (len > spacing || (len && len < spacing - 52))
1242 spacing = round_up(len + 4, 32);
1245 len += fprintf(fp, "%*s", spacing - len, "");
1250 __weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1251 struct thread *thread __maybe_unused,
1252 struct machine *machine __maybe_unused)
1256 static int perf_sample__fprintf_insn(struct perf_sample *sample,
1257 struct perf_event_attr *attr,
1258 struct thread *thread,
1259 struct machine *machine, FILE *fp)
1263 if (sample->insn_len == 0 && native_arch)
1264 arch_fetch_insn(sample, thread, machine);
1266 if (PRINT_FIELD(INSNLEN))
1267 printed += fprintf(fp, " ilen: %d", sample->insn_len);
1268 if (PRINT_FIELD(INSN) && sample->insn_len) {
1271 printed += fprintf(fp, " insn:");
1272 for (i = 0; i < sample->insn_len; i++)
1273 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
1275 if (PRINT_FIELD(BRSTACKINSN))
1276 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1281 static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1282 struct perf_event_attr *attr, FILE *fp)
1286 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1289 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1291 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1292 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1295 static int perf_sample__fprintf_bts(struct perf_sample *sample,
1296 struct evsel *evsel,
1297 struct thread *thread,
1298 struct addr_location *al,
1299 struct machine *machine, FILE *fp)
1301 struct perf_event_attr *attr = &evsel->core.attr;
1302 unsigned int type = output_type(attr->type);
1303 bool print_srcline_last = false;
1306 if (PRINT_FIELD(CALLINDENT))
1307 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
1309 /* print branch_from information */
1310 if (PRINT_FIELD(IP)) {
1311 unsigned int print_opts = output[type].print_ip_opts;
1312 struct callchain_cursor *cursor = NULL;
1314 if (symbol_conf.use_callchain && sample->callchain &&
1315 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1316 sample, NULL, NULL, scripting_max_stack) == 0)
1317 cursor = &callchain_cursor;
1319 if (cursor == NULL) {
1320 printed += fprintf(fp, " ");
1321 if (print_opts & EVSEL__PRINT_SRCLINE) {
1322 print_srcline_last = true;
1323 print_opts &= ~EVSEL__PRINT_SRCLINE;
1326 printed += fprintf(fp, "\n");
1328 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
1331 /* print branch_to information */
1332 if (PRINT_FIELD(ADDR) ||
1333 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
1334 !output[type].user_set)) {
1335 printed += fprintf(fp, " => ");
1336 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
1339 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1341 if (print_srcline_last)
1342 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
1344 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1345 printed += fprintf(fp, "\n");
1346 if (PRINT_FIELD(SRCCODE)) {
1347 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1348 &thread->srccode_state);
1351 printed += printf("\n");
1360 } sample_flags[] = {
1361 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1362 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1363 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1364 {PERF_IP_FLAG_BRANCH, "jmp"},
1365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1366 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1367 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1368 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1369 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1370 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1371 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1372 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1373 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1377 static const char *sample_flags_to_name(u32 flags)
1381 for (i = 0; sample_flags[i].name ; i++) {
1382 if (sample_flags[i].flags == flags)
1383 return sample_flags[i].name;
1389 static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1391 const char *chars = PERF_IP_FLAG_CHARS;
1392 const int n = strlen(PERF_IP_FLAG_CHARS);
1393 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1394 const char *name = NULL;
1398 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1400 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1402 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1403 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1405 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1408 if (flags & PERF_IP_FLAG_TRACE_END) {
1409 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1411 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
1414 for (i = 0; i < n; i++, flags >>= 1) {
1416 str[pos++] = chars[i];
1418 for (; i < 32; i++, flags >>= 1) {
1424 return fprintf(fp, " %-19s ", str);
1427 struct printer_data {
1433 static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1435 void *extra, FILE *fp)
1437 unsigned char ch = (unsigned char)val;
1438 struct printer_data *printer_data = extra;
1442 case BINARY_PRINT_DATA_BEGIN:
1443 printed += fprintf(fp, "\n");
1445 case BINARY_PRINT_LINE_BEGIN:
1446 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
1449 case BINARY_PRINT_ADDR:
1450 printed += fprintf(fp, " %04x:", val);
1452 case BINARY_PRINT_NUM_DATA:
1453 printed += fprintf(fp, " %02x", val);
1455 case BINARY_PRINT_NUM_PAD:
1456 printed += fprintf(fp, " ");
1458 case BINARY_PRINT_SEP:
1459 printed += fprintf(fp, " ");
1461 case BINARY_PRINT_CHAR_DATA:
1462 if (printer_data->hit_nul && ch)
1463 printer_data->is_printable = false;
1466 printed += fprintf(fp, "%c", '.');
1468 if (!printer_data->is_printable)
1472 printer_data->hit_nul = true;
1474 printer_data->is_printable = false;
1476 printed += fprintf(fp, "%c", ch);
1479 case BINARY_PRINT_CHAR_PAD:
1480 printed += fprintf(fp, " ");
1482 case BINARY_PRINT_LINE_END:
1483 printed += fprintf(fp, "\n");
1484 printer_data->line_no++;
1486 case BINARY_PRINT_DATA_END:
1494 static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
1496 unsigned int nr_bytes = sample->raw_size;
1497 struct printer_data printer_data = {0, false, true};
1498 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1499 sample__fprintf_bpf_output, &printer_data, fp);
1501 if (printer_data.is_printable && printer_data.hit_nul)
1502 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1507 static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
1509 if (len > 0 && len < spacing)
1510 return fprintf(fp, "%*s", spacing - len, "");
1515 static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
1517 return perf_sample__fprintf_spacing(len, 34, fp);
1520 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
1522 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1525 if (perf_sample__bad_synth_size(sample, *data))
1528 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
1529 data->ip, le64_to_cpu(data->payload));
1530 return len + perf_sample__fprintf_pt_spacing(len, fp);
1533 static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
1535 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1538 if (perf_sample__bad_synth_size(sample, *data))
1541 len = fprintf(fp, " hints: %#x extensions: %#x ",
1542 data->hints, data->extensions);
1543 return len + perf_sample__fprintf_pt_spacing(len, fp);
1546 static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
1548 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1551 if (perf_sample__bad_synth_size(sample, *data))
1554 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1555 data->hw, data->cstate, data->subcstate);
1556 return len + perf_sample__fprintf_pt_spacing(len, fp);
1559 static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
1561 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1564 if (perf_sample__bad_synth_size(sample, *data))
1567 len = fprintf(fp, " IP: %u ", data->ip);
1568 return len + perf_sample__fprintf_pt_spacing(len, fp);
1571 static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
1573 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1576 if (perf_sample__bad_synth_size(sample, *data))
1579 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1580 data->deepest_cstate, data->last_cstate,
1582 return len + perf_sample__fprintf_pt_spacing(len, fp);
1585 static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
1587 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1588 unsigned int percent, freq;
1591 if (perf_sample__bad_synth_size(sample, *data))
1594 freq = (le32_to_cpu(data->freq) + 500) / 1000;
1595 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
1596 if (data->max_nonturbo) {
1597 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1598 len += fprintf(fp, "(%3u%%) ", percent);
1600 return len + perf_sample__fprintf_pt_spacing(len, fp);
1603 static int perf_sample__fprintf_synth(struct perf_sample *sample,
1604 struct evsel *evsel, FILE *fp)
1606 switch (evsel->core.attr.config) {
1607 case PERF_SYNTH_INTEL_PTWRITE:
1608 return perf_sample__fprintf_synth_ptwrite(sample, fp);
1609 case PERF_SYNTH_INTEL_MWAIT:
1610 return perf_sample__fprintf_synth_mwait(sample, fp);
1611 case PERF_SYNTH_INTEL_PWRE:
1612 return perf_sample__fprintf_synth_pwre(sample, fp);
1613 case PERF_SYNTH_INTEL_EXSTOP:
1614 return perf_sample__fprintf_synth_exstop(sample, fp);
1615 case PERF_SYNTH_INTEL_PWRX:
1616 return perf_sample__fprintf_synth_pwrx(sample, fp);
1617 case PERF_SYNTH_INTEL_CBR:
1618 return perf_sample__fprintf_synth_cbr(sample, fp);
1626 struct perf_script {
1627 struct perf_tool tool;
1628 struct perf_session *session;
1629 bool show_task_events;
1630 bool show_mmap_events;
1631 bool show_switch_events;
1632 bool show_namespace_events;
1633 bool show_lost_events;
1634 bool show_round_events;
1635 bool show_bpf_events;
1637 bool per_event_dump;
1638 struct evswitch evswitch;
1639 struct perf_cpu_map *cpus;
1640 struct perf_thread_map *threads;
1642 const char *time_str;
1643 struct perf_time_interval *ptime_range;
1648 static int perf_evlist__max_name_len(struct evlist *evlist)
1650 struct evsel *evsel;
1653 evlist__for_each_entry(evlist, evsel) {
1654 int len = strlen(perf_evsel__name(evsel));
1656 max = MAX(len, max);
1662 static int data_src__fprintf(u64 data_src, FILE *fp)
1664 struct mem_info mi = { .data_src.val = data_src };
1670 perf_script__meminfo_scnprintf(decode, 100, &mi);
1672 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1676 return fprintf(fp, "%-*s", maxlen, out);
1680 struct perf_sample *sample;
1681 struct thread *thread;
1682 struct evsel *evsel;
1686 static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1687 void *ctx, const char *color,
1689 const char *unit, double val)
1691 struct metric_ctx *mctx = ctx;
1695 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1696 PERF_RECORD_SAMPLE, mctx->fp);
1697 fputs("\tmetric: ", mctx->fp);
1699 color_fprintf(mctx->fp, color, fmt, val);
1702 fprintf(mctx->fp, " %s\n", unit);
1705 static void script_new_line(struct perf_stat_config *config __maybe_unused,
1708 struct metric_ctx *mctx = ctx;
1710 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1711 PERF_RECORD_SAMPLE, mctx->fp);
1712 fputs("\tmetric: ", mctx->fp);
1715 static void perf_sample__fprint_metric(struct perf_script *script,
1716 struct thread *thread,
1717 struct evsel *evsel,
1718 struct perf_sample *sample,
1721 struct perf_stat_output_ctx ctx = {
1722 .print_metric = script_print_metric,
1723 .new_line = script_new_line,
1724 .ctx = &(struct metric_ctx) {
1730 .force_header = false,
1736 perf_evlist__alloc_stats(script->session->evlist, false);
1737 if (evsel_script(evsel->leader)->gnum++ == 0)
1738 perf_stat__reset_shadow_stats();
1739 val = sample->period * evsel->scale;
1740 perf_stat__update_shadow_stats(evsel,
1744 evsel_script(evsel)->val = val;
1745 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
1746 for_each_group_member (ev2, evsel->leader) {
1747 perf_stat__print_shadow_stats(&stat_config, ev2,
1748 evsel_script(ev2)->val,
1754 evsel_script(evsel->leader)->gnum = 0;
1758 static bool show_event(struct perf_sample *sample,
1759 struct evsel *evsel,
1760 struct thread *thread,
1761 struct addr_location *al)
1763 int depth = thread_stack__depth(thread, sample->cpu);
1765 if (!symbol_conf.graph_function)
1768 if (thread->filter) {
1769 if (depth <= thread->filter_entry_depth) {
1770 thread->filter = false;
1775 const char *s = symbol_conf.graph_function;
1777 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1783 nlen = strlen(name);
1785 unsigned len = strcspn(s, ",");
1786 if (nlen == len && !strncmp(name, s, len)) {
1787 thread->filter = true;
1788 thread->filter_entry_depth = depth;
1799 static void process_event(struct perf_script *script,
1800 struct perf_sample *sample, struct evsel *evsel,
1801 struct addr_location *al,
1802 struct machine *machine)
1804 struct thread *thread = al->thread;
1805 struct perf_event_attr *attr = &evsel->core.attr;
1806 unsigned int type = output_type(attr->type);
1807 struct evsel_script *es = evsel->priv;
1810 if (output[type].fields == 0)
1813 if (!show_event(sample, evsel, thread, al))
1816 if (evswitch__discard(&script->evswitch, evsel))
1821 perf_sample__fprintf_start(sample, thread, evsel,
1822 PERF_RECORD_SAMPLE, fp);
1824 if (PRINT_FIELD(PERIOD))
1825 fprintf(fp, "%10" PRIu64 " ", sample->period);
1827 if (PRINT_FIELD(EVNAME)) {
1828 const char *evname = perf_evsel__name(evsel);
1830 if (!script->name_width)
1831 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1833 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
1837 perf_sample__fprintf_flags(sample->flags, fp);
1839 if (is_bts_event(attr)) {
1840 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
1844 if (PRINT_FIELD(TRACE) && sample->raw_data) {
1845 event_format__fprintf(evsel->tp_format, sample->cpu,
1846 sample->raw_data, sample->raw_size, fp);
1849 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
1850 perf_sample__fprintf_synth(sample, evsel, fp);
1852 if (PRINT_FIELD(ADDR))
1853 perf_sample__fprintf_addr(sample, thread, attr, fp);
1855 if (PRINT_FIELD(DATA_SRC))
1856 data_src__fprintf(sample->data_src, fp);
1858 if (PRINT_FIELD(WEIGHT))
1859 fprintf(fp, "%16" PRIu64, sample->weight);
1861 if (PRINT_FIELD(IP)) {
1862 struct callchain_cursor *cursor = NULL;
1864 if (symbol_conf.use_callchain && sample->callchain &&
1865 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1866 sample, NULL, NULL, scripting_max_stack) == 0)
1867 cursor = &callchain_cursor;
1869 fputc(cursor ? '\n' : ' ', fp);
1870 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
1873 if (PRINT_FIELD(IREGS))
1874 perf_sample__fprintf_iregs(sample, attr, fp);
1876 if (PRINT_FIELD(UREGS))
1877 perf_sample__fprintf_uregs(sample, attr, fp);
1879 if (PRINT_FIELD(BRSTACK))
1880 perf_sample__fprintf_brstack(sample, thread, attr, fp);
1881 else if (PRINT_FIELD(BRSTACKSYM))
1882 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
1883 else if (PRINT_FIELD(BRSTACKOFF))
1884 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
1886 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1887 perf_sample__fprintf_bpf_output(sample, fp);
1888 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1890 if (PRINT_FIELD(PHYS_ADDR))
1891 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1893 perf_sample__fprintf_ipc(sample, attr, fp);
1897 if (PRINT_FIELD(SRCCODE)) {
1898 if (map__fprintf_srccode(al->map, al->addr, stdout,
1899 &thread->srccode_state))
1903 if (PRINT_FIELD(METRIC))
1904 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
1910 static struct scripting_ops *scripting_ops;
1912 static void __process_stat(struct evsel *counter, u64 tstamp)
1914 int nthreads = perf_thread_map__nr(counter->core.threads);
1915 int ncpus = perf_evsel__nr_cpus(counter);
1917 static int header_printed;
1919 if (counter->core.system_wide)
1922 if (!header_printed) {
1923 printf("%3s %8s %15s %15s %15s %15s %s\n",
1924 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1928 for (thread = 0; thread < nthreads; thread++) {
1929 for (cpu = 0; cpu < ncpus; cpu++) {
1930 struct perf_counts_values *counts;
1932 counts = perf_counts(counter->counts, cpu, thread);
1934 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1935 counter->core.cpus->map[cpu],
1936 perf_thread_map__pid(counter->core.threads, thread),
1941 perf_evsel__name(counter));
1946 static void process_stat(struct evsel *counter, u64 tstamp)
1948 if (scripting_ops && scripting_ops->process_stat)
1949 scripting_ops->process_stat(&stat_config, counter, tstamp);
1951 __process_stat(counter, tstamp);
1954 static void process_stat_interval(u64 tstamp)
1956 if (scripting_ops && scripting_ops->process_stat_interval)
1957 scripting_ops->process_stat_interval(tstamp);
1960 static void setup_scripting(void)
1962 setup_perl_scripting();
1963 setup_python_scripting();
1966 static int flush_scripting(void)
1968 return scripting_ops ? scripting_ops->flush_script() : 0;
1971 static int cleanup_scripting(void)
1973 pr_debug("\nperf script stopped\n");
1975 return scripting_ops ? scripting_ops->stop_script() : 0;
1978 static bool filter_cpu(struct perf_sample *sample)
1981 return !test_bit(sample->cpu, cpu_bitmap);
1985 static int process_sample_event(struct perf_tool *tool,
1986 union perf_event *event,
1987 struct perf_sample *sample,
1988 struct evsel *evsel,
1989 struct machine *machine)
1991 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1992 struct addr_location al;
1994 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2000 if (sample->time < last_timestamp) {
2001 pr_err("Samples misordered, previous: %" PRIu64
2002 " this: %" PRIu64 "\n", last_timestamp,
2006 last_timestamp = sample->time;
2010 if (machine__resolve(machine, &al, sample) < 0) {
2011 pr_err("problem processing %d event, skipping it.\n",
2012 event->header.type);
2019 if (filter_cpu(sample))
2023 scripting_ops->process_event(event, sample, evsel, &al);
2025 process_event(scr, sample, evsel, &al, machine);
2028 addr_location__put(&al);
2032 static int process_attr(struct perf_tool *tool, union perf_event *event,
2033 struct evlist **pevlist)
2035 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2036 struct evlist *evlist;
2037 struct evsel *evsel, *pos;
2039 static struct evsel_script *es;
2041 err = perf_event__process_attr(tool, event, pevlist);
2046 evsel = perf_evlist__last(*pevlist);
2049 if (scr->per_event_dump) {
2050 evsel->priv = perf_evsel_script__new(evsel,
2051 scr->session->data);
2053 es = zalloc(sizeof(*es));
2061 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2062 evsel->core.attr.type != PERF_TYPE_SYNTH)
2065 evlist__for_each_entry(evlist, pos) {
2066 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
2070 set_print_ip_opts(&evsel->core.attr);
2072 if (evsel->core.attr.sample_type)
2073 err = perf_evsel__check_attr(evsel, scr->session);
2078 static int process_comm_event(struct perf_tool *tool,
2079 union perf_event *event,
2080 struct perf_sample *sample,
2081 struct machine *machine)
2083 struct thread *thread;
2084 struct perf_script *script = container_of(tool, struct perf_script, tool);
2085 struct perf_session *session = script->session;
2086 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2089 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2090 if (thread == NULL) {
2091 pr_debug("problem processing COMM event, skipping it.\n");
2095 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2098 if (!evsel->core.attr.sample_id_all) {
2101 sample->tid = event->comm.tid;
2102 sample->pid = event->comm.pid;
2104 if (!filter_cpu(sample)) {
2105 perf_sample__fprintf_start(sample, thread, evsel,
2106 PERF_RECORD_COMM, stdout);
2107 perf_event__fprintf(event, stdout);
2111 thread__put(thread);
2115 static int process_namespaces_event(struct perf_tool *tool,
2116 union perf_event *event,
2117 struct perf_sample *sample,
2118 struct machine *machine)
2120 struct thread *thread;
2121 struct perf_script *script = container_of(tool, struct perf_script, tool);
2122 struct perf_session *session = script->session;
2123 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2126 thread = machine__findnew_thread(machine, event->namespaces.pid,
2127 event->namespaces.tid);
2128 if (thread == NULL) {
2129 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2133 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2136 if (!evsel->core.attr.sample_id_all) {
2139 sample->tid = event->namespaces.tid;
2140 sample->pid = event->namespaces.pid;
2142 if (!filter_cpu(sample)) {
2143 perf_sample__fprintf_start(sample, thread, evsel,
2144 PERF_RECORD_NAMESPACES, stdout);
2145 perf_event__fprintf(event, stdout);
2149 thread__put(thread);
2153 static int process_fork_event(struct perf_tool *tool,
2154 union perf_event *event,
2155 struct perf_sample *sample,
2156 struct machine *machine)
2158 struct thread *thread;
2159 struct perf_script *script = container_of(tool, struct perf_script, tool);
2160 struct perf_session *session = script->session;
2161 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2163 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2166 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2167 if (thread == NULL) {
2168 pr_debug("problem processing FORK event, skipping it.\n");
2172 if (!evsel->core.attr.sample_id_all) {
2174 sample->time = event->fork.time;
2175 sample->tid = event->fork.tid;
2176 sample->pid = event->fork.pid;
2178 if (!filter_cpu(sample)) {
2179 perf_sample__fprintf_start(sample, thread, evsel,
2180 PERF_RECORD_FORK, stdout);
2181 perf_event__fprintf(event, stdout);
2183 thread__put(thread);
2187 static int process_exit_event(struct perf_tool *tool,
2188 union perf_event *event,
2189 struct perf_sample *sample,
2190 struct machine *machine)
2193 struct thread *thread;
2194 struct perf_script *script = container_of(tool, struct perf_script, tool);
2195 struct perf_session *session = script->session;
2196 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2198 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2199 if (thread == NULL) {
2200 pr_debug("problem processing EXIT event, skipping it.\n");
2204 if (!evsel->core.attr.sample_id_all) {
2207 sample->tid = event->fork.tid;
2208 sample->pid = event->fork.pid;
2210 if (!filter_cpu(sample)) {
2211 perf_sample__fprintf_start(sample, thread, evsel,
2212 PERF_RECORD_EXIT, stdout);
2213 perf_event__fprintf(event, stdout);
2216 if (perf_event__process_exit(tool, event, sample, machine) < 0)
2219 thread__put(thread);
2223 static int process_mmap_event(struct perf_tool *tool,
2224 union perf_event *event,
2225 struct perf_sample *sample,
2226 struct machine *machine)
2228 struct thread *thread;
2229 struct perf_script *script = container_of(tool, struct perf_script, tool);
2230 struct perf_session *session = script->session;
2231 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2233 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2236 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2237 if (thread == NULL) {
2238 pr_debug("problem processing MMAP event, skipping it.\n");
2242 if (!evsel->core.attr.sample_id_all) {
2245 sample->tid = event->mmap.tid;
2246 sample->pid = event->mmap.pid;
2248 if (!filter_cpu(sample)) {
2249 perf_sample__fprintf_start(sample, thread, evsel,
2250 PERF_RECORD_MMAP, stdout);
2251 perf_event__fprintf(event, stdout);
2253 thread__put(thread);
2257 static int process_mmap2_event(struct perf_tool *tool,
2258 union perf_event *event,
2259 struct perf_sample *sample,
2260 struct machine *machine)
2262 struct thread *thread;
2263 struct perf_script *script = container_of(tool, struct perf_script, tool);
2264 struct perf_session *session = script->session;
2265 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2267 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2270 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2271 if (thread == NULL) {
2272 pr_debug("problem processing MMAP2 event, skipping it.\n");
2276 if (!evsel->core.attr.sample_id_all) {
2279 sample->tid = event->mmap2.tid;
2280 sample->pid = event->mmap2.pid;
2282 if (!filter_cpu(sample)) {
2283 perf_sample__fprintf_start(sample, thread, evsel,
2284 PERF_RECORD_MMAP2, stdout);
2285 perf_event__fprintf(event, stdout);
2287 thread__put(thread);
2291 static int process_switch_event(struct perf_tool *tool,
2292 union perf_event *event,
2293 struct perf_sample *sample,
2294 struct machine *machine)
2296 struct thread *thread;
2297 struct perf_script *script = container_of(tool, struct perf_script, tool);
2298 struct perf_session *session = script->session;
2299 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2301 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2304 if (scripting_ops && scripting_ops->process_switch)
2305 scripting_ops->process_switch(event, sample, machine);
2307 if (!script->show_switch_events)
2310 thread = machine__findnew_thread(machine, sample->pid,
2312 if (thread == NULL) {
2313 pr_debug("problem processing SWITCH event, skipping it.\n");
2317 if (!filter_cpu(sample)) {
2318 perf_sample__fprintf_start(sample, thread, evsel,
2319 PERF_RECORD_SWITCH, stdout);
2320 perf_event__fprintf(event, stdout);
2322 thread__put(thread);
2327 process_lost_event(struct perf_tool *tool,
2328 union perf_event *event,
2329 struct perf_sample *sample,
2330 struct machine *machine)
2332 struct perf_script *script = container_of(tool, struct perf_script, tool);
2333 struct perf_session *session = script->session;
2334 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2335 struct thread *thread;
2337 thread = machine__findnew_thread(machine, sample->pid,
2342 if (!filter_cpu(sample)) {
2343 perf_sample__fprintf_start(sample, thread, evsel,
2344 PERF_RECORD_LOST, stdout);
2345 perf_event__fprintf(event, stdout);
2347 thread__put(thread);
2352 process_finished_round_event(struct perf_tool *tool __maybe_unused,
2353 union perf_event *event,
2354 struct ordered_events *oe __maybe_unused)
2357 perf_event__fprintf(event, stdout);
2362 process_bpf_events(struct perf_tool *tool __maybe_unused,
2363 union perf_event *event,
2364 struct perf_sample *sample,
2365 struct machine *machine)
2367 struct thread *thread;
2368 struct perf_script *script = container_of(tool, struct perf_script, tool);
2369 struct perf_session *session = script->session;
2370 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2372 if (machine__process_ksymbol(machine, event, sample) < 0)
2375 if (!evsel->core.attr.sample_id_all) {
2376 perf_event__fprintf(event, stdout);
2380 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2381 if (thread == NULL) {
2382 pr_debug("problem processing MMAP event, skipping it.\n");
2386 if (!filter_cpu(sample)) {
2387 perf_sample__fprintf_start(sample, thread, evsel,
2388 event->header.type, stdout);
2389 perf_event__fprintf(event, stdout);
2392 thread__put(thread);
2396 static void sig_handler(int sig __maybe_unused)
2401 static void perf_script__fclose_per_event_dump(struct perf_script *script)
2403 struct evlist *evlist = script->session->evlist;
2404 struct evsel *evsel;
2406 evlist__for_each_entry(evlist, evsel) {
2409 perf_evsel_script__delete(evsel->priv);
2414 static int perf_script__fopen_per_event_dump(struct perf_script *script)
2416 struct evsel *evsel;
2418 evlist__for_each_entry(script->session->evlist, evsel) {
2420 * Already setup? I.e. we may be called twice in cases like
2421 * Intel PT, one for the intel_pt// and dummy events, then
2422 * for the evsels syntheized from the auxtrace info.
2424 * Ses perf_script__process_auxtrace_info.
2426 if (evsel->priv != NULL)
2429 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
2430 if (evsel->priv == NULL)
2431 goto out_err_fclose;
2437 perf_script__fclose_per_event_dump(script);
2441 static int perf_script__setup_per_event_dump(struct perf_script *script)
2443 struct evsel *evsel;
2444 static struct evsel_script es_stdout;
2446 if (script->per_event_dump)
2447 return perf_script__fopen_per_event_dump(script);
2449 es_stdout.fp = stdout;
2451 evlist__for_each_entry(script->session->evlist, evsel)
2452 evsel->priv = &es_stdout;
2457 static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2459 struct evsel *evsel;
2461 evlist__for_each_entry(script->session->evlist, evsel) {
2462 struct evsel_script *es = evsel->priv;
2464 perf_evsel_script__fprintf(es, stdout);
2465 perf_evsel_script__delete(es);
2470 static int __cmd_script(struct perf_script *script)
2474 signal(SIGINT, sig_handler);
2476 perf_stat__init_shadow_stats();
2478 /* override event processing functions */
2479 if (script->show_task_events) {
2480 script->tool.comm = process_comm_event;
2481 script->tool.fork = process_fork_event;
2482 script->tool.exit = process_exit_event;
2484 if (script->show_mmap_events) {
2485 script->tool.mmap = process_mmap_event;
2486 script->tool.mmap2 = process_mmap2_event;
2488 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
2489 script->tool.context_switch = process_switch_event;
2490 if (script->show_namespace_events)
2491 script->tool.namespaces = process_namespaces_event;
2492 if (script->show_lost_events)
2493 script->tool.lost = process_lost_event;
2494 if (script->show_round_events) {
2495 script->tool.ordered_events = false;
2496 script->tool.finished_round = process_finished_round_event;
2498 if (script->show_bpf_events) {
2499 script->tool.ksymbol = process_bpf_events;
2500 script->tool.bpf = process_bpf_events;
2503 if (perf_script__setup_per_event_dump(script)) {
2504 pr_err("Couldn't create the per event dump files\n");
2508 ret = perf_session__process_events(script->session);
2510 if (script->per_event_dump)
2511 perf_script__exit_per_event_dump_stats(script);
2514 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
2519 struct script_spec {
2520 struct list_head node;
2521 struct scripting_ops *ops;
2525 static LIST_HEAD(script_specs);
2527 static struct script_spec *script_spec__new(const char *spec,
2528 struct scripting_ops *ops)
2530 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2533 strcpy(s->spec, spec);
2540 static void script_spec__add(struct script_spec *s)
2542 list_add_tail(&s->node, &script_specs);
2545 static struct script_spec *script_spec__find(const char *spec)
2547 struct script_spec *s;
2549 list_for_each_entry(s, &script_specs, node)
2550 if (strcasecmp(s->spec, spec) == 0)
2555 int script_spec_register(const char *spec, struct scripting_ops *ops)
2557 struct script_spec *s;
2559 s = script_spec__find(spec);
2563 s = script_spec__new(spec, ops);
2567 script_spec__add(s);
2572 static struct scripting_ops *script_spec__lookup(const char *spec)
2574 struct script_spec *s = script_spec__find(spec);
2581 static void list_available_languages(void)
2583 struct script_spec *s;
2585 fprintf(stderr, "\n");
2586 fprintf(stderr, "Scripting language extensions (used in "
2587 "perf script -s [spec:]script.[spec]):\n\n");
2589 list_for_each_entry(s, &script_specs, node)
2590 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2592 fprintf(stderr, "\n");
2595 static int parse_scriptname(const struct option *opt __maybe_unused,
2596 const char *str, int unset __maybe_unused)
2598 char spec[PATH_MAX];
2599 const char *script, *ext;
2602 if (strcmp(str, "lang") == 0) {
2603 list_available_languages();
2607 script = strchr(str, ':');
2610 if (len >= PATH_MAX) {
2611 fprintf(stderr, "invalid language specifier");
2614 strncpy(spec, str, len);
2616 scripting_ops = script_spec__lookup(spec);
2617 if (!scripting_ops) {
2618 fprintf(stderr, "invalid language specifier");
2624 ext = strrchr(script, '.');
2626 fprintf(stderr, "invalid script extension");
2629 scripting_ops = script_spec__lookup(++ext);
2630 if (!scripting_ops) {
2631 fprintf(stderr, "invalid script extension");
2636 script_name = strdup(script);
2641 static int parse_output_fields(const struct option *opt __maybe_unused,
2642 const char *arg, int unset __maybe_unused)
2644 char *tok, *strtok_saveptr = NULL;
2645 int i, imax = ARRAY_SIZE(all_output_options);
2648 char *str = strdup(arg);
2650 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
2655 /* first word can state for which event type the user is specifying
2656 * the fields. If no type exists, the specified fields apply to all
2657 * event types found in the file minus the invalid fields for a type.
2659 tok = strchr(str, ':');
2663 if (!strcmp(str, "hw"))
2664 type = PERF_TYPE_HARDWARE;
2665 else if (!strcmp(str, "sw"))
2666 type = PERF_TYPE_SOFTWARE;
2667 else if (!strcmp(str, "trace"))
2668 type = PERF_TYPE_TRACEPOINT;
2669 else if (!strcmp(str, "raw"))
2670 type = PERF_TYPE_RAW;
2671 else if (!strcmp(str, "break"))
2672 type = PERF_TYPE_BREAKPOINT;
2673 else if (!strcmp(str, "synth"))
2674 type = OUTPUT_TYPE_SYNTH;
2676 fprintf(stderr, "Invalid event type in field string.\n");
2681 if (output[type].user_set)
2682 pr_warning("Overriding previous field request for %s events.\n",
2685 /* Don't override defaults for +- */
2686 if (strchr(tok, '+') || strchr(tok, '-'))
2689 output[type].fields = 0;
2690 output[type].user_set = true;
2691 output[type].wildcard_set = false;
2695 if (strlen(str) == 0) {
2697 "Cannot set fields to 'none' for all event types.\n");
2702 /* Don't override defaults for +- */
2703 if (strchr(str, '+') || strchr(str, '-'))
2706 if (output_set_by_user())
2707 pr_warning("Overriding previous field request for all events.\n");
2709 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2710 output[j].fields = 0;
2711 output[j].user_set = true;
2712 output[j].wildcard_set = true;
2717 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
2723 } else if (*tok == '-') {
2729 if (change != SET && change != DEFAULT)
2734 for (i = 0; i < imax; ++i) {
2735 if (strcmp(tok, all_output_options[i].str) == 0)
2738 if (i == imax && strcmp(tok, "flags") == 0) {
2739 print_flags = change == REMOVE ? false : true;
2743 fprintf(stderr, "Invalid field requested.\n");
2749 /* add user option to all events types for
2752 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2753 if (output[j].invalid_fields & all_output_options[i].field) {
2754 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2755 all_output_options[i].str, event_type(j));
2757 if (change == REMOVE) {
2758 output[j].fields &= ~all_output_options[i].field;
2759 output[j].user_set_fields &= ~all_output_options[i].field;
2761 output[j].fields |= all_output_options[i].field;
2762 output[j].user_set_fields |= all_output_options[i].field;
2764 output[j].user_set = true;
2765 output[j].wildcard_set = true;
2769 if (output[type].invalid_fields & all_output_options[i].field) {
2770 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2771 all_output_options[i].str, event_type(type));
2776 if (change == REMOVE)
2777 output[type].fields &= ~all_output_options[i].field;
2779 output[type].fields |= all_output_options[i].field;
2780 output[type].user_set = true;
2781 output[type].wildcard_set = true;
2786 if (output[type].fields == 0) {
2787 pr_debug("No fields requested for %s type. "
2788 "Events will not be displayed.\n", event_type(type));
2794 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2801 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2802 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2803 if ((lang_dirent->d_type == DT_DIR || \
2804 (lang_dirent->d_type == DT_UNKNOWN && \
2805 is_directory(scripts_path, lang_dirent))) && \
2806 (strcmp(lang_dirent->d_name, ".")) && \
2807 (strcmp(lang_dirent->d_name, "..")))
2809 #define for_each_script(lang_path, lang_dir, script_dirent) \
2810 while ((script_dirent = readdir(lang_dir)) != NULL) \
2811 if (script_dirent->d_type != DT_DIR && \
2812 (script_dirent->d_type != DT_UNKNOWN || \
2813 !is_directory(lang_path, script_dirent)))
2816 #define RECORD_SUFFIX "-record"
2817 #define REPORT_SUFFIX "-report"
2819 struct script_desc {
2820 struct list_head node;
2826 static LIST_HEAD(script_descs);
2828 static struct script_desc *script_desc__new(const char *name)
2830 struct script_desc *s = zalloc(sizeof(*s));
2832 if (s != NULL && name)
2833 s->name = strdup(name);
2838 static void script_desc__delete(struct script_desc *s)
2841 zfree(&s->half_liner);
2846 static void script_desc__add(struct script_desc *s)
2848 list_add_tail(&s->node, &script_descs);
2851 static struct script_desc *script_desc__find(const char *name)
2853 struct script_desc *s;
2855 list_for_each_entry(s, &script_descs, node)
2856 if (strcasecmp(s->name, name) == 0)
2861 static struct script_desc *script_desc__findnew(const char *name)
2863 struct script_desc *s = script_desc__find(name);
2868 s = script_desc__new(name);
2872 script_desc__add(s);
2877 static const char *ends_with(const char *str, const char *suffix)
2879 size_t suffix_len = strlen(suffix);
2880 const char *p = str;
2882 if (strlen(str) > suffix_len) {
2883 p = str + strlen(str) - suffix_len;
2884 if (!strncmp(p, suffix, suffix_len))
2891 static int read_script_info(struct script_desc *desc, const char *filename)
2893 char line[BUFSIZ], *p;
2896 fp = fopen(filename, "r");
2900 while (fgets(line, sizeof(line), fp)) {
2901 p = skip_spaces(line);
2907 if (strlen(p) && *p == '!')
2911 if (strlen(p) && p[strlen(p) - 1] == '\n')
2912 p[strlen(p) - 1] = '\0';
2914 if (!strncmp(p, "description:", strlen("description:"))) {
2915 p += strlen("description:");
2916 desc->half_liner = strdup(skip_spaces(p));
2920 if (!strncmp(p, "args:", strlen("args:"))) {
2921 p += strlen("args:");
2922 desc->args = strdup(skip_spaces(p));
2932 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2934 char *script_root, *str;
2936 script_root = strdup(script_dirent->d_name);
2940 str = (char *)ends_with(script_root, suffix);
2950 static int list_available_scripts(const struct option *opt __maybe_unused,
2951 const char *s __maybe_unused,
2952 int unset __maybe_unused)
2954 struct dirent *script_dirent, *lang_dirent;
2955 char scripts_path[MAXPATHLEN];
2956 DIR *scripts_dir, *lang_dir;
2957 char script_path[MAXPATHLEN];
2958 char lang_path[MAXPATHLEN];
2959 struct script_desc *desc;
2960 char first_half[BUFSIZ];
2963 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2965 scripts_dir = opendir(scripts_path);
2968 "open(%s) failed.\n"
2969 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2974 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2975 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2976 lang_dirent->d_name);
2977 lang_dir = opendir(lang_path);
2981 for_each_script(lang_path, lang_dir, script_dirent) {
2982 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
2984 desc = script_desc__findnew(script_root);
2985 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2986 lang_path, script_dirent->d_name);
2987 read_script_info(desc, script_path);
2993 fprintf(stdout, "List of available trace scripts:\n");
2994 list_for_each_entry(desc, &script_descs, node) {
2995 sprintf(first_half, "%s %s", desc->name,
2996 desc->args ? desc->args : "");
2997 fprintf(stdout, " %-36s %s\n", first_half,
2998 desc->half_liner ? desc->half_liner : "");
3005 * Some scripts specify the required events in their "xxx-record" file,
3006 * this function will check if the events in perf.data match those
3007 * mentioned in the "xxx-record".
3009 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3010 * which is covered well now. And new parsing code should be added to
3011 * cover the future complexing formats like event groups etc.
3013 static int check_ev_match(char *dir_name, char *scriptname,
3014 struct perf_session *session)
3016 char filename[MAXPATHLEN], evname[128];
3017 char line[BUFSIZ], *p;
3022 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
3024 fp = fopen(filename, "r");
3028 while (fgets(line, sizeof(line), fp)) {
3029 p = skip_spaces(line);
3034 p = strstr(p, "-e");
3040 len = strcspn(p, " \t");
3044 snprintf(evname, len + 1, "%s", p);
3047 evlist__for_each_entry(session->evlist, pos) {
3048 if (!strcmp(perf_evsel__name(pos), evname)) {
3066 * Return -1 if none is found, otherwise the actual scripts number.
3068 * Currently the only user of this function is the script browser, which
3069 * will list all statically runnable scripts, select one, execute it and
3070 * show the output in a perf browser.
3072 int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3075 struct dirent *script_dirent, *lang_dirent;
3076 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
3077 DIR *scripts_dir, *lang_dir;
3078 struct perf_session *session;
3079 struct perf_data data = {
3081 .mode = PERF_DATA_MODE_READ,
3086 session = perf_session__new(&data, false, NULL);
3087 if (IS_ERR(session))
3088 return PTR_ERR(session);
3090 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3092 scripts_dir = opendir(scripts_path);
3094 perf_session__delete(session);
3098 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3099 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3100 lang_dirent->d_name);
3101 #ifndef HAVE_LIBPERL_SUPPORT
3102 if (strstr(lang_path, "perl"))
3105 #ifndef HAVE_LIBPYTHON_SUPPORT
3106 if (strstr(lang_path, "python"))
3110 lang_dir = opendir(lang_path);
3114 for_each_script(lang_path, lang_dir, script_dirent) {
3115 /* Skip those real time scripts: xxxtop.p[yl] */
3116 if (strstr(script_dirent->d_name, "top."))
3120 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3122 script_dirent->d_name);
3123 temp = strchr(script_dirent->d_name, '.');
3124 snprintf(scripts_array[i],
3125 (temp - script_dirent->d_name) + 1,
3126 "%s", script_dirent->d_name);
3128 if (check_ev_match(lang_path,
3129 scripts_array[i], session))
3137 closedir(scripts_dir);
3138 perf_session__delete(session);
3142 static char *get_script_path(const char *script_root, const char *suffix)
3144 struct dirent *script_dirent, *lang_dirent;
3145 char scripts_path[MAXPATHLEN];
3146 char script_path[MAXPATHLEN];
3147 DIR *scripts_dir, *lang_dir;
3148 char lang_path[MAXPATHLEN];
3149 char *__script_root;
3151 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3153 scripts_dir = opendir(scripts_path);
3157 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3158 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3159 lang_dirent->d_name);
3160 lang_dir = opendir(lang_path);
3164 for_each_script(lang_path, lang_dir, script_dirent) {
3165 __script_root = get_script_root(script_dirent, suffix);
3166 if (__script_root && !strcmp(script_root, __script_root)) {
3167 free(__script_root);
3169 closedir(scripts_dir);
3170 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3171 lang_path, script_dirent->d_name);
3172 return strdup(script_path);
3174 free(__script_root);
3178 closedir(scripts_dir);
3183 static bool is_top_script(const char *script_path)
3185 return ends_with(script_path, "top") == NULL ? false : true;
3188 static int has_required_arg(char *script_path)
3190 struct script_desc *desc;
3194 desc = script_desc__new(NULL);
3196 if (read_script_info(desc, script_path))
3202 for (p = desc->args; *p; p++)
3206 script_desc__delete(desc);
3211 static int have_cmd(int argc, const char **argv)
3213 char **__argv = malloc(sizeof(const char *) * argc);
3216 pr_err("malloc failed\n");
3220 memcpy(__argv, argv, sizeof(const char *) * argc);
3221 argc = parse_options(argc, (const char **)__argv, record_options,
3222 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3225 system_wide = (argc == 0);
3230 static void script__setup_sample_type(struct perf_script *script)
3232 struct perf_session *session = script->session;
3233 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3235 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3236 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
3237 (sample_type & PERF_SAMPLE_STACK_USER)) {
3238 callchain_param.record_mode = CALLCHAIN_DWARF;
3239 dwarf_callchain_users = true;
3240 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
3241 callchain_param.record_mode = CALLCHAIN_LBR;
3243 callchain_param.record_mode = CALLCHAIN_FP;
3247 static int process_stat_round_event(struct perf_session *session,
3248 union perf_event *event)
3250 struct perf_record_stat_round *round = &event->stat_round;
3251 struct evsel *counter;
3253 evlist__for_each_entry(session->evlist, counter) {
3254 perf_stat_process_counter(&stat_config, counter);
3255 process_stat(counter, round->time);
3258 process_stat_interval(round->time);
3262 static int process_stat_config_event(struct perf_session *session __maybe_unused,
3263 union perf_event *event)
3265 perf_event__read_stat_config(&stat_config, &event->stat_config);
3269 static int set_maps(struct perf_script *script)
3271 struct evlist *evlist = script->session->evlist;
3273 if (!script->cpus || !script->threads)
3276 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3279 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
3281 if (perf_evlist__alloc_stats(evlist, true))
3284 script->allocated = true;
3289 int process_thread_map_event(struct perf_session *session,
3290 union perf_event *event)
3292 struct perf_tool *tool = session->tool;
3293 struct perf_script *script = container_of(tool, struct perf_script, tool);
3295 if (script->threads) {
3296 pr_warning("Extra thread map event, ignoring.\n");
3300 script->threads = thread_map__new_event(&event->thread_map);
3301 if (!script->threads)
3304 return set_maps(script);
3308 int process_cpu_map_event(struct perf_session *session,
3309 union perf_event *event)
3311 struct perf_tool *tool = session->tool;
3312 struct perf_script *script = container_of(tool, struct perf_script, tool);
3315 pr_warning("Extra cpu map event, ignoring.\n");
3319 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3323 return set_maps(script);
3326 static int process_feature_event(struct perf_session *session,
3327 union perf_event *event)
3329 if (event->feat.feat_id < HEADER_LAST_FEATURE)
3330 return perf_event__process_feature(session, event);
3334 #ifdef HAVE_AUXTRACE_SUPPORT
3335 static int perf_script__process_auxtrace_info(struct perf_session *session,
3336 union perf_event *event)
3338 struct perf_tool *tool = session->tool;
3340 int ret = perf_event__process_auxtrace_info(session, event);
3343 struct perf_script *script = container_of(tool, struct perf_script, tool);
3345 ret = perf_script__setup_per_event_dump(script);
3351 #define perf_script__process_auxtrace_info 0
3354 static int parse_insn_trace(const struct option *opt __maybe_unused,
3355 const char *str __maybe_unused,
3356 int unset __maybe_unused)
3358 parse_output_fields(NULL, "+insn,-event,-period", 0);
3359 itrace_parse_synth_opts(opt, "i0ns", 0);
3360 symbol_conf.nanosecs = true;
3364 static int parse_xed(const struct option *opt __maybe_unused,
3365 const char *str __maybe_unused,
3366 int unset __maybe_unused)
3368 force_pager("xed -F insn: -A -64 | less");
3372 static int parse_call_trace(const struct option *opt __maybe_unused,
3373 const char *str __maybe_unused,
3374 int unset __maybe_unused)
3376 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3377 itrace_parse_synth_opts(opt, "cewp", 0);
3378 symbol_conf.nanosecs = true;
3379 symbol_conf.pad_output_len_dso = 50;
3383 static int parse_callret_trace(const struct option *opt __maybe_unused,
3384 const char *str __maybe_unused,
3385 int unset __maybe_unused)
3387 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3388 itrace_parse_synth_opts(opt, "crewp", 0);
3389 symbol_conf.nanosecs = true;
3393 int cmd_script(int argc, const char **argv)
3395 bool show_full_info = false;
3396 bool header = false;
3397 bool header_only = false;
3398 bool script_started = false;
3399 char *rec_script_path = NULL;
3400 char *rep_script_path = NULL;
3401 struct perf_session *session;
3402 struct itrace_synth_opts itrace_synth_opts = {
3404 .default_no_sample = true,
3407 char *script_path = NULL;
3408 const char **__argv;
3410 struct perf_script script = {
3412 .sample = process_sample_event,
3413 .mmap = perf_event__process_mmap,
3414 .mmap2 = perf_event__process_mmap2,
3415 .comm = perf_event__process_comm,
3416 .namespaces = perf_event__process_namespaces,
3417 .exit = perf_event__process_exit,
3418 .fork = perf_event__process_fork,
3419 .attr = process_attr,
3420 .event_update = perf_event__process_event_update,
3421 .tracing_data = perf_event__process_tracing_data,
3422 .feature = process_feature_event,
3423 .build_id = perf_event__process_build_id,
3424 .id_index = perf_event__process_id_index,
3425 .auxtrace_info = perf_script__process_auxtrace_info,
3426 .auxtrace = perf_event__process_auxtrace,
3427 .auxtrace_error = perf_event__process_auxtrace_error,
3428 .stat = perf_event__process_stat_event,
3429 .stat_round = process_stat_round_event,
3430 .stat_config = process_stat_config_event,
3431 .thread_map = process_thread_map_event,
3432 .cpu_map = process_cpu_map_event,
3433 .ordered_events = true,
3434 .ordering_requires_timestamps = true,
3437 struct perf_data data = {
3438 .mode = PERF_DATA_MODE_READ,
3440 const struct option options[] = {
3441 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3442 "dump raw trace in ASCII"),
3443 OPT_INCR('v', "verbose", &verbose,
3444 "be more verbose (show symbol address, etc)"),
3445 OPT_BOOLEAN('L', "Latency", &latency_format,
3446 "show latency attributes (irqs/preemption disabled, etc)"),
3447 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3448 list_available_scripts),
3449 OPT_CALLBACK('s', "script", NULL, "name",
3450 "script file name (lang:script name, script name, or *)",
3452 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3453 "generate perf-script.xx script in specified language"),
3454 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3455 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3456 "do various checks like samples ordering and lost events"),
3457 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3458 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
3459 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3460 "file", "vmlinux pathname"),
3461 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3462 "file", "kallsyms pathname"),
3463 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3464 "When printing symbols do not display call chain"),
3465 OPT_CALLBACK(0, "symfs", NULL, "directory",
3466 "Look for files with symbols relative to this directory",
3467 symbol__config_symfs),
3468 OPT_CALLBACK('F', "fields", NULL, "str",
3469 "comma separated output fields prepend with 'type:'. "
3470 "+field to add and -field to remove."
3471 "Valid types: hw,sw,trace,raw,synth. "
3472 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
3473 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3474 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3475 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
3476 parse_output_fields),
3477 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3478 "system-wide collection from all CPUs"),
3479 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3480 "only consider these symbols"),
3481 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3482 "Decode instructions from itrace", parse_insn_trace),
3483 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3484 "Run xed disassembler on output", parse_xed),
3485 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3486 "Decode calls from from itrace", parse_call_trace),
3487 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3488 "Decode calls and returns from itrace", parse_callret_trace),
3489 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3490 "Only print symbols and callees with --call-trace/--call-ret-trace"),
3491 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3492 "Stop display of callgraph at these symbols"),
3493 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3494 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3495 "only display events for these comms"),
3496 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3497 "only consider symbols in these pids"),
3498 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3499 "only consider symbols in these tids"),
3500 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3501 "Set the maximum stack depth when parsing the callchain, "
3502 "anything beyond the specified depth will be ignored. "
3503 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3504 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
3505 OPT_BOOLEAN('I', "show-info", &show_full_info,
3506 "display extended information from perf.data file"),
3507 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3508 "Show the path of [kernel.kallsyms]"),
3509 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3510 "Show the fork/comm/exit events"),
3511 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3512 "Show the mmap events"),
3513 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3514 "Show context switch events (if recorded)"),
3515 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3516 "Show namespace events (if recorded)"),
3517 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3518 "Show lost events (if recorded)"),
3519 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3520 "Show round events (if recorded)"),
3521 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3522 "Show bpf related events (if recorded)"),
3523 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3524 "Dump trace output to files named by the monitored events"),
3525 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
3526 OPT_INTEGER(0, "max-blocks", &max_blocks,
3527 "Maximum number of code blocks to dump with brstackinsn"),
3528 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
3529 "Use 9 decimal places when displaying time"),
3530 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3531 "Instruction Tracing options\n" ITRACE_HELP,
3532 itrace_parse_synth_opts),
3533 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3534 "Show full source file name path for source lines"),
3535 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3536 "Enable symbol demangling"),
3537 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3538 "Enable kernel symbol demangling"),
3539 OPT_STRING(0, "time", &script.time_str, "str",
3540 "Time span of interest (start,stop)"),
3541 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3542 "Show inline function"),
3543 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3544 "guest mount directory under which every guest os"
3545 " instance has a subdir"),
3546 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3547 "file", "file saving guest os vmlinux"),
3548 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3549 "file", "file saving guest os /proc/kallsyms"),
3550 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3551 "file", "file saving guest os /proc/modules"),
3552 OPTS_EVSWITCH(&script.evswitch),
3555 const char * const script_subcommands[] = { "record", "report", NULL };
3556 const char *script_usage[] = {
3557 "perf script [<options>]",
3558 "perf script [<options>] record <script> [<record-options>] <command>",
3559 "perf script [<options>] report <script> [script-args]",
3560 "perf script [<options>] <script> [<record-options>] <command>",
3561 "perf script [<options>] <top-script> [script-args]",
3565 perf_set_singlethreaded();
3569 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
3570 PARSE_OPT_STOP_AT_NON_OPTION);
3572 if (symbol_conf.guestmount ||
3573 symbol_conf.default_guest_vmlinux_name ||
3574 symbol_conf.default_guest_kallsyms ||
3575 symbol_conf.default_guest_modules) {
3577 * Enable guest sample processing.
3582 data.path = input_name;
3583 data.force = symbol_conf.force;
3585 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3586 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3587 if (!rec_script_path)
3588 return cmd_record(argc, argv);
3591 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3592 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3593 if (!rep_script_path) {
3595 "Please specify a valid report script"
3596 "(see 'perf script -l' for listing)\n");
3601 if (script.time_str && reltime) {
3602 fprintf(stderr, "Don't combine --reltime with --time\n");
3606 if (itrace_synth_opts.callchain &&
3607 itrace_synth_opts.callchain_sz > scripting_max_stack)
3608 scripting_max_stack = itrace_synth_opts.callchain_sz;
3610 /* make sure PERF_EXEC_PATH is set for scripts */
3611 set_argv_exec_path(get_argv_exec_path());
3613 if (argc && !script_name && !rec_script_path && !rep_script_path) {
3618 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3619 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3621 if (!rec_script_path && !rep_script_path) {
3622 usage_with_options_msg(script_usage, options,
3623 "Couldn't find script `%s'\n\n See perf"
3624 " script -l for available scripts.\n", argv[0]);
3627 if (is_top_script(argv[0])) {
3628 rep_args = argc - 1;
3632 rep_args = has_required_arg(rep_script_path);
3633 rec_args = (argc - 1) - rep_args;
3635 usage_with_options_msg(script_usage, options,
3636 "`%s' script requires options."
3637 "\n\n See perf script -l for available "
3638 "scripts and options.\n", argv[0]);
3642 if (pipe(live_pipe) < 0) {
3643 perror("failed to create pipe");
3649 perror("failed to fork");
3656 dup2(live_pipe[1], 1);
3657 close(live_pipe[0]);
3659 if (is_top_script(argv[0])) {
3661 } else if (!system_wide) {
3662 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3668 __argv = malloc((argc + 6) * sizeof(const char *));
3670 pr_err("malloc failed\n");
3675 __argv[j++] = "/bin/sh";
3676 __argv[j++] = rec_script_path;
3682 for (i = rep_args + 1; i < argc; i++)
3683 __argv[j++] = argv[i];
3686 execvp("/bin/sh", (char **)__argv);
3691 dup2(live_pipe[0], 0);
3692 close(live_pipe[1]);
3694 __argv = malloc((argc + 4) * sizeof(const char *));
3696 pr_err("malloc failed\n");
3702 __argv[j++] = "/bin/sh";
3703 __argv[j++] = rep_script_path;
3704 for (i = 1; i < rep_args + 1; i++)
3705 __argv[j++] = argv[i];
3710 execvp("/bin/sh", (char **)__argv);
3715 if (rec_script_path)
3716 script_path = rec_script_path;
3717 if (rep_script_path)
3718 script_path = rep_script_path;
3723 if (!rec_script_path)
3724 system_wide = false;
3725 else if (!system_wide) {
3726 if (have_cmd(argc - 1, &argv[1]) != 0) {
3732 __argv = malloc((argc + 2) * sizeof(const char *));
3734 pr_err("malloc failed\n");
3739 __argv[j++] = "/bin/sh";
3740 __argv[j++] = script_path;
3743 for (i = 2; i < argc; i++)
3744 __argv[j++] = argv[i];
3747 execvp("/bin/sh", (char **)__argv);
3757 session = perf_session__new(&data, false, &script.tool);
3758 if (IS_ERR(session))
3759 return PTR_ERR(session);
3761 if (header || header_only) {
3762 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
3763 perf_session__fprintf_info(session, stdout, show_full_info);
3768 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
3770 if (symbol__init(&session->header.env) < 0)
3774 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3775 !strcmp(uts.machine, session->header.env.arch) ||
3776 (!strcmp(uts.machine, "x86_64") &&
3777 !strcmp(session->header.env.arch, "i386")))
3780 script.session = session;
3781 script__setup_sample_type(&script);
3783 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3784 symbol_conf.graph_function)
3785 itrace_synth_opts.thread_stack = true;
3787 session->itrace_synth_opts = &itrace_synth_opts;
3790 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3793 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
3797 symbol_conf.use_callchain = true;
3799 symbol_conf.use_callchain = false;
3801 if (session->tevent.pevent &&
3802 tep_set_function_resolver(session->tevent.pevent,
3803 machine__resolve_kernel_addr,
3804 &session->machines.host) < 0) {
3805 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3810 if (generate_script_lang) {
3811 struct stat perf_stat;
3814 if (output_set_by_user()) {
3816 "custom fields not supported for generated scripts");
3821 input = open(data.path, O_RDONLY); /* input_name */
3824 perror("failed to open file");
3828 err = fstat(input, &perf_stat);
3830 perror("failed to stat file");
3834 if (!perf_stat.st_size) {
3835 fprintf(stderr, "zero-sized file, nothing to do!\n");
3839 scripting_ops = script_spec__lookup(generate_script_lang);
3840 if (!scripting_ops) {
3841 fprintf(stderr, "invalid language specifier");
3846 err = scripting_ops->generate_script(session->tevent.pevent,
3852 err = scripting_ops->start_script(script_name, argc, argv);
3855 pr_debug("perf script started with script %s\n\n", script_name);
3856 script_started = true;
3860 err = perf_session__check_output_opt(session);
3864 if (script.time_str) {
3865 err = perf_time__parse_for_ranges(script.time_str, session,
3866 &script.ptime_range,
3872 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3877 err = evswitch__init(&script.evswitch, session->evlist, stderr);
3881 err = __cmd_script(&script);
3886 if (script.ptime_range) {
3887 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
3888 zfree(&script.ptime_range);
3891 perf_evlist__free_stats(session->evlist);
3892 perf_session__delete(session);
3895 cleanup_scripting();