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/util.h"
15 static char const *script_name
;
16 static char const *generate_script_lang
;
17 static bool debug_mode
;
18 static u64 last_timestamp
;
19 static u64 nr_unordered
;
21 static int default_start_script(const char *script __unused
,
23 const char **argv __unused
)
28 static int default_stop_script(void)
33 static int default_generate_script(const char *outfile __unused
)
38 static struct scripting_ops default_scripting_ops
= {
39 .start_script
= default_start_script
,
40 .stop_script
= default_stop_script
,
41 .process_event
= print_event
,
42 .generate_script
= default_generate_script
,
45 static struct scripting_ops
*scripting_ops
;
47 static void setup_scripting(void)
49 setup_perl_scripting();
50 setup_python_scripting();
52 scripting_ops
= &default_scripting_ops
;
55 static int cleanup_scripting(void)
57 pr_debug("\nperf trace script stopped\n");
59 return scripting_ops
->stop_script();
62 static char const *input_name
= "perf.data";
64 static int process_sample_event(event_t
*event
, struct perf_session
*session
)
66 struct sample_data data
;
67 struct thread
*thread
;
69 memset(&data
, 0, sizeof(data
));
74 event__parse_sample(event
, session
->sample_type
, &data
);
76 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event
->header
.misc
,
77 data
.pid
, data
.tid
, data
.ip
, data
.period
);
79 thread
= perf_session__findnew(session
, event
->ip
.pid
);
81 pr_debug("problem processing %d event, skipping it.\n",
86 if (session
->sample_type
& PERF_SAMPLE_RAW
) {
88 if (data
.time
< last_timestamp
) {
89 pr_err("Samples misordered, previous: %llu "
90 "this: %llu\n", last_timestamp
,
94 last_timestamp
= data
.time
;
98 * FIXME: better resolve from pid from the struct trace_entry
99 * field, although it should be the same than this perf
102 scripting_ops
->process_event(data
.cpu
, data
.raw_data
,
104 data
.time
, thread
->comm
);
107 session
->hists
.stats
.total_period
+= data
.period
;
113 static int process_lost_event(event_t
*event
, struct perf_session
*session __used
)
115 nr_lost
+= event
->lost
.lost
;
120 static struct perf_event_ops event_ops
= {
121 .sample
= process_sample_event
,
122 .comm
= event__process_comm
,
123 .attr
= event__process_attr
,
124 .event_type
= event__process_event_type
,
125 .tracing_data
= event__process_tracing_data
,
126 .build_id
= event__process_build_id
,
127 .lost
= process_lost_event
,
128 .ordered_samples
= true,
131 extern volatile int session_done
;
133 static void sig_handler(int sig __unused
)
138 static int __cmd_trace(struct perf_session
*session
)
142 signal(SIGINT
, sig_handler
);
144 ret
= perf_session__process_events(session
, &event_ops
);
147 pr_err("Misordered timestamps: %llu\n", nr_unordered
);
148 pr_err("Lost events: %llu\n", nr_lost
);
155 struct list_head node
;
156 struct scripting_ops
*ops
;
160 LIST_HEAD(script_specs
);
162 static struct script_spec
*script_spec__new(const char *spec
,
163 struct scripting_ops
*ops
)
165 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
168 strcpy(s
->spec
, spec
);
175 static void script_spec__delete(struct script_spec
*s
)
181 static void script_spec__add(struct script_spec
*s
)
183 list_add_tail(&s
->node
, &script_specs
);
186 static struct script_spec
*script_spec__find(const char *spec
)
188 struct script_spec
*s
;
190 list_for_each_entry(s
, &script_specs
, node
)
191 if (strcasecmp(s
->spec
, spec
) == 0)
196 static struct script_spec
*script_spec__findnew(const char *spec
,
197 struct scripting_ops
*ops
)
199 struct script_spec
*s
= script_spec__find(spec
);
204 s
= script_spec__new(spec
, ops
);
206 goto out_delete_spec
;
213 script_spec__delete(s
);
218 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
220 struct script_spec
*s
;
222 s
= script_spec__find(spec
);
226 s
= script_spec__findnew(spec
, ops
);
233 static struct scripting_ops
*script_spec__lookup(const char *spec
)
235 struct script_spec
*s
= script_spec__find(spec
);
242 static void list_available_languages(void)
244 struct script_spec
*s
;
246 fprintf(stderr
, "\n");
247 fprintf(stderr
, "Scripting language extensions (used in "
248 "perf trace -s [spec:]script.[spec]):\n\n");
250 list_for_each_entry(s
, &script_specs
, node
)
251 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
253 fprintf(stderr
, "\n");
256 static int parse_scriptname(const struct option
*opt __used
,
257 const char *str
, int unset __used
)
260 const char *script
, *ext
;
263 if (strcmp(str
, "lang") == 0) {
264 list_available_languages();
268 script
= strchr(str
, ':');
271 if (len
>= PATH_MAX
) {
272 fprintf(stderr
, "invalid language specifier");
275 strncpy(spec
, str
, len
);
277 scripting_ops
= script_spec__lookup(spec
);
278 if (!scripting_ops
) {
279 fprintf(stderr
, "invalid language specifier");
285 ext
= strrchr(script
, '.');
287 fprintf(stderr
, "invalid script extension");
290 scripting_ops
= script_spec__lookup(++ext
);
291 if (!scripting_ops
) {
292 fprintf(stderr
, "invalid script extension");
297 script_name
= strdup(script
);
302 #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
303 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
305 if (lang_dirent.d_type == DT_DIR && \
306 (strcmp(lang_dirent.d_name, ".")) && \
307 (strcmp(lang_dirent.d_name, "..")))
309 #define for_each_script(lang_dir, script_dirent, script_next) \
310 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
312 if (script_dirent.d_type != DT_DIR)
315 #define RECORD_SUFFIX "-record"
316 #define REPORT_SUFFIX "-report"
319 struct list_head node
;
325 LIST_HEAD(script_descs
);
327 static struct script_desc
*script_desc__new(const char *name
)
329 struct script_desc
*s
= zalloc(sizeof(*s
));
332 s
->name
= strdup(name
);
337 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 char *ends_with(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_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_dir
, script_dirent
, script_next
) {
473 script_root
= strdup(script_dirent
.d_name
);
474 str
= 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_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_dir
, script_dirent
, script_next
) {
521 __script_root
= strdup(script_dirent
.d_name
);
522 str
= 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 const char * const trace_usage
[] = {
541 "perf trace [<options>] <command>",
545 static const struct option options
[] = {
546 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
547 "dump raw trace in ASCII"),
548 OPT_INCR('v', "verbose", &verbose
,
549 "be more verbose (show symbol address, etc)"),
550 OPT_BOOLEAN('L', "Latency", &latency_format
,
551 "show latency attributes (irqs/preemption disabled, etc)"),
552 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
553 list_available_scripts
),
554 OPT_CALLBACK('s', "script", NULL
, "name",
555 "script file name (lang:script name, script name, or *)",
557 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
558 "generate perf-trace.xx script in specified language"),
559 OPT_STRING('i', "input", &input_name
, "file",
561 OPT_BOOLEAN('d', "debug-mode", &debug_mode
,
562 "do various checks like samples ordering and lost events"),
567 int cmd_trace(int argc
, const char **argv
, const char *prefix __used
)
569 struct perf_session
*session
;
570 const char *suffix
= NULL
;
575 if (argc
>= 2 && strncmp(argv
[1], "rec", strlen("rec")) == 0) {
578 "Please specify a record script\n");
581 suffix
= RECORD_SUFFIX
;
584 if (argc
>= 2 && strncmp(argv
[1], "rep", strlen("rep")) == 0) {
587 "Please specify a report script\n");
590 suffix
= REPORT_SUFFIX
;
593 /* make sure PERF_EXEC_PATH is set for scripts */
594 perf_set_argv_exec_path(perf_exec_path());
596 if (!suffix
&& argc
>= 2 && strncmp(argv
[1], "-", strlen("-")) != 0) {
597 char *record_script_path
, *report_script_path
;
601 record_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
602 if (!record_script_path
) {
603 fprintf(stderr
, "record script not found\n");
607 report_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
608 if (!report_script_path
) {
609 fprintf(stderr
, "report script not found\n");
613 if (pipe(live_pipe
) < 0) {
614 perror("failed to create pipe");
620 perror("failed to fork");
625 dup2(live_pipe
[1], 1);
628 __argv
= malloc(6 * sizeof(const char *));
629 __argv
[0] = "/bin/sh";
630 __argv
[1] = record_script_path
;
636 execvp("/bin/sh", (char **)__argv
);
640 dup2(live_pipe
[0], 0);
643 __argv
= malloc((argc
+ 3) * sizeof(const char *));
644 __argv
[0] = "/bin/sh";
645 __argv
[1] = report_script_path
;
646 for (i
= 2; i
< argc
; i
++)
652 execvp("/bin/sh", (char **)__argv
);
657 script_path
= get_script_path(argv
[2], suffix
);
659 fprintf(stderr
, "script not found\n");
663 __argv
= malloc((argc
+ 1) * sizeof(const char *));
664 __argv
[0] = "/bin/sh";
665 __argv
[1] = script_path
;
666 for (i
= 3; i
< argc
; i
++)
667 __argv
[i
- 1] = argv
[i
];
668 __argv
[argc
- 1] = NULL
;
670 execvp("/bin/sh", (char **)__argv
);
676 argc
= parse_options(argc
, argv
, options
, trace_usage
,
677 PARSE_OPT_STOP_AT_NON_OPTION
);
679 if (symbol__init() < 0)
684 session
= perf_session__new(input_name
, O_RDONLY
, 0, false);
688 if (strcmp(input_name
, "-") &&
689 !perf_session__has_traces(session
, "record -R"))
692 if (generate_script_lang
) {
693 struct stat perf_stat
;
695 int input
= open(input_name
, O_RDONLY
);
697 perror("failed to open file");
701 err
= fstat(input
, &perf_stat
);
703 perror("failed to stat file");
707 if (!perf_stat
.st_size
) {
708 fprintf(stderr
, "zero-sized file, nothing to do!\n");
712 scripting_ops
= script_spec__lookup(generate_script_lang
);
713 if (!scripting_ops
) {
714 fprintf(stderr
, "invalid language specifier");
718 err
= scripting_ops
->generate_script("perf-trace");
723 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
726 pr_debug("perf trace started with script %s\n\n", script_name
);
729 err
= __cmd_trace(session
);
731 perf_session__delete(session
);