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 <linux/bitmap.h>
26 #include <linux/stringify.h>
27 #include <linux/time64.h>
29 #include "util/mem-events.h"
31 static char const *script_name;
32 static char const *generate_script_lang;
33 static bool debug_mode;
34 static u64 last_timestamp;
35 static u64 nr_unordered;
36 static bool no_callchain;
37 static bool latency_format;
38 static bool system_wide;
39 static bool print_flags;
41 static const char *cpu_list;
42 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
43 static struct perf_stat_config stat_config;
45 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
47 enum perf_output_field {
48 PERF_OUTPUT_COMM = 1U << 0,
49 PERF_OUTPUT_TID = 1U << 1,
50 PERF_OUTPUT_PID = 1U << 2,
51 PERF_OUTPUT_TIME = 1U << 3,
52 PERF_OUTPUT_CPU = 1U << 4,
53 PERF_OUTPUT_EVNAME = 1U << 5,
54 PERF_OUTPUT_TRACE = 1U << 6,
55 PERF_OUTPUT_IP = 1U << 7,
56 PERF_OUTPUT_SYM = 1U << 8,
57 PERF_OUTPUT_DSO = 1U << 9,
58 PERF_OUTPUT_ADDR = 1U << 10,
59 PERF_OUTPUT_SYMOFFSET = 1U << 11,
60 PERF_OUTPUT_SRCLINE = 1U << 12,
61 PERF_OUTPUT_PERIOD = 1U << 13,
62 PERF_OUTPUT_IREGS = 1U << 14,
63 PERF_OUTPUT_BRSTACK = 1U << 15,
64 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
65 PERF_OUTPUT_DATA_SRC = 1U << 17,
66 PERF_OUTPUT_WEIGHT = 1U << 18,
67 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
68 PERF_OUTPUT_CALLINDENT = 1U << 20,
69 PERF_OUTPUT_INSN = 1U << 21,
70 PERF_OUTPUT_INSNLEN = 1U << 22,
73 struct output_option {
75 enum perf_output_field field;
76 } all_output_options[] = {
77 {.str = "comm", .field = PERF_OUTPUT_COMM},
78 {.str = "tid", .field = PERF_OUTPUT_TID},
79 {.str = "pid", .field = PERF_OUTPUT_PID},
80 {.str = "time", .field = PERF_OUTPUT_TIME},
81 {.str = "cpu", .field = PERF_OUTPUT_CPU},
82 {.str = "event", .field = PERF_OUTPUT_EVNAME},
83 {.str = "trace", .field = PERF_OUTPUT_TRACE},
84 {.str = "ip", .field = PERF_OUTPUT_IP},
85 {.str = "sym", .field = PERF_OUTPUT_SYM},
86 {.str = "dso", .field = PERF_OUTPUT_DSO},
87 {.str = "addr", .field = PERF_OUTPUT_ADDR},
88 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
89 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
90 {.str = "period", .field = PERF_OUTPUT_PERIOD},
91 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
92 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
93 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
94 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
95 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
96 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
97 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
98 {.str = "insn", .field = PERF_OUTPUT_INSN},
99 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
102 /* default set to maintain compatibility with current format */
106 unsigned int print_ip_opts;
109 } output[PERF_TYPE_MAX] = {
111 [PERF_TYPE_HARDWARE] = {
114 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
115 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
116 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
117 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
120 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
123 [PERF_TYPE_SOFTWARE] = {
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 |
130 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
132 .invalid_fields = PERF_OUTPUT_TRACE,
135 [PERF_TYPE_TRACEPOINT] = {
138 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
139 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
140 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
146 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
147 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
148 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
149 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
150 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
151 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
153 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
156 [PERF_TYPE_BREAKPOINT] = {
159 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
160 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
161 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
162 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
165 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
169 static bool output_set_by_user(void)
172 for (j = 0; j < PERF_TYPE_MAX; ++j) {
173 if (output[j].user_set)
179 static const char *output_field2str(enum perf_output_field field)
181 int i, imax = ARRAY_SIZE(all_output_options);
182 const char *str = "";
184 for (i = 0; i < imax; ++i) {
185 if (all_output_options[i].field == field) {
186 str = all_output_options[i].str;
193 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
195 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
196 u64 sample_type, const char *sample_msg,
197 enum perf_output_field field,
200 struct perf_event_attr *attr = &evsel->attr;
201 int type = attr->type;
204 if (attr->sample_type & sample_type)
207 if (output[type].user_set) {
210 evname = perf_evsel__name(evsel);
211 pr_err("Samples for '%s' event do not have %s attribute set. "
212 "Cannot print '%s' field.\n",
213 evname, sample_msg, output_field2str(field));
217 /* user did not ask for it explicitly so remove from the default list */
218 output[type].fields &= ~field;
219 evname = perf_evsel__name(evsel);
220 pr_debug("Samples for '%s' event do not have %s attribute set. "
221 "Skipping '%s' field.\n",
222 evname, sample_msg, output_field2str(field));
227 static int perf_evsel__check_stype(struct perf_evsel *evsel,
228 u64 sample_type, const char *sample_msg,
229 enum perf_output_field field)
231 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
235 static int perf_evsel__check_attr(struct perf_evsel *evsel,
236 struct perf_session *session)
238 struct perf_event_attr *attr = &evsel->attr;
241 if (perf_header__has_feat(&session->header, HEADER_STAT))
244 allow_user_set = perf_header__has_feat(&session->header,
247 if (PRINT_FIELD(TRACE) &&
248 !perf_session__has_traces(session, "record -R"))
251 if (PRINT_FIELD(IP)) {
252 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
257 if (PRINT_FIELD(ADDR) &&
258 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
259 PERF_OUTPUT_ADDR, allow_user_set))
262 if (PRINT_FIELD(DATA_SRC) &&
263 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
264 PERF_OUTPUT_DATA_SRC))
267 if (PRINT_FIELD(WEIGHT) &&
268 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
272 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
273 pr_err("Display of symbols requested but neither sample IP nor "
274 "sample address\nis selected. Hence, no addresses to convert "
278 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
279 pr_err("Display of offsets requested but symbol is not"
283 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
284 pr_err("Display of DSO requested but neither sample IP nor "
285 "sample address\nis selected. Hence, no addresses to convert "
289 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
290 pr_err("Display of source line number requested but sample IP is not\n"
291 "selected. Hence, no address to lookup the source line number.\n");
295 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
296 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
297 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
300 if (PRINT_FIELD(TIME) &&
301 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
305 if (PRINT_FIELD(CPU) &&
306 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
307 PERF_OUTPUT_CPU, allow_user_set))
310 if (PRINT_FIELD(PERIOD) &&
311 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
315 if (PRINT_FIELD(IREGS) &&
316 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
323 static void set_print_ip_opts(struct perf_event_attr *attr)
325 unsigned int type = attr->type;
327 output[type].print_ip_opts = 0;
329 output[type].print_ip_opts |= EVSEL__PRINT_IP;
331 if (PRINT_FIELD(SYM))
332 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
334 if (PRINT_FIELD(DSO))
335 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
337 if (PRINT_FIELD(SYMOFFSET))
338 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
340 if (PRINT_FIELD(SRCLINE))
341 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
345 * verify all user requested events exist and the samples
346 * have the expected data
348 static int perf_session__check_output_opt(struct perf_session *session)
351 struct perf_evsel *evsel;
353 for (j = 0; j < PERF_TYPE_MAX; ++j) {
354 evsel = perf_session__find_first_evtype(session, j);
357 * even if fields is set to 0 (ie., show nothing) event must
358 * exist if user explicitly includes it on the command line
360 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
361 pr_err("%s events do not exist. "
362 "Remove corresponding -f option to proceed.\n",
367 if (evsel && output[j].fields &&
368 perf_evsel__check_attr(evsel, session))
374 set_print_ip_opts(&evsel->attr);
378 bool use_callchain = false;
379 bool not_pipe = false;
381 evlist__for_each_entry(session->evlist, evsel) {
383 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
384 use_callchain = true;
388 if (not_pipe && !use_callchain)
389 symbol_conf.use_callchain = false;
393 * set default for tracepoints to print symbols only
394 * if callchains are present
396 if (symbol_conf.use_callchain &&
397 !output[PERF_TYPE_TRACEPOINT].user_set) {
398 struct perf_event_attr *attr;
400 j = PERF_TYPE_TRACEPOINT;
402 evlist__for_each_entry(session->evlist, evsel) {
403 if (evsel->attr.type != j)
408 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
409 output[j].fields |= PERF_OUTPUT_IP;
410 output[j].fields |= PERF_OUTPUT_SYM;
411 output[j].fields |= PERF_OUTPUT_DSO;
412 set_print_ip_opts(attr);
422 static void print_sample_iregs(struct perf_sample *sample,
423 struct perf_event_attr *attr)
425 struct regs_dump *regs = &sample->intr_regs;
426 uint64_t mask = attr->sample_regs_intr;
432 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
433 u64 val = regs->regs[i++];
434 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
438 static void print_sample_start(struct perf_sample *sample,
439 struct thread *thread,
440 struct perf_evsel *evsel)
442 struct perf_event_attr *attr = &evsel->attr;
445 unsigned long long nsecs;
447 if (PRINT_FIELD(COMM)) {
449 printf("%8.8s ", thread__comm_str(thread));
450 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
451 printf("%s ", thread__comm_str(thread));
453 printf("%16s ", thread__comm_str(thread));
456 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
457 printf("%5d/%-5d ", sample->pid, sample->tid);
458 else if (PRINT_FIELD(PID))
459 printf("%5d ", sample->pid);
460 else if (PRINT_FIELD(TID))
461 printf("%5d ", sample->tid);
463 if (PRINT_FIELD(CPU)) {
465 printf("%3d ", sample->cpu);
467 printf("[%03d] ", sample->cpu);
470 if (PRINT_FIELD(TIME)) {
471 nsecs = sample->time;
472 secs = nsecs / NSEC_PER_SEC;
473 nsecs -= secs * NSEC_PER_SEC;
474 usecs = nsecs / NSEC_PER_USEC;
476 printf("%5lu.%09llu: ", secs, nsecs);
478 printf("%5lu.%06lu: ", secs, usecs);
483 mispred_str(struct branch_entry *br)
485 if (!(br->flags.mispred || br->flags.predicted))
488 return br->flags.predicted ? 'P' : 'M';
491 static void print_sample_brstack(struct perf_sample *sample)
493 struct branch_stack *br = sample->branch_stack;
499 for (i = 0; i < br->nr; i++) {
500 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
503 mispred_str( br->entries + i),
504 br->entries[i].flags.in_tx? 'X' : '-',
505 br->entries[i].flags.abort? 'A' : '-',
506 br->entries[i].flags.cycles);
510 static void print_sample_brstacksym(struct perf_sample *sample,
511 struct thread *thread)
513 struct branch_stack *br = sample->branch_stack;
514 struct addr_location alf, alt;
520 for (i = 0; i < br->nr; i++) {
522 memset(&alf, 0, sizeof(alf));
523 memset(&alt, 0, sizeof(alt));
524 from = br->entries[i].from;
525 to = br->entries[i].to;
527 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
529 alf.sym = map__find_symbol(alf.map, alf.addr);
531 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
533 alt.sym = map__find_symbol(alt.map, alt.addr);
535 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
537 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
538 printf("/%c/%c/%c/%d ",
539 mispred_str( br->entries + i),
540 br->entries[i].flags.in_tx? 'X' : '-',
541 br->entries[i].flags.abort? 'A' : '-',
542 br->entries[i].flags.cycles);
547 static void print_sample_addr(struct perf_sample *sample,
548 struct thread *thread,
549 struct perf_event_attr *attr)
551 struct addr_location al;
553 printf("%16" PRIx64, sample->addr);
555 if (!sample_addr_correlates_sym(attr))
558 thread__resolve(thread, &al, sample);
560 if (PRINT_FIELD(SYM)) {
562 if (PRINT_FIELD(SYMOFFSET))
563 symbol__fprintf_symname_offs(al.sym, &al, stdout);
565 symbol__fprintf_symname(al.sym, stdout);
568 if (PRINT_FIELD(DSO)) {
570 map__fprintf_dsoname(al.map, stdout);
575 static void print_sample_callindent(struct perf_sample *sample,
576 struct perf_evsel *evsel,
577 struct thread *thread,
578 struct addr_location *al)
580 struct perf_event_attr *attr = &evsel->attr;
581 size_t depth = thread_stack__depth(thread);
582 struct addr_location addr_al;
583 const char *name = NULL;
589 * The 'return' has already been popped off the stack so the depth has
590 * to be adjusted to match the 'call'.
592 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
595 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
596 if (sample_addr_correlates_sym(attr)) {
597 thread__resolve(thread, &addr_al, sample);
599 name = addr_al.sym->name;
605 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
607 name = al->sym->name;
613 len = printf("%*s%s", (int)depth * 4, "", name);
615 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
621 * Try to keep the output length from changing frequently so that the
622 * output lines up more nicely.
624 if (len > spacing || (len && len < spacing - 52))
625 spacing = round_up(len + 4, 32);
628 printf("%*s", spacing - len, "");
631 static void print_insn(struct perf_sample *sample,
632 struct perf_event_attr *attr)
634 if (PRINT_FIELD(INSNLEN))
635 printf(" ilen: %d", sample->insn_len);
636 if (PRINT_FIELD(INSN)) {
640 for (i = 0; i < sample->insn_len; i++)
641 printf(" %02x", (unsigned char)sample->insn[i]);
645 static void print_sample_bts(struct perf_sample *sample,
646 struct perf_evsel *evsel,
647 struct thread *thread,
648 struct addr_location *al)
650 struct perf_event_attr *attr = &evsel->attr;
651 bool print_srcline_last = false;
653 if (PRINT_FIELD(CALLINDENT))
654 print_sample_callindent(sample, evsel, thread, al);
656 /* print branch_from information */
657 if (PRINT_FIELD(IP)) {
658 unsigned int print_opts = output[attr->type].print_ip_opts;
659 struct callchain_cursor *cursor = NULL;
661 if (symbol_conf.use_callchain && sample->callchain &&
662 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
663 sample, NULL, NULL, scripting_max_stack) == 0)
664 cursor = &callchain_cursor;
666 if (cursor == NULL) {
668 if (print_opts & EVSEL__PRINT_SRCLINE) {
669 print_srcline_last = true;
670 print_opts &= ~EVSEL__PRINT_SRCLINE;
675 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
678 /* print branch_to information */
679 if (PRINT_FIELD(ADDR) ||
680 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
681 !output[attr->type].user_set)) {
683 print_sample_addr(sample, thread, attr);
686 if (print_srcline_last)
687 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
689 print_insn(sample, attr);
698 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
699 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
700 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
701 {PERF_IP_FLAG_BRANCH, "jmp"},
702 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
703 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
704 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
705 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
706 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
707 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
708 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
709 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
710 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
714 static void print_sample_flags(u32 flags)
716 const char *chars = PERF_IP_FLAG_CHARS;
717 const int n = strlen(PERF_IP_FLAG_CHARS);
718 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
719 const char *name = NULL;
723 for (i = 0; sample_flags[i].name ; i++) {
724 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
725 name = sample_flags[i].name;
730 for (i = 0; i < n; i++, flags >>= 1) {
732 str[pos++] = chars[i];
734 for (; i < 32; i++, flags >>= 1) {
741 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
743 printf(" %-11s ", str);
746 struct printer_data {
753 print_sample_bpf_output_printer(enum binary_printer_ops op,
757 unsigned char ch = (unsigned char)val;
758 struct printer_data *printer_data = extra;
761 case BINARY_PRINT_DATA_BEGIN:
764 case BINARY_PRINT_LINE_BEGIN:
765 printf("%17s", !printer_data->line_no ? "BPF output:" :
768 case BINARY_PRINT_ADDR:
769 printf(" %04x:", val);
771 case BINARY_PRINT_NUM_DATA:
772 printf(" %02x", val);
774 case BINARY_PRINT_NUM_PAD:
777 case BINARY_PRINT_SEP:
780 case BINARY_PRINT_CHAR_DATA:
781 if (printer_data->hit_nul && ch)
782 printer_data->is_printable = false;
787 if (!printer_data->is_printable)
791 printer_data->hit_nul = true;
793 printer_data->is_printable = false;
798 case BINARY_PRINT_CHAR_PAD:
801 case BINARY_PRINT_LINE_END:
803 printer_data->line_no++;
805 case BINARY_PRINT_DATA_END:
811 static void print_sample_bpf_output(struct perf_sample *sample)
813 unsigned int nr_bytes = sample->raw_size;
814 struct printer_data printer_data = {0, false, true};
816 print_binary(sample->raw_data, nr_bytes, 8,
817 print_sample_bpf_output_printer, &printer_data);
819 if (printer_data.is_printable && printer_data.hit_nul)
820 printf("%17s \"%s\"\n", "BPF string:",
821 (char *)(sample->raw_data));
825 struct perf_tool tool;
826 struct perf_session *session;
827 bool show_task_events;
828 bool show_mmap_events;
829 bool show_switch_events;
831 struct cpu_map *cpus;
832 struct thread_map *threads;
836 static int perf_evlist__max_name_len(struct perf_evlist *evlist)
838 struct perf_evsel *evsel;
841 evlist__for_each_entry(evlist, evsel) {
842 int len = strlen(perf_evsel__name(evsel));
850 static size_t data_src__printf(u64 data_src)
852 struct mem_info mi = { .data_src.val = data_src };
858 perf_script__meminfo_scnprintf(decode, 100, &mi);
860 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
864 return printf("%-*s", maxlen, out);
867 static void process_event(struct perf_script *script,
868 struct perf_sample *sample, struct perf_evsel *evsel,
869 struct addr_location *al)
871 struct thread *thread = al->thread;
872 struct perf_event_attr *attr = &evsel->attr;
874 if (output[attr->type].fields == 0)
877 print_sample_start(sample, thread, evsel);
879 if (PRINT_FIELD(PERIOD))
880 printf("%10" PRIu64 " ", sample->period);
882 if (PRINT_FIELD(EVNAME)) {
883 const char *evname = perf_evsel__name(evsel);
885 if (!script->name_width)
886 script->name_width = perf_evlist__max_name_len(script->session->evlist);
888 printf("%*s: ", script->name_width,
889 evname ? evname : "[unknown]");
893 print_sample_flags(sample->flags);
895 if (is_bts_event(attr)) {
896 print_sample_bts(sample, evsel, thread, al);
900 if (PRINT_FIELD(TRACE))
901 event_format__print(evsel->tp_format, sample->cpu,
902 sample->raw_data, sample->raw_size);
903 if (PRINT_FIELD(ADDR))
904 print_sample_addr(sample, thread, attr);
906 if (PRINT_FIELD(DATA_SRC))
907 data_src__printf(sample->data_src);
909 if (PRINT_FIELD(WEIGHT))
910 printf("%16" PRIu64, sample->weight);
912 if (PRINT_FIELD(IP)) {
913 struct callchain_cursor *cursor = NULL;
915 if (symbol_conf.use_callchain && sample->callchain &&
916 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
917 sample, NULL, NULL, scripting_max_stack) == 0)
918 cursor = &callchain_cursor;
920 putchar(cursor ? '\n' : ' ');
921 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
924 if (PRINT_FIELD(IREGS))
925 print_sample_iregs(sample, attr);
927 if (PRINT_FIELD(BRSTACK))
928 print_sample_brstack(sample);
929 else if (PRINT_FIELD(BRSTACKSYM))
930 print_sample_brstacksym(sample, thread);
932 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
933 print_sample_bpf_output(sample);
934 print_insn(sample, attr);
938 static struct scripting_ops *scripting_ops;
940 static void __process_stat(struct perf_evsel *counter, u64 tstamp)
942 int nthreads = thread_map__nr(counter->threads);
943 int ncpus = perf_evsel__nr_cpus(counter);
945 static int header_printed;
947 if (counter->system_wide)
950 if (!header_printed) {
951 printf("%3s %8s %15s %15s %15s %15s %s\n",
952 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
956 for (thread = 0; thread < nthreads; thread++) {
957 for (cpu = 0; cpu < ncpus; cpu++) {
958 struct perf_counts_values *counts;
960 counts = perf_counts(counter->counts, cpu, thread);
962 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
963 counter->cpus->map[cpu],
964 thread_map__pid(counter->threads, thread),
969 perf_evsel__name(counter));
974 static void process_stat(struct perf_evsel *counter, u64 tstamp)
976 if (scripting_ops && scripting_ops->process_stat)
977 scripting_ops->process_stat(&stat_config, counter, tstamp);
979 __process_stat(counter, tstamp);
982 static void process_stat_interval(u64 tstamp)
984 if (scripting_ops && scripting_ops->process_stat_interval)
985 scripting_ops->process_stat_interval(tstamp);
988 static void setup_scripting(void)
990 setup_perl_scripting();
991 setup_python_scripting();
994 static int flush_scripting(void)
996 return scripting_ops ? scripting_ops->flush_script() : 0;
999 static int cleanup_scripting(void)
1001 pr_debug("\nperf script stopped\n");
1003 return scripting_ops ? scripting_ops->stop_script() : 0;
1006 static int process_sample_event(struct perf_tool *tool,
1007 union perf_event *event,
1008 struct perf_sample *sample,
1009 struct perf_evsel *evsel,
1010 struct machine *machine)
1012 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1013 struct addr_location al;
1016 if (sample->time < last_timestamp) {
1017 pr_err("Samples misordered, previous: %" PRIu64
1018 " this: %" PRIu64 "\n", last_timestamp,
1022 last_timestamp = sample->time;
1026 if (machine__resolve(machine, &al, sample) < 0) {
1027 pr_err("problem processing %d event, skipping it.\n",
1028 event->header.type);
1035 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
1039 scripting_ops->process_event(event, sample, evsel, &al);
1041 process_event(scr, sample, evsel, &al);
1044 addr_location__put(&al);
1048 static int process_attr(struct perf_tool *tool, union perf_event *event,
1049 struct perf_evlist **pevlist)
1051 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1052 struct perf_evlist *evlist;
1053 struct perf_evsel *evsel, *pos;
1056 err = perf_event__process_attr(tool, event, pevlist);
1061 evsel = perf_evlist__last(*pevlist);
1063 if (evsel->attr.type >= PERF_TYPE_MAX)
1066 evlist__for_each_entry(evlist, pos) {
1067 if (pos->attr.type == evsel->attr.type && pos != evsel)
1071 set_print_ip_opts(&evsel->attr);
1073 if (evsel->attr.sample_type)
1074 err = perf_evsel__check_attr(evsel, scr->session);
1079 static int process_comm_event(struct perf_tool *tool,
1080 union perf_event *event,
1081 struct perf_sample *sample,
1082 struct machine *machine)
1084 struct thread *thread;
1085 struct perf_script *script = container_of(tool, struct perf_script, tool);
1086 struct perf_session *session = script->session;
1087 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1090 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1091 if (thread == NULL) {
1092 pr_debug("problem processing COMM event, skipping it.\n");
1096 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1099 if (!evsel->attr.sample_id_all) {
1102 sample->tid = event->comm.tid;
1103 sample->pid = event->comm.pid;
1105 print_sample_start(sample, thread, evsel);
1106 perf_event__fprintf(event, stdout);
1109 thread__put(thread);
1113 static int process_fork_event(struct perf_tool *tool,
1114 union perf_event *event,
1115 struct perf_sample *sample,
1116 struct machine *machine)
1118 struct thread *thread;
1119 struct perf_script *script = container_of(tool, struct perf_script, tool);
1120 struct perf_session *session = script->session;
1121 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1123 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1126 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1127 if (thread == NULL) {
1128 pr_debug("problem processing FORK event, skipping it.\n");
1132 if (!evsel->attr.sample_id_all) {
1134 sample->time = event->fork.time;
1135 sample->tid = event->fork.tid;
1136 sample->pid = event->fork.pid;
1138 print_sample_start(sample, thread, evsel);
1139 perf_event__fprintf(event, stdout);
1140 thread__put(thread);
1144 static int process_exit_event(struct perf_tool *tool,
1145 union perf_event *event,
1146 struct perf_sample *sample,
1147 struct machine *machine)
1150 struct thread *thread;
1151 struct perf_script *script = container_of(tool, struct perf_script, tool);
1152 struct perf_session *session = script->session;
1153 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1155 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1156 if (thread == NULL) {
1157 pr_debug("problem processing EXIT event, skipping it.\n");
1161 if (!evsel->attr.sample_id_all) {
1164 sample->tid = event->fork.tid;
1165 sample->pid = event->fork.pid;
1167 print_sample_start(sample, thread, evsel);
1168 perf_event__fprintf(event, stdout);
1170 if (perf_event__process_exit(tool, event, sample, machine) < 0)
1173 thread__put(thread);
1177 static int process_mmap_event(struct perf_tool *tool,
1178 union perf_event *event,
1179 struct perf_sample *sample,
1180 struct machine *machine)
1182 struct thread *thread;
1183 struct perf_script *script = container_of(tool, struct perf_script, tool);
1184 struct perf_session *session = script->session;
1185 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1187 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1190 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1191 if (thread == NULL) {
1192 pr_debug("problem processing MMAP event, skipping it.\n");
1196 if (!evsel->attr.sample_id_all) {
1199 sample->tid = event->mmap.tid;
1200 sample->pid = event->mmap.pid;
1202 print_sample_start(sample, thread, evsel);
1203 perf_event__fprintf(event, stdout);
1204 thread__put(thread);
1208 static int process_mmap2_event(struct perf_tool *tool,
1209 union perf_event *event,
1210 struct perf_sample *sample,
1211 struct machine *machine)
1213 struct thread *thread;
1214 struct perf_script *script = container_of(tool, struct perf_script, tool);
1215 struct perf_session *session = script->session;
1216 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1218 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1221 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1222 if (thread == NULL) {
1223 pr_debug("problem processing MMAP2 event, skipping it.\n");
1227 if (!evsel->attr.sample_id_all) {
1230 sample->tid = event->mmap2.tid;
1231 sample->pid = event->mmap2.pid;
1233 print_sample_start(sample, thread, evsel);
1234 perf_event__fprintf(event, stdout);
1235 thread__put(thread);
1239 static int process_switch_event(struct perf_tool *tool,
1240 union perf_event *event,
1241 struct perf_sample *sample,
1242 struct machine *machine)
1244 struct thread *thread;
1245 struct perf_script *script = container_of(tool, struct perf_script, tool);
1246 struct perf_session *session = script->session;
1247 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1249 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1252 thread = machine__findnew_thread(machine, sample->pid,
1254 if (thread == NULL) {
1255 pr_debug("problem processing SWITCH event, skipping it.\n");
1259 print_sample_start(sample, thread, evsel);
1260 perf_event__fprintf(event, stdout);
1261 thread__put(thread);
1265 static void sig_handler(int sig __maybe_unused)
1270 static int __cmd_script(struct perf_script *script)
1274 signal(SIGINT, sig_handler);
1276 /* override event processing functions */
1277 if (script->show_task_events) {
1278 script->tool.comm = process_comm_event;
1279 script->tool.fork = process_fork_event;
1280 script->tool.exit = process_exit_event;
1282 if (script->show_mmap_events) {
1283 script->tool.mmap = process_mmap_event;
1284 script->tool.mmap2 = process_mmap2_event;
1286 if (script->show_switch_events)
1287 script->tool.context_switch = process_switch_event;
1289 ret = perf_session__process_events(script->session);
1292 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
1297 struct script_spec {
1298 struct list_head node;
1299 struct scripting_ops *ops;
1303 static LIST_HEAD(script_specs);
1305 static struct script_spec *script_spec__new(const char *spec,
1306 struct scripting_ops *ops)
1308 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1311 strcpy(s->spec, spec);
1318 static void script_spec__add(struct script_spec *s)
1320 list_add_tail(&s->node, &script_specs);
1323 static struct script_spec *script_spec__find(const char *spec)
1325 struct script_spec *s;
1327 list_for_each_entry(s, &script_specs, node)
1328 if (strcasecmp(s->spec, spec) == 0)
1333 int script_spec_register(const char *spec, struct scripting_ops *ops)
1335 struct script_spec *s;
1337 s = script_spec__find(spec);
1341 s = script_spec__new(spec, ops);
1345 script_spec__add(s);
1350 static struct scripting_ops *script_spec__lookup(const char *spec)
1352 struct script_spec *s = script_spec__find(spec);
1359 static void list_available_languages(void)
1361 struct script_spec *s;
1363 fprintf(stderr, "\n");
1364 fprintf(stderr, "Scripting language extensions (used in "
1365 "perf script -s [spec:]script.[spec]):\n\n");
1367 list_for_each_entry(s, &script_specs, node)
1368 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1370 fprintf(stderr, "\n");
1373 static int parse_scriptname(const struct option *opt __maybe_unused,
1374 const char *str, int unset __maybe_unused)
1376 char spec[PATH_MAX];
1377 const char *script, *ext;
1380 if (strcmp(str, "lang") == 0) {
1381 list_available_languages();
1385 script = strchr(str, ':');
1388 if (len >= PATH_MAX) {
1389 fprintf(stderr, "invalid language specifier");
1392 strncpy(spec, str, len);
1394 scripting_ops = script_spec__lookup(spec);
1395 if (!scripting_ops) {
1396 fprintf(stderr, "invalid language specifier");
1402 ext = strrchr(script, '.');
1404 fprintf(stderr, "invalid script extension");
1407 scripting_ops = script_spec__lookup(++ext);
1408 if (!scripting_ops) {
1409 fprintf(stderr, "invalid script extension");
1414 script_name = strdup(script);
1419 static int parse_output_fields(const struct option *opt __maybe_unused,
1420 const char *arg, int unset __maybe_unused)
1423 int i, imax = ARRAY_SIZE(all_output_options);
1426 char *str = strdup(arg);
1432 /* first word can state for which event type the user is specifying
1433 * the fields. If no type exists, the specified fields apply to all
1434 * event types found in the file minus the invalid fields for a type.
1436 tok = strchr(str, ':');
1440 if (!strcmp(str, "hw"))
1441 type = PERF_TYPE_HARDWARE;
1442 else if (!strcmp(str, "sw"))
1443 type = PERF_TYPE_SOFTWARE;
1444 else if (!strcmp(str, "trace"))
1445 type = PERF_TYPE_TRACEPOINT;
1446 else if (!strcmp(str, "raw"))
1447 type = PERF_TYPE_RAW;
1448 else if (!strcmp(str, "break"))
1449 type = PERF_TYPE_BREAKPOINT;
1451 fprintf(stderr, "Invalid event type in field string.\n");
1456 if (output[type].user_set)
1457 pr_warning("Overriding previous field request for %s events.\n",
1460 output[type].fields = 0;
1461 output[type].user_set = true;
1462 output[type].wildcard_set = false;
1466 if (strlen(str) == 0) {
1468 "Cannot set fields to 'none' for all event types.\n");
1473 if (output_set_by_user())
1474 pr_warning("Overriding previous field request for all events.\n");
1476 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1477 output[j].fields = 0;
1478 output[j].user_set = true;
1479 output[j].wildcard_set = true;
1483 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
1484 for (i = 0; i < imax; ++i) {
1485 if (strcmp(tok, all_output_options[i].str) == 0)
1488 if (i == imax && strcmp(tok, "flags") == 0) {
1493 fprintf(stderr, "Invalid field requested.\n");
1499 /* add user option to all events types for
1502 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1503 if (output[j].invalid_fields & all_output_options[i].field) {
1504 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1505 all_output_options[i].str, event_type(j));
1507 output[j].fields |= all_output_options[i].field;
1510 if (output[type].invalid_fields & all_output_options[i].field) {
1511 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1512 all_output_options[i].str, event_type(type));
1517 output[type].fields |= all_output_options[i].field;
1522 if (output[type].fields == 0) {
1523 pr_debug("No fields requested for %s type. "
1524 "Events will not be displayed.\n", event_type(type));
1533 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1534 static int is_directory(const char *base_path, const struct dirent *dent)
1536 char path[PATH_MAX];
1539 sprintf(path, "%s/%s", base_path, dent->d_name);
1540 if (stat(path, &st))
1543 return S_ISDIR(st.st_mode);
1546 #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1547 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1548 if ((lang_dirent->d_type == DT_DIR || \
1549 (lang_dirent->d_type == DT_UNKNOWN && \
1550 is_directory(scripts_path, lang_dirent))) && \
1551 (strcmp(lang_dirent->d_name, ".")) && \
1552 (strcmp(lang_dirent->d_name, "..")))
1554 #define for_each_script(lang_path, lang_dir, script_dirent) \
1555 while ((script_dirent = readdir(lang_dir)) != NULL) \
1556 if (script_dirent->d_type != DT_DIR && \
1557 (script_dirent->d_type != DT_UNKNOWN || \
1558 !is_directory(lang_path, script_dirent)))
1561 #define RECORD_SUFFIX "-record"
1562 #define REPORT_SUFFIX "-report"
1564 struct script_desc {
1565 struct list_head node;
1571 static LIST_HEAD(script_descs);
1573 static struct script_desc *script_desc__new(const char *name)
1575 struct script_desc *s = zalloc(sizeof(*s));
1577 if (s != NULL && name)
1578 s->name = strdup(name);
1583 static void script_desc__delete(struct script_desc *s)
1586 zfree(&s->half_liner);
1591 static void script_desc__add(struct script_desc *s)
1593 list_add_tail(&s->node, &script_descs);
1596 static struct script_desc *script_desc__find(const char *name)
1598 struct script_desc *s;
1600 list_for_each_entry(s, &script_descs, node)
1601 if (strcasecmp(s->name, name) == 0)
1606 static struct script_desc *script_desc__findnew(const char *name)
1608 struct script_desc *s = script_desc__find(name);
1613 s = script_desc__new(name);
1615 goto out_delete_desc;
1617 script_desc__add(s);
1622 script_desc__delete(s);
1627 static const char *ends_with(const char *str, const char *suffix)
1629 size_t suffix_len = strlen(suffix);
1630 const char *p = str;
1632 if (strlen(str) > suffix_len) {
1633 p = str + strlen(str) - suffix_len;
1634 if (!strncmp(p, suffix, suffix_len))
1641 static int read_script_info(struct script_desc *desc, const char *filename)
1643 char line[BUFSIZ], *p;
1646 fp = fopen(filename, "r");
1650 while (fgets(line, sizeof(line), fp)) {
1657 if (strlen(p) && *p == '!')
1661 if (strlen(p) && p[strlen(p) - 1] == '\n')
1662 p[strlen(p) - 1] = '\0';
1664 if (!strncmp(p, "description:", strlen("description:"))) {
1665 p += strlen("description:");
1666 desc->half_liner = strdup(ltrim(p));
1670 if (!strncmp(p, "args:", strlen("args:"))) {
1671 p += strlen("args:");
1672 desc->args = strdup(ltrim(p));
1682 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1684 char *script_root, *str;
1686 script_root = strdup(script_dirent->d_name);
1690 str = (char *)ends_with(script_root, suffix);
1700 static int list_available_scripts(const struct option *opt __maybe_unused,
1701 const char *s __maybe_unused,
1702 int unset __maybe_unused)
1704 struct dirent *script_dirent, *lang_dirent;
1705 char scripts_path[MAXPATHLEN];
1706 DIR *scripts_dir, *lang_dir;
1707 char script_path[MAXPATHLEN];
1708 char lang_path[MAXPATHLEN];
1709 struct script_desc *desc;
1710 char first_half[BUFSIZ];
1713 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1715 scripts_dir = opendir(scripts_path);
1718 "open(%s) failed.\n"
1719 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
1724 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
1725 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1726 lang_dirent->d_name);
1727 lang_dir = opendir(lang_path);
1731 for_each_script(lang_path, lang_dir, script_dirent) {
1732 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
1734 desc = script_desc__findnew(script_root);
1735 snprintf(script_path, MAXPATHLEN, "%s/%s",
1736 lang_path, script_dirent->d_name);
1737 read_script_info(desc, script_path);
1743 fprintf(stdout, "List of available trace scripts:\n");
1744 list_for_each_entry(desc, &script_descs, node) {
1745 sprintf(first_half, "%s %s", desc->name,
1746 desc->args ? desc->args : "");
1747 fprintf(stdout, " %-36s %s\n", first_half,
1748 desc->half_liner ? desc->half_liner : "");
1755 * Some scripts specify the required events in their "xxx-record" file,
1756 * this function will check if the events in perf.data match those
1757 * mentioned in the "xxx-record".
1759 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1760 * which is covered well now. And new parsing code should be added to
1761 * cover the future complexing formats like event groups etc.
1763 static int check_ev_match(char *dir_name, char *scriptname,
1764 struct perf_session *session)
1766 char filename[MAXPATHLEN], evname[128];
1767 char line[BUFSIZ], *p;
1768 struct perf_evsel *pos;
1772 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1774 fp = fopen(filename, "r");
1778 while (fgets(line, sizeof(line), fp)) {
1784 p = strstr(p, "-e");
1790 len = strcspn(p, " \t");
1794 snprintf(evname, len + 1, "%s", p);
1797 evlist__for_each_entry(session->evlist, pos) {
1798 if (!strcmp(perf_evsel__name(pos), evname)) {
1816 * Return -1 if none is found, otherwise the actual scripts number.
1818 * Currently the only user of this function is the script browser, which
1819 * will list all statically runnable scripts, select one, execute it and
1820 * show the output in a perf browser.
1822 int find_scripts(char **scripts_array, char **scripts_path_array)
1824 struct dirent *script_dirent, *lang_dirent;
1825 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
1826 DIR *scripts_dir, *lang_dir;
1827 struct perf_session *session;
1828 struct perf_data_file file = {
1830 .mode = PERF_DATA_MODE_READ,
1835 session = perf_session__new(&file, false, NULL);
1839 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1841 scripts_dir = opendir(scripts_path);
1843 perf_session__delete(session);
1847 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
1848 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1849 lang_dirent->d_name);
1851 if (strstr(lang_path, "perl"))
1855 if (strstr(lang_path, "python"))
1859 lang_dir = opendir(lang_path);
1863 for_each_script(lang_path, lang_dir, script_dirent) {
1864 /* Skip those real time scripts: xxxtop.p[yl] */
1865 if (strstr(script_dirent->d_name, "top."))
1867 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1868 script_dirent->d_name);
1869 temp = strchr(script_dirent->d_name, '.');
1870 snprintf(scripts_array[i],
1871 (temp - script_dirent->d_name) + 1,
1872 "%s", script_dirent->d_name);
1874 if (check_ev_match(lang_path,
1875 scripts_array[i], session))
1883 closedir(scripts_dir);
1884 perf_session__delete(session);
1888 static char *get_script_path(const char *script_root, const char *suffix)
1890 struct dirent *script_dirent, *lang_dirent;
1891 char scripts_path[MAXPATHLEN];
1892 char script_path[MAXPATHLEN];
1893 DIR *scripts_dir, *lang_dir;
1894 char lang_path[MAXPATHLEN];
1895 char *__script_root;
1897 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1899 scripts_dir = opendir(scripts_path);
1903 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
1904 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1905 lang_dirent->d_name);
1906 lang_dir = opendir(lang_path);
1910 for_each_script(lang_path, lang_dir, script_dirent) {
1911 __script_root = get_script_root(script_dirent, suffix);
1912 if (__script_root && !strcmp(script_root, __script_root)) {
1913 free(__script_root);
1915 closedir(scripts_dir);
1916 snprintf(script_path, MAXPATHLEN, "%s/%s",
1917 lang_path, script_dirent->d_name);
1918 return strdup(script_path);
1920 free(__script_root);
1924 closedir(scripts_dir);
1929 static bool is_top_script(const char *script_path)
1931 return ends_with(script_path, "top") == NULL ? false : true;
1934 static int has_required_arg(char *script_path)
1936 struct script_desc *desc;
1940 desc = script_desc__new(NULL);
1942 if (read_script_info(desc, script_path))
1948 for (p = desc->args; *p; p++)
1952 script_desc__delete(desc);
1957 static int have_cmd(int argc, const char **argv)
1959 char **__argv = malloc(sizeof(const char *) * argc);
1962 pr_err("malloc failed\n");
1966 memcpy(__argv, argv, sizeof(const char *) * argc);
1967 argc = parse_options(argc, (const char **)__argv, record_options,
1968 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1971 system_wide = (argc == 0);
1976 static void script__setup_sample_type(struct perf_script *script)
1978 struct perf_session *session = script->session;
1979 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1981 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1982 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1983 (sample_type & PERF_SAMPLE_STACK_USER))
1984 callchain_param.record_mode = CALLCHAIN_DWARF;
1985 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1986 callchain_param.record_mode = CALLCHAIN_LBR;
1988 callchain_param.record_mode = CALLCHAIN_FP;
1992 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1993 union perf_event *event,
1994 struct perf_session *session)
1996 struct stat_round_event *round = &event->stat_round;
1997 struct perf_evsel *counter;
1999 evlist__for_each_entry(session->evlist, counter) {
2000 perf_stat_process_counter(&stat_config, counter);
2001 process_stat(counter, round->time);
2004 process_stat_interval(round->time);
2008 static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2009 union perf_event *event,
2010 struct perf_session *session __maybe_unused)
2012 perf_event__read_stat_config(&stat_config, &event->stat_config);
2016 static int set_maps(struct perf_script *script)
2018 struct perf_evlist *evlist = script->session->evlist;
2020 if (!script->cpus || !script->threads)
2023 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2026 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2028 if (perf_evlist__alloc_stats(evlist, true))
2031 script->allocated = true;
2036 int process_thread_map_event(struct perf_tool *tool,
2037 union perf_event *event,
2038 struct perf_session *session __maybe_unused)
2040 struct perf_script *script = container_of(tool, struct perf_script, tool);
2042 if (script->threads) {
2043 pr_warning("Extra thread map event, ignoring.\n");
2047 script->threads = thread_map__new_event(&event->thread_map);
2048 if (!script->threads)
2051 return set_maps(script);
2055 int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2056 union perf_event *event,
2057 struct perf_session *session __maybe_unused)
2059 struct perf_script *script = container_of(tool, struct perf_script, tool);
2062 pr_warning("Extra cpu map event, ignoring.\n");
2066 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2070 return set_maps(script);
2073 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
2075 bool show_full_info = false;
2076 bool header = false;
2077 bool header_only = false;
2078 bool script_started = false;
2079 char *rec_script_path = NULL;
2080 char *rep_script_path = NULL;
2081 struct perf_session *session;
2082 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
2083 char *script_path = NULL;
2084 const char **__argv;
2086 struct perf_script script = {
2088 .sample = process_sample_event,
2089 .mmap = perf_event__process_mmap,
2090 .mmap2 = perf_event__process_mmap2,
2091 .comm = perf_event__process_comm,
2092 .exit = perf_event__process_exit,
2093 .fork = perf_event__process_fork,
2094 .attr = process_attr,
2095 .event_update = perf_event__process_event_update,
2096 .tracing_data = perf_event__process_tracing_data,
2097 .build_id = perf_event__process_build_id,
2098 .id_index = perf_event__process_id_index,
2099 .auxtrace_info = perf_event__process_auxtrace_info,
2100 .auxtrace = perf_event__process_auxtrace,
2101 .auxtrace_error = perf_event__process_auxtrace_error,
2102 .stat = perf_event__process_stat_event,
2103 .stat_round = process_stat_round_event,
2104 .stat_config = process_stat_config_event,
2105 .thread_map = process_thread_map_event,
2106 .cpu_map = process_cpu_map_event,
2107 .ordered_events = true,
2108 .ordering_requires_timestamps = true,
2111 struct perf_data_file file = {
2112 .mode = PERF_DATA_MODE_READ,
2114 const struct option options[] = {
2115 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2116 "dump raw trace in ASCII"),
2117 OPT_INCR('v', "verbose", &verbose,
2118 "be more verbose (show symbol address, etc)"),
2119 OPT_BOOLEAN('L', "Latency", &latency_format,
2120 "show latency attributes (irqs/preemption disabled, etc)"),
2121 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2122 list_available_scripts),
2123 OPT_CALLBACK('s', "script", NULL, "name",
2124 "script file name (lang:script name, script name, or *)",
2126 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2127 "generate perf-script.xx script in specified language"),
2128 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2129 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2130 "do various checks like samples ordering and lost events"),
2131 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2132 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
2133 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2134 "file", "vmlinux pathname"),
2135 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2136 "file", "kallsyms pathname"),
2137 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2138 "When printing symbols do not display call chain"),
2139 OPT_CALLBACK(0, "symfs", NULL, "directory",
2140 "Look for files with symbols relative to this directory",
2141 symbol__config_symfs),
2142 OPT_CALLBACK('F', "fields", NULL, "str",
2143 "comma separated output fields prepend with 'type:'. "
2144 "Valid types: hw,sw,trace,raw. "
2145 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2146 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
2147 "bpf-output,callindent,insn,insnlen", parse_output_fields),
2148 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2149 "system-wide collection from all CPUs"),
2150 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2151 "only consider these symbols"),
2152 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2153 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2154 "only display events for these comms"),
2155 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2156 "only consider symbols in these pids"),
2157 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2158 "only consider symbols in these tids"),
2159 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2160 "Set the maximum stack depth when parsing the callchain, "
2161 "anything beyond the specified depth will be ignored. "
2162 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
2163 OPT_BOOLEAN('I', "show-info", &show_full_info,
2164 "display extended information from perf.data file"),
2165 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2166 "Show the path of [kernel.kallsyms]"),
2167 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2168 "Show the fork/comm/exit events"),
2169 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2170 "Show the mmap events"),
2171 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2172 "Show context switch events (if recorded)"),
2173 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
2174 OPT_BOOLEAN(0, "ns", &nanosecs,
2175 "Use 9 decimal places when displaying time"),
2176 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2177 "Instruction Tracing options",
2178 itrace_parse_synth_opts),
2179 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2180 "Show full source file name path for source lines"),
2181 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2182 "Enable symbol demangling"),
2183 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2184 "Enable kernel symbol demangling"),
2188 const char * const script_subcommands[] = { "record", "report", NULL };
2189 const char *script_usage[] = {
2190 "perf script [<options>]",
2191 "perf script [<options>] record <script> [<record-options>] <command>",
2192 "perf script [<options>] report <script> [script-args]",
2193 "perf script [<options>] <script> [<record-options>] <command>",
2194 "perf script [<options>] <top-script> [script-args]",
2200 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
2201 PARSE_OPT_STOP_AT_NON_OPTION);
2203 file.path = input_name;
2205 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2206 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2207 if (!rec_script_path)
2208 return cmd_record(argc, argv, NULL);
2211 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2212 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2213 if (!rep_script_path) {
2215 "Please specify a valid report script"
2216 "(see 'perf script -l' for listing)\n");
2221 if (itrace_synth_opts.callchain &&
2222 itrace_synth_opts.callchain_sz > scripting_max_stack)
2223 scripting_max_stack = itrace_synth_opts.callchain_sz;
2225 /* make sure PERF_EXEC_PATH is set for scripts */
2226 set_argv_exec_path(get_argv_exec_path());
2228 if (argc && !script_name && !rec_script_path && !rep_script_path) {
2233 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2234 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2236 if (!rec_script_path && !rep_script_path) {
2237 usage_with_options_msg(script_usage, options,
2238 "Couldn't find script `%s'\n\n See perf"
2239 " script -l for available scripts.\n", argv[0]);
2242 if (is_top_script(argv[0])) {
2243 rep_args = argc - 1;
2247 rep_args = has_required_arg(rep_script_path);
2248 rec_args = (argc - 1) - rep_args;
2250 usage_with_options_msg(script_usage, options,
2251 "`%s' script requires options."
2252 "\n\n See perf script -l for available "
2253 "scripts and options.\n", argv[0]);
2257 if (pipe(live_pipe) < 0) {
2258 perror("failed to create pipe");
2264 perror("failed to fork");
2271 dup2(live_pipe[1], 1);
2272 close(live_pipe[0]);
2274 if (is_top_script(argv[0])) {
2276 } else if (!system_wide) {
2277 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2283 __argv = malloc((argc + 6) * sizeof(const char *));
2285 pr_err("malloc failed\n");
2290 __argv[j++] = "/bin/sh";
2291 __argv[j++] = rec_script_path;
2297 for (i = rep_args + 1; i < argc; i++)
2298 __argv[j++] = argv[i];
2301 execvp("/bin/sh", (char **)__argv);
2306 dup2(live_pipe[0], 0);
2307 close(live_pipe[1]);
2309 __argv = malloc((argc + 4) * sizeof(const char *));
2311 pr_err("malloc failed\n");
2317 __argv[j++] = "/bin/sh";
2318 __argv[j++] = rep_script_path;
2319 for (i = 1; i < rep_args + 1; i++)
2320 __argv[j++] = argv[i];
2325 execvp("/bin/sh", (char **)__argv);
2330 if (rec_script_path)
2331 script_path = rec_script_path;
2332 if (rep_script_path)
2333 script_path = rep_script_path;
2338 if (!rec_script_path)
2339 system_wide = false;
2340 else if (!system_wide) {
2341 if (have_cmd(argc - 1, &argv[1]) != 0) {
2347 __argv = malloc((argc + 2) * sizeof(const char *));
2349 pr_err("malloc failed\n");
2354 __argv[j++] = "/bin/sh";
2355 __argv[j++] = script_path;
2358 for (i = 2; i < argc; i++)
2359 __argv[j++] = argv[i];
2362 execvp("/bin/sh", (char **)__argv);
2370 session = perf_session__new(&file, false, &script.tool);
2371 if (session == NULL)
2374 if (header || header_only) {
2375 perf_session__fprintf_info(session, stdout, show_full_info);
2380 if (symbol__init(&session->header.env) < 0)
2383 script.session = session;
2384 script__setup_sample_type(&script);
2386 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2387 itrace_synth_opts.thread_stack = true;
2389 session->itrace_synth_opts = &itrace_synth_opts;
2392 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2398 symbol_conf.use_callchain = true;
2400 symbol_conf.use_callchain = false;
2402 if (session->tevent.pevent &&
2403 pevent_set_function_resolver(session->tevent.pevent,
2404 machine__resolve_kernel_addr,
2405 &session->machines.host) < 0) {
2406 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2410 if (generate_script_lang) {
2411 struct stat perf_stat;
2414 if (output_set_by_user()) {
2416 "custom fields not supported for generated scripts");
2421 input = open(file.path, O_RDONLY); /* input_name */
2424 perror("failed to open file");
2428 err = fstat(input, &perf_stat);
2430 perror("failed to stat file");
2434 if (!perf_stat.st_size) {
2435 fprintf(stderr, "zero-sized file, nothing to do!\n");
2439 scripting_ops = script_spec__lookup(generate_script_lang);
2440 if (!scripting_ops) {
2441 fprintf(stderr, "invalid language specifier");
2446 err = scripting_ops->generate_script(session->tevent.pevent,
2452 err = scripting_ops->start_script(script_name, argc, argv);
2455 pr_debug("perf script started with script %s\n\n", script_name);
2456 script_started = true;
2460 err = perf_session__check_output_opt(session);
2464 err = __cmd_script(&script);
2469 perf_evlist__free_stats(session->evlist);
2470 perf_session__delete(session);
2473 cleanup_scripting();