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/parse-options.h"
14 #include "util/util.h"
16 static char const *script_name
;
17 static char const *generate_script_lang
;
18 static bool debug_mode
;
19 static u64 last_timestamp
;
20 static u64 nr_unordered
;
21 extern const struct option record_options
[];
23 static int default_start_script(const char *script __unused
,
25 const char **argv __unused
)
30 static int default_stop_script(void)
35 static int default_generate_script(const char *outfile __unused
)
40 static struct scripting_ops default_scripting_ops
= {
41 .start_script
= default_start_script
,
42 .stop_script
= default_stop_script
,
43 .process_event
= print_event
,
44 .generate_script
= default_generate_script
,
47 static struct scripting_ops
*scripting_ops
;
49 static void setup_scripting(void)
51 setup_perl_scripting();
52 setup_python_scripting();
54 scripting_ops
= &default_scripting_ops
;
57 static int cleanup_scripting(void)
59 pr_debug("\nperf script stopped\n");
61 return scripting_ops
->stop_script();
64 static char const *input_name
= "perf.data";
66 static int process_sample_event(event_t
*event
, struct sample_data
*sample
,
67 struct perf_session
*session
)
69 struct thread
*thread
= perf_session__findnew(session
, event
->ip
.pid
);
72 pr_debug("problem processing %d event, skipping it.\n",
77 if (session
->sample_type
& PERF_SAMPLE_RAW
) {
79 if (sample
->time
< last_timestamp
) {
80 pr_err("Samples misordered, previous: %llu "
81 "this: %llu\n", last_timestamp
,
85 last_timestamp
= sample
->time
;
89 * FIXME: better resolve from pid from the struct trace_entry
90 * field, although it should be the same than this perf
93 scripting_ops
->process_event(sample
->cpu
, sample
->raw_data
,
95 sample
->time
, thread
->comm
);
98 session
->hists
.stats
.total_period
+= sample
->period
;
104 static int process_lost_event(event_t
*event
, struct sample_data
*sample __used
,
105 struct perf_session
*session __used
)
107 nr_lost
+= event
->lost
.lost
;
112 static struct perf_event_ops event_ops
= {
113 .sample
= process_sample_event
,
114 .comm
= event__process_comm
,
115 .attr
= event__process_attr
,
116 .event_type
= event__process_event_type
,
117 .tracing_data
= event__process_tracing_data
,
118 .build_id
= event__process_build_id
,
119 .lost
= process_lost_event
,
120 .ordering_requires_timestamps
= true,
121 .ordered_samples
= true,
124 extern volatile int session_done
;
126 static void sig_handler(int sig __unused
)
131 static int __cmd_script(struct perf_session
*session
)
135 signal(SIGINT
, sig_handler
);
137 ret
= perf_session__process_events(session
, &event_ops
);
140 pr_err("Misordered timestamps: %llu\n", nr_unordered
);
141 pr_err("Lost events: %llu\n", nr_lost
);
148 struct list_head node
;
149 struct scripting_ops
*ops
;
153 LIST_HEAD(script_specs
);
155 static struct script_spec
*script_spec__new(const char *spec
,
156 struct scripting_ops
*ops
)
158 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
161 strcpy(s
->spec
, spec
);
168 static void script_spec__delete(struct script_spec
*s
)
174 static void script_spec__add(struct script_spec
*s
)
176 list_add_tail(&s
->node
, &script_specs
);
179 static struct script_spec
*script_spec__find(const char *spec
)
181 struct script_spec
*s
;
183 list_for_each_entry(s
, &script_specs
, node
)
184 if (strcasecmp(s
->spec
, spec
) == 0)
189 static struct script_spec
*script_spec__findnew(const char *spec
,
190 struct scripting_ops
*ops
)
192 struct script_spec
*s
= script_spec__find(spec
);
197 s
= script_spec__new(spec
, ops
);
199 goto out_delete_spec
;
206 script_spec__delete(s
);
211 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
213 struct script_spec
*s
;
215 s
= script_spec__find(spec
);
219 s
= script_spec__findnew(spec
, ops
);
226 static struct scripting_ops
*script_spec__lookup(const char *spec
)
228 struct script_spec
*s
= script_spec__find(spec
);
235 static void list_available_languages(void)
237 struct script_spec
*s
;
239 fprintf(stderr
, "\n");
240 fprintf(stderr
, "Scripting language extensions (used in "
241 "perf script -s [spec:]script.[spec]):\n\n");
243 list_for_each_entry(s
, &script_specs
, node
)
244 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
246 fprintf(stderr
, "\n");
249 static int parse_scriptname(const struct option
*opt __used
,
250 const char *str
, int unset __used
)
253 const char *script
, *ext
;
256 if (strcmp(str
, "lang") == 0) {
257 list_available_languages();
261 script
= strchr(str
, ':');
264 if (len
>= PATH_MAX
) {
265 fprintf(stderr
, "invalid language specifier");
268 strncpy(spec
, str
, len
);
270 scripting_ops
= script_spec__lookup(spec
);
271 if (!scripting_ops
) {
272 fprintf(stderr
, "invalid language specifier");
278 ext
= strrchr(script
, '.');
280 fprintf(stderr
, "invalid script extension");
283 scripting_ops
= script_spec__lookup(++ext
);
284 if (!scripting_ops
) {
285 fprintf(stderr
, "invalid script extension");
290 script_name
= strdup(script
);
295 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
296 static int is_directory(const char *base_path
, const struct dirent
*dent
)
301 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
305 return S_ISDIR(st
.st_mode
);
308 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
309 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
311 if ((lang_dirent.d_type == DT_DIR || \
312 (lang_dirent.d_type == DT_UNKNOWN && \
313 is_directory(scripts_path, &lang_dirent))) && \
314 (strcmp(lang_dirent.d_name, ".")) && \
315 (strcmp(lang_dirent.d_name, "..")))
317 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
318 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
320 if (script_dirent.d_type != DT_DIR && \
321 (script_dirent.d_type != DT_UNKNOWN || \
322 !is_directory(lang_path, &script_dirent)))
325 #define RECORD_SUFFIX "-record"
326 #define REPORT_SUFFIX "-report"
329 struct list_head node
;
335 LIST_HEAD(script_descs
);
337 static struct script_desc
*script_desc__new(const char *name
)
339 struct script_desc
*s
= zalloc(sizeof(*s
));
341 if (s
!= NULL
&& name
)
342 s
->name
= strdup(name
);
347 static void script_desc__delete(struct script_desc
*s
)
355 static void script_desc__add(struct script_desc
*s
)
357 list_add_tail(&s
->node
, &script_descs
);
360 static struct script_desc
*script_desc__find(const char *name
)
362 struct script_desc
*s
;
364 list_for_each_entry(s
, &script_descs
, node
)
365 if (strcasecmp(s
->name
, name
) == 0)
370 static struct script_desc
*script_desc__findnew(const char *name
)
372 struct script_desc
*s
= script_desc__find(name
);
377 s
= script_desc__new(name
);
379 goto out_delete_desc
;
386 script_desc__delete(s
);
391 static const char *ends_with(const char *str
, const char *suffix
)
393 size_t suffix_len
= strlen(suffix
);
396 if (strlen(str
) > suffix_len
) {
397 p
= str
+ strlen(str
) - suffix_len
;
398 if (!strncmp(p
, suffix
, suffix_len
))
405 static char *ltrim(char *str
)
407 int len
= strlen(str
);
409 while (len
&& isspace(*str
)) {
417 static int read_script_info(struct script_desc
*desc
, const char *filename
)
419 char line
[BUFSIZ
], *p
;
422 fp
= fopen(filename
, "r");
426 while (fgets(line
, sizeof(line
), fp
)) {
433 if (strlen(p
) && *p
== '!')
437 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
438 p
[strlen(p
) - 1] = '\0';
440 if (!strncmp(p
, "description:", strlen("description:"))) {
441 p
+= strlen("description:");
442 desc
->half_liner
= strdup(ltrim(p
));
446 if (!strncmp(p
, "args:", strlen("args:"))) {
447 p
+= strlen("args:");
448 desc
->args
= strdup(ltrim(p
));
458 static int list_available_scripts(const struct option
*opt __used
,
459 const char *s __used
, int unset __used
)
461 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
462 char scripts_path
[MAXPATHLEN
];
463 DIR *scripts_dir
, *lang_dir
;
464 char script_path
[MAXPATHLEN
];
465 char lang_path
[MAXPATHLEN
];
466 struct script_desc
*desc
;
467 char first_half
[BUFSIZ
];
471 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
473 scripts_dir
= opendir(scripts_path
);
477 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
478 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
480 lang_dir
= opendir(lang_path
);
484 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
485 script_root
= strdup(script_dirent
.d_name
);
486 str
= (char *)ends_with(script_root
, REPORT_SUFFIX
);
489 desc
= script_desc__findnew(script_root
);
490 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
491 lang_path
, script_dirent
.d_name
);
492 read_script_info(desc
, script_path
);
498 fprintf(stdout
, "List of available trace scripts:\n");
499 list_for_each_entry(desc
, &script_descs
, node
) {
500 sprintf(first_half
, "%s %s", desc
->name
,
501 desc
->args
? desc
->args
: "");
502 fprintf(stdout
, " %-36s %s\n", first_half
,
503 desc
->half_liner
? desc
->half_liner
: "");
509 static char *get_script_path(const char *script_root
, const char *suffix
)
511 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
512 char scripts_path
[MAXPATHLEN
];
513 char script_path
[MAXPATHLEN
];
514 DIR *scripts_dir
, *lang_dir
;
515 char lang_path
[MAXPATHLEN
];
516 char *str
, *__script_root
;
519 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
521 scripts_dir
= opendir(scripts_path
);
525 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
526 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
528 lang_dir
= opendir(lang_path
);
532 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
533 __script_root
= strdup(script_dirent
.d_name
);
534 str
= (char *)ends_with(__script_root
, suffix
);
537 if (strcmp(__script_root
, script_root
))
539 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
540 lang_path
, script_dirent
.d_name
);
541 path
= strdup(script_path
);
552 static bool is_top_script(const char *script_path
)
554 return ends_with(script_path
, "top") == NULL
? false : true;
557 static int has_required_arg(char *script_path
)
559 struct script_desc
*desc
;
563 desc
= script_desc__new(NULL
);
565 if (read_script_info(desc
, script_path
))
571 for (p
= desc
->args
; *p
; p
++)
575 script_desc__delete(desc
);
580 static const char * const script_usage
[] = {
581 "perf script [<options>]",
582 "perf script [<options>] record <script> [<record-options>] <command>",
583 "perf script [<options>] report <script> [script-args]",
584 "perf script [<options>] <script> [<record-options>] <command>",
585 "perf script [<options>] <top-script> [script-args]",
589 static const struct option options
[] = {
590 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
591 "dump raw trace in ASCII"),
592 OPT_INCR('v', "verbose", &verbose
,
593 "be more verbose (show symbol address, etc)"),
594 OPT_BOOLEAN('L', "Latency", &latency_format
,
595 "show latency attributes (irqs/preemption disabled, etc)"),
596 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
597 list_available_scripts
),
598 OPT_CALLBACK('s', "script", NULL
, "name",
599 "script file name (lang:script name, script name, or *)",
601 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
602 "generate perf-script.xx script in specified language"),
603 OPT_STRING('i', "input", &input_name
, "file",
605 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
606 "do various checks like samples ordering and lost events"),
611 static bool have_cmd(int argc
, const char **argv
)
613 char **__argv
= malloc(sizeof(const char *) * argc
);
617 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
618 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
619 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
625 int cmd_script(int argc
, const char **argv
, const char *prefix __used
)
627 char *rec_script_path
= NULL
;
628 char *rep_script_path
= NULL
;
629 struct perf_session
*session
;
630 char *script_path
= NULL
;
637 argc
= parse_options(argc
, argv
, options
, script_usage
,
638 PARSE_OPT_STOP_AT_NON_OPTION
);
640 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
641 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
642 if (!rec_script_path
)
643 return cmd_record(argc
, argv
, NULL
);
646 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
647 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
648 if (!rep_script_path
) {
650 "Please specify a valid report script"
651 "(see 'perf script -l' for listing)\n");
656 /* make sure PERF_EXEC_PATH is set for scripts */
657 perf_set_argv_exec_path(perf_exec_path());
659 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
664 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
665 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
667 if (!rec_script_path
&& !rep_script_path
) {
668 fprintf(stderr
, " Couldn't find script %s\n\n See perf"
669 " script -l for available scripts.\n", argv
[0]);
670 usage_with_options(script_usage
, options
);
673 if (is_top_script(argv
[0])) {
678 rep_args
= has_required_arg(rep_script_path
);
679 rec_args
= (argc
- 1) - rep_args
;
681 fprintf(stderr
, " %s script requires options."
682 "\n\n See perf script -l for available "
683 "scripts and options.\n", argv
[0]);
684 usage_with_options(script_usage
, options
);
688 if (pipe(live_pipe
) < 0) {
689 perror("failed to create pipe");
695 perror("failed to fork");
703 dup2(live_pipe
[1], 1);
706 if (!is_top_script(argv
[0]))
707 system_wide
= !have_cmd(argc
- rep_args
,
710 __argv
= malloc((argc
+ 6) * sizeof(const char *));
714 __argv
[j
++] = "/bin/sh";
715 __argv
[j
++] = rec_script_path
;
721 for (i
= rep_args
+ 1; i
< argc
; i
++)
722 __argv
[j
++] = argv
[i
];
725 execvp("/bin/sh", (char **)__argv
);
730 dup2(live_pipe
[0], 0);
733 __argv
= malloc((argc
+ 4) * sizeof(const char *));
737 __argv
[j
++] = "/bin/sh";
738 __argv
[j
++] = rep_script_path
;
739 for (i
= 1; i
< rep_args
+ 1; i
++)
740 __argv
[j
++] = argv
[i
];
745 execvp("/bin/sh", (char **)__argv
);
751 script_path
= rec_script_path
;
753 script_path
= rep_script_path
;
760 system_wide
= !have_cmd(argc
- 1, &argv
[1]);
762 __argv
= malloc((argc
+ 2) * sizeof(const char *));
765 __argv
[j
++] = "/bin/sh";
766 __argv
[j
++] = script_path
;
769 for (i
= 2; i
< argc
; i
++)
770 __argv
[j
++] = argv
[i
];
773 execvp("/bin/sh", (char **)__argv
);
778 if (symbol__init() < 0)
783 session
= perf_session__new(input_name
, O_RDONLY
, 0, false, &event_ops
);
787 if (strcmp(input_name
, "-") &&
788 !perf_session__has_traces(session
, "record -R"))
791 if (generate_script_lang
) {
792 struct stat perf_stat
;
794 int input
= open(input_name
, O_RDONLY
);
796 perror("failed to open file");
800 err
= fstat(input
, &perf_stat
);
802 perror("failed to stat file");
806 if (!perf_stat
.st_size
) {
807 fprintf(stderr
, "zero-sized file, nothing to do!\n");
811 scripting_ops
= script_spec__lookup(generate_script_lang
);
812 if (!scripting_ops
) {
813 fprintf(stderr
, "invalid language specifier");
817 err
= scripting_ops
->generate_script("perf-script");
822 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
825 pr_debug("perf script started with script %s\n\n", script_name
);
828 err
= __cmd_script(session
);
830 perf_session__delete(session
);