1 // SPDX-License-Identifier: GPL-2.0
5 #include "util/cache.h"
6 #include "util/debug.h"
7 #include <subcmd/exec-cmd.h>
8 #include "util/header.h"
9 #include <subcmd/parse-options.h>
10 #include "util/perf_regs.h"
11 #include "util/session.h"
12 #include "util/tool.h"
13 #include "util/symbol.h"
14 #include "util/thread.h"
15 #include "util/trace-event.h"
16 #include "util/util.h"
17 #include "util/evlist.h"
18 #include "util/evsel.h"
19 #include "util/sort.h"
20 #include "util/data.h"
21 #include "util/auxtrace.h"
22 #include "util/cpumap.h"
23 #include "util/thread_map.h"
24 #include "util/stat.h"
25 #include "util/color.h"
26 #include "util/string2.h"
27 #include "util/thread-stack.h"
28 #include "util/time-utils.h"
29 #include "util/path.h"
30 #include "print_binary.h"
31 #include <linux/bitmap.h>
32 #include <linux/kernel.h>
33 #include <linux/stringify.h>
34 #include <linux/time64.h>
36 #include "util/mem-events.h"
37 #include "util/dump-insn.h"
42 #include <sys/param.h>
43 #include <sys/types.h>
48 #include "sane_ctype.h"
50 static char const *script_name;
51 static char const *generate_script_lang;
52 static bool debug_mode;
53 static u64 last_timestamp;
54 static u64 nr_unordered;
55 static bool no_callchain;
56 static bool latency_format;
57 static bool system_wide;
58 static bool print_flags;
60 static const char *cpu_list;
61 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
62 static struct perf_stat_config stat_config;
63 static int max_blocks;
65 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
67 enum perf_output_field {
68 PERF_OUTPUT_COMM = 1U << 0,
69 PERF_OUTPUT_TID = 1U << 1,
70 PERF_OUTPUT_PID = 1U << 2,
71 PERF_OUTPUT_TIME = 1U << 3,
72 PERF_OUTPUT_CPU = 1U << 4,
73 PERF_OUTPUT_EVNAME = 1U << 5,
74 PERF_OUTPUT_TRACE = 1U << 6,
75 PERF_OUTPUT_IP = 1U << 7,
76 PERF_OUTPUT_SYM = 1U << 8,
77 PERF_OUTPUT_DSO = 1U << 9,
78 PERF_OUTPUT_ADDR = 1U << 10,
79 PERF_OUTPUT_SYMOFFSET = 1U << 11,
80 PERF_OUTPUT_SRCLINE = 1U << 12,
81 PERF_OUTPUT_PERIOD = 1U << 13,
82 PERF_OUTPUT_IREGS = 1U << 14,
83 PERF_OUTPUT_BRSTACK = 1U << 15,
84 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
85 PERF_OUTPUT_DATA_SRC = 1U << 17,
86 PERF_OUTPUT_WEIGHT = 1U << 18,
87 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
88 PERF_OUTPUT_CALLINDENT = 1U << 20,
89 PERF_OUTPUT_INSN = 1U << 21,
90 PERF_OUTPUT_INSNLEN = 1U << 22,
91 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
92 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
93 PERF_OUTPUT_SYNTH = 1U << 25,
94 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
95 PERF_OUTPUT_UREGS = 1U << 27,
96 PERF_OUTPUT_METRIC = 1U << 28,
97 PERF_OUTPUT_MISC = 1U << 29,
100 struct output_option {
102 enum perf_output_field field;
103 } all_output_options[] = {
104 {.str = "comm", .field = PERF_OUTPUT_COMM},
105 {.str = "tid", .field = PERF_OUTPUT_TID},
106 {.str = "pid", .field = PERF_OUTPUT_PID},
107 {.str = "time", .field = PERF_OUTPUT_TIME},
108 {.str = "cpu", .field = PERF_OUTPUT_CPU},
109 {.str = "event", .field = PERF_OUTPUT_EVNAME},
110 {.str = "trace", .field = PERF_OUTPUT_TRACE},
111 {.str = "ip", .field = PERF_OUTPUT_IP},
112 {.str = "sym", .field = PERF_OUTPUT_SYM},
113 {.str = "dso", .field = PERF_OUTPUT_DSO},
114 {.str = "addr", .field = PERF_OUTPUT_ADDR},
115 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
116 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
117 {.str = "period", .field = PERF_OUTPUT_PERIOD},
118 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
119 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
120 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
121 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
122 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
123 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
124 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
125 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
126 {.str = "insn", .field = PERF_OUTPUT_INSN},
127 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
128 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
129 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
130 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
131 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
132 {.str = "metric", .field = PERF_OUTPUT_METRIC},
133 {.str = "misc", .field = PERF_OUTPUT_MISC},
137 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
141 /* default set to maintain compatibility with current format */
145 unsigned int print_ip_opts;
148 } output[OUTPUT_TYPE_MAX] = {
150 [PERF_TYPE_HARDWARE] = {
153 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
154 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
155 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
156 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
157 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
159 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
162 [PERF_TYPE_SOFTWARE] = {
165 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
166 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
167 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
168 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
169 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
170 PERF_OUTPUT_BPF_OUTPUT,
172 .invalid_fields = PERF_OUTPUT_TRACE,
175 [PERF_TYPE_TRACEPOINT] = {
178 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
179 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
180 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
183 [PERF_TYPE_HW_CACHE] = {
186 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
187 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
188 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
189 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
190 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
192 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
198 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
199 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
200 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
201 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
202 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
203 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
204 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
206 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
209 [PERF_TYPE_BREAKPOINT] = {
212 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
213 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
214 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
215 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
216 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
218 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
221 [OUTPUT_TYPE_SYNTH] = {
224 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
225 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
226 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
227 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
228 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
230 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
234 struct perf_evsel_script {
238 /* For metric output */
243 static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
245 return (struct perf_evsel_script *)evsel->priv;
248 static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
249 struct perf_data *data)
251 struct perf_evsel_script *es = zalloc(sizeof(*es));
254 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
256 es->fp = fopen(es->filename, "w");
258 goto out_free_filename;
263 zfree(&es->filename);
269 static void perf_evsel_script__delete(struct perf_evsel_script *es)
271 zfree(&es->filename);
277 static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
281 fstat(fileno(es->fp), &st);
282 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
283 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
286 static inline int output_type(unsigned int type)
289 case PERF_TYPE_SYNTH:
290 return OUTPUT_TYPE_SYNTH;
296 static inline unsigned int attr_type(unsigned int type)
299 case OUTPUT_TYPE_SYNTH:
300 return PERF_TYPE_SYNTH;
306 static bool output_set_by_user(void)
309 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
310 if (output[j].user_set)
316 static const char *output_field2str(enum perf_output_field field)
318 int i, imax = ARRAY_SIZE(all_output_options);
319 const char *str = "";
321 for (i = 0; i < imax; ++i) {
322 if (all_output_options[i].field == field) {
323 str = all_output_options[i].str;
330 #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
332 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
333 u64 sample_type, const char *sample_msg,
334 enum perf_output_field field,
337 struct perf_event_attr *attr = &evsel->attr;
338 int type = output_type(attr->type);
341 if (attr->sample_type & sample_type)
344 if (output[type].user_set) {
347 evname = perf_evsel__name(evsel);
348 pr_err("Samples for '%s' event do not have %s attribute set. "
349 "Cannot print '%s' field.\n",
350 evname, sample_msg, output_field2str(field));
354 /* user did not ask for it explicitly so remove from the default list */
355 output[type].fields &= ~field;
356 evname = perf_evsel__name(evsel);
357 pr_debug("Samples for '%s' event do not have %s attribute set. "
358 "Skipping '%s' field.\n",
359 evname, sample_msg, output_field2str(field));
364 static int perf_evsel__check_stype(struct perf_evsel *evsel,
365 u64 sample_type, const char *sample_msg,
366 enum perf_output_field field)
368 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
372 static int perf_evsel__check_attr(struct perf_evsel *evsel,
373 struct perf_session *session)
375 struct perf_event_attr *attr = &evsel->attr;
378 if (perf_header__has_feat(&session->header, HEADER_STAT))
381 allow_user_set = perf_header__has_feat(&session->header,
384 if (PRINT_FIELD(TRACE) &&
385 !perf_session__has_traces(session, "record -R"))
388 if (PRINT_FIELD(IP)) {
389 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
394 if (PRINT_FIELD(ADDR) &&
395 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
396 PERF_OUTPUT_ADDR, allow_user_set))
399 if (PRINT_FIELD(DATA_SRC) &&
400 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
401 PERF_OUTPUT_DATA_SRC))
404 if (PRINT_FIELD(WEIGHT) &&
405 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
409 if (PRINT_FIELD(SYM) &&
410 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
411 pr_err("Display of symbols requested but neither sample IP nor "
412 "sample address\navailable. Hence, no addresses to convert "
416 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
417 pr_err("Display of offsets requested but symbol is not"
421 if (PRINT_FIELD(DSO) &&
422 !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
423 pr_err("Display of DSO requested but no address to convert.\n");
426 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
427 pr_err("Display of source line number requested but sample IP is not\n"
428 "selected. Hence, no address to lookup the source line number.\n");
431 if (PRINT_FIELD(BRSTACKINSN) &&
432 !(perf_evlist__combined_branch_type(session->evlist) &
433 PERF_SAMPLE_BRANCH_ANY)) {
434 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
435 "Hint: run 'perf record -b ...'\n");
438 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
439 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
440 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
443 if (PRINT_FIELD(TIME) &&
444 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
448 if (PRINT_FIELD(CPU) &&
449 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
450 PERF_OUTPUT_CPU, allow_user_set))
453 if (PRINT_FIELD(IREGS) &&
454 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
458 if (PRINT_FIELD(UREGS) &&
459 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
463 if (PRINT_FIELD(PHYS_ADDR) &&
464 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
465 PERF_OUTPUT_PHYS_ADDR))
471 static void set_print_ip_opts(struct perf_event_attr *attr)
473 unsigned int type = output_type(attr->type);
475 output[type].print_ip_opts = 0;
477 output[type].print_ip_opts |= EVSEL__PRINT_IP;
479 if (PRINT_FIELD(SYM))
480 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
482 if (PRINT_FIELD(DSO))
483 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
485 if (PRINT_FIELD(SYMOFFSET))
486 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
488 if (PRINT_FIELD(SRCLINE))
489 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
493 * verify all user requested events exist and the samples
494 * have the expected data
496 static int perf_session__check_output_opt(struct perf_session *session)
499 struct perf_evsel *evsel;
501 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
502 evsel = perf_session__find_first_evtype(session, attr_type(j));
505 * even if fields is set to 0 (ie., show nothing) event must
506 * exist if user explicitly includes it on the command line
508 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
509 j != OUTPUT_TYPE_SYNTH) {
510 pr_err("%s events do not exist. "
511 "Remove corresponding -F option to proceed.\n",
516 if (evsel && output[j].fields &&
517 perf_evsel__check_attr(evsel, session))
523 set_print_ip_opts(&evsel->attr);
527 bool use_callchain = false;
528 bool not_pipe = false;
530 evlist__for_each_entry(session->evlist, evsel) {
532 if (evsel__has_callchain(evsel)) {
533 use_callchain = true;
537 if (not_pipe && !use_callchain)
538 symbol_conf.use_callchain = false;
542 * set default for tracepoints to print symbols only
543 * if callchains are present
545 if (symbol_conf.use_callchain &&
546 !output[PERF_TYPE_TRACEPOINT].user_set) {
547 j = PERF_TYPE_TRACEPOINT;
549 evlist__for_each_entry(session->evlist, evsel) {
550 if (evsel->attr.type != j)
553 if (evsel__has_callchain(evsel)) {
554 output[j].fields |= PERF_OUTPUT_IP;
555 output[j].fields |= PERF_OUTPUT_SYM;
556 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
557 output[j].fields |= PERF_OUTPUT_DSO;
558 set_print_ip_opts(&evsel->attr);
568 static int perf_sample__fprintf_iregs(struct perf_sample *sample,
569 struct perf_event_attr *attr, FILE *fp)
571 struct regs_dump *regs = &sample->intr_regs;
572 uint64_t mask = attr->sample_regs_intr;
579 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
580 u64 val = regs->regs[i++];
581 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
587 static int perf_sample__fprintf_uregs(struct perf_sample *sample,
588 struct perf_event_attr *attr, FILE *fp)
590 struct regs_dump *regs = &sample->user_regs;
591 uint64_t mask = attr->sample_regs_user;
595 if (!regs || !regs->regs)
598 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
600 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
601 u64 val = regs->regs[i++];
602 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
608 static int perf_sample__fprintf_start(struct perf_sample *sample,
609 struct thread *thread,
610 struct perf_evsel *evsel,
613 struct perf_event_attr *attr = &evsel->attr;
615 unsigned long long nsecs;
618 if (PRINT_FIELD(COMM)) {
620 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
621 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
622 printed += fprintf(fp, "%s ", thread__comm_str(thread));
624 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
627 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
628 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
629 else if (PRINT_FIELD(PID))
630 printed += fprintf(fp, "%5d ", sample->pid);
631 else if (PRINT_FIELD(TID))
632 printed += fprintf(fp, "%5d ", sample->tid);
634 if (PRINT_FIELD(CPU)) {
636 printed += fprintf(fp, "%3d ", sample->cpu);
638 printed += fprintf(fp, "[%03d] ", sample->cpu);
641 if (PRINT_FIELD(MISC)) {
645 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
648 ret += fprintf(fp, "K");
650 ret += fprintf(fp, "U");
652 ret += fprintf(fp, "H");
653 if (has(GUEST_KERNEL))
654 ret += fprintf(fp, "G");
656 ret += fprintf(fp, "g");
659 case PERF_RECORD_MMAP:
660 case PERF_RECORD_MMAP2:
662 ret += fprintf(fp, "M");
664 case PERF_RECORD_COMM:
666 ret += fprintf(fp, "E");
668 case PERF_RECORD_SWITCH:
669 case PERF_RECORD_SWITCH_CPU_WIDE:
670 if (has(SWITCH_OUT)) {
671 ret += fprintf(fp, "S");
672 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
673 ret += fprintf(fp, "p");
681 ret += fprintf(fp, "%*s", 6 - ret, " ");
685 if (PRINT_FIELD(TIME)) {
686 nsecs = sample->time;
687 secs = nsecs / NSEC_PER_SEC;
688 nsecs -= secs * NSEC_PER_SEC;
691 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
693 char sample_time[32];
694 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
695 printed += fprintf(fp, "%12s: ", sample_time);
703 mispred_str(struct branch_entry *br)
705 if (!(br->flags.mispred || br->flags.predicted))
708 return br->flags.predicted ? 'P' : 'M';
711 static int perf_sample__fprintf_brstack(struct perf_sample *sample,
712 struct thread *thread,
713 struct perf_event_attr *attr, FILE *fp)
715 struct branch_stack *br = sample->branch_stack;
716 struct addr_location alf, alt;
723 for (i = 0; i < br->nr; i++) {
724 from = br->entries[i].from;
725 to = br->entries[i].to;
727 if (PRINT_FIELD(DSO)) {
728 memset(&alf, 0, sizeof(alf));
729 memset(&alt, 0, sizeof(alt));
730 thread__find_map(thread, sample->cpumode, from, &alf);
731 thread__find_map(thread, sample->cpumode, to, &alt);
734 printed += fprintf(fp, " 0x%"PRIx64, from);
735 if (PRINT_FIELD(DSO)) {
736 printed += fprintf(fp, "(");
737 printed += map__fprintf_dsoname(alf.map, fp);
738 printed += fprintf(fp, ")");
741 printed += fprintf(fp, "/0x%"PRIx64, to);
742 if (PRINT_FIELD(DSO)) {
743 printed += fprintf(fp, "(");
744 printed += map__fprintf_dsoname(alt.map, fp);
745 printed += fprintf(fp, ")");
748 printed += fprintf(fp, "/%c/%c/%c/%d ",
749 mispred_str( br->entries + i),
750 br->entries[i].flags.in_tx? 'X' : '-',
751 br->entries[i].flags.abort? 'A' : '-',
752 br->entries[i].flags.cycles);
758 static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
759 struct thread *thread,
760 struct perf_event_attr *attr, FILE *fp)
762 struct branch_stack *br = sample->branch_stack;
763 struct addr_location alf, alt;
770 for (i = 0; i < br->nr; i++) {
772 memset(&alf, 0, sizeof(alf));
773 memset(&alt, 0, sizeof(alt));
774 from = br->entries[i].from;
775 to = br->entries[i].to;
777 thread__find_symbol(thread, sample->cpumode, from, &alf);
778 thread__find_symbol(thread, sample->cpumode, to, &alt);
780 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
781 if (PRINT_FIELD(DSO)) {
782 printed += fprintf(fp, "(");
783 printed += map__fprintf_dsoname(alf.map, fp);
784 printed += fprintf(fp, ")");
786 printed += fprintf(fp, "%c", '/');
787 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
788 if (PRINT_FIELD(DSO)) {
789 printed += fprintf(fp, "(");
790 printed += map__fprintf_dsoname(alt.map, fp);
791 printed += fprintf(fp, ")");
793 printed += fprintf(fp, "/%c/%c/%c/%d ",
794 mispred_str( br->entries + i),
795 br->entries[i].flags.in_tx? 'X' : '-',
796 br->entries[i].flags.abort? 'A' : '-',
797 br->entries[i].flags.cycles);
803 static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
804 struct thread *thread,
805 struct perf_event_attr *attr, FILE *fp)
807 struct branch_stack *br = sample->branch_stack;
808 struct addr_location alf, alt;
815 for (i = 0; i < br->nr; i++) {
817 memset(&alf, 0, sizeof(alf));
818 memset(&alt, 0, sizeof(alt));
819 from = br->entries[i].from;
820 to = br->entries[i].to;
822 if (thread__find_map(thread, sample->cpumode, from, &alf) &&
823 !alf.map->dso->adjust_symbols)
824 from = map__map_ip(alf.map, from);
826 if (thread__find_map(thread, sample->cpumode, to, &alt) &&
827 !alt.map->dso->adjust_symbols)
828 to = map__map_ip(alt.map, to);
830 printed += fprintf(fp, " 0x%"PRIx64, from);
831 if (PRINT_FIELD(DSO)) {
832 printed += fprintf(fp, "(");
833 printed += map__fprintf_dsoname(alf.map, fp);
834 printed += fprintf(fp, ")");
836 printed += fprintf(fp, "/0x%"PRIx64, to);
837 if (PRINT_FIELD(DSO)) {
838 printed += fprintf(fp, "(");
839 printed += map__fprintf_dsoname(alt.map, fp);
840 printed += fprintf(fp, ")");
842 printed += fprintf(fp, "/%c/%c/%c/%d ",
843 mispred_str(br->entries + i),
844 br->entries[i].flags.in_tx ? 'X' : '-',
845 br->entries[i].flags.abort ? 'A' : '-',
846 br->entries[i].flags.cycles);
851 #define MAXBB 16384UL
853 static int grab_bb(u8 *buffer, u64 start, u64 end,
854 struct machine *machine, struct thread *thread,
855 bool *is64bit, u8 *cpumode, bool last)
858 struct addr_location al;
864 kernel = machine__kernel_ip(machine, start);
866 *cpumode = PERF_RECORD_MISC_KERNEL;
868 *cpumode = PERF_RECORD_MISC_USER;
871 * Block overlaps between kernel and user.
872 * This can happen due to ring filtering
873 * On Intel CPUs the entry into the kernel is filtered,
874 * but the exit is not. Let the caller patch it up.
876 if (kernel != machine__kernel_ip(machine, end)) {
877 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
881 memset(&al, 0, sizeof(al));
882 if (end - start > MAXBB - MAXINSN) {
884 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
886 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
890 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
891 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
894 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
895 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
899 /* Load maps to ensure dso->is_64_bit has been updated */
902 offset = al.map->map_ip(al.map, start);
903 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
904 end - start + MAXINSN);
906 *is64bit = al.map->dso->is_64_bit;
908 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
913 static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
914 struct perf_insn *x, u8 *inbuf, int len,
917 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
918 dump_insn(x, ip, inbuf, len, NULL),
919 en->flags.predicted ? " PRED" : "",
920 en->flags.mispred ? " MISPRED" : "",
921 en->flags.in_tx ? " INTX" : "",
922 en->flags.abort ? " ABORT" : "");
923 if (en->flags.cycles) {
924 printed += fprintf(fp, " %d cycles", en->flags.cycles);
926 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
928 return printed + fprintf(fp, "\n");
931 static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
932 u8 cpumode, int cpu, struct symbol **lastsym,
933 struct perf_event_attr *attr, FILE *fp)
935 struct addr_location al;
936 int off, printed = 0;
938 memset(&al, 0, sizeof(al));
940 thread__find_map(thread, cpumode, addr, &al);
942 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
948 al.sym = map__find_symbol(al.map, al.addr);
953 if (al.addr < al.sym->end)
954 off = al.addr - al.sym->start;
956 off = al.addr - al.map->start - al.sym->start;
957 printed += fprintf(fp, "\t%s", al.sym->name);
959 printed += fprintf(fp, "%+d", off);
960 printed += fprintf(fp, ":");
961 if (PRINT_FIELD(SRCLINE))
962 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
963 printed += fprintf(fp, "\n");
969 static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
970 struct thread *thread,
971 struct perf_event_attr *attr,
972 struct machine *machine, FILE *fp)
974 struct branch_stack *br = sample->branch_stack;
976 int i, insn, len, nr, ilen, printed = 0;
980 struct symbol *lastsym = NULL;
985 if (max_blocks && nr > max_blocks + 1)
991 printed += fprintf(fp, "%c", '\n');
993 /* Handle first from jump, of which we don't know the entry. */
994 len = grab_bb(buffer, br->entries[nr-1].from,
995 br->entries[nr-1].from,
996 machine, thread, &x.is64bit, &x.cpumode, false);
998 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
999 x.cpumode, x.cpu, &lastsym, attr, fp);
1000 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
1001 &x, buffer, len, 0, fp);
1004 /* Print all blocks */
1005 for (i = nr - 2; i >= 0; i--) {
1006 if (br->entries[i].from || br->entries[i].to)
1007 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1008 br->entries[i].from,
1010 start = br->entries[i + 1].to;
1011 end = br->entries[i].from;
1013 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1014 /* Patch up missing kernel transfers due to ring filters */
1015 if (len == -ENXIO && i > 0) {
1016 end = br->entries[--i].from;
1017 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1018 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1024 for (off = 0;; off += ilen) {
1025 uint64_t ip = start + off;
1027 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1029 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
1032 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1033 dump_insn(&x, ip, buffer + off, len - off, &ilen));
1042 * Hit the branch? In this case we are already done, and the target
1043 * has not been executed yet.
1045 if (br->entries[0].from == sample->ip)
1047 if (br->entries[0].flags.abort)
1051 * Print final block upto sample
1053 start = br->entries[0].to;
1055 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
1056 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1058 /* Print at least last IP if basic block did not work */
1059 len = grab_bb(buffer, sample->ip, sample->ip,
1060 machine, thread, &x.is64bit, &x.cpumode, false);
1064 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
1065 dump_insn(&x, sample->ip, buffer, len, NULL));
1068 for (off = 0; off <= end - start; off += ilen) {
1069 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1070 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
1078 static int perf_sample__fprintf_addr(struct perf_sample *sample,
1079 struct thread *thread,
1080 struct perf_event_attr *attr, FILE *fp)
1082 struct addr_location al;
1083 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
1085 if (!sample_addr_correlates_sym(attr))
1088 thread__resolve(thread, &al, sample);
1090 if (PRINT_FIELD(SYM)) {
1091 printed += fprintf(fp, " ");
1092 if (PRINT_FIELD(SYMOFFSET))
1093 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
1095 printed += symbol__fprintf_symname(al.sym, fp);
1098 if (PRINT_FIELD(DSO)) {
1099 printed += fprintf(fp, " (");
1100 printed += map__fprintf_dsoname(al.map, fp);
1101 printed += fprintf(fp, ")");
1107 static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1108 struct perf_evsel *evsel,
1109 struct thread *thread,
1110 struct addr_location *al, FILE *fp)
1112 struct perf_event_attr *attr = &evsel->attr;
1113 size_t depth = thread_stack__depth(thread);
1114 struct addr_location addr_al;
1115 const char *name = NULL;
1122 * The 'return' has already been popped off the stack so the depth has
1123 * to be adjusted to match the 'call'.
1125 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1128 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1129 if (sample_addr_correlates_sym(attr)) {
1130 thread__resolve(thread, &addr_al, sample);
1132 name = addr_al.sym->name;
1138 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1140 name = al->sym->name;
1145 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1146 dlen += fprintf(fp, "(");
1147 dlen += map__fprintf_dsoname(al->map, fp);
1148 dlen += fprintf(fp, ")\t");
1152 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
1154 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
1160 * Try to keep the output length from changing frequently so that the
1161 * output lines up more nicely.
1163 if (len > spacing || (len && len < spacing - 52))
1164 spacing = round_up(len + 4, 32);
1167 len += fprintf(fp, "%*s", spacing - len, "");
1172 static int perf_sample__fprintf_insn(struct perf_sample *sample,
1173 struct perf_event_attr *attr,
1174 struct thread *thread,
1175 struct machine *machine, FILE *fp)
1179 if (PRINT_FIELD(INSNLEN))
1180 printed += fprintf(fp, " ilen: %d", sample->insn_len);
1181 if (PRINT_FIELD(INSN)) {
1184 printed += fprintf(fp, " insn:");
1185 for (i = 0; i < sample->insn_len; i++)
1186 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
1188 if (PRINT_FIELD(BRSTACKINSN))
1189 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1194 static int perf_sample__fprintf_bts(struct perf_sample *sample,
1195 struct perf_evsel *evsel,
1196 struct thread *thread,
1197 struct addr_location *al,
1198 struct machine *machine, FILE *fp)
1200 struct perf_event_attr *attr = &evsel->attr;
1201 unsigned int type = output_type(attr->type);
1202 bool print_srcline_last = false;
1205 if (PRINT_FIELD(CALLINDENT))
1206 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
1208 /* print branch_from information */
1209 if (PRINT_FIELD(IP)) {
1210 unsigned int print_opts = output[type].print_ip_opts;
1211 struct callchain_cursor *cursor = NULL;
1213 if (symbol_conf.use_callchain && sample->callchain &&
1214 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1215 sample, NULL, NULL, scripting_max_stack) == 0)
1216 cursor = &callchain_cursor;
1218 if (cursor == NULL) {
1219 printed += fprintf(fp, " ");
1220 if (print_opts & EVSEL__PRINT_SRCLINE) {
1221 print_srcline_last = true;
1222 print_opts &= ~EVSEL__PRINT_SRCLINE;
1225 printed += fprintf(fp, "\n");
1227 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
1230 /* print branch_to information */
1231 if (PRINT_FIELD(ADDR) ||
1232 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
1233 !output[type].user_set)) {
1234 printed += fprintf(fp, " => ");
1235 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
1238 if (print_srcline_last)
1239 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
1241 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1242 return printed + fprintf(fp, "\n");
1248 } sample_flags[] = {
1249 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1250 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1251 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1252 {PERF_IP_FLAG_BRANCH, "jmp"},
1253 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1254 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1255 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1256 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1257 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1258 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1259 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1260 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1261 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1265 static const char *sample_flags_to_name(u32 flags)
1269 for (i = 0; sample_flags[i].name ; i++) {
1270 if (sample_flags[i].flags == flags)
1271 return sample_flags[i].name;
1277 static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1279 const char *chars = PERF_IP_FLAG_CHARS;
1280 const int n = strlen(PERF_IP_FLAG_CHARS);
1281 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1282 const char *name = NULL;
1286 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1288 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1290 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1291 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1293 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1296 if (flags & PERF_IP_FLAG_TRACE_END) {
1297 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1299 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
1302 for (i = 0; i < n; i++, flags >>= 1) {
1304 str[pos++] = chars[i];
1306 for (; i < 32; i++, flags >>= 1) {
1312 return fprintf(fp, " %-19s ", str);
1315 struct printer_data {
1321 static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1323 void *extra, FILE *fp)
1325 unsigned char ch = (unsigned char)val;
1326 struct printer_data *printer_data = extra;
1330 case BINARY_PRINT_DATA_BEGIN:
1331 printed += fprintf(fp, "\n");
1333 case BINARY_PRINT_LINE_BEGIN:
1334 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
1337 case BINARY_PRINT_ADDR:
1338 printed += fprintf(fp, " %04x:", val);
1340 case BINARY_PRINT_NUM_DATA:
1341 printed += fprintf(fp, " %02x", val);
1343 case BINARY_PRINT_NUM_PAD:
1344 printed += fprintf(fp, " ");
1346 case BINARY_PRINT_SEP:
1347 printed += fprintf(fp, " ");
1349 case BINARY_PRINT_CHAR_DATA:
1350 if (printer_data->hit_nul && ch)
1351 printer_data->is_printable = false;
1354 printed += fprintf(fp, "%c", '.');
1356 if (!printer_data->is_printable)
1360 printer_data->hit_nul = true;
1362 printer_data->is_printable = false;
1364 printed += fprintf(fp, "%c", ch);
1367 case BINARY_PRINT_CHAR_PAD:
1368 printed += fprintf(fp, " ");
1370 case BINARY_PRINT_LINE_END:
1371 printed += fprintf(fp, "\n");
1372 printer_data->line_no++;
1374 case BINARY_PRINT_DATA_END:
1382 static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
1384 unsigned int nr_bytes = sample->raw_size;
1385 struct printer_data printer_data = {0, false, true};
1386 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1387 sample__fprintf_bpf_output, &printer_data, fp);
1389 if (printer_data.is_printable && printer_data.hit_nul)
1390 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1395 static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
1397 if (len > 0 && len < spacing)
1398 return fprintf(fp, "%*s", spacing - len, "");
1403 static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
1405 return perf_sample__fprintf_spacing(len, 34, fp);
1408 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
1410 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1413 if (perf_sample__bad_synth_size(sample, *data))
1416 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
1417 data->ip, le64_to_cpu(data->payload));
1418 return len + perf_sample__fprintf_pt_spacing(len, fp);
1421 static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
1423 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1426 if (perf_sample__bad_synth_size(sample, *data))
1429 len = fprintf(fp, " hints: %#x extensions: %#x ",
1430 data->hints, data->extensions);
1431 return len + perf_sample__fprintf_pt_spacing(len, fp);
1434 static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
1436 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1439 if (perf_sample__bad_synth_size(sample, *data))
1442 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1443 data->hw, data->cstate, data->subcstate);
1444 return len + perf_sample__fprintf_pt_spacing(len, fp);
1447 static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
1449 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1452 if (perf_sample__bad_synth_size(sample, *data))
1455 len = fprintf(fp, " IP: %u ", data->ip);
1456 return len + perf_sample__fprintf_pt_spacing(len, fp);
1459 static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
1461 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1464 if (perf_sample__bad_synth_size(sample, *data))
1467 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1468 data->deepest_cstate, data->last_cstate,
1470 return len + perf_sample__fprintf_pt_spacing(len, fp);
1473 static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
1475 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1476 unsigned int percent, freq;
1479 if (perf_sample__bad_synth_size(sample, *data))
1482 freq = (le32_to_cpu(data->freq) + 500) / 1000;
1483 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
1484 if (data->max_nonturbo) {
1485 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1486 len += fprintf(fp, "(%3u%%) ", percent);
1488 return len + perf_sample__fprintf_pt_spacing(len, fp);
1491 static int perf_sample__fprintf_synth(struct perf_sample *sample,
1492 struct perf_evsel *evsel, FILE *fp)
1494 switch (evsel->attr.config) {
1495 case PERF_SYNTH_INTEL_PTWRITE:
1496 return perf_sample__fprintf_synth_ptwrite(sample, fp);
1497 case PERF_SYNTH_INTEL_MWAIT:
1498 return perf_sample__fprintf_synth_mwait(sample, fp);
1499 case PERF_SYNTH_INTEL_PWRE:
1500 return perf_sample__fprintf_synth_pwre(sample, fp);
1501 case PERF_SYNTH_INTEL_EXSTOP:
1502 return perf_sample__fprintf_synth_exstop(sample, fp);
1503 case PERF_SYNTH_INTEL_PWRX:
1504 return perf_sample__fprintf_synth_pwrx(sample, fp);
1505 case PERF_SYNTH_INTEL_CBR:
1506 return perf_sample__fprintf_synth_cbr(sample, fp);
1514 struct perf_script {
1515 struct perf_tool tool;
1516 struct perf_session *session;
1517 bool show_task_events;
1518 bool show_mmap_events;
1519 bool show_switch_events;
1520 bool show_namespace_events;
1521 bool show_lost_events;
1522 bool show_round_events;
1524 bool per_event_dump;
1525 struct cpu_map *cpus;
1526 struct thread_map *threads;
1528 const char *time_str;
1529 struct perf_time_interval *ptime_range;
1534 static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1536 struct perf_evsel *evsel;
1539 evlist__for_each_entry(evlist, evsel) {
1540 int len = strlen(perf_evsel__name(evsel));
1542 max = MAX(len, max);
1548 static int data_src__fprintf(u64 data_src, FILE *fp)
1550 struct mem_info mi = { .data_src.val = data_src };
1556 perf_script__meminfo_scnprintf(decode, 100, &mi);
1558 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1562 return fprintf(fp, "%-*s", maxlen, out);
1566 struct perf_sample *sample;
1567 struct thread *thread;
1568 struct perf_evsel *evsel;
1572 static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1573 void *ctx, const char *color,
1575 const char *unit, double val)
1577 struct metric_ctx *mctx = ctx;
1581 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1582 PERF_RECORD_SAMPLE, mctx->fp);
1583 fputs("\tmetric: ", mctx->fp);
1585 color_fprintf(mctx->fp, color, fmt, val);
1588 fprintf(mctx->fp, " %s\n", unit);
1591 static void script_new_line(struct perf_stat_config *config __maybe_unused,
1594 struct metric_ctx *mctx = ctx;
1596 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
1597 PERF_RECORD_SAMPLE, mctx->fp);
1598 fputs("\tmetric: ", mctx->fp);
1601 static void perf_sample__fprint_metric(struct perf_script *script,
1602 struct thread *thread,
1603 struct perf_evsel *evsel,
1604 struct perf_sample *sample,
1607 struct perf_stat_output_ctx ctx = {
1608 .print_metric = script_print_metric,
1609 .new_line = script_new_line,
1610 .ctx = &(struct metric_ctx) {
1616 .force_header = false,
1618 struct perf_evsel *ev2;
1623 perf_stat__init_shadow_stats();
1627 perf_evlist__alloc_stats(script->session->evlist, false);
1628 if (evsel_script(evsel->leader)->gnum++ == 0)
1629 perf_stat__reset_shadow_stats();
1630 val = sample->period * evsel->scale;
1631 perf_stat__update_shadow_stats(evsel,
1635 evsel_script(evsel)->val = val;
1636 if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
1637 for_each_group_member (ev2, evsel->leader) {
1638 perf_stat__print_shadow_stats(&stat_config, ev2,
1639 evsel_script(ev2)->val,
1645 evsel_script(evsel->leader)->gnum = 0;
1649 static void process_event(struct perf_script *script,
1650 struct perf_sample *sample, struct perf_evsel *evsel,
1651 struct addr_location *al,
1652 struct machine *machine)
1654 struct thread *thread = al->thread;
1655 struct perf_event_attr *attr = &evsel->attr;
1656 unsigned int type = output_type(attr->type);
1657 struct perf_evsel_script *es = evsel->priv;
1660 if (output[type].fields == 0)
1665 perf_sample__fprintf_start(sample, thread, evsel,
1666 PERF_RECORD_SAMPLE, fp);
1668 if (PRINT_FIELD(PERIOD))
1669 fprintf(fp, "%10" PRIu64 " ", sample->period);
1671 if (PRINT_FIELD(EVNAME)) {
1672 const char *evname = perf_evsel__name(evsel);
1674 if (!script->name_width)
1675 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1677 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
1681 perf_sample__fprintf_flags(sample->flags, fp);
1683 if (is_bts_event(attr)) {
1684 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
1688 if (PRINT_FIELD(TRACE)) {
1689 event_format__fprintf(evsel->tp_format, sample->cpu,
1690 sample->raw_data, sample->raw_size, fp);
1693 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
1694 perf_sample__fprintf_synth(sample, evsel, fp);
1696 if (PRINT_FIELD(ADDR))
1697 perf_sample__fprintf_addr(sample, thread, attr, fp);
1699 if (PRINT_FIELD(DATA_SRC))
1700 data_src__fprintf(sample->data_src, fp);
1702 if (PRINT_FIELD(WEIGHT))
1703 fprintf(fp, "%16" PRIu64, sample->weight);
1705 if (PRINT_FIELD(IP)) {
1706 struct callchain_cursor *cursor = NULL;
1708 if (symbol_conf.use_callchain && sample->callchain &&
1709 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1710 sample, NULL, NULL, scripting_max_stack) == 0)
1711 cursor = &callchain_cursor;
1713 fputc(cursor ? '\n' : ' ', fp);
1714 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
1717 if (PRINT_FIELD(IREGS))
1718 perf_sample__fprintf_iregs(sample, attr, fp);
1720 if (PRINT_FIELD(UREGS))
1721 perf_sample__fprintf_uregs(sample, attr, fp);
1723 if (PRINT_FIELD(BRSTACK))
1724 perf_sample__fprintf_brstack(sample, thread, attr, fp);
1725 else if (PRINT_FIELD(BRSTACKSYM))
1726 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
1727 else if (PRINT_FIELD(BRSTACKOFF))
1728 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
1730 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1731 perf_sample__fprintf_bpf_output(sample, fp);
1732 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1734 if (PRINT_FIELD(PHYS_ADDR))
1735 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1738 if (PRINT_FIELD(METRIC))
1739 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
1742 static struct scripting_ops *scripting_ops;
1744 static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1746 int nthreads = thread_map__nr(counter->threads);
1747 int ncpus = perf_evsel__nr_cpus(counter);
1749 static int header_printed;
1751 if (counter->system_wide)
1754 if (!header_printed) {
1755 printf("%3s %8s %15s %15s %15s %15s %s\n",
1756 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1760 for (thread = 0; thread < nthreads; thread++) {
1761 for (cpu = 0; cpu < ncpus; cpu++) {
1762 struct perf_counts_values *counts;
1764 counts = perf_counts(counter->counts, cpu, thread);
1766 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1767 counter->cpus->map[cpu],
1768 thread_map__pid(counter->threads, thread),
1773 perf_evsel__name(counter));
1778 static void process_stat(struct perf_evsel *counter, u64 tstamp)
1780 if (scripting_ops && scripting_ops->process_stat)
1781 scripting_ops->process_stat(&stat_config, counter, tstamp);
1783 __process_stat(counter, tstamp);
1786 static void process_stat_interval(u64 tstamp)
1788 if (scripting_ops && scripting_ops->process_stat_interval)
1789 scripting_ops->process_stat_interval(tstamp);
1792 static void setup_scripting(void)
1794 setup_perl_scripting();
1795 setup_python_scripting();
1798 static int flush_scripting(void)
1800 return scripting_ops ? scripting_ops->flush_script() : 0;
1803 static int cleanup_scripting(void)
1805 pr_debug("\nperf script stopped\n");
1807 return scripting_ops ? scripting_ops->stop_script() : 0;
1810 static int process_sample_event(struct perf_tool *tool,
1811 union perf_event *event,
1812 struct perf_sample *sample,
1813 struct perf_evsel *evsel,
1814 struct machine *machine)
1816 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1817 struct addr_location al;
1819 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1825 if (sample->time < last_timestamp) {
1826 pr_err("Samples misordered, previous: %" PRIu64
1827 " this: %" PRIu64 "\n", last_timestamp,
1831 last_timestamp = sample->time;
1835 if (machine__resolve(machine, &al, sample) < 0) {
1836 pr_err("problem processing %d event, skipping it.\n",
1837 event->header.type);
1844 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
1848 scripting_ops->process_event(event, sample, evsel, &al);
1850 process_event(scr, sample, evsel, &al, machine);
1853 addr_location__put(&al);
1857 static int process_attr(struct perf_tool *tool, union perf_event *event,
1858 struct perf_evlist **pevlist)
1860 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1861 struct perf_evlist *evlist;
1862 struct perf_evsel *evsel, *pos;
1864 static struct perf_evsel_script *es;
1866 err = perf_event__process_attr(tool, event, pevlist);
1871 evsel = perf_evlist__last(*pevlist);
1874 if (scr->per_event_dump) {
1875 evsel->priv = perf_evsel_script__new(evsel,
1876 scr->session->data);
1878 es = zalloc(sizeof(*es));
1886 if (evsel->attr.type >= PERF_TYPE_MAX &&
1887 evsel->attr.type != PERF_TYPE_SYNTH)
1890 evlist__for_each_entry(evlist, pos) {
1891 if (pos->attr.type == evsel->attr.type && pos != evsel)
1895 set_print_ip_opts(&evsel->attr);
1897 if (evsel->attr.sample_type)
1898 err = perf_evsel__check_attr(evsel, scr->session);
1903 static int process_comm_event(struct perf_tool *tool,
1904 union perf_event *event,
1905 struct perf_sample *sample,
1906 struct machine *machine)
1908 struct thread *thread;
1909 struct perf_script *script = container_of(tool, struct perf_script, tool);
1910 struct perf_session *session = script->session;
1911 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1914 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1915 if (thread == NULL) {
1916 pr_debug("problem processing COMM event, skipping it.\n");
1920 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1923 if (!evsel->attr.sample_id_all) {
1926 sample->tid = event->comm.tid;
1927 sample->pid = event->comm.pid;
1929 perf_sample__fprintf_start(sample, thread, evsel,
1930 PERF_RECORD_COMM, stdout);
1931 perf_event__fprintf(event, stdout);
1934 thread__put(thread);
1938 static int process_namespaces_event(struct perf_tool *tool,
1939 union perf_event *event,
1940 struct perf_sample *sample,
1941 struct machine *machine)
1943 struct thread *thread;
1944 struct perf_script *script = container_of(tool, struct perf_script, tool);
1945 struct perf_session *session = script->session;
1946 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1949 thread = machine__findnew_thread(machine, event->namespaces.pid,
1950 event->namespaces.tid);
1951 if (thread == NULL) {
1952 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1956 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1959 if (!evsel->attr.sample_id_all) {
1962 sample->tid = event->namespaces.tid;
1963 sample->pid = event->namespaces.pid;
1965 perf_sample__fprintf_start(sample, thread, evsel,
1966 PERF_RECORD_NAMESPACES, stdout);
1967 perf_event__fprintf(event, stdout);
1970 thread__put(thread);
1974 static int process_fork_event(struct perf_tool *tool,
1975 union perf_event *event,
1976 struct perf_sample *sample,
1977 struct machine *machine)
1979 struct thread *thread;
1980 struct perf_script *script = container_of(tool, struct perf_script, tool);
1981 struct perf_session *session = script->session;
1982 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1984 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1987 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1988 if (thread == NULL) {
1989 pr_debug("problem processing FORK event, skipping it.\n");
1993 if (!evsel->attr.sample_id_all) {
1995 sample->time = event->fork.time;
1996 sample->tid = event->fork.tid;
1997 sample->pid = event->fork.pid;
1999 perf_sample__fprintf_start(sample, thread, evsel,
2000 PERF_RECORD_FORK, stdout);
2001 perf_event__fprintf(event, stdout);
2002 thread__put(thread);
2006 static int process_exit_event(struct perf_tool *tool,
2007 union perf_event *event,
2008 struct perf_sample *sample,
2009 struct machine *machine)
2012 struct thread *thread;
2013 struct perf_script *script = container_of(tool, struct perf_script, tool);
2014 struct perf_session *session = script->session;
2015 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2017 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2018 if (thread == NULL) {
2019 pr_debug("problem processing EXIT event, skipping it.\n");
2023 if (!evsel->attr.sample_id_all) {
2026 sample->tid = event->fork.tid;
2027 sample->pid = event->fork.pid;
2029 perf_sample__fprintf_start(sample, thread, evsel,
2030 PERF_RECORD_EXIT, stdout);
2031 perf_event__fprintf(event, stdout);
2033 if (perf_event__process_exit(tool, event, sample, machine) < 0)
2036 thread__put(thread);
2040 static int process_mmap_event(struct perf_tool *tool,
2041 union perf_event *event,
2042 struct perf_sample *sample,
2043 struct machine *machine)
2045 struct thread *thread;
2046 struct perf_script *script = container_of(tool, struct perf_script, tool);
2047 struct perf_session *session = script->session;
2048 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2050 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2053 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2054 if (thread == NULL) {
2055 pr_debug("problem processing MMAP event, skipping it.\n");
2059 if (!evsel->attr.sample_id_all) {
2062 sample->tid = event->mmap.tid;
2063 sample->pid = event->mmap.pid;
2065 perf_sample__fprintf_start(sample, thread, evsel,
2066 PERF_RECORD_MMAP, stdout);
2067 perf_event__fprintf(event, stdout);
2068 thread__put(thread);
2072 static int process_mmap2_event(struct perf_tool *tool,
2073 union perf_event *event,
2074 struct perf_sample *sample,
2075 struct machine *machine)
2077 struct thread *thread;
2078 struct perf_script *script = container_of(tool, struct perf_script, tool);
2079 struct perf_session *session = script->session;
2080 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2082 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2085 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2086 if (thread == NULL) {
2087 pr_debug("problem processing MMAP2 event, skipping it.\n");
2091 if (!evsel->attr.sample_id_all) {
2094 sample->tid = event->mmap2.tid;
2095 sample->pid = event->mmap2.pid;
2097 perf_sample__fprintf_start(sample, thread, evsel,
2098 PERF_RECORD_MMAP2, stdout);
2099 perf_event__fprintf(event, stdout);
2100 thread__put(thread);
2104 static int process_switch_event(struct perf_tool *tool,
2105 union perf_event *event,
2106 struct perf_sample *sample,
2107 struct machine *machine)
2109 struct thread *thread;
2110 struct perf_script *script = container_of(tool, struct perf_script, tool);
2111 struct perf_session *session = script->session;
2112 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2114 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2117 thread = machine__findnew_thread(machine, sample->pid,
2119 if (thread == NULL) {
2120 pr_debug("problem processing SWITCH event, skipping it.\n");
2124 perf_sample__fprintf_start(sample, thread, evsel,
2125 PERF_RECORD_SWITCH, stdout);
2126 perf_event__fprintf(event, stdout);
2127 thread__put(thread);
2132 process_lost_event(struct perf_tool *tool,
2133 union perf_event *event,
2134 struct perf_sample *sample,
2135 struct machine *machine)
2137 struct perf_script *script = container_of(tool, struct perf_script, tool);
2138 struct perf_session *session = script->session;
2139 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
2140 struct thread *thread;
2142 thread = machine__findnew_thread(machine, sample->pid,
2147 perf_sample__fprintf_start(sample, thread, evsel,
2148 PERF_RECORD_LOST, stdout);
2149 perf_event__fprintf(event, stdout);
2150 thread__put(thread);
2155 process_finished_round_event(struct perf_tool *tool __maybe_unused,
2156 union perf_event *event,
2157 struct ordered_events *oe __maybe_unused)
2160 perf_event__fprintf(event, stdout);
2164 static void sig_handler(int sig __maybe_unused)
2169 static void perf_script__fclose_per_event_dump(struct perf_script *script)
2171 struct perf_evlist *evlist = script->session->evlist;
2172 struct perf_evsel *evsel;
2174 evlist__for_each_entry(evlist, evsel) {
2177 perf_evsel_script__delete(evsel->priv);
2182 static int perf_script__fopen_per_event_dump(struct perf_script *script)
2184 struct perf_evsel *evsel;
2186 evlist__for_each_entry(script->session->evlist, evsel) {
2188 * Already setup? I.e. we may be called twice in cases like
2189 * Intel PT, one for the intel_pt// and dummy events, then
2190 * for the evsels syntheized from the auxtrace info.
2192 * Ses perf_script__process_auxtrace_info.
2194 if (evsel->priv != NULL)
2197 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
2198 if (evsel->priv == NULL)
2199 goto out_err_fclose;
2205 perf_script__fclose_per_event_dump(script);
2209 static int perf_script__setup_per_event_dump(struct perf_script *script)
2211 struct perf_evsel *evsel;
2212 static struct perf_evsel_script es_stdout;
2214 if (script->per_event_dump)
2215 return perf_script__fopen_per_event_dump(script);
2217 es_stdout.fp = stdout;
2219 evlist__for_each_entry(script->session->evlist, evsel)
2220 evsel->priv = &es_stdout;
2225 static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2227 struct perf_evsel *evsel;
2229 evlist__for_each_entry(script->session->evlist, evsel) {
2230 struct perf_evsel_script *es = evsel->priv;
2232 perf_evsel_script__fprintf(es, stdout);
2233 perf_evsel_script__delete(es);
2238 static int __cmd_script(struct perf_script *script)
2242 signal(SIGINT, sig_handler);
2244 /* override event processing functions */
2245 if (script->show_task_events) {
2246 script->tool.comm = process_comm_event;
2247 script->tool.fork = process_fork_event;
2248 script->tool.exit = process_exit_event;
2250 if (script->show_mmap_events) {
2251 script->tool.mmap = process_mmap_event;
2252 script->tool.mmap2 = process_mmap2_event;
2254 if (script->show_switch_events)
2255 script->tool.context_switch = process_switch_event;
2256 if (script->show_namespace_events)
2257 script->tool.namespaces = process_namespaces_event;
2258 if (script->show_lost_events)
2259 script->tool.lost = process_lost_event;
2260 if (script->show_round_events) {
2261 script->tool.ordered_events = false;
2262 script->tool.finished_round = process_finished_round_event;
2265 if (perf_script__setup_per_event_dump(script)) {
2266 pr_err("Couldn't create the per event dump files\n");
2270 ret = perf_session__process_events(script->session);
2272 if (script->per_event_dump)
2273 perf_script__exit_per_event_dump_stats(script);
2276 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
2281 struct script_spec {
2282 struct list_head node;
2283 struct scripting_ops *ops;
2287 static LIST_HEAD(script_specs);
2289 static struct script_spec *script_spec__new(const char *spec,
2290 struct scripting_ops *ops)
2292 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2295 strcpy(s->spec, spec);
2302 static void script_spec__add(struct script_spec *s)
2304 list_add_tail(&s->node, &script_specs);
2307 static struct script_spec *script_spec__find(const char *spec)
2309 struct script_spec *s;
2311 list_for_each_entry(s, &script_specs, node)
2312 if (strcasecmp(s->spec, spec) == 0)
2317 int script_spec_register(const char *spec, struct scripting_ops *ops)
2319 struct script_spec *s;
2321 s = script_spec__find(spec);
2325 s = script_spec__new(spec, ops);
2329 script_spec__add(s);
2334 static struct scripting_ops *script_spec__lookup(const char *spec)
2336 struct script_spec *s = script_spec__find(spec);
2343 static void list_available_languages(void)
2345 struct script_spec *s;
2347 fprintf(stderr, "\n");
2348 fprintf(stderr, "Scripting language extensions (used in "
2349 "perf script -s [spec:]script.[spec]):\n\n");
2351 list_for_each_entry(s, &script_specs, node)
2352 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2354 fprintf(stderr, "\n");
2357 static int parse_scriptname(const struct option *opt __maybe_unused,
2358 const char *str, int unset __maybe_unused)
2360 char spec[PATH_MAX];
2361 const char *script, *ext;
2364 if (strcmp(str, "lang") == 0) {
2365 list_available_languages();
2369 script = strchr(str, ':');
2372 if (len >= PATH_MAX) {
2373 fprintf(stderr, "invalid language specifier");
2376 strncpy(spec, str, len);
2378 scripting_ops = script_spec__lookup(spec);
2379 if (!scripting_ops) {
2380 fprintf(stderr, "invalid language specifier");
2386 ext = strrchr(script, '.');
2388 fprintf(stderr, "invalid script extension");
2391 scripting_ops = script_spec__lookup(++ext);
2392 if (!scripting_ops) {
2393 fprintf(stderr, "invalid script extension");
2398 script_name = strdup(script);
2403 static int parse_output_fields(const struct option *opt __maybe_unused,
2404 const char *arg, int unset __maybe_unused)
2406 char *tok, *strtok_saveptr = NULL;
2407 int i, imax = ARRAY_SIZE(all_output_options);
2410 char *str = strdup(arg);
2412 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
2417 /* first word can state for which event type the user is specifying
2418 * the fields. If no type exists, the specified fields apply to all
2419 * event types found in the file minus the invalid fields for a type.
2421 tok = strchr(str, ':');
2425 if (!strcmp(str, "hw"))
2426 type = PERF_TYPE_HARDWARE;
2427 else if (!strcmp(str, "sw"))
2428 type = PERF_TYPE_SOFTWARE;
2429 else if (!strcmp(str, "trace"))
2430 type = PERF_TYPE_TRACEPOINT;
2431 else if (!strcmp(str, "raw"))
2432 type = PERF_TYPE_RAW;
2433 else if (!strcmp(str, "break"))
2434 type = PERF_TYPE_BREAKPOINT;
2435 else if (!strcmp(str, "synth"))
2436 type = OUTPUT_TYPE_SYNTH;
2438 fprintf(stderr, "Invalid event type in field string.\n");
2443 if (output[type].user_set)
2444 pr_warning("Overriding previous field request for %s events.\n",
2447 output[type].fields = 0;
2448 output[type].user_set = true;
2449 output[type].wildcard_set = false;
2453 if (strlen(str) == 0) {
2455 "Cannot set fields to 'none' for all event types.\n");
2460 /* Don't override defaults for +- */
2461 if (strchr(str, '+') || strchr(str, '-'))
2464 if (output_set_by_user())
2465 pr_warning("Overriding previous field request for all events.\n");
2467 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2468 output[j].fields = 0;
2469 output[j].user_set = true;
2470 output[j].wildcard_set = true;
2475 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
2481 } else if (*tok == '-') {
2487 if (change != SET && change != DEFAULT)
2492 for (i = 0; i < imax; ++i) {
2493 if (strcmp(tok, all_output_options[i].str) == 0)
2496 if (i == imax && strcmp(tok, "flags") == 0) {
2497 print_flags = change == REMOVE ? false : true;
2501 fprintf(stderr, "Invalid field requested.\n");
2507 /* add user option to all events types for
2510 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2511 if (output[j].invalid_fields & all_output_options[i].field) {
2512 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2513 all_output_options[i].str, event_type(j));
2515 if (change == REMOVE)
2516 output[j].fields &= ~all_output_options[i].field;
2518 output[j].fields |= all_output_options[i].field;
2519 output[j].user_set = true;
2520 output[j].wildcard_set = true;
2524 if (output[type].invalid_fields & all_output_options[i].field) {
2525 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2526 all_output_options[i].str, event_type(type));
2531 output[type].user_set = true;
2532 output[type].wildcard_set = true;
2537 if (output[type].fields == 0) {
2538 pr_debug("No fields requested for %s type. "
2539 "Events will not be displayed.\n", event_type(type));
2545 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2552 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2553 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2554 if ((lang_dirent->d_type == DT_DIR || \
2555 (lang_dirent->d_type == DT_UNKNOWN && \
2556 is_directory(scripts_path, lang_dirent))) && \
2557 (strcmp(lang_dirent->d_name, ".")) && \
2558 (strcmp(lang_dirent->d_name, "..")))
2560 #define for_each_script(lang_path, lang_dir, script_dirent) \
2561 while ((script_dirent = readdir(lang_dir)) != NULL) \
2562 if (script_dirent->d_type != DT_DIR && \
2563 (script_dirent->d_type != DT_UNKNOWN || \
2564 !is_directory(lang_path, script_dirent)))
2567 #define RECORD_SUFFIX "-record"
2568 #define REPORT_SUFFIX "-report"
2570 struct script_desc {
2571 struct list_head node;
2577 static LIST_HEAD(script_descs);
2579 static struct script_desc *script_desc__new(const char *name)
2581 struct script_desc *s = zalloc(sizeof(*s));
2583 if (s != NULL && name)
2584 s->name = strdup(name);
2589 static void script_desc__delete(struct script_desc *s)
2592 zfree(&s->half_liner);
2597 static void script_desc__add(struct script_desc *s)
2599 list_add_tail(&s->node, &script_descs);
2602 static struct script_desc *script_desc__find(const char *name)
2604 struct script_desc *s;
2606 list_for_each_entry(s, &script_descs, node)
2607 if (strcasecmp(s->name, name) == 0)
2612 static struct script_desc *script_desc__findnew(const char *name)
2614 struct script_desc *s = script_desc__find(name);
2619 s = script_desc__new(name);
2623 script_desc__add(s);
2628 static const char *ends_with(const char *str, const char *suffix)
2630 size_t suffix_len = strlen(suffix);
2631 const char *p = str;
2633 if (strlen(str) > suffix_len) {
2634 p = str + strlen(str) - suffix_len;
2635 if (!strncmp(p, suffix, suffix_len))
2642 static int read_script_info(struct script_desc *desc, const char *filename)
2644 char line[BUFSIZ], *p;
2647 fp = fopen(filename, "r");
2651 while (fgets(line, sizeof(line), fp)) {
2658 if (strlen(p) && *p == '!')
2662 if (strlen(p) && p[strlen(p) - 1] == '\n')
2663 p[strlen(p) - 1] = '\0';
2665 if (!strncmp(p, "description:", strlen("description:"))) {
2666 p += strlen("description:");
2667 desc->half_liner = strdup(ltrim(p));
2671 if (!strncmp(p, "args:", strlen("args:"))) {
2672 p += strlen("args:");
2673 desc->args = strdup(ltrim(p));
2683 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2685 char *script_root, *str;
2687 script_root = strdup(script_dirent->d_name);
2691 str = (char *)ends_with(script_root, suffix);
2701 static int list_available_scripts(const struct option *opt __maybe_unused,
2702 const char *s __maybe_unused,
2703 int unset __maybe_unused)
2705 struct dirent *script_dirent, *lang_dirent;
2706 char scripts_path[MAXPATHLEN];
2707 DIR *scripts_dir, *lang_dir;
2708 char script_path[MAXPATHLEN];
2709 char lang_path[MAXPATHLEN];
2710 struct script_desc *desc;
2711 char first_half[BUFSIZ];
2714 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2716 scripts_dir = opendir(scripts_path);
2719 "open(%s) failed.\n"
2720 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2725 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2726 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2727 lang_dirent->d_name);
2728 lang_dir = opendir(lang_path);
2732 for_each_script(lang_path, lang_dir, script_dirent) {
2733 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
2735 desc = script_desc__findnew(script_root);
2736 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2737 lang_path, script_dirent->d_name);
2738 read_script_info(desc, script_path);
2744 fprintf(stdout, "List of available trace scripts:\n");
2745 list_for_each_entry(desc, &script_descs, node) {
2746 sprintf(first_half, "%s %s", desc->name,
2747 desc->args ? desc->args : "");
2748 fprintf(stdout, " %-36s %s\n", first_half,
2749 desc->half_liner ? desc->half_liner : "");
2756 * Some scripts specify the required events in their "xxx-record" file,
2757 * this function will check if the events in perf.data match those
2758 * mentioned in the "xxx-record".
2760 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2761 * which is covered well now. And new parsing code should be added to
2762 * cover the future complexing formats like event groups etc.
2764 static int check_ev_match(char *dir_name, char *scriptname,
2765 struct perf_session *session)
2767 char filename[MAXPATHLEN], evname[128];
2768 char line[BUFSIZ], *p;
2769 struct perf_evsel *pos;
2773 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
2775 fp = fopen(filename, "r");
2779 while (fgets(line, sizeof(line), fp)) {
2785 p = strstr(p, "-e");
2791 len = strcspn(p, " \t");
2795 snprintf(evname, len + 1, "%s", p);
2798 evlist__for_each_entry(session->evlist, pos) {
2799 if (!strcmp(perf_evsel__name(pos), evname)) {
2817 * Return -1 if none is found, otherwise the actual scripts number.
2819 * Currently the only user of this function is the script browser, which
2820 * will list all statically runnable scripts, select one, execute it and
2821 * show the output in a perf browser.
2823 int find_scripts(char **scripts_array, char **scripts_path_array)
2825 struct dirent *script_dirent, *lang_dirent;
2826 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
2827 DIR *scripts_dir, *lang_dir;
2828 struct perf_session *session;
2829 struct perf_data data = {
2833 .mode = PERF_DATA_MODE_READ,
2838 session = perf_session__new(&data, false, NULL);
2842 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2844 scripts_dir = opendir(scripts_path);
2846 perf_session__delete(session);
2850 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2851 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2852 lang_dirent->d_name);
2853 #ifndef HAVE_LIBPERL_SUPPORT
2854 if (strstr(lang_path, "perl"))
2857 #ifndef HAVE_LIBPYTHON_SUPPORT
2858 if (strstr(lang_path, "python"))
2862 lang_dir = opendir(lang_path);
2866 for_each_script(lang_path, lang_dir, script_dirent) {
2867 /* Skip those real time scripts: xxxtop.p[yl] */
2868 if (strstr(script_dirent->d_name, "top."))
2870 sprintf(scripts_path_array[i], "%s/%s", lang_path,
2871 script_dirent->d_name);
2872 temp = strchr(script_dirent->d_name, '.');
2873 snprintf(scripts_array[i],
2874 (temp - script_dirent->d_name) + 1,
2875 "%s", script_dirent->d_name);
2877 if (check_ev_match(lang_path,
2878 scripts_array[i], session))
2886 closedir(scripts_dir);
2887 perf_session__delete(session);
2891 static char *get_script_path(const char *script_root, const char *suffix)
2893 struct dirent *script_dirent, *lang_dirent;
2894 char scripts_path[MAXPATHLEN];
2895 char script_path[MAXPATHLEN];
2896 DIR *scripts_dir, *lang_dir;
2897 char lang_path[MAXPATHLEN];
2898 char *__script_root;
2900 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2902 scripts_dir = opendir(scripts_path);
2906 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2907 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2908 lang_dirent->d_name);
2909 lang_dir = opendir(lang_path);
2913 for_each_script(lang_path, lang_dir, script_dirent) {
2914 __script_root = get_script_root(script_dirent, suffix);
2915 if (__script_root && !strcmp(script_root, __script_root)) {
2916 free(__script_root);
2918 closedir(scripts_dir);
2919 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2920 lang_path, script_dirent->d_name);
2921 return strdup(script_path);
2923 free(__script_root);
2927 closedir(scripts_dir);
2932 static bool is_top_script(const char *script_path)
2934 return ends_with(script_path, "top") == NULL ? false : true;
2937 static int has_required_arg(char *script_path)
2939 struct script_desc *desc;
2943 desc = script_desc__new(NULL);
2945 if (read_script_info(desc, script_path))
2951 for (p = desc->args; *p; p++)
2955 script_desc__delete(desc);
2960 static int have_cmd(int argc, const char **argv)
2962 char **__argv = malloc(sizeof(const char *) * argc);
2965 pr_err("malloc failed\n");
2969 memcpy(__argv, argv, sizeof(const char *) * argc);
2970 argc = parse_options(argc, (const char **)__argv, record_options,
2971 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2974 system_wide = (argc == 0);
2979 static void script__setup_sample_type(struct perf_script *script)
2981 struct perf_session *session = script->session;
2982 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2984 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2985 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2986 (sample_type & PERF_SAMPLE_STACK_USER)) {
2987 callchain_param.record_mode = CALLCHAIN_DWARF;
2988 dwarf_callchain_users = true;
2989 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2990 callchain_param.record_mode = CALLCHAIN_LBR;
2992 callchain_param.record_mode = CALLCHAIN_FP;
2996 static int process_stat_round_event(struct perf_session *session,
2997 union perf_event *event)
2999 struct stat_round_event *round = &event->stat_round;
3000 struct perf_evsel *counter;
3002 evlist__for_each_entry(session->evlist, counter) {
3003 perf_stat_process_counter(&stat_config, counter);
3004 process_stat(counter, round->time);
3007 process_stat_interval(round->time);
3011 static int process_stat_config_event(struct perf_session *session __maybe_unused,
3012 union perf_event *event)
3014 perf_event__read_stat_config(&stat_config, &event->stat_config);
3018 static int set_maps(struct perf_script *script)
3020 struct perf_evlist *evlist = script->session->evlist;
3022 if (!script->cpus || !script->threads)
3025 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3028 perf_evlist__set_maps(evlist, script->cpus, script->threads);
3030 if (perf_evlist__alloc_stats(evlist, true))
3033 script->allocated = true;
3038 int process_thread_map_event(struct perf_session *session,
3039 union perf_event *event)
3041 struct perf_tool *tool = session->tool;
3042 struct perf_script *script = container_of(tool, struct perf_script, tool);
3044 if (script->threads) {
3045 pr_warning("Extra thread map event, ignoring.\n");
3049 script->threads = thread_map__new_event(&event->thread_map);
3050 if (!script->threads)
3053 return set_maps(script);
3057 int process_cpu_map_event(struct perf_session *session,
3058 union perf_event *event)
3060 struct perf_tool *tool = session->tool;
3061 struct perf_script *script = container_of(tool, struct perf_script, tool);
3064 pr_warning("Extra cpu map event, ignoring.\n");
3068 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3072 return set_maps(script);
3075 static int process_feature_event(struct perf_session *session,
3076 union perf_event *event)
3078 if (event->feat.feat_id < HEADER_LAST_FEATURE)
3079 return perf_event__process_feature(session, event);
3083 #ifdef HAVE_AUXTRACE_SUPPORT
3084 static int perf_script__process_auxtrace_info(struct perf_session *session,
3085 union perf_event *event)
3087 struct perf_tool *tool = session->tool;
3089 int ret = perf_event__process_auxtrace_info(session, event);
3092 struct perf_script *script = container_of(tool, struct perf_script, tool);
3094 ret = perf_script__setup_per_event_dump(script);
3100 #define perf_script__process_auxtrace_info 0
3103 int cmd_script(int argc, const char **argv)
3105 bool show_full_info = false;
3106 bool header = false;
3107 bool header_only = false;
3108 bool script_started = false;
3109 char *rec_script_path = NULL;
3110 char *rep_script_path = NULL;
3111 struct perf_session *session;
3112 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
3113 char *script_path = NULL;
3114 const char **__argv;
3116 struct perf_script script = {
3118 .sample = process_sample_event,
3119 .mmap = perf_event__process_mmap,
3120 .mmap2 = perf_event__process_mmap2,
3121 .comm = perf_event__process_comm,
3122 .namespaces = perf_event__process_namespaces,
3123 .exit = perf_event__process_exit,
3124 .fork = perf_event__process_fork,
3125 .attr = process_attr,
3126 .event_update = perf_event__process_event_update,
3127 .tracing_data = perf_event__process_tracing_data,
3128 .feature = process_feature_event,
3129 .build_id = perf_event__process_build_id,
3130 .id_index = perf_event__process_id_index,
3131 .auxtrace_info = perf_script__process_auxtrace_info,
3132 .auxtrace = perf_event__process_auxtrace,
3133 .auxtrace_error = perf_event__process_auxtrace_error,
3134 .stat = perf_event__process_stat_event,
3135 .stat_round = process_stat_round_event,
3136 .stat_config = process_stat_config_event,
3137 .thread_map = process_thread_map_event,
3138 .cpu_map = process_cpu_map_event,
3139 .ordered_events = true,
3140 .ordering_requires_timestamps = true,
3143 struct perf_data data = {
3144 .mode = PERF_DATA_MODE_READ,
3146 const struct option options[] = {
3147 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3148 "dump raw trace in ASCII"),
3149 OPT_INCR('v', "verbose", &verbose,
3150 "be more verbose (show symbol address, etc)"),
3151 OPT_BOOLEAN('L', "Latency", &latency_format,
3152 "show latency attributes (irqs/preemption disabled, etc)"),
3153 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3154 list_available_scripts),
3155 OPT_CALLBACK('s', "script", NULL, "name",
3156 "script file name (lang:script name, script name, or *)",
3158 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3159 "generate perf-script.xx script in specified language"),
3160 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3161 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3162 "do various checks like samples ordering and lost events"),
3163 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3164 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
3165 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3166 "file", "vmlinux pathname"),
3167 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3168 "file", "kallsyms pathname"),
3169 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3170 "When printing symbols do not display call chain"),
3171 OPT_CALLBACK(0, "symfs", NULL, "directory",
3172 "Look for files with symbols relative to this directory",
3173 symbol__config_symfs),
3174 OPT_CALLBACK('F', "fields", NULL, "str",
3175 "comma separated output fields prepend with 'type:'. "
3176 "+field to add and -field to remove."
3177 "Valid types: hw,sw,trace,raw,synth. "
3178 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
3179 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3180 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
3181 "callindent,insn,insnlen,synth,phys_addr,metric,misc",
3182 parse_output_fields),
3183 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3184 "system-wide collection from all CPUs"),
3185 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3186 "only consider these symbols"),
3187 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3188 "Stop display of callgraph at these symbols"),
3189 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3190 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3191 "only display events for these comms"),
3192 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3193 "only consider symbols in these pids"),
3194 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3195 "only consider symbols in these tids"),
3196 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3197 "Set the maximum stack depth when parsing the callchain, "
3198 "anything beyond the specified depth will be ignored. "
3199 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3200 OPT_BOOLEAN('I', "show-info", &show_full_info,
3201 "display extended information from perf.data file"),
3202 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3203 "Show the path of [kernel.kallsyms]"),
3204 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3205 "Show the fork/comm/exit events"),
3206 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3207 "Show the mmap events"),
3208 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3209 "Show context switch events (if recorded)"),
3210 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3211 "Show namespace events (if recorded)"),
3212 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3213 "Show lost events (if recorded)"),
3214 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3215 "Show round events (if recorded)"),
3216 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3217 "Dump trace output to files named by the monitored events"),
3218 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
3219 OPT_INTEGER(0, "max-blocks", &max_blocks,
3220 "Maximum number of code blocks to dump with brstackinsn"),
3221 OPT_BOOLEAN(0, "ns", &nanosecs,
3222 "Use 9 decimal places when displaying time"),
3223 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3224 "Instruction Tracing options\n" ITRACE_HELP,
3225 itrace_parse_synth_opts),
3226 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3227 "Show full source file name path for source lines"),
3228 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3229 "Enable symbol demangling"),
3230 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3231 "Enable kernel symbol demangling"),
3232 OPT_STRING(0, "time", &script.time_str, "str",
3233 "Time span of interest (start,stop)"),
3234 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3235 "Show inline function"),
3238 const char * const script_subcommands[] = { "record", "report", NULL };
3239 const char *script_usage[] = {
3240 "perf script [<options>]",
3241 "perf script [<options>] record <script> [<record-options>] <command>",
3242 "perf script [<options>] report <script> [script-args]",
3243 "perf script [<options>] <script> [<record-options>] <command>",
3244 "perf script [<options>] <top-script> [script-args]",
3248 perf_set_singlethreaded();
3252 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
3253 PARSE_OPT_STOP_AT_NON_OPTION);
3255 data.file.path = input_name;
3256 data.force = symbol_conf.force;
3258 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3259 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3260 if (!rec_script_path)
3261 return cmd_record(argc, argv);
3264 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3265 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3266 if (!rep_script_path) {
3268 "Please specify a valid report script"
3269 "(see 'perf script -l' for listing)\n");
3274 if (itrace_synth_opts.callchain &&
3275 itrace_synth_opts.callchain_sz > scripting_max_stack)
3276 scripting_max_stack = itrace_synth_opts.callchain_sz;
3278 /* make sure PERF_EXEC_PATH is set for scripts */
3279 set_argv_exec_path(get_argv_exec_path());
3281 if (argc && !script_name && !rec_script_path && !rep_script_path) {
3286 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3287 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3289 if (!rec_script_path && !rep_script_path) {
3290 usage_with_options_msg(script_usage, options,
3291 "Couldn't find script `%s'\n\n See perf"
3292 " script -l for available scripts.\n", argv[0]);
3295 if (is_top_script(argv[0])) {
3296 rep_args = argc - 1;
3300 rep_args = has_required_arg(rep_script_path);
3301 rec_args = (argc - 1) - rep_args;
3303 usage_with_options_msg(script_usage, options,
3304 "`%s' script requires options."
3305 "\n\n See perf script -l for available "
3306 "scripts and options.\n", argv[0]);
3310 if (pipe(live_pipe) < 0) {
3311 perror("failed to create pipe");
3317 perror("failed to fork");
3324 dup2(live_pipe[1], 1);
3325 close(live_pipe[0]);
3327 if (is_top_script(argv[0])) {
3329 } else if (!system_wide) {
3330 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3336 __argv = malloc((argc + 6) * sizeof(const char *));
3338 pr_err("malloc failed\n");
3343 __argv[j++] = "/bin/sh";
3344 __argv[j++] = rec_script_path;
3350 for (i = rep_args + 1; i < argc; i++)
3351 __argv[j++] = argv[i];
3354 execvp("/bin/sh", (char **)__argv);
3359 dup2(live_pipe[0], 0);
3360 close(live_pipe[1]);
3362 __argv = malloc((argc + 4) * sizeof(const char *));
3364 pr_err("malloc failed\n");
3370 __argv[j++] = "/bin/sh";
3371 __argv[j++] = rep_script_path;
3372 for (i = 1; i < rep_args + 1; i++)
3373 __argv[j++] = argv[i];
3378 execvp("/bin/sh", (char **)__argv);
3383 if (rec_script_path)
3384 script_path = rec_script_path;
3385 if (rep_script_path)
3386 script_path = rep_script_path;
3391 if (!rec_script_path)
3392 system_wide = false;
3393 else if (!system_wide) {
3394 if (have_cmd(argc - 1, &argv[1]) != 0) {
3400 __argv = malloc((argc + 2) * sizeof(const char *));
3402 pr_err("malloc failed\n");
3407 __argv[j++] = "/bin/sh";
3408 __argv[j++] = script_path;
3411 for (i = 2; i < argc; i++)
3412 __argv[j++] = argv[i];
3415 execvp("/bin/sh", (char **)__argv);
3423 session = perf_session__new(&data, false, &script.tool);
3424 if (session == NULL)
3427 if (header || header_only) {
3428 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
3429 perf_session__fprintf_info(session, stdout, show_full_info);
3434 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
3436 if (symbol__init(&session->header.env) < 0)
3439 script.session = session;
3440 script__setup_sample_type(&script);
3442 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3443 itrace_synth_opts.thread_stack = true;
3445 session->itrace_synth_opts = &itrace_synth_opts;
3448 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3451 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
3455 symbol_conf.use_callchain = true;
3457 symbol_conf.use_callchain = false;
3459 if (session->tevent.pevent &&
3460 tep_set_function_resolver(session->tevent.pevent,
3461 machine__resolve_kernel_addr,
3462 &session->machines.host) < 0) {
3463 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3468 if (generate_script_lang) {
3469 struct stat perf_stat;
3472 if (output_set_by_user()) {
3474 "custom fields not supported for generated scripts");
3479 input = open(data.file.path, O_RDONLY); /* input_name */
3482 perror("failed to open file");
3486 err = fstat(input, &perf_stat);
3488 perror("failed to stat file");
3492 if (!perf_stat.st_size) {
3493 fprintf(stderr, "zero-sized file, nothing to do!\n");
3497 scripting_ops = script_spec__lookup(generate_script_lang);
3498 if (!scripting_ops) {
3499 fprintf(stderr, "invalid language specifier");
3504 err = scripting_ops->generate_script(session->tevent.pevent,
3510 err = scripting_ops->start_script(script_name, argc, argv);
3513 pr_debug("perf script started with script %s\n\n", script_name);
3514 script_started = true;
3518 err = perf_session__check_output_opt(session);
3522 script.ptime_range = perf_time__range_alloc(script.time_str,
3523 &script.range_size);
3524 if (!script.ptime_range) {
3529 /* needs to be parsed after looking up reference time */
3530 if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
3531 if (session->evlist->first_sample_time == 0 &&
3532 session->evlist->last_sample_time == 0) {
3533 pr_err("HINT: no first/last sample time found in perf data.\n"
3534 "Please use latest perf binary to execute 'perf record'\n"
3535 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
3540 script.range_num = perf_time__percent_parse_str(
3541 script.ptime_range, script.range_size,
3543 session->evlist->first_sample_time,
3544 session->evlist->last_sample_time);
3546 if (script.range_num < 0) {
3547 pr_err("Invalid time string\n");
3552 script.range_num = 1;
3555 err = __cmd_script(&script);
3560 zfree(&script.ptime_range);
3562 perf_evlist__free_stats(session->evlist);
3563 perf_session__delete(session);
3566 cleanup_scripting();