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/session.h"
10 #include "util/tool.h"
11 #include "util/symbol.h"
12 #include "util/thread.h"
13 #include "util/trace-event.h"
14 #include "util/util.h"
15 #include "util/evlist.h"
16 #include "util/evsel.h"
17 #include "util/sort.h"
18 #include "util/data.h"
19 #include <linux/bitmap.h>
21 static char const *script_name;
22 static char const *generate_script_lang;
23 static bool debug_mode;
24 static u64 last_timestamp;
25 static u64 nr_unordered;
26 extern const struct option record_options[];
27 static bool no_callchain;
28 static bool latency_format;
29 static bool system_wide;
30 static const char *cpu_list;
31 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
33 enum perf_output_field {
34 PERF_OUTPUT_COMM = 1U << 0,
35 PERF_OUTPUT_TID = 1U << 1,
36 PERF_OUTPUT_PID = 1U << 2,
37 PERF_OUTPUT_TIME = 1U << 3,
38 PERF_OUTPUT_CPU = 1U << 4,
39 PERF_OUTPUT_EVNAME = 1U << 5,
40 PERF_OUTPUT_TRACE = 1U << 6,
41 PERF_OUTPUT_IP = 1U << 7,
42 PERF_OUTPUT_SYM = 1U << 8,
43 PERF_OUTPUT_DSO = 1U << 9,
44 PERF_OUTPUT_ADDR = 1U << 10,
45 PERF_OUTPUT_SYMOFFSET = 1U << 11,
46 PERF_OUTPUT_SRCLINE = 1U << 12,
47 PERF_OUTPUT_PERIOD = 1U << 13,
50 struct output_option {
52 enum perf_output_field field;
53 } all_output_options[] = {
54 {.str = "comm", .field = PERF_OUTPUT_COMM},
55 {.str = "tid", .field = PERF_OUTPUT_TID},
56 {.str = "pid", .field = PERF_OUTPUT_PID},
57 {.str = "time", .field = PERF_OUTPUT_TIME},
58 {.str = "cpu", .field = PERF_OUTPUT_CPU},
59 {.str = "event", .field = PERF_OUTPUT_EVNAME},
60 {.str = "trace", .field = PERF_OUTPUT_TRACE},
61 {.str = "ip", .field = PERF_OUTPUT_IP},
62 {.str = "sym", .field = PERF_OUTPUT_SYM},
63 {.str = "dso", .field = PERF_OUTPUT_DSO},
64 {.str = "addr", .field = PERF_OUTPUT_ADDR},
65 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
66 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
67 {.str = "period", .field = PERF_OUTPUT_PERIOD},
70 /* default set to maintain compatibility with current format */
74 unsigned int print_ip_opts;
77 } output[PERF_TYPE_MAX] = {
79 [PERF_TYPE_HARDWARE] = {
82 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
83 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
84 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
85 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
87 .invalid_fields = PERF_OUTPUT_TRACE,
90 [PERF_TYPE_SOFTWARE] = {
93 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
94 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
95 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
96 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
98 .invalid_fields = PERF_OUTPUT_TRACE,
101 [PERF_TYPE_TRACEPOINT] = {
104 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
105 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
106 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
112 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
113 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
114 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
115 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
117 .invalid_fields = PERF_OUTPUT_TRACE,
121 static bool output_set_by_user(void)
124 for (j = 0; j < PERF_TYPE_MAX; ++j) {
125 if (output[j].user_set)
131 static const char *output_field2str(enum perf_output_field field)
133 int i, imax = ARRAY_SIZE(all_output_options);
134 const char *str = "";
136 for (i = 0; i < imax; ++i) {
137 if (all_output_options[i].field == field) {
138 str = all_output_options[i].str;
145 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
147 static int perf_evsel__check_stype(struct perf_evsel *evsel,
148 u64 sample_type, const char *sample_msg,
149 enum perf_output_field field)
151 struct perf_event_attr *attr = &evsel->attr;
152 int type = attr->type;
155 if (attr->sample_type & sample_type)
158 if (output[type].user_set) {
159 evname = perf_evsel__name(evsel);
160 pr_err("Samples for '%s' event do not have %s attribute set. "
161 "Cannot print '%s' field.\n",
162 evname, sample_msg, output_field2str(field));
166 /* user did not ask for it explicitly so remove from the default list */
167 output[type].fields &= ~field;
168 evname = perf_evsel__name(evsel);
169 pr_debug("Samples for '%s' event do not have %s attribute set. "
170 "Skipping '%s' field.\n",
171 evname, sample_msg, output_field2str(field));
176 static int perf_evsel__check_attr(struct perf_evsel *evsel,
177 struct perf_session *session)
179 struct perf_event_attr *attr = &evsel->attr;
181 if (PRINT_FIELD(TRACE) &&
182 !perf_session__has_traces(session, "record -R"))
185 if (PRINT_FIELD(IP)) {
186 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
191 if (PRINT_FIELD(ADDR) &&
192 perf_evsel__check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
196 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
197 pr_err("Display of symbols requested but neither sample IP nor "
198 "sample address\nis selected. Hence, no addresses to convert "
202 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
203 pr_err("Display of offsets requested but symbol is not"
207 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
208 pr_err("Display of DSO requested but neither sample IP nor "
209 "sample address\nis selected. Hence, no addresses to convert "
213 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
214 pr_err("Display of source line number requested but sample IP is not\n"
215 "selected. Hence, no address to lookup the source line number.\n");
219 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
220 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
221 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
224 if (PRINT_FIELD(TIME) &&
225 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
229 if (PRINT_FIELD(CPU) &&
230 perf_evsel__check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
234 if (PRINT_FIELD(PERIOD) &&
235 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
242 static void set_print_ip_opts(struct perf_event_attr *attr)
244 unsigned int type = attr->type;
246 output[type].print_ip_opts = 0;
248 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
250 if (PRINT_FIELD(SYM))
251 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
253 if (PRINT_FIELD(DSO))
254 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
256 if (PRINT_FIELD(SYMOFFSET))
257 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
259 if (PRINT_FIELD(SRCLINE))
260 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
264 * verify all user requested events exist and the samples
265 * have the expected data
267 static int perf_session__check_output_opt(struct perf_session *session)
270 struct perf_evsel *evsel;
272 for (j = 0; j < PERF_TYPE_MAX; ++j) {
273 evsel = perf_session__find_first_evtype(session, j);
276 * even if fields is set to 0 (ie., show nothing) event must
277 * exist if user explicitly includes it on the command line
279 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
280 pr_err("%s events do not exist. "
281 "Remove corresponding -f option to proceed.\n",
286 if (evsel && output[j].fields &&
287 perf_evsel__check_attr(evsel, session))
293 set_print_ip_opts(&evsel->attr);
297 bool use_callchain = false;
299 evlist__for_each(session->evlist, evsel) {
300 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
301 use_callchain = true;
306 symbol_conf.use_callchain = false;
310 * set default for tracepoints to print symbols only
311 * if callchains are present
313 if (symbol_conf.use_callchain &&
314 !output[PERF_TYPE_TRACEPOINT].user_set) {
315 struct perf_event_attr *attr;
317 j = PERF_TYPE_TRACEPOINT;
318 evsel = perf_session__find_first_evtype(session, j);
324 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
325 output[j].fields |= PERF_OUTPUT_IP;
326 output[j].fields |= PERF_OUTPUT_SYM;
327 output[j].fields |= PERF_OUTPUT_DSO;
328 set_print_ip_opts(attr);
336 static void print_sample_start(struct perf_sample *sample,
337 struct thread *thread,
338 struct perf_evsel *evsel)
340 struct perf_event_attr *attr = &evsel->attr;
343 unsigned long long nsecs;
345 if (PRINT_FIELD(COMM)) {
347 printf("%8.8s ", thread__comm_str(thread));
348 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
349 printf("%s ", thread__comm_str(thread));
351 printf("%16s ", thread__comm_str(thread));
354 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
355 printf("%5d/%-5d ", sample->pid, sample->tid);
356 else if (PRINT_FIELD(PID))
357 printf("%5d ", sample->pid);
358 else if (PRINT_FIELD(TID))
359 printf("%5d ", sample->tid);
361 if (PRINT_FIELD(CPU)) {
363 printf("%3d ", sample->cpu);
365 printf("[%03d] ", sample->cpu);
368 if (PRINT_FIELD(TIME)) {
369 nsecs = sample->time;
370 secs = nsecs / NSECS_PER_SEC;
371 nsecs -= secs * NSECS_PER_SEC;
372 usecs = nsecs / NSECS_PER_USEC;
373 printf("%5lu.%06lu: ", secs, usecs);
377 static void print_sample_addr(union perf_event *event,
378 struct perf_sample *sample,
379 struct machine *machine,
380 struct thread *thread,
381 struct perf_event_attr *attr)
383 struct addr_location al;
385 printf("%16" PRIx64, sample->addr);
387 if (!sample_addr_correlates_sym(attr))
390 perf_event__preprocess_sample_addr(event, sample, machine, thread, &al);
392 if (PRINT_FIELD(SYM)) {
394 if (PRINT_FIELD(SYMOFFSET))
395 symbol__fprintf_symname_offs(al.sym, &al, stdout);
397 symbol__fprintf_symname(al.sym, stdout);
400 if (PRINT_FIELD(DSO)) {
402 map__fprintf_dsoname(al.map, stdout);
407 static void print_sample_bts(union perf_event *event,
408 struct perf_sample *sample,
409 struct perf_evsel *evsel,
410 struct thread *thread,
411 struct addr_location *al)
413 struct perf_event_attr *attr = &evsel->attr;
414 bool print_srcline_last = false;
416 /* print branch_from information */
417 if (PRINT_FIELD(IP)) {
418 unsigned int print_opts = output[attr->type].print_ip_opts;
420 if (symbol_conf.use_callchain && sample->callchain) {
424 if (print_opts & PRINT_IP_OPT_SRCLINE) {
425 print_srcline_last = true;
426 print_opts &= ~PRINT_IP_OPT_SRCLINE;
429 perf_evsel__print_ip(evsel, sample, al, print_opts,
430 PERF_MAX_STACK_DEPTH);
433 /* print branch_to information */
434 if (PRINT_FIELD(ADDR) ||
435 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
436 !output[attr->type].user_set)) {
438 print_sample_addr(event, sample, al->machine, thread, attr);
441 if (print_srcline_last)
442 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
447 static void process_event(union perf_event *event, struct perf_sample *sample,
448 struct perf_evsel *evsel, struct thread *thread,
449 struct addr_location *al)
451 struct perf_event_attr *attr = &evsel->attr;
453 if (output[attr->type].fields == 0)
456 print_sample_start(sample, thread, evsel);
458 if (PRINT_FIELD(PERIOD))
459 printf("%10" PRIu64 " ", sample->period);
461 if (PRINT_FIELD(EVNAME)) {
462 const char *evname = perf_evsel__name(evsel);
463 printf("%s: ", evname ? evname : "[unknown]");
466 if (is_bts_event(attr)) {
467 print_sample_bts(event, sample, evsel, thread, al);
471 if (PRINT_FIELD(TRACE))
472 event_format__print(evsel->tp_format, sample->cpu,
473 sample->raw_data, sample->raw_size);
474 if (PRINT_FIELD(ADDR))
475 print_sample_addr(event, sample, al->machine, thread, attr);
477 if (PRINT_FIELD(IP)) {
478 if (!symbol_conf.use_callchain)
483 perf_evsel__print_ip(evsel, sample, al,
484 output[attr->type].print_ip_opts,
485 PERF_MAX_STACK_DEPTH);
491 static int default_start_script(const char *script __maybe_unused,
492 int argc __maybe_unused,
493 const char **argv __maybe_unused)
498 static int default_flush_script(void)
503 static int default_stop_script(void)
508 static int default_generate_script(struct pevent *pevent __maybe_unused,
509 const char *outfile __maybe_unused)
514 static struct scripting_ops default_scripting_ops = {
515 .start_script = default_start_script,
516 .flush_script = default_flush_script,
517 .stop_script = default_stop_script,
518 .process_event = process_event,
519 .generate_script = default_generate_script,
522 static struct scripting_ops *scripting_ops;
524 static void setup_scripting(void)
526 setup_perl_scripting();
527 setup_python_scripting();
529 scripting_ops = &default_scripting_ops;
532 static int flush_scripting(void)
534 return scripting_ops->flush_script();
537 static int cleanup_scripting(void)
539 pr_debug("\nperf script stopped\n");
541 return scripting_ops->stop_script();
544 static int process_sample_event(struct perf_tool *tool __maybe_unused,
545 union perf_event *event,
546 struct perf_sample *sample,
547 struct perf_evsel *evsel,
548 struct machine *machine)
550 struct addr_location al;
551 struct thread *thread = machine__findnew_thread(machine, sample->pid,
554 if (thread == NULL) {
555 pr_debug("problem processing %d event, skipping it.\n",
561 if (sample->time < last_timestamp) {
562 pr_err("Samples misordered, previous: %" PRIu64
563 " this: %" PRIu64 "\n", last_timestamp,
567 last_timestamp = sample->time;
571 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
572 pr_err("problem processing %d event, skipping it.\n",
580 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
583 scripting_ops->process_event(event, sample, evsel, thread, &al);
589 struct perf_tool tool;
590 struct perf_session *session;
591 bool show_task_events;
592 bool show_mmap_events;
595 static int process_attr(struct perf_tool *tool, union perf_event *event,
596 struct perf_evlist **pevlist)
598 struct perf_script *scr = container_of(tool, struct perf_script, tool);
599 struct perf_evlist *evlist;
600 struct perf_evsel *evsel, *pos;
603 err = perf_event__process_attr(tool, event, pevlist);
608 evsel = perf_evlist__last(*pevlist);
610 if (evsel->attr.type >= PERF_TYPE_MAX)
613 evlist__for_each(evlist, pos) {
614 if (pos->attr.type == evsel->attr.type && pos != evsel)
618 set_print_ip_opts(&evsel->attr);
620 return perf_evsel__check_attr(evsel, scr->session);
623 static int process_comm_event(struct perf_tool *tool,
624 union perf_event *event,
625 struct perf_sample *sample,
626 struct machine *machine)
628 struct thread *thread;
629 struct perf_script *script = container_of(tool, struct perf_script, tool);
630 struct perf_session *session = script->session;
631 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
634 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
635 if (thread == NULL) {
636 pr_debug("problem processing COMM event, skipping it.\n");
640 if (perf_event__process_comm(tool, event, sample, machine) < 0)
643 if (!evsel->attr.sample_id_all) {
646 sample->tid = event->comm.tid;
647 sample->pid = event->comm.pid;
649 print_sample_start(sample, thread, evsel);
650 perf_event__fprintf(event, stdout);
657 static int process_fork_event(struct perf_tool *tool,
658 union perf_event *event,
659 struct perf_sample *sample,
660 struct machine *machine)
662 struct thread *thread;
663 struct perf_script *script = container_of(tool, struct perf_script, tool);
664 struct perf_session *session = script->session;
665 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
667 if (perf_event__process_fork(tool, event, sample, machine) < 0)
670 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
671 if (thread == NULL) {
672 pr_debug("problem processing FORK event, skipping it.\n");
676 if (!evsel->attr.sample_id_all) {
678 sample->time = event->fork.time;
679 sample->tid = event->fork.tid;
680 sample->pid = event->fork.pid;
682 print_sample_start(sample, thread, evsel);
683 perf_event__fprintf(event, stdout);
687 static int process_exit_event(struct perf_tool *tool,
688 union perf_event *event,
689 struct perf_sample *sample,
690 struct machine *machine)
692 struct thread *thread;
693 struct perf_script *script = container_of(tool, struct perf_script, tool);
694 struct perf_session *session = script->session;
695 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
697 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
698 if (thread == NULL) {
699 pr_debug("problem processing EXIT event, skipping it.\n");
703 if (!evsel->attr.sample_id_all) {
706 sample->tid = event->comm.tid;
707 sample->pid = event->comm.pid;
709 print_sample_start(sample, thread, evsel);
710 perf_event__fprintf(event, stdout);
712 if (perf_event__process_exit(tool, event, sample, machine) < 0)
718 static int process_mmap_event(struct perf_tool *tool,
719 union perf_event *event,
720 struct perf_sample *sample,
721 struct machine *machine)
723 struct thread *thread;
724 struct perf_script *script = container_of(tool, struct perf_script, tool);
725 struct perf_session *session = script->session;
726 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
728 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
731 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
732 if (thread == NULL) {
733 pr_debug("problem processing MMAP event, skipping it.\n");
737 if (!evsel->attr.sample_id_all) {
740 sample->tid = event->mmap.tid;
741 sample->pid = event->mmap.pid;
743 print_sample_start(sample, thread, evsel);
744 perf_event__fprintf(event, stdout);
749 static int process_mmap2_event(struct perf_tool *tool,
750 union perf_event *event,
751 struct perf_sample *sample,
752 struct machine *machine)
754 struct thread *thread;
755 struct perf_script *script = container_of(tool, struct perf_script, tool);
756 struct perf_session *session = script->session;
757 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
759 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
762 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
763 if (thread == NULL) {
764 pr_debug("problem processing MMAP2 event, skipping it.\n");
768 if (!evsel->attr.sample_id_all) {
771 sample->tid = event->mmap2.tid;
772 sample->pid = event->mmap2.pid;
774 print_sample_start(sample, thread, evsel);
775 perf_event__fprintf(event, stdout);
780 static void sig_handler(int sig __maybe_unused)
785 static int __cmd_script(struct perf_script *script)
789 signal(SIGINT, sig_handler);
791 /* override event processing functions */
792 if (script->show_task_events) {
793 script->tool.comm = process_comm_event;
794 script->tool.fork = process_fork_event;
795 script->tool.exit = process_exit_event;
797 if (script->show_mmap_events) {
798 script->tool.mmap = process_mmap_event;
799 script->tool.mmap2 = process_mmap2_event;
802 ret = perf_session__process_events(script->session, &script->tool);
805 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
811 struct list_head node;
812 struct scripting_ops *ops;
816 static LIST_HEAD(script_specs);
818 static struct script_spec *script_spec__new(const char *spec,
819 struct scripting_ops *ops)
821 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
824 strcpy(s->spec, spec);
831 static void script_spec__add(struct script_spec *s)
833 list_add_tail(&s->node, &script_specs);
836 static struct script_spec *script_spec__find(const char *spec)
838 struct script_spec *s;
840 list_for_each_entry(s, &script_specs, node)
841 if (strcasecmp(s->spec, spec) == 0)
846 static struct script_spec *script_spec__findnew(const char *spec,
847 struct scripting_ops *ops)
849 struct script_spec *s = script_spec__find(spec);
854 s = script_spec__new(spec, ops);
863 int script_spec_register(const char *spec, struct scripting_ops *ops)
865 struct script_spec *s;
867 s = script_spec__find(spec);
871 s = script_spec__findnew(spec, ops);
878 static struct scripting_ops *script_spec__lookup(const char *spec)
880 struct script_spec *s = script_spec__find(spec);
887 static void list_available_languages(void)
889 struct script_spec *s;
891 fprintf(stderr, "\n");
892 fprintf(stderr, "Scripting language extensions (used in "
893 "perf script -s [spec:]script.[spec]):\n\n");
895 list_for_each_entry(s, &script_specs, node)
896 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
898 fprintf(stderr, "\n");
901 static int parse_scriptname(const struct option *opt __maybe_unused,
902 const char *str, int unset __maybe_unused)
905 const char *script, *ext;
908 if (strcmp(str, "lang") == 0) {
909 list_available_languages();
913 script = strchr(str, ':');
916 if (len >= PATH_MAX) {
917 fprintf(stderr, "invalid language specifier");
920 strncpy(spec, str, len);
922 scripting_ops = script_spec__lookup(spec);
923 if (!scripting_ops) {
924 fprintf(stderr, "invalid language specifier");
930 ext = strrchr(script, '.');
932 fprintf(stderr, "invalid script extension");
935 scripting_ops = script_spec__lookup(++ext);
936 if (!scripting_ops) {
937 fprintf(stderr, "invalid script extension");
942 script_name = strdup(script);
947 static int parse_output_fields(const struct option *opt __maybe_unused,
948 const char *arg, int unset __maybe_unused)
951 int i, imax = ARRAY_SIZE(all_output_options);
954 char *str = strdup(arg);
960 /* first word can state for which event type the user is specifying
961 * the fields. If no type exists, the specified fields apply to all
962 * event types found in the file minus the invalid fields for a type.
964 tok = strchr(str, ':');
968 if (!strcmp(str, "hw"))
969 type = PERF_TYPE_HARDWARE;
970 else if (!strcmp(str, "sw"))
971 type = PERF_TYPE_SOFTWARE;
972 else if (!strcmp(str, "trace"))
973 type = PERF_TYPE_TRACEPOINT;
974 else if (!strcmp(str, "raw"))
975 type = PERF_TYPE_RAW;
977 fprintf(stderr, "Invalid event type in field string.\n");
982 if (output[type].user_set)
983 pr_warning("Overriding previous field request for %s events.\n",
986 output[type].fields = 0;
987 output[type].user_set = true;
988 output[type].wildcard_set = false;
992 if (strlen(str) == 0) {
994 "Cannot set fields to 'none' for all event types.\n");
999 if (output_set_by_user())
1000 pr_warning("Overriding previous field request for all events.\n");
1002 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1003 output[j].fields = 0;
1004 output[j].user_set = true;
1005 output[j].wildcard_set = true;
1009 tok = strtok(tok, ",");
1011 for (i = 0; i < imax; ++i) {
1012 if (strcmp(tok, all_output_options[i].str) == 0)
1016 fprintf(stderr, "Invalid field requested.\n");
1022 /* add user option to all events types for
1025 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1026 if (output[j].invalid_fields & all_output_options[i].field) {
1027 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1028 all_output_options[i].str, event_type(j));
1030 output[j].fields |= all_output_options[i].field;
1033 if (output[type].invalid_fields & all_output_options[i].field) {
1034 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1035 all_output_options[i].str, event_type(type));
1040 output[type].fields |= all_output_options[i].field;
1043 tok = strtok(NULL, ",");
1047 if (output[type].fields == 0) {
1048 pr_debug("No fields requested for %s type. "
1049 "Events will not be displayed.\n", event_type(type));
1058 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1059 static int is_directory(const char *base_path, const struct dirent *dent)
1061 char path[PATH_MAX];
1064 sprintf(path, "%s/%s", base_path, dent->d_name);
1065 if (stat(path, &st))
1068 return S_ISDIR(st.st_mode);
1071 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
1072 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1074 if ((lang_dirent.d_type == DT_DIR || \
1075 (lang_dirent.d_type == DT_UNKNOWN && \
1076 is_directory(scripts_path, &lang_dirent))) && \
1077 (strcmp(lang_dirent.d_name, ".")) && \
1078 (strcmp(lang_dirent.d_name, "..")))
1080 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
1081 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1083 if (script_dirent.d_type != DT_DIR && \
1084 (script_dirent.d_type != DT_UNKNOWN || \
1085 !is_directory(lang_path, &script_dirent)))
1088 #define RECORD_SUFFIX "-record"
1089 #define REPORT_SUFFIX "-report"
1091 struct script_desc {
1092 struct list_head node;
1098 static LIST_HEAD(script_descs);
1100 static struct script_desc *script_desc__new(const char *name)
1102 struct script_desc *s = zalloc(sizeof(*s));
1104 if (s != NULL && name)
1105 s->name = strdup(name);
1110 static void script_desc__delete(struct script_desc *s)
1113 zfree(&s->half_liner);
1118 static void script_desc__add(struct script_desc *s)
1120 list_add_tail(&s->node, &script_descs);
1123 static struct script_desc *script_desc__find(const char *name)
1125 struct script_desc *s;
1127 list_for_each_entry(s, &script_descs, node)
1128 if (strcasecmp(s->name, name) == 0)
1133 static struct script_desc *script_desc__findnew(const char *name)
1135 struct script_desc *s = script_desc__find(name);
1140 s = script_desc__new(name);
1142 goto out_delete_desc;
1144 script_desc__add(s);
1149 script_desc__delete(s);
1154 static const char *ends_with(const char *str, const char *suffix)
1156 size_t suffix_len = strlen(suffix);
1157 const char *p = str;
1159 if (strlen(str) > suffix_len) {
1160 p = str + strlen(str) - suffix_len;
1161 if (!strncmp(p, suffix, suffix_len))
1168 static int read_script_info(struct script_desc *desc, const char *filename)
1170 char line[BUFSIZ], *p;
1173 fp = fopen(filename, "r");
1177 while (fgets(line, sizeof(line), fp)) {
1184 if (strlen(p) && *p == '!')
1188 if (strlen(p) && p[strlen(p) - 1] == '\n')
1189 p[strlen(p) - 1] = '\0';
1191 if (!strncmp(p, "description:", strlen("description:"))) {
1192 p += strlen("description:");
1193 desc->half_liner = strdup(ltrim(p));
1197 if (!strncmp(p, "args:", strlen("args:"))) {
1198 p += strlen("args:");
1199 desc->args = strdup(ltrim(p));
1209 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1211 char *script_root, *str;
1213 script_root = strdup(script_dirent->d_name);
1217 str = (char *)ends_with(script_root, suffix);
1227 static int list_available_scripts(const struct option *opt __maybe_unused,
1228 const char *s __maybe_unused,
1229 int unset __maybe_unused)
1231 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1232 char scripts_path[MAXPATHLEN];
1233 DIR *scripts_dir, *lang_dir;
1234 char script_path[MAXPATHLEN];
1235 char lang_path[MAXPATHLEN];
1236 struct script_desc *desc;
1237 char first_half[BUFSIZ];
1240 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1242 scripts_dir = opendir(scripts_path);
1246 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1247 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1248 lang_dirent.d_name);
1249 lang_dir = opendir(lang_path);
1253 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1254 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1256 desc = script_desc__findnew(script_root);
1257 snprintf(script_path, MAXPATHLEN, "%s/%s",
1258 lang_path, script_dirent.d_name);
1259 read_script_info(desc, script_path);
1265 fprintf(stdout, "List of available trace scripts:\n");
1266 list_for_each_entry(desc, &script_descs, node) {
1267 sprintf(first_half, "%s %s", desc->name,
1268 desc->args ? desc->args : "");
1269 fprintf(stdout, " %-36s %s\n", first_half,
1270 desc->half_liner ? desc->half_liner : "");
1277 * Some scripts specify the required events in their "xxx-record" file,
1278 * this function will check if the events in perf.data match those
1279 * mentioned in the "xxx-record".
1281 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1282 * which is covered well now. And new parsing code should be added to
1283 * cover the future complexing formats like event groups etc.
1285 static int check_ev_match(char *dir_name, char *scriptname,
1286 struct perf_session *session)
1288 char filename[MAXPATHLEN], evname[128];
1289 char line[BUFSIZ], *p;
1290 struct perf_evsel *pos;
1294 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1296 fp = fopen(filename, "r");
1300 while (fgets(line, sizeof(line), fp)) {
1306 p = strstr(p, "-e");
1312 len = strcspn(p, " \t");
1316 snprintf(evname, len + 1, "%s", p);
1319 evlist__for_each(session->evlist, pos) {
1320 if (!strcmp(perf_evsel__name(pos), evname)) {
1338 * Return -1 if none is found, otherwise the actual scripts number.
1340 * Currently the only user of this function is the script browser, which
1341 * will list all statically runnable scripts, select one, execute it and
1342 * show the output in a perf browser.
1344 int find_scripts(char **scripts_array, char **scripts_path_array)
1346 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1347 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
1348 DIR *scripts_dir, *lang_dir;
1349 struct perf_session *session;
1350 struct perf_data_file file = {
1352 .mode = PERF_DATA_MODE_READ,
1357 session = perf_session__new(&file, false, NULL);
1361 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1363 scripts_dir = opendir(scripts_path);
1365 perf_session__delete(session);
1369 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1370 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1371 lang_dirent.d_name);
1373 if (strstr(lang_path, "perl"))
1377 if (strstr(lang_path, "python"))
1381 lang_dir = opendir(lang_path);
1385 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1386 /* Skip those real time scripts: xxxtop.p[yl] */
1387 if (strstr(script_dirent.d_name, "top."))
1389 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1390 script_dirent.d_name);
1391 temp = strchr(script_dirent.d_name, '.');
1392 snprintf(scripts_array[i],
1393 (temp - script_dirent.d_name) + 1,
1394 "%s", script_dirent.d_name);
1396 if (check_ev_match(lang_path,
1397 scripts_array[i], session))
1405 closedir(scripts_dir);
1406 perf_session__delete(session);
1410 static char *get_script_path(const char *script_root, const char *suffix)
1412 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1413 char scripts_path[MAXPATHLEN];
1414 char script_path[MAXPATHLEN];
1415 DIR *scripts_dir, *lang_dir;
1416 char lang_path[MAXPATHLEN];
1417 char *__script_root;
1419 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1421 scripts_dir = opendir(scripts_path);
1425 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1426 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1427 lang_dirent.d_name);
1428 lang_dir = opendir(lang_path);
1432 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1433 __script_root = get_script_root(&script_dirent, suffix);
1434 if (__script_root && !strcmp(script_root, __script_root)) {
1435 free(__script_root);
1437 closedir(scripts_dir);
1438 snprintf(script_path, MAXPATHLEN, "%s/%s",
1439 lang_path, script_dirent.d_name);
1440 return strdup(script_path);
1442 free(__script_root);
1446 closedir(scripts_dir);
1451 static bool is_top_script(const char *script_path)
1453 return ends_with(script_path, "top") == NULL ? false : true;
1456 static int has_required_arg(char *script_path)
1458 struct script_desc *desc;
1462 desc = script_desc__new(NULL);
1464 if (read_script_info(desc, script_path))
1470 for (p = desc->args; *p; p++)
1474 script_desc__delete(desc);
1479 static int have_cmd(int argc, const char **argv)
1481 char **__argv = malloc(sizeof(const char *) * argc);
1484 pr_err("malloc failed\n");
1488 memcpy(__argv, argv, sizeof(const char *) * argc);
1489 argc = parse_options(argc, (const char **)__argv, record_options,
1490 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1493 system_wide = (argc == 0);
1498 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1500 bool show_full_info = false;
1501 bool header = false;
1502 bool header_only = false;
1503 bool script_started = false;
1504 char *rec_script_path = NULL;
1505 char *rep_script_path = NULL;
1506 struct perf_session *session;
1507 char *script_path = NULL;
1508 const char **__argv;
1510 struct perf_script script = {
1512 .sample = process_sample_event,
1513 .mmap = perf_event__process_mmap,
1514 .mmap2 = perf_event__process_mmap2,
1515 .comm = perf_event__process_comm,
1516 .exit = perf_event__process_exit,
1517 .fork = perf_event__process_fork,
1518 .attr = process_attr,
1519 .tracing_data = perf_event__process_tracing_data,
1520 .build_id = perf_event__process_build_id,
1521 .ordered_events = true,
1522 .ordering_requires_timestamps = true,
1525 const struct option options[] = {
1526 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1527 "dump raw trace in ASCII"),
1528 OPT_INCR('v', "verbose", &verbose,
1529 "be more verbose (show symbol address, etc)"),
1530 OPT_BOOLEAN('L', "Latency", &latency_format,
1531 "show latency attributes (irqs/preemption disabled, etc)"),
1532 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1533 list_available_scripts),
1534 OPT_CALLBACK('s', "script", NULL, "name",
1535 "script file name (lang:script name, script name, or *)",
1537 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1538 "generate perf-script.xx script in specified language"),
1539 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1540 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1541 "do various checks like samples ordering and lost events"),
1542 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1543 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
1544 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1545 "file", "vmlinux pathname"),
1546 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1547 "file", "kallsyms pathname"),
1548 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1549 "When printing symbols do not display call chain"),
1550 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1551 "Look for files with symbols relative to this directory"),
1552 OPT_CALLBACK('f', "fields", NULL, "str",
1553 "comma separated output fields prepend with 'type:'. "
1554 "Valid types: hw,sw,trace,raw. "
1555 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1556 "addr,symoff,period", parse_output_fields),
1557 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1558 "system-wide collection from all CPUs"),
1559 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1560 "only consider these symbols"),
1561 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1562 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1563 "only display events for these comms"),
1564 OPT_BOOLEAN('I', "show-info", &show_full_info,
1565 "display extended information from perf.data file"),
1566 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1567 "Show the path of [kernel.kallsyms]"),
1568 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1569 "Show the fork/comm/exit events"),
1570 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1571 "Show the mmap events"),
1574 const char * const script_usage[] = {
1575 "perf script [<options>]",
1576 "perf script [<options>] record <script> [<record-options>] <command>",
1577 "perf script [<options>] report <script> [script-args]",
1578 "perf script [<options>] <script> [<record-options>] <command>",
1579 "perf script [<options>] <top-script> [script-args]",
1582 struct perf_data_file file = {
1583 .mode = PERF_DATA_MODE_READ,
1588 argc = parse_options(argc, argv, options, script_usage,
1589 PARSE_OPT_STOP_AT_NON_OPTION);
1591 file.path = input_name;
1593 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1594 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1595 if (!rec_script_path)
1596 return cmd_record(argc, argv, NULL);
1599 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1600 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1601 if (!rep_script_path) {
1603 "Please specify a valid report script"
1604 "(see 'perf script -l' for listing)\n");
1609 /* make sure PERF_EXEC_PATH is set for scripts */
1610 perf_set_argv_exec_path(perf_exec_path());
1612 if (argc && !script_name && !rec_script_path && !rep_script_path) {
1617 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1618 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1620 if (!rec_script_path && !rep_script_path) {
1621 fprintf(stderr, " Couldn't find script %s\n\n See perf"
1622 " script -l for available scripts.\n", argv[0]);
1623 usage_with_options(script_usage, options);
1626 if (is_top_script(argv[0])) {
1627 rep_args = argc - 1;
1631 rep_args = has_required_arg(rep_script_path);
1632 rec_args = (argc - 1) - rep_args;
1634 fprintf(stderr, " %s script requires options."
1635 "\n\n See perf script -l for available "
1636 "scripts and options.\n", argv[0]);
1637 usage_with_options(script_usage, options);
1641 if (pipe(live_pipe) < 0) {
1642 perror("failed to create pipe");
1648 perror("failed to fork");
1655 dup2(live_pipe[1], 1);
1656 close(live_pipe[0]);
1658 if (is_top_script(argv[0])) {
1660 } else if (!system_wide) {
1661 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1667 __argv = malloc((argc + 6) * sizeof(const char *));
1669 pr_err("malloc failed\n");
1674 __argv[j++] = "/bin/sh";
1675 __argv[j++] = rec_script_path;
1681 for (i = rep_args + 1; i < argc; i++)
1682 __argv[j++] = argv[i];
1685 execvp("/bin/sh", (char **)__argv);
1690 dup2(live_pipe[0], 0);
1691 close(live_pipe[1]);
1693 __argv = malloc((argc + 4) * sizeof(const char *));
1695 pr_err("malloc failed\n");
1701 __argv[j++] = "/bin/sh";
1702 __argv[j++] = rep_script_path;
1703 for (i = 1; i < rep_args + 1; i++)
1704 __argv[j++] = argv[i];
1709 execvp("/bin/sh", (char **)__argv);
1714 if (rec_script_path)
1715 script_path = rec_script_path;
1716 if (rep_script_path)
1717 script_path = rep_script_path;
1722 if (!rec_script_path)
1723 system_wide = false;
1724 else if (!system_wide) {
1725 if (have_cmd(argc - 1, &argv[1]) != 0) {
1731 __argv = malloc((argc + 2) * sizeof(const char *));
1733 pr_err("malloc failed\n");
1738 __argv[j++] = "/bin/sh";
1739 __argv[j++] = script_path;
1742 for (i = 2; i < argc; i++)
1743 __argv[j++] = argv[i];
1746 execvp("/bin/sh", (char **)__argv);
1754 session = perf_session__new(&file, false, &script.tool);
1755 if (session == NULL)
1758 if (header || header_only) {
1759 perf_session__fprintf_info(session, stdout, show_full_info);
1764 if (symbol__init(&session->header.env) < 0)
1767 script.session = session;
1770 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1776 symbol_conf.use_callchain = true;
1778 symbol_conf.use_callchain = false;
1780 if (generate_script_lang) {
1781 struct stat perf_stat;
1784 if (output_set_by_user()) {
1786 "custom fields not supported for generated scripts");
1791 input = open(file.path, O_RDONLY); /* input_name */
1794 perror("failed to open file");
1798 err = fstat(input, &perf_stat);
1800 perror("failed to stat file");
1804 if (!perf_stat.st_size) {
1805 fprintf(stderr, "zero-sized file, nothing to do!\n");
1809 scripting_ops = script_spec__lookup(generate_script_lang);
1810 if (!scripting_ops) {
1811 fprintf(stderr, "invalid language specifier");
1816 err = scripting_ops->generate_script(session->tevent.pevent,
1822 err = scripting_ops->start_script(script_name, argc, argv);
1825 pr_debug("perf script started with script %s\n\n", script_name);
1826 script_started = true;
1830 err = perf_session__check_output_opt(session);
1834 err = __cmd_script(&script);
1839 perf_session__delete(session);
1842 cleanup_scripting();