4 #include "util/cache.h"
5 #include "util/debug.h"
6 #include <subcmd/exec-cmd.h>
7 #include "util/header.h"
8 #include <subcmd/parse-options.h>
9 #include "util/perf_regs.h"
10 #include "util/session.h"
11 #include "util/tool.h"
12 #include "util/symbol.h"
13 #include "util/thread.h"
14 #include "util/trace-event.h"
15 #include "util/util.h"
16 #include "util/evlist.h"
17 #include "util/evsel.h"
18 #include "util/sort.h"
19 #include "util/data.h"
20 #include "util/auxtrace.h"
21 #include "util/cpumap.h"
22 #include "util/thread_map.h"
23 #include "util/stat.h"
24 #include "util/thread-stack.h"
25 #include "util/time-utils.h"
26 #include <linux/bitmap.h>
27 #include <linux/kernel.h>
28 #include <linux/stringify.h>
29 #include <linux/time64.h>
31 #include "util/mem-events.h"
32 #include "util/dump-insn.h"
35 static char const *script_name;
36 static char const *generate_script_lang;
37 static bool debug_mode;
38 static u64 last_timestamp;
39 static u64 nr_unordered;
40 static bool no_callchain;
41 static bool latency_format;
42 static bool system_wide;
43 static bool print_flags;
45 static const char *cpu_list;
46 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
47 static struct perf_stat_config stat_config;
48 static int max_blocks;
50 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
52 enum perf_output_field {
53 PERF_OUTPUT_COMM = 1U << 0,
54 PERF_OUTPUT_TID = 1U << 1,
55 PERF_OUTPUT_PID = 1U << 2,
56 PERF_OUTPUT_TIME = 1U << 3,
57 PERF_OUTPUT_CPU = 1U << 4,
58 PERF_OUTPUT_EVNAME = 1U << 5,
59 PERF_OUTPUT_TRACE = 1U << 6,
60 PERF_OUTPUT_IP = 1U << 7,
61 PERF_OUTPUT_SYM = 1U << 8,
62 PERF_OUTPUT_DSO = 1U << 9,
63 PERF_OUTPUT_ADDR = 1U << 10,
64 PERF_OUTPUT_SYMOFFSET = 1U << 11,
65 PERF_OUTPUT_SRCLINE = 1U << 12,
66 PERF_OUTPUT_PERIOD = 1U << 13,
67 PERF_OUTPUT_IREGS = 1U << 14,
68 PERF_OUTPUT_BRSTACK = 1U << 15,
69 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
70 PERF_OUTPUT_DATA_SRC = 1U << 17,
71 PERF_OUTPUT_WEIGHT = 1U << 18,
72 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
73 PERF_OUTPUT_CALLINDENT = 1U << 20,
74 PERF_OUTPUT_INSN = 1U << 21,
75 PERF_OUTPUT_INSNLEN = 1U << 22,
76 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
79 struct output_option {
81 enum perf_output_field field;
82 } all_output_options[] = {
83 {.str = "comm", .field = PERF_OUTPUT_COMM},
84 {.str = "tid", .field = PERF_OUTPUT_TID},
85 {.str = "pid", .field = PERF_OUTPUT_PID},
86 {.str = "time", .field = PERF_OUTPUT_TIME},
87 {.str = "cpu", .field = PERF_OUTPUT_CPU},
88 {.str = "event", .field = PERF_OUTPUT_EVNAME},
89 {.str = "trace", .field = PERF_OUTPUT_TRACE},
90 {.str = "ip", .field = PERF_OUTPUT_IP},
91 {.str = "sym", .field = PERF_OUTPUT_SYM},
92 {.str = "dso", .field = PERF_OUTPUT_DSO},
93 {.str = "addr", .field = PERF_OUTPUT_ADDR},
94 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
95 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
96 {.str = "period", .field = PERF_OUTPUT_PERIOD},
97 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
98 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
99 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
100 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
101 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
102 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
103 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
104 {.str = "insn", .field = PERF_OUTPUT_INSN},
105 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
106 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
109 /* default set to maintain compatibility with current format */
113 unsigned int print_ip_opts;
116 } output[PERF_TYPE_MAX] = {
118 [PERF_TYPE_HARDWARE] = {
121 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
122 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
123 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
124 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
127 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
130 [PERF_TYPE_SOFTWARE] = {
133 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
134 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
135 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
136 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
137 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
139 .invalid_fields = PERF_OUTPUT_TRACE,
142 [PERF_TYPE_TRACEPOINT] = {
145 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
146 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
147 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
153 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
154 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
155 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
156 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
157 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
158 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
160 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
163 [PERF_TYPE_BREAKPOINT] = {
166 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
167 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
168 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
169 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
172 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
176 static bool output_set_by_user(void)
179 for (j = 0; j < PERF_TYPE_MAX; ++j) {
180 if (output[j].user_set)
186 static const char *output_field2str(enum perf_output_field field)
188 int i, imax = ARRAY_SIZE(all_output_options);
189 const char *str = "";
191 for (i = 0; i < imax; ++i) {
192 if (all_output_options[i].field == field) {
193 str = all_output_options[i].str;
200 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
202 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
203 u64 sample_type, const char *sample_msg,
204 enum perf_output_field field,
207 struct perf_event_attr *attr = &evsel->attr;
208 int type = attr->type;
211 if (attr->sample_type & sample_type)
214 if (output[type].user_set) {
217 evname = perf_evsel__name(evsel);
218 pr_err("Samples for '%s' event do not have %s attribute set. "
219 "Cannot print '%s' field.\n",
220 evname, sample_msg, output_field2str(field));
224 /* user did not ask for it explicitly so remove from the default list */
225 output[type].fields &= ~field;
226 evname = perf_evsel__name(evsel);
227 pr_debug("Samples for '%s' event do not have %s attribute set. "
228 "Skipping '%s' field.\n",
229 evname, sample_msg, output_field2str(field));
234 static int perf_evsel__check_stype(struct perf_evsel *evsel,
235 u64 sample_type, const char *sample_msg,
236 enum perf_output_field field)
238 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
242 static int perf_evsel__check_attr(struct perf_evsel *evsel,
243 struct perf_session *session)
245 struct perf_event_attr *attr = &evsel->attr;
248 if (perf_header__has_feat(&session->header, HEADER_STAT))
251 allow_user_set = perf_header__has_feat(&session->header,
254 if (PRINT_FIELD(TRACE) &&
255 !perf_session__has_traces(session, "record -R"))
258 if (PRINT_FIELD(IP)) {
259 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
264 if (PRINT_FIELD(ADDR) &&
265 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
266 PERF_OUTPUT_ADDR, allow_user_set))
269 if (PRINT_FIELD(DATA_SRC) &&
270 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
271 PERF_OUTPUT_DATA_SRC))
274 if (PRINT_FIELD(WEIGHT) &&
275 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
279 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
280 pr_err("Display of symbols requested but neither sample IP nor "
281 "sample address\nis selected. Hence, no addresses to convert "
285 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
286 pr_err("Display of offsets requested but symbol is not"
290 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
291 pr_err("Display of DSO requested but neither sample IP nor "
292 "sample address\nis selected. Hence, no addresses to convert "
296 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
297 pr_err("Display of source line number requested but sample IP is not\n"
298 "selected. Hence, no address to lookup the source line number.\n");
301 if (PRINT_FIELD(BRSTACKINSN) &&
302 !(perf_evlist__combined_branch_type(session->evlist) &
303 PERF_SAMPLE_BRANCH_ANY)) {
304 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
305 "Hint: run 'perf record -b ...'\n");
308 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
309 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
310 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
313 if (PRINT_FIELD(TIME) &&
314 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
318 if (PRINT_FIELD(CPU) &&
319 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
320 PERF_OUTPUT_CPU, allow_user_set))
323 if (PRINT_FIELD(PERIOD) &&
324 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
328 if (PRINT_FIELD(IREGS) &&
329 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
336 static void set_print_ip_opts(struct perf_event_attr *attr)
338 unsigned int type = attr->type;
340 output[type].print_ip_opts = 0;
342 output[type].print_ip_opts |= EVSEL__PRINT_IP;
344 if (PRINT_FIELD(SYM))
345 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
347 if (PRINT_FIELD(DSO))
348 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
350 if (PRINT_FIELD(SYMOFFSET))
351 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
353 if (PRINT_FIELD(SRCLINE))
354 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
358 * verify all user requested events exist and the samples
359 * have the expected data
361 static int perf_session__check_output_opt(struct perf_session *session)
364 struct perf_evsel *evsel;
366 for (j = 0; j < PERF_TYPE_MAX; ++j) {
367 evsel = perf_session__find_first_evtype(session, j);
370 * even if fields is set to 0 (ie., show nothing) event must
371 * exist if user explicitly includes it on the command line
373 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
374 pr_err("%s events do not exist. "
375 "Remove corresponding -f option to proceed.\n",
380 if (evsel && output[j].fields &&
381 perf_evsel__check_attr(evsel, session))
387 set_print_ip_opts(&evsel->attr);
391 bool use_callchain = false;
392 bool not_pipe = false;
394 evlist__for_each_entry(session->evlist, evsel) {
396 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
397 use_callchain = true;
401 if (not_pipe && !use_callchain)
402 symbol_conf.use_callchain = false;
406 * set default for tracepoints to print symbols only
407 * if callchains are present
409 if (symbol_conf.use_callchain &&
410 !output[PERF_TYPE_TRACEPOINT].user_set) {
411 struct perf_event_attr *attr;
413 j = PERF_TYPE_TRACEPOINT;
415 evlist__for_each_entry(session->evlist, evsel) {
416 if (evsel->attr.type != j)
421 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
422 output[j].fields |= PERF_OUTPUT_IP;
423 output[j].fields |= PERF_OUTPUT_SYM;
424 output[j].fields |= PERF_OUTPUT_DSO;
425 set_print_ip_opts(attr);
435 static void print_sample_iregs(struct perf_sample *sample,
436 struct perf_event_attr *attr)
438 struct regs_dump *regs = &sample->intr_regs;
439 uint64_t mask = attr->sample_regs_intr;
445 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
446 u64 val = regs->regs[i++];
447 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
451 static void print_sample_start(struct perf_sample *sample,
452 struct thread *thread,
453 struct perf_evsel *evsel)
455 struct perf_event_attr *attr = &evsel->attr;
457 unsigned long long nsecs;
459 if (PRINT_FIELD(COMM)) {
461 printf("%8.8s ", thread__comm_str(thread));
462 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
463 printf("%s ", thread__comm_str(thread));
465 printf("%16s ", thread__comm_str(thread));
468 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
469 printf("%5d/%-5d ", sample->pid, sample->tid);
470 else if (PRINT_FIELD(PID))
471 printf("%5d ", sample->pid);
472 else if (PRINT_FIELD(TID))
473 printf("%5d ", sample->tid);
475 if (PRINT_FIELD(CPU)) {
477 printf("%3d ", sample->cpu);
479 printf("[%03d] ", sample->cpu);
482 if (PRINT_FIELD(TIME)) {
483 nsecs = sample->time;
484 secs = nsecs / NSEC_PER_SEC;
485 nsecs -= secs * NSEC_PER_SEC;
488 printf("%5lu.%09llu: ", secs, nsecs);
490 char sample_time[32];
491 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
492 printf("%12s: ", sample_time);
498 mispred_str(struct branch_entry *br)
500 if (!(br->flags.mispred || br->flags.predicted))
503 return br->flags.predicted ? 'P' : 'M';
506 static void print_sample_brstack(struct perf_sample *sample)
508 struct branch_stack *br = sample->branch_stack;
514 for (i = 0; i < br->nr; i++) {
515 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
518 mispred_str( br->entries + i),
519 br->entries[i].flags.in_tx? 'X' : '-',
520 br->entries[i].flags.abort? 'A' : '-',
521 br->entries[i].flags.cycles);
525 static void print_sample_brstacksym(struct perf_sample *sample,
526 struct thread *thread)
528 struct branch_stack *br = sample->branch_stack;
529 struct addr_location alf, alt;
535 for (i = 0; i < br->nr; i++) {
537 memset(&alf, 0, sizeof(alf));
538 memset(&alt, 0, sizeof(alt));
539 from = br->entries[i].from;
540 to = br->entries[i].to;
542 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
544 alf.sym = map__find_symbol(alf.map, alf.addr);
546 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
548 alt.sym = map__find_symbol(alt.map, alt.addr);
550 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
552 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
553 printf("/%c/%c/%c/%d ",
554 mispred_str( br->entries + i),
555 br->entries[i].flags.in_tx? 'X' : '-',
556 br->entries[i].flags.abort? 'A' : '-',
557 br->entries[i].flags.cycles);
561 #define MAXBB 16384UL
563 static int grab_bb(u8 *buffer, u64 start, u64 end,
564 struct machine *machine, struct thread *thread,
565 bool *is64bit, u8 *cpumode, bool last)
568 struct addr_location al;
574 kernel = machine__kernel_ip(machine, start);
576 *cpumode = PERF_RECORD_MISC_KERNEL;
578 *cpumode = PERF_RECORD_MISC_USER;
581 * Block overlaps between kernel and user.
582 * This can happen due to ring filtering
583 * On Intel CPUs the entry into the kernel is filtered,
584 * but the exit is not. Let the caller patch it up.
586 if (kernel != machine__kernel_ip(machine, end)) {
587 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
592 memset(&al, 0, sizeof(al));
593 if (end - start > MAXBB - MAXINSN) {
595 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
597 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
601 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
602 if (!al.map || !al.map->dso) {
603 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
606 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
607 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
611 /* Load maps to ensure dso->is_64_bit has been updated */
614 offset = al.map->map_ip(al.map, start);
615 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
616 end - start + MAXINSN);
618 *is64bit = al.map->dso->is_64_bit;
620 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
625 static void print_jump(uint64_t ip, struct branch_entry *en,
626 struct perf_insn *x, u8 *inbuf, int len,
629 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
631 dump_insn(x, ip, inbuf, len, NULL),
632 en->flags.predicted ? " PRED" : "",
633 en->flags.mispred ? " MISPRED" : "",
634 en->flags.in_tx ? " INTX" : "",
635 en->flags.abort ? " ABORT" : "");
636 if (en->flags.cycles) {
637 printf(" %d cycles", en->flags.cycles);
639 printf(" %.2f IPC", (float)insn / en->flags.cycles);
644 static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
645 uint64_t addr, struct symbol **lastsym,
646 struct perf_event_attr *attr)
648 struct addr_location al;
651 memset(&al, 0, sizeof(al));
653 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
655 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
657 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
663 al.sym = map__find_symbol(al.map, al.addr);
668 if (al.addr < al.sym->end)
669 off = al.addr - al.sym->start;
671 off = al.addr - al.map->start - al.sym->start;
672 printf("\t%s", al.sym->name);
676 if (PRINT_FIELD(SRCLINE))
677 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
682 static void print_sample_brstackinsn(struct perf_sample *sample,
683 struct thread *thread,
684 struct perf_event_attr *attr,
685 struct machine *machine)
687 struct branch_stack *br = sample->branch_stack;
689 int i, insn, len, nr, ilen;
693 struct symbol *lastsym = NULL;
698 if (max_blocks && nr > max_blocks + 1)
706 /* Handle first from jump, of which we don't know the entry. */
707 len = grab_bb(buffer, br->entries[nr-1].from,
708 br->entries[nr-1].from,
709 machine, thread, &x.is64bit, &x.cpumode, false);
711 print_ip_sym(thread, x.cpumode, x.cpu,
712 br->entries[nr - 1].from, &lastsym, attr);
713 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
717 /* Print all blocks */
718 for (i = nr - 2; i >= 0; i--) {
719 if (br->entries[i].from || br->entries[i].to)
720 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
723 start = br->entries[i + 1].to;
724 end = br->entries[i].from;
726 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
727 /* Patch up missing kernel transfers due to ring filters */
728 if (len == -ENXIO && i > 0) {
729 end = br->entries[--i].from;
730 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
731 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
737 for (off = 0;; off += ilen) {
738 uint64_t ip = start + off;
740 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
742 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
745 printf("\t%016" PRIx64 "\t%s\n", ip,
746 dump_insn(&x, ip, buffer + off, len - off, &ilen));
755 * Hit the branch? In this case we are already done, and the target
756 * has not been executed yet.
758 if (br->entries[0].from == sample->ip)
760 if (br->entries[0].flags.abort)
764 * Print final block upto sample
766 start = br->entries[0].to;
768 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
769 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
771 /* Print at least last IP if basic block did not work */
772 len = grab_bb(buffer, sample->ip, sample->ip,
773 machine, thread, &x.is64bit, &x.cpumode, false);
777 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
778 dump_insn(&x, sample->ip, buffer, len, NULL));
781 for (off = 0; off <= end - start; off += ilen) {
782 printf("\t%016" PRIx64 "\t%s\n", start + off,
783 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
789 static void print_sample_addr(struct perf_sample *sample,
790 struct thread *thread,
791 struct perf_event_attr *attr)
793 struct addr_location al;
795 printf("%16" PRIx64, sample->addr);
797 if (!sample_addr_correlates_sym(attr))
800 thread__resolve(thread, &al, sample);
802 if (PRINT_FIELD(SYM)) {
804 if (PRINT_FIELD(SYMOFFSET))
805 symbol__fprintf_symname_offs(al.sym, &al, stdout);
807 symbol__fprintf_symname(al.sym, stdout);
810 if (PRINT_FIELD(DSO)) {
812 map__fprintf_dsoname(al.map, stdout);
817 static void print_sample_callindent(struct perf_sample *sample,
818 struct perf_evsel *evsel,
819 struct thread *thread,
820 struct addr_location *al)
822 struct perf_event_attr *attr = &evsel->attr;
823 size_t depth = thread_stack__depth(thread);
824 struct addr_location addr_al;
825 const char *name = NULL;
831 * The 'return' has already been popped off the stack so the depth has
832 * to be adjusted to match the 'call'.
834 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
837 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
838 if (sample_addr_correlates_sym(attr)) {
839 thread__resolve(thread, &addr_al, sample);
841 name = addr_al.sym->name;
847 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
849 name = al->sym->name;
855 len = printf("%*s%s", (int)depth * 4, "", name);
857 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
863 * Try to keep the output length from changing frequently so that the
864 * output lines up more nicely.
866 if (len > spacing || (len && len < spacing - 52))
867 spacing = round_up(len + 4, 32);
870 printf("%*s", spacing - len, "");
873 static void print_insn(struct perf_sample *sample,
874 struct perf_event_attr *attr,
875 struct thread *thread,
876 struct machine *machine)
878 if (PRINT_FIELD(INSNLEN))
879 printf(" ilen: %d", sample->insn_len);
880 if (PRINT_FIELD(INSN)) {
884 for (i = 0; i < sample->insn_len; i++)
885 printf(" %02x", (unsigned char)sample->insn[i]);
887 if (PRINT_FIELD(BRSTACKINSN))
888 print_sample_brstackinsn(sample, thread, attr, machine);
891 static void print_sample_bts(struct perf_sample *sample,
892 struct perf_evsel *evsel,
893 struct thread *thread,
894 struct addr_location *al,
895 struct machine *machine)
897 struct perf_event_attr *attr = &evsel->attr;
898 bool print_srcline_last = false;
900 if (PRINT_FIELD(CALLINDENT))
901 print_sample_callindent(sample, evsel, thread, al);
903 /* print branch_from information */
904 if (PRINT_FIELD(IP)) {
905 unsigned int print_opts = output[attr->type].print_ip_opts;
906 struct callchain_cursor *cursor = NULL;
908 if (symbol_conf.use_callchain && sample->callchain &&
909 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
910 sample, NULL, NULL, scripting_max_stack) == 0)
911 cursor = &callchain_cursor;
913 if (cursor == NULL) {
915 if (print_opts & EVSEL__PRINT_SRCLINE) {
916 print_srcline_last = true;
917 print_opts &= ~EVSEL__PRINT_SRCLINE;
922 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
925 /* print branch_to information */
926 if (PRINT_FIELD(ADDR) ||
927 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
928 !output[attr->type].user_set)) {
930 print_sample_addr(sample, thread, attr);
933 if (print_srcline_last)
934 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
936 print_insn(sample, attr, thread, machine);
945 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
946 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
947 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
948 {PERF_IP_FLAG_BRANCH, "jmp"},
949 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
950 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
951 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
952 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
953 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
954 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
955 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
956 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
957 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
961 static void print_sample_flags(u32 flags)
963 const char *chars = PERF_IP_FLAG_CHARS;
964 const int n = strlen(PERF_IP_FLAG_CHARS);
965 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
966 const char *name = NULL;
970 for (i = 0; sample_flags[i].name ; i++) {
971 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
972 name = sample_flags[i].name;
977 for (i = 0; i < n; i++, flags >>= 1) {
979 str[pos++] = chars[i];
981 for (; i < 32; i++, flags >>= 1) {
988 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
990 printf(" %-11s ", str);
993 struct printer_data {
1000 print_sample_bpf_output_printer(enum binary_printer_ops op,
1004 unsigned char ch = (unsigned char)val;
1005 struct printer_data *printer_data = extra;
1008 case BINARY_PRINT_DATA_BEGIN:
1011 case BINARY_PRINT_LINE_BEGIN:
1012 printf("%17s", !printer_data->line_no ? "BPF output:" :
1015 case BINARY_PRINT_ADDR:
1016 printf(" %04x:", val);
1018 case BINARY_PRINT_NUM_DATA:
1019 printf(" %02x", val);
1021 case BINARY_PRINT_NUM_PAD:
1024 case BINARY_PRINT_SEP:
1027 case BINARY_PRINT_CHAR_DATA:
1028 if (printer_data->hit_nul && ch)
1029 printer_data->is_printable = false;
1034 if (!printer_data->is_printable)
1038 printer_data->hit_nul = true;
1040 printer_data->is_printable = false;
1045 case BINARY_PRINT_CHAR_PAD:
1048 case BINARY_PRINT_LINE_END:
1050 printer_data->line_no++;
1052 case BINARY_PRINT_DATA_END:
1058 static void print_sample_bpf_output(struct perf_sample *sample)
1060 unsigned int nr_bytes = sample->raw_size;
1061 struct printer_data printer_data = {0, false, true};
1063 print_binary(sample->raw_data, nr_bytes, 8,
1064 print_sample_bpf_output_printer, &printer_data);
1066 if (printer_data.is_printable && printer_data.hit_nul)
1067 printf("%17s \"%s\"\n", "BPF string:",
1068 (char *)(sample->raw_data));
1071 struct perf_script {
1072 struct perf_tool tool;
1073 struct perf_session *session;
1074 bool show_task_events;
1075 bool show_mmap_events;
1076 bool show_switch_events;
1077 bool show_namespace_events;
1079 struct cpu_map *cpus;
1080 struct thread_map *threads;
1082 const char *time_str;
1083 struct perf_time_interval ptime;
1086 static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1088 struct perf_evsel *evsel;
1091 evlist__for_each_entry(evlist, evsel) {
1092 int len = strlen(perf_evsel__name(evsel));
1094 max = MAX(len, max);
1100 static size_t data_src__printf(u64 data_src)
1102 struct mem_info mi = { .data_src.val = data_src };
1108 perf_script__meminfo_scnprintf(decode, 100, &mi);
1110 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1114 return printf("%-*s", maxlen, out);
1117 static void process_event(struct perf_script *script,
1118 struct perf_sample *sample, struct perf_evsel *evsel,
1119 struct addr_location *al,
1120 struct machine *machine)
1122 struct thread *thread = al->thread;
1123 struct perf_event_attr *attr = &evsel->attr;
1125 if (output[attr->type].fields == 0)
1128 print_sample_start(sample, thread, evsel);
1130 if (PRINT_FIELD(PERIOD))
1131 printf("%10" PRIu64 " ", sample->period);
1133 if (PRINT_FIELD(EVNAME)) {
1134 const char *evname = perf_evsel__name(evsel);
1136 if (!script->name_width)
1137 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1139 printf("%*s: ", script->name_width,
1140 evname ? evname : "[unknown]");
1144 print_sample_flags(sample->flags);
1146 if (is_bts_event(attr)) {
1147 print_sample_bts(sample, evsel, thread, al, machine);
1151 if (PRINT_FIELD(TRACE))
1152 event_format__print(evsel->tp_format, sample->cpu,
1153 sample->raw_data, sample->raw_size);
1154 if (PRINT_FIELD(ADDR))
1155 print_sample_addr(sample, thread, attr);
1157 if (PRINT_FIELD(DATA_SRC))
1158 data_src__printf(sample->data_src);
1160 if (PRINT_FIELD(WEIGHT))
1161 printf("%16" PRIu64, sample->weight);
1163 if (PRINT_FIELD(IP)) {
1164 struct callchain_cursor *cursor = NULL;
1166 if (symbol_conf.use_callchain && sample->callchain &&
1167 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1168 sample, NULL, NULL, scripting_max_stack) == 0)
1169 cursor = &callchain_cursor;
1171 putchar(cursor ? '\n' : ' ');
1172 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
1175 if (PRINT_FIELD(IREGS))
1176 print_sample_iregs(sample, attr);
1178 if (PRINT_FIELD(BRSTACK))
1179 print_sample_brstack(sample);
1180 else if (PRINT_FIELD(BRSTACKSYM))
1181 print_sample_brstacksym(sample, thread);
1183 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1184 print_sample_bpf_output(sample);
1185 print_insn(sample, attr, thread, machine);
1189 static struct scripting_ops *scripting_ops;
1191 static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1193 int nthreads = thread_map__nr(counter->threads);
1194 int ncpus = perf_evsel__nr_cpus(counter);
1196 static int header_printed;
1198 if (counter->system_wide)
1201 if (!header_printed) {
1202 printf("%3s %8s %15s %15s %15s %15s %s\n",
1203 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1207 for (thread = 0; thread < nthreads; thread++) {
1208 for (cpu = 0; cpu < ncpus; cpu++) {
1209 struct perf_counts_values *counts;
1211 counts = perf_counts(counter->counts, cpu, thread);
1213 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1214 counter->cpus->map[cpu],
1215 thread_map__pid(counter->threads, thread),
1220 perf_evsel__name(counter));
1225 static void process_stat(struct perf_evsel *counter, u64 tstamp)
1227 if (scripting_ops && scripting_ops->process_stat)
1228 scripting_ops->process_stat(&stat_config, counter, tstamp);
1230 __process_stat(counter, tstamp);
1233 static void process_stat_interval(u64 tstamp)
1235 if (scripting_ops && scripting_ops->process_stat_interval)
1236 scripting_ops->process_stat_interval(tstamp);
1239 static void setup_scripting(void)
1241 setup_perl_scripting();
1242 setup_python_scripting();
1245 static int flush_scripting(void)
1247 return scripting_ops ? scripting_ops->flush_script() : 0;
1250 static int cleanup_scripting(void)
1252 pr_debug("\nperf script stopped\n");
1254 return scripting_ops ? scripting_ops->stop_script() : 0;
1257 static int process_sample_event(struct perf_tool *tool,
1258 union perf_event *event,
1259 struct perf_sample *sample,
1260 struct perf_evsel *evsel,
1261 struct machine *machine)
1263 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1264 struct addr_location al;
1266 if (perf_time__skip_sample(&scr->ptime, sample->time))
1270 if (sample->time < last_timestamp) {
1271 pr_err("Samples misordered, previous: %" PRIu64
1272 " this: %" PRIu64 "\n", last_timestamp,
1276 last_timestamp = sample->time;
1280 if (machine__resolve(machine, &al, sample) < 0) {
1281 pr_err("problem processing %d event, skipping it.\n",
1282 event->header.type);
1289 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
1293 scripting_ops->process_event(event, sample, evsel, &al);
1295 process_event(scr, sample, evsel, &al, machine);
1298 addr_location__put(&al);
1302 static int process_attr(struct perf_tool *tool, union perf_event *event,
1303 struct perf_evlist **pevlist)
1305 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1306 struct perf_evlist *evlist;
1307 struct perf_evsel *evsel, *pos;
1310 err = perf_event__process_attr(tool, event, pevlist);
1315 evsel = perf_evlist__last(*pevlist);
1317 if (evsel->attr.type >= PERF_TYPE_MAX)
1320 evlist__for_each_entry(evlist, pos) {
1321 if (pos->attr.type == evsel->attr.type && pos != evsel)
1325 set_print_ip_opts(&evsel->attr);
1327 if (evsel->attr.sample_type)
1328 err = perf_evsel__check_attr(evsel, scr->session);
1333 static int process_comm_event(struct perf_tool *tool,
1334 union perf_event *event,
1335 struct perf_sample *sample,
1336 struct machine *machine)
1338 struct thread *thread;
1339 struct perf_script *script = container_of(tool, struct perf_script, tool);
1340 struct perf_session *session = script->session;
1341 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1344 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1345 if (thread == NULL) {
1346 pr_debug("problem processing COMM event, skipping it.\n");
1350 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1353 if (!evsel->attr.sample_id_all) {
1356 sample->tid = event->comm.tid;
1357 sample->pid = event->comm.pid;
1359 print_sample_start(sample, thread, evsel);
1360 perf_event__fprintf(event, stdout);
1363 thread__put(thread);
1367 static int process_namespaces_event(struct perf_tool *tool,
1368 union perf_event *event,
1369 struct perf_sample *sample,
1370 struct machine *machine)
1372 struct thread *thread;
1373 struct perf_script *script = container_of(tool, struct perf_script, tool);
1374 struct perf_session *session = script->session;
1375 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1378 thread = machine__findnew_thread(machine, event->namespaces.pid,
1379 event->namespaces.tid);
1380 if (thread == NULL) {
1381 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1385 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1388 if (!evsel->attr.sample_id_all) {
1391 sample->tid = event->namespaces.tid;
1392 sample->pid = event->namespaces.pid;
1394 print_sample_start(sample, thread, evsel);
1395 perf_event__fprintf(event, stdout);
1398 thread__put(thread);
1402 static int process_fork_event(struct perf_tool *tool,
1403 union perf_event *event,
1404 struct perf_sample *sample,
1405 struct machine *machine)
1407 struct thread *thread;
1408 struct perf_script *script = container_of(tool, struct perf_script, tool);
1409 struct perf_session *session = script->session;
1410 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1412 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1415 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1416 if (thread == NULL) {
1417 pr_debug("problem processing FORK event, skipping it.\n");
1421 if (!evsel->attr.sample_id_all) {
1423 sample->time = event->fork.time;
1424 sample->tid = event->fork.tid;
1425 sample->pid = event->fork.pid;
1427 print_sample_start(sample, thread, evsel);
1428 perf_event__fprintf(event, stdout);
1429 thread__put(thread);
1433 static int process_exit_event(struct perf_tool *tool,
1434 union perf_event *event,
1435 struct perf_sample *sample,
1436 struct machine *machine)
1439 struct thread *thread;
1440 struct perf_script *script = container_of(tool, struct perf_script, tool);
1441 struct perf_session *session = script->session;
1442 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1444 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1445 if (thread == NULL) {
1446 pr_debug("problem processing EXIT event, skipping it.\n");
1450 if (!evsel->attr.sample_id_all) {
1453 sample->tid = event->fork.tid;
1454 sample->pid = event->fork.pid;
1456 print_sample_start(sample, thread, evsel);
1457 perf_event__fprintf(event, stdout);
1459 if (perf_event__process_exit(tool, event, sample, machine) < 0)
1462 thread__put(thread);
1466 static int process_mmap_event(struct perf_tool *tool,
1467 union perf_event *event,
1468 struct perf_sample *sample,
1469 struct machine *machine)
1471 struct thread *thread;
1472 struct perf_script *script = container_of(tool, struct perf_script, tool);
1473 struct perf_session *session = script->session;
1474 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1476 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1479 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1480 if (thread == NULL) {
1481 pr_debug("problem processing MMAP event, skipping it.\n");
1485 if (!evsel->attr.sample_id_all) {
1488 sample->tid = event->mmap.tid;
1489 sample->pid = event->mmap.pid;
1491 print_sample_start(sample, thread, evsel);
1492 perf_event__fprintf(event, stdout);
1493 thread__put(thread);
1497 static int process_mmap2_event(struct perf_tool *tool,
1498 union perf_event *event,
1499 struct perf_sample *sample,
1500 struct machine *machine)
1502 struct thread *thread;
1503 struct perf_script *script = container_of(tool, struct perf_script, tool);
1504 struct perf_session *session = script->session;
1505 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1507 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1510 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1511 if (thread == NULL) {
1512 pr_debug("problem processing MMAP2 event, skipping it.\n");
1516 if (!evsel->attr.sample_id_all) {
1519 sample->tid = event->mmap2.tid;
1520 sample->pid = event->mmap2.pid;
1522 print_sample_start(sample, thread, evsel);
1523 perf_event__fprintf(event, stdout);
1524 thread__put(thread);
1528 static int process_switch_event(struct perf_tool *tool,
1529 union perf_event *event,
1530 struct perf_sample *sample,
1531 struct machine *machine)
1533 struct thread *thread;
1534 struct perf_script *script = container_of(tool, struct perf_script, tool);
1535 struct perf_session *session = script->session;
1536 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1538 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1541 thread = machine__findnew_thread(machine, sample->pid,
1543 if (thread == NULL) {
1544 pr_debug("problem processing SWITCH event, skipping it.\n");
1548 print_sample_start(sample, thread, evsel);
1549 perf_event__fprintf(event, stdout);
1550 thread__put(thread);
1554 static void sig_handler(int sig __maybe_unused)
1559 static int __cmd_script(struct perf_script *script)
1563 signal(SIGINT, sig_handler);
1565 /* override event processing functions */
1566 if (script->show_task_events) {
1567 script->tool.comm = process_comm_event;
1568 script->tool.fork = process_fork_event;
1569 script->tool.exit = process_exit_event;
1571 if (script->show_mmap_events) {
1572 script->tool.mmap = process_mmap_event;
1573 script->tool.mmap2 = process_mmap2_event;
1575 if (script->show_switch_events)
1576 script->tool.context_switch = process_switch_event;
1577 if (script->show_namespace_events)
1578 script->tool.namespaces = process_namespaces_event;
1580 ret = perf_session__process_events(script->session);
1583 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
1588 struct script_spec {
1589 struct list_head node;
1590 struct scripting_ops *ops;
1594 static LIST_HEAD(script_specs);
1596 static struct script_spec *script_spec__new(const char *spec,
1597 struct scripting_ops *ops)
1599 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1602 strcpy(s->spec, spec);
1609 static void script_spec__add(struct script_spec *s)
1611 list_add_tail(&s->node, &script_specs);
1614 static struct script_spec *script_spec__find(const char *spec)
1616 struct script_spec *s;
1618 list_for_each_entry(s, &script_specs, node)
1619 if (strcasecmp(s->spec, spec) == 0)
1624 int script_spec_register(const char *spec, struct scripting_ops *ops)
1626 struct script_spec *s;
1628 s = script_spec__find(spec);
1632 s = script_spec__new(spec, ops);
1636 script_spec__add(s);
1641 static struct scripting_ops *script_spec__lookup(const char *spec)
1643 struct script_spec *s = script_spec__find(spec);
1650 static void list_available_languages(void)
1652 struct script_spec *s;
1654 fprintf(stderr, "\n");
1655 fprintf(stderr, "Scripting language extensions (used in "
1656 "perf script -s [spec:]script.[spec]):\n\n");
1658 list_for_each_entry(s, &script_specs, node)
1659 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1661 fprintf(stderr, "\n");
1664 static int parse_scriptname(const struct option *opt __maybe_unused,
1665 const char *str, int unset __maybe_unused)
1667 char spec[PATH_MAX];
1668 const char *script, *ext;
1671 if (strcmp(str, "lang") == 0) {
1672 list_available_languages();
1676 script = strchr(str, ':');
1679 if (len >= PATH_MAX) {
1680 fprintf(stderr, "invalid language specifier");
1683 strncpy(spec, str, len);
1685 scripting_ops = script_spec__lookup(spec);
1686 if (!scripting_ops) {
1687 fprintf(stderr, "invalid language specifier");
1693 ext = strrchr(script, '.');
1695 fprintf(stderr, "invalid script extension");
1698 scripting_ops = script_spec__lookup(++ext);
1699 if (!scripting_ops) {
1700 fprintf(stderr, "invalid script extension");
1705 script_name = strdup(script);
1710 static int parse_output_fields(const struct option *opt __maybe_unused,
1711 const char *arg, int unset __maybe_unused)
1713 char *tok, *strtok_saveptr = NULL;
1714 int i, imax = ARRAY_SIZE(all_output_options);
1717 char *str = strdup(arg);
1723 /* first word can state for which event type the user is specifying
1724 * the fields. If no type exists, the specified fields apply to all
1725 * event types found in the file minus the invalid fields for a type.
1727 tok = strchr(str, ':');
1731 if (!strcmp(str, "hw"))
1732 type = PERF_TYPE_HARDWARE;
1733 else if (!strcmp(str, "sw"))
1734 type = PERF_TYPE_SOFTWARE;
1735 else if (!strcmp(str, "trace"))
1736 type = PERF_TYPE_TRACEPOINT;
1737 else if (!strcmp(str, "raw"))
1738 type = PERF_TYPE_RAW;
1739 else if (!strcmp(str, "break"))
1740 type = PERF_TYPE_BREAKPOINT;
1742 fprintf(stderr, "Invalid event type in field string.\n");
1747 if (output[type].user_set)
1748 pr_warning("Overriding previous field request for %s events.\n",
1751 output[type].fields = 0;
1752 output[type].user_set = true;
1753 output[type].wildcard_set = false;
1757 if (strlen(str) == 0) {
1759 "Cannot set fields to 'none' for all event types.\n");
1764 if (output_set_by_user())
1765 pr_warning("Overriding previous field request for all events.\n");
1767 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1768 output[j].fields = 0;
1769 output[j].user_set = true;
1770 output[j].wildcard_set = true;
1774 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
1775 for (i = 0; i < imax; ++i) {
1776 if (strcmp(tok, all_output_options[i].str) == 0)
1779 if (i == imax && strcmp(tok, "flags") == 0) {
1784 fprintf(stderr, "Invalid field requested.\n");
1790 /* add user option to all events types for
1793 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1794 if (output[j].invalid_fields & all_output_options[i].field) {
1795 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1796 all_output_options[i].str, event_type(j));
1798 output[j].fields |= all_output_options[i].field;
1801 if (output[type].invalid_fields & all_output_options[i].field) {
1802 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1803 all_output_options[i].str, event_type(type));
1808 output[type].fields |= all_output_options[i].field;
1813 if (output[type].fields == 0) {
1814 pr_debug("No fields requested for %s type. "
1815 "Events will not be displayed.\n", event_type(type));
1824 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1825 static int is_directory(const char *base_path, const struct dirent *dent)
1827 char path[PATH_MAX];
1830 sprintf(path, "%s/%s", base_path, dent->d_name);
1831 if (stat(path, &st))
1834 return S_ISDIR(st.st_mode);
1837 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1838 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1839 if ((lang_dirent->d_type == DT_DIR || \
1840 (lang_dirent->d_type == DT_UNKNOWN && \
1841 is_directory(scripts_path, lang_dirent))) && \
1842 (strcmp(lang_dirent->d_name, ".")) && \
1843 (strcmp(lang_dirent->d_name, "..")))
1845 #define for_each_script(lang_path, lang_dir, script_dirent) \
1846 while ((script_dirent = readdir(lang_dir)) != NULL) \
1847 if (script_dirent->d_type != DT_DIR && \
1848 (script_dirent->d_type != DT_UNKNOWN || \
1849 !is_directory(lang_path, script_dirent)))
1852 #define RECORD_SUFFIX "-record"
1853 #define REPORT_SUFFIX "-report"
1855 struct script_desc {
1856 struct list_head node;
1862 static LIST_HEAD(script_descs);
1864 static struct script_desc *script_desc__new(const char *name)
1866 struct script_desc *s = zalloc(sizeof(*s));
1868 if (s != NULL && name)
1869 s->name = strdup(name);
1874 static void script_desc__delete(struct script_desc *s)
1877 zfree(&s->half_liner);
1882 static void script_desc__add(struct script_desc *s)
1884 list_add_tail(&s->node, &script_descs);
1887 static struct script_desc *script_desc__find(const char *name)
1889 struct script_desc *s;
1891 list_for_each_entry(s, &script_descs, node)
1892 if (strcasecmp(s->name, name) == 0)
1897 static struct script_desc *script_desc__findnew(const char *name)
1899 struct script_desc *s = script_desc__find(name);
1904 s = script_desc__new(name);
1906 goto out_delete_desc;
1908 script_desc__add(s);
1913 script_desc__delete(s);
1918 static const char *ends_with(const char *str, const char *suffix)
1920 size_t suffix_len = strlen(suffix);
1921 const char *p = str;
1923 if (strlen(str) > suffix_len) {
1924 p = str + strlen(str) - suffix_len;
1925 if (!strncmp(p, suffix, suffix_len))
1932 static int read_script_info(struct script_desc *desc, const char *filename)
1934 char line[BUFSIZ], *p;
1937 fp = fopen(filename, "r");
1941 while (fgets(line, sizeof(line), fp)) {
1948 if (strlen(p) && *p == '!')
1952 if (strlen(p) && p[strlen(p) - 1] == '\n')
1953 p[strlen(p) - 1] = '\0';
1955 if (!strncmp(p, "description:", strlen("description:"))) {
1956 p += strlen("description:");
1957 desc->half_liner = strdup(ltrim(p));
1961 if (!strncmp(p, "args:", strlen("args:"))) {
1962 p += strlen("args:");
1963 desc->args = strdup(ltrim(p));
1973 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1975 char *script_root, *str;
1977 script_root = strdup(script_dirent->d_name);
1981 str = (char *)ends_with(script_root, suffix);
1991 static int list_available_scripts(const struct option *opt __maybe_unused,
1992 const char *s __maybe_unused,
1993 int unset __maybe_unused)
1995 struct dirent *script_dirent, *lang_dirent;
1996 char scripts_path[MAXPATHLEN];
1997 DIR *scripts_dir, *lang_dir;
1998 char script_path[MAXPATHLEN];
1999 char lang_path[MAXPATHLEN];
2000 struct script_desc *desc;
2001 char first_half[BUFSIZ];
2004 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2006 scripts_dir = opendir(scripts_path);
2009 "open(%s) failed.\n"
2010 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2015 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2016 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2017 lang_dirent->d_name);
2018 lang_dir = opendir(lang_path);
2022 for_each_script(lang_path, lang_dir, script_dirent) {
2023 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
2025 desc = script_desc__findnew(script_root);
2026 snprintf(script_path, MAXPATHLEN, "%s/%s",
2027 lang_path, script_dirent->d_name);
2028 read_script_info(desc, script_path);
2034 fprintf(stdout, "List of available trace scripts:\n");
2035 list_for_each_entry(desc, &script_descs, node) {
2036 sprintf(first_half, "%s %s", desc->name,
2037 desc->args ? desc->args : "");
2038 fprintf(stdout, " %-36s %s\n", first_half,
2039 desc->half_liner ? desc->half_liner : "");
2046 * Some scripts specify the required events in their "xxx-record" file,
2047 * this function will check if the events in perf.data match those
2048 * mentioned in the "xxx-record".
2050 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2051 * which is covered well now. And new parsing code should be added to
2052 * cover the future complexing formats like event groups etc.
2054 static int check_ev_match(char *dir_name, char *scriptname,
2055 struct perf_session *session)
2057 char filename[MAXPATHLEN], evname[128];
2058 char line[BUFSIZ], *p;
2059 struct perf_evsel *pos;
2063 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2065 fp = fopen(filename, "r");
2069 while (fgets(line, sizeof(line), fp)) {
2075 p = strstr(p, "-e");
2081 len = strcspn(p, " \t");
2085 snprintf(evname, len + 1, "%s", p);
2088 evlist__for_each_entry(session->evlist, pos) {
2089 if (!strcmp(perf_evsel__name(pos), evname)) {
2107 * Return -1 if none is found, otherwise the actual scripts number.
2109 * Currently the only user of this function is the script browser, which
2110 * will list all statically runnable scripts, select one, execute it and
2111 * show the output in a perf browser.
2113 int find_scripts(char **scripts_array, char **scripts_path_array)
2115 struct dirent *script_dirent, *lang_dirent;
2116 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
2117 DIR *scripts_dir, *lang_dir;
2118 struct perf_session *session;
2119 struct perf_data_file file = {
2121 .mode = PERF_DATA_MODE_READ,
2126 session = perf_session__new(&file, false, NULL);
2130 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2132 scripts_dir = opendir(scripts_path);
2134 perf_session__delete(session);
2138 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2139 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2140 lang_dirent->d_name);
2142 if (strstr(lang_path, "perl"))
2146 if (strstr(lang_path, "python"))
2150 lang_dir = opendir(lang_path);
2154 for_each_script(lang_path, lang_dir, script_dirent) {
2155 /* Skip those real time scripts: xxxtop.p[yl] */
2156 if (strstr(script_dirent->d_name, "top."))
2158 sprintf(scripts_path_array[i], "%s/%s", lang_path,
2159 script_dirent->d_name);
2160 temp = strchr(script_dirent->d_name, '.');
2161 snprintf(scripts_array[i],
2162 (temp - script_dirent->d_name) + 1,
2163 "%s", script_dirent->d_name);
2165 if (check_ev_match(lang_path,
2166 scripts_array[i], session))
2174 closedir(scripts_dir);
2175 perf_session__delete(session);
2179 static char *get_script_path(const char *script_root, const char *suffix)
2181 struct dirent *script_dirent, *lang_dirent;
2182 char scripts_path[MAXPATHLEN];
2183 char script_path[MAXPATHLEN];
2184 DIR *scripts_dir, *lang_dir;
2185 char lang_path[MAXPATHLEN];
2186 char *__script_root;
2188 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2190 scripts_dir = opendir(scripts_path);
2194 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2195 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2196 lang_dirent->d_name);
2197 lang_dir = opendir(lang_path);
2201 for_each_script(lang_path, lang_dir, script_dirent) {
2202 __script_root = get_script_root(script_dirent, suffix);
2203 if (__script_root && !strcmp(script_root, __script_root)) {
2204 free(__script_root);
2206 closedir(scripts_dir);
2207 snprintf(script_path, MAXPATHLEN, "%s/%s",
2208 lang_path, script_dirent->d_name);
2209 return strdup(script_path);
2211 free(__script_root);
2215 closedir(scripts_dir);
2220 static bool is_top_script(const char *script_path)
2222 return ends_with(script_path, "top") == NULL ? false : true;
2225 static int has_required_arg(char *script_path)
2227 struct script_desc *desc;
2231 desc = script_desc__new(NULL);
2233 if (read_script_info(desc, script_path))
2239 for (p = desc->args; *p; p++)
2243 script_desc__delete(desc);
2248 static int have_cmd(int argc, const char **argv)
2250 char **__argv = malloc(sizeof(const char *) * argc);
2253 pr_err("malloc failed\n");
2257 memcpy(__argv, argv, sizeof(const char *) * argc);
2258 argc = parse_options(argc, (const char **)__argv, record_options,
2259 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2262 system_wide = (argc == 0);
2267 static void script__setup_sample_type(struct perf_script *script)
2269 struct perf_session *session = script->session;
2270 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2272 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2273 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2274 (sample_type & PERF_SAMPLE_STACK_USER))
2275 callchain_param.record_mode = CALLCHAIN_DWARF;
2276 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2277 callchain_param.record_mode = CALLCHAIN_LBR;
2279 callchain_param.record_mode = CALLCHAIN_FP;
2283 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2284 union perf_event *event,
2285 struct perf_session *session)
2287 struct stat_round_event *round = &event->stat_round;
2288 struct perf_evsel *counter;
2290 evlist__for_each_entry(session->evlist, counter) {
2291 perf_stat_process_counter(&stat_config, counter);
2292 process_stat(counter, round->time);
2295 process_stat_interval(round->time);
2299 static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2300 union perf_event *event,
2301 struct perf_session *session __maybe_unused)
2303 perf_event__read_stat_config(&stat_config, &event->stat_config);
2307 static int set_maps(struct perf_script *script)
2309 struct perf_evlist *evlist = script->session->evlist;
2311 if (!script->cpus || !script->threads)
2314 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2317 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2319 if (perf_evlist__alloc_stats(evlist, true))
2322 script->allocated = true;
2327 int process_thread_map_event(struct perf_tool *tool,
2328 union perf_event *event,
2329 struct perf_session *session __maybe_unused)
2331 struct perf_script *script = container_of(tool, struct perf_script, tool);
2333 if (script->threads) {
2334 pr_warning("Extra thread map event, ignoring.\n");
2338 script->threads = thread_map__new_event(&event->thread_map);
2339 if (!script->threads)
2342 return set_maps(script);
2346 int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2347 union perf_event *event,
2348 struct perf_session *session __maybe_unused)
2350 struct perf_script *script = container_of(tool, struct perf_script, tool);
2353 pr_warning("Extra cpu map event, ignoring.\n");
2357 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2361 return set_maps(script);
2364 int cmd_script(int argc, const char **argv)
2366 bool show_full_info = false;
2367 bool header = false;
2368 bool header_only = false;
2369 bool script_started = false;
2370 char *rec_script_path = NULL;
2371 char *rep_script_path = NULL;
2372 struct perf_session *session;
2373 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
2374 char *script_path = NULL;
2375 const char **__argv;
2377 struct perf_script script = {
2379 .sample = process_sample_event,
2380 .mmap = perf_event__process_mmap,
2381 .mmap2 = perf_event__process_mmap2,
2382 .comm = perf_event__process_comm,
2383 .namespaces = perf_event__process_namespaces,
2384 .exit = perf_event__process_exit,
2385 .fork = perf_event__process_fork,
2386 .attr = process_attr,
2387 .event_update = perf_event__process_event_update,
2388 .tracing_data = perf_event__process_tracing_data,
2389 .build_id = perf_event__process_build_id,
2390 .id_index = perf_event__process_id_index,
2391 .auxtrace_info = perf_event__process_auxtrace_info,
2392 .auxtrace = perf_event__process_auxtrace,
2393 .auxtrace_error = perf_event__process_auxtrace_error,
2394 .stat = perf_event__process_stat_event,
2395 .stat_round = process_stat_round_event,
2396 .stat_config = process_stat_config_event,
2397 .thread_map = process_thread_map_event,
2398 .cpu_map = process_cpu_map_event,
2399 .ordered_events = true,
2400 .ordering_requires_timestamps = true,
2403 struct perf_data_file file = {
2404 .mode = PERF_DATA_MODE_READ,
2406 const struct option options[] = {
2407 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2408 "dump raw trace in ASCII"),
2409 OPT_INCR('v', "verbose", &verbose,
2410 "be more verbose (show symbol address, etc)"),
2411 OPT_BOOLEAN('L', "Latency", &latency_format,
2412 "show latency attributes (irqs/preemption disabled, etc)"),
2413 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2414 list_available_scripts),
2415 OPT_CALLBACK('s', "script", NULL, "name",
2416 "script file name (lang:script name, script name, or *)",
2418 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2419 "generate perf-script.xx script in specified language"),
2420 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2421 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2422 "do various checks like samples ordering and lost events"),
2423 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2424 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
2425 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2426 "file", "vmlinux pathname"),
2427 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2428 "file", "kallsyms pathname"),
2429 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2430 "When printing symbols do not display call chain"),
2431 OPT_CALLBACK(0, "symfs", NULL, "directory",
2432 "Look for files with symbols relative to this directory",
2433 symbol__config_symfs),
2434 OPT_CALLBACK('F', "fields", NULL, "str",
2435 "comma separated output fields prepend with 'type:'. "
2436 "Valid types: hw,sw,trace,raw. "
2437 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2438 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
2439 "bpf-output,callindent,insn,insnlen,brstackinsn",
2440 parse_output_fields),
2441 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2442 "system-wide collection from all CPUs"),
2443 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2444 "only consider these symbols"),
2445 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2446 "Stop display of callgraph at these symbols"),
2447 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2448 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2449 "only display events for these comms"),
2450 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2451 "only consider symbols in these pids"),
2452 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2453 "only consider symbols in these tids"),
2454 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2455 "Set the maximum stack depth when parsing the callchain, "
2456 "anything beyond the specified depth will be ignored. "
2457 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
2458 OPT_BOOLEAN('I', "show-info", &show_full_info,
2459 "display extended information from perf.data file"),
2460 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2461 "Show the path of [kernel.kallsyms]"),
2462 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2463 "Show the fork/comm/exit events"),
2464 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2465 "Show the mmap events"),
2466 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2467 "Show context switch events (if recorded)"),
2468 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2469 "Show namespace events (if recorded)"),
2470 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2471 OPT_INTEGER(0, "max-blocks", &max_blocks,
2472 "Maximum number of code blocks to dump with brstackinsn"),
2473 OPT_BOOLEAN(0, "ns", &nanosecs,
2474 "Use 9 decimal places when displaying time"),
2475 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2476 "Instruction Tracing options",
2477 itrace_parse_synth_opts),
2478 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2479 "Show full source file name path for source lines"),
2480 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2481 "Enable symbol demangling"),
2482 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2483 "Enable kernel symbol demangling"),
2484 OPT_STRING(0, "time", &script.time_str, "str",
2485 "Time span of interest (start,stop)"),
2488 const char * const script_subcommands[] = { "record", "report", NULL };
2489 const char *script_usage[] = {
2490 "perf script [<options>]",
2491 "perf script [<options>] record <script> [<record-options>] <command>",
2492 "perf script [<options>] report <script> [script-args]",
2493 "perf script [<options>] <script> [<record-options>] <command>",
2494 "perf script [<options>] <top-script> [script-args]",
2500 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
2501 PARSE_OPT_STOP_AT_NON_OPTION);
2503 file.path = input_name;
2504 file.force = symbol_conf.force;
2506 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2507 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2508 if (!rec_script_path)
2509 return cmd_record(argc, argv);
2512 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2513 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2514 if (!rep_script_path) {
2516 "Please specify a valid report script"
2517 "(see 'perf script -l' for listing)\n");
2522 if (itrace_synth_opts.callchain &&
2523 itrace_synth_opts.callchain_sz > scripting_max_stack)
2524 scripting_max_stack = itrace_synth_opts.callchain_sz;
2526 /* make sure PERF_EXEC_PATH is set for scripts */
2527 set_argv_exec_path(get_argv_exec_path());
2529 if (argc && !script_name && !rec_script_path && !rep_script_path) {
2534 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2535 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2537 if (!rec_script_path && !rep_script_path) {
2538 usage_with_options_msg(script_usage, options,
2539 "Couldn't find script `%s'\n\n See perf"
2540 " script -l for available scripts.\n", argv[0]);
2543 if (is_top_script(argv[0])) {
2544 rep_args = argc - 1;
2548 rep_args = has_required_arg(rep_script_path);
2549 rec_args = (argc - 1) - rep_args;
2551 usage_with_options_msg(script_usage, options,
2552 "`%s' script requires options."
2553 "\n\n See perf script -l for available "
2554 "scripts and options.\n", argv[0]);
2558 if (pipe(live_pipe) < 0) {
2559 perror("failed to create pipe");
2565 perror("failed to fork");
2572 dup2(live_pipe[1], 1);
2573 close(live_pipe[0]);
2575 if (is_top_script(argv[0])) {
2577 } else if (!system_wide) {
2578 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2584 __argv = malloc((argc + 6) * sizeof(const char *));
2586 pr_err("malloc failed\n");
2591 __argv[j++] = "/bin/sh";
2592 __argv[j++] = rec_script_path;
2598 for (i = rep_args + 1; i < argc; i++)
2599 __argv[j++] = argv[i];
2602 execvp("/bin/sh", (char **)__argv);
2607 dup2(live_pipe[0], 0);
2608 close(live_pipe[1]);
2610 __argv = malloc((argc + 4) * sizeof(const char *));
2612 pr_err("malloc failed\n");
2618 __argv[j++] = "/bin/sh";
2619 __argv[j++] = rep_script_path;
2620 for (i = 1; i < rep_args + 1; i++)
2621 __argv[j++] = argv[i];
2626 execvp("/bin/sh", (char **)__argv);
2631 if (rec_script_path)
2632 script_path = rec_script_path;
2633 if (rep_script_path)
2634 script_path = rep_script_path;
2639 if (!rec_script_path)
2640 system_wide = false;
2641 else if (!system_wide) {
2642 if (have_cmd(argc - 1, &argv[1]) != 0) {
2648 __argv = malloc((argc + 2) * sizeof(const char *));
2650 pr_err("malloc failed\n");
2655 __argv[j++] = "/bin/sh";
2656 __argv[j++] = script_path;
2659 for (i = 2; i < argc; i++)
2660 __argv[j++] = argv[i];
2663 execvp("/bin/sh", (char **)__argv);
2671 session = perf_session__new(&file, false, &script.tool);
2672 if (session == NULL)
2675 if (header || header_only) {
2676 perf_session__fprintf_info(session, stdout, show_full_info);
2681 if (symbol__init(&session->header.env) < 0)
2684 script.session = session;
2685 script__setup_sample_type(&script);
2687 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2688 itrace_synth_opts.thread_stack = true;
2690 session->itrace_synth_opts = &itrace_synth_opts;
2693 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2699 symbol_conf.use_callchain = true;
2701 symbol_conf.use_callchain = false;
2703 if (session->tevent.pevent &&
2704 pevent_set_function_resolver(session->tevent.pevent,
2705 machine__resolve_kernel_addr,
2706 &session->machines.host) < 0) {
2707 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2711 if (generate_script_lang) {
2712 struct stat perf_stat;
2715 if (output_set_by_user()) {
2717 "custom fields not supported for generated scripts");
2722 input = open(file.path, O_RDONLY); /* input_name */
2725 perror("failed to open file");
2729 err = fstat(input, &perf_stat);
2731 perror("failed to stat file");
2735 if (!perf_stat.st_size) {
2736 fprintf(stderr, "zero-sized file, nothing to do!\n");
2740 scripting_ops = script_spec__lookup(generate_script_lang);
2741 if (!scripting_ops) {
2742 fprintf(stderr, "invalid language specifier");
2747 err = scripting_ops->generate_script(session->tevent.pevent,
2753 err = scripting_ops->start_script(script_name, argc, argv);
2756 pr_debug("perf script started with script %s\n\n", script_name);
2757 script_started = true;
2761 err = perf_session__check_output_opt(session);
2765 /* needs to be parsed after looking up reference time */
2766 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2767 pr_err("Invalid time string\n");
2771 err = __cmd_script(&script);
2776 perf_evlist__free_stats(session->evlist);
2777 perf_session__delete(session);
2780 cleanup_scripting();