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