perf script: If type not given fields apply to all event types
[linux-2.6-block.git] / tools / perf / builtin-script.c
CommitLineData
5f9c39dc
FW
1#include "builtin.h"
2
b7eead86 3#include "perf.h"
5f9c39dc 4#include "util/cache.h"
b7eead86
AG
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"
5f9c39dc
FW
10#include "util/symbol.h"
11#include "util/thread.h"
cf72344d 12#include "util/trace-event.h"
34c86ea9 13#include "util/parse-options.h"
b7eead86 14#include "util/util.h"
1424dc96
DA
15#include "util/evlist.h"
16#include "util/evsel.h"
5f9c39dc 17
956ffd02
TZ
18static char const *script_name;
19static char const *generate_script_lang;
ffabd99e 20static bool debug_mode;
e1889d75 21static u64 last_timestamp;
6fcf7ddb 22static u64 nr_unordered;
34c86ea9 23extern const struct option record_options[];
c0230b2b 24static bool no_callchain;
956ffd02 25
745f43e3
DA
26enum perf_output_field {
27 PERF_OUTPUT_COMM = 1U << 0,
28 PERF_OUTPUT_TID = 1U << 1,
29 PERF_OUTPUT_PID = 1U << 2,
30 PERF_OUTPUT_TIME = 1U << 3,
31 PERF_OUTPUT_CPU = 1U << 4,
32 PERF_OUTPUT_EVNAME = 1U << 5,
33 PERF_OUTPUT_TRACE = 1U << 6,
c0230b2b 34 PERF_OUTPUT_SYM = 1U << 7,
745f43e3
DA
35};
36
37struct output_option {
38 const char *str;
39 enum perf_output_field field;
40} all_output_options[] = {
41 {.str = "comm", .field = PERF_OUTPUT_COMM},
42 {.str = "tid", .field = PERF_OUTPUT_TID},
43 {.str = "pid", .field = PERF_OUTPUT_PID},
44 {.str = "time", .field = PERF_OUTPUT_TIME},
45 {.str = "cpu", .field = PERF_OUTPUT_CPU},
46 {.str = "event", .field = PERF_OUTPUT_EVNAME},
47 {.str = "trace", .field = PERF_OUTPUT_TRACE},
c0230b2b 48 {.str = "sym", .field = PERF_OUTPUT_SYM},
745f43e3
DA
49};
50
51/* default set to maintain compatibility with current format */
2c9e45f7
DA
52static struct {
53 bool user_set;
54 u64 fields;
55 u64 invalid_fields;
56} output[PERF_TYPE_MAX] = {
57
58 [PERF_TYPE_HARDWARE] = {
59 .user_set = false,
60
61 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
62 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
63 PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM,
64
65 .invalid_fields = PERF_OUTPUT_TRACE,
66 },
67
68 [PERF_TYPE_SOFTWARE] = {
69 .user_set = false,
70
71 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
72 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
73 PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM,
74
75 .invalid_fields = PERF_OUTPUT_TRACE,
76 },
77
78 [PERF_TYPE_TRACEPOINT] = {
79 .user_set = false,
80
81 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
82 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
83 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
84 },
1424dc96 85};
745f43e3 86
2c9e45f7
DA
87static bool output_set_by_user(void)
88{
89 int j;
90 for (j = 0; j < PERF_TYPE_MAX; ++j) {
91 if (output[j].user_set)
92 return true;
93 }
94 return false;
95}
745f43e3 96
2c9e45f7 97#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
1424dc96
DA
98
99static int perf_session__check_attr(struct perf_session *session,
100 struct perf_event_attr *attr)
101{
102 if (PRINT_FIELD(TRACE) &&
103 !perf_session__has_traces(session, "record -R"))
104 return -EINVAL;
105
106 if (PRINT_FIELD(SYM)) {
107 if (!(session->sample_type & PERF_SAMPLE_IP)) {
108 pr_err("Samples do not contain IP data.\n");
109 return -EINVAL;
110 }
111 if (!no_callchain &&
112 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
113 symbol_conf.use_callchain = false;
114 }
115
116 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
117 !(session->sample_type & PERF_SAMPLE_TID)) {
118 pr_err("Samples do not contain TID/PID data.\n");
119 return -EINVAL;
120 }
121
122 if (PRINT_FIELD(TIME) &&
123 !(session->sample_type & PERF_SAMPLE_TIME)) {
124 pr_err("Samples do not contain timestamps.\n");
125 return -EINVAL;
126 }
127
128 if (PRINT_FIELD(CPU) &&
129 !(session->sample_type & PERF_SAMPLE_CPU)) {
130 pr_err("Samples do not contain cpu.\n");
131 return -EINVAL;
132 }
133
134 return 0;
135}
745f43e3 136
c70c94b4 137static void print_sample_start(struct perf_sample *sample,
1424dc96
DA
138 struct thread *thread,
139 struct perf_event_attr *attr)
c70c94b4
DA
140{
141 int type;
142 struct event *event;
143 const char *evname = NULL;
144 unsigned long secs;
145 unsigned long usecs;
745f43e3
DA
146 unsigned long long nsecs;
147
148 if (PRINT_FIELD(COMM)) {
149 if (latency_format)
150 printf("%8.8s ", thread->comm);
c0230b2b
DA
151 else if (PRINT_FIELD(SYM) && symbol_conf.use_callchain)
152 printf("%s ", thread->comm);
745f43e3
DA
153 else
154 printf("%16s ", thread->comm);
155 }
c70c94b4 156
745f43e3
DA
157 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
158 printf("%5d/%-5d ", sample->pid, sample->tid);
159 else if (PRINT_FIELD(PID))
160 printf("%5d ", sample->pid);
161 else if (PRINT_FIELD(TID))
162 printf("%5d ", sample->tid);
163
164 if (PRINT_FIELD(CPU)) {
165 if (latency_format)
166 printf("%3d ", sample->cpu);
167 else
168 printf("[%03d] ", sample->cpu);
169 }
c70c94b4 170
745f43e3
DA
171 if (PRINT_FIELD(TIME)) {
172 nsecs = sample->time;
173 secs = nsecs / NSECS_PER_SEC;
174 nsecs -= secs * NSECS_PER_SEC;
175 usecs = nsecs / NSECS_PER_USEC;
176 printf("%5lu.%06lu: ", secs, usecs);
177 }
c70c94b4 178
745f43e3 179 if (PRINT_FIELD(EVNAME)) {
1424dc96
DA
180 if (attr->type == PERF_TYPE_TRACEPOINT) {
181 type = trace_parse_common_type(sample->raw_data);
182 event = trace_find_event(type);
183 if (event)
184 evname = event->name;
185 } else
186 evname = __event_name(attr->type, attr->config);
c70c94b4 187
745f43e3
DA
188 printf("%s: ", evname ? evname : "(unknown)");
189 }
c70c94b4
DA
190}
191
be6d842a
DA
192static void process_event(union perf_event *event __unused,
193 struct perf_sample *sample,
9e69c210 194 struct perf_evsel *evsel,
1424dc96 195 struct perf_session *session,
be6d842a
DA
196 struct thread *thread)
197{
9e69c210 198 struct perf_event_attr *attr = &evsel->attr;
1424dc96 199
2c9e45f7 200 if (output[attr->type].fields == 0)
1424dc96
DA
201 return;
202
203 if (perf_session__check_attr(session, attr) < 0)
204 return;
205
206 print_sample_start(sample, thread, attr);
745f43e3
DA
207
208 if (PRINT_FIELD(TRACE))
209 print_trace_event(sample->cpu, sample->raw_data,
210 sample->raw_size);
211
c0230b2b
DA
212 if (PRINT_FIELD(SYM)) {
213 if (!symbol_conf.use_callchain)
214 printf(" ");
215 else
216 printf("\n");
217 perf_session__print_symbols(event, sample, session);
218 }
219
c70c94b4 220 printf("\n");
be6d842a
DA
221}
222
586bc5cc
TZ
223static int default_start_script(const char *script __unused,
224 int argc __unused,
225 const char **argv __unused)
956ffd02
TZ
226{
227 return 0;
228}
229
230static int default_stop_script(void)
231{
232 return 0;
233}
234
586bc5cc 235static int default_generate_script(const char *outfile __unused)
956ffd02
TZ
236{
237 return 0;
238}
239
240static struct scripting_ops default_scripting_ops = {
241 .start_script = default_start_script,
242 .stop_script = default_stop_script,
be6d842a 243 .process_event = process_event,
956ffd02
TZ
244 .generate_script = default_generate_script,
245};
246
247static struct scripting_ops *scripting_ops;
248
249static void setup_scripting(void)
250{
16c632de 251 setup_perl_scripting();
7e4b21b8 252 setup_python_scripting();
16c632de 253
956ffd02
TZ
254 scripting_ops = &default_scripting_ops;
255}
256
257static int cleanup_scripting(void)
258{
133dc4c3 259 pr_debug("\nperf script stopped\n");
3824a4e8 260
956ffd02
TZ
261 return scripting_ops->stop_script();
262}
263
956ffd02 264static char const *input_name = "perf.data";
5f9c39dc 265
8115d60c
ACM
266static int process_sample_event(union perf_event *event,
267 struct perf_sample *sample,
9e69c210 268 struct perf_evsel *evsel,
640c03ce 269 struct perf_session *session)
5f9c39dc 270{
640c03ce 271 struct thread *thread = perf_session__findnew(session, event->ip.pid);
6ddf259d 272
5f9c39dc 273 if (thread == NULL) {
6beba7ad
ACM
274 pr_debug("problem processing %d event, skipping it.\n",
275 event->header.type);
5f9c39dc
FW
276 return -1;
277 }
278
1424dc96
DA
279 if (debug_mode) {
280 if (sample->time < last_timestamp) {
281 pr_err("Samples misordered, previous: %" PRIu64
282 " this: %" PRIu64 "\n", last_timestamp,
283 sample->time);
284 nr_unordered++;
e1889d75 285 }
1424dc96
DA
286 last_timestamp = sample->time;
287 return 0;
5f9c39dc 288 }
9e69c210 289 scripting_ops->process_event(event, sample, evsel, session, thread);
5f9c39dc 290
640c03ce 291 session->hists.stats.total_period += sample->period;
5f9c39dc
FW
292 return 0;
293}
294
301a0b02 295static struct perf_event_ops event_ops = {
8115d60c 296 .sample = process_sample_event,
c0230b2b 297 .mmap = perf_event__process_mmap,
8115d60c 298 .comm = perf_event__process_comm,
c0230b2b
DA
299 .exit = perf_event__process_task,
300 .fork = perf_event__process_task,
8115d60c
ACM
301 .attr = perf_event__process_attr,
302 .event_type = perf_event__process_event_type,
303 .tracing_data = perf_event__process_tracing_data,
304 .build_id = perf_event__process_build_id,
e0a808c6 305 .ordered_samples = true,
8115d60c 306 .ordering_requires_timestamps = true,
016e92fb
FW
307};
308
c239da3b
TZ
309extern volatile int session_done;
310
311static void sig_handler(int sig __unused)
312{
313 session_done = 1;
314}
315
133dc4c3 316static int __cmd_script(struct perf_session *session)
5f9c39dc 317{
6fcf7ddb
FW
318 int ret;
319
c239da3b
TZ
320 signal(SIGINT, sig_handler);
321
6fcf7ddb
FW
322 ret = perf_session__process_events(session, &event_ops);
323
6d8afb56 324 if (debug_mode)
9486aa38 325 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
6fcf7ddb
FW
326
327 return ret;
5f9c39dc
FW
328}
329
956ffd02
TZ
330struct script_spec {
331 struct list_head node;
332 struct scripting_ops *ops;
333 char spec[0];
334};
335
eccdfe2d 336static LIST_HEAD(script_specs);
956ffd02
TZ
337
338static struct script_spec *script_spec__new(const char *spec,
339 struct scripting_ops *ops)
340{
341 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
342
343 if (s != NULL) {
344 strcpy(s->spec, spec);
345 s->ops = ops;
346 }
347
348 return s;
349}
350
351static void script_spec__delete(struct script_spec *s)
352{
353 free(s->spec);
354 free(s);
355}
356
357static void script_spec__add(struct script_spec *s)
358{
359 list_add_tail(&s->node, &script_specs);
360}
361
362static struct script_spec *script_spec__find(const char *spec)
363{
364 struct script_spec *s;
365
366 list_for_each_entry(s, &script_specs, node)
367 if (strcasecmp(s->spec, spec) == 0)
368 return s;
369 return NULL;
370}
371
372static struct script_spec *script_spec__findnew(const char *spec,
373 struct scripting_ops *ops)
374{
375 struct script_spec *s = script_spec__find(spec);
376
377 if (s)
378 return s;
379
380 s = script_spec__new(spec, ops);
381 if (!s)
382 goto out_delete_spec;
383
384 script_spec__add(s);
385
386 return s;
387
388out_delete_spec:
389 script_spec__delete(s);
390
391 return NULL;
392}
393
394int script_spec_register(const char *spec, struct scripting_ops *ops)
395{
396 struct script_spec *s;
397
398 s = script_spec__find(spec);
399 if (s)
400 return -1;
401
402 s = script_spec__findnew(spec, ops);
403 if (!s)
404 return -1;
405
406 return 0;
407}
408
409static struct scripting_ops *script_spec__lookup(const char *spec)
410{
411 struct script_spec *s = script_spec__find(spec);
412 if (!s)
413 return NULL;
414
415 return s->ops;
416}
417
418static void list_available_languages(void)
419{
420 struct script_spec *s;
421
422 fprintf(stderr, "\n");
423 fprintf(stderr, "Scripting language extensions (used in "
133dc4c3 424 "perf script -s [spec:]script.[spec]):\n\n");
956ffd02
TZ
425
426 list_for_each_entry(s, &script_specs, node)
427 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
428
429 fprintf(stderr, "\n");
430}
431
432static int parse_scriptname(const struct option *opt __used,
433 const char *str, int unset __used)
434{
435 char spec[PATH_MAX];
436 const char *script, *ext;
437 int len;
438
f526d68b 439 if (strcmp(str, "lang") == 0) {
956ffd02 440 list_available_languages();
f526d68b 441 exit(0);
956ffd02
TZ
442 }
443
444 script = strchr(str, ':');
445 if (script) {
446 len = script - str;
447 if (len >= PATH_MAX) {
448 fprintf(stderr, "invalid language specifier");
449 return -1;
450 }
451 strncpy(spec, str, len);
452 spec[len] = '\0';
453 scripting_ops = script_spec__lookup(spec);
454 if (!scripting_ops) {
455 fprintf(stderr, "invalid language specifier");
456 return -1;
457 }
458 script++;
459 } else {
460 script = str;
d1e95bb5 461 ext = strrchr(script, '.');
956ffd02
TZ
462 if (!ext) {
463 fprintf(stderr, "invalid script extension");
464 return -1;
465 }
466 scripting_ops = script_spec__lookup(++ext);
467 if (!scripting_ops) {
468 fprintf(stderr, "invalid script extension");
469 return -1;
470 }
471 }
472
473 script_name = strdup(script);
474
475 return 0;
476}
477
745f43e3
DA
478static int parse_output_fields(const struct option *opt __used,
479 const char *arg, int unset __used)
480{
481 char *tok;
482 int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
2c9e45f7 483 int j;
745f43e3
DA
484 int rc = 0;
485 char *str = strdup(arg);
1424dc96 486 int type = -1;
745f43e3
DA
487
488 if (!str)
489 return -ENOMEM;
490
2c9e45f7
DA
491 /* first word can state for which event type the user is specifying
492 * the fields. If no type exists, the specified fields apply to all
493 * event types found in the file minus the invalid fields for a type.
1424dc96 494 */
2c9e45f7
DA
495 tok = strchr(str, ':');
496 if (tok) {
497 *tok = '\0';
498 tok++;
499 if (!strcmp(str, "hw"))
500 type = PERF_TYPE_HARDWARE;
501 else if (!strcmp(str, "sw"))
502 type = PERF_TYPE_SOFTWARE;
503 else if (!strcmp(str, "trace"))
504 type = PERF_TYPE_TRACEPOINT;
505 else {
506 fprintf(stderr, "Invalid event type in field string.\n");
507 return -EINVAL;
508 }
509
510 if (output[type].user_set)
511 pr_warning("Overriding previous field request for %s events.\n",
512 event_type(type));
513
514 output[type].fields = 0;
515 output[type].user_set = true;
516
517 } else {
518 tok = str;
519 if (strlen(str) == 0) {
520 fprintf(stderr,
521 "Cannot set fields to 'none' for all event types.\n");
522 rc = -EINVAL;
523 goto out;
524 }
525
526 if (output_set_by_user())
527 pr_warning("Overriding previous field request for all events.\n");
528
529 for (j = 0; j < PERF_TYPE_MAX; ++j) {
530 output[j].fields = 0;
531 output[j].user_set = true;
532 }
745f43e3
DA
533 }
534
2c9e45f7
DA
535 tok = strtok(tok, ",");
536 while (tok) {
745f43e3 537 for (i = 0; i < imax; ++i) {
2c9e45f7 538 if (strcmp(tok, all_output_options[i].str) == 0)
745f43e3 539 break;
745f43e3
DA
540 }
541 if (i == imax) {
2c9e45f7 542 fprintf(stderr, "Invalid field requested.\n");
745f43e3 543 rc = -EINVAL;
2c9e45f7 544 goto out;
745f43e3
DA
545 }
546
2c9e45f7
DA
547 if (type == -1) {
548 /* add user option to all events types for
549 * which it is valid
550 */
551 for (j = 0; j < PERF_TYPE_MAX; ++j) {
552 if (output[j].invalid_fields & all_output_options[i].field) {
553 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
554 all_output_options[i].str, event_type(j));
555 } else
556 output[j].fields |= all_output_options[i].field;
557 }
558 } else {
559 if (output[type].invalid_fields & all_output_options[i].field) {
560 fprintf(stderr, "\'%s\' not valid for %s events.\n",
561 all_output_options[i].str, event_type(type));
562
563 rc = -EINVAL;
564 goto out;
565 }
566 output[type].fields |= all_output_options[i].field;
567 }
568
569 tok = strtok(NULL, ",");
745f43e3
DA
570 }
571
2c9e45f7
DA
572 if (type >= 0) {
573 if (output[type].fields == 0) {
574 pr_debug("No fields requested for %s type. "
575 "Events will not be displayed.\n", event_type(type));
576 }
577 }
745f43e3 578
2c9e45f7 579out:
745f43e3
DA
580 free(str);
581 return rc;
582}
583
008f29d3
SB
584/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
585static int is_directory(const char *base_path, const struct dirent *dent)
586{
587 char path[PATH_MAX];
588 struct stat st;
589
590 sprintf(path, "%s/%s", base_path, dent->d_name);
591 if (stat(path, &st))
592 return 0;
593
594 return S_ISDIR(st.st_mode);
595}
596
597#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
4b9c0c59
TZ
598 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
599 lang_next) \
008f29d3
SB
600 if ((lang_dirent.d_type == DT_DIR || \
601 (lang_dirent.d_type == DT_UNKNOWN && \
602 is_directory(scripts_path, &lang_dirent))) && \
4b9c0c59
TZ
603 (strcmp(lang_dirent.d_name, ".")) && \
604 (strcmp(lang_dirent.d_name, "..")))
605
008f29d3 606#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
4b9c0c59
TZ
607 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
608 script_next) \
008f29d3
SB
609 if (script_dirent.d_type != DT_DIR && \
610 (script_dirent.d_type != DT_UNKNOWN || \
611 !is_directory(lang_path, &script_dirent)))
4b9c0c59
TZ
612
613
614#define RECORD_SUFFIX "-record"
615#define REPORT_SUFFIX "-report"
616
617struct script_desc {
618 struct list_head node;
619 char *name;
620 char *half_liner;
621 char *args;
622};
623
eccdfe2d 624static LIST_HEAD(script_descs);
4b9c0c59
TZ
625
626static struct script_desc *script_desc__new(const char *name)
627{
628 struct script_desc *s = zalloc(sizeof(*s));
629
b5b87312 630 if (s != NULL && name)
4b9c0c59
TZ
631 s->name = strdup(name);
632
633 return s;
634}
635
636static void script_desc__delete(struct script_desc *s)
637{
638 free(s->name);
e8719adf
TZ
639 free(s->half_liner);
640 free(s->args);
4b9c0c59
TZ
641 free(s);
642}
643
644static void script_desc__add(struct script_desc *s)
645{
646 list_add_tail(&s->node, &script_descs);
647}
648
649static struct script_desc *script_desc__find(const char *name)
650{
651 struct script_desc *s;
652
653 list_for_each_entry(s, &script_descs, node)
654 if (strcasecmp(s->name, name) == 0)
655 return s;
656 return NULL;
657}
658
659static struct script_desc *script_desc__findnew(const char *name)
660{
661 struct script_desc *s = script_desc__find(name);
662
663 if (s)
664 return s;
665
666 s = script_desc__new(name);
667 if (!s)
668 goto out_delete_desc;
669
670 script_desc__add(s);
671
672 return s;
673
674out_delete_desc:
675 script_desc__delete(s);
676
677 return NULL;
678}
679
965bb6be 680static const char *ends_with(const char *str, const char *suffix)
4b9c0c59
TZ
681{
682 size_t suffix_len = strlen(suffix);
965bb6be 683 const char *p = str;
4b9c0c59
TZ
684
685 if (strlen(str) > suffix_len) {
686 p = str + strlen(str) - suffix_len;
687 if (!strncmp(p, suffix, suffix_len))
688 return p;
689 }
690
691 return NULL;
692}
693
694static char *ltrim(char *str)
695{
696 int len = strlen(str);
697
698 while (len && isspace(*str)) {
699 len--;
700 str++;
701 }
702
703 return str;
704}
705
706static int read_script_info(struct script_desc *desc, const char *filename)
707{
708 char line[BUFSIZ], *p;
709 FILE *fp;
710
711 fp = fopen(filename, "r");
712 if (!fp)
713 return -1;
714
715 while (fgets(line, sizeof(line), fp)) {
716 p = ltrim(line);
717 if (strlen(p) == 0)
718 continue;
719 if (*p != '#')
720 continue;
721 p++;
722 if (strlen(p) && *p == '!')
723 continue;
724
725 p = ltrim(p);
726 if (strlen(p) && p[strlen(p) - 1] == '\n')
727 p[strlen(p) - 1] = '\0';
728
729 if (!strncmp(p, "description:", strlen("description:"))) {
730 p += strlen("description:");
731 desc->half_liner = strdup(ltrim(p));
732 continue;
733 }
734
735 if (!strncmp(p, "args:", strlen("args:"))) {
736 p += strlen("args:");
737 desc->args = strdup(ltrim(p));
738 continue;
739 }
740 }
741
742 fclose(fp);
743
744 return 0;
745}
746
747static int list_available_scripts(const struct option *opt __used,
748 const char *s __used, int unset __used)
749{
750 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
751 char scripts_path[MAXPATHLEN];
752 DIR *scripts_dir, *lang_dir;
753 char script_path[MAXPATHLEN];
754 char lang_path[MAXPATHLEN];
755 struct script_desc *desc;
756 char first_half[BUFSIZ];
757 char *script_root;
758 char *str;
759
760 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
761
762 scripts_dir = opendir(scripts_path);
763 if (!scripts_dir)
764 return -1;
765
008f29d3 766 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
4b9c0c59
TZ
767 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
768 lang_dirent.d_name);
769 lang_dir = opendir(lang_path);
770 if (!lang_dir)
771 continue;
772
008f29d3 773 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
4b9c0c59 774 script_root = strdup(script_dirent.d_name);
965bb6be 775 str = (char *)ends_with(script_root, REPORT_SUFFIX);
4b9c0c59
TZ
776 if (str) {
777 *str = '\0';
778 desc = script_desc__findnew(script_root);
779 snprintf(script_path, MAXPATHLEN, "%s/%s",
780 lang_path, script_dirent.d_name);
781 read_script_info(desc, script_path);
782 }
783 free(script_root);
784 }
785 }
786
787 fprintf(stdout, "List of available trace scripts:\n");
788 list_for_each_entry(desc, &script_descs, node) {
789 sprintf(first_half, "%s %s", desc->name,
790 desc->args ? desc->args : "");
791 fprintf(stdout, " %-36s %s\n", first_half,
792 desc->half_liner ? desc->half_liner : "");
793 }
794
795 exit(0);
796}
797
3875294f
TZ
798static char *get_script_path(const char *script_root, const char *suffix)
799{
800 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
801 char scripts_path[MAXPATHLEN];
802 char script_path[MAXPATHLEN];
803 DIR *scripts_dir, *lang_dir;
804 char lang_path[MAXPATHLEN];
805 char *str, *__script_root;
806 char *path = NULL;
807
808 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
809
810 scripts_dir = opendir(scripts_path);
811 if (!scripts_dir)
812 return NULL;
813
008f29d3 814 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
3875294f
TZ
815 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
816 lang_dirent.d_name);
817 lang_dir = opendir(lang_path);
818 if (!lang_dir)
819 continue;
820
008f29d3 821 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
3875294f 822 __script_root = strdup(script_dirent.d_name);
965bb6be 823 str = (char *)ends_with(__script_root, suffix);
3875294f
TZ
824 if (str) {
825 *str = '\0';
826 if (strcmp(__script_root, script_root))
827 continue;
828 snprintf(script_path, MAXPATHLEN, "%s/%s",
829 lang_path, script_dirent.d_name);
830 path = strdup(script_path);
831 free(__script_root);
832 break;
833 }
834 free(__script_root);
835 }
836 }
837
838 return path;
839}
840
b5b87312
TZ
841static bool is_top_script(const char *script_path)
842{
965bb6be 843 return ends_with(script_path, "top") == NULL ? false : true;
b5b87312
TZ
844}
845
846static int has_required_arg(char *script_path)
847{
848 struct script_desc *desc;
849 int n_args = 0;
850 char *p;
851
852 desc = script_desc__new(NULL);
853
854 if (read_script_info(desc, script_path))
855 goto out;
856
857 if (!desc->args)
858 goto out;
859
860 for (p = desc->args; *p; p++)
861 if (*p == '<')
862 n_args++;
863out:
864 script_desc__delete(desc);
865
866 return n_args;
867}
868
133dc4c3
IM
869static const char * const script_usage[] = {
870 "perf script [<options>]",
871 "perf script [<options>] record <script> [<record-options>] <command>",
872 "perf script [<options>] report <script> [script-args]",
873 "perf script [<options>] <script> [<record-options>] <command>",
874 "perf script [<options>] <top-script> [script-args]",
5f9c39dc
FW
875 NULL
876};
877
878static const struct option options[] = {
879 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
880 "dump raw trace in ASCII"),
c0555642 881 OPT_INCR('v', "verbose", &verbose,
5f9c39dc 882 "be more verbose (show symbol address, etc)"),
4b9c0c59 883 OPT_BOOLEAN('L', "Latency", &latency_format,
cda48461 884 "show latency attributes (irqs/preemption disabled, etc)"),
4b9c0c59
TZ
885 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
886 list_available_scripts),
956ffd02
TZ
887 OPT_CALLBACK('s', "script", NULL, "name",
888 "script file name (lang:script name, script name, or *)",
889 parse_scriptname),
890 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
133dc4c3 891 "generate perf-script.xx script in specified language"),
408f0d18
HM
892 OPT_STRING('i', "input", &input_name, "file",
893 "input file name"),
ffabd99e
FW
894 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
895 "do various checks like samples ordering and lost events"),
c0230b2b
DA
896 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
897 "file", "vmlinux pathname"),
898 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
899 "file", "kallsyms pathname"),
900 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
901 "When printing symbols do not display call chain"),
902 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
903 "Look for files with symbols relative to this directory"),
745f43e3 904 OPT_CALLBACK('f', "fields", NULL, "str",
1424dc96 905 "comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace. Fields: comm,tid,pid,time,cpu,event,trace,sym",
745f43e3 906 parse_output_fields),
956ffd02 907
1909629f 908 OPT_END()
5f9c39dc
FW
909};
910
34c86ea9
TZ
911static bool have_cmd(int argc, const char **argv)
912{
913 char **__argv = malloc(sizeof(const char *) * argc);
914
915 if (!__argv)
916 die("malloc");
917 memcpy(__argv, argv, sizeof(const char *) * argc);
918 argc = parse_options(argc, (const char **)__argv, record_options,
919 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
920 free(__argv);
921
922 return argc != 0;
923}
924
133dc4c3 925int cmd_script(int argc, const char **argv, const char *prefix __used)
5f9c39dc 926{
b5b87312
TZ
927 char *rec_script_path = NULL;
928 char *rep_script_path = NULL;
d8f66248 929 struct perf_session *session;
b5b87312 930 char *script_path = NULL;
3875294f 931 const char **__argv;
b5b87312
TZ
932 bool system_wide;
933 int i, j, err;
3875294f 934
b5b87312
TZ
935 setup_scripting();
936
133dc4c3 937 argc = parse_options(argc, argv, options, script_usage,
b5b87312
TZ
938 PARSE_OPT_STOP_AT_NON_OPTION);
939
940 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
941 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
942 if (!rec_script_path)
943 return cmd_record(argc, argv, NULL);
3875294f
TZ
944 }
945
b5b87312
TZ
946 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
947 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
948 if (!rep_script_path) {
3875294f 949 fprintf(stderr,
b5b87312 950 "Please specify a valid report script"
133dc4c3 951 "(see 'perf script -l' for listing)\n");
3875294f
TZ
952 return -1;
953 }
3875294f
TZ
954 }
955
44e668c6
BH
956 /* make sure PERF_EXEC_PATH is set for scripts */
957 perf_set_argv_exec_path(perf_exec_path());
958
b5b87312 959 if (argc && !script_name && !rec_script_path && !rep_script_path) {
a0cccc2e 960 int live_pipe[2];
b5b87312 961 int rep_args;
a0cccc2e
TZ
962 pid_t pid;
963
b5b87312
TZ
964 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
965 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
966
967 if (!rec_script_path && !rep_script_path) {
968 fprintf(stderr, " Couldn't find script %s\n\n See perf"
133dc4c3
IM
969 " script -l for available scripts.\n", argv[0]);
970 usage_with_options(script_usage, options);
a0cccc2e
TZ
971 }
972
b5b87312
TZ
973 if (is_top_script(argv[0])) {
974 rep_args = argc - 1;
975 } else {
976 int rec_args;
977
978 rep_args = has_required_arg(rep_script_path);
979 rec_args = (argc - 1) - rep_args;
980 if (rec_args < 0) {
981 fprintf(stderr, " %s script requires options."
133dc4c3 982 "\n\n See perf script -l for available "
b5b87312 983 "scripts and options.\n", argv[0]);
133dc4c3 984 usage_with_options(script_usage, options);
b5b87312 985 }
a0cccc2e
TZ
986 }
987
988 if (pipe(live_pipe) < 0) {
989 perror("failed to create pipe");
990 exit(-1);
991 }
992
993 pid = fork();
994 if (pid < 0) {
995 perror("failed to fork");
996 exit(-1);
997 }
998
999 if (!pid) {
b5b87312
TZ
1000 system_wide = true;
1001 j = 0;
1002
a0cccc2e
TZ
1003 dup2(live_pipe[1], 1);
1004 close(live_pipe[0]);
1005
b5b87312
TZ
1006 if (!is_top_script(argv[0]))
1007 system_wide = !have_cmd(argc - rep_args,
1008 &argv[rep_args]);
1009
1010 __argv = malloc((argc + 6) * sizeof(const char *));
e8719adf
TZ
1011 if (!__argv)
1012 die("malloc");
1013
b5b87312
TZ
1014 __argv[j++] = "/bin/sh";
1015 __argv[j++] = rec_script_path;
1016 if (system_wide)
1017 __argv[j++] = "-a";
1018 __argv[j++] = "-q";
1019 __argv[j++] = "-o";
1020 __argv[j++] = "-";
1021 for (i = rep_args + 1; i < argc; i++)
1022 __argv[j++] = argv[i];
1023 __argv[j++] = NULL;
a0cccc2e
TZ
1024
1025 execvp("/bin/sh", (char **)__argv);
e8719adf 1026 free(__argv);
a0cccc2e
TZ
1027 exit(-1);
1028 }
1029
1030 dup2(live_pipe[0], 0);
1031 close(live_pipe[1]);
1032
b5b87312 1033 __argv = malloc((argc + 4) * sizeof(const char *));
e8719adf
TZ
1034 if (!__argv)
1035 die("malloc");
b5b87312
TZ
1036 j = 0;
1037 __argv[j++] = "/bin/sh";
1038 __argv[j++] = rep_script_path;
1039 for (i = 1; i < rep_args + 1; i++)
1040 __argv[j++] = argv[i];
1041 __argv[j++] = "-i";
1042 __argv[j++] = "-";
1043 __argv[j++] = NULL;
a0cccc2e
TZ
1044
1045 execvp("/bin/sh", (char **)__argv);
e8719adf 1046 free(__argv);
a0cccc2e
TZ
1047 exit(-1);
1048 }
1049
b5b87312
TZ
1050 if (rec_script_path)
1051 script_path = rec_script_path;
1052 if (rep_script_path)
1053 script_path = rep_script_path;
34c86ea9 1054
b5b87312
TZ
1055 if (script_path) {
1056 system_wide = false;
1057 j = 0;
3875294f 1058
b5b87312
TZ
1059 if (rec_script_path)
1060 system_wide = !have_cmd(argc - 1, &argv[1]);
34c86ea9 1061
b5b87312 1062 __argv = malloc((argc + 2) * sizeof(const char *));
e8719adf
TZ
1063 if (!__argv)
1064 die("malloc");
34c86ea9
TZ
1065 __argv[j++] = "/bin/sh";
1066 __argv[j++] = script_path;
1067 if (system_wide)
1068 __argv[j++] = "-a";
b5b87312 1069 for (i = 2; i < argc; i++)
34c86ea9
TZ
1070 __argv[j++] = argv[i];
1071 __argv[j++] = NULL;
3875294f
TZ
1072
1073 execvp("/bin/sh", (char **)__argv);
e8719adf 1074 free(__argv);
3875294f
TZ
1075 exit(-1);
1076 }
956ffd02 1077
655000e7
ACM
1078 if (symbol__init() < 0)
1079 return -1;
cf4fee50
TZ
1080 if (!script_name)
1081 setup_pager();
5f9c39dc 1082
21ef97f0 1083 session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
d8f66248
ACM
1084 if (session == NULL)
1085 return -ENOMEM;
1086
1424dc96 1087 if (!no_callchain)
c0230b2b
DA
1088 symbol_conf.use_callchain = true;
1089 else
1090 symbol_conf.use_callchain = false;
1091
956ffd02
TZ
1092 if (generate_script_lang) {
1093 struct stat perf_stat;
745f43e3
DA
1094 int input;
1095
2c9e45f7 1096 if (output_set_by_user()) {
745f43e3
DA
1097 fprintf(stderr,
1098 "custom fields not supported for generated scripts");
1099 return -1;
1100 }
956ffd02 1101
745f43e3 1102 input = open(input_name, O_RDONLY);
956ffd02
TZ
1103 if (input < 0) {
1104 perror("failed to open file");
1105 exit(-1);
1106 }
1107
1108 err = fstat(input, &perf_stat);
1109 if (err < 0) {
1110 perror("failed to stat file");
1111 exit(-1);
1112 }
1113
1114 if (!perf_stat.st_size) {
1115 fprintf(stderr, "zero-sized file, nothing to do!\n");
1116 exit(0);
1117 }
1118
1119 scripting_ops = script_spec__lookup(generate_script_lang);
1120 if (!scripting_ops) {
1121 fprintf(stderr, "invalid language specifier");
1122 return -1;
1123 }
1124
133dc4c3 1125 err = scripting_ops->generate_script("perf-script");
956ffd02
TZ
1126 goto out;
1127 }
1128
1129 if (script_name) {
586bc5cc 1130 err = scripting_ops->start_script(script_name, argc, argv);
956ffd02
TZ
1131 if (err)
1132 goto out;
133dc4c3 1133 pr_debug("perf script started with script %s\n\n", script_name);
956ffd02
TZ
1134 }
1135
133dc4c3 1136 err = __cmd_script(session);
956ffd02 1137
d8f66248 1138 perf_session__delete(session);
956ffd02
TZ
1139 cleanup_scripting();
1140out:
1141 return err;
5f9c39dc 1142}