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 return scripting_ops->stop_script();
59 #include "util/parse-options.h"
62 #include "util/debug.h"
64 #include "util/trace-event.h"
65 #include "util/exec_cmd.h"
67 static char const *input_name = "perf.data";
69 static int process_sample_event(event_t *event, struct perf_session *session)
71 struct sample_data data;
72 struct thread *thread;
74 memset(&data, 0, sizeof(data));
79 event__parse_sample(event, session->sample_type, &data);
81 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
82 data.pid, data.tid, data.ip, 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 if (data.time < last_timestamp) {
94 pr_err("Samples misordered, previous: %llu "
95 "this: %llu\n", last_timestamp,
98 last_timestamp = data.time;
101 * FIXME: better resolve from pid from the struct trace_entry
102 * field, although it should be the same than this perf
105 scripting_ops->process_event(data.cpu, data.raw_data,
107 data.time, thread->comm);
110 session->events_stats.total += data.period;
114 static struct perf_event_ops event_ops = {
115 .sample = process_sample_event,
116 .comm = event__process_comm,
117 .attr = event__process_attr,
118 .event_type = event__process_event_type,
119 .tracing_data = event__process_tracing_data,
120 .build_id = event__process_build_id,
121 .ordered_samples = true,
124 extern volatile int session_done;
126 static void sig_handler(int sig __unused)
131 static int __cmd_trace(struct perf_session *session)
133 signal(SIGINT, sig_handler);
135 return perf_session__process_events(session, &event_ops);
139 struct list_head node;
140 struct scripting_ops *ops;
144 LIST_HEAD(script_specs);
146 static struct script_spec *script_spec__new(const char *spec,
147 struct scripting_ops *ops)
149 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
152 strcpy(s->spec, spec);
159 static void script_spec__delete(struct script_spec *s)
165 static void script_spec__add(struct script_spec *s)
167 list_add_tail(&s->node, &script_specs);
170 static struct script_spec *script_spec__find(const char *spec)
172 struct script_spec *s;
174 list_for_each_entry(s, &script_specs, node)
175 if (strcasecmp(s->spec, spec) == 0)
180 static struct script_spec *script_spec__findnew(const char *spec,
181 struct scripting_ops *ops)
183 struct script_spec *s = script_spec__find(spec);
188 s = script_spec__new(spec, ops);
190 goto out_delete_spec;
197 script_spec__delete(s);
202 int script_spec_register(const char *spec, struct scripting_ops *ops)
204 struct script_spec *s;
206 s = script_spec__find(spec);
210 s = script_spec__findnew(spec, ops);
217 static struct scripting_ops *script_spec__lookup(const char *spec)
219 struct script_spec *s = script_spec__find(spec);
226 static void list_available_languages(void)
228 struct script_spec *s;
230 fprintf(stderr, "\n");
231 fprintf(stderr, "Scripting language extensions (used in "
232 "perf trace -s [spec:]script.[spec]):\n\n");
234 list_for_each_entry(s, &script_specs, node)
235 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
237 fprintf(stderr, "\n");
240 static int parse_scriptname(const struct option *opt __used,
241 const char *str, int unset __used)
244 const char *script, *ext;
247 if (strcmp(str, "lang") == 0) {
248 list_available_languages();
252 script = strchr(str, ':');
255 if (len >= PATH_MAX) {
256 fprintf(stderr, "invalid language specifier");
259 strncpy(spec, str, len);
261 scripting_ops = script_spec__lookup(spec);
262 if (!scripting_ops) {
263 fprintf(stderr, "invalid language specifier");
269 ext = strchr(script, '.');
271 fprintf(stderr, "invalid script extension");
274 scripting_ops = script_spec__lookup(++ext);
275 if (!scripting_ops) {
276 fprintf(stderr, "invalid script extension");
281 script_name = strdup(script);
286 #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
287 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
289 if (lang_dirent.d_type == DT_DIR && \
290 (strcmp(lang_dirent.d_name, ".")) && \
291 (strcmp(lang_dirent.d_name, "..")))
293 #define for_each_script(lang_dir, script_dirent, script_next) \
294 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
296 if (script_dirent.d_type != DT_DIR)
299 #define RECORD_SUFFIX "-record"
300 #define REPORT_SUFFIX "-report"
303 struct list_head node;
309 LIST_HEAD(script_descs);
311 static struct script_desc *script_desc__new(const char *name)
313 struct script_desc *s = zalloc(sizeof(*s));
316 s->name = strdup(name);
321 static void script_desc__delete(struct script_desc *s)
327 static void script_desc__add(struct script_desc *s)
329 list_add_tail(&s->node, &script_descs);
332 static struct script_desc *script_desc__find(const char *name)
334 struct script_desc *s;
336 list_for_each_entry(s, &script_descs, node)
337 if (strcasecmp(s->name, name) == 0)
342 static struct script_desc *script_desc__findnew(const char *name)
344 struct script_desc *s = script_desc__find(name);
349 s = script_desc__new(name);
351 goto out_delete_desc;
358 script_desc__delete(s);
363 static char *ends_with(char *str, const char *suffix)
365 size_t suffix_len = strlen(suffix);
368 if (strlen(str) > suffix_len) {
369 p = str + strlen(str) - suffix_len;
370 if (!strncmp(p, suffix, suffix_len))
377 static char *ltrim(char *str)
379 int len = strlen(str);
381 while (len && isspace(*str)) {
389 static int read_script_info(struct script_desc *desc, const char *filename)
391 char line[BUFSIZ], *p;
394 fp = fopen(filename, "r");
398 while (fgets(line, sizeof(line), fp)) {
405 if (strlen(p) && *p == '!')
409 if (strlen(p) && p[strlen(p) - 1] == '\n')
410 p[strlen(p) - 1] = '\0';
412 if (!strncmp(p, "description:", strlen("description:"))) {
413 p += strlen("description:");
414 desc->half_liner = strdup(ltrim(p));
418 if (!strncmp(p, "args:", strlen("args:"))) {
419 p += strlen("args:");
420 desc->args = strdup(ltrim(p));
430 static int list_available_scripts(const struct option *opt __used,
431 const char *s __used, int unset __used)
433 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
434 char scripts_path[MAXPATHLEN];
435 DIR *scripts_dir, *lang_dir;
436 char script_path[MAXPATHLEN];
437 char lang_path[MAXPATHLEN];
438 struct script_desc *desc;
439 char first_half[BUFSIZ];
443 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
445 scripts_dir = opendir(scripts_path);
449 for_each_lang(scripts_dir, lang_dirent, lang_next) {
450 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
452 lang_dir = opendir(lang_path);
456 for_each_script(lang_dir, script_dirent, script_next) {
457 script_root = strdup(script_dirent.d_name);
458 str = ends_with(script_root, REPORT_SUFFIX);
461 desc = script_desc__findnew(script_root);
462 snprintf(script_path, MAXPATHLEN, "%s/%s",
463 lang_path, script_dirent.d_name);
464 read_script_info(desc, script_path);
470 fprintf(stdout, "List of available trace scripts:\n");
471 list_for_each_entry(desc, &script_descs, node) {
472 sprintf(first_half, "%s %s", desc->name,
473 desc->args ? desc->args : "");
474 fprintf(stdout, " %-36s %s\n", first_half,
475 desc->half_liner ? desc->half_liner : "");
481 static char *get_script_path(const char *script_root, const char *suffix)
483 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
484 char scripts_path[MAXPATHLEN];
485 char script_path[MAXPATHLEN];
486 DIR *scripts_dir, *lang_dir;
487 char lang_path[MAXPATHLEN];
488 char *str, *__script_root;
491 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
493 scripts_dir = opendir(scripts_path);
497 for_each_lang(scripts_dir, lang_dirent, lang_next) {
498 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
500 lang_dir = opendir(lang_path);
504 for_each_script(lang_dir, script_dirent, script_next) {
505 __script_root = strdup(script_dirent.d_name);
506 str = ends_with(__script_root, suffix);
509 if (strcmp(__script_root, script_root))
511 snprintf(script_path, MAXPATHLEN, "%s/%s",
512 lang_path, script_dirent.d_name);
513 path = strdup(script_path);
524 static const char * const trace_usage[] = {
525 "perf trace [<options>] <command>",
529 static const struct option options[] = {
530 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
531 "dump raw trace in ASCII"),
532 OPT_INCR('v', "verbose", &verbose,
533 "be more verbose (show symbol address, etc)"),
534 OPT_BOOLEAN('L', "Latency", &latency_format,
535 "show latency attributes (irqs/preemption disabled, etc)"),
536 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
537 list_available_scripts),
538 OPT_CALLBACK('s', "script", NULL, "name",
539 "script file name (lang:script name, script name, or *)",
541 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
542 "generate perf-trace.xx script in specified language"),
543 OPT_STRING('i', "input", &input_name, "file",
545 OPT_BOOLEAN('d', "debug-ordering", &debug_ordering,
546 "check that samples time ordering is monotonic"),
551 int cmd_trace(int argc, const char **argv, const char *prefix __used)
553 struct perf_session *session;
554 const char *suffix = NULL;
559 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
562 "Please specify a record script\n");
565 suffix = RECORD_SUFFIX;
568 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
571 "Please specify a report script\n");
574 suffix = REPORT_SUFFIX;
577 if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
578 char *record_script_path, *report_script_path;
582 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
583 if (!record_script_path) {
584 fprintf(stderr, "record script not found\n");
588 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
589 if (!report_script_path) {
590 fprintf(stderr, "report script not found\n");
594 if (pipe(live_pipe) < 0) {
595 perror("failed to create pipe");
601 perror("failed to fork");
606 dup2(live_pipe[1], 1);
609 __argv = malloc(5 * sizeof(const char *));
610 __argv[0] = "/bin/sh";
611 __argv[1] = record_script_path;
616 execvp("/bin/sh", (char **)__argv);
620 dup2(live_pipe[0], 0);
623 __argv = malloc((argc + 3) * sizeof(const char *));
624 __argv[0] = "/bin/sh";
625 __argv[1] = report_script_path;
626 for (i = 2; i < argc; i++)
632 execvp("/bin/sh", (char **)__argv);
637 script_path = get_script_path(argv[2], suffix);
639 fprintf(stderr, "script not found\n");
643 __argv = malloc((argc + 1) * sizeof(const char *));
644 __argv[0] = "/bin/sh";
645 __argv[1] = script_path;
646 for (i = 3; i < argc; i++)
647 __argv[i - 1] = argv[i];
648 __argv[argc - 1] = NULL;
650 execvp("/bin/sh", (char **)__argv);
656 argc = parse_options(argc, argv, options, trace_usage,
657 PARSE_OPT_STOP_AT_NON_OPTION);
659 if (symbol__init() < 0)
664 session = perf_session__new(input_name, O_RDONLY, 0, false);
668 if (strcmp(input_name, "-") &&
669 !perf_session__has_traces(session, "record -R"))
672 if (generate_script_lang) {
673 struct stat perf_stat;
675 int input = open(input_name, O_RDONLY);
677 perror("failed to open file");
681 err = fstat(input, &perf_stat);
683 perror("failed to stat file");
687 if (!perf_stat.st_size) {
688 fprintf(stderr, "zero-sized file, nothing to do!\n");
692 scripting_ops = script_spec__lookup(generate_script_lang);
693 if (!scripting_ops) {
694 fprintf(stderr, "invalid language specifier");
698 err = scripting_ops->generate_script("perf-trace");
703 err = scripting_ops->start_script(script_name, argc, argv);
708 err = __cmd_trace(session);
710 perf_session__delete(session);