perf evlist: Rename for_each() macros to for_each_entry()
[deliverable/linux.git] / tools / perf / builtin-script.c
CommitLineData
5f9c39dc
FW
1#include "builtin.h"
2
b7eead86 3#include "perf.h"
5f9c39dc 4#include "util/cache.h"
b7eead86 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>
6125cc8d 25#include <linux/stringify.h>
cfc8874a 26#include "asm/bug.h"
c19ac912 27#include "util/mem-events.h"
5f9c39dc 28
956ffd02
TZ
29static char const *script_name;
30static char const *generate_script_lang;
ffabd99e 31static bool debug_mode;
e1889d75 32static u64 last_timestamp;
6fcf7ddb 33static u64 nr_unordered;
c0230b2b 34static bool no_callchain;
47390ae2 35static bool latency_format;
317df650 36static bool system_wide;
400ea6d3 37static bool print_flags;
83e19860 38static bool nanosecs;
5d67be97
AB
39static const char *cpu_list;
40static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
91a2c3d5 41static struct perf_stat_config stat_config;
956ffd02 42
44cbe729 43unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
03cd1fed 44
745f43e3
DA
45enum perf_output_field {
46 PERF_OUTPUT_COMM = 1U << 0,
47 PERF_OUTPUT_TID = 1U << 1,
48 PERF_OUTPUT_PID = 1U << 2,
49 PERF_OUTPUT_TIME = 1U << 3,
50 PERF_OUTPUT_CPU = 1U << 4,
51 PERF_OUTPUT_EVNAME = 1U << 5,
52 PERF_OUTPUT_TRACE = 1U << 6,
787bef17
DA
53 PERF_OUTPUT_IP = 1U << 7,
54 PERF_OUTPUT_SYM = 1U << 8,
610723f2 55 PERF_OUTPUT_DSO = 1U << 9,
7cec0922 56 PERF_OUTPUT_ADDR = 1U << 10,
a978f2ab 57 PERF_OUTPUT_SYMOFFSET = 1U << 11,
cc8fae1d 58 PERF_OUTPUT_SRCLINE = 1U << 12,
535aeaae 59 PERF_OUTPUT_PERIOD = 1U << 13,
fc36f948 60 PERF_OUTPUT_IREGS = 1U << 14,
dc323ce8
SE
61 PERF_OUTPUT_BRSTACK = 1U << 15,
62 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
94ddddfa
JO
63 PERF_OUTPUT_DATA_SRC = 1U << 17,
64 PERF_OUTPUT_WEIGHT = 1U << 18,
30372f04 65 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
745f43e3
DA
66};
67
68struct output_option {
69 const char *str;
70 enum perf_output_field field;
71} all_output_options[] = {
72 {.str = "comm", .field = PERF_OUTPUT_COMM},
73 {.str = "tid", .field = PERF_OUTPUT_TID},
74 {.str = "pid", .field = PERF_OUTPUT_PID},
75 {.str = "time", .field = PERF_OUTPUT_TIME},
76 {.str = "cpu", .field = PERF_OUTPUT_CPU},
77 {.str = "event", .field = PERF_OUTPUT_EVNAME},
78 {.str = "trace", .field = PERF_OUTPUT_TRACE},
787bef17 79 {.str = "ip", .field = PERF_OUTPUT_IP},
c0230b2b 80 {.str = "sym", .field = PERF_OUTPUT_SYM},
610723f2 81 {.str = "dso", .field = PERF_OUTPUT_DSO},
7cec0922 82 {.str = "addr", .field = PERF_OUTPUT_ADDR},
a978f2ab 83 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
cc8fae1d 84 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
535aeaae 85 {.str = "period", .field = PERF_OUTPUT_PERIOD},
fc36f948 86 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
dc323ce8
SE
87 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
88 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
94ddddfa
JO
89 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
90 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
30372f04 91 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
745f43e3
DA
92};
93
94/* default set to maintain compatibility with current format */
2c9e45f7
DA
95static struct {
96 bool user_set;
9cbdb702 97 bool wildcard_set;
a6ffaf91 98 unsigned int print_ip_opts;
2c9e45f7
DA
99 u64 fields;
100 u64 invalid_fields;
101} output[PERF_TYPE_MAX] = {
102
103 [PERF_TYPE_HARDWARE] = {
104 .user_set = false,
105
106 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
107 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 108 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71
JO
109 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
110 PERF_OUTPUT_PERIOD,
2c9e45f7 111
30372f04 112 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
2c9e45f7
DA
113 },
114
115 [PERF_TYPE_SOFTWARE] = {
116 .user_set = false,
117
118 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
119 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 120 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71 121 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
30372f04 122 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
2c9e45f7
DA
123
124 .invalid_fields = PERF_OUTPUT_TRACE,
125 },
126
127 [PERF_TYPE_TRACEPOINT] = {
128 .user_set = false,
129
130 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
131 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
30372f04 132 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
2c9e45f7 133 },
0817a6a3
AS
134
135 [PERF_TYPE_RAW] = {
136 .user_set = false,
137
138 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
139 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 140 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
e8564b71 141 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
ff7b1915
JO
142 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
143 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
0817a6a3 144
30372f04 145 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
0817a6a3 146 },
27cfef00
WN
147
148 [PERF_TYPE_BREAKPOINT] = {
149 .user_set = false,
150
151 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
152 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
153 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
154 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
155 PERF_OUTPUT_PERIOD,
156
30372f04 157 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
27cfef00 158 },
1424dc96 159};
745f43e3 160
2c9e45f7
DA
161static bool output_set_by_user(void)
162{
163 int j;
164 for (j = 0; j < PERF_TYPE_MAX; ++j) {
165 if (output[j].user_set)
166 return true;
167 }
168 return false;
169}
745f43e3 170
9cbdb702
DA
171static const char *output_field2str(enum perf_output_field field)
172{
173 int i, imax = ARRAY_SIZE(all_output_options);
174 const char *str = "";
175
176 for (i = 0; i < imax; ++i) {
177 if (all_output_options[i].field == field) {
178 str = all_output_options[i].str;
179 break;
180 }
181 }
182 return str;
183}
184
2c9e45f7 185#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
1424dc96 186
6d5cdd64
AH
187static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
188 u64 sample_type, const char *sample_msg,
189 enum perf_output_field field,
190 bool allow_user_set)
1424dc96 191{
5bff01f6 192 struct perf_event_attr *attr = &evsel->attr;
9cbdb702
DA
193 int type = attr->type;
194 const char *evname;
195
196 if (attr->sample_type & sample_type)
197 return 0;
198
199 if (output[type].user_set) {
6d5cdd64
AH
200 if (allow_user_set)
201 return 0;
5bff01f6 202 evname = perf_evsel__name(evsel);
9cbdb702
DA
203 pr_err("Samples for '%s' event do not have %s attribute set. "
204 "Cannot print '%s' field.\n",
205 evname, sample_msg, output_field2str(field));
206 return -1;
207 }
208
209 /* user did not ask for it explicitly so remove from the default list */
210 output[type].fields &= ~field;
5bff01f6 211 evname = perf_evsel__name(evsel);
9cbdb702
DA
212 pr_debug("Samples for '%s' event do not have %s attribute set. "
213 "Skipping '%s' field.\n",
214 evname, sample_msg, output_field2str(field));
215
216 return 0;
217}
218
6d5cdd64
AH
219static int perf_evsel__check_stype(struct perf_evsel *evsel,
220 u64 sample_type, const char *sample_msg,
221 enum perf_output_field field)
222{
223 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
224 false);
225}
226
9cbdb702
DA
227static int perf_evsel__check_attr(struct perf_evsel *evsel,
228 struct perf_session *session)
229{
230 struct perf_event_attr *attr = &evsel->attr;
6d5cdd64
AH
231 bool allow_user_set;
232
e099eba8
JO
233 if (perf_header__has_feat(&session->header, HEADER_STAT))
234 return 0;
235
6d5cdd64
AH
236 allow_user_set = perf_header__has_feat(&session->header,
237 HEADER_AUXTRACE);
9cbdb702 238
1424dc96
DA
239 if (PRINT_FIELD(TRACE) &&
240 !perf_session__has_traces(session, "record -R"))
241 return -EINVAL;
242
787bef17 243 if (PRINT_FIELD(IP)) {
5bff01f6
ACM
244 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
245 PERF_OUTPUT_IP))
1424dc96 246 return -EINVAL;
1424dc96 247 }
7cec0922
DA
248
249 if (PRINT_FIELD(ADDR) &&
6d5cdd64
AH
250 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
251 PERF_OUTPUT_ADDR, allow_user_set))
7cec0922
DA
252 return -EINVAL;
253
94ddddfa
JO
254 if (PRINT_FIELD(DATA_SRC) &&
255 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
256 PERF_OUTPUT_DATA_SRC))
257 return -EINVAL;
258
259 if (PRINT_FIELD(WEIGHT) &&
260 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
261 PERF_OUTPUT_WEIGHT))
262 return -EINVAL;
263
7cec0922
DA
264 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
265 pr_err("Display of symbols requested but neither sample IP nor "
266 "sample address\nis selected. Hence, no addresses to convert "
267 "to symbols.\n");
787bef17
DA
268 return -EINVAL;
269 }
a978f2ab
AN
270 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
271 pr_err("Display of offsets requested but symbol is not"
272 "selected.\n");
273 return -EINVAL;
274 }
7cec0922
DA
275 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
276 pr_err("Display of DSO requested but neither sample IP nor "
277 "sample address\nis selected. Hence, no addresses to convert "
278 "to DSO.\n");
610723f2
DA
279 return -EINVAL;
280 }
cc8fae1d
AH
281 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
282 pr_err("Display of source line number requested but sample IP is not\n"
283 "selected. Hence, no address to lookup the source line number.\n");
284 return -EINVAL;
285 }
1424dc96
DA
286
287 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
5bff01f6
ACM
288 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
289 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
1424dc96 290 return -EINVAL;
1424dc96
DA
291
292 if (PRINT_FIELD(TIME) &&
5bff01f6
ACM
293 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
294 PERF_OUTPUT_TIME))
1424dc96 295 return -EINVAL;
1424dc96
DA
296
297 if (PRINT_FIELD(CPU) &&
6d5cdd64
AH
298 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
299 PERF_OUTPUT_CPU, allow_user_set))
1424dc96 300 return -EINVAL;
9cbdb702 301
535aeaae
JO
302 if (PRINT_FIELD(PERIOD) &&
303 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
304 PERF_OUTPUT_PERIOD))
305 return -EINVAL;
306
fc36f948
SE
307 if (PRINT_FIELD(IREGS) &&
308 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
309 PERF_OUTPUT_IREGS))
310 return -EINVAL;
311
9cbdb702
DA
312 return 0;
313}
314
7ea95727
AH
315static void set_print_ip_opts(struct perf_event_attr *attr)
316{
317 unsigned int type = attr->type;
318
319 output[type].print_ip_opts = 0;
320 if (PRINT_FIELD(IP))
e20ab86e 321 output[type].print_ip_opts |= EVSEL__PRINT_IP;
7ea95727
AH
322
323 if (PRINT_FIELD(SYM))
e20ab86e 324 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
7ea95727
AH
325
326 if (PRINT_FIELD(DSO))
e20ab86e 327 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
7ea95727
AH
328
329 if (PRINT_FIELD(SYMOFFSET))
e20ab86e 330 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
cc8fae1d
AH
331
332 if (PRINT_FIELD(SRCLINE))
e20ab86e 333 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
7ea95727
AH
334}
335
9cbdb702
DA
336/*
337 * verify all user requested events exist and the samples
338 * have the expected data
339 */
340static int perf_session__check_output_opt(struct perf_session *session)
341{
40f20e50 342 unsigned int j;
9cbdb702
DA
343 struct perf_evsel *evsel;
344
345 for (j = 0; j < PERF_TYPE_MAX; ++j) {
346 evsel = perf_session__find_first_evtype(session, j);
347
348 /*
349 * even if fields is set to 0 (ie., show nothing) event must
350 * exist if user explicitly includes it on the command line
351 */
352 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
353 pr_err("%s events do not exist. "
354 "Remove corresponding -f option to proceed.\n",
355 event_type(j));
356 return -1;
357 }
358
359 if (evsel && output[j].fields &&
360 perf_evsel__check_attr(evsel, session))
361 return -1;
a6ffaf91
DA
362
363 if (evsel == NULL)
364 continue;
365
7ea95727 366 set_print_ip_opts(&evsel->attr);
1424dc96
DA
367 }
368
98526ee7
AH
369 if (!no_callchain) {
370 bool use_callchain = false;
371
e5cadb93 372 evlist__for_each_entry(session->evlist, evsel) {
98526ee7
AH
373 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
374 use_callchain = true;
375 break;
376 }
377 }
378 if (!use_callchain)
379 symbol_conf.use_callchain = false;
380 }
381
80b8b496
DA
382 /*
383 * set default for tracepoints to print symbols only
384 * if callchains are present
385 */
386 if (symbol_conf.use_callchain &&
387 !output[PERF_TYPE_TRACEPOINT].user_set) {
388 struct perf_event_attr *attr;
389
390 j = PERF_TYPE_TRACEPOINT;
80b8b496 391
e5cadb93 392 evlist__for_each_entry(session->evlist, evsel) {
40f20e50
HK
393 if (evsel->attr.type != j)
394 continue;
395
396 attr = &evsel->attr;
80b8b496 397
40f20e50
HK
398 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
399 output[j].fields |= PERF_OUTPUT_IP;
400 output[j].fields |= PERF_OUTPUT_SYM;
401 output[j].fields |= PERF_OUTPUT_DSO;
402 set_print_ip_opts(attr);
403 goto out;
404 }
80b8b496
DA
405 }
406 }
407
408out:
1424dc96
DA
409 return 0;
410}
745f43e3 411
a3dff304 412static void print_sample_iregs(struct perf_sample *sample,
fc36f948
SE
413 struct perf_event_attr *attr)
414{
415 struct regs_dump *regs = &sample->intr_regs;
416 uint64_t mask = attr->sample_regs_intr;
417 unsigned i = 0, r;
418
419 if (!regs)
420 return;
421
422 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
423 u64 val = regs->regs[i++];
424 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
425 }
426}
427
fcf65bf1 428static void print_sample_start(struct perf_sample *sample,
1424dc96 429 struct thread *thread,
5bff01f6 430 struct perf_evsel *evsel)
c70c94b4 431{
5bff01f6 432 struct perf_event_attr *attr = &evsel->attr;
c70c94b4
DA
433 unsigned long secs;
434 unsigned long usecs;
745f43e3
DA
435 unsigned long long nsecs;
436
437 if (PRINT_FIELD(COMM)) {
438 if (latency_format)
b9c5143a 439 printf("%8.8s ", thread__comm_str(thread));
787bef17 440 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
b9c5143a 441 printf("%s ", thread__comm_str(thread));
745f43e3 442 else
b9c5143a 443 printf("%16s ", thread__comm_str(thread));
745f43e3 444 }
c70c94b4 445
745f43e3
DA
446 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
447 printf("%5d/%-5d ", sample->pid, sample->tid);
448 else if (PRINT_FIELD(PID))
449 printf("%5d ", sample->pid);
450 else if (PRINT_FIELD(TID))
451 printf("%5d ", sample->tid);
452
453 if (PRINT_FIELD(CPU)) {
454 if (latency_format)
455 printf("%3d ", sample->cpu);
456 else
457 printf("[%03d] ", sample->cpu);
458 }
c70c94b4 459
745f43e3
DA
460 if (PRINT_FIELD(TIME)) {
461 nsecs = sample->time;
462 secs = nsecs / NSECS_PER_SEC;
463 nsecs -= secs * NSECS_PER_SEC;
464 usecs = nsecs / NSECS_PER_USEC;
83e19860
AH
465 if (nanosecs)
466 printf("%5lu.%09llu: ", secs, nsecs);
467 else
468 printf("%5lu.%06lu: ", secs, usecs);
745f43e3 469 }
c70c94b4
DA
470}
471
dc323ce8
SE
472static inline char
473mispred_str(struct branch_entry *br)
474{
475 if (!(br->flags.mispred || br->flags.predicted))
476 return '-';
477
478 return br->flags.predicted ? 'P' : 'M';
479}
480
a3dff304 481static void print_sample_brstack(struct perf_sample *sample)
dc323ce8
SE
482{
483 struct branch_stack *br = sample->branch_stack;
484 u64 i;
485
486 if (!(br && br->nr))
487 return;
488
489 for (i = 0; i < br->nr; i++) {
490 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
491 br->entries[i].from,
492 br->entries[i].to,
493 mispred_str( br->entries + i),
494 br->entries[i].flags.in_tx? 'X' : '-',
495 br->entries[i].flags.abort? 'A' : '-',
496 br->entries[i].flags.cycles);
497 }
498}
499
a3dff304
ACM
500static void print_sample_brstacksym(struct perf_sample *sample,
501 struct thread *thread)
dc323ce8
SE
502{
503 struct branch_stack *br = sample->branch_stack;
504 struct addr_location alf, alt;
dc323ce8
SE
505 u64 i, from, to;
506
507 if (!(br && br->nr))
508 return;
509
510 for (i = 0; i < br->nr; i++) {
511
512 memset(&alf, 0, sizeof(alf));
513 memset(&alt, 0, sizeof(alt));
514 from = br->entries[i].from;
515 to = br->entries[i].to;
516
473398a2 517 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
dc323ce8
SE
518 if (alf.map)
519 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
520
473398a2 521 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
dc323ce8
SE
522 if (alt.map)
523 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
524
525 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
526 putchar('/');
527 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
528 printf("/%c/%c/%c/%d ",
529 mispred_str( br->entries + i),
530 br->entries[i].flags.in_tx? 'X' : '-',
531 br->entries[i].flags.abort? 'A' : '-',
532 br->entries[i].flags.cycles);
533 }
534}
535
536
a3dff304 537static void print_sample_addr(struct perf_sample *sample,
7cec0922
DA
538 struct thread *thread,
539 struct perf_event_attr *attr)
540{
541 struct addr_location al;
7cec0922
DA
542
543 printf("%16" PRIx64, sample->addr);
544
545 if (!sample_addr_correlates_sym(attr))
546 return;
547
c2740a87 548 thread__resolve(thread, &al, sample);
7cec0922
DA
549
550 if (PRINT_FIELD(SYM)) {
547a92e0 551 printf(" ");
a978f2ab
AN
552 if (PRINT_FIELD(SYMOFFSET))
553 symbol__fprintf_symname_offs(al.sym, &al, stdout);
554 else
555 symbol__fprintf_symname(al.sym, stdout);
7cec0922
DA
556 }
557
558 if (PRINT_FIELD(DSO)) {
547a92e0
AN
559 printf(" (");
560 map__fprintf_dsoname(al.map, stdout);
561 printf(")");
7cec0922
DA
562 }
563}
564
a3dff304 565static void print_sample_bts(struct perf_sample *sample,
95582596 566 struct perf_evsel *evsel,
a2cb3cf2
AH
567 struct thread *thread,
568 struct addr_location *al)
95582596
AN
569{
570 struct perf_event_attr *attr = &evsel->attr;
8066be5f 571 bool print_srcline_last = false;
95582596
AN
572
573 /* print branch_from information */
574 if (PRINT_FIELD(IP)) {
8066be5f 575 unsigned int print_opts = output[attr->type].print_ip_opts;
e557b674 576 struct callchain_cursor *cursor = NULL;
8066be5f 577
6f736735 578 if (symbol_conf.use_callchain && sample->callchain &&
e557b674 579 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
6f736735 580 sample, NULL, NULL, scripting_max_stack) == 0)
e557b674 581 cursor = &callchain_cursor;
6f736735
ACM
582
583 if (cursor == NULL) {
584 putchar(' ');
e20ab86e 585 if (print_opts & EVSEL__PRINT_SRCLINE) {
8066be5f 586 print_srcline_last = true;
e20ab86e 587 print_opts &= ~EVSEL__PRINT_SRCLINE;
8066be5f 588 }
6f736735
ACM
589 } else
590 putchar('\n');
591
592 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
95582596
AN
593 }
594
95582596 595 /* print branch_to information */
243be3dd
AH
596 if (PRINT_FIELD(ADDR) ||
597 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
578bea40
AH
598 !output[attr->type].user_set)) {
599 printf(" => ");
a3dff304 600 print_sample_addr(sample, thread, attr);
578bea40 601 }
95582596 602
8066be5f
AH
603 if (print_srcline_last)
604 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
605
95582596
AN
606 printf("\n");
607}
608
400ea6d3
AH
609static void print_sample_flags(u32 flags)
610{
611 const char *chars = PERF_IP_FLAG_CHARS;
612 const int n = strlen(PERF_IP_FLAG_CHARS);
613 char str[33];
614 int i, pos = 0;
615
616 for (i = 0; i < n; i++, flags >>= 1) {
617 if (flags & 1)
618 str[pos++] = chars[i];
619 }
620 for (; i < 32; i++, flags >>= 1) {
621 if (flags & 1)
622 str[pos++] = '?';
623 }
624 str[pos] = 0;
625 printf(" %-4s ", str);
626}
627
30372f04
WN
628struct printer_data {
629 int line_no;
630 bool hit_nul;
631 bool is_printable;
632};
633
634static void
635print_sample_bpf_output_printer(enum binary_printer_ops op,
636 unsigned int val,
637 void *extra)
638{
639 unsigned char ch = (unsigned char)val;
640 struct printer_data *printer_data = extra;
641
642 switch (op) {
643 case BINARY_PRINT_DATA_BEGIN:
644 printf("\n");
645 break;
646 case BINARY_PRINT_LINE_BEGIN:
647 printf("%17s", !printer_data->line_no ? "BPF output:" :
648 " ");
649 break;
650 case BINARY_PRINT_ADDR:
651 printf(" %04x:", val);
652 break;
653 case BINARY_PRINT_NUM_DATA:
654 printf(" %02x", val);
655 break;
656 case BINARY_PRINT_NUM_PAD:
657 printf(" ");
658 break;
659 case BINARY_PRINT_SEP:
660 printf(" ");
661 break;
662 case BINARY_PRINT_CHAR_DATA:
663 if (printer_data->hit_nul && ch)
664 printer_data->is_printable = false;
665
666 if (!isprint(ch)) {
667 printf("%c", '.');
668
669 if (!printer_data->is_printable)
670 break;
671
672 if (ch == '\0')
673 printer_data->hit_nul = true;
674 else
675 printer_data->is_printable = false;
676 } else {
677 printf("%c", ch);
678 }
679 break;
680 case BINARY_PRINT_CHAR_PAD:
681 printf(" ");
682 break;
683 case BINARY_PRINT_LINE_END:
684 printf("\n");
685 printer_data->line_no++;
686 break;
687 case BINARY_PRINT_DATA_END:
688 default:
689 break;
690 }
691}
692
693static void print_sample_bpf_output(struct perf_sample *sample)
694{
695 unsigned int nr_bytes = sample->raw_size;
696 struct printer_data printer_data = {0, false, true};
697
698 print_binary(sample->raw_data, nr_bytes, 8,
699 print_sample_bpf_output_printer, &printer_data);
700
701 if (printer_data.is_printable && printer_data.hit_nul)
702 printf("%17s \"%s\"\n", "BPF string:",
703 (char *)(sample->raw_data));
704}
705
809e9423
JO
706struct perf_script {
707 struct perf_tool tool;
708 struct perf_session *session;
709 bool show_task_events;
710 bool show_mmap_events;
711 bool show_switch_events;
cfc8874a
JO
712 bool allocated;
713 struct cpu_map *cpus;
714 struct thread_map *threads;
9cdbc409 715 int name_width;
809e9423
JO
716};
717
9cdbc409
JO
718static int perf_evlist__max_name_len(struct perf_evlist *evlist)
719{
720 struct perf_evsel *evsel;
721 int max = 0;
722
e5cadb93 723 evlist__for_each_entry(evlist, evsel) {
9cdbc409
JO
724 int len = strlen(perf_evsel__name(evsel));
725
726 max = MAX(len, max);
727 }
728
729 return max;
730}
731
c19ac912
JO
732static size_t data_src__printf(u64 data_src)
733{
734 struct mem_info mi = { .data_src.val = data_src };
735 char decode[100];
736 char out[100];
737 static int maxlen;
738 int len;
739
740 perf_script__meminfo_scnprintf(decode, 100, &mi);
741
742 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
743 if (maxlen < len)
744 maxlen = len;
745
746 return printf("%-*s", maxlen, out);
747}
748
a3dff304 749static void process_event(struct perf_script *script,
809e9423
JO
750 struct perf_sample *sample, struct perf_evsel *evsel,
751 struct addr_location *al)
be6d842a 752{
f9d5d549 753 struct thread *thread = al->thread;
9e69c210 754 struct perf_event_attr *attr = &evsel->attr;
1424dc96 755
2c9e45f7 756 if (output[attr->type].fields == 0)
1424dc96
DA
757 return;
758
fcf65bf1 759 print_sample_start(sample, thread, evsel);
745f43e3 760
535aeaae
JO
761 if (PRINT_FIELD(PERIOD))
762 printf("%10" PRIu64 " ", sample->period);
763
e944d3d7
NK
764 if (PRINT_FIELD(EVNAME)) {
765 const char *evname = perf_evsel__name(evsel);
9cdbc409
JO
766
767 if (!script->name_width)
768 script->name_width = perf_evlist__max_name_len(script->session->evlist);
769
770 printf("%*s: ", script->name_width,
771 evname ? evname : "[unknown]");
e944d3d7
NK
772 }
773
400ea6d3
AH
774 if (print_flags)
775 print_sample_flags(sample->flags);
776
95582596 777 if (is_bts_event(attr)) {
a3dff304 778 print_sample_bts(sample, evsel, thread, al);
95582596
AN
779 return;
780 }
781
745f43e3 782 if (PRINT_FIELD(TRACE))
fcf65bf1
ACM
783 event_format__print(evsel->tp_format, sample->cpu,
784 sample->raw_data, sample->raw_size);
7cec0922 785 if (PRINT_FIELD(ADDR))
a3dff304 786 print_sample_addr(sample, thread, attr);
7cec0922 787
94ddddfa 788 if (PRINT_FIELD(DATA_SRC))
c19ac912 789 data_src__printf(sample->data_src);
94ddddfa
JO
790
791 if (PRINT_FIELD(WEIGHT))
792 printf("%16" PRIu64, sample->weight);
793
787bef17 794 if (PRINT_FIELD(IP)) {
e557b674 795 struct callchain_cursor *cursor = NULL;
6f736735 796
92231521 797 if (symbol_conf.use_callchain && sample->callchain &&
e557b674 798 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
6f736735 799 sample, NULL, NULL, scripting_max_stack) == 0)
e557b674 800 cursor = &callchain_cursor;
a6ffaf91 801
6f736735
ACM
802 putchar(cursor ? '\n' : ' ');
803 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
c0230b2b
DA
804 }
805
fc36f948 806 if (PRINT_FIELD(IREGS))
a3dff304 807 print_sample_iregs(sample, attr);
fc36f948 808
dc323ce8 809 if (PRINT_FIELD(BRSTACK))
a3dff304 810 print_sample_brstack(sample);
dc323ce8 811 else if (PRINT_FIELD(BRSTACKSYM))
a3dff304 812 print_sample_brstacksym(sample, thread);
dc323ce8 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
bb3eb566 908 if (machine__resolve(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
a3dff304 923 process_event(scr, 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
e5cadb93 948 evlist__for_each_entry(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
a5e8e825
ACM
1428#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1429 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1430 if ((lang_dirent->d_type == DT_DIR || \
1431 (lang_dirent->d_type == DT_UNKNOWN && \
1432 is_directory(scripts_path, lang_dirent))) && \
1433 (strcmp(lang_dirent->d_name, ".")) && \
1434 (strcmp(lang_dirent->d_name, "..")))
1435
1436#define for_each_script(lang_path, lang_dir, script_dirent) \
1437 while ((script_dirent = readdir(lang_dir)) != NULL) \
1438 if (script_dirent->d_type != DT_DIR && \
1439 (script_dirent->d_type != DT_UNKNOWN || \
1440 !is_directory(lang_path, script_dirent)))
4b9c0c59
TZ
1441
1442
1443#define RECORD_SUFFIX "-record"
1444#define REPORT_SUFFIX "-report"
1445
1446struct script_desc {
1447 struct list_head node;
1448 char *name;
1449 char *half_liner;
1450 char *args;
1451};
1452
eccdfe2d 1453static LIST_HEAD(script_descs);
4b9c0c59
TZ
1454
1455static struct script_desc *script_desc__new(const char *name)
1456{
1457 struct script_desc *s = zalloc(sizeof(*s));
1458
b5b87312 1459 if (s != NULL && name)
4b9c0c59
TZ
1460 s->name = strdup(name);
1461
1462 return s;
1463}
1464
1465static void script_desc__delete(struct script_desc *s)
1466{
74cf249d
ACM
1467 zfree(&s->name);
1468 zfree(&s->half_liner);
1469 zfree(&s->args);
4b9c0c59
TZ
1470 free(s);
1471}
1472
1473static void script_desc__add(struct script_desc *s)
1474{
1475 list_add_tail(&s->node, &script_descs);
1476}
1477
1478static struct script_desc *script_desc__find(const char *name)
1479{
1480 struct script_desc *s;
1481
1482 list_for_each_entry(s, &script_descs, node)
1483 if (strcasecmp(s->name, name) == 0)
1484 return s;
1485 return NULL;
1486}
1487
1488static struct script_desc *script_desc__findnew(const char *name)
1489{
1490 struct script_desc *s = script_desc__find(name);
1491
1492 if (s)
1493 return s;
1494
1495 s = script_desc__new(name);
1496 if (!s)
1497 goto out_delete_desc;
1498
1499 script_desc__add(s);
1500
1501 return s;
1502
1503out_delete_desc:
1504 script_desc__delete(s);
1505
1506 return NULL;
1507}
1508
965bb6be 1509static const char *ends_with(const char *str, const char *suffix)
4b9c0c59
TZ
1510{
1511 size_t suffix_len = strlen(suffix);
965bb6be 1512 const char *p = str;
4b9c0c59
TZ
1513
1514 if (strlen(str) > suffix_len) {
1515 p = str + strlen(str) - suffix_len;
1516 if (!strncmp(p, suffix, suffix_len))
1517 return p;
1518 }
1519
1520 return NULL;
1521}
1522
4b9c0c59
TZ
1523static int read_script_info(struct script_desc *desc, const char *filename)
1524{
1525 char line[BUFSIZ], *p;
1526 FILE *fp;
1527
1528 fp = fopen(filename, "r");
1529 if (!fp)
1530 return -1;
1531
1532 while (fgets(line, sizeof(line), fp)) {
1533 p = ltrim(line);
1534 if (strlen(p) == 0)
1535 continue;
1536 if (*p != '#')
1537 continue;
1538 p++;
1539 if (strlen(p) && *p == '!')
1540 continue;
1541
1542 p = ltrim(p);
1543 if (strlen(p) && p[strlen(p) - 1] == '\n')
1544 p[strlen(p) - 1] = '\0';
1545
1546 if (!strncmp(p, "description:", strlen("description:"))) {
1547 p += strlen("description:");
1548 desc->half_liner = strdup(ltrim(p));
1549 continue;
1550 }
1551
1552 if (!strncmp(p, "args:", strlen("args:"))) {
1553 p += strlen("args:");
1554 desc->args = strdup(ltrim(p));
1555 continue;
1556 }
1557 }
1558
1559 fclose(fp);
1560
1561 return 0;
1562}
1563
38efb539
RR
1564static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1565{
1566 char *script_root, *str;
1567
1568 script_root = strdup(script_dirent->d_name);
1569 if (!script_root)
1570 return NULL;
1571
1572 str = (char *)ends_with(script_root, suffix);
1573 if (!str) {
1574 free(script_root);
1575 return NULL;
1576 }
1577
1578 *str = '\0';
1579 return script_root;
1580}
1581
1d037ca1
IT
1582static int list_available_scripts(const struct option *opt __maybe_unused,
1583 const char *s __maybe_unused,
1584 int unset __maybe_unused)
4b9c0c59 1585{
a5e8e825 1586 struct dirent *script_dirent, *lang_dirent;
4b9c0c59
TZ
1587 char scripts_path[MAXPATHLEN];
1588 DIR *scripts_dir, *lang_dir;
1589 char script_path[MAXPATHLEN];
1590 char lang_path[MAXPATHLEN];
1591 struct script_desc *desc;
1592 char first_half[BUFSIZ];
1593 char *script_root;
4b9c0c59 1594
46113a54 1595 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
4b9c0c59
TZ
1596
1597 scripts_dir = opendir(scripts_path);
1598 if (!scripts_dir)
1599 return -1;
1600
a5e8e825 1601 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
4b9c0c59 1602 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
a5e8e825 1603 lang_dirent->d_name);
4b9c0c59
TZ
1604 lang_dir = opendir(lang_path);
1605 if (!lang_dir)
1606 continue;
1607
a5e8e825
ACM
1608 for_each_script(lang_path, lang_dir, script_dirent) {
1609 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
38efb539 1610 if (script_root) {
4b9c0c59
TZ
1611 desc = script_desc__findnew(script_root);
1612 snprintf(script_path, MAXPATHLEN, "%s/%s",
a5e8e825 1613 lang_path, script_dirent->d_name);
4b9c0c59 1614 read_script_info(desc, script_path);
38efb539 1615 free(script_root);
4b9c0c59 1616 }
4b9c0c59
TZ
1617 }
1618 }
1619
1620 fprintf(stdout, "List of available trace scripts:\n");
1621 list_for_each_entry(desc, &script_descs, node) {
1622 sprintf(first_half, "%s %s", desc->name,
1623 desc->args ? desc->args : "");
1624 fprintf(stdout, " %-36s %s\n", first_half,
1625 desc->half_liner ? desc->half_liner : "");
1626 }
1627
1628 exit(0);
1629}
1630
49e639e2
FT
1631/*
1632 * Some scripts specify the required events in their "xxx-record" file,
1633 * this function will check if the events in perf.data match those
1634 * mentioned in the "xxx-record".
1635 *
1636 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1637 * which is covered well now. And new parsing code should be added to
1638 * cover the future complexing formats like event groups etc.
1639 */
1640static int check_ev_match(char *dir_name, char *scriptname,
1641 struct perf_session *session)
1642{
1643 char filename[MAXPATHLEN], evname[128];
1644 char line[BUFSIZ], *p;
1645 struct perf_evsel *pos;
1646 int match, len;
1647 FILE *fp;
1648
1649 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1650
1651 fp = fopen(filename, "r");
1652 if (!fp)
1653 return -1;
1654
1655 while (fgets(line, sizeof(line), fp)) {
1656 p = ltrim(line);
1657 if (*p == '#')
1658 continue;
1659
1660 while (strlen(p)) {
1661 p = strstr(p, "-e");
1662 if (!p)
1663 break;
1664
1665 p += 2;
1666 p = ltrim(p);
1667 len = strcspn(p, " \t");
1668 if (!len)
1669 break;
1670
1671 snprintf(evname, len + 1, "%s", p);
1672
1673 match = 0;
e5cadb93 1674 evlist__for_each_entry(session->evlist, pos) {
49e639e2
FT
1675 if (!strcmp(perf_evsel__name(pos), evname)) {
1676 match = 1;
1677 break;
1678 }
1679 }
1680
1681 if (!match) {
1682 fclose(fp);
1683 return -1;
1684 }
1685 }
1686 }
1687
1688 fclose(fp);
1689 return 0;
1690}
1691
e5f3705e
FT
1692/*
1693 * Return -1 if none is found, otherwise the actual scripts number.
1694 *
1695 * Currently the only user of this function is the script browser, which
1696 * will list all statically runnable scripts, select one, execute it and
1697 * show the output in a perf browser.
1698 */
1699int find_scripts(char **scripts_array, char **scripts_path_array)
1700{
a5e8e825 1701 struct dirent *script_dirent, *lang_dirent;
49e639e2 1702 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
e5f3705e 1703 DIR *scripts_dir, *lang_dir;
49e639e2 1704 struct perf_session *session;
f5fc1412
JO
1705 struct perf_data_file file = {
1706 .path = input_name,
1707 .mode = PERF_DATA_MODE_READ,
1708 };
e5f3705e
FT
1709 char *temp;
1710 int i = 0;
1711
f5fc1412 1712 session = perf_session__new(&file, false, NULL);
49e639e2
FT
1713 if (!session)
1714 return -1;
1715
46113a54 1716 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
e5f3705e
FT
1717
1718 scripts_dir = opendir(scripts_path);
49e639e2
FT
1719 if (!scripts_dir) {
1720 perf_session__delete(session);
e5f3705e 1721 return -1;
49e639e2 1722 }
e5f3705e 1723
a5e8e825 1724 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
e5f3705e 1725 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
a5e8e825 1726 lang_dirent->d_name);
e5f3705e
FT
1727#ifdef NO_LIBPERL
1728 if (strstr(lang_path, "perl"))
1729 continue;
1730#endif
1731#ifdef NO_LIBPYTHON
1732 if (strstr(lang_path, "python"))
1733 continue;
1734#endif
1735
1736 lang_dir = opendir(lang_path);
1737 if (!lang_dir)
1738 continue;
1739
a5e8e825 1740 for_each_script(lang_path, lang_dir, script_dirent) {
e5f3705e 1741 /* Skip those real time scripts: xxxtop.p[yl] */
a5e8e825 1742 if (strstr(script_dirent->d_name, "top."))
e5f3705e
FT
1743 continue;
1744 sprintf(scripts_path_array[i], "%s/%s", lang_path,
a5e8e825
ACM
1745 script_dirent->d_name);
1746 temp = strchr(script_dirent->d_name, '.');
e5f3705e 1747 snprintf(scripts_array[i],
a5e8e825
ACM
1748 (temp - script_dirent->d_name) + 1,
1749 "%s", script_dirent->d_name);
49e639e2
FT
1750
1751 if (check_ev_match(lang_path,
1752 scripts_array[i], session))
1753 continue;
1754
e5f3705e
FT
1755 i++;
1756 }
49e639e2 1757 closedir(lang_dir);
e5f3705e
FT
1758 }
1759
49e639e2
FT
1760 closedir(scripts_dir);
1761 perf_session__delete(session);
e5f3705e
FT
1762 return i;
1763}
1764
3875294f
TZ
1765static char *get_script_path(const char *script_root, const char *suffix)
1766{
a5e8e825 1767 struct dirent *script_dirent, *lang_dirent;
3875294f
TZ
1768 char scripts_path[MAXPATHLEN];
1769 char script_path[MAXPATHLEN];
1770 DIR *scripts_dir, *lang_dir;
1771 char lang_path[MAXPATHLEN];
38efb539 1772 char *__script_root;
3875294f 1773
46113a54 1774 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3875294f
TZ
1775
1776 scripts_dir = opendir(scripts_path);
1777 if (!scripts_dir)
1778 return NULL;
1779
a5e8e825 1780 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3875294f 1781 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
a5e8e825 1782 lang_dirent->d_name);
3875294f
TZ
1783 lang_dir = opendir(lang_path);
1784 if (!lang_dir)
1785 continue;
1786
a5e8e825
ACM
1787 for_each_script(lang_path, lang_dir, script_dirent) {
1788 __script_root = get_script_root(script_dirent, suffix);
38efb539
RR
1789 if (__script_root && !strcmp(script_root, __script_root)) {
1790 free(__script_root);
946ef2a2
NK
1791 closedir(lang_dir);
1792 closedir(scripts_dir);
3875294f 1793 snprintf(script_path, MAXPATHLEN, "%s/%s",
a5e8e825 1794 lang_path, script_dirent->d_name);
38efb539 1795 return strdup(script_path);
3875294f
TZ
1796 }
1797 free(__script_root);
1798 }
946ef2a2 1799 closedir(lang_dir);
3875294f 1800 }
946ef2a2 1801 closedir(scripts_dir);
3875294f 1802
38efb539 1803 return NULL;
3875294f
TZ
1804}
1805
b5b87312
TZ
1806static bool is_top_script(const char *script_path)
1807{
965bb6be 1808 return ends_with(script_path, "top") == NULL ? false : true;
b5b87312
TZ
1809}
1810
1811static int has_required_arg(char *script_path)
1812{
1813 struct script_desc *desc;
1814 int n_args = 0;
1815 char *p;
1816
1817 desc = script_desc__new(NULL);
1818
1819 if (read_script_info(desc, script_path))
1820 goto out;
1821
1822 if (!desc->args)
1823 goto out;
1824
1825 for (p = desc->args; *p; p++)
1826 if (*p == '<')
1827 n_args++;
1828out:
1829 script_desc__delete(desc);
1830
1831 return n_args;
1832}
1833
69b6470e
ACM
1834static int have_cmd(int argc, const char **argv)
1835{
1836 char **__argv = malloc(sizeof(const char *) * argc);
1837
1838 if (!__argv) {
1839 pr_err("malloc failed\n");
1840 return -1;
1841 }
1842
1843 memcpy(__argv, argv, sizeof(const char *) * argc);
1844 argc = parse_options(argc, (const char **)__argv, record_options,
1845 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1846 free(__argv);
5f9c39dc 1847
69b6470e
ACM
1848 system_wide = (argc == 0);
1849
1850 return 0;
1851}
1852
7322d6c9
JO
1853static void script__setup_sample_type(struct perf_script *script)
1854{
1855 struct perf_session *session = script->session;
1856 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1857
1858 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1859 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1860 (sample_type & PERF_SAMPLE_STACK_USER))
1861 callchain_param.record_mode = CALLCHAIN_DWARF;
1862 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1863 callchain_param.record_mode = CALLCHAIN_LBR;
1864 else
1865 callchain_param.record_mode = CALLCHAIN_FP;
1866 }
1867}
1868
e099eba8
JO
1869static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1870 union perf_event *event,
1871 struct perf_session *session)
1872{
1873 struct stat_round_event *round = &event->stat_round;
1874 struct perf_evsel *counter;
1875
e5cadb93 1876 evlist__for_each_entry(session->evlist, counter) {
e099eba8
JO
1877 perf_stat_process_counter(&stat_config, counter);
1878 process_stat(counter, round->time);
1879 }
1880
1881 process_stat_interval(round->time);
1882 return 0;
1883}
1884
91a2c3d5
JO
1885static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1886 union perf_event *event,
1887 struct perf_session *session __maybe_unused)
1888{
1889 perf_event__read_stat_config(&stat_config, &event->stat_config);
1890 return 0;
1891}
1892
cfc8874a
JO
1893static int set_maps(struct perf_script *script)
1894{
1895 struct perf_evlist *evlist = script->session->evlist;
1896
1897 if (!script->cpus || !script->threads)
1898 return 0;
1899
1900 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1901 return -EINVAL;
1902
1903 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1904
1905 if (perf_evlist__alloc_stats(evlist, true))
1906 return -ENOMEM;
1907
1908 script->allocated = true;
1909 return 0;
1910}
1911
1912static
1913int process_thread_map_event(struct perf_tool *tool,
1914 union perf_event *event,
1915 struct perf_session *session __maybe_unused)
1916{
1917 struct perf_script *script = container_of(tool, struct perf_script, tool);
1918
1919 if (script->threads) {
1920 pr_warning("Extra thread map event, ignoring.\n");
1921 return 0;
1922 }
1923
1924 script->threads = thread_map__new_event(&event->thread_map);
1925 if (!script->threads)
1926 return -ENOMEM;
1927
1928 return set_maps(script);
1929}
1930
1931static
1932int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1933 union perf_event *event,
1934 struct perf_session *session __maybe_unused)
1935{
1936 struct perf_script *script = container_of(tool, struct perf_script, tool);
1937
1938 if (script->cpus) {
1939 pr_warning("Extra cpu map event, ignoring.\n");
1940 return 0;
1941 }
1942
1943 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1944 if (!script->cpus)
1945 return -ENOMEM;
1946
1947 return set_maps(script);
1948}
1949
69b6470e
ACM
1950int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1951{
1952 bool show_full_info = false;
e90debdd
JO
1953 bool header = false;
1954 bool header_only = false;
6cc870f0 1955 bool script_started = false;
69b6470e
ACM
1956 char *rec_script_path = NULL;
1957 char *rep_script_path = NULL;
1958 struct perf_session *session;
7a680eb9 1959 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
69b6470e
ACM
1960 char *script_path = NULL;
1961 const char **__argv;
6cc870f0 1962 int i, j, err = 0;
6f3e5eda
AH
1963 struct perf_script script = {
1964 .tool = {
1965 .sample = process_sample_event,
1966 .mmap = perf_event__process_mmap,
1967 .mmap2 = perf_event__process_mmap2,
1968 .comm = perf_event__process_comm,
1969 .exit = perf_event__process_exit,
1970 .fork = perf_event__process_fork,
7ea95727 1971 .attr = process_attr,
91daee30 1972 .event_update = perf_event__process_event_update,
6f3e5eda
AH
1973 .tracing_data = perf_event__process_tracing_data,
1974 .build_id = perf_event__process_build_id,
7a680eb9
AH
1975 .id_index = perf_event__process_id_index,
1976 .auxtrace_info = perf_event__process_auxtrace_info,
1977 .auxtrace = perf_event__process_auxtrace,
1978 .auxtrace_error = perf_event__process_auxtrace_error,
e099eba8
JO
1979 .stat = perf_event__process_stat_event,
1980 .stat_round = process_stat_round_event,
91a2c3d5 1981 .stat_config = process_stat_config_event,
cfc8874a
JO
1982 .thread_map = process_thread_map_event,
1983 .cpu_map = process_cpu_map_event,
0a8cb85c 1984 .ordered_events = true,
6f3e5eda
AH
1985 .ordering_requires_timestamps = true,
1986 },
1987 };
06af0f2c
YS
1988 struct perf_data_file file = {
1989 .mode = PERF_DATA_MODE_READ,
1990 };
69b6470e 1991 const struct option options[] = {
5f9c39dc
FW
1992 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1993 "dump raw trace in ASCII"),
c0555642 1994 OPT_INCR('v', "verbose", &verbose,
69b6470e 1995 "be more verbose (show symbol address, etc)"),
4b9c0c59 1996 OPT_BOOLEAN('L', "Latency", &latency_format,
cda48461 1997 "show latency attributes (irqs/preemption disabled, etc)"),
4b9c0c59
TZ
1998 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1999 list_available_scripts),
956ffd02
TZ
2000 OPT_CALLBACK('s', "script", NULL, "name",
2001 "script file name (lang:script name, script name, or *)",
2002 parse_scriptname),
2003 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
133dc4c3 2004 "generate perf-script.xx script in specified language"),
69b6470e 2005 OPT_STRING('i', "input", &input_name, "file", "input file name"),
ffabd99e
FW
2006 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2007 "do various checks like samples ordering and lost events"),
e90debdd
JO
2008 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2009 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
c0230b2b
DA
2010 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2011 "file", "vmlinux pathname"),
2012 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2013 "file", "kallsyms pathname"),
2014 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2015 "When printing symbols do not display call chain"),
a7066709
HK
2016 OPT_CALLBACK(0, "symfs", NULL, "directory",
2017 "Look for files with symbols relative to this directory",
2018 symbol__config_symfs),
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"),
6125cc8d
ACM
2035 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2036 "Set the maximum stack depth when parsing the callchain, "
2037 "anything beyond the specified depth will be ignored. "
4cb93446 2038 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
fbe96f29
SE
2039 OPT_BOOLEAN('I', "show-info", &show_full_info,
2040 "display extended information from perf.data file"),
0bc8d205
AN
2041 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2042 "Show the path of [kernel.kallsyms]"),
ad7ebb9a
NK
2043 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2044 "Show the fork/comm/exit events"),
ba1ddf42
NK
2045 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2046 "Show the mmap events"),
7c14898b
AH
2047 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2048 "Show context switch events (if recorded)"),
06af0f2c 2049 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
83e19860
AH
2050 OPT_BOOLEAN(0, "ns", &nanosecs,
2051 "Use 9 decimal places when displaying time"),
7a680eb9
AH
2052 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2053 "Instruction Tracing options",
2054 itrace_parse_synth_opts),
a9710ba0
AK
2055 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2056 "Show full source file name path for source lines"),
77e0070d
MD
2057 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2058 "Enable symbol demangling"),
2059 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2060 "Enable kernel symbol demangling"),
2061
1909629f 2062 OPT_END()
69b6470e 2063 };
40cae2b7
YS
2064 const char * const script_subcommands[] = { "record", "report", NULL };
2065 const char *script_usage[] = {
69b6470e
ACM
2066 "perf script [<options>]",
2067 "perf script [<options>] record <script> [<record-options>] <command>",
2068 "perf script [<options>] report <script> [script-args]",
2069 "perf script [<options>] <script> [<record-options>] <command>",
2070 "perf script [<options>] <top-script> [script-args]",
2071 NULL
2072 };
3875294f 2073
b5b87312
TZ
2074 setup_scripting();
2075
40cae2b7 2076 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
b5b87312
TZ
2077 PARSE_OPT_STOP_AT_NON_OPTION);
2078
f5fc1412
JO
2079 file.path = input_name;
2080
b5b87312
TZ
2081 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2082 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2083 if (!rec_script_path)
2084 return cmd_record(argc, argv, NULL);
3875294f
TZ
2085 }
2086
b5b87312
TZ
2087 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2088 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2089 if (!rep_script_path) {
3875294f 2090 fprintf(stderr,
b5b87312 2091 "Please specify a valid report script"
133dc4c3 2092 "(see 'perf script -l' for listing)\n");
3875294f
TZ
2093 return -1;
2094 }
3875294f
TZ
2095 }
2096
3c5b645f
AH
2097 if (itrace_synth_opts.callchain &&
2098 itrace_synth_opts.callchain_sz > scripting_max_stack)
2099 scripting_max_stack = itrace_synth_opts.callchain_sz;
2100
44e668c6 2101 /* make sure PERF_EXEC_PATH is set for scripts */
46113a54 2102 set_argv_exec_path(get_argv_exec_path());
44e668c6 2103
b5b87312 2104 if (argc && !script_name && !rec_script_path && !rep_script_path) {
a0cccc2e 2105 int live_pipe[2];
b5b87312 2106 int rep_args;
a0cccc2e
TZ
2107 pid_t pid;
2108
b5b87312
TZ
2109 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2110 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2111
2112 if (!rec_script_path && !rep_script_path) {
c7118369
NK
2113 usage_with_options_msg(script_usage, options,
2114 "Couldn't find script `%s'\n\n See perf"
133dc4c3 2115 " script -l for available scripts.\n", argv[0]);
a0cccc2e
TZ
2116 }
2117
b5b87312
TZ
2118 if (is_top_script(argv[0])) {
2119 rep_args = argc - 1;
2120 } else {
2121 int rec_args;
2122
2123 rep_args = has_required_arg(rep_script_path);
2124 rec_args = (argc - 1) - rep_args;
2125 if (rec_args < 0) {
c7118369
NK
2126 usage_with_options_msg(script_usage, options,
2127 "`%s' script requires options."
133dc4c3 2128 "\n\n See perf script -l for available "
b5b87312 2129 "scripts and options.\n", argv[0]);
b5b87312 2130 }
a0cccc2e
TZ
2131 }
2132
2133 if (pipe(live_pipe) < 0) {
2134 perror("failed to create pipe");
d54b1a9e 2135 return -1;
a0cccc2e
TZ
2136 }
2137
2138 pid = fork();
2139 if (pid < 0) {
2140 perror("failed to fork");
d54b1a9e 2141 return -1;
a0cccc2e
TZ
2142 }
2143
2144 if (!pid) {
b5b87312
TZ
2145 j = 0;
2146
a0cccc2e
TZ
2147 dup2(live_pipe[1], 1);
2148 close(live_pipe[0]);
2149
317df650
RR
2150 if (is_top_script(argv[0])) {
2151 system_wide = true;
2152 } else if (!system_wide) {
d54b1a9e
DA
2153 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2154 err = -1;
2155 goto out;
2156 }
317df650 2157 }
b5b87312
TZ
2158
2159 __argv = malloc((argc + 6) * sizeof(const char *));
d54b1a9e
DA
2160 if (!__argv) {
2161 pr_err("malloc failed\n");
2162 err = -ENOMEM;
2163 goto out;
2164 }
e8719adf 2165
b5b87312
TZ
2166 __argv[j++] = "/bin/sh";
2167 __argv[j++] = rec_script_path;
2168 if (system_wide)
2169 __argv[j++] = "-a";
2170 __argv[j++] = "-q";
2171 __argv[j++] = "-o";
2172 __argv[j++] = "-";
2173 for (i = rep_args + 1; i < argc; i++)
2174 __argv[j++] = argv[i];
2175 __argv[j++] = NULL;
a0cccc2e
TZ
2176
2177 execvp("/bin/sh", (char **)__argv);
e8719adf 2178 free(__argv);
a0cccc2e
TZ
2179 exit(-1);
2180 }
2181
2182 dup2(live_pipe[0], 0);
2183 close(live_pipe[1]);
2184
b5b87312 2185 __argv = malloc((argc + 4) * sizeof(const char *));
d54b1a9e
DA
2186 if (!__argv) {
2187 pr_err("malloc failed\n");
2188 err = -ENOMEM;
2189 goto out;
2190 }
2191
b5b87312
TZ
2192 j = 0;
2193 __argv[j++] = "/bin/sh";
2194 __argv[j++] = rep_script_path;
2195 for (i = 1; i < rep_args + 1; i++)
2196 __argv[j++] = argv[i];
2197 __argv[j++] = "-i";
2198 __argv[j++] = "-";
2199 __argv[j++] = NULL;
a0cccc2e
TZ
2200
2201 execvp("/bin/sh", (char **)__argv);
e8719adf 2202 free(__argv);
a0cccc2e
TZ
2203 exit(-1);
2204 }
2205
b5b87312
TZ
2206 if (rec_script_path)
2207 script_path = rec_script_path;
2208 if (rep_script_path)
2209 script_path = rep_script_path;
34c86ea9 2210
b5b87312 2211 if (script_path) {
b5b87312 2212 j = 0;
3875294f 2213
317df650
RR
2214 if (!rec_script_path)
2215 system_wide = false;
d54b1a9e
DA
2216 else if (!system_wide) {
2217 if (have_cmd(argc - 1, &argv[1]) != 0) {
2218 err = -1;
2219 goto out;
2220 }
2221 }
34c86ea9 2222
b5b87312 2223 __argv = malloc((argc + 2) * sizeof(const char *));
d54b1a9e
DA
2224 if (!__argv) {
2225 pr_err("malloc failed\n");
2226 err = -ENOMEM;
2227 goto out;
2228 }
2229
34c86ea9
TZ
2230 __argv[j++] = "/bin/sh";
2231 __argv[j++] = script_path;
2232 if (system_wide)
2233 __argv[j++] = "-a";
b5b87312 2234 for (i = 2; i < argc; i++)
34c86ea9
TZ
2235 __argv[j++] = argv[i];
2236 __argv[j++] = NULL;
3875294f
TZ
2237
2238 execvp("/bin/sh", (char **)__argv);
e8719adf 2239 free(__argv);
3875294f
TZ
2240 exit(-1);
2241 }
956ffd02 2242
cf4fee50
TZ
2243 if (!script_name)
2244 setup_pager();
5f9c39dc 2245
6f3e5eda 2246 session = perf_session__new(&file, false, &script.tool);
d8f66248 2247 if (session == NULL)
52e02834 2248 return -1;
d8f66248 2249
e90debdd
JO
2250 if (header || header_only) {
2251 perf_session__fprintf_info(session, stdout, show_full_info);
2252 if (header_only)
6cc870f0 2253 goto out_delete;
e90debdd
JO
2254 }
2255
0a7e6d1b 2256 if (symbol__init(&session->header.env) < 0)
38520dc3
NK
2257 goto out_delete;
2258
6f3e5eda 2259 script.session = session;
7322d6c9 2260 script__setup_sample_type(&script);
6f3e5eda 2261
7a680eb9
AH
2262 session->itrace_synth_opts = &itrace_synth_opts;
2263
5d67be97 2264 if (cpu_list) {
6cc870f0
NK
2265 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2266 if (err < 0)
2267 goto out_delete;
5d67be97
AB
2268 }
2269
1424dc96 2270 if (!no_callchain)
c0230b2b
DA
2271 symbol_conf.use_callchain = true;
2272 else
2273 symbol_conf.use_callchain = false;
2274
9ee67421
ACM
2275 if (session->tevent.pevent &&
2276 pevent_set_function_resolver(session->tevent.pevent,
ccb3a829
ACM
2277 machine__resolve_kernel_addr,
2278 &session->machines.host) < 0) {
2279 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2280 return -1;
2281 }
2282
956ffd02
TZ
2283 if (generate_script_lang) {
2284 struct stat perf_stat;
745f43e3
DA
2285 int input;
2286
2c9e45f7 2287 if (output_set_by_user()) {
745f43e3
DA
2288 fprintf(stderr,
2289 "custom fields not supported for generated scripts");
6cc870f0
NK
2290 err = -EINVAL;
2291 goto out_delete;
745f43e3 2292 }
956ffd02 2293
cc9784bd 2294 input = open(file.path, O_RDONLY); /* input_name */
956ffd02 2295 if (input < 0) {
6cc870f0 2296 err = -errno;
956ffd02 2297 perror("failed to open file");
6cc870f0 2298 goto out_delete;
956ffd02
TZ
2299 }
2300
2301 err = fstat(input, &perf_stat);
2302 if (err < 0) {
2303 perror("failed to stat file");
6cc870f0 2304 goto out_delete;
956ffd02
TZ
2305 }
2306
2307 if (!perf_stat.st_size) {
2308 fprintf(stderr, "zero-sized file, nothing to do!\n");
6cc870f0 2309 goto out_delete;
956ffd02
TZ
2310 }
2311
2312 scripting_ops = script_spec__lookup(generate_script_lang);
2313 if (!scripting_ops) {
2314 fprintf(stderr, "invalid language specifier");
6cc870f0
NK
2315 err = -ENOENT;
2316 goto out_delete;
956ffd02
TZ
2317 }
2318
29f5ffd3 2319 err = scripting_ops->generate_script(session->tevent.pevent,
da378962 2320 "perf-script");
6cc870f0 2321 goto out_delete;
956ffd02
TZ
2322 }
2323
2324 if (script_name) {
586bc5cc 2325 err = scripting_ops->start_script(script_name, argc, argv);
956ffd02 2326 if (err)
6cc870f0 2327 goto out_delete;
133dc4c3 2328 pr_debug("perf script started with script %s\n\n", script_name);
6cc870f0 2329 script_started = true;
956ffd02
TZ
2330 }
2331
9cbdb702
DA
2332
2333 err = perf_session__check_output_opt(session);
2334 if (err < 0)
6cc870f0 2335 goto out_delete;
9cbdb702 2336
6f3e5eda 2337 err = __cmd_script(&script);
956ffd02 2338
d445dd2a
AH
2339 flush_scripting();
2340
6cc870f0 2341out_delete:
cfc8874a 2342 perf_evlist__free_stats(session->evlist);
d8f66248 2343 perf_session__delete(session);
6cc870f0
NK
2344
2345 if (script_started)
2346 cleanup_scripting();
956ffd02
TZ
2347out:
2348 return err;
5f9c39dc 2349}
This page took 0.37601 seconds and 5 git commands to generate.