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
;
102 static struct perf_event_ops event_ops
= {
103 .sample
= process_sample_event
,
104 .comm
= event__process_comm
,
105 .attr
= event__process_attr
,
106 .event_type
= event__process_event_type
,
107 .tracing_data
= event__process_tracing_data
,
108 .build_id
= event__process_build_id
,
109 .ordering_requires_timestamps
= true,
110 .ordered_samples
= true,
113 extern volatile int session_done
;
115 static void sig_handler(int sig __unused
)
120 static int __cmd_script(struct perf_session
*session
)
124 signal(SIGINT
, sig_handler
);
126 ret
= perf_session__process_events(session
, &event_ops
);
129 pr_err("Misordered timestamps: %llu\n", nr_unordered
);
135 struct list_head node
;
136 struct scripting_ops
*ops
;
140 static LIST_HEAD(script_specs
);
142 static struct script_spec
*script_spec__new(const char *spec
,
143 struct scripting_ops
*ops
)
145 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
148 strcpy(s
->spec
, spec
);
155 static void script_spec__delete(struct script_spec
*s
)
161 static void script_spec__add(struct script_spec
*s
)
163 list_add_tail(&s
->node
, &script_specs
);
166 static struct script_spec
*script_spec__find(const char *spec
)
168 struct script_spec
*s
;
170 list_for_each_entry(s
, &script_specs
, node
)
171 if (strcasecmp(s
->spec
, spec
) == 0)
176 static struct script_spec
*script_spec__findnew(const char *spec
,
177 struct scripting_ops
*ops
)
179 struct script_spec
*s
= script_spec__find(spec
);
184 s
= script_spec__new(spec
, ops
);
186 goto out_delete_spec
;
193 script_spec__delete(s
);
198 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
200 struct script_spec
*s
;
202 s
= script_spec__find(spec
);
206 s
= script_spec__findnew(spec
, ops
);
213 static struct scripting_ops
*script_spec__lookup(const char *spec
)
215 struct script_spec
*s
= script_spec__find(spec
);
222 static void list_available_languages(void)
224 struct script_spec
*s
;
226 fprintf(stderr
, "\n");
227 fprintf(stderr
, "Scripting language extensions (used in "
228 "perf script -s [spec:]script.[spec]):\n\n");
230 list_for_each_entry(s
, &script_specs
, node
)
231 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
233 fprintf(stderr
, "\n");
236 static int parse_scriptname(const struct option
*opt __used
,
237 const char *str
, int unset __used
)
240 const char *script
, *ext
;
243 if (strcmp(str
, "lang") == 0) {
244 list_available_languages();
248 script
= strchr(str
, ':');
251 if (len
>= PATH_MAX
) {
252 fprintf(stderr
, "invalid language specifier");
255 strncpy(spec
, str
, len
);
257 scripting_ops
= script_spec__lookup(spec
);
258 if (!scripting_ops
) {
259 fprintf(stderr
, "invalid language specifier");
265 ext
= strrchr(script
, '.');
267 fprintf(stderr
, "invalid script extension");
270 scripting_ops
= script_spec__lookup(++ext
);
271 if (!scripting_ops
) {
272 fprintf(stderr
, "invalid script extension");
277 script_name
= strdup(script
);
282 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
283 static int is_directory(const char *base_path
, const struct dirent
*dent
)
288 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
292 return S_ISDIR(st
.st_mode
);
295 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
296 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
298 if ((lang_dirent.d_type == DT_DIR || \
299 (lang_dirent.d_type == DT_UNKNOWN && \
300 is_directory(scripts_path, &lang_dirent))) && \
301 (strcmp(lang_dirent.d_name, ".")) && \
302 (strcmp(lang_dirent.d_name, "..")))
304 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
305 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
307 if (script_dirent.d_type != DT_DIR && \
308 (script_dirent.d_type != DT_UNKNOWN || \
309 !is_directory(lang_path, &script_dirent)))
312 #define RECORD_SUFFIX "-record"
313 #define REPORT_SUFFIX "-report"
316 struct list_head node
;
322 static LIST_HEAD(script_descs
);
324 static struct script_desc
*script_desc__new(const char *name
)
326 struct script_desc
*s
= zalloc(sizeof(*s
));
328 if (s
!= NULL
&& name
)
329 s
->name
= strdup(name
);
334 static void script_desc__delete(struct script_desc
*s
)
342 static void script_desc__add(struct script_desc
*s
)
344 list_add_tail(&s
->node
, &script_descs
);
347 static struct script_desc
*script_desc__find(const char *name
)
349 struct script_desc
*s
;
351 list_for_each_entry(s
, &script_descs
, node
)
352 if (strcasecmp(s
->name
, name
) == 0)
357 static struct script_desc
*script_desc__findnew(const char *name
)
359 struct script_desc
*s
= script_desc__find(name
);
364 s
= script_desc__new(name
);
366 goto out_delete_desc
;
373 script_desc__delete(s
);
378 static const char *ends_with(const char *str
, const char *suffix
)
380 size_t suffix_len
= strlen(suffix
);
383 if (strlen(str
) > suffix_len
) {
384 p
= str
+ strlen(str
) - suffix_len
;
385 if (!strncmp(p
, suffix
, suffix_len
))
392 static char *ltrim(char *str
)
394 int len
= strlen(str
);
396 while (len
&& isspace(*str
)) {
404 static int read_script_info(struct script_desc
*desc
, const char *filename
)
406 char line
[BUFSIZ
], *p
;
409 fp
= fopen(filename
, "r");
413 while (fgets(line
, sizeof(line
), fp
)) {
420 if (strlen(p
) && *p
== '!')
424 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
425 p
[strlen(p
) - 1] = '\0';
427 if (!strncmp(p
, "description:", strlen("description:"))) {
428 p
+= strlen("description:");
429 desc
->half_liner
= strdup(ltrim(p
));
433 if (!strncmp(p
, "args:", strlen("args:"))) {
434 p
+= strlen("args:");
435 desc
->args
= strdup(ltrim(p
));
445 static int list_available_scripts(const struct option
*opt __used
,
446 const char *s __used
, int unset __used
)
448 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
449 char scripts_path
[MAXPATHLEN
];
450 DIR *scripts_dir
, *lang_dir
;
451 char script_path
[MAXPATHLEN
];
452 char lang_path
[MAXPATHLEN
];
453 struct script_desc
*desc
;
454 char first_half
[BUFSIZ
];
458 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
460 scripts_dir
= opendir(scripts_path
);
464 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
465 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
467 lang_dir
= opendir(lang_path
);
471 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
472 script_root
= strdup(script_dirent
.d_name
);
473 str
= (char *)ends_with(script_root
, REPORT_SUFFIX
);
476 desc
= script_desc__findnew(script_root
);
477 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
478 lang_path
, script_dirent
.d_name
);
479 read_script_info(desc
, script_path
);
485 fprintf(stdout
, "List of available trace scripts:\n");
486 list_for_each_entry(desc
, &script_descs
, node
) {
487 sprintf(first_half
, "%s %s", desc
->name
,
488 desc
->args
? desc
->args
: "");
489 fprintf(stdout
, " %-36s %s\n", first_half
,
490 desc
->half_liner
? desc
->half_liner
: "");
496 static char *get_script_path(const char *script_root
, const char *suffix
)
498 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
499 char scripts_path
[MAXPATHLEN
];
500 char script_path
[MAXPATHLEN
];
501 DIR *scripts_dir
, *lang_dir
;
502 char lang_path
[MAXPATHLEN
];
503 char *str
, *__script_root
;
506 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
508 scripts_dir
= opendir(scripts_path
);
512 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
513 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
515 lang_dir
= opendir(lang_path
);
519 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
520 __script_root
= strdup(script_dirent
.d_name
);
521 str
= (char *)ends_with(__script_root
, suffix
);
524 if (strcmp(__script_root
, script_root
))
526 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
527 lang_path
, script_dirent
.d_name
);
528 path
= strdup(script_path
);
539 static bool is_top_script(const char *script_path
)
541 return ends_with(script_path
, "top") == NULL
? false : true;
544 static int has_required_arg(char *script_path
)
546 struct script_desc
*desc
;
550 desc
= script_desc__new(NULL
);
552 if (read_script_info(desc
, script_path
))
558 for (p
= desc
->args
; *p
; p
++)
562 script_desc__delete(desc
);
567 static const char * const script_usage
[] = {
568 "perf script [<options>]",
569 "perf script [<options>] record <script> [<record-options>] <command>",
570 "perf script [<options>] report <script> [script-args]",
571 "perf script [<options>] <script> [<record-options>] <command>",
572 "perf script [<options>] <top-script> [script-args]",
576 static const struct option options
[] = {
577 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
578 "dump raw trace in ASCII"),
579 OPT_INCR('v', "verbose", &verbose
,
580 "be more verbose (show symbol address, etc)"),
581 OPT_BOOLEAN('L', "Latency", &latency_format
,
582 "show latency attributes (irqs/preemption disabled, etc)"),
583 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
584 list_available_scripts
),
585 OPT_CALLBACK('s', "script", NULL
, "name",
586 "script file name (lang:script name, script name, or *)",
588 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
589 "generate perf-script.xx script in specified language"),
590 OPT_STRING('i', "input", &input_name
, "file",
592 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
593 "do various checks like samples ordering and lost events"),
598 static bool have_cmd(int argc
, const char **argv
)
600 char **__argv
= malloc(sizeof(const char *) * argc
);
604 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
605 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
606 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
612 int cmd_script(int argc
, const char **argv
, const char *prefix __used
)
614 char *rec_script_path
= NULL
;
615 char *rep_script_path
= NULL
;
616 struct perf_session
*session
;
617 char *script_path
= NULL
;
624 argc
= parse_options(argc
, argv
, options
, script_usage
,
625 PARSE_OPT_STOP_AT_NON_OPTION
);
627 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
628 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
629 if (!rec_script_path
)
630 return cmd_record(argc
, argv
, NULL
);
633 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
634 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
635 if (!rep_script_path
) {
637 "Please specify a valid report script"
638 "(see 'perf script -l' for listing)\n");
643 /* make sure PERF_EXEC_PATH is set for scripts */
644 perf_set_argv_exec_path(perf_exec_path());
646 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
651 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
652 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
654 if (!rec_script_path
&& !rep_script_path
) {
655 fprintf(stderr
, " Couldn't find script %s\n\n See perf"
656 " script -l for available scripts.\n", argv
[0]);
657 usage_with_options(script_usage
, options
);
660 if (is_top_script(argv
[0])) {
665 rep_args
= has_required_arg(rep_script_path
);
666 rec_args
= (argc
- 1) - rep_args
;
668 fprintf(stderr
, " %s script requires options."
669 "\n\n See perf script -l for available "
670 "scripts and options.\n", argv
[0]);
671 usage_with_options(script_usage
, options
);
675 if (pipe(live_pipe
) < 0) {
676 perror("failed to create pipe");
682 perror("failed to fork");
690 dup2(live_pipe
[1], 1);
693 if (!is_top_script(argv
[0]))
694 system_wide
= !have_cmd(argc
- rep_args
,
697 __argv
= malloc((argc
+ 6) * sizeof(const char *));
701 __argv
[j
++] = "/bin/sh";
702 __argv
[j
++] = rec_script_path
;
708 for (i
= rep_args
+ 1; i
< argc
; i
++)
709 __argv
[j
++] = argv
[i
];
712 execvp("/bin/sh", (char **)__argv
);
717 dup2(live_pipe
[0], 0);
720 __argv
= malloc((argc
+ 4) * sizeof(const char *));
724 __argv
[j
++] = "/bin/sh";
725 __argv
[j
++] = rep_script_path
;
726 for (i
= 1; i
< rep_args
+ 1; i
++)
727 __argv
[j
++] = argv
[i
];
732 execvp("/bin/sh", (char **)__argv
);
738 script_path
= rec_script_path
;
740 script_path
= rep_script_path
;
747 system_wide
= !have_cmd(argc
- 1, &argv
[1]);
749 __argv
= malloc((argc
+ 2) * sizeof(const char *));
752 __argv
[j
++] = "/bin/sh";
753 __argv
[j
++] = script_path
;
756 for (i
= 2; i
< argc
; i
++)
757 __argv
[j
++] = argv
[i
];
760 execvp("/bin/sh", (char **)__argv
);
765 if (symbol__init() < 0)
770 session
= perf_session__new(input_name
, O_RDONLY
, 0, false, &event_ops
);
774 if (strcmp(input_name
, "-") &&
775 !perf_session__has_traces(session
, "record -R"))
778 if (generate_script_lang
) {
779 struct stat perf_stat
;
781 int input
= open(input_name
, O_RDONLY
);
783 perror("failed to open file");
787 err
= fstat(input
, &perf_stat
);
789 perror("failed to stat file");
793 if (!perf_stat
.st_size
) {
794 fprintf(stderr
, "zero-sized file, nothing to do!\n");
798 scripting_ops
= script_spec__lookup(generate_script_lang
);
799 if (!scripting_ops
) {
800 fprintf(stderr
, "invalid language specifier");
804 err
= scripting_ops
->generate_script("perf-script");
809 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
812 pr_debug("perf script started with script %s\n\n", script_name
);
815 err
= __cmd_script(session
);
817 perf_session__delete(session
);