kvm tools: Add ivshmem device
[linux-2.6/next.git] / tools / perf / builtin-script.c
blob22747de7234b548cd1fadac1c53d9884849df935
1 #include "builtin.h"
3 #include "perf.h"
4 #include "util/cache.h"
5 #include "util/debug.h"
6 #include "util/exec_cmd.h"
7 #include "util/header.h"
8 #include "util/parse-options.h"
9 #include "util/session.h"
10 #include "util/symbol.h"
11 #include "util/thread.h"
12 #include "util/trace-event.h"
13 #include "util/util.h"
14 #include "util/evlist.h"
15 #include "util/evsel.h"
17 static char const *script_name;
18 static char const *generate_script_lang;
19 static bool debug_mode;
20 static u64 last_timestamp;
21 static u64 nr_unordered;
22 extern const struct option record_options[];
23 static bool no_callchain;
25 enum perf_output_field {
26 PERF_OUTPUT_COMM = 1U << 0,
27 PERF_OUTPUT_TID = 1U << 1,
28 PERF_OUTPUT_PID = 1U << 2,
29 PERF_OUTPUT_TIME = 1U << 3,
30 PERF_OUTPUT_CPU = 1U << 4,
31 PERF_OUTPUT_EVNAME = 1U << 5,
32 PERF_OUTPUT_TRACE = 1U << 6,
33 PERF_OUTPUT_SYM = 1U << 7,
36 struct output_option {
37 const char *str;
38 enum perf_output_field field;
39 } all_output_options[] = {
40 {.str = "comm", .field = PERF_OUTPUT_COMM},
41 {.str = "tid", .field = PERF_OUTPUT_TID},
42 {.str = "pid", .field = PERF_OUTPUT_PID},
43 {.str = "time", .field = PERF_OUTPUT_TIME},
44 {.str = "cpu", .field = PERF_OUTPUT_CPU},
45 {.str = "event", .field = PERF_OUTPUT_EVNAME},
46 {.str = "trace", .field = PERF_OUTPUT_TRACE},
47 {.str = "sym", .field = PERF_OUTPUT_SYM},
50 /* default set to maintain compatibility with current format */
51 static struct {
52 bool user_set;
53 bool wildcard_set;
54 u64 fields;
55 u64 invalid_fields;
56 } output[PERF_TYPE_MAX] = {
58 [PERF_TYPE_HARDWARE] = {
59 .user_set = false,
61 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
62 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
63 PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM,
65 .invalid_fields = PERF_OUTPUT_TRACE,
68 [PERF_TYPE_SOFTWARE] = {
69 .user_set = false,
71 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
72 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
73 PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM,
75 .invalid_fields = PERF_OUTPUT_TRACE,
78 [PERF_TYPE_TRACEPOINT] = {
79 .user_set = false,
81 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
82 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
83 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
86 [PERF_TYPE_RAW] = {
87 .user_set = false,
89 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
90 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
91 PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM,
93 .invalid_fields = PERF_OUTPUT_TRACE,
97 static bool output_set_by_user(void)
99 int j;
100 for (j = 0; j < PERF_TYPE_MAX; ++j) {
101 if (output[j].user_set)
102 return true;
104 return false;
107 static const char *output_field2str(enum perf_output_field field)
109 int i, imax = ARRAY_SIZE(all_output_options);
110 const char *str = "";
112 for (i = 0; i < imax; ++i) {
113 if (all_output_options[i].field == field) {
114 str = all_output_options[i].str;
115 break;
118 return str;
121 #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
123 static int perf_event_attr__check_stype(struct perf_event_attr *attr,
124 u64 sample_type, const char *sample_msg,
125 enum perf_output_field field)
127 int type = attr->type;
128 const char *evname;
130 if (attr->sample_type & sample_type)
131 return 0;
133 if (output[type].user_set) {
134 evname = __event_name(attr->type, attr->config);
135 pr_err("Samples for '%s' event do not have %s attribute set. "
136 "Cannot print '%s' field.\n",
137 evname, sample_msg, output_field2str(field));
138 return -1;
141 /* user did not ask for it explicitly so remove from the default list */
142 output[type].fields &= ~field;
143 evname = __event_name(attr->type, attr->config);
144 pr_debug("Samples for '%s' event do not have %s attribute set. "
145 "Skipping '%s' field.\n",
146 evname, sample_msg, output_field2str(field));
148 return 0;
151 static int perf_evsel__check_attr(struct perf_evsel *evsel,
152 struct perf_session *session)
154 struct perf_event_attr *attr = &evsel->attr;
156 if (PRINT_FIELD(TRACE) &&
157 !perf_session__has_traces(session, "record -R"))
158 return -EINVAL;
160 if (PRINT_FIELD(SYM)) {
161 if (perf_event_attr__check_stype(attr, PERF_SAMPLE_IP, "IP",
162 PERF_OUTPUT_SYM))
163 return -EINVAL;
165 if (!no_callchain &&
166 !(attr->sample_type & PERF_SAMPLE_CALLCHAIN))
167 symbol_conf.use_callchain = false;
170 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
171 perf_event_attr__check_stype(attr, PERF_SAMPLE_TID, "TID",
172 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
173 return -EINVAL;
175 if (PRINT_FIELD(TIME) &&
176 perf_event_attr__check_stype(attr, PERF_SAMPLE_TIME, "TIME",
177 PERF_OUTPUT_TIME))
178 return -EINVAL;
180 if (PRINT_FIELD(CPU) &&
181 perf_event_attr__check_stype(attr, PERF_SAMPLE_CPU, "CPU",
182 PERF_OUTPUT_CPU))
183 return -EINVAL;
185 return 0;
189 * verify all user requested events exist and the samples
190 * have the expected data
192 static int perf_session__check_output_opt(struct perf_session *session)
194 int j;
195 struct perf_evsel *evsel;
197 for (j = 0; j < PERF_TYPE_MAX; ++j) {
198 evsel = perf_session__find_first_evtype(session, j);
201 * even if fields is set to 0 (ie., show nothing) event must
202 * exist if user explicitly includes it on the command line
204 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
205 pr_err("%s events do not exist. "
206 "Remove corresponding -f option to proceed.\n",
207 event_type(j));
208 return -1;
211 if (evsel && output[j].fields &&
212 perf_evsel__check_attr(evsel, session))
213 return -1;
216 return 0;
219 static void print_sample_start(struct perf_sample *sample,
220 struct thread *thread,
221 struct perf_event_attr *attr)
223 int type;
224 struct event *event;
225 const char *evname = NULL;
226 unsigned long secs;
227 unsigned long usecs;
228 unsigned long long nsecs;
230 if (PRINT_FIELD(COMM)) {
231 if (latency_format)
232 printf("%8.8s ", thread->comm);
233 else if (PRINT_FIELD(SYM) && symbol_conf.use_callchain)
234 printf("%s ", thread->comm);
235 else
236 printf("%16s ", thread->comm);
239 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
240 printf("%5d/%-5d ", sample->pid, sample->tid);
241 else if (PRINT_FIELD(PID))
242 printf("%5d ", sample->pid);
243 else if (PRINT_FIELD(TID))
244 printf("%5d ", sample->tid);
246 if (PRINT_FIELD(CPU)) {
247 if (latency_format)
248 printf("%3d ", sample->cpu);
249 else
250 printf("[%03d] ", sample->cpu);
253 if (PRINT_FIELD(TIME)) {
254 nsecs = sample->time;
255 secs = nsecs / NSECS_PER_SEC;
256 nsecs -= secs * NSECS_PER_SEC;
257 usecs = nsecs / NSECS_PER_USEC;
258 printf("%5lu.%06lu: ", secs, usecs);
261 if (PRINT_FIELD(EVNAME)) {
262 if (attr->type == PERF_TYPE_TRACEPOINT) {
263 type = trace_parse_common_type(sample->raw_data);
264 event = trace_find_event(type);
265 if (event)
266 evname = event->name;
267 } else
268 evname = __event_name(attr->type, attr->config);
270 printf("%s: ", evname ? evname : "(unknown)");
274 static void process_event(union perf_event *event __unused,
275 struct perf_sample *sample,
276 struct perf_evsel *evsel,
277 struct perf_session *session,
278 struct thread *thread)
280 struct perf_event_attr *attr = &evsel->attr;
282 if (output[attr->type].fields == 0)
283 return;
285 print_sample_start(sample, thread, attr);
287 if (PRINT_FIELD(TRACE))
288 print_trace_event(sample->cpu, sample->raw_data,
289 sample->raw_size);
291 if (PRINT_FIELD(SYM)) {
292 if (!symbol_conf.use_callchain)
293 printf(" ");
294 else
295 printf("\n");
296 perf_session__print_symbols(event, sample, session);
299 printf("\n");
302 static int default_start_script(const char *script __unused,
303 int argc __unused,
304 const char **argv __unused)
306 return 0;
309 static int default_stop_script(void)
311 return 0;
314 static int default_generate_script(const char *outfile __unused)
316 return 0;
319 static struct scripting_ops default_scripting_ops = {
320 .start_script = default_start_script,
321 .stop_script = default_stop_script,
322 .process_event = process_event,
323 .generate_script = default_generate_script,
326 static struct scripting_ops *scripting_ops;
328 static void setup_scripting(void)
330 setup_perl_scripting();
331 setup_python_scripting();
333 scripting_ops = &default_scripting_ops;
336 static int cleanup_scripting(void)
338 pr_debug("\nperf script stopped\n");
340 return scripting_ops->stop_script();
343 static char const *input_name = "perf.data";
345 static int process_sample_event(union perf_event *event,
346 struct perf_sample *sample,
347 struct perf_evsel *evsel,
348 struct perf_session *session)
350 struct thread *thread = perf_session__findnew(session, event->ip.pid);
352 if (thread == NULL) {
353 pr_debug("problem processing %d event, skipping it.\n",
354 event->header.type);
355 return -1;
358 if (debug_mode) {
359 if (sample->time < last_timestamp) {
360 pr_err("Samples misordered, previous: %" PRIu64
361 " this: %" PRIu64 "\n", last_timestamp,
362 sample->time);
363 nr_unordered++;
365 last_timestamp = sample->time;
366 return 0;
368 scripting_ops->process_event(event, sample, evsel, session, thread);
370 session->hists.stats.total_period += sample->period;
371 return 0;
374 static struct perf_event_ops event_ops = {
375 .sample = process_sample_event,
376 .mmap = perf_event__process_mmap,
377 .comm = perf_event__process_comm,
378 .exit = perf_event__process_task,
379 .fork = perf_event__process_task,
380 .attr = perf_event__process_attr,
381 .event_type = perf_event__process_event_type,
382 .tracing_data = perf_event__process_tracing_data,
383 .build_id = perf_event__process_build_id,
384 .ordered_samples = true,
385 .ordering_requires_timestamps = true,
388 extern volatile int session_done;
390 static void sig_handler(int sig __unused)
392 session_done = 1;
395 static int __cmd_script(struct perf_session *session)
397 int ret;
399 signal(SIGINT, sig_handler);
401 ret = perf_session__process_events(session, &event_ops);
403 if (debug_mode)
404 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
406 return ret;
409 struct script_spec {
410 struct list_head node;
411 struct scripting_ops *ops;
412 char spec[0];
415 static LIST_HEAD(script_specs);
417 static struct script_spec *script_spec__new(const char *spec,
418 struct scripting_ops *ops)
420 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
422 if (s != NULL) {
423 strcpy(s->spec, spec);
424 s->ops = ops;
427 return s;
430 static void script_spec__delete(struct script_spec *s)
432 free(s->spec);
433 free(s);
436 static void script_spec__add(struct script_spec *s)
438 list_add_tail(&s->node, &script_specs);
441 static struct script_spec *script_spec__find(const char *spec)
443 struct script_spec *s;
445 list_for_each_entry(s, &script_specs, node)
446 if (strcasecmp(s->spec, spec) == 0)
447 return s;
448 return NULL;
451 static struct script_spec *script_spec__findnew(const char *spec,
452 struct scripting_ops *ops)
454 struct script_spec *s = script_spec__find(spec);
456 if (s)
457 return s;
459 s = script_spec__new(spec, ops);
460 if (!s)
461 goto out_delete_spec;
463 script_spec__add(s);
465 return s;
467 out_delete_spec:
468 script_spec__delete(s);
470 return NULL;
473 int script_spec_register(const char *spec, struct scripting_ops *ops)
475 struct script_spec *s;
477 s = script_spec__find(spec);
478 if (s)
479 return -1;
481 s = script_spec__findnew(spec, ops);
482 if (!s)
483 return -1;
485 return 0;
488 static struct scripting_ops *script_spec__lookup(const char *spec)
490 struct script_spec *s = script_spec__find(spec);
491 if (!s)
492 return NULL;
494 return s->ops;
497 static void list_available_languages(void)
499 struct script_spec *s;
501 fprintf(stderr, "\n");
502 fprintf(stderr, "Scripting language extensions (used in "
503 "perf script -s [spec:]script.[spec]):\n\n");
505 list_for_each_entry(s, &script_specs, node)
506 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
508 fprintf(stderr, "\n");
511 static int parse_scriptname(const struct option *opt __used,
512 const char *str, int unset __used)
514 char spec[PATH_MAX];
515 const char *script, *ext;
516 int len;
518 if (strcmp(str, "lang") == 0) {
519 list_available_languages();
520 exit(0);
523 script = strchr(str, ':');
524 if (script) {
525 len = script - str;
526 if (len >= PATH_MAX) {
527 fprintf(stderr, "invalid language specifier");
528 return -1;
530 strncpy(spec, str, len);
531 spec[len] = '\0';
532 scripting_ops = script_spec__lookup(spec);
533 if (!scripting_ops) {
534 fprintf(stderr, "invalid language specifier");
535 return -1;
537 script++;
538 } else {
539 script = str;
540 ext = strrchr(script, '.');
541 if (!ext) {
542 fprintf(stderr, "invalid script extension");
543 return -1;
545 scripting_ops = script_spec__lookup(++ext);
546 if (!scripting_ops) {
547 fprintf(stderr, "invalid script extension");
548 return -1;
552 script_name = strdup(script);
554 return 0;
557 static int parse_output_fields(const struct option *opt __used,
558 const char *arg, int unset __used)
560 char *tok;
561 int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
562 int j;
563 int rc = 0;
564 char *str = strdup(arg);
565 int type = -1;
567 if (!str)
568 return -ENOMEM;
570 /* first word can state for which event type the user is specifying
571 * the fields. If no type exists, the specified fields apply to all
572 * event types found in the file minus the invalid fields for a type.
574 tok = strchr(str, ':');
575 if (tok) {
576 *tok = '\0';
577 tok++;
578 if (!strcmp(str, "hw"))
579 type = PERF_TYPE_HARDWARE;
580 else if (!strcmp(str, "sw"))
581 type = PERF_TYPE_SOFTWARE;
582 else if (!strcmp(str, "trace"))
583 type = PERF_TYPE_TRACEPOINT;
584 else if (!strcmp(str, "raw"))
585 type = PERF_TYPE_RAW;
586 else {
587 fprintf(stderr, "Invalid event type in field string.\n");
588 return -EINVAL;
591 if (output[type].user_set)
592 pr_warning("Overriding previous field request for %s events.\n",
593 event_type(type));
595 output[type].fields = 0;
596 output[type].user_set = true;
597 output[type].wildcard_set = false;
599 } else {
600 tok = str;
601 if (strlen(str) == 0) {
602 fprintf(stderr,
603 "Cannot set fields to 'none' for all event types.\n");
604 rc = -EINVAL;
605 goto out;
608 if (output_set_by_user())
609 pr_warning("Overriding previous field request for all events.\n");
611 for (j = 0; j < PERF_TYPE_MAX; ++j) {
612 output[j].fields = 0;
613 output[j].user_set = true;
614 output[j].wildcard_set = true;
618 tok = strtok(tok, ",");
619 while (tok) {
620 for (i = 0; i < imax; ++i) {
621 if (strcmp(tok, all_output_options[i].str) == 0)
622 break;
624 if (i == imax) {
625 fprintf(stderr, "Invalid field requested.\n");
626 rc = -EINVAL;
627 goto out;
630 if (type == -1) {
631 /* add user option to all events types for
632 * which it is valid
634 for (j = 0; j < PERF_TYPE_MAX; ++j) {
635 if (output[j].invalid_fields & all_output_options[i].field) {
636 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
637 all_output_options[i].str, event_type(j));
638 } else
639 output[j].fields |= all_output_options[i].field;
641 } else {
642 if (output[type].invalid_fields & all_output_options[i].field) {
643 fprintf(stderr, "\'%s\' not valid for %s events.\n",
644 all_output_options[i].str, event_type(type));
646 rc = -EINVAL;
647 goto out;
649 output[type].fields |= all_output_options[i].field;
652 tok = strtok(NULL, ",");
655 if (type >= 0) {
656 if (output[type].fields == 0) {
657 pr_debug("No fields requested for %s type. "
658 "Events will not be displayed.\n", event_type(type));
662 out:
663 free(str);
664 return rc;
667 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
668 static int is_directory(const char *base_path, const struct dirent *dent)
670 char path[PATH_MAX];
671 struct stat st;
673 sprintf(path, "%s/%s", base_path, dent->d_name);
674 if (stat(path, &st))
675 return 0;
677 return S_ISDIR(st.st_mode);
680 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
681 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
682 lang_next) \
683 if ((lang_dirent.d_type == DT_DIR || \
684 (lang_dirent.d_type == DT_UNKNOWN && \
685 is_directory(scripts_path, &lang_dirent))) && \
686 (strcmp(lang_dirent.d_name, ".")) && \
687 (strcmp(lang_dirent.d_name, "..")))
689 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
690 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
691 script_next) \
692 if (script_dirent.d_type != DT_DIR && \
693 (script_dirent.d_type != DT_UNKNOWN || \
694 !is_directory(lang_path, &script_dirent)))
697 #define RECORD_SUFFIX "-record"
698 #define REPORT_SUFFIX "-report"
700 struct script_desc {
701 struct list_head node;
702 char *name;
703 char *half_liner;
704 char *args;
707 static LIST_HEAD(script_descs);
709 static struct script_desc *script_desc__new(const char *name)
711 struct script_desc *s = zalloc(sizeof(*s));
713 if (s != NULL && name)
714 s->name = strdup(name);
716 return s;
719 static void script_desc__delete(struct script_desc *s)
721 free(s->name);
722 free(s->half_liner);
723 free(s->args);
724 free(s);
727 static void script_desc__add(struct script_desc *s)
729 list_add_tail(&s->node, &script_descs);
732 static struct script_desc *script_desc__find(const char *name)
734 struct script_desc *s;
736 list_for_each_entry(s, &script_descs, node)
737 if (strcasecmp(s->name, name) == 0)
738 return s;
739 return NULL;
742 static struct script_desc *script_desc__findnew(const char *name)
744 struct script_desc *s = script_desc__find(name);
746 if (s)
747 return s;
749 s = script_desc__new(name);
750 if (!s)
751 goto out_delete_desc;
753 script_desc__add(s);
755 return s;
757 out_delete_desc:
758 script_desc__delete(s);
760 return NULL;
763 static const char *ends_with(const char *str, const char *suffix)
765 size_t suffix_len = strlen(suffix);
766 const char *p = str;
768 if (strlen(str) > suffix_len) {
769 p = str + strlen(str) - suffix_len;
770 if (!strncmp(p, suffix, suffix_len))
771 return p;
774 return NULL;
777 static char *ltrim(char *str)
779 int len = strlen(str);
781 while (len && isspace(*str)) {
782 len--;
783 str++;
786 return str;
789 static int read_script_info(struct script_desc *desc, const char *filename)
791 char line[BUFSIZ], *p;
792 FILE *fp;
794 fp = fopen(filename, "r");
795 if (!fp)
796 return -1;
798 while (fgets(line, sizeof(line), fp)) {
799 p = ltrim(line);
800 if (strlen(p) == 0)
801 continue;
802 if (*p != '#')
803 continue;
804 p++;
805 if (strlen(p) && *p == '!')
806 continue;
808 p = ltrim(p);
809 if (strlen(p) && p[strlen(p) - 1] == '\n')
810 p[strlen(p) - 1] = '\0';
812 if (!strncmp(p, "description:", strlen("description:"))) {
813 p += strlen("description:");
814 desc->half_liner = strdup(ltrim(p));
815 continue;
818 if (!strncmp(p, "args:", strlen("args:"))) {
819 p += strlen("args:");
820 desc->args = strdup(ltrim(p));
821 continue;
825 fclose(fp);
827 return 0;
830 static int list_available_scripts(const struct option *opt __used,
831 const char *s __used, int unset __used)
833 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
834 char scripts_path[MAXPATHLEN];
835 DIR *scripts_dir, *lang_dir;
836 char script_path[MAXPATHLEN];
837 char lang_path[MAXPATHLEN];
838 struct script_desc *desc;
839 char first_half[BUFSIZ];
840 char *script_root;
841 char *str;
843 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
845 scripts_dir = opendir(scripts_path);
846 if (!scripts_dir)
847 return -1;
849 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
850 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
851 lang_dirent.d_name);
852 lang_dir = opendir(lang_path);
853 if (!lang_dir)
854 continue;
856 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
857 script_root = strdup(script_dirent.d_name);
858 str = (char *)ends_with(script_root, REPORT_SUFFIX);
859 if (str) {
860 *str = '\0';
861 desc = script_desc__findnew(script_root);
862 snprintf(script_path, MAXPATHLEN, "%s/%s",
863 lang_path, script_dirent.d_name);
864 read_script_info(desc, script_path);
866 free(script_root);
870 fprintf(stdout, "List of available trace scripts:\n");
871 list_for_each_entry(desc, &script_descs, node) {
872 sprintf(first_half, "%s %s", desc->name,
873 desc->args ? desc->args : "");
874 fprintf(stdout, " %-36s %s\n", first_half,
875 desc->half_liner ? desc->half_liner : "");
878 exit(0);
881 static char *get_script_path(const char *script_root, const char *suffix)
883 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
884 char scripts_path[MAXPATHLEN];
885 char script_path[MAXPATHLEN];
886 DIR *scripts_dir, *lang_dir;
887 char lang_path[MAXPATHLEN];
888 char *str, *__script_root;
889 char *path = NULL;
891 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
893 scripts_dir = opendir(scripts_path);
894 if (!scripts_dir)
895 return NULL;
897 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
898 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
899 lang_dirent.d_name);
900 lang_dir = opendir(lang_path);
901 if (!lang_dir)
902 continue;
904 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
905 __script_root = strdup(script_dirent.d_name);
906 str = (char *)ends_with(__script_root, suffix);
907 if (str) {
908 *str = '\0';
909 if (strcmp(__script_root, script_root))
910 continue;
911 snprintf(script_path, MAXPATHLEN, "%s/%s",
912 lang_path, script_dirent.d_name);
913 path = strdup(script_path);
914 free(__script_root);
915 break;
917 free(__script_root);
921 return path;
924 static bool is_top_script(const char *script_path)
926 return ends_with(script_path, "top") == NULL ? false : true;
929 static int has_required_arg(char *script_path)
931 struct script_desc *desc;
932 int n_args = 0;
933 char *p;
935 desc = script_desc__new(NULL);
937 if (read_script_info(desc, script_path))
938 goto out;
940 if (!desc->args)
941 goto out;
943 for (p = desc->args; *p; p++)
944 if (*p == '<')
945 n_args++;
946 out:
947 script_desc__delete(desc);
949 return n_args;
952 static const char * const script_usage[] = {
953 "perf script [<options>]",
954 "perf script [<options>] record <script> [<record-options>] <command>",
955 "perf script [<options>] report <script> [script-args]",
956 "perf script [<options>] <script> [<record-options>] <command>",
957 "perf script [<options>] <top-script> [script-args]",
958 NULL
961 static const struct option options[] = {
962 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
963 "dump raw trace in ASCII"),
964 OPT_INCR('v', "verbose", &verbose,
965 "be more verbose (show symbol address, etc)"),
966 OPT_BOOLEAN('L', "Latency", &latency_format,
967 "show latency attributes (irqs/preemption disabled, etc)"),
968 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
969 list_available_scripts),
970 OPT_CALLBACK('s', "script", NULL, "name",
971 "script file name (lang:script name, script name, or *)",
972 parse_scriptname),
973 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
974 "generate perf-script.xx script in specified language"),
975 OPT_STRING('i', "input", &input_name, "file",
976 "input file name"),
977 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
978 "do various checks like samples ordering and lost events"),
979 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
980 "file", "vmlinux pathname"),
981 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
982 "file", "kallsyms pathname"),
983 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
984 "When printing symbols do not display call chain"),
985 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
986 "Look for files with symbols relative to this directory"),
987 OPT_CALLBACK('f', "fields", NULL, "str",
988 "comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace,raw. Fields: comm,tid,pid,time,cpu,event,trace,sym",
989 parse_output_fields),
991 OPT_END()
994 static bool have_cmd(int argc, const char **argv)
996 char **__argv = malloc(sizeof(const char *) * argc);
998 if (!__argv)
999 die("malloc");
1000 memcpy(__argv, argv, sizeof(const char *) * argc);
1001 argc = parse_options(argc, (const char **)__argv, record_options,
1002 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1003 free(__argv);
1005 return argc != 0;
1008 int cmd_script(int argc, const char **argv, const char *prefix __used)
1010 char *rec_script_path = NULL;
1011 char *rep_script_path = NULL;
1012 struct perf_session *session;
1013 char *script_path = NULL;
1014 const char **__argv;
1015 bool system_wide;
1016 int i, j, err;
1018 setup_scripting();
1020 argc = parse_options(argc, argv, options, script_usage,
1021 PARSE_OPT_STOP_AT_NON_OPTION);
1023 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1024 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1025 if (!rec_script_path)
1026 return cmd_record(argc, argv, NULL);
1029 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1030 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1031 if (!rep_script_path) {
1032 fprintf(stderr,
1033 "Please specify a valid report script"
1034 "(see 'perf script -l' for listing)\n");
1035 return -1;
1039 /* make sure PERF_EXEC_PATH is set for scripts */
1040 perf_set_argv_exec_path(perf_exec_path());
1042 if (argc && !script_name && !rec_script_path && !rep_script_path) {
1043 int live_pipe[2];
1044 int rep_args;
1045 pid_t pid;
1047 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1048 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1050 if (!rec_script_path && !rep_script_path) {
1051 fprintf(stderr, " Couldn't find script %s\n\n See perf"
1052 " script -l for available scripts.\n", argv[0]);
1053 usage_with_options(script_usage, options);
1056 if (is_top_script(argv[0])) {
1057 rep_args = argc - 1;
1058 } else {
1059 int rec_args;
1061 rep_args = has_required_arg(rep_script_path);
1062 rec_args = (argc - 1) - rep_args;
1063 if (rec_args < 0) {
1064 fprintf(stderr, " %s script requires options."
1065 "\n\n See perf script -l for available "
1066 "scripts and options.\n", argv[0]);
1067 usage_with_options(script_usage, options);
1071 if (pipe(live_pipe) < 0) {
1072 perror("failed to create pipe");
1073 exit(-1);
1076 pid = fork();
1077 if (pid < 0) {
1078 perror("failed to fork");
1079 exit(-1);
1082 if (!pid) {
1083 system_wide = true;
1084 j = 0;
1086 dup2(live_pipe[1], 1);
1087 close(live_pipe[0]);
1089 if (!is_top_script(argv[0]))
1090 system_wide = !have_cmd(argc - rep_args,
1091 &argv[rep_args]);
1093 __argv = malloc((argc + 6) * sizeof(const char *));
1094 if (!__argv)
1095 die("malloc");
1097 __argv[j++] = "/bin/sh";
1098 __argv[j++] = rec_script_path;
1099 if (system_wide)
1100 __argv[j++] = "-a";
1101 __argv[j++] = "-q";
1102 __argv[j++] = "-o";
1103 __argv[j++] = "-";
1104 for (i = rep_args + 1; i < argc; i++)
1105 __argv[j++] = argv[i];
1106 __argv[j++] = NULL;
1108 execvp("/bin/sh", (char **)__argv);
1109 free(__argv);
1110 exit(-1);
1113 dup2(live_pipe[0], 0);
1114 close(live_pipe[1]);
1116 __argv = malloc((argc + 4) * sizeof(const char *));
1117 if (!__argv)
1118 die("malloc");
1119 j = 0;
1120 __argv[j++] = "/bin/sh";
1121 __argv[j++] = rep_script_path;
1122 for (i = 1; i < rep_args + 1; i++)
1123 __argv[j++] = argv[i];
1124 __argv[j++] = "-i";
1125 __argv[j++] = "-";
1126 __argv[j++] = NULL;
1128 execvp("/bin/sh", (char **)__argv);
1129 free(__argv);
1130 exit(-1);
1133 if (rec_script_path)
1134 script_path = rec_script_path;
1135 if (rep_script_path)
1136 script_path = rep_script_path;
1138 if (script_path) {
1139 system_wide = false;
1140 j = 0;
1142 if (rec_script_path)
1143 system_wide = !have_cmd(argc - 1, &argv[1]);
1145 __argv = malloc((argc + 2) * sizeof(const char *));
1146 if (!__argv)
1147 die("malloc");
1148 __argv[j++] = "/bin/sh";
1149 __argv[j++] = script_path;
1150 if (system_wide)
1151 __argv[j++] = "-a";
1152 for (i = 2; i < argc; i++)
1153 __argv[j++] = argv[i];
1154 __argv[j++] = NULL;
1156 execvp("/bin/sh", (char **)__argv);
1157 free(__argv);
1158 exit(-1);
1161 if (symbol__init() < 0)
1162 return -1;
1163 if (!script_name)
1164 setup_pager();
1166 session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
1167 if (session == NULL)
1168 return -ENOMEM;
1170 if (!no_callchain)
1171 symbol_conf.use_callchain = true;
1172 else
1173 symbol_conf.use_callchain = false;
1175 if (generate_script_lang) {
1176 struct stat perf_stat;
1177 int input;
1179 if (output_set_by_user()) {
1180 fprintf(stderr,
1181 "custom fields not supported for generated scripts");
1182 return -1;
1185 input = open(input_name, O_RDONLY);
1186 if (input < 0) {
1187 perror("failed to open file");
1188 exit(-1);
1191 err = fstat(input, &perf_stat);
1192 if (err < 0) {
1193 perror("failed to stat file");
1194 exit(-1);
1197 if (!perf_stat.st_size) {
1198 fprintf(stderr, "zero-sized file, nothing to do!\n");
1199 exit(0);
1202 scripting_ops = script_spec__lookup(generate_script_lang);
1203 if (!scripting_ops) {
1204 fprintf(stderr, "invalid language specifier");
1205 return -1;
1208 err = scripting_ops->generate_script("perf-script");
1209 goto out;
1212 if (script_name) {
1213 err = scripting_ops->start_script(script_name, argc, argv);
1214 if (err)
1215 goto out;
1216 pr_debug("perf script started with script %s\n\n", script_name);
1220 err = perf_session__check_output_opt(session);
1221 if (err < 0)
1222 goto out;
1224 err = __cmd_script(session);
1226 perf_session__delete(session);
1227 cleanup_scripting();
1228 out:
1229 return err;