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 perf_session *session)
68 struct sample_data data;
69 struct thread *thread;
71 memset(&data, 0, sizeof(data));
76 event__parse_sample(event, session->sample_type, &data);
78 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
79 data.pid, data.tid, data.ip, data.period);
81 thread = perf_session__findnew(session, event->ip.pid);
83 pr_debug("problem processing %d event, skipping it.\n",
88 if (session->sample_type & PERF_SAMPLE_RAW) {
90 if (data.time < last_timestamp) {
91 pr_err("Samples misordered, previous: %llu "
92 "this: %llu\n", last_timestamp,
96 last_timestamp = data.time;
100 * FIXME: better resolve from pid from the struct trace_entry
101 * field, although it should be the same than this perf
104 scripting_ops->process_event(data.cpu, data.raw_data,
106 data.time, thread->comm);
109 session->hists.stats.total_period += data.period;
115 static int process_lost_event(event_t *event, struct perf_session *session __used)
117 nr_lost += event->lost.lost;
122 static struct perf_event_ops event_ops = {
123 .sample = process_sample_event,
124 .comm = event__process_comm,
125 .attr = event__process_attr,
126 .event_type = event__process_event_type,
127 .tracing_data = event__process_tracing_data,
128 .build_id = event__process_build_id,
129 .lost = process_lost_event,
130 .ordered_samples = true,
133 extern volatile int session_done;
135 static void sig_handler(int sig __unused)
140 static int __cmd_script(struct perf_session *session)
144 signal(SIGINT, sig_handler);
146 ret = perf_session__process_events(session, &event_ops);
149 pr_err("Misordered timestamps: %llu\n", nr_unordered);
150 pr_err("Lost events: %llu\n", nr_lost);
157 struct list_head node;
158 struct scripting_ops *ops;
162 LIST_HEAD(script_specs);
164 static struct script_spec *script_spec__new(const char *spec,
165 struct scripting_ops *ops)
167 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
170 strcpy(s->spec, spec);
177 static void script_spec__delete(struct script_spec *s)
183 static void script_spec__add(struct script_spec *s)
185 list_add_tail(&s->node, &script_specs);
188 static struct script_spec *script_spec__find(const char *spec)
190 struct script_spec *s;
192 list_for_each_entry(s, &script_specs, node)
193 if (strcasecmp(s->spec, spec) == 0)
198 static struct script_spec *script_spec__findnew(const char *spec,
199 struct scripting_ops *ops)
201 struct script_spec *s = script_spec__find(spec);
206 s = script_spec__new(spec, ops);
208 goto out_delete_spec;
215 script_spec__delete(s);
220 int script_spec_register(const char *spec, struct scripting_ops *ops)
222 struct script_spec *s;
224 s = script_spec__find(spec);
228 s = script_spec__findnew(spec, ops);
235 static struct scripting_ops *script_spec__lookup(const char *spec)
237 struct script_spec *s = script_spec__find(spec);
244 static void list_available_languages(void)
246 struct script_spec *s;
248 fprintf(stderr, "\n");
249 fprintf(stderr, "Scripting language extensions (used in "
250 "perf script -s [spec:]script.[spec]):\n\n");
252 list_for_each_entry(s, &script_specs, node)
253 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
255 fprintf(stderr, "\n");
258 static int parse_scriptname(const struct option *opt __used,
259 const char *str, int unset __used)
262 const char *script, *ext;
265 if (strcmp(str, "lang") == 0) {
266 list_available_languages();
270 script = strchr(str, ':');
273 if (len >= PATH_MAX) {
274 fprintf(stderr, "invalid language specifier");
277 strncpy(spec, str, len);
279 scripting_ops = script_spec__lookup(spec);
280 if (!scripting_ops) {
281 fprintf(stderr, "invalid language specifier");
287 ext = strrchr(script, '.');
289 fprintf(stderr, "invalid script extension");
292 scripting_ops = script_spec__lookup(++ext);
293 if (!scripting_ops) {
294 fprintf(stderr, "invalid script extension");
299 script_name = strdup(script);
304 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
305 static int is_directory(const char *base_path, const struct dirent *dent)
310 sprintf(path, "%s/%s", base_path, dent->d_name);
314 return S_ISDIR(st.st_mode);
317 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
318 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
320 if ((lang_dirent.d_type == DT_DIR || \
321 (lang_dirent.d_type == DT_UNKNOWN && \
322 is_directory(scripts_path, &lang_dirent))) && \
323 (strcmp(lang_dirent.d_name, ".")) && \
324 (strcmp(lang_dirent.d_name, "..")))
326 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
327 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
329 if (script_dirent.d_type != DT_DIR && \
330 (script_dirent.d_type != DT_UNKNOWN || \
331 !is_directory(lang_path, &script_dirent)))
334 #define RECORD_SUFFIX "-record"
335 #define REPORT_SUFFIX "-report"
338 struct list_head node;
344 LIST_HEAD(script_descs);
346 static struct script_desc *script_desc__new(const char *name)
348 struct script_desc *s = zalloc(sizeof(*s));
350 if (s != NULL && name)
351 s->name = strdup(name);
356 static void script_desc__delete(struct script_desc *s)
364 static void script_desc__add(struct script_desc *s)
366 list_add_tail(&s->node, &script_descs);
369 static struct script_desc *script_desc__find(const char *name)
371 struct script_desc *s;
373 list_for_each_entry(s, &script_descs, node)
374 if (strcasecmp(s->name, name) == 0)
379 static struct script_desc *script_desc__findnew(const char *name)
381 struct script_desc *s = script_desc__find(name);
386 s = script_desc__new(name);
388 goto out_delete_desc;
395 script_desc__delete(s);
400 static char *ends_with(char *str, const char *suffix)
402 size_t suffix_len = strlen(suffix);
405 if (strlen(str) > suffix_len) {
406 p = str + strlen(str) - suffix_len;
407 if (!strncmp(p, suffix, suffix_len))
414 static char *ltrim(char *str)
416 int len = strlen(str);
418 while (len && isspace(*str)) {
426 static int read_script_info(struct script_desc *desc, const char *filename)
428 char line[BUFSIZ], *p;
431 fp = fopen(filename, "r");
435 while (fgets(line, sizeof(line), fp)) {
442 if (strlen(p) && *p == '!')
446 if (strlen(p) && p[strlen(p) - 1] == '\n')
447 p[strlen(p) - 1] = '\0';
449 if (!strncmp(p, "description:", strlen("description:"))) {
450 p += strlen("description:");
451 desc->half_liner = strdup(ltrim(p));
455 if (!strncmp(p, "args:", strlen("args:"))) {
456 p += strlen("args:");
457 desc->args = strdup(ltrim(p));
467 static int list_available_scripts(const struct option *opt __used,
468 const char *s __used, int unset __used)
470 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
471 char scripts_path[MAXPATHLEN];
472 DIR *scripts_dir, *lang_dir;
473 char script_path[MAXPATHLEN];
474 char lang_path[MAXPATHLEN];
475 struct script_desc *desc;
476 char first_half[BUFSIZ];
480 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
482 scripts_dir = opendir(scripts_path);
486 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
487 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
489 lang_dir = opendir(lang_path);
493 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
494 script_root = strdup(script_dirent.d_name);
495 str = ends_with(script_root, REPORT_SUFFIX);
498 desc = script_desc__findnew(script_root);
499 snprintf(script_path, MAXPATHLEN, "%s/%s",
500 lang_path, script_dirent.d_name);
501 read_script_info(desc, script_path);
507 fprintf(stdout, "List of available trace scripts:\n");
508 list_for_each_entry(desc, &script_descs, node) {
509 sprintf(first_half, "%s %s", desc->name,
510 desc->args ? desc->args : "");
511 fprintf(stdout, " %-36s %s\n", first_half,
512 desc->half_liner ? desc->half_liner : "");
518 static char *get_script_path(const char *script_root, const char *suffix)
520 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
521 char scripts_path[MAXPATHLEN];
522 char script_path[MAXPATHLEN];
523 DIR *scripts_dir, *lang_dir;
524 char lang_path[MAXPATHLEN];
525 char *str, *__script_root;
528 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
530 scripts_dir = opendir(scripts_path);
534 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
535 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
537 lang_dir = opendir(lang_path);
541 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
542 __script_root = strdup(script_dirent.d_name);
543 str = ends_with(__script_root, suffix);
546 if (strcmp(__script_root, script_root))
548 snprintf(script_path, MAXPATHLEN, "%s/%s",
549 lang_path, script_dirent.d_name);
550 path = strdup(script_path);
561 static bool is_top_script(const char *script_path)
563 return ends_with((char *)script_path, "top") == NULL ? false : true;
566 static int has_required_arg(char *script_path)
568 struct script_desc *desc;
572 desc = script_desc__new(NULL);
574 if (read_script_info(desc, script_path))
580 for (p = desc->args; *p; p++)
584 script_desc__delete(desc);
589 static const char * const script_usage[] = {
590 "perf script [<options>]",
591 "perf script [<options>] record <script> [<record-options>] <command>",
592 "perf script [<options>] report <script> [script-args]",
593 "perf script [<options>] <script> [<record-options>] <command>",
594 "perf script [<options>] <top-script> [script-args]",
598 static const struct option options[] = {
599 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
600 "dump raw trace in ASCII"),
601 OPT_INCR('v', "verbose", &verbose,
602 "be more verbose (show symbol address, etc)"),
603 OPT_BOOLEAN('L', "Latency", &latency_format,
604 "show latency attributes (irqs/preemption disabled, etc)"),
605 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
606 list_available_scripts),
607 OPT_CALLBACK('s', "script", NULL, "name",
608 "script file name (lang:script name, script name, or *)",
610 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
611 "generate perf-script.xx script in specified language"),
612 OPT_STRING('i', "input", &input_name, "file",
614 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
615 "do various checks like samples ordering and lost events"),
620 static bool have_cmd(int argc, const char **argv)
622 char **__argv = malloc(sizeof(const char *) * argc);
626 memcpy(__argv, argv, sizeof(const char *) * argc);
627 argc = parse_options(argc, (const char **)__argv, record_options,
628 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
634 int cmd_script(int argc, const char **argv, const char *prefix __used)
636 char *rec_script_path = NULL;
637 char *rep_script_path = NULL;
638 struct perf_session *session;
639 char *script_path = NULL;
646 argc = parse_options(argc, argv, options, script_usage,
647 PARSE_OPT_STOP_AT_NON_OPTION);
649 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
650 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
651 if (!rec_script_path)
652 return cmd_record(argc, argv, NULL);
655 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
656 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
657 if (!rep_script_path) {
659 "Please specify a valid report script"
660 "(see 'perf script -l' for listing)\n");
665 /* make sure PERF_EXEC_PATH is set for scripts */
666 perf_set_argv_exec_path(perf_exec_path());
668 if (argc && !script_name && !rec_script_path && !rep_script_path) {
673 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
674 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
676 if (!rec_script_path && !rep_script_path) {
677 fprintf(stderr, " Couldn't find script %s\n\n See perf"
678 " script -l for available scripts.\n", argv[0]);
679 usage_with_options(script_usage, options);
682 if (is_top_script(argv[0])) {
687 rep_args = has_required_arg(rep_script_path);
688 rec_args = (argc - 1) - rep_args;
690 fprintf(stderr, " %s script requires options."
691 "\n\n See perf script -l for available "
692 "scripts and options.\n", argv[0]);
693 usage_with_options(script_usage, options);
697 if (pipe(live_pipe) < 0) {
698 perror("failed to create pipe");
704 perror("failed to fork");
712 dup2(live_pipe[1], 1);
715 if (!is_top_script(argv[0]))
716 system_wide = !have_cmd(argc - rep_args,
719 __argv = malloc((argc + 6) * sizeof(const char *));
723 __argv[j++] = "/bin/sh";
724 __argv[j++] = rec_script_path;
730 for (i = rep_args + 1; i < argc; i++)
731 __argv[j++] = argv[i];
734 execvp("/bin/sh", (char **)__argv);
739 dup2(live_pipe[0], 0);
742 __argv = malloc((argc + 4) * sizeof(const char *));
746 __argv[j++] = "/bin/sh";
747 __argv[j++] = rep_script_path;
748 for (i = 1; i < rep_args + 1; i++)
749 __argv[j++] = argv[i];
754 execvp("/bin/sh", (char **)__argv);
760 script_path = rec_script_path;
762 script_path = rep_script_path;
769 system_wide = !have_cmd(argc - 1, &argv[1]);
771 __argv = malloc((argc + 2) * sizeof(const char *));
774 __argv[j++] = "/bin/sh";
775 __argv[j++] = script_path;
778 for (i = 2; i < argc; i++)
779 __argv[j++] = argv[i];
782 execvp("/bin/sh", (char **)__argv);
787 if (symbol__init() < 0)
792 session = perf_session__new(input_name, O_RDONLY, 0, false);
796 if (strcmp(input_name, "-") &&
797 !perf_session__has_traces(session, "record -R"))
800 if (generate_script_lang) {
801 struct stat perf_stat;
803 int input = open(input_name, O_RDONLY);
805 perror("failed to open file");
809 err = fstat(input, &perf_stat);
811 perror("failed to stat file");
815 if (!perf_stat.st_size) {
816 fprintf(stderr, "zero-sized file, nothing to do!\n");
820 scripting_ops = script_spec__lookup(generate_script_lang);
821 if (!scripting_ops) {
822 fprintf(stderr, "invalid language specifier");
826 err = scripting_ops->generate_script("perf-script");
831 err = scripting_ops->start_script(script_name, argc, argv);
834 pr_debug("perf script started with script %s\n\n", script_name);
837 err = __cmd_script(session);
839 perf_session__delete(session);