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,
131 struct perf_tool tool;
132 struct perf_session *session;
133 bool show_task_events;
134 bool show_mmap_events;
135 bool show_switch_events;
136 bool show_namespace_events;
137 bool show_lost_events;
138 bool show_round_events;
139 bool show_bpf_events;
140 bool show_cgroup_events;
141 bool show_text_poke_events;
145 struct evswitch evswitch;
146 struct perf_cpu_map *cpus;
147 struct perf_thread_map *threads;
149 const char *time_str;
150 struct perf_time_interval *ptime_range;
155 struct output_option {
157 enum perf_output_field field;
158 } all_output_options[] = {
159 {.str = "comm", .field = PERF_OUTPUT_COMM},
160 {.str = "tid", .field = PERF_OUTPUT_TID},
161 {.str = "pid", .field = PERF_OUTPUT_PID},
162 {.str = "time", .field = PERF_OUTPUT_TIME},
163 {.str = "cpu", .field = PERF_OUTPUT_CPU},
164 {.str = "event", .field = PERF_OUTPUT_EVNAME},
165 {.str = "trace", .field = PERF_OUTPUT_TRACE},
166 {.str = "ip", .field = PERF_OUTPUT_IP},
167 {.str = "sym", .field = PERF_OUTPUT_SYM},
168 {.str = "dso", .field = PERF_OUTPUT_DSO},
169 {.str = "addr", .field = PERF_OUTPUT_ADDR},
170 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
171 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
172 {.str = "period", .field = PERF_OUTPUT_PERIOD},
173 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
174 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
175 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
176 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
177 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
178 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
179 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
180 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
181 {.str = "insn", .field = PERF_OUTPUT_INSN},
182 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
183 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
184 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
185 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
186 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
187 {.str = "metric", .field = PERF_OUTPUT_METRIC},
188 {.str = "misc", .field = PERF_OUTPUT_MISC},
189 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
190 {.str = "ipc", .field = PERF_OUTPUT_IPC},
191 {.str = "tod", .field = PERF_OUTPUT_TOD},
192 {.str = "data_page_size", .field = PERF_OUTPUT_DATA_PAGE_SIZE},
193 {.str = "code_page_size", .field = PERF_OUTPUT_CODE_PAGE_SIZE},
194 {.str = "ins_lat", .field = PERF_OUTPUT_INS_LAT},
195 {.str = "brstackinsnlen", .field = PERF_OUTPUT_BRSTACKINSNLEN},
199 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
204 /* default set to maintain compatibility with current format */
208 unsigned int print_ip_opts;
212 u64 user_unset_fields;
213 } output[OUTPUT_TYPE_MAX] = {
215 [PERF_TYPE_HARDWARE] = {
218 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
219 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
220 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
221 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
222 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
224 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
227 [PERF_TYPE_SOFTWARE] = {
230 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
231 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
232 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
233 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
234 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
235 PERF_OUTPUT_BPF_OUTPUT,
237 .invalid_fields = PERF_OUTPUT_TRACE,
240 [PERF_TYPE_TRACEPOINT] = {
243 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
244 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
245 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
248 [PERF_TYPE_HW_CACHE] = {
251 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
252 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
253 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
254 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
255 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
257 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
263 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
264 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
265 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
266 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
267 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
268 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
269 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR |
270 PERF_OUTPUT_DATA_PAGE_SIZE | PERF_OUTPUT_CODE_PAGE_SIZE |
273 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
276 [PERF_TYPE_BREAKPOINT] = {
279 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
280 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
281 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
282 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
283 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
285 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
288 [OUTPUT_TYPE_SYNTH] = {
291 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
292 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
293 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
294 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
295 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
297 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
300 [OUTPUT_TYPE_OTHER] = {
303 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
304 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
305 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
306 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
307 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
309 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
313 struct evsel_script {
317 /* For metric output */
322 static inline struct evsel_script *evsel_script(struct evsel *evsel)
324 return (struct evsel_script *)evsel->priv;
327 static struct evsel_script *evsel_script__new(struct evsel *evsel, struct perf_data *data)
329 struct evsel_script *es = zalloc(sizeof(*es));
332 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, evsel__name(evsel)) < 0)
334 es->fp = fopen(es->filename, "w");
336 goto out_free_filename;
341 zfree(&es->filename);
347 static void evsel_script__delete(struct evsel_script *es)
349 zfree(&es->filename);
355 static int evsel_script__fprintf(struct evsel_script *es, FILE *fp)
359 fstat(fileno(es->fp), &st);
360 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
361 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
364 static inline int output_type(unsigned int type)
367 case PERF_TYPE_SYNTH:
368 return OUTPUT_TYPE_SYNTH;
370 if (type < PERF_TYPE_MAX)
374 return OUTPUT_TYPE_OTHER;
377 static bool output_set_by_user(void)
380 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
381 if (output[j].user_set)
387 static const char *output_field2str(enum perf_output_field field)
389 int i, imax = ARRAY_SIZE(all_output_options);
390 const char *str = "";
392 for (i = 0; i < imax; ++i) {
393 if (all_output_options[i].field == field) {
394 str = all_output_options[i].str;
401 #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
403 static int evsel__do_check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
404 enum perf_output_field field, bool allow_user_set)
406 struct perf_event_attr *attr = &evsel->core.attr;
407 int type = output_type(attr->type);
410 if (attr->sample_type & sample_type)
413 if (output[type].user_set_fields & field) {
416 evname = evsel__name(evsel);
417 pr_err("Samples for '%s' event do not have %s attribute set. "
418 "Cannot print '%s' field.\n",
419 evname, sample_msg, output_field2str(field));
423 /* user did not ask for it explicitly so remove from the default list */
424 output[type].fields &= ~field;
425 evname = evsel__name(evsel);
426 pr_debug("Samples for '%s' event do not have %s attribute set. "
427 "Skipping '%s' field.\n",
428 evname, sample_msg, output_field2str(field));
433 static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
434 enum perf_output_field field)
436 return evsel__do_check_stype(evsel, sample_type, sample_msg, field, false);
439 static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
441 struct perf_event_attr *attr = &evsel->core.attr;
444 if (perf_header__has_feat(&session->header, HEADER_STAT))
447 allow_user_set = perf_header__has_feat(&session->header,
450 if (PRINT_FIELD(TRACE) &&
451 !perf_session__has_traces(session, "record -R"))
454 if (PRINT_FIELD(IP)) {
455 if (evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", PERF_OUTPUT_IP))
459 if (PRINT_FIELD(ADDR) &&
460 evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", PERF_OUTPUT_ADDR, allow_user_set))
463 if (PRINT_FIELD(DATA_SRC) &&
464 evsel__do_check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", PERF_OUTPUT_DATA_SRC, allow_user_set))
467 if (PRINT_FIELD(WEIGHT) &&
468 evsel__do_check_stype(evsel, PERF_SAMPLE_WEIGHT_TYPE, "WEIGHT", PERF_OUTPUT_WEIGHT, allow_user_set))
471 if (PRINT_FIELD(SYM) &&
472 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
473 pr_err("Display of symbols requested but neither sample IP nor "
474 "sample address\navailable. Hence, no addresses to convert "
478 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
479 pr_err("Display of offsets requested but symbol is not"
483 if (PRINT_FIELD(DSO) &&
484 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
485 pr_err("Display of DSO requested but no address to convert.\n");
488 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
489 pr_err("Display of source line number requested but sample IP is not\n"
490 "selected. Hence, no address to lookup the source line number.\n");
493 if ((PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN)) && !allow_user_set &&
494 !(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY)) {
495 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
496 "Hint: run 'perf record -b ...'\n");
499 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
500 evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", PERF_OUTPUT_TID|PERF_OUTPUT_PID))
503 if (PRINT_FIELD(TIME) &&
504 evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", PERF_OUTPUT_TIME))
507 if (PRINT_FIELD(CPU) &&
508 evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", PERF_OUTPUT_CPU, allow_user_set))
511 if (PRINT_FIELD(IREGS) &&
512 evsel__do_check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", PERF_OUTPUT_IREGS, allow_user_set))
515 if (PRINT_FIELD(UREGS) &&
516 evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS", PERF_OUTPUT_UREGS))
519 if (PRINT_FIELD(PHYS_ADDR) &&
520 evsel__do_check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR", PERF_OUTPUT_PHYS_ADDR, allow_user_set))
523 if (PRINT_FIELD(DATA_PAGE_SIZE) &&
524 evsel__check_stype(evsel, PERF_SAMPLE_DATA_PAGE_SIZE, "DATA_PAGE_SIZE", PERF_OUTPUT_DATA_PAGE_SIZE))
527 if (PRINT_FIELD(CODE_PAGE_SIZE) &&
528 evsel__check_stype(evsel, PERF_SAMPLE_CODE_PAGE_SIZE, "CODE_PAGE_SIZE", PERF_OUTPUT_CODE_PAGE_SIZE))
531 if (PRINT_FIELD(INS_LAT) &&
532 evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT_STRUCT, "WEIGHT_STRUCT", PERF_OUTPUT_INS_LAT))
538 static void set_print_ip_opts(struct perf_event_attr *attr)
540 unsigned int type = output_type(attr->type);
542 output[type].print_ip_opts = 0;
544 output[type].print_ip_opts |= EVSEL__PRINT_IP;
546 if (PRINT_FIELD(SYM))
547 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
549 if (PRINT_FIELD(DSO))
550 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
552 if (PRINT_FIELD(SYMOFFSET))
553 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
555 if (PRINT_FIELD(SRCLINE))
556 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
559 static struct evsel *find_first_output_type(struct evlist *evlist,
564 evlist__for_each_entry(evlist, evsel) {
565 if (output_type(evsel->core.attr.type) == (int)type)
572 * verify all user requested events exist and the samples
573 * have the expected data
575 static int perf_session__check_output_opt(struct perf_session *session)
581 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
582 evsel = find_first_output_type(session->evlist, j);
585 * even if fields is set to 0 (ie., show nothing) event must
586 * exist if user explicitly includes it on the command line
588 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
589 j != OUTPUT_TYPE_SYNTH) {
590 pr_err("%s events do not exist. "
591 "Remove corresponding -F option to proceed.\n",
596 if (evsel && output[j].fields &&
597 evsel__check_attr(evsel, session))
603 set_print_ip_opts(&evsel->core.attr);
604 tod |= output[j].fields & PERF_OUTPUT_TOD;
608 bool use_callchain = false;
609 bool not_pipe = false;
611 evlist__for_each_entry(session->evlist, evsel) {
613 if (evsel__has_callchain(evsel)) {
614 use_callchain = true;
618 if (not_pipe && !use_callchain)
619 symbol_conf.use_callchain = false;
623 * set default for tracepoints to print symbols only
624 * if callchains are present
626 if (symbol_conf.use_callchain &&
627 !output[PERF_TYPE_TRACEPOINT].user_set) {
628 j = PERF_TYPE_TRACEPOINT;
630 evlist__for_each_entry(session->evlist, evsel) {
631 if (evsel->core.attr.type != j)
634 if (evsel__has_callchain(evsel)) {
635 output[j].fields |= PERF_OUTPUT_IP;
636 output[j].fields |= PERF_OUTPUT_SYM;
637 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
638 output[j].fields |= PERF_OUTPUT_DSO;
639 set_print_ip_opts(&evsel->core.attr);
645 if (tod && !session->header.env.clock.enabled) {
646 pr_err("Can't provide 'tod' time, missing clock data. "
647 "Please record with -k/--clockid option.\n");
654 static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask, const char *arch,
660 if (!regs || !regs->regs)
663 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
665 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
666 u64 val = regs->regs[i++];
667 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r, arch), val);
673 #define DEFAULT_TOD_FMT "%F %H:%M:%S"
676 tod_scnprintf(struct perf_script *script, char *buf, int buflen,
679 u64 tod_ns, clockid_ns;
680 struct perf_env *env;
687 if (buflen < 64 || !script)
690 env = &script->session->header.env;
691 if (!env->clock.enabled) {
692 scnprintf(buf, buflen, "disabled");
696 clockid_ns = env->clock.clockid_ns;
697 tod_ns = env->clock.tod_ns;
699 if (timestamp > clockid_ns)
700 tod_ns += timestamp - clockid_ns;
702 tod_ns -= clockid_ns - timestamp;
704 sec = (time_t) (tod_ns / NSEC_PER_SEC);
705 nsec = tod_ns - sec * NSEC_PER_SEC;
707 if (localtime_r(&sec, <ime) == NULL) {
708 scnprintf(buf, buflen, "failed");
710 strftime(date, sizeof(date), DEFAULT_TOD_FMT, <ime);
712 if (symbol_conf.nanosecs) {
713 snprintf(buf, buflen, "%s.%09lu", date, nsec);
715 snprintf(buf, buflen, "%s.%06lu",
716 date, nsec / NSEC_PER_USEC);
723 static int perf_sample__fprintf_iregs(struct perf_sample *sample,
724 struct perf_event_attr *attr, const char *arch, FILE *fp)
726 return perf_sample__fprintf_regs(&sample->intr_regs,
727 attr->sample_regs_intr, arch, fp);
730 static int perf_sample__fprintf_uregs(struct perf_sample *sample,
731 struct perf_event_attr *attr, const char *arch, FILE *fp)
733 return perf_sample__fprintf_regs(&sample->user_regs,
734 attr->sample_regs_user, arch, fp);
737 static int perf_sample__fprintf_start(struct perf_script *script,
738 struct perf_sample *sample,
739 struct thread *thread,
743 struct perf_event_attr *attr = &evsel->core.attr;
745 unsigned long long nsecs;
749 if (PRINT_FIELD(COMM)) {
750 const char *comm = thread ? thread__comm_str(thread) : ":-1";
753 printed += fprintf(fp, "%8.8s ", comm);
754 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
755 printed += fprintf(fp, "%s ", comm);
757 printed += fprintf(fp, "%16s ", comm);
760 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
761 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
762 else if (PRINT_FIELD(PID))
763 printed += fprintf(fp, "%5d ", sample->pid);
764 else if (PRINT_FIELD(TID))
765 printed += fprintf(fp, "%5d ", sample->tid);
767 if (PRINT_FIELD(CPU)) {
769 printed += fprintf(fp, "%3d ", sample->cpu);
771 printed += fprintf(fp, "[%03d] ", sample->cpu);
774 if (PRINT_FIELD(MISC)) {
778 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
781 ret += fprintf(fp, "K");
783 ret += fprintf(fp, "U");
785 ret += fprintf(fp, "H");
786 if (has(GUEST_KERNEL))
787 ret += fprintf(fp, "G");
789 ret += fprintf(fp, "g");
792 case PERF_RECORD_MMAP:
793 case PERF_RECORD_MMAP2:
795 ret += fprintf(fp, "M");
797 case PERF_RECORD_COMM:
799 ret += fprintf(fp, "E");
801 case PERF_RECORD_SWITCH:
802 case PERF_RECORD_SWITCH_CPU_WIDE:
803 if (has(SWITCH_OUT)) {
804 ret += fprintf(fp, "S");
805 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
806 ret += fprintf(fp, "p");
814 ret += fprintf(fp, "%*s", 6 - ret, " ");
818 if (PRINT_FIELD(TOD)) {
819 tod_scnprintf(script, tstr, sizeof(tstr), sample->time);
820 printed += fprintf(fp, "%s ", tstr);
823 if (PRINT_FIELD(TIME)) {
824 u64 t = sample->time;
827 initial_time = sample->time;
828 t = sample->time - initial_time;
829 } else if (deltatime) {
831 t = sample->time - previous_time;
835 previous_time = sample->time;
838 secs = nsecs / NSEC_PER_SEC;
839 nsecs -= secs * NSEC_PER_SEC;
841 if (symbol_conf.nanosecs)
842 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
844 char sample_time[32];
845 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
846 printed += fprintf(fp, "%12s: ", sample_time);
854 mispred_str(struct branch_entry *br)
856 if (!(br->flags.mispred || br->flags.predicted))
859 return br->flags.predicted ? 'P' : 'M';
862 static int print_bstack_flags(FILE *fp, struct branch_entry *br)
864 return fprintf(fp, "/%c/%c/%c/%d/%s ",
866 br->flags.in_tx ? 'X' : '-',
867 br->flags.abort ? 'A' : '-',
869 br->flags.type ? branch_type_name(br->flags.type) : "-");
872 static int perf_sample__fprintf_brstack(struct perf_sample *sample,
873 struct thread *thread,
874 struct perf_event_attr *attr, FILE *fp)
876 struct branch_stack *br = sample->branch_stack;
877 struct branch_entry *entries = perf_sample__branch_entries(sample);
878 struct addr_location alf, alt;
885 for (i = 0; i < br->nr; i++) {
886 from = entries[i].from;
889 if (PRINT_FIELD(DSO)) {
890 memset(&alf, 0, sizeof(alf));
891 memset(&alt, 0, sizeof(alt));
892 thread__find_map_fb(thread, sample->cpumode, from, &alf);
893 thread__find_map_fb(thread, sample->cpumode, to, &alt);
896 printed += fprintf(fp, " 0x%"PRIx64, from);
897 if (PRINT_FIELD(DSO)) {
898 printed += fprintf(fp, "(");
899 printed += map__fprintf_dsoname(alf.map, fp);
900 printed += fprintf(fp, ")");
903 printed += fprintf(fp, "/0x%"PRIx64, to);
904 if (PRINT_FIELD(DSO)) {
905 printed += fprintf(fp, "(");
906 printed += map__fprintf_dsoname(alt.map, fp);
907 printed += fprintf(fp, ")");
910 printed += print_bstack_flags(fp, entries + i);
916 static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
917 struct thread *thread,
918 struct perf_event_attr *attr, FILE *fp)
920 struct branch_stack *br = sample->branch_stack;
921 struct branch_entry *entries = perf_sample__branch_entries(sample);
922 struct addr_location alf, alt;
929 for (i = 0; i < br->nr; i++) {
931 memset(&alf, 0, sizeof(alf));
932 memset(&alt, 0, sizeof(alt));
933 from = entries[i].from;
936 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
937 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
939 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
940 if (PRINT_FIELD(DSO)) {
941 printed += fprintf(fp, "(");
942 printed += map__fprintf_dsoname(alf.map, fp);
943 printed += fprintf(fp, ")");
945 printed += fprintf(fp, "%c", '/');
946 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
947 if (PRINT_FIELD(DSO)) {
948 printed += fprintf(fp, "(");
949 printed += map__fprintf_dsoname(alt.map, fp);
950 printed += fprintf(fp, ")");
952 printed += print_bstack_flags(fp, entries + i);
958 static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
959 struct thread *thread,
960 struct perf_event_attr *attr, FILE *fp)
962 struct branch_stack *br = sample->branch_stack;
963 struct branch_entry *entries = perf_sample__branch_entries(sample);
964 struct addr_location alf, alt;
971 for (i = 0; i < br->nr; i++) {
973 memset(&alf, 0, sizeof(alf));
974 memset(&alt, 0, sizeof(alt));
975 from = entries[i].from;
978 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
979 !alf.map->dso->adjust_symbols)
980 from = map__map_ip(alf.map, from);
982 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
983 !alt.map->dso->adjust_symbols)
984 to = map__map_ip(alt.map, to);
986 printed += fprintf(fp, " 0x%"PRIx64, from);
987 if (PRINT_FIELD(DSO)) {
988 printed += fprintf(fp, "(");
989 printed += map__fprintf_dsoname(alf.map, fp);
990 printed += fprintf(fp, ")");
992 printed += fprintf(fp, "/0x%"PRIx64, to);
993 if (PRINT_FIELD(DSO)) {
994 printed += fprintf(fp, "(");
995 printed += map__fprintf_dsoname(alt.map, fp);
996 printed += fprintf(fp, ")");
998 printed += print_bstack_flags(fp, entries + i);
1003 #define MAXBB 16384UL
1005 static int grab_bb(u8 *buffer, u64 start, u64 end,
1006 struct machine *machine, struct thread *thread,
1007 bool *is64bit, u8 *cpumode, bool last)
1010 struct addr_location al;
1016 kernel = machine__kernel_ip(machine, start);
1018 *cpumode = PERF_RECORD_MISC_KERNEL;
1020 *cpumode = PERF_RECORD_MISC_USER;
1023 * Block overlaps between kernel and user.
1024 * This can happen due to ring filtering
1025 * On Intel CPUs the entry into the kernel is filtered,
1026 * but the exit is not. Let the caller patch it up.
1028 if (kernel != machine__kernel_ip(machine, end)) {
1029 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
1033 memset(&al, 0, sizeof(al));
1034 if (end - start > MAXBB - MAXINSN) {
1036 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
1038 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
1042 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
1043 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
1046 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
1047 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
1051 /* Load maps to ensure dso->is_64_bit has been updated */
1054 offset = al.map->map_ip(al.map, start);
1055 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
1056 end - start + MAXINSN);
1058 *is64bit = al.map->dso->is_64_bit;
1060 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
1065 static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srccode_state *state)
1073 if (!map || !map->dso)
1075 srcfile = get_srcline_split(map->dso,
1076 map__rip_2objdump(map, addr),
1081 /* Avoid redundant printing */
1084 !strcmp(state->srcfile, srcfile) &&
1085 state->line == line) {
1090 srccode = find_sourceline(srcfile, line, &len);
1094 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
1097 state->srcfile = srcfile;
1107 static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
1109 struct addr_location al;
1112 memset(&al, 0, sizeof(al));
1113 thread__find_map(thread, cpumode, addr, &al);
1116 ret = map__fprintf_srccode(al.map, al.addr, stdout,
1117 &thread->srccode_state);
1119 ret += printf("\n");
1123 static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
1124 struct perf_insn *x, u8 *inbuf, int len,
1125 int insn, FILE *fp, int *total_cycles,
1126 struct perf_event_attr *attr)
1129 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t", ip,
1130 dump_insn(x, ip, inbuf, len, &ilen));
1132 if (PRINT_FIELD(BRSTACKINSNLEN))
1133 printed += fprintf(fp, "ilen: %d\t", ilen);
1135 printed += fprintf(fp, "#%s%s%s%s",
1136 en->flags.predicted ? " PRED" : "",
1137 en->flags.mispred ? " MISPRED" : "",
1138 en->flags.in_tx ? " INTX" : "",
1139 en->flags.abort ? " ABORT" : "");
1140 if (en->flags.cycles) {
1141 *total_cycles += en->flags.cycles;
1142 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
1144 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
1146 return printed + fprintf(fp, "\n");
1149 static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
1150 u8 cpumode, int cpu, struct symbol **lastsym,
1151 struct perf_event_attr *attr, FILE *fp)
1153 struct addr_location al;
1154 int off, printed = 0;
1156 memset(&al, 0, sizeof(al));
1158 thread__find_map(thread, cpumode, addr, &al);
1160 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
1166 al.sym = map__find_symbol(al.map, al.addr);
1171 if (al.addr < al.sym->end)
1172 off = al.addr - al.sym->start;
1174 off = al.addr - al.map->start - al.sym->start;
1175 printed += fprintf(fp, "\t%s", al.sym->name);
1177 printed += fprintf(fp, "%+d", off);
1178 printed += fprintf(fp, ":");
1179 if (PRINT_FIELD(SRCLINE))
1180 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
1181 printed += fprintf(fp, "\n");
1187 static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1188 struct thread *thread,
1189 struct perf_event_attr *attr,
1190 struct machine *machine, FILE *fp)
1192 struct branch_stack *br = sample->branch_stack;
1193 struct branch_entry *entries = perf_sample__branch_entries(sample);
1195 int i, insn, len, nr, ilen, printed = 0;
1199 struct symbol *lastsym = NULL;
1200 int total_cycles = 0;
1202 if (!(br && br->nr))
1205 if (max_blocks && nr > max_blocks + 1)
1206 nr = max_blocks + 1;
1209 x.cpu = sample->cpu;
1211 printed += fprintf(fp, "%c", '\n');
1213 /* Handle first from jump, of which we don't know the entry. */
1214 len = grab_bb(buffer, entries[nr-1].from,
1216 machine, thread, &x.is64bit, &x.cpumode, false);
1218 printed += ip__fprintf_sym(entries[nr - 1].from, thread,
1219 x.cpumode, x.cpu, &lastsym, attr, fp);
1220 printed += ip__fprintf_jump(entries[nr - 1].from, &entries[nr - 1],
1221 &x, buffer, len, 0, fp, &total_cycles,
1223 if (PRINT_FIELD(SRCCODE))
1224 printed += print_srccode(thread, x.cpumode, entries[nr - 1].from);
1227 /* Print all blocks */
1228 for (i = nr - 2; i >= 0; i--) {
1229 if (entries[i].from || entries[i].to)
1230 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1233 start = entries[i + 1].to;
1234 end = entries[i].from;
1236 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1237 /* Patch up missing kernel transfers due to ring filters */
1238 if (len == -ENXIO && i > 0) {
1239 end = entries[--i].from;
1240 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1241 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1247 for (off = 0; off < (unsigned)len; off += ilen) {
1248 uint64_t ip = start + off;
1250 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1252 printed += ip__fprintf_jump(ip, &entries[i], &x, buffer + off, len - off, ++insn, fp,
1253 &total_cycles, attr);
1254 if (PRINT_FIELD(SRCCODE))
1255 printed += print_srccode(thread, x.cpumode, ip);
1259 printed += fprintf(fp, "\t%016" PRIx64 "\t%s", ip,
1260 dump_insn(&x, ip, buffer + off, len - off, &ilen));
1261 if (PRINT_FIELD(BRSTACKINSNLEN))
1262 printed += fprintf(fp, "\tilen: %d", ilen);
1263 printed += fprintf(fp, "\n");
1266 if (PRINT_FIELD(SRCCODE))
1267 print_srccode(thread, x.cpumode, ip);
1271 if (off != end - start)
1272 printed += fprintf(fp, "\tmismatch of LBR data and executable\n");
1276 * Hit the branch? In this case we are already done, and the target
1277 * has not been executed yet.
1279 if (entries[0].from == sample->ip)
1281 if (entries[0].flags.abort)
1285 * Print final block upto sample
1287 * Due to pipeline delays the LBRs might be missing a branch
1288 * or two, which can result in very large or negative blocks
1289 * between final branch and sample. When this happens just
1290 * continue walking after the last TO until we hit a branch.
1292 start = entries[0].to;
1295 /* Missing jump. Scan 128 bytes for the next branch */
1298 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
1299 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1301 /* Print at least last IP if basic block did not work */
1302 len = grab_bb(buffer, sample->ip, sample->ip,
1303 machine, thread, &x.is64bit, &x.cpumode, false);
1307 printed += fprintf(fp, "\t%016" PRIx64 "\t%s", sample->ip,
1308 dump_insn(&x, sample->ip, buffer, len, &ilen));
1309 if (PRINT_FIELD(BRSTACKINSNLEN))
1310 printed += fprintf(fp, "\tilen: %d", ilen);
1311 printed += fprintf(fp, "\n");
1312 if (PRINT_FIELD(SRCCODE))
1313 print_srccode(thread, x.cpumode, sample->ip);
1316 for (off = 0; off <= end - start; off += ilen) {
1318 printed += fprintf(fp, "\t%016" PRIx64 "\t%s", start + off,
1319 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
1320 if (PRINT_FIELD(BRSTACKINSNLEN))
1321 printed += fprintf(fp, "\tilen: %d", ilen);
1322 printed += fprintf(fp, "\n");
1325 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1327 * Hit a missing branch. Just stop.
1329 printed += fprintf(fp, "\t... not reaching sample ...\n");
1332 if (PRINT_FIELD(SRCCODE))
1333 print_srccode(thread, x.cpumode, start + off);
1339 static int perf_sample__fprintf_addr(struct perf_sample *sample,
1340 struct thread *thread,
1341 struct perf_event_attr *attr, FILE *fp)
1343 struct addr_location al;
1344 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
1346 if (!sample_addr_correlates_sym(attr))
1349 thread__resolve(thread, &al, sample);
1351 if (PRINT_FIELD(SYM)) {
1352 printed += fprintf(fp, " ");
1353 if (PRINT_FIELD(SYMOFFSET))
1354 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
1356 printed += symbol__fprintf_symname(al.sym, fp);
1359 if (PRINT_FIELD(DSO)) {
1360 printed += fprintf(fp, " (");
1361 printed += map__fprintf_dsoname(al.map, fp);
1362 printed += fprintf(fp, ")");
1368 static const char *resolve_branch_sym(struct perf_sample *sample,
1369 struct evsel *evsel,
1370 struct thread *thread,
1371 struct addr_location *al,
1372 struct addr_location *addr_al,
1375 struct perf_event_attr *attr = &evsel->core.attr;
1376 const char *name = NULL;
1378 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1379 if (sample_addr_correlates_sym(attr)) {
1380 if (!addr_al->thread)
1381 thread__resolve(thread, addr_al, sample);
1383 name = addr_al->sym->name;
1389 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1391 name = al->sym->name;
1398 static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1399 struct evsel *evsel,
1400 struct thread *thread,
1401 struct addr_location *al,
1402 struct addr_location *addr_al,
1405 struct perf_event_attr *attr = &evsel->core.attr;
1406 size_t depth = thread_stack__depth(thread, sample->cpu);
1407 const char *name = NULL;
1414 * The 'return' has already been popped off the stack so the depth has
1415 * to be adjusted to match the 'call'.
1417 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1420 name = resolve_branch_sym(sample, evsel, thread, al, addr_al, &ip);
1422 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1423 dlen += fprintf(fp, "(");
1424 dlen += map__fprintf_dsoname(al->map, fp);
1425 dlen += fprintf(fp, ")\t");
1429 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
1431 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
1437 * Try to keep the output length from changing frequently so that the
1438 * output lines up more nicely.
1440 if (len > spacing || (len && len < spacing - 52))
1441 spacing = round_up(len + 4, 32);
1444 len += fprintf(fp, "%*s", spacing - len, "");
1449 __weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1450 struct thread *thread __maybe_unused,
1451 struct machine *machine __maybe_unused)
1455 void script_fetch_insn(struct perf_sample *sample, struct thread *thread,
1456 struct machine *machine)
1458 if (sample->insn_len == 0 && native_arch)
1459 arch_fetch_insn(sample, thread, machine);
1462 static int perf_sample__fprintf_insn(struct perf_sample *sample,
1463 struct perf_event_attr *attr,
1464 struct thread *thread,
1465 struct machine *machine, FILE *fp)
1469 script_fetch_insn(sample, thread, machine);
1471 if (PRINT_FIELD(INSNLEN))
1472 printed += fprintf(fp, " ilen: %d", sample->insn_len);
1473 if (PRINT_FIELD(INSN) && sample->insn_len) {
1476 printed += fprintf(fp, " insn:");
1477 for (i = 0; i < sample->insn_len; i++)
1478 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
1480 if (PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN))
1481 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1486 static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1487 struct perf_event_attr *attr, FILE *fp)
1491 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1494 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1496 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1497 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1500 static int perf_sample__fprintf_bts(struct perf_sample *sample,
1501 struct evsel *evsel,
1502 struct thread *thread,
1503 struct addr_location *al,
1504 struct addr_location *addr_al,
1505 struct machine *machine, FILE *fp)
1507 struct perf_event_attr *attr = &evsel->core.attr;
1508 unsigned int type = output_type(attr->type);
1509 bool print_srcline_last = false;
1512 if (PRINT_FIELD(CALLINDENT))
1513 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, addr_al, fp);
1515 /* print branch_from information */
1516 if (PRINT_FIELD(IP)) {
1517 unsigned int print_opts = output[type].print_ip_opts;
1518 struct callchain_cursor *cursor = NULL;
1520 if (symbol_conf.use_callchain && sample->callchain &&
1521 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1522 sample, NULL, NULL, scripting_max_stack) == 0)
1523 cursor = &callchain_cursor;
1525 if (cursor == NULL) {
1526 printed += fprintf(fp, " ");
1527 if (print_opts & EVSEL__PRINT_SRCLINE) {
1528 print_srcline_last = true;
1529 print_opts &= ~EVSEL__PRINT_SRCLINE;
1532 printed += fprintf(fp, "\n");
1534 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor,
1535 symbol_conf.bt_stop_list, fp);
1538 /* print branch_to information */
1539 if (PRINT_FIELD(ADDR) ||
1540 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
1541 !output[type].user_set)) {
1542 printed += fprintf(fp, " => ");
1543 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
1546 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1548 if (print_srcline_last)
1549 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
1551 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1552 printed += fprintf(fp, "\n");
1553 if (PRINT_FIELD(SRCCODE)) {
1554 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1555 &thread->srccode_state);
1558 printed += printf("\n");
1567 } sample_flags[] = {
1568 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1569 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1570 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1571 {PERF_IP_FLAG_BRANCH, "jmp"},
1572 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1573 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1574 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1575 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1576 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1577 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1578 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1579 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1580 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1581 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vmentry"},
1582 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vmexit"},
1586 static const char *sample_flags_to_name(u32 flags)
1590 for (i = 0; sample_flags[i].name ; i++) {
1591 if (sample_flags[i].flags == flags)
1592 return sample_flags[i].name;
1598 int perf_sample__sprintf_flags(u32 flags, char *str, size_t sz)
1600 u32 xf = PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_INTR_DISABLE |
1601 PERF_IP_FLAG_INTR_TOGGLE;
1602 const char *chars = PERF_IP_FLAG_CHARS;
1603 const size_t n = strlen(PERF_IP_FLAG_CHARS);
1604 const char *name = NULL;
1609 snprintf(xs, sizeof(xs), "(%s%s%s)",
1610 flags & PERF_IP_FLAG_IN_TX ? "x" : "",
1611 flags & PERF_IP_FLAG_INTR_DISABLE ? "D" : "",
1612 flags & PERF_IP_FLAG_INTR_TOGGLE ? "t" : "");
1614 name = sample_flags_to_name(flags & ~xf);
1616 return snprintf(str, sz, "%-15s%6s", name, xs);
1618 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1619 name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_BEGIN));
1621 return snprintf(str, sz, "tr strt %-7s%6s", name, xs);
1624 if (flags & PERF_IP_FLAG_TRACE_END) {
1625 name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_END));
1627 return snprintf(str, sz, "tr end %-7s%6s", name, xs);
1630 for (i = 0; i < n; i++, flags >>= 1) {
1631 if ((flags & 1) && pos < sz)
1632 str[pos++] = chars[i];
1634 for (; i < 32; i++, flags >>= 1) {
1635 if ((flags & 1) && pos < sz)
1644 static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1646 char str[SAMPLE_FLAGS_BUF_SIZE];
1648 perf_sample__sprintf_flags(flags, str, sizeof(str));
1649 return fprintf(fp, " %-21s ", str);
1652 struct printer_data {
1658 static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1660 void *extra, FILE *fp)
1662 unsigned char ch = (unsigned char)val;
1663 struct printer_data *printer_data = extra;
1667 case BINARY_PRINT_DATA_BEGIN:
1668 printed += fprintf(fp, "\n");
1670 case BINARY_PRINT_LINE_BEGIN:
1671 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
1674 case BINARY_PRINT_ADDR:
1675 printed += fprintf(fp, " %04x:", val);
1677 case BINARY_PRINT_NUM_DATA:
1678 printed += fprintf(fp, " %02x", val);
1680 case BINARY_PRINT_NUM_PAD:
1681 printed += fprintf(fp, " ");
1683 case BINARY_PRINT_SEP:
1684 printed += fprintf(fp, " ");
1686 case BINARY_PRINT_CHAR_DATA:
1687 if (printer_data->hit_nul && ch)
1688 printer_data->is_printable = false;
1691 printed += fprintf(fp, "%c", '.');
1693 if (!printer_data->is_printable)
1697 printer_data->hit_nul = true;
1699 printer_data->is_printable = false;
1701 printed += fprintf(fp, "%c", ch);
1704 case BINARY_PRINT_CHAR_PAD:
1705 printed += fprintf(fp, " ");
1707 case BINARY_PRINT_LINE_END:
1708 printed += fprintf(fp, "\n");
1709 printer_data->line_no++;
1711 case BINARY_PRINT_DATA_END:
1719 static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
1721 unsigned int nr_bytes = sample->raw_size;
1722 struct printer_data printer_data = {0, false, true};
1723 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1724 sample__fprintf_bpf_output, &printer_data, fp);
1726 if (printer_data.is_printable && printer_data.hit_nul)
1727 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1732 static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
1734 if (len > 0 && len < spacing)
1735 return fprintf(fp, "%*s", spacing - len, "");
1740 static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
1742 return perf_sample__fprintf_spacing(len, 34, fp);
1745 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
1747 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1750 if (perf_sample__bad_synth_size(sample, *data))
1753 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
1754 data->ip, le64_to_cpu(data->payload));
1755 return len + perf_sample__fprintf_pt_spacing(len, fp);
1758 static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
1760 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1763 if (perf_sample__bad_synth_size(sample, *data))
1766 len = fprintf(fp, " hints: %#x extensions: %#x ",
1767 data->hints, data->extensions);
1768 return len + perf_sample__fprintf_pt_spacing(len, fp);
1771 static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
1773 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1776 if (perf_sample__bad_synth_size(sample, *data))
1779 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1780 data->hw, data->cstate, data->subcstate);
1781 return len + perf_sample__fprintf_pt_spacing(len, fp);
1784 static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
1786 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1789 if (perf_sample__bad_synth_size(sample, *data))
1792 len = fprintf(fp, " IP: %u ", data->ip);
1793 return len + perf_sample__fprintf_pt_spacing(len, fp);
1796 static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
1798 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1801 if (perf_sample__bad_synth_size(sample, *data))
1804 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1805 data->deepest_cstate, data->last_cstate,
1807 return len + perf_sample__fprintf_pt_spacing(len, fp);
1810 static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
1812 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1813 unsigned int percent, freq;
1816 if (perf_sample__bad_synth_size(sample, *data))
1819 freq = (le32_to_cpu(data->freq) + 500) / 1000;
1820 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
1821 if (data->max_nonturbo) {
1822 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1823 len += fprintf(fp, "(%3u%%) ", percent);
1825 return len + perf_sample__fprintf_pt_spacing(len, fp);
1828 static int perf_sample__fprintf_synth_psb(struct perf_sample *sample, FILE *fp)
1830 struct perf_synth_intel_psb *data = perf_sample__synth_ptr(sample);
1833 if (perf_sample__bad_synth_size(sample, *data))
1836 len = fprintf(fp, " psb offs: %#" PRIx64, data->offset);
1837 return len + perf_sample__fprintf_pt_spacing(len, fp);
1840 /* Intel PT Event Trace */
1841 static int perf_sample__fprintf_synth_evt(struct perf_sample *sample, FILE *fp)
1843 struct perf_synth_intel_evt *data = perf_sample__synth_ptr(sample);
1844 const char *cfe[32] = {NULL, "INTR", "IRET", "SMI", "RSM", "SIPI",
1845 "INIT", "VMENTRY", "VMEXIT", "VMEXIT_INTR",
1847 const char *evd[64] = {"PFA", "VMXQ", "VMXR"};
1851 if (perf_sample__bad_synth_size(sample, *data))
1854 s = cfe[data->type];
1856 len = fprintf(fp, " cfe: %s IP: %d vector: %u",
1857 s, data->ip, data->vector);
1859 len = fprintf(fp, " cfe: %u IP: %d vector: %u",
1860 data->type, data->ip, data->vector);
1862 for (i = 0; i < data->evd_cnt; i++) {
1863 unsigned int et = data->evd[i].evd_type & 0x3f;
1867 len += fprintf(fp, " %s: %#" PRIx64,
1868 s, data->evd[i].payload);
1870 len += fprintf(fp, " EVD_%u: %#" PRIx64,
1871 et, data->evd[i].payload);
1874 return len + perf_sample__fprintf_pt_spacing(len, fp);
1877 static int perf_sample__fprintf_synth_iflag_chg(struct perf_sample *sample, FILE *fp)
1879 struct perf_synth_intel_iflag_chg *data = perf_sample__synth_ptr(sample);
1882 if (perf_sample__bad_synth_size(sample, *data))
1885 len = fprintf(fp, " IFLAG: %d->%d %s branch", !data->iflag, data->iflag,
1886 data->via_branch ? "via" : "non");
1887 return len + perf_sample__fprintf_pt_spacing(len, fp);
1890 static int perf_sample__fprintf_synth(struct perf_sample *sample,
1891 struct evsel *evsel, FILE *fp)
1893 switch (evsel->core.attr.config) {
1894 case PERF_SYNTH_INTEL_PTWRITE:
1895 return perf_sample__fprintf_synth_ptwrite(sample, fp);
1896 case PERF_SYNTH_INTEL_MWAIT:
1897 return perf_sample__fprintf_synth_mwait(sample, fp);
1898 case PERF_SYNTH_INTEL_PWRE:
1899 return perf_sample__fprintf_synth_pwre(sample, fp);
1900 case PERF_SYNTH_INTEL_EXSTOP:
1901 return perf_sample__fprintf_synth_exstop(sample, fp);
1902 case PERF_SYNTH_INTEL_PWRX:
1903 return perf_sample__fprintf_synth_pwrx(sample, fp);
1904 case PERF_SYNTH_INTEL_CBR:
1905 return perf_sample__fprintf_synth_cbr(sample, fp);
1906 case PERF_SYNTH_INTEL_PSB:
1907 return perf_sample__fprintf_synth_psb(sample, fp);
1908 case PERF_SYNTH_INTEL_EVT:
1909 return perf_sample__fprintf_synth_evt(sample, fp);
1910 case PERF_SYNTH_INTEL_IFLAG_CHG:
1911 return perf_sample__fprintf_synth_iflag_chg(sample, fp);
1919 static int evlist__max_name_len(struct evlist *evlist)
1921 struct evsel *evsel;
1924 evlist__for_each_entry(evlist, evsel) {
1925 int len = strlen(evsel__name(evsel));
1927 max = MAX(len, max);
1933 static int data_src__fprintf(u64 data_src, FILE *fp)
1935 struct mem_info mi = { .data_src.val = data_src };
1941 perf_script__meminfo_scnprintf(decode, 100, &mi);
1943 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1947 return fprintf(fp, "%-*s", maxlen, out);
1951 struct perf_sample *sample;
1952 struct thread *thread;
1953 struct evsel *evsel;
1957 static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1958 void *ctx, const char *color,
1960 const char *unit, double val)
1962 struct metric_ctx *mctx = ctx;
1966 perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
1967 PERF_RECORD_SAMPLE, mctx->fp);
1968 fputs("\tmetric: ", mctx->fp);
1970 color_fprintf(mctx->fp, color, fmt, val);
1973 fprintf(mctx->fp, " %s\n", unit);
1976 static void script_new_line(struct perf_stat_config *config __maybe_unused,
1979 struct metric_ctx *mctx = ctx;
1981 perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
1982 PERF_RECORD_SAMPLE, mctx->fp);
1983 fputs("\tmetric: ", mctx->fp);
1986 static void perf_sample__fprint_metric(struct perf_script *script,
1987 struct thread *thread,
1988 struct evsel *evsel,
1989 struct perf_sample *sample,
1992 struct evsel *leader = evsel__leader(evsel);
1993 struct perf_stat_output_ctx ctx = {
1994 .print_metric = script_print_metric,
1995 .new_line = script_new_line,
1996 .ctx = &(struct metric_ctx) {
2002 .force_header = false,
2008 evlist__alloc_stats(script->session->evlist, false);
2009 if (evsel_script(leader)->gnum++ == 0)
2010 perf_stat__reset_shadow_stats();
2011 val = sample->period * evsel->scale;
2012 perf_stat__update_shadow_stats(evsel,
2016 evsel_script(evsel)->val = val;
2017 if (evsel_script(leader)->gnum == leader->core.nr_members) {
2018 for_each_group_member (ev2, leader) {
2019 perf_stat__print_shadow_stats(&stat_config, ev2,
2020 evsel_script(ev2)->val,
2026 evsel_script(leader)->gnum = 0;
2030 static bool show_event(struct perf_sample *sample,
2031 struct evsel *evsel,
2032 struct thread *thread,
2033 struct addr_location *al,
2034 struct addr_location *addr_al)
2036 int depth = thread_stack__depth(thread, sample->cpu);
2038 if (!symbol_conf.graph_function)
2041 if (thread->filter) {
2042 if (depth <= thread->filter_entry_depth) {
2043 thread->filter = false;
2048 const char *s = symbol_conf.graph_function;
2050 const char *name = resolve_branch_sym(sample, evsel, thread, al, addr_al,
2056 nlen = strlen(name);
2058 unsigned len = strcspn(s, ",");
2059 if (nlen == len && !strncmp(name, s, len)) {
2060 thread->filter = true;
2061 thread->filter_entry_depth = depth;
2072 static void process_event(struct perf_script *script,
2073 struct perf_sample *sample, struct evsel *evsel,
2074 struct addr_location *al,
2075 struct addr_location *addr_al,
2076 struct machine *machine)
2078 struct thread *thread = al->thread;
2079 struct perf_event_attr *attr = &evsel->core.attr;
2080 unsigned int type = output_type(attr->type);
2081 struct evsel_script *es = evsel->priv;
2083 char str[PAGE_SIZE_NAME_LEN];
2084 const char *arch = perf_env__arch(machine->env);
2086 if (output[type].fields == 0)
2091 perf_sample__fprintf_start(script, sample, thread, evsel,
2092 PERF_RECORD_SAMPLE, fp);
2094 if (PRINT_FIELD(PERIOD))
2095 fprintf(fp, "%10" PRIu64 " ", sample->period);
2097 if (PRINT_FIELD(EVNAME)) {
2098 const char *evname = evsel__name(evsel);
2100 if (!script->name_width)
2101 script->name_width = evlist__max_name_len(script->session->evlist);
2103 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
2107 perf_sample__fprintf_flags(sample->flags, fp);
2109 if (is_bts_event(attr)) {
2110 perf_sample__fprintf_bts(sample, evsel, thread, al, addr_al, machine, fp);
2114 if (PRINT_FIELD(TRACE) && sample->raw_data) {
2115 event_format__fprintf(evsel->tp_format, sample->cpu,
2116 sample->raw_data, sample->raw_size, fp);
2119 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
2120 perf_sample__fprintf_synth(sample, evsel, fp);
2122 if (PRINT_FIELD(ADDR))
2123 perf_sample__fprintf_addr(sample, thread, attr, fp);
2125 if (PRINT_FIELD(DATA_SRC))
2126 data_src__fprintf(sample->data_src, fp);
2128 if (PRINT_FIELD(WEIGHT))
2129 fprintf(fp, "%16" PRIu64, sample->weight);
2131 if (PRINT_FIELD(INS_LAT))
2132 fprintf(fp, "%16" PRIu16, sample->ins_lat);
2134 if (PRINT_FIELD(IP)) {
2135 struct callchain_cursor *cursor = NULL;
2137 if (script->stitch_lbr)
2138 al->thread->lbr_stitch_enable = true;
2140 if (symbol_conf.use_callchain && sample->callchain &&
2141 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
2142 sample, NULL, NULL, scripting_max_stack) == 0)
2143 cursor = &callchain_cursor;
2145 fputc(cursor ? '\n' : ' ', fp);
2146 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor,
2147 symbol_conf.bt_stop_list, fp);
2150 if (PRINT_FIELD(IREGS))
2151 perf_sample__fprintf_iregs(sample, attr, arch, fp);
2153 if (PRINT_FIELD(UREGS))
2154 perf_sample__fprintf_uregs(sample, attr, arch, fp);
2156 if (PRINT_FIELD(BRSTACK))
2157 perf_sample__fprintf_brstack(sample, thread, attr, fp);
2158 else if (PRINT_FIELD(BRSTACKSYM))
2159 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
2160 else if (PRINT_FIELD(BRSTACKOFF))
2161 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
2163 if (evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
2164 perf_sample__fprintf_bpf_output(sample, fp);
2165 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
2167 if (PRINT_FIELD(PHYS_ADDR))
2168 fprintf(fp, "%16" PRIx64, sample->phys_addr);
2170 if (PRINT_FIELD(DATA_PAGE_SIZE))
2171 fprintf(fp, " %s", get_page_size_name(sample->data_page_size, str));
2173 if (PRINT_FIELD(CODE_PAGE_SIZE))
2174 fprintf(fp, " %s", get_page_size_name(sample->code_page_size, str));
2176 perf_sample__fprintf_ipc(sample, attr, fp);
2180 if (PRINT_FIELD(SRCCODE)) {
2181 if (map__fprintf_srccode(al->map, al->addr, stdout,
2182 &thread->srccode_state))
2186 if (PRINT_FIELD(METRIC))
2187 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
2193 static struct scripting_ops *scripting_ops;
2195 static void __process_stat(struct evsel *counter, u64 tstamp)
2197 int nthreads = perf_thread_map__nr(counter->core.threads);
2199 struct perf_cpu cpu;
2200 static int header_printed;
2202 if (counter->core.system_wide)
2205 if (!header_printed) {
2206 printf("%3s %8s %15s %15s %15s %15s %s\n",
2207 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
2211 for (thread = 0; thread < nthreads; thread++) {
2212 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
2213 struct perf_counts_values *counts;
2215 counts = perf_counts(counter->counts, idx, thread);
2217 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
2219 perf_thread_map__pid(counter->core.threads, thread),
2224 evsel__name(counter));
2229 static void process_stat(struct evsel *counter, u64 tstamp)
2231 if (scripting_ops && scripting_ops->process_stat)
2232 scripting_ops->process_stat(&stat_config, counter, tstamp);
2234 __process_stat(counter, tstamp);
2237 static void process_stat_interval(u64 tstamp)
2239 if (scripting_ops && scripting_ops->process_stat_interval)
2240 scripting_ops->process_stat_interval(tstamp);
2243 static void setup_scripting(void)
2245 setup_perl_scripting();
2246 setup_python_scripting();
2249 static int flush_scripting(void)
2251 return scripting_ops ? scripting_ops->flush_script() : 0;
2254 static int cleanup_scripting(void)
2256 pr_debug("\nperf script stopped\n");
2258 return scripting_ops ? scripting_ops->stop_script() : 0;
2261 static bool filter_cpu(struct perf_sample *sample)
2263 if (cpu_list && sample->cpu != (u32)-1)
2264 return !test_bit(sample->cpu, cpu_bitmap);
2268 static int process_sample_event(struct perf_tool *tool,
2269 union perf_event *event,
2270 struct perf_sample *sample,
2271 struct evsel *evsel,
2272 struct machine *machine)
2274 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2275 struct addr_location al;
2276 struct addr_location addr_al;
2279 /* Set thread to NULL to indicate addr_al and al are not initialized */
2280 addr_al.thread = NULL;
2283 ret = dlfilter__filter_event_early(dlfilter, event, sample, evsel, machine, &al, &addr_al);
2290 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2296 if (sample->time < last_timestamp) {
2297 pr_err("Samples misordered, previous: %" PRIu64
2298 " this: %" PRIu64 "\n", last_timestamp,
2302 last_timestamp = sample->time;
2306 if (filter_cpu(sample))
2309 if (!al.thread && machine__resolve(machine, &al, sample) < 0) {
2310 pr_err("problem processing %d event, skipping it.\n",
2311 event->header.type);
2319 if (!show_event(sample, evsel, al.thread, &al, &addr_al))
2322 if (evswitch__discard(&scr->evswitch, evsel))
2325 ret = dlfilter__filter_event(dlfilter, event, sample, evsel, machine, &al, &addr_al);
2332 if (scripting_ops) {
2333 struct addr_location *addr_al_ptr = NULL;
2335 if ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
2336 sample_addr_correlates_sym(&evsel->core.attr)) {
2337 if (!addr_al.thread)
2338 thread__resolve(al.thread, &addr_al, sample);
2339 addr_al_ptr = &addr_al;
2341 scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr);
2343 process_event(scr, sample, evsel, &al, &addr_al, machine);
2348 addr_location__put(&al);
2352 static int process_attr(struct perf_tool *tool, union perf_event *event,
2353 struct evlist **pevlist)
2355 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2356 struct evlist *evlist;
2357 struct evsel *evsel, *pos;
2360 static struct evsel_script *es;
2362 err = perf_event__process_attr(tool, event, pevlist);
2367 evsel = evlist__last(*pevlist);
2370 if (scr->per_event_dump) {
2371 evsel->priv = evsel_script__new(evsel, scr->session->data);
2373 es = zalloc(sizeof(*es));
2381 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2382 evsel->core.attr.type != PERF_TYPE_SYNTH)
2385 evlist__for_each_entry(evlist, pos) {
2386 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
2390 if (evsel->core.attr.sample_type) {
2391 err = evsel__check_attr(evsel, scr->session);
2397 * Check if we need to enable callchains based
2398 * on events sample_type.
2400 sample_type = evlist__combined_sample_type(evlist);
2401 callchain_param_setup(sample_type, perf_env__arch((*pevlist)->env));
2403 /* Enable fields for callchain entries */
2404 if (symbol_conf.use_callchain &&
2405 (sample_type & PERF_SAMPLE_CALLCHAIN ||
2406 sample_type & PERF_SAMPLE_BRANCH_STACK ||
2407 (sample_type & PERF_SAMPLE_REGS_USER &&
2408 sample_type & PERF_SAMPLE_STACK_USER))) {
2409 int type = output_type(evsel->core.attr.type);
2411 if (!(output[type].user_unset_fields & PERF_OUTPUT_IP))
2412 output[type].fields |= PERF_OUTPUT_IP;
2413 if (!(output[type].user_unset_fields & PERF_OUTPUT_SYM))
2414 output[type].fields |= PERF_OUTPUT_SYM;
2416 set_print_ip_opts(&evsel->core.attr);
2420 static int print_event_with_time(struct perf_tool *tool,
2421 union perf_event *event,
2422 struct perf_sample *sample,
2423 struct machine *machine,
2424 pid_t pid, pid_t tid, u64 timestamp)
2426 struct perf_script *script = container_of(tool, struct perf_script, tool);
2427 struct perf_session *session = script->session;
2428 struct evsel *evsel = evlist__id2evsel(session->evlist, sample->id);
2429 struct thread *thread = NULL;
2431 if (evsel && !evsel->core.attr.sample_id_all) {
2433 sample->time = timestamp;
2438 if (filter_cpu(sample))
2442 thread = machine__findnew_thread(machine, pid, tid);
2445 perf_sample__fprintf_start(script, sample, thread, evsel,
2446 event->header.type, stdout);
2449 perf_event__fprintf(event, machine, stdout);
2451 thread__put(thread);
2456 static int print_event(struct perf_tool *tool, union perf_event *event,
2457 struct perf_sample *sample, struct machine *machine,
2458 pid_t pid, pid_t tid)
2460 return print_event_with_time(tool, event, sample, machine, pid, tid, 0);
2463 static int process_comm_event(struct perf_tool *tool,
2464 union perf_event *event,
2465 struct perf_sample *sample,
2466 struct machine *machine)
2468 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2471 return print_event(tool, event, sample, machine, event->comm.pid,
2475 static int process_namespaces_event(struct perf_tool *tool,
2476 union perf_event *event,
2477 struct perf_sample *sample,
2478 struct machine *machine)
2480 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2483 return print_event(tool, event, sample, machine, event->namespaces.pid,
2484 event->namespaces.tid);
2487 static int process_cgroup_event(struct perf_tool *tool,
2488 union perf_event *event,
2489 struct perf_sample *sample,
2490 struct machine *machine)
2492 if (perf_event__process_cgroup(tool, event, sample, machine) < 0)
2495 return print_event(tool, event, sample, machine, sample->pid,
2499 static int process_fork_event(struct perf_tool *tool,
2500 union perf_event *event,
2501 struct perf_sample *sample,
2502 struct machine *machine)
2504 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2507 return print_event_with_time(tool, event, sample, machine,
2508 event->fork.pid, event->fork.tid,
2511 static int process_exit_event(struct perf_tool *tool,
2512 union perf_event *event,
2513 struct perf_sample *sample,
2514 struct machine *machine)
2516 /* Print before 'exit' deletes anything */
2517 if (print_event_with_time(tool, event, sample, machine, event->fork.pid,
2518 event->fork.tid, event->fork.time))
2521 return perf_event__process_exit(tool, event, sample, machine);
2524 static int process_mmap_event(struct perf_tool *tool,
2525 union perf_event *event,
2526 struct perf_sample *sample,
2527 struct machine *machine)
2529 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2532 return print_event(tool, event, sample, machine, event->mmap.pid,
2536 static int process_mmap2_event(struct perf_tool *tool,
2537 union perf_event *event,
2538 struct perf_sample *sample,
2539 struct machine *machine)
2541 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2544 return print_event(tool, event, sample, machine, event->mmap2.pid,
2548 static int process_switch_event(struct perf_tool *tool,
2549 union perf_event *event,
2550 struct perf_sample *sample,
2551 struct machine *machine)
2553 struct perf_script *script = container_of(tool, struct perf_script, tool);
2555 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2558 if (scripting_ops && scripting_ops->process_switch && !filter_cpu(sample))
2559 scripting_ops->process_switch(event, sample, machine);
2561 if (!script->show_switch_events)
2564 return print_event(tool, event, sample, machine, sample->pid,
2568 static int process_auxtrace_error(struct perf_session *session,
2569 union perf_event *event)
2571 if (scripting_ops && scripting_ops->process_auxtrace_error) {
2572 scripting_ops->process_auxtrace_error(session, event);
2576 return perf_event__process_auxtrace_error(session, event);
2580 process_lost_event(struct perf_tool *tool,
2581 union perf_event *event,
2582 struct perf_sample *sample,
2583 struct machine *machine)
2585 return print_event(tool, event, sample, machine, sample->pid,
2590 process_throttle_event(struct perf_tool *tool __maybe_unused,
2591 union perf_event *event,
2592 struct perf_sample *sample,
2593 struct machine *machine)
2595 if (scripting_ops && scripting_ops->process_throttle)
2596 scripting_ops->process_throttle(event, sample, machine);
2601 process_finished_round_event(struct perf_tool *tool __maybe_unused,
2602 union perf_event *event,
2603 struct ordered_events *oe __maybe_unused)
2606 perf_event__fprintf(event, NULL, stdout);
2611 process_bpf_events(struct perf_tool *tool __maybe_unused,
2612 union perf_event *event,
2613 struct perf_sample *sample,
2614 struct machine *machine)
2616 if (machine__process_ksymbol(machine, event, sample) < 0)
2619 return print_event(tool, event, sample, machine, sample->pid,
2623 static int process_text_poke_events(struct perf_tool *tool,
2624 union perf_event *event,
2625 struct perf_sample *sample,
2626 struct machine *machine)
2628 if (perf_event__process_text_poke(tool, event, sample, machine) < 0)
2631 return print_event(tool, event, sample, machine, sample->pid,
2635 static void sig_handler(int sig __maybe_unused)
2640 static void perf_script__fclose_per_event_dump(struct perf_script *script)
2642 struct evlist *evlist = script->session->evlist;
2643 struct evsel *evsel;
2645 evlist__for_each_entry(evlist, evsel) {
2648 evsel_script__delete(evsel->priv);
2653 static int perf_script__fopen_per_event_dump(struct perf_script *script)
2655 struct evsel *evsel;
2657 evlist__for_each_entry(script->session->evlist, evsel) {
2659 * Already setup? I.e. we may be called twice in cases like
2660 * Intel PT, one for the intel_pt// and dummy events, then
2661 * for the evsels synthesized from the auxtrace info.
2663 * Ses perf_script__process_auxtrace_info.
2665 if (evsel->priv != NULL)
2668 evsel->priv = evsel_script__new(evsel, script->session->data);
2669 if (evsel->priv == NULL)
2670 goto out_err_fclose;
2676 perf_script__fclose_per_event_dump(script);
2680 static int perf_script__setup_per_event_dump(struct perf_script *script)
2682 struct evsel *evsel;
2683 static struct evsel_script es_stdout;
2685 if (script->per_event_dump)
2686 return perf_script__fopen_per_event_dump(script);
2688 es_stdout.fp = stdout;
2690 evlist__for_each_entry(script->session->evlist, evsel)
2691 evsel->priv = &es_stdout;
2696 static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2698 struct evsel *evsel;
2700 evlist__for_each_entry(script->session->evlist, evsel) {
2701 struct evsel_script *es = evsel->priv;
2703 evsel_script__fprintf(es, stdout);
2704 evsel_script__delete(es);
2709 static void perf_script__exit(struct perf_script *script)
2711 perf_thread_map__put(script->threads);
2712 perf_cpu_map__put(script->cpus);
2715 static int __cmd_script(struct perf_script *script)
2719 signal(SIGINT, sig_handler);
2721 perf_stat__init_shadow_stats();
2723 /* override event processing functions */
2724 if (script->show_task_events) {
2725 script->tool.comm = process_comm_event;
2726 script->tool.fork = process_fork_event;
2727 script->tool.exit = process_exit_event;
2729 if (script->show_mmap_events) {
2730 script->tool.mmap = process_mmap_event;
2731 script->tool.mmap2 = process_mmap2_event;
2733 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
2734 script->tool.context_switch = process_switch_event;
2735 if (scripting_ops && scripting_ops->process_auxtrace_error)
2736 script->tool.auxtrace_error = process_auxtrace_error;
2737 if (script->show_namespace_events)
2738 script->tool.namespaces = process_namespaces_event;
2739 if (script->show_cgroup_events)
2740 script->tool.cgroup = process_cgroup_event;
2741 if (script->show_lost_events)
2742 script->tool.lost = process_lost_event;
2743 if (script->show_round_events) {
2744 script->tool.ordered_events = false;
2745 script->tool.finished_round = process_finished_round_event;
2747 if (script->show_bpf_events) {
2748 script->tool.ksymbol = process_bpf_events;
2749 script->tool.bpf = process_bpf_events;
2751 if (script->show_text_poke_events) {
2752 script->tool.ksymbol = process_bpf_events;
2753 script->tool.text_poke = process_text_poke_events;
2756 if (perf_script__setup_per_event_dump(script)) {
2757 pr_err("Couldn't create the per event dump files\n");
2761 ret = perf_session__process_events(script->session);
2763 if (script->per_event_dump)
2764 perf_script__exit_per_event_dump_stats(script);
2767 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
2772 struct script_spec {
2773 struct list_head node;
2774 struct scripting_ops *ops;
2778 static LIST_HEAD(script_specs);
2780 static struct script_spec *script_spec__new(const char *spec,
2781 struct scripting_ops *ops)
2783 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2786 strcpy(s->spec, spec);
2793 static void script_spec__add(struct script_spec *s)
2795 list_add_tail(&s->node, &script_specs);
2798 static struct script_spec *script_spec__find(const char *spec)
2800 struct script_spec *s;
2802 list_for_each_entry(s, &script_specs, node)
2803 if (strcasecmp(s->spec, spec) == 0)
2808 int script_spec_register(const char *spec, struct scripting_ops *ops)
2810 struct script_spec *s;
2812 s = script_spec__find(spec);
2816 s = script_spec__new(spec, ops);
2820 script_spec__add(s);
2825 static struct scripting_ops *script_spec__lookup(const char *spec)
2827 struct script_spec *s = script_spec__find(spec);
2834 static void list_available_languages(void)
2836 struct script_spec *s;
2838 fprintf(stderr, "\n");
2839 fprintf(stderr, "Scripting language extensions (used in "
2840 "perf script -s [spec:]script.[spec]):\n\n");
2842 list_for_each_entry(s, &script_specs, node)
2843 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2845 fprintf(stderr, "\n");
2848 /* Find script file relative to current directory or exec path */
2849 static char *find_script(const char *script)
2851 char path[PATH_MAX];
2853 if (!scripting_ops) {
2854 const char *ext = strrchr(script, '.');
2859 scripting_ops = script_spec__lookup(++ext);
2864 if (access(script, R_OK)) {
2865 char *exec_path = get_argv_exec_path();
2869 snprintf(path, sizeof(path), "%s/scripts/%s/%s",
2870 exec_path, scripting_ops->dirname, script);
2873 if (access(script, R_OK))
2876 return strdup(script);
2879 static int parse_scriptname(const struct option *opt __maybe_unused,
2880 const char *str, int unset __maybe_unused)
2882 char spec[PATH_MAX];
2883 const char *script, *ext;
2886 if (strcmp(str, "lang") == 0) {
2887 list_available_languages();
2891 script = strchr(str, ':');
2894 if (len >= PATH_MAX) {
2895 fprintf(stderr, "invalid language specifier");
2898 strncpy(spec, str, len);
2900 scripting_ops = script_spec__lookup(spec);
2901 if (!scripting_ops) {
2902 fprintf(stderr, "invalid language specifier");
2908 ext = strrchr(script, '.');
2910 fprintf(stderr, "invalid script extension");
2913 scripting_ops = script_spec__lookup(++ext);
2914 if (!scripting_ops) {
2915 fprintf(stderr, "invalid script extension");
2920 script_name = find_script(script);
2922 script_name = strdup(script);
2927 static int parse_output_fields(const struct option *opt __maybe_unused,
2928 const char *arg, int unset __maybe_unused)
2930 char *tok, *strtok_saveptr = NULL;
2931 int i, imax = ARRAY_SIZE(all_output_options);
2934 char *str = strdup(arg);
2936 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
2941 /* first word can state for which event type the user is specifying
2942 * the fields. If no type exists, the specified fields apply to all
2943 * event types found in the file minus the invalid fields for a type.
2945 tok = strchr(str, ':');
2949 if (!strcmp(str, "hw"))
2950 type = PERF_TYPE_HARDWARE;
2951 else if (!strcmp(str, "sw"))
2952 type = PERF_TYPE_SOFTWARE;
2953 else if (!strcmp(str, "trace"))
2954 type = PERF_TYPE_TRACEPOINT;
2955 else if (!strcmp(str, "raw"))
2956 type = PERF_TYPE_RAW;
2957 else if (!strcmp(str, "break"))
2958 type = PERF_TYPE_BREAKPOINT;
2959 else if (!strcmp(str, "synth"))
2960 type = OUTPUT_TYPE_SYNTH;
2962 fprintf(stderr, "Invalid event type in field string.\n");
2967 if (output[type].user_set)
2968 pr_warning("Overriding previous field request for %s events.\n",
2971 /* Don't override defaults for +- */
2972 if (strchr(tok, '+') || strchr(tok, '-'))
2975 output[type].fields = 0;
2976 output[type].user_set = true;
2977 output[type].wildcard_set = false;
2981 if (strlen(str) == 0) {
2983 "Cannot set fields to 'none' for all event types.\n");
2988 /* Don't override defaults for +- */
2989 if (strchr(str, '+') || strchr(str, '-'))
2992 if (output_set_by_user())
2993 pr_warning("Overriding previous field request for all events.\n");
2995 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2996 output[j].fields = 0;
2997 output[j].user_set = true;
2998 output[j].wildcard_set = true;
3003 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
3009 } else if (*tok == '-') {
3015 if (change != SET && change != DEFAULT)
3020 for (i = 0; i < imax; ++i) {
3021 if (strcmp(tok, all_output_options[i].str) == 0)
3024 if (i == imax && strcmp(tok, "flags") == 0) {
3025 print_flags = change != REMOVE;
3029 fprintf(stderr, "Invalid field requested.\n");
3035 /* add user option to all events types for
3038 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
3039 if (output[j].invalid_fields & all_output_options[i].field) {
3040 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
3041 all_output_options[i].str, event_type(j));
3043 if (change == REMOVE) {
3044 output[j].fields &= ~all_output_options[i].field;
3045 output[j].user_set_fields &= ~all_output_options[i].field;
3046 output[j].user_unset_fields |= all_output_options[i].field;
3048 output[j].fields |= all_output_options[i].field;
3049 output[j].user_set_fields |= all_output_options[i].field;
3050 output[j].user_unset_fields &= ~all_output_options[i].field;
3052 output[j].user_set = true;
3053 output[j].wildcard_set = true;
3057 if (output[type].invalid_fields & all_output_options[i].field) {
3058 fprintf(stderr, "\'%s\' not valid for %s events.\n",
3059 all_output_options[i].str, event_type(type));
3064 if (change == REMOVE)
3065 output[type].fields &= ~all_output_options[i].field;
3067 output[type].fields |= all_output_options[i].field;
3068 output[type].user_set = true;
3069 output[type].wildcard_set = true;
3074 if (output[type].fields == 0) {
3075 pr_debug("No fields requested for %s type. "
3076 "Events will not be displayed.\n", event_type(type));
3082 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
3089 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
3090 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
3091 if ((lang_dirent->d_type == DT_DIR || \
3092 (lang_dirent->d_type == DT_UNKNOWN && \
3093 is_directory(scripts_path, lang_dirent))) && \
3094 (strcmp(lang_dirent->d_name, ".")) && \
3095 (strcmp(lang_dirent->d_name, "..")))
3097 #define for_each_script(lang_path, lang_dir, script_dirent) \
3098 while ((script_dirent = readdir(lang_dir)) != NULL) \
3099 if (script_dirent->d_type != DT_DIR && \
3100 (script_dirent->d_type != DT_UNKNOWN || \
3101 !is_directory(lang_path, script_dirent)))
3104 #define RECORD_SUFFIX "-record"
3105 #define REPORT_SUFFIX "-report"
3107 struct script_desc {
3108 struct list_head node;
3114 static LIST_HEAD(script_descs);
3116 static struct script_desc *script_desc__new(const char *name)
3118 struct script_desc *s = zalloc(sizeof(*s));
3120 if (s != NULL && name)
3121 s->name = strdup(name);
3126 static void script_desc__delete(struct script_desc *s)
3129 zfree(&s->half_liner);
3134 static void script_desc__add(struct script_desc *s)
3136 list_add_tail(&s->node, &script_descs);
3139 static struct script_desc *script_desc__find(const char *name)
3141 struct script_desc *s;
3143 list_for_each_entry(s, &script_descs, node)
3144 if (strcasecmp(s->name, name) == 0)
3149 static struct script_desc *script_desc__findnew(const char *name)
3151 struct script_desc *s = script_desc__find(name);
3156 s = script_desc__new(name);
3160 script_desc__add(s);
3165 static const char *ends_with(const char *str, const char *suffix)
3167 size_t suffix_len = strlen(suffix);
3168 const char *p = str;
3170 if (strlen(str) > suffix_len) {
3171 p = str + strlen(str) - suffix_len;
3172 if (!strncmp(p, suffix, suffix_len))
3179 static int read_script_info(struct script_desc *desc, const char *filename)
3181 char line[BUFSIZ], *p;
3184 fp = fopen(filename, "r");
3188 while (fgets(line, sizeof(line), fp)) {
3189 p = skip_spaces(line);
3195 if (strlen(p) && *p == '!')
3199 if (strlen(p) && p[strlen(p) - 1] == '\n')
3200 p[strlen(p) - 1] = '\0';
3202 if (!strncmp(p, "description:", strlen("description:"))) {
3203 p += strlen("description:");
3204 desc->half_liner = strdup(skip_spaces(p));
3208 if (!strncmp(p, "args:", strlen("args:"))) {
3209 p += strlen("args:");
3210 desc->args = strdup(skip_spaces(p));
3220 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
3222 char *script_root, *str;
3224 script_root = strdup(script_dirent->d_name);
3228 str = (char *)ends_with(script_root, suffix);
3238 static int list_available_scripts(const struct option *opt __maybe_unused,
3239 const char *s __maybe_unused,
3240 int unset __maybe_unused)
3242 struct dirent *script_dirent, *lang_dirent;
3243 char scripts_path[MAXPATHLEN];
3244 DIR *scripts_dir, *lang_dir;
3245 char script_path[MAXPATHLEN];
3246 char lang_path[MAXPATHLEN];
3247 struct script_desc *desc;
3248 char first_half[BUFSIZ];
3251 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3253 scripts_dir = opendir(scripts_path);
3256 "open(%s) failed.\n"
3257 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
3262 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3263 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3264 lang_dirent->d_name);
3265 lang_dir = opendir(lang_path);
3269 for_each_script(lang_path, lang_dir, script_dirent) {
3270 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
3272 desc = script_desc__findnew(script_root);
3273 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3274 lang_path, script_dirent->d_name);
3275 read_script_info(desc, script_path);
3281 fprintf(stdout, "List of available trace scripts:\n");
3282 list_for_each_entry(desc, &script_descs, node) {
3283 sprintf(first_half, "%s %s", desc->name,
3284 desc->args ? desc->args : "");
3285 fprintf(stdout, " %-36s %s\n", first_half,
3286 desc->half_liner ? desc->half_liner : "");
3292 static int add_dlarg(const struct option *opt __maybe_unused,
3293 const char *s, int unset __maybe_unused)
3295 char *arg = strdup(s);
3301 a = realloc(dlargv, sizeof(dlargv[0]) * (dlargc + 1));
3308 dlargv[dlargc++] = arg;
3313 static void free_dlarg(void)
3316 free(dlargv[dlargc]);
3321 * Some scripts specify the required events in their "xxx-record" file,
3322 * this function will check if the events in perf.data match those
3323 * mentioned in the "xxx-record".
3325 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3326 * which is covered well now. And new parsing code should be added to
3327 * cover the future complex formats like event groups etc.
3329 static int check_ev_match(char *dir_name, char *scriptname,
3330 struct perf_session *session)
3332 char filename[MAXPATHLEN], evname[128];
3333 char line[BUFSIZ], *p;
3338 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
3340 fp = fopen(filename, "r");
3344 while (fgets(line, sizeof(line), fp)) {
3345 p = skip_spaces(line);
3350 p = strstr(p, "-e");
3356 len = strcspn(p, " \t");
3360 snprintf(evname, len + 1, "%s", p);
3363 evlist__for_each_entry(session->evlist, pos) {
3364 if (!strcmp(evsel__name(pos), evname)) {
3382 * Return -1 if none is found, otherwise the actual scripts number.
3384 * Currently the only user of this function is the script browser, which
3385 * will list all statically runnable scripts, select one, execute it and
3386 * show the output in a perf browser.
3388 int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3391 struct dirent *script_dirent, *lang_dirent;
3392 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
3393 DIR *scripts_dir, *lang_dir;
3394 struct perf_session *session;
3395 struct perf_data data = {
3397 .mode = PERF_DATA_MODE_READ,
3402 session = perf_session__new(&data, NULL);
3403 if (IS_ERR(session))
3404 return PTR_ERR(session);
3406 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3408 scripts_dir = opendir(scripts_path);
3410 perf_session__delete(session);
3414 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3415 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3416 lang_dirent->d_name);
3417 #ifndef HAVE_LIBPERL_SUPPORT
3418 if (strstr(lang_path, "perl"))
3421 #ifndef HAVE_LIBPYTHON_SUPPORT
3422 if (strstr(lang_path, "python"))
3426 lang_dir = opendir(lang_path);
3430 for_each_script(lang_path, lang_dir, script_dirent) {
3431 /* Skip those real time scripts: xxxtop.p[yl] */
3432 if (strstr(script_dirent->d_name, "top."))
3436 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3438 script_dirent->d_name);
3439 temp = strchr(script_dirent->d_name, '.');
3440 snprintf(scripts_array[i],
3441 (temp - script_dirent->d_name) + 1,
3442 "%s", script_dirent->d_name);
3444 if (check_ev_match(lang_path,
3445 scripts_array[i], session))
3453 closedir(scripts_dir);
3454 perf_session__delete(session);
3458 static char *get_script_path(const char *script_root, const char *suffix)
3460 struct dirent *script_dirent, *lang_dirent;
3461 char scripts_path[MAXPATHLEN];
3462 char script_path[MAXPATHLEN];
3463 DIR *scripts_dir, *lang_dir;
3464 char lang_path[MAXPATHLEN];
3465 char *__script_root;
3467 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3469 scripts_dir = opendir(scripts_path);
3473 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3474 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3475 lang_dirent->d_name);
3476 lang_dir = opendir(lang_path);
3480 for_each_script(lang_path, lang_dir, script_dirent) {
3481 __script_root = get_script_root(script_dirent, suffix);
3482 if (__script_root && !strcmp(script_root, __script_root)) {
3483 free(__script_root);
3484 closedir(scripts_dir);
3485 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3486 lang_path, script_dirent->d_name);
3488 return strdup(script_path);
3490 free(__script_root);
3494 closedir(scripts_dir);
3499 static bool is_top_script(const char *script_path)
3501 return ends_with(script_path, "top") != NULL;
3504 static int has_required_arg(char *script_path)
3506 struct script_desc *desc;
3510 desc = script_desc__new(NULL);
3512 if (read_script_info(desc, script_path))
3518 for (p = desc->args; *p; p++)
3522 script_desc__delete(desc);
3527 static int have_cmd(int argc, const char **argv)
3529 char **__argv = malloc(sizeof(const char *) * argc);
3532 pr_err("malloc failed\n");
3536 memcpy(__argv, argv, sizeof(const char *) * argc);
3537 argc = parse_options(argc, (const char **)__argv, record_options,
3538 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3541 system_wide = (argc == 0);
3546 static void script__setup_sample_type(struct perf_script *script)
3548 struct perf_session *session = script->session;
3549 u64 sample_type = evlist__combined_sample_type(session->evlist);
3551 callchain_param_setup(sample_type, perf_env__arch(session->machines.host.env));
3553 if (script->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) {
3554 pr_warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
3555 "Please apply --call-graph lbr when recording.\n");
3556 script->stitch_lbr = false;
3560 static int process_stat_round_event(struct perf_session *session,
3561 union perf_event *event)
3563 struct perf_record_stat_round *round = &event->stat_round;
3564 struct evsel *counter;
3566 evlist__for_each_entry(session->evlist, counter) {
3567 perf_stat_process_counter(&stat_config, counter);
3568 process_stat(counter, round->time);
3571 process_stat_interval(round->time);
3575 static int process_stat_config_event(struct perf_session *session __maybe_unused,
3576 union perf_event *event)
3578 perf_event__read_stat_config(&stat_config, &event->stat_config);
3582 static int set_maps(struct perf_script *script)
3584 struct evlist *evlist = script->session->evlist;
3586 if (!script->cpus || !script->threads)
3589 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3592 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
3594 if (evlist__alloc_stats(evlist, true))
3597 script->allocated = true;
3602 int process_thread_map_event(struct perf_session *session,
3603 union perf_event *event)
3605 struct perf_tool *tool = session->tool;
3606 struct perf_script *script = container_of(tool, struct perf_script, tool);
3608 if (script->threads) {
3609 pr_warning("Extra thread map event, ignoring.\n");
3613 script->threads = thread_map__new_event(&event->thread_map);
3614 if (!script->threads)
3617 return set_maps(script);
3621 int process_cpu_map_event(struct perf_session *session,
3622 union perf_event *event)
3624 struct perf_tool *tool = session->tool;
3625 struct perf_script *script = container_of(tool, struct perf_script, tool);
3628 pr_warning("Extra cpu map event, ignoring.\n");
3632 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3636 return set_maps(script);
3639 static int process_feature_event(struct perf_session *session,
3640 union perf_event *event)
3642 if (event->feat.feat_id < HEADER_LAST_FEATURE)
3643 return perf_event__process_feature(session, event);
3647 #ifdef HAVE_AUXTRACE_SUPPORT
3648 static int perf_script__process_auxtrace_info(struct perf_session *session,
3649 union perf_event *event)
3651 struct perf_tool *tool = session->tool;
3653 int ret = perf_event__process_auxtrace_info(session, event);
3656 struct perf_script *script = container_of(tool, struct perf_script, tool);
3658 ret = perf_script__setup_per_event_dump(script);
3664 #define perf_script__process_auxtrace_info 0
3667 static int parse_insn_trace(const struct option *opt __maybe_unused,
3668 const char *str __maybe_unused,
3669 int unset __maybe_unused)
3671 parse_output_fields(NULL, "+insn,-event,-period", 0);
3672 itrace_parse_synth_opts(opt, "i0ns", 0);
3673 symbol_conf.nanosecs = true;
3677 static int parse_xed(const struct option *opt __maybe_unused,
3678 const char *str __maybe_unused,
3679 int unset __maybe_unused)
3682 force_pager("xed -F insn: -A -64 | less");
3684 force_pager("xed -F insn: -A -64");
3688 static int parse_call_trace(const struct option *opt __maybe_unused,
3689 const char *str __maybe_unused,
3690 int unset __maybe_unused)
3692 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3693 itrace_parse_synth_opts(opt, "cewp", 0);
3694 symbol_conf.nanosecs = true;
3695 symbol_conf.pad_output_len_dso = 50;
3699 static int parse_callret_trace(const struct option *opt __maybe_unused,
3700 const char *str __maybe_unused,
3701 int unset __maybe_unused)
3703 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3704 itrace_parse_synth_opts(opt, "crewp", 0);
3705 symbol_conf.nanosecs = true;
3709 int cmd_script(int argc, const char **argv)
3711 bool show_full_info = false;
3712 bool header = false;
3713 bool header_only = false;
3714 bool script_started = false;
3715 char *rec_script_path = NULL;
3716 char *rep_script_path = NULL;
3717 struct perf_session *session;
3718 struct itrace_synth_opts itrace_synth_opts = {
3720 .default_no_sample = true,
3723 char *script_path = NULL;
3724 const char *dlfilter_file = NULL;
3725 const char **__argv;
3727 struct perf_script script = {
3729 .sample = process_sample_event,
3730 .mmap = perf_event__process_mmap,
3731 .mmap2 = perf_event__process_mmap2,
3732 .comm = perf_event__process_comm,
3733 .namespaces = perf_event__process_namespaces,
3734 .cgroup = perf_event__process_cgroup,
3735 .exit = perf_event__process_exit,
3736 .fork = perf_event__process_fork,
3737 .attr = process_attr,
3738 .event_update = perf_event__process_event_update,
3739 .tracing_data = perf_event__process_tracing_data,
3740 .feature = process_feature_event,
3741 .build_id = perf_event__process_build_id,
3742 .id_index = perf_event__process_id_index,
3743 .auxtrace_info = perf_script__process_auxtrace_info,
3744 .auxtrace = perf_event__process_auxtrace,
3745 .auxtrace_error = perf_event__process_auxtrace_error,
3746 .stat = perf_event__process_stat_event,
3747 .stat_round = process_stat_round_event,
3748 .stat_config = process_stat_config_event,
3749 .thread_map = process_thread_map_event,
3750 .cpu_map = process_cpu_map_event,
3751 .throttle = process_throttle_event,
3752 .unthrottle = process_throttle_event,
3753 .ordered_events = true,
3754 .ordering_requires_timestamps = true,
3757 struct perf_data data = {
3758 .mode = PERF_DATA_MODE_READ,
3760 const struct option options[] = {
3761 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3762 "dump raw trace in ASCII"),
3763 OPT_INCR('v', "verbose", &verbose,
3764 "be more verbose (show symbol address, etc)"),
3765 OPT_BOOLEAN('L', "Latency", &latency_format,
3766 "show latency attributes (irqs/preemption disabled, etc)"),
3767 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3768 list_available_scripts),
3769 OPT_CALLBACK_NOOPT(0, "list-dlfilters", NULL, NULL, "list available dlfilters",
3770 list_available_dlfilters),
3771 OPT_CALLBACK('s', "script", NULL, "name",
3772 "script file name (lang:script name, script name, or *)",
3774 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3775 "generate perf-script.xx script in specified language"),
3776 OPT_STRING(0, "dlfilter", &dlfilter_file, "file", "filter .so file name"),
3777 OPT_CALLBACK(0, "dlarg", NULL, "argument", "filter argument",
3779 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3780 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3781 "do various checks like samples ordering and lost events"),
3782 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3783 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
3784 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3785 "file", "vmlinux pathname"),
3786 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3787 "file", "kallsyms pathname"),
3788 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3789 "When printing symbols do not display call chain"),
3790 OPT_CALLBACK(0, "symfs", NULL, "directory",
3791 "Look for files with symbols relative to this directory",
3792 symbol__config_symfs),
3793 OPT_CALLBACK('F', "fields", NULL, "str",
3794 "comma separated output fields prepend with 'type:'. "
3795 "+field to add and -field to remove."
3796 "Valid types: hw,sw,trace,raw,synth. "
3797 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
3798 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3799 "brstacksym,flags,bpf-output,brstackinsn,brstackinsnlen,brstackoff,"
3800 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc,tod,"
3801 "data_page_size,code_page_size,ins_lat",
3802 parse_output_fields),
3803 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3804 "system-wide collection from all CPUs"),
3805 OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
3806 "only consider symbols in these DSOs"),
3807 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3808 "only consider these symbols"),
3809 OPT_INTEGER(0, "addr-range", &symbol_conf.addr_range,
3810 "Use with -S to list traced records within address range"),
3811 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3812 "Decode instructions from itrace", parse_insn_trace),
3813 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3814 "Run xed disassembler on output", parse_xed),
3815 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3816 "Decode calls from from itrace", parse_call_trace),
3817 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3818 "Decode calls and returns from itrace", parse_callret_trace),
3819 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3820 "Only print symbols and callees with --call-trace/--call-ret-trace"),
3821 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3822 "Stop display of callgraph at these symbols"),
3823 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3824 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3825 "only display events for these comms"),
3826 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3827 "only consider symbols in these pids"),
3828 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3829 "only consider symbols in these tids"),
3830 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3831 "Set the maximum stack depth when parsing the callchain, "
3832 "anything beyond the specified depth will be ignored. "
3833 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
3834 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
3835 OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
3836 OPT_BOOLEAN('I', "show-info", &show_full_info,
3837 "display extended information from perf.data file"),
3838 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3839 "Show the path of [kernel.kallsyms]"),
3840 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3841 "Show the fork/comm/exit events"),
3842 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3843 "Show the mmap events"),
3844 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3845 "Show context switch events (if recorded)"),
3846 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3847 "Show namespace events (if recorded)"),
3848 OPT_BOOLEAN('\0', "show-cgroup-events", &script.show_cgroup_events,
3849 "Show cgroup events (if recorded)"),
3850 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3851 "Show lost events (if recorded)"),
3852 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3853 "Show round events (if recorded)"),
3854 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3855 "Show bpf related events (if recorded)"),
3856 OPT_BOOLEAN('\0', "show-text-poke-events", &script.show_text_poke_events,
3857 "Show text poke related events (if recorded)"),
3858 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3859 "Dump trace output to files named by the monitored events"),
3860 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
3861 OPT_INTEGER(0, "max-blocks", &max_blocks,
3862 "Maximum number of code blocks to dump with brstackinsn"),
3863 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
3864 "Use 9 decimal places when displaying time"),
3865 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
3866 "Instruction Tracing options\n" ITRACE_HELP,
3867 itrace_parse_synth_opts),
3868 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3869 "Show full source file name path for source lines"),
3870 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3871 "Enable symbol demangling"),
3872 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3873 "Enable kernel symbol demangling"),
3874 OPT_STRING(0, "time", &script.time_str, "str",
3875 "Time span of interest (start,stop)"),
3876 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3877 "Show inline function"),
3878 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3879 "guest mount directory under which every guest os"
3880 " instance has a subdir"),
3881 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3882 "file", "file saving guest os vmlinux"),
3883 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3884 "file", "file saving guest os /proc/kallsyms"),
3885 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3886 "file", "file saving guest os /proc/modules"),
3887 OPT_BOOLEAN('\0', "stitch-lbr", &script.stitch_lbr,
3888 "Enable LBR callgraph stitching approach"),
3889 OPTS_EVSWITCH(&script.evswitch),
3892 const char * const script_subcommands[] = { "record", "report", NULL };
3893 const char *script_usage[] = {
3894 "perf script [<options>]",
3895 "perf script [<options>] record <script> [<record-options>] <command>",
3896 "perf script [<options>] report <script> [script-args]",
3897 "perf script [<options>] <script> [<record-options>] <command>",
3898 "perf script [<options>] <top-script> [script-args]",
3902 perf_set_singlethreaded();
3906 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
3907 PARSE_OPT_STOP_AT_NON_OPTION);
3909 if (symbol_conf.guestmount ||
3910 symbol_conf.default_guest_vmlinux_name ||
3911 symbol_conf.default_guest_kallsyms ||
3912 symbol_conf.default_guest_modules) {
3914 * Enable guest sample processing.
3919 data.path = input_name;
3920 data.force = symbol_conf.force;
3922 if (symbol__validate_sym_arguments())
3925 if (argc > 1 && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
3926 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3927 if (!rec_script_path)
3928 return cmd_record(argc, argv);
3931 if (argc > 1 && strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
3932 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3933 if (!rep_script_path) {
3935 "Please specify a valid report script"
3936 "(see 'perf script -l' for listing)\n");
3941 if (reltime && deltatime) {
3943 "reltime and deltatime - the two don't get along well. "
3944 "Please limit to --reltime or --deltatime.\n");
3948 if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
3949 itrace_synth_opts.callchain_sz > scripting_max_stack)
3950 scripting_max_stack = itrace_synth_opts.callchain_sz;
3952 /* make sure PERF_EXEC_PATH is set for scripts */
3953 set_argv_exec_path(get_argv_exec_path());
3955 if (argc && !script_name && !rec_script_path && !rep_script_path) {
3960 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3961 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3963 if (!rec_script_path && !rep_script_path) {
3964 script_name = find_script(argv[0]);
3970 usage_with_options_msg(script_usage, options,
3971 "Couldn't find script `%s'\n\n See perf"
3972 " script -l for available scripts.\n", argv[0]);
3975 if (is_top_script(argv[0])) {
3976 rep_args = argc - 1;
3980 rep_args = has_required_arg(rep_script_path);
3981 rec_args = (argc - 1) - rep_args;
3983 usage_with_options_msg(script_usage, options,
3984 "`%s' script requires options."
3985 "\n\n See perf script -l for available "
3986 "scripts and options.\n", argv[0]);
3990 if (pipe(live_pipe) < 0) {
3991 perror("failed to create pipe");
3997 perror("failed to fork");
4004 dup2(live_pipe[1], 1);
4005 close(live_pipe[0]);
4007 if (is_top_script(argv[0])) {
4009 } else if (!system_wide) {
4010 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
4016 __argv = malloc((argc + 6) * sizeof(const char *));
4018 pr_err("malloc failed\n");
4023 __argv[j++] = "/bin/sh";
4024 __argv[j++] = rec_script_path;
4030 for (i = rep_args + 1; i < argc; i++)
4031 __argv[j++] = argv[i];
4034 execvp("/bin/sh", (char **)__argv);
4039 dup2(live_pipe[0], 0);
4040 close(live_pipe[1]);
4042 __argv = malloc((argc + 4) * sizeof(const char *));
4044 pr_err("malloc failed\n");
4050 __argv[j++] = "/bin/sh";
4051 __argv[j++] = rep_script_path;
4052 for (i = 1; i < rep_args + 1; i++)
4053 __argv[j++] = argv[i];
4058 execvp("/bin/sh", (char **)__argv);
4063 if (rec_script_path)
4064 script_path = rec_script_path;
4065 if (rep_script_path)
4066 script_path = rep_script_path;
4071 if (!rec_script_path)
4072 system_wide = false;
4073 else if (!system_wide) {
4074 if (have_cmd(argc - 1, &argv[1]) != 0) {
4080 __argv = malloc((argc + 2) * sizeof(const char *));
4082 pr_err("malloc failed\n");
4087 __argv[j++] = "/bin/sh";
4088 __argv[j++] = script_path;
4091 for (i = 2; i < argc; i++)
4092 __argv[j++] = argv[i];
4095 execvp("/bin/sh", (char **)__argv);
4100 if (dlfilter_file) {
4101 dlfilter = dlfilter__new(dlfilter_file, dlargc, dlargv);
4111 session = perf_session__new(&data, &script.tool);
4112 if (IS_ERR(session))
4113 return PTR_ERR(session);
4115 if (header || header_only) {
4116 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
4117 perf_session__fprintf_info(session, stdout, show_full_info);
4122 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
4124 if (symbol__init(&session->header.env) < 0)
4128 if (data.is_pipe) { /* Assume pipe_mode indicates native_arch */
4130 } else if (session->header.env.arch) {
4131 if (!strcmp(uts.machine, session->header.env.arch))
4133 else if (!strcmp(uts.machine, "x86_64") &&
4134 !strcmp(session->header.env.arch, "i386"))
4138 script.session = session;
4139 script__setup_sample_type(&script);
4141 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
4142 symbol_conf.graph_function)
4143 itrace_synth_opts.thread_stack = true;
4145 session->itrace_synth_opts = &itrace_synth_opts;
4148 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
4151 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
4155 symbol_conf.use_callchain = true;
4157 symbol_conf.use_callchain = false;
4159 if (session->tevent.pevent &&
4160 tep_set_function_resolver(session->tevent.pevent,
4161 machine__resolve_kernel_addr,
4162 &session->machines.host) < 0) {
4163 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
4168 if (generate_script_lang) {
4169 struct stat perf_stat;
4172 if (output_set_by_user()) {
4174 "custom fields not supported for generated scripts");
4179 input = open(data.path, O_RDONLY); /* input_name */
4182 perror("failed to open file");
4186 err = fstat(input, &perf_stat);
4188 perror("failed to stat file");
4192 if (!perf_stat.st_size) {
4193 fprintf(stderr, "zero-sized file, nothing to do!\n");
4197 scripting_ops = script_spec__lookup(generate_script_lang);
4198 if (!scripting_ops) {
4199 fprintf(stderr, "invalid language specifier");
4204 err = scripting_ops->generate_script(session->tevent.pevent,
4209 err = dlfilter__start(dlfilter, session);
4214 err = scripting_ops->start_script(script_name, argc, argv, session);
4217 pr_debug("perf script started with script %s\n\n", script_name);
4218 script_started = true;
4222 err = perf_session__check_output_opt(session);
4226 if (script.time_str) {
4227 err = perf_time__parse_for_ranges_reltime(script.time_str, session,
4228 &script.ptime_range,
4235 itrace_synth_opts__set_time_range(&itrace_synth_opts,
4240 err = evswitch__init(&script.evswitch, session->evlist, stderr);
4244 if (zstd_init(&(session->zstd_data), 0) < 0)
4245 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
4247 err = __cmd_script(&script);
4252 if (script.ptime_range) {
4253 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
4254 zfree(&script.ptime_range);
4257 zstd_fini(&(session->zstd_data));
4258 evlist__free_stats(session->evlist);
4259 perf_session__delete(session);
4260 perf_script__exit(&script);
4263 cleanup_scripting();
4264 dlfilter__cleanup(dlfilter);