]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/builtin-script.c
perf inject: Add Instruction Tracing support
[mirror_ubuntu-hirsute-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"
36385be5 17#include "util/sort.h"
f5fc1412 18#include "util/data.h"
7a680eb9 19#include "util/auxtrace.h"
5d67be97 20#include <linux/bitmap.h>
5f9c39dc 21
956ffd02
TZ
22static char const *script_name;
23static char const *generate_script_lang;
ffabd99e 24static bool debug_mode;
e1889d75 25static u64 last_timestamp;
6fcf7ddb 26static u64 nr_unordered;
c0230b2b 27static bool no_callchain;
47390ae2 28static bool latency_format;
317df650 29static bool system_wide;
5d67be97
AB
30static const char *cpu_list;
31static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
956ffd02 32
745f43e3
DA
33enum perf_output_field {
34 PERF_OUTPUT_COMM = 1U << 0,
35 PERF_OUTPUT_TID = 1U << 1,
36 PERF_OUTPUT_PID = 1U << 2,
37 PERF_OUTPUT_TIME = 1U << 3,
38 PERF_OUTPUT_CPU = 1U << 4,
39 PERF_OUTPUT_EVNAME = 1U << 5,
40 PERF_OUTPUT_TRACE = 1U << 6,
787bef17
DA
41 PERF_OUTPUT_IP = 1U << 7,
42 PERF_OUTPUT_SYM = 1U << 8,
610723f2 43 PERF_OUTPUT_DSO = 1U << 9,
7cec0922 44 PERF_OUTPUT_ADDR = 1U << 10,
a978f2ab 45 PERF_OUTPUT_SYMOFFSET = 1U << 11,
cc8fae1d 46 PERF_OUTPUT_SRCLINE = 1U << 12,
535aeaae 47 PERF_OUTPUT_PERIOD = 1U << 13,
745f43e3
DA
48};
49
50struct output_option {
51 const char *str;
52 enum perf_output_field field;
53} all_output_options[] = {
54 {.str = "comm", .field = PERF_OUTPUT_COMM},
55 {.str = "tid", .field = PERF_OUTPUT_TID},
56 {.str = "pid", .field = PERF_OUTPUT_PID},
57 {.str = "time", .field = PERF_OUTPUT_TIME},
58 {.str = "cpu", .field = PERF_OUTPUT_CPU},
59 {.str = "event", .field = PERF_OUTPUT_EVNAME},
60 {.str = "trace", .field = PERF_OUTPUT_TRACE},
787bef17 61 {.str = "ip", .field = PERF_OUTPUT_IP},
c0230b2b 62 {.str = "sym", .field = PERF_OUTPUT_SYM},
610723f2 63 {.str = "dso", .field = PERF_OUTPUT_DSO},
7cec0922 64 {.str = "addr", .field = PERF_OUTPUT_ADDR},
a978f2ab 65 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
cc8fae1d 66 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
535aeaae 67 {.str = "period", .field = PERF_OUTPUT_PERIOD},
745f43e3
DA
68};
69
70/* default set to maintain compatibility with current format */
2c9e45f7
DA
71static struct {
72 bool user_set;
9cbdb702 73 bool wildcard_set;
a6ffaf91 74 unsigned int print_ip_opts;
2c9e45f7
DA
75 u64 fields;
76 u64 invalid_fields;
77} output[PERF_TYPE_MAX] = {
78
79 [PERF_TYPE_HARDWARE] = {
80 .user_set = false,
81
82 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
83 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 84 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71
JO
85 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
86 PERF_OUTPUT_PERIOD,
2c9e45f7
DA
87
88 .invalid_fields = PERF_OUTPUT_TRACE,
89 },
90
91 [PERF_TYPE_SOFTWARE] = {
92 .user_set = false,
93
94 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
95 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 96 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71
JO
97 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
98 PERF_OUTPUT_PERIOD,
2c9e45f7
DA
99
100 .invalid_fields = PERF_OUTPUT_TRACE,
101 },
102
103 [PERF_TYPE_TRACEPOINT] = {
104 .user_set = false,
105
106 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
107 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
108 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
109 },
0817a6a3
AS
110
111 [PERF_TYPE_RAW] = {
112 .user_set = false,
113
114 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
115 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 116 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71
JO
117 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
118 PERF_OUTPUT_PERIOD,
0817a6a3
AS
119
120 .invalid_fields = PERF_OUTPUT_TRACE,
121 },
1424dc96 122};
745f43e3 123
2c9e45f7
DA
124static bool output_set_by_user(void)
125{
126 int j;
127 for (j = 0; j < PERF_TYPE_MAX; ++j) {
128 if (output[j].user_set)
129 return true;
130 }
131 return false;
132}
745f43e3 133
9cbdb702
DA
134static const char *output_field2str(enum perf_output_field field)
135{
136 int i, imax = ARRAY_SIZE(all_output_options);
137 const char *str = "";
138
139 for (i = 0; i < imax; ++i) {
140 if (all_output_options[i].field == field) {
141 str = all_output_options[i].str;
142 break;
143 }
144 }
145 return str;
146}
147
2c9e45f7 148#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
1424dc96 149
5bff01f6
ACM
150static int perf_evsel__check_stype(struct perf_evsel *evsel,
151 u64 sample_type, const char *sample_msg,
152 enum perf_output_field field)
1424dc96 153{
5bff01f6 154 struct perf_event_attr *attr = &evsel->attr;
9cbdb702
DA
155 int type = attr->type;
156 const char *evname;
157
158 if (attr->sample_type & sample_type)
159 return 0;
160
161 if (output[type].user_set) {
5bff01f6 162 evname = perf_evsel__name(evsel);
9cbdb702
DA
163 pr_err("Samples for '%s' event do not have %s attribute set. "
164 "Cannot print '%s' field.\n",
165 evname, sample_msg, output_field2str(field));
166 return -1;
167 }
168
169 /* user did not ask for it explicitly so remove from the default list */
170 output[type].fields &= ~field;
5bff01f6 171 evname = perf_evsel__name(evsel);
9cbdb702
DA
172 pr_debug("Samples for '%s' event do not have %s attribute set. "
173 "Skipping '%s' field.\n",
174 evname, sample_msg, output_field2str(field));
175
176 return 0;
177}
178
179static int perf_evsel__check_attr(struct perf_evsel *evsel,
180 struct perf_session *session)
181{
182 struct perf_event_attr *attr = &evsel->attr;
183
1424dc96
DA
184 if (PRINT_FIELD(TRACE) &&
185 !perf_session__has_traces(session, "record -R"))
186 return -EINVAL;
187
787bef17 188 if (PRINT_FIELD(IP)) {
5bff01f6
ACM
189 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
190 PERF_OUTPUT_IP))
1424dc96 191 return -EINVAL;
1424dc96 192 }
7cec0922
DA
193
194 if (PRINT_FIELD(ADDR) &&
5bff01f6
ACM
195 perf_evsel__check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
196 PERF_OUTPUT_ADDR))
7cec0922
DA
197 return -EINVAL;
198
199 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
200 pr_err("Display of symbols requested but neither sample IP nor "
201 "sample address\nis selected. Hence, no addresses to convert "
202 "to symbols.\n");
787bef17
DA
203 return -EINVAL;
204 }
a978f2ab
AN
205 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
206 pr_err("Display of offsets requested but symbol is not"
207 "selected.\n");
208 return -EINVAL;
209 }
7cec0922
DA
210 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
211 pr_err("Display of DSO requested but neither sample IP nor "
212 "sample address\nis selected. Hence, no addresses to convert "
213 "to DSO.\n");
610723f2
DA
214 return -EINVAL;
215 }
cc8fae1d
AH
216 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
217 pr_err("Display of source line number requested but sample IP is not\n"
218 "selected. Hence, no address to lookup the source line number.\n");
219 return -EINVAL;
220 }
1424dc96
DA
221
222 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
5bff01f6
ACM
223 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
224 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
1424dc96 225 return -EINVAL;
1424dc96
DA
226
227 if (PRINT_FIELD(TIME) &&
5bff01f6
ACM
228 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
229 PERF_OUTPUT_TIME))
1424dc96 230 return -EINVAL;
1424dc96
DA
231
232 if (PRINT_FIELD(CPU) &&
5bff01f6
ACM
233 perf_evsel__check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
234 PERF_OUTPUT_CPU))
1424dc96 235 return -EINVAL;
9cbdb702 236
535aeaae
JO
237 if (PRINT_FIELD(PERIOD) &&
238 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
239 PERF_OUTPUT_PERIOD))
240 return -EINVAL;
241
9cbdb702
DA
242 return 0;
243}
244
7ea95727
AH
245static void set_print_ip_opts(struct perf_event_attr *attr)
246{
247 unsigned int type = attr->type;
248
249 output[type].print_ip_opts = 0;
250 if (PRINT_FIELD(IP))
251 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
252
253 if (PRINT_FIELD(SYM))
254 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
255
256 if (PRINT_FIELD(DSO))
257 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
258
259 if (PRINT_FIELD(SYMOFFSET))
260 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
cc8fae1d
AH
261
262 if (PRINT_FIELD(SRCLINE))
263 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
7ea95727
AH
264}
265
9cbdb702
DA
266/*
267 * verify all user requested events exist and the samples
268 * have the expected data
269 */
270static int perf_session__check_output_opt(struct perf_session *session)
271{
272 int j;
273 struct perf_evsel *evsel;
274
275 for (j = 0; j < PERF_TYPE_MAX; ++j) {
276 evsel = perf_session__find_first_evtype(session, j);
277
278 /*
279 * even if fields is set to 0 (ie., show nothing) event must
280 * exist if user explicitly includes it on the command line
281 */
282 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
283 pr_err("%s events do not exist. "
284 "Remove corresponding -f option to proceed.\n",
285 event_type(j));
286 return -1;
287 }
288
289 if (evsel && output[j].fields &&
290 perf_evsel__check_attr(evsel, session))
291 return -1;
a6ffaf91
DA
292
293 if (evsel == NULL)
294 continue;
295
7ea95727 296 set_print_ip_opts(&evsel->attr);
1424dc96
DA
297 }
298
98526ee7
AH
299 if (!no_callchain) {
300 bool use_callchain = false;
301
302 evlist__for_each(session->evlist, evsel) {
303 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
304 use_callchain = true;
305 break;
306 }
307 }
308 if (!use_callchain)
309 symbol_conf.use_callchain = false;
310 }
311
80b8b496
DA
312 /*
313 * set default for tracepoints to print symbols only
314 * if callchains are present
315 */
316 if (symbol_conf.use_callchain &&
317 !output[PERF_TYPE_TRACEPOINT].user_set) {
318 struct perf_event_attr *attr;
319
320 j = PERF_TYPE_TRACEPOINT;
321 evsel = perf_session__find_first_evtype(session, j);
322 if (evsel == NULL)
323 goto out;
324
325 attr = &evsel->attr;
326
327 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
328 output[j].fields |= PERF_OUTPUT_IP;
329 output[j].fields |= PERF_OUTPUT_SYM;
330 output[j].fields |= PERF_OUTPUT_DSO;
331 set_print_ip_opts(attr);
332 }
333 }
334
335out:
1424dc96
DA
336 return 0;
337}
745f43e3 338
fcf65bf1 339static void print_sample_start(struct perf_sample *sample,
1424dc96 340 struct thread *thread,
5bff01f6 341 struct perf_evsel *evsel)
c70c94b4 342{
5bff01f6 343 struct perf_event_attr *attr = &evsel->attr;
c70c94b4
DA
344 unsigned long secs;
345 unsigned long usecs;
745f43e3
DA
346 unsigned long long nsecs;
347
348 if (PRINT_FIELD(COMM)) {
349 if (latency_format)
b9c5143a 350 printf("%8.8s ", thread__comm_str(thread));
787bef17 351 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
b9c5143a 352 printf("%s ", thread__comm_str(thread));
745f43e3 353 else
b9c5143a 354 printf("%16s ", thread__comm_str(thread));
745f43e3 355 }
c70c94b4 356
745f43e3
DA
357 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
358 printf("%5d/%-5d ", sample->pid, sample->tid);
359 else if (PRINT_FIELD(PID))
360 printf("%5d ", sample->pid);
361 else if (PRINT_FIELD(TID))
362 printf("%5d ", sample->tid);
363
364 if (PRINT_FIELD(CPU)) {
365 if (latency_format)
366 printf("%3d ", sample->cpu);
367 else
368 printf("[%03d] ", sample->cpu);
369 }
c70c94b4 370
745f43e3
DA
371 if (PRINT_FIELD(TIME)) {
372 nsecs = sample->time;
373 secs = nsecs / NSECS_PER_SEC;
374 nsecs -= secs * NSECS_PER_SEC;
375 usecs = nsecs / NSECS_PER_USEC;
376 printf("%5lu.%06lu: ", secs, usecs);
377 }
c70c94b4
DA
378}
379
7cec0922
DA
380static void print_sample_addr(union perf_event *event,
381 struct perf_sample *sample,
7cec0922
DA
382 struct thread *thread,
383 struct perf_event_attr *attr)
384{
385 struct addr_location al;
7cec0922
DA
386
387 printf("%16" PRIx64, sample->addr);
388
389 if (!sample_addr_correlates_sym(attr))
390 return;
391
bb871a9c 392 perf_event__preprocess_sample_addr(event, sample, thread, &al);
7cec0922
DA
393
394 if (PRINT_FIELD(SYM)) {
547a92e0 395 printf(" ");
a978f2ab
AN
396 if (PRINT_FIELD(SYMOFFSET))
397 symbol__fprintf_symname_offs(al.sym, &al, stdout);
398 else
399 symbol__fprintf_symname(al.sym, stdout);
7cec0922
DA
400 }
401
402 if (PRINT_FIELD(DSO)) {
547a92e0
AN
403 printf(" (");
404 map__fprintf_dsoname(al.map, stdout);
405 printf(")");
7cec0922
DA
406 }
407}
408
95582596
AN
409static void print_sample_bts(union perf_event *event,
410 struct perf_sample *sample,
411 struct perf_evsel *evsel,
a2cb3cf2
AH
412 struct thread *thread,
413 struct addr_location *al)
95582596
AN
414{
415 struct perf_event_attr *attr = &evsel->attr;
8066be5f 416 bool print_srcline_last = false;
95582596
AN
417
418 /* print branch_from information */
419 if (PRINT_FIELD(IP)) {
8066be5f
AH
420 unsigned int print_opts = output[attr->type].print_ip_opts;
421
422 if (symbol_conf.use_callchain && sample->callchain) {
95582596 423 printf("\n");
8066be5f
AH
424 } else {
425 printf(" ");
426 if (print_opts & PRINT_IP_OPT_SRCLINE) {
427 print_srcline_last = true;
428 print_opts &= ~PRINT_IP_OPT_SRCLINE;
429 }
430 }
431 perf_evsel__print_ip(evsel, sample, al, print_opts,
307cbb92 432 PERF_MAX_STACK_DEPTH);
95582596
AN
433 }
434
95582596 435 /* print branch_to information */
243be3dd
AH
436 if (PRINT_FIELD(ADDR) ||
437 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
578bea40
AH
438 !output[attr->type].user_set)) {
439 printf(" => ");
bb871a9c 440 print_sample_addr(event, sample, thread, attr);
578bea40 441 }
95582596 442
8066be5f
AH
443 if (print_srcline_last)
444 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
445
95582596
AN
446 printf("\n");
447}
448
97822433 449static void process_event(union perf_event *event, struct perf_sample *sample,
f9d5d549 450 struct perf_evsel *evsel, struct addr_location *al)
be6d842a 451{
f9d5d549 452 struct thread *thread = al->thread;
9e69c210 453 struct perf_event_attr *attr = &evsel->attr;
1424dc96 454
2c9e45f7 455 if (output[attr->type].fields == 0)
1424dc96
DA
456 return;
457
fcf65bf1 458 print_sample_start(sample, thread, evsel);
745f43e3 459
535aeaae
JO
460 if (PRINT_FIELD(PERIOD))
461 printf("%10" PRIu64 " ", sample->period);
462
e944d3d7
NK
463 if (PRINT_FIELD(EVNAME)) {
464 const char *evname = perf_evsel__name(evsel);
465 printf("%s: ", evname ? evname : "[unknown]");
466 }
467
95582596 468 if (is_bts_event(attr)) {
cc22e575 469 print_sample_bts(event, sample, evsel, thread, al);
95582596
AN
470 return;
471 }
472
745f43e3 473 if (PRINT_FIELD(TRACE))
fcf65bf1
ACM
474 event_format__print(evsel->tp_format, sample->cpu,
475 sample->raw_data, sample->raw_size);
7cec0922 476 if (PRINT_FIELD(ADDR))
bb871a9c 477 print_sample_addr(event, sample, thread, attr);
7cec0922 478
787bef17 479 if (PRINT_FIELD(IP)) {
c0230b2b
DA
480 if (!symbol_conf.use_callchain)
481 printf(" ");
482 else
483 printf("\n");
a6ffaf91 484
cc22e575 485 perf_evsel__print_ip(evsel, sample, al,
307cbb92
DA
486 output[attr->type].print_ip_opts,
487 PERF_MAX_STACK_DEPTH);
c0230b2b
DA
488 }
489
c70c94b4 490 printf("\n");
be6d842a
DA
491}
492
1d037ca1
IT
493static int default_start_script(const char *script __maybe_unused,
494 int argc __maybe_unused,
495 const char **argv __maybe_unused)
956ffd02
TZ
496{
497 return 0;
498}
499
d445dd2a
AH
500static int default_flush_script(void)
501{
502 return 0;
503}
504
956ffd02
TZ
505static int default_stop_script(void)
506{
507 return 0;
508}
509
1d037ca1
IT
510static int default_generate_script(struct pevent *pevent __maybe_unused,
511 const char *outfile __maybe_unused)
956ffd02
TZ
512{
513 return 0;
514}
515
516static struct scripting_ops default_scripting_ops = {
517 .start_script = default_start_script,
d445dd2a 518 .flush_script = default_flush_script,
956ffd02 519 .stop_script = default_stop_script,
be6d842a 520 .process_event = process_event,
956ffd02
TZ
521 .generate_script = default_generate_script,
522};
523
524static struct scripting_ops *scripting_ops;
525
526static void setup_scripting(void)
527{
16c632de 528 setup_perl_scripting();
7e4b21b8 529 setup_python_scripting();
16c632de 530
956ffd02
TZ
531 scripting_ops = &default_scripting_ops;
532}
533
d445dd2a
AH
534static int flush_scripting(void)
535{
536 return scripting_ops->flush_script();
537}
538
956ffd02
TZ
539static int cleanup_scripting(void)
540{
133dc4c3 541 pr_debug("\nperf script stopped\n");
3824a4e8 542
956ffd02
TZ
543 return scripting_ops->stop_script();
544}
545
1d037ca1 546static int process_sample_event(struct perf_tool *tool __maybe_unused,
d20deb64 547 union perf_event *event,
8115d60c 548 struct perf_sample *sample,
9e69c210 549 struct perf_evsel *evsel,
743eb868 550 struct machine *machine)
5f9c39dc 551{
e7984b7b 552 struct addr_location al;
5f9c39dc 553
1424dc96
DA
554 if (debug_mode) {
555 if (sample->time < last_timestamp) {
556 pr_err("Samples misordered, previous: %" PRIu64
557 " this: %" PRIu64 "\n", last_timestamp,
558 sample->time);
559 nr_unordered++;
e1889d75 560 }
1424dc96
DA
561 last_timestamp = sample->time;
562 return 0;
5f9c39dc 563 }
5d67be97 564
e44baa3e 565 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
e7984b7b
DA
566 pr_err("problem processing %d event, skipping it.\n",
567 event->header.type);
568 return -1;
569 }
570
571 if (al.filtered)
572 return 0;
573
5d67be97
AB
574 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
575 return 0;
576
f9d5d549 577 scripting_ops->process_event(event, sample, evsel, &al);
5f9c39dc
FW
578
579 return 0;
580}
581
6f3e5eda
AH
582struct perf_script {
583 struct perf_tool tool;
584 struct perf_session *session;
ad7ebb9a 585 bool show_task_events;
ba1ddf42 586 bool show_mmap_events;
016e92fb
FW
587};
588
7ea95727
AH
589static int process_attr(struct perf_tool *tool, union perf_event *event,
590 struct perf_evlist **pevlist)
591{
592 struct perf_script *scr = container_of(tool, struct perf_script, tool);
593 struct perf_evlist *evlist;
594 struct perf_evsel *evsel, *pos;
595 int err;
596
597 err = perf_event__process_attr(tool, event, pevlist);
598 if (err)
599 return err;
600
601 evlist = *pevlist;
602 evsel = perf_evlist__last(*pevlist);
603
604 if (evsel->attr.type >= PERF_TYPE_MAX)
605 return 0;
606
0050f7aa 607 evlist__for_each(evlist, pos) {
7ea95727
AH
608 if (pos->attr.type == evsel->attr.type && pos != evsel)
609 return 0;
610 }
611
612 set_print_ip_opts(&evsel->attr);
613
614 return perf_evsel__check_attr(evsel, scr->session);
615}
616
ad7ebb9a
NK
617static int process_comm_event(struct perf_tool *tool,
618 union perf_event *event,
619 struct perf_sample *sample,
620 struct machine *machine)
621{
622 struct thread *thread;
623 struct perf_script *script = container_of(tool, struct perf_script, tool);
624 struct perf_session *session = script->session;
625 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
626 int ret = -1;
627
628 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
629 if (thread == NULL) {
630 pr_debug("problem processing COMM event, skipping it.\n");
631 return -1;
632 }
633
634 if (perf_event__process_comm(tool, event, sample, machine) < 0)
635 goto out;
636
637 if (!evsel->attr.sample_id_all) {
638 sample->cpu = 0;
639 sample->time = 0;
640 sample->tid = event->comm.tid;
641 sample->pid = event->comm.pid;
642 }
643 print_sample_start(sample, thread, evsel);
644 perf_event__fprintf(event, stdout);
645 ret = 0;
646
647out:
648 return ret;
649}
650
651static int process_fork_event(struct perf_tool *tool,
652 union perf_event *event,
653 struct perf_sample *sample,
654 struct machine *machine)
655{
656 struct thread *thread;
657 struct perf_script *script = container_of(tool, struct perf_script, tool);
658 struct perf_session *session = script->session;
659 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
660
661 if (perf_event__process_fork(tool, event, sample, machine) < 0)
662 return -1;
663
664 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
665 if (thread == NULL) {
666 pr_debug("problem processing FORK event, skipping it.\n");
667 return -1;
668 }
669
670 if (!evsel->attr.sample_id_all) {
671 sample->cpu = 0;
672 sample->time = event->fork.time;
673 sample->tid = event->fork.tid;
674 sample->pid = event->fork.pid;
675 }
676 print_sample_start(sample, thread, evsel);
677 perf_event__fprintf(event, stdout);
678
679 return 0;
680}
681static int process_exit_event(struct perf_tool *tool,
682 union perf_event *event,
683 struct perf_sample *sample,
684 struct machine *machine)
685{
686 struct thread *thread;
687 struct perf_script *script = container_of(tool, struct perf_script, tool);
688 struct perf_session *session = script->session;
689 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
690
691 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
692 if (thread == NULL) {
693 pr_debug("problem processing EXIT event, skipping it.\n");
694 return -1;
695 }
696
697 if (!evsel->attr.sample_id_all) {
698 sample->cpu = 0;
699 sample->time = 0;
700 sample->tid = event->comm.tid;
701 sample->pid = event->comm.pid;
702 }
703 print_sample_start(sample, thread, evsel);
704 perf_event__fprintf(event, stdout);
705
706 if (perf_event__process_exit(tool, event, sample, machine) < 0)
707 return -1;
708
709 return 0;
710}
711
ba1ddf42
NK
712static int process_mmap_event(struct perf_tool *tool,
713 union perf_event *event,
714 struct perf_sample *sample,
715 struct machine *machine)
716{
717 struct thread *thread;
718 struct perf_script *script = container_of(tool, struct perf_script, tool);
719 struct perf_session *session = script->session;
720 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
721
722 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
723 return -1;
724
725 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
726 if (thread == NULL) {
727 pr_debug("problem processing MMAP event, skipping it.\n");
728 return -1;
729 }
730
731 if (!evsel->attr.sample_id_all) {
732 sample->cpu = 0;
733 sample->time = 0;
734 sample->tid = event->mmap.tid;
735 sample->pid = event->mmap.pid;
736 }
737 print_sample_start(sample, thread, evsel);
738 perf_event__fprintf(event, stdout);
739
740 return 0;
741}
742
743static int process_mmap2_event(struct perf_tool *tool,
744 union perf_event *event,
745 struct perf_sample *sample,
746 struct machine *machine)
747{
748 struct thread *thread;
749 struct perf_script *script = container_of(tool, struct perf_script, tool);
750 struct perf_session *session = script->session;
751 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
752
753 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
754 return -1;
755
756 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
757 if (thread == NULL) {
758 pr_debug("problem processing MMAP2 event, skipping it.\n");
759 return -1;
760 }
761
762 if (!evsel->attr.sample_id_all) {
763 sample->cpu = 0;
764 sample->time = 0;
765 sample->tid = event->mmap2.tid;
766 sample->pid = event->mmap2.pid;
767 }
768 print_sample_start(sample, thread, evsel);
769 perf_event__fprintf(event, stdout);
770
771 return 0;
772}
773
1d037ca1 774static void sig_handler(int sig __maybe_unused)
c239da3b
TZ
775{
776 session_done = 1;
777}
778
6f3e5eda 779static int __cmd_script(struct perf_script *script)
5f9c39dc 780{
6fcf7ddb
FW
781 int ret;
782
c239da3b
TZ
783 signal(SIGINT, sig_handler);
784
ad7ebb9a
NK
785 /* override event processing functions */
786 if (script->show_task_events) {
787 script->tool.comm = process_comm_event;
788 script->tool.fork = process_fork_event;
789 script->tool.exit = process_exit_event;
790 }
ba1ddf42
NK
791 if (script->show_mmap_events) {
792 script->tool.mmap = process_mmap_event;
793 script->tool.mmap2 = process_mmap2_event;
794 }
ad7ebb9a 795
b7b61cbe 796 ret = perf_session__process_events(script->session);
6fcf7ddb 797
6d8afb56 798 if (debug_mode)
9486aa38 799 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
6fcf7ddb
FW
800
801 return ret;
5f9c39dc
FW
802}
803
956ffd02
TZ
804struct script_spec {
805 struct list_head node;
806 struct scripting_ops *ops;
807 char spec[0];
808};
809
eccdfe2d 810static LIST_HEAD(script_specs);
956ffd02
TZ
811
812static struct script_spec *script_spec__new(const char *spec,
813 struct scripting_ops *ops)
814{
815 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
816
817 if (s != NULL) {
818 strcpy(s->spec, spec);
819 s->ops = ops;
820 }
821
822 return s;
823}
824
956ffd02
TZ
825static void script_spec__add(struct script_spec *s)
826{
827 list_add_tail(&s->node, &script_specs);
828}
829
830static struct script_spec *script_spec__find(const char *spec)
831{
832 struct script_spec *s;
833
834 list_for_each_entry(s, &script_specs, node)
835 if (strcasecmp(s->spec, spec) == 0)
836 return s;
837 return NULL;
838}
839
840static struct script_spec *script_spec__findnew(const char *spec,
841 struct scripting_ops *ops)
842{
843 struct script_spec *s = script_spec__find(spec);
844
845 if (s)
846 return s;
847
848 s = script_spec__new(spec, ops);
849 if (!s)
466e2876 850 return NULL;
956ffd02
TZ
851
852 script_spec__add(s);
853
854 return s;
956ffd02
TZ
855}
856
857int script_spec_register(const char *spec, struct scripting_ops *ops)
858{
859 struct script_spec *s;
860
861 s = script_spec__find(spec);
862 if (s)
863 return -1;
864
865 s = script_spec__findnew(spec, ops);
866 if (!s)
867 return -1;
868
869 return 0;
870}
871
872static struct scripting_ops *script_spec__lookup(const char *spec)
873{
874 struct script_spec *s = script_spec__find(spec);
875 if (!s)
876 return NULL;
877
878 return s->ops;
879}
880
881static void list_available_languages(void)
882{
883 struct script_spec *s;
884
885 fprintf(stderr, "\n");
886 fprintf(stderr, "Scripting language extensions (used in "
133dc4c3 887 "perf script -s [spec:]script.[spec]):\n\n");
956ffd02
TZ
888
889 list_for_each_entry(s, &script_specs, node)
890 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
891
892 fprintf(stderr, "\n");
893}
894
1d037ca1
IT
895static int parse_scriptname(const struct option *opt __maybe_unused,
896 const char *str, int unset __maybe_unused)
956ffd02
TZ
897{
898 char spec[PATH_MAX];
899 const char *script, *ext;
900 int len;
901
f526d68b 902 if (strcmp(str, "lang") == 0) {
956ffd02 903 list_available_languages();
f526d68b 904 exit(0);
956ffd02
TZ
905 }
906
907 script = strchr(str, ':');
908 if (script) {
909 len = script - str;
910 if (len >= PATH_MAX) {
911 fprintf(stderr, "invalid language specifier");
912 return -1;
913 }
914 strncpy(spec, str, len);
915 spec[len] = '\0';
916 scripting_ops = script_spec__lookup(spec);
917 if (!scripting_ops) {
918 fprintf(stderr, "invalid language specifier");
919 return -1;
920 }
921 script++;
922 } else {
923 script = str;
d1e95bb5 924 ext = strrchr(script, '.');
956ffd02
TZ
925 if (!ext) {
926 fprintf(stderr, "invalid script extension");
927 return -1;
928 }
929 scripting_ops = script_spec__lookup(++ext);
930 if (!scripting_ops) {
931 fprintf(stderr, "invalid script extension");
932 return -1;
933 }
934 }
935
936 script_name = strdup(script);
937
938 return 0;
939}
940
1d037ca1
IT
941static int parse_output_fields(const struct option *opt __maybe_unused,
942 const char *arg, int unset __maybe_unused)
745f43e3
DA
943{
944 char *tok;
50ca19ae 945 int i, imax = ARRAY_SIZE(all_output_options);
2c9e45f7 946 int j;
745f43e3
DA
947 int rc = 0;
948 char *str = strdup(arg);
1424dc96 949 int type = -1;
745f43e3
DA
950
951 if (!str)
952 return -ENOMEM;
953
2c9e45f7
DA
954 /* first word can state for which event type the user is specifying
955 * the fields. If no type exists, the specified fields apply to all
956 * event types found in the file minus the invalid fields for a type.
1424dc96 957 */
2c9e45f7
DA
958 tok = strchr(str, ':');
959 if (tok) {
960 *tok = '\0';
961 tok++;
962 if (!strcmp(str, "hw"))
963 type = PERF_TYPE_HARDWARE;
964 else if (!strcmp(str, "sw"))
965 type = PERF_TYPE_SOFTWARE;
966 else if (!strcmp(str, "trace"))
967 type = PERF_TYPE_TRACEPOINT;
0817a6a3
AS
968 else if (!strcmp(str, "raw"))
969 type = PERF_TYPE_RAW;
2c9e45f7
DA
970 else {
971 fprintf(stderr, "Invalid event type in field string.\n");
38efb539
RR
972 rc = -EINVAL;
973 goto out;
2c9e45f7
DA
974 }
975
976 if (output[type].user_set)
977 pr_warning("Overriding previous field request for %s events.\n",
978 event_type(type));
979
980 output[type].fields = 0;
981 output[type].user_set = true;
9cbdb702 982 output[type].wildcard_set = false;
2c9e45f7
DA
983
984 } else {
985 tok = str;
986 if (strlen(str) == 0) {
987 fprintf(stderr,
988 "Cannot set fields to 'none' for all event types.\n");
989 rc = -EINVAL;
990 goto out;
991 }
992
993 if (output_set_by_user())
994 pr_warning("Overriding previous field request for all events.\n");
995
996 for (j = 0; j < PERF_TYPE_MAX; ++j) {
997 output[j].fields = 0;
998 output[j].user_set = true;
9cbdb702 999 output[j].wildcard_set = true;
2c9e45f7 1000 }
745f43e3
DA
1001 }
1002
2c9e45f7
DA
1003 tok = strtok(tok, ",");
1004 while (tok) {
745f43e3 1005 for (i = 0; i < imax; ++i) {
2c9e45f7 1006 if (strcmp(tok, all_output_options[i].str) == 0)
745f43e3 1007 break;
745f43e3
DA
1008 }
1009 if (i == imax) {
2c9e45f7 1010 fprintf(stderr, "Invalid field requested.\n");
745f43e3 1011 rc = -EINVAL;
2c9e45f7 1012 goto out;
745f43e3
DA
1013 }
1014
2c9e45f7
DA
1015 if (type == -1) {
1016 /* add user option to all events types for
1017 * which it is valid
1018 */
1019 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1020 if (output[j].invalid_fields & all_output_options[i].field) {
1021 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1022 all_output_options[i].str, event_type(j));
1023 } else
1024 output[j].fields |= all_output_options[i].field;
1025 }
1026 } else {
1027 if (output[type].invalid_fields & all_output_options[i].field) {
1028 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1029 all_output_options[i].str, event_type(type));
1030
1031 rc = -EINVAL;
1032 goto out;
1033 }
1034 output[type].fields |= all_output_options[i].field;
1035 }
1036
1037 tok = strtok(NULL, ",");
745f43e3
DA
1038 }
1039
2c9e45f7
DA
1040 if (type >= 0) {
1041 if (output[type].fields == 0) {
1042 pr_debug("No fields requested for %s type. "
1043 "Events will not be displayed.\n", event_type(type));
1044 }
1045 }
745f43e3 1046
2c9e45f7 1047out:
745f43e3
DA
1048 free(str);
1049 return rc;
1050}
1051
008f29d3
SB
1052/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1053static int is_directory(const char *base_path, const struct dirent *dent)
1054{
1055 char path[PATH_MAX];
1056 struct stat st;
1057
1058 sprintf(path, "%s/%s", base_path, dent->d_name);
1059 if (stat(path, &st))
1060 return 0;
1061
1062 return S_ISDIR(st.st_mode);
1063}
1064
1065#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
4b9c0c59
TZ
1066 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1067 lang_next) \
008f29d3
SB
1068 if ((lang_dirent.d_type == DT_DIR || \
1069 (lang_dirent.d_type == DT_UNKNOWN && \
1070 is_directory(scripts_path, &lang_dirent))) && \
4b9c0c59
TZ
1071 (strcmp(lang_dirent.d_name, ".")) && \
1072 (strcmp(lang_dirent.d_name, "..")))
1073
008f29d3 1074#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
4b9c0c59
TZ
1075 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1076 script_next) \
008f29d3
SB
1077 if (script_dirent.d_type != DT_DIR && \
1078 (script_dirent.d_type != DT_UNKNOWN || \
1079 !is_directory(lang_path, &script_dirent)))
4b9c0c59
TZ
1080
1081
1082#define RECORD_SUFFIX "-record"
1083#define REPORT_SUFFIX "-report"
1084
1085struct script_desc {
1086 struct list_head node;
1087 char *name;
1088 char *half_liner;
1089 char *args;
1090};
1091
eccdfe2d 1092static LIST_HEAD(script_descs);
4b9c0c59
TZ
1093
1094static struct script_desc *script_desc__new(const char *name)
1095{
1096 struct script_desc *s = zalloc(sizeof(*s));
1097
b5b87312 1098 if (s != NULL && name)
4b9c0c59
TZ
1099 s->name = strdup(name);
1100
1101 return s;
1102}
1103
1104static void script_desc__delete(struct script_desc *s)
1105{
74cf249d
ACM
1106 zfree(&s->name);
1107 zfree(&s->half_liner);
1108 zfree(&s->args);
4b9c0c59
TZ
1109 free(s);
1110}
1111
1112static void script_desc__add(struct script_desc *s)
1113{
1114 list_add_tail(&s->node, &script_descs);
1115}
1116
1117static struct script_desc *script_desc__find(const char *name)
1118{
1119 struct script_desc *s;
1120
1121 list_for_each_entry(s, &script_descs, node)
1122 if (strcasecmp(s->name, name) == 0)
1123 return s;
1124 return NULL;
1125}
1126
1127static struct script_desc *script_desc__findnew(const char *name)
1128{
1129 struct script_desc *s = script_desc__find(name);
1130
1131 if (s)
1132 return s;
1133
1134 s = script_desc__new(name);
1135 if (!s)
1136 goto out_delete_desc;
1137
1138 script_desc__add(s);
1139
1140 return s;
1141
1142out_delete_desc:
1143 script_desc__delete(s);
1144
1145 return NULL;
1146}
1147
965bb6be 1148static const char *ends_with(const char *str, const char *suffix)
4b9c0c59
TZ
1149{
1150 size_t suffix_len = strlen(suffix);
965bb6be 1151 const char *p = str;
4b9c0c59
TZ
1152
1153 if (strlen(str) > suffix_len) {
1154 p = str + strlen(str) - suffix_len;
1155 if (!strncmp(p, suffix, suffix_len))
1156 return p;
1157 }
1158
1159 return NULL;
1160}
1161
4b9c0c59
TZ
1162static int read_script_info(struct script_desc *desc, const char *filename)
1163{
1164 char line[BUFSIZ], *p;
1165 FILE *fp;
1166
1167 fp = fopen(filename, "r");
1168 if (!fp)
1169 return -1;
1170
1171 while (fgets(line, sizeof(line), fp)) {
1172 p = ltrim(line);
1173 if (strlen(p) == 0)
1174 continue;
1175 if (*p != '#')
1176 continue;
1177 p++;
1178 if (strlen(p) && *p == '!')
1179 continue;
1180
1181 p = ltrim(p);
1182 if (strlen(p) && p[strlen(p) - 1] == '\n')
1183 p[strlen(p) - 1] = '\0';
1184
1185 if (!strncmp(p, "description:", strlen("description:"))) {
1186 p += strlen("description:");
1187 desc->half_liner = strdup(ltrim(p));
1188 continue;
1189 }
1190
1191 if (!strncmp(p, "args:", strlen("args:"))) {
1192 p += strlen("args:");
1193 desc->args = strdup(ltrim(p));
1194 continue;
1195 }
1196 }
1197
1198 fclose(fp);
1199
1200 return 0;
1201}
1202
38efb539
RR
1203static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1204{
1205 char *script_root, *str;
1206
1207 script_root = strdup(script_dirent->d_name);
1208 if (!script_root)
1209 return NULL;
1210
1211 str = (char *)ends_with(script_root, suffix);
1212 if (!str) {
1213 free(script_root);
1214 return NULL;
1215 }
1216
1217 *str = '\0';
1218 return script_root;
1219}
1220
1d037ca1
IT
1221static int list_available_scripts(const struct option *opt __maybe_unused,
1222 const char *s __maybe_unused,
1223 int unset __maybe_unused)
4b9c0c59
TZ
1224{
1225 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1226 char scripts_path[MAXPATHLEN];
1227 DIR *scripts_dir, *lang_dir;
1228 char script_path[MAXPATHLEN];
1229 char lang_path[MAXPATHLEN];
1230 struct script_desc *desc;
1231 char first_half[BUFSIZ];
1232 char *script_root;
4b9c0c59
TZ
1233
1234 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1235
1236 scripts_dir = opendir(scripts_path);
1237 if (!scripts_dir)
1238 return -1;
1239
008f29d3 1240 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
4b9c0c59
TZ
1241 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1242 lang_dirent.d_name);
1243 lang_dir = opendir(lang_path);
1244 if (!lang_dir)
1245 continue;
1246
008f29d3 1247 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
38efb539
RR
1248 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1249 if (script_root) {
4b9c0c59
TZ
1250 desc = script_desc__findnew(script_root);
1251 snprintf(script_path, MAXPATHLEN, "%s/%s",
1252 lang_path, script_dirent.d_name);
1253 read_script_info(desc, script_path);
38efb539 1254 free(script_root);
4b9c0c59 1255 }
4b9c0c59
TZ
1256 }
1257 }
1258
1259 fprintf(stdout, "List of available trace scripts:\n");
1260 list_for_each_entry(desc, &script_descs, node) {
1261 sprintf(first_half, "%s %s", desc->name,
1262 desc->args ? desc->args : "");
1263 fprintf(stdout, " %-36s %s\n", first_half,
1264 desc->half_liner ? desc->half_liner : "");
1265 }
1266
1267 exit(0);
1268}
1269
49e639e2
FT
1270/*
1271 * Some scripts specify the required events in their "xxx-record" file,
1272 * this function will check if the events in perf.data match those
1273 * mentioned in the "xxx-record".
1274 *
1275 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1276 * which is covered well now. And new parsing code should be added to
1277 * cover the future complexing formats like event groups etc.
1278 */
1279static int check_ev_match(char *dir_name, char *scriptname,
1280 struct perf_session *session)
1281{
1282 char filename[MAXPATHLEN], evname[128];
1283 char line[BUFSIZ], *p;
1284 struct perf_evsel *pos;
1285 int match, len;
1286 FILE *fp;
1287
1288 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1289
1290 fp = fopen(filename, "r");
1291 if (!fp)
1292 return -1;
1293
1294 while (fgets(line, sizeof(line), fp)) {
1295 p = ltrim(line);
1296 if (*p == '#')
1297 continue;
1298
1299 while (strlen(p)) {
1300 p = strstr(p, "-e");
1301 if (!p)
1302 break;
1303
1304 p += 2;
1305 p = ltrim(p);
1306 len = strcspn(p, " \t");
1307 if (!len)
1308 break;
1309
1310 snprintf(evname, len + 1, "%s", p);
1311
1312 match = 0;
0050f7aa 1313 evlist__for_each(session->evlist, pos) {
49e639e2
FT
1314 if (!strcmp(perf_evsel__name(pos), evname)) {
1315 match = 1;
1316 break;
1317 }
1318 }
1319
1320 if (!match) {
1321 fclose(fp);
1322 return -1;
1323 }
1324 }
1325 }
1326
1327 fclose(fp);
1328 return 0;
1329}
1330
e5f3705e
FT
1331/*
1332 * Return -1 if none is found, otherwise the actual scripts number.
1333 *
1334 * Currently the only user of this function is the script browser, which
1335 * will list all statically runnable scripts, select one, execute it and
1336 * show the output in a perf browser.
1337 */
1338int find_scripts(char **scripts_array, char **scripts_path_array)
1339{
1340 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
49e639e2 1341 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
e5f3705e 1342 DIR *scripts_dir, *lang_dir;
49e639e2 1343 struct perf_session *session;
f5fc1412
JO
1344 struct perf_data_file file = {
1345 .path = input_name,
1346 .mode = PERF_DATA_MODE_READ,
1347 };
e5f3705e
FT
1348 char *temp;
1349 int i = 0;
1350
f5fc1412 1351 session = perf_session__new(&file, false, NULL);
49e639e2
FT
1352 if (!session)
1353 return -1;
1354
e5f3705e
FT
1355 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1356
1357 scripts_dir = opendir(scripts_path);
49e639e2
FT
1358 if (!scripts_dir) {
1359 perf_session__delete(session);
e5f3705e 1360 return -1;
49e639e2 1361 }
e5f3705e
FT
1362
1363 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1364 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1365 lang_dirent.d_name);
1366#ifdef NO_LIBPERL
1367 if (strstr(lang_path, "perl"))
1368 continue;
1369#endif
1370#ifdef NO_LIBPYTHON
1371 if (strstr(lang_path, "python"))
1372 continue;
1373#endif
1374
1375 lang_dir = opendir(lang_path);
1376 if (!lang_dir)
1377 continue;
1378
1379 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1380 /* Skip those real time scripts: xxxtop.p[yl] */
1381 if (strstr(script_dirent.d_name, "top."))
1382 continue;
1383 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1384 script_dirent.d_name);
1385 temp = strchr(script_dirent.d_name, '.');
1386 snprintf(scripts_array[i],
1387 (temp - script_dirent.d_name) + 1,
1388 "%s", script_dirent.d_name);
49e639e2
FT
1389
1390 if (check_ev_match(lang_path,
1391 scripts_array[i], session))
1392 continue;
1393
e5f3705e
FT
1394 i++;
1395 }
49e639e2 1396 closedir(lang_dir);
e5f3705e
FT
1397 }
1398
49e639e2
FT
1399 closedir(scripts_dir);
1400 perf_session__delete(session);
e5f3705e
FT
1401 return i;
1402}
1403
3875294f
TZ
1404static char *get_script_path(const char *script_root, const char *suffix)
1405{
1406 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1407 char scripts_path[MAXPATHLEN];
1408 char script_path[MAXPATHLEN];
1409 DIR *scripts_dir, *lang_dir;
1410 char lang_path[MAXPATHLEN];
38efb539 1411 char *__script_root;
3875294f
TZ
1412
1413 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1414
1415 scripts_dir = opendir(scripts_path);
1416 if (!scripts_dir)
1417 return NULL;
1418
008f29d3 1419 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
3875294f
TZ
1420 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1421 lang_dirent.d_name);
1422 lang_dir = opendir(lang_path);
1423 if (!lang_dir)
1424 continue;
1425
008f29d3 1426 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
38efb539
RR
1427 __script_root = get_script_root(&script_dirent, suffix);
1428 if (__script_root && !strcmp(script_root, __script_root)) {
1429 free(__script_root);
946ef2a2
NK
1430 closedir(lang_dir);
1431 closedir(scripts_dir);
3875294f
TZ
1432 snprintf(script_path, MAXPATHLEN, "%s/%s",
1433 lang_path, script_dirent.d_name);
38efb539 1434 return strdup(script_path);
3875294f
TZ
1435 }
1436 free(__script_root);
1437 }
946ef2a2 1438 closedir(lang_dir);
3875294f 1439 }
946ef2a2 1440 closedir(scripts_dir);
3875294f 1441
38efb539 1442 return NULL;
3875294f
TZ
1443}
1444
b5b87312
TZ
1445static bool is_top_script(const char *script_path)
1446{
965bb6be 1447 return ends_with(script_path, "top") == NULL ? false : true;
b5b87312
TZ
1448}
1449
1450static int has_required_arg(char *script_path)
1451{
1452 struct script_desc *desc;
1453 int n_args = 0;
1454 char *p;
1455
1456 desc = script_desc__new(NULL);
1457
1458 if (read_script_info(desc, script_path))
1459 goto out;
1460
1461 if (!desc->args)
1462 goto out;
1463
1464 for (p = desc->args; *p; p++)
1465 if (*p == '<')
1466 n_args++;
1467out:
1468 script_desc__delete(desc);
1469
1470 return n_args;
1471}
1472
69b6470e
ACM
1473static int have_cmd(int argc, const char **argv)
1474{
1475 char **__argv = malloc(sizeof(const char *) * argc);
1476
1477 if (!__argv) {
1478 pr_err("malloc failed\n");
1479 return -1;
1480 }
1481
1482 memcpy(__argv, argv, sizeof(const char *) * argc);
1483 argc = parse_options(argc, (const char **)__argv, record_options,
1484 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1485 free(__argv);
5f9c39dc 1486
69b6470e
ACM
1487 system_wide = (argc == 0);
1488
1489 return 0;
1490}
1491
1492int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1493{
1494 bool show_full_info = false;
e90debdd
JO
1495 bool header = false;
1496 bool header_only = false;
6cc870f0 1497 bool script_started = false;
69b6470e
ACM
1498 char *rec_script_path = NULL;
1499 char *rep_script_path = NULL;
1500 struct perf_session *session;
7a680eb9 1501 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
69b6470e
ACM
1502 char *script_path = NULL;
1503 const char **__argv;
6cc870f0 1504 int i, j, err = 0;
6f3e5eda
AH
1505 struct perf_script script = {
1506 .tool = {
1507 .sample = process_sample_event,
1508 .mmap = perf_event__process_mmap,
1509 .mmap2 = perf_event__process_mmap2,
1510 .comm = perf_event__process_comm,
1511 .exit = perf_event__process_exit,
1512 .fork = perf_event__process_fork,
7ea95727 1513 .attr = process_attr,
6f3e5eda
AH
1514 .tracing_data = perf_event__process_tracing_data,
1515 .build_id = perf_event__process_build_id,
7a680eb9
AH
1516 .id_index = perf_event__process_id_index,
1517 .auxtrace_info = perf_event__process_auxtrace_info,
1518 .auxtrace = perf_event__process_auxtrace,
1519 .auxtrace_error = perf_event__process_auxtrace_error,
0a8cb85c 1520 .ordered_events = true,
6f3e5eda
AH
1521 .ordering_requires_timestamps = true,
1522 },
1523 };
06af0f2c
YS
1524 struct perf_data_file file = {
1525 .mode = PERF_DATA_MODE_READ,
1526 };
69b6470e 1527 const struct option options[] = {
5f9c39dc
FW
1528 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1529 "dump raw trace in ASCII"),
c0555642 1530 OPT_INCR('v', "verbose", &verbose,
69b6470e 1531 "be more verbose (show symbol address, etc)"),
4b9c0c59 1532 OPT_BOOLEAN('L', "Latency", &latency_format,
cda48461 1533 "show latency attributes (irqs/preemption disabled, etc)"),
4b9c0c59
TZ
1534 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1535 list_available_scripts),
956ffd02
TZ
1536 OPT_CALLBACK('s', "script", NULL, "name",
1537 "script file name (lang:script name, script name, or *)",
1538 parse_scriptname),
1539 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
133dc4c3 1540 "generate perf-script.xx script in specified language"),
69b6470e 1541 OPT_STRING('i', "input", &input_name, "file", "input file name"),
ffabd99e
FW
1542 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1543 "do various checks like samples ordering and lost events"),
e90debdd
JO
1544 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1545 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
c0230b2b
DA
1546 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1547 "file", "vmlinux pathname"),
1548 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1549 "file", "kallsyms pathname"),
1550 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1551 "When printing symbols do not display call chain"),
1552 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1553 "Look for files with symbols relative to this directory"),
06af0f2c 1554 OPT_CALLBACK('F', "fields", NULL, "str",
a978f2ab
AN
1555 "comma separated output fields prepend with 'type:'. "
1556 "Valid types: hw,sw,trace,raw. "
1557 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
535aeaae 1558 "addr,symoff,period", parse_output_fields),
317df650 1559 OPT_BOOLEAN('a', "all-cpus", &system_wide,
69b6470e 1560 "system-wide collection from all CPUs"),
36385be5
FT
1561 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1562 "only consider these symbols"),
c8e66720 1563 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
e7984b7b
DA
1564 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1565 "only display events for these comms"),
e03eaa40
DA
1566 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1567 "only consider symbols in these pids"),
1568 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1569 "only consider symbols in these tids"),
fbe96f29
SE
1570 OPT_BOOLEAN('I', "show-info", &show_full_info,
1571 "display extended information from perf.data file"),
0bc8d205
AN
1572 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1573 "Show the path of [kernel.kallsyms]"),
ad7ebb9a
NK
1574 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1575 "Show the fork/comm/exit events"),
ba1ddf42
NK
1576 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1577 "Show the mmap events"),
06af0f2c 1578 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
7a680eb9
AH
1579 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1580 "Instruction Tracing options",
1581 itrace_parse_synth_opts),
1909629f 1582 OPT_END()
69b6470e 1583 };
40cae2b7
YS
1584 const char * const script_subcommands[] = { "record", "report", NULL };
1585 const char *script_usage[] = {
69b6470e
ACM
1586 "perf script [<options>]",
1587 "perf script [<options>] record <script> [<record-options>] <command>",
1588 "perf script [<options>] report <script> [script-args]",
1589 "perf script [<options>] <script> [<record-options>] <command>",
1590 "perf script [<options>] <top-script> [script-args]",
1591 NULL
1592 };
3875294f 1593
b5b87312
TZ
1594 setup_scripting();
1595
40cae2b7 1596 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
b5b87312
TZ
1597 PARSE_OPT_STOP_AT_NON_OPTION);
1598
f5fc1412
JO
1599 file.path = input_name;
1600
b5b87312
TZ
1601 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1602 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1603 if (!rec_script_path)
1604 return cmd_record(argc, argv, NULL);
3875294f
TZ
1605 }
1606
b5b87312
TZ
1607 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1608 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1609 if (!rep_script_path) {
3875294f 1610 fprintf(stderr,
b5b87312 1611 "Please specify a valid report script"
133dc4c3 1612 "(see 'perf script -l' for listing)\n");
3875294f
TZ
1613 return -1;
1614 }
3875294f
TZ
1615 }
1616
44e668c6
BH
1617 /* make sure PERF_EXEC_PATH is set for scripts */
1618 perf_set_argv_exec_path(perf_exec_path());
1619
b5b87312 1620 if (argc && !script_name && !rec_script_path && !rep_script_path) {
a0cccc2e 1621 int live_pipe[2];
b5b87312 1622 int rep_args;
a0cccc2e
TZ
1623 pid_t pid;
1624
b5b87312
TZ
1625 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1626 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1627
1628 if (!rec_script_path && !rep_script_path) {
1629 fprintf(stderr, " Couldn't find script %s\n\n See perf"
133dc4c3
IM
1630 " script -l for available scripts.\n", argv[0]);
1631 usage_with_options(script_usage, options);
a0cccc2e
TZ
1632 }
1633
b5b87312
TZ
1634 if (is_top_script(argv[0])) {
1635 rep_args = argc - 1;
1636 } else {
1637 int rec_args;
1638
1639 rep_args = has_required_arg(rep_script_path);
1640 rec_args = (argc - 1) - rep_args;
1641 if (rec_args < 0) {
1642 fprintf(stderr, " %s script requires options."
133dc4c3 1643 "\n\n See perf script -l for available "
b5b87312 1644 "scripts and options.\n", argv[0]);
133dc4c3 1645 usage_with_options(script_usage, options);
b5b87312 1646 }
a0cccc2e
TZ
1647 }
1648
1649 if (pipe(live_pipe) < 0) {
1650 perror("failed to create pipe");
d54b1a9e 1651 return -1;
a0cccc2e
TZ
1652 }
1653
1654 pid = fork();
1655 if (pid < 0) {
1656 perror("failed to fork");
d54b1a9e 1657 return -1;
a0cccc2e
TZ
1658 }
1659
1660 if (!pid) {
b5b87312
TZ
1661 j = 0;
1662
a0cccc2e
TZ
1663 dup2(live_pipe[1], 1);
1664 close(live_pipe[0]);
1665
317df650
RR
1666 if (is_top_script(argv[0])) {
1667 system_wide = true;
1668 } else if (!system_wide) {
d54b1a9e
DA
1669 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1670 err = -1;
1671 goto out;
1672 }
317df650 1673 }
b5b87312
TZ
1674
1675 __argv = malloc((argc + 6) * sizeof(const char *));
d54b1a9e
DA
1676 if (!__argv) {
1677 pr_err("malloc failed\n");
1678 err = -ENOMEM;
1679 goto out;
1680 }
e8719adf 1681
b5b87312
TZ
1682 __argv[j++] = "/bin/sh";
1683 __argv[j++] = rec_script_path;
1684 if (system_wide)
1685 __argv[j++] = "-a";
1686 __argv[j++] = "-q";
1687 __argv[j++] = "-o";
1688 __argv[j++] = "-";
1689 for (i = rep_args + 1; i < argc; i++)
1690 __argv[j++] = argv[i];
1691 __argv[j++] = NULL;
a0cccc2e
TZ
1692
1693 execvp("/bin/sh", (char **)__argv);
e8719adf 1694 free(__argv);
a0cccc2e
TZ
1695 exit(-1);
1696 }
1697
1698 dup2(live_pipe[0], 0);
1699 close(live_pipe[1]);
1700
b5b87312 1701 __argv = malloc((argc + 4) * sizeof(const char *));
d54b1a9e
DA
1702 if (!__argv) {
1703 pr_err("malloc failed\n");
1704 err = -ENOMEM;
1705 goto out;
1706 }
1707
b5b87312
TZ
1708 j = 0;
1709 __argv[j++] = "/bin/sh";
1710 __argv[j++] = rep_script_path;
1711 for (i = 1; i < rep_args + 1; i++)
1712 __argv[j++] = argv[i];
1713 __argv[j++] = "-i";
1714 __argv[j++] = "-";
1715 __argv[j++] = NULL;
a0cccc2e
TZ
1716
1717 execvp("/bin/sh", (char **)__argv);
e8719adf 1718 free(__argv);
a0cccc2e
TZ
1719 exit(-1);
1720 }
1721
b5b87312
TZ
1722 if (rec_script_path)
1723 script_path = rec_script_path;
1724 if (rep_script_path)
1725 script_path = rep_script_path;
34c86ea9 1726
b5b87312 1727 if (script_path) {
b5b87312 1728 j = 0;
3875294f 1729
317df650
RR
1730 if (!rec_script_path)
1731 system_wide = false;
d54b1a9e
DA
1732 else if (!system_wide) {
1733 if (have_cmd(argc - 1, &argv[1]) != 0) {
1734 err = -1;
1735 goto out;
1736 }
1737 }
34c86ea9 1738
b5b87312 1739 __argv = malloc((argc + 2) * sizeof(const char *));
d54b1a9e
DA
1740 if (!__argv) {
1741 pr_err("malloc failed\n");
1742 err = -ENOMEM;
1743 goto out;
1744 }
1745
34c86ea9
TZ
1746 __argv[j++] = "/bin/sh";
1747 __argv[j++] = script_path;
1748 if (system_wide)
1749 __argv[j++] = "-a";
b5b87312 1750 for (i = 2; i < argc; i++)
34c86ea9
TZ
1751 __argv[j++] = argv[i];
1752 __argv[j++] = NULL;
3875294f
TZ
1753
1754 execvp("/bin/sh", (char **)__argv);
e8719adf 1755 free(__argv);
3875294f
TZ
1756 exit(-1);
1757 }
956ffd02 1758
cf4fee50
TZ
1759 if (!script_name)
1760 setup_pager();
5f9c39dc 1761
6f3e5eda 1762 session = perf_session__new(&file, false, &script.tool);
d8f66248 1763 if (session == NULL)
52e02834 1764 return -1;
d8f66248 1765
e90debdd
JO
1766 if (header || header_only) {
1767 perf_session__fprintf_info(session, stdout, show_full_info);
1768 if (header_only)
6cc870f0 1769 goto out_delete;
e90debdd
JO
1770 }
1771
0a7e6d1b 1772 if (symbol__init(&session->header.env) < 0)
38520dc3
NK
1773 goto out_delete;
1774
6f3e5eda
AH
1775 script.session = session;
1776
7a680eb9
AH
1777 session->itrace_synth_opts = &itrace_synth_opts;
1778
5d67be97 1779 if (cpu_list) {
6cc870f0
NK
1780 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1781 if (err < 0)
1782 goto out_delete;
5d67be97
AB
1783 }
1784
1424dc96 1785 if (!no_callchain)
c0230b2b
DA
1786 symbol_conf.use_callchain = true;
1787 else
1788 symbol_conf.use_callchain = false;
1789
956ffd02
TZ
1790 if (generate_script_lang) {
1791 struct stat perf_stat;
745f43e3
DA
1792 int input;
1793
2c9e45f7 1794 if (output_set_by_user()) {
745f43e3
DA
1795 fprintf(stderr,
1796 "custom fields not supported for generated scripts");
6cc870f0
NK
1797 err = -EINVAL;
1798 goto out_delete;
745f43e3 1799 }
956ffd02 1800
cc9784bd 1801 input = open(file.path, O_RDONLY); /* input_name */
956ffd02 1802 if (input < 0) {
6cc870f0 1803 err = -errno;
956ffd02 1804 perror("failed to open file");
6cc870f0 1805 goto out_delete;
956ffd02
TZ
1806 }
1807
1808 err = fstat(input, &perf_stat);
1809 if (err < 0) {
1810 perror("failed to stat file");
6cc870f0 1811 goto out_delete;
956ffd02
TZ
1812 }
1813
1814 if (!perf_stat.st_size) {
1815 fprintf(stderr, "zero-sized file, nothing to do!\n");
6cc870f0 1816 goto out_delete;
956ffd02
TZ
1817 }
1818
1819 scripting_ops = script_spec__lookup(generate_script_lang);
1820 if (!scripting_ops) {
1821 fprintf(stderr, "invalid language specifier");
6cc870f0
NK
1822 err = -ENOENT;
1823 goto out_delete;
956ffd02
TZ
1824 }
1825
29f5ffd3 1826 err = scripting_ops->generate_script(session->tevent.pevent,
da378962 1827 "perf-script");
6cc870f0 1828 goto out_delete;
956ffd02
TZ
1829 }
1830
1831 if (script_name) {
586bc5cc 1832 err = scripting_ops->start_script(script_name, argc, argv);
956ffd02 1833 if (err)
6cc870f0 1834 goto out_delete;
133dc4c3 1835 pr_debug("perf script started with script %s\n\n", script_name);
6cc870f0 1836 script_started = true;
956ffd02
TZ
1837 }
1838
9cbdb702
DA
1839
1840 err = perf_session__check_output_opt(session);
1841 if (err < 0)
6cc870f0 1842 goto out_delete;
9cbdb702 1843
6f3e5eda 1844 err = __cmd_script(&script);
956ffd02 1845
d445dd2a
AH
1846 flush_scripting();
1847
6cc870f0 1848out_delete:
d8f66248 1849 perf_session__delete(session);
6cc870f0
NK
1850
1851 if (script_started)
1852 cleanup_scripting();
956ffd02
TZ
1853out:
1854 return err;
5f9c39dc 1855}