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,
48 struct output_option
{
50 enum perf_output_field field
;
51 } all_output_options
[] = {
52 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
53 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
54 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
55 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
56 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
57 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
58 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
59 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
60 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
61 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
62 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
63 {.str
= "symoff", .field
= PERF_OUTPUT_SYMOFFSET
},
66 /* default set to maintain compatibility with current format */
70 unsigned int print_ip_opts
;
73 } output
[PERF_TYPE_MAX
] = {
75 [PERF_TYPE_HARDWARE
] = {
78 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
79 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
80 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
81 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
,
83 .invalid_fields
= PERF_OUTPUT_TRACE
,
86 [PERF_TYPE_SOFTWARE
] = {
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
,
94 .invalid_fields
= PERF_OUTPUT_TRACE
,
97 [PERF_TYPE_TRACEPOINT
] = {
100 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
101 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
102 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
,
108 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
109 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
110 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
111 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
,
113 .invalid_fields
= PERF_OUTPUT_TRACE
,
117 static bool output_set_by_user(void)
120 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
121 if (output
[j
].user_set
)
127 static const char *output_field2str(enum perf_output_field field
)
129 int i
, imax
= ARRAY_SIZE(all_output_options
);
130 const char *str
= "";
132 for (i
= 0; i
< imax
; ++i
) {
133 if (all_output_options
[i
].field
== field
) {
134 str
= all_output_options
[i
].str
;
141 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
143 static int perf_evsel__check_stype(struct perf_evsel
*evsel
,
144 u64 sample_type
, const char *sample_msg
,
145 enum perf_output_field field
)
147 struct perf_event_attr
*attr
= &evsel
->attr
;
148 int type
= attr
->type
;
151 if (attr
->sample_type
& sample_type
)
154 if (output
[type
].user_set
) {
155 evname
= perf_evsel__name(evsel
);
156 pr_err("Samples for '%s' event do not have %s attribute set. "
157 "Cannot print '%s' field.\n",
158 evname
, sample_msg
, output_field2str(field
));
162 /* user did not ask for it explicitly so remove from the default list */
163 output
[type
].fields
&= ~field
;
164 evname
= perf_evsel__name(evsel
);
165 pr_debug("Samples for '%s' event do not have %s attribute set. "
166 "Skipping '%s' field.\n",
167 evname
, sample_msg
, output_field2str(field
));
172 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
173 struct perf_session
*session
)
175 struct perf_event_attr
*attr
= &evsel
->attr
;
177 if (PRINT_FIELD(TRACE
) &&
178 !perf_session__has_traces(session
, "record -R"))
181 if (PRINT_FIELD(IP
)) {
182 if (perf_evsel__check_stype(evsel
, PERF_SAMPLE_IP
, "IP",
187 !(attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
))
188 symbol_conf
.use_callchain
= false;
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 "
214 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
215 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TID
, "TID",
216 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
219 if (PRINT_FIELD(TIME
) &&
220 perf_evsel__check_stype(evsel
, PERF_SAMPLE_TIME
, "TIME",
224 if (PRINT_FIELD(CPU
) &&
225 perf_evsel__check_stype(evsel
, PERF_SAMPLE_CPU
, "CPU",
233 * verify all user requested events exist and the samples
234 * have the expected data
236 static int perf_session__check_output_opt(struct perf_session
*session
)
239 struct perf_evsel
*evsel
;
240 struct perf_event_attr
*attr
;
242 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
243 evsel
= perf_session__find_first_evtype(session
, j
);
246 * even if fields is set to 0 (ie., show nothing) event must
247 * exist if user explicitly includes it on the command line
249 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
) {
250 pr_err("%s events do not exist. "
251 "Remove corresponding -f option to proceed.\n",
256 if (evsel
&& output
[j
].fields
&&
257 perf_evsel__check_attr(evsel
, session
))
265 output
[j
].print_ip_opts
= 0;
267 output
[j
].print_ip_opts
|= PRINT_IP_OPT_IP
;
269 if (PRINT_FIELD(SYM
))
270 output
[j
].print_ip_opts
|= PRINT_IP_OPT_SYM
;
272 if (PRINT_FIELD(DSO
))
273 output
[j
].print_ip_opts
|= PRINT_IP_OPT_DSO
;
275 if (PRINT_FIELD(SYMOFFSET
))
276 output
[j
].print_ip_opts
|= PRINT_IP_OPT_SYMOFFSET
;
282 static void print_sample_start(struct perf_sample
*sample
,
283 struct thread
*thread
,
284 struct perf_evsel
*evsel
)
286 struct perf_event_attr
*attr
= &evsel
->attr
;
287 const char *evname
= NULL
;
290 unsigned long long nsecs
;
292 if (PRINT_FIELD(COMM
)) {
294 printf("%8.8s ", thread
->comm
);
295 else if (PRINT_FIELD(IP
) && symbol_conf
.use_callchain
)
296 printf("%s ", thread
->comm
);
298 printf("%16s ", thread
->comm
);
301 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
302 printf("%5d/%-5d ", sample
->pid
, sample
->tid
);
303 else if (PRINT_FIELD(PID
))
304 printf("%5d ", sample
->pid
);
305 else if (PRINT_FIELD(TID
))
306 printf("%5d ", sample
->tid
);
308 if (PRINT_FIELD(CPU
)) {
310 printf("%3d ", sample
->cpu
);
312 printf("[%03d] ", sample
->cpu
);
315 if (PRINT_FIELD(TIME
)) {
316 nsecs
= sample
->time
;
317 secs
= nsecs
/ NSECS_PER_SEC
;
318 nsecs
-= secs
* NSECS_PER_SEC
;
319 usecs
= nsecs
/ NSECS_PER_USEC
;
320 printf("%5lu.%06lu: ", secs
, usecs
);
323 if (PRINT_FIELD(EVNAME
)) {
324 evname
= perf_evsel__name(evsel
);
325 printf("%s: ", evname
? evname
: "[unknown]");
329 static bool is_bts_event(struct perf_event_attr
*attr
)
331 return ((attr
->type
== PERF_TYPE_HARDWARE
) &&
332 (attr
->config
& PERF_COUNT_HW_BRANCH_INSTRUCTIONS
) &&
333 (attr
->sample_period
== 1));
336 static bool sample_addr_correlates_sym(struct perf_event_attr
*attr
)
338 if ((attr
->type
== PERF_TYPE_SOFTWARE
) &&
339 ((attr
->config
== PERF_COUNT_SW_PAGE_FAULTS
) ||
340 (attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MIN
) ||
341 (attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
)))
344 if (is_bts_event(attr
))
350 static void print_sample_addr(union perf_event
*event
,
351 struct perf_sample
*sample
,
352 struct machine
*machine
,
353 struct thread
*thread
,
354 struct perf_event_attr
*attr
)
356 struct addr_location al
;
357 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
359 printf("%16" PRIx64
, sample
->addr
);
361 if (!sample_addr_correlates_sym(attr
))
364 thread__find_addr_map(thread
, machine
, cpumode
, MAP__FUNCTION
,
367 thread__find_addr_map(thread
, machine
, cpumode
, MAP__VARIABLE
,
370 al
.cpu
= sample
->cpu
;
374 al
.sym
= map__find_symbol(al
.map
, al
.addr
, NULL
);
376 if (PRINT_FIELD(SYM
)) {
378 if (PRINT_FIELD(SYMOFFSET
))
379 symbol__fprintf_symname_offs(al
.sym
, &al
, stdout
);
381 symbol__fprintf_symname(al
.sym
, stdout
);
384 if (PRINT_FIELD(DSO
)) {
386 map__fprintf_dsoname(al
.map
, stdout
);
391 static void print_sample_bts(union perf_event
*event
,
392 struct perf_sample
*sample
,
393 struct perf_evsel
*evsel
,
394 struct machine
*machine
,
395 struct thread
*thread
)
397 struct perf_event_attr
*attr
= &evsel
->attr
;
399 /* print branch_from information */
400 if (PRINT_FIELD(IP
)) {
401 if (!symbol_conf
.use_callchain
)
405 perf_evsel__print_ip(evsel
, event
, sample
, machine
,
406 output
[attr
->type
].print_ip_opts
,
407 PERF_MAX_STACK_DEPTH
);
412 /* print branch_to information */
413 if (PRINT_FIELD(ADDR
) ||
414 ((evsel
->attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
415 !output
[attr
->type
].user_set
))
416 print_sample_addr(event
, sample
, machine
, thread
, attr
);
421 static void process_event(union perf_event
*event
, struct perf_sample
*sample
,
422 struct perf_evsel
*evsel
, struct machine
*machine
,
423 struct thread
*thread
,
424 struct addr_location
*al __maybe_unused
)
426 struct perf_event_attr
*attr
= &evsel
->attr
;
428 if (output
[attr
->type
].fields
== 0)
431 print_sample_start(sample
, thread
, evsel
);
433 if (is_bts_event(attr
)) {
434 print_sample_bts(event
, sample
, evsel
, machine
, thread
);
438 if (PRINT_FIELD(TRACE
))
439 event_format__print(evsel
->tp_format
, sample
->cpu
,
440 sample
->raw_data
, sample
->raw_size
);
441 if (PRINT_FIELD(ADDR
))
442 print_sample_addr(event
, sample
, machine
, thread
, attr
);
444 if (PRINT_FIELD(IP
)) {
445 if (!symbol_conf
.use_callchain
)
450 perf_evsel__print_ip(evsel
, event
, sample
, machine
,
451 output
[attr
->type
].print_ip_opts
,
452 PERF_MAX_STACK_DEPTH
);
458 static int default_start_script(const char *script __maybe_unused
,
459 int argc __maybe_unused
,
460 const char **argv __maybe_unused
)
465 static int default_stop_script(void)
470 static int default_generate_script(struct pevent
*pevent __maybe_unused
,
471 const char *outfile __maybe_unused
)
476 static struct scripting_ops default_scripting_ops
= {
477 .start_script
= default_start_script
,
478 .stop_script
= default_stop_script
,
479 .process_event
= process_event
,
480 .generate_script
= default_generate_script
,
483 static struct scripting_ops
*scripting_ops
;
485 static void setup_scripting(void)
487 setup_perl_scripting();
488 setup_python_scripting();
490 scripting_ops
= &default_scripting_ops
;
493 static int cleanup_scripting(void)
495 pr_debug("\nperf script stopped\n");
497 return scripting_ops
->stop_script();
500 static int process_sample_event(struct perf_tool
*tool __maybe_unused
,
501 union perf_event
*event
,
502 struct perf_sample
*sample
,
503 struct perf_evsel
*evsel
,
504 struct machine
*machine
)
506 struct addr_location al
;
507 struct thread
*thread
= machine__findnew_thread(machine
, sample
->pid
,
510 if (thread
== NULL
) {
511 pr_debug("problem processing %d event, skipping it.\n",
517 if (sample
->time
< last_timestamp
) {
518 pr_err("Samples misordered, previous: %" PRIu64
519 " this: %" PRIu64
"\n", last_timestamp
,
523 last_timestamp
= sample
->time
;
527 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
) < 0) {
528 pr_err("problem processing %d event, skipping it.\n",
536 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
))
539 scripting_ops
->process_event(event
, sample
, evsel
, machine
, thread
, &al
);
541 evsel
->hists
.stats
.total_period
+= sample
->period
;
546 struct perf_tool tool
;
547 struct perf_session
*session
;
550 static void sig_handler(int sig __maybe_unused
)
555 static int __cmd_script(struct perf_script
*script
)
559 signal(SIGINT
, sig_handler
);
561 ret
= perf_session__process_events(script
->session
, &script
->tool
);
564 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
570 struct list_head node
;
571 struct scripting_ops
*ops
;
575 static LIST_HEAD(script_specs
);
577 static struct script_spec
*script_spec__new(const char *spec
,
578 struct scripting_ops
*ops
)
580 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
583 strcpy(s
->spec
, spec
);
590 static void script_spec__add(struct script_spec
*s
)
592 list_add_tail(&s
->node
, &script_specs
);
595 static struct script_spec
*script_spec__find(const char *spec
)
597 struct script_spec
*s
;
599 list_for_each_entry(s
, &script_specs
, node
)
600 if (strcasecmp(s
->spec
, spec
) == 0)
605 static struct script_spec
*script_spec__findnew(const char *spec
,
606 struct scripting_ops
*ops
)
608 struct script_spec
*s
= script_spec__find(spec
);
613 s
= script_spec__new(spec
, ops
);
622 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
624 struct script_spec
*s
;
626 s
= script_spec__find(spec
);
630 s
= script_spec__findnew(spec
, ops
);
637 static struct scripting_ops
*script_spec__lookup(const char *spec
)
639 struct script_spec
*s
= script_spec__find(spec
);
646 static void list_available_languages(void)
648 struct script_spec
*s
;
650 fprintf(stderr
, "\n");
651 fprintf(stderr
, "Scripting language extensions (used in "
652 "perf script -s [spec:]script.[spec]):\n\n");
654 list_for_each_entry(s
, &script_specs
, node
)
655 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
657 fprintf(stderr
, "\n");
660 static int parse_scriptname(const struct option
*opt __maybe_unused
,
661 const char *str
, int unset __maybe_unused
)
664 const char *script
, *ext
;
667 if (strcmp(str
, "lang") == 0) {
668 list_available_languages();
672 script
= strchr(str
, ':');
675 if (len
>= PATH_MAX
) {
676 fprintf(stderr
, "invalid language specifier");
679 strncpy(spec
, str
, len
);
681 scripting_ops
= script_spec__lookup(spec
);
682 if (!scripting_ops
) {
683 fprintf(stderr
, "invalid language specifier");
689 ext
= strrchr(script
, '.');
691 fprintf(stderr
, "invalid script extension");
694 scripting_ops
= script_spec__lookup(++ext
);
695 if (!scripting_ops
) {
696 fprintf(stderr
, "invalid script extension");
701 script_name
= strdup(script
);
706 static int parse_output_fields(const struct option
*opt __maybe_unused
,
707 const char *arg
, int unset __maybe_unused
)
710 int i
, imax
= ARRAY_SIZE(all_output_options
);
713 char *str
= strdup(arg
);
719 /* first word can state for which event type the user is specifying
720 * the fields. If no type exists, the specified fields apply to all
721 * event types found in the file minus the invalid fields for a type.
723 tok
= strchr(str
, ':');
727 if (!strcmp(str
, "hw"))
728 type
= PERF_TYPE_HARDWARE
;
729 else if (!strcmp(str
, "sw"))
730 type
= PERF_TYPE_SOFTWARE
;
731 else if (!strcmp(str
, "trace"))
732 type
= PERF_TYPE_TRACEPOINT
;
733 else if (!strcmp(str
, "raw"))
734 type
= PERF_TYPE_RAW
;
736 fprintf(stderr
, "Invalid event type in field string.\n");
741 if (output
[type
].user_set
)
742 pr_warning("Overriding previous field request for %s events.\n",
745 output
[type
].fields
= 0;
746 output
[type
].user_set
= true;
747 output
[type
].wildcard_set
= false;
751 if (strlen(str
) == 0) {
753 "Cannot set fields to 'none' for all event types.\n");
758 if (output_set_by_user())
759 pr_warning("Overriding previous field request for all events.\n");
761 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
762 output
[j
].fields
= 0;
763 output
[j
].user_set
= true;
764 output
[j
].wildcard_set
= true;
768 tok
= strtok(tok
, ",");
770 for (i
= 0; i
< imax
; ++i
) {
771 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
775 fprintf(stderr
, "Invalid field requested.\n");
781 /* add user option to all events types for
784 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
785 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
786 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
787 all_output_options
[i
].str
, event_type(j
));
789 output
[j
].fields
|= all_output_options
[i
].field
;
792 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
793 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
794 all_output_options
[i
].str
, event_type(type
));
799 output
[type
].fields
|= all_output_options
[i
].field
;
802 tok
= strtok(NULL
, ",");
806 if (output
[type
].fields
== 0) {
807 pr_debug("No fields requested for %s type. "
808 "Events will not be displayed.\n", event_type(type
));
817 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
818 static int is_directory(const char *base_path
, const struct dirent
*dent
)
823 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
827 return S_ISDIR(st
.st_mode
);
830 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
831 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
833 if ((lang_dirent.d_type == DT_DIR || \
834 (lang_dirent.d_type == DT_UNKNOWN && \
835 is_directory(scripts_path, &lang_dirent))) && \
836 (strcmp(lang_dirent.d_name, ".")) && \
837 (strcmp(lang_dirent.d_name, "..")))
839 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
840 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
842 if (script_dirent.d_type != DT_DIR && \
843 (script_dirent.d_type != DT_UNKNOWN || \
844 !is_directory(lang_path, &script_dirent)))
847 #define RECORD_SUFFIX "-record"
848 #define REPORT_SUFFIX "-report"
851 struct list_head node
;
857 static LIST_HEAD(script_descs
);
859 static struct script_desc
*script_desc__new(const char *name
)
861 struct script_desc
*s
= zalloc(sizeof(*s
));
863 if (s
!= NULL
&& name
)
864 s
->name
= strdup(name
);
869 static void script_desc__delete(struct script_desc
*s
)
877 static void script_desc__add(struct script_desc
*s
)
879 list_add_tail(&s
->node
, &script_descs
);
882 static struct script_desc
*script_desc__find(const char *name
)
884 struct script_desc
*s
;
886 list_for_each_entry(s
, &script_descs
, node
)
887 if (strcasecmp(s
->name
, name
) == 0)
892 static struct script_desc
*script_desc__findnew(const char *name
)
894 struct script_desc
*s
= script_desc__find(name
);
899 s
= script_desc__new(name
);
901 goto out_delete_desc
;
908 script_desc__delete(s
);
913 static const char *ends_with(const char *str
, const char *suffix
)
915 size_t suffix_len
= strlen(suffix
);
918 if (strlen(str
) > suffix_len
) {
919 p
= str
+ strlen(str
) - suffix_len
;
920 if (!strncmp(p
, suffix
, suffix_len
))
927 static int read_script_info(struct script_desc
*desc
, const char *filename
)
929 char line
[BUFSIZ
], *p
;
932 fp
= fopen(filename
, "r");
936 while (fgets(line
, sizeof(line
), fp
)) {
943 if (strlen(p
) && *p
== '!')
947 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
948 p
[strlen(p
) - 1] = '\0';
950 if (!strncmp(p
, "description:", strlen("description:"))) {
951 p
+= strlen("description:");
952 desc
->half_liner
= strdup(ltrim(p
));
956 if (!strncmp(p
, "args:", strlen("args:"))) {
957 p
+= strlen("args:");
958 desc
->args
= strdup(ltrim(p
));
968 static char *get_script_root(struct dirent
*script_dirent
, const char *suffix
)
970 char *script_root
, *str
;
972 script_root
= strdup(script_dirent
->d_name
);
976 str
= (char *)ends_with(script_root
, suffix
);
986 static int list_available_scripts(const struct option
*opt __maybe_unused
,
987 const char *s __maybe_unused
,
988 int unset __maybe_unused
)
990 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
991 char scripts_path
[MAXPATHLEN
];
992 DIR *scripts_dir
, *lang_dir
;
993 char script_path
[MAXPATHLEN
];
994 char lang_path
[MAXPATHLEN
];
995 struct script_desc
*desc
;
996 char first_half
[BUFSIZ
];
999 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
1001 scripts_dir
= opendir(scripts_path
);
1005 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
1006 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1007 lang_dirent
.d_name
);
1008 lang_dir
= opendir(lang_path
);
1012 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1013 script_root
= get_script_root(&script_dirent
, REPORT_SUFFIX
);
1015 desc
= script_desc__findnew(script_root
);
1016 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1017 lang_path
, script_dirent
.d_name
);
1018 read_script_info(desc
, script_path
);
1024 fprintf(stdout
, "List of available trace scripts:\n");
1025 list_for_each_entry(desc
, &script_descs
, node
) {
1026 sprintf(first_half
, "%s %s", desc
->name
,
1027 desc
->args
? desc
->args
: "");
1028 fprintf(stdout
, " %-36s %s\n", first_half
,
1029 desc
->half_liner
? desc
->half_liner
: "");
1036 * Some scripts specify the required events in their "xxx-record" file,
1037 * this function will check if the events in perf.data match those
1038 * mentioned in the "xxx-record".
1040 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1041 * which is covered well now. And new parsing code should be added to
1042 * cover the future complexing formats like event groups etc.
1044 static int check_ev_match(char *dir_name
, char *scriptname
,
1045 struct perf_session
*session
)
1047 char filename
[MAXPATHLEN
], evname
[128];
1048 char line
[BUFSIZ
], *p
;
1049 struct perf_evsel
*pos
;
1053 sprintf(filename
, "%s/bin/%s-record", dir_name
, scriptname
);
1055 fp
= fopen(filename
, "r");
1059 while (fgets(line
, sizeof(line
), fp
)) {
1065 p
= strstr(p
, "-e");
1071 len
= strcspn(p
, " \t");
1075 snprintf(evname
, len
+ 1, "%s", p
);
1078 list_for_each_entry(pos
,
1079 &session
->evlist
->entries
, node
) {
1080 if (!strcmp(perf_evsel__name(pos
), evname
)) {
1098 * Return -1 if none is found, otherwise the actual scripts number.
1100 * Currently the only user of this function is the script browser, which
1101 * will list all statically runnable scripts, select one, execute it and
1102 * show the output in a perf browser.
1104 int find_scripts(char **scripts_array
, char **scripts_path_array
)
1106 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
1107 char scripts_path
[MAXPATHLEN
], lang_path
[MAXPATHLEN
];
1108 DIR *scripts_dir
, *lang_dir
;
1109 struct perf_session
*session
;
1110 struct perf_data_file file
= {
1112 .mode
= PERF_DATA_MODE_READ
,
1117 session
= perf_session__new(&file
, false, NULL
);
1121 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
1123 scripts_dir
= opendir(scripts_path
);
1125 perf_session__delete(session
);
1129 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
1130 snprintf(lang_path
, MAXPATHLEN
, "%s/%s", scripts_path
,
1131 lang_dirent
.d_name
);
1133 if (strstr(lang_path
, "perl"))
1137 if (strstr(lang_path
, "python"))
1141 lang_dir
= opendir(lang_path
);
1145 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1146 /* Skip those real time scripts: xxxtop.p[yl] */
1147 if (strstr(script_dirent
.d_name
, "top."))
1149 sprintf(scripts_path_array
[i
], "%s/%s", lang_path
,
1150 script_dirent
.d_name
);
1151 temp
= strchr(script_dirent
.d_name
, '.');
1152 snprintf(scripts_array
[i
],
1153 (temp
- script_dirent
.d_name
) + 1,
1154 "%s", script_dirent
.d_name
);
1156 if (check_ev_match(lang_path
,
1157 scripts_array
[i
], session
))
1165 closedir(scripts_dir
);
1166 perf_session__delete(session
);
1170 static char *get_script_path(const char *script_root
, const char *suffix
)
1172 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
1173 char scripts_path
[MAXPATHLEN
];
1174 char script_path
[MAXPATHLEN
];
1175 DIR *scripts_dir
, *lang_dir
;
1176 char lang_path
[MAXPATHLEN
];
1177 char *__script_root
;
1179 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
1181 scripts_dir
= opendir(scripts_path
);
1185 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
1186 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
1187 lang_dirent
.d_name
);
1188 lang_dir
= opendir(lang_path
);
1192 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1193 __script_root
= get_script_root(&script_dirent
, suffix
);
1194 if (__script_root
&& !strcmp(script_root
, __script_root
)) {
1195 free(__script_root
);
1197 closedir(scripts_dir
);
1198 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1199 lang_path
, script_dirent
.d_name
);
1200 return strdup(script_path
);
1202 free(__script_root
);
1206 closedir(scripts_dir
);
1211 static bool is_top_script(const char *script_path
)
1213 return ends_with(script_path
, "top") == NULL
? false : true;
1216 static int has_required_arg(char *script_path
)
1218 struct script_desc
*desc
;
1222 desc
= script_desc__new(NULL
);
1224 if (read_script_info(desc
, script_path
))
1230 for (p
= desc
->args
; *p
; p
++)
1234 script_desc__delete(desc
);
1239 static int have_cmd(int argc
, const char **argv
)
1241 char **__argv
= malloc(sizeof(const char *) * argc
);
1244 pr_err("malloc failed\n");
1248 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
1249 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
1250 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
1253 system_wide
= (argc
== 0);
1258 int cmd_script(int argc
, const char **argv
, const char *prefix __maybe_unused
)
1260 bool show_full_info
= false;
1261 char *rec_script_path
= NULL
;
1262 char *rep_script_path
= NULL
;
1263 struct perf_session
*session
;
1264 char *script_path
= NULL
;
1265 const char **__argv
;
1267 struct perf_script script
= {
1269 .sample
= process_sample_event
,
1270 .mmap
= perf_event__process_mmap
,
1271 .mmap2
= perf_event__process_mmap2
,
1272 .comm
= perf_event__process_comm
,
1273 .exit
= perf_event__process_exit
,
1274 .fork
= perf_event__process_fork
,
1275 .attr
= perf_event__process_attr
,
1276 .tracing_data
= perf_event__process_tracing_data
,
1277 .build_id
= perf_event__process_build_id
,
1278 .ordered_samples
= true,
1279 .ordering_requires_timestamps
= true,
1282 const struct option options
[] = {
1283 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1284 "dump raw trace in ASCII"),
1285 OPT_INCR('v', "verbose", &verbose
,
1286 "be more verbose (show symbol address, etc)"),
1287 OPT_BOOLEAN('L', "Latency", &latency_format
,
1288 "show latency attributes (irqs/preemption disabled, etc)"),
1289 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
1290 list_available_scripts
),
1291 OPT_CALLBACK('s', "script", NULL
, "name",
1292 "script file name (lang:script name, script name, or *)",
1294 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
1295 "generate perf-script.xx script in specified language"),
1296 OPT_STRING('i', "input", &input_name
, "file", "input file name"),
1297 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
1298 "do various checks like samples ordering and lost events"),
1299 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
1300 "file", "vmlinux pathname"),
1301 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
1302 "file", "kallsyms pathname"),
1303 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
1304 "When printing symbols do not display call chain"),
1305 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
1306 "Look for files with symbols relative to this directory"),
1307 OPT_CALLBACK('f', "fields", NULL
, "str",
1308 "comma separated output fields prepend with 'type:'. "
1309 "Valid types: hw,sw,trace,raw. "
1310 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1311 "addr,symoff", parse_output_fields
),
1312 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
1313 "system-wide collection from all CPUs"),
1314 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
1315 "only consider these symbols"),
1316 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
1317 OPT_STRING('c', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
1318 "only display events for these comms"),
1319 OPT_BOOLEAN('I', "show-info", &show_full_info
,
1320 "display extended information from perf.data file"),
1321 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf
.show_kernel_path
,
1322 "Show the path of [kernel.kallsyms]"),
1325 const char * const script_usage
[] = {
1326 "perf script [<options>]",
1327 "perf script [<options>] record <script> [<record-options>] <command>",
1328 "perf script [<options>] report <script> [script-args]",
1329 "perf script [<options>] <script> [<record-options>] <command>",
1330 "perf script [<options>] <top-script> [script-args]",
1333 struct perf_data_file file
= {
1334 .mode
= PERF_DATA_MODE_READ
,
1339 argc
= parse_options(argc
, argv
, options
, script_usage
,
1340 PARSE_OPT_STOP_AT_NON_OPTION
);
1342 file
.path
= input_name
;
1344 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
1345 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
1346 if (!rec_script_path
)
1347 return cmd_record(argc
, argv
, NULL
);
1350 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
1351 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
1352 if (!rep_script_path
) {
1354 "Please specify a valid report script"
1355 "(see 'perf script -l' for listing)\n");
1360 /* make sure PERF_EXEC_PATH is set for scripts */
1361 perf_set_argv_exec_path(perf_exec_path());
1363 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
1368 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
1369 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
1371 if (!rec_script_path
&& !rep_script_path
) {
1372 fprintf(stderr
, " Couldn't find script %s\n\n See perf"
1373 " script -l for available scripts.\n", argv
[0]);
1374 usage_with_options(script_usage
, options
);
1377 if (is_top_script(argv
[0])) {
1378 rep_args
= argc
- 1;
1382 rep_args
= has_required_arg(rep_script_path
);
1383 rec_args
= (argc
- 1) - rep_args
;
1385 fprintf(stderr
, " %s script requires options."
1386 "\n\n See perf script -l for available "
1387 "scripts and options.\n", argv
[0]);
1388 usage_with_options(script_usage
, options
);
1392 if (pipe(live_pipe
) < 0) {
1393 perror("failed to create pipe");
1399 perror("failed to fork");
1406 dup2(live_pipe
[1], 1);
1407 close(live_pipe
[0]);
1409 if (is_top_script(argv
[0])) {
1411 } else if (!system_wide
) {
1412 if (have_cmd(argc
- rep_args
, &argv
[rep_args
]) != 0) {
1418 __argv
= malloc((argc
+ 6) * sizeof(const char *));
1420 pr_err("malloc failed\n");
1425 __argv
[j
++] = "/bin/sh";
1426 __argv
[j
++] = rec_script_path
;
1432 for (i
= rep_args
+ 1; i
< argc
; i
++)
1433 __argv
[j
++] = argv
[i
];
1436 execvp("/bin/sh", (char **)__argv
);
1441 dup2(live_pipe
[0], 0);
1442 close(live_pipe
[1]);
1444 __argv
= malloc((argc
+ 4) * sizeof(const char *));
1446 pr_err("malloc failed\n");
1452 __argv
[j
++] = "/bin/sh";
1453 __argv
[j
++] = rep_script_path
;
1454 for (i
= 1; i
< rep_args
+ 1; i
++)
1455 __argv
[j
++] = argv
[i
];
1460 execvp("/bin/sh", (char **)__argv
);
1465 if (rec_script_path
)
1466 script_path
= rec_script_path
;
1467 if (rep_script_path
)
1468 script_path
= rep_script_path
;
1473 if (!rec_script_path
)
1474 system_wide
= false;
1475 else if (!system_wide
) {
1476 if (have_cmd(argc
- 1, &argv
[1]) != 0) {
1482 __argv
= malloc((argc
+ 2) * sizeof(const char *));
1484 pr_err("malloc failed\n");
1489 __argv
[j
++] = "/bin/sh";
1490 __argv
[j
++] = script_path
;
1493 for (i
= 2; i
< argc
; i
++)
1494 __argv
[j
++] = argv
[i
];
1497 execvp("/bin/sh", (char **)__argv
);
1502 if (symbol__init() < 0)
1507 session
= perf_session__new(&file
, false, &script
.tool
);
1508 if (session
== NULL
)
1511 script
.session
= session
;
1514 if (perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
))
1518 if (!script_name
&& !generate_script_lang
)
1519 perf_session__fprintf_info(session
, stdout
, show_full_info
);
1522 symbol_conf
.use_callchain
= true;
1524 symbol_conf
.use_callchain
= false;
1526 if (generate_script_lang
) {
1527 struct stat perf_stat
;
1530 if (output_set_by_user()) {
1532 "custom fields not supported for generated scripts");
1536 input
= open(file
.path
, O_RDONLY
); /* input_name */
1538 perror("failed to open file");
1542 err
= fstat(input
, &perf_stat
);
1544 perror("failed to stat file");
1548 if (!perf_stat
.st_size
) {
1549 fprintf(stderr
, "zero-sized file, nothing to do!\n");
1553 scripting_ops
= script_spec__lookup(generate_script_lang
);
1554 if (!scripting_ops
) {
1555 fprintf(stderr
, "invalid language specifier");
1559 err
= scripting_ops
->generate_script(session
->pevent
,
1565 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
1568 pr_debug("perf script started with script %s\n\n", script_name
);
1572 err
= perf_session__check_output_opt(session
);
1576 err
= __cmd_script(&script
);
1578 perf_session__delete(session
);
1579 cleanup_scripting();