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(union perf_event *event,
67 struct perf_sample *sample,
68 struct perf_session *session)
70 struct thread *thread = perf_session__findnew(session, event->ip.pid);
73 pr_debug("problem processing %d event, skipping it.\n",
78 if (session->sample_type & PERF_SAMPLE_RAW) {
80 if (sample->time < last_timestamp) {
81 pr_err("Samples misordered, previous: %" PRIu64
82 " this: %" PRIu64 "\n", last_timestamp,
86 last_timestamp = sample->time;
90 * FIXME: better resolve from pid from the struct trace_entry
91 * field, although it should be the same than this perf
94 scripting_ops->process_event(sample->cpu, sample->raw_data,
96 sample->time, thread->comm);
99 session->hists.stats.total_period += sample->period;
103 static struct perf_event_ops event_ops = {
104 .sample = process_sample_event,
105 .comm = perf_event__process_comm,
106 .attr = perf_event__process_attr,
107 .event_type = perf_event__process_event_type,
108 .tracing_data = perf_event__process_tracing_data,
109 .build_id = perf_event__process_build_id,
110 .ordered_samples = true,
111 .ordering_requires_timestamps = true,
114 extern volatile int session_done;
116 static void sig_handler(int sig __unused)
121 static int __cmd_script(struct perf_session *session)
125 signal(SIGINT, sig_handler);
127 ret = perf_session__process_events(session, &event_ops);
130 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
136 struct list_head node;
137 struct scripting_ops *ops;
141 static LIST_HEAD(script_specs);
143 static struct script_spec *script_spec__new(const char *spec,
144 struct scripting_ops *ops)
146 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
149 strcpy(s->spec, spec);
156 static void script_spec__delete(struct script_spec *s)
162 static void script_spec__add(struct script_spec *s)
164 list_add_tail(&s->node, &script_specs);
167 static struct script_spec *script_spec__find(const char *spec)
169 struct script_spec *s;
171 list_for_each_entry(s, &script_specs, node)
172 if (strcasecmp(s->spec, spec) == 0)
177 static struct script_spec *script_spec__findnew(const char *spec,
178 struct scripting_ops *ops)
180 struct script_spec *s = script_spec__find(spec);
185 s = script_spec__new(spec, ops);
187 goto out_delete_spec;
194 script_spec__delete(s);
199 int script_spec_register(const char *spec, struct scripting_ops *ops)
201 struct script_spec *s;
203 s = script_spec__find(spec);
207 s = script_spec__findnew(spec, ops);
214 static struct scripting_ops *script_spec__lookup(const char *spec)
216 struct script_spec *s = script_spec__find(spec);
223 static void list_available_languages(void)
225 struct script_spec *s;
227 fprintf(stderr, "\n");
228 fprintf(stderr, "Scripting language extensions (used in "
229 "perf script -s [spec:]script.[spec]):\n\n");
231 list_for_each_entry(s, &script_specs, node)
232 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
234 fprintf(stderr, "\n");
237 static int parse_scriptname(const struct option *opt __used,
238 const char *str, int unset __used)
241 const char *script, *ext;
244 if (strcmp(str, "lang") == 0) {
245 list_available_languages();
249 script = strchr(str, ':');
252 if (len >= PATH_MAX) {
253 fprintf(stderr, "invalid language specifier");
256 strncpy(spec, str, len);
258 scripting_ops = script_spec__lookup(spec);
259 if (!scripting_ops) {
260 fprintf(stderr, "invalid language specifier");
266 ext = strrchr(script, '.');
268 fprintf(stderr, "invalid script extension");
271 scripting_ops = script_spec__lookup(++ext);
272 if (!scripting_ops) {
273 fprintf(stderr, "invalid script extension");
278 script_name = strdup(script);
283 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
284 static int is_directory(const char *base_path, const struct dirent *dent)
289 sprintf(path, "%s/%s", base_path, dent->d_name);
293 return S_ISDIR(st.st_mode);
296 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
297 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
299 if ((lang_dirent.d_type == DT_DIR || \
300 (lang_dirent.d_type == DT_UNKNOWN && \
301 is_directory(scripts_path, &lang_dirent))) && \
302 (strcmp(lang_dirent.d_name, ".")) && \
303 (strcmp(lang_dirent.d_name, "..")))
305 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
306 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
308 if (script_dirent.d_type != DT_DIR && \
309 (script_dirent.d_type != DT_UNKNOWN || \
310 !is_directory(lang_path, &script_dirent)))
313 #define RECORD_SUFFIX "-record"
314 #define REPORT_SUFFIX "-report"
317 struct list_head node;
323 static LIST_HEAD(script_descs);
325 static struct script_desc *script_desc__new(const char *name)
327 struct script_desc *s = zalloc(sizeof(*s));
329 if (s != NULL && name)
330 s->name = strdup(name);
335 static void script_desc__delete(struct script_desc *s)
343 static void script_desc__add(struct script_desc *s)
345 list_add_tail(&s->node, &script_descs);
348 static struct script_desc *script_desc__find(const char *name)
350 struct script_desc *s;
352 list_for_each_entry(s, &script_descs, node)
353 if (strcasecmp(s->name, name) == 0)
358 static struct script_desc *script_desc__findnew(const char *name)
360 struct script_desc *s = script_desc__find(name);
365 s = script_desc__new(name);
367 goto out_delete_desc;
374 script_desc__delete(s);
379 static const char *ends_with(const char *str, const char *suffix)
381 size_t suffix_len = strlen(suffix);
384 if (strlen(str) > suffix_len) {
385 p = str + strlen(str) - suffix_len;
386 if (!strncmp(p, suffix, suffix_len))
393 static char *ltrim(char *str)
395 int len = strlen(str);
397 while (len && isspace(*str)) {
405 static int read_script_info(struct script_desc *desc, const char *filename)
407 char line[BUFSIZ], *p;
410 fp = fopen(filename, "r");
414 while (fgets(line, sizeof(line), fp)) {
421 if (strlen(p) && *p == '!')
425 if (strlen(p) && p[strlen(p) - 1] == '\n')
426 p[strlen(p) - 1] = '\0';
428 if (!strncmp(p, "description:", strlen("description:"))) {
429 p += strlen("description:");
430 desc->half_liner = strdup(ltrim(p));
434 if (!strncmp(p, "args:", strlen("args:"))) {
435 p += strlen("args:");
436 desc->args = strdup(ltrim(p));
446 static int list_available_scripts(const struct option *opt __used,
447 const char *s __used, int unset __used)
449 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
450 char scripts_path[MAXPATHLEN];
451 DIR *scripts_dir, *lang_dir;
452 char script_path[MAXPATHLEN];
453 char lang_path[MAXPATHLEN];
454 struct script_desc *desc;
455 char first_half[BUFSIZ];
459 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
461 scripts_dir = opendir(scripts_path);
465 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
466 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
468 lang_dir = opendir(lang_path);
472 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
473 script_root = strdup(script_dirent.d_name);
474 str = (char *)ends_with(script_root, REPORT_SUFFIX);
477 desc = script_desc__findnew(script_root);
478 snprintf(script_path, MAXPATHLEN, "%s/%s",
479 lang_path, script_dirent.d_name);
480 read_script_info(desc, script_path);
486 fprintf(stdout, "List of available trace scripts:\n");
487 list_for_each_entry(desc, &script_descs, node) {
488 sprintf(first_half, "%s %s", desc->name,
489 desc->args ? desc->args : "");
490 fprintf(stdout, " %-36s %s\n", first_half,
491 desc->half_liner ? desc->half_liner : "");
497 static char *get_script_path(const char *script_root, const char *suffix)
499 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
500 char scripts_path[MAXPATHLEN];
501 char script_path[MAXPATHLEN];
502 DIR *scripts_dir, *lang_dir;
503 char lang_path[MAXPATHLEN];
504 char *str, *__script_root;
507 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
509 scripts_dir = opendir(scripts_path);
513 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
514 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
516 lang_dir = opendir(lang_path);
520 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
521 __script_root = strdup(script_dirent.d_name);
522 str = (char *)ends_with(__script_root, suffix);
525 if (strcmp(__script_root, script_root))
527 snprintf(script_path, MAXPATHLEN, "%s/%s",
528 lang_path, script_dirent.d_name);
529 path = strdup(script_path);
540 static bool is_top_script(const char *script_path)
542 return ends_with(script_path, "top") == NULL ? false : true;
545 static int has_required_arg(char *script_path)
547 struct script_desc *desc;
551 desc = script_desc__new(NULL);
553 if (read_script_info(desc, script_path))
559 for (p = desc->args; *p; p++)
563 script_desc__delete(desc);
568 static const char * const script_usage[] = {
569 "perf script [<options>]",
570 "perf script [<options>] record <script> [<record-options>] <command>",
571 "perf script [<options>] report <script> [script-args]",
572 "perf script [<options>] <script> [<record-options>] <command>",
573 "perf script [<options>] <top-script> [script-args]",
577 static const struct option options[] = {
578 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
579 "dump raw trace in ASCII"),
580 OPT_INCR('v', "verbose", &verbose,
581 "be more verbose (show symbol address, etc)"),
582 OPT_BOOLEAN('L', "Latency", &latency_format,
583 "show latency attributes (irqs/preemption disabled, etc)"),
584 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
585 list_available_scripts),
586 OPT_CALLBACK('s', "script", NULL, "name",
587 "script file name (lang:script name, script name, or *)",
589 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
590 "generate perf-script.xx script in specified language"),
591 OPT_STRING('i', "input", &input_name, "file",
593 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
594 "do various checks like samples ordering and lost events"),
599 static bool have_cmd(int argc, const char **argv)
601 char **__argv = malloc(sizeof(const char *) * argc);
605 memcpy(__argv, argv, sizeof(const char *) * argc);
606 argc = parse_options(argc, (const char **)__argv, record_options,
607 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
613 int cmd_script(int argc, const char **argv, const char *prefix __used)
615 char *rec_script_path = NULL;
616 char *rep_script_path = NULL;
617 struct perf_session *session;
618 char *script_path = NULL;
625 argc = parse_options(argc, argv, options, script_usage,
626 PARSE_OPT_STOP_AT_NON_OPTION);
628 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
629 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
630 if (!rec_script_path)
631 return cmd_record(argc, argv, NULL);
634 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
635 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
636 if (!rep_script_path) {
638 "Please specify a valid report script"
639 "(see 'perf script -l' for listing)\n");
644 /* make sure PERF_EXEC_PATH is set for scripts */
645 perf_set_argv_exec_path(perf_exec_path());
647 if (argc && !script_name && !rec_script_path && !rep_script_path) {
652 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
653 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
655 if (!rec_script_path && !rep_script_path) {
656 fprintf(stderr, " Couldn't find script %s\n\n See perf"
657 " script -l for available scripts.\n", argv[0]);
658 usage_with_options(script_usage, options);
661 if (is_top_script(argv[0])) {
666 rep_args = has_required_arg(rep_script_path);
667 rec_args = (argc - 1) - rep_args;
669 fprintf(stderr, " %s script requires options."
670 "\n\n See perf script -l for available "
671 "scripts and options.\n", argv[0]);
672 usage_with_options(script_usage, options);
676 if (pipe(live_pipe) < 0) {
677 perror("failed to create pipe");
683 perror("failed to fork");
691 dup2(live_pipe[1], 1);
694 if (!is_top_script(argv[0]))
695 system_wide = !have_cmd(argc - rep_args,
698 __argv = malloc((argc + 6) * sizeof(const char *));
702 __argv[j++] = "/bin/sh";
703 __argv[j++] = rec_script_path;
709 for (i = rep_args + 1; i < argc; i++)
710 __argv[j++] = argv[i];
713 execvp("/bin/sh", (char **)__argv);
718 dup2(live_pipe[0], 0);
721 __argv = malloc((argc + 4) * sizeof(const char *));
725 __argv[j++] = "/bin/sh";
726 __argv[j++] = rep_script_path;
727 for (i = 1; i < rep_args + 1; i++)
728 __argv[j++] = argv[i];
733 execvp("/bin/sh", (char **)__argv);
739 script_path = rec_script_path;
741 script_path = rep_script_path;
748 system_wide = !have_cmd(argc - 1, &argv[1]);
750 __argv = malloc((argc + 2) * sizeof(const char *));
753 __argv[j++] = "/bin/sh";
754 __argv[j++] = script_path;
757 for (i = 2; i < argc; i++)
758 __argv[j++] = argv[i];
761 execvp("/bin/sh", (char **)__argv);
766 if (symbol__init() < 0)
771 session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
775 if (strcmp(input_name, "-") &&
776 !perf_session__has_traces(session, "record -R"))
779 if (generate_script_lang) {
780 struct stat perf_stat;
782 int input = open(input_name, O_RDONLY);
784 perror("failed to open file");
788 err = fstat(input, &perf_stat);
790 perror("failed to stat file");
794 if (!perf_stat.st_size) {
795 fprintf(stderr, "zero-sized file, nothing to do!\n");
799 scripting_ops = script_spec__lookup(generate_script_lang);
800 if (!scripting_ops) {
801 fprintf(stderr, "invalid language specifier");
805 err = scripting_ops->generate_script("perf-script");
810 err = scripting_ops->start_script(script_name, argc, argv);
813 pr_debug("perf script started with script %s\n\n", script_name);
816 err = __cmd_script(session);
818 perf_session__delete(session);