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 <linux/bitmap.h>
26 #include "util/mem-events.h"
28 static char const *script_name;
29 static char const *generate_script_lang;
30 static bool debug_mode;
31 static u64 last_timestamp;
32 static u64 nr_unordered;
33 static bool no_callchain;
34 static bool latency_format;
35 static bool system_wide;
36 static bool print_flags;
38 static const char *cpu_list;
39 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
40 static struct perf_stat_config stat_config;
42 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
44 enum perf_output_field {
45 PERF_OUTPUT_COMM = 1U << 0,
46 PERF_OUTPUT_TID = 1U << 1,
47 PERF_OUTPUT_PID = 1U << 2,
48 PERF_OUTPUT_TIME = 1U << 3,
49 PERF_OUTPUT_CPU = 1U << 4,
50 PERF_OUTPUT_EVNAME = 1U << 5,
51 PERF_OUTPUT_TRACE = 1U << 6,
52 PERF_OUTPUT_IP = 1U << 7,
53 PERF_OUTPUT_SYM = 1U << 8,
54 PERF_OUTPUT_DSO = 1U << 9,
55 PERF_OUTPUT_ADDR = 1U << 10,
56 PERF_OUTPUT_SYMOFFSET = 1U << 11,
57 PERF_OUTPUT_SRCLINE = 1U << 12,
58 PERF_OUTPUT_PERIOD = 1U << 13,
59 PERF_OUTPUT_IREGS = 1U << 14,
60 PERF_OUTPUT_BRSTACK = 1U << 15,
61 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
62 PERF_OUTPUT_DATA_SRC = 1U << 17,
63 PERF_OUTPUT_WEIGHT = 1U << 18,
66 struct output_option {
68 enum perf_output_field field;
69 } all_output_options[] = {
70 {.str = "comm", .field = PERF_OUTPUT_COMM},
71 {.str = "tid", .field = PERF_OUTPUT_TID},
72 {.str = "pid", .field = PERF_OUTPUT_PID},
73 {.str = "time", .field = PERF_OUTPUT_TIME},
74 {.str = "cpu", .field = PERF_OUTPUT_CPU},
75 {.str = "event", .field = PERF_OUTPUT_EVNAME},
76 {.str = "trace", .field = PERF_OUTPUT_TRACE},
77 {.str = "ip", .field = PERF_OUTPUT_IP},
78 {.str = "sym", .field = PERF_OUTPUT_SYM},
79 {.str = "dso", .field = PERF_OUTPUT_DSO},
80 {.str = "addr", .field = PERF_OUTPUT_ADDR},
81 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
82 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
83 {.str = "period", .field = PERF_OUTPUT_PERIOD},
84 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
85 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
86 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
87 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
88 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
91 /* default set to maintain compatibility with current format */
95 unsigned int print_ip_opts;
98 } output[PERF_TYPE_MAX] = {
100 [PERF_TYPE_HARDWARE] = {
103 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
104 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
105 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
106 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
109 .invalid_fields = PERF_OUTPUT_TRACE,
112 [PERF_TYPE_SOFTWARE] = {
115 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
116 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
117 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
118 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
121 .invalid_fields = PERF_OUTPUT_TRACE,
124 [PERF_TYPE_TRACEPOINT] = {
127 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
128 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
129 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
135 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
136 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
137 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
138 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
139 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
140 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
142 .invalid_fields = PERF_OUTPUT_TRACE,
145 [PERF_TYPE_BREAKPOINT] = {
148 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
149 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
150 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
151 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
154 .invalid_fields = PERF_OUTPUT_TRACE,
158 static bool output_set_by_user(void)
161 for (j = 0; j < PERF_TYPE_MAX; ++j) {
162 if (output[j].user_set)
168 static const char *output_field2str(enum perf_output_field field)
170 int i, imax = ARRAY_SIZE(all_output_options);
171 const char *str = "";
173 for (i = 0; i < imax; ++i) {
174 if (all_output_options[i].field == field) {
175 str = all_output_options[i].str;
182 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
184 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
185 u64 sample_type, const char *sample_msg,
186 enum perf_output_field field,
189 struct perf_event_attr *attr = &evsel->attr;
190 int type = attr->type;
193 if (attr->sample_type & sample_type)
196 if (output[type].user_set) {
199 evname = perf_evsel__name(evsel);
200 pr_err("Samples for '%s' event do not have %s attribute set. "
201 "Cannot print '%s' field.\n",
202 evname, sample_msg, output_field2str(field));
206 /* user did not ask for it explicitly so remove from the default list */
207 output[type].fields &= ~field;
208 evname = perf_evsel__name(evsel);
209 pr_debug("Samples for '%s' event do not have %s attribute set. "
210 "Skipping '%s' field.\n",
211 evname, sample_msg, output_field2str(field));
216 static int perf_evsel__check_stype(struct perf_evsel *evsel,
217 u64 sample_type, const char *sample_msg,
218 enum perf_output_field field)
220 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
224 static int perf_evsel__check_attr(struct perf_evsel *evsel,
225 struct perf_session *session)
227 struct perf_event_attr *attr = &evsel->attr;
230 if (perf_header__has_feat(&session->header, HEADER_STAT))
233 allow_user_set = perf_header__has_feat(&session->header,
236 if (PRINT_FIELD(TRACE) &&
237 !perf_session__has_traces(session, "record -R"))
240 if (PRINT_FIELD(IP)) {
241 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
246 if (PRINT_FIELD(ADDR) &&
247 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
248 PERF_OUTPUT_ADDR, allow_user_set))
251 if (PRINT_FIELD(DATA_SRC) &&
252 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
253 PERF_OUTPUT_DATA_SRC))
256 if (PRINT_FIELD(WEIGHT) &&
257 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
261 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
262 pr_err("Display of symbols requested but neither sample IP nor "
263 "sample address\nis selected. Hence, no addresses to convert "
267 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
268 pr_err("Display of offsets requested but symbol is not"
272 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
273 pr_err("Display of DSO requested but neither sample IP nor "
274 "sample address\nis selected. Hence, no addresses to convert "
278 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
279 pr_err("Display of source line number requested but sample IP is not\n"
280 "selected. Hence, no address to lookup the source line number.\n");
284 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
285 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
286 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
289 if (PRINT_FIELD(TIME) &&
290 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
294 if (PRINT_FIELD(CPU) &&
295 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
296 PERF_OUTPUT_CPU, allow_user_set))
299 if (PRINT_FIELD(PERIOD) &&
300 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
304 if (PRINT_FIELD(IREGS) &&
305 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
312 static void set_print_ip_opts(struct perf_event_attr *attr)
314 unsigned int type = attr->type;
316 output[type].print_ip_opts = 0;
318 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
320 if (PRINT_FIELD(SYM))
321 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
323 if (PRINT_FIELD(DSO))
324 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
326 if (PRINT_FIELD(SYMOFFSET))
327 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
329 if (PRINT_FIELD(SRCLINE))
330 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
334 * verify all user requested events exist and the samples
335 * have the expected data
337 static int perf_session__check_output_opt(struct perf_session *session)
340 struct perf_evsel *evsel;
342 for (j = 0; j < PERF_TYPE_MAX; ++j) {
343 evsel = perf_session__find_first_evtype(session, j);
346 * even if fields is set to 0 (ie., show nothing) event must
347 * exist if user explicitly includes it on the command line
349 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
350 pr_err("%s events do not exist. "
351 "Remove corresponding -f option to proceed.\n",
356 if (evsel && output[j].fields &&
357 perf_evsel__check_attr(evsel, session))
363 set_print_ip_opts(&evsel->attr);
367 bool use_callchain = false;
369 evlist__for_each(session->evlist, evsel) {
370 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
371 use_callchain = true;
376 symbol_conf.use_callchain = false;
380 * set default for tracepoints to print symbols only
381 * if callchains are present
383 if (symbol_conf.use_callchain &&
384 !output[PERF_TYPE_TRACEPOINT].user_set) {
385 struct perf_event_attr *attr;
387 j = PERF_TYPE_TRACEPOINT;
388 evsel = perf_session__find_first_evtype(session, j);
394 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
395 output[j].fields |= PERF_OUTPUT_IP;
396 output[j].fields |= PERF_OUTPUT_SYM;
397 output[j].fields |= PERF_OUTPUT_DSO;
398 set_print_ip_opts(attr);
406 static void print_sample_iregs(union perf_event *event __maybe_unused,
407 struct perf_sample *sample,
408 struct thread *thread __maybe_unused,
409 struct perf_event_attr *attr)
411 struct regs_dump *regs = &sample->intr_regs;
412 uint64_t mask = attr->sample_regs_intr;
418 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
419 u64 val = regs->regs[i++];
420 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
424 static void print_sample_start(struct perf_sample *sample,
425 struct thread *thread,
426 struct perf_evsel *evsel)
428 struct perf_event_attr *attr = &evsel->attr;
431 unsigned long long nsecs;
433 if (PRINT_FIELD(COMM)) {
435 printf("%8.8s ", thread__comm_str(thread));
436 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
437 printf("%s ", thread__comm_str(thread));
439 printf("%16s ", thread__comm_str(thread));
442 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
443 printf("%5d/%-5d ", sample->pid, sample->tid);
444 else if (PRINT_FIELD(PID))
445 printf("%5d ", sample->pid);
446 else if (PRINT_FIELD(TID))
447 printf("%5d ", sample->tid);
449 if (PRINT_FIELD(CPU)) {
451 printf("%3d ", sample->cpu);
453 printf("[%03d] ", sample->cpu);
456 if (PRINT_FIELD(TIME)) {
457 nsecs = sample->time;
458 secs = nsecs / NSECS_PER_SEC;
459 nsecs -= secs * NSECS_PER_SEC;
460 usecs = nsecs / NSECS_PER_USEC;
462 printf("%5lu.%09llu: ", secs, nsecs);
464 printf("%5lu.%06lu: ", secs, usecs);
469 mispred_str(struct branch_entry *br)
471 if (!(br->flags.mispred || br->flags.predicted))
474 return br->flags.predicted ? 'P' : 'M';
477 static void print_sample_brstack(union perf_event *event __maybe_unused,
478 struct perf_sample *sample,
479 struct thread *thread __maybe_unused,
480 struct perf_event_attr *attr __maybe_unused)
482 struct branch_stack *br = sample->branch_stack;
488 for (i = 0; i < br->nr; i++) {
489 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
492 mispred_str( br->entries + i),
493 br->entries[i].flags.in_tx? 'X' : '-',
494 br->entries[i].flags.abort? 'A' : '-',
495 br->entries[i].flags.cycles);
499 static void print_sample_brstacksym(union perf_event *event __maybe_unused,
500 struct perf_sample *sample,
501 struct thread *thread __maybe_unused,
502 struct perf_event_attr *attr __maybe_unused)
504 struct branch_stack *br = sample->branch_stack;
505 struct addr_location alf, alt;
506 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
512 for (i = 0; i < br->nr; i++) {
514 memset(&alf, 0, sizeof(alf));
515 memset(&alt, 0, sizeof(alt));
516 from = br->entries[i].from;
517 to = br->entries[i].to;
519 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
521 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
523 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
525 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
527 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
529 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
530 printf("/%c/%c/%c/%d ",
531 mispred_str( br->entries + i),
532 br->entries[i].flags.in_tx? 'X' : '-',
533 br->entries[i].flags.abort? 'A' : '-',
534 br->entries[i].flags.cycles);
539 static void print_sample_addr(union perf_event *event,
540 struct perf_sample *sample,
541 struct thread *thread,
542 struct perf_event_attr *attr)
544 struct addr_location al;
546 printf("%16" PRIx64, sample->addr);
548 if (!sample_addr_correlates_sym(attr))
551 perf_event__preprocess_sample_addr(event, sample, thread, &al);
553 if (PRINT_FIELD(SYM)) {
555 if (PRINT_FIELD(SYMOFFSET))
556 symbol__fprintf_symname_offs(al.sym, &al, stdout);
558 symbol__fprintf_symname(al.sym, stdout);
561 if (PRINT_FIELD(DSO)) {
563 map__fprintf_dsoname(al.map, stdout);
568 static void print_sample_bts(union perf_event *event,
569 struct perf_sample *sample,
570 struct perf_evsel *evsel,
571 struct thread *thread,
572 struct addr_location *al)
574 struct perf_event_attr *attr = &evsel->attr;
575 bool print_srcline_last = false;
577 /* print branch_from information */
578 if (PRINT_FIELD(IP)) {
579 unsigned int print_opts = output[attr->type].print_ip_opts;
581 if (symbol_conf.use_callchain && sample->callchain) {
585 if (print_opts & PRINT_IP_OPT_SRCLINE) {
586 print_srcline_last = true;
587 print_opts &= ~PRINT_IP_OPT_SRCLINE;
590 perf_evsel__print_ip(evsel, sample, al, print_opts,
591 scripting_max_stack);
594 /* print branch_to information */
595 if (PRINT_FIELD(ADDR) ||
596 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
597 !output[attr->type].user_set)) {
599 print_sample_addr(event, sample, thread, attr);
602 if (print_srcline_last)
603 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
608 static void print_sample_flags(u32 flags)
610 const char *chars = PERF_IP_FLAG_CHARS;
611 const int n = strlen(PERF_IP_FLAG_CHARS);
615 for (i = 0; i < n; i++, flags >>= 1) {
617 str[pos++] = chars[i];
619 for (; i < 32; i++, flags >>= 1) {
624 printf(" %-4s ", str);
628 struct perf_tool tool;
629 struct perf_session *session;
630 bool show_task_events;
631 bool show_mmap_events;
632 bool show_switch_events;
634 struct cpu_map *cpus;
635 struct thread_map *threads;
639 static int perf_evlist__max_name_len(struct perf_evlist *evlist)
641 struct perf_evsel *evsel;
644 evlist__for_each(evlist, evsel) {
645 int len = strlen(perf_evsel__name(evsel));
653 static size_t data_src__printf(u64 data_src)
655 struct mem_info mi = { .data_src.val = data_src };
661 perf_script__meminfo_scnprintf(decode, 100, &mi);
663 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
667 return printf("%-*s", maxlen, out);
670 static void process_event(struct perf_script *script, union perf_event *event,
671 struct perf_sample *sample, struct perf_evsel *evsel,
672 struct addr_location *al)
674 struct thread *thread = al->thread;
675 struct perf_event_attr *attr = &evsel->attr;
677 if (output[attr->type].fields == 0)
680 print_sample_start(sample, thread, evsel);
682 if (PRINT_FIELD(PERIOD))
683 printf("%10" PRIu64 " ", sample->period);
685 if (PRINT_FIELD(EVNAME)) {
686 const char *evname = perf_evsel__name(evsel);
688 if (!script->name_width)
689 script->name_width = perf_evlist__max_name_len(script->session->evlist);
691 printf("%*s: ", script->name_width,
692 evname ? evname : "[unknown]");
696 print_sample_flags(sample->flags);
698 if (is_bts_event(attr)) {
699 print_sample_bts(event, sample, evsel, thread, al);
703 if (PRINT_FIELD(TRACE))
704 event_format__print(evsel->tp_format, sample->cpu,
705 sample->raw_data, sample->raw_size);
706 if (PRINT_FIELD(ADDR))
707 print_sample_addr(event, sample, thread, attr);
709 if (PRINT_FIELD(DATA_SRC))
710 data_src__printf(sample->data_src);
712 if (PRINT_FIELD(WEIGHT))
713 printf("%16" PRIu64, sample->weight);
715 if (PRINT_FIELD(IP)) {
716 if (!symbol_conf.use_callchain)
721 perf_evsel__print_ip(evsel, sample, al,
722 output[attr->type].print_ip_opts,
723 scripting_max_stack);
726 if (PRINT_FIELD(IREGS))
727 print_sample_iregs(event, sample, thread, attr);
729 if (PRINT_FIELD(BRSTACK))
730 print_sample_brstack(event, sample, thread, attr);
731 else if (PRINT_FIELD(BRSTACKSYM))
732 print_sample_brstacksym(event, sample, thread, attr);
737 static struct scripting_ops *scripting_ops;
739 static void __process_stat(struct perf_evsel *counter, u64 tstamp)
741 int nthreads = thread_map__nr(counter->threads);
742 int ncpus = perf_evsel__nr_cpus(counter);
744 static int header_printed;
746 if (counter->system_wide)
749 if (!header_printed) {
750 printf("%3s %8s %15s %15s %15s %15s %s\n",
751 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
755 for (thread = 0; thread < nthreads; thread++) {
756 for (cpu = 0; cpu < ncpus; cpu++) {
757 struct perf_counts_values *counts;
759 counts = perf_counts(counter->counts, cpu, thread);
761 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
762 counter->cpus->map[cpu],
763 thread_map__pid(counter->threads, thread),
768 perf_evsel__name(counter));
773 static void process_stat(struct perf_evsel *counter, u64 tstamp)
775 if (scripting_ops && scripting_ops->process_stat)
776 scripting_ops->process_stat(&stat_config, counter, tstamp);
778 __process_stat(counter, tstamp);
781 static void process_stat_interval(u64 tstamp)
783 if (scripting_ops && scripting_ops->process_stat_interval)
784 scripting_ops->process_stat_interval(tstamp);
787 static void setup_scripting(void)
789 setup_perl_scripting();
790 setup_python_scripting();
793 static int flush_scripting(void)
795 return scripting_ops ? scripting_ops->flush_script() : 0;
798 static int cleanup_scripting(void)
800 pr_debug("\nperf script stopped\n");
802 return scripting_ops ? scripting_ops->stop_script() : 0;
805 static int process_sample_event(struct perf_tool *tool,
806 union perf_event *event,
807 struct perf_sample *sample,
808 struct perf_evsel *evsel,
809 struct machine *machine)
811 struct perf_script *scr = container_of(tool, struct perf_script, tool);
812 struct addr_location al;
815 if (sample->time < last_timestamp) {
816 pr_err("Samples misordered, previous: %" PRIu64
817 " this: %" PRIu64 "\n", last_timestamp,
821 last_timestamp = sample->time;
825 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
826 pr_err("problem processing %d event, skipping it.\n",
834 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
838 scripting_ops->process_event(event, sample, evsel, &al);
840 process_event(scr, event, sample, evsel, &al);
843 addr_location__put(&al);
847 static int process_attr(struct perf_tool *tool, union perf_event *event,
848 struct perf_evlist **pevlist)
850 struct perf_script *scr = container_of(tool, struct perf_script, tool);
851 struct perf_evlist *evlist;
852 struct perf_evsel *evsel, *pos;
855 err = perf_event__process_attr(tool, event, pevlist);
860 evsel = perf_evlist__last(*pevlist);
862 if (evsel->attr.type >= PERF_TYPE_MAX)
865 evlist__for_each(evlist, pos) {
866 if (pos->attr.type == evsel->attr.type && pos != evsel)
870 set_print_ip_opts(&evsel->attr);
872 if (evsel->attr.sample_type)
873 err = perf_evsel__check_attr(evsel, scr->session);
878 static int process_comm_event(struct perf_tool *tool,
879 union perf_event *event,
880 struct perf_sample *sample,
881 struct machine *machine)
883 struct thread *thread;
884 struct perf_script *script = container_of(tool, struct perf_script, tool);
885 struct perf_session *session = script->session;
886 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
889 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
890 if (thread == NULL) {
891 pr_debug("problem processing COMM event, skipping it.\n");
895 if (perf_event__process_comm(tool, event, sample, machine) < 0)
898 if (!evsel->attr.sample_id_all) {
901 sample->tid = event->comm.tid;
902 sample->pid = event->comm.pid;
904 print_sample_start(sample, thread, evsel);
905 perf_event__fprintf(event, stdout);
912 static int process_fork_event(struct perf_tool *tool,
913 union perf_event *event,
914 struct perf_sample *sample,
915 struct machine *machine)
917 struct thread *thread;
918 struct perf_script *script = container_of(tool, struct perf_script, tool);
919 struct perf_session *session = script->session;
920 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
922 if (perf_event__process_fork(tool, event, sample, machine) < 0)
925 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
926 if (thread == NULL) {
927 pr_debug("problem processing FORK event, skipping it.\n");
931 if (!evsel->attr.sample_id_all) {
933 sample->time = event->fork.time;
934 sample->tid = event->fork.tid;
935 sample->pid = event->fork.pid;
937 print_sample_start(sample, thread, evsel);
938 perf_event__fprintf(event, stdout);
943 static int process_exit_event(struct perf_tool *tool,
944 union perf_event *event,
945 struct perf_sample *sample,
946 struct machine *machine)
949 struct thread *thread;
950 struct perf_script *script = container_of(tool, struct perf_script, tool);
951 struct perf_session *session = script->session;
952 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
954 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
955 if (thread == NULL) {
956 pr_debug("problem processing EXIT event, skipping it.\n");
960 if (!evsel->attr.sample_id_all) {
963 sample->tid = event->fork.tid;
964 sample->pid = event->fork.pid;
966 print_sample_start(sample, thread, evsel);
967 perf_event__fprintf(event, stdout);
969 if (perf_event__process_exit(tool, event, sample, machine) < 0)
976 static int process_mmap_event(struct perf_tool *tool,
977 union perf_event *event,
978 struct perf_sample *sample,
979 struct machine *machine)
981 struct thread *thread;
982 struct perf_script *script = container_of(tool, struct perf_script, tool);
983 struct perf_session *session = script->session;
984 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
986 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
989 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
990 if (thread == NULL) {
991 pr_debug("problem processing MMAP event, skipping it.\n");
995 if (!evsel->attr.sample_id_all) {
998 sample->tid = event->mmap.tid;
999 sample->pid = event->mmap.pid;
1001 print_sample_start(sample, thread, evsel);
1002 perf_event__fprintf(event, stdout);
1003 thread__put(thread);
1007 static int process_mmap2_event(struct perf_tool *tool,
1008 union perf_event *event,
1009 struct perf_sample *sample,
1010 struct machine *machine)
1012 struct thread *thread;
1013 struct perf_script *script = container_of(tool, struct perf_script, tool);
1014 struct perf_session *session = script->session;
1015 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1017 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1020 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1021 if (thread == NULL) {
1022 pr_debug("problem processing MMAP2 event, skipping it.\n");
1026 if (!evsel->attr.sample_id_all) {
1029 sample->tid = event->mmap2.tid;
1030 sample->pid = event->mmap2.pid;
1032 print_sample_start(sample, thread, evsel);
1033 perf_event__fprintf(event, stdout);
1034 thread__put(thread);
1038 static int process_switch_event(struct perf_tool *tool,
1039 union perf_event *event,
1040 struct perf_sample *sample,
1041 struct machine *machine)
1043 struct thread *thread;
1044 struct perf_script *script = container_of(tool, struct perf_script, tool);
1045 struct perf_session *session = script->session;
1046 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1048 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1051 thread = machine__findnew_thread(machine, sample->pid,
1053 if (thread == NULL) {
1054 pr_debug("problem processing SWITCH event, skipping it.\n");
1058 print_sample_start(sample, thread, evsel);
1059 perf_event__fprintf(event, stdout);
1060 thread__put(thread);
1064 static void sig_handler(int sig __maybe_unused)
1069 static int __cmd_script(struct perf_script *script)
1073 signal(SIGINT, sig_handler);
1075 /* override event processing functions */
1076 if (script->show_task_events) {
1077 script->tool.comm = process_comm_event;
1078 script->tool.fork = process_fork_event;
1079 script->tool.exit = process_exit_event;
1081 if (script->show_mmap_events) {
1082 script->tool.mmap = process_mmap_event;
1083 script->tool.mmap2 = process_mmap2_event;
1085 if (script->show_switch_events)
1086 script->tool.context_switch = process_switch_event;
1088 ret = perf_session__process_events(script->session);
1091 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
1096 struct script_spec {
1097 struct list_head node;
1098 struct scripting_ops *ops;
1102 static LIST_HEAD(script_specs);
1104 static struct script_spec *script_spec__new(const char *spec,
1105 struct scripting_ops *ops)
1107 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1110 strcpy(s->spec, spec);
1117 static void script_spec__add(struct script_spec *s)
1119 list_add_tail(&s->node, &script_specs);
1122 static struct script_spec *script_spec__find(const char *spec)
1124 struct script_spec *s;
1126 list_for_each_entry(s, &script_specs, node)
1127 if (strcasecmp(s->spec, spec) == 0)
1132 static struct script_spec *script_spec__findnew(const char *spec,
1133 struct scripting_ops *ops)
1135 struct script_spec *s = script_spec__find(spec);
1140 s = script_spec__new(spec, ops);
1144 script_spec__add(s);
1149 int script_spec_register(const char *spec, struct scripting_ops *ops)
1151 struct script_spec *s;
1153 s = script_spec__find(spec);
1157 s = script_spec__findnew(spec, ops);
1164 static struct scripting_ops *script_spec__lookup(const char *spec)
1166 struct script_spec *s = script_spec__find(spec);
1173 static void list_available_languages(void)
1175 struct script_spec *s;
1177 fprintf(stderr, "\n");
1178 fprintf(stderr, "Scripting language extensions (used in "
1179 "perf script -s [spec:]script.[spec]):\n\n");
1181 list_for_each_entry(s, &script_specs, node)
1182 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1184 fprintf(stderr, "\n");
1187 static int parse_scriptname(const struct option *opt __maybe_unused,
1188 const char *str, int unset __maybe_unused)
1190 char spec[PATH_MAX];
1191 const char *script, *ext;
1194 if (strcmp(str, "lang") == 0) {
1195 list_available_languages();
1199 script = strchr(str, ':');
1202 if (len >= PATH_MAX) {
1203 fprintf(stderr, "invalid language specifier");
1206 strncpy(spec, str, len);
1208 scripting_ops = script_spec__lookup(spec);
1209 if (!scripting_ops) {
1210 fprintf(stderr, "invalid language specifier");
1216 ext = strrchr(script, '.');
1218 fprintf(stderr, "invalid script extension");
1221 scripting_ops = script_spec__lookup(++ext);
1222 if (!scripting_ops) {
1223 fprintf(stderr, "invalid script extension");
1228 script_name = strdup(script);
1233 static int parse_output_fields(const struct option *opt __maybe_unused,
1234 const char *arg, int unset __maybe_unused)
1237 int i, imax = ARRAY_SIZE(all_output_options);
1240 char *str = strdup(arg);
1246 /* first word can state for which event type the user is specifying
1247 * the fields. If no type exists, the specified fields apply to all
1248 * event types found in the file minus the invalid fields for a type.
1250 tok = strchr(str, ':');
1254 if (!strcmp(str, "hw"))
1255 type = PERF_TYPE_HARDWARE;
1256 else if (!strcmp(str, "sw"))
1257 type = PERF_TYPE_SOFTWARE;
1258 else if (!strcmp(str, "trace"))
1259 type = PERF_TYPE_TRACEPOINT;
1260 else if (!strcmp(str, "raw"))
1261 type = PERF_TYPE_RAW;
1262 else if (!strcmp(str, "break"))
1263 type = PERF_TYPE_BREAKPOINT;
1265 fprintf(stderr, "Invalid event type in field string.\n");
1270 if (output[type].user_set)
1271 pr_warning("Overriding previous field request for %s events.\n",
1274 output[type].fields = 0;
1275 output[type].user_set = true;
1276 output[type].wildcard_set = false;
1280 if (strlen(str) == 0) {
1282 "Cannot set fields to 'none' for all event types.\n");
1287 if (output_set_by_user())
1288 pr_warning("Overriding previous field request for all events.\n");
1290 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1291 output[j].fields = 0;
1292 output[j].user_set = true;
1293 output[j].wildcard_set = true;
1297 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
1298 for (i = 0; i < imax; ++i) {
1299 if (strcmp(tok, all_output_options[i].str) == 0)
1302 if (i == imax && strcmp(tok, "flags") == 0) {
1307 fprintf(stderr, "Invalid field requested.\n");
1313 /* add user option to all events types for
1316 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1317 if (output[j].invalid_fields & all_output_options[i].field) {
1318 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1319 all_output_options[i].str, event_type(j));
1321 output[j].fields |= all_output_options[i].field;
1324 if (output[type].invalid_fields & all_output_options[i].field) {
1325 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1326 all_output_options[i].str, event_type(type));
1331 output[type].fields |= all_output_options[i].field;
1336 if (output[type].fields == 0) {
1337 pr_debug("No fields requested for %s type. "
1338 "Events will not be displayed.\n", event_type(type));
1347 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1348 static int is_directory(const char *base_path, const struct dirent *dent)
1350 char path[PATH_MAX];
1353 sprintf(path, "%s/%s", base_path, dent->d_name);
1354 if (stat(path, &st))
1357 return S_ISDIR(st.st_mode);
1360 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
1361 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1363 if ((lang_dirent.d_type == DT_DIR || \
1364 (lang_dirent.d_type == DT_UNKNOWN && \
1365 is_directory(scripts_path, &lang_dirent))) && \
1366 (strcmp(lang_dirent.d_name, ".")) && \
1367 (strcmp(lang_dirent.d_name, "..")))
1369 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
1370 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1372 if (script_dirent.d_type != DT_DIR && \
1373 (script_dirent.d_type != DT_UNKNOWN || \
1374 !is_directory(lang_path, &script_dirent)))
1377 #define RECORD_SUFFIX "-record"
1378 #define REPORT_SUFFIX "-report"
1380 struct script_desc {
1381 struct list_head node;
1387 static LIST_HEAD(script_descs);
1389 static struct script_desc *script_desc__new(const char *name)
1391 struct script_desc *s = zalloc(sizeof(*s));
1393 if (s != NULL && name)
1394 s->name = strdup(name);
1399 static void script_desc__delete(struct script_desc *s)
1402 zfree(&s->half_liner);
1407 static void script_desc__add(struct script_desc *s)
1409 list_add_tail(&s->node, &script_descs);
1412 static struct script_desc *script_desc__find(const char *name)
1414 struct script_desc *s;
1416 list_for_each_entry(s, &script_descs, node)
1417 if (strcasecmp(s->name, name) == 0)
1422 static struct script_desc *script_desc__findnew(const char *name)
1424 struct script_desc *s = script_desc__find(name);
1429 s = script_desc__new(name);
1431 goto out_delete_desc;
1433 script_desc__add(s);
1438 script_desc__delete(s);
1443 static const char *ends_with(const char *str, const char *suffix)
1445 size_t suffix_len = strlen(suffix);
1446 const char *p = str;
1448 if (strlen(str) > suffix_len) {
1449 p = str + strlen(str) - suffix_len;
1450 if (!strncmp(p, suffix, suffix_len))
1457 static int read_script_info(struct script_desc *desc, const char *filename)
1459 char line[BUFSIZ], *p;
1462 fp = fopen(filename, "r");
1466 while (fgets(line, sizeof(line), fp)) {
1473 if (strlen(p) && *p == '!')
1477 if (strlen(p) && p[strlen(p) - 1] == '\n')
1478 p[strlen(p) - 1] = '\0';
1480 if (!strncmp(p, "description:", strlen("description:"))) {
1481 p += strlen("description:");
1482 desc->half_liner = strdup(ltrim(p));
1486 if (!strncmp(p, "args:", strlen("args:"))) {
1487 p += strlen("args:");
1488 desc->args = strdup(ltrim(p));
1498 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1500 char *script_root, *str;
1502 script_root = strdup(script_dirent->d_name);
1506 str = (char *)ends_with(script_root, suffix);
1516 static int list_available_scripts(const struct option *opt __maybe_unused,
1517 const char *s __maybe_unused,
1518 int unset __maybe_unused)
1520 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1521 char scripts_path[MAXPATHLEN];
1522 DIR *scripts_dir, *lang_dir;
1523 char script_path[MAXPATHLEN];
1524 char lang_path[MAXPATHLEN];
1525 struct script_desc *desc;
1526 char first_half[BUFSIZ];
1529 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1531 scripts_dir = opendir(scripts_path);
1535 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1536 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1537 lang_dirent.d_name);
1538 lang_dir = opendir(lang_path);
1542 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1543 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1545 desc = script_desc__findnew(script_root);
1546 snprintf(script_path, MAXPATHLEN, "%s/%s",
1547 lang_path, script_dirent.d_name);
1548 read_script_info(desc, script_path);
1554 fprintf(stdout, "List of available trace scripts:\n");
1555 list_for_each_entry(desc, &script_descs, node) {
1556 sprintf(first_half, "%s %s", desc->name,
1557 desc->args ? desc->args : "");
1558 fprintf(stdout, " %-36s %s\n", first_half,
1559 desc->half_liner ? desc->half_liner : "");
1566 * Some scripts specify the required events in their "xxx-record" file,
1567 * this function will check if the events in perf.data match those
1568 * mentioned in the "xxx-record".
1570 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1571 * which is covered well now. And new parsing code should be added to
1572 * cover the future complexing formats like event groups etc.
1574 static int check_ev_match(char *dir_name, char *scriptname,
1575 struct perf_session *session)
1577 char filename[MAXPATHLEN], evname[128];
1578 char line[BUFSIZ], *p;
1579 struct perf_evsel *pos;
1583 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1585 fp = fopen(filename, "r");
1589 while (fgets(line, sizeof(line), fp)) {
1595 p = strstr(p, "-e");
1601 len = strcspn(p, " \t");
1605 snprintf(evname, len + 1, "%s", p);
1608 evlist__for_each(session->evlist, pos) {
1609 if (!strcmp(perf_evsel__name(pos), evname)) {
1627 * Return -1 if none is found, otherwise the actual scripts number.
1629 * Currently the only user of this function is the script browser, which
1630 * will list all statically runnable scripts, select one, execute it and
1631 * show the output in a perf browser.
1633 int find_scripts(char **scripts_array, char **scripts_path_array)
1635 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1636 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
1637 DIR *scripts_dir, *lang_dir;
1638 struct perf_session *session;
1639 struct perf_data_file file = {
1641 .mode = PERF_DATA_MODE_READ,
1646 session = perf_session__new(&file, false, NULL);
1650 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1652 scripts_dir = opendir(scripts_path);
1654 perf_session__delete(session);
1658 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1659 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1660 lang_dirent.d_name);
1662 if (strstr(lang_path, "perl"))
1666 if (strstr(lang_path, "python"))
1670 lang_dir = opendir(lang_path);
1674 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1675 /* Skip those real time scripts: xxxtop.p[yl] */
1676 if (strstr(script_dirent.d_name, "top."))
1678 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1679 script_dirent.d_name);
1680 temp = strchr(script_dirent.d_name, '.');
1681 snprintf(scripts_array[i],
1682 (temp - script_dirent.d_name) + 1,
1683 "%s", script_dirent.d_name);
1685 if (check_ev_match(lang_path,
1686 scripts_array[i], session))
1694 closedir(scripts_dir);
1695 perf_session__delete(session);
1699 static char *get_script_path(const char *script_root, const char *suffix)
1701 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1702 char scripts_path[MAXPATHLEN];
1703 char script_path[MAXPATHLEN];
1704 DIR *scripts_dir, *lang_dir;
1705 char lang_path[MAXPATHLEN];
1706 char *__script_root;
1708 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1710 scripts_dir = opendir(scripts_path);
1714 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1715 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1716 lang_dirent.d_name);
1717 lang_dir = opendir(lang_path);
1721 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1722 __script_root = get_script_root(&script_dirent, suffix);
1723 if (__script_root && !strcmp(script_root, __script_root)) {
1724 free(__script_root);
1726 closedir(scripts_dir);
1727 snprintf(script_path, MAXPATHLEN, "%s/%s",
1728 lang_path, script_dirent.d_name);
1729 return strdup(script_path);
1731 free(__script_root);
1735 closedir(scripts_dir);
1740 static bool is_top_script(const char *script_path)
1742 return ends_with(script_path, "top") == NULL ? false : true;
1745 static int has_required_arg(char *script_path)
1747 struct script_desc *desc;
1751 desc = script_desc__new(NULL);
1753 if (read_script_info(desc, script_path))
1759 for (p = desc->args; *p; p++)
1763 script_desc__delete(desc);
1768 static int have_cmd(int argc, const char **argv)
1770 char **__argv = malloc(sizeof(const char *) * argc);
1773 pr_err("malloc failed\n");
1777 memcpy(__argv, argv, sizeof(const char *) * argc);
1778 argc = parse_options(argc, (const char **)__argv, record_options,
1779 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1782 system_wide = (argc == 0);
1787 static void script__setup_sample_type(struct perf_script *script)
1789 struct perf_session *session = script->session;
1790 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1792 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1793 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1794 (sample_type & PERF_SAMPLE_STACK_USER))
1795 callchain_param.record_mode = CALLCHAIN_DWARF;
1796 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1797 callchain_param.record_mode = CALLCHAIN_LBR;
1799 callchain_param.record_mode = CALLCHAIN_FP;
1803 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1804 union perf_event *event,
1805 struct perf_session *session)
1807 struct stat_round_event *round = &event->stat_round;
1808 struct perf_evsel *counter;
1810 evlist__for_each(session->evlist, counter) {
1811 perf_stat_process_counter(&stat_config, counter);
1812 process_stat(counter, round->time);
1815 process_stat_interval(round->time);
1819 static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1820 union perf_event *event,
1821 struct perf_session *session __maybe_unused)
1823 perf_event__read_stat_config(&stat_config, &event->stat_config);
1827 static int set_maps(struct perf_script *script)
1829 struct perf_evlist *evlist = script->session->evlist;
1831 if (!script->cpus || !script->threads)
1834 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1837 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1839 if (perf_evlist__alloc_stats(evlist, true))
1842 script->allocated = true;
1847 int process_thread_map_event(struct perf_tool *tool,
1848 union perf_event *event,
1849 struct perf_session *session __maybe_unused)
1851 struct perf_script *script = container_of(tool, struct perf_script, tool);
1853 if (script->threads) {
1854 pr_warning("Extra thread map event, ignoring.\n");
1858 script->threads = thread_map__new_event(&event->thread_map);
1859 if (!script->threads)
1862 return set_maps(script);
1866 int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1867 union perf_event *event,
1868 struct perf_session *session __maybe_unused)
1870 struct perf_script *script = container_of(tool, struct perf_script, tool);
1873 pr_warning("Extra cpu map event, ignoring.\n");
1877 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1881 return set_maps(script);
1884 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1886 bool show_full_info = false;
1887 bool header = false;
1888 bool header_only = false;
1889 bool script_started = false;
1890 char *rec_script_path = NULL;
1891 char *rep_script_path = NULL;
1892 struct perf_session *session;
1893 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
1894 char *script_path = NULL;
1895 const char **__argv;
1897 struct perf_script script = {
1899 .sample = process_sample_event,
1900 .mmap = perf_event__process_mmap,
1901 .mmap2 = perf_event__process_mmap2,
1902 .comm = perf_event__process_comm,
1903 .exit = perf_event__process_exit,
1904 .fork = perf_event__process_fork,
1905 .attr = process_attr,
1906 .tracing_data = perf_event__process_tracing_data,
1907 .build_id = perf_event__process_build_id,
1908 .id_index = perf_event__process_id_index,
1909 .auxtrace_info = perf_event__process_auxtrace_info,
1910 .auxtrace = perf_event__process_auxtrace,
1911 .auxtrace_error = perf_event__process_auxtrace_error,
1912 .stat = perf_event__process_stat_event,
1913 .stat_round = process_stat_round_event,
1914 .stat_config = process_stat_config_event,
1915 .thread_map = process_thread_map_event,
1916 .cpu_map = process_cpu_map_event,
1917 .ordered_events = true,
1918 .ordering_requires_timestamps = true,
1921 struct perf_data_file file = {
1922 .mode = PERF_DATA_MODE_READ,
1924 const struct option options[] = {
1925 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1926 "dump raw trace in ASCII"),
1927 OPT_INCR('v', "verbose", &verbose,
1928 "be more verbose (show symbol address, etc)"),
1929 OPT_BOOLEAN('L', "Latency", &latency_format,
1930 "show latency attributes (irqs/preemption disabled, etc)"),
1931 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1932 list_available_scripts),
1933 OPT_CALLBACK('s', "script", NULL, "name",
1934 "script file name (lang:script name, script name, or *)",
1936 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1937 "generate perf-script.xx script in specified language"),
1938 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1939 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1940 "do various checks like samples ordering and lost events"),
1941 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1942 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
1943 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1944 "file", "vmlinux pathname"),
1945 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1946 "file", "kallsyms pathname"),
1947 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1948 "When printing symbols do not display call chain"),
1949 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1950 "Look for files with symbols relative to this directory"),
1951 OPT_CALLBACK('F', "fields", NULL, "str",
1952 "comma separated output fields prepend with 'type:'. "
1953 "Valid types: hw,sw,trace,raw. "
1954 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1955 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
1956 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1957 "system-wide collection from all CPUs"),
1958 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1959 "only consider these symbols"),
1960 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1961 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1962 "only display events for these comms"),
1963 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1964 "only consider symbols in these pids"),
1965 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1966 "only consider symbols in these tids"),
1967 OPT_BOOLEAN('I', "show-info", &show_full_info,
1968 "display extended information from perf.data file"),
1969 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1970 "Show the path of [kernel.kallsyms]"),
1971 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1972 "Show the fork/comm/exit events"),
1973 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1974 "Show the mmap events"),
1975 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1976 "Show context switch events (if recorded)"),
1977 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
1978 OPT_BOOLEAN(0, "ns", &nanosecs,
1979 "Use 9 decimal places when displaying time"),
1980 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1981 "Instruction Tracing options",
1982 itrace_parse_synth_opts),
1983 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1984 "Show full source file name path for source lines"),
1985 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1986 "Enable symbol demangling"),
1987 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1988 "Enable kernel symbol demangling"),
1992 const char * const script_subcommands[] = { "record", "report", NULL };
1993 const char *script_usage[] = {
1994 "perf script [<options>]",
1995 "perf script [<options>] record <script> [<record-options>] <command>",
1996 "perf script [<options>] report <script> [script-args]",
1997 "perf script [<options>] <script> [<record-options>] <command>",
1998 "perf script [<options>] <top-script> [script-args]",
2004 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
2005 PARSE_OPT_STOP_AT_NON_OPTION);
2007 file.path = input_name;
2009 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2010 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2011 if (!rec_script_path)
2012 return cmd_record(argc, argv, NULL);
2015 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2016 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2017 if (!rep_script_path) {
2019 "Please specify a valid report script"
2020 "(see 'perf script -l' for listing)\n");
2025 if (itrace_synth_opts.callchain &&
2026 itrace_synth_opts.callchain_sz > scripting_max_stack)
2027 scripting_max_stack = itrace_synth_opts.callchain_sz;
2029 /* make sure PERF_EXEC_PATH is set for scripts */
2030 set_argv_exec_path(get_argv_exec_path());
2032 if (argc && !script_name && !rec_script_path && !rep_script_path) {
2037 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2038 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2040 if (!rec_script_path && !rep_script_path) {
2041 usage_with_options_msg(script_usage, options,
2042 "Couldn't find script `%s'\n\n See perf"
2043 " script -l for available scripts.\n", argv[0]);
2046 if (is_top_script(argv[0])) {
2047 rep_args = argc - 1;
2051 rep_args = has_required_arg(rep_script_path);
2052 rec_args = (argc - 1) - rep_args;
2054 usage_with_options_msg(script_usage, options,
2055 "`%s' script requires options."
2056 "\n\n See perf script -l for available "
2057 "scripts and options.\n", argv[0]);
2061 if (pipe(live_pipe) < 0) {
2062 perror("failed to create pipe");
2068 perror("failed to fork");
2075 dup2(live_pipe[1], 1);
2076 close(live_pipe[0]);
2078 if (is_top_script(argv[0])) {
2080 } else if (!system_wide) {
2081 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2087 __argv = malloc((argc + 6) * sizeof(const char *));
2089 pr_err("malloc failed\n");
2094 __argv[j++] = "/bin/sh";
2095 __argv[j++] = rec_script_path;
2101 for (i = rep_args + 1; i < argc; i++)
2102 __argv[j++] = argv[i];
2105 execvp("/bin/sh", (char **)__argv);
2110 dup2(live_pipe[0], 0);
2111 close(live_pipe[1]);
2113 __argv = malloc((argc + 4) * sizeof(const char *));
2115 pr_err("malloc failed\n");
2121 __argv[j++] = "/bin/sh";
2122 __argv[j++] = rep_script_path;
2123 for (i = 1; i < rep_args + 1; i++)
2124 __argv[j++] = argv[i];
2129 execvp("/bin/sh", (char **)__argv);
2134 if (rec_script_path)
2135 script_path = rec_script_path;
2136 if (rep_script_path)
2137 script_path = rep_script_path;
2142 if (!rec_script_path)
2143 system_wide = false;
2144 else if (!system_wide) {
2145 if (have_cmd(argc - 1, &argv[1]) != 0) {
2151 __argv = malloc((argc + 2) * sizeof(const char *));
2153 pr_err("malloc failed\n");
2158 __argv[j++] = "/bin/sh";
2159 __argv[j++] = script_path;
2162 for (i = 2; i < argc; i++)
2163 __argv[j++] = argv[i];
2166 execvp("/bin/sh", (char **)__argv);
2174 session = perf_session__new(&file, false, &script.tool);
2175 if (session == NULL)
2178 if (header || header_only) {
2179 perf_session__fprintf_info(session, stdout, show_full_info);
2184 if (symbol__init(&session->header.env) < 0)
2187 script.session = session;
2188 script__setup_sample_type(&script);
2190 session->itrace_synth_opts = &itrace_synth_opts;
2193 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2199 symbol_conf.use_callchain = true;
2201 symbol_conf.use_callchain = false;
2203 if (session->tevent.pevent &&
2204 pevent_set_function_resolver(session->tevent.pevent,
2205 machine__resolve_kernel_addr,
2206 &session->machines.host) < 0) {
2207 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2211 if (generate_script_lang) {
2212 struct stat perf_stat;
2215 if (output_set_by_user()) {
2217 "custom fields not supported for generated scripts");
2222 input = open(file.path, O_RDONLY); /* input_name */
2225 perror("failed to open file");
2229 err = fstat(input, &perf_stat);
2231 perror("failed to stat file");
2235 if (!perf_stat.st_size) {
2236 fprintf(stderr, "zero-sized file, nothing to do!\n");
2240 scripting_ops = script_spec__lookup(generate_script_lang);
2241 if (!scripting_ops) {
2242 fprintf(stderr, "invalid language specifier");
2247 err = scripting_ops->generate_script(session->tevent.pevent,
2253 err = scripting_ops->start_script(script_name, argc, argv);
2256 pr_debug("perf script started with script %s\n\n", script_name);
2257 script_started = true;
2261 err = perf_session__check_output_opt(session);
2265 err = __cmd_script(&script);
2270 perf_evlist__free_stats(session->evlist);
2271 perf_session__delete(session);
2274 cleanup_scripting();