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>
27 static char const *script_name;
28 static char const *generate_script_lang;
29 static bool debug_mode;
30 static u64 last_timestamp;
31 static u64 nr_unordered;
32 static bool no_callchain;
33 static bool latency_format;
34 static bool system_wide;
35 static bool print_flags;
37 static const char *cpu_list;
38 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
40 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
42 enum perf_output_field {
43 PERF_OUTPUT_COMM = 1U << 0,
44 PERF_OUTPUT_TID = 1U << 1,
45 PERF_OUTPUT_PID = 1U << 2,
46 PERF_OUTPUT_TIME = 1U << 3,
47 PERF_OUTPUT_CPU = 1U << 4,
48 PERF_OUTPUT_EVNAME = 1U << 5,
49 PERF_OUTPUT_TRACE = 1U << 6,
50 PERF_OUTPUT_IP = 1U << 7,
51 PERF_OUTPUT_SYM = 1U << 8,
52 PERF_OUTPUT_DSO = 1U << 9,
53 PERF_OUTPUT_ADDR = 1U << 10,
54 PERF_OUTPUT_SYMOFFSET = 1U << 11,
55 PERF_OUTPUT_SRCLINE = 1U << 12,
56 PERF_OUTPUT_PERIOD = 1U << 13,
57 PERF_OUTPUT_IREGS = 1U << 14,
58 PERF_OUTPUT_BRSTACK = 1U << 15,
59 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
62 struct output_option {
64 enum perf_output_field field;
65 } all_output_options[] = {
66 {.str = "comm", .field = PERF_OUTPUT_COMM},
67 {.str = "tid", .field = PERF_OUTPUT_TID},
68 {.str = "pid", .field = PERF_OUTPUT_PID},
69 {.str = "time", .field = PERF_OUTPUT_TIME},
70 {.str = "cpu", .field = PERF_OUTPUT_CPU},
71 {.str = "event", .field = PERF_OUTPUT_EVNAME},
72 {.str = "trace", .field = PERF_OUTPUT_TRACE},
73 {.str = "ip", .field = PERF_OUTPUT_IP},
74 {.str = "sym", .field = PERF_OUTPUT_SYM},
75 {.str = "dso", .field = PERF_OUTPUT_DSO},
76 {.str = "addr", .field = PERF_OUTPUT_ADDR},
77 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
78 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
79 {.str = "period", .field = PERF_OUTPUT_PERIOD},
80 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
81 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
82 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
85 /* default set to maintain compatibility with current format */
89 unsigned int print_ip_opts;
92 } output[PERF_TYPE_MAX] = {
94 [PERF_TYPE_HARDWARE] = {
97 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
98 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
99 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
100 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
103 .invalid_fields = PERF_OUTPUT_TRACE,
106 [PERF_TYPE_SOFTWARE] = {
109 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
110 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
111 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
112 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
115 .invalid_fields = PERF_OUTPUT_TRACE,
118 [PERF_TYPE_TRACEPOINT] = {
121 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
122 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
123 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
129 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
130 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
131 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
132 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
135 .invalid_fields = PERF_OUTPUT_TRACE,
138 [PERF_TYPE_BREAKPOINT] = {
141 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
142 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
143 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
144 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
147 .invalid_fields = PERF_OUTPUT_TRACE,
151 static bool output_set_by_user(void)
154 for (j = 0; j < PERF_TYPE_MAX; ++j) {
155 if (output[j].user_set)
161 static const char *output_field2str(enum perf_output_field field)
163 int i, imax = ARRAY_SIZE(all_output_options);
164 const char *str = "";
166 for (i = 0; i < imax; ++i) {
167 if (all_output_options[i].field == field) {
168 str = all_output_options[i].str;
175 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
177 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
178 u64 sample_type, const char *sample_msg,
179 enum perf_output_field field,
182 struct perf_event_attr *attr = &evsel->attr;
183 int type = attr->type;
186 if (attr->sample_type & sample_type)
189 if (output[type].user_set) {
192 evname = perf_evsel__name(evsel);
193 pr_err("Samples for '%s' event do not have %s attribute set. "
194 "Cannot print '%s' field.\n",
195 evname, sample_msg, output_field2str(field));
199 /* user did not ask for it explicitly so remove from the default list */
200 output[type].fields &= ~field;
201 evname = perf_evsel__name(evsel);
202 pr_debug("Samples for '%s' event do not have %s attribute set. "
203 "Skipping '%s' field.\n",
204 evname, sample_msg, output_field2str(field));
209 static int perf_evsel__check_stype(struct perf_evsel *evsel,
210 u64 sample_type, const char *sample_msg,
211 enum perf_output_field field)
213 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
217 static int perf_evsel__check_attr(struct perf_evsel *evsel,
218 struct perf_session *session)
220 struct perf_event_attr *attr = &evsel->attr;
223 allow_user_set = perf_header__has_feat(&session->header,
226 if (PRINT_FIELD(TRACE) &&
227 !perf_session__has_traces(session, "record -R"))
230 if (PRINT_FIELD(IP)) {
231 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
236 if (PRINT_FIELD(ADDR) &&
237 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
238 PERF_OUTPUT_ADDR, allow_user_set))
241 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
242 pr_err("Display of symbols requested but neither sample IP nor "
243 "sample address\nis selected. Hence, no addresses to convert "
247 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
248 pr_err("Display of offsets requested but symbol is not"
252 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
253 pr_err("Display of DSO requested but neither sample IP nor "
254 "sample address\nis selected. Hence, no addresses to convert "
258 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
259 pr_err("Display of source line number requested but sample IP is not\n"
260 "selected. Hence, no address to lookup the source line number.\n");
264 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
265 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
266 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
269 if (PRINT_FIELD(TIME) &&
270 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
274 if (PRINT_FIELD(CPU) &&
275 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
276 PERF_OUTPUT_CPU, allow_user_set))
279 if (PRINT_FIELD(PERIOD) &&
280 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
284 if (PRINT_FIELD(IREGS) &&
285 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
292 static void set_print_ip_opts(struct perf_event_attr *attr)
294 unsigned int type = attr->type;
296 output[type].print_ip_opts = 0;
298 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
300 if (PRINT_FIELD(SYM))
301 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
303 if (PRINT_FIELD(DSO))
304 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
306 if (PRINT_FIELD(SYMOFFSET))
307 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
309 if (PRINT_FIELD(SRCLINE))
310 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
314 * verify all user requested events exist and the samples
315 * have the expected data
317 static int perf_session__check_output_opt(struct perf_session *session)
320 struct perf_evsel *evsel;
322 for (j = 0; j < PERF_TYPE_MAX; ++j) {
323 evsel = perf_session__find_first_evtype(session, j);
326 * even if fields is set to 0 (ie., show nothing) event must
327 * exist if user explicitly includes it on the command line
329 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
330 pr_err("%s events do not exist. "
331 "Remove corresponding -f option to proceed.\n",
336 if (evsel && output[j].fields &&
337 perf_evsel__check_attr(evsel, session))
343 set_print_ip_opts(&evsel->attr);
347 bool use_callchain = false;
349 evlist__for_each(session->evlist, evsel) {
350 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
351 use_callchain = true;
356 symbol_conf.use_callchain = false;
360 * set default for tracepoints to print symbols only
361 * if callchains are present
363 if (symbol_conf.use_callchain &&
364 !output[PERF_TYPE_TRACEPOINT].user_set) {
365 struct perf_event_attr *attr;
367 j = PERF_TYPE_TRACEPOINT;
368 evsel = perf_session__find_first_evtype(session, j);
374 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
375 output[j].fields |= PERF_OUTPUT_IP;
376 output[j].fields |= PERF_OUTPUT_SYM;
377 output[j].fields |= PERF_OUTPUT_DSO;
378 set_print_ip_opts(attr);
386 static void print_sample_iregs(union perf_event *event __maybe_unused,
387 struct perf_sample *sample,
388 struct thread *thread __maybe_unused,
389 struct perf_event_attr *attr)
391 struct regs_dump *regs = &sample->intr_regs;
392 uint64_t mask = attr->sample_regs_intr;
398 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
399 u64 val = regs->regs[i++];
400 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
404 static void print_sample_start(struct perf_sample *sample,
405 struct thread *thread,
406 struct perf_evsel *evsel)
408 struct perf_event_attr *attr = &evsel->attr;
411 unsigned long long nsecs;
413 if (PRINT_FIELD(COMM)) {
415 printf("%8.8s ", thread__comm_str(thread));
416 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
417 printf("%s ", thread__comm_str(thread));
419 printf("%16s ", thread__comm_str(thread));
422 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
423 printf("%5d/%-5d ", sample->pid, sample->tid);
424 else if (PRINT_FIELD(PID))
425 printf("%5d ", sample->pid);
426 else if (PRINT_FIELD(TID))
427 printf("%5d ", sample->tid);
429 if (PRINT_FIELD(CPU)) {
431 printf("%3d ", sample->cpu);
433 printf("[%03d] ", sample->cpu);
436 if (PRINT_FIELD(TIME)) {
437 nsecs = sample->time;
438 secs = nsecs / NSECS_PER_SEC;
439 nsecs -= secs * NSECS_PER_SEC;
440 usecs = nsecs / NSECS_PER_USEC;
442 printf("%5lu.%09llu: ", secs, nsecs);
444 printf("%5lu.%06lu: ", secs, usecs);
449 mispred_str(struct branch_entry *br)
451 if (!(br->flags.mispred || br->flags.predicted))
454 return br->flags.predicted ? 'P' : 'M';
457 static void print_sample_brstack(union perf_event *event __maybe_unused,
458 struct perf_sample *sample,
459 struct thread *thread __maybe_unused,
460 struct perf_event_attr *attr __maybe_unused)
462 struct branch_stack *br = sample->branch_stack;
468 for (i = 0; i < br->nr; i++) {
469 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
472 mispred_str( br->entries + i),
473 br->entries[i].flags.in_tx? 'X' : '-',
474 br->entries[i].flags.abort? 'A' : '-',
475 br->entries[i].flags.cycles);
479 static void print_sample_brstacksym(union perf_event *event __maybe_unused,
480 struct perf_sample *sample,
481 struct thread *thread __maybe_unused,
482 struct perf_event_attr *attr __maybe_unused)
484 struct branch_stack *br = sample->branch_stack;
485 struct addr_location alf, alt;
486 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
492 for (i = 0; i < br->nr; i++) {
494 memset(&alf, 0, sizeof(alf));
495 memset(&alt, 0, sizeof(alt));
496 from = br->entries[i].from;
497 to = br->entries[i].to;
499 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
501 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
503 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
505 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
507 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
509 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
510 printf("/%c/%c/%c/%d ",
511 mispred_str( br->entries + i),
512 br->entries[i].flags.in_tx? 'X' : '-',
513 br->entries[i].flags.abort? 'A' : '-',
514 br->entries[i].flags.cycles);
519 static void print_sample_addr(union perf_event *event,
520 struct perf_sample *sample,
521 struct thread *thread,
522 struct perf_event_attr *attr)
524 struct addr_location al;
526 printf("%16" PRIx64, sample->addr);
528 if (!sample_addr_correlates_sym(attr))
531 perf_event__preprocess_sample_addr(event, sample, thread, &al);
533 if (PRINT_FIELD(SYM)) {
535 if (PRINT_FIELD(SYMOFFSET))
536 symbol__fprintf_symname_offs(al.sym, &al, stdout);
538 symbol__fprintf_symname(al.sym, stdout);
541 if (PRINT_FIELD(DSO)) {
543 map__fprintf_dsoname(al.map, stdout);
548 static void print_sample_bts(union perf_event *event,
549 struct perf_sample *sample,
550 struct perf_evsel *evsel,
551 struct thread *thread,
552 struct addr_location *al)
554 struct perf_event_attr *attr = &evsel->attr;
555 bool print_srcline_last = false;
557 /* print branch_from information */
558 if (PRINT_FIELD(IP)) {
559 unsigned int print_opts = output[attr->type].print_ip_opts;
561 if (symbol_conf.use_callchain && sample->callchain) {
565 if (print_opts & PRINT_IP_OPT_SRCLINE) {
566 print_srcline_last = true;
567 print_opts &= ~PRINT_IP_OPT_SRCLINE;
570 perf_evsel__print_ip(evsel, sample, al, print_opts,
571 scripting_max_stack);
574 /* print branch_to information */
575 if (PRINT_FIELD(ADDR) ||
576 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
577 !output[attr->type].user_set)) {
579 print_sample_addr(event, sample, thread, attr);
582 if (print_srcline_last)
583 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
588 static void print_sample_flags(u32 flags)
590 const char *chars = PERF_IP_FLAG_CHARS;
591 const int n = strlen(PERF_IP_FLAG_CHARS);
595 for (i = 0; i < n; i++, flags >>= 1) {
597 str[pos++] = chars[i];
599 for (; i < 32; i++, flags >>= 1) {
604 printf(" %-4s ", str);
608 struct perf_tool tool;
609 struct perf_session *session;
610 bool show_task_events;
611 bool show_mmap_events;
612 bool show_switch_events;
614 struct cpu_map *cpus;
615 struct thread_map *threads;
618 static void process_event(struct perf_script *script __maybe_unused, union perf_event *event,
619 struct perf_sample *sample, struct perf_evsel *evsel,
620 struct addr_location *al)
622 struct thread *thread = al->thread;
623 struct perf_event_attr *attr = &evsel->attr;
625 if (output[attr->type].fields == 0)
628 print_sample_start(sample, thread, evsel);
630 if (PRINT_FIELD(PERIOD))
631 printf("%10" PRIu64 " ", sample->period);
633 if (PRINT_FIELD(EVNAME)) {
634 const char *evname = perf_evsel__name(evsel);
635 printf("%s: ", evname ? evname : "[unknown]");
639 print_sample_flags(sample->flags);
641 if (is_bts_event(attr)) {
642 print_sample_bts(event, sample, evsel, thread, al);
646 if (PRINT_FIELD(TRACE))
647 event_format__print(evsel->tp_format, sample->cpu,
648 sample->raw_data, sample->raw_size);
649 if (PRINT_FIELD(ADDR))
650 print_sample_addr(event, sample, thread, attr);
652 if (PRINT_FIELD(IP)) {
653 if (!symbol_conf.use_callchain)
658 perf_evsel__print_ip(evsel, sample, al,
659 output[attr->type].print_ip_opts,
660 scripting_max_stack);
663 if (PRINT_FIELD(IREGS))
664 print_sample_iregs(event, sample, thread, attr);
666 if (PRINT_FIELD(BRSTACK))
667 print_sample_brstack(event, sample, thread, attr);
668 else if (PRINT_FIELD(BRSTACKSYM))
669 print_sample_brstacksym(event, sample, thread, attr);
674 static struct scripting_ops *scripting_ops;
676 static void setup_scripting(void)
678 setup_perl_scripting();
679 setup_python_scripting();
682 static int flush_scripting(void)
684 return scripting_ops ? scripting_ops->flush_script() : 0;
687 static int cleanup_scripting(void)
689 pr_debug("\nperf script stopped\n");
691 return scripting_ops ? scripting_ops->stop_script() : 0;
694 static int process_sample_event(struct perf_tool *tool,
695 union perf_event *event,
696 struct perf_sample *sample,
697 struct perf_evsel *evsel,
698 struct machine *machine)
700 struct perf_script *scr = container_of(tool, struct perf_script, tool);
701 struct addr_location al;
704 if (sample->time < last_timestamp) {
705 pr_err("Samples misordered, previous: %" PRIu64
706 " this: %" PRIu64 "\n", last_timestamp,
710 last_timestamp = sample->time;
714 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
715 pr_err("problem processing %d event, skipping it.\n",
723 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
727 scripting_ops->process_event(event, sample, evsel, &al);
729 process_event(scr, event, sample, evsel, &al);
732 addr_location__put(&al);
736 static int process_attr(struct perf_tool *tool, union perf_event *event,
737 struct perf_evlist **pevlist)
739 struct perf_script *scr = container_of(tool, struct perf_script, tool);
740 struct perf_evlist *evlist;
741 struct perf_evsel *evsel, *pos;
744 err = perf_event__process_attr(tool, event, pevlist);
749 evsel = perf_evlist__last(*pevlist);
751 if (evsel->attr.type >= PERF_TYPE_MAX)
754 evlist__for_each(evlist, pos) {
755 if (pos->attr.type == evsel->attr.type && pos != evsel)
759 set_print_ip_opts(&evsel->attr);
761 if (evsel->attr.sample_type)
762 err = perf_evsel__check_attr(evsel, scr->session);
767 static int process_comm_event(struct perf_tool *tool,
768 union perf_event *event,
769 struct perf_sample *sample,
770 struct machine *machine)
772 struct thread *thread;
773 struct perf_script *script = container_of(tool, struct perf_script, tool);
774 struct perf_session *session = script->session;
775 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
778 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
779 if (thread == NULL) {
780 pr_debug("problem processing COMM event, skipping it.\n");
784 if (perf_event__process_comm(tool, event, sample, machine) < 0)
787 if (!evsel->attr.sample_id_all) {
790 sample->tid = event->comm.tid;
791 sample->pid = event->comm.pid;
793 print_sample_start(sample, thread, evsel);
794 perf_event__fprintf(event, stdout);
801 static int process_fork_event(struct perf_tool *tool,
802 union perf_event *event,
803 struct perf_sample *sample,
804 struct machine *machine)
806 struct thread *thread;
807 struct perf_script *script = container_of(tool, struct perf_script, tool);
808 struct perf_session *session = script->session;
809 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
811 if (perf_event__process_fork(tool, event, sample, machine) < 0)
814 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
815 if (thread == NULL) {
816 pr_debug("problem processing FORK event, skipping it.\n");
820 if (!evsel->attr.sample_id_all) {
822 sample->time = event->fork.time;
823 sample->tid = event->fork.tid;
824 sample->pid = event->fork.pid;
826 print_sample_start(sample, thread, evsel);
827 perf_event__fprintf(event, stdout);
832 static int process_exit_event(struct perf_tool *tool,
833 union perf_event *event,
834 struct perf_sample *sample,
835 struct machine *machine)
838 struct thread *thread;
839 struct perf_script *script = container_of(tool, struct perf_script, tool);
840 struct perf_session *session = script->session;
841 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
843 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
844 if (thread == NULL) {
845 pr_debug("problem processing EXIT event, skipping it.\n");
849 if (!evsel->attr.sample_id_all) {
852 sample->tid = event->fork.tid;
853 sample->pid = event->fork.pid;
855 print_sample_start(sample, thread, evsel);
856 perf_event__fprintf(event, stdout);
858 if (perf_event__process_exit(tool, event, sample, machine) < 0)
865 static int process_mmap_event(struct perf_tool *tool,
866 union perf_event *event,
867 struct perf_sample *sample,
868 struct machine *machine)
870 struct thread *thread;
871 struct perf_script *script = container_of(tool, struct perf_script, tool);
872 struct perf_session *session = script->session;
873 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
875 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
878 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
879 if (thread == NULL) {
880 pr_debug("problem processing MMAP event, skipping it.\n");
884 if (!evsel->attr.sample_id_all) {
887 sample->tid = event->mmap.tid;
888 sample->pid = event->mmap.pid;
890 print_sample_start(sample, thread, evsel);
891 perf_event__fprintf(event, stdout);
896 static int process_mmap2_event(struct perf_tool *tool,
897 union perf_event *event,
898 struct perf_sample *sample,
899 struct machine *machine)
901 struct thread *thread;
902 struct perf_script *script = container_of(tool, struct perf_script, tool);
903 struct perf_session *session = script->session;
904 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
906 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
909 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
910 if (thread == NULL) {
911 pr_debug("problem processing MMAP2 event, skipping it.\n");
915 if (!evsel->attr.sample_id_all) {
918 sample->tid = event->mmap2.tid;
919 sample->pid = event->mmap2.pid;
921 print_sample_start(sample, thread, evsel);
922 perf_event__fprintf(event, stdout);
927 static int process_switch_event(struct perf_tool *tool,
928 union perf_event *event,
929 struct perf_sample *sample,
930 struct machine *machine)
932 struct thread *thread;
933 struct perf_script *script = container_of(tool, struct perf_script, tool);
934 struct perf_session *session = script->session;
935 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
937 if (perf_event__process_switch(tool, event, sample, machine) < 0)
940 thread = machine__findnew_thread(machine, sample->pid,
942 if (thread == NULL) {
943 pr_debug("problem processing SWITCH event, skipping it.\n");
947 print_sample_start(sample, thread, evsel);
948 perf_event__fprintf(event, stdout);
953 static void sig_handler(int sig __maybe_unused)
958 static int __cmd_script(struct perf_script *script)
962 signal(SIGINT, sig_handler);
964 /* override event processing functions */
965 if (script->show_task_events) {
966 script->tool.comm = process_comm_event;
967 script->tool.fork = process_fork_event;
968 script->tool.exit = process_exit_event;
970 if (script->show_mmap_events) {
971 script->tool.mmap = process_mmap_event;
972 script->tool.mmap2 = process_mmap2_event;
974 if (script->show_switch_events)
975 script->tool.context_switch = process_switch_event;
977 ret = perf_session__process_events(script->session);
980 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
986 struct list_head node;
987 struct scripting_ops *ops;
991 static LIST_HEAD(script_specs);
993 static struct script_spec *script_spec__new(const char *spec,
994 struct scripting_ops *ops)
996 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
999 strcpy(s->spec, spec);
1006 static void script_spec__add(struct script_spec *s)
1008 list_add_tail(&s->node, &script_specs);
1011 static struct script_spec *script_spec__find(const char *spec)
1013 struct script_spec *s;
1015 list_for_each_entry(s, &script_specs, node)
1016 if (strcasecmp(s->spec, spec) == 0)
1021 static struct script_spec *script_spec__findnew(const char *spec,
1022 struct scripting_ops *ops)
1024 struct script_spec *s = script_spec__find(spec);
1029 s = script_spec__new(spec, ops);
1033 script_spec__add(s);
1038 int script_spec_register(const char *spec, struct scripting_ops *ops)
1040 struct script_spec *s;
1042 s = script_spec__find(spec);
1046 s = script_spec__findnew(spec, ops);
1053 static struct scripting_ops *script_spec__lookup(const char *spec)
1055 struct script_spec *s = script_spec__find(spec);
1062 static void list_available_languages(void)
1064 struct script_spec *s;
1066 fprintf(stderr, "\n");
1067 fprintf(stderr, "Scripting language extensions (used in "
1068 "perf script -s [spec:]script.[spec]):\n\n");
1070 list_for_each_entry(s, &script_specs, node)
1071 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1073 fprintf(stderr, "\n");
1076 static int parse_scriptname(const struct option *opt __maybe_unused,
1077 const char *str, int unset __maybe_unused)
1079 char spec[PATH_MAX];
1080 const char *script, *ext;
1083 if (strcmp(str, "lang") == 0) {
1084 list_available_languages();
1088 script = strchr(str, ':');
1091 if (len >= PATH_MAX) {
1092 fprintf(stderr, "invalid language specifier");
1095 strncpy(spec, str, len);
1097 scripting_ops = script_spec__lookup(spec);
1098 if (!scripting_ops) {
1099 fprintf(stderr, "invalid language specifier");
1105 ext = strrchr(script, '.');
1107 fprintf(stderr, "invalid script extension");
1110 scripting_ops = script_spec__lookup(++ext);
1111 if (!scripting_ops) {
1112 fprintf(stderr, "invalid script extension");
1117 script_name = strdup(script);
1122 static int parse_output_fields(const struct option *opt __maybe_unused,
1123 const char *arg, int unset __maybe_unused)
1126 int i, imax = ARRAY_SIZE(all_output_options);
1129 char *str = strdup(arg);
1135 /* first word can state for which event type the user is specifying
1136 * the fields. If no type exists, the specified fields apply to all
1137 * event types found in the file minus the invalid fields for a type.
1139 tok = strchr(str, ':');
1143 if (!strcmp(str, "hw"))
1144 type = PERF_TYPE_HARDWARE;
1145 else if (!strcmp(str, "sw"))
1146 type = PERF_TYPE_SOFTWARE;
1147 else if (!strcmp(str, "trace"))
1148 type = PERF_TYPE_TRACEPOINT;
1149 else if (!strcmp(str, "raw"))
1150 type = PERF_TYPE_RAW;
1151 else if (!strcmp(str, "break"))
1152 type = PERF_TYPE_BREAKPOINT;
1154 fprintf(stderr, "Invalid event type in field string.\n");
1159 if (output[type].user_set)
1160 pr_warning("Overriding previous field request for %s events.\n",
1163 output[type].fields = 0;
1164 output[type].user_set = true;
1165 output[type].wildcard_set = false;
1169 if (strlen(str) == 0) {
1171 "Cannot set fields to 'none' for all event types.\n");
1176 if (output_set_by_user())
1177 pr_warning("Overriding previous field request for all events.\n");
1179 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1180 output[j].fields = 0;
1181 output[j].user_set = true;
1182 output[j].wildcard_set = true;
1186 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
1187 for (i = 0; i < imax; ++i) {
1188 if (strcmp(tok, all_output_options[i].str) == 0)
1191 if (i == imax && strcmp(tok, "flags") == 0) {
1196 fprintf(stderr, "Invalid field requested.\n");
1202 /* add user option to all events types for
1205 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1206 if (output[j].invalid_fields & all_output_options[i].field) {
1207 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1208 all_output_options[i].str, event_type(j));
1210 output[j].fields |= all_output_options[i].field;
1213 if (output[type].invalid_fields & all_output_options[i].field) {
1214 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1215 all_output_options[i].str, event_type(type));
1220 output[type].fields |= all_output_options[i].field;
1225 if (output[type].fields == 0) {
1226 pr_debug("No fields requested for %s type. "
1227 "Events will not be displayed.\n", event_type(type));
1236 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1237 static int is_directory(const char *base_path, const struct dirent *dent)
1239 char path[PATH_MAX];
1242 sprintf(path, "%s/%s", base_path, dent->d_name);
1243 if (stat(path, &st))
1246 return S_ISDIR(st.st_mode);
1249 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
1250 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1252 if ((lang_dirent.d_type == DT_DIR || \
1253 (lang_dirent.d_type == DT_UNKNOWN && \
1254 is_directory(scripts_path, &lang_dirent))) && \
1255 (strcmp(lang_dirent.d_name, ".")) && \
1256 (strcmp(lang_dirent.d_name, "..")))
1258 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
1259 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1261 if (script_dirent.d_type != DT_DIR && \
1262 (script_dirent.d_type != DT_UNKNOWN || \
1263 !is_directory(lang_path, &script_dirent)))
1266 #define RECORD_SUFFIX "-record"
1267 #define REPORT_SUFFIX "-report"
1269 struct script_desc {
1270 struct list_head node;
1276 static LIST_HEAD(script_descs);
1278 static struct script_desc *script_desc__new(const char *name)
1280 struct script_desc *s = zalloc(sizeof(*s));
1282 if (s != NULL && name)
1283 s->name = strdup(name);
1288 static void script_desc__delete(struct script_desc *s)
1291 zfree(&s->half_liner);
1296 static void script_desc__add(struct script_desc *s)
1298 list_add_tail(&s->node, &script_descs);
1301 static struct script_desc *script_desc__find(const char *name)
1303 struct script_desc *s;
1305 list_for_each_entry(s, &script_descs, node)
1306 if (strcasecmp(s->name, name) == 0)
1311 static struct script_desc *script_desc__findnew(const char *name)
1313 struct script_desc *s = script_desc__find(name);
1318 s = script_desc__new(name);
1320 goto out_delete_desc;
1322 script_desc__add(s);
1327 script_desc__delete(s);
1332 static const char *ends_with(const char *str, const char *suffix)
1334 size_t suffix_len = strlen(suffix);
1335 const char *p = str;
1337 if (strlen(str) > suffix_len) {
1338 p = str + strlen(str) - suffix_len;
1339 if (!strncmp(p, suffix, suffix_len))
1346 static int read_script_info(struct script_desc *desc, const char *filename)
1348 char line[BUFSIZ], *p;
1351 fp = fopen(filename, "r");
1355 while (fgets(line, sizeof(line), fp)) {
1362 if (strlen(p) && *p == '!')
1366 if (strlen(p) && p[strlen(p) - 1] == '\n')
1367 p[strlen(p) - 1] = '\0';
1369 if (!strncmp(p, "description:", strlen("description:"))) {
1370 p += strlen("description:");
1371 desc->half_liner = strdup(ltrim(p));
1375 if (!strncmp(p, "args:", strlen("args:"))) {
1376 p += strlen("args:");
1377 desc->args = strdup(ltrim(p));
1387 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1389 char *script_root, *str;
1391 script_root = strdup(script_dirent->d_name);
1395 str = (char *)ends_with(script_root, suffix);
1405 static int list_available_scripts(const struct option *opt __maybe_unused,
1406 const char *s __maybe_unused,
1407 int unset __maybe_unused)
1409 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1410 char scripts_path[MAXPATHLEN];
1411 DIR *scripts_dir, *lang_dir;
1412 char script_path[MAXPATHLEN];
1413 char lang_path[MAXPATHLEN];
1414 struct script_desc *desc;
1415 char first_half[BUFSIZ];
1418 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1420 scripts_dir = opendir(scripts_path);
1424 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1425 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1426 lang_dirent.d_name);
1427 lang_dir = opendir(lang_path);
1431 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1432 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1434 desc = script_desc__findnew(script_root);
1435 snprintf(script_path, MAXPATHLEN, "%s/%s",
1436 lang_path, script_dirent.d_name);
1437 read_script_info(desc, script_path);
1443 fprintf(stdout, "List of available trace scripts:\n");
1444 list_for_each_entry(desc, &script_descs, node) {
1445 sprintf(first_half, "%s %s", desc->name,
1446 desc->args ? desc->args : "");
1447 fprintf(stdout, " %-36s %s\n", first_half,
1448 desc->half_liner ? desc->half_liner : "");
1455 * Some scripts specify the required events in their "xxx-record" file,
1456 * this function will check if the events in perf.data match those
1457 * mentioned in the "xxx-record".
1459 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1460 * which is covered well now. And new parsing code should be added to
1461 * cover the future complexing formats like event groups etc.
1463 static int check_ev_match(char *dir_name, char *scriptname,
1464 struct perf_session *session)
1466 char filename[MAXPATHLEN], evname[128];
1467 char line[BUFSIZ], *p;
1468 struct perf_evsel *pos;
1472 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1474 fp = fopen(filename, "r");
1478 while (fgets(line, sizeof(line), fp)) {
1484 p = strstr(p, "-e");
1490 len = strcspn(p, " \t");
1494 snprintf(evname, len + 1, "%s", p);
1497 evlist__for_each(session->evlist, pos) {
1498 if (!strcmp(perf_evsel__name(pos), evname)) {
1516 * Return -1 if none is found, otherwise the actual scripts number.
1518 * Currently the only user of this function is the script browser, which
1519 * will list all statically runnable scripts, select one, execute it and
1520 * show the output in a perf browser.
1522 int find_scripts(char **scripts_array, char **scripts_path_array)
1524 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1525 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
1526 DIR *scripts_dir, *lang_dir;
1527 struct perf_session *session;
1528 struct perf_data_file file = {
1530 .mode = PERF_DATA_MODE_READ,
1535 session = perf_session__new(&file, false, NULL);
1539 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1541 scripts_dir = opendir(scripts_path);
1543 perf_session__delete(session);
1547 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1548 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1549 lang_dirent.d_name);
1551 if (strstr(lang_path, "perl"))
1555 if (strstr(lang_path, "python"))
1559 lang_dir = opendir(lang_path);
1563 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1564 /* Skip those real time scripts: xxxtop.p[yl] */
1565 if (strstr(script_dirent.d_name, "top."))
1567 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1568 script_dirent.d_name);
1569 temp = strchr(script_dirent.d_name, '.');
1570 snprintf(scripts_array[i],
1571 (temp - script_dirent.d_name) + 1,
1572 "%s", script_dirent.d_name);
1574 if (check_ev_match(lang_path,
1575 scripts_array[i], session))
1583 closedir(scripts_dir);
1584 perf_session__delete(session);
1588 static char *get_script_path(const char *script_root, const char *suffix)
1590 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1591 char scripts_path[MAXPATHLEN];
1592 char script_path[MAXPATHLEN];
1593 DIR *scripts_dir, *lang_dir;
1594 char lang_path[MAXPATHLEN];
1595 char *__script_root;
1597 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
1599 scripts_dir = opendir(scripts_path);
1603 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1604 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1605 lang_dirent.d_name);
1606 lang_dir = opendir(lang_path);
1610 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1611 __script_root = get_script_root(&script_dirent, suffix);
1612 if (__script_root && !strcmp(script_root, __script_root)) {
1613 free(__script_root);
1615 closedir(scripts_dir);
1616 snprintf(script_path, MAXPATHLEN, "%s/%s",
1617 lang_path, script_dirent.d_name);
1618 return strdup(script_path);
1620 free(__script_root);
1624 closedir(scripts_dir);
1629 static bool is_top_script(const char *script_path)
1631 return ends_with(script_path, "top") == NULL ? false : true;
1634 static int has_required_arg(char *script_path)
1636 struct script_desc *desc;
1640 desc = script_desc__new(NULL);
1642 if (read_script_info(desc, script_path))
1648 for (p = desc->args; *p; p++)
1652 script_desc__delete(desc);
1657 static int have_cmd(int argc, const char **argv)
1659 char **__argv = malloc(sizeof(const char *) * argc);
1662 pr_err("malloc failed\n");
1666 memcpy(__argv, argv, sizeof(const char *) * argc);
1667 argc = parse_options(argc, (const char **)__argv, record_options,
1668 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1671 system_wide = (argc == 0);
1676 static void script__setup_sample_type(struct perf_script *script)
1678 struct perf_session *session = script->session;
1679 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1681 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1682 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1683 (sample_type & PERF_SAMPLE_STACK_USER))
1684 callchain_param.record_mode = CALLCHAIN_DWARF;
1685 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1686 callchain_param.record_mode = CALLCHAIN_LBR;
1688 callchain_param.record_mode = CALLCHAIN_FP;
1692 static int set_maps(struct perf_script *script)
1694 struct perf_evlist *evlist = script->session->evlist;
1696 if (!script->cpus || !script->threads)
1699 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1702 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1704 if (perf_evlist__alloc_stats(evlist, true))
1707 script->allocated = true;
1712 int process_thread_map_event(struct perf_tool *tool,
1713 union perf_event *event,
1714 struct perf_session *session __maybe_unused)
1716 struct perf_script *script = container_of(tool, struct perf_script, tool);
1718 if (script->threads) {
1719 pr_warning("Extra thread map event, ignoring.\n");
1723 script->threads = thread_map__new_event(&event->thread_map);
1724 if (!script->threads)
1727 return set_maps(script);
1731 int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1732 union perf_event *event,
1733 struct perf_session *session __maybe_unused)
1735 struct perf_script *script = container_of(tool, struct perf_script, tool);
1738 pr_warning("Extra cpu map event, ignoring.\n");
1742 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1746 return set_maps(script);
1749 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1751 bool show_full_info = false;
1752 bool header = false;
1753 bool header_only = false;
1754 bool script_started = false;
1755 char *rec_script_path = NULL;
1756 char *rep_script_path = NULL;
1757 struct perf_session *session;
1758 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
1759 char *script_path = NULL;
1760 const char **__argv;
1762 struct perf_script script = {
1764 .sample = process_sample_event,
1765 .mmap = perf_event__process_mmap,
1766 .mmap2 = perf_event__process_mmap2,
1767 .comm = perf_event__process_comm,
1768 .exit = perf_event__process_exit,
1769 .fork = perf_event__process_fork,
1770 .attr = process_attr,
1771 .tracing_data = perf_event__process_tracing_data,
1772 .build_id = perf_event__process_build_id,
1773 .id_index = perf_event__process_id_index,
1774 .auxtrace_info = perf_event__process_auxtrace_info,
1775 .auxtrace = perf_event__process_auxtrace,
1776 .auxtrace_error = perf_event__process_auxtrace_error,
1777 .thread_map = process_thread_map_event,
1778 .cpu_map = process_cpu_map_event,
1779 .ordered_events = true,
1780 .ordering_requires_timestamps = true,
1783 struct perf_data_file file = {
1784 .mode = PERF_DATA_MODE_READ,
1786 const struct option options[] = {
1787 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1788 "dump raw trace in ASCII"),
1789 OPT_INCR('v', "verbose", &verbose,
1790 "be more verbose (show symbol address, etc)"),
1791 OPT_BOOLEAN('L', "Latency", &latency_format,
1792 "show latency attributes (irqs/preemption disabled, etc)"),
1793 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1794 list_available_scripts),
1795 OPT_CALLBACK('s', "script", NULL, "name",
1796 "script file name (lang:script name, script name, or *)",
1798 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1799 "generate perf-script.xx script in specified language"),
1800 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1801 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1802 "do various checks like samples ordering and lost events"),
1803 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1804 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
1805 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1806 "file", "vmlinux pathname"),
1807 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1808 "file", "kallsyms pathname"),
1809 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1810 "When printing symbols do not display call chain"),
1811 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1812 "Look for files with symbols relative to this directory"),
1813 OPT_CALLBACK('F', "fields", NULL, "str",
1814 "comma separated output fields prepend with 'type:'. "
1815 "Valid types: hw,sw,trace,raw. "
1816 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1817 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
1818 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1819 "system-wide collection from all CPUs"),
1820 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1821 "only consider these symbols"),
1822 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1823 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1824 "only display events for these comms"),
1825 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1826 "only consider symbols in these pids"),
1827 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1828 "only consider symbols in these tids"),
1829 OPT_BOOLEAN('I', "show-info", &show_full_info,
1830 "display extended information from perf.data file"),
1831 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1832 "Show the path of [kernel.kallsyms]"),
1833 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1834 "Show the fork/comm/exit events"),
1835 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1836 "Show the mmap events"),
1837 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1838 "Show context switch events (if recorded)"),
1839 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
1840 OPT_BOOLEAN(0, "ns", &nanosecs,
1841 "Use 9 decimal places when displaying time"),
1842 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1843 "Instruction Tracing options",
1844 itrace_parse_synth_opts),
1845 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1846 "Show full source file name path for source lines"),
1847 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1848 "Enable symbol demangling"),
1849 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1850 "Enable kernel symbol demangling"),
1854 const char * const script_subcommands[] = { "record", "report", NULL };
1855 const char *script_usage[] = {
1856 "perf script [<options>]",
1857 "perf script [<options>] record <script> [<record-options>] <command>",
1858 "perf script [<options>] report <script> [script-args]",
1859 "perf script [<options>] <script> [<record-options>] <command>",
1860 "perf script [<options>] <top-script> [script-args]",
1866 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
1867 PARSE_OPT_STOP_AT_NON_OPTION);
1869 file.path = input_name;
1871 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1872 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1873 if (!rec_script_path)
1874 return cmd_record(argc, argv, NULL);
1877 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1878 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1879 if (!rep_script_path) {
1881 "Please specify a valid report script"
1882 "(see 'perf script -l' for listing)\n");
1887 if (itrace_synth_opts.callchain &&
1888 itrace_synth_opts.callchain_sz > scripting_max_stack)
1889 scripting_max_stack = itrace_synth_opts.callchain_sz;
1891 /* make sure PERF_EXEC_PATH is set for scripts */
1892 set_argv_exec_path(get_argv_exec_path());
1894 if (argc && !script_name && !rec_script_path && !rep_script_path) {
1899 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1900 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1902 if (!rec_script_path && !rep_script_path) {
1903 usage_with_options_msg(script_usage, options,
1904 "Couldn't find script `%s'\n\n See perf"
1905 " script -l for available scripts.\n", argv[0]);
1908 if (is_top_script(argv[0])) {
1909 rep_args = argc - 1;
1913 rep_args = has_required_arg(rep_script_path);
1914 rec_args = (argc - 1) - rep_args;
1916 usage_with_options_msg(script_usage, options,
1917 "`%s' script requires options."
1918 "\n\n See perf script -l for available "
1919 "scripts and options.\n", argv[0]);
1923 if (pipe(live_pipe) < 0) {
1924 perror("failed to create pipe");
1930 perror("failed to fork");
1937 dup2(live_pipe[1], 1);
1938 close(live_pipe[0]);
1940 if (is_top_script(argv[0])) {
1942 } else if (!system_wide) {
1943 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1949 __argv = malloc((argc + 6) * sizeof(const char *));
1951 pr_err("malloc failed\n");
1956 __argv[j++] = "/bin/sh";
1957 __argv[j++] = rec_script_path;
1963 for (i = rep_args + 1; i < argc; i++)
1964 __argv[j++] = argv[i];
1967 execvp("/bin/sh", (char **)__argv);
1972 dup2(live_pipe[0], 0);
1973 close(live_pipe[1]);
1975 __argv = malloc((argc + 4) * sizeof(const char *));
1977 pr_err("malloc failed\n");
1983 __argv[j++] = "/bin/sh";
1984 __argv[j++] = rep_script_path;
1985 for (i = 1; i < rep_args + 1; i++)
1986 __argv[j++] = argv[i];
1991 execvp("/bin/sh", (char **)__argv);
1996 if (rec_script_path)
1997 script_path = rec_script_path;
1998 if (rep_script_path)
1999 script_path = rep_script_path;
2004 if (!rec_script_path)
2005 system_wide = false;
2006 else if (!system_wide) {
2007 if (have_cmd(argc - 1, &argv[1]) != 0) {
2013 __argv = malloc((argc + 2) * sizeof(const char *));
2015 pr_err("malloc failed\n");
2020 __argv[j++] = "/bin/sh";
2021 __argv[j++] = script_path;
2024 for (i = 2; i < argc; i++)
2025 __argv[j++] = argv[i];
2028 execvp("/bin/sh", (char **)__argv);
2036 session = perf_session__new(&file, false, &script.tool);
2037 if (session == NULL)
2040 if (header || header_only) {
2041 perf_session__fprintf_info(session, stdout, show_full_info);
2046 if (symbol__init(&session->header.env) < 0)
2049 script.session = session;
2050 script__setup_sample_type(&script);
2052 session->itrace_synth_opts = &itrace_synth_opts;
2055 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2061 symbol_conf.use_callchain = true;
2063 symbol_conf.use_callchain = false;
2065 if (session->tevent.pevent &&
2066 pevent_set_function_resolver(session->tevent.pevent,
2067 machine__resolve_kernel_addr,
2068 &session->machines.host) < 0) {
2069 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2073 if (generate_script_lang) {
2074 struct stat perf_stat;
2077 if (output_set_by_user()) {
2079 "custom fields not supported for generated scripts");
2084 input = open(file.path, O_RDONLY); /* input_name */
2087 perror("failed to open file");
2091 err = fstat(input, &perf_stat);
2093 perror("failed to stat file");
2097 if (!perf_stat.st_size) {
2098 fprintf(stderr, "zero-sized file, nothing to do!\n");
2102 scripting_ops = script_spec__lookup(generate_script_lang);
2103 if (!scripting_ops) {
2104 fprintf(stderr, "invalid language specifier");
2109 err = scripting_ops->generate_script(session->tevent.pevent,
2115 err = scripting_ops->start_script(script_name, argc, argv);
2118 pr_debug("perf script started with script %s\n\n", script_name);
2119 script_started = true;
2123 err = perf_session__check_output_opt(session);
2127 err = __cmd_script(&script);
2132 perf_evlist__free_stats(session->evlist);
2133 perf_session__delete(session);
2136 cleanup_scripting();