4 #include "util/cache.h"
5 #include "util/debug.h"
6 #include "util/exec_cmd.h"
7 #include "util/header.h"
8 #include "util/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 <linux/bitmap.h>
23 static char const *script_name;
24 static char const *generate_script_lang;
25 static bool debug_mode;
26 static u64 last_timestamp;
27 static u64 nr_unordered;
28 static bool no_callchain;
29 static bool latency_format;
30 static bool system_wide;
31 static bool print_flags;
33 static const char *cpu_list;
34 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
36 enum perf_output_field {
37 PERF_OUTPUT_COMM = 1U << 0,
38 PERF_OUTPUT_TID = 1U << 1,
39 PERF_OUTPUT_PID = 1U << 2,
40 PERF_OUTPUT_TIME = 1U << 3,
41 PERF_OUTPUT_CPU = 1U << 4,
42 PERF_OUTPUT_EVNAME = 1U << 5,
43 PERF_OUTPUT_TRACE = 1U << 6,
44 PERF_OUTPUT_IP = 1U << 7,
45 PERF_OUTPUT_SYM = 1U << 8,
46 PERF_OUTPUT_DSO = 1U << 9,
47 PERF_OUTPUT_ADDR = 1U << 10,
48 PERF_OUTPUT_SYMOFFSET = 1U << 11,
49 PERF_OUTPUT_SRCLINE = 1U << 12,
50 PERF_OUTPUT_PERIOD = 1U << 13,
51 PERF_OUTPUT_IREGS = 1U << 14,
54 struct output_option {
56 enum perf_output_field field;
57 } all_output_options[] = {
58 {.str = "comm", .field = PERF_OUTPUT_COMM},
59 {.str = "tid", .field = PERF_OUTPUT_TID},
60 {.str = "pid", .field = PERF_OUTPUT_PID},
61 {.str = "time", .field = PERF_OUTPUT_TIME},
62 {.str = "cpu", .field = PERF_OUTPUT_CPU},
63 {.str = "event", .field = PERF_OUTPUT_EVNAME},
64 {.str = "trace", .field = PERF_OUTPUT_TRACE},
65 {.str = "ip", .field = PERF_OUTPUT_IP},
66 {.str = "sym", .field = PERF_OUTPUT_SYM},
67 {.str = "dso", .field = PERF_OUTPUT_DSO},
68 {.str = "addr", .field = PERF_OUTPUT_ADDR},
69 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
70 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
71 {.str = "period", .field = PERF_OUTPUT_PERIOD},
72 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
75 /* default set to maintain compatibility with current format */
79 unsigned int print_ip_opts;
82 } output[PERF_TYPE_MAX] = {
84 [PERF_TYPE_HARDWARE] = {
87 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
88 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
89 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
90 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
93 .invalid_fields = PERF_OUTPUT_TRACE,
96 [PERF_TYPE_SOFTWARE] = {
99 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
100 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
101 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
102 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
105 .invalid_fields = PERF_OUTPUT_TRACE,
108 [PERF_TYPE_TRACEPOINT] = {
111 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
112 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
113 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
119 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
120 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
121 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
122 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
125 .invalid_fields = PERF_OUTPUT_TRACE,
129 static bool output_set_by_user(void)
132 for (j = 0; j < PERF_TYPE_MAX; ++j) {
133 if (output[j].user_set)
139 static const char *output_field2str(enum perf_output_field field)
141 int i, imax = ARRAY_SIZE(all_output_options);
142 const char *str = "";
144 for (i = 0; i < imax; ++i) {
145 if (all_output_options[i].field == field) {
146 str = all_output_options[i].str;
153 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
155 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
156 u64 sample_type, const char *sample_msg,
157 enum perf_output_field field,
160 struct perf_event_attr *attr = &evsel->attr;
161 int type = attr->type;
164 if (attr->sample_type & sample_type)
167 if (output[type].user_set) {
170 evname = perf_evsel__name(evsel);
171 pr_err("Samples for '%s' event do not have %s attribute set. "
172 "Cannot print '%s' field.\n",
173 evname, sample_msg, output_field2str(field));
177 /* user did not ask for it explicitly so remove from the default list */
178 output[type].fields &= ~field;
179 evname = perf_evsel__name(evsel);
180 pr_debug("Samples for '%s' event do not have %s attribute set. "
181 "Skipping '%s' field.\n",
182 evname, sample_msg, output_field2str(field));
187 static int perf_evsel__check_stype(struct perf_evsel *evsel,
188 u64 sample_type, const char *sample_msg,
189 enum perf_output_field field)
191 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
195 static int perf_evsel__check_attr(struct perf_evsel *evsel,
196 struct perf_session *session)
198 struct perf_event_attr *attr = &evsel->attr;
201 allow_user_set = perf_header__has_feat(&session->header,
204 if (PRINT_FIELD(TRACE) &&
205 !perf_session__has_traces(session, "record -R"))
208 if (PRINT_FIELD(IP)) {
209 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
214 if (PRINT_FIELD(ADDR) &&
215 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
216 PERF_OUTPUT_ADDR, allow_user_set))
219 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
220 pr_err("Display of symbols requested but neither sample IP nor "
221 "sample address\nis selected. Hence, no addresses to convert "
225 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
226 pr_err("Display of offsets requested but symbol is not"
230 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
231 pr_err("Display of DSO requested but neither sample IP nor "
232 "sample address\nis selected. Hence, no addresses to convert "
236 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
237 pr_err("Display of source line number requested but sample IP is not\n"
238 "selected. Hence, no address to lookup the source line number.\n");
242 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
243 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
244 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
247 if (PRINT_FIELD(TIME) &&
248 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
252 if (PRINT_FIELD(CPU) &&
253 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
254 PERF_OUTPUT_CPU, allow_user_set))
257 if (PRINT_FIELD(PERIOD) &&
258 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
262 if (PRINT_FIELD(IREGS) &&
263 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
270 static void set_print_ip_opts(struct perf_event_attr *attr)
272 unsigned int type = attr->type;
274 output[type].print_ip_opts = 0;
276 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
278 if (PRINT_FIELD(SYM))
279 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
281 if (PRINT_FIELD(DSO))
282 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
284 if (PRINT_FIELD(SYMOFFSET))
285 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
287 if (PRINT_FIELD(SRCLINE))
288 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
292 * verify all user requested events exist and the samples
293 * have the expected data
295 static int perf_session__check_output_opt(struct perf_session *session)
298 struct perf_evsel *evsel;
300 for (j = 0; j < PERF_TYPE_MAX; ++j) {
301 evsel = perf_session__find_first_evtype(session, j);
304 * even if fields is set to 0 (ie., show nothing) event must
305 * exist if user explicitly includes it on the command line
307 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
308 pr_err("%s events do not exist. "
309 "Remove corresponding -f option to proceed.\n",
314 if (evsel && output[j].fields &&
315 perf_evsel__check_attr(evsel, session))
321 set_print_ip_opts(&evsel->attr);
325 bool use_callchain = false;
327 evlist__for_each(session->evlist, evsel) {
328 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
329 use_callchain = true;
334 symbol_conf.use_callchain = false;
338 * set default for tracepoints to print symbols only
339 * if callchains are present
341 if (symbol_conf.use_callchain &&
342 !output[PERF_TYPE_TRACEPOINT].user_set) {
343 struct perf_event_attr *attr;
345 j = PERF_TYPE_TRACEPOINT;
346 evsel = perf_session__find_first_evtype(session, j);
352 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
353 output[j].fields |= PERF_OUTPUT_IP;
354 output[j].fields |= PERF_OUTPUT_SYM;
355 output[j].fields |= PERF_OUTPUT_DSO;
356 set_print_ip_opts(attr);
364 static void print_sample_iregs(union perf_event *event __maybe_unused,
365 struct perf_sample *sample,
366 struct thread *thread __maybe_unused,
367 struct perf_event_attr *attr)
369 struct regs_dump *regs = &sample->intr_regs;
370 uint64_t mask = attr->sample_regs_intr;
376 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
377 u64 val = regs->regs[i++];
378 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
382 static void print_sample_start(struct perf_sample *sample,
383 struct thread *thread,
384 struct perf_evsel *evsel)
386 struct perf_event_attr *attr = &evsel->attr;
389 unsigned long long nsecs;
391 if (PRINT_FIELD(COMM)) {
393 printf("%8.8s ", thread__comm_str(thread));
394 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
395 printf("%s ", thread__comm_str(thread));
397 printf("%16s ", thread__comm_str(thread));
400 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
401 printf("%5d/%-5d ", sample->pid, sample->tid);
402 else if (PRINT_FIELD(PID))
403 printf("%5d ", sample->pid);
404 else if (PRINT_FIELD(TID))
405 printf("%5d ", sample->tid);
407 if (PRINT_FIELD(CPU)) {
409 printf("%3d ", sample->cpu);
411 printf("[%03d] ", sample->cpu);
414 if (PRINT_FIELD(TIME)) {
415 nsecs = sample->time;
416 secs = nsecs / NSECS_PER_SEC;
417 nsecs -= secs * NSECS_PER_SEC;
418 usecs = nsecs / NSECS_PER_USEC;
420 printf("%5lu.%09llu: ", secs, nsecs);
422 printf("%5lu.%06lu: ", secs, usecs);
426 static void print_sample_addr(union perf_event *event,
427 struct perf_sample *sample,
428 struct thread *thread,
429 struct perf_event_attr *attr)
431 struct addr_location al;
433 printf("%16" PRIx64, sample->addr);
435 if (!sample_addr_correlates_sym(attr))
438 perf_event__preprocess_sample_addr(event, sample, thread, &al);
440 if (PRINT_FIELD(SYM)) {
442 if (PRINT_FIELD(SYMOFFSET))
443 symbol__fprintf_symname_offs(al.sym, &al, stdout);
445 symbol__fprintf_symname(al.sym, stdout);
448 if (PRINT_FIELD(DSO)) {
450 map__fprintf_dsoname(al.map, stdout);
455 static void print_sample_bts(union perf_event *event,
456 struct perf_sample *sample,
457 struct perf_evsel *evsel,
458 struct thread *thread,
459 struct addr_location *al)
461 struct perf_event_attr *attr = &evsel->attr;
462 bool print_srcline_last = false;
464 /* print branch_from information */
465 if (PRINT_FIELD(IP)) {
466 unsigned int print_opts = output[attr->type].print_ip_opts;
468 if (symbol_conf.use_callchain && sample->callchain) {
472 if (print_opts & PRINT_IP_OPT_SRCLINE) {
473 print_srcline_last = true;
474 print_opts &= ~PRINT_IP_OPT_SRCLINE;
477 perf_evsel__print_ip(evsel, sample, al, print_opts,
478 PERF_MAX_STACK_DEPTH);
481 /* print branch_to information */
482 if (PRINT_FIELD(ADDR) ||
483 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
484 !output[attr->type].user_set)) {
486 print_sample_addr(event, sample, thread, attr);
489 if (print_srcline_last)
490 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
495 static void print_sample_flags(u32 flags)
497 const char *chars = PERF_IP_FLAG_CHARS;
498 const int n = strlen(PERF_IP_FLAG_CHARS);
502 for (i = 0; i < n; i++, flags >>= 1) {
504 str[pos++] = chars[i];
506 for (; i < 32; i++, flags >>= 1) {
511 printf(" %-4s ", str);
514 static void process_event(union perf_event *event, struct perf_sample *sample,
515 struct perf_evsel *evsel, struct addr_location *al)
517 struct thread *thread = al->thread;
518 struct perf_event_attr *attr = &evsel->attr;
520 if (output[attr->type].fields == 0)
523 print_sample_start(sample, thread, evsel);
525 if (PRINT_FIELD(PERIOD))
526 printf("%10" PRIu64 " ", sample->period);
528 if (PRINT_FIELD(EVNAME)) {
529 const char *evname = perf_evsel__name(evsel);
530 printf("%s: ", evname ? evname : "[unknown]");
534 print_sample_flags(sample->flags);
536 if (is_bts_event(attr)) {
537 print_sample_bts(event, sample, evsel, thread, al);
541 if (PRINT_FIELD(TRACE))
542 event_format__print(evsel->tp_format, sample->cpu,
543 sample->raw_data, sample->raw_size);
544 if (PRINT_FIELD(ADDR))
545 print_sample_addr(event, sample, thread, attr);
547 if (PRINT_FIELD(IP)) {
548 if (!symbol_conf.use_callchain)
553 perf_evsel__print_ip(evsel, sample, al,
554 output[attr->type].print_ip_opts,
555 PERF_MAX_STACK_DEPTH);
558 if (PRINT_FIELD(IREGS))
559 print_sample_iregs(event, sample, thread, attr);
564 static int default_start_script(const char *script __maybe_unused,
565 int argc __maybe_unused,
566 const char **argv __maybe_unused)
571 static int default_flush_script(void)
576 static int default_stop_script(void)
581 static int default_generate_script(struct pevent *pevent __maybe_unused,
582 const char *outfile __maybe_unused)
587 static struct scripting_ops default_scripting_ops = {
588 .start_script = default_start_script,
589 .flush_script = default_flush_script,
590 .stop_script = default_stop_script,
591 .process_event = process_event,
592 .generate_script = default_generate_script,
595 static struct scripting_ops *scripting_ops;
597 static void setup_scripting(void)
599 setup_perl_scripting();
600 setup_python_scripting();
602 scripting_ops = &default_scripting_ops;
605 static int flush_scripting(void)
607 return scripting_ops->flush_script();
610 static int cleanup_scripting(void)
612 pr_debug("\nperf script stopped\n");
614 return scripting_ops->stop_script();
617 static int process_sample_event(struct perf_tool *tool __maybe_unused,
618 union perf_event *event,
619 struct perf_sample *sample,
620 struct perf_evsel *evsel,
621 struct machine *machine)
623 struct addr_location al;
626 if (sample->time < last_timestamp) {
627 pr_err("Samples misordered, previous: %" PRIu64
628 " this: %" PRIu64 "\n", last_timestamp,
632 last_timestamp = sample->time;
636 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
637 pr_err("problem processing %d event, skipping it.\n",
645 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
648 scripting_ops->process_event(event, sample, evsel, &al);
650 addr_location__put(&al);
655 struct perf_tool tool;
656 struct perf_session *session;
657 bool show_task_events;
658 bool show_mmap_events;
659 bool show_switch_events;
662 static int process_attr(struct perf_tool *tool, union perf_event *event,
663 struct perf_evlist **pevlist)
665 struct perf_script *scr = container_of(tool, struct perf_script, tool);
666 struct perf_evlist *evlist;
667 struct perf_evsel *evsel, *pos;
670 err = perf_event__process_attr(tool, event, pevlist);
675 evsel = perf_evlist__last(*pevlist);
677 if (evsel->attr.type >= PERF_TYPE_MAX)
680 evlist__for_each(evlist, pos) {
681 if (pos->attr.type == evsel->attr.type && pos != evsel)
685 set_print_ip_opts(&evsel->attr);
687 return perf_evsel__check_attr(evsel, scr->session);
690 static int process_comm_event(struct perf_tool *tool,
691 union perf_event *event,
692 struct perf_sample *sample,
693 struct machine *machine)
695 struct thread *thread;
696 struct perf_script *script = container_of(tool, struct perf_script, tool);
697 struct perf_session *session = script->session;
698 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
701 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
702 if (thread == NULL) {
703 pr_debug("problem processing COMM event, skipping it.\n");
707 if (perf_event__process_comm(tool, event, sample, machine) < 0)
710 if (!evsel->attr.sample_id_all) {
713 sample->tid = event->comm.tid;
714 sample->pid = event->comm.pid;
716 print_sample_start(sample, thread, evsel);
717 perf_event__fprintf(event, stdout);
724 static int process_fork_event(struct perf_tool *tool,
725 union perf_event *event,
726 struct perf_sample *sample,
727 struct machine *machine)
729 struct thread *thread;
730 struct perf_script *script = container_of(tool, struct perf_script, tool);
731 struct perf_session *session = script->session;
732 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
734 if (perf_event__process_fork(tool, event, sample, machine) < 0)
737 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
738 if (thread == NULL) {
739 pr_debug("problem processing FORK event, skipping it.\n");
743 if (!evsel->attr.sample_id_all) {
745 sample->time = event->fork.time;
746 sample->tid = event->fork.tid;
747 sample->pid = event->fork.pid;
749 print_sample_start(sample, thread, evsel);
750 perf_event__fprintf(event, stdout);
755 static int process_exit_event(struct perf_tool *tool,
756 union perf_event *event,
757 struct perf_sample *sample,
758 struct machine *machine)
761 struct thread *thread;
762 struct perf_script *script = container_of(tool, struct perf_script, tool);
763 struct perf_session *session = script->session;
764 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
766 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
767 if (thread == NULL) {
768 pr_debug("problem processing EXIT event, skipping it.\n");
772 if (!evsel->attr.sample_id_all) {
775 sample->tid = event->fork.tid;
776 sample->pid = event->fork.pid;
778 print_sample_start(sample, thread, evsel);
779 perf_event__fprintf(event, stdout);
781 if (perf_event__process_exit(tool, event, sample, machine) < 0)
788 static int process_mmap_event(struct perf_tool *tool,
789 union perf_event *event,
790 struct perf_sample *sample,
791 struct machine *machine)
793 struct thread *thread;
794 struct perf_script *script = container_of(tool, struct perf_script, tool);
795 struct perf_session *session = script->session;
796 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
798 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
801 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
802 if (thread == NULL) {
803 pr_debug("problem processing MMAP event, skipping it.\n");
807 if (!evsel->attr.sample_id_all) {
810 sample->tid = event->mmap.tid;
811 sample->pid = event->mmap.pid;
813 print_sample_start(sample, thread, evsel);
814 perf_event__fprintf(event, stdout);
819 static int process_mmap2_event(struct perf_tool *tool,
820 union perf_event *event,
821 struct perf_sample *sample,
822 struct machine *machine)
824 struct thread *thread;
825 struct perf_script *script = container_of(tool, struct perf_script, tool);
826 struct perf_session *session = script->session;
827 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
829 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
832 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
833 if (thread == NULL) {
834 pr_debug("problem processing MMAP2 event, skipping it.\n");
838 if (!evsel->attr.sample_id_all) {
841 sample->tid = event->mmap2.tid;
842 sample->pid = event->mmap2.pid;
844 print_sample_start(sample, thread, evsel);
845 perf_event__fprintf(event, stdout);
850 static int process_switch_event(struct perf_tool *tool,
851 union perf_event *event,
852 struct perf_sample *sample,
853 struct machine *machine)
855 struct thread *thread;
856 struct perf_script *script = container_of(tool, struct perf_script, tool);
857 struct perf_session *session = script->session;
858 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
860 if (perf_event__process_switch(tool, event, sample, machine) < 0)
863 thread = machine__findnew_thread(machine, sample->pid,
865 if (thread == NULL) {
866 pr_debug("problem processing SWITCH event, skipping it.\n");
870 print_sample_start(sample, thread, evsel);
871 perf_event__fprintf(event, stdout);
876 static void sig_handler(int sig __maybe_unused)
881 static int __cmd_script(struct perf_script *script)
885 signal(SIGINT, sig_handler);
887 /* override event processing functions */
888 if (script->show_task_events) {
889 script->tool.comm = process_comm_event;
890 script->tool.fork = process_fork_event;
891 script->tool.exit = process_exit_event;
893 if (script->show_mmap_events) {
894 script->tool.mmap = process_mmap_event;
895 script->tool.mmap2 = process_mmap2_event;
897 if (script->show_switch_events)
898 script->tool.context_switch = process_switch_event;
900 ret = perf_session__process_events(script->session);
903 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
909 struct list_head node;
910 struct scripting_ops *ops;
914 static LIST_HEAD(script_specs);
916 static struct script_spec *script_spec__new(const char *spec,
917 struct scripting_ops *ops)
919 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
922 strcpy(s->spec, spec);
929 static void script_spec__add(struct script_spec *s)
931 list_add_tail(&s->node, &script_specs);
934 static struct script_spec *script_spec__find(const char *spec)
936 struct script_spec *s;
938 list_for_each_entry(s, &script_specs, node)
939 if (strcasecmp(s->spec, spec) == 0)
944 static struct script_spec *script_spec__findnew(const char *spec,
945 struct scripting_ops *ops)
947 struct script_spec *s = script_spec__find(spec);
952 s = script_spec__new(spec, ops);
961 int script_spec_register(const char *spec, struct scripting_ops *ops)
963 struct script_spec *s;
965 s = script_spec__find(spec);
969 s = script_spec__findnew(spec, ops);
976 static struct scripting_ops *script_spec__lookup(const char *spec)
978 struct script_spec *s = script_spec__find(spec);
985 static void list_available_languages(void)
987 struct script_spec *s;
989 fprintf(stderr, "\n");
990 fprintf(stderr, "Scripting language extensions (used in "
991 "perf script -s [spec:]script.[spec]):\n\n");
993 list_for_each_entry(s, &script_specs, node)
994 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
996 fprintf(stderr, "\n");
999 static int parse_scriptname(const struct option *opt __maybe_unused,
1000 const char *str, int unset __maybe_unused)
1002 char spec[PATH_MAX];
1003 const char *script, *ext;
1006 if (strcmp(str, "lang") == 0) {
1007 list_available_languages();
1011 script = strchr(str, ':');
1014 if (len >= PATH_MAX) {
1015 fprintf(stderr, "invalid language specifier");
1018 strncpy(spec, str, len);
1020 scripting_ops = script_spec__lookup(spec);
1021 if (!scripting_ops) {
1022 fprintf(stderr, "invalid language specifier");
1028 ext = strrchr(script, '.');
1030 fprintf(stderr, "invalid script extension");
1033 scripting_ops = script_spec__lookup(++ext);
1034 if (!scripting_ops) {
1035 fprintf(stderr, "invalid script extension");
1040 script_name = strdup(script);
1045 static int parse_output_fields(const struct option *opt __maybe_unused,
1046 const char *arg, int unset __maybe_unused)
1049 int i, imax = ARRAY_SIZE(all_output_options);
1052 char *str = strdup(arg);
1058 /* first word can state for which event type the user is specifying
1059 * the fields. If no type exists, the specified fields apply to all
1060 * event types found in the file minus the invalid fields for a type.
1062 tok = strchr(str, ':');
1066 if (!strcmp(str, "hw"))
1067 type = PERF_TYPE_HARDWARE;
1068 else if (!strcmp(str, "sw"))
1069 type = PERF_TYPE_SOFTWARE;
1070 else if (!strcmp(str, "trace"))
1071 type = PERF_TYPE_TRACEPOINT;
1072 else if (!strcmp(str, "raw"))
1073 type = PERF_TYPE_RAW;
1075 fprintf(stderr, "Invalid event type in field string.\n");
1080 if (output[type].user_set)
1081 pr_warning("Overriding previous field request for %s events.\n",
1084 output[type].fields = 0;
1085 output[type].user_set = true;
1086 output[type].wildcard_set = false;
1090 if (strlen(str) == 0) {
1092 "Cannot set fields to 'none' for all event types.\n");
1097 if (output_set_by_user())
1098 pr_warning("Overriding previous field request for all events.\n");
1100 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1101 output[j].fields = 0;
1102 output[j].user_set = true;
1103 output[j].wildcard_set = true;
1107 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
1108 for (i = 0; i < imax; ++i) {
1109 if (strcmp(tok, all_output_options[i].str) == 0)
1112 if (i == imax && strcmp(tok, "flags") == 0) {
1117 fprintf(stderr, "Invalid field requested.\n");
1123 /* add user option to all events types for
1126 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1127 if (output[j].invalid_fields & all_output_options[i].field) {
1128 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1129 all_output_options[i].str, event_type(j));
1131 output[j].fields |= all_output_options[i].field;
1134 if (output[type].invalid_fields & all_output_options[i].field) {
1135 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1136 all_output_options[i].str, event_type(type));
1141 output[type].fields |= all_output_options[i].field;
1146 if (output[type].fields == 0) {
1147 pr_debug("No fields requested for %s type. "
1148 "Events will not be displayed.\n", event_type(type));
1157 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1158 static int is_directory(const char *base_path, const struct dirent *dent)
1160 char path[PATH_MAX];
1163 sprintf(path, "%s/%s", base_path, dent->d_name);
1164 if (stat(path, &st))
1167 return S_ISDIR(st.st_mode);
1170 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
1171 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1173 if ((lang_dirent.d_type == DT_DIR || \
1174 (lang_dirent.d_type == DT_UNKNOWN && \
1175 is_directory(scripts_path, &lang_dirent))) && \
1176 (strcmp(lang_dirent.d_name, ".")) && \
1177 (strcmp(lang_dirent.d_name, "..")))
1179 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
1180 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1182 if (script_dirent.d_type != DT_DIR && \
1183 (script_dirent.d_type != DT_UNKNOWN || \
1184 !is_directory(lang_path, &script_dirent)))
1187 #define RECORD_SUFFIX "-record"
1188 #define REPORT_SUFFIX "-report"
1190 struct script_desc {
1191 struct list_head node;
1197 static LIST_HEAD(script_descs);
1199 static struct script_desc *script_desc__new(const char *name)
1201 struct script_desc *s = zalloc(sizeof(*s));
1203 if (s != NULL && name)
1204 s->name = strdup(name);
1209 static void script_desc__delete(struct script_desc *s)
1212 zfree(&s->half_liner);
1217 static void script_desc__add(struct script_desc *s)
1219 list_add_tail(&s->node, &script_descs);
1222 static struct script_desc *script_desc__find(const char *name)
1224 struct script_desc *s;
1226 list_for_each_entry(s, &script_descs, node)
1227 if (strcasecmp(s->name, name) == 0)
1232 static struct script_desc *script_desc__findnew(const char *name)
1234 struct script_desc *s = script_desc__find(name);
1239 s = script_desc__new(name);
1241 goto out_delete_desc;
1243 script_desc__add(s);
1248 script_desc__delete(s);
1253 static const char *ends_with(const char *str, const char *suffix)
1255 size_t suffix_len = strlen(suffix);
1256 const char *p = str;
1258 if (strlen(str) > suffix_len) {
1259 p = str + strlen(str) - suffix_len;
1260 if (!strncmp(p, suffix, suffix_len))
1267 static int read_script_info(struct script_desc *desc, const char *filename)
1269 char line[BUFSIZ], *p;
1272 fp = fopen(filename, "r");
1276 while (fgets(line, sizeof(line), fp)) {
1283 if (strlen(p) && *p == '!')
1287 if (strlen(p) && p[strlen(p) - 1] == '\n')
1288 p[strlen(p) - 1] = '\0';
1290 if (!strncmp(p, "description:", strlen("description:"))) {
1291 p += strlen("description:");
1292 desc->half_liner = strdup(ltrim(p));
1296 if (!strncmp(p, "args:", strlen("args:"))) {
1297 p += strlen("args:");
1298 desc->args = strdup(ltrim(p));
1308 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1310 char *script_root, *str;
1312 script_root = strdup(script_dirent->d_name);
1316 str = (char *)ends_with(script_root, suffix);
1326 static int list_available_scripts(const struct option *opt __maybe_unused,
1327 const char *s __maybe_unused,
1328 int unset __maybe_unused)
1330 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1331 char scripts_path[MAXPATHLEN];
1332 DIR *scripts_dir, *lang_dir;
1333 char script_path[MAXPATHLEN];
1334 char lang_path[MAXPATHLEN];
1335 struct script_desc *desc;
1336 char first_half[BUFSIZ];
1339 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1341 scripts_dir = opendir(scripts_path);
1345 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1346 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1347 lang_dirent.d_name);
1348 lang_dir = opendir(lang_path);
1352 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1353 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1355 desc = script_desc__findnew(script_root);
1356 snprintf(script_path, MAXPATHLEN, "%s/%s",
1357 lang_path, script_dirent.d_name);
1358 read_script_info(desc, script_path);
1364 fprintf(stdout, "List of available trace scripts:\n");
1365 list_for_each_entry(desc, &script_descs, node) {
1366 sprintf(first_half, "%s %s", desc->name,
1367 desc->args ? desc->args : "");
1368 fprintf(stdout, " %-36s %s\n", first_half,
1369 desc->half_liner ? desc->half_liner : "");
1376 * Some scripts specify the required events in their "xxx-record" file,
1377 * this function will check if the events in perf.data match those
1378 * mentioned in the "xxx-record".
1380 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1381 * which is covered well now. And new parsing code should be added to
1382 * cover the future complexing formats like event groups etc.
1384 static int check_ev_match(char *dir_name, char *scriptname,
1385 struct perf_session *session)
1387 char filename[MAXPATHLEN], evname[128];
1388 char line[BUFSIZ], *p;
1389 struct perf_evsel *pos;
1393 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1395 fp = fopen(filename, "r");
1399 while (fgets(line, sizeof(line), fp)) {
1405 p = strstr(p, "-e");
1411 len = strcspn(p, " \t");
1415 snprintf(evname, len + 1, "%s", p);
1418 evlist__for_each(session->evlist, pos) {
1419 if (!strcmp(perf_evsel__name(pos), evname)) {
1437 * Return -1 if none is found, otherwise the actual scripts number.
1439 * Currently the only user of this function is the script browser, which
1440 * will list all statically runnable scripts, select one, execute it and
1441 * show the output in a perf browser.
1443 int find_scripts(char **scripts_array, char **scripts_path_array)
1445 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1446 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
1447 DIR *scripts_dir, *lang_dir;
1448 struct perf_session *session;
1449 struct perf_data_file file = {
1451 .mode = PERF_DATA_MODE_READ,
1456 session = perf_session__new(&file, false, NULL);
1460 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1462 scripts_dir = opendir(scripts_path);
1464 perf_session__delete(session);
1468 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1469 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1470 lang_dirent.d_name);
1472 if (strstr(lang_path, "perl"))
1476 if (strstr(lang_path, "python"))
1480 lang_dir = opendir(lang_path);
1484 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1485 /* Skip those real time scripts: xxxtop.p[yl] */
1486 if (strstr(script_dirent.d_name, "top."))
1488 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1489 script_dirent.d_name);
1490 temp = strchr(script_dirent.d_name, '.');
1491 snprintf(scripts_array[i],
1492 (temp - script_dirent.d_name) + 1,
1493 "%s", script_dirent.d_name);
1495 if (check_ev_match(lang_path,
1496 scripts_array[i], session))
1504 closedir(scripts_dir);
1505 perf_session__delete(session);
1509 static char *get_script_path(const char *script_root, const char *suffix)
1511 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1512 char scripts_path[MAXPATHLEN];
1513 char script_path[MAXPATHLEN];
1514 DIR *scripts_dir, *lang_dir;
1515 char lang_path[MAXPATHLEN];
1516 char *__script_root;
1518 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1520 scripts_dir = opendir(scripts_path);
1524 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1525 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1526 lang_dirent.d_name);
1527 lang_dir = opendir(lang_path);
1531 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1532 __script_root = get_script_root(&script_dirent, suffix);
1533 if (__script_root && !strcmp(script_root, __script_root)) {
1534 free(__script_root);
1536 closedir(scripts_dir);
1537 snprintf(script_path, MAXPATHLEN, "%s/%s",
1538 lang_path, script_dirent.d_name);
1539 return strdup(script_path);
1541 free(__script_root);
1545 closedir(scripts_dir);
1550 static bool is_top_script(const char *script_path)
1552 return ends_with(script_path, "top") == NULL ? false : true;
1555 static int has_required_arg(char *script_path)
1557 struct script_desc *desc;
1561 desc = script_desc__new(NULL);
1563 if (read_script_info(desc, script_path))
1569 for (p = desc->args; *p; p++)
1573 script_desc__delete(desc);
1578 static int have_cmd(int argc, const char **argv)
1580 char **__argv = malloc(sizeof(const char *) * argc);
1583 pr_err("malloc failed\n");
1587 memcpy(__argv, argv, sizeof(const char *) * argc);
1588 argc = parse_options(argc, (const char **)__argv, record_options,
1589 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1592 system_wide = (argc == 0);
1597 static void script__setup_sample_type(struct perf_script *script)
1599 struct perf_session *session = script->session;
1600 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1602 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1603 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1604 (sample_type & PERF_SAMPLE_STACK_USER))
1605 callchain_param.record_mode = CALLCHAIN_DWARF;
1606 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1607 callchain_param.record_mode = CALLCHAIN_LBR;
1609 callchain_param.record_mode = CALLCHAIN_FP;
1613 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1615 bool show_full_info = false;
1616 bool header = false;
1617 bool header_only = false;
1618 bool script_started = false;
1619 char *rec_script_path = NULL;
1620 char *rep_script_path = NULL;
1621 struct perf_session *session;
1622 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
1623 char *script_path = NULL;
1624 const char **__argv;
1626 struct perf_script script = {
1628 .sample = process_sample_event,
1629 .mmap = perf_event__process_mmap,
1630 .mmap2 = perf_event__process_mmap2,
1631 .comm = perf_event__process_comm,
1632 .exit = perf_event__process_exit,
1633 .fork = perf_event__process_fork,
1634 .attr = process_attr,
1635 .tracing_data = perf_event__process_tracing_data,
1636 .build_id = perf_event__process_build_id,
1637 .id_index = perf_event__process_id_index,
1638 .auxtrace_info = perf_event__process_auxtrace_info,
1639 .auxtrace = perf_event__process_auxtrace,
1640 .auxtrace_error = perf_event__process_auxtrace_error,
1641 .ordered_events = true,
1642 .ordering_requires_timestamps = true,
1645 struct perf_data_file file = {
1646 .mode = PERF_DATA_MODE_READ,
1648 const struct option options[] = {
1649 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1650 "dump raw trace in ASCII"),
1651 OPT_INCR('v', "verbose", &verbose,
1652 "be more verbose (show symbol address, etc)"),
1653 OPT_BOOLEAN('L', "Latency", &latency_format,
1654 "show latency attributes (irqs/preemption disabled, etc)"),
1655 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1656 list_available_scripts),
1657 OPT_CALLBACK('s', "script", NULL, "name",
1658 "script file name (lang:script name, script name, or *)",
1660 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1661 "generate perf-script.xx script in specified language"),
1662 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1663 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1664 "do various checks like samples ordering and lost events"),
1665 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1666 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
1667 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1668 "file", "vmlinux pathname"),
1669 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1670 "file", "kallsyms pathname"),
1671 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1672 "When printing symbols do not display call chain"),
1673 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1674 "Look for files with symbols relative to this directory"),
1675 OPT_CALLBACK('F', "fields", NULL, "str",
1676 "comma separated output fields prepend with 'type:'. "
1677 "Valid types: hw,sw,trace,raw. "
1678 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1679 "addr,symoff,period,iregs,flags", parse_output_fields),
1680 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1681 "system-wide collection from all CPUs"),
1682 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1683 "only consider these symbols"),
1684 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1685 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1686 "only display events for these comms"),
1687 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1688 "only consider symbols in these pids"),
1689 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1690 "only consider symbols in these tids"),
1691 OPT_BOOLEAN('I', "show-info", &show_full_info,
1692 "display extended information from perf.data file"),
1693 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1694 "Show the path of [kernel.kallsyms]"),
1695 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1696 "Show the fork/comm/exit events"),
1697 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1698 "Show the mmap events"),
1699 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1700 "Show context switch events (if recorded)"),
1701 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
1702 OPT_BOOLEAN(0, "ns", &nanosecs,
1703 "Use 9 decimal places when displaying time"),
1704 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1705 "Instruction Tracing options",
1706 itrace_parse_synth_opts),
1707 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1708 "Show full source file name path for source lines"),
1709 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1710 "Enable symbol demangling"),
1711 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1712 "Enable kernel symbol demangling"),
1716 const char * const script_subcommands[] = { "record", "report", NULL };
1717 const char *script_usage[] = {
1718 "perf script [<options>]",
1719 "perf script [<options>] record <script> [<record-options>] <command>",
1720 "perf script [<options>] report <script> [script-args]",
1721 "perf script [<options>] <script> [<record-options>] <command>",
1722 "perf script [<options>] <top-script> [script-args]",
1728 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
1729 PARSE_OPT_STOP_AT_NON_OPTION);
1731 file.path = input_name;
1733 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1734 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1735 if (!rec_script_path)
1736 return cmd_record(argc, argv, NULL);
1739 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1740 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1741 if (!rep_script_path) {
1743 "Please specify a valid report script"
1744 "(see 'perf script -l' for listing)\n");
1749 /* make sure PERF_EXEC_PATH is set for scripts */
1750 perf_set_argv_exec_path(perf_exec_path());
1752 if (argc && !script_name && !rec_script_path && !rep_script_path) {
1757 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1758 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1760 if (!rec_script_path && !rep_script_path) {
1761 fprintf(stderr, " Couldn't find script %s\n\n See perf"
1762 " script -l for available scripts.\n", argv[0]);
1763 usage_with_options(script_usage, options);
1766 if (is_top_script(argv[0])) {
1767 rep_args = argc - 1;
1771 rep_args = has_required_arg(rep_script_path);
1772 rec_args = (argc - 1) - rep_args;
1774 fprintf(stderr, " %s script requires options."
1775 "\n\n See perf script -l for available "
1776 "scripts and options.\n", argv[0]);
1777 usage_with_options(script_usage, options);
1781 if (pipe(live_pipe) < 0) {
1782 perror("failed to create pipe");
1788 perror("failed to fork");
1795 dup2(live_pipe[1], 1);
1796 close(live_pipe[0]);
1798 if (is_top_script(argv[0])) {
1800 } else if (!system_wide) {
1801 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1807 __argv = malloc((argc + 6) * sizeof(const char *));
1809 pr_err("malloc failed\n");
1814 __argv[j++] = "/bin/sh";
1815 __argv[j++] = rec_script_path;
1821 for (i = rep_args + 1; i < argc; i++)
1822 __argv[j++] = argv[i];
1825 execvp("/bin/sh", (char **)__argv);
1830 dup2(live_pipe[0], 0);
1831 close(live_pipe[1]);
1833 __argv = malloc((argc + 4) * sizeof(const char *));
1835 pr_err("malloc failed\n");
1841 __argv[j++] = "/bin/sh";
1842 __argv[j++] = rep_script_path;
1843 for (i = 1; i < rep_args + 1; i++)
1844 __argv[j++] = argv[i];
1849 execvp("/bin/sh", (char **)__argv);
1854 if (rec_script_path)
1855 script_path = rec_script_path;
1856 if (rep_script_path)
1857 script_path = rep_script_path;
1862 if (!rec_script_path)
1863 system_wide = false;
1864 else if (!system_wide) {
1865 if (have_cmd(argc - 1, &argv[1]) != 0) {
1871 __argv = malloc((argc + 2) * sizeof(const char *));
1873 pr_err("malloc failed\n");
1878 __argv[j++] = "/bin/sh";
1879 __argv[j++] = script_path;
1882 for (i = 2; i < argc; i++)
1883 __argv[j++] = argv[i];
1886 execvp("/bin/sh", (char **)__argv);
1894 session = perf_session__new(&file, false, &script.tool);
1895 if (session == NULL)
1898 if (header || header_only) {
1899 perf_session__fprintf_info(session, stdout, show_full_info);
1904 if (symbol__init(&session->header.env) < 0)
1907 script.session = session;
1908 script__setup_sample_type(&script);
1910 session->itrace_synth_opts = &itrace_synth_opts;
1913 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1919 symbol_conf.use_callchain = true;
1921 symbol_conf.use_callchain = false;
1923 if (session->tevent.pevent &&
1924 pevent_set_function_resolver(session->tevent.pevent,
1925 machine__resolve_kernel_addr,
1926 &session->machines.host) < 0) {
1927 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
1931 if (generate_script_lang) {
1932 struct stat perf_stat;
1935 if (output_set_by_user()) {
1937 "custom fields not supported for generated scripts");
1942 input = open(file.path, O_RDONLY); /* input_name */
1945 perror("failed to open file");
1949 err = fstat(input, &perf_stat);
1951 perror("failed to stat file");
1955 if (!perf_stat.st_size) {
1956 fprintf(stderr, "zero-sized file, nothing to do!\n");
1960 scripting_ops = script_spec__lookup(generate_script_lang);
1961 if (!scripting_ops) {
1962 fprintf(stderr, "invalid language specifier");
1967 err = scripting_ops->generate_script(session->tevent.pevent,
1973 err = scripting_ops->start_script(script_name, argc, argv);
1976 pr_debug("perf script started with script %s\n\n", script_name);
1977 script_started = true;
1981 err = perf_session__check_output_opt(session);
1985 err = __cmd_script(&script);
1990 perf_session__delete(session);
1993 cleanup_scripting();