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/symbol.h"
11 #include "util/thread.h"
12 #include "util/trace-event.h"
13 #include "util/util.h"
14 #include "util/evlist.h"
15 #include "util/evsel.h"
17 static char const *script_name
;
18 static char const *generate_script_lang
;
19 static bool debug_mode
;
20 static u64 last_timestamp
;
21 static u64 nr_unordered
;
22 extern const struct option record_options
[];
23 static bool no_callchain
;
25 enum perf_output_field
{
26 PERF_OUTPUT_COMM
= 1U << 0,
27 PERF_OUTPUT_TID
= 1U << 1,
28 PERF_OUTPUT_PID
= 1U << 2,
29 PERF_OUTPUT_TIME
= 1U << 3,
30 PERF_OUTPUT_CPU
= 1U << 4,
31 PERF_OUTPUT_EVNAME
= 1U << 5,
32 PERF_OUTPUT_TRACE
= 1U << 6,
33 PERF_OUTPUT_SYM
= 1U << 7,
36 struct output_option
{
38 enum perf_output_field field
;
39 } all_output_options
[] = {
40 {.str
= "comm", .field
= PERF_OUTPUT_COMM
},
41 {.str
= "tid", .field
= PERF_OUTPUT_TID
},
42 {.str
= "pid", .field
= PERF_OUTPUT_PID
},
43 {.str
= "time", .field
= PERF_OUTPUT_TIME
},
44 {.str
= "cpu", .field
= PERF_OUTPUT_CPU
},
45 {.str
= "event", .field
= PERF_OUTPUT_EVNAME
},
46 {.str
= "trace", .field
= PERF_OUTPUT_TRACE
},
47 {.str
= "sym", .field
= PERF_OUTPUT_SYM
},
50 /* default set to maintain compatibility with current format */
56 } output
[PERF_TYPE_MAX
] = {
58 [PERF_TYPE_HARDWARE
] = {
61 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
62 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
63 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_SYM
,
65 .invalid_fields
= PERF_OUTPUT_TRACE
,
68 [PERF_TYPE_SOFTWARE
] = {
71 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
72 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
73 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_SYM
,
75 .invalid_fields
= PERF_OUTPUT_TRACE
,
78 [PERF_TYPE_TRACEPOINT
] = {
81 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
82 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
83 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_TRACE
,
89 .fields
= PERF_OUTPUT_COMM
| PERF_OUTPUT_TID
|
90 PERF_OUTPUT_CPU
| PERF_OUTPUT_TIME
|
91 PERF_OUTPUT_EVNAME
| PERF_OUTPUT_SYM
,
93 .invalid_fields
= PERF_OUTPUT_TRACE
,
97 static bool output_set_by_user(void)
100 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
101 if (output
[j
].user_set
)
107 static const char *output_field2str(enum perf_output_field field
)
109 int i
, imax
= ARRAY_SIZE(all_output_options
);
110 const char *str
= "";
112 for (i
= 0; i
< imax
; ++i
) {
113 if (all_output_options
[i
].field
== field
) {
114 str
= all_output_options
[i
].str
;
121 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
123 static int perf_event_attr__check_stype(struct perf_event_attr
*attr
,
124 u64 sample_type
, const char *sample_msg
,
125 enum perf_output_field field
)
127 int type
= attr
->type
;
130 if (attr
->sample_type
& sample_type
)
133 if (output
[type
].user_set
) {
134 evname
= __event_name(attr
->type
, attr
->config
);
135 pr_err("Samples for '%s' event do not have %s attribute set. "
136 "Cannot print '%s' field.\n",
137 evname
, sample_msg
, output_field2str(field
));
141 /* user did not ask for it explicitly so remove from the default list */
142 output
[type
].fields
&= ~field
;
143 evname
= __event_name(attr
->type
, attr
->config
);
144 pr_debug("Samples for '%s' event do not have %s attribute set. "
145 "Skipping '%s' field.\n",
146 evname
, sample_msg
, output_field2str(field
));
151 static int perf_evsel__check_attr(struct perf_evsel
*evsel
,
152 struct perf_session
*session
)
154 struct perf_event_attr
*attr
= &evsel
->attr
;
156 if (PRINT_FIELD(TRACE
) &&
157 !perf_session__has_traces(session
, "record -R"))
160 if (PRINT_FIELD(SYM
)) {
161 if (perf_event_attr__check_stype(attr
, PERF_SAMPLE_IP
, "IP",
166 !(attr
->sample_type
& PERF_SAMPLE_CALLCHAIN
))
167 symbol_conf
.use_callchain
= false;
170 if ((PRINT_FIELD(PID
) || PRINT_FIELD(TID
)) &&
171 perf_event_attr__check_stype(attr
, PERF_SAMPLE_TID
, "TID",
172 PERF_OUTPUT_TID
|PERF_OUTPUT_PID
))
175 if (PRINT_FIELD(TIME
) &&
176 perf_event_attr__check_stype(attr
, PERF_SAMPLE_TIME
, "TIME",
180 if (PRINT_FIELD(CPU
) &&
181 perf_event_attr__check_stype(attr
, PERF_SAMPLE_CPU
, "CPU",
189 * verify all user requested events exist and the samples
190 * have the expected data
192 static int perf_session__check_output_opt(struct perf_session
*session
)
195 struct perf_evsel
*evsel
;
197 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
198 evsel
= perf_session__find_first_evtype(session
, j
);
201 * even if fields is set to 0 (ie., show nothing) event must
202 * exist if user explicitly includes it on the command line
204 if (!evsel
&& output
[j
].user_set
&& !output
[j
].wildcard_set
) {
205 pr_err("%s events do not exist. "
206 "Remove corresponding -f option to proceed.\n",
211 if (evsel
&& output
[j
].fields
&&
212 perf_evsel__check_attr(evsel
, session
))
219 static void print_sample_start(struct perf_sample
*sample
,
220 struct thread
*thread
,
221 struct perf_event_attr
*attr
)
225 const char *evname
= NULL
;
228 unsigned long long nsecs
;
230 if (PRINT_FIELD(COMM
)) {
232 printf("%8.8s ", thread
->comm
);
233 else if (PRINT_FIELD(SYM
) && symbol_conf
.use_callchain
)
234 printf("%s ", thread
->comm
);
236 printf("%16s ", thread
->comm
);
239 if (PRINT_FIELD(PID
) && PRINT_FIELD(TID
))
240 printf("%5d/%-5d ", sample
->pid
, sample
->tid
);
241 else if (PRINT_FIELD(PID
))
242 printf("%5d ", sample
->pid
);
243 else if (PRINT_FIELD(TID
))
244 printf("%5d ", sample
->tid
);
246 if (PRINT_FIELD(CPU
)) {
248 printf("%3d ", sample
->cpu
);
250 printf("[%03d] ", sample
->cpu
);
253 if (PRINT_FIELD(TIME
)) {
254 nsecs
= sample
->time
;
255 secs
= nsecs
/ NSECS_PER_SEC
;
256 nsecs
-= secs
* NSECS_PER_SEC
;
257 usecs
= nsecs
/ NSECS_PER_USEC
;
258 printf("%5lu.%06lu: ", secs
, usecs
);
261 if (PRINT_FIELD(EVNAME
)) {
262 if (attr
->type
== PERF_TYPE_TRACEPOINT
) {
263 type
= trace_parse_common_type(sample
->raw_data
);
264 event
= trace_find_event(type
);
266 evname
= event
->name
;
268 evname
= __event_name(attr
->type
, attr
->config
);
270 printf("%s: ", evname
? evname
: "(unknown)");
274 static void process_event(union perf_event
*event __unused
,
275 struct perf_sample
*sample
,
276 struct perf_evsel
*evsel
,
277 struct perf_session
*session
,
278 struct thread
*thread
)
280 struct perf_event_attr
*attr
= &evsel
->attr
;
282 if (output
[attr
->type
].fields
== 0)
285 print_sample_start(sample
, thread
, attr
);
287 if (PRINT_FIELD(TRACE
))
288 print_trace_event(sample
->cpu
, sample
->raw_data
,
291 if (PRINT_FIELD(SYM
)) {
292 if (!symbol_conf
.use_callchain
)
296 perf_session__print_symbols(event
, sample
, session
);
302 static int default_start_script(const char *script __unused
,
304 const char **argv __unused
)
309 static int default_stop_script(void)
314 static int default_generate_script(const char *outfile __unused
)
319 static struct scripting_ops default_scripting_ops
= {
320 .start_script
= default_start_script
,
321 .stop_script
= default_stop_script
,
322 .process_event
= process_event
,
323 .generate_script
= default_generate_script
,
326 static struct scripting_ops
*scripting_ops
;
328 static void setup_scripting(void)
330 setup_perl_scripting();
331 setup_python_scripting();
333 scripting_ops
= &default_scripting_ops
;
336 static int cleanup_scripting(void)
338 pr_debug("\nperf script stopped\n");
340 return scripting_ops
->stop_script();
343 static char const *input_name
= "perf.data";
345 static int process_sample_event(union perf_event
*event
,
346 struct perf_sample
*sample
,
347 struct perf_evsel
*evsel
,
348 struct perf_session
*session
)
350 struct thread
*thread
= perf_session__findnew(session
, event
->ip
.pid
);
352 if (thread
== NULL
) {
353 pr_debug("problem processing %d event, skipping it.\n",
359 if (sample
->time
< last_timestamp
) {
360 pr_err("Samples misordered, previous: %" PRIu64
361 " this: %" PRIu64
"\n", last_timestamp
,
365 last_timestamp
= sample
->time
;
368 scripting_ops
->process_event(event
, sample
, evsel
, session
, thread
);
370 session
->hists
.stats
.total_period
+= sample
->period
;
374 static struct perf_event_ops event_ops
= {
375 .sample
= process_sample_event
,
376 .mmap
= perf_event__process_mmap
,
377 .comm
= perf_event__process_comm
,
378 .exit
= perf_event__process_task
,
379 .fork
= perf_event__process_task
,
380 .attr
= perf_event__process_attr
,
381 .event_type
= perf_event__process_event_type
,
382 .tracing_data
= perf_event__process_tracing_data
,
383 .build_id
= perf_event__process_build_id
,
384 .ordered_samples
= true,
385 .ordering_requires_timestamps
= true,
388 extern volatile int session_done
;
390 static void sig_handler(int sig __unused
)
395 static int __cmd_script(struct perf_session
*session
)
399 signal(SIGINT
, sig_handler
);
401 ret
= perf_session__process_events(session
, &event_ops
);
404 pr_err("Misordered timestamps: %" PRIu64
"\n", nr_unordered
);
410 struct list_head node
;
411 struct scripting_ops
*ops
;
415 static LIST_HEAD(script_specs
);
417 static struct script_spec
*script_spec__new(const char *spec
,
418 struct scripting_ops
*ops
)
420 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
423 strcpy(s
->spec
, spec
);
430 static void script_spec__delete(struct script_spec
*s
)
436 static void script_spec__add(struct script_spec
*s
)
438 list_add_tail(&s
->node
, &script_specs
);
441 static struct script_spec
*script_spec__find(const char *spec
)
443 struct script_spec
*s
;
445 list_for_each_entry(s
, &script_specs
, node
)
446 if (strcasecmp(s
->spec
, spec
) == 0)
451 static struct script_spec
*script_spec__findnew(const char *spec
,
452 struct scripting_ops
*ops
)
454 struct script_spec
*s
= script_spec__find(spec
);
459 s
= script_spec__new(spec
, ops
);
461 goto out_delete_spec
;
468 script_spec__delete(s
);
473 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
475 struct script_spec
*s
;
477 s
= script_spec__find(spec
);
481 s
= script_spec__findnew(spec
, ops
);
488 static struct scripting_ops
*script_spec__lookup(const char *spec
)
490 struct script_spec
*s
= script_spec__find(spec
);
497 static void list_available_languages(void)
499 struct script_spec
*s
;
501 fprintf(stderr
, "\n");
502 fprintf(stderr
, "Scripting language extensions (used in "
503 "perf script -s [spec:]script.[spec]):\n\n");
505 list_for_each_entry(s
, &script_specs
, node
)
506 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
508 fprintf(stderr
, "\n");
511 static int parse_scriptname(const struct option
*opt __used
,
512 const char *str
, int unset __used
)
515 const char *script
, *ext
;
518 if (strcmp(str
, "lang") == 0) {
519 list_available_languages();
523 script
= strchr(str
, ':');
526 if (len
>= PATH_MAX
) {
527 fprintf(stderr
, "invalid language specifier");
530 strncpy(spec
, str
, len
);
532 scripting_ops
= script_spec__lookup(spec
);
533 if (!scripting_ops
) {
534 fprintf(stderr
, "invalid language specifier");
540 ext
= strrchr(script
, '.');
542 fprintf(stderr
, "invalid script extension");
545 scripting_ops
= script_spec__lookup(++ext
);
546 if (!scripting_ops
) {
547 fprintf(stderr
, "invalid script extension");
552 script_name
= strdup(script
);
557 static int parse_output_fields(const struct option
*opt __used
,
558 const char *arg
, int unset __used
)
561 int i
, imax
= sizeof(all_output_options
) / sizeof(struct output_option
);
564 char *str
= strdup(arg
);
570 /* first word can state for which event type the user is specifying
571 * the fields. If no type exists, the specified fields apply to all
572 * event types found in the file minus the invalid fields for a type.
574 tok
= strchr(str
, ':');
578 if (!strcmp(str
, "hw"))
579 type
= PERF_TYPE_HARDWARE
;
580 else if (!strcmp(str
, "sw"))
581 type
= PERF_TYPE_SOFTWARE
;
582 else if (!strcmp(str
, "trace"))
583 type
= PERF_TYPE_TRACEPOINT
;
584 else if (!strcmp(str
, "raw"))
585 type
= PERF_TYPE_RAW
;
587 fprintf(stderr
, "Invalid event type in field string.\n");
591 if (output
[type
].user_set
)
592 pr_warning("Overriding previous field request for %s events.\n",
595 output
[type
].fields
= 0;
596 output
[type
].user_set
= true;
597 output
[type
].wildcard_set
= false;
601 if (strlen(str
) == 0) {
603 "Cannot set fields to 'none' for all event types.\n");
608 if (output_set_by_user())
609 pr_warning("Overriding previous field request for all events.\n");
611 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
612 output
[j
].fields
= 0;
613 output
[j
].user_set
= true;
614 output
[j
].wildcard_set
= true;
618 tok
= strtok(tok
, ",");
620 for (i
= 0; i
< imax
; ++i
) {
621 if (strcmp(tok
, all_output_options
[i
].str
) == 0)
625 fprintf(stderr
, "Invalid field requested.\n");
631 /* add user option to all events types for
634 for (j
= 0; j
< PERF_TYPE_MAX
; ++j
) {
635 if (output
[j
].invalid_fields
& all_output_options
[i
].field
) {
636 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
637 all_output_options
[i
].str
, event_type(j
));
639 output
[j
].fields
|= all_output_options
[i
].field
;
642 if (output
[type
].invalid_fields
& all_output_options
[i
].field
) {
643 fprintf(stderr
, "\'%s\' not valid for %s events.\n",
644 all_output_options
[i
].str
, event_type(type
));
649 output
[type
].fields
|= all_output_options
[i
].field
;
652 tok
= strtok(NULL
, ",");
656 if (output
[type
].fields
== 0) {
657 pr_debug("No fields requested for %s type. "
658 "Events will not be displayed.\n", event_type(type
));
667 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
668 static int is_directory(const char *base_path
, const struct dirent
*dent
)
673 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
677 return S_ISDIR(st
.st_mode
);
680 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
681 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
683 if ((lang_dirent.d_type == DT_DIR || \
684 (lang_dirent.d_type == DT_UNKNOWN && \
685 is_directory(scripts_path, &lang_dirent))) && \
686 (strcmp(lang_dirent.d_name, ".")) && \
687 (strcmp(lang_dirent.d_name, "..")))
689 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
690 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
692 if (script_dirent.d_type != DT_DIR && \
693 (script_dirent.d_type != DT_UNKNOWN || \
694 !is_directory(lang_path, &script_dirent)))
697 #define RECORD_SUFFIX "-record"
698 #define REPORT_SUFFIX "-report"
701 struct list_head node
;
707 static LIST_HEAD(script_descs
);
709 static struct script_desc
*script_desc__new(const char *name
)
711 struct script_desc
*s
= zalloc(sizeof(*s
));
713 if (s
!= NULL
&& name
)
714 s
->name
= strdup(name
);
719 static void script_desc__delete(struct script_desc
*s
)
727 static void script_desc__add(struct script_desc
*s
)
729 list_add_tail(&s
->node
, &script_descs
);
732 static struct script_desc
*script_desc__find(const char *name
)
734 struct script_desc
*s
;
736 list_for_each_entry(s
, &script_descs
, node
)
737 if (strcasecmp(s
->name
, name
) == 0)
742 static struct script_desc
*script_desc__findnew(const char *name
)
744 struct script_desc
*s
= script_desc__find(name
);
749 s
= script_desc__new(name
);
751 goto out_delete_desc
;
758 script_desc__delete(s
);
763 static const char *ends_with(const char *str
, const char *suffix
)
765 size_t suffix_len
= strlen(suffix
);
768 if (strlen(str
) > suffix_len
) {
769 p
= str
+ strlen(str
) - suffix_len
;
770 if (!strncmp(p
, suffix
, suffix_len
))
777 static char *ltrim(char *str
)
779 int len
= strlen(str
);
781 while (len
&& isspace(*str
)) {
789 static int read_script_info(struct script_desc
*desc
, const char *filename
)
791 char line
[BUFSIZ
], *p
;
794 fp
= fopen(filename
, "r");
798 while (fgets(line
, sizeof(line
), fp
)) {
805 if (strlen(p
) && *p
== '!')
809 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
810 p
[strlen(p
) - 1] = '\0';
812 if (!strncmp(p
, "description:", strlen("description:"))) {
813 p
+= strlen("description:");
814 desc
->half_liner
= strdup(ltrim(p
));
818 if (!strncmp(p
, "args:", strlen("args:"))) {
819 p
+= strlen("args:");
820 desc
->args
= strdup(ltrim(p
));
830 static int list_available_scripts(const struct option
*opt __used
,
831 const char *s __used
, int unset __used
)
833 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
834 char scripts_path
[MAXPATHLEN
];
835 DIR *scripts_dir
, *lang_dir
;
836 char script_path
[MAXPATHLEN
];
837 char lang_path
[MAXPATHLEN
];
838 struct script_desc
*desc
;
839 char first_half
[BUFSIZ
];
843 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
845 scripts_dir
= opendir(scripts_path
);
849 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
850 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
852 lang_dir
= opendir(lang_path
);
856 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
857 script_root
= strdup(script_dirent
.d_name
);
858 str
= (char *)ends_with(script_root
, REPORT_SUFFIX
);
861 desc
= script_desc__findnew(script_root
);
862 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
863 lang_path
, script_dirent
.d_name
);
864 read_script_info(desc
, script_path
);
870 fprintf(stdout
, "List of available trace scripts:\n");
871 list_for_each_entry(desc
, &script_descs
, node
) {
872 sprintf(first_half
, "%s %s", desc
->name
,
873 desc
->args
? desc
->args
: "");
874 fprintf(stdout
, " %-36s %s\n", first_half
,
875 desc
->half_liner
? desc
->half_liner
: "");
881 static char *get_script_path(const char *script_root
, const char *suffix
)
883 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
884 char scripts_path
[MAXPATHLEN
];
885 char script_path
[MAXPATHLEN
];
886 DIR *scripts_dir
, *lang_dir
;
887 char lang_path
[MAXPATHLEN
];
888 char *str
, *__script_root
;
891 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
893 scripts_dir
= opendir(scripts_path
);
897 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
898 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
900 lang_dir
= opendir(lang_path
);
904 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
905 __script_root
= strdup(script_dirent
.d_name
);
906 str
= (char *)ends_with(__script_root
, suffix
);
909 if (strcmp(__script_root
, script_root
))
911 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
912 lang_path
, script_dirent
.d_name
);
913 path
= strdup(script_path
);
924 static bool is_top_script(const char *script_path
)
926 return ends_with(script_path
, "top") == NULL
? false : true;
929 static int has_required_arg(char *script_path
)
931 struct script_desc
*desc
;
935 desc
= script_desc__new(NULL
);
937 if (read_script_info(desc
, script_path
))
943 for (p
= desc
->args
; *p
; p
++)
947 script_desc__delete(desc
);
952 static const char * const script_usage
[] = {
953 "perf script [<options>]",
954 "perf script [<options>] record <script> [<record-options>] <command>",
955 "perf script [<options>] report <script> [script-args]",
956 "perf script [<options>] <script> [<record-options>] <command>",
957 "perf script [<options>] <top-script> [script-args]",
961 static const struct option options
[] = {
962 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
963 "dump raw trace in ASCII"),
964 OPT_INCR('v', "verbose", &verbose
,
965 "be more verbose (show symbol address, etc)"),
966 OPT_BOOLEAN('L', "Latency", &latency_format
,
967 "show latency attributes (irqs/preemption disabled, etc)"),
968 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
969 list_available_scripts
),
970 OPT_CALLBACK('s', "script", NULL
, "name",
971 "script file name (lang:script name, script name, or *)",
973 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
974 "generate perf-script.xx script in specified language"),
975 OPT_STRING('i', "input", &input_name
, "file",
977 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
978 "do various checks like samples ordering and lost events"),
979 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
980 "file", "vmlinux pathname"),
981 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
982 "file", "kallsyms pathname"),
983 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain
,
984 "When printing symbols do not display call chain"),
985 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
986 "Look for files with symbols relative to this directory"),
987 OPT_CALLBACK('f', "fields", NULL
, "str",
988 "comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace,raw. Fields: comm,tid,pid,time,cpu,event,trace,sym",
989 parse_output_fields
),
994 static bool have_cmd(int argc
, const char **argv
)
996 char **__argv
= malloc(sizeof(const char *) * argc
);
1000 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
1001 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
1002 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
1008 int cmd_script(int argc
, const char **argv
, const char *prefix __used
)
1010 char *rec_script_path
= NULL
;
1011 char *rep_script_path
= NULL
;
1012 struct perf_session
*session
;
1013 char *script_path
= NULL
;
1014 const char **__argv
;
1020 argc
= parse_options(argc
, argv
, options
, script_usage
,
1021 PARSE_OPT_STOP_AT_NON_OPTION
);
1023 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
1024 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
1025 if (!rec_script_path
)
1026 return cmd_record(argc
, argv
, NULL
);
1029 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
1030 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
1031 if (!rep_script_path
) {
1033 "Please specify a valid report script"
1034 "(see 'perf script -l' for listing)\n");
1039 /* make sure PERF_EXEC_PATH is set for scripts */
1040 perf_set_argv_exec_path(perf_exec_path());
1042 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
1047 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
1048 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
1050 if (!rec_script_path
&& !rep_script_path
) {
1051 fprintf(stderr
, " Couldn't find script %s\n\n See perf"
1052 " script -l for available scripts.\n", argv
[0]);
1053 usage_with_options(script_usage
, options
);
1056 if (is_top_script(argv
[0])) {
1057 rep_args
= argc
- 1;
1061 rep_args
= has_required_arg(rep_script_path
);
1062 rec_args
= (argc
- 1) - rep_args
;
1064 fprintf(stderr
, " %s script requires options."
1065 "\n\n See perf script -l for available "
1066 "scripts and options.\n", argv
[0]);
1067 usage_with_options(script_usage
, options
);
1071 if (pipe(live_pipe
) < 0) {
1072 perror("failed to create pipe");
1078 perror("failed to fork");
1086 dup2(live_pipe
[1], 1);
1087 close(live_pipe
[0]);
1089 if (!is_top_script(argv
[0]))
1090 system_wide
= !have_cmd(argc
- rep_args
,
1093 __argv
= malloc((argc
+ 6) * sizeof(const char *));
1097 __argv
[j
++] = "/bin/sh";
1098 __argv
[j
++] = rec_script_path
;
1104 for (i
= rep_args
+ 1; i
< argc
; i
++)
1105 __argv
[j
++] = argv
[i
];
1108 execvp("/bin/sh", (char **)__argv
);
1113 dup2(live_pipe
[0], 0);
1114 close(live_pipe
[1]);
1116 __argv
= malloc((argc
+ 4) * sizeof(const char *));
1120 __argv
[j
++] = "/bin/sh";
1121 __argv
[j
++] = rep_script_path
;
1122 for (i
= 1; i
< rep_args
+ 1; i
++)
1123 __argv
[j
++] = argv
[i
];
1128 execvp("/bin/sh", (char **)__argv
);
1133 if (rec_script_path
)
1134 script_path
= rec_script_path
;
1135 if (rep_script_path
)
1136 script_path
= rep_script_path
;
1139 system_wide
= false;
1142 if (rec_script_path
)
1143 system_wide
= !have_cmd(argc
- 1, &argv
[1]);
1145 __argv
= malloc((argc
+ 2) * sizeof(const char *));
1148 __argv
[j
++] = "/bin/sh";
1149 __argv
[j
++] = script_path
;
1152 for (i
= 2; i
< argc
; i
++)
1153 __argv
[j
++] = argv
[i
];
1156 execvp("/bin/sh", (char **)__argv
);
1161 if (symbol__init() < 0)
1166 session
= perf_session__new(input_name
, O_RDONLY
, 0, false, &event_ops
);
1167 if (session
== NULL
)
1171 symbol_conf
.use_callchain
= true;
1173 symbol_conf
.use_callchain
= false;
1175 if (generate_script_lang
) {
1176 struct stat perf_stat
;
1179 if (output_set_by_user()) {
1181 "custom fields not supported for generated scripts");
1185 input
= open(input_name
, O_RDONLY
);
1187 perror("failed to open file");
1191 err
= fstat(input
, &perf_stat
);
1193 perror("failed to stat file");
1197 if (!perf_stat
.st_size
) {
1198 fprintf(stderr
, "zero-sized file, nothing to do!\n");
1202 scripting_ops
= script_spec__lookup(generate_script_lang
);
1203 if (!scripting_ops
) {
1204 fprintf(stderr
, "invalid language specifier");
1208 err
= scripting_ops
->generate_script("perf-script");
1213 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
1216 pr_debug("perf script started with script %s\n\n", script_name
);
1220 err
= perf_session__check_output_opt(session
);
1224 err
= __cmd_script(session
);
1226 perf_session__delete(session
);
1227 cleanup_scripting();