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/string2.h"
25 #include "util/thread-stack.h"
26 #include "util/time-utils.h"
27 #include "print_binary.h"
28 #include <linux/bitmap.h>
29 #include <linux/kernel.h>
30 #include <linux/stringify.h>
31 #include <linux/time64.h>
33 #include "util/mem-events.h"
34 #include "util/dump-insn.h"
38 #include "sane_ctype.h"
40 static char const *script_name;
41 static char const *generate_script_lang;
42 static bool debug_mode;
43 static u64 last_timestamp;
44 static u64 nr_unordered;
45 static bool no_callchain;
46 static bool latency_format;
47 static bool system_wide;
48 static bool print_flags;
50 static const char *cpu_list;
51 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
52 static struct perf_stat_config stat_config;
53 static int max_blocks;
55 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
57 enum perf_output_field {
58 PERF_OUTPUT_COMM = 1U << 0,
59 PERF_OUTPUT_TID = 1U << 1,
60 PERF_OUTPUT_PID = 1U << 2,
61 PERF_OUTPUT_TIME = 1U << 3,
62 PERF_OUTPUT_CPU = 1U << 4,
63 PERF_OUTPUT_EVNAME = 1U << 5,
64 PERF_OUTPUT_TRACE = 1U << 6,
65 PERF_OUTPUT_IP = 1U << 7,
66 PERF_OUTPUT_SYM = 1U << 8,
67 PERF_OUTPUT_DSO = 1U << 9,
68 PERF_OUTPUT_ADDR = 1U << 10,
69 PERF_OUTPUT_SYMOFFSET = 1U << 11,
70 PERF_OUTPUT_SRCLINE = 1U << 12,
71 PERF_OUTPUT_PERIOD = 1U << 13,
72 PERF_OUTPUT_IREGS = 1U << 14,
73 PERF_OUTPUT_BRSTACK = 1U << 15,
74 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
75 PERF_OUTPUT_DATA_SRC = 1U << 17,
76 PERF_OUTPUT_WEIGHT = 1U << 18,
77 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
78 PERF_OUTPUT_CALLINDENT = 1U << 20,
79 PERF_OUTPUT_INSN = 1U << 21,
80 PERF_OUTPUT_INSNLEN = 1U << 22,
81 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
84 struct output_option {
86 enum perf_output_field field;
87 } all_output_options[] = {
88 {.str = "comm", .field = PERF_OUTPUT_COMM},
89 {.str = "tid", .field = PERF_OUTPUT_TID},
90 {.str = "pid", .field = PERF_OUTPUT_PID},
91 {.str = "time", .field = PERF_OUTPUT_TIME},
92 {.str = "cpu", .field = PERF_OUTPUT_CPU},
93 {.str = "event", .field = PERF_OUTPUT_EVNAME},
94 {.str = "trace", .field = PERF_OUTPUT_TRACE},
95 {.str = "ip", .field = PERF_OUTPUT_IP},
96 {.str = "sym", .field = PERF_OUTPUT_SYM},
97 {.str = "dso", .field = PERF_OUTPUT_DSO},
98 {.str = "addr", .field = PERF_OUTPUT_ADDR},
99 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
100 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
101 {.str = "period", .field = PERF_OUTPUT_PERIOD},
102 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
103 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
104 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
105 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
106 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
107 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
108 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
109 {.str = "insn", .field = PERF_OUTPUT_INSN},
110 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
111 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
114 /* default set to maintain compatibility with current format */
118 unsigned int print_ip_opts;
121 } output[PERF_TYPE_MAX] = {
123 [PERF_TYPE_HARDWARE] = {
126 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
127 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
128 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
129 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
132 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
135 [PERF_TYPE_SOFTWARE] = {
138 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
139 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
140 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
141 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
142 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
144 .invalid_fields = PERF_OUTPUT_TRACE,
147 [PERF_TYPE_TRACEPOINT] = {
150 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
151 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
152 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
158 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
159 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
160 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
161 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
162 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
163 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
165 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
168 [PERF_TYPE_BREAKPOINT] = {
171 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
172 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
173 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
174 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
177 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
181 static bool output_set_by_user(void)
184 for (j = 0; j < PERF_TYPE_MAX; ++j) {
185 if (output[j].user_set)
191 static const char *output_field2str(enum perf_output_field field)
193 int i, imax = ARRAY_SIZE(all_output_options);
194 const char *str = "";
196 for (i = 0; i < imax; ++i) {
197 if (all_output_options[i].field == field) {
198 str = all_output_options[i].str;
205 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
207 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
208 u64 sample_type, const char *sample_msg,
209 enum perf_output_field field,
212 struct perf_event_attr *attr = &evsel->attr;
213 int type = attr->type;
216 if (attr->sample_type & sample_type)
219 if (output[type].user_set) {
222 evname = perf_evsel__name(evsel);
223 pr_err("Samples for '%s' event do not have %s attribute set. "
224 "Cannot print '%s' field.\n",
225 evname, sample_msg, output_field2str(field));
229 /* user did not ask for it explicitly so remove from the default list */
230 output[type].fields &= ~field;
231 evname = perf_evsel__name(evsel);
232 pr_debug("Samples for '%s' event do not have %s attribute set. "
233 "Skipping '%s' field.\n",
234 evname, sample_msg, output_field2str(field));
239 static int perf_evsel__check_stype(struct perf_evsel *evsel,
240 u64 sample_type, const char *sample_msg,
241 enum perf_output_field field)
243 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
247 static int perf_evsel__check_attr(struct perf_evsel *evsel,
248 struct perf_session *session)
250 struct perf_event_attr *attr = &evsel->attr;
253 if (perf_header__has_feat(&session->header, HEADER_STAT))
256 allow_user_set = perf_header__has_feat(&session->header,
259 if (PRINT_FIELD(TRACE) &&
260 !perf_session__has_traces(session, "record -R"))
263 if (PRINT_FIELD(IP)) {
264 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
269 if (PRINT_FIELD(ADDR) &&
270 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
271 PERF_OUTPUT_ADDR, allow_user_set))
274 if (PRINT_FIELD(DATA_SRC) &&
275 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
276 PERF_OUTPUT_DATA_SRC))
279 if (PRINT_FIELD(WEIGHT) &&
280 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
284 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
285 pr_err("Display of symbols requested but neither sample IP nor "
286 "sample address\nis selected. Hence, no addresses to convert "
290 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
291 pr_err("Display of offsets requested but symbol is not"
295 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
296 pr_err("Display of DSO requested but neither sample IP nor "
297 "sample address\nis selected. Hence, no addresses to convert "
301 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
302 pr_err("Display of source line number requested but sample IP is not\n"
303 "selected. Hence, no address to lookup the source line number.\n");
306 if (PRINT_FIELD(BRSTACKINSN) &&
307 !(perf_evlist__combined_branch_type(session->evlist) &
308 PERF_SAMPLE_BRANCH_ANY)) {
309 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
310 "Hint: run 'perf record -b ...'\n");
313 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
314 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
315 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
318 if (PRINT_FIELD(TIME) &&
319 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
323 if (PRINT_FIELD(CPU) &&
324 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
325 PERF_OUTPUT_CPU, allow_user_set))
328 if (PRINT_FIELD(PERIOD) &&
329 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
333 if (PRINT_FIELD(IREGS) &&
334 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
341 static void set_print_ip_opts(struct perf_event_attr *attr)
343 unsigned int type = attr->type;
345 output[type].print_ip_opts = 0;
347 output[type].print_ip_opts |= EVSEL__PRINT_IP;
349 if (PRINT_FIELD(SYM))
350 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
352 if (PRINT_FIELD(DSO))
353 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
355 if (PRINT_FIELD(SYMOFFSET))
356 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
358 if (PRINT_FIELD(SRCLINE))
359 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
363 * verify all user requested events exist and the samples
364 * have the expected data
366 static int perf_session__check_output_opt(struct perf_session *session)
369 struct perf_evsel *evsel;
371 for (j = 0; j < PERF_TYPE_MAX; ++j) {
372 evsel = perf_session__find_first_evtype(session, j);
375 * even if fields is set to 0 (ie., show nothing) event must
376 * exist if user explicitly includes it on the command line
378 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
379 pr_err("%s events do not exist. "
380 "Remove corresponding -f option to proceed.\n",
385 if (evsel && output[j].fields &&
386 perf_evsel__check_attr(evsel, session))
392 set_print_ip_opts(&evsel->attr);
396 bool use_callchain = false;
397 bool not_pipe = false;
399 evlist__for_each_entry(session->evlist, evsel) {
401 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
402 use_callchain = true;
406 if (not_pipe && !use_callchain)
407 symbol_conf.use_callchain = false;
411 * set default for tracepoints to print symbols only
412 * if callchains are present
414 if (symbol_conf.use_callchain &&
415 !output[PERF_TYPE_TRACEPOINT].user_set) {
416 struct perf_event_attr *attr;
418 j = PERF_TYPE_TRACEPOINT;
420 evlist__for_each_entry(session->evlist, evsel) {
421 if (evsel->attr.type != j)
426 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
427 output[j].fields |= PERF_OUTPUT_IP;
428 output[j].fields |= PERF_OUTPUT_SYM;
429 output[j].fields |= PERF_OUTPUT_DSO;
430 set_print_ip_opts(attr);
440 static void print_sample_iregs(struct perf_sample *sample,
441 struct perf_event_attr *attr)
443 struct regs_dump *regs = &sample->intr_regs;
444 uint64_t mask = attr->sample_regs_intr;
450 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
451 u64 val = regs->regs[i++];
452 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
456 static void print_sample_start(struct perf_sample *sample,
457 struct thread *thread,
458 struct perf_evsel *evsel)
460 struct perf_event_attr *attr = &evsel->attr;
462 unsigned long long nsecs;
464 if (PRINT_FIELD(COMM)) {
466 printf("%8.8s ", thread__comm_str(thread));
467 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
468 printf("%s ", thread__comm_str(thread));
470 printf("%16s ", thread__comm_str(thread));
473 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
474 printf("%5d/%-5d ", sample->pid, sample->tid);
475 else if (PRINT_FIELD(PID))
476 printf("%5d ", sample->pid);
477 else if (PRINT_FIELD(TID))
478 printf("%5d ", sample->tid);
480 if (PRINT_FIELD(CPU)) {
482 printf("%3d ", sample->cpu);
484 printf("[%03d] ", sample->cpu);
487 if (PRINT_FIELD(TIME)) {
488 nsecs = sample->time;
489 secs = nsecs / NSEC_PER_SEC;
490 nsecs -= secs * NSEC_PER_SEC;
493 printf("%5lu.%09llu: ", secs, nsecs);
495 char sample_time[32];
496 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
497 printf("%12s: ", sample_time);
503 mispred_str(struct branch_entry *br)
505 if (!(br->flags.mispred || br->flags.predicted))
508 return br->flags.predicted ? 'P' : 'M';
511 static void print_sample_brstack(struct perf_sample *sample)
513 struct branch_stack *br = sample->branch_stack;
519 for (i = 0; i < br->nr; i++) {
520 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
523 mispred_str( br->entries + i),
524 br->entries[i].flags.in_tx? 'X' : '-',
525 br->entries[i].flags.abort? 'A' : '-',
526 br->entries[i].flags.cycles);
530 static void print_sample_brstacksym(struct perf_sample *sample,
531 struct thread *thread)
533 struct branch_stack *br = sample->branch_stack;
534 struct addr_location alf, alt;
540 for (i = 0; i < br->nr; i++) {
542 memset(&alf, 0, sizeof(alf));
543 memset(&alt, 0, sizeof(alt));
544 from = br->entries[i].from;
545 to = br->entries[i].to;
547 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
549 alf.sym = map__find_symbol(alf.map, alf.addr);
551 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
553 alt.sym = map__find_symbol(alt.map, alt.addr);
555 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
557 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
558 printf("/%c/%c/%c/%d ",
559 mispred_str( br->entries + i),
560 br->entries[i].flags.in_tx? 'X' : '-',
561 br->entries[i].flags.abort? 'A' : '-',
562 br->entries[i].flags.cycles);
566 #define MAXBB 16384UL
568 static int grab_bb(u8 *buffer, u64 start, u64 end,
569 struct machine *machine, struct thread *thread,
570 bool *is64bit, u8 *cpumode, bool last)
573 struct addr_location al;
579 kernel = machine__kernel_ip(machine, start);
581 *cpumode = PERF_RECORD_MISC_KERNEL;
583 *cpumode = PERF_RECORD_MISC_USER;
586 * Block overlaps between kernel and user.
587 * This can happen due to ring filtering
588 * On Intel CPUs the entry into the kernel is filtered,
589 * but the exit is not. Let the caller patch it up.
591 if (kernel != machine__kernel_ip(machine, end)) {
592 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
597 memset(&al, 0, sizeof(al));
598 if (end - start > MAXBB - MAXINSN) {
600 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
602 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
606 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
607 if (!al.map || !al.map->dso) {
608 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
611 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
612 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
616 /* Load maps to ensure dso->is_64_bit has been updated */
619 offset = al.map->map_ip(al.map, start);
620 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
621 end - start + MAXINSN);
623 *is64bit = al.map->dso->is_64_bit;
625 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
630 static void print_jump(uint64_t ip, struct branch_entry *en,
631 struct perf_insn *x, u8 *inbuf, int len,
634 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
636 dump_insn(x, ip, inbuf, len, NULL),
637 en->flags.predicted ? " PRED" : "",
638 en->flags.mispred ? " MISPRED" : "",
639 en->flags.in_tx ? " INTX" : "",
640 en->flags.abort ? " ABORT" : "");
641 if (en->flags.cycles) {
642 printf(" %d cycles", en->flags.cycles);
644 printf(" %.2f IPC", (float)insn / en->flags.cycles);
649 static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
650 uint64_t addr, struct symbol **lastsym,
651 struct perf_event_attr *attr)
653 struct addr_location al;
656 memset(&al, 0, sizeof(al));
658 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
660 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
662 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
668 al.sym = map__find_symbol(al.map, al.addr);
673 if (al.addr < al.sym->end)
674 off = al.addr - al.sym->start;
676 off = al.addr - al.map->start - al.sym->start;
677 printf("\t%s", al.sym->name);
681 if (PRINT_FIELD(SRCLINE))
682 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
687 static void print_sample_brstackinsn(struct perf_sample *sample,
688 struct thread *thread,
689 struct perf_event_attr *attr,
690 struct machine *machine)
692 struct branch_stack *br = sample->branch_stack;
694 int i, insn, len, nr, ilen;
698 struct symbol *lastsym = NULL;
703 if (max_blocks && nr > max_blocks + 1)
711 /* Handle first from jump, of which we don't know the entry. */
712 len = grab_bb(buffer, br->entries[nr-1].from,
713 br->entries[nr-1].from,
714 machine, thread, &x.is64bit, &x.cpumode, false);
716 print_ip_sym(thread, x.cpumode, x.cpu,
717 br->entries[nr - 1].from, &lastsym, attr);
718 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
722 /* Print all blocks */
723 for (i = nr - 2; i >= 0; i--) {
724 if (br->entries[i].from || br->entries[i].to)
725 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
728 start = br->entries[i + 1].to;
729 end = br->entries[i].from;
731 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
732 /* Patch up missing kernel transfers due to ring filters */
733 if (len == -ENXIO && i > 0) {
734 end = br->entries[--i].from;
735 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
736 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
742 for (off = 0;; off += ilen) {
743 uint64_t ip = start + off;
745 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
747 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
750 printf("\t%016" PRIx64 "\t%s\n", ip,
751 dump_insn(&x, ip, buffer + off, len - off, &ilen));
760 * Hit the branch? In this case we are already done, and the target
761 * has not been executed yet.
763 if (br->entries[0].from == sample->ip)
765 if (br->entries[0].flags.abort)
769 * Print final block upto sample
771 start = br->entries[0].to;
773 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
774 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
776 /* Print at least last IP if basic block did not work */
777 len = grab_bb(buffer, sample->ip, sample->ip,
778 machine, thread, &x.is64bit, &x.cpumode, false);
782 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
783 dump_insn(&x, sample->ip, buffer, len, NULL));
786 for (off = 0; off <= end - start; off += ilen) {
787 printf("\t%016" PRIx64 "\t%s\n", start + off,
788 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
794 static void print_sample_addr(struct perf_sample *sample,
795 struct thread *thread,
796 struct perf_event_attr *attr)
798 struct addr_location al;
800 printf("%16" PRIx64, sample->addr);
802 if (!sample_addr_correlates_sym(attr))
805 thread__resolve(thread, &al, sample);
807 if (PRINT_FIELD(SYM)) {
809 if (PRINT_FIELD(SYMOFFSET))
810 symbol__fprintf_symname_offs(al.sym, &al, stdout);
812 symbol__fprintf_symname(al.sym, stdout);
815 if (PRINT_FIELD(DSO)) {
817 map__fprintf_dsoname(al.map, stdout);
822 static void print_sample_callindent(struct perf_sample *sample,
823 struct perf_evsel *evsel,
824 struct thread *thread,
825 struct addr_location *al)
827 struct perf_event_attr *attr = &evsel->attr;
828 size_t depth = thread_stack__depth(thread);
829 struct addr_location addr_al;
830 const char *name = NULL;
836 * The 'return' has already been popped off the stack so the depth has
837 * to be adjusted to match the 'call'.
839 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
842 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
843 if (sample_addr_correlates_sym(attr)) {
844 thread__resolve(thread, &addr_al, sample);
846 name = addr_al.sym->name;
852 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
854 name = al->sym->name;
860 len = printf("%*s%s", (int)depth * 4, "", name);
862 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
868 * Try to keep the output length from changing frequently so that the
869 * output lines up more nicely.
871 if (len > spacing || (len && len < spacing - 52))
872 spacing = round_up(len + 4, 32);
875 printf("%*s", spacing - len, "");
878 static void print_insn(struct perf_sample *sample,
879 struct perf_event_attr *attr,
880 struct thread *thread,
881 struct machine *machine)
883 if (PRINT_FIELD(INSNLEN))
884 printf(" ilen: %d", sample->insn_len);
885 if (PRINT_FIELD(INSN)) {
889 for (i = 0; i < sample->insn_len; i++)
890 printf(" %02x", (unsigned char)sample->insn[i]);
892 if (PRINT_FIELD(BRSTACKINSN))
893 print_sample_brstackinsn(sample, thread, attr, machine);
896 static void print_sample_bts(struct perf_sample *sample,
897 struct perf_evsel *evsel,
898 struct thread *thread,
899 struct addr_location *al,
900 struct machine *machine)
902 struct perf_event_attr *attr = &evsel->attr;
903 bool print_srcline_last = false;
905 if (PRINT_FIELD(CALLINDENT))
906 print_sample_callindent(sample, evsel, thread, al);
908 /* print branch_from information */
909 if (PRINT_FIELD(IP)) {
910 unsigned int print_opts = output[attr->type].print_ip_opts;
911 struct callchain_cursor *cursor = NULL;
913 if (symbol_conf.use_callchain && sample->callchain &&
914 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
915 sample, NULL, NULL, scripting_max_stack) == 0)
916 cursor = &callchain_cursor;
918 if (cursor == NULL) {
920 if (print_opts & EVSEL__PRINT_SRCLINE) {
921 print_srcline_last = true;
922 print_opts &= ~EVSEL__PRINT_SRCLINE;
927 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
930 /* print branch_to information */
931 if (PRINT_FIELD(ADDR) ||
932 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
933 !output[attr->type].user_set)) {
935 print_sample_addr(sample, thread, attr);
938 if (print_srcline_last)
939 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
941 print_insn(sample, attr, thread, machine);
950 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
951 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
952 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
953 {PERF_IP_FLAG_BRANCH, "jmp"},
954 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
955 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
956 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
957 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
958 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
959 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
960 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
961 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
962 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
966 static void print_sample_flags(u32 flags)
968 const char *chars = PERF_IP_FLAG_CHARS;
969 const int n = strlen(PERF_IP_FLAG_CHARS);
970 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
971 const char *name = NULL;
975 for (i = 0; sample_flags[i].name ; i++) {
976 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
977 name = sample_flags[i].name;
982 for (i = 0; i < n; i++, flags >>= 1) {
984 str[pos++] = chars[i];
986 for (; i < 32; i++, flags >>= 1) {
993 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
995 printf(" %-11s ", str);
998 struct printer_data {
1005 print_sample_bpf_output_printer(enum binary_printer_ops op,
1009 unsigned char ch = (unsigned char)val;
1010 struct printer_data *printer_data = extra;
1013 case BINARY_PRINT_DATA_BEGIN:
1016 case BINARY_PRINT_LINE_BEGIN:
1017 printf("%17s", !printer_data->line_no ? "BPF output:" :
1020 case BINARY_PRINT_ADDR:
1021 printf(" %04x:", val);
1023 case BINARY_PRINT_NUM_DATA:
1024 printf(" %02x", val);
1026 case BINARY_PRINT_NUM_PAD:
1029 case BINARY_PRINT_SEP:
1032 case BINARY_PRINT_CHAR_DATA:
1033 if (printer_data->hit_nul && ch)
1034 printer_data->is_printable = false;
1039 if (!printer_data->is_printable)
1043 printer_data->hit_nul = true;
1045 printer_data->is_printable = false;
1050 case BINARY_PRINT_CHAR_PAD:
1053 case BINARY_PRINT_LINE_END:
1055 printer_data->line_no++;
1057 case BINARY_PRINT_DATA_END:
1063 static void print_sample_bpf_output(struct perf_sample *sample)
1065 unsigned int nr_bytes = sample->raw_size;
1066 struct printer_data printer_data = {0, false, true};
1068 print_binary(sample->raw_data, nr_bytes, 8,
1069 print_sample_bpf_output_printer, &printer_data);
1071 if (printer_data.is_printable && printer_data.hit_nul)
1072 printf("%17s \"%s\"\n", "BPF string:",
1073 (char *)(sample->raw_data));
1076 struct perf_script {
1077 struct perf_tool tool;
1078 struct perf_session *session;
1079 bool show_task_events;
1080 bool show_mmap_events;
1081 bool show_switch_events;
1082 bool show_namespace_events;
1084 struct cpu_map *cpus;
1085 struct thread_map *threads;
1087 const char *time_str;
1088 struct perf_time_interval ptime;
1091 static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1093 struct perf_evsel *evsel;
1096 evlist__for_each_entry(evlist, evsel) {
1097 int len = strlen(perf_evsel__name(evsel));
1099 max = MAX(len, max);
1105 static size_t data_src__printf(u64 data_src)
1107 struct mem_info mi = { .data_src.val = data_src };
1113 perf_script__meminfo_scnprintf(decode, 100, &mi);
1115 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1119 return printf("%-*s", maxlen, out);
1122 static void process_event(struct perf_script *script,
1123 struct perf_sample *sample, struct perf_evsel *evsel,
1124 struct addr_location *al,
1125 struct machine *machine)
1127 struct thread *thread = al->thread;
1128 struct perf_event_attr *attr = &evsel->attr;
1130 if (output[attr->type].fields == 0)
1133 print_sample_start(sample, thread, evsel);
1135 if (PRINT_FIELD(PERIOD))
1136 printf("%10" PRIu64 " ", sample->period);
1138 if (PRINT_FIELD(EVNAME)) {
1139 const char *evname = perf_evsel__name(evsel);
1141 if (!script->name_width)
1142 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1144 printf("%*s: ", script->name_width,
1145 evname ? evname : "[unknown]");
1149 print_sample_flags(sample->flags);
1151 if (is_bts_event(attr)) {
1152 print_sample_bts(sample, evsel, thread, al, machine);
1156 if (PRINT_FIELD(TRACE))
1157 event_format__print(evsel->tp_format, sample->cpu,
1158 sample->raw_data, sample->raw_size);
1159 if (PRINT_FIELD(ADDR))
1160 print_sample_addr(sample, thread, attr);
1162 if (PRINT_FIELD(DATA_SRC))
1163 data_src__printf(sample->data_src);
1165 if (PRINT_FIELD(WEIGHT))
1166 printf("%16" PRIu64, sample->weight);
1168 if (PRINT_FIELD(IP)) {
1169 struct callchain_cursor *cursor = NULL;
1171 if (symbol_conf.use_callchain && sample->callchain &&
1172 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1173 sample, NULL, NULL, scripting_max_stack) == 0)
1174 cursor = &callchain_cursor;
1176 putchar(cursor ? '\n' : ' ');
1177 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
1180 if (PRINT_FIELD(IREGS))
1181 print_sample_iregs(sample, attr);
1183 if (PRINT_FIELD(BRSTACK))
1184 print_sample_brstack(sample);
1185 else if (PRINT_FIELD(BRSTACKSYM))
1186 print_sample_brstacksym(sample, thread);
1188 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1189 print_sample_bpf_output(sample);
1190 print_insn(sample, attr, thread, machine);
1194 static struct scripting_ops *scripting_ops;
1196 static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1198 int nthreads = thread_map__nr(counter->threads);
1199 int ncpus = perf_evsel__nr_cpus(counter);
1201 static int header_printed;
1203 if (counter->system_wide)
1206 if (!header_printed) {
1207 printf("%3s %8s %15s %15s %15s %15s %s\n",
1208 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1212 for (thread = 0; thread < nthreads; thread++) {
1213 for (cpu = 0; cpu < ncpus; cpu++) {
1214 struct perf_counts_values *counts;
1216 counts = perf_counts(counter->counts, cpu, thread);
1218 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1219 counter->cpus->map[cpu],
1220 thread_map__pid(counter->threads, thread),
1225 perf_evsel__name(counter));
1230 static void process_stat(struct perf_evsel *counter, u64 tstamp)
1232 if (scripting_ops && scripting_ops->process_stat)
1233 scripting_ops->process_stat(&stat_config, counter, tstamp);
1235 __process_stat(counter, tstamp);
1238 static void process_stat_interval(u64 tstamp)
1240 if (scripting_ops && scripting_ops->process_stat_interval)
1241 scripting_ops->process_stat_interval(tstamp);
1244 static void setup_scripting(void)
1246 setup_perl_scripting();
1247 setup_python_scripting();
1250 static int flush_scripting(void)
1252 return scripting_ops ? scripting_ops->flush_script() : 0;
1255 static int cleanup_scripting(void)
1257 pr_debug("\nperf script stopped\n");
1259 return scripting_ops ? scripting_ops->stop_script() : 0;
1262 static int process_sample_event(struct perf_tool *tool,
1263 union perf_event *event,
1264 struct perf_sample *sample,
1265 struct perf_evsel *evsel,
1266 struct machine *machine)
1268 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1269 struct addr_location al;
1271 if (perf_time__skip_sample(&scr->ptime, sample->time))
1275 if (sample->time < last_timestamp) {
1276 pr_err("Samples misordered, previous: %" PRIu64
1277 " this: %" PRIu64 "\n", last_timestamp,
1281 last_timestamp = sample->time;
1285 if (machine__resolve(machine, &al, sample) < 0) {
1286 pr_err("problem processing %d event, skipping it.\n",
1287 event->header.type);
1294 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
1298 scripting_ops->process_event(event, sample, evsel, &al);
1300 process_event(scr, sample, evsel, &al, machine);
1303 addr_location__put(&al);
1307 static int process_attr(struct perf_tool *tool, union perf_event *event,
1308 struct perf_evlist **pevlist)
1310 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1311 struct perf_evlist *evlist;
1312 struct perf_evsel *evsel, *pos;
1315 err = perf_event__process_attr(tool, event, pevlist);
1320 evsel = perf_evlist__last(*pevlist);
1322 if (evsel->attr.type >= PERF_TYPE_MAX)
1325 evlist__for_each_entry(evlist, pos) {
1326 if (pos->attr.type == evsel->attr.type && pos != evsel)
1330 set_print_ip_opts(&evsel->attr);
1332 if (evsel->attr.sample_type)
1333 err = perf_evsel__check_attr(evsel, scr->session);
1338 static int process_comm_event(struct perf_tool *tool,
1339 union perf_event *event,
1340 struct perf_sample *sample,
1341 struct machine *machine)
1343 struct thread *thread;
1344 struct perf_script *script = container_of(tool, struct perf_script, tool);
1345 struct perf_session *session = script->session;
1346 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1349 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1350 if (thread == NULL) {
1351 pr_debug("problem processing COMM event, skipping it.\n");
1355 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1358 if (!evsel->attr.sample_id_all) {
1361 sample->tid = event->comm.tid;
1362 sample->pid = event->comm.pid;
1364 print_sample_start(sample, thread, evsel);
1365 perf_event__fprintf(event, stdout);
1368 thread__put(thread);
1372 static int process_namespaces_event(struct perf_tool *tool,
1373 union perf_event *event,
1374 struct perf_sample *sample,
1375 struct machine *machine)
1377 struct thread *thread;
1378 struct perf_script *script = container_of(tool, struct perf_script, tool);
1379 struct perf_session *session = script->session;
1380 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1383 thread = machine__findnew_thread(machine, event->namespaces.pid,
1384 event->namespaces.tid);
1385 if (thread == NULL) {
1386 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1390 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1393 if (!evsel->attr.sample_id_all) {
1396 sample->tid = event->namespaces.tid;
1397 sample->pid = event->namespaces.pid;
1399 print_sample_start(sample, thread, evsel);
1400 perf_event__fprintf(event, stdout);
1403 thread__put(thread);
1407 static int process_fork_event(struct perf_tool *tool,
1408 union perf_event *event,
1409 struct perf_sample *sample,
1410 struct machine *machine)
1412 struct thread *thread;
1413 struct perf_script *script = container_of(tool, struct perf_script, tool);
1414 struct perf_session *session = script->session;
1415 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1417 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1420 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1421 if (thread == NULL) {
1422 pr_debug("problem processing FORK event, skipping it.\n");
1426 if (!evsel->attr.sample_id_all) {
1428 sample->time = event->fork.time;
1429 sample->tid = event->fork.tid;
1430 sample->pid = event->fork.pid;
1432 print_sample_start(sample, thread, evsel);
1433 perf_event__fprintf(event, stdout);
1434 thread__put(thread);
1438 static int process_exit_event(struct perf_tool *tool,
1439 union perf_event *event,
1440 struct perf_sample *sample,
1441 struct machine *machine)
1444 struct thread *thread;
1445 struct perf_script *script = container_of(tool, struct perf_script, tool);
1446 struct perf_session *session = script->session;
1447 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1449 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1450 if (thread == NULL) {
1451 pr_debug("problem processing EXIT event, skipping it.\n");
1455 if (!evsel->attr.sample_id_all) {
1458 sample->tid = event->fork.tid;
1459 sample->pid = event->fork.pid;
1461 print_sample_start(sample, thread, evsel);
1462 perf_event__fprintf(event, stdout);
1464 if (perf_event__process_exit(tool, event, sample, machine) < 0)
1467 thread__put(thread);
1471 static int process_mmap_event(struct perf_tool *tool,
1472 union perf_event *event,
1473 struct perf_sample *sample,
1474 struct machine *machine)
1476 struct thread *thread;
1477 struct perf_script *script = container_of(tool, struct perf_script, tool);
1478 struct perf_session *session = script->session;
1479 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1481 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1484 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1485 if (thread == NULL) {
1486 pr_debug("problem processing MMAP event, skipping it.\n");
1490 if (!evsel->attr.sample_id_all) {
1493 sample->tid = event->mmap.tid;
1494 sample->pid = event->mmap.pid;
1496 print_sample_start(sample, thread, evsel);
1497 perf_event__fprintf(event, stdout);
1498 thread__put(thread);
1502 static int process_mmap2_event(struct perf_tool *tool,
1503 union perf_event *event,
1504 struct perf_sample *sample,
1505 struct machine *machine)
1507 struct thread *thread;
1508 struct perf_script *script = container_of(tool, struct perf_script, tool);
1509 struct perf_session *session = script->session;
1510 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1512 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1515 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1516 if (thread == NULL) {
1517 pr_debug("problem processing MMAP2 event, skipping it.\n");
1521 if (!evsel->attr.sample_id_all) {
1524 sample->tid = event->mmap2.tid;
1525 sample->pid = event->mmap2.pid;
1527 print_sample_start(sample, thread, evsel);
1528 perf_event__fprintf(event, stdout);
1529 thread__put(thread);
1533 static int process_switch_event(struct perf_tool *tool,
1534 union perf_event *event,
1535 struct perf_sample *sample,
1536 struct machine *machine)
1538 struct thread *thread;
1539 struct perf_script *script = container_of(tool, struct perf_script, tool);
1540 struct perf_session *session = script->session;
1541 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1543 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1546 thread = machine__findnew_thread(machine, sample->pid,
1548 if (thread == NULL) {
1549 pr_debug("problem processing SWITCH event, skipping it.\n");
1553 print_sample_start(sample, thread, evsel);
1554 perf_event__fprintf(event, stdout);
1555 thread__put(thread);
1559 static void sig_handler(int sig __maybe_unused)
1564 static int __cmd_script(struct perf_script *script)
1568 signal(SIGINT, sig_handler);
1570 /* override event processing functions */
1571 if (script->show_task_events) {
1572 script->tool.comm = process_comm_event;
1573 script->tool.fork = process_fork_event;
1574 script->tool.exit = process_exit_event;
1576 if (script->show_mmap_events) {
1577 script->tool.mmap = process_mmap_event;
1578 script->tool.mmap2 = process_mmap2_event;
1580 if (script->show_switch_events)
1581 script->tool.context_switch = process_switch_event;
1582 if (script->show_namespace_events)
1583 script->tool.namespaces = process_namespaces_event;
1585 ret = perf_session__process_events(script->session);
1588 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
1593 struct script_spec {
1594 struct list_head node;
1595 struct scripting_ops *ops;
1599 static LIST_HEAD(script_specs);
1601 static struct script_spec *script_spec__new(const char *spec,
1602 struct scripting_ops *ops)
1604 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1607 strcpy(s->spec, spec);
1614 static void script_spec__add(struct script_spec *s)
1616 list_add_tail(&s->node, &script_specs);
1619 static struct script_spec *script_spec__find(const char *spec)
1621 struct script_spec *s;
1623 list_for_each_entry(s, &script_specs, node)
1624 if (strcasecmp(s->spec, spec) == 0)
1629 int script_spec_register(const char *spec, struct scripting_ops *ops)
1631 struct script_spec *s;
1633 s = script_spec__find(spec);
1637 s = script_spec__new(spec, ops);
1641 script_spec__add(s);
1646 static struct scripting_ops *script_spec__lookup(const char *spec)
1648 struct script_spec *s = script_spec__find(spec);
1655 static void list_available_languages(void)
1657 struct script_spec *s;
1659 fprintf(stderr, "\n");
1660 fprintf(stderr, "Scripting language extensions (used in "
1661 "perf script -s [spec:]script.[spec]):\n\n");
1663 list_for_each_entry(s, &script_specs, node)
1664 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1666 fprintf(stderr, "\n");
1669 static int parse_scriptname(const struct option *opt __maybe_unused,
1670 const char *str, int unset __maybe_unused)
1672 char spec[PATH_MAX];
1673 const char *script, *ext;
1676 if (strcmp(str, "lang") == 0) {
1677 list_available_languages();
1681 script = strchr(str, ':');
1684 if (len >= PATH_MAX) {
1685 fprintf(stderr, "invalid language specifier");
1688 strncpy(spec, str, len);
1690 scripting_ops = script_spec__lookup(spec);
1691 if (!scripting_ops) {
1692 fprintf(stderr, "invalid language specifier");
1698 ext = strrchr(script, '.');
1700 fprintf(stderr, "invalid script extension");
1703 scripting_ops = script_spec__lookup(++ext);
1704 if (!scripting_ops) {
1705 fprintf(stderr, "invalid script extension");
1710 script_name = strdup(script);
1715 static int parse_output_fields(const struct option *opt __maybe_unused,
1716 const char *arg, int unset __maybe_unused)
1718 char *tok, *strtok_saveptr = NULL;
1719 int i, imax = ARRAY_SIZE(all_output_options);
1722 char *str = strdup(arg);
1728 /* first word can state for which event type the user is specifying
1729 * the fields. If no type exists, the specified fields apply to all
1730 * event types found in the file minus the invalid fields for a type.
1732 tok = strchr(str, ':');
1736 if (!strcmp(str, "hw"))
1737 type = PERF_TYPE_HARDWARE;
1738 else if (!strcmp(str, "sw"))
1739 type = PERF_TYPE_SOFTWARE;
1740 else if (!strcmp(str, "trace"))
1741 type = PERF_TYPE_TRACEPOINT;
1742 else if (!strcmp(str, "raw"))
1743 type = PERF_TYPE_RAW;
1744 else if (!strcmp(str, "break"))
1745 type = PERF_TYPE_BREAKPOINT;
1747 fprintf(stderr, "Invalid event type in field string.\n");
1752 if (output[type].user_set)
1753 pr_warning("Overriding previous field request for %s events.\n",
1756 output[type].fields = 0;
1757 output[type].user_set = true;
1758 output[type].wildcard_set = false;
1762 if (strlen(str) == 0) {
1764 "Cannot set fields to 'none' for all event types.\n");
1769 if (output_set_by_user())
1770 pr_warning("Overriding previous field request for all events.\n");
1772 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1773 output[j].fields = 0;
1774 output[j].user_set = true;
1775 output[j].wildcard_set = true;
1779 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
1780 for (i = 0; i < imax; ++i) {
1781 if (strcmp(tok, all_output_options[i].str) == 0)
1784 if (i == imax && strcmp(tok, "flags") == 0) {
1789 fprintf(stderr, "Invalid field requested.\n");
1795 /* add user option to all events types for
1798 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1799 if (output[j].invalid_fields & all_output_options[i].field) {
1800 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1801 all_output_options[i].str, event_type(j));
1803 output[j].fields |= all_output_options[i].field;
1806 if (output[type].invalid_fields & all_output_options[i].field) {
1807 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1808 all_output_options[i].str, event_type(type));
1813 output[type].fields |= all_output_options[i].field;
1818 if (output[type].fields == 0) {
1819 pr_debug("No fields requested for %s type. "
1820 "Events will not be displayed.\n", event_type(type));
1829 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1830 static int is_directory(const char *base_path, const struct dirent *dent)
1832 char path[PATH_MAX];
1835 sprintf(path, "%s/%s", base_path, dent->d_name);
1836 if (stat(path, &st))
1839 return S_ISDIR(st.st_mode);
1842 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1843 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1844 if ((lang_dirent->d_type == DT_DIR || \
1845 (lang_dirent->d_type == DT_UNKNOWN && \
1846 is_directory(scripts_path, lang_dirent))) && \
1847 (strcmp(lang_dirent->d_name, ".")) && \
1848 (strcmp(lang_dirent->d_name, "..")))
1850 #define for_each_script(lang_path, lang_dir, script_dirent) \
1851 while ((script_dirent = readdir(lang_dir)) != NULL) \
1852 if (script_dirent->d_type != DT_DIR && \
1853 (script_dirent->d_type != DT_UNKNOWN || \
1854 !is_directory(lang_path, script_dirent)))
1857 #define RECORD_SUFFIX "-record"
1858 #define REPORT_SUFFIX "-report"
1860 struct script_desc {
1861 struct list_head node;
1867 static LIST_HEAD(script_descs);
1869 static struct script_desc *script_desc__new(const char *name)
1871 struct script_desc *s = zalloc(sizeof(*s));
1873 if (s != NULL && name)
1874 s->name = strdup(name);
1879 static void script_desc__delete(struct script_desc *s)
1882 zfree(&s->half_liner);
1887 static void script_desc__add(struct script_desc *s)
1889 list_add_tail(&s->node, &script_descs);
1892 static struct script_desc *script_desc__find(const char *name)
1894 struct script_desc *s;
1896 list_for_each_entry(s, &script_descs, node)
1897 if (strcasecmp(s->name, name) == 0)
1902 static struct script_desc *script_desc__findnew(const char *name)
1904 struct script_desc *s = script_desc__find(name);
1909 s = script_desc__new(name);
1911 goto out_delete_desc;
1913 script_desc__add(s);
1918 script_desc__delete(s);
1923 static const char *ends_with(const char *str, const char *suffix)
1925 size_t suffix_len = strlen(suffix);
1926 const char *p = str;
1928 if (strlen(str) > suffix_len) {
1929 p = str + strlen(str) - suffix_len;
1930 if (!strncmp(p, suffix, suffix_len))
1937 static int read_script_info(struct script_desc *desc, const char *filename)
1939 char line[BUFSIZ], *p;
1942 fp = fopen(filename, "r");
1946 while (fgets(line, sizeof(line), fp)) {
1953 if (strlen(p) && *p == '!')
1957 if (strlen(p) && p[strlen(p) - 1] == '\n')
1958 p[strlen(p) - 1] = '\0';
1960 if (!strncmp(p, "description:", strlen("description:"))) {
1961 p += strlen("description:");
1962 desc->half_liner = strdup(ltrim(p));
1966 if (!strncmp(p, "args:", strlen("args:"))) {
1967 p += strlen("args:");
1968 desc->args = strdup(ltrim(p));
1978 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1980 char *script_root, *str;
1982 script_root = strdup(script_dirent->d_name);
1986 str = (char *)ends_with(script_root, suffix);
1996 static int list_available_scripts(const struct option *opt __maybe_unused,
1997 const char *s __maybe_unused,
1998 int unset __maybe_unused)
2000 struct dirent *script_dirent, *lang_dirent;
2001 char scripts_path[MAXPATHLEN];
2002 DIR *scripts_dir, *lang_dir;
2003 char script_path[MAXPATHLEN];
2004 char lang_path[MAXPATHLEN];
2005 struct script_desc *desc;
2006 char first_half[BUFSIZ];
2009 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2011 scripts_dir = opendir(scripts_path);
2014 "open(%s) failed.\n"
2015 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2020 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2021 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2022 lang_dirent->d_name);
2023 lang_dir = opendir(lang_path);
2027 for_each_script(lang_path, lang_dir, script_dirent) {
2028 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
2030 desc = script_desc__findnew(script_root);
2031 snprintf(script_path, MAXPATHLEN, "%s/%s",
2032 lang_path, script_dirent->d_name);
2033 read_script_info(desc, script_path);
2039 fprintf(stdout, "List of available trace scripts:\n");
2040 list_for_each_entry(desc, &script_descs, node) {
2041 sprintf(first_half, "%s %s", desc->name,
2042 desc->args ? desc->args : "");
2043 fprintf(stdout, " %-36s %s\n", first_half,
2044 desc->half_liner ? desc->half_liner : "");
2051 * Some scripts specify the required events in their "xxx-record" file,
2052 * this function will check if the events in perf.data match those
2053 * mentioned in the "xxx-record".
2055 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2056 * which is covered well now. And new parsing code should be added to
2057 * cover the future complexing formats like event groups etc.
2059 static int check_ev_match(char *dir_name, char *scriptname,
2060 struct perf_session *session)
2062 char filename[MAXPATHLEN], evname[128];
2063 char line[BUFSIZ], *p;
2064 struct perf_evsel *pos;
2068 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2070 fp = fopen(filename, "r");
2074 while (fgets(line, sizeof(line), fp)) {
2080 p = strstr(p, "-e");
2086 len = strcspn(p, " \t");
2090 snprintf(evname, len + 1, "%s", p);
2093 evlist__for_each_entry(session->evlist, pos) {
2094 if (!strcmp(perf_evsel__name(pos), evname)) {
2112 * Return -1 if none is found, otherwise the actual scripts number.
2114 * Currently the only user of this function is the script browser, which
2115 * will list all statically runnable scripts, select one, execute it and
2116 * show the output in a perf browser.
2118 int find_scripts(char **scripts_array, char **scripts_path_array)
2120 struct dirent *script_dirent, *lang_dirent;
2121 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
2122 DIR *scripts_dir, *lang_dir;
2123 struct perf_session *session;
2124 struct perf_data_file file = {
2126 .mode = PERF_DATA_MODE_READ,
2131 session = perf_session__new(&file, false, NULL);
2135 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2137 scripts_dir = opendir(scripts_path);
2139 perf_session__delete(session);
2143 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2144 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2145 lang_dirent->d_name);
2147 if (strstr(lang_path, "perl"))
2151 if (strstr(lang_path, "python"))
2155 lang_dir = opendir(lang_path);
2159 for_each_script(lang_path, lang_dir, script_dirent) {
2160 /* Skip those real time scripts: xxxtop.p[yl] */
2161 if (strstr(script_dirent->d_name, "top."))
2163 sprintf(scripts_path_array[i], "%s/%s", lang_path,
2164 script_dirent->d_name);
2165 temp = strchr(script_dirent->d_name, '.');
2166 snprintf(scripts_array[i],
2167 (temp - script_dirent->d_name) + 1,
2168 "%s", script_dirent->d_name);
2170 if (check_ev_match(lang_path,
2171 scripts_array[i], session))
2179 closedir(scripts_dir);
2180 perf_session__delete(session);
2184 static char *get_script_path(const char *script_root, const char *suffix)
2186 struct dirent *script_dirent, *lang_dirent;
2187 char scripts_path[MAXPATHLEN];
2188 char script_path[MAXPATHLEN];
2189 DIR *scripts_dir, *lang_dir;
2190 char lang_path[MAXPATHLEN];
2191 char *__script_root;
2193 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2195 scripts_dir = opendir(scripts_path);
2199 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2200 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2201 lang_dirent->d_name);
2202 lang_dir = opendir(lang_path);
2206 for_each_script(lang_path, lang_dir, script_dirent) {
2207 __script_root = get_script_root(script_dirent, suffix);
2208 if (__script_root && !strcmp(script_root, __script_root)) {
2209 free(__script_root);
2211 closedir(scripts_dir);
2212 snprintf(script_path, MAXPATHLEN, "%s/%s",
2213 lang_path, script_dirent->d_name);
2214 return strdup(script_path);
2216 free(__script_root);
2220 closedir(scripts_dir);
2225 static bool is_top_script(const char *script_path)
2227 return ends_with(script_path, "top") == NULL ? false : true;
2230 static int has_required_arg(char *script_path)
2232 struct script_desc *desc;
2236 desc = script_desc__new(NULL);
2238 if (read_script_info(desc, script_path))
2244 for (p = desc->args; *p; p++)
2248 script_desc__delete(desc);
2253 static int have_cmd(int argc, const char **argv)
2255 char **__argv = malloc(sizeof(const char *) * argc);
2258 pr_err("malloc failed\n");
2262 memcpy(__argv, argv, sizeof(const char *) * argc);
2263 argc = parse_options(argc, (const char **)__argv, record_options,
2264 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2267 system_wide = (argc == 0);
2272 static void script__setup_sample_type(struct perf_script *script)
2274 struct perf_session *session = script->session;
2275 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2277 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2278 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2279 (sample_type & PERF_SAMPLE_STACK_USER))
2280 callchain_param.record_mode = CALLCHAIN_DWARF;
2281 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2282 callchain_param.record_mode = CALLCHAIN_LBR;
2284 callchain_param.record_mode = CALLCHAIN_FP;
2288 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2289 union perf_event *event,
2290 struct perf_session *session)
2292 struct stat_round_event *round = &event->stat_round;
2293 struct perf_evsel *counter;
2295 evlist__for_each_entry(session->evlist, counter) {
2296 perf_stat_process_counter(&stat_config, counter);
2297 process_stat(counter, round->time);
2300 process_stat_interval(round->time);
2304 static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2305 union perf_event *event,
2306 struct perf_session *session __maybe_unused)
2308 perf_event__read_stat_config(&stat_config, &event->stat_config);
2312 static int set_maps(struct perf_script *script)
2314 struct perf_evlist *evlist = script->session->evlist;
2316 if (!script->cpus || !script->threads)
2319 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2322 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2324 if (perf_evlist__alloc_stats(evlist, true))
2327 script->allocated = true;
2332 int process_thread_map_event(struct perf_tool *tool,
2333 union perf_event *event,
2334 struct perf_session *session __maybe_unused)
2336 struct perf_script *script = container_of(tool, struct perf_script, tool);
2338 if (script->threads) {
2339 pr_warning("Extra thread map event, ignoring.\n");
2343 script->threads = thread_map__new_event(&event->thread_map);
2344 if (!script->threads)
2347 return set_maps(script);
2351 int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2352 union perf_event *event,
2353 struct perf_session *session __maybe_unused)
2355 struct perf_script *script = container_of(tool, struct perf_script, tool);
2358 pr_warning("Extra cpu map event, ignoring.\n");
2362 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2366 return set_maps(script);
2369 int cmd_script(int argc, const char **argv)
2371 bool show_full_info = false;
2372 bool header = false;
2373 bool header_only = false;
2374 bool script_started = false;
2375 char *rec_script_path = NULL;
2376 char *rep_script_path = NULL;
2377 struct perf_session *session;
2378 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
2379 char *script_path = NULL;
2380 const char **__argv;
2382 struct perf_script script = {
2384 .sample = process_sample_event,
2385 .mmap = perf_event__process_mmap,
2386 .mmap2 = perf_event__process_mmap2,
2387 .comm = perf_event__process_comm,
2388 .namespaces = perf_event__process_namespaces,
2389 .exit = perf_event__process_exit,
2390 .fork = perf_event__process_fork,
2391 .attr = process_attr,
2392 .event_update = perf_event__process_event_update,
2393 .tracing_data = perf_event__process_tracing_data,
2394 .build_id = perf_event__process_build_id,
2395 .id_index = perf_event__process_id_index,
2396 .auxtrace_info = perf_event__process_auxtrace_info,
2397 .auxtrace = perf_event__process_auxtrace,
2398 .auxtrace_error = perf_event__process_auxtrace_error,
2399 .stat = perf_event__process_stat_event,
2400 .stat_round = process_stat_round_event,
2401 .stat_config = process_stat_config_event,
2402 .thread_map = process_thread_map_event,
2403 .cpu_map = process_cpu_map_event,
2404 .ordered_events = true,
2405 .ordering_requires_timestamps = true,
2408 struct perf_data_file file = {
2409 .mode = PERF_DATA_MODE_READ,
2411 const struct option options[] = {
2412 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2413 "dump raw trace in ASCII"),
2414 OPT_INCR('v', "verbose", &verbose,
2415 "be more verbose (show symbol address, etc)"),
2416 OPT_BOOLEAN('L', "Latency", &latency_format,
2417 "show latency attributes (irqs/preemption disabled, etc)"),
2418 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2419 list_available_scripts),
2420 OPT_CALLBACK('s', "script", NULL, "name",
2421 "script file name (lang:script name, script name, or *)",
2423 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2424 "generate perf-script.xx script in specified language"),
2425 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2426 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2427 "do various checks like samples ordering and lost events"),
2428 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2429 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
2430 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2431 "file", "vmlinux pathname"),
2432 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2433 "file", "kallsyms pathname"),
2434 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2435 "When printing symbols do not display call chain"),
2436 OPT_CALLBACK(0, "symfs", NULL, "directory",
2437 "Look for files with symbols relative to this directory",
2438 symbol__config_symfs),
2439 OPT_CALLBACK('F', "fields", NULL, "str",
2440 "comma separated output fields prepend with 'type:'. "
2441 "Valid types: hw,sw,trace,raw. "
2442 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2443 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
2444 "bpf-output,callindent,insn,insnlen,brstackinsn",
2445 parse_output_fields),
2446 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2447 "system-wide collection from all CPUs"),
2448 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2449 "only consider these symbols"),
2450 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2451 "Stop display of callgraph at these symbols"),
2452 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2453 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2454 "only display events for these comms"),
2455 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2456 "only consider symbols in these pids"),
2457 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2458 "only consider symbols in these tids"),
2459 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2460 "Set the maximum stack depth when parsing the callchain, "
2461 "anything beyond the specified depth will be ignored. "
2462 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
2463 OPT_BOOLEAN('I', "show-info", &show_full_info,
2464 "display extended information from perf.data file"),
2465 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2466 "Show the path of [kernel.kallsyms]"),
2467 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2468 "Show the fork/comm/exit events"),
2469 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2470 "Show the mmap events"),
2471 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2472 "Show context switch events (if recorded)"),
2473 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2474 "Show namespace events (if recorded)"),
2475 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2476 OPT_INTEGER(0, "max-blocks", &max_blocks,
2477 "Maximum number of code blocks to dump with brstackinsn"),
2478 OPT_BOOLEAN(0, "ns", &nanosecs,
2479 "Use 9 decimal places when displaying time"),
2480 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2481 "Instruction Tracing options",
2482 itrace_parse_synth_opts),
2483 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2484 "Show full source file name path for source lines"),
2485 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2486 "Enable symbol demangling"),
2487 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2488 "Enable kernel symbol demangling"),
2489 OPT_STRING(0, "time", &script.time_str, "str",
2490 "Time span of interest (start,stop)"),
2493 const char * const script_subcommands[] = { "record", "report", NULL };
2494 const char *script_usage[] = {
2495 "perf script [<options>]",
2496 "perf script [<options>] record <script> [<record-options>] <command>",
2497 "perf script [<options>] report <script> [script-args]",
2498 "perf script [<options>] <script> [<record-options>] <command>",
2499 "perf script [<options>] <top-script> [script-args]",
2505 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
2506 PARSE_OPT_STOP_AT_NON_OPTION);
2508 file.path = input_name;
2509 file.force = symbol_conf.force;
2511 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2512 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2513 if (!rec_script_path)
2514 return cmd_record(argc, argv);
2517 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2518 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2519 if (!rep_script_path) {
2521 "Please specify a valid report script"
2522 "(see 'perf script -l' for listing)\n");
2527 if (itrace_synth_opts.callchain &&
2528 itrace_synth_opts.callchain_sz > scripting_max_stack)
2529 scripting_max_stack = itrace_synth_opts.callchain_sz;
2531 /* make sure PERF_EXEC_PATH is set for scripts */
2532 set_argv_exec_path(get_argv_exec_path());
2534 if (argc && !script_name && !rec_script_path && !rep_script_path) {
2539 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2540 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2542 if (!rec_script_path && !rep_script_path) {
2543 usage_with_options_msg(script_usage, options,
2544 "Couldn't find script `%s'\n\n See perf"
2545 " script -l for available scripts.\n", argv[0]);
2548 if (is_top_script(argv[0])) {
2549 rep_args = argc - 1;
2553 rep_args = has_required_arg(rep_script_path);
2554 rec_args = (argc - 1) - rep_args;
2556 usage_with_options_msg(script_usage, options,
2557 "`%s' script requires options."
2558 "\n\n See perf script -l for available "
2559 "scripts and options.\n", argv[0]);
2563 if (pipe(live_pipe) < 0) {
2564 perror("failed to create pipe");
2570 perror("failed to fork");
2577 dup2(live_pipe[1], 1);
2578 close(live_pipe[0]);
2580 if (is_top_script(argv[0])) {
2582 } else if (!system_wide) {
2583 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2589 __argv = malloc((argc + 6) * sizeof(const char *));
2591 pr_err("malloc failed\n");
2596 __argv[j++] = "/bin/sh";
2597 __argv[j++] = rec_script_path;
2603 for (i = rep_args + 1; i < argc; i++)
2604 __argv[j++] = argv[i];
2607 execvp("/bin/sh", (char **)__argv);
2612 dup2(live_pipe[0], 0);
2613 close(live_pipe[1]);
2615 __argv = malloc((argc + 4) * sizeof(const char *));
2617 pr_err("malloc failed\n");
2623 __argv[j++] = "/bin/sh";
2624 __argv[j++] = rep_script_path;
2625 for (i = 1; i < rep_args + 1; i++)
2626 __argv[j++] = argv[i];
2631 execvp("/bin/sh", (char **)__argv);
2636 if (rec_script_path)
2637 script_path = rec_script_path;
2638 if (rep_script_path)
2639 script_path = rep_script_path;
2644 if (!rec_script_path)
2645 system_wide = false;
2646 else if (!system_wide) {
2647 if (have_cmd(argc - 1, &argv[1]) != 0) {
2653 __argv = malloc((argc + 2) * sizeof(const char *));
2655 pr_err("malloc failed\n");
2660 __argv[j++] = "/bin/sh";
2661 __argv[j++] = script_path;
2664 for (i = 2; i < argc; i++)
2665 __argv[j++] = argv[i];
2668 execvp("/bin/sh", (char **)__argv);
2676 session = perf_session__new(&file, false, &script.tool);
2677 if (session == NULL)
2680 if (header || header_only) {
2681 perf_session__fprintf_info(session, stdout, show_full_info);
2686 if (symbol__init(&session->header.env) < 0)
2689 script.session = session;
2690 script__setup_sample_type(&script);
2692 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2693 itrace_synth_opts.thread_stack = true;
2695 session->itrace_synth_opts = &itrace_synth_opts;
2698 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2704 symbol_conf.use_callchain = true;
2706 symbol_conf.use_callchain = false;
2708 if (session->tevent.pevent &&
2709 pevent_set_function_resolver(session->tevent.pevent,
2710 machine__resolve_kernel_addr,
2711 &session->machines.host) < 0) {
2712 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2716 if (generate_script_lang) {
2717 struct stat perf_stat;
2720 if (output_set_by_user()) {
2722 "custom fields not supported for generated scripts");
2727 input = open(file.path, O_RDONLY); /* input_name */
2730 perror("failed to open file");
2734 err = fstat(input, &perf_stat);
2736 perror("failed to stat file");
2740 if (!perf_stat.st_size) {
2741 fprintf(stderr, "zero-sized file, nothing to do!\n");
2745 scripting_ops = script_spec__lookup(generate_script_lang);
2746 if (!scripting_ops) {
2747 fprintf(stderr, "invalid language specifier");
2752 err = scripting_ops->generate_script(session->tevent.pevent,
2758 err = scripting_ops->start_script(script_name, argc, argv);
2761 pr_debug("perf script started with script %s\n\n", script_name);
2762 script_started = true;
2766 err = perf_session__check_output_opt(session);
2770 /* needs to be parsed after looking up reference time */
2771 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
2772 pr_err("Invalid time string\n");
2776 err = __cmd_script(&script);
2781 perf_evlist__free_stats(session->evlist);
2782 perf_session__delete(session);
2785 cleanup_scripting();