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