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: %p period: %Ld\n",
81 (void *)(long)data
.ip
,
82 (long long)data
.period
);
84 thread
= perf_session__findnew(session
, event
->ip
.pid
);
86 pr_debug("problem processing %d event, skipping it.\n",
91 if (session
->sample_type
& PERF_SAMPLE_RAW
) {
93 * FIXME: better resolve from pid from the struct trace_entry
94 * field, although it should be the same than this perf
97 scripting_ops
->process_event(data
.cpu
, data
.raw_data
,
99 data
.time
, thread
->comm
);
102 session
->events_stats
.total
+= data
.period
;
106 static struct perf_event_ops event_ops
= {
107 .process_sample_event
= process_sample_event
,
108 .process_comm_event
= event__process_comm
,
111 static int __cmd_trace(struct perf_session
*session
)
113 return perf_session__process_events(session
, &event_ops
);
117 struct list_head node
;
118 struct scripting_ops
*ops
;
122 LIST_HEAD(script_specs
);
124 static struct script_spec
*script_spec__new(const char *spec
,
125 struct scripting_ops
*ops
)
127 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
130 strcpy(s
->spec
, spec
);
137 static void script_spec__delete(struct script_spec
*s
)
143 static void script_spec__add(struct script_spec
*s
)
145 list_add_tail(&s
->node
, &script_specs
);
148 static struct script_spec
*script_spec__find(const char *spec
)
150 struct script_spec
*s
;
152 list_for_each_entry(s
, &script_specs
, node
)
153 if (strcasecmp(s
->spec
, spec
) == 0)
158 static struct script_spec
*script_spec__findnew(const char *spec
,
159 struct scripting_ops
*ops
)
161 struct script_spec
*s
= script_spec__find(spec
);
166 s
= script_spec__new(spec
, ops
);
168 goto out_delete_spec
;
175 script_spec__delete(s
);
180 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
182 struct script_spec
*s
;
184 s
= script_spec__find(spec
);
188 s
= script_spec__findnew(spec
, ops
);
195 static struct scripting_ops
*script_spec__lookup(const char *spec
)
197 struct script_spec
*s
= script_spec__find(spec
);
204 static void list_available_languages(void)
206 struct script_spec
*s
;
208 fprintf(stderr
, "\n");
209 fprintf(stderr
, "Scripting language extensions (used in "
210 "perf trace -s [spec:]script.[spec]):\n\n");
212 list_for_each_entry(s
, &script_specs
, node
)
213 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
215 fprintf(stderr
, "\n");
218 static int parse_scriptname(const struct option
*opt __used
,
219 const char *str
, int unset __used
)
222 const char *script
, *ext
;
225 if (strcmp(str
, "list") == 0) {
226 list_available_languages();
230 script
= strchr(str
, ':');
233 if (len
>= PATH_MAX
) {
234 fprintf(stderr
, "invalid language specifier");
237 strncpy(spec
, str
, len
);
239 scripting_ops
= script_spec__lookup(spec
);
240 if (!scripting_ops
) {
241 fprintf(stderr
, "invalid language specifier");
247 ext
= strchr(script
, '.');
249 fprintf(stderr
, "invalid script extension");
252 scripting_ops
= script_spec__lookup(++ext
);
253 if (!scripting_ops
) {
254 fprintf(stderr
, "invalid script extension");
259 script_name
= strdup(script
);
264 #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
265 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
267 if (lang_dirent.d_type == DT_DIR && \
268 (strcmp(lang_dirent.d_name, ".")) && \
269 (strcmp(lang_dirent.d_name, "..")))
271 #define for_each_script(lang_dir, script_dirent, script_next) \
272 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
274 if (script_dirent.d_type != DT_DIR)
277 #define RECORD_SUFFIX "-record"
278 #define REPORT_SUFFIX "-report"
281 struct list_head node
;
287 LIST_HEAD(script_descs
);
289 static struct script_desc
*script_desc__new(const char *name
)
291 struct script_desc
*s
= zalloc(sizeof(*s
));
294 s
->name
= strdup(name
);
299 static void script_desc__delete(struct script_desc
*s
)
305 static void script_desc__add(struct script_desc
*s
)
307 list_add_tail(&s
->node
, &script_descs
);
310 static struct script_desc
*script_desc__find(const char *name
)
312 struct script_desc
*s
;
314 list_for_each_entry(s
, &script_descs
, node
)
315 if (strcasecmp(s
->name
, name
) == 0)
320 static struct script_desc
*script_desc__findnew(const char *name
)
322 struct script_desc
*s
= script_desc__find(name
);
327 s
= script_desc__new(name
);
329 goto out_delete_desc
;
336 script_desc__delete(s
);
341 static char *ends_with(char *str
, const char *suffix
)
343 size_t suffix_len
= strlen(suffix
);
346 if (strlen(str
) > suffix_len
) {
347 p
= str
+ strlen(str
) - suffix_len
;
348 if (!strncmp(p
, suffix
, suffix_len
))
355 static char *ltrim(char *str
)
357 int len
= strlen(str
);
359 while (len
&& isspace(*str
)) {
367 static int read_script_info(struct script_desc
*desc
, const char *filename
)
369 char line
[BUFSIZ
], *p
;
372 fp
= fopen(filename
, "r");
376 while (fgets(line
, sizeof(line
), fp
)) {
383 if (strlen(p
) && *p
== '!')
387 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
388 p
[strlen(p
) - 1] = '\0';
390 if (!strncmp(p
, "description:", strlen("description:"))) {
391 p
+= strlen("description:");
392 desc
->half_liner
= strdup(ltrim(p
));
396 if (!strncmp(p
, "args:", strlen("args:"))) {
397 p
+= strlen("args:");
398 desc
->args
= strdup(ltrim(p
));
408 static int list_available_scripts(const struct option
*opt __used
,
409 const char *s __used
, int unset __used
)
411 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
412 char scripts_path
[MAXPATHLEN
];
413 DIR *scripts_dir
, *lang_dir
;
414 char script_path
[MAXPATHLEN
];
415 char lang_path
[MAXPATHLEN
];
416 struct script_desc
*desc
;
417 char first_half
[BUFSIZ
];
421 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
423 scripts_dir
= opendir(scripts_path
);
427 for_each_lang(scripts_dir
, lang_dirent
, lang_next
) {
428 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
430 lang_dir
= opendir(lang_path
);
434 for_each_script(lang_dir
, script_dirent
, script_next
) {
435 script_root
= strdup(script_dirent
.d_name
);
436 str
= ends_with(script_root
, REPORT_SUFFIX
);
439 desc
= script_desc__findnew(script_root
);
440 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
441 lang_path
, script_dirent
.d_name
);
442 read_script_info(desc
, script_path
);
448 fprintf(stdout
, "List of available trace scripts:\n");
449 list_for_each_entry(desc
, &script_descs
, node
) {
450 sprintf(first_half
, "%s %s", desc
->name
,
451 desc
->args
? desc
->args
: "");
452 fprintf(stdout
, " %-36s %s\n", first_half
,
453 desc
->half_liner
? desc
->half_liner
: "");
459 static char *get_script_path(const char *script_root
, const char *suffix
)
461 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
462 char scripts_path
[MAXPATHLEN
];
463 char script_path
[MAXPATHLEN
];
464 DIR *scripts_dir
, *lang_dir
;
465 char lang_path
[MAXPATHLEN
];
466 char *str
, *__script_root
;
469 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
471 scripts_dir
= opendir(scripts_path
);
475 for_each_lang(scripts_dir
, lang_dirent
, lang_next
) {
476 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
478 lang_dir
= opendir(lang_path
);
482 for_each_script(lang_dir
, script_dirent
, script_next
) {
483 __script_root
= strdup(script_dirent
.d_name
);
484 str
= ends_with(__script_root
, suffix
);
487 if (strcmp(__script_root
, script_root
))
489 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
490 lang_path
, script_dirent
.d_name
);
491 path
= strdup(script_path
);
502 static const char * const trace_usage
[] = {
503 "perf trace [<options>] <command>",
507 static const struct option options
[] = {
508 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
509 "dump raw trace in ASCII"),
510 OPT_BOOLEAN('v', "verbose", &verbose
,
511 "be more verbose (show symbol address, etc)"),
512 OPT_BOOLEAN('L', "Latency", &latency_format
,
513 "show latency attributes (irqs/preemption disabled, etc)"),
514 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
515 list_available_scripts
),
516 OPT_CALLBACK('s', "script", NULL
, "name",
517 "script file name (lang:script name, script name, or *)",
519 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
520 "generate perf-trace.xx script in specified language"),
525 int cmd_trace(int argc
, const char **argv
, const char *prefix __used
)
527 struct perf_session
*session
;
528 const char *suffix
= NULL
;
533 if (argc
>= 2 && strncmp(argv
[1], "rec", strlen("rec")) == 0) {
536 "Please specify a record script\n");
539 suffix
= RECORD_SUFFIX
;
542 if (argc
>= 2 && strncmp(argv
[1], "rep", strlen("rep")) == 0) {
545 "Please specify a report script\n");
548 suffix
= REPORT_SUFFIX
;
552 script_path
= get_script_path(argv
[2], suffix
);
554 fprintf(stderr
, "script not found\n");
558 __argv
= malloc((argc
+ 1) * sizeof(const char *));
559 __argv
[0] = "/bin/sh";
560 __argv
[1] = script_path
;
561 for (i
= 3; i
< argc
; i
++)
562 __argv
[i
- 1] = argv
[i
];
563 __argv
[argc
- 1] = NULL
;
565 execvp("/bin/sh", (char **)__argv
);
571 argc
= parse_options(argc
, argv
, options
, trace_usage
,
572 PARSE_OPT_STOP_AT_NON_OPTION
);
574 if (symbol__init() < 0)
578 session
= perf_session__new(input_name
, O_RDONLY
, 0);
582 if (!perf_session__has_traces(session
, "record -R"))
585 if (generate_script_lang
) {
586 struct stat perf_stat
;
588 int input
= open(input_name
, O_RDONLY
);
590 perror("failed to open file");
594 err
= fstat(input
, &perf_stat
);
596 perror("failed to stat file");
600 if (!perf_stat
.st_size
) {
601 fprintf(stderr
, "zero-sized file, nothing to do!\n");
605 scripting_ops
= script_spec__lookup(generate_script_lang
);
606 if (!scripting_ops
) {
607 fprintf(stderr
, "invalid language specifier");
611 perf_header__read(&session
->header
, input
);
612 err
= scripting_ops
->generate_script("perf-trace");
617 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
622 err
= __cmd_trace(session
);
624 perf_session__delete(session
);