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 .ordered_samples
= true,
123 extern volatile int session_done
;
125 static void sig_handler(int sig __unused
)
130 static int __cmd_script(struct perf_session
*session
)
134 signal(SIGINT
, sig_handler
);
136 ret
= perf_session__process_events(session
, &event_ops
);
139 pr_err("Misordered timestamps: %llu\n", nr_unordered
);
140 pr_err("Lost events: %llu\n", nr_lost
);
147 struct list_head node
;
148 struct scripting_ops
*ops
;
152 LIST_HEAD(script_specs
);
154 static struct script_spec
*script_spec__new(const char *spec
,
155 struct scripting_ops
*ops
)
157 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
160 strcpy(s
->spec
, spec
);
167 static void script_spec__delete(struct script_spec
*s
)
173 static void script_spec__add(struct script_spec
*s
)
175 list_add_tail(&s
->node
, &script_specs
);
178 static struct script_spec
*script_spec__find(const char *spec
)
180 struct script_spec
*s
;
182 list_for_each_entry(s
, &script_specs
, node
)
183 if (strcasecmp(s
->spec
, spec
) == 0)
188 static struct script_spec
*script_spec__findnew(const char *spec
,
189 struct scripting_ops
*ops
)
191 struct script_spec
*s
= script_spec__find(spec
);
196 s
= script_spec__new(spec
, ops
);
198 goto out_delete_spec
;
205 script_spec__delete(s
);
210 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
212 struct script_spec
*s
;
214 s
= script_spec__find(spec
);
218 s
= script_spec__findnew(spec
, ops
);
225 static struct scripting_ops
*script_spec__lookup(const char *spec
)
227 struct script_spec
*s
= script_spec__find(spec
);
234 static void list_available_languages(void)
236 struct script_spec
*s
;
238 fprintf(stderr
, "\n");
239 fprintf(stderr
, "Scripting language extensions (used in "
240 "perf script -s [spec:]script.[spec]):\n\n");
242 list_for_each_entry(s
, &script_specs
, node
)
243 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
245 fprintf(stderr
, "\n");
248 static int parse_scriptname(const struct option
*opt __used
,
249 const char *str
, int unset __used
)
252 const char *script
, *ext
;
255 if (strcmp(str
, "lang") == 0) {
256 list_available_languages();
260 script
= strchr(str
, ':');
263 if (len
>= PATH_MAX
) {
264 fprintf(stderr
, "invalid language specifier");
267 strncpy(spec
, str
, len
);
269 scripting_ops
= script_spec__lookup(spec
);
270 if (!scripting_ops
) {
271 fprintf(stderr
, "invalid language specifier");
277 ext
= strrchr(script
, '.');
279 fprintf(stderr
, "invalid script extension");
282 scripting_ops
= script_spec__lookup(++ext
);
283 if (!scripting_ops
) {
284 fprintf(stderr
, "invalid script extension");
289 script_name
= strdup(script
);
294 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
295 static int is_directory(const char *base_path
, const struct dirent
*dent
)
300 sprintf(path
, "%s/%s", base_path
, dent
->d_name
);
304 return S_ISDIR(st
.st_mode
);
307 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
308 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
310 if ((lang_dirent.d_type == DT_DIR || \
311 (lang_dirent.d_type == DT_UNKNOWN && \
312 is_directory(scripts_path, &lang_dirent))) && \
313 (strcmp(lang_dirent.d_name, ".")) && \
314 (strcmp(lang_dirent.d_name, "..")))
316 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
317 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
319 if (script_dirent.d_type != DT_DIR && \
320 (script_dirent.d_type != DT_UNKNOWN || \
321 !is_directory(lang_path, &script_dirent)))
324 #define RECORD_SUFFIX "-record"
325 #define REPORT_SUFFIX "-report"
328 struct list_head node
;
334 LIST_HEAD(script_descs
);
336 static struct script_desc
*script_desc__new(const char *name
)
338 struct script_desc
*s
= zalloc(sizeof(*s
));
340 if (s
!= NULL
&& name
)
341 s
->name
= strdup(name
);
346 static void script_desc__delete(struct script_desc
*s
)
354 static void script_desc__add(struct script_desc
*s
)
356 list_add_tail(&s
->node
, &script_descs
);
359 static struct script_desc
*script_desc__find(const char *name
)
361 struct script_desc
*s
;
363 list_for_each_entry(s
, &script_descs
, node
)
364 if (strcasecmp(s
->name
, name
) == 0)
369 static struct script_desc
*script_desc__findnew(const char *name
)
371 struct script_desc
*s
= script_desc__find(name
);
376 s
= script_desc__new(name
);
378 goto out_delete_desc
;
385 script_desc__delete(s
);
390 static const char *ends_with(const char *str
, const char *suffix
)
392 size_t suffix_len
= strlen(suffix
);
395 if (strlen(str
) > suffix_len
) {
396 p
= str
+ strlen(str
) - suffix_len
;
397 if (!strncmp(p
, suffix
, suffix_len
))
404 static char *ltrim(char *str
)
406 int len
= strlen(str
);
408 while (len
&& isspace(*str
)) {
416 static int read_script_info(struct script_desc
*desc
, const char *filename
)
418 char line
[BUFSIZ
], *p
;
421 fp
= fopen(filename
, "r");
425 while (fgets(line
, sizeof(line
), fp
)) {
432 if (strlen(p
) && *p
== '!')
436 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
437 p
[strlen(p
) - 1] = '\0';
439 if (!strncmp(p
, "description:", strlen("description:"))) {
440 p
+= strlen("description:");
441 desc
->half_liner
= strdup(ltrim(p
));
445 if (!strncmp(p
, "args:", strlen("args:"))) {
446 p
+= strlen("args:");
447 desc
->args
= strdup(ltrim(p
));
457 static int list_available_scripts(const struct option
*opt __used
,
458 const char *s __used
, int unset __used
)
460 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
461 char scripts_path
[MAXPATHLEN
];
462 DIR *scripts_dir
, *lang_dir
;
463 char script_path
[MAXPATHLEN
];
464 char lang_path
[MAXPATHLEN
];
465 struct script_desc
*desc
;
466 char first_half
[BUFSIZ
];
470 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
472 scripts_dir
= opendir(scripts_path
);
476 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
477 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
479 lang_dir
= opendir(lang_path
);
483 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
484 script_root
= strdup(script_dirent
.d_name
);
485 str
= (char *)ends_with(script_root
, REPORT_SUFFIX
);
488 desc
= script_desc__findnew(script_root
);
489 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
490 lang_path
, script_dirent
.d_name
);
491 read_script_info(desc
, script_path
);
497 fprintf(stdout
, "List of available trace scripts:\n");
498 list_for_each_entry(desc
, &script_descs
, node
) {
499 sprintf(first_half
, "%s %s", desc
->name
,
500 desc
->args
? desc
->args
: "");
501 fprintf(stdout
, " %-36s %s\n", first_half
,
502 desc
->half_liner
? desc
->half_liner
: "");
508 static char *get_script_path(const char *script_root
, const char *suffix
)
510 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
511 char scripts_path
[MAXPATHLEN
];
512 char script_path
[MAXPATHLEN
];
513 DIR *scripts_dir
, *lang_dir
;
514 char lang_path
[MAXPATHLEN
];
515 char *str
, *__script_root
;
518 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
520 scripts_dir
= opendir(scripts_path
);
524 for_each_lang(scripts_path
, scripts_dir
, lang_dirent
, lang_next
) {
525 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
527 lang_dir
= opendir(lang_path
);
531 for_each_script(lang_path
, lang_dir
, script_dirent
, script_next
) {
532 __script_root
= strdup(script_dirent
.d_name
);
533 str
= (char *)ends_with(__script_root
, suffix
);
536 if (strcmp(__script_root
, script_root
))
538 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
539 lang_path
, script_dirent
.d_name
);
540 path
= strdup(script_path
);
551 static bool is_top_script(const char *script_path
)
553 return ends_with(script_path
, "top") == NULL
? false : true;
556 static int has_required_arg(char *script_path
)
558 struct script_desc
*desc
;
562 desc
= script_desc__new(NULL
);
564 if (read_script_info(desc
, script_path
))
570 for (p
= desc
->args
; *p
; p
++)
574 script_desc__delete(desc
);
579 static const char * const script_usage
[] = {
580 "perf script [<options>]",
581 "perf script [<options>] record <script> [<record-options>] <command>",
582 "perf script [<options>] report <script> [script-args]",
583 "perf script [<options>] <script> [<record-options>] <command>",
584 "perf script [<options>] <top-script> [script-args]",
588 static const struct option options
[] = {
589 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
590 "dump raw trace in ASCII"),
591 OPT_INCR('v', "verbose", &verbose
,
592 "be more verbose (show symbol address, etc)"),
593 OPT_BOOLEAN('L', "Latency", &latency_format
,
594 "show latency attributes (irqs/preemption disabled, etc)"),
595 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
596 list_available_scripts
),
597 OPT_CALLBACK('s', "script", NULL
, "name",
598 "script file name (lang:script name, script name, or *)",
600 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
601 "generate perf-script.xx script in specified language"),
602 OPT_STRING('i', "input", &input_name
, "file",
604 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
605 "do various checks like samples ordering and lost events"),
610 static bool have_cmd(int argc
, const char **argv
)
612 char **__argv
= malloc(sizeof(const char *) * argc
);
616 memcpy(__argv
, argv
, sizeof(const char *) * argc
);
617 argc
= parse_options(argc
, (const char **)__argv
, record_options
,
618 NULL
, PARSE_OPT_STOP_AT_NON_OPTION
);
624 int cmd_script(int argc
, const char **argv
, const char *prefix __used
)
626 char *rec_script_path
= NULL
;
627 char *rep_script_path
= NULL
;
628 struct perf_session
*session
;
629 char *script_path
= NULL
;
636 argc
= parse_options(argc
, argv
, options
, script_usage
,
637 PARSE_OPT_STOP_AT_NON_OPTION
);
639 if (argc
> 1 && !strncmp(argv
[0], "rec", strlen("rec"))) {
640 rec_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
641 if (!rec_script_path
)
642 return cmd_record(argc
, argv
, NULL
);
645 if (argc
> 1 && !strncmp(argv
[0], "rep", strlen("rep"))) {
646 rep_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
647 if (!rep_script_path
) {
649 "Please specify a valid report script"
650 "(see 'perf script -l' for listing)\n");
655 /* make sure PERF_EXEC_PATH is set for scripts */
656 perf_set_argv_exec_path(perf_exec_path());
658 if (argc
&& !script_name
&& !rec_script_path
&& !rep_script_path
) {
663 rec_script_path
= get_script_path(argv
[0], RECORD_SUFFIX
);
664 rep_script_path
= get_script_path(argv
[0], REPORT_SUFFIX
);
666 if (!rec_script_path
&& !rep_script_path
) {
667 fprintf(stderr
, " Couldn't find script %s\n\n See perf"
668 " script -l for available scripts.\n", argv
[0]);
669 usage_with_options(script_usage
, options
);
672 if (is_top_script(argv
[0])) {
677 rep_args
= has_required_arg(rep_script_path
);
678 rec_args
= (argc
- 1) - rep_args
;
680 fprintf(stderr
, " %s script requires options."
681 "\n\n See perf script -l for available "
682 "scripts and options.\n", argv
[0]);
683 usage_with_options(script_usage
, options
);
687 if (pipe(live_pipe
) < 0) {
688 perror("failed to create pipe");
694 perror("failed to fork");
702 dup2(live_pipe
[1], 1);
705 if (!is_top_script(argv
[0]))
706 system_wide
= !have_cmd(argc
- rep_args
,
709 __argv
= malloc((argc
+ 6) * sizeof(const char *));
713 __argv
[j
++] = "/bin/sh";
714 __argv
[j
++] = rec_script_path
;
720 for (i
= rep_args
+ 1; i
< argc
; i
++)
721 __argv
[j
++] = argv
[i
];
724 execvp("/bin/sh", (char **)__argv
);
729 dup2(live_pipe
[0], 0);
732 __argv
= malloc((argc
+ 4) * sizeof(const char *));
736 __argv
[j
++] = "/bin/sh";
737 __argv
[j
++] = rep_script_path
;
738 for (i
= 1; i
< rep_args
+ 1; i
++)
739 __argv
[j
++] = argv
[i
];
744 execvp("/bin/sh", (char **)__argv
);
750 script_path
= rec_script_path
;
752 script_path
= rep_script_path
;
759 system_wide
= !have_cmd(argc
- 1, &argv
[1]);
761 __argv
= malloc((argc
+ 2) * sizeof(const char *));
764 __argv
[j
++] = "/bin/sh";
765 __argv
[j
++] = script_path
;
768 for (i
= 2; i
< argc
; i
++)
769 __argv
[j
++] = argv
[i
];
772 execvp("/bin/sh", (char **)__argv
);
777 if (symbol__init() < 0)
782 session
= perf_session__new(input_name
, O_RDONLY
, 0, false);
786 if (strcmp(input_name
, "-") &&
787 !perf_session__has_traces(session
, "record -R"))
790 if (generate_script_lang
) {
791 struct stat perf_stat
;
793 int input
= open(input_name
, O_RDONLY
);
795 perror("failed to open file");
799 err
= fstat(input
, &perf_stat
);
801 perror("failed to stat file");
805 if (!perf_stat
.st_size
) {
806 fprintf(stderr
, "zero-sized file, nothing to do!\n");
810 scripting_ops
= script_spec__lookup(generate_script_lang
);
811 if (!scripting_ops
) {
812 fprintf(stderr
, "invalid language specifier");
816 err
= scripting_ops
->generate_script("perf-script");
821 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
824 pr_debug("perf script started with script %s\n\n", script_name
);
827 err
= __cmd_script(session
);
829 perf_session__delete(session
);