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