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 <linux/bitmap.h>
19 static char const *script_name
;
20 static char const *generate_script_lang
;
21 static bool debug_mode
;
22 static u64 last_timestamp
;
23 static u64 nr_unordered
;
24 extern const struct option record_options
[];
25 static bool no_callchain
;
26 static bool show_full_info
;
27 static const char *cpu_list
;
28 static DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
30 enum perf_output_field
{
31 PERF_OUTPUT_COMM
= 1U << 0,
32 PERF_OUTPUT_TID
= 1U << 1,
33 PERF_OUTPUT_PID
= 1U << 2,
34 PERF_OUTPUT_TIME
= 1U << 3,
35 PERF_OUTPUT_CPU
= 1U << 4,
36 PERF_OUTPUT_EVNAME
= 1U << 5,
37 PERF_OUTPUT_TRACE
= 1U << 6,
38 PERF_OUTPUT_IP
= 1U << 7,
39 PERF_OUTPUT_SYM
= 1U << 8,
40 PERF_OUTPUT_DSO
= 1U << 9,
41 PERF_OUTPUT_ADDR
= 1U << 10,
44 struct output_option
{
46 enum perf_output_field field
;
47 } all_output_options
[] = {
48 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
49 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
50 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
51 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
52 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
53 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
54 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
55 {.str
= "ip", .field
= PERF_OUTPUT_IP
},
56 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
57 {.str
= "dso", .field
= PERF_OUTPUT_DSO
},
58 {.str
= "addr", .field
= PERF_OUTPUT_ADDR
},
61 /* default set to maintain compatibility with current format */
67 } output
[PERF_TYPE_MAX
] = {
69 [PERF_TYPE_HARDWARE
] = {
72 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
73 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
74 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
75 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
,
77 .invalid_fields
= PERF_OUTPUT_TRACE
,
80 [PERF_TYPE_SOFTWARE
] = {
83 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
84 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
85 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
86 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
,
88 .invalid_fields
= PERF_OUTPUT_TRACE
,
91 [PERF_TYPE_TRACEPOINT
] = {
94 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
95 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
96 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
,
102 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
103 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
104 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_IP
|
105 PERF_OUTPUT_SYM
| PERF_OUTPUT_DSO
,
107 .invalid_fields
= PERF_OUTPUT_TRACE
,
111 static bool output_set_by_user(void)
114 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
115 if (output
[j
].user_set
)
121 static const char *output_field2str(enum perf_output_field field
)
123 int i
, imax
= ARRAY_SIZE(all_output_options
);
124 const char *str
= "";
126 for (i
= 0; i
< imax
; ++i
) {
127 if (all_output_options
[i
].field
== field
) {
128 str
= all_output_options
[i
].str
;
135 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
137 static int perf_event_attr__check_stype(struct perf_event_attr
*attr
,
138 u64 sample_type
, const char *sample_msg
,
139 enum perf_output_field field
)
141 int type
= attr
->type
;
144 if (attr
->sample_type
& sample_type
)
147 if (output
[type
].user_set
) {
148 evname
= __event_name(attr
->type
, attr
->config
);
149 pr_err("Samples for '%s' event do not have %s attribute set. "
150 "Cannot print '%s' field.\n",
151 evname
, sample_msg
, output_field2str(field
));
155 /* user did not ask for it explicitly so remove from the default list */
156 output
[type
].fields
&= ~field
;
157 evname
= __event_name(attr
->type
, attr
->config
);
158 pr_debug("Samples for '%s' event do not have %s attribute set. "
159 "Skipping '%s' field.\n",
160 evname
, sample_msg
, output_field2str(field
));
165 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
166 struct perf_session
*session
)
168 struct perf_event_attr
*attr
= &evsel
->attr
;
170 if (PRINT_FIELD(TRACE
) &&
171 !perf_session__has_traces(session
, "record -R"))
174 if (PRINT_FIELD(IP
)) {
175 if (perf_event_attr__check_stype(attr
, PERF_SAMPLE_IP
, "IP",
180 !(attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
))
181 symbol_conf
.use_callchain
= false;
184 if (PRINT_FIELD(ADDR
) &&
185 perf_event_attr__check_stype(attr
, PERF_SAMPLE_ADDR
, "ADDR",
189 if (PRINT_FIELD(SYM
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
190 pr_err("Display of symbols requested but neither sample IP nor "
191 "sample address\nis selected. Hence, no addresses to convert "
195 if (PRINT_FIELD(DSO
) && !PRINT_FIELD(IP
) && !PRINT_FIELD(ADDR
)) {
196 pr_err("Display of DSO requested but neither sample IP nor "
197 "sample address\nis selected. Hence, no addresses to convert "
202 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
203 perf_event_attr__check_stype(attr
, PERF_SAMPLE_TID
, "TID",
204 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
207 if (PRINT_FIELD(TIME
) &&
208 perf_event_attr__check_stype(attr
, PERF_SAMPLE_TIME
, "TIME",
212 if (PRINT_FIELD(CPU
) &&
213 perf_event_attr__check_stype(attr
, PERF_SAMPLE_CPU
, "CPU",
221 * verify all user requested events exist and the samples
222 * have the expected data
224 static int perf_session__check_output_opt(struct perf_session
*session
)
227 struct perf_evsel
*evsel
;
229 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
230 evsel
= perf_session__find_first_evtype(session
, j
);
233 * even if fields is set to 0 (ie., show nothing) event must
234 * exist if user explicitly includes it on the command line
236 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
) {
237 pr_err("%s events do not exist. "
238 "Remove corresponding -f option to proceed.\n",
243 if (evsel
&& output
[j
].fields
&&
244 perf_evsel__check_attr(evsel
, session
))
251 static void print_sample_start(struct perf_sample
*sample
,
252 struct thread
*thread
,
253 struct perf_event_attr
*attr
)
257 const char *evname
= NULL
;
260 unsigned long long nsecs
;
262 if (PRINT_FIELD(COMM
)) {
264 printf("%8.8s ", thread
->comm
);
265 else if (PRINT_FIELD(IP
) && symbol_conf
.use_callchain
)
266 printf("%s ", thread
->comm
);
268 printf("%16s ", thread
->comm
);
271 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
272 printf("%5d/%-5d ", sample
->pid
, sample
->tid
);
273 else if (PRINT_FIELD(PID
))
274 printf("%5d ", sample
->pid
);
275 else if (PRINT_FIELD(TID
))
276 printf("%5d ", sample
->tid
);
278 if (PRINT_FIELD(CPU
)) {
280 printf("%3d ", sample
->cpu
);
282 printf("[%03d] ", sample
->cpu
);
285 if (PRINT_FIELD(TIME
)) {
286 nsecs
= sample
->time
;
287 secs
= nsecs
/ NSECS_PER_SEC
;
288 nsecs
-= secs
* NSECS_PER_SEC
;
289 usecs
= nsecs
/ NSECS_PER_USEC
;
290 printf("%5lu.%06lu: ", secs
, usecs
);
293 if (PRINT_FIELD(EVNAME
)) {
294 if (attr
->type
== PERF_TYPE_TRACEPOINT
) {
295 type
= trace_parse_common_type(sample
->raw_data
);
296 event
= trace_find_event(type
);
298 evname
= event
->name
;
300 evname
= __event_name(attr
->type
, attr
->config
);
302 printf("%s: ", evname
? evname
: "(unknown)");
306 static bool sample_addr_correlates_sym(struct perf_event_attr
*attr
)
308 if ((attr
->type
== PERF_TYPE_SOFTWARE
) &&
309 ((attr
->config
== PERF_COUNT_SW_PAGE_FAULTS
) ||
310 (attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MIN
) ||
311 (attr
->config
== PERF_COUNT_SW_PAGE_FAULTS_MAJ
)))
317 static void print_sample_addr(union perf_event
*event
,
318 struct perf_sample
*sample
,
319 struct machine
*machine
,
320 struct thread
*thread
,
321 struct perf_event_attr
*attr
)
323 struct addr_location al
;
324 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
325 const char *symname
, *dsoname
;
327 printf("%16" PRIx64
, sample
->addr
);
329 if (!sample_addr_correlates_sym(attr
))
332 thread__find_addr_map(thread
, machine
, cpumode
, MAP__FUNCTION
,
335 thread__find_addr_map(thread
, machine
, cpumode
, MAP__VARIABLE
,
338 al
.cpu
= sample
->cpu
;
342 al
.sym
= map__find_symbol(al
.map
, al
.addr
, NULL
);
344 if (PRINT_FIELD(SYM
)) {
345 if (al
.sym
&& al
.sym
->name
)
346 symname
= al
.sym
->name
;
350 printf(" %16s", symname
);
353 if (PRINT_FIELD(DSO
)) {
354 if (al
.map
&& al
.map
->dso
&& al
.map
->dso
->name
)
355 dsoname
= al
.map
->dso
->name
;
359 printf(" (%s)", dsoname
);
363 static void process_event(union perf_event
*event __unused
,
364 struct perf_sample
*sample
,
365 struct perf_evsel
*evsel
,
366 struct machine
*machine
,
367 struct thread
*thread
)
369 struct perf_event_attr
*attr
= &evsel
->attr
;
371 if (output
[attr
->type
].fields
== 0)
374 print_sample_start(sample
, thread
, attr
);
376 if (PRINT_FIELD(TRACE
))
377 print_trace_event(sample
->cpu
, sample
->raw_data
,
380 if (PRINT_FIELD(ADDR
))
381 print_sample_addr(event
, sample
, machine
, thread
, attr
);
383 if (PRINT_FIELD(IP
)) {
384 if (!symbol_conf
.use_callchain
)
388 perf_event__print_ip(event
, sample
, machine
, evsel
,
389 PRINT_FIELD(SYM
), PRINT_FIELD(DSO
));
395 static int default_start_script(const char *script __unused
,
397 const char **argv __unused
)
402 static int default_stop_script(void)
407 static int default_generate_script(const char *outfile __unused
)
412 static struct scripting_ops default_scripting_ops
= {
413 .start_script
= default_start_script
,
414 .stop_script
= default_stop_script
,
415 .process_event
= process_event
,
416 .generate_script
= default_generate_script
,
419 static struct scripting_ops
*scripting_ops
;
421 static void setup_scripting(void)
423 setup_perl_scripting();
424 setup_python_scripting();
426 scripting_ops
= &default_scripting_ops
;
429 static int cleanup_scripting(void)
431 pr_debug("\nperf script stopped\n");
433 return scripting_ops
->stop_script();
436 static char const *input_name
= "perf.data";
438 static int process_sample_event(struct perf_tool
*tool __used
,
439 union perf_event
*event
,
440 struct perf_sample
*sample
,
441 struct perf_evsel
*evsel
,
442 struct machine
*machine
)
444 struct thread
*thread
= machine__findnew_thread(machine
, event
->ip
.pid
);
446 if (thread
== NULL
) {
447 pr_debug("problem processing %d event, skipping it.\n",
453 if (sample
->time
< last_timestamp
) {
454 pr_err("Samples misordered, previous: %" PRIu64
455 " this: %" PRIu64
"\n", last_timestamp
,
459 last_timestamp
= sample
->time
;
463 if (cpu_list
&& !test_bit(sample
->cpu
, cpu_bitmap
))
466 scripting_ops
->process_event(event
, sample
, evsel
, machine
, thread
);
468 evsel
->hists
.stats
.total_period
+= sample
->period
;
472 static struct perf_tool perf_script
= {
473 .sample
= process_sample_event
,
474 .mmap
= perf_event__process_mmap
,
475 .comm
= perf_event__process_comm
,
476 .exit
= perf_event__process_task
,
477 .fork
= perf_event__process_task
,
478 .attr
= perf_event__process_attr
,
479 .event_type
= perf_event__process_event_type
,
480 .tracing_data
= perf_event__process_tracing_data
,
481 .build_id
= perf_event__process_build_id
,
482 .ordered_samples
= true,
483 .ordering_requires_timestamps
= true,
486 extern volatile int session_done
;
488 static void sig_handler(int sig __unused
)
493 static int __cmd_script(struct perf_session
*session
)
497 signal(SIGINT
, sig_handler
);
499 ret
= perf_session__process_events(session
, &perf_script
);
502 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
508 struct list_head node
;
509 struct scripting_ops
*ops
;
513 static LIST_HEAD(script_specs
);
515 static struct script_spec
*script_spec__new(const char *spec
,
516 struct scripting_ops
*ops
)
518 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
521 strcpy(s
->spec
, spec
);
528 static void script_spec__delete(struct script_spec
*s
)
534 static void script_spec__add(struct script_spec
*s
)
536 list_add_tail(&s
->node
, &script_specs
);
539 static struct script_spec
*script_spec__find(const char *spec
)
541 struct script_spec
*s
;
543 list_for_each_entry(s
, &script_specs
, node
)
544 if (strcasecmp(s
->spec
, spec
) == 0)
549 static struct script_spec
*script_spec__findnew(const char *spec
,
550 struct scripting_ops
*ops
)
552 struct script_spec
*s
= script_spec__find(spec
);
557 s
= script_spec__new(spec
, ops
);
559 goto out_delete_spec
;
566 script_spec__delete(s
);
571 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
573 struct script_spec
*s
;
575 s
= script_spec__find(spec
);
579 s
= script_spec__findnew(spec
, ops
);
586 static struct scripting_ops
*script_spec__lookup(const char *spec
)
588 struct script_spec
*s
= script_spec__find(spec
);
595 static void list_available_languages(void)
597 struct script_spec
*s
;
599 fprintf(stderr
, "\n");
600 fprintf(stderr
, "Scripting language extensions (used in "
601 "perf script -s [spec:]script.[spec]):\n\n");
603 list_for_each_entry(s
, &script_specs
, node
)
604 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
606 fprintf(stderr
, "\n");
609 static int parse_scriptname(const struct option
*opt __used
,
610 const char *str
, int unset __used
)
613 const char *script
, *ext
;
616 if (strcmp(str
, "lang") == 0) {
617 list_available_languages();
621 script
= strchr(str
, ':');
624 if (len
>= PATH_MAX
) {
625 fprintf(stderr
, "invalid language specifier");
628 strncpy(spec
, str
, len
);
630 scripting_ops
= script_spec__lookup(spec
);
631 if (!scripting_ops
) {
632 fprintf(stderr
, "invalid language specifier");
638 ext
= strrchr(script
, '.');
640 fprintf(stderr
, "invalid script extension");
643 scripting_ops
= script_spec__lookup(++ext
);
644 if (!scripting_ops
) {
645 fprintf(stderr
, "invalid script extension");
650 script_name
= strdup(script
);
655 static int parse_output_fields(const struct option
*opt __used
,
656 const char *arg
, int unset __used
)
659 int i
, imax
= sizeof(all_output_options
) / sizeof(struct output_option
);
662 char *str
= strdup(arg
);
668 /* first word can state for which event type the user is specifying
669 * the fields. If no type exists, the specified fields apply to all
670 * event types found in the file minus the invalid fields for a type.
672 tok
= strchr(str
, ':');
676 if (!strcmp(str
, "hw"))
677 type
= PERF_TYPE_HARDWARE
;
678 else if (!strcmp(str
, "sw"))
679 type
= PERF_TYPE_SOFTWARE
;
680 else if (!strcmp(str
, "trace"))
681 type
= PERF_TYPE_TRACEPOINT
;
682 else if (!strcmp(str
, "raw"))
683 type
= PERF_TYPE_RAW
;
685 fprintf(stderr
, "Invalid event type in field string.\n");
689 if (output
[type
].user_set
)
690 pr_warning("Overriding previous field request for %s events.\n",
693 output
[type
].fields
= 0;
694 output
[type
].user_set
= true;
695 output
[type
].wildcard_set
= false;
699 if (strlen(str
) == 0) {
701 "Cannot set fields to 'none' for all event types.\n");
706 if (output_set_by_user())
707 pr_warning("Overriding previous field request for all events.\n");
709 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
710 output
[j
].fields
= 0;
711 output
[j
].user_set
= true;
712 output
[j
].wildcard_set
= true;
716 tok
= strtok(tok
, ",");
718 for (i
= 0; i
< imax
; ++i
) {
719 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
723 fprintf(stderr
, "Invalid field requested.\n");
729 /* add user option to all events types for
732 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
733 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
734 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
735 all_output_options
[i
].str
, event_type(j
));
737 output
[j
].fields
|= all_output_options
[i
].field
;
740 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
741 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
742 all_output_options
[i
].str
, event_type(type
));
747 output
[type
].fields
|= all_output_options
[i
].field
;
750 tok
= strtok(NULL
, ",");
754 if (output
[type
].fields
== 0) {
755 pr_debug("No fields requested for %s type. "
756 "Events will not be displayed.\n", event_type(type
));
765 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
766 static int is_directory(const char *base_path
, const struct dirent
*dent
)
771 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
775 return S_ISDIR(st
.st_mode
);
778 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
779 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
781 if ((lang_dirent.d_type == DT_DIR || \
782 (lang_dirent.d_type == DT_UNKNOWN && \
783 is_directory(scripts_path, &lang_dirent))) && \
784 (strcmp(lang_dirent.d_name, ".")) && \
785 (strcmp(lang_dirent.d_name, "..")))
787 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
788 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
790 if (script_dirent.d_type != DT_DIR && \
791 (script_dirent.d_type != DT_UNKNOWN || \
792 !is_directory(lang_path, &script_dirent)))
795 #define RECORD_SUFFIX "-record"
796 #define REPORT_SUFFIX "-report"
799 struct list_head node
;
805 static LIST_HEAD(script_descs
);
807 static struct script_desc
*script_desc__new(const char *name
)
809 struct script_desc
*s
= zalloc(sizeof(*s
));
811 if (s
!= NULL
&& name
)
812 s
->name
= strdup(name
);
817 static void script_desc__delete(struct script_desc
*s
)
825 static void script_desc__add(struct script_desc
*s
)
827 list_add_tail(&s
->node
, &script_descs
);
830 static struct script_desc
*script_desc__find(const char *name
)
832 struct script_desc
*s
;
834 list_for_each_entry(s
, &script_descs
, node
)
835 if (strcasecmp(s
->name
, name
) == 0)
840 static struct script_desc
*script_desc__findnew(const char *name
)
842 struct script_desc
*s
= script_desc__find(name
);
847 s
= script_desc__new(name
);
849 goto out_delete_desc
;
856 script_desc__delete(s
);
861 static const char *ends_with(const char *str
, const char *suffix
)
863 size_t suffix_len
= strlen(suffix
);
866 if (strlen(str
) > suffix_len
) {
867 p
= str
+ strlen(str
) - suffix_len
;
868 if (!strncmp(p
, suffix
, suffix_len
))
875 static char *ltrim(char *str
)
877 int len
= strlen(str
);
879 while (len
&& isspace(*str
)) {
887 static int read_script_info(struct script_desc
*desc
, const char *filename
)
889 char line
[BUFSIZ
], *p
;
892 fp
= fopen(filename
, "r");
896 while (fgets(line
, sizeof(line
), fp
)) {
903 if (strlen(p
) && *p
== '!')
907 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
908 p
[strlen(p
) - 1] = '\0';
910 if (!strncmp(p
, "description:", strlen("description:"))) {
911 p
+= strlen("description:");
912 desc
->half_liner
= strdup(ltrim(p
));
916 if (!strncmp(p
, "args:", strlen("args:"))) {
917 p
+= strlen("args:");
918 desc
->args
= strdup(ltrim(p
));
928 static int list_available_scripts(const struct option
*opt __used
,
929 const char *s __used
, int unset __used
)
931 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
932 char scripts_path
[MAXPATHLEN
];
933 DIR *scripts_dir
, *lang_dir
;
934 char script_path
[MAXPATHLEN
];
935 char lang_path
[MAXPATHLEN
];
936 struct script_desc
*desc
;
937 char first_half
[BUFSIZ
];
941 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
943 scripts_dir
= opendir(scripts_path
);
947 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
948 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
950 lang_dir
= opendir(lang_path
);
954 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
955 script_root
= strdup(script_dirent
.d_name
);
956 str
= (char *)ends_with(script_root
, REPORT_SUFFIX
);
959 desc
= script_desc__findnew(script_root
);
960 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
961 lang_path
, script_dirent
.d_name
);
962 read_script_info(desc
, script_path
);
968 fprintf(stdout
, "List of available trace scripts:\n");
969 list_for_each_entry(desc
, &script_descs
, node
) {
970 sprintf(first_half
, "%s %s", desc
->name
,
971 desc
->args
? desc
->args
: "");
972 fprintf(stdout
, " %-36s %s\n", first_half
,
973 desc
->half_liner
? desc
->half_liner
: "");
979 static char *get_script_path(const char *script_root
, const char *suffix
)
981 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
982 char scripts_path
[MAXPATHLEN
];
983 char script_path
[MAXPATHLEN
];
984 DIR *scripts_dir
, *lang_dir
;
985 char lang_path
[MAXPATHLEN
];
986 char *str
, *__script_root
;
989 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
991 scripts_dir
= opendir(scripts_path
);
995 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
996 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
998 lang_dir
= opendir(lang_path
);
1002 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
1003 __script_root
= strdup(script_dirent
.d_name
);
1004 str
= (char *)ends_with(__script_root
, suffix
);
1007 if (strcmp(__script_root
, script_root
))
1009 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
1010 lang_path
, script_dirent
.d_name
);
1011 path
= strdup(script_path
);
1012 free(__script_root
);
1015 free(__script_root
);
1022 static bool is_top_script(const char *script_path
)
1024 return ends_with(script_path
, "top") == NULL
? false : true;
1027 static int has_required_arg(char *script_path
)
1029 struct script_desc
*desc
;
1033 desc
= script_desc__new(NULL
);
1035 if (read_script_info(desc
, script_path
))
1041 for (p
= desc
->args
; *p
; p
++)
1045 script_desc__delete(desc
);
1050 static const char * const script_usage
[] = {
1051 "perf script [<options>]",
1052 "perf script [<options>] record <script> [<record-options>] <command>",
1053 "perf script [<options>] report <script> [script-args]",
1054 "perf script [<options>] <script> [<record-options>] <command>",
1055 "perf script [<options>] <top-script> [script-args]",
1059 static const struct option options
[] = {
1060 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1061 "dump raw trace in ASCII"),
1062 OPT_INCR('v', "verbose", &verbose
,
1063 "be more verbose (show symbol address, etc)"),
1064 OPT_BOOLEAN('L', "Latency", &latency_format
,
1065 "show latency attributes (irqs/preemption disabled, etc)"),
1066 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
1067 list_available_scripts
),
1068 OPT_CALLBACK('s', "script", NULL
, "name",
1069 "script file name (lang:script name, script name, or *)",
1071 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
1072 "generate perf-script.xx script in specified language"),
1073 OPT_STRING('i', "input", &input_name
, "file",
1075 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
1076 "do various checks like samples ordering and lost events"),
1077 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
1078 "file", "vmlinux pathname"),
1079 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
1080 "file", "kallsyms pathname"),
1081 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
1082 "When printing symbols do not display call chain"),
1083 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
1084 "Look for files with symbols relative to this directory"),
1085 OPT_CALLBACK('f', "fields", NULL
, "str",
1086 "comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace,raw. Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,addr",
1087 parse_output_fields
),
1088 OPT_STRING('C', "cpu", &cpu_list
, "cpu", "list of cpus to profile"),
1089 OPT_BOOLEAN('I', "show-info", &show_full_info
,
1090 "display extended information from perf.data file"),
1094 static bool have_cmd(int argc
, const char **argv
)
1096 char **__argv
= malloc(sizeof(const char *) * argc
);
1100 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
1101 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
1102 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
1108 int cmd_script(int argc
, const char **argv
, const char *prefix __used
)
1110 char *rec_script_path
= NULL
;
1111 char *rep_script_path
= NULL
;
1112 struct perf_session
*session
;
1113 char *script_path
= NULL
;
1114 const char **__argv
;
1120 argc
= parse_options(argc
, argv
, options
, script_usage
,
1121 PARSE_OPT_STOP_AT_NON_OPTION
);
1123 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
1124 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
1125 if (!rec_script_path
)
1126 return cmd_record(argc
, argv
, NULL
);
1129 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
1130 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
1131 if (!rep_script_path
) {
1133 "Please specify a valid report script"
1134 "(see 'perf script -l' for listing)\n");
1139 /* make sure PERF_EXEC_PATH is set for scripts */
1140 perf_set_argv_exec_path(perf_exec_path());
1142 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
1147 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
1148 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
1150 if (!rec_script_path
&& !rep_script_path
) {
1151 fprintf(stderr
, " Couldn't find script %s\n\n See perf"
1152 " script -l for available scripts.\n", argv
[0]);
1153 usage_with_options(script_usage
, options
);
1156 if (is_top_script(argv
[0])) {
1157 rep_args
= argc
- 1;
1161 rep_args
= has_required_arg(rep_script_path
);
1162 rec_args
= (argc
- 1) - rep_args
;
1164 fprintf(stderr
, " %s script requires options."
1165 "\n\n See perf script -l for available "
1166 "scripts and options.\n", argv
[0]);
1167 usage_with_options(script_usage
, options
);
1171 if (pipe(live_pipe
) < 0) {
1172 perror("failed to create pipe");
1178 perror("failed to fork");
1186 dup2(live_pipe
[1], 1);
1187 close(live_pipe
[0]);
1189 if (!is_top_script(argv
[0]))
1190 system_wide
= !have_cmd(argc
- rep_args
,
1193 __argv
= malloc((argc
+ 6) * sizeof(const char *));
1197 __argv
[j
++] = "/bin/sh";
1198 __argv
[j
++] = rec_script_path
;
1204 for (i
= rep_args
+ 1; i
< argc
; i
++)
1205 __argv
[j
++] = argv
[i
];
1208 execvp("/bin/sh", (char **)__argv
);
1213 dup2(live_pipe
[0], 0);
1214 close(live_pipe
[1]);
1216 __argv
= malloc((argc
+ 4) * sizeof(const char *));
1220 __argv
[j
++] = "/bin/sh";
1221 __argv
[j
++] = rep_script_path
;
1222 for (i
= 1; i
< rep_args
+ 1; i
++)
1223 __argv
[j
++] = argv
[i
];
1228 execvp("/bin/sh", (char **)__argv
);
1233 if (rec_script_path
)
1234 script_path
= rec_script_path
;
1235 if (rep_script_path
)
1236 script_path
= rep_script_path
;
1239 system_wide
= false;
1242 if (rec_script_path
)
1243 system_wide
= !have_cmd(argc
- 1, &argv
[1]);
1245 __argv
= malloc((argc
+ 2) * sizeof(const char *));
1248 __argv
[j
++] = "/bin/sh";
1249 __argv
[j
++] = script_path
;
1252 for (i
= 2; i
< argc
; i
++)
1253 __argv
[j
++] = argv
[i
];
1256 execvp("/bin/sh", (char **)__argv
);
1261 if (symbol__init() < 0)
1266 session
= perf_session__new(input_name
, O_RDONLY
, 0, false, &perf_script
);
1267 if (session
== NULL
)
1271 if (perf_session__cpu_bitmap(session
, cpu_list
, cpu_bitmap
))
1275 perf_session__fprintf_info(session
, stdout
, show_full_info
);
1278 symbol_conf
.use_callchain
= true;
1280 symbol_conf
.use_callchain
= false;
1282 if (generate_script_lang
) {
1283 struct stat perf_stat
;
1286 if (output_set_by_user()) {
1288 "custom fields not supported for generated scripts");
1292 input
= open(input_name
, O_RDONLY
);
1294 perror("failed to open file");
1298 err
= fstat(input
, &perf_stat
);
1300 perror("failed to stat file");
1304 if (!perf_stat
.st_size
) {
1305 fprintf(stderr
, "zero-sized file, nothing to do!\n");
1309 scripting_ops
= script_spec__lookup(generate_script_lang
);
1310 if (!scripting_ops
) {
1311 fprintf(stderr
, "invalid language specifier");
1315 err
= scripting_ops
->generate_script("perf-script");
1320 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
1323 pr_debug("perf script started with script %s\n\n", script_name
);
1327 err
= perf_session__check_output_opt(session
);
1331 err
= __cmd_script(session
);
1333 perf_session__delete(session
);
1334 cleanup_scripting();