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 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
38 enum perf_output_field {
39 PERF_OUTPUT_COMM = 1U << 0,
40 PERF_OUTPUT_TID = 1U << 1,
41 PERF_OUTPUT_PID = 1U << 2,
42 PERF_OUTPUT_TIME = 1U << 3,
43 PERF_OUTPUT_CPU = 1U << 4,
44 PERF_OUTPUT_EVNAME = 1U << 5,
45 PERF_OUTPUT_TRACE = 1U << 6,
46 PERF_OUTPUT_IP = 1U << 7,
47 PERF_OUTPUT_SYM = 1U << 8,
48 PERF_OUTPUT_DSO = 1U << 9,
49 PERF_OUTPUT_ADDR = 1U << 10,
50 PERF_OUTPUT_SYMOFFSET = 1U << 11,
51 PERF_OUTPUT_SRCLINE = 1U << 12,
52 PERF_OUTPUT_PERIOD = 1U << 13,
53 PERF_OUTPUT_IREGS = 1U << 14,
56 struct output_option {
58 enum perf_output_field field;
59 } all_output_options[] = {
60 {.str = "comm", .field = PERF_OUTPUT_COMM},
61 {.str = "tid", .field = PERF_OUTPUT_TID},
62 {.str = "pid", .field = PERF_OUTPUT_PID},
63 {.str = "time", .field = PERF_OUTPUT_TIME},
64 {.str = "cpu", .field = PERF_OUTPUT_CPU},
65 {.str = "event", .field = PERF_OUTPUT_EVNAME},
66 {.str = "trace", .field = PERF_OUTPUT_TRACE},
67 {.str = "ip", .field = PERF_OUTPUT_IP},
68 {.str = "sym", .field = PERF_OUTPUT_SYM},
69 {.str = "dso", .field = PERF_OUTPUT_DSO},
70 {.str = "addr", .field = PERF_OUTPUT_ADDR},
71 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
72 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
73 {.str = "period", .field = PERF_OUTPUT_PERIOD},
74 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
77 /* default set to maintain compatibility with current format */
81 unsigned int print_ip_opts;
84 } output[PERF_TYPE_MAX] = {
86 [PERF_TYPE_HARDWARE] = {
89 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
90 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
91 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
92 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
95 .invalid_fields = PERF_OUTPUT_TRACE,
98 [PERF_TYPE_SOFTWARE] = {
101 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
102 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
103 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
104 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
107 .invalid_fields = PERF_OUTPUT_TRACE,
110 [PERF_TYPE_TRACEPOINT] = {
113 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
114 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
115 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
121 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
122 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
123 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
124 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
127 .invalid_fields = PERF_OUTPUT_TRACE,
131 static bool output_set_by_user(void)
134 for (j = 0; j < PERF_TYPE_MAX; ++j) {
135 if (output[j].user_set)
141 static const char *output_field2str(enum perf_output_field field)
143 int i, imax = ARRAY_SIZE(all_output_options);
144 const char *str = "";
146 for (i = 0; i < imax; ++i) {
147 if (all_output_options[i].field == field) {
148 str = all_output_options[i].str;
155 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
157 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
158 u64 sample_type, const char *sample_msg,
159 enum perf_output_field field,
162 struct perf_event_attr *attr = &evsel->attr;
163 int type = attr->type;
166 if (attr->sample_type & sample_type)
169 if (output[type].user_set) {
172 evname = perf_evsel__name(evsel);
173 pr_err("Samples for '%s' event do not have %s attribute set. "
174 "Cannot print '%s' field.\n",
175 evname, sample_msg, output_field2str(field));
179 /* user did not ask for it explicitly so remove from the default list */
180 output[type].fields &= ~field;
181 evname = perf_evsel__name(evsel);
182 pr_debug("Samples for '%s' event do not have %s attribute set. "
183 "Skipping '%s' field.\n",
184 evname, sample_msg, output_field2str(field));
189 static int perf_evsel__check_stype(struct perf_evsel *evsel,
190 u64 sample_type, const char *sample_msg,
191 enum perf_output_field field)
193 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
197 static int perf_evsel__check_attr(struct perf_evsel *evsel,
198 struct perf_session *session)
200 struct perf_event_attr *attr = &evsel->attr;
203 allow_user_set = perf_header__has_feat(&session->header,
206 if (PRINT_FIELD(TRACE) &&
207 !perf_session__has_traces(session, "record -R"))
210 if (PRINT_FIELD(IP)) {
211 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
216 if (PRINT_FIELD(ADDR) &&
217 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
218 PERF_OUTPUT_ADDR, allow_user_set))
221 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
222 pr_err("Display of symbols requested but neither sample IP nor "
223 "sample address\nis selected. Hence, no addresses to convert "
227 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
228 pr_err("Display of offsets requested but symbol is not"
232 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
233 pr_err("Display of DSO requested but neither sample IP nor "
234 "sample address\nis selected. Hence, no addresses to convert "
238 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
239 pr_err("Display of source line number requested but sample IP is not\n"
240 "selected. Hence, no address to lookup the source line number.\n");
244 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
245 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
246 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
249 if (PRINT_FIELD(TIME) &&
250 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
254 if (PRINT_FIELD(CPU) &&
255 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
256 PERF_OUTPUT_CPU, allow_user_set))
259 if (PRINT_FIELD(PERIOD) &&
260 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
264 if (PRINT_FIELD(IREGS) &&
265 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
272 static void set_print_ip_opts(struct perf_event_attr *attr)
274 unsigned int type = attr->type;
276 output[type].print_ip_opts = 0;
278 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
280 if (PRINT_FIELD(SYM))
281 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
283 if (PRINT_FIELD(DSO))
284 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
286 if (PRINT_FIELD(SYMOFFSET))
287 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
289 if (PRINT_FIELD(SRCLINE))
290 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
294 * verify all user requested events exist and the samples
295 * have the expected data
297 static int perf_session__check_output_opt(struct perf_session *session)
300 struct perf_evsel *evsel;
302 for (j = 0; j < PERF_TYPE_MAX; ++j) {
303 evsel = perf_session__find_first_evtype(session, j);
306 * even if fields is set to 0 (ie., show nothing) event must
307 * exist if user explicitly includes it on the command line
309 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
310 pr_err("%s events do not exist. "
311 "Remove corresponding -f option to proceed.\n",
316 if (evsel && output[j].fields &&
317 perf_evsel__check_attr(evsel, session))
323 set_print_ip_opts(&evsel->attr);
327 bool use_callchain = false;
329 evlist__for_each(session->evlist, evsel) {
330 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
331 use_callchain = true;
336 symbol_conf.use_callchain = false;
340 * set default for tracepoints to print symbols only
341 * if callchains are present
343 if (symbol_conf.use_callchain &&
344 !output[PERF_TYPE_TRACEPOINT].user_set) {
345 struct perf_event_attr *attr;
347 j = PERF_TYPE_TRACEPOINT;
348 evsel = perf_session__find_first_evtype(session, j);
354 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
355 output[j].fields |= PERF_OUTPUT_IP;
356 output[j].fields |= PERF_OUTPUT_SYM;
357 output[j].fields |= PERF_OUTPUT_DSO;
358 set_print_ip_opts(attr);
366 static void print_sample_iregs(union perf_event *event __maybe_unused,
367 struct perf_sample *sample,
368 struct thread *thread __maybe_unused,
369 struct perf_event_attr *attr)
371 struct regs_dump *regs = &sample->intr_regs;
372 uint64_t mask = attr->sample_regs_intr;
378 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
379 u64 val = regs->regs[i++];
380 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
384 static void print_sample_start(struct perf_sample *sample,
385 struct thread *thread,
386 struct perf_evsel *evsel)
388 struct perf_event_attr *attr = &evsel->attr;
391 unsigned long long nsecs;
393 if (PRINT_FIELD(COMM)) {
395 printf("%8.8s ", thread__comm_str(thread));
396 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
397 printf("%s ", thread__comm_str(thread));
399 printf("%16s ", thread__comm_str(thread));
402 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
403 printf("%5d/%-5d ", sample->pid, sample->tid);
404 else if (PRINT_FIELD(PID))
405 printf("%5d ", sample->pid);
406 else if (PRINT_FIELD(TID))
407 printf("%5d ", sample->tid);
409 if (PRINT_FIELD(CPU)) {
411 printf("%3d ", sample->cpu);
413 printf("[%03d] ", sample->cpu);
416 if (PRINT_FIELD(TIME)) {
417 nsecs = sample->time;
418 secs = nsecs / NSECS_PER_SEC;
419 nsecs -= secs * NSECS_PER_SEC;
420 usecs = nsecs / NSECS_PER_USEC;
422 printf("%5lu.%09llu: ", secs, nsecs);
424 printf("%5lu.%06lu: ", secs, usecs);
428 static void print_sample_addr(union perf_event *event,
429 struct perf_sample *sample,
430 struct thread *thread,
431 struct perf_event_attr *attr)
433 struct addr_location al;
435 printf("%16" PRIx64, sample->addr);
437 if (!sample_addr_correlates_sym(attr))
440 perf_event__preprocess_sample_addr(event, sample, thread, &al);
442 if (PRINT_FIELD(SYM)) {
444 if (PRINT_FIELD(SYMOFFSET))
445 symbol__fprintf_symname_offs(al.sym, &al, stdout);
447 symbol__fprintf_symname(al.sym, stdout);
450 if (PRINT_FIELD(DSO)) {
452 map__fprintf_dsoname(al.map, stdout);
457 static void print_sample_bts(union perf_event *event,
458 struct perf_sample *sample,
459 struct perf_evsel *evsel,
460 struct thread *thread,
461 struct addr_location *al)
463 struct perf_event_attr *attr = &evsel->attr;
464 bool print_srcline_last = false;
466 /* print branch_from information */
467 if (PRINT_FIELD(IP)) {
468 unsigned int print_opts = output[attr->type].print_ip_opts;
470 if (symbol_conf.use_callchain && sample->callchain) {
474 if (print_opts & PRINT_IP_OPT_SRCLINE) {
475 print_srcline_last = true;
476 print_opts &= ~PRINT_IP_OPT_SRCLINE;
479 perf_evsel__print_ip(evsel, sample, al, print_opts,
480 scripting_max_stack);
483 /* print branch_to information */
484 if (PRINT_FIELD(ADDR) ||
485 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
486 !output[attr->type].user_set)) {
488 print_sample_addr(event, sample, thread, attr);
491 if (print_srcline_last)
492 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
497 static void print_sample_flags(u32 flags)
499 const char *chars = PERF_IP_FLAG_CHARS;
500 const int n = strlen(PERF_IP_FLAG_CHARS);
504 for (i = 0; i < n; i++, flags >>= 1) {
506 str[pos++] = chars[i];
508 for (; i < 32; i++, flags >>= 1) {
513 printf(" %-4s ", str);
516 static void process_event(union perf_event *event, struct perf_sample *sample,
517 struct perf_evsel *evsel, struct addr_location *al)
519 struct thread *thread = al->thread;
520 struct perf_event_attr *attr = &evsel->attr;
522 if (output[attr->type].fields == 0)
525 print_sample_start(sample, thread, evsel);
527 if (PRINT_FIELD(PERIOD))
528 printf("%10" PRIu64 " ", sample->period);
530 if (PRINT_FIELD(EVNAME)) {
531 const char *evname = perf_evsel__name(evsel);
532 printf("%s: ", evname ? evname : "[unknown]");
536 print_sample_flags(sample->flags);
538 if (is_bts_event(attr)) {
539 print_sample_bts(event, sample, evsel, thread, al);
543 if (PRINT_FIELD(TRACE))
544 event_format__print(evsel->tp_format, sample->cpu,
545 sample->raw_data, sample->raw_size);
546 if (PRINT_FIELD(ADDR))
547 print_sample_addr(event, sample, thread, attr);
549 if (PRINT_FIELD(IP)) {
550 if (!symbol_conf.use_callchain)
555 perf_evsel__print_ip(evsel, sample, al,
556 output[attr->type].print_ip_opts,
557 scripting_max_stack);
560 if (PRINT_FIELD(IREGS))
561 print_sample_iregs(event, sample, thread, attr);
566 static int default_start_script(const char *script __maybe_unused,
567 int argc __maybe_unused,
568 const char **argv __maybe_unused)
573 static int default_flush_script(void)
578 static int default_stop_script(void)
583 static int default_generate_script(struct pevent *pevent __maybe_unused,
584 const char *outfile __maybe_unused)
589 static struct scripting_ops default_scripting_ops = {
590 .start_script = default_start_script,
591 .flush_script = default_flush_script,
592 .stop_script = default_stop_script,
593 .process_event = process_event,
594 .generate_script = default_generate_script,
597 static struct scripting_ops *scripting_ops;
599 static void setup_scripting(void)
601 setup_perl_scripting();
602 setup_python_scripting();
604 scripting_ops = &default_scripting_ops;
607 static int flush_scripting(void)
609 return scripting_ops->flush_script();
612 static int cleanup_scripting(void)
614 pr_debug("\nperf script stopped\n");
616 return scripting_ops->stop_script();
619 static int process_sample_event(struct perf_tool *tool __maybe_unused,
620 union perf_event *event,
621 struct perf_sample *sample,
622 struct perf_evsel *evsel,
623 struct machine *machine)
625 struct addr_location al;
628 if (sample->time < last_timestamp) {
629 pr_err("Samples misordered, previous: %" PRIu64
630 " this: %" PRIu64 "\n", last_timestamp,
634 last_timestamp = sample->time;
638 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
639 pr_err("problem processing %d event, skipping it.\n",
647 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
650 scripting_ops->process_event(event, sample, evsel, &al);
652 addr_location__put(&al);
657 struct perf_tool tool;
658 struct perf_session *session;
659 bool show_task_events;
660 bool show_mmap_events;
661 bool show_switch_events;
664 static int process_attr(struct perf_tool *tool, union perf_event *event,
665 struct perf_evlist **pevlist)
667 struct perf_script *scr = container_of(tool, struct perf_script, tool);
668 struct perf_evlist *evlist;
669 struct perf_evsel *evsel, *pos;
672 err = perf_event__process_attr(tool, event, pevlist);
677 evsel = perf_evlist__last(*pevlist);
679 if (evsel->attr.type >= PERF_TYPE_MAX)
682 evlist__for_each(evlist, pos) {
683 if (pos->attr.type == evsel->attr.type && pos != evsel)
687 set_print_ip_opts(&evsel->attr);
689 return perf_evsel__check_attr(evsel, scr->session);
692 static int process_comm_event(struct perf_tool *tool,
693 union perf_event *event,
694 struct perf_sample *sample,
695 struct machine *machine)
697 struct thread *thread;
698 struct perf_script *script = container_of(tool, struct perf_script, tool);
699 struct perf_session *session = script->session;
700 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
703 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
704 if (thread == NULL) {
705 pr_debug("problem processing COMM event, skipping it.\n");
709 if (perf_event__process_comm(tool, event, sample, machine) < 0)
712 if (!evsel->attr.sample_id_all) {
715 sample->tid = event->comm.tid;
716 sample->pid = event->comm.pid;
718 print_sample_start(sample, thread, evsel);
719 perf_event__fprintf(event, stdout);
726 static int process_fork_event(struct perf_tool *tool,
727 union perf_event *event,
728 struct perf_sample *sample,
729 struct machine *machine)
731 struct thread *thread;
732 struct perf_script *script = container_of(tool, struct perf_script, tool);
733 struct perf_session *session = script->session;
734 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
736 if (perf_event__process_fork(tool, event, sample, machine) < 0)
739 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
740 if (thread == NULL) {
741 pr_debug("problem processing FORK event, skipping it.\n");
745 if (!evsel->attr.sample_id_all) {
747 sample->time = event->fork.time;
748 sample->tid = event->fork.tid;
749 sample->pid = event->fork.pid;
751 print_sample_start(sample, thread, evsel);
752 perf_event__fprintf(event, stdout);
757 static int process_exit_event(struct perf_tool *tool,
758 union perf_event *event,
759 struct perf_sample *sample,
760 struct machine *machine)
763 struct thread *thread;
764 struct perf_script *script = container_of(tool, struct perf_script, tool);
765 struct perf_session *session = script->session;
766 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
768 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
769 if (thread == NULL) {
770 pr_debug("problem processing EXIT event, skipping it.\n");
774 if (!evsel->attr.sample_id_all) {
777 sample->tid = event->fork.tid;
778 sample->pid = event->fork.pid;
780 print_sample_start(sample, thread, evsel);
781 perf_event__fprintf(event, stdout);
783 if (perf_event__process_exit(tool, event, sample, machine) < 0)
790 static int process_mmap_event(struct perf_tool *tool,
791 union perf_event *event,
792 struct perf_sample *sample,
793 struct machine *machine)
795 struct thread *thread;
796 struct perf_script *script = container_of(tool, struct perf_script, tool);
797 struct perf_session *session = script->session;
798 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
800 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
803 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
804 if (thread == NULL) {
805 pr_debug("problem processing MMAP event, skipping it.\n");
809 if (!evsel->attr.sample_id_all) {
812 sample->tid = event->mmap.tid;
813 sample->pid = event->mmap.pid;
815 print_sample_start(sample, thread, evsel);
816 perf_event__fprintf(event, stdout);
821 static int process_mmap2_event(struct perf_tool *tool,
822 union perf_event *event,
823 struct perf_sample *sample,
824 struct machine *machine)
826 struct thread *thread;
827 struct perf_script *script = container_of(tool, struct perf_script, tool);
828 struct perf_session *session = script->session;
829 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
831 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
834 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
835 if (thread == NULL) {
836 pr_debug("problem processing MMAP2 event, skipping it.\n");
840 if (!evsel->attr.sample_id_all) {
843 sample->tid = event->mmap2.tid;
844 sample->pid = event->mmap2.pid;
846 print_sample_start(sample, thread, evsel);
847 perf_event__fprintf(event, stdout);
852 static int process_switch_event(struct perf_tool *tool,
853 union perf_event *event,
854 struct perf_sample *sample,
855 struct machine *machine)
857 struct thread *thread;
858 struct perf_script *script = container_of(tool, struct perf_script, tool);
859 struct perf_session *session = script->session;
860 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
862 if (perf_event__process_switch(tool, event, sample, machine) < 0)
865 thread = machine__findnew_thread(machine, sample->pid,
867 if (thread == NULL) {
868 pr_debug("problem processing SWITCH event, skipping it.\n");
872 print_sample_start(sample, thread, evsel);
873 perf_event__fprintf(event, stdout);
878 static void sig_handler(int sig __maybe_unused)
883 static int __cmd_script(struct perf_script *script)
887 signal(SIGINT, sig_handler);
889 /* override event processing functions */
890 if (script->show_task_events) {
891 script->tool.comm = process_comm_event;
892 script->tool.fork = process_fork_event;
893 script->tool.exit = process_exit_event;
895 if (script->show_mmap_events) {
896 script->tool.mmap = process_mmap_event;
897 script->tool.mmap2 = process_mmap2_event;
899 if (script->show_switch_events)
900 script->tool.context_switch = process_switch_event;
902 ret = perf_session__process_events(script->session);
905 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
911 struct list_head node;
912 struct scripting_ops *ops;
916 static LIST_HEAD(script_specs);
918 static struct script_spec *script_spec__new(const char *spec,
919 struct scripting_ops *ops)
921 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
924 strcpy(s->spec, spec);
931 static void script_spec__add(struct script_spec *s)
933 list_add_tail(&s->node, &script_specs);
936 static struct script_spec *script_spec__find(const char *spec)
938 struct script_spec *s;
940 list_for_each_entry(s, &script_specs, node)
941 if (strcasecmp(s->spec, spec) == 0)
946 static struct script_spec *script_spec__findnew(const char *spec,
947 struct scripting_ops *ops)
949 struct script_spec *s = script_spec__find(spec);
954 s = script_spec__new(spec, ops);
963 int script_spec_register(const char *spec, struct scripting_ops *ops)
965 struct script_spec *s;
967 s = script_spec__find(spec);
971 s = script_spec__findnew(spec, ops);
978 static struct scripting_ops *script_spec__lookup(const char *spec)
980 struct script_spec *s = script_spec__find(spec);
987 static void list_available_languages(void)
989 struct script_spec *s;
991 fprintf(stderr, "\n");
992 fprintf(stderr, "Scripting language extensions (used in "
993 "perf script -s [spec:]script.[spec]):\n\n");
995 list_for_each_entry(s, &script_specs, node)
996 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
998 fprintf(stderr, "\n");
1001 static int parse_scriptname(const struct option *opt __maybe_unused,
1002 const char *str, int unset __maybe_unused)
1004 char spec[PATH_MAX];
1005 const char *script, *ext;
1008 if (strcmp(str, "lang") == 0) {
1009 list_available_languages();
1013 script = strchr(str, ':');
1016 if (len >= PATH_MAX) {
1017 fprintf(stderr, "invalid language specifier");
1020 strncpy(spec, str, len);
1022 scripting_ops = script_spec__lookup(spec);
1023 if (!scripting_ops) {
1024 fprintf(stderr, "invalid language specifier");
1030 ext = strrchr(script, '.');
1032 fprintf(stderr, "invalid script extension");
1035 scripting_ops = script_spec__lookup(++ext);
1036 if (!scripting_ops) {
1037 fprintf(stderr, "invalid script extension");
1042 script_name = strdup(script);
1047 static int parse_output_fields(const struct option *opt __maybe_unused,
1048 const char *arg, int unset __maybe_unused)
1051 int i, imax = ARRAY_SIZE(all_output_options);
1054 char *str = strdup(arg);
1060 /* first word can state for which event type the user is specifying
1061 * the fields. If no type exists, the specified fields apply to all
1062 * event types found in the file minus the invalid fields for a type.
1064 tok = strchr(str, ':');
1068 if (!strcmp(str, "hw"))
1069 type = PERF_TYPE_HARDWARE;
1070 else if (!strcmp(str, "sw"))
1071 type = PERF_TYPE_SOFTWARE;
1072 else if (!strcmp(str, "trace"))
1073 type = PERF_TYPE_TRACEPOINT;
1074 else if (!strcmp(str, "raw"))
1075 type = PERF_TYPE_RAW;
1077 fprintf(stderr, "Invalid event type in field string.\n");
1082 if (output[type].user_set)
1083 pr_warning("Overriding previous field request for %s events.\n",
1086 output[type].fields = 0;
1087 output[type].user_set = true;
1088 output[type].wildcard_set = false;
1092 if (strlen(str) == 0) {
1094 "Cannot set fields to 'none' for all event types.\n");
1099 if (output_set_by_user())
1100 pr_warning("Overriding previous field request for all events.\n");
1102 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1103 output[j].fields = 0;
1104 output[j].user_set = true;
1105 output[j].wildcard_set = true;
1109 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
1110 for (i = 0; i < imax; ++i) {
1111 if (strcmp(tok, all_output_options[i].str) == 0)
1114 if (i == imax && strcmp(tok, "flags") == 0) {
1119 fprintf(stderr, "Invalid field requested.\n");
1125 /* add user option to all events types for
1128 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1129 if (output[j].invalid_fields & all_output_options[i].field) {
1130 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1131 all_output_options[i].str, event_type(j));
1133 output[j].fields |= all_output_options[i].field;
1136 if (output[type].invalid_fields & all_output_options[i].field) {
1137 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1138 all_output_options[i].str, event_type(type));
1143 output[type].fields |= all_output_options[i].field;
1148 if (output[type].fields == 0) {
1149 pr_debug("No fields requested for %s type. "
1150 "Events will not be displayed.\n", event_type(type));
1159 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1160 static int is_directory(const char *base_path, const struct dirent *dent)
1162 char path[PATH_MAX];
1165 sprintf(path, "%s/%s", base_path, dent->d_name);
1166 if (stat(path, &st))
1169 return S_ISDIR(st.st_mode);
1172 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
1173 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1175 if ((lang_dirent.d_type == DT_DIR || \
1176 (lang_dirent.d_type == DT_UNKNOWN && \
1177 is_directory(scripts_path, &lang_dirent))) && \
1178 (strcmp(lang_dirent.d_name, ".")) && \
1179 (strcmp(lang_dirent.d_name, "..")))
1181 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
1182 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1184 if (script_dirent.d_type != DT_DIR && \
1185 (script_dirent.d_type != DT_UNKNOWN || \
1186 !is_directory(lang_path, &script_dirent)))
1189 #define RECORD_SUFFIX "-record"
1190 #define REPORT_SUFFIX "-report"
1192 struct script_desc {
1193 struct list_head node;
1199 static LIST_HEAD(script_descs);
1201 static struct script_desc *script_desc__new(const char *name)
1203 struct script_desc *s = zalloc(sizeof(*s));
1205 if (s != NULL && name)
1206 s->name = strdup(name);
1211 static void script_desc__delete(struct script_desc *s)
1214 zfree(&s->half_liner);
1219 static void script_desc__add(struct script_desc *s)
1221 list_add_tail(&s->node, &script_descs);
1224 static struct script_desc *script_desc__find(const char *name)
1226 struct script_desc *s;
1228 list_for_each_entry(s, &script_descs, node)
1229 if (strcasecmp(s->name, name) == 0)
1234 static struct script_desc *script_desc__findnew(const char *name)
1236 struct script_desc *s = script_desc__find(name);
1241 s = script_desc__new(name);
1243 goto out_delete_desc;
1245 script_desc__add(s);
1250 script_desc__delete(s);
1255 static const char *ends_with(const char *str, const char *suffix)
1257 size_t suffix_len = strlen(suffix);
1258 const char *p = str;
1260 if (strlen(str) > suffix_len) {
1261 p = str + strlen(str) - suffix_len;
1262 if (!strncmp(p, suffix, suffix_len))
1269 static int read_script_info(struct script_desc *desc, const char *filename)
1271 char line[BUFSIZ], *p;
1274 fp = fopen(filename, "r");
1278 while (fgets(line, sizeof(line), fp)) {
1285 if (strlen(p) && *p == '!')
1289 if (strlen(p) && p[strlen(p) - 1] == '\n')
1290 p[strlen(p) - 1] = '\0';
1292 if (!strncmp(p, "description:", strlen("description:"))) {
1293 p += strlen("description:");
1294 desc->half_liner = strdup(ltrim(p));
1298 if (!strncmp(p, "args:", strlen("args:"))) {
1299 p += strlen("args:");
1300 desc->args = strdup(ltrim(p));
1310 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1312 char *script_root, *str;
1314 script_root = strdup(script_dirent->d_name);
1318 str = (char *)ends_with(script_root, suffix);
1328 static int list_available_scripts(const struct option *opt __maybe_unused,
1329 const char *s __maybe_unused,
1330 int unset __maybe_unused)
1332 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1333 char scripts_path[MAXPATHLEN];
1334 DIR *scripts_dir, *lang_dir;
1335 char script_path[MAXPATHLEN];
1336 char lang_path[MAXPATHLEN];
1337 struct script_desc *desc;
1338 char first_half[BUFSIZ];
1341 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1343 scripts_dir = opendir(scripts_path);
1347 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1348 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1349 lang_dirent.d_name);
1350 lang_dir = opendir(lang_path);
1354 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1355 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1357 desc = script_desc__findnew(script_root);
1358 snprintf(script_path, MAXPATHLEN, "%s/%s",
1359 lang_path, script_dirent.d_name);
1360 read_script_info(desc, script_path);
1366 fprintf(stdout, "List of available trace scripts:\n");
1367 list_for_each_entry(desc, &script_descs, node) {
1368 sprintf(first_half, "%s %s", desc->name,
1369 desc->args ? desc->args : "");
1370 fprintf(stdout, " %-36s %s\n", first_half,
1371 desc->half_liner ? desc->half_liner : "");
1378 * Some scripts specify the required events in their "xxx-record" file,
1379 * this function will check if the events in perf.data match those
1380 * mentioned in the "xxx-record".
1382 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1383 * which is covered well now. And new parsing code should be added to
1384 * cover the future complexing formats like event groups etc.
1386 static int check_ev_match(char *dir_name, char *scriptname,
1387 struct perf_session *session)
1389 char filename[MAXPATHLEN], evname[128];
1390 char line[BUFSIZ], *p;
1391 struct perf_evsel *pos;
1395 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1397 fp = fopen(filename, "r");
1401 while (fgets(line, sizeof(line), fp)) {
1407 p = strstr(p, "-e");
1413 len = strcspn(p, " \t");
1417 snprintf(evname, len + 1, "%s", p);
1420 evlist__for_each(session->evlist, pos) {
1421 if (!strcmp(perf_evsel__name(pos), evname)) {
1439 * Return -1 if none is found, otherwise the actual scripts number.
1441 * Currently the only user of this function is the script browser, which
1442 * will list all statically runnable scripts, select one, execute it and
1443 * show the output in a perf browser.
1445 int find_scripts(char **scripts_array, char **scripts_path_array)
1447 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1448 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
1449 DIR *scripts_dir, *lang_dir;
1450 struct perf_session *session;
1451 struct perf_data_file file = {
1453 .mode = PERF_DATA_MODE_READ,
1458 session = perf_session__new(&file, false, NULL);
1462 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1464 scripts_dir = opendir(scripts_path);
1466 perf_session__delete(session);
1470 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1471 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1472 lang_dirent.d_name);
1474 if (strstr(lang_path, "perl"))
1478 if (strstr(lang_path, "python"))
1482 lang_dir = opendir(lang_path);
1486 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1487 /* Skip those real time scripts: xxxtop.p[yl] */
1488 if (strstr(script_dirent.d_name, "top."))
1490 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1491 script_dirent.d_name);
1492 temp = strchr(script_dirent.d_name, '.');
1493 snprintf(scripts_array[i],
1494 (temp - script_dirent.d_name) + 1,
1495 "%s", script_dirent.d_name);
1497 if (check_ev_match(lang_path,
1498 scripts_array[i], session))
1506 closedir(scripts_dir);
1507 perf_session__delete(session);
1511 static char *get_script_path(const char *script_root, const char *suffix)
1513 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1514 char scripts_path[MAXPATHLEN];
1515 char script_path[MAXPATHLEN];
1516 DIR *scripts_dir, *lang_dir;
1517 char lang_path[MAXPATHLEN];
1518 char *__script_root;
1520 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1522 scripts_dir = opendir(scripts_path);
1526 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1527 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1528 lang_dirent.d_name);
1529 lang_dir = opendir(lang_path);
1533 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1534 __script_root = get_script_root(&script_dirent, suffix);
1535 if (__script_root && !strcmp(script_root, __script_root)) {
1536 free(__script_root);
1538 closedir(scripts_dir);
1539 snprintf(script_path, MAXPATHLEN, "%s/%s",
1540 lang_path, script_dirent.d_name);
1541 return strdup(script_path);
1543 free(__script_root);
1547 closedir(scripts_dir);
1552 static bool is_top_script(const char *script_path)
1554 return ends_with(script_path, "top") == NULL ? false : true;
1557 static int has_required_arg(char *script_path)
1559 struct script_desc *desc;
1563 desc = script_desc__new(NULL);
1565 if (read_script_info(desc, script_path))
1571 for (p = desc->args; *p; p++)
1575 script_desc__delete(desc);
1580 static int have_cmd(int argc, const char **argv)
1582 char **__argv = malloc(sizeof(const char *) * argc);
1585 pr_err("malloc failed\n");
1589 memcpy(__argv, argv, sizeof(const char *) * argc);
1590 argc = parse_options(argc, (const char **)__argv, record_options,
1591 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1594 system_wide = (argc == 0);
1599 static void script__setup_sample_type(struct perf_script *script)
1601 struct perf_session *session = script->session;
1602 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1604 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1605 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1606 (sample_type & PERF_SAMPLE_STACK_USER))
1607 callchain_param.record_mode = CALLCHAIN_DWARF;
1608 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1609 callchain_param.record_mode = CALLCHAIN_LBR;
1611 callchain_param.record_mode = CALLCHAIN_FP;
1615 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1617 bool show_full_info = false;
1618 bool header = false;
1619 bool header_only = false;
1620 bool script_started = false;
1621 char *rec_script_path = NULL;
1622 char *rep_script_path = NULL;
1623 struct perf_session *session;
1624 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
1625 char *script_path = NULL;
1626 const char **__argv;
1628 struct perf_script script = {
1630 .sample = process_sample_event,
1631 .mmap = perf_event__process_mmap,
1632 .mmap2 = perf_event__process_mmap2,
1633 .comm = perf_event__process_comm,
1634 .exit = perf_event__process_exit,
1635 .fork = perf_event__process_fork,
1636 .attr = process_attr,
1637 .tracing_data = perf_event__process_tracing_data,
1638 .build_id = perf_event__process_build_id,
1639 .id_index = perf_event__process_id_index,
1640 .auxtrace_info = perf_event__process_auxtrace_info,
1641 .auxtrace = perf_event__process_auxtrace,
1642 .auxtrace_error = perf_event__process_auxtrace_error,
1643 .ordered_events = true,
1644 .ordering_requires_timestamps = true,
1647 struct perf_data_file file = {
1648 .mode = PERF_DATA_MODE_READ,
1650 const struct option options[] = {
1651 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1652 "dump raw trace in ASCII"),
1653 OPT_INCR('v', "verbose", &verbose,
1654 "be more verbose (show symbol address, etc)"),
1655 OPT_BOOLEAN('L', "Latency", &latency_format,
1656 "show latency attributes (irqs/preemption disabled, etc)"),
1657 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1658 list_available_scripts),
1659 OPT_CALLBACK('s', "script", NULL, "name",
1660 "script file name (lang:script name, script name, or *)",
1662 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1663 "generate perf-script.xx script in specified language"),
1664 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1665 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1666 "do various checks like samples ordering and lost events"),
1667 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1668 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
1669 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1670 "file", "vmlinux pathname"),
1671 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1672 "file", "kallsyms pathname"),
1673 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1674 "When printing symbols do not display call chain"),
1675 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1676 "Look for files with symbols relative to this directory"),
1677 OPT_CALLBACK('F', "fields", NULL, "str",
1678 "comma separated output fields prepend with 'type:'. "
1679 "Valid types: hw,sw,trace,raw. "
1680 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1681 "addr,symoff,period,iregs,flags", parse_output_fields),
1682 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1683 "system-wide collection from all CPUs"),
1684 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1685 "only consider these symbols"),
1686 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1687 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1688 "only display events for these comms"),
1689 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1690 "only consider symbols in these pids"),
1691 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1692 "only consider symbols in these tids"),
1693 OPT_BOOLEAN('I', "show-info", &show_full_info,
1694 "display extended information from perf.data file"),
1695 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1696 "Show the path of [kernel.kallsyms]"),
1697 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1698 "Show the fork/comm/exit events"),
1699 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1700 "Show the mmap events"),
1701 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1702 "Show context switch events (if recorded)"),
1703 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
1704 OPT_BOOLEAN(0, "ns", &nanosecs,
1705 "Use 9 decimal places when displaying time"),
1706 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1707 "Instruction Tracing options",
1708 itrace_parse_synth_opts),
1709 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1710 "Show full source file name path for source lines"),
1711 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1712 "Enable symbol demangling"),
1713 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1714 "Enable kernel symbol demangling"),
1718 const char * const script_subcommands[] = { "record", "report", NULL };
1719 const char *script_usage[] = {
1720 "perf script [<options>]",
1721 "perf script [<options>] record <script> [<record-options>] <command>",
1722 "perf script [<options>] report <script> [script-args]",
1723 "perf script [<options>] <script> [<record-options>] <command>",
1724 "perf script [<options>] <top-script> [script-args]",
1730 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
1731 PARSE_OPT_STOP_AT_NON_OPTION);
1733 file.path = input_name;
1735 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1736 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1737 if (!rec_script_path)
1738 return cmd_record(argc, argv, NULL);
1741 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1742 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1743 if (!rep_script_path) {
1745 "Please specify a valid report script"
1746 "(see 'perf script -l' for listing)\n");
1751 /* make sure PERF_EXEC_PATH is set for scripts */
1752 perf_set_argv_exec_path(perf_exec_path());
1754 if (argc && !script_name && !rec_script_path && !rep_script_path) {
1759 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1760 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1762 if (!rec_script_path && !rep_script_path) {
1763 fprintf(stderr, " Couldn't find script %s\n\n See perf"
1764 " script -l for available scripts.\n", argv[0]);
1765 usage_with_options(script_usage, options);
1768 if (is_top_script(argv[0])) {
1769 rep_args = argc - 1;
1773 rep_args = has_required_arg(rep_script_path);
1774 rec_args = (argc - 1) - rep_args;
1776 fprintf(stderr, " %s script requires options."
1777 "\n\n See perf script -l for available "
1778 "scripts and options.\n", argv[0]);
1779 usage_with_options(script_usage, options);
1783 if (pipe(live_pipe) < 0) {
1784 perror("failed to create pipe");
1790 perror("failed to fork");
1797 dup2(live_pipe[1], 1);
1798 close(live_pipe[0]);
1800 if (is_top_script(argv[0])) {
1802 } else if (!system_wide) {
1803 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1809 __argv = malloc((argc + 6) * sizeof(const char *));
1811 pr_err("malloc failed\n");
1816 __argv[j++] = "/bin/sh";
1817 __argv[j++] = rec_script_path;
1823 for (i = rep_args + 1; i < argc; i++)
1824 __argv[j++] = argv[i];
1827 execvp("/bin/sh", (char **)__argv);
1832 dup2(live_pipe[0], 0);
1833 close(live_pipe[1]);
1835 __argv = malloc((argc + 4) * sizeof(const char *));
1837 pr_err("malloc failed\n");
1843 __argv[j++] = "/bin/sh";
1844 __argv[j++] = rep_script_path;
1845 for (i = 1; i < rep_args + 1; i++)
1846 __argv[j++] = argv[i];
1851 execvp("/bin/sh", (char **)__argv);
1856 if (rec_script_path)
1857 script_path = rec_script_path;
1858 if (rep_script_path)
1859 script_path = rep_script_path;
1864 if (!rec_script_path)
1865 system_wide = false;
1866 else if (!system_wide) {
1867 if (have_cmd(argc - 1, &argv[1]) != 0) {
1873 __argv = malloc((argc + 2) * sizeof(const char *));
1875 pr_err("malloc failed\n");
1880 __argv[j++] = "/bin/sh";
1881 __argv[j++] = script_path;
1884 for (i = 2; i < argc; i++)
1885 __argv[j++] = argv[i];
1888 execvp("/bin/sh", (char **)__argv);
1896 session = perf_session__new(&file, false, &script.tool);
1897 if (session == NULL)
1900 if (header || header_only) {
1901 perf_session__fprintf_info(session, stdout, show_full_info);
1906 if (symbol__init(&session->header.env) < 0)
1909 script.session = session;
1910 script__setup_sample_type(&script);
1912 session->itrace_synth_opts = &itrace_synth_opts;
1915 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1921 symbol_conf.use_callchain = true;
1923 symbol_conf.use_callchain = false;
1925 if (session->tevent.pevent &&
1926 pevent_set_function_resolver(session->tevent.pevent,
1927 machine__resolve_kernel_addr,
1928 &session->machines.host) < 0) {
1929 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
1933 if (generate_script_lang) {
1934 struct stat perf_stat;
1937 if (output_set_by_user()) {
1939 "custom fields not supported for generated scripts");
1944 input = open(file.path, O_RDONLY); /* input_name */
1947 perror("failed to open file");
1951 err = fstat(input, &perf_stat);
1953 perror("failed to stat file");
1957 if (!perf_stat.st_size) {
1958 fprintf(stderr, "zero-sized file, nothing to do!\n");
1962 scripting_ops = script_spec__lookup(generate_script_lang);
1963 if (!scripting_ops) {
1964 fprintf(stderr, "invalid language specifier");
1969 err = scripting_ops->generate_script(session->tevent.pevent,
1975 err = scripting_ops->start_script(script_name, argc, argv);
1978 pr_debug("perf script started with script %s\n\n", script_name);
1979 script_started = true;
1983 err = perf_session__check_output_opt(session);
1987 err = __cmd_script(&script);
1992 perf_session__delete(session);
1995 cleanup_scripting();