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"
19 #include "util/evlist.h"
20 #include "util/evsel.h"
21 #include "util/evsel_fprintf.h"
22 #include "util/evswitch.h"
23 #include "util/sort.h"
24 #include "util/data.h"
25 #include "util/auxtrace.h"
26 #include "util/cpumap.h"
27 #include "util/thread_map.h"
28 #include "util/stat.h"
29 #include "util/color.h"
30 #include "util/string2.h"
31 #include "util/thread-stack.h"
32 #include "util/time-utils.h"
33 #include "util/path.h"
34 #include "util/event.h"
36 #include "print_binary.h"
38 #include <linux/bitmap.h>
39 #include <linux/kernel.h>
40 #include <linux/stringify.h>
41 #include <linux/time64.h>
42 #include <linux/zalloc.h>
43 #include <sys/utsname.h>
45 #include "util/mem-events.h"
46 #include "util/dump-insn.h"
51 #include <sys/param.h>
52 #include <sys/types.h>
56 #include <subcmd/pager.h>
57 #include <perf/evlist.h>
58 #include <linux/err.h>
59 #include "util/dlfilter.h"
60 #include "util/record.h"
61 #include "util/util.h"
64 #include <linux/ctype.h>
66 static char const *script_name;
67 static char const *generate_script_lang;
69 static bool deltatime;
70 static u64 initial_time;
71 static u64 previous_time;
72 static bool debug_mode;
73 static u64 last_timestamp;
74 static u64 nr_unordered;
75 static bool no_callchain;
76 static bool latency_format;
77 static bool system_wide;
78 static bool print_flags;
79 static const char *cpu_list;
80 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
81 static struct perf_stat_config stat_config;
82 static int max_blocks;
83 static bool native_arch;
84 static struct dlfilter *dlfilter;
88 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
90 enum perf_output_field {
91 PERF_OUTPUT_COMM = 1ULL << 0,
92 PERF_OUTPUT_TID = 1ULL << 1,
93 PERF_OUTPUT_PID = 1ULL << 2,
94 PERF_OUTPUT_TIME = 1ULL << 3,
95 PERF_OUTPUT_CPU = 1ULL << 4,
96 PERF_OUTPUT_EVNAME = 1ULL << 5,
97 PERF_OUTPUT_TRACE = 1ULL << 6,
98 PERF_OUTPUT_IP = 1ULL << 7,
99 PERF_OUTPUT_SYM = 1ULL << 8,
100 PERF_OUTPUT_DSO = 1ULL << 9,
101 PERF_OUTPUT_ADDR = 1ULL << 10,
102 PERF_OUTPUT_SYMOFFSET = 1ULL << 11,
103 PERF_OUTPUT_SRCLINE = 1ULL << 12,
104 PERF_OUTPUT_PERIOD = 1ULL << 13,
105 PERF_OUTPUT_IREGS = 1ULL << 14,
106 PERF_OUTPUT_BRSTACK = 1ULL << 15,
107 PERF_OUTPUT_BRSTACKSYM = 1ULL << 16,
108 PERF_OUTPUT_DATA_SRC = 1ULL << 17,
109 PERF_OUTPUT_WEIGHT = 1ULL << 18,
110 PERF_OUTPUT_BPF_OUTPUT = 1ULL << 19,
111 PERF_OUTPUT_CALLINDENT = 1ULL << 20,
112 PERF_OUTPUT_INSN = 1ULL << 21,
113 PERF_OUTPUT_INSNLEN = 1ULL << 22,
114 PERF_OUTPUT_BRSTACKINSN = 1ULL << 23,
115 PERF_OUTPUT_BRSTACKOFF = 1ULL << 24,
116 PERF_OUTPUT_SYNTH = 1ULL << 25,
117 PERF_OUTPUT_PHYS_ADDR = 1ULL << 26,
118 PERF_OUTPUT_UREGS = 1ULL << 27,
119 PERF_OUTPUT_METRIC = 1ULL << 28,
120 PERF_OUTPUT_MISC = 1ULL << 29,
121 PERF_OUTPUT_SRCCODE = 1ULL << 30,
122 PERF_OUTPUT_IPC = 1ULL << 31,
123 PERF_OUTPUT_TOD = 1ULL << 32,
124 PERF_OUTPUT_DATA_PAGE_SIZE = 1ULL << 33,
125 PERF_OUTPUT_CODE_PAGE_SIZE = 1ULL << 34,
126 PERF_OUTPUT_INS_LAT = 1ULL << 35,
127 PERF_OUTPUT_BRSTACKINSNLEN = 1ULL << 36,
128 PERF_OUTPUT_MACHINE_PID = 1ULL << 37,
129 PERF_OUTPUT_VCPU = 1ULL << 38,
133 struct perf_tool tool;
134 struct perf_session *session;
135 bool show_task_events;
136 bool show_mmap_events;
137 bool show_switch_events;
138 bool show_namespace_events;
139 bool show_lost_events;
140 bool show_round_events;
141 bool show_bpf_events;
142 bool show_cgroup_events;
143 bool show_text_poke_events;
147 struct evswitch evswitch;
148 struct perf_cpu_map *cpus;
149 struct perf_thread_map *threads;
151 const char *time_str;
152 struct perf_time_interval *ptime_range;
157 struct output_option {
159 enum perf_output_field field;
160 } all_output_options[] = {
161 {.str = "comm", .field = PERF_OUTPUT_COMM},
162 {.str = "tid", .field = PERF_OUTPUT_TID},
163 {.str = "pid", .field = PERF_OUTPUT_PID},
164 {.str = "time", .field = PERF_OUTPUT_TIME},
165 {.str = "cpu", .field = PERF_OUTPUT_CPU},
166 {.str = "event", .field = PERF_OUTPUT_EVNAME},
167 {.str = "trace", .field = PERF_OUTPUT_TRACE},
168 {.str = "ip", .field = PERF_OUTPUT_IP},
169 {.str = "sym", .field = PERF_OUTPUT_SYM},
170 {.str = "dso", .field = PERF_OUTPUT_DSO},
171 {.str = "addr", .field = PERF_OUTPUT_ADDR},
172 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
173 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
174 {.str = "period", .field = PERF_OUTPUT_PERIOD},
175 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
176 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
177 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
178 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
179 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
180 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
181 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
182 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
183 {.str = "insn", .field = PERF_OUTPUT_INSN},
184 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
185 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
186 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
187 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
188 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
189 {.str = "metric", .field = PERF_OUTPUT_METRIC},
190 {.str = "misc", .field = PERF_OUTPUT_MISC},
191 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
192 {.str = "ipc", .field = PERF_OUTPUT_IPC},
193 {.str = "tod", .field = PERF_OUTPUT_TOD},
194 {.str = "data_page_size", .field = PERF_OUTPUT_DATA_PAGE_SIZE},
195 {.str = "code_page_size", .field = PERF_OUTPUT_CODE_PAGE_SIZE},
196 {.str = "ins_lat", .field = PERF_OUTPUT_INS_LAT},
197 {.str = "brstackinsnlen", .field = PERF_OUTPUT_BRSTACKINSNLEN},
198 {.str = "machine_pid", .field = PERF_OUTPUT_MACHINE_PID},
199 {.str = "vcpu", .field = PERF_OUTPUT_VCPU},
203 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
208 /* default set to maintain compatibility with current format */
212 unsigned int print_ip_opts;
216 u64 user_unset_fields;
217 } output[OUTPUT_TYPE_MAX] = {
219 [PERF_TYPE_HARDWARE] = {
222 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
223 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
224 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
225 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
226 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
228 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
231 [PERF_TYPE_SOFTWARE] = {
234 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
235 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
236 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
237 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
238 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
239 PERF_OUTPUT_BPF_OUTPUT,
241 .invalid_fields = PERF_OUTPUT_TRACE,
244 [PERF_TYPE_TRACEPOINT] = {
247 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
248 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
249 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
252 [PERF_TYPE_HW_CACHE] = {
255 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
256 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
257 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
258 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
259 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
261 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
267 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
268 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
269 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
270 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
271 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
272 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
273 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR |
274 PERF_OUTPUT_DATA_PAGE_SIZE | PERF_OUTPUT_CODE_PAGE_SIZE |
277 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
280 [PERF_TYPE_BREAKPOINT] = {
283 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
284 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
285 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
286 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
287 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
289 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
292 [OUTPUT_TYPE_SYNTH] = {
295 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
296 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
297 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
298 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
299 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
301 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
304 [OUTPUT_TYPE_OTHER] = {
307 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
308 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
309 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
310 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
311 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
313 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
317 struct evsel_script {
321 /* For metric output */
326 static inline struct evsel_script *evsel_script(struct evsel *evsel)
328 return (struct evsel_script *)evsel->priv;
331 static struct evsel_script *evsel_script__new(struct evsel *evsel, struct perf_data *data)
333 struct evsel_script *es = zalloc(sizeof(*es));
336 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, evsel__name(evsel)) < 0)
338 es->fp = fopen(es->filename, "w");
340 goto out_free_filename;
345 zfree(&es->filename);
351 static void evsel_script__delete(struct evsel_script *es)
353 zfree(&es->filename);
359 static int evsel_script__fprintf(struct evsel_script *es, FILE *fp)
363 fstat(fileno(es->fp), &st);
364 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
365 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
368 static inline int output_type(unsigned int type)
371 case PERF_TYPE_SYNTH:
372 return OUTPUT_TYPE_SYNTH;
374 if (type < PERF_TYPE_MAX)
378 return OUTPUT_TYPE_OTHER;
381 static bool output_set_by_user(void)
384 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
385 if (output[j].user_set)
391 static const char *output_field2str(enum perf_output_field field)
393 int i, imax = ARRAY_SIZE(all_output_options);
394 const char *str = "";
396 for (i = 0; i < imax; ++i) {
397 if (all_output_options[i].field == field) {
398 str = all_output_options[i].str;
405 #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
407 static int evsel__do_check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
408 enum perf_output_field field, bool allow_user_set)
410 struct perf_event_attr *attr = &evsel->core.attr;
411 int type = output_type(attr->type);
414 if (attr->sample_type & sample_type)
417 if (output[type].user_set_fields & field) {
420 evname = evsel__name(evsel);
421 pr_err("Samples for '%s' event do not have %s attribute set. "
422 "Cannot print '%s' field.\n",
423 evname, sample_msg, output_field2str(field));
427 /* user did not ask for it explicitly so remove from the default list */
428 output[type].fields &= ~field;
429 evname = evsel__name(evsel);
430 pr_debug("Samples for '%s' event do not have %s attribute set. "
431 "Skipping '%s' field.\n",
432 evname, sample_msg, output_field2str(field));
437 static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
438 enum perf_output_field field)
440 return evsel__do_check_stype(evsel, sample_type, sample_msg, field, false);
443 static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
445 struct perf_event_attr *attr = &evsel->core.attr;
448 if (evsel__is_dummy_event(evsel))
451 if (perf_header__has_feat(&session->header, HEADER_STAT))
454 allow_user_set = perf_header__has_feat(&session->header,
457 if (PRINT_FIELD(TRACE) &&
458 !perf_session__has_traces(session, "record -R"))
461 if (PRINT_FIELD(IP)) {
462 if (evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", PERF_OUTPUT_IP))
466 if (PRINT_FIELD(ADDR) &&
467 evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", PERF_OUTPUT_ADDR, allow_user_set))
470 if (PRINT_FIELD(DATA_SRC) &&
471 evsel__do_check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", PERF_OUTPUT_DATA_SRC, allow_user_set))
474 if (PRINT_FIELD(WEIGHT) &&
475 evsel__do_check_stype(evsel, PERF_SAMPLE_WEIGHT_TYPE, "WEIGHT", PERF_OUTPUT_WEIGHT, allow_user_set))
478 if (PRINT_FIELD(SYM) &&
479 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
480 pr_err("Display of symbols requested but neither sample IP nor "
481 "sample address\navailable. Hence, no addresses to convert "
485 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
486 pr_err("Display of offsets requested but symbol is not"
490 if (PRINT_FIELD(DSO) &&
491 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
492 pr_err("Display of DSO requested but no address to convert.\n");
495 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
496 pr_err("Display of source line number requested but sample IP is not\n"
497 "selected. Hence, no address to lookup the source line number.\n");
500 if ((PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN)) && !allow_user_set &&
501 !(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY)) {
502 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
503 "Hint: run 'perf record -b ...'\n");
506 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
507 evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", PERF_OUTPUT_TID|PERF_OUTPUT_PID))
510 if (PRINT_FIELD(TIME) &&
511 evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", PERF_OUTPUT_TIME))
514 if (PRINT_FIELD(CPU) &&
515 evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", PERF_OUTPUT_CPU, allow_user_set))
518 if (PRINT_FIELD(IREGS) &&
519 evsel__do_check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", PERF_OUTPUT_IREGS, allow_user_set))
522 if (PRINT_FIELD(UREGS) &&
523 evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS", PERF_OUTPUT_UREGS))
526 if (PRINT_FIELD(PHYS_ADDR) &&
527 evsel__do_check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR", PERF_OUTPUT_PHYS_ADDR, allow_user_set))
530 if (PRINT_FIELD(DATA_PAGE_SIZE) &&
531 evsel__check_stype(evsel, PERF_SAMPLE_DATA_PAGE_SIZE, "DATA_PAGE_SIZE", PERF_OUTPUT_DATA_PAGE_SIZE))
534 if (PRINT_FIELD(CODE_PAGE_SIZE) &&
535 evsel__check_stype(evsel, PERF_SAMPLE_CODE_PAGE_SIZE, "CODE_PAGE_SIZE", PERF_OUTPUT_CODE_PAGE_SIZE))
538 if (PRINT_FIELD(INS_LAT) &&
539 evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT_STRUCT, "WEIGHT_STRUCT", PERF_OUTPUT_INS_LAT))
545 static void set_print_ip_opts(struct perf_event_attr *attr)
547 unsigned int type = output_type(attr->type);
549 output[type].print_ip_opts = 0;
551 output[type].print_ip_opts |= EVSEL__PRINT_IP;
553 if (PRINT_FIELD(SYM))
554 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
556 if (PRINT_FIELD(DSO))
557 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
559 if (PRINT_FIELD(SYMOFFSET))
560 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
562 if (PRINT_FIELD(SRCLINE))
563 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
566 static struct evsel *find_first_output_type(struct evlist *evlist,
571 evlist__for_each_entry(evlist, evsel) {
572 if (evsel__is_dummy_event(evsel))
574 if (output_type(evsel->core.attr.type) == (int)type)
581 * verify all user requested events exist and the samples
582 * have the expected data
584 static int perf_session__check_output_opt(struct perf_session *session)
590 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
591 evsel = find_first_output_type(session->evlist, j);
594 * even if fields is set to 0 (ie., show nothing) event must
595 * exist if user explicitly includes it on the command line
597 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
598 j != OUTPUT_TYPE_SYNTH) {
599 pr_err("%s events do not exist. "
600 "Remove corresponding -F option to proceed.\n",
605 if (evsel && output[j].fields &&
606 evsel__check_attr(evsel, session))
612 set_print_ip_opts(&evsel->core.attr);
613 tod |= output[j].fields & PERF_OUTPUT_TOD;
617 bool use_callchain = false;
618 bool not_pipe = false;
620 evlist__for_each_entry(session->evlist, evsel) {
622 if (evsel__has_callchain(evsel)) {
623 use_callchain = true;
627 if (not_pipe && !use_callchain)
628 symbol_conf.use_callchain = false;
632 * set default for tracepoints to print symbols only
633 * if callchains are present
635 if (symbol_conf.use_callchain &&
636 !output[PERF_TYPE_TRACEPOINT].user_set) {
637 j = PERF_TYPE_TRACEPOINT;
639 evlist__for_each_entry(session->evlist, evsel) {
640 if (evsel->core.attr.type != j)
643 if (evsel__has_callchain(evsel)) {
644 output[j].fields |= PERF_OUTPUT_IP;
645 output[j].fields |= PERF_OUTPUT_SYM;
646 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
647 output[j].fields |= PERF_OUTPUT_DSO;
648 set_print_ip_opts(&evsel->core.attr);
654 if (tod && !session->header.env.clock.enabled) {
655 pr_err("Can't provide 'tod' time, missing clock data. "
656 "Please record with -k/--clockid option.\n");
663 static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask, const char *arch,
669 if (!regs || !regs->regs)
672 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
674 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
675 u64 val = regs->regs[i++];
676 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r, arch), val);
682 #define DEFAULT_TOD_FMT "%F %H:%M:%S"
685 tod_scnprintf(struct perf_script *script, char *buf, int buflen,
688 u64 tod_ns, clockid_ns;
689 struct perf_env *env;
696 if (buflen < 64 || !script)
699 env = &script->session->header.env;
700 if (!env->clock.enabled) {
701 scnprintf(buf, buflen, "disabled");
705 clockid_ns = env->clock.clockid_ns;
706 tod_ns = env->clock.tod_ns;
708 if (timestamp > clockid_ns)
709 tod_ns += timestamp - clockid_ns;
711 tod_ns -= clockid_ns - timestamp;
713 sec = (time_t) (tod_ns / NSEC_PER_SEC);
714 nsec = tod_ns - sec * NSEC_PER_SEC;
716 if (localtime_r(&sec, <ime) == NULL) {
717 scnprintf(buf, buflen, "failed");
719 strftime(date, sizeof(date), DEFAULT_TOD_FMT, <ime);
721 if (symbol_conf.nanosecs) {
722 snprintf(buf, buflen, "%s.%09lu", date, nsec);
724 snprintf(buf, buflen, "%s.%06lu",
725 date, nsec / NSEC_PER_USEC);
732 static int perf_sample__fprintf_iregs(struct perf_sample *sample,
733 struct perf_event_attr *attr, const char *arch, FILE *fp)
735 return perf_sample__fprintf_regs(&sample->intr_regs,
736 attr->sample_regs_intr, arch, fp);
739 static int perf_sample__fprintf_uregs(struct perf_sample *sample,
740 struct perf_event_attr *attr, const char *arch, FILE *fp)
742 return perf_sample__fprintf_regs(&sample->user_regs,
743 attr->sample_regs_user, arch, fp);
746 static int perf_sample__fprintf_start(struct perf_script *script,
747 struct perf_sample *sample,
748 struct thread *thread,
752 struct perf_event_attr *attr = &evsel->core.attr;
754 unsigned long long nsecs;
758 if (PRINT_FIELD(MACHINE_PID) && sample->machine_pid)
759 printed += fprintf(fp, "VM:%5d ", sample->machine_pid);
761 /* Print VCPU only for guest events i.e. with machine_pid */
762 if (PRINT_FIELD(VCPU) && sample->machine_pid)
763 printed += fprintf(fp, "VCPU:%03d ", sample->vcpu);
765 if (PRINT_FIELD(COMM)) {
766 const char *comm = thread ? thread__comm_str(thread) : ":-1";
769 printed += fprintf(fp, "%8.8s ", comm);
770 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
771 printed += fprintf(fp, "%s ", comm);
773 printed += fprintf(fp, "%16s ", comm);
776 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
777 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
778 else if (PRINT_FIELD(PID))
779 printed += fprintf(fp, "%5d ", sample->pid);
780 else if (PRINT_FIELD(TID))
781 printed += fprintf(fp, "%5d ", sample->tid);
783 if (PRINT_FIELD(CPU)) {
785 printed += fprintf(fp, "%3d ", sample->cpu);
787 printed += fprintf(fp, "[%03d] ", sample->cpu);
790 if (PRINT_FIELD(MISC)) {
794 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
797 ret += fprintf(fp, "K");
799 ret += fprintf(fp, "U");
801 ret += fprintf(fp, "H");
802 if (has(GUEST_KERNEL))
803 ret += fprintf(fp, "G");
805 ret += fprintf(fp, "g");
808 case PERF_RECORD_MMAP:
809 case PERF_RECORD_MMAP2:
811 ret += fprintf(fp, "M");
813 case PERF_RECORD_COMM:
815 ret += fprintf(fp, "E");
817 case PERF_RECORD_SWITCH:
818 case PERF_RECORD_SWITCH_CPU_WIDE:
819 if (has(SWITCH_OUT)) {
820 ret += fprintf(fp, "S");
821 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
822 ret += fprintf(fp, "p");
830 ret += fprintf(fp, "%*s", 6 - ret, " ");
834 if (PRINT_FIELD(TOD)) {
835 tod_scnprintf(script, tstr, sizeof(tstr), sample->time);
836 printed += fprintf(fp, "%s ", tstr);
839 if (PRINT_FIELD(TIME)) {
840 u64 t = sample->time;
843 initial_time = sample->time;
844 t = sample->time - initial_time;
845 } else if (deltatime) {
847 t = sample->time - previous_time;
851 previous_time = sample->time;
854 secs = nsecs / NSEC_PER_SEC;
855 nsecs -= secs * NSEC_PER_SEC;
857 if (symbol_conf.nanosecs)
858 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
860 char sample_time[32];
861 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
862 printed += fprintf(fp, "%12s: ", sample_time);
870 mispred_str(struct branch_entry *br)
872 if (!(br->flags.mispred || br->flags.predicted))
875 return br->flags.predicted ? 'P' : 'M';
878 static int print_bstack_flags(FILE *fp, struct branch_entry *br)
880 return fprintf(fp, "/%c/%c/%c/%d/%s ",
882 br->flags.in_tx ? 'X' : '-',
883 br->flags.abort ? 'A' : '-',
885 get_branch_type(br));
888 static int perf_sample__fprintf_brstack(struct perf_sample *sample,
889 struct thread *thread,
890 struct perf_event_attr *attr, FILE *fp)
892 struct branch_stack *br = sample->branch_stack;
893 struct branch_entry *entries = perf_sample__branch_entries(sample);
894 struct addr_location alf, alt;
901 for (i = 0; i < br->nr; i++) {
902 from = entries[i].from;
905 if (PRINT_FIELD(DSO)) {
906 memset(&alf, 0, sizeof(alf));
907 memset(&alt, 0, sizeof(alt));
908 thread__find_map_fb(thread, sample->cpumode, from, &alf);
909 thread__find_map_fb(thread, sample->cpumode, to, &alt);
912 printed += fprintf(fp, " 0x%"PRIx64, from);
913 if (PRINT_FIELD(DSO)) {
914 printed += fprintf(fp, "(");
915 printed += map__fprintf_dsoname(alf.map, fp);
916 printed += fprintf(fp, ")");
919 printed += fprintf(fp, "/0x%"PRIx64, to);
920 if (PRINT_FIELD(DSO)) {
921 printed += fprintf(fp, "(");
922 printed += map__fprintf_dsoname(alt.map, fp);
923 printed += fprintf(fp, ")");
926 printed += print_bstack_flags(fp, entries + i);
932 static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
933 struct thread *thread,
934 struct perf_event_attr *attr, FILE *fp)
936 struct branch_stack *br = sample->branch_stack;
937 struct branch_entry *entries = perf_sample__branch_entries(sample);
938 struct addr_location alf, alt;
945 for (i = 0; i < br->nr; i++) {
947 memset(&alf, 0, sizeof(alf));
948 memset(&alt, 0, sizeof(alt));
949 from = entries[i].from;
952 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
953 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
955 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
956 if (PRINT_FIELD(DSO)) {
957 printed += fprintf(fp, "(");
958 printed += map__fprintf_dsoname(alf.map, fp);
959 printed += fprintf(fp, ")");
961 printed += fprintf(fp, "%c", '/');
962 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
963 if (PRINT_FIELD(DSO)) {
964 printed += fprintf(fp, "(");
965 printed += map__fprintf_dsoname(alt.map, fp);
966 printed += fprintf(fp, ")");
968 printed += print_bstack_flags(fp, entries + i);
974 static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
975 struct thread *thread,
976 struct perf_event_attr *attr, FILE *fp)
978 struct branch_stack *br = sample->branch_stack;
979 struct branch_entry *entries = perf_sample__branch_entries(sample);
980 struct addr_location alf, alt;
987 for (i = 0; i < br->nr; i++) {
989 memset(&alf, 0, sizeof(alf));
990 memset(&alt, 0, sizeof(alt));
991 from = entries[i].from;
994 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
995 !alf.map->dso->adjust_symbols)
996 from = map__map_ip(alf.map, from);
998 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
999 !alt.map->dso->adjust_symbols)
1000 to = map__map_ip(alt.map, to);
1002 printed += fprintf(fp, " 0x%"PRIx64, from);
1003 if (PRINT_FIELD(DSO)) {
1004 printed += fprintf(fp, "(");
1005 printed += map__fprintf_dsoname(alf.map, fp);
1006 printed += fprintf(fp, ")");
1008 printed += fprintf(fp, "/0x%"PRIx64, to);
1009 if (PRINT_FIELD(DSO)) {
1010 printed += fprintf(fp, "(");
1011 printed += map__fprintf_dsoname(alt.map, fp);
1012 printed += fprintf(fp, ")");
1014 printed += print_bstack_flags(fp, entries + i);
1019 #define MAXBB 16384UL
1021 static int grab_bb(u8 *buffer, u64 start, u64 end,
1022 struct machine *machine, struct thread *thread,
1023 bool *is64bit, u8 *cpumode, bool last)
1026 struct addr_location al;
1032 kernel = machine__kernel_ip(machine, start);
1034 *cpumode = PERF_RECORD_MISC_KERNEL;
1036 *cpumode = PERF_RECORD_MISC_USER;
1039 * Block overlaps between kernel and user.
1040 * This can happen due to ring filtering
1041 * On Intel CPUs the entry into the kernel is filtered,
1042 * but the exit is not. Let the caller patch it up.
1044 if (kernel != machine__kernel_ip(machine, end)) {
1045 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
1049 memset(&al, 0, sizeof(al));
1050 if (end - start > MAXBB - MAXINSN) {
1052 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
1054 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
1058 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
1059 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
1062 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
1063 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
1067 /* Load maps to ensure dso->is_64_bit has been updated */
1070 offset = al.map->map_ip(al.map, start);
1071 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
1072 end - start + MAXINSN);
1074 *is64bit = al.map->dso->is_64_bit;
1076 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
1081 static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srccode_state *state)
1089 if (!map || !map->dso)
1091 srcfile = get_srcline_split(map->dso,
1092 map__rip_2objdump(map, addr),
1097 /* Avoid redundant printing */
1100 !strcmp(state->srcfile, srcfile) &&
1101 state->line == line) {
1106 srccode = find_sourceline(srcfile, line, &len);
1110 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
1113 state->srcfile = srcfile;
1123 static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
1125 struct addr_location al;
1128 memset(&al, 0, sizeof(al));
1129 thread__find_map(thread, cpumode, addr, &al);
1132 ret = map__fprintf_srccode(al.map, al.addr, stdout,
1133 &thread->srccode_state);
1135 ret += printf("\n");
1139 static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
1140 struct perf_insn *x, u8 *inbuf, int len,
1141 int insn, FILE *fp, int *total_cycles,
1142 struct perf_event_attr *attr)
1145 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t", ip,
1146 dump_insn(x, ip, inbuf, len, &ilen));
1148 if (PRINT_FIELD(BRSTACKINSNLEN))
1149 printed += fprintf(fp, "ilen: %d\t", ilen);
1151 printed += fprintf(fp, "#%s%s%s%s",
1152 en->flags.predicted ? " PRED" : "",
1153 en->flags.mispred ? " MISPRED" : "",
1154 en->flags.in_tx ? " INTX" : "",
1155 en->flags.abort ? " ABORT" : "");
1156 if (en->flags.cycles) {
1157 *total_cycles += en->flags.cycles;
1158 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
1160 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
1162 return printed + fprintf(fp, "\n");
1165 static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
1166 u8 cpumode, int cpu, struct symbol **lastsym,
1167 struct perf_event_attr *attr, FILE *fp)
1169 struct addr_location al;
1170 int off, printed = 0;
1172 memset(&al, 0, sizeof(al));
1174 thread__find_map(thread, cpumode, addr, &al);
1176 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
1182 al.sym = map__find_symbol(al.map, al.addr);
1187 if (al.addr < al.sym->end)
1188 off = al.addr - al.sym->start;
1190 off = al.addr - al.map->start - al.sym->start;
1191 printed += fprintf(fp, "\t%s", al.sym->name);
1193 printed += fprintf(fp, "%+d", off);
1194 printed += fprintf(fp, ":");
1195 if (PRINT_FIELD(SRCLINE))
1196 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
1197 printed += fprintf(fp, "\n");
1203 static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1204 struct thread *thread,
1205 struct perf_event_attr *attr,
1206 struct machine *machine, FILE *fp)
1208 struct branch_stack *br = sample->branch_stack;
1209 struct branch_entry *entries = perf_sample__branch_entries(sample);
1211 int i, insn, len, nr, ilen, printed = 0;
1215 struct symbol *lastsym = NULL;
1216 int total_cycles = 0;
1218 if (!(br && br->nr))
1221 if (max_blocks && nr > max_blocks + 1)
1222 nr = max_blocks + 1;
1225 x.cpu = sample->cpu;
1227 printed += fprintf(fp, "%c", '\n');
1229 /* Handle first from jump, of which we don't know the entry. */
1230 len = grab_bb(buffer, entries[nr-1].from,
1232 machine, thread, &x.is64bit, &x.cpumode, false);
1234 printed += ip__fprintf_sym(entries[nr - 1].from, thread,
1235 x.cpumode, x.cpu, &lastsym, attr, fp);
1236 printed += ip__fprintf_jump(entries[nr - 1].from, &entries[nr - 1],
1237 &x, buffer, len, 0, fp, &total_cycles,
1239 if (PRINT_FIELD(SRCCODE))
1240 printed += print_srccode(thread, x.cpumode, entries[nr - 1].from);
1243 /* Print all blocks */
1244 for (i = nr - 2; i >= 0; i--) {
1245 if (entries[i].from || entries[i].to)
1246 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1249 start = entries[i + 1].to;
1250 end = entries[i].from;
1252 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1253 /* Patch up missing kernel transfers due to ring filters */
1254 if (len == -ENXIO && i > 0) {
1255 end = entries[--i].from;
1256 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1257 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1263 for (off = 0; off < (unsigned)len; off += ilen) {
1264 uint64_t ip = start + off;
1266 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1268 printed += ip__fprintf_jump(ip, &entries[i], &x, buffer + off, len - off, ++insn, fp,
1269 &total_cycles, attr);
1270 if (PRINT_FIELD(SRCCODE))
1271 printed += print_srccode(thread, x.cpumode, ip);
1275 printed += fprintf(fp, "\t%016" PRIx64 "\t%s", ip,
1276 dump_insn(&x, ip, buffer + off, len - off, &ilen));
1277 if (PRINT_FIELD(BRSTACKINSNLEN))
1278 printed += fprintf(fp, "\tilen: %d", ilen);
1279 printed += fprintf(fp, "\n");
1282 if (PRINT_FIELD(SRCCODE))
1283 print_srccode(thread, x.cpumode, ip);
1287 if (off != end - start)
1288 printed += fprintf(fp, "\tmismatch of LBR data and executable\n");
1292 * Hit the branch? In this case we are already done, and the target
1293 * has not been executed yet.
1295 if (entries[0].from == sample->ip)
1297 if (entries[0].flags.abort)
1301 * Print final block upto sample
1303 * Due to pipeline delays the LBRs might be missing a branch
1304 * or two, which can result in very large or negative blocks
1305 * between final branch and sample. When this happens just
1306 * continue walking after the last TO until we hit a branch.
1308 start = entries[0].to;
1311 /* Missing jump. Scan 128 bytes for the next branch */
1314 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
1315 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1317 /* Print at least last IP if basic block did not work */
1318 len = grab_bb(buffer, sample->ip, sample->ip,
1319 machine, thread, &x.is64bit, &x.cpumode, false);
1323 printed += fprintf(fp, "\t%016" PRIx64 "\t%s", sample->ip,
1324 dump_insn(&x, sample->ip, buffer, len, &ilen));
1325 if (PRINT_FIELD(BRSTACKINSNLEN))
1326 printed += fprintf(fp, "\tilen: %d", ilen);
1327 printed += fprintf(fp, "\n");
1328 if (PRINT_FIELD(SRCCODE))
1329 print_srccode(thread, x.cpumode, sample->ip);
1332 for (off = 0; off <= end - start; off += ilen) {
1334 printed += fprintf(fp, "\t%016" PRIx64 "\t%s", start + off,
1335 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
1336 if (PRINT_FIELD(BRSTACKINSNLEN))
1337 printed += fprintf(fp, "\tilen: %d", ilen);
1338 printed += fprintf(fp, "\n");
1341 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1343 * Hit a missing branch. Just stop.
1345 printed += fprintf(fp, "\t... not reaching sample ...\n");
1348 if (PRINT_FIELD(SRCCODE))
1349 print_srccode(thread, x.cpumode, start + off);
1355 static int perf_sample__fprintf_addr(struct perf_sample *sample,
1356 struct thread *thread,
1357 struct perf_event_attr *attr, FILE *fp)
1359 struct addr_location al;
1360 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
1362 if (!sample_addr_correlates_sym(attr))
1365 thread__resolve(thread, &al, sample);
1367 if (PRINT_FIELD(SYM)) {
1368 printed += fprintf(fp, " ");
1369 if (PRINT_FIELD(SYMOFFSET))
1370 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
1372 printed += symbol__fprintf_symname(al.sym, fp);
1375 if (PRINT_FIELD(DSO)) {
1376 printed += fprintf(fp, " (");
1377 printed += map__fprintf_dsoname(al.map, fp);
1378 printed += fprintf(fp, ")");
1384 static const char *resolve_branch_sym(struct perf_sample *sample,
1385 struct evsel *evsel,
1386 struct thread *thread,
1387 struct addr_location *al,
1388 struct addr_location *addr_al,
1391 struct perf_event_attr *attr = &evsel->core.attr;
1392 const char *name = NULL;
1394 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1395 if (sample_addr_correlates_sym(attr)) {
1396 if (!addr_al->thread)
1397 thread__resolve(thread, addr_al, sample);
1399 name = addr_al->sym->name;
1405 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1407 name = al->sym->name;
1414 static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1415 struct evsel *evsel,
1416 struct thread *thread,
1417 struct addr_location *al,
1418 struct addr_location *addr_al,
1421 struct perf_event_attr *attr = &evsel->core.attr;
1422 size_t depth = thread_stack__depth(thread, sample->cpu);
1423 const char *name = NULL;
1430 * The 'return' has already been popped off the stack so the depth has
1431 * to be adjusted to match the 'call'.
1433 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1436 name = resolve_branch_sym(sample, evsel, thread, al, addr_al, &ip);
1438 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1439 dlen += fprintf(fp, "(");
1440 dlen += map__fprintf_dsoname(al->map, fp);
1441 dlen += fprintf(fp, ")\t");
1445 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
1447 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
1453 * Try to keep the output length from changing frequently so that the
1454 * output lines up more nicely.
1456 if (len > spacing || (len && len < spacing - 52))
1457 spacing = round_up(len + 4, 32);
1460 len += fprintf(fp, "%*s", spacing - len, "");
1465 __weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1466 struct thread *thread __maybe_unused,
1467 struct machine *machine __maybe_unused)
1471 void script_fetch_insn(struct perf_sample *sample, struct thread *thread,
1472 struct machine *machine)
1474 if (sample->insn_len == 0 && native_arch)
1475 arch_fetch_insn(sample, thread, machine);
1478 static int perf_sample__fprintf_insn(struct perf_sample *sample,
1479 struct perf_event_attr *attr,
1480 struct thread *thread,
1481 struct machine *machine, FILE *fp)
1485 script_fetch_insn(sample, thread, machine);
1487 if (PRINT_FIELD(INSNLEN))
1488 printed += fprintf(fp, " ilen: %d", sample->insn_len);
1489 if (PRINT_FIELD(INSN) && sample->insn_len) {
1492 printed += fprintf(fp, " insn:");
1493 for (i = 0; i < sample->insn_len; i++)
1494 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
1496 if (PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN))
1497 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1502 static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1503 struct perf_event_attr *attr, FILE *fp)
1507 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1510 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1512 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1513 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1516 static int perf_sample__fprintf_bts(struct perf_sample *sample,
1517 struct evsel *evsel,
1518 struct thread *thread,
1519 struct addr_location *al,
1520 struct addr_location *addr_al,
1521 struct machine *machine, FILE *fp)
1523 struct perf_event_attr *attr = &evsel->core.attr;
1524 unsigned int type = output_type(attr->type);
1525 bool print_srcline_last = false;
1528 if (PRINT_FIELD(CALLINDENT))
1529 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, addr_al, fp);
1531 /* print branch_from information */
1532 if (PRINT_FIELD(IP)) {
1533 unsigned int print_opts = output[type].print_ip_opts;
1534 struct callchain_cursor *cursor = NULL;
1536 if (symbol_conf.use_callchain && sample->callchain &&
1537 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1538 sample, NULL, NULL, scripting_max_stack) == 0)
1539 cursor = &callchain_cursor;
1541 if (cursor == NULL) {
1542 printed += fprintf(fp, " ");
1543 if (print_opts & EVSEL__PRINT_SRCLINE) {
1544 print_srcline_last = true;
1545 print_opts &= ~EVSEL__PRINT_SRCLINE;
1548 printed += fprintf(fp, "\n");
1550 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor,
1551 symbol_conf.bt_stop_list, fp);
1554 /* print branch_to information */
1555 if (PRINT_FIELD(ADDR) ||
1556 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
1557 !output[type].user_set)) {
1558 printed += fprintf(fp, " => ");
1559 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
1562 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1564 if (print_srcline_last)
1565 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
1567 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1568 printed += fprintf(fp, "\n");
1569 if (PRINT_FIELD(SRCCODE)) {
1570 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1571 &thread->srccode_state);
1574 printed += printf("\n");
1583 } sample_flags[] = {
1584 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1585 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1586 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1587 {PERF_IP_FLAG_BRANCH, "jmp"},
1588 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1589 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1590 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1591 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1592 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1593 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1594 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1595 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1596 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1597 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vmentry"},
1598 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vmexit"},
1602 static const char *sample_flags_to_name(u32 flags)
1606 for (i = 0; sample_flags[i].name ; i++) {
1607 if (sample_flags[i].flags == flags)
1608 return sample_flags[i].name;
1614 int perf_sample__sprintf_flags(u32 flags, char *str, size_t sz)
1616 u32 xf = PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_INTR_DISABLE |
1617 PERF_IP_FLAG_INTR_TOGGLE;
1618 const char *chars = PERF_IP_FLAG_CHARS;
1619 const size_t n = strlen(PERF_IP_FLAG_CHARS);
1620 const char *name = NULL;
1625 snprintf(xs, sizeof(xs), "(%s%s%s)",
1626 flags & PERF_IP_FLAG_IN_TX ? "x" : "",
1627 flags & PERF_IP_FLAG_INTR_DISABLE ? "D" : "",
1628 flags & PERF_IP_FLAG_INTR_TOGGLE ? "t" : "");
1630 name = sample_flags_to_name(flags & ~xf);
1632 return snprintf(str, sz, "%-15s%6s", name, xs);
1634 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1635 name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_BEGIN));
1637 return snprintf(str, sz, "tr strt %-7s%6s", name, xs);
1640 if (flags & PERF_IP_FLAG_TRACE_END) {
1641 name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_END));
1643 return snprintf(str, sz, "tr end %-7s%6s", name, xs);
1646 for (i = 0; i < n; i++, flags >>= 1) {
1647 if ((flags & 1) && pos < sz)
1648 str[pos++] = chars[i];
1650 for (; i < 32; i++, flags >>= 1) {
1651 if ((flags & 1) && pos < sz)
1660 static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1662 char str[SAMPLE_FLAGS_BUF_SIZE];
1664 perf_sample__sprintf_flags(flags, str, sizeof(str));
1665 return fprintf(fp, " %-21s ", str);
1668 struct printer_data {
1674 static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1676 void *extra, FILE *fp)
1678 unsigned char ch = (unsigned char)val;
1679 struct printer_data *printer_data = extra;
1683 case BINARY_PRINT_DATA_BEGIN:
1684 printed += fprintf(fp, "\n");
1686 case BINARY_PRINT_LINE_BEGIN:
1687 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
1690 case BINARY_PRINT_ADDR:
1691 printed += fprintf(fp, " %04x:", val);
1693 case BINARY_PRINT_NUM_DATA:
1694 printed += fprintf(fp, " %02x", val);
1696 case BINARY_PRINT_NUM_PAD:
1697 printed += fprintf(fp, " ");
1699 case BINARY_PRINT_SEP:
1700 printed += fprintf(fp, " ");
1702 case BINARY_PRINT_CHAR_DATA:
1703 if (printer_data->hit_nul && ch)
1704 printer_data->is_printable = false;
1707 printed += fprintf(fp, "%c", '.');
1709 if (!printer_data->is_printable)
1713 printer_data->hit_nul = true;
1715 printer_data->is_printable = false;
1717 printed += fprintf(fp, "%c", ch);
1720 case BINARY_PRINT_CHAR_PAD:
1721 printed += fprintf(fp, " ");
1723 case BINARY_PRINT_LINE_END:
1724 printed += fprintf(fp, "\n");
1725 printer_data->line_no++;
1727 case BINARY_PRINT_DATA_END:
1735 static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
1737 unsigned int nr_bytes = sample->raw_size;
1738 struct printer_data printer_data = {0, false, true};
1739 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1740 sample__fprintf_bpf_output, &printer_data, fp);
1742 if (printer_data.is_printable && printer_data.hit_nul)
1743 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1748 static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
1750 if (len > 0 && len < spacing)
1751 return fprintf(fp, "%*s", spacing - len, "");
1756 static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
1758 return perf_sample__fprintf_spacing(len, 34, fp);
1761 /* If a value contains only printable ASCII characters padded with NULLs */
1762 static bool ptw_is_prt(u64 val)
1767 for (i = 0; i < sizeof(val); i++) {
1768 c = ((char *)&val)[i];
1771 if (!isprint(c) || !isascii(c))
1774 for (; i < sizeof(val); i++) {
1775 c = ((char *)&val)[i];
1782 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
1784 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1785 char str[sizeof(u64) + 1] = "";
1789 if (perf_sample__bad_synth_size(sample, *data))
1792 val = le64_to_cpu(data->payload);
1793 if (ptw_is_prt(val)) {
1794 memcpy(str, &val, sizeof(val));
1795 str[sizeof(val)] = 0;
1797 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " %s ",
1798 data->ip, val, str);
1799 return len + perf_sample__fprintf_pt_spacing(len, fp);
1802 static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
1804 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1807 if (perf_sample__bad_synth_size(sample, *data))
1810 len = fprintf(fp, " hints: %#x extensions: %#x ",
1811 data->hints, data->extensions);
1812 return len + perf_sample__fprintf_pt_spacing(len, fp);
1815 static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
1817 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1820 if (perf_sample__bad_synth_size(sample, *data))
1823 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1824 data->hw, data->cstate, data->subcstate);
1825 return len + perf_sample__fprintf_pt_spacing(len, fp);
1828 static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
1830 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1833 if (perf_sample__bad_synth_size(sample, *data))
1836 len = fprintf(fp, " IP: %u ", data->ip);
1837 return len + perf_sample__fprintf_pt_spacing(len, fp);
1840 static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
1842 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1845 if (perf_sample__bad_synth_size(sample, *data))
1848 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1849 data->deepest_cstate, data->last_cstate,
1851 return len + perf_sample__fprintf_pt_spacing(len, fp);
1854 static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
1856 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1857 unsigned int percent, freq;
1860 if (perf_sample__bad_synth_size(sample, *data))
1863 freq = (le32_to_cpu(data->freq) + 500) / 1000;
1864 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
1865 if (data->max_nonturbo) {
1866 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1867 len += fprintf(fp, "(%3u%%) ", percent);
1869 return len + perf_sample__fprintf_pt_spacing(len, fp);
1872 static int perf_sample__fprintf_synth_psb(struct perf_sample *sample, FILE *fp)
1874 struct perf_synth_intel_psb *data = perf_sample__synth_ptr(sample);
1877 if (perf_sample__bad_synth_size(sample, *data))
1880 len = fprintf(fp, " psb offs: %#" PRIx64, data->offset);
1881 return len + perf_sample__fprintf_pt_spacing(len, fp);
1884 /* Intel PT Event Trace */
1885 static int perf_sample__fprintf_synth_evt(struct perf_sample *sample, FILE *fp)
1887 struct perf_synth_intel_evt *data = perf_sample__synth_ptr(sample);
1888 const char *cfe[32] = {NULL, "INTR", "IRET", "SMI", "RSM", "SIPI",
1889 "INIT", "VMENTRY", "VMEXIT", "VMEXIT_INTR",
1891 const char *evd[64] = {"PFA", "VMXQ", "VMXR"};
1895 if (perf_sample__bad_synth_size(sample, *data))
1898 s = cfe[data->type];
1900 len = fprintf(fp, " cfe: %s IP: %d vector: %u",
1901 s, data->ip, data->vector);
1903 len = fprintf(fp, " cfe: %u IP: %d vector: %u",
1904 data->type, data->ip, data->vector);
1906 for (i = 0; i < data->evd_cnt; i++) {
1907 unsigned int et = data->evd[i].evd_type & 0x3f;
1911 len += fprintf(fp, " %s: %#" PRIx64,
1912 s, data->evd[i].payload);
1914 len += fprintf(fp, " EVD_%u: %#" PRIx64,
1915 et, data->evd[i].payload);
1918 return len + perf_sample__fprintf_pt_spacing(len, fp);
1921 static int perf_sample__fprintf_synth_iflag_chg(struct perf_sample *sample, FILE *fp)
1923 struct perf_synth_intel_iflag_chg *data = perf_sample__synth_ptr(sample);
1926 if (perf_sample__bad_synth_size(sample, *data))
1929 len = fprintf(fp, " IFLAG: %d->%d %s branch", !data->iflag, data->iflag,
1930 data->via_branch ? "via" : "non");
1931 return len + perf_sample__fprintf_pt_spacing(len, fp);
1934 static int perf_sample__fprintf_synth(struct perf_sample *sample,
1935 struct evsel *evsel, FILE *fp)
1937 switch (evsel->core.attr.config) {
1938 case PERF_SYNTH_INTEL_PTWRITE:
1939 return perf_sample__fprintf_synth_ptwrite(sample, fp);
1940 case PERF_SYNTH_INTEL_MWAIT:
1941 return perf_sample__fprintf_synth_mwait(sample, fp);
1942 case PERF_SYNTH_INTEL_PWRE:
1943 return perf_sample__fprintf_synth_pwre(sample, fp);
1944 case PERF_SYNTH_INTEL_EXSTOP:
1945 return perf_sample__fprintf_synth_exstop(sample, fp);
1946 case PERF_SYNTH_INTEL_PWRX:
1947 return perf_sample__fprintf_synth_pwrx(sample, fp);
1948 case PERF_SYNTH_INTEL_CBR:
1949 return perf_sample__fprintf_synth_cbr(sample, fp);
1950 case PERF_SYNTH_INTEL_PSB:
1951 return perf_sample__fprintf_synth_psb(sample, fp);
1952 case PERF_SYNTH_INTEL_EVT:
1953 return perf_sample__fprintf_synth_evt(sample, fp);
1954 case PERF_SYNTH_INTEL_IFLAG_CHG:
1955 return perf_sample__fprintf_synth_iflag_chg(sample, fp);
1963 static int evlist__max_name_len(struct evlist *evlist)
1965 struct evsel *evsel;
1968 evlist__for_each_entry(evlist, evsel) {
1969 int len = strlen(evsel__name(evsel));
1971 max = MAX(len, max);
1977 static int data_src__fprintf(u64 data_src, FILE *fp)
1979 struct mem_info mi = { .data_src.val = data_src };
1985 perf_script__meminfo_scnprintf(decode, 100, &mi);
1987 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1991 return fprintf(fp, "%-*s", maxlen, out);
1995 struct perf_sample *sample;
1996 struct thread *thread;
1997 struct evsel *evsel;
2001 static void script_print_metric(struct perf_stat_config *config __maybe_unused,
2002 void *ctx, const char *color,
2004 const char *unit, double val)
2006 struct metric_ctx *mctx = ctx;
2010 perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
2011 PERF_RECORD_SAMPLE, mctx->fp);
2012 fputs("\tmetric: ", mctx->fp);
2014 color_fprintf(mctx->fp, color, fmt, val);
2017 fprintf(mctx->fp, " %s\n", unit);
2020 static void script_new_line(struct perf_stat_config *config __maybe_unused,
2023 struct metric_ctx *mctx = ctx;
2025 perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
2026 PERF_RECORD_SAMPLE, mctx->fp);
2027 fputs("\tmetric: ", mctx->fp);
2030 static void perf_sample__fprint_metric(struct perf_script *script,
2031 struct thread *thread,
2032 struct evsel *evsel,
2033 struct perf_sample *sample,
2036 struct evsel *leader = evsel__leader(evsel);
2037 struct perf_stat_output_ctx ctx = {
2038 .print_metric = script_print_metric,
2039 .new_line = script_new_line,
2040 .ctx = &(struct metric_ctx) {
2046 .force_header = false,
2052 evlist__alloc_stats(script->session->evlist, false);
2053 if (evsel_script(leader)->gnum++ == 0)
2054 perf_stat__reset_shadow_stats();
2055 val = sample->period * evsel->scale;
2056 perf_stat__update_shadow_stats(evsel,
2060 evsel_script(evsel)->val = val;
2061 if (evsel_script(leader)->gnum == leader->core.nr_members) {
2062 for_each_group_member (ev2, leader) {
2063 perf_stat__print_shadow_stats(&stat_config, ev2,
2064 evsel_script(ev2)->val,
2070 evsel_script(leader)->gnum = 0;
2074 static bool show_event(struct perf_sample *sample,
2075 struct evsel *evsel,
2076 struct thread *thread,
2077 struct addr_location *al,
2078 struct addr_location *addr_al)
2080 int depth = thread_stack__depth(thread, sample->cpu);
2082 if (!symbol_conf.graph_function)
2085 if (thread->filter) {
2086 if (depth <= thread->filter_entry_depth) {
2087 thread->filter = false;
2092 const char *s = symbol_conf.graph_function;
2094 const char *name = resolve_branch_sym(sample, evsel, thread, al, addr_al,
2100 nlen = strlen(name);
2102 unsigned len = strcspn(s, ",");
2103 if (nlen == len && !strncmp(name, s, len)) {
2104 thread->filter = true;
2105 thread->filter_entry_depth = depth;
2116 static void process_event(struct perf_script *script,
2117 struct perf_sample *sample, struct evsel *evsel,
2118 struct addr_location *al,
2119 struct addr_location *addr_al,
2120 struct machine *machine)
2122 struct thread *thread = al->thread;
2123 struct perf_event_attr *attr = &evsel->core.attr;
2124 unsigned int type = output_type(attr->type);
2125 struct evsel_script *es = evsel->priv;
2127 char str[PAGE_SIZE_NAME_LEN];
2128 const char *arch = perf_env__arch(machine->env);
2130 if (output[type].fields == 0)
2135 perf_sample__fprintf_start(script, sample, thread, evsel,
2136 PERF_RECORD_SAMPLE, fp);
2138 if (PRINT_FIELD(PERIOD))
2139 fprintf(fp, "%10" PRIu64 " ", sample->period);
2141 if (PRINT_FIELD(EVNAME)) {
2142 const char *evname = evsel__name(evsel);
2144 if (!script->name_width)
2145 script->name_width = evlist__max_name_len(script->session->evlist);
2147 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
2151 perf_sample__fprintf_flags(sample->flags, fp);
2153 if (is_bts_event(attr)) {
2154 perf_sample__fprintf_bts(sample, evsel, thread, al, addr_al, machine, fp);
2158 if (PRINT_FIELD(TRACE) && sample->raw_data) {
2159 event_format__fprintf(evsel->tp_format, sample->cpu,
2160 sample->raw_data, sample->raw_size, fp);
2163 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
2164 perf_sample__fprintf_synth(sample, evsel, fp);
2166 if (PRINT_FIELD(ADDR))
2167 perf_sample__fprintf_addr(sample, thread, attr, fp);
2169 if (PRINT_FIELD(DATA_SRC))
2170 data_src__fprintf(sample->data_src, fp);
2172 if (PRINT_FIELD(WEIGHT))
2173 fprintf(fp, "%16" PRIu64, sample->weight);
2175 if (PRINT_FIELD(INS_LAT))
2176 fprintf(fp, "%16" PRIu16, sample->ins_lat);
2178 if (PRINT_FIELD(IP)) {
2179 struct callchain_cursor *cursor = NULL;
2181 if (script->stitch_lbr)
2182 al->thread->lbr_stitch_enable = true;
2184 if (symbol_conf.use_callchain && sample->callchain &&
2185 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
2186 sample, NULL, NULL, scripting_max_stack) == 0)
2187 cursor = &callchain_cursor;
2189 fputc(cursor ? '\n' : ' ', fp);
2190 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor,
2191 symbol_conf.bt_stop_list, fp);
2194 if (PRINT_FIELD(IREGS))
2195 perf_sample__fprintf_iregs(sample, attr, arch, fp);
2197 if (PRINT_FIELD(UREGS))
2198 perf_sample__fprintf_uregs(sample, attr, arch, fp);
2200 if (PRINT_FIELD(BRSTACK))
2201 perf_sample__fprintf_brstack(sample, thread, attr, fp);
2202 else if (PRINT_FIELD(BRSTACKSYM))
2203 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
2204 else if (PRINT_FIELD(BRSTACKOFF))
2205 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
2207 if (evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
2208 perf_sample__fprintf_bpf_output(sample, fp);
2209 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
2211 if (PRINT_FIELD(PHYS_ADDR))
2212 fprintf(fp, "%16" PRIx64, sample->phys_addr);
2214 if (PRINT_FIELD(DATA_PAGE_SIZE))
2215 fprintf(fp, " %s", get_page_size_name(sample->data_page_size, str));
2217 if (PRINT_FIELD(CODE_PAGE_SIZE))
2218 fprintf(fp, " %s", get_page_size_name(sample->code_page_size, str));
2220 perf_sample__fprintf_ipc(sample, attr, fp);
2224 if (PRINT_FIELD(SRCCODE)) {
2225 if (map__fprintf_srccode(al->map, al->addr, stdout,
2226 &thread->srccode_state))
2230 if (PRINT_FIELD(METRIC))
2231 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
2237 static struct scripting_ops *scripting_ops;
2239 static void __process_stat(struct evsel *counter, u64 tstamp)
2241 int nthreads = perf_thread_map__nr(counter->core.threads);
2243 struct perf_cpu cpu;
2244 static int header_printed;
2246 if (!header_printed) {
2247 printf("%3s %8s %15s %15s %15s %15s %s\n",
2248 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
2252 for (thread = 0; thread < nthreads; thread++) {
2253 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
2254 struct perf_counts_values *counts;
2256 counts = perf_counts(counter->counts, idx, thread);
2258 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
2260 perf_thread_map__pid(counter->core.threads, thread),
2265 evsel__name(counter));
2270 static void process_stat(struct evsel *counter, u64 tstamp)
2272 if (scripting_ops && scripting_ops->process_stat)
2273 scripting_ops->process_stat(&stat_config, counter, tstamp);
2275 __process_stat(counter, tstamp);
2278 static void process_stat_interval(u64 tstamp)
2280 if (scripting_ops && scripting_ops->process_stat_interval)
2281 scripting_ops->process_stat_interval(tstamp);
2284 static void setup_scripting(void)
2286 setup_perl_scripting();
2287 setup_python_scripting();
2290 static int flush_scripting(void)
2292 return scripting_ops ? scripting_ops->flush_script() : 0;
2295 static int cleanup_scripting(void)
2297 pr_debug("\nperf script stopped\n");
2299 return scripting_ops ? scripting_ops->stop_script() : 0;
2302 static bool filter_cpu(struct perf_sample *sample)
2304 if (cpu_list && sample->cpu != (u32)-1)
2305 return !test_bit(sample->cpu, cpu_bitmap);
2309 static int process_sample_event(struct perf_tool *tool,
2310 union perf_event *event,
2311 struct perf_sample *sample,
2312 struct evsel *evsel,
2313 struct machine *machine)
2315 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2316 struct addr_location al;
2317 struct addr_location addr_al;
2320 /* Set thread to NULL to indicate addr_al and al are not initialized */
2321 addr_al.thread = NULL;
2324 ret = dlfilter__filter_event_early(dlfilter, event, sample, evsel, machine, &al, &addr_al);
2331 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2337 if (sample->time < last_timestamp) {
2338 pr_err("Samples misordered, previous: %" PRIu64
2339 " this: %" PRIu64 "\n", last_timestamp,
2343 last_timestamp = sample->time;
2347 if (filter_cpu(sample))
2350 if (!al.thread && machine__resolve(machine, &al, sample) < 0) {
2351 pr_err("problem processing %d event, skipping it.\n",
2352 event->header.type);
2360 if (!show_event(sample, evsel, al.thread, &al, &addr_al))
2363 if (evswitch__discard(&scr->evswitch, evsel))
2366 ret = dlfilter__filter_event(dlfilter, event, sample, evsel, machine, &al, &addr_al);
2373 if (scripting_ops) {
2374 struct addr_location *addr_al_ptr = NULL;
2376 if ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
2377 sample_addr_correlates_sym(&evsel->core.attr)) {
2378 if (!addr_al.thread)
2379 thread__resolve(al.thread, &addr_al, sample);
2380 addr_al_ptr = &addr_al;
2382 scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr);
2384 process_event(scr, sample, evsel, &al, &addr_al, machine);
2389 addr_location__put(&al);
2393 static int process_attr(struct perf_tool *tool, union perf_event *event,
2394 struct evlist **pevlist)
2396 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2397 struct evlist *evlist;
2398 struct evsel *evsel, *pos;
2401 static struct evsel_script *es;
2403 err = perf_event__process_attr(tool, event, pevlist);
2408 evsel = evlist__last(*pevlist);
2411 if (scr->per_event_dump) {
2412 evsel->priv = evsel_script__new(evsel, scr->session->data);
2414 es = zalloc(sizeof(*es));
2422 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2423 evsel->core.attr.type != PERF_TYPE_SYNTH)
2426 evlist__for_each_entry(evlist, pos) {
2427 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
2431 if (evsel->core.attr.sample_type) {
2432 err = evsel__check_attr(evsel, scr->session);
2438 * Check if we need to enable callchains based
2439 * on events sample_type.
2441 sample_type = evlist__combined_sample_type(evlist);
2442 callchain_param_setup(sample_type, perf_env__arch((*pevlist)->env));
2444 /* Enable fields for callchain entries */
2445 if (symbol_conf.use_callchain &&
2446 (sample_type & PERF_SAMPLE_CALLCHAIN ||
2447 sample_type & PERF_SAMPLE_BRANCH_STACK ||
2448 (sample_type & PERF_SAMPLE_REGS_USER &&
2449 sample_type & PERF_SAMPLE_STACK_USER))) {
2450 int type = output_type(evsel->core.attr.type);
2452 if (!(output[type].user_unset_fields & PERF_OUTPUT_IP))
2453 output[type].fields |= PERF_OUTPUT_IP;
2454 if (!(output[type].user_unset_fields & PERF_OUTPUT_SYM))
2455 output[type].fields |= PERF_OUTPUT_SYM;
2457 set_print_ip_opts(&evsel->core.attr);
2461 static int print_event_with_time(struct perf_tool *tool,
2462 union perf_event *event,
2463 struct perf_sample *sample,
2464 struct machine *machine,
2465 pid_t pid, pid_t tid, u64 timestamp)
2467 struct perf_script *script = container_of(tool, struct perf_script, tool);
2468 struct perf_session *session = script->session;
2469 struct evsel *evsel = evlist__id2evsel(session->evlist, sample->id);
2470 struct thread *thread = NULL;
2472 if (evsel && !evsel->core.attr.sample_id_all) {
2474 sample->time = timestamp;
2479 if (filter_cpu(sample))
2483 thread = machine__findnew_thread(machine, pid, tid);
2486 perf_sample__fprintf_start(script, sample, thread, evsel,
2487 event->header.type, stdout);
2490 perf_event__fprintf(event, machine, stdout);
2492 thread__put(thread);
2497 static int print_event(struct perf_tool *tool, union perf_event *event,
2498 struct perf_sample *sample, struct machine *machine,
2499 pid_t pid, pid_t tid)
2501 return print_event_with_time(tool, event, sample, machine, pid, tid, 0);
2504 static int process_comm_event(struct perf_tool *tool,
2505 union perf_event *event,
2506 struct perf_sample *sample,
2507 struct machine *machine)
2509 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2512 return print_event(tool, event, sample, machine, event->comm.pid,
2516 static int process_namespaces_event(struct perf_tool *tool,
2517 union perf_event *event,
2518 struct perf_sample *sample,
2519 struct machine *machine)
2521 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2524 return print_event(tool, event, sample, machine, event->namespaces.pid,
2525 event->namespaces.tid);
2528 static int process_cgroup_event(struct perf_tool *tool,
2529 union perf_event *event,
2530 struct perf_sample *sample,
2531 struct machine *machine)
2533 if (perf_event__process_cgroup(tool, event, sample, machine) < 0)
2536 return print_event(tool, event, sample, machine, sample->pid,
2540 static int process_fork_event(struct perf_tool *tool,
2541 union perf_event *event,
2542 struct perf_sample *sample,
2543 struct machine *machine)
2545 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2548 return print_event_with_time(tool, event, sample, machine,
2549 event->fork.pid, event->fork.tid,
2552 static int process_exit_event(struct perf_tool *tool,
2553 union perf_event *event,
2554 struct perf_sample *sample,
2555 struct machine *machine)
2557 /* Print before 'exit' deletes anything */
2558 if (print_event_with_time(tool, event, sample, machine, event->fork.pid,
2559 event->fork.tid, event->fork.time))
2562 return perf_event__process_exit(tool, event, sample, machine);
2565 static int process_mmap_event(struct perf_tool *tool,
2566 union perf_event *event,
2567 struct perf_sample *sample,
2568 struct machine *machine)
2570 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2573 return print_event(tool, event, sample, machine, event->mmap.pid,
2577 static int process_mmap2_event(struct perf_tool *tool,
2578 union perf_event *event,
2579 struct perf_sample *sample,
2580 struct machine *machine)
2582 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2585 return print_event(tool, event, sample, machine, event->mmap2.pid,
2589 static int process_switch_event(struct perf_tool *tool,
2590 union perf_event *event,
2591 struct perf_sample *sample,
2592 struct machine *machine)
2594 struct perf_script *script = container_of(tool, struct perf_script, tool);
2596 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2599 if (scripting_ops && scripting_ops->process_switch && !filter_cpu(sample))
2600 scripting_ops->process_switch(event, sample, machine);
2602 if (!script->show_switch_events)
2605 return print_event(tool, event, sample, machine, sample->pid,
2609 static int process_auxtrace_error(struct perf_session *session,
2610 union perf_event *event)
2612 if (scripting_ops && scripting_ops->process_auxtrace_error) {
2613 scripting_ops->process_auxtrace_error(session, event);
2617 return perf_event__process_auxtrace_error(session, event);
2621 process_lost_event(struct perf_tool *tool,
2622 union perf_event *event,
2623 struct perf_sample *sample,
2624 struct machine *machine)
2626 return print_event(tool, event, sample, machine, sample->pid,
2631 process_throttle_event(struct perf_tool *tool __maybe_unused,
2632 union perf_event *event,
2633 struct perf_sample *sample,
2634 struct machine *machine)
2636 if (scripting_ops && scripting_ops->process_throttle)
2637 scripting_ops->process_throttle(event, sample, machine);
2642 process_finished_round_event(struct perf_tool *tool __maybe_unused,
2643 union perf_event *event,
2644 struct ordered_events *oe __maybe_unused)
2647 perf_event__fprintf(event, NULL, stdout);
2652 process_bpf_events(struct perf_tool *tool __maybe_unused,
2653 union perf_event *event,
2654 struct perf_sample *sample,
2655 struct machine *machine)
2657 if (machine__process_ksymbol(machine, event, sample) < 0)
2660 return print_event(tool, event, sample, machine, sample->pid,
2664 static int process_text_poke_events(struct perf_tool *tool,
2665 union perf_event *event,
2666 struct perf_sample *sample,
2667 struct machine *machine)
2669 if (perf_event__process_text_poke(tool, event, sample, machine) < 0)
2672 return print_event(tool, event, sample, machine, sample->pid,
2676 static void sig_handler(int sig __maybe_unused)
2681 static void perf_script__fclose_per_event_dump(struct perf_script *script)
2683 struct evlist *evlist = script->session->evlist;
2684 struct evsel *evsel;
2686 evlist__for_each_entry(evlist, evsel) {
2689 evsel_script__delete(evsel->priv);
2694 static int perf_script__fopen_per_event_dump(struct perf_script *script)
2696 struct evsel *evsel;
2698 evlist__for_each_entry(script->session->evlist, evsel) {
2700 * Already setup? I.e. we may be called twice in cases like
2701 * Intel PT, one for the intel_pt// and dummy events, then
2702 * for the evsels synthesized from the auxtrace info.
2704 * Ses perf_script__process_auxtrace_info.
2706 if (evsel->priv != NULL)
2709 evsel->priv = evsel_script__new(evsel, script->session->data);
2710 if (evsel->priv == NULL)
2711 goto out_err_fclose;
2717 perf_script__fclose_per_event_dump(script);
2721 static int perf_script__setup_per_event_dump(struct perf_script *script)
2723 struct evsel *evsel;
2724 static struct evsel_script es_stdout;
2726 if (script->per_event_dump)
2727 return perf_script__fopen_per_event_dump(script);
2729 es_stdout.fp = stdout;
2731 evlist__for_each_entry(script->session->evlist, evsel)
2732 evsel->priv = &es_stdout;
2737 static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2739 struct evsel *evsel;
2741 evlist__for_each_entry(script->session->evlist, evsel) {
2742 struct evsel_script *es = evsel->priv;
2744 evsel_script__fprintf(es, stdout);
2745 evsel_script__delete(es);
2750 static void perf_script__exit(struct perf_script *script)
2752 perf_thread_map__put(script->threads);
2753 perf_cpu_map__put(script->cpus);
2756 static int __cmd_script(struct perf_script *script)
2760 signal(SIGINT, sig_handler);
2762 perf_stat__init_shadow_stats();
2764 /* override event processing functions */
2765 if (script->show_task_events) {
2766 script->tool.comm = process_comm_event;
2767 script->tool.fork = process_fork_event;
2768 script->tool.exit = process_exit_event;
2770 if (script->show_mmap_events) {
2771 script->tool.mmap = process_mmap_event;
2772 script->tool.mmap2 = process_mmap2_event;
2774 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
2775 script->tool.context_switch = process_switch_event;
2776 if (scripting_ops && scripting_ops->process_auxtrace_error)
2777 script->tool.auxtrace_error = process_auxtrace_error;
2778 if (script->show_namespace_events)
2779 script->tool.namespaces = process_namespaces_event;
2780 if (script->show_cgroup_events)
2781 script->tool.cgroup = process_cgroup_event;
2782 if (script->show_lost_events)
2783 script->tool.lost = process_lost_event;
2784 if (script->show_round_events) {
2785 script->tool.ordered_events = false;
2786 script->tool.finished_round = process_finished_round_event;
2788 if (script->show_bpf_events) {
2789 script->tool.ksymbol = process_bpf_events;
2790 script->tool.bpf = process_bpf_events;
2792 if (script->show_text_poke_events) {
2793 script->tool.ksymbol = process_bpf_events;
2794 script->tool.text_poke = process_text_poke_events;
2797 if (perf_script__setup_per_event_dump(script)) {
2798 pr_err("Couldn't create the per event dump files\n");
2802 ret = perf_session__process_events(script->session);
2804 if (script->per_event_dump)
2805 perf_script__exit_per_event_dump_stats(script);
2808 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
2813 struct script_spec {
2814 struct list_head node;
2815 struct scripting_ops *ops;
2819 static LIST_HEAD(script_specs);
2821 static struct script_spec *script_spec__new(const char *spec,
2822 struct scripting_ops *ops)
2824 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2827 strcpy(s->spec, spec);
2834 static void script_spec__add(struct script_spec *s)
2836 list_add_tail(&s->node, &script_specs);
2839 static struct script_spec *script_spec__find(const char *spec)
2841 struct script_spec *s;
2843 list_for_each_entry(s, &script_specs, node)
2844 if (strcasecmp(s->spec, spec) == 0)
2849 int script_spec_register(const char *spec, struct scripting_ops *ops)
2851 struct script_spec *s;
2853 s = script_spec__find(spec);
2857 s = script_spec__new(spec, ops);
2861 script_spec__add(s);
2866 static struct scripting_ops *script_spec__lookup(const char *spec)
2868 struct script_spec *s = script_spec__find(spec);
2875 static void list_available_languages(void)
2877 struct script_spec *s;
2879 fprintf(stderr, "\n");
2880 fprintf(stderr, "Scripting language extensions (used in "
2881 "perf script -s [spec:]script.[spec]):\n\n");
2883 list_for_each_entry(s, &script_specs, node)
2884 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2886 fprintf(stderr, "\n");
2889 /* Find script file relative to current directory or exec path */
2890 static char *find_script(const char *script)
2892 char path[PATH_MAX];
2894 if (!scripting_ops) {
2895 const char *ext = strrchr(script, '.');
2900 scripting_ops = script_spec__lookup(++ext);
2905 if (access(script, R_OK)) {
2906 char *exec_path = get_argv_exec_path();
2910 snprintf(path, sizeof(path), "%s/scripts/%s/%s",
2911 exec_path, scripting_ops->dirname, script);
2914 if (access(script, R_OK))
2917 return strdup(script);
2920 static int parse_scriptname(const struct option *opt __maybe_unused,
2921 const char *str, int unset __maybe_unused)
2923 char spec[PATH_MAX];
2924 const char *script, *ext;
2927 if (strcmp(str, "lang") == 0) {
2928 list_available_languages();
2932 script = strchr(str, ':');
2935 if (len >= PATH_MAX) {
2936 fprintf(stderr, "invalid language specifier");
2939 strncpy(spec, str, len);
2941 scripting_ops = script_spec__lookup(spec);
2942 if (!scripting_ops) {
2943 fprintf(stderr, "invalid language specifier");
2949 ext = strrchr(script, '.');
2951 fprintf(stderr, "invalid script extension");
2954 scripting_ops = script_spec__lookup(++ext);
2955 if (!scripting_ops) {
2956 fprintf(stderr, "invalid script extension");
2961 script_name = find_script(script);
2963 script_name = strdup(script);
2968 static int parse_output_fields(const struct option *opt __maybe_unused,
2969 const char *arg, int unset __maybe_unused)
2971 char *tok, *strtok_saveptr = NULL;
2972 int i, imax = ARRAY_SIZE(all_output_options);
2975 char *str = strdup(arg);
2977 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
2982 /* first word can state for which event type the user is specifying
2983 * the fields. If no type exists, the specified fields apply to all
2984 * event types found in the file minus the invalid fields for a type.
2986 tok = strchr(str, ':');
2990 if (!strcmp(str, "hw"))
2991 type = PERF_TYPE_HARDWARE;
2992 else if (!strcmp(str, "sw"))
2993 type = PERF_TYPE_SOFTWARE;
2994 else if (!strcmp(str, "trace"))
2995 type = PERF_TYPE_TRACEPOINT;
2996 else if (!strcmp(str, "raw"))
2997 type = PERF_TYPE_RAW;
2998 else if (!strcmp(str, "break"))
2999 type = PERF_TYPE_BREAKPOINT;
3000 else if (!strcmp(str, "synth"))
3001 type = OUTPUT_TYPE_SYNTH;
3003 fprintf(stderr, "Invalid event type in field string.\n");
3008 if (output[type].user_set)
3009 pr_warning("Overriding previous field request for %s events.\n",
3012 /* Don't override defaults for +- */
3013 if (strchr(tok, '+') || strchr(tok, '-'))
3016 output[type].fields = 0;
3017 output[type].user_set = true;
3018 output[type].wildcard_set = false;
3022 if (strlen(str) == 0) {
3024 "Cannot set fields to 'none' for all event types.\n");
3029 /* Don't override defaults for +- */
3030 if (strchr(str, '+') || strchr(str, '-'))
3033 if (output_set_by_user())
3034 pr_warning("Overriding previous field request for all events.\n");
3036 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
3037 output[j].fields = 0;
3038 output[j].user_set = true;
3039 output[j].wildcard_set = true;
3044 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
3050 } else if (*tok == '-') {
3056 if (change != SET && change != DEFAULT)
3061 for (i = 0; i < imax; ++i) {
3062 if (strcmp(tok, all_output_options[i].str) == 0)
3065 if (i == imax && strcmp(tok, "flags") == 0) {
3066 print_flags = change != REMOVE;
3070 fprintf(stderr, "Invalid field requested.\n");
3076 /* add user option to all events types for
3079 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
3080 if (output[j].invalid_fields & all_output_options[i].field) {
3081 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
3082 all_output_options[i].str, event_type(j));
3084 if (change == REMOVE) {
3085 output[j].fields &= ~all_output_options[i].field;
3086 output[j].user_set_fields &= ~all_output_options[i].field;
3087 output[j].user_unset_fields |= all_output_options[i].field;
3089 output[j].fields |= all_output_options[i].field;
3090 output[j].user_set_fields |= all_output_options[i].field;
3091 output[j].user_unset_fields &= ~all_output_options[i].field;
3093 output[j].user_set = true;
3094 output[j].wildcard_set = true;
3098 if (output[type].invalid_fields & all_output_options[i].field) {
3099 fprintf(stderr, "\'%s\' not valid for %s events.\n",
3100 all_output_options[i].str, event_type(type));
3105 if (change == REMOVE)
3106 output[type].fields &= ~all_output_options[i].field;
3108 output[type].fields |= all_output_options[i].field;
3109 output[type].user_set = true;
3110 output[type].wildcard_set = true;
3115 if (output[type].fields == 0) {
3116 pr_debug("No fields requested for %s type. "
3117 "Events will not be displayed.\n", event_type(type));
3123 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
3130 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
3131 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
3132 if ((lang_dirent->d_type == DT_DIR || \
3133 (lang_dirent->d_type == DT_UNKNOWN && \
3134 is_directory(scripts_path, lang_dirent))) && \
3135 (strcmp(lang_dirent->d_name, ".")) && \
3136 (strcmp(lang_dirent->d_name, "..")))
3138 #define for_each_script(lang_path, lang_dir, script_dirent) \
3139 while ((script_dirent = readdir(lang_dir)) != NULL) \
3140 if (script_dirent->d_type != DT_DIR && \
3141 (script_dirent->d_type != DT_UNKNOWN || \
3142 !is_directory(lang_path, script_dirent)))
3145 #define RECORD_SUFFIX "-record"
3146 #define REPORT_SUFFIX "-report"
3148 struct script_desc {
3149 struct list_head node;
3155 static LIST_HEAD(script_descs);
3157 static struct script_desc *script_desc__new(const char *name)
3159 struct script_desc *s = zalloc(sizeof(*s));
3161 if (s != NULL && name)
3162 s->name = strdup(name);
3167 static void script_desc__delete(struct script_desc *s)
3170 zfree(&s->half_liner);
3175 static void script_desc__add(struct script_desc *s)
3177 list_add_tail(&s->node, &script_descs);
3180 static struct script_desc *script_desc__find(const char *name)
3182 struct script_desc *s;
3184 list_for_each_entry(s, &script_descs, node)
3185 if (strcasecmp(s->name, name) == 0)
3190 static struct script_desc *script_desc__findnew(const char *name)
3192 struct script_desc *s = script_desc__find(name);
3197 s = script_desc__new(name);
3201 script_desc__add(s);
3206 static const char *ends_with(const char *str, const char *suffix)
3208 size_t suffix_len = strlen(suffix);
3209 const char *p = str;
3211 if (strlen(str) > suffix_len) {
3212 p = str + strlen(str) - suffix_len;
3213 if (!strncmp(p, suffix, suffix_len))
3220 static int read_script_info(struct script_desc *desc, const char *filename)
3222 char line[BUFSIZ], *p;
3225 fp = fopen(filename, "r");
3229 while (fgets(line, sizeof(line), fp)) {
3230 p = skip_spaces(line);
3236 if (strlen(p) && *p == '!')
3240 if (strlen(p) && p[strlen(p) - 1] == '\n')
3241 p[strlen(p) - 1] = '\0';
3243 if (!strncmp(p, "description:", strlen("description:"))) {
3244 p += strlen("description:");
3245 desc->half_liner = strdup(skip_spaces(p));
3249 if (!strncmp(p, "args:", strlen("args:"))) {
3250 p += strlen("args:");
3251 desc->args = strdup(skip_spaces(p));
3261 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
3263 char *script_root, *str;
3265 script_root = strdup(script_dirent->d_name);
3269 str = (char *)ends_with(script_root, suffix);
3279 static int list_available_scripts(const struct option *opt __maybe_unused,
3280 const char *s __maybe_unused,
3281 int unset __maybe_unused)
3283 struct dirent *script_dirent, *lang_dirent;
3284 char scripts_path[MAXPATHLEN];
3285 DIR *scripts_dir, *lang_dir;
3286 char script_path[MAXPATHLEN];
3287 char lang_path[MAXPATHLEN];
3288 struct script_desc *desc;
3289 char first_half[BUFSIZ];
3292 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3294 scripts_dir = opendir(scripts_path);
3297 "open(%s) failed.\n"
3298 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
3303 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3304 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3305 lang_dirent->d_name);
3306 lang_dir = opendir(lang_path);
3310 for_each_script(lang_path, lang_dir, script_dirent) {
3311 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
3313 desc = script_desc__findnew(script_root);
3314 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3315 lang_path, script_dirent->d_name);
3316 read_script_info(desc, script_path);
3322 fprintf(stdout, "List of available trace scripts:\n");
3323 list_for_each_entry(desc, &script_descs, node) {
3324 sprintf(first_half, "%s %s", desc->name,
3325 desc->args ? desc->args : "");
3326 fprintf(stdout, " %-36s %s\n", first_half,
3327 desc->half_liner ? desc->half_liner : "");
3333 static int add_dlarg(const struct option *opt __maybe_unused,
3334 const char *s, int unset __maybe_unused)
3336 char *arg = strdup(s);
3342 a = realloc(dlargv, sizeof(dlargv[0]) * (dlargc + 1));
3349 dlargv[dlargc++] = arg;
3354 static void free_dlarg(void)
3357 free(dlargv[dlargc]);
3362 * Some scripts specify the required events in their "xxx-record" file,
3363 * this function will check if the events in perf.data match those
3364 * mentioned in the "xxx-record".
3366 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3367 * which is covered well now. And new parsing code should be added to
3368 * cover the future complex formats like event groups etc.
3370 static int check_ev_match(char *dir_name, char *scriptname,
3371 struct perf_session *session)
3373 char filename[MAXPATHLEN], evname[128];
3374 char line[BUFSIZ], *p;
3379 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
3381 fp = fopen(filename, "r");
3385 while (fgets(line, sizeof(line), fp)) {
3386 p = skip_spaces(line);
3391 p = strstr(p, "-e");
3397 len = strcspn(p, " \t");
3401 snprintf(evname, len + 1, "%s", p);
3404 evlist__for_each_entry(session->evlist, pos) {
3405 if (!strcmp(evsel__name(pos), evname)) {
3423 * Return -1 if none is found, otherwise the actual scripts number.
3425 * Currently the only user of this function is the script browser, which
3426 * will list all statically runnable scripts, select one, execute it and
3427 * show the output in a perf browser.
3429 int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3432 struct dirent *script_dirent, *lang_dirent;
3433 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
3434 DIR *scripts_dir, *lang_dir;
3435 struct perf_session *session;
3436 struct perf_data data = {
3438 .mode = PERF_DATA_MODE_READ,
3443 session = perf_session__new(&data, NULL);
3444 if (IS_ERR(session))
3445 return PTR_ERR(session);
3447 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3449 scripts_dir = opendir(scripts_path);
3451 perf_session__delete(session);
3455 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3456 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3457 lang_dirent->d_name);
3458 #ifndef HAVE_LIBPERL_SUPPORT
3459 if (strstr(lang_path, "perl"))
3462 #ifndef HAVE_LIBPYTHON_SUPPORT
3463 if (strstr(lang_path, "python"))
3467 lang_dir = opendir(lang_path);
3471 for_each_script(lang_path, lang_dir, script_dirent) {
3472 /* Skip those real time scripts: xxxtop.p[yl] */
3473 if (strstr(script_dirent->d_name, "top."))
3477 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3479 script_dirent->d_name);
3480 temp = strchr(script_dirent->d_name, '.');
3481 snprintf(scripts_array[i],
3482 (temp - script_dirent->d_name) + 1,
3483 "%s", script_dirent->d_name);
3485 if (check_ev_match(lang_path,
3486 scripts_array[i], session))
3494 closedir(scripts_dir);
3495 perf_session__delete(session);
3499 static char *get_script_path(const char *script_root, const char *suffix)
3501 struct dirent *script_dirent, *lang_dirent;
3502 char scripts_path[MAXPATHLEN];
3503 char script_path[MAXPATHLEN];
3504 DIR *scripts_dir, *lang_dir;
3505 char lang_path[MAXPATHLEN];
3506 char *__script_root;
3508 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3510 scripts_dir = opendir(scripts_path);
3514 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3515 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3516 lang_dirent->d_name);
3517 lang_dir = opendir(lang_path);
3521 for_each_script(lang_path, lang_dir, script_dirent) {
3522 __script_root = get_script_root(script_dirent, suffix);
3523 if (__script_root && !strcmp(script_root, __script_root)) {
3524 free(__script_root);
3525 closedir(scripts_dir);
3526 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3527 lang_path, script_dirent->d_name);
3529 return strdup(script_path);
3531 free(__script_root);
3535 closedir(scripts_dir);
3540 static bool is_top_script(const char *script_path)
3542 return ends_with(script_path, "top") != NULL;
3545 static int has_required_arg(char *script_path)
3547 struct script_desc *desc;
3551 desc = script_desc__new(NULL);
3553 if (read_script_info(desc, script_path))
3559 for (p = desc->args; *p; p++)
3563 script_desc__delete(desc);
3568 static int have_cmd(int argc, const char **argv)
3570 char **__argv = malloc(sizeof(const char *) * argc);
3573 pr_err("malloc failed\n");
3577 memcpy(__argv, argv, sizeof(const char *) * argc);
3578 argc = parse_options(argc, (const char **)__argv, record_options,
3579 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3582 system_wide = (argc == 0);
3587 static void script__setup_sample_type(struct perf_script *script)
3589 struct perf_session *session = script->session;
3590 u64 sample_type = evlist__combined_sample_type(session->evlist);
3592 callchain_param_setup(sample_type, perf_env__arch(session->machines.host.env));
3594 if (script->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) {
3595 pr_warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
3596 "Please apply --call-graph lbr when recording.\n");
3597 script->stitch_lbr = false;
3601 static int process_stat_round_event(struct perf_session *session,
3602 union perf_event *event)
3604 struct perf_record_stat_round *round = &event->stat_round;
3605 struct evsel *counter;
3607 evlist__for_each_entry(session->evlist, counter) {
3608 perf_stat_process_counter(&stat_config, counter);
3609 process_stat(counter, round->time);
3612 process_stat_interval(round->time);
3616 static int process_stat_config_event(struct perf_session *session __maybe_unused,
3617 union perf_event *event)
3619 perf_event__read_stat_config(&stat_config, &event->stat_config);
3623 static int set_maps(struct perf_script *script)
3625 struct evlist *evlist = script->session->evlist;
3627 if (!script->cpus || !script->threads)
3630 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3633 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
3635 if (evlist__alloc_stats(evlist, true))
3638 script->allocated = true;
3643 int process_thread_map_event(struct perf_session *session,
3644 union perf_event *event)
3646 struct perf_tool *tool = session->tool;
3647 struct perf_script *script = container_of(tool, struct perf_script, tool);
3650 perf_event__fprintf_thread_map(event, stdout);
3652 if (script->threads) {
3653 pr_warning("Extra thread map event, ignoring.\n");
3657 script->threads = thread_map__new_event(&event->thread_map);
3658 if (!script->threads)
3661 return set_maps(script);
3665 int process_cpu_map_event(struct perf_session *session,
3666 union perf_event *event)
3668 struct perf_tool *tool = session->tool;
3669 struct perf_script *script = container_of(tool, struct perf_script, tool);
3672 perf_event__fprintf_cpu_map(event, stdout);
3675 pr_warning("Extra cpu map event, ignoring.\n");
3679 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3683 return set_maps(script);
3686 static int process_feature_event(struct perf_session *session,
3687 union perf_event *event)
3689 if (event->feat.feat_id < HEADER_LAST_FEATURE)
3690 return perf_event__process_feature(session, event);
3694 #ifdef HAVE_AUXTRACE_SUPPORT
3695 static int perf_script__process_auxtrace_info(struct perf_session *session,
3696 union perf_event *event)
3698 struct perf_tool *tool = session->tool;
3700 int ret = perf_event__process_auxtrace_info(session, event);
3703 struct perf_script *script = container_of(tool, struct perf_script, tool);
3705 ret = perf_script__setup_per_event_dump(script);
3711 #define perf_script__process_auxtrace_info 0
3714 static int parse_insn_trace(const struct option *opt __maybe_unused,
3715 const char *str __maybe_unused,
3716 int unset __maybe_unused)
3718 parse_output_fields(NULL, "+insn,-event,-period", 0);
3719 itrace_parse_synth_opts(opt, "i0ns", 0);
3720 symbol_conf.nanosecs = true;
3724 static int parse_xed(const struct option *opt __maybe_unused,
3725 const char *str __maybe_unused,
3726 int unset __maybe_unused)
3729 force_pager("xed -F insn: -A -64 | less");
3731 force_pager("xed -F insn: -A -64");
3735 static int parse_call_trace(const struct option *opt __maybe_unused,
3736 const char *str __maybe_unused,
3737 int unset __maybe_unused)
3739 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3740 itrace_parse_synth_opts(opt, "cewp", 0);
3741 symbol_conf.nanosecs = true;
3742 symbol_conf.pad_output_len_dso = 50;
3746 static int parse_callret_trace(const struct option *opt __maybe_unused,
3747 const char *str __maybe_unused,
3748 int unset __maybe_unused)
3750 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3751 itrace_parse_synth_opts(opt, "crewp", 0);
3752 symbol_conf.nanosecs = true;
3756 int cmd_script(int argc, const char **argv)
3758 bool show_full_info = false;
3759 bool header = false;
3760 bool header_only = false;
3761 bool script_started = false;
3762 bool unsorted_dump = false;
3763 char *rec_script_path = NULL;
3764 char *rep_script_path = NULL;
3765 struct perf_session *session;
3766 struct itrace_synth_opts itrace_synth_opts = {
3768 .default_no_sample = true,
3771 char *script_path = NULL;
3772 const char *dlfilter_file = NULL;
3773 const char **__argv;
3775 struct perf_script script = {
3777 .sample = process_sample_event,
3778 .mmap = perf_event__process_mmap,
3779 .mmap2 = perf_event__process_mmap2,
3780 .comm = perf_event__process_comm,
3781 .namespaces = perf_event__process_namespaces,
3782 .cgroup = perf_event__process_cgroup,
3783 .exit = perf_event__process_exit,
3784 .fork = perf_event__process_fork,
3785 .attr = process_attr,
3786 .event_update = perf_event__process_event_update,
3787 .tracing_data = perf_event__process_tracing_data,
3788 .feature = process_feature_event,
3789 .build_id = perf_event__process_build_id,
3790 .id_index = perf_event__process_id_index,
3791 .auxtrace_info = perf_script__process_auxtrace_info,
3792 .auxtrace = perf_event__process_auxtrace,
3793 .auxtrace_error = perf_event__process_auxtrace_error,
3794 .stat = perf_event__process_stat_event,
3795 .stat_round = process_stat_round_event,
3796 .stat_config = process_stat_config_event,
3797 .thread_map = process_thread_map_event,
3798 .cpu_map = process_cpu_map_event,
3799 .throttle = process_throttle_event,
3800 .unthrottle = process_throttle_event,
3801 .ordered_events = true,
3802 .ordering_requires_timestamps = true,
3805 struct perf_data data = {
3806 .mode = PERF_DATA_MODE_READ,
3808 const struct option options[] = {
3809 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3810 "dump raw trace in ASCII"),
3811 OPT_BOOLEAN(0, "dump-unsorted-raw-trace", &unsorted_dump,
3812 "dump unsorted raw trace in ASCII"),
3813 OPT_INCR('v', "verbose", &verbose,
3814 "be more verbose (show symbol address, etc)"),
3815 OPT_BOOLEAN('L', "Latency", &latency_format,
3816 "show latency attributes (irqs/preemption disabled, etc)"),
3817 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3818 list_available_scripts),
3819 OPT_CALLBACK_NOOPT(0, "list-dlfilters", NULL, NULL, "list available dlfilters",
3820 list_available_dlfilters),
3821 OPT_CALLBACK('s', "script", NULL, "name",
3822 "script file name (lang:script name, script name, or *)",
3824 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3825 "generate perf-script.xx script in specified language"),
3826 OPT_STRING(0, "dlfilter", &dlfilter_file, "file", "filter .so file name"),
3827 OPT_CALLBACK(0, "dlarg", NULL, "argument", "filter argument",
3829 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3830 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3831 "do various checks like samples ordering and lost events"),
3832 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3833 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
3834 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3835 "file", "vmlinux pathname"),
3836 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3837 "file", "kallsyms pathname"),
3838 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3839 "When printing symbols do not display call chain"),
3840 OPT_CALLBACK(0, "symfs", NULL, "directory",
3841 "Look for files with symbols relative to this directory",
3842 symbol__config_symfs),
3843 OPT_CALLBACK('F', "fields", NULL, "str",
3844 "comma separated output fields prepend with 'type:'. "
3845 "+field to add and -field to remove."
3846 "Valid types: hw,sw,trace,raw,synth. "
3847 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
3848 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3849 "brstacksym,flags,data_src,weight,bpf-output,brstackinsn,"
3850 "brstackinsnlen,brstackoff,callindent,insn,insnlen,synth,"
3851 "phys_addr,metric,misc,srccode,ipc,tod,data_page_size,"
3852 "code_page_size,ins_lat",
3853 parse_output_fields),
3854 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3855 "system-wide collection from all CPUs"),
3856 OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
3857 "only consider symbols in these DSOs"),
3858 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3859 "only consider these symbols"),
3860 OPT_INTEGER(0, "addr-range", &symbol_conf.addr_range,
3861 "Use with -S to list traced records within address range"),
3862 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3863 "Decode instructions from itrace", parse_insn_trace),
3864 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3865 "Run xed disassembler on output", parse_xed),
3866 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3867 "Decode calls from itrace", parse_call_trace),
3868 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3869 "Decode calls and returns from itrace", parse_callret_trace),
3870 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3871 "Only print symbols and callees with --call-trace/--call-ret-trace"),
3872 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3873 "Stop display of callgraph at these symbols"),
3874 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3875 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3876 "only display events for these comms"),
3877 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3878 "only consider symbols in these pids"),
3879 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3880 "only consider symbols in these tids"),
3881 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3882 "Set the maximum stack depth when parsing the callchain, "
3883 "anything beyond the specified depth will be ignored. "
3884 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3885 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
3886 OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
3887 OPT_BOOLEAN('I', "show-info", &show_full_info,
3888 "display extended information from perf.data file"),
3889 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3890 "Show the path of [kernel.kallsyms]"),
3891 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3892 "Show the fork/comm/exit events"),
3893 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3894 "Show the mmap events"),
3895 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3896 "Show context switch events (if recorded)"),
3897 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3898 "Show namespace events (if recorded)"),
3899 OPT_BOOLEAN('\0', "show-cgroup-events", &script.show_cgroup_events,
3900 "Show cgroup events (if recorded)"),
3901 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3902 "Show lost events (if recorded)"),
3903 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3904 "Show round events (if recorded)"),
3905 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3906 "Show bpf related events (if recorded)"),
3907 OPT_BOOLEAN('\0', "show-text-poke-events", &script.show_text_poke_events,
3908 "Show text poke related events (if recorded)"),
3909 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3910 "Dump trace output to files named by the monitored events"),
3911 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
3912 OPT_INTEGER(0, "max-blocks", &max_blocks,
3913 "Maximum number of code blocks to dump with brstackinsn"),
3914 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
3915 "Use 9 decimal places when displaying time"),
3916 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3917 "Instruction Tracing options\n" ITRACE_HELP,
3918 itrace_parse_synth_opts),
3919 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3920 "Show full source file name path for source lines"),
3921 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3922 "Enable symbol demangling"),
3923 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3924 "Enable kernel symbol demangling"),
3925 OPT_STRING(0, "time", &script.time_str, "str",
3926 "Time span of interest (start,stop)"),
3927 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3928 "Show inline function"),
3929 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3930 "guest mount directory under which every guest os"
3931 " instance has a subdir"),
3932 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3933 "file", "file saving guest os vmlinux"),
3934 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3935 "file", "file saving guest os /proc/kallsyms"),
3936 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3937 "file", "file saving guest os /proc/modules"),
3938 OPT_BOOLEAN(0, "guest-code", &symbol_conf.guest_code,
3939 "Guest code can be found in hypervisor process"),
3940 OPT_BOOLEAN('\0', "stitch-lbr", &script.stitch_lbr,
3941 "Enable LBR callgraph stitching approach"),
3942 OPTS_EVSWITCH(&script.evswitch),
3945 const char * const script_subcommands[] = { "record", "report", NULL };
3946 const char *script_usage[] = {
3947 "perf script [<options>]",
3948 "perf script [<options>] record <script> [<record-options>] <command>",
3949 "perf script [<options>] report <script> [script-args]",
3950 "perf script [<options>] <script> [<record-options>] <command>",
3951 "perf script [<options>] <top-script> [script-args]",
3955 perf_set_singlethreaded();
3959 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
3960 PARSE_OPT_STOP_AT_NON_OPTION);
3962 if (symbol_conf.guestmount ||
3963 symbol_conf.default_guest_vmlinux_name ||
3964 symbol_conf.default_guest_kallsyms ||
3965 symbol_conf.default_guest_modules ||
3966 symbol_conf.guest_code) {
3968 * Enable guest sample processing.
3973 data.path = input_name;
3974 data.force = symbol_conf.force;
3976 if (unsorted_dump) {
3978 script.tool.ordered_events = false;
3981 if (symbol__validate_sym_arguments())
3984 if (argc > 1 && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
3985 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3986 if (!rec_script_path)
3987 return cmd_record(argc, argv);
3990 if (argc > 1 && strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
3991 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3992 if (!rep_script_path) {
3994 "Please specify a valid report script"
3995 "(see 'perf script -l' for listing)\n");
4000 if (reltime && deltatime) {
4002 "reltime and deltatime - the two don't get along well. "
4003 "Please limit to --reltime or --deltatime.\n");
4007 if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
4008 itrace_synth_opts.callchain_sz > scripting_max_stack)
4009 scripting_max_stack = itrace_synth_opts.callchain_sz;
4011 /* make sure PERF_EXEC_PATH is set for scripts */
4012 set_argv_exec_path(get_argv_exec_path());
4014 if (argc && !script_name && !rec_script_path && !rep_script_path) {
4019 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
4020 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
4022 if (!rec_script_path && !rep_script_path) {
4023 script_name = find_script(argv[0]);
4029 usage_with_options_msg(script_usage, options,
4030 "Couldn't find script `%s'\n\n See perf"
4031 " script -l for available scripts.\n", argv[0]);
4034 if (is_top_script(argv[0])) {
4035 rep_args = argc - 1;
4039 rep_args = has_required_arg(rep_script_path);
4040 rec_args = (argc - 1) - rep_args;
4042 usage_with_options_msg(script_usage, options,
4043 "`%s' script requires options."
4044 "\n\n See perf script -l for available "
4045 "scripts and options.\n", argv[0]);
4049 if (pipe(live_pipe) < 0) {
4050 perror("failed to create pipe");
4056 perror("failed to fork");
4063 dup2(live_pipe[1], 1);
4064 close(live_pipe[0]);
4066 if (is_top_script(argv[0])) {
4068 } else if (!system_wide) {
4069 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
4075 __argv = malloc((argc + 6) * sizeof(const char *));
4077 pr_err("malloc failed\n");
4082 __argv[j++] = "/bin/sh";
4083 __argv[j++] = rec_script_path;
4089 for (i = rep_args + 1; i < argc; i++)
4090 __argv[j++] = argv[i];
4093 execvp("/bin/sh", (char **)__argv);
4098 dup2(live_pipe[0], 0);
4099 close(live_pipe[1]);
4101 __argv = malloc((argc + 4) * sizeof(const char *));
4103 pr_err("malloc failed\n");
4109 __argv[j++] = "/bin/sh";
4110 __argv[j++] = rep_script_path;
4111 for (i = 1; i < rep_args + 1; i++)
4112 __argv[j++] = argv[i];
4117 execvp("/bin/sh", (char **)__argv);
4122 if (rec_script_path)
4123 script_path = rec_script_path;
4124 if (rep_script_path)
4125 script_path = rep_script_path;
4130 if (!rec_script_path)
4131 system_wide = false;
4132 else if (!system_wide) {
4133 if (have_cmd(argc - 1, &argv[1]) != 0) {
4139 __argv = malloc((argc + 2) * sizeof(const char *));
4141 pr_err("malloc failed\n");
4146 __argv[j++] = "/bin/sh";
4147 __argv[j++] = script_path;
4150 for (i = 2; i < argc; i++)
4151 __argv[j++] = argv[i];
4154 execvp("/bin/sh", (char **)__argv);
4159 if (dlfilter_file) {
4160 dlfilter = dlfilter__new(dlfilter_file, dlargc, dlargv);
4170 session = perf_session__new(&data, &script.tool);
4171 if (IS_ERR(session))
4172 return PTR_ERR(session);
4174 if (header || header_only) {
4175 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
4176 perf_session__fprintf_info(session, stdout, show_full_info);
4181 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
4183 if (symbol__init(&session->header.env) < 0)
4187 if (data.is_pipe) { /* Assume pipe_mode indicates native_arch */
4189 } else if (session->header.env.arch) {
4190 if (!strcmp(uts.machine, session->header.env.arch))
4192 else if (!strcmp(uts.machine, "x86_64") &&
4193 !strcmp(session->header.env.arch, "i386"))
4197 script.session = session;
4198 script__setup_sample_type(&script);
4200 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
4201 symbol_conf.graph_function)
4202 itrace_synth_opts.thread_stack = true;
4204 session->itrace_synth_opts = &itrace_synth_opts;
4207 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
4210 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
4214 symbol_conf.use_callchain = true;
4216 symbol_conf.use_callchain = false;
4218 if (session->tevent.pevent &&
4219 tep_set_function_resolver(session->tevent.pevent,
4220 machine__resolve_kernel_addr,
4221 &session->machines.host) < 0) {
4222 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
4227 if (generate_script_lang) {
4228 struct stat perf_stat;
4231 if (output_set_by_user()) {
4233 "custom fields not supported for generated scripts");
4238 input = open(data.path, O_RDONLY); /* input_name */
4241 perror("failed to open file");
4245 err = fstat(input, &perf_stat);
4247 perror("failed to stat file");
4251 if (!perf_stat.st_size) {
4252 fprintf(stderr, "zero-sized file, nothing to do!\n");
4256 scripting_ops = script_spec__lookup(generate_script_lang);
4257 if (!scripting_ops) {
4258 fprintf(stderr, "invalid language specifier");
4263 err = scripting_ops->generate_script(session->tevent.pevent,
4268 err = dlfilter__start(dlfilter, session);
4273 err = scripting_ops->start_script(script_name, argc, argv, session);
4276 pr_debug("perf script started with script %s\n\n", script_name);
4277 script_started = true;
4281 err = perf_session__check_output_opt(session);
4285 if (script.time_str) {
4286 err = perf_time__parse_for_ranges_reltime(script.time_str, session,
4287 &script.ptime_range,
4294 itrace_synth_opts__set_time_range(&itrace_synth_opts,
4299 err = evswitch__init(&script.evswitch, session->evlist, stderr);
4303 if (zstd_init(&(session->zstd_data), 0) < 0)
4304 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
4306 err = __cmd_script(&script);
4311 if (script.ptime_range) {
4312 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
4313 zfree(&script.ptime_range);
4316 zstd_fini(&(session->zstd_data));
4317 evlist__free_stats(session->evlist);
4318 perf_session__delete(session);
4319 perf_script__exit(&script);
4322 cleanup_scripting();
4323 dlfilter__cleanup(dlfilter);