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;
16 static u64 nr_unordered;
18 static int default_start_script(const char *script __unused,
20 const char **argv __unused)
25 static int default_stop_script(void)
30 static int default_generate_script(const char *outfile __unused)
35 static struct scripting_ops default_scripting_ops = {
36 .start_script = default_start_script,
37 .stop_script = default_stop_script,
38 .process_event = print_event,
39 .generate_script = default_generate_script,
42 static struct scripting_ops *scripting_ops;
44 static void setup_scripting(void)
46 /* make sure PERF_EXEC_PATH is set for scripts */
47 perf_set_argv_exec_path(perf_exec_path());
49 setup_perl_scripting();
50 setup_python_scripting();
52 scripting_ops = &default_scripting_ops;
55 static int cleanup_scripting(void)
57 pr_debug("\nperf trace script stopped\n");
59 return scripting_ops->stop_script();
62 #include "util/parse-options.h"
65 #include "util/debug.h"
67 #include "util/trace-event.h"
68 #include "util/exec_cmd.h"
70 static char const *input_name = "perf.data";
72 static int process_sample_event(event_t *event, struct perf_session *session)
74 struct sample_data data;
75 struct thread *thread;
77 memset(&data, 0, sizeof(data));
82 event__parse_sample(event, session->sample_type, &data);
84 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
85 data.pid, data.tid, data.ip, data.period);
87 thread = perf_session__findnew(session, event->ip.pid);
89 pr_debug("problem processing %d event, skipping it.\n",
94 if (session->sample_type & PERF_SAMPLE_RAW) {
96 if (data.time < last_timestamp) {
97 pr_err("Samples misordered, previous: %llu "
98 "this: %llu\n", last_timestamp,
102 last_timestamp = data.time;
106 * FIXME: better resolve from pid from the struct trace_entry
107 * field, although it should be the same than this perf
110 scripting_ops->process_event(data.cpu, data.raw_data,
112 data.time, thread->comm);
115 session->hists.stats.total_period += data.period;
119 static struct perf_event_ops event_ops = {
120 .sample = process_sample_event,
121 .comm = event__process_comm,
122 .attr = event__process_attr,
123 .event_type = event__process_event_type,
124 .tracing_data = event__process_tracing_data,
125 .build_id = event__process_build_id,
126 .ordered_samples = true,
129 extern volatile int session_done;
131 static void sig_handler(int sig __unused)
136 static int __cmd_trace(struct perf_session *session)
140 signal(SIGINT, sig_handler);
142 ret = perf_session__process_events(session, &event_ops);
145 pr_err("Misordered timestamps: %llu\n", nr_unordered);
151 struct list_head node;
152 struct scripting_ops *ops;
156 LIST_HEAD(script_specs);
158 static struct script_spec *script_spec__new(const char *spec,
159 struct scripting_ops *ops)
161 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
164 strcpy(s->spec, spec);
171 static void script_spec__delete(struct script_spec *s)
177 static void script_spec__add(struct script_spec *s)
179 list_add_tail(&s->node, &script_specs);
182 static struct script_spec *script_spec__find(const char *spec)
184 struct script_spec *s;
186 list_for_each_entry(s, &script_specs, node)
187 if (strcasecmp(s->spec, spec) == 0)
192 static struct script_spec *script_spec__findnew(const char *spec,
193 struct scripting_ops *ops)
195 struct script_spec *s = script_spec__find(spec);
200 s = script_spec__new(spec, ops);
202 goto out_delete_spec;
209 script_spec__delete(s);
214 int script_spec_register(const char *spec, struct scripting_ops *ops)
216 struct script_spec *s;
218 s = script_spec__find(spec);
222 s = script_spec__findnew(spec, ops);
229 static struct scripting_ops *script_spec__lookup(const char *spec)
231 struct script_spec *s = script_spec__find(spec);
238 static void list_available_languages(void)
240 struct script_spec *s;
242 fprintf(stderr, "\n");
243 fprintf(stderr, "Scripting language extensions (used in "
244 "perf trace -s [spec:]script.[spec]):\n\n");
246 list_for_each_entry(s, &script_specs, node)
247 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
249 fprintf(stderr, "\n");
252 static int parse_scriptname(const struct option *opt __used,
253 const char *str, int unset __used)
256 const char *script, *ext;
259 if (strcmp(str, "lang") == 0) {
260 list_available_languages();
264 script = strchr(str, ':');
267 if (len >= PATH_MAX) {
268 fprintf(stderr, "invalid language specifier");
271 strncpy(spec, str, len);
273 scripting_ops = script_spec__lookup(spec);
274 if (!scripting_ops) {
275 fprintf(stderr, "invalid language specifier");
281 ext = strchr(script, '.');
283 fprintf(stderr, "invalid script extension");
286 scripting_ops = script_spec__lookup(++ext);
287 if (!scripting_ops) {
288 fprintf(stderr, "invalid script extension");
293 script_name = strdup(script);
298 #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
299 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
301 if (lang_dirent.d_type == DT_DIR && \
302 (strcmp(lang_dirent.d_name, ".")) && \
303 (strcmp(lang_dirent.d_name, "..")))
305 #define for_each_script(lang_dir, script_dirent, script_next) \
306 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
308 if (script_dirent.d_type != DT_DIR)
311 #define RECORD_SUFFIX "-record"
312 #define REPORT_SUFFIX "-report"
315 struct list_head node;
321 LIST_HEAD(script_descs);
323 static struct script_desc *script_desc__new(const char *name)
325 struct script_desc *s = zalloc(sizeof(*s));
328 s->name = strdup(name);
333 static void script_desc__delete(struct script_desc *s)
339 static void script_desc__add(struct script_desc *s)
341 list_add_tail(&s->node, &script_descs);
344 static struct script_desc *script_desc__find(const char *name)
346 struct script_desc *s;
348 list_for_each_entry(s, &script_descs, node)
349 if (strcasecmp(s->name, name) == 0)
354 static struct script_desc *script_desc__findnew(const char *name)
356 struct script_desc *s = script_desc__find(name);
361 s = script_desc__new(name);
363 goto out_delete_desc;
370 script_desc__delete(s);
375 static char *ends_with(char *str, const char *suffix)
377 size_t suffix_len = strlen(suffix);
380 if (strlen(str) > suffix_len) {
381 p = str + strlen(str) - suffix_len;
382 if (!strncmp(p, suffix, suffix_len))
389 static char *ltrim(char *str)
391 int len = strlen(str);
393 while (len && isspace(*str)) {
401 static int read_script_info(struct script_desc *desc, const char *filename)
403 char line[BUFSIZ], *p;
406 fp = fopen(filename, "r");
410 while (fgets(line, sizeof(line), fp)) {
417 if (strlen(p) && *p == '!')
421 if (strlen(p) && p[strlen(p) - 1] == '\n')
422 p[strlen(p) - 1] = '\0';
424 if (!strncmp(p, "description:", strlen("description:"))) {
425 p += strlen("description:");
426 desc->half_liner = strdup(ltrim(p));
430 if (!strncmp(p, "args:", strlen("args:"))) {
431 p += strlen("args:");
432 desc->args = strdup(ltrim(p));
442 static int list_available_scripts(const struct option *opt __used,
443 const char *s __used, int unset __used)
445 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
446 char scripts_path[MAXPATHLEN];
447 DIR *scripts_dir, *lang_dir;
448 char script_path[MAXPATHLEN];
449 char lang_path[MAXPATHLEN];
450 struct script_desc *desc;
451 char first_half[BUFSIZ];
455 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
457 scripts_dir = opendir(scripts_path);
461 for_each_lang(scripts_dir, lang_dirent, lang_next) {
462 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
464 lang_dir = opendir(lang_path);
468 for_each_script(lang_dir, script_dirent, script_next) {
469 script_root = strdup(script_dirent.d_name);
470 str = ends_with(script_root, REPORT_SUFFIX);
473 desc = script_desc__findnew(script_root);
474 snprintf(script_path, MAXPATHLEN, "%s/%s",
475 lang_path, script_dirent.d_name);
476 read_script_info(desc, script_path);
482 fprintf(stdout, "List of available trace scripts:\n");
483 list_for_each_entry(desc, &script_descs, node) {
484 sprintf(first_half, "%s %s", desc->name,
485 desc->args ? desc->args : "");
486 fprintf(stdout, " %-36s %s\n", first_half,
487 desc->half_liner ? desc->half_liner : "");
493 static char *get_script_path(const char *script_root, const char *suffix)
495 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
496 char scripts_path[MAXPATHLEN];
497 char script_path[MAXPATHLEN];
498 DIR *scripts_dir, *lang_dir;
499 char lang_path[MAXPATHLEN];
500 char *str, *__script_root;
503 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
505 scripts_dir = opendir(scripts_path);
509 for_each_lang(scripts_dir, lang_dirent, lang_next) {
510 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
512 lang_dir = opendir(lang_path);
516 for_each_script(lang_dir, script_dirent, script_next) {
517 __script_root = strdup(script_dirent.d_name);
518 str = ends_with(__script_root, suffix);
521 if (strcmp(__script_root, script_root))
523 snprintf(script_path, MAXPATHLEN, "%s/%s",
524 lang_path, script_dirent.d_name);
525 path = strdup(script_path);
536 static const char * const trace_usage[] = {
537 "perf trace [<options>] <command>",
541 static const struct option options[] = {
542 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
543 "dump raw trace in ASCII"),
544 OPT_INCR('v', "verbose", &verbose,
545 "be more verbose (show symbol address, etc)"),
546 OPT_BOOLEAN('L', "Latency", &latency_format,
547 "show latency attributes (irqs/preemption disabled, etc)"),
548 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
549 list_available_scripts),
550 OPT_CALLBACK('s', "script", NULL, "name",
551 "script file name (lang:script name, script name, or *)",
553 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
554 "generate perf-trace.xx script in specified language"),
555 OPT_STRING('i', "input", &input_name, "file",
557 OPT_BOOLEAN('d', "debug-ordering", &debug_ordering,
558 "check that samples time ordering is monotonic"),
563 int cmd_trace(int argc, const char **argv, const char *prefix __used)
565 struct perf_session *session;
566 const char *suffix = NULL;
571 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
574 "Please specify a record script\n");
577 suffix = RECORD_SUFFIX;
580 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
583 "Please specify a report script\n");
586 suffix = REPORT_SUFFIX;
589 if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
590 char *record_script_path, *report_script_path;
594 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
595 if (!record_script_path) {
596 fprintf(stderr, "record script not found\n");
600 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
601 if (!report_script_path) {
602 fprintf(stderr, "report script not found\n");
606 if (pipe(live_pipe) < 0) {
607 perror("failed to create pipe");
613 perror("failed to fork");
618 dup2(live_pipe[1], 1);
621 __argv = malloc(5 * sizeof(const char *));
622 __argv[0] = "/bin/sh";
623 __argv[1] = record_script_path;
628 execvp("/bin/sh", (char **)__argv);
632 dup2(live_pipe[0], 0);
635 __argv = malloc((argc + 3) * sizeof(const char *));
636 __argv[0] = "/bin/sh";
637 __argv[1] = report_script_path;
638 for (i = 2; i < argc; i++)
644 execvp("/bin/sh", (char **)__argv);
649 script_path = get_script_path(argv[2], suffix);
651 fprintf(stderr, "script not found\n");
655 __argv = malloc((argc + 1) * sizeof(const char *));
656 __argv[0] = "/bin/sh";
657 __argv[1] = script_path;
658 for (i = 3; i < argc; i++)
659 __argv[i - 1] = argv[i];
660 __argv[argc - 1] = NULL;
662 execvp("/bin/sh", (char **)__argv);
668 argc = parse_options(argc, argv, options, trace_usage,
669 PARSE_OPT_STOP_AT_NON_OPTION);
671 if (symbol__init() < 0)
676 session = perf_session__new(input_name, O_RDONLY, 0, false);
680 if (strcmp(input_name, "-") &&
681 !perf_session__has_traces(session, "record -R"))
684 if (generate_script_lang) {
685 struct stat perf_stat;
687 int input = open(input_name, O_RDONLY);
689 perror("failed to open file");
693 err = fstat(input, &perf_stat);
695 perror("failed to stat file");
699 if (!perf_stat.st_size) {
700 fprintf(stderr, "zero-sized file, nothing to do!\n");
704 scripting_ops = script_spec__lookup(generate_script_lang);
705 if (!scripting_ops) {
706 fprintf(stderr, "invalid language specifier");
710 err = scripting_ops->generate_script("perf-trace");
715 err = scripting_ops->start_script(script_name, argc, argv);
718 pr_debug("perf trace started with script %s\n\n", script_name);
721 err = __cmd_trace(session);
723 perf_session__delete(session);