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
,
109 .sample_type_check
= perf_session__has_traces
,
112 static int __cmd_trace(struct perf_session
*session
)
114 return perf_session__process_events(session
, &event_ops
);
118 struct list_head node
;
119 struct scripting_ops
*ops
;
123 LIST_HEAD(script_specs
);
125 static struct script_spec
*script_spec__new(const char *spec
,
126 struct scripting_ops
*ops
)
128 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
131 strcpy(s
->spec
, spec
);
138 static void script_spec__delete(struct script_spec
*s
)
144 static void script_spec__add(struct script_spec
*s
)
146 list_add_tail(&s
->node
, &script_specs
);
149 static struct script_spec
*script_spec__find(const char *spec
)
151 struct script_spec
*s
;
153 list_for_each_entry(s
, &script_specs
, node
)
154 if (strcasecmp(s
->spec
, spec
) == 0)
159 static struct script_spec
*script_spec__findnew(const char *spec
,
160 struct scripting_ops
*ops
)
162 struct script_spec
*s
= script_spec__find(spec
);
167 s
= script_spec__new(spec
, ops
);
169 goto out_delete_spec
;
176 script_spec__delete(s
);
181 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
183 struct script_spec
*s
;
185 s
= script_spec__find(spec
);
189 s
= script_spec__findnew(spec
, ops
);
196 static struct scripting_ops
*script_spec__lookup(const char *spec
)
198 struct script_spec
*s
= script_spec__find(spec
);
205 static void list_available_languages(void)
207 struct script_spec
*s
;
209 fprintf(stderr
, "\n");
210 fprintf(stderr
, "Scripting language extensions (used in "
211 "perf trace -s [spec:]script.[spec]):\n\n");
213 list_for_each_entry(s
, &script_specs
, node
)
214 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
216 fprintf(stderr
, "\n");
219 static int parse_scriptname(const struct option
*opt __used
,
220 const char *str
, int unset __used
)
223 const char *script
, *ext
;
226 if (strcmp(str
, "list") == 0) {
227 list_available_languages();
231 script
= strchr(str
, ':');
234 if (len
>= PATH_MAX
) {
235 fprintf(stderr
, "invalid language specifier");
238 strncpy(spec
, str
, len
);
240 scripting_ops
= script_spec__lookup(spec
);
241 if (!scripting_ops
) {
242 fprintf(stderr
, "invalid language specifier");
248 ext
= strchr(script
, '.');
250 fprintf(stderr
, "invalid script extension");
253 scripting_ops
= script_spec__lookup(++ext
);
254 if (!scripting_ops
) {
255 fprintf(stderr
, "invalid script extension");
260 script_name
= strdup(script
);
265 #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
266 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
268 if (lang_dirent.d_type == DT_DIR && \
269 (strcmp(lang_dirent.d_name, ".")) && \
270 (strcmp(lang_dirent.d_name, "..")))
272 #define for_each_script(lang_dir, script_dirent, script_next) \
273 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
275 if (script_dirent.d_type != DT_DIR)
278 #define RECORD_SUFFIX "-record"
279 #define REPORT_SUFFIX "-report"
282 struct list_head node
;
288 LIST_HEAD(script_descs
);
290 static struct script_desc
*script_desc__new(const char *name
)
292 struct script_desc
*s
= zalloc(sizeof(*s
));
295 s
->name
= strdup(name
);
300 static void script_desc__delete(struct script_desc
*s
)
306 static void script_desc__add(struct script_desc
*s
)
308 list_add_tail(&s
->node
, &script_descs
);
311 static struct script_desc
*script_desc__find(const char *name
)
313 struct script_desc
*s
;
315 list_for_each_entry(s
, &script_descs
, node
)
316 if (strcasecmp(s
->name
, name
) == 0)
321 static struct script_desc
*script_desc__findnew(const char *name
)
323 struct script_desc
*s
= script_desc__find(name
);
328 s
= script_desc__new(name
);
330 goto out_delete_desc
;
337 script_desc__delete(s
);
342 static char *ends_with(char *str
, const char *suffix
)
344 size_t suffix_len
= strlen(suffix
);
347 if (strlen(str
) > suffix_len
) {
348 p
= str
+ strlen(str
) - suffix_len
;
349 if (!strncmp(p
, suffix
, suffix_len
))
356 static char *ltrim(char *str
)
358 int len
= strlen(str
);
360 while (len
&& isspace(*str
)) {
368 static int read_script_info(struct script_desc
*desc
, const char *filename
)
370 char line
[BUFSIZ
], *p
;
373 fp
= fopen(filename
, "r");
377 while (fgets(line
, sizeof(line
), fp
)) {
384 if (strlen(p
) && *p
== '!')
388 if (strlen(p
) && p
[strlen(p
) - 1] == '\n')
389 p
[strlen(p
) - 1] = '\0';
391 if (!strncmp(p
, "description:", strlen("description:"))) {
392 p
+= strlen("description:");
393 desc
->half_liner
= strdup(ltrim(p
));
397 if (!strncmp(p
, "args:", strlen("args:"))) {
398 p
+= strlen("args:");
399 desc
->args
= strdup(ltrim(p
));
409 static int list_available_scripts(const struct option
*opt __used
,
410 const char *s __used
, int unset __used
)
412 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
413 char scripts_path
[MAXPATHLEN
];
414 DIR *scripts_dir
, *lang_dir
;
415 char script_path
[MAXPATHLEN
];
416 char lang_path
[MAXPATHLEN
];
417 struct script_desc
*desc
;
418 char first_half
[BUFSIZ
];
422 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
424 scripts_dir
= opendir(scripts_path
);
428 for_each_lang(scripts_dir
, lang_dirent
, lang_next
) {
429 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
431 lang_dir
= opendir(lang_path
);
435 for_each_script(lang_dir
, script_dirent
, script_next
) {
436 script_root
= strdup(script_dirent
.d_name
);
437 str
= ends_with(script_root
, REPORT_SUFFIX
);
440 desc
= script_desc__findnew(script_root
);
441 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
442 lang_path
, script_dirent
.d_name
);
443 read_script_info(desc
, script_path
);
449 fprintf(stdout
, "List of available trace scripts:\n");
450 list_for_each_entry(desc
, &script_descs
, node
) {
451 sprintf(first_half
, "%s %s", desc
->name
,
452 desc
->args
? desc
->args
: "");
453 fprintf(stdout
, " %-36s %s\n", first_half
,
454 desc
->half_liner
? desc
->half_liner
: "");
460 static char *get_script_path(const char *script_root
, const char *suffix
)
462 struct dirent
*script_next
, *lang_next
, script_dirent
, lang_dirent
;
463 char scripts_path
[MAXPATHLEN
];
464 char script_path
[MAXPATHLEN
];
465 DIR *scripts_dir
, *lang_dir
;
466 char lang_path
[MAXPATHLEN
];
467 char *str
, *__script_root
;
470 snprintf(scripts_path
, MAXPATHLEN
, "%s/scripts", perf_exec_path());
472 scripts_dir
= opendir(scripts_path
);
476 for_each_lang(scripts_dir
, lang_dirent
, lang_next
) {
477 snprintf(lang_path
, MAXPATHLEN
, "%s/%s/bin", scripts_path
,
479 lang_dir
= opendir(lang_path
);
483 for_each_script(lang_dir
, script_dirent
, script_next
) {
484 __script_root
= strdup(script_dirent
.d_name
);
485 str
= ends_with(__script_root
, suffix
);
488 if (strcmp(__script_root
, script_root
))
490 snprintf(script_path
, MAXPATHLEN
, "%s/%s",
491 lang_path
, script_dirent
.d_name
);
492 path
= strdup(script_path
);
503 static const char * const trace_usage
[] = {
504 "perf trace [<options>] <command>",
508 static const struct option options
[] = {
509 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
510 "dump raw trace in ASCII"),
511 OPT_BOOLEAN('v', "verbose", &verbose
,
512 "be more verbose (show symbol address, etc)"),
513 OPT_BOOLEAN('L', "Latency", &latency_format
,
514 "show latency attributes (irqs/preemption disabled, etc)"),
515 OPT_CALLBACK_NOOPT('l', "list", NULL
, NULL
, "list available scripts",
516 list_available_scripts
),
517 OPT_CALLBACK('s', "script", NULL
, "name",
518 "script file name (lang:script name, script name, or *)",
520 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
521 "generate perf-trace.xx script in specified language"),
526 int cmd_trace(int argc
, const char **argv
, const char *prefix __used
)
528 struct perf_session
*session
;
529 const char *suffix
= NULL
;
534 if (argc
>= 2 && strncmp(argv
[1], "rec", strlen("rec")) == 0) {
537 "Please specify a record script\n");
540 suffix
= RECORD_SUFFIX
;
543 if (argc
>= 2 && strncmp(argv
[1], "rep", strlen("rep")) == 0) {
546 "Please specify a report script\n");
549 suffix
= REPORT_SUFFIX
;
553 script_path
= get_script_path(argv
[2], suffix
);
555 fprintf(stderr
, "script not found\n");
559 __argv
= malloc((argc
+ 1) * sizeof(const char *));
560 __argv
[0] = "/bin/sh";
561 __argv
[1] = script_path
;
562 for (i
= 3; i
< argc
; i
++)
563 __argv
[i
- 1] = argv
[i
];
564 __argv
[argc
- 1] = NULL
;
566 execvp("/bin/sh", (char **)__argv
);
572 argc
= parse_options(argc
, argv
, options
, trace_usage
,
573 PARSE_OPT_STOP_AT_NON_OPTION
);
575 if (symbol__init() < 0)
579 session
= perf_session__new(input_name
, O_RDONLY
, 0);
583 if (generate_script_lang
) {
584 struct stat perf_stat
;
586 int input
= open(input_name
, O_RDONLY
);
588 perror("failed to open file");
592 err
= fstat(input
, &perf_stat
);
594 perror("failed to stat file");
598 if (!perf_stat
.st_size
) {
599 fprintf(stderr
, "zero-sized file, nothing to do!\n");
603 scripting_ops
= script_spec__lookup(generate_script_lang
);
604 if (!scripting_ops
) {
605 fprintf(stderr
, "invalid language specifier");
609 perf_header__read(&session
->header
, input
);
610 err
= scripting_ops
->generate_script("perf-trace");
615 err
= scripting_ops
->start_script(script_name
, argc
, argv
);
620 err
= __cmd_trace(session
);
622 perf_session__delete(session
);