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 enum perf_output_field {
24 PERF_OUTPUT_COMM = 1U << 0,
25 PERF_OUTPUT_TID = 1U << 1,
26 PERF_OUTPUT_PID = 1U << 2,
27 PERF_OUTPUT_TIME = 1U << 3,
28 PERF_OUTPUT_CPU = 1U << 4,
29 PERF_OUTPUT_EVNAME = 1U << 5,
30 PERF_OUTPUT_TRACE = 1U << 6,
33 struct output_option {
35 enum perf_output_field field;
36 } all_output_options[] = {
37 {.str = "comm", .field = PERF_OUTPUT_COMM},
38 {.str = "tid", .field = PERF_OUTPUT_TID},
39 {.str = "pid", .field = PERF_OUTPUT_PID},
40 {.str = "time", .field = PERF_OUTPUT_TIME},
41 {.str = "cpu", .field = PERF_OUTPUT_CPU},
42 {.str = "event", .field = PERF_OUTPUT_EVNAME},
43 {.str = "trace", .field = PERF_OUTPUT_TRACE},
46 /* default set to maintain compatibility with current format */
47 static u64 output_fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | \
48 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | \
49 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE;
51 static bool output_set_by_user;
53 #define PRINT_FIELD(x) (output_fields & PERF_OUTPUT_##x)
55 static void print_sample_start(struct perf_sample *sample,
56 struct thread *thread)
60 const char *evname = NULL;
63 unsigned long long nsecs;
65 if (PRINT_FIELD(COMM)) {
67 printf("%8.8s ", thread->comm);
69 printf("%16s ", thread->comm);
72 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
73 printf("%5d/%-5d ", sample->pid, sample->tid);
74 else if (PRINT_FIELD(PID))
75 printf("%5d ", sample->pid);
76 else if (PRINT_FIELD(TID))
77 printf("%5d ", sample->tid);
79 if (PRINT_FIELD(CPU)) {
81 printf("%3d ", sample->cpu);
83 printf("[%03d] ", sample->cpu);
86 if (PRINT_FIELD(TIME)) {
88 secs = nsecs / NSECS_PER_SEC;
89 nsecs -= secs * NSECS_PER_SEC;
90 usecs = nsecs / NSECS_PER_USEC;
91 printf("%5lu.%06lu: ", secs, usecs);
94 if (PRINT_FIELD(EVNAME)) {
95 type = trace_parse_common_type(sample->raw_data);
96 event = trace_find_event(type);
100 printf("%s: ", evname ? evname : "(unknown)");
104 static void process_event(union perf_event *event __unused,
105 struct perf_sample *sample,
106 struct perf_session *session __unused,
107 struct thread *thread)
109 print_sample_start(sample, thread);
111 if (PRINT_FIELD(TRACE))
112 print_trace_event(sample->cpu, sample->raw_data,
118 static int default_start_script(const char *script __unused,
120 const char **argv __unused)
125 static int default_stop_script(void)
130 static int default_generate_script(const char *outfile __unused)
135 static struct scripting_ops default_scripting_ops = {
136 .start_script = default_start_script,
137 .stop_script = default_stop_script,
138 .process_event = process_event,
139 .generate_script = default_generate_script,
142 static struct scripting_ops *scripting_ops;
144 static void setup_scripting(void)
146 setup_perl_scripting();
147 setup_python_scripting();
149 scripting_ops = &default_scripting_ops;
152 static int cleanup_scripting(void)
154 pr_debug("\nperf script stopped\n");
156 return scripting_ops->stop_script();
159 static char const *input_name = "perf.data";
161 static int process_sample_event(union perf_event *event,
162 struct perf_sample *sample,
163 struct perf_session *session)
165 struct thread *thread = perf_session__findnew(session, event->ip.pid);
167 if (thread == NULL) {
168 pr_debug("problem processing %d event, skipping it.\n",
173 if (session->sample_type & PERF_SAMPLE_RAW) {
175 if (sample->time < last_timestamp) {
176 pr_err("Samples misordered, previous: %" PRIu64
177 " this: %" PRIu64 "\n", last_timestamp,
181 last_timestamp = sample->time;
184 scripting_ops->process_event(event, sample, session, thread);
187 session->hists.stats.total_period += sample->period;
191 static struct perf_event_ops event_ops = {
192 .sample = process_sample_event,
193 .comm = perf_event__process_comm,
194 .attr = perf_event__process_attr,
195 .event_type = perf_event__process_event_type,
196 .tracing_data = perf_event__process_tracing_data,
197 .build_id = perf_event__process_build_id,
198 .ordered_samples = true,
199 .ordering_requires_timestamps = true,
202 extern volatile int session_done;
204 static void sig_handler(int sig __unused)
209 static int __cmd_script(struct perf_session *session)
213 signal(SIGINT, sig_handler);
215 ret = perf_session__process_events(session, &event_ops);
218 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
224 struct list_head node;
225 struct scripting_ops *ops;
229 static LIST_HEAD(script_specs);
231 static struct script_spec *script_spec__new(const char *spec,
232 struct scripting_ops *ops)
234 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
237 strcpy(s->spec, spec);
244 static void script_spec__delete(struct script_spec *s)
250 static void script_spec__add(struct script_spec *s)
252 list_add_tail(&s->node, &script_specs);
255 static struct script_spec *script_spec__find(const char *spec)
257 struct script_spec *s;
259 list_for_each_entry(s, &script_specs, node)
260 if (strcasecmp(s->spec, spec) == 0)
265 static struct script_spec *script_spec__findnew(const char *spec,
266 struct scripting_ops *ops)
268 struct script_spec *s = script_spec__find(spec);
273 s = script_spec__new(spec, ops);
275 goto out_delete_spec;
282 script_spec__delete(s);
287 int script_spec_register(const char *spec, struct scripting_ops *ops)
289 struct script_spec *s;
291 s = script_spec__find(spec);
295 s = script_spec__findnew(spec, ops);
302 static struct scripting_ops *script_spec__lookup(const char *spec)
304 struct script_spec *s = script_spec__find(spec);
311 static void list_available_languages(void)
313 struct script_spec *s;
315 fprintf(stderr, "\n");
316 fprintf(stderr, "Scripting language extensions (used in "
317 "perf script -s [spec:]script.[spec]):\n\n");
319 list_for_each_entry(s, &script_specs, node)
320 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
322 fprintf(stderr, "\n");
325 static int parse_scriptname(const struct option *opt __used,
326 const char *str, int unset __used)
329 const char *script, *ext;
332 if (strcmp(str, "lang") == 0) {
333 list_available_languages();
337 script = strchr(str, ':');
340 if (len >= PATH_MAX) {
341 fprintf(stderr, "invalid language specifier");
344 strncpy(spec, str, len);
346 scripting_ops = script_spec__lookup(spec);
347 if (!scripting_ops) {
348 fprintf(stderr, "invalid language specifier");
354 ext = strrchr(script, '.');
356 fprintf(stderr, "invalid script extension");
359 scripting_ops = script_spec__lookup(++ext);
360 if (!scripting_ops) {
361 fprintf(stderr, "invalid script extension");
366 script_name = strdup(script);
371 static int parse_output_fields(const struct option *opt __used,
372 const char *arg, int unset __used)
375 int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
377 char *str = strdup(arg);
382 tok = strtok(str, ",");
384 fprintf(stderr, "Invalid field string.");
390 for (i = 0; i < imax; ++i) {
391 if (strcmp(tok, all_output_options[i].str) == 0) {
392 output_fields |= all_output_options[i].field;
397 fprintf(stderr, "Invalid field requested.");
402 tok = strtok(NULL, ",");
407 output_set_by_user = true;
413 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
414 static int is_directory(const char *base_path, const struct dirent *dent)
419 sprintf(path, "%s/%s", base_path, dent->d_name);
423 return S_ISDIR(st.st_mode);
426 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
427 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
429 if ((lang_dirent.d_type == DT_DIR || \
430 (lang_dirent.d_type == DT_UNKNOWN && \
431 is_directory(scripts_path, &lang_dirent))) && \
432 (strcmp(lang_dirent.d_name, ".")) && \
433 (strcmp(lang_dirent.d_name, "..")))
435 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
436 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
438 if (script_dirent.d_type != DT_DIR && \
439 (script_dirent.d_type != DT_UNKNOWN || \
440 !is_directory(lang_path, &script_dirent)))
443 #define RECORD_SUFFIX "-record"
444 #define REPORT_SUFFIX "-report"
447 struct list_head node;
453 static LIST_HEAD(script_descs);
455 static struct script_desc *script_desc__new(const char *name)
457 struct script_desc *s = zalloc(sizeof(*s));
459 if (s != NULL && name)
460 s->name = strdup(name);
465 static void script_desc__delete(struct script_desc *s)
473 static void script_desc__add(struct script_desc *s)
475 list_add_tail(&s->node, &script_descs);
478 static struct script_desc *script_desc__find(const char *name)
480 struct script_desc *s;
482 list_for_each_entry(s, &script_descs, node)
483 if (strcasecmp(s->name, name) == 0)
488 static struct script_desc *script_desc__findnew(const char *name)
490 struct script_desc *s = script_desc__find(name);
495 s = script_desc__new(name);
497 goto out_delete_desc;
504 script_desc__delete(s);
509 static const char *ends_with(const char *str, const char *suffix)
511 size_t suffix_len = strlen(suffix);
514 if (strlen(str) > suffix_len) {
515 p = str + strlen(str) - suffix_len;
516 if (!strncmp(p, suffix, suffix_len))
523 static char *ltrim(char *str)
525 int len = strlen(str);
527 while (len && isspace(*str)) {
535 static int read_script_info(struct script_desc *desc, const char *filename)
537 char line[BUFSIZ], *p;
540 fp = fopen(filename, "r");
544 while (fgets(line, sizeof(line), fp)) {
551 if (strlen(p) && *p == '!')
555 if (strlen(p) && p[strlen(p) - 1] == '\n')
556 p[strlen(p) - 1] = '\0';
558 if (!strncmp(p, "description:", strlen("description:"))) {
559 p += strlen("description:");
560 desc->half_liner = strdup(ltrim(p));
564 if (!strncmp(p, "args:", strlen("args:"))) {
565 p += strlen("args:");
566 desc->args = strdup(ltrim(p));
576 static int list_available_scripts(const struct option *opt __used,
577 const char *s __used, int unset __used)
579 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
580 char scripts_path[MAXPATHLEN];
581 DIR *scripts_dir, *lang_dir;
582 char script_path[MAXPATHLEN];
583 char lang_path[MAXPATHLEN];
584 struct script_desc *desc;
585 char first_half[BUFSIZ];
589 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
591 scripts_dir = opendir(scripts_path);
595 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
596 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
598 lang_dir = opendir(lang_path);
602 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
603 script_root = strdup(script_dirent.d_name);
604 str = (char *)ends_with(script_root, REPORT_SUFFIX);
607 desc = script_desc__findnew(script_root);
608 snprintf(script_path, MAXPATHLEN, "%s/%s",
609 lang_path, script_dirent.d_name);
610 read_script_info(desc, script_path);
616 fprintf(stdout, "List of available trace scripts:\n");
617 list_for_each_entry(desc, &script_descs, node) {
618 sprintf(first_half, "%s %s", desc->name,
619 desc->args ? desc->args : "");
620 fprintf(stdout, " %-36s %s\n", first_half,
621 desc->half_liner ? desc->half_liner : "");
627 static char *get_script_path(const char *script_root, const char *suffix)
629 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
630 char scripts_path[MAXPATHLEN];
631 char script_path[MAXPATHLEN];
632 DIR *scripts_dir, *lang_dir;
633 char lang_path[MAXPATHLEN];
634 char *str, *__script_root;
637 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
639 scripts_dir = opendir(scripts_path);
643 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
644 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
646 lang_dir = opendir(lang_path);
650 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
651 __script_root = strdup(script_dirent.d_name);
652 str = (char *)ends_with(__script_root, suffix);
655 if (strcmp(__script_root, script_root))
657 snprintf(script_path, MAXPATHLEN, "%s/%s",
658 lang_path, script_dirent.d_name);
659 path = strdup(script_path);
670 static bool is_top_script(const char *script_path)
672 return ends_with(script_path, "top") == NULL ? false : true;
675 static int has_required_arg(char *script_path)
677 struct script_desc *desc;
681 desc = script_desc__new(NULL);
683 if (read_script_info(desc, script_path))
689 for (p = desc->args; *p; p++)
693 script_desc__delete(desc);
698 static const char * const script_usage[] = {
699 "perf script [<options>]",
700 "perf script [<options>] record <script> [<record-options>] <command>",
701 "perf script [<options>] report <script> [script-args]",
702 "perf script [<options>] <script> [<record-options>] <command>",
703 "perf script [<options>] <top-script> [script-args]",
707 static const struct option options[] = {
708 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
709 "dump raw trace in ASCII"),
710 OPT_INCR('v', "verbose", &verbose,
711 "be more verbose (show symbol address, etc)"),
712 OPT_BOOLEAN('L', "Latency", &latency_format,
713 "show latency attributes (irqs/preemption disabled, etc)"),
714 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
715 list_available_scripts),
716 OPT_CALLBACK('s', "script", NULL, "name",
717 "script file name (lang:script name, script name, or *)",
719 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
720 "generate perf-script.xx script in specified language"),
721 OPT_STRING('i', "input", &input_name, "file",
723 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
724 "do various checks like samples ordering and lost events"),
725 OPT_CALLBACK('f', "fields", NULL, "str",
726 "comma separated output fields. Options: comm,tid,pid,time,cpu,event,trace",
727 parse_output_fields),
732 static bool have_cmd(int argc, const char **argv)
734 char **__argv = malloc(sizeof(const char *) * argc);
738 memcpy(__argv, argv, sizeof(const char *) * argc);
739 argc = parse_options(argc, (const char **)__argv, record_options,
740 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
746 int cmd_script(int argc, const char **argv, const char *prefix __used)
748 char *rec_script_path = NULL;
749 char *rep_script_path = NULL;
750 struct perf_session *session;
751 char *script_path = NULL;
758 argc = parse_options(argc, argv, options, script_usage,
759 PARSE_OPT_STOP_AT_NON_OPTION);
761 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
762 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
763 if (!rec_script_path)
764 return cmd_record(argc, argv, NULL);
767 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
768 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
769 if (!rep_script_path) {
771 "Please specify a valid report script"
772 "(see 'perf script -l' for listing)\n");
777 /* make sure PERF_EXEC_PATH is set for scripts */
778 perf_set_argv_exec_path(perf_exec_path());
780 if (argc && !script_name && !rec_script_path && !rep_script_path) {
785 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
786 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
788 if (!rec_script_path && !rep_script_path) {
789 fprintf(stderr, " Couldn't find script %s\n\n See perf"
790 " script -l for available scripts.\n", argv[0]);
791 usage_with_options(script_usage, options);
794 if (is_top_script(argv[0])) {
799 rep_args = has_required_arg(rep_script_path);
800 rec_args = (argc - 1) - rep_args;
802 fprintf(stderr, " %s script requires options."
803 "\n\n See perf script -l for available "
804 "scripts and options.\n", argv[0]);
805 usage_with_options(script_usage, options);
809 if (pipe(live_pipe) < 0) {
810 perror("failed to create pipe");
816 perror("failed to fork");
824 dup2(live_pipe[1], 1);
827 if (!is_top_script(argv[0]))
828 system_wide = !have_cmd(argc - rep_args,
831 __argv = malloc((argc + 6) * sizeof(const char *));
835 __argv[j++] = "/bin/sh";
836 __argv[j++] = rec_script_path;
842 for (i = rep_args + 1; i < argc; i++)
843 __argv[j++] = argv[i];
846 execvp("/bin/sh", (char **)__argv);
851 dup2(live_pipe[0], 0);
854 __argv = malloc((argc + 4) * sizeof(const char *));
858 __argv[j++] = "/bin/sh";
859 __argv[j++] = rep_script_path;
860 for (i = 1; i < rep_args + 1; i++)
861 __argv[j++] = argv[i];
866 execvp("/bin/sh", (char **)__argv);
872 script_path = rec_script_path;
874 script_path = rep_script_path;
881 system_wide = !have_cmd(argc - 1, &argv[1]);
883 __argv = malloc((argc + 2) * sizeof(const char *));
886 __argv[j++] = "/bin/sh";
887 __argv[j++] = script_path;
890 for (i = 2; i < argc; i++)
891 __argv[j++] = argv[i];
894 execvp("/bin/sh", (char **)__argv);
899 if (symbol__init() < 0)
904 session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
908 if (strcmp(input_name, "-") &&
909 !perf_session__has_traces(session, "record -R"))
912 if (generate_script_lang) {
913 struct stat perf_stat;
916 if (output_set_by_user) {
918 "custom fields not supported for generated scripts");
922 input = open(input_name, O_RDONLY);
924 perror("failed to open file");
928 err = fstat(input, &perf_stat);
930 perror("failed to stat file");
934 if (!perf_stat.st_size) {
935 fprintf(stderr, "zero-sized file, nothing to do!\n");
939 scripting_ops = script_spec__lookup(generate_script_lang);
940 if (!scripting_ops) {
941 fprintf(stderr, "invalid language specifier");
945 err = scripting_ops->generate_script("perf-script");
950 err = scripting_ops->start_script(script_name, argc, argv);
953 pr_debug("perf script started with script %s\n\n", script_name);
956 err = __cmd_script(session);
958 perf_session__delete(session);