4 #include "util/cache.h"
5 #include "util/symbol.h"
6 #include "util/thread.h"
7 #include "util/header.h"
8 #include "util/exec_cmd.h"
9 #include "util/trace-event.h"
10 #include "util/session.h"
12 static char const *script_name
;
13 static char const *generate_script_lang
;
14 static bool debug_ordering
;
15 static u64 last_timestamp
;
17 static int default_start_script(const char *script __unused
,
19 const char **argv __unused
)
24 static int default_stop_script(void)
29 static int default_generate_script(const char *outfile __unused
)
34 static struct scripting_ops default_scripting_ops
= {
35 .start_script
= default_start_script
,
36 .stop_script
= default_stop_script
,
37 .process_event
= print_event
,
38 .generate_script
= default_generate_script
,
41 static struct scripting_ops
*scripting_ops
;
43 static void setup_scripting(void)
45 /* make sure PERF_EXEC_PATH is set for scripts */
46 perf_set_argv_exec_path(perf_exec_path());
48 setup_perl_scripting();
49 setup_python_scripting();
51 scripting_ops
= &default_scripting_ops
;
54 static int cleanup_scripting(void)
56 pr_debug("\nperf trace script stopped\n");
58 return scripting_ops
->stop_script();
61 #include "util/parse-options.h"
64 #include "util/debug.h"
66 #include "util/trace-event.h"
67 #include "util/exec_cmd.h"
69 static char const *input_name
= "perf.data";
71 static int process_sample_event(event_t
*event
, struct perf_session
*session
)
73 struct sample_data data
;
74 struct thread
*thread
;
76 memset(&data
, 0, sizeof(data
));
81 event__parse_sample(event
, session
->sample_type
, &data
);
83 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event
->header
.misc
,
84 data
.pid
, data
.tid
, data
.ip
, data
.period
);
86 thread
= perf_session__findnew(session
, event
->ip
.pid
);
88 pr_debug("problem processing %d event, skipping it.\n",
93 if (session
->sample_type
& PERF_SAMPLE_RAW
) {
95 if (data
.time
< last_timestamp
) {
96 pr_err("Samples misordered, previous: %llu "
97 "this: %llu\n", last_timestamp
,
100 last_timestamp
= data
.time
;
103 * FIXME: better resolve from pid from the struct trace_entry
104 * field, although it should be the same than this perf
107 scripting_ops
->process_event(data
.cpu
, data
.raw_data
,
109 data
.time
, thread
->comm
);
112 session
->hists
.stats
.total_period
+= data
.period
;
116 static struct perf_event_ops event_ops
= {
117 .sample
= process_sample_event
,
118 .comm
= event__process_comm
,
119 .attr
= event__process_attr
,
120 .event_type
= event__process_event_type
,
121 .tracing_data
= event__process_tracing_data
,
122 .build_id
= event__process_build_id
,
123 .ordered_samples
= true,
126 extern volatile int session_done
;
128 static void sig_handler(int sig __unused
)
133 static int __cmd_trace(struct perf_session
*session
)
135 signal(SIGINT
, sig_handler
);
137 return perf_session__process_events(session
, &event_ops
);
141 struct list_head node
;
142 struct scripting_ops
*ops
;
146 LIST_HEAD(script_specs
);
148 static struct script_spec
*script_spec__new(const char *spec
,
149 struct scripting_ops
*ops
)
151 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
154 strcpy(s
->spec
, spec
);
161 static void script_spec__delete(struct script_spec
*s
)
167 static void script_spec__add(struct script_spec
*s
)
169 list_add_tail(&s
->node
, &script_specs
);
172 static struct script_spec
*script_spec__find(const char *spec
)
174 struct script_spec
*s
;
176 list_for_each_entry(s
, &script_specs
, node
)
177 if (strcasecmp(s
->spec
, spec
) == 0)
182 static struct script_spec
*script_spec__findnew(const char *spec
,
183 struct scripting_ops
*ops
)
185 struct script_spec
*s
= script_spec__find(spec
);
190 s
= script_spec__new(spec
, ops
);
192 goto out_delete_spec
;
199 script_spec__delete(s
);
204 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
206 struct script_spec
*s
;
208 s
= script_spec__find(spec
);
212 s
= script_spec__findnew(spec
, ops
);
219 static struct scripting_ops
*script_spec__lookup(const char *spec
)
221 struct script_spec
*s
= script_spec__find(spec
);
228 static void list_available_languages(void)
230 struct script_spec
*s
;
232 fprintf(stderr
, "\n");
233 fprintf(stderr
, "Scripting language extensions (used in "
234 "perf trace -s [spec:]script.[spec]):\n\n");
236 list_for_each_entry(s
, &script_specs
, node
)
237 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
239 fprintf(stderr
, "\n");
242 static int parse_scriptname(const struct option
*opt __used
,
243 const char *str
, int unset __used
)
246 const char *script
, *ext
;
249 if (strcmp(str
, "lang") == 0) {
250 list_available_languages();
254 script
= strchr(str
, ':');
257 if (len
>= PATH_MAX
) {
258 fprintf(stderr
, "invalid language specifier");
261 strncpy(spec
, str
, len
);
263 scripting_ops
= script_spec__lookup(spec
);
264 if (!scripting_ops
) {
265 fprintf(stderr
, "invalid language specifier");
271 ext
= strchr(script
, '.');
273 fprintf(stderr
, "invalid script extension");
276 scripting_ops
= script_spec__lookup(++ext
);
277 if (!scripting_ops
) {
278 fprintf(stderr
, "invalid script extension");
283 script_name
= strdup(script
);
288 #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
289 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
291 if (lang_dirent.d_type == DT_DIR && \
292 (strcmp(lang_dirent.d_name, ".")) && \
293 (strcmp(lang_dirent.d_name, "..")))
295 #define for_each_script(lang_dir, script_dirent, script_next) \
296 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
298 if (script_dirent.d_type != DT_DIR)
301 #define RECORD_SUFFIX "-record"
302 #define REPORT_SUFFIX "-report"
305 struct list_head node
;
311 LIST_HEAD(script_descs
);
313 static struct script_desc
*script_desc__new(const char *name
)
315 struct script_desc
*s
= zalloc(sizeof(*s
));
318 s
->name
= strdup(name
);
323 static void script_desc__delete(struct script_desc
*s
)
329 static void script_desc__add(struct script_desc
*s
)
331 list_add_tail(&s
->node
, &script_descs
);
334 static struct script_desc
*script_desc__find(const char *name
)
336 struct script_desc
*s
;
338 list_for_each_entry(s
, &script_descs
, node
)
339 if (strcasecmp(s
->name
, name
) == 0)
344 static struct script_desc
*script_desc__findnew(const char *name
)
346 struct script_desc
*s
= script_desc__find(name
);
351 s
= script_desc__new(name
);
353 goto out_delete_desc
;
360 script_desc__delete(s
);
365 static char *ends_with(char *str
, const char *suffix
)
367 size_t suffix_len
= strlen(suffix
);
370 if (strlen(str
) > suffix_len
) {
371 p
= str
+ strlen(str
) - suffix_len
;
372 if (!strncmp(p
, suffix
, suffix_len
))
379 static char *ltrim(char *str
)
381 int len
= strlen(str
);
383 while (len
&& isspace(*str
)) {
391 static int read_script_info(struct script_desc
*desc
, const char *filename
)
393 char line
[BUFSIZ
], *p
;
396 fp
= fopen(filename
, "r");
400 while (fgets(line
, sizeof(line
), fp
)) {
407 if (strlen(p
) && *p
== '!')
411 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
412 p
[strlen(p
) - 1] = '\0';
414 if (!strncmp(p
, "description:", strlen("description:"))) {
415 p
+= strlen("description:");
416 desc
->half_liner
= strdup(ltrim(p
));
420 if (!strncmp(p
, "args:", strlen("args:"))) {
421 p
+= strlen("args:");
422 desc
->args
= strdup(ltrim(p
));
432 static int list_available_scripts(const struct option
*opt __used
,
433 const char *s __used
, int unset __used
)
435 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
436 char scripts_path
[MAXPATHLEN
];
437 DIR *scripts_dir
, *lang_dir
;
438 char script_path
[MAXPATHLEN
];
439 char lang_path
[MAXPATHLEN
];
440 struct script_desc
*desc
;
441 char first_half
[BUFSIZ
];
445 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
447 scripts_dir
= opendir(scripts_path
);
451 for_each_lang(scripts_dir
, lang_dirent
, lang_next
) {
452 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
454 lang_dir
= opendir(lang_path
);
458 for_each_script(lang_dir
, script_dirent
, script_next
) {
459 script_root
= strdup(script_dirent
.d_name
);
460 str
= ends_with(script_root
, REPORT_SUFFIX
);
463 desc
= script_desc__findnew(script_root
);
464 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
465 lang_path
, script_dirent
.d_name
);
466 read_script_info(desc
, script_path
);
472 fprintf(stdout
, "List of available trace scripts:\n");
473 list_for_each_entry(desc
, &script_descs
, node
) {
474 sprintf(first_half
, "%s %s", desc
->name
,
475 desc
->args
? desc
->args
: "");
476 fprintf(stdout
, " %-36s %s\n", first_half
,
477 desc
->half_liner
? desc
->half_liner
: "");
483 static char *get_script_path(const char *script_root
, const char *suffix
)
485 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
486 char scripts_path
[MAXPATHLEN
];
487 char script_path
[MAXPATHLEN
];
488 DIR *scripts_dir
, *lang_dir
;
489 char lang_path
[MAXPATHLEN
];
490 char *str
, *__script_root
;
493 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
495 scripts_dir
= opendir(scripts_path
);
499 for_each_lang(scripts_dir
, lang_dirent
, lang_next
) {
500 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
502 lang_dir
= opendir(lang_path
);
506 for_each_script(lang_dir
, script_dirent
, script_next
) {
507 __script_root
= strdup(script_dirent
.d_name
);
508 str
= ends_with(__script_root
, suffix
);
511 if (strcmp(__script_root
, script_root
))
513 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
514 lang_path
, script_dirent
.d_name
);
515 path
= strdup(script_path
);
526 static const char * const trace_usage
[] = {
527 "perf trace [<options>] <command>",
531 static const struct option options
[] = {
532 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
533 "dump raw trace in ASCII"),
534 OPT_INCR('v', "verbose", &verbose
,
535 "be more verbose (show symbol address, etc)"),
536 OPT_BOOLEAN('L', "Latency", &latency_format
,
537 "show latency attributes (irqs/preemption disabled, etc)"),
538 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
539 list_available_scripts
),
540 OPT_CALLBACK('s', "script", NULL
, "name",
541 "script file name (lang:script name, script name, or *)",
543 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
544 "generate perf-trace.xx script in specified language"),
545 OPT_STRING('i', "input", &input_name
, "file",
547 OPT_BOOLEAN('d', "debug-ordering", &debug_ordering
,
548 "check that samples time ordering is monotonic"),
553 int cmd_trace(int argc
, const char **argv
, const char *prefix __used
)
555 struct perf_session
*session
;
556 const char *suffix
= NULL
;
561 if (argc
>= 2 && strncmp(argv
[1], "rec", strlen("rec")) == 0) {
564 "Please specify a record script\n");
567 suffix
= RECORD_SUFFIX
;
570 if (argc
>= 2 && strncmp(argv
[1], "rep", strlen("rep")) == 0) {
573 "Please specify a report script\n");
576 suffix
= REPORT_SUFFIX
;
579 if (!suffix
&& argc
>= 2 && strncmp(argv
[1], "-", strlen("-")) != 0) {
580 char *record_script_path
, *report_script_path
;
584 record_script_path
= get_script_path(argv
[1], RECORD_SUFFIX
);
585 if (!record_script_path
) {
586 fprintf(stderr
, "record script not found\n");
590 report_script_path
= get_script_path(argv
[1], REPORT_SUFFIX
);
591 if (!report_script_path
) {
592 fprintf(stderr
, "report script not found\n");
596 if (pipe(live_pipe
) < 0) {
597 perror("failed to create pipe");
603 perror("failed to fork");
608 dup2(live_pipe
[1], 1);
611 __argv
= malloc(5 * sizeof(const char *));
612 __argv
[0] = "/bin/sh";
613 __argv
[1] = record_script_path
;
618 execvp("/bin/sh", (char **)__argv
);
622 dup2(live_pipe
[0], 0);
625 __argv
= malloc((argc
+ 3) * sizeof(const char *));
626 __argv
[0] = "/bin/sh";
627 __argv
[1] = report_script_path
;
628 for (i
= 2; i
< argc
; i
++)
634 execvp("/bin/sh", (char **)__argv
);
639 script_path
= get_script_path(argv
[2], suffix
);
641 fprintf(stderr
, "script not found\n");
645 __argv
= malloc((argc
+ 1) * sizeof(const char *));
646 __argv
[0] = "/bin/sh";
647 __argv
[1] = script_path
;
648 for (i
= 3; i
< argc
; i
++)
649 __argv
[i
- 1] = argv
[i
];
650 __argv
[argc
- 1] = NULL
;
652 execvp("/bin/sh", (char **)__argv
);
658 argc
= parse_options(argc
, argv
, options
, trace_usage
,
659 PARSE_OPT_STOP_AT_NON_OPTION
);
661 if (symbol__init() < 0)
666 session
= perf_session__new(input_name
, O_RDONLY
, 0, false);
670 if (strcmp(input_name
, "-") &&
671 !perf_session__has_traces(session
, "record -R"))
674 if (generate_script_lang
) {
675 struct stat perf_stat
;
677 int input
= open(input_name
, O_RDONLY
);
679 perror("failed to open file");
683 err
= fstat(input
, &perf_stat
);
685 perror("failed to stat file");
689 if (!perf_stat
.st_size
) {
690 fprintf(stderr
, "zero-sized file, nothing to do!\n");
694 scripting_ops
= script_spec__lookup(generate_script_lang
);
695 if (!scripting_ops
) {
696 fprintf(stderr
, "invalid language specifier");
700 err
= scripting_ops
->generate_script("perf-trace");
705 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
708 pr_debug("perf trace started with script %s\n\n", script_name
);
711 err
= __cmd_trace(session
);
713 perf_session__delete(session
);