Merge tag 'leds_for_4.6' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewsk...
[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 5#include "util/debug.h"
4b6ab94e 6#include <subcmd/exec-cmd.h>
b7eead86 7#include "util/header.h"
4b6ab94e 8#include <subcmd/parse-options.h>
fc36f948 9#include "util/perf_regs.h"
b7eead86 10#include "util/session.h"
45694aa7 11#include "util/tool.h"
5f9c39dc
FW
12#include "util/symbol.h"
13#include "util/thread.h"
cf72344d 14#include "util/trace-event.h"
b7eead86 15#include "util/util.h"
1424dc96
DA
16#include "util/evlist.h"
17#include "util/evsel.h"
36385be5 18#include "util/sort.h"
f5fc1412 19#include "util/data.h"
7a680eb9 20#include "util/auxtrace.h"
cfc8874a
JO
21#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
5d67be97 24#include <linux/bitmap.h>
cfc8874a 25#include "asm/bug.h"
c19ac912 26#include "util/mem-events.h"
5f9c39dc 27
956ffd02
TZ
28static char const *script_name;
29static char const *generate_script_lang;
ffabd99e 30static bool debug_mode;
e1889d75 31static u64 last_timestamp;
6fcf7ddb 32static u64 nr_unordered;
c0230b2b 33static bool no_callchain;
47390ae2 34static bool latency_format;
317df650 35static bool system_wide;
400ea6d3 36static bool print_flags;
83e19860 37static bool nanosecs;
5d67be97
AB
38static const char *cpu_list;
39static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
91a2c3d5 40static struct perf_stat_config stat_config;
956ffd02 41
44cbe729 42unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
03cd1fed 43
745f43e3
DA
44enum perf_output_field {
45 PERF_OUTPUT_COMM = 1U << 0,
46 PERF_OUTPUT_TID = 1U << 1,
47 PERF_OUTPUT_PID = 1U << 2,
48 PERF_OUTPUT_TIME = 1U << 3,
49 PERF_OUTPUT_CPU = 1U << 4,
50 PERF_OUTPUT_EVNAME = 1U << 5,
51 PERF_OUTPUT_TRACE = 1U << 6,
787bef17
DA
52 PERF_OUTPUT_IP = 1U << 7,
53 PERF_OUTPUT_SYM = 1U << 8,
610723f2 54 PERF_OUTPUT_DSO = 1U << 9,
7cec0922 55 PERF_OUTPUT_ADDR = 1U << 10,
a978f2ab 56 PERF_OUTPUT_SYMOFFSET = 1U << 11,
cc8fae1d 57 PERF_OUTPUT_SRCLINE = 1U << 12,
535aeaae 58 PERF_OUTPUT_PERIOD = 1U << 13,
fc36f948 59 PERF_OUTPUT_IREGS = 1U << 14,
dc323ce8
SE
60 PERF_OUTPUT_BRSTACK = 1U << 15,
61 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
94ddddfa
JO
62 PERF_OUTPUT_DATA_SRC = 1U << 17,
63 PERF_OUTPUT_WEIGHT = 1U << 18,
30372f04 64 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
745f43e3
DA
65};
66
67struct output_option {
68 const char *str;
69 enum perf_output_field field;
70} all_output_options[] = {
71 {.str = "comm", .field = PERF_OUTPUT_COMM},
72 {.str = "tid", .field = PERF_OUTPUT_TID},
73 {.str = "pid", .field = PERF_OUTPUT_PID},
74 {.str = "time", .field = PERF_OUTPUT_TIME},
75 {.str = "cpu", .field = PERF_OUTPUT_CPU},
76 {.str = "event", .field = PERF_OUTPUT_EVNAME},
77 {.str = "trace", .field = PERF_OUTPUT_TRACE},
787bef17 78 {.str = "ip", .field = PERF_OUTPUT_IP},
c0230b2b 79 {.str = "sym", .field = PERF_OUTPUT_SYM},
610723f2 80 {.str = "dso", .field = PERF_OUTPUT_DSO},
7cec0922 81 {.str = "addr", .field = PERF_OUTPUT_ADDR},
a978f2ab 82 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
cc8fae1d 83 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
535aeaae 84 {.str = "period", .field = PERF_OUTPUT_PERIOD},
fc36f948 85 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
dc323ce8
SE
86 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
87 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
94ddddfa
JO
88 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
89 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
30372f04 90 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
745f43e3
DA
91};
92
93/* default set to maintain compatibility with current format */
2c9e45f7
DA
94static struct {
95 bool user_set;
9cbdb702 96 bool wildcard_set;
a6ffaf91 97 unsigned int print_ip_opts;
2c9e45f7
DA
98 u64 fields;
99 u64 invalid_fields;
100} output[PERF_TYPE_MAX] = {
101
102 [PERF_TYPE_HARDWARE] = {
103 .user_set = false,
104
105 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
106 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 107 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71
JO
108 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
109 PERF_OUTPUT_PERIOD,
2c9e45f7 110
30372f04 111 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
2c9e45f7
DA
112 },
113
114 [PERF_TYPE_SOFTWARE] = {
115 .user_set = false,
116
117 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
118 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 119 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71 120 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
30372f04 121 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
2c9e45f7
DA
122
123 .invalid_fields = PERF_OUTPUT_TRACE,
124 },
125
126 [PERF_TYPE_TRACEPOINT] = {
127 .user_set = false,
128
129 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
130 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
30372f04 131 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
2c9e45f7 132 },
0817a6a3
AS
133
134 [PERF_TYPE_RAW] = {
135 .user_set = false,
136
137 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
138 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 139 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71 140 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
ff7b1915
JO
141 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
142 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
0817a6a3 143
30372f04 144 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
0817a6a3 145 },
27cfef00
WN
146
147 [PERF_TYPE_BREAKPOINT] = {
148 .user_set = false,
149
150 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
151 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
152 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
153 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
154 PERF_OUTPUT_PERIOD,
155
30372f04 156 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
27cfef00 157 },
1424dc96 158};
745f43e3 159
2c9e45f7
DA
160static bool output_set_by_user(void)
161{
162 int j;
163 for (j = 0; j < PERF_TYPE_MAX; ++j) {
164 if (output[j].user_set)
165 return true;
166 }
167 return false;
168}
745f43e3 169
9cbdb702
DA
170static const char *output_field2str(enum perf_output_field field)
171{
172 int i, imax = ARRAY_SIZE(all_output_options);
173 const char *str = "";
174
175 for (i = 0; i < imax; ++i) {
176 if (all_output_options[i].field == field) {
177 str = all_output_options[i].str;
178 break;
179 }
180 }
181 return str;
182}
183
2c9e45f7 184#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
1424dc96 185
6d5cdd64
AH
186static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
187 u64 sample_type, const char *sample_msg,
188 enum perf_output_field field,
189 bool allow_user_set)
1424dc96 190{
5bff01f6 191 struct perf_event_attr *attr = &evsel->attr;
9cbdb702
DA
192 int type = attr->type;
193 const char *evname;
194
195 if (attr->sample_type & sample_type)
196 return 0;
197
198 if (output[type].user_set) {
6d5cdd64
AH
199 if (allow_user_set)
200 return 0;
5bff01f6 201 evname = perf_evsel__name(evsel);
9cbdb702
DA
202 pr_err("Samples for '%s' event do not have %s attribute set. "
203 "Cannot print '%s' field.\n",
204 evname, sample_msg, output_field2str(field));
205 return -1;
206 }
207
208 /* user did not ask for it explicitly so remove from the default list */
209 output[type].fields &= ~field;
5bff01f6 210 evname = perf_evsel__name(evsel);
9cbdb702
DA
211 pr_debug("Samples for '%s' event do not have %s attribute set. "
212 "Skipping '%s' field.\n",
213 evname, sample_msg, output_field2str(field));
214
215 return 0;
216}
217
6d5cdd64
AH
218static int perf_evsel__check_stype(struct perf_evsel *evsel,
219 u64 sample_type, const char *sample_msg,
220 enum perf_output_field field)
221{
222 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
223 false);
224}
225
9cbdb702
DA
226static int perf_evsel__check_attr(struct perf_evsel *evsel,
227 struct perf_session *session)
228{
229 struct perf_event_attr *attr = &evsel->attr;
6d5cdd64
AH
230 bool allow_user_set;
231
e099eba8
JO
232 if (perf_header__has_feat(&session->header, HEADER_STAT))
233 return 0;
234
6d5cdd64
AH
235 allow_user_set = perf_header__has_feat(&session->header,
236 HEADER_AUXTRACE);
9cbdb702 237
1424dc96
DA
238 if (PRINT_FIELD(TRACE) &&
239 !perf_session__has_traces(session, "record -R"))
240 return -EINVAL;
241
787bef17 242 if (PRINT_FIELD(IP)) {
5bff01f6
ACM
243 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
244 PERF_OUTPUT_IP))
1424dc96 245 return -EINVAL;
1424dc96 246 }
7cec0922
DA
247
248 if (PRINT_FIELD(ADDR) &&
6d5cdd64
AH
249 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
250 PERF_OUTPUT_ADDR, allow_user_set))
7cec0922
DA
251 return -EINVAL;
252
94ddddfa
JO
253 if (PRINT_FIELD(DATA_SRC) &&
254 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
255 PERF_OUTPUT_DATA_SRC))
256 return -EINVAL;
257
258 if (PRINT_FIELD(WEIGHT) &&
259 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
260 PERF_OUTPUT_WEIGHT))
261 return -EINVAL;
262
7cec0922
DA
263 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
264 pr_err("Display of symbols requested but neither sample IP nor "
265 "sample address\nis selected. Hence, no addresses to convert "
266 "to symbols.\n");
787bef17
DA
267 return -EINVAL;
268 }
a978f2ab
AN
269 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
270 pr_err("Display of offsets requested but symbol is not"
271 "selected.\n");
272 return -EINVAL;
273 }
7cec0922
DA
274 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
275 pr_err("Display of DSO requested but neither sample IP nor "
276 "sample address\nis selected. Hence, no addresses to convert "
277 "to DSO.\n");
610723f2
DA
278 return -EINVAL;
279 }
cc8fae1d
AH
280 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
281 pr_err("Display of source line number requested but sample IP is not\n"
282 "selected. Hence, no address to lookup the source line number.\n");
283 return -EINVAL;
284 }
1424dc96
DA
285
286 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
5bff01f6
ACM
287 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
288 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
1424dc96 289 return -EINVAL;
1424dc96
DA
290
291 if (PRINT_FIELD(TIME) &&
5bff01f6
ACM
292 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
293 PERF_OUTPUT_TIME))
1424dc96 294 return -EINVAL;
1424dc96
DA
295
296 if (PRINT_FIELD(CPU) &&
6d5cdd64
AH
297 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
298 PERF_OUTPUT_CPU, allow_user_set))
1424dc96 299 return -EINVAL;
9cbdb702 300
535aeaae
JO
301 if (PRINT_FIELD(PERIOD) &&
302 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
303 PERF_OUTPUT_PERIOD))
304 return -EINVAL;
305
fc36f948
SE
306 if (PRINT_FIELD(IREGS) &&
307 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
308 PERF_OUTPUT_IREGS))
309 return -EINVAL;
310
9cbdb702
DA
311 return 0;
312}
313
7ea95727
AH
314static void set_print_ip_opts(struct perf_event_attr *attr)
315{
316 unsigned int type = attr->type;
317
318 output[type].print_ip_opts = 0;
319 if (PRINT_FIELD(IP))
320 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
321
322 if (PRINT_FIELD(SYM))
323 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
324
325 if (PRINT_FIELD(DSO))
326 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
327
328 if (PRINT_FIELD(SYMOFFSET))
329 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
cc8fae1d
AH
330
331 if (PRINT_FIELD(SRCLINE))
332 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
7ea95727
AH
333}
334
9cbdb702
DA
335/*
336 * verify all user requested events exist and the samples
337 * have the expected data
338 */
339static int perf_session__check_output_opt(struct perf_session *session)
340{
341 int j;
342 struct perf_evsel *evsel;
343
344 for (j = 0; j < PERF_TYPE_MAX; ++j) {
345 evsel = perf_session__find_first_evtype(session, j);
346
347 /*
348 * even if fields is set to 0 (ie., show nothing) event must
349 * exist if user explicitly includes it on the command line
350 */
351 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
352 pr_err("%s events do not exist. "
353 "Remove corresponding -f option to proceed.\n",
354 event_type(j));
355 return -1;
356 }
357
358 if (evsel && output[j].fields &&
359 perf_evsel__check_attr(evsel, session))
360 return -1;
a6ffaf91
DA
361
362 if (evsel == NULL)
363 continue;
364
7ea95727 365 set_print_ip_opts(&evsel->attr);
1424dc96
DA
366 }
367
98526ee7
AH
368 if (!no_callchain) {
369 bool use_callchain = false;
370
371 evlist__for_each(session->evlist, evsel) {
372 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
373 use_callchain = true;
374 break;
375 }
376 }
377 if (!use_callchain)
378 symbol_conf.use_callchain = false;
379 }
380
80b8b496
DA
381 /*
382 * set default for tracepoints to print symbols only
383 * if callchains are present
384 */
385 if (symbol_conf.use_callchain &&
386 !output[PERF_TYPE_TRACEPOINT].user_set) {
387 struct perf_event_attr *attr;
388
389 j = PERF_TYPE_TRACEPOINT;
390 evsel = perf_session__find_first_evtype(session, j);
391 if (evsel == NULL)
392 goto out;
393
394 attr = &evsel->attr;
395
396 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
397 output[j].fields |= PERF_OUTPUT_IP;
398 output[j].fields |= PERF_OUTPUT_SYM;
399 output[j].fields |= PERF_OUTPUT_DSO;
400 set_print_ip_opts(attr);
401 }
402 }
403
404out:
1424dc96
DA
405 return 0;
406}
745f43e3 407
fc36f948
SE
408static void print_sample_iregs(union perf_event *event __maybe_unused,
409 struct perf_sample *sample,
410 struct thread *thread __maybe_unused,
411 struct perf_event_attr *attr)
412{
413 struct regs_dump *regs = &sample->intr_regs;
414 uint64_t mask = attr->sample_regs_intr;
415 unsigned i = 0, r;
416
417 if (!regs)
418 return;
419
420 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
421 u64 val = regs->regs[i++];
422 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
423 }
424}
425
fcf65bf1 426static void print_sample_start(struct perf_sample *sample,
1424dc96 427 struct thread *thread,
5bff01f6 428 struct perf_evsel *evsel)
c70c94b4 429{
5bff01f6 430 struct perf_event_attr *attr = &evsel->attr;
c70c94b4
DA
431 unsigned long secs;
432 unsigned long usecs;
745f43e3
DA
433 unsigned long long nsecs;
434
435 if (PRINT_FIELD(COMM)) {
436 if (latency_format)
b9c5143a 437 printf("%8.8s ", thread__comm_str(thread));
787bef17 438 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
b9c5143a 439 printf("%s ", thread__comm_str(thread));
745f43e3 440 else
b9c5143a 441 printf("%16s ", thread__comm_str(thread));
745f43e3 442 }
c70c94b4 443
745f43e3
DA
444 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
445 printf("%5d/%-5d ", sample->pid, sample->tid);
446 else if (PRINT_FIELD(PID))
447 printf("%5d ", sample->pid);
448 else if (PRINT_FIELD(TID))
449 printf("%5d ", sample->tid);
450
451 if (PRINT_FIELD(CPU)) {
452 if (latency_format)
453 printf("%3d ", sample->cpu);
454 else
455 printf("[%03d] ", sample->cpu);
456 }
c70c94b4 457
745f43e3
DA
458 if (PRINT_FIELD(TIME)) {
459 nsecs = sample->time;
460 secs = nsecs / NSECS_PER_SEC;
461 nsecs -= secs * NSECS_PER_SEC;
462 usecs = nsecs / NSECS_PER_USEC;
83e19860
AH
463 if (nanosecs)
464 printf("%5lu.%09llu: ", secs, nsecs);
465 else
466 printf("%5lu.%06lu: ", secs, usecs);
745f43e3 467 }
c70c94b4
DA
468}
469
dc323ce8
SE
470static inline char
471mispred_str(struct branch_entry *br)
472{
473 if (!(br->flags.mispred || br->flags.predicted))
474 return '-';
475
476 return br->flags.predicted ? 'P' : 'M';
477}
478
479static void print_sample_brstack(union perf_event *event __maybe_unused,
480 struct perf_sample *sample,
481 struct thread *thread __maybe_unused,
482 struct perf_event_attr *attr __maybe_unused)
483{
484 struct branch_stack *br = sample->branch_stack;
485 u64 i;
486
487 if (!(br && br->nr))
488 return;
489
490 for (i = 0; i < br->nr; i++) {
491 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
492 br->entries[i].from,
493 br->entries[i].to,
494 mispred_str( br->entries + i),
495 br->entries[i].flags.in_tx? 'X' : '-',
496 br->entries[i].flags.abort? 'A' : '-',
497 br->entries[i].flags.cycles);
498 }
499}
500
501static void print_sample_brstacksym(union perf_event *event __maybe_unused,
502 struct perf_sample *sample,
503 struct thread *thread __maybe_unused,
504 struct perf_event_attr *attr __maybe_unused)
505{
506 struct branch_stack *br = sample->branch_stack;
507 struct addr_location alf, alt;
508 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
509 u64 i, from, to;
510
511 if (!(br && br->nr))
512 return;
513
514 for (i = 0; i < br->nr; i++) {
515
516 memset(&alf, 0, sizeof(alf));
517 memset(&alt, 0, sizeof(alt));
518 from = br->entries[i].from;
519 to = br->entries[i].to;
520
521 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
522 if (alf.map)
523 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
524
525 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
526 if (alt.map)
527 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
528
529 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
530 putchar('/');
531 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
532 printf("/%c/%c/%c/%d ",
533 mispred_str( br->entries + i),
534 br->entries[i].flags.in_tx? 'X' : '-',
535 br->entries[i].flags.abort? 'A' : '-',
536 br->entries[i].flags.cycles);
537 }
538}
539
540
7cec0922
DA
541static void print_sample_addr(union perf_event *event,
542 struct perf_sample *sample,
7cec0922
DA
543 struct thread *thread,
544 struct perf_event_attr *attr)
545{
546 struct addr_location al;
7cec0922
DA
547
548 printf("%16" PRIx64, sample->addr);
549
550 if (!sample_addr_correlates_sym(attr))
551 return;
552
bb871a9c 553 perf_event__preprocess_sample_addr(event, sample, thread, &al);
7cec0922
DA
554
555 if (PRINT_FIELD(SYM)) {
547a92e0 556 printf(" ");
a978f2ab
AN
557 if (PRINT_FIELD(SYMOFFSET))
558 symbol__fprintf_symname_offs(al.sym, &al, stdout);
559 else
560 symbol__fprintf_symname(al.sym, stdout);
7cec0922
DA
561 }
562
563 if (PRINT_FIELD(DSO)) {
547a92e0
AN
564 printf(" (");
565 map__fprintf_dsoname(al.map, stdout);
566 printf(")");
7cec0922
DA
567 }
568}
569
95582596
AN
570static void print_sample_bts(union perf_event *event,
571 struct perf_sample *sample,
572 struct perf_evsel *evsel,
a2cb3cf2
AH
573 struct thread *thread,
574 struct addr_location *al)
95582596
AN
575{
576 struct perf_event_attr *attr = &evsel->attr;
8066be5f 577 bool print_srcline_last = false;
95582596
AN
578
579 /* print branch_from information */
580 if (PRINT_FIELD(IP)) {
8066be5f
AH
581 unsigned int print_opts = output[attr->type].print_ip_opts;
582
583 if (symbol_conf.use_callchain && sample->callchain) {
95582596 584 printf("\n");
8066be5f
AH
585 } else {
586 printf(" ");
587 if (print_opts & PRINT_IP_OPT_SRCLINE) {
588 print_srcline_last = true;
589 print_opts &= ~PRINT_IP_OPT_SRCLINE;
590 }
591 }
592 perf_evsel__print_ip(evsel, sample, al, print_opts,
03cd1fed 593 scripting_max_stack);
95582596
AN
594 }
595
95582596 596 /* print branch_to information */
243be3dd
AH
597 if (PRINT_FIELD(ADDR) ||
598 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
578bea40
AH
599 !output[attr->type].user_set)) {
600 printf(" => ");
bb871a9c 601 print_sample_addr(event, sample, thread, attr);
578bea40 602 }
95582596 603
8066be5f
AH
604 if (print_srcline_last)
605 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
606
95582596
AN
607 printf("\n");
608}
609
400ea6d3
AH
610static void print_sample_flags(u32 flags)
611{
612 const char *chars = PERF_IP_FLAG_CHARS;
613 const int n = strlen(PERF_IP_FLAG_CHARS);
614 char str[33];
615 int i, pos = 0;
616
617 for (i = 0; i < n; i++, flags >>= 1) {
618 if (flags & 1)
619 str[pos++] = chars[i];
620 }
621 for (; i < 32; i++, flags >>= 1) {
622 if (flags & 1)
623 str[pos++] = '?';
624 }
625 str[pos] = 0;
626 printf(" %-4s ", str);
627}
628
30372f04
WN
629struct printer_data {
630 int line_no;
631 bool hit_nul;
632 bool is_printable;
633};
634
635static void
636print_sample_bpf_output_printer(enum binary_printer_ops op,
637 unsigned int val,
638 void *extra)
639{
640 unsigned char ch = (unsigned char)val;
641 struct printer_data *printer_data = extra;
642
643 switch (op) {
644 case BINARY_PRINT_DATA_BEGIN:
645 printf("\n");
646 break;
647 case BINARY_PRINT_LINE_BEGIN:
648 printf("%17s", !printer_data->line_no ? "BPF output:" :
649 " ");
650 break;
651 case BINARY_PRINT_ADDR:
652 printf(" %04x:", val);
653 break;
654 case BINARY_PRINT_NUM_DATA:
655 printf(" %02x", val);
656 break;
657 case BINARY_PRINT_NUM_PAD:
658 printf(" ");
659 break;
660 case BINARY_PRINT_SEP:
661 printf(" ");
662 break;
663 case BINARY_PRINT_CHAR_DATA:
664 if (printer_data->hit_nul && ch)
665 printer_data->is_printable = false;
666
667 if (!isprint(ch)) {
668 printf("%c", '.');
669
670 if (!printer_data->is_printable)
671 break;
672
673 if (ch == '\0')
674 printer_data->hit_nul = true;
675 else
676 printer_data->is_printable = false;
677 } else {
678 printf("%c", ch);
679 }
680 break;
681 case BINARY_PRINT_CHAR_PAD:
682 printf(" ");
683 break;
684 case BINARY_PRINT_LINE_END:
685 printf("\n");
686 printer_data->line_no++;
687 break;
688 case BINARY_PRINT_DATA_END:
689 default:
690 break;
691 }
692}
693
694static void print_sample_bpf_output(struct perf_sample *sample)
695{
696 unsigned int nr_bytes = sample->raw_size;
697 struct printer_data printer_data = {0, false, true};
698
699 print_binary(sample->raw_data, nr_bytes, 8,
700 print_sample_bpf_output_printer, &printer_data);
701
702 if (printer_data.is_printable && printer_data.hit_nul)
703 printf("%17s \"%s\"\n", "BPF string:",
704 (char *)(sample->raw_data));
705}
706
809e9423
JO
707struct perf_script {
708 struct perf_tool tool;
709 struct perf_session *session;
710 bool show_task_events;
711 bool show_mmap_events;
712 bool show_switch_events;
cfc8874a
JO
713 bool allocated;
714 struct cpu_map *cpus;
715 struct thread_map *threads;
9cdbc409 716 int name_width;
809e9423
JO
717};
718
9cdbc409
JO
719static int perf_evlist__max_name_len(struct perf_evlist *evlist)
720{
721 struct perf_evsel *evsel;
722 int max = 0;
723
724 evlist__for_each(evlist, evsel) {
725 int len = strlen(perf_evsel__name(evsel));
726
727 max = MAX(len, max);
728 }
729
730 return max;
731}
732
c19ac912
JO
733static size_t data_src__printf(u64 data_src)
734{
735 struct mem_info mi = { .data_src.val = data_src };
736 char decode[100];
737 char out[100];
738 static int maxlen;
739 int len;
740
741 perf_script__meminfo_scnprintf(decode, 100, &mi);
742
743 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
744 if (maxlen < len)
745 maxlen = len;
746
747 return printf("%-*s", maxlen, out);
748}
749
9cdbc409 750static void process_event(struct perf_script *script, union perf_event *event,
809e9423
JO
751 struct perf_sample *sample, struct perf_evsel *evsel,
752 struct addr_location *al)
be6d842a 753{
f9d5d549 754 struct thread *thread = al->thread;
9e69c210 755 struct perf_event_attr *attr = &evsel->attr;
1424dc96 756
2c9e45f7 757 if (output[attr->type].fields == 0)
1424dc96
DA
758 return;
759
fcf65bf1 760 print_sample_start(sample, thread, evsel);
745f43e3 761
535aeaae
JO
762 if (PRINT_FIELD(PERIOD))
763 printf("%10" PRIu64 " ", sample->period);
764
e944d3d7
NK
765 if (PRINT_FIELD(EVNAME)) {
766 const char *evname = perf_evsel__name(evsel);
9cdbc409
JO
767
768 if (!script->name_width)
769 script->name_width = perf_evlist__max_name_len(script->session->evlist);
770
771 printf("%*s: ", script->name_width,
772 evname ? evname : "[unknown]");
e944d3d7
NK
773 }
774
400ea6d3
AH
775 if (print_flags)
776 print_sample_flags(sample->flags);
777
95582596 778 if (is_bts_event(attr)) {
cc22e575 779 print_sample_bts(event, sample, evsel, thread, al);
95582596
AN
780 return;
781 }
782
745f43e3 783 if (PRINT_FIELD(TRACE))
fcf65bf1
ACM
784 event_format__print(evsel->tp_format, sample->cpu,
785 sample->raw_data, sample->raw_size);
7cec0922 786 if (PRINT_FIELD(ADDR))
bb871a9c 787 print_sample_addr(event, sample, thread, attr);
7cec0922 788
94ddddfa 789 if (PRINT_FIELD(DATA_SRC))
c19ac912 790 data_src__printf(sample->data_src);
94ddddfa
JO
791
792 if (PRINT_FIELD(WEIGHT))
793 printf("%16" PRIu64, sample->weight);
794
787bef17 795 if (PRINT_FIELD(IP)) {
c0230b2b
DA
796 if (!symbol_conf.use_callchain)
797 printf(" ");
798 else
799 printf("\n");
a6ffaf91 800
cc22e575 801 perf_evsel__print_ip(evsel, sample, al,
307cbb92 802 output[attr->type].print_ip_opts,
03cd1fed 803 scripting_max_stack);
c0230b2b
DA
804 }
805
fc36f948
SE
806 if (PRINT_FIELD(IREGS))
807 print_sample_iregs(event, sample, thread, attr);
808
dc323ce8
SE
809 if (PRINT_FIELD(BRSTACK))
810 print_sample_brstack(event, sample, thread, attr);
811 else if (PRINT_FIELD(BRSTACKSYM))
812 print_sample_brstacksym(event, sample, thread, attr);
813
30372f04
WN
814 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
815 print_sample_bpf_output(sample);
816
c70c94b4 817 printf("\n");
be6d842a
DA
818}
819
956ffd02
TZ
820static struct scripting_ops *scripting_ops;
821
36e33c53
JO
822static void __process_stat(struct perf_evsel *counter, u64 tstamp)
823{
824 int nthreads = thread_map__nr(counter->threads);
825 int ncpus = perf_evsel__nr_cpus(counter);
826 int cpu, thread;
827 static int header_printed;
828
829 if (counter->system_wide)
830 nthreads = 1;
831
832 if (!header_printed) {
833 printf("%3s %8s %15s %15s %15s %15s %s\n",
834 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
835 header_printed = 1;
836 }
837
838 for (thread = 0; thread < nthreads; thread++) {
839 for (cpu = 0; cpu < ncpus; cpu++) {
840 struct perf_counts_values *counts;
841
842 counts = perf_counts(counter->counts, cpu, thread);
843
844 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
845 counter->cpus->map[cpu],
846 thread_map__pid(counter->threads, thread),
847 counts->val,
848 counts->ena,
849 counts->run,
850 tstamp,
851 perf_evsel__name(counter));
852 }
853 }
854}
855
e099eba8
JO
856static void process_stat(struct perf_evsel *counter, u64 tstamp)
857{
858 if (scripting_ops && scripting_ops->process_stat)
859 scripting_ops->process_stat(&stat_config, counter, tstamp);
36e33c53
JO
860 else
861 __process_stat(counter, tstamp);
e099eba8
JO
862}
863
864static void process_stat_interval(u64 tstamp)
865{
866 if (scripting_ops && scripting_ops->process_stat_interval)
867 scripting_ops->process_stat_interval(tstamp);
868}
869
956ffd02
TZ
870static void setup_scripting(void)
871{
16c632de 872 setup_perl_scripting();
7e4b21b8 873 setup_python_scripting();
956ffd02
TZ
874}
875
d445dd2a
AH
876static int flush_scripting(void)
877{
2aaecfc5 878 return scripting_ops ? scripting_ops->flush_script() : 0;
d445dd2a
AH
879}
880
956ffd02
TZ
881static int cleanup_scripting(void)
882{
133dc4c3 883 pr_debug("\nperf script stopped\n");
3824a4e8 884
2aaecfc5 885 return scripting_ops ? scripting_ops->stop_script() : 0;
956ffd02
TZ
886}
887
809e9423 888static int process_sample_event(struct perf_tool *tool,
d20deb64 889 union perf_event *event,
8115d60c 890 struct perf_sample *sample,
9e69c210 891 struct perf_evsel *evsel,
743eb868 892 struct machine *machine)
5f9c39dc 893{
809e9423 894 struct perf_script *scr = container_of(tool, struct perf_script, tool);
e7984b7b 895 struct addr_location al;
5f9c39dc 896
1424dc96
DA
897 if (debug_mode) {
898 if (sample->time < last_timestamp) {
899 pr_err("Samples misordered, previous: %" PRIu64
900 " this: %" PRIu64 "\n", last_timestamp,
901 sample->time);
902 nr_unordered++;
e1889d75 903 }
1424dc96
DA
904 last_timestamp = sample->time;
905 return 0;
5f9c39dc 906 }
5d67be97 907
e44baa3e 908 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
e7984b7b
DA
909 pr_err("problem processing %d event, skipping it.\n",
910 event->header.type);
911 return -1;
912 }
913
914 if (al.filtered)
b91fc39f 915 goto out_put;
e7984b7b 916
5d67be97 917 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
b91fc39f 918 goto out_put;
5d67be97 919
2aaecfc5
JO
920 if (scripting_ops)
921 scripting_ops->process_event(event, sample, evsel, &al);
922 else
809e9423 923 process_event(scr, event, sample, evsel, &al);
2aaecfc5 924
b91fc39f
ACM
925out_put:
926 addr_location__put(&al);
5f9c39dc
FW
927 return 0;
928}
929
7ea95727
AH
930static int process_attr(struct perf_tool *tool, union perf_event *event,
931 struct perf_evlist **pevlist)
932{
933 struct perf_script *scr = container_of(tool, struct perf_script, tool);
934 struct perf_evlist *evlist;
935 struct perf_evsel *evsel, *pos;
936 int err;
937
938 err = perf_event__process_attr(tool, event, pevlist);
939 if (err)
940 return err;
941
942 evlist = *pevlist;
943 evsel = perf_evlist__last(*pevlist);
944
945 if (evsel->attr.type >= PERF_TYPE_MAX)
946 return 0;
947
0050f7aa 948 evlist__for_each(evlist, pos) {
7ea95727
AH
949 if (pos->attr.type == evsel->attr.type && pos != evsel)
950 return 0;
951 }
952
953 set_print_ip_opts(&evsel->attr);
954
d2b5a315
JO
955 if (evsel->attr.sample_type)
956 err = perf_evsel__check_attr(evsel, scr->session);
957
958 return err;
7ea95727
AH
959}
960
ad7ebb9a
NK
961static int process_comm_event(struct perf_tool *tool,
962 union perf_event *event,
963 struct perf_sample *sample,
964 struct machine *machine)
965{
966 struct thread *thread;
967 struct perf_script *script = container_of(tool, struct perf_script, tool);
968 struct perf_session *session = script->session;
06b234ec 969 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ad7ebb9a
NK
970 int ret = -1;
971
972 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
973 if (thread == NULL) {
974 pr_debug("problem processing COMM event, skipping it.\n");
975 return -1;
976 }
977
978 if (perf_event__process_comm(tool, event, sample, machine) < 0)
979 goto out;
980
981 if (!evsel->attr.sample_id_all) {
982 sample->cpu = 0;
983 sample->time = 0;
984 sample->tid = event->comm.tid;
985 sample->pid = event->comm.pid;
986 }
987 print_sample_start(sample, thread, evsel);
988 perf_event__fprintf(event, stdout);
989 ret = 0;
ad7ebb9a 990out:
b91fc39f 991 thread__put(thread);
ad7ebb9a
NK
992 return ret;
993}
994
995static int process_fork_event(struct perf_tool *tool,
996 union perf_event *event,
997 struct perf_sample *sample,
998 struct machine *machine)
999{
1000 struct thread *thread;
1001 struct perf_script *script = container_of(tool, struct perf_script, tool);
1002 struct perf_session *session = script->session;
06b234ec 1003 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ad7ebb9a
NK
1004
1005 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1006 return -1;
1007
1008 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1009 if (thread == NULL) {
1010 pr_debug("problem processing FORK event, skipping it.\n");
1011 return -1;
1012 }
1013
1014 if (!evsel->attr.sample_id_all) {
1015 sample->cpu = 0;
1016 sample->time = event->fork.time;
1017 sample->tid = event->fork.tid;
1018 sample->pid = event->fork.pid;
1019 }
1020 print_sample_start(sample, thread, evsel);
1021 perf_event__fprintf(event, stdout);
b91fc39f 1022 thread__put(thread);
ad7ebb9a
NK
1023
1024 return 0;
1025}
1026static int process_exit_event(struct perf_tool *tool,
1027 union perf_event *event,
1028 struct perf_sample *sample,
1029 struct machine *machine)
1030{
b91fc39f 1031 int err = 0;
ad7ebb9a
NK
1032 struct thread *thread;
1033 struct perf_script *script = container_of(tool, struct perf_script, tool);
1034 struct perf_session *session = script->session;
06b234ec 1035 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ad7ebb9a
NK
1036
1037 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1038 if (thread == NULL) {
1039 pr_debug("problem processing EXIT event, skipping it.\n");
1040 return -1;
1041 }
1042
1043 if (!evsel->attr.sample_id_all) {
1044 sample->cpu = 0;
1045 sample->time = 0;
53ff6bc3
AH
1046 sample->tid = event->fork.tid;
1047 sample->pid = event->fork.pid;
ad7ebb9a
NK
1048 }
1049 print_sample_start(sample, thread, evsel);
1050 perf_event__fprintf(event, stdout);
1051
1052 if (perf_event__process_exit(tool, event, sample, machine) < 0)
b91fc39f 1053 err = -1;
ad7ebb9a 1054
b91fc39f
ACM
1055 thread__put(thread);
1056 return err;
ad7ebb9a
NK
1057}
1058
ba1ddf42
NK
1059static int process_mmap_event(struct perf_tool *tool,
1060 union perf_event *event,
1061 struct perf_sample *sample,
1062 struct machine *machine)
1063{
1064 struct thread *thread;
1065 struct perf_script *script = container_of(tool, struct perf_script, tool);
1066 struct perf_session *session = script->session;
06b234ec 1067 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ba1ddf42
NK
1068
1069 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1070 return -1;
1071
1072 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1073 if (thread == NULL) {
1074 pr_debug("problem processing MMAP event, skipping it.\n");
1075 return -1;
1076 }
1077
1078 if (!evsel->attr.sample_id_all) {
1079 sample->cpu = 0;
1080 sample->time = 0;
1081 sample->tid = event->mmap.tid;
1082 sample->pid = event->mmap.pid;
1083 }
1084 print_sample_start(sample, thread, evsel);
1085 perf_event__fprintf(event, stdout);
b91fc39f 1086 thread__put(thread);
ba1ddf42
NK
1087 return 0;
1088}
1089
1090static int process_mmap2_event(struct perf_tool *tool,
1091 union perf_event *event,
1092 struct perf_sample *sample,
1093 struct machine *machine)
1094{
1095 struct thread *thread;
1096 struct perf_script *script = container_of(tool, struct perf_script, tool);
1097 struct perf_session *session = script->session;
06b234ec 1098 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ba1ddf42
NK
1099
1100 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1101 return -1;
1102
1103 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1104 if (thread == NULL) {
1105 pr_debug("problem processing MMAP2 event, skipping it.\n");
1106 return -1;
1107 }
1108
1109 if (!evsel->attr.sample_id_all) {
1110 sample->cpu = 0;
1111 sample->time = 0;
1112 sample->tid = event->mmap2.tid;
1113 sample->pid = event->mmap2.pid;
1114 }
1115 print_sample_start(sample, thread, evsel);
1116 perf_event__fprintf(event, stdout);
b91fc39f 1117 thread__put(thread);
ba1ddf42
NK
1118 return 0;
1119}
1120
7c14898b
AH
1121static int process_switch_event(struct perf_tool *tool,
1122 union perf_event *event,
1123 struct perf_sample *sample,
1124 struct machine *machine)
1125{
1126 struct thread *thread;
1127 struct perf_script *script = container_of(tool, struct perf_script, tool);
1128 struct perf_session *session = script->session;
1129 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1130
1131 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1132 return -1;
1133
1134 thread = machine__findnew_thread(machine, sample->pid,
1135 sample->tid);
1136 if (thread == NULL) {
1137 pr_debug("problem processing SWITCH event, skipping it.\n");
1138 return -1;
1139 }
1140
1141 print_sample_start(sample, thread, evsel);
1142 perf_event__fprintf(event, stdout);
1143 thread__put(thread);
1144 return 0;
1145}
1146
1d037ca1 1147static void sig_handler(int sig __maybe_unused)
c239da3b
TZ
1148{
1149 session_done = 1;
1150}
1151
6f3e5eda 1152static int __cmd_script(struct perf_script *script)
5f9c39dc 1153{
6fcf7ddb
FW
1154 int ret;
1155
c239da3b
TZ
1156 signal(SIGINT, sig_handler);
1157
ad7ebb9a
NK
1158 /* override event processing functions */
1159 if (script->show_task_events) {
1160 script->tool.comm = process_comm_event;
1161 script->tool.fork = process_fork_event;
1162 script->tool.exit = process_exit_event;
1163 }
ba1ddf42
NK
1164 if (script->show_mmap_events) {
1165 script->tool.mmap = process_mmap_event;
1166 script->tool.mmap2 = process_mmap2_event;
1167 }
7c14898b
AH
1168 if (script->show_switch_events)
1169 script->tool.context_switch = process_switch_event;
ad7ebb9a 1170
b7b61cbe 1171 ret = perf_session__process_events(script->session);
6fcf7ddb 1172
6d8afb56 1173 if (debug_mode)
9486aa38 1174 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
6fcf7ddb
FW
1175
1176 return ret;
5f9c39dc
FW
1177}
1178
956ffd02
TZ
1179struct script_spec {
1180 struct list_head node;
1181 struct scripting_ops *ops;
1182 char spec[0];
1183};
1184
eccdfe2d 1185static LIST_HEAD(script_specs);
956ffd02
TZ
1186
1187static struct script_spec *script_spec__new(const char *spec,
1188 struct scripting_ops *ops)
1189{
1190 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1191
1192 if (s != NULL) {
1193 strcpy(s->spec, spec);
1194 s->ops = ops;
1195 }
1196
1197 return s;
1198}
1199
956ffd02
TZ
1200static void script_spec__add(struct script_spec *s)
1201{
1202 list_add_tail(&s->node, &script_specs);
1203}
1204
1205static struct script_spec *script_spec__find(const char *spec)
1206{
1207 struct script_spec *s;
1208
1209 list_for_each_entry(s, &script_specs, node)
1210 if (strcasecmp(s->spec, spec) == 0)
1211 return s;
1212 return NULL;
1213}
1214
956ffd02
TZ
1215int script_spec_register(const char *spec, struct scripting_ops *ops)
1216{
1217 struct script_spec *s;
1218
1219 s = script_spec__find(spec);
1220 if (s)
1221 return -1;
1222
8560bae0 1223 s = script_spec__new(spec, ops);
956ffd02
TZ
1224 if (!s)
1225 return -1;
8560bae0
TS
1226 else
1227 script_spec__add(s);
956ffd02
TZ
1228
1229 return 0;
1230}
1231
1232static struct scripting_ops *script_spec__lookup(const char *spec)
1233{
1234 struct script_spec *s = script_spec__find(spec);
1235 if (!s)
1236 return NULL;
1237
1238 return s->ops;
1239}
1240
1241static void list_available_languages(void)
1242{
1243 struct script_spec *s;
1244
1245 fprintf(stderr, "\n");
1246 fprintf(stderr, "Scripting language extensions (used in "
133dc4c3 1247 "perf script -s [spec:]script.[spec]):\n\n");
956ffd02
TZ
1248
1249 list_for_each_entry(s, &script_specs, node)
1250 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1251
1252 fprintf(stderr, "\n");
1253}
1254
1d037ca1
IT
1255static int parse_scriptname(const struct option *opt __maybe_unused,
1256 const char *str, int unset __maybe_unused)
956ffd02
TZ
1257{
1258 char spec[PATH_MAX];
1259 const char *script, *ext;
1260 int len;
1261
f526d68b 1262 if (strcmp(str, "lang") == 0) {
956ffd02 1263 list_available_languages();
f526d68b 1264 exit(0);
956ffd02
TZ
1265 }
1266
1267 script = strchr(str, ':');
1268 if (script) {
1269 len = script - str;
1270 if (len >= PATH_MAX) {
1271 fprintf(stderr, "invalid language specifier");
1272 return -1;
1273 }
1274 strncpy(spec, str, len);
1275 spec[len] = '\0';
1276 scripting_ops = script_spec__lookup(spec);
1277 if (!scripting_ops) {
1278 fprintf(stderr, "invalid language specifier");
1279 return -1;
1280 }
1281 script++;
1282 } else {
1283 script = str;
d1e95bb5 1284 ext = strrchr(script, '.');
956ffd02
TZ
1285 if (!ext) {
1286 fprintf(stderr, "invalid script extension");
1287 return -1;
1288 }
1289 scripting_ops = script_spec__lookup(++ext);
1290 if (!scripting_ops) {
1291 fprintf(stderr, "invalid script extension");
1292 return -1;
1293 }
1294 }
1295
1296 script_name = strdup(script);
1297
1298 return 0;
1299}
1300
1d037ca1
IT
1301static int parse_output_fields(const struct option *opt __maybe_unused,
1302 const char *arg, int unset __maybe_unused)
745f43e3
DA
1303{
1304 char *tok;
50ca19ae 1305 int i, imax = ARRAY_SIZE(all_output_options);
2c9e45f7 1306 int j;
745f43e3
DA
1307 int rc = 0;
1308 char *str = strdup(arg);
1424dc96 1309 int type = -1;
745f43e3
DA
1310
1311 if (!str)
1312 return -ENOMEM;
1313
2c9e45f7
DA
1314 /* first word can state for which event type the user is specifying
1315 * the fields. If no type exists, the specified fields apply to all
1316 * event types found in the file minus the invalid fields for a type.
1424dc96 1317 */
2c9e45f7
DA
1318 tok = strchr(str, ':');
1319 if (tok) {
1320 *tok = '\0';
1321 tok++;
1322 if (!strcmp(str, "hw"))
1323 type = PERF_TYPE_HARDWARE;
1324 else if (!strcmp(str, "sw"))
1325 type = PERF_TYPE_SOFTWARE;
1326 else if (!strcmp(str, "trace"))
1327 type = PERF_TYPE_TRACEPOINT;
0817a6a3
AS
1328 else if (!strcmp(str, "raw"))
1329 type = PERF_TYPE_RAW;
27cfef00
WN
1330 else if (!strcmp(str, "break"))
1331 type = PERF_TYPE_BREAKPOINT;
2c9e45f7
DA
1332 else {
1333 fprintf(stderr, "Invalid event type in field string.\n");
38efb539
RR
1334 rc = -EINVAL;
1335 goto out;
2c9e45f7
DA
1336 }
1337
1338 if (output[type].user_set)
1339 pr_warning("Overriding previous field request for %s events.\n",
1340 event_type(type));
1341
1342 output[type].fields = 0;
1343 output[type].user_set = true;
9cbdb702 1344 output[type].wildcard_set = false;
2c9e45f7
DA
1345
1346 } else {
1347 tok = str;
1348 if (strlen(str) == 0) {
1349 fprintf(stderr,
1350 "Cannot set fields to 'none' for all event types.\n");
1351 rc = -EINVAL;
1352 goto out;
1353 }
1354
1355 if (output_set_by_user())
1356 pr_warning("Overriding previous field request for all events.\n");
1357
1358 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1359 output[j].fields = 0;
1360 output[j].user_set = true;
9cbdb702 1361 output[j].wildcard_set = true;
2c9e45f7 1362 }
745f43e3
DA
1363 }
1364
400ea6d3 1365 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
745f43e3 1366 for (i = 0; i < imax; ++i) {
2c9e45f7 1367 if (strcmp(tok, all_output_options[i].str) == 0)
745f43e3 1368 break;
745f43e3 1369 }
400ea6d3
AH
1370 if (i == imax && strcmp(tok, "flags") == 0) {
1371 print_flags = true;
1372 continue;
1373 }
745f43e3 1374 if (i == imax) {
2c9e45f7 1375 fprintf(stderr, "Invalid field requested.\n");
745f43e3 1376 rc = -EINVAL;
2c9e45f7 1377 goto out;
745f43e3
DA
1378 }
1379
2c9e45f7
DA
1380 if (type == -1) {
1381 /* add user option to all events types for
1382 * which it is valid
1383 */
1384 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1385 if (output[j].invalid_fields & all_output_options[i].field) {
1386 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1387 all_output_options[i].str, event_type(j));
1388 } else
1389 output[j].fields |= all_output_options[i].field;
1390 }
1391 } else {
1392 if (output[type].invalid_fields & all_output_options[i].field) {
1393 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1394 all_output_options[i].str, event_type(type));
1395
1396 rc = -EINVAL;
1397 goto out;
1398 }
1399 output[type].fields |= all_output_options[i].field;
1400 }
745f43e3
DA
1401 }
1402
2c9e45f7
DA
1403 if (type >= 0) {
1404 if (output[type].fields == 0) {
1405 pr_debug("No fields requested for %s type. "
1406 "Events will not be displayed.\n", event_type(type));
1407 }
1408 }
745f43e3 1409
2c9e45f7 1410out:
745f43e3
DA
1411 free(str);
1412 return rc;
1413}
1414
008f29d3
SB
1415/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1416static int is_directory(const char *base_path, const struct dirent *dent)
1417{
1418 char path[PATH_MAX];
1419 struct stat st;
1420
1421 sprintf(path, "%s/%s", base_path, dent->d_name);
1422 if (stat(path, &st))
1423 return 0;
1424
1425 return S_ISDIR(st.st_mode);
1426}
1427
1428#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
4b9c0c59
TZ
1429 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1430 lang_next) \
008f29d3
SB
1431 if ((lang_dirent.d_type == DT_DIR || \
1432 (lang_dirent.d_type == DT_UNKNOWN && \
1433 is_directory(scripts_path, &lang_dirent))) && \
4b9c0c59
TZ
1434 (strcmp(lang_dirent.d_name, ".")) && \
1435 (strcmp(lang_dirent.d_name, "..")))
1436
008f29d3 1437#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
4b9c0c59
TZ
1438 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1439 script_next) \
008f29d3
SB
1440 if (script_dirent.d_type != DT_DIR && \
1441 (script_dirent.d_type != DT_UNKNOWN || \
1442 !is_directory(lang_path, &script_dirent)))
4b9c0c59
TZ
1443
1444
1445#define RECORD_SUFFIX "-record"
1446#define REPORT_SUFFIX "-report"
1447
1448struct script_desc {
1449 struct list_head node;
1450 char *name;
1451 char *half_liner;
1452 char *args;
1453};
1454
eccdfe2d 1455static LIST_HEAD(script_descs);
4b9c0c59
TZ
1456
1457static struct script_desc *script_desc__new(const char *name)
1458{
1459 struct script_desc *s = zalloc(sizeof(*s));
1460
b5b87312 1461 if (s != NULL && name)
4b9c0c59
TZ
1462 s->name = strdup(name);
1463
1464 return s;
1465}
1466
1467static void script_desc__delete(struct script_desc *s)
1468{
74cf249d
ACM
1469 zfree(&s->name);
1470 zfree(&s->half_liner);
1471 zfree(&s->args);
4b9c0c59
TZ
1472 free(s);
1473}
1474
1475static void script_desc__add(struct script_desc *s)
1476{
1477 list_add_tail(&s->node, &script_descs);
1478}
1479
1480static struct script_desc *script_desc__find(const char *name)
1481{
1482 struct script_desc *s;
1483
1484 list_for_each_entry(s, &script_descs, node)
1485 if (strcasecmp(s->name, name) == 0)
1486 return s;
1487 return NULL;
1488}
1489
1490static struct script_desc *script_desc__findnew(const char *name)
1491{
1492 struct script_desc *s = script_desc__find(name);
1493
1494 if (s)
1495 return s;
1496
1497 s = script_desc__new(name);
1498 if (!s)
1499 goto out_delete_desc;
1500
1501 script_desc__add(s);
1502
1503 return s;
1504
1505out_delete_desc:
1506 script_desc__delete(s);
1507
1508 return NULL;
1509}
1510
965bb6be 1511static const char *ends_with(const char *str, const char *suffix)
4b9c0c59
TZ
1512{
1513 size_t suffix_len = strlen(suffix);
965bb6be 1514 const char *p = str;
4b9c0c59
TZ
1515
1516 if (strlen(str) > suffix_len) {
1517 p = str + strlen(str) - suffix_len;
1518 if (!strncmp(p, suffix, suffix_len))
1519 return p;
1520 }
1521
1522 return NULL;
1523}
1524
4b9c0c59
TZ
1525static int read_script_info(struct script_desc *desc, const char *filename)
1526{
1527 char line[BUFSIZ], *p;
1528 FILE *fp;
1529
1530 fp = fopen(filename, "r");
1531 if (!fp)
1532 return -1;
1533
1534 while (fgets(line, sizeof(line), fp)) {
1535 p = ltrim(line);
1536 if (strlen(p) == 0)
1537 continue;
1538 if (*p != '#')
1539 continue;
1540 p++;
1541 if (strlen(p) && *p == '!')
1542 continue;
1543
1544 p = ltrim(p);
1545 if (strlen(p) && p[strlen(p) - 1] == '\n')
1546 p[strlen(p) - 1] = '\0';
1547
1548 if (!strncmp(p, "description:", strlen("description:"))) {
1549 p += strlen("description:");
1550 desc->half_liner = strdup(ltrim(p));
1551 continue;
1552 }
1553
1554 if (!strncmp(p, "args:", strlen("args:"))) {
1555 p += strlen("args:");
1556 desc->args = strdup(ltrim(p));
1557 continue;
1558 }
1559 }
1560
1561 fclose(fp);
1562
1563 return 0;
1564}
1565
38efb539
RR
1566static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1567{
1568 char *script_root, *str;
1569
1570 script_root = strdup(script_dirent->d_name);
1571 if (!script_root)
1572 return NULL;
1573
1574 str = (char *)ends_with(script_root, suffix);
1575 if (!str) {
1576 free(script_root);
1577 return NULL;
1578 }
1579
1580 *str = '\0';
1581 return script_root;
1582}
1583
1d037ca1
IT
1584static int list_available_scripts(const struct option *opt __maybe_unused,
1585 const char *s __maybe_unused,
1586 int unset __maybe_unused)
4b9c0c59
TZ
1587{
1588 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1589 char scripts_path[MAXPATHLEN];
1590 DIR *scripts_dir, *lang_dir;
1591 char script_path[MAXPATHLEN];
1592 char lang_path[MAXPATHLEN];
1593 struct script_desc *desc;
1594 char first_half[BUFSIZ];
1595 char *script_root;
4b9c0c59 1596
46113a54 1597 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
4b9c0c59
TZ
1598
1599 scripts_dir = opendir(scripts_path);
1600 if (!scripts_dir)
1601 return -1;
1602
008f29d3 1603 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
4b9c0c59
TZ
1604 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1605 lang_dirent.d_name);
1606 lang_dir = opendir(lang_path);
1607 if (!lang_dir)
1608 continue;
1609
008f29d3 1610 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
38efb539
RR
1611 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1612 if (script_root) {
4b9c0c59
TZ
1613 desc = script_desc__findnew(script_root);
1614 snprintf(script_path, MAXPATHLEN, "%s/%s",
1615 lang_path, script_dirent.d_name);
1616 read_script_info(desc, script_path);
38efb539 1617 free(script_root);
4b9c0c59 1618 }
4b9c0c59
TZ
1619 }
1620 }
1621
1622 fprintf(stdout, "List of available trace scripts:\n");
1623 list_for_each_entry(desc, &script_descs, node) {
1624 sprintf(first_half, "%s %s", desc->name,
1625 desc->args ? desc->args : "");
1626 fprintf(stdout, " %-36s %s\n", first_half,
1627 desc->half_liner ? desc->half_liner : "");
1628 }
1629
1630 exit(0);
1631}
1632
49e639e2
FT
1633/*
1634 * Some scripts specify the required events in their "xxx-record" file,
1635 * this function will check if the events in perf.data match those
1636 * mentioned in the "xxx-record".
1637 *
1638 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1639 * which is covered well now. And new parsing code should be added to
1640 * cover the future complexing formats like event groups etc.
1641 */
1642static int check_ev_match(char *dir_name, char *scriptname,
1643 struct perf_session *session)
1644{
1645 char filename[MAXPATHLEN], evname[128];
1646 char line[BUFSIZ], *p;
1647 struct perf_evsel *pos;
1648 int match, len;
1649 FILE *fp;
1650
1651 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1652
1653 fp = fopen(filename, "r");
1654 if (!fp)
1655 return -1;
1656
1657 while (fgets(line, sizeof(line), fp)) {
1658 p = ltrim(line);
1659 if (*p == '#')
1660 continue;
1661
1662 while (strlen(p)) {
1663 p = strstr(p, "-e");
1664 if (!p)
1665 break;
1666
1667 p += 2;
1668 p = ltrim(p);
1669 len = strcspn(p, " \t");
1670 if (!len)
1671 break;
1672
1673 snprintf(evname, len + 1, "%s", p);
1674
1675 match = 0;
0050f7aa 1676 evlist__for_each(session->evlist, pos) {
49e639e2
FT
1677 if (!strcmp(perf_evsel__name(pos), evname)) {
1678 match = 1;
1679 break;
1680 }
1681 }
1682
1683 if (!match) {
1684 fclose(fp);
1685 return -1;
1686 }
1687 }
1688 }
1689
1690 fclose(fp);
1691 return 0;
1692}
1693
e5f3705e
FT
1694/*
1695 * Return -1 if none is found, otherwise the actual scripts number.
1696 *
1697 * Currently the only user of this function is the script browser, which
1698 * will list all statically runnable scripts, select one, execute it and
1699 * show the output in a perf browser.
1700 */
1701int find_scripts(char **scripts_array, char **scripts_path_array)
1702{
1703 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
49e639e2 1704 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
e5f3705e 1705 DIR *scripts_dir, *lang_dir;
49e639e2 1706 struct perf_session *session;
f5fc1412
JO
1707 struct perf_data_file file = {
1708 .path = input_name,
1709 .mode = PERF_DATA_MODE_READ,
1710 };
e5f3705e
FT
1711 char *temp;
1712 int i = 0;
1713
f5fc1412 1714 session = perf_session__new(&file, false, NULL);
49e639e2
FT
1715 if (!session)
1716 return -1;
1717
46113a54 1718 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
e5f3705e
FT
1719
1720 scripts_dir = opendir(scripts_path);
49e639e2
FT
1721 if (!scripts_dir) {
1722 perf_session__delete(session);
e5f3705e 1723 return -1;
49e639e2 1724 }
e5f3705e
FT
1725
1726 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1727 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1728 lang_dirent.d_name);
1729#ifdef NO_LIBPERL
1730 if (strstr(lang_path, "perl"))
1731 continue;
1732#endif
1733#ifdef NO_LIBPYTHON
1734 if (strstr(lang_path, "python"))
1735 continue;
1736#endif
1737
1738 lang_dir = opendir(lang_path);
1739 if (!lang_dir)
1740 continue;
1741
1742 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1743 /* Skip those real time scripts: xxxtop.p[yl] */
1744 if (strstr(script_dirent.d_name, "top."))
1745 continue;
1746 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1747 script_dirent.d_name);
1748 temp = strchr(script_dirent.d_name, '.');
1749 snprintf(scripts_array[i],
1750 (temp - script_dirent.d_name) + 1,
1751 "%s", script_dirent.d_name);
49e639e2
FT
1752
1753 if (check_ev_match(lang_path,
1754 scripts_array[i], session))
1755 continue;
1756
e5f3705e
FT
1757 i++;
1758 }
49e639e2 1759 closedir(lang_dir);
e5f3705e
FT
1760 }
1761
49e639e2
FT
1762 closedir(scripts_dir);
1763 perf_session__delete(session);
e5f3705e
FT
1764 return i;
1765}
1766
3875294f
TZ
1767static char *get_script_path(const char *script_root, const char *suffix)
1768{
1769 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1770 char scripts_path[MAXPATHLEN];
1771 char script_path[MAXPATHLEN];
1772 DIR *scripts_dir, *lang_dir;
1773 char lang_path[MAXPATHLEN];
38efb539 1774 char *__script_root;
3875294f 1775
46113a54 1776 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3875294f
TZ
1777
1778 scripts_dir = opendir(scripts_path);
1779 if (!scripts_dir)
1780 return NULL;
1781
008f29d3 1782 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
3875294f
TZ
1783 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1784 lang_dirent.d_name);
1785 lang_dir = opendir(lang_path);
1786 if (!lang_dir)
1787 continue;
1788
008f29d3 1789 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
38efb539
RR
1790 __script_root = get_script_root(&script_dirent, suffix);
1791 if (__script_root && !strcmp(script_root, __script_root)) {
1792 free(__script_root);
946ef2a2
NK
1793 closedir(lang_dir);
1794 closedir(scripts_dir);
3875294f
TZ
1795 snprintf(script_path, MAXPATHLEN, "%s/%s",
1796 lang_path, script_dirent.d_name);
38efb539 1797 return strdup(script_path);
3875294f
TZ
1798 }
1799 free(__script_root);
1800 }
946ef2a2 1801 closedir(lang_dir);
3875294f 1802 }
946ef2a2 1803 closedir(scripts_dir);
3875294f 1804
38efb539 1805 return NULL;
3875294f
TZ
1806}
1807
b5b87312
TZ
1808static bool is_top_script(const char *script_path)
1809{
965bb6be 1810 return ends_with(script_path, "top") == NULL ? false : true;
b5b87312
TZ
1811}
1812
1813static int has_required_arg(char *script_path)
1814{
1815 struct script_desc *desc;
1816 int n_args = 0;
1817 char *p;
1818
1819 desc = script_desc__new(NULL);
1820
1821 if (read_script_info(desc, script_path))
1822 goto out;
1823
1824 if (!desc->args)
1825 goto out;
1826
1827 for (p = desc->args; *p; p++)
1828 if (*p == '<')
1829 n_args++;
1830out:
1831 script_desc__delete(desc);
1832
1833 return n_args;
1834}
1835
69b6470e
ACM
1836static int have_cmd(int argc, const char **argv)
1837{
1838 char **__argv = malloc(sizeof(const char *) * argc);
1839
1840 if (!__argv) {
1841 pr_err("malloc failed\n");
1842 return -1;
1843 }
1844
1845 memcpy(__argv, argv, sizeof(const char *) * argc);
1846 argc = parse_options(argc, (const char **)__argv, record_options,
1847 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1848 free(__argv);
5f9c39dc 1849
69b6470e
ACM
1850 system_wide = (argc == 0);
1851
1852 return 0;
1853}
1854
7322d6c9
JO
1855static void script__setup_sample_type(struct perf_script *script)
1856{
1857 struct perf_session *session = script->session;
1858 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1859
1860 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1861 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1862 (sample_type & PERF_SAMPLE_STACK_USER))
1863 callchain_param.record_mode = CALLCHAIN_DWARF;
1864 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1865 callchain_param.record_mode = CALLCHAIN_LBR;
1866 else
1867 callchain_param.record_mode = CALLCHAIN_FP;
1868 }
1869}
1870
e099eba8
JO
1871static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1872 union perf_event *event,
1873 struct perf_session *session)
1874{
1875 struct stat_round_event *round = &event->stat_round;
1876 struct perf_evsel *counter;
1877
1878 evlist__for_each(session->evlist, counter) {
1879 perf_stat_process_counter(&stat_config, counter);
1880 process_stat(counter, round->time);
1881 }
1882
1883 process_stat_interval(round->time);
1884 return 0;
1885}
1886
91a2c3d5
JO
1887static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1888 union perf_event *event,
1889 struct perf_session *session __maybe_unused)
1890{
1891 perf_event__read_stat_config(&stat_config, &event->stat_config);
1892 return 0;
1893}
1894
cfc8874a
JO
1895static int set_maps(struct perf_script *script)
1896{
1897 struct perf_evlist *evlist = script->session->evlist;
1898
1899 if (!script->cpus || !script->threads)
1900 return 0;
1901
1902 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1903 return -EINVAL;
1904
1905 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1906
1907 if (perf_evlist__alloc_stats(evlist, true))
1908 return -ENOMEM;
1909
1910 script->allocated = true;
1911 return 0;
1912}
1913
1914static
1915int process_thread_map_event(struct perf_tool *tool,
1916 union perf_event *event,
1917 struct perf_session *session __maybe_unused)
1918{
1919 struct perf_script *script = container_of(tool, struct perf_script, tool);
1920
1921 if (script->threads) {
1922 pr_warning("Extra thread map event, ignoring.\n");
1923 return 0;
1924 }
1925
1926 script->threads = thread_map__new_event(&event->thread_map);
1927 if (!script->threads)
1928 return -ENOMEM;
1929
1930 return set_maps(script);
1931}
1932
1933static
1934int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1935 union perf_event *event,
1936 struct perf_session *session __maybe_unused)
1937{
1938 struct perf_script *script = container_of(tool, struct perf_script, tool);
1939
1940 if (script->cpus) {
1941 pr_warning("Extra cpu map event, ignoring.\n");
1942 return 0;
1943 }
1944
1945 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1946 if (!script->cpus)
1947 return -ENOMEM;
1948
1949 return set_maps(script);
1950}
1951
69b6470e
ACM
1952int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1953{
1954 bool show_full_info = false;
e90debdd
JO
1955 bool header = false;
1956 bool header_only = false;
6cc870f0 1957 bool script_started = false;
69b6470e
ACM
1958 char *rec_script_path = NULL;
1959 char *rep_script_path = NULL;
1960 struct perf_session *session;
7a680eb9 1961 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
69b6470e
ACM
1962 char *script_path = NULL;
1963 const char **__argv;
6cc870f0 1964 int i, j, err = 0;
6f3e5eda
AH
1965 struct perf_script script = {
1966 .tool = {
1967 .sample = process_sample_event,
1968 .mmap = perf_event__process_mmap,
1969 .mmap2 = perf_event__process_mmap2,
1970 .comm = perf_event__process_comm,
1971 .exit = perf_event__process_exit,
1972 .fork = perf_event__process_fork,
7ea95727 1973 .attr = process_attr,
6f3e5eda
AH
1974 .tracing_data = perf_event__process_tracing_data,
1975 .build_id = perf_event__process_build_id,
7a680eb9
AH
1976 .id_index = perf_event__process_id_index,
1977 .auxtrace_info = perf_event__process_auxtrace_info,
1978 .auxtrace = perf_event__process_auxtrace,
1979 .auxtrace_error = perf_event__process_auxtrace_error,
e099eba8
JO
1980 .stat = perf_event__process_stat_event,
1981 .stat_round = process_stat_round_event,
91a2c3d5 1982 .stat_config = process_stat_config_event,
cfc8874a
JO
1983 .thread_map = process_thread_map_event,
1984 .cpu_map = process_cpu_map_event,
0a8cb85c 1985 .ordered_events = true,
6f3e5eda
AH
1986 .ordering_requires_timestamps = true,
1987 },
1988 };
06af0f2c
YS
1989 struct perf_data_file file = {
1990 .mode = PERF_DATA_MODE_READ,
1991 };
69b6470e 1992 const struct option options[] = {
5f9c39dc
FW
1993 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1994 "dump raw trace in ASCII"),
c0555642 1995 OPT_INCR('v', "verbose", &verbose,
69b6470e 1996 "be more verbose (show symbol address, etc)"),
4b9c0c59 1997 OPT_BOOLEAN('L', "Latency", &latency_format,
cda48461 1998 "show latency attributes (irqs/preemption disabled, etc)"),
4b9c0c59
TZ
1999 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2000 list_available_scripts),
956ffd02
TZ
2001 OPT_CALLBACK('s', "script", NULL, "name",
2002 "script file name (lang:script name, script name, or *)",
2003 parse_scriptname),
2004 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
133dc4c3 2005 "generate perf-script.xx script in specified language"),
69b6470e 2006 OPT_STRING('i', "input", &input_name, "file", "input file name"),
ffabd99e
FW
2007 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2008 "do various checks like samples ordering and lost events"),
e90debdd
JO
2009 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2010 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
c0230b2b
DA
2011 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2012 "file", "vmlinux pathname"),
2013 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2014 "file", "kallsyms pathname"),
2015 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2016 "When printing symbols do not display call chain"),
2017 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
2018 "Look for files with symbols relative to this directory"),
06af0f2c 2019 OPT_CALLBACK('F', "fields", NULL, "str",
a978f2ab
AN
2020 "comma separated output fields prepend with 'type:'. "
2021 "Valid types: hw,sw,trace,raw. "
2022 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
dc323ce8 2023 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
317df650 2024 OPT_BOOLEAN('a', "all-cpus", &system_wide,
69b6470e 2025 "system-wide collection from all CPUs"),
36385be5
FT
2026 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2027 "only consider these symbols"),
c8e66720 2028 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
e7984b7b
DA
2029 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2030 "only display events for these comms"),
e03eaa40
DA
2031 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2032 "only consider symbols in these pids"),
2033 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2034 "only consider symbols in these tids"),
fbe96f29
SE
2035 OPT_BOOLEAN('I', "show-info", &show_full_info,
2036 "display extended information from perf.data file"),
0bc8d205
AN
2037 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2038 "Show the path of [kernel.kallsyms]"),
ad7ebb9a
NK
2039 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2040 "Show the fork/comm/exit events"),
ba1ddf42
NK
2041 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2042 "Show the mmap events"),
7c14898b
AH
2043 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2044 "Show context switch events (if recorded)"),
06af0f2c 2045 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
83e19860
AH
2046 OPT_BOOLEAN(0, "ns", &nanosecs,
2047 "Use 9 decimal places when displaying time"),
7a680eb9
AH
2048 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2049 "Instruction Tracing options",
2050 itrace_parse_synth_opts),
a9710ba0
AK
2051 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2052 "Show full source file name path for source lines"),
77e0070d
MD
2053 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2054 "Enable symbol demangling"),
2055 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2056 "Enable kernel symbol demangling"),
2057
1909629f 2058 OPT_END()
69b6470e 2059 };
40cae2b7
YS
2060 const char * const script_subcommands[] = { "record", "report", NULL };
2061 const char *script_usage[] = {
69b6470e
ACM
2062 "perf script [<options>]",
2063 "perf script [<options>] record <script> [<record-options>] <command>",
2064 "perf script [<options>] report <script> [script-args]",
2065 "perf script [<options>] <script> [<record-options>] <command>",
2066 "perf script [<options>] <top-script> [script-args]",
2067 NULL
2068 };
3875294f 2069
b5b87312
TZ
2070 setup_scripting();
2071
40cae2b7 2072 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
b5b87312
TZ
2073 PARSE_OPT_STOP_AT_NON_OPTION);
2074
f5fc1412
JO
2075 file.path = input_name;
2076
b5b87312
TZ
2077 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2078 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2079 if (!rec_script_path)
2080 return cmd_record(argc, argv, NULL);
3875294f
TZ
2081 }
2082
b5b87312
TZ
2083 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2084 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2085 if (!rep_script_path) {
3875294f 2086 fprintf(stderr,
b5b87312 2087 "Please specify a valid report script"
133dc4c3 2088 "(see 'perf script -l' for listing)\n");
3875294f
TZ
2089 return -1;
2090 }
3875294f
TZ
2091 }
2092
3c5b645f
AH
2093 if (itrace_synth_opts.callchain &&
2094 itrace_synth_opts.callchain_sz > scripting_max_stack)
2095 scripting_max_stack = itrace_synth_opts.callchain_sz;
2096
44e668c6 2097 /* make sure PERF_EXEC_PATH is set for scripts */
46113a54 2098 set_argv_exec_path(get_argv_exec_path());
44e668c6 2099
b5b87312 2100 if (argc && !script_name && !rec_script_path && !rep_script_path) {
a0cccc2e 2101 int live_pipe[2];
b5b87312 2102 int rep_args;
a0cccc2e
TZ
2103 pid_t pid;
2104
b5b87312
TZ
2105 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2106 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2107
2108 if (!rec_script_path && !rep_script_path) {
c7118369
NK
2109 usage_with_options_msg(script_usage, options,
2110 "Couldn't find script `%s'\n\n See perf"
133dc4c3 2111 " script -l for available scripts.\n", argv[0]);
a0cccc2e
TZ
2112 }
2113
b5b87312
TZ
2114 if (is_top_script(argv[0])) {
2115 rep_args = argc - 1;
2116 } else {
2117 int rec_args;
2118
2119 rep_args = has_required_arg(rep_script_path);
2120 rec_args = (argc - 1) - rep_args;
2121 if (rec_args < 0) {
c7118369
NK
2122 usage_with_options_msg(script_usage, options,
2123 "`%s' script requires options."
133dc4c3 2124 "\n\n See perf script -l for available "
b5b87312 2125 "scripts and options.\n", argv[0]);
b5b87312 2126 }
a0cccc2e
TZ
2127 }
2128
2129 if (pipe(live_pipe) < 0) {
2130 perror("failed to create pipe");
d54b1a9e 2131 return -1;
a0cccc2e
TZ
2132 }
2133
2134 pid = fork();
2135 if (pid < 0) {
2136 perror("failed to fork");
d54b1a9e 2137 return -1;
a0cccc2e
TZ
2138 }
2139
2140 if (!pid) {
b5b87312
TZ
2141 j = 0;
2142
a0cccc2e
TZ
2143 dup2(live_pipe[1], 1);
2144 close(live_pipe[0]);
2145
317df650
RR
2146 if (is_top_script(argv[0])) {
2147 system_wide = true;
2148 } else if (!system_wide) {
d54b1a9e
DA
2149 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2150 err = -1;
2151 goto out;
2152 }
317df650 2153 }
b5b87312
TZ
2154
2155 __argv = malloc((argc + 6) * sizeof(const char *));
d54b1a9e
DA
2156 if (!__argv) {
2157 pr_err("malloc failed\n");
2158 err = -ENOMEM;
2159 goto out;
2160 }
e8719adf 2161
b5b87312
TZ
2162 __argv[j++] = "/bin/sh";
2163 __argv[j++] = rec_script_path;
2164 if (system_wide)
2165 __argv[j++] = "-a";
2166 __argv[j++] = "-q";
2167 __argv[j++] = "-o";
2168 __argv[j++] = "-";
2169 for (i = rep_args + 1; i < argc; i++)
2170 __argv[j++] = argv[i];
2171 __argv[j++] = NULL;
a0cccc2e
TZ
2172
2173 execvp("/bin/sh", (char **)__argv);
e8719adf 2174 free(__argv);
a0cccc2e
TZ
2175 exit(-1);
2176 }
2177
2178 dup2(live_pipe[0], 0);
2179 close(live_pipe[1]);
2180
b5b87312 2181 __argv = malloc((argc + 4) * sizeof(const char *));
d54b1a9e
DA
2182 if (!__argv) {
2183 pr_err("malloc failed\n");
2184 err = -ENOMEM;
2185 goto out;
2186 }
2187
b5b87312
TZ
2188 j = 0;
2189 __argv[j++] = "/bin/sh";
2190 __argv[j++] = rep_script_path;
2191 for (i = 1; i < rep_args + 1; i++)
2192 __argv[j++] = argv[i];
2193 __argv[j++] = "-i";
2194 __argv[j++] = "-";
2195 __argv[j++] = NULL;
a0cccc2e
TZ
2196
2197 execvp("/bin/sh", (char **)__argv);
e8719adf 2198 free(__argv);
a0cccc2e
TZ
2199 exit(-1);
2200 }
2201
b5b87312
TZ
2202 if (rec_script_path)
2203 script_path = rec_script_path;
2204 if (rep_script_path)
2205 script_path = rep_script_path;
34c86ea9 2206
b5b87312 2207 if (script_path) {
b5b87312 2208 j = 0;
3875294f 2209
317df650
RR
2210 if (!rec_script_path)
2211 system_wide = false;
d54b1a9e
DA
2212 else if (!system_wide) {
2213 if (have_cmd(argc - 1, &argv[1]) != 0) {
2214 err = -1;
2215 goto out;
2216 }
2217 }
34c86ea9 2218
b5b87312 2219 __argv = malloc((argc + 2) * sizeof(const char *));
d54b1a9e
DA
2220 if (!__argv) {
2221 pr_err("malloc failed\n");
2222 err = -ENOMEM;
2223 goto out;
2224 }
2225
34c86ea9
TZ
2226 __argv[j++] = "/bin/sh";
2227 __argv[j++] = script_path;
2228 if (system_wide)
2229 __argv[j++] = "-a";
b5b87312 2230 for (i = 2; i < argc; i++)
34c86ea9
TZ
2231 __argv[j++] = argv[i];
2232 __argv[j++] = NULL;
3875294f
TZ
2233
2234 execvp("/bin/sh", (char **)__argv);
e8719adf 2235 free(__argv);
3875294f
TZ
2236 exit(-1);
2237 }
956ffd02 2238
cf4fee50
TZ
2239 if (!script_name)
2240 setup_pager();
5f9c39dc 2241
6f3e5eda 2242 session = perf_session__new(&file, false, &script.tool);
d8f66248 2243 if (session == NULL)
52e02834 2244 return -1;
d8f66248 2245
e90debdd
JO
2246 if (header || header_only) {
2247 perf_session__fprintf_info(session, stdout, show_full_info);
2248 if (header_only)
6cc870f0 2249 goto out_delete;
e90debdd
JO
2250 }
2251
0a7e6d1b 2252 if (symbol__init(&session->header.env) < 0)
38520dc3
NK
2253 goto out_delete;
2254
6f3e5eda 2255 script.session = session;
7322d6c9 2256 script__setup_sample_type(&script);
6f3e5eda 2257
7a680eb9
AH
2258 session->itrace_synth_opts = &itrace_synth_opts;
2259
5d67be97 2260 if (cpu_list) {
6cc870f0
NK
2261 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2262 if (err < 0)
2263 goto out_delete;
5d67be97
AB
2264 }
2265
1424dc96 2266 if (!no_callchain)
c0230b2b
DA
2267 symbol_conf.use_callchain = true;
2268 else
2269 symbol_conf.use_callchain = false;
2270
9ee67421
ACM
2271 if (session->tevent.pevent &&
2272 pevent_set_function_resolver(session->tevent.pevent,
ccb3a829
ACM
2273 machine__resolve_kernel_addr,
2274 &session->machines.host) < 0) {
2275 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2276 return -1;
2277 }
2278
956ffd02
TZ
2279 if (generate_script_lang) {
2280 struct stat perf_stat;
745f43e3
DA
2281 int input;
2282
2c9e45f7 2283 if (output_set_by_user()) {
745f43e3
DA
2284 fprintf(stderr,
2285 "custom fields not supported for generated scripts");
6cc870f0
NK
2286 err = -EINVAL;
2287 goto out_delete;
745f43e3 2288 }
956ffd02 2289
cc9784bd 2290 input = open(file.path, O_RDONLY); /* input_name */
956ffd02 2291 if (input < 0) {
6cc870f0 2292 err = -errno;
956ffd02 2293 perror("failed to open file");
6cc870f0 2294 goto out_delete;
956ffd02
TZ
2295 }
2296
2297 err = fstat(input, &perf_stat);
2298 if (err < 0) {
2299 perror("failed to stat file");
6cc870f0 2300 goto out_delete;
956ffd02
TZ
2301 }
2302
2303 if (!perf_stat.st_size) {
2304 fprintf(stderr, "zero-sized file, nothing to do!\n");
6cc870f0 2305 goto out_delete;
956ffd02
TZ
2306 }
2307
2308 scripting_ops = script_spec__lookup(generate_script_lang);
2309 if (!scripting_ops) {
2310 fprintf(stderr, "invalid language specifier");
6cc870f0
NK
2311 err = -ENOENT;
2312 goto out_delete;
956ffd02
TZ
2313 }
2314
29f5ffd3 2315 err = scripting_ops->generate_script(session->tevent.pevent,
da378962 2316 "perf-script");
6cc870f0 2317 goto out_delete;
956ffd02
TZ
2318 }
2319
2320 if (script_name) {
586bc5cc 2321 err = scripting_ops->start_script(script_name, argc, argv);
956ffd02 2322 if (err)
6cc870f0 2323 goto out_delete;
133dc4c3 2324 pr_debug("perf script started with script %s\n\n", script_name);
6cc870f0 2325 script_started = true;
956ffd02
TZ
2326 }
2327
9cbdb702
DA
2328
2329 err = perf_session__check_output_opt(session);
2330 if (err < 0)
6cc870f0 2331 goto out_delete;
9cbdb702 2332
6f3e5eda 2333 err = __cmd_script(&script);
956ffd02 2334
d445dd2a
AH
2335 flush_scripting();
2336
6cc870f0 2337out_delete:
cfc8874a 2338 perf_evlist__free_stats(session->evlist);
d8f66248 2339 perf_session__delete(session);
6cc870f0
NK
2340
2341 if (script_started)
2342 cleanup_scripting();
956ffd02
TZ
2343out:
2344 return err;
5f9c39dc 2345}