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
;
15 static int default_start_script(const char *script __unused
,
17 const char **argv __unused
)
22 static int default_stop_script(void)
27 static int default_generate_script(const char *outfile __unused
)
32 static struct scripting_ops default_scripting_ops
= {
33 .start_script
= default_start_script
,
34 .stop_script
= default_stop_script
,
35 .process_event
= print_event
,
36 .generate_script
= default_generate_script
,
39 static struct scripting_ops
*scripting_ops
;
41 static void setup_scripting(void)
43 /* make sure PERF_EXEC_PATH is set for scripts */
44 perf_set_argv_exec_path(perf_exec_path());
46 setup_perl_scripting();
48 scripting_ops
= &default_scripting_ops
;
51 static int cleanup_scripting(void)
53 return scripting_ops
->stop_script();
56 #include "util/parse-options.h"
59 #include "util/debug.h"
61 #include "util/trace-event.h"
62 #include "util/exec_cmd.h"
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 * 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(data
.cpu
, data
.raw_data
,
96 data
.time
, thread
->comm
);
99 session
->events_stats
.total
+= data
.period
;
103 static struct perf_event_ops event_ops
= {
104 .sample
= process_sample_event
,
105 .comm
= event__process_comm
,
108 static int __cmd_trace(struct perf_session
*session
)
110 return perf_session__process_events(session
, &event_ops
);
114 struct list_head node
;
115 struct scripting_ops
*ops
;
119 LIST_HEAD(script_specs
);
121 static struct script_spec
*script_spec__new(const char *spec
,
122 struct scripting_ops
*ops
)
124 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
127 strcpy(s
->spec
, spec
);
134 static void script_spec__delete(struct script_spec
*s
)
140 static void script_spec__add(struct script_spec
*s
)
142 list_add_tail(&s
->node
, &script_specs
);
145 static struct script_spec
*script_spec__find(const char *spec
)
147 struct script_spec
*s
;
149 list_for_each_entry(s
, &script_specs
, node
)
150 if (strcasecmp(s
->spec
, spec
) == 0)
155 static struct script_spec
*script_spec__findnew(const char *spec
,
156 struct scripting_ops
*ops
)
158 struct script_spec
*s
= script_spec__find(spec
);
163 s
= script_spec__new(spec
, ops
);
165 goto out_delete_spec
;
172 script_spec__delete(s
);
177 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
179 struct script_spec
*s
;
181 s
= script_spec__find(spec
);
185 s
= script_spec__findnew(spec
, ops
);
192 static struct scripting_ops
*script_spec__lookup(const char *spec
)
194 struct script_spec
*s
= script_spec__find(spec
);
201 static void list_available_languages(void)
203 struct script_spec
*s
;
205 fprintf(stderr
, "\n");
206 fprintf(stderr
, "Scripting language extensions (used in "
207 "perf trace -s [spec:]script.[spec]):\n\n");
209 list_for_each_entry(s
, &script_specs
, node
)
210 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
212 fprintf(stderr
, "\n");
215 static int parse_scriptname(const struct option
*opt __used
,
216 const char *str
, int unset __used
)
219 const char *script
, *ext
;
222 if (strcmp(str
, "list") == 0) {
223 list_available_languages();
227 script
= strchr(str
, ':');
230 if (len
>= PATH_MAX
) {
231 fprintf(stderr
, "invalid language specifier");
234 strncpy(spec
, str
, len
);
236 scripting_ops
= script_spec__lookup(spec
);
237 if (!scripting_ops
) {
238 fprintf(stderr
, "invalid language specifier");
244 ext
= strchr(script
, '.');
246 fprintf(stderr
, "invalid script extension");
249 scripting_ops
= script_spec__lookup(++ext
);
250 if (!scripting_ops
) {
251 fprintf(stderr
, "invalid script extension");
256 script_name
= strdup(script
);
261 #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
262 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
264 if (lang_dirent.d_type == DT_DIR && \
265 (strcmp(lang_dirent.d_name, ".")) && \
266 (strcmp(lang_dirent.d_name, "..")))
268 #define for_each_script(lang_dir, script_dirent, script_next) \
269 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
271 if (script_dirent.d_type != DT_DIR)
274 #define RECORD_SUFFIX "-record"
275 #define REPORT_SUFFIX "-report"
278 struct list_head node
;
284 LIST_HEAD(script_descs
);
286 static struct script_desc
*script_desc__new(const char *name
)
288 struct script_desc
*s
= zalloc(sizeof(*s
));
291 s
->name
= strdup(name
);
296 static void script_desc__delete(struct script_desc
*s
)
302 static void script_desc__add(struct script_desc
*s
)
304 list_add_tail(&s
->node
, &script_descs
);
307 static struct script_desc
*script_desc__find(const char *name
)
309 struct script_desc
*s
;
311 list_for_each_entry(s
, &script_descs
, node
)
312 if (strcasecmp(s
->name
, name
) == 0)
317 static struct script_desc
*script_desc__findnew(const char *name
)
319 struct script_desc
*s
= script_desc__find(name
);
324 s
= script_desc__new(name
);
326 goto out_delete_desc
;
333 script_desc__delete(s
);
338 static char *ends_with(char *str
, const char *suffix
)
340 size_t suffix_len
= strlen(suffix
);
343 if (strlen(str
) > suffix_len
) {
344 p
= str
+ strlen(str
) - suffix_len
;
345 if (!strncmp(p
, suffix
, suffix_len
))
352 static char *ltrim(char *str
)
354 int len
= strlen(str
);
356 while (len
&& isspace(*str
)) {
364 static int read_script_info(struct script_desc
*desc
, const char *filename
)
366 char line
[BUFSIZ
], *p
;
369 fp
= fopen(filename
, "r");
373 while (fgets(line
, sizeof(line
), fp
)) {
380 if (strlen(p
) && *p
== '!')
384 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
385 p
[strlen(p
) - 1] = '\0';
387 if (!strncmp(p
, "description:", strlen("description:"))) {
388 p
+= strlen("description:");
389 desc
->half_liner
= strdup(ltrim(p
));
393 if (!strncmp(p
, "args:", strlen("args:"))) {
394 p
+= strlen("args:");
395 desc
->args
= strdup(ltrim(p
));
405 static int list_available_scripts(const struct option
*opt __used
,
406 const char *s __used
, int unset __used
)
408 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
409 char scripts_path
[MAXPATHLEN
];
410 DIR *scripts_dir
, *lang_dir
;
411 char script_path
[MAXPATHLEN
];
412 char lang_path
[MAXPATHLEN
];
413 struct script_desc
*desc
;
414 char first_half
[BUFSIZ
];
418 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
420 scripts_dir
= opendir(scripts_path
);
424 for_each_lang(scripts_dir
, lang_dirent
, lang_next
) {
425 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
427 lang_dir
= opendir(lang_path
);
431 for_each_script(lang_dir
, script_dirent
, script_next
) {
432 script_root
= strdup(script_dirent
.d_name
);
433 str
= ends_with(script_root
, REPORT_SUFFIX
);
436 desc
= script_desc__findnew(script_root
);
437 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
438 lang_path
, script_dirent
.d_name
);
439 read_script_info(desc
, script_path
);
445 fprintf(stdout
, "List of available trace scripts:\n");
446 list_for_each_entry(desc
, &script_descs
, node
) {
447 sprintf(first_half
, "%s %s", desc
->name
,
448 desc
->args
? desc
->args
: "");
449 fprintf(stdout
, " %-36s %s\n", first_half
,
450 desc
->half_liner
? desc
->half_liner
: "");
456 static char *get_script_path(const char *script_root
, const char *suffix
)
458 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
459 char scripts_path
[MAXPATHLEN
];
460 char script_path
[MAXPATHLEN
];
461 DIR *scripts_dir
, *lang_dir
;
462 char lang_path
[MAXPATHLEN
];
463 char *str
, *__script_root
;
466 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
468 scripts_dir
= opendir(scripts_path
);
472 for_each_lang(scripts_dir
, lang_dirent
, lang_next
) {
473 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
475 lang_dir
= opendir(lang_path
);
479 for_each_script(lang_dir
, script_dirent
, script_next
) {
480 __script_root
= strdup(script_dirent
.d_name
);
481 str
= ends_with(__script_root
, suffix
);
484 if (strcmp(__script_root
, script_root
))
486 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
487 lang_path
, script_dirent
.d_name
);
488 path
= strdup(script_path
);
499 static const char * const trace_usage
[] = {
500 "perf trace [<options>] <command>",
504 static const struct option options
[] = {
505 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
506 "dump raw trace in ASCII"),
507 OPT_BOOLEAN('v', "verbose", &verbose
,
508 "be more verbose (show symbol address, etc)"),
509 OPT_BOOLEAN('L', "Latency", &latency_format
,
510 "show latency attributes (irqs/preemption disabled, etc)"),
511 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
512 list_available_scripts
),
513 OPT_CALLBACK('s', "script", NULL
, "name",
514 "script file name (lang:script name, script name, or *)",
516 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
517 "generate perf-trace.xx script in specified language"),
518 OPT_STRING('i', "input", &input_name
, "file",
524 int cmd_trace(int argc
, const char **argv
, const char *prefix __used
)
526 struct perf_session
*session
;
527 const char *suffix
= NULL
;
532 if (argc
>= 2 && strncmp(argv
[1], "rec", strlen("rec")) == 0) {
535 "Please specify a record script\n");
538 suffix
= RECORD_SUFFIX
;
541 if (argc
>= 2 && strncmp(argv
[1], "rep", strlen("rep")) == 0) {
544 "Please specify a report script\n");
547 suffix
= REPORT_SUFFIX
;
551 script_path
= get_script_path(argv
[2], suffix
);
553 fprintf(stderr
, "script not found\n");
557 __argv
= malloc((argc
+ 1) * sizeof(const char *));
558 __argv
[0] = "/bin/sh";
559 __argv
[1] = script_path
;
560 for (i
= 3; i
< argc
; i
++)
561 __argv
[i
- 1] = argv
[i
];
562 __argv
[argc
- 1] = NULL
;
564 execvp("/bin/sh", (char **)__argv
);
570 argc
= parse_options(argc
, argv
, options
, trace_usage
,
571 PARSE_OPT_STOP_AT_NON_OPTION
);
573 if (symbol__init() < 0)
577 session
= perf_session__new(input_name
, O_RDONLY
, 0);
581 if (!perf_session__has_traces(session
, "record -R"))
584 if (generate_script_lang
) {
585 struct stat perf_stat
;
587 int input
= open(input_name
, O_RDONLY
);
589 perror("failed to open file");
593 err
= fstat(input
, &perf_stat
);
595 perror("failed to stat file");
599 if (!perf_stat
.st_size
) {
600 fprintf(stderr
, "zero-sized file, nothing to do!\n");
604 scripting_ops
= script_spec__lookup(generate_script_lang
);
605 if (!scripting_ops
) {
606 fprintf(stderr
, "invalid language specifier");
610 perf_header__read(&session
->header
, input
);
611 err
= scripting_ops
->generate_script("perf-trace");
616 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
621 err
= __cmd_trace(session
);
623 perf_session__delete(session
);