perf top: Add --all-cgroups option
[linux-block.git] / tools / perf / builtin-script.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5f9c39dc
FW
2#include "builtin.h"
3
0f31c019 4#include "util/counts.h"
b7eead86 5#include "util/debug.h"
4a3cec84 6#include "util/dso.h"
4b6ab94e 7#include <subcmd/exec-cmd.h>
b7eead86 8#include "util/header.h"
4b6ab94e 9#include <subcmd/parse-options.h>
fc36f948 10#include "util/perf_regs.h"
b7eead86 11#include "util/session.h"
45694aa7 12#include "util/tool.h"
1101f69a 13#include "util/map.h"
97b9d866 14#include "util/srcline.h"
5f9c39dc
FW
15#include "util/symbol.h"
16#include "util/thread.h"
cf72344d 17#include "util/trace-event.h"
1424dc96
DA
18#include "util/evlist.h"
19#include "util/evsel.h"
ca125277 20#include "util/evsel_fprintf.h"
d2360442 21#include "util/evswitch.h"
36385be5 22#include "util/sort.h"
f5fc1412 23#include "util/data.h"
7a680eb9 24#include "util/auxtrace.h"
cfc8874a
JO
25#include "util/cpumap.h"
26#include "util/thread_map.h"
27#include "util/stat.h"
4bd1bef8 28#include "util/color.h"
a067558e 29#include "util/string2.h"
e216708d 30#include "util/thread-stack.h"
a91f4c47 31#include "util/time-utils.h"
06c3f2aa 32#include "util/path.h"
fa0d9846 33#include "ui/ui.h"
fea01392 34#include "print_binary.h"
3ab481a1 35#include "archinsn.h"
5d67be97 36#include <linux/bitmap.h>
877a7a11 37#include <linux/kernel.h>
6125cc8d 38#include <linux/stringify.h>
bd48c63e 39#include <linux/time64.h>
7f7c536f 40#include <linux/zalloc.h>
3ab481a1 41#include <sys/utsname.h>
cfc8874a 42#include "asm/bug.h"
c19ac912 43#include "util/mem-events.h"
48d02a1d 44#include "util/dump-insn.h"
76b31a29 45#include <dirent.h>
a43783ae 46#include <errno.h>
fd20e811 47#include <inttypes.h>
9607ad3a 48#include <signal.h>
391e4206 49#include <sys/param.h>
7a8ef4c4
ACM
50#include <sys/types.h>
51#include <sys/stat.h>
bafae98e 52#include <fcntl.h>
7a8ef4c4 53#include <unistd.h>
b585ebdb 54#include <subcmd/pager.h>
453fa030 55#include <perf/evlist.h>
6ef81c55 56#include <linux/err.h>
aeb00b1a 57#include "util/record.h"
2da39f1c 58#include "util/util.h"
c1a604df 59#include "perf.h"
5f9c39dc 60
3052ba56 61#include <linux/ctype.h>
3d689ed6 62
956ffd02
TZ
63static char const *script_name;
64static char const *generate_script_lang;
90b10f47 65static bool reltime;
26567ed7 66static bool deltatime;
90b10f47 67static u64 initial_time;
26567ed7 68static u64 previous_time;
ffabd99e 69static bool debug_mode;
e1889d75 70static u64 last_timestamp;
6fcf7ddb 71static u64 nr_unordered;
c0230b2b 72static bool no_callchain;
47390ae2 73static bool latency_format;
317df650 74static bool system_wide;
400ea6d3 75static bool print_flags;
5d67be97
AB
76static const char *cpu_list;
77static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
91a2c3d5 78static struct perf_stat_config stat_config;
48d02a1d 79static int max_blocks;
3ab481a1 80static bool native_arch;
956ffd02 81
44cbe729 82unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
03cd1fed 83
745f43e3
DA
84enum perf_output_field {
85 PERF_OUTPUT_COMM = 1U << 0,
86 PERF_OUTPUT_TID = 1U << 1,
87 PERF_OUTPUT_PID = 1U << 2,
88 PERF_OUTPUT_TIME = 1U << 3,
89 PERF_OUTPUT_CPU = 1U << 4,
90 PERF_OUTPUT_EVNAME = 1U << 5,
91 PERF_OUTPUT_TRACE = 1U << 6,
787bef17
DA
92 PERF_OUTPUT_IP = 1U << 7,
93 PERF_OUTPUT_SYM = 1U << 8,
610723f2 94 PERF_OUTPUT_DSO = 1U << 9,
7cec0922 95 PERF_OUTPUT_ADDR = 1U << 10,
a978f2ab 96 PERF_OUTPUT_SYMOFFSET = 1U << 11,
cc8fae1d 97 PERF_OUTPUT_SRCLINE = 1U << 12,
535aeaae 98 PERF_OUTPUT_PERIOD = 1U << 13,
fc36f948 99 PERF_OUTPUT_IREGS = 1U << 14,
dc323ce8
SE
100 PERF_OUTPUT_BRSTACK = 1U << 15,
101 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
94ddddfa
JO
102 PERF_OUTPUT_DATA_SRC = 1U << 17,
103 PERF_OUTPUT_WEIGHT = 1U << 18,
30372f04 104 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
e216708d 105 PERF_OUTPUT_CALLINDENT = 1U << 20,
224e2c97
AK
106 PERF_OUTPUT_INSN = 1U << 21,
107 PERF_OUTPUT_INSNLEN = 1U << 22,
48d02a1d 108 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
106dacd8 109 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
47e78084 110 PERF_OUTPUT_SYNTH = 1U << 25,
49d58f04 111 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
b1491ace 112 PERF_OUTPUT_UREGS = 1U << 27,
4bd1bef8 113 PERF_OUTPUT_METRIC = 1U << 28,
28a0b398 114 PERF_OUTPUT_MISC = 1U << 29,
dd2e18e9 115 PERF_OUTPUT_SRCCODE = 1U << 30,
68fb45bf 116 PERF_OUTPUT_IPC = 1U << 31,
745f43e3
DA
117};
118
119struct output_option {
120 const char *str;
121 enum perf_output_field field;
122} all_output_options[] = {
123 {.str = "comm", .field = PERF_OUTPUT_COMM},
124 {.str = "tid", .field = PERF_OUTPUT_TID},
125 {.str = "pid", .field = PERF_OUTPUT_PID},
126 {.str = "time", .field = PERF_OUTPUT_TIME},
127 {.str = "cpu", .field = PERF_OUTPUT_CPU},
128 {.str = "event", .field = PERF_OUTPUT_EVNAME},
129 {.str = "trace", .field = PERF_OUTPUT_TRACE},
787bef17 130 {.str = "ip", .field = PERF_OUTPUT_IP},
c0230b2b 131 {.str = "sym", .field = PERF_OUTPUT_SYM},
610723f2 132 {.str = "dso", .field = PERF_OUTPUT_DSO},
7cec0922 133 {.str = "addr", .field = PERF_OUTPUT_ADDR},
a978f2ab 134 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
cc8fae1d 135 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
535aeaae 136 {.str = "period", .field = PERF_OUTPUT_PERIOD},
fc36f948 137 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
b1491ace 138 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
dc323ce8
SE
139 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
140 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
94ddddfa
JO
141 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
142 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
30372f04 143 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
e216708d 144 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
224e2c97
AK
145 {.str = "insn", .field = PERF_OUTPUT_INSN},
146 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
48d02a1d 147 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
106dacd8 148 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
47e78084 149 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
49d58f04 150 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
4bd1bef8 151 {.str = "metric", .field = PERF_OUTPUT_METRIC},
28a0b398 152 {.str = "misc", .field = PERF_OUTPUT_MISC},
dd2e18e9 153 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
68fb45bf 154 {.str = "ipc", .field = PERF_OUTPUT_IPC},
745f43e3
DA
155};
156
1405720d
AH
157enum {
158 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
159 OUTPUT_TYPE_MAX
160};
161
745f43e3 162/* default set to maintain compatibility with current format */
2c9e45f7
DA
163static struct {
164 bool user_set;
9cbdb702 165 bool wildcard_set;
a6ffaf91 166 unsigned int print_ip_opts;
2c9e45f7
DA
167 u64 fields;
168 u64 invalid_fields;
4b6ac811 169 u64 user_set_fields;
1405720d 170} output[OUTPUT_TYPE_MAX] = {
2c9e45f7
DA
171
172 [PERF_TYPE_HARDWARE] = {
173 .user_set = false,
174
175 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
176 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 177 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
7903a708
SD
178 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
179 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
2c9e45f7 180
30372f04 181 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
2c9e45f7
DA
182 },
183
184 [PERF_TYPE_SOFTWARE] = {
185 .user_set = false,
186
187 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
188 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 189 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
7903a708
SD
190 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
191 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
192 PERF_OUTPUT_BPF_OUTPUT,
2c9e45f7
DA
193
194 .invalid_fields = PERF_OUTPUT_TRACE,
195 },
196
197 [PERF_TYPE_TRACEPOINT] = {
198 .user_set = false,
199
200 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
201 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
30372f04 202 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
2c9e45f7 203 },
0817a6a3 204
fad76d43
ST
205 [PERF_TYPE_HW_CACHE] = {
206 .user_set = false,
207
208 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
209 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
210 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
211 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
212 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
213
214 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
215 },
216
0817a6a3
AS
217 [PERF_TYPE_RAW] = {
218 .user_set = false,
219
220 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
221 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
787bef17 222 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
7903a708
SD
223 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
224 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
225 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
226 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
0817a6a3 227
30372f04 228 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
0817a6a3 229 },
27cfef00
WN
230
231 [PERF_TYPE_BREAKPOINT] = {
232 .user_set = false,
233
234 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
235 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
236 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
7903a708
SD
237 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
238 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
27cfef00 239
30372f04 240 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
27cfef00 241 },
1405720d
AH
242
243 [OUTPUT_TYPE_SYNTH] = {
244 .user_set = false,
245
246 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
247 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
248 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
7903a708
SD
249 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
250 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
1405720d
AH
251
252 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
253 },
1424dc96 254};
745f43e3 255
32dcd021 256struct evsel_script {
642ee1c6
ACM
257 char *filename;
258 FILE *fp;
259 u64 samples;
4bd1bef8
AK
260 /* For metric output */
261 u64 val;
262 int gnum;
642ee1c6
ACM
263};
264
32dcd021 265static inline struct evsel_script *evsel_script(struct evsel *evsel)
4bd1bef8 266{
32dcd021 267 return (struct evsel_script *)evsel->priv;
4bd1bef8
AK
268}
269
32dcd021 270static struct evsel_script *perf_evsel_script__new(struct evsel *evsel,
8ceb41d7 271 struct perf_data *data)
642ee1c6 272{
32dcd021 273 struct evsel_script *es = zalloc(sizeof(*es));
642ee1c6
ACM
274
275 if (es != NULL) {
eae8ad80 276 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
642ee1c6
ACM
277 goto out_free;
278 es->fp = fopen(es->filename, "w");
279 if (es->fp == NULL)
280 goto out_free_filename;
642ee1c6
ACM
281 }
282
283 return es;
284out_free_filename:
285 zfree(&es->filename);
286out_free:
287 free(es);
288 return NULL;
289}
290
32dcd021 291static void perf_evsel_script__delete(struct evsel_script *es)
642ee1c6
ACM
292{
293 zfree(&es->filename);
294 fclose(es->fp);
295 es->fp = NULL;
296 free(es);
297}
298
32dcd021 299static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp)
642ee1c6
ACM
300{
301 struct stat st;
302
303 fstat(fileno(es->fp), &st);
304 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
305 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
306}
307
1405720d
AH
308static inline int output_type(unsigned int type)
309{
310 switch (type) {
311 case PERF_TYPE_SYNTH:
312 return OUTPUT_TYPE_SYNTH;
313 default:
314 return type;
315 }
316}
317
318static inline unsigned int attr_type(unsigned int type)
319{
320 switch (type) {
321 case OUTPUT_TYPE_SYNTH:
322 return PERF_TYPE_SYNTH;
323 default:
324 return type;
325 }
326}
327
2c9e45f7
DA
328static bool output_set_by_user(void)
329{
330 int j;
1405720d 331 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2c9e45f7
DA
332 if (output[j].user_set)
333 return true;
334 }
335 return false;
336}
745f43e3 337
9cbdb702
DA
338static const char *output_field2str(enum perf_output_field field)
339{
340 int i, imax = ARRAY_SIZE(all_output_options);
341 const char *str = "";
342
343 for (i = 0; i < imax; ++i) {
344 if (all_output_options[i].field == field) {
345 str = all_output_options[i].str;
346 break;
347 }
348 }
349 return str;
350}
351
1405720d 352#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
1424dc96 353
32dcd021 354static int perf_evsel__do_check_stype(struct evsel *evsel,
6d5cdd64
AH
355 u64 sample_type, const char *sample_msg,
356 enum perf_output_field field,
357 bool allow_user_set)
1424dc96 358{
1fc632ce 359 struct perf_event_attr *attr = &evsel->core.attr;
1405720d 360 int type = output_type(attr->type);
9cbdb702
DA
361 const char *evname;
362
363 if (attr->sample_type & sample_type)
364 return 0;
365
4b6ac811 366 if (output[type].user_set_fields & field) {
6d5cdd64
AH
367 if (allow_user_set)
368 return 0;
5bff01f6 369 evname = perf_evsel__name(evsel);
9cbdb702
DA
370 pr_err("Samples for '%s' event do not have %s attribute set. "
371 "Cannot print '%s' field.\n",
372 evname, sample_msg, output_field2str(field));
373 return -1;
374 }
375
376 /* user did not ask for it explicitly so remove from the default list */
377 output[type].fields &= ~field;
5bff01f6 378 evname = perf_evsel__name(evsel);
9cbdb702
DA
379 pr_debug("Samples for '%s' event do not have %s attribute set. "
380 "Skipping '%s' field.\n",
381 evname, sample_msg, output_field2str(field));
382
383 return 0;
384}
385
32dcd021 386static int perf_evsel__check_stype(struct evsel *evsel,
6d5cdd64
AH
387 u64 sample_type, const char *sample_msg,
388 enum perf_output_field field)
389{
390 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
391 false);
392}
393
32dcd021 394static int perf_evsel__check_attr(struct evsel *evsel,
9cbdb702
DA
395 struct perf_session *session)
396{
1fc632ce 397 struct perf_event_attr *attr = &evsel->core.attr;
6d5cdd64
AH
398 bool allow_user_set;
399
e099eba8
JO
400 if (perf_header__has_feat(&session->header, HEADER_STAT))
401 return 0;
402
6d5cdd64
AH
403 allow_user_set = perf_header__has_feat(&session->header,
404 HEADER_AUXTRACE);
9cbdb702 405
1424dc96
DA
406 if (PRINT_FIELD(TRACE) &&
407 !perf_session__has_traces(session, "record -R"))
408 return -EINVAL;
409
787bef17 410 if (PRINT_FIELD(IP)) {
5bff01f6
ACM
411 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
412 PERF_OUTPUT_IP))
1424dc96 413 return -EINVAL;
1424dc96 414 }
7cec0922
DA
415
416 if (PRINT_FIELD(ADDR) &&
6d5cdd64
AH
417 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
418 PERF_OUTPUT_ADDR, allow_user_set))
7cec0922
DA
419 return -EINVAL;
420
94ddddfa
JO
421 if (PRINT_FIELD(DATA_SRC) &&
422 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
423 PERF_OUTPUT_DATA_SRC))
424 return -EINVAL;
425
426 if (PRINT_FIELD(WEIGHT) &&
427 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
428 PERF_OUTPUT_WEIGHT))
429 return -EINVAL;
430
37fed3de 431 if (PRINT_FIELD(SYM) &&
1fc632ce 432 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
7cec0922 433 pr_err("Display of symbols requested but neither sample IP nor "
37fed3de 434 "sample address\navailable. Hence, no addresses to convert "
7cec0922 435 "to symbols.\n");
787bef17
DA
436 return -EINVAL;
437 }
a978f2ab
AN
438 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
439 pr_err("Display of offsets requested but symbol is not"
440 "selected.\n");
441 return -EINVAL;
442 }
37fed3de 443 if (PRINT_FIELD(DSO) &&
1fc632ce 444 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
37fed3de 445 pr_err("Display of DSO requested but no address to convert.\n");
610723f2
DA
446 return -EINVAL;
447 }
dd2e18e9 448 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
cc8fae1d
AH
449 pr_err("Display of source line number requested but sample IP is not\n"
450 "selected. Hence, no address to lookup the source line number.\n");
451 return -EINVAL;
452 }
0cd032d3 453 if (PRINT_FIELD(BRSTACKINSN) && !allow_user_set &&
48d02a1d
AK
454 !(perf_evlist__combined_branch_type(session->evlist) &
455 PERF_SAMPLE_BRANCH_ANY)) {
456 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
457 "Hint: run 'perf record -b ...'\n");
458 return -EINVAL;
459 }
1424dc96 460 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
5bff01f6
ACM
461 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
462 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
1424dc96 463 return -EINVAL;
1424dc96
DA
464
465 if (PRINT_FIELD(TIME) &&
5bff01f6
ACM
466 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
467 PERF_OUTPUT_TIME))
1424dc96 468 return -EINVAL;
1424dc96
DA
469
470 if (PRINT_FIELD(CPU) &&
6d5cdd64
AH
471 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
472 PERF_OUTPUT_CPU, allow_user_set))
1424dc96 473 return -EINVAL;
9cbdb702 474
fc36f948
SE
475 if (PRINT_FIELD(IREGS) &&
476 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
477 PERF_OUTPUT_IREGS))
478 return -EINVAL;
479
b1491ace
AK
480 if (PRINT_FIELD(UREGS) &&
481 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
482 PERF_OUTPUT_UREGS))
483 return -EINVAL;
484
49d58f04
KL
485 if (PRINT_FIELD(PHYS_ADDR) &&
486 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
487 PERF_OUTPUT_PHYS_ADDR))
488 return -EINVAL;
489
9cbdb702
DA
490 return 0;
491}
492
7ea95727
AH
493static void set_print_ip_opts(struct perf_event_attr *attr)
494{
1405720d 495 unsigned int type = output_type(attr->type);
7ea95727
AH
496
497 output[type].print_ip_opts = 0;
498 if (PRINT_FIELD(IP))
e20ab86e 499 output[type].print_ip_opts |= EVSEL__PRINT_IP;
7ea95727
AH
500
501 if (PRINT_FIELD(SYM))
e20ab86e 502 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
7ea95727
AH
503
504 if (PRINT_FIELD(DSO))
e20ab86e 505 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
7ea95727
AH
506
507 if (PRINT_FIELD(SYMOFFSET))
e20ab86e 508 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
cc8fae1d
AH
509
510 if (PRINT_FIELD(SRCLINE))
e20ab86e 511 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
7ea95727
AH
512}
513
9cbdb702
DA
514/*
515 * verify all user requested events exist and the samples
516 * have the expected data
517 */
518static int perf_session__check_output_opt(struct perf_session *session)
519{
40f20e50 520 unsigned int j;
32dcd021 521 struct evsel *evsel;
9cbdb702 522
1405720d
AH
523 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
524 evsel = perf_session__find_first_evtype(session, attr_type(j));
9cbdb702
DA
525
526 /*
527 * even if fields is set to 0 (ie., show nothing) event must
528 * exist if user explicitly includes it on the command line
529 */
1405720d
AH
530 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
531 j != OUTPUT_TYPE_SYNTH) {
9cbdb702 532 pr_err("%s events do not exist. "
701516ae 533 "Remove corresponding -F option to proceed.\n",
9cbdb702
DA
534 event_type(j));
535 return -1;
536 }
537
538 if (evsel && output[j].fields &&
539 perf_evsel__check_attr(evsel, session))
540 return -1;
a6ffaf91
DA
541
542 if (evsel == NULL)
543 continue;
544
1fc632ce 545 set_print_ip_opts(&evsel->core.attr);
1424dc96
DA
546 }
547
98526ee7
AH
548 if (!no_callchain) {
549 bool use_callchain = false;
71ac899b 550 bool not_pipe = false;
98526ee7 551
e5cadb93 552 evlist__for_each_entry(session->evlist, evsel) {
71ac899b 553 not_pipe = true;
27de9b2b 554 if (evsel__has_callchain(evsel)) {
98526ee7
AH
555 use_callchain = true;
556 break;
557 }
558 }
71ac899b 559 if (not_pipe && !use_callchain)
98526ee7
AH
560 symbol_conf.use_callchain = false;
561 }
562
80b8b496
DA
563 /*
564 * set default for tracepoints to print symbols only
565 * if callchains are present
566 */
567 if (symbol_conf.use_callchain &&
568 !output[PERF_TYPE_TRACEPOINT].user_set) {
80b8b496 569 j = PERF_TYPE_TRACEPOINT;
80b8b496 570
e5cadb93 571 evlist__for_each_entry(session->evlist, evsel) {
1fc632ce 572 if (evsel->core.attr.type != j)
40f20e50
HK
573 continue;
574
27de9b2b 575 if (evsel__has_callchain(evsel)) {
40f20e50
HK
576 output[j].fields |= PERF_OUTPUT_IP;
577 output[j].fields |= PERF_OUTPUT_SYM;
7903a708 578 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
40f20e50 579 output[j].fields |= PERF_OUTPUT_DSO;
1fc632ce 580 set_print_ip_opts(&evsel->core.attr);
40f20e50
HK
581 goto out;
582 }
80b8b496
DA
583 }
584 }
585
586out:
1424dc96
DA
587 return 0;
588}
745f43e3 589
9add8fe8
MW
590static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
591 FILE *fp
592)
b1491ace 593{
b1491ace 594 unsigned i = 0, r;
a1a58707 595 int printed = 0;
b1491ace
AK
596
597 if (!regs || !regs->regs)
a1a58707 598 return 0;
b1491ace 599
a1a58707 600 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
b1491ace
AK
601
602 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
603 u64 val = regs->regs[i++];
a1a58707 604 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
b1491ace 605 }
a1a58707 606
b07d16f7
MW
607 fprintf(fp, "\n");
608
a1a58707 609 return printed;
b1491ace
AK
610}
611
9add8fe8
MW
612static int perf_sample__fprintf_iregs(struct perf_sample *sample,
613 struct perf_event_attr *attr, FILE *fp)
614{
615 return perf_sample__fprintf_regs(&sample->intr_regs,
616 attr->sample_regs_intr, fp);
617}
618
619static int perf_sample__fprintf_uregs(struct perf_sample *sample,
620 struct perf_event_attr *attr, FILE *fp)
621{
622 return perf_sample__fprintf_regs(&sample->user_regs,
623 attr->sample_regs_user, fp);
624}
625
a1a58707
ACM
626static int perf_sample__fprintf_start(struct perf_sample *sample,
627 struct thread *thread,
32dcd021 628 struct evsel *evsel,
28a0b398 629 u32 type, FILE *fp)
c70c94b4 630{
1fc632ce 631 struct perf_event_attr *attr = &evsel->core.attr;
c70c94b4 632 unsigned long secs;
745f43e3 633 unsigned long long nsecs;
a1a58707 634 int printed = 0;
745f43e3
DA
635
636 if (PRINT_FIELD(COMM)) {
637 if (latency_format)
a1a58707 638 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
b879833c 639 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
a1a58707 640 printed += fprintf(fp, "%s ", thread__comm_str(thread));
745f43e3 641 else
a1a58707 642 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
745f43e3 643 }
c70c94b4 644
745f43e3 645 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
a1a58707 646 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
745f43e3 647 else if (PRINT_FIELD(PID))
a1a58707 648 printed += fprintf(fp, "%5d ", sample->pid);
745f43e3 649 else if (PRINT_FIELD(TID))
a1a58707 650 printed += fprintf(fp, "%5d ", sample->tid);
745f43e3
DA
651
652 if (PRINT_FIELD(CPU)) {
653 if (latency_format)
a1a58707 654 printed += fprintf(fp, "%3d ", sample->cpu);
745f43e3 655 else
a1a58707 656 printed += fprintf(fp, "[%03d] ", sample->cpu);
745f43e3 657 }
c70c94b4 658
28a0b398
JO
659 if (PRINT_FIELD(MISC)) {
660 int ret = 0;
661
662 #define has(m) \
663 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
664
665 if (has(KERNEL))
666 ret += fprintf(fp, "K");
667 if (has(USER))
668 ret += fprintf(fp, "U");
669 if (has(HYPERVISOR))
670 ret += fprintf(fp, "H");
671 if (has(GUEST_KERNEL))
672 ret += fprintf(fp, "G");
673 if (has(GUEST_USER))
674 ret += fprintf(fp, "g");
675
676 switch (type) {
677 case PERF_RECORD_MMAP:
678 case PERF_RECORD_MMAP2:
679 if (has(MMAP_DATA))
680 ret += fprintf(fp, "M");
681 break;
682 case PERF_RECORD_COMM:
683 if (has(COMM_EXEC))
684 ret += fprintf(fp, "E");
685 break;
686 case PERF_RECORD_SWITCH:
687 case PERF_RECORD_SWITCH_CPU_WIDE:
bf30cc18 688 if (has(SWITCH_OUT)) {
28a0b398 689 ret += fprintf(fp, "S");
bf30cc18
AB
690 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
691 ret += fprintf(fp, "p");
692 }
28a0b398
JO
693 default:
694 break;
695 }
696
697 #undef has
698
699 ret += fprintf(fp, "%*s", 6 - ret, " ");
700 printed += ret;
701 }
702
745f43e3 703 if (PRINT_FIELD(TIME)) {
90b10f47
AK
704 u64 t = sample->time;
705 if (reltime) {
706 if (!initial_time)
707 initial_time = sample->time;
708 t = sample->time - initial_time;
26567ed7
HPP
709 } else if (deltatime) {
710 if (previous_time)
711 t = sample->time - previous_time;
712 else {
713 t = 0;
714 }
715 previous_time = sample->time;
90b10f47
AK
716 }
717 nsecs = t;
bd48c63e
ACM
718 secs = nsecs / NSEC_PER_SEC;
719 nsecs -= secs * NSEC_PER_SEC;
99620a5d 720
52bab886 721 if (symbol_conf.nanosecs)
a1a58707 722 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
99620a5d
NK
723 else {
724 char sample_time[32];
90b10f47 725 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
a1a58707 726 printed += fprintf(fp, "%12s: ", sample_time);
99620a5d 727 }
745f43e3 728 }
a1a58707
ACM
729
730 return printed;
c70c94b4
DA
731}
732
dc323ce8
SE
733static inline char
734mispred_str(struct branch_entry *br)
735{
736 if (!(br->flags.mispred || br->flags.predicted))
737 return '-';
738
739 return br->flags.predicted ? 'P' : 'M';
740}
741
a1a58707
ACM
742static int perf_sample__fprintf_brstack(struct perf_sample *sample,
743 struct thread *thread,
744 struct perf_event_attr *attr, FILE *fp)
dc323ce8
SE
745{
746 struct branch_stack *br = sample->branch_stack;
42bbabed 747 struct branch_entry *entries = perf_sample__branch_entries(sample);
55b9b508
MS
748 struct addr_location alf, alt;
749 u64 i, from, to;
a1a58707 750 int printed = 0;
dc323ce8
SE
751
752 if (!(br && br->nr))
a1a58707 753 return 0;
dc323ce8
SE
754
755 for (i = 0; i < br->nr; i++) {
42bbabed
KL
756 from = entries[i].from;
757 to = entries[i].to;
55b9b508
MS
758
759 if (PRINT_FIELD(DSO)) {
760 memset(&alf, 0, sizeof(alf));
761 memset(&alt, 0, sizeof(alt));
692d0e63
AH
762 thread__find_map_fb(thread, sample->cpumode, from, &alf);
763 thread__find_map_fb(thread, sample->cpumode, to, &alt);
55b9b508
MS
764 }
765
a1a58707 766 printed += fprintf(fp, " 0x%"PRIx64, from);
55b9b508 767 if (PRINT_FIELD(DSO)) {
a1a58707
ACM
768 printed += fprintf(fp, "(");
769 printed += map__fprintf_dsoname(alf.map, fp);
770 printed += fprintf(fp, ")");
55b9b508
MS
771 }
772
a1a58707 773 printed += fprintf(fp, "/0x%"PRIx64, to);
55b9b508 774 if (PRINT_FIELD(DSO)) {
a1a58707
ACM
775 printed += fprintf(fp, "(");
776 printed += map__fprintf_dsoname(alt.map, fp);
777 printed += fprintf(fp, ")");
55b9b508
MS
778 }
779
a1a58707 780 printed += fprintf(fp, "/%c/%c/%c/%d ",
42bbabed
KL
781 mispred_str(entries + i),
782 entries[i].flags.in_tx ? 'X' : '-',
783 entries[i].flags.abort ? 'A' : '-',
784 entries[i].flags.cycles);
dc323ce8 785 }
a1a58707
ACM
786
787 return printed;
dc323ce8
SE
788}
789
a1a58707
ACM
790static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
791 struct thread *thread,
792 struct perf_event_attr *attr, FILE *fp)
dc323ce8
SE
793{
794 struct branch_stack *br = sample->branch_stack;
42bbabed 795 struct branch_entry *entries = perf_sample__branch_entries(sample);
dc323ce8 796 struct addr_location alf, alt;
dc323ce8 797 u64 i, from, to;
a1a58707 798 int printed = 0;
dc323ce8
SE
799
800 if (!(br && br->nr))
a1a58707 801 return 0;
dc323ce8
SE
802
803 for (i = 0; i < br->nr; i++) {
804
805 memset(&alf, 0, sizeof(alf));
806 memset(&alt, 0, sizeof(alt));
42bbabed
KL
807 from = entries[i].from;
808 to = entries[i].to;
dc323ce8 809
692d0e63
AH
810 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
811 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
dc323ce8 812
a1a58707 813 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
55b9b508 814 if (PRINT_FIELD(DSO)) {
a1a58707
ACM
815 printed += fprintf(fp, "(");
816 printed += map__fprintf_dsoname(alf.map, fp);
817 printed += fprintf(fp, ")");
55b9b508 818 }
a1a58707
ACM
819 printed += fprintf(fp, "%c", '/');
820 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
55b9b508 821 if (PRINT_FIELD(DSO)) {
a1a58707
ACM
822 printed += fprintf(fp, "(");
823 printed += map__fprintf_dsoname(alt.map, fp);
824 printed += fprintf(fp, ")");
55b9b508 825 }
a1a58707 826 printed += fprintf(fp, "/%c/%c/%c/%d ",
42bbabed
KL
827 mispred_str(entries + i),
828 entries[i].flags.in_tx ? 'X' : '-',
829 entries[i].flags.abort ? 'A' : '-',
830 entries[i].flags.cycles);
dc323ce8 831 }
a1a58707
ACM
832
833 return printed;
dc323ce8
SE
834}
835
a1a58707
ACM
836static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
837 struct thread *thread,
838 struct perf_event_attr *attr, FILE *fp)
106dacd8
MS
839{
840 struct branch_stack *br = sample->branch_stack;
42bbabed 841 struct branch_entry *entries = perf_sample__branch_entries(sample);
106dacd8
MS
842 struct addr_location alf, alt;
843 u64 i, from, to;
a1a58707 844 int printed = 0;
106dacd8
MS
845
846 if (!(br && br->nr))
a1a58707 847 return 0;
106dacd8
MS
848
849 for (i = 0; i < br->nr; i++) {
850
851 memset(&alf, 0, sizeof(alf));
852 memset(&alt, 0, sizeof(alt));
42bbabed
KL
853 from = entries[i].from;
854 to = entries[i].to;
106dacd8 855
692d0e63 856 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
71a84b5a 857 !alf.map->dso->adjust_symbols)
106dacd8
MS
858 from = map__map_ip(alf.map, from);
859
692d0e63 860 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
71a84b5a 861 !alt.map->dso->adjust_symbols)
106dacd8
MS
862 to = map__map_ip(alt.map, to);
863
a1a58707 864 printed += fprintf(fp, " 0x%"PRIx64, from);
106dacd8 865 if (PRINT_FIELD(DSO)) {
a1a58707
ACM
866 printed += fprintf(fp, "(");
867 printed += map__fprintf_dsoname(alf.map, fp);
868 printed += fprintf(fp, ")");
106dacd8 869 }
a1a58707 870 printed += fprintf(fp, "/0x%"PRIx64, to);
106dacd8 871 if (PRINT_FIELD(DSO)) {
a1a58707
ACM
872 printed += fprintf(fp, "(");
873 printed += map__fprintf_dsoname(alt.map, fp);
874 printed += fprintf(fp, ")");
106dacd8 875 }
a1a58707 876 printed += fprintf(fp, "/%c/%c/%c/%d ",
42bbabed
KL
877 mispred_str(entries + i),
878 entries[i].flags.in_tx ? 'X' : '-',
879 entries[i].flags.abort ? 'A' : '-',
880 entries[i].flags.cycles);
106dacd8 881 }
a1a58707
ACM
882
883 return printed;
106dacd8 884}
48d02a1d
AK
885#define MAXBB 16384UL
886
887static int grab_bb(u8 *buffer, u64 start, u64 end,
888 struct machine *machine, struct thread *thread,
889 bool *is64bit, u8 *cpumode, bool last)
890{
891 long offset, len;
892 struct addr_location al;
893 bool kernel;
894
895 if (!start || !end)
896 return 0;
897
898 kernel = machine__kernel_ip(machine, start);
899 if (kernel)
900 *cpumode = PERF_RECORD_MISC_KERNEL;
901 else
902 *cpumode = PERF_RECORD_MISC_USER;
903
904 /*
905 * Block overlaps between kernel and user.
906 * This can happen due to ring filtering
907 * On Intel CPUs the entry into the kernel is filtered,
908 * but the exit is not. Let the caller patch it up.
909 */
910 if (kernel != machine__kernel_ip(machine, end)) {
5ce2c5b4 911 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
48d02a1d
AK
912 return -ENXIO;
913 }
914
915 memset(&al, 0, sizeof(al));
916 if (end - start > MAXBB - MAXINSN) {
917 if (last)
5ce2c5b4 918 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
48d02a1d 919 else
5ce2c5b4 920 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
48d02a1d
AK
921 return 0;
922 }
923
71a84b5a 924 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
5ce2c5b4 925 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
48d02a1d
AK
926 return 0;
927 }
928 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
5ce2c5b4 929 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
48d02a1d
AK
930 return 0;
931 }
932
933 /* Load maps to ensure dso->is_64_bit has been updated */
934 map__load(al.map);
935
936 offset = al.map->map_ip(al.map, start);
937 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
938 end - start + MAXINSN);
939
940 *is64bit = al.map->dso->is_64_bit;
941 if (len <= 0)
5ce2c5b4 942 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
48d02a1d
AK
943 start, end);
944 return len;
945}
946
540a63ea
ACM
947static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srccode_state *state)
948{
949 char *srcfile;
950 int ret = 0;
951 unsigned line;
952 int len;
953 char *srccode;
954
955 if (!map || !map->dso)
956 return 0;
957 srcfile = get_srcline_split(map->dso,
958 map__rip_2objdump(map, addr),
959 &line);
960 if (!srcfile)
961 return 0;
962
963 /* Avoid redundant printing */
964 if (state &&
965 state->srcfile &&
966 !strcmp(state->srcfile, srcfile) &&
967 state->line == line) {
968 free(srcfile);
969 return 0;
970 }
971
972 srccode = find_sourceline(srcfile, line, &len);
973 if (!srccode)
974 goto out_free_line;
975
976 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
977
978 if (state) {
979 state->srcfile = srcfile;
980 state->line = line;
981 }
982 return ret;
983
984out_free_line:
985 free(srcfile);
986 return ret;
987}
988
dd2e18e9
AK
989static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
990{
991 struct addr_location al;
992 int ret = 0;
993
994 memset(&al, 0, sizeof(al));
995 thread__find_map(thread, cpumode, addr, &al);
996 if (!al.map)
997 return 0;
998 ret = map__fprintf_srccode(al.map, al.addr, stdout,
999 &thread->srccode_state);
1000 if (ret)
1001 ret += printf("\n");
1002 return ret;
1003}
1004
a1a58707
ACM
1005static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
1006 struct perf_insn *x, u8 *inbuf, int len,
fe57120e 1007 int insn, FILE *fp, int *total_cycles)
48d02a1d 1008{
a1a58707
ACM
1009 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
1010 dump_insn(x, ip, inbuf, len, NULL),
1011 en->flags.predicted ? " PRED" : "",
1012 en->flags.mispred ? " MISPRED" : "",
1013 en->flags.in_tx ? " INTX" : "",
1014 en->flags.abort ? " ABORT" : "");
48d02a1d 1015 if (en->flags.cycles) {
fe57120e
AK
1016 *total_cycles += en->flags.cycles;
1017 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
48d02a1d 1018 if (insn)
a1a58707 1019 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
48d02a1d 1020 }
a1a58707 1021 return printed + fprintf(fp, "\n");
48d02a1d
AK
1022}
1023
a1a58707
ACM
1024static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
1025 u8 cpumode, int cpu, struct symbol **lastsym,
1026 struct perf_event_attr *attr, FILE *fp)
48d02a1d
AK
1027{
1028 struct addr_location al;
a1a58707 1029 int off, printed = 0;
48d02a1d
AK
1030
1031 memset(&al, 0, sizeof(al));
1032
404eb5a4
ACM
1033 thread__find_map(thread, cpumode, addr, &al);
1034
48d02a1d 1035 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
a1a58707 1036 return 0;
48d02a1d
AK
1037
1038 al.cpu = cpu;
1039 al.sym = NULL;
1040 if (al.map)
1041 al.sym = map__find_symbol(al.map, al.addr);
1042
1043 if (!al.sym)
a1a58707 1044 return 0;
48d02a1d
AK
1045
1046 if (al.addr < al.sym->end)
1047 off = al.addr - al.sym->start;
1048 else
1049 off = al.addr - al.map->start - al.sym->start;
a1a58707 1050 printed += fprintf(fp, "\t%s", al.sym->name);
48d02a1d 1051 if (off)
a1a58707
ACM
1052 printed += fprintf(fp, "%+d", off);
1053 printed += fprintf(fp, ":");
48d02a1d 1054 if (PRINT_FIELD(SRCLINE))
a1a58707
ACM
1055 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
1056 printed += fprintf(fp, "\n");
48d02a1d 1057 *lastsym = al.sym;
a1a58707
ACM
1058
1059 return printed;
48d02a1d
AK
1060}
1061
a1a58707
ACM
1062static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1063 struct thread *thread,
1064 struct perf_event_attr *attr,
1065 struct machine *machine, FILE *fp)
48d02a1d
AK
1066{
1067 struct branch_stack *br = sample->branch_stack;
42bbabed 1068 struct branch_entry *entries = perf_sample__branch_entries(sample);
48d02a1d 1069 u64 start, end;
a1a58707 1070 int i, insn, len, nr, ilen, printed = 0;
48d02a1d
AK
1071 struct perf_insn x;
1072 u8 buffer[MAXBB];
1073 unsigned off;
1074 struct symbol *lastsym = NULL;
fe57120e 1075 int total_cycles = 0;
48d02a1d
AK
1076
1077 if (!(br && br->nr))
a1a58707 1078 return 0;
48d02a1d
AK
1079 nr = br->nr;
1080 if (max_blocks && nr > max_blocks + 1)
1081 nr = max_blocks + 1;
1082
1083 x.thread = thread;
1084 x.cpu = sample->cpu;
1085
a1a58707 1086 printed += fprintf(fp, "%c", '\n');
48d02a1d
AK
1087
1088 /* Handle first from jump, of which we don't know the entry. */
42bbabed
KL
1089 len = grab_bb(buffer, entries[nr-1].from,
1090 entries[nr-1].from,
48d02a1d
AK
1091 machine, thread, &x.is64bit, &x.cpumode, false);
1092 if (len > 0) {
42bbabed 1093 printed += ip__fprintf_sym(entries[nr - 1].from, thread,
a1a58707 1094 x.cpumode, x.cpu, &lastsym, attr, fp);
42bbabed 1095 printed += ip__fprintf_jump(entries[nr - 1].from, &entries[nr - 1],
fe57120e 1096 &x, buffer, len, 0, fp, &total_cycles);
dd2e18e9 1097 if (PRINT_FIELD(SRCCODE))
42bbabed 1098 printed += print_srccode(thread, x.cpumode, entries[nr - 1].from);
48d02a1d
AK
1099 }
1100
1101 /* Print all blocks */
1102 for (i = nr - 2; i >= 0; i--) {
42bbabed 1103 if (entries[i].from || entries[i].to)
48d02a1d 1104 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
42bbabed
KL
1105 entries[i].from,
1106 entries[i].to);
1107 start = entries[i + 1].to;
1108 end = entries[i].from;
48d02a1d
AK
1109
1110 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1111 /* Patch up missing kernel transfers due to ring filters */
1112 if (len == -ENXIO && i > 0) {
42bbabed 1113 end = entries[--i].from;
48d02a1d
AK
1114 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1115 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1116 }
1117 if (len <= 0)
1118 continue;
1119
1120 insn = 0;
e98df280 1121 for (off = 0; off < (unsigned)len; off += ilen) {
48d02a1d
AK
1122 uint64_t ip = start + off;
1123
a1a58707 1124 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
48d02a1d 1125 if (ip == end) {
42bbabed 1126 printed += ip__fprintf_jump(ip, &entries[i], &x, buffer + off, len - off, ++insn, fp,
fe57120e 1127 &total_cycles);
dd2e18e9
AK
1128 if (PRINT_FIELD(SRCCODE))
1129 printed += print_srccode(thread, x.cpumode, ip);
48d02a1d
AK
1130 break;
1131 } else {
e98df280 1132 ilen = 0;
a1a58707
ACM
1133 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1134 dump_insn(&x, ip, buffer + off, len - off, &ilen));
48d02a1d
AK
1135 if (ilen == 0)
1136 break;
dd2e18e9
AK
1137 if (PRINT_FIELD(SRCCODE))
1138 print_srccode(thread, x.cpumode, ip);
48d02a1d
AK
1139 insn++;
1140 }
1141 }
5172672d 1142 if (off != end - start)
e98df280 1143 printed += fprintf(fp, "\tmismatch of LBR data and executable\n");
48d02a1d
AK
1144 }
1145
1146 /*
1147 * Hit the branch? In this case we are already done, and the target
1148 * has not been executed yet.
1149 */
42bbabed 1150 if (entries[0].from == sample->ip)
a1a58707 1151 goto out;
42bbabed 1152 if (entries[0].flags.abort)
a1a58707 1153 goto out;
48d02a1d
AK
1154
1155 /*
1156 * Print final block upto sample
61f61159
AK
1157 *
1158 * Due to pipeline delays the LBRs might be missing a branch
1159 * or two, which can result in very large or negative blocks
1160 * between final branch and sample. When this happens just
1161 * continue walking after the last TO until we hit a branch.
48d02a1d 1162 */
42bbabed 1163 start = entries[0].to;
48d02a1d 1164 end = sample->ip;
61f61159
AK
1165 if (end < start) {
1166 /* Missing jump. Scan 128 bytes for the next branch */
1167 end = start + 128;
1168 }
48d02a1d 1169 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
a1a58707 1170 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
48d02a1d
AK
1171 if (len <= 0) {
1172 /* Print at least last IP if basic block did not work */
1173 len = grab_bb(buffer, sample->ip, sample->ip,
1174 machine, thread, &x.is64bit, &x.cpumode, false);
1175 if (len <= 0)
a1a58707 1176 goto out;
a1a58707 1177 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
48d02a1d 1178 dump_insn(&x, sample->ip, buffer, len, NULL));
dd2e18e9
AK
1179 if (PRINT_FIELD(SRCCODE))
1180 print_srccode(thread, x.cpumode, sample->ip);
a1a58707 1181 goto out;
48d02a1d
AK
1182 }
1183 for (off = 0; off <= end - start; off += ilen) {
e98df280 1184 ilen = 0;
a1a58707
ACM
1185 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1186 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
48d02a1d
AK
1187 if (ilen == 0)
1188 break;
61f61159
AK
1189 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1190 /*
1191 * Hit a missing branch. Just stop.
1192 */
1193 printed += fprintf(fp, "\t... not reaching sample ...\n");
1194 break;
1195 }
dd2e18e9
AK
1196 if (PRINT_FIELD(SRCCODE))
1197 print_srccode(thread, x.cpumode, start + off);
48d02a1d 1198 }
a1a58707
ACM
1199out:
1200 return printed;
48d02a1d 1201}
dc323ce8 1202
a1a58707
ACM
1203static int perf_sample__fprintf_addr(struct perf_sample *sample,
1204 struct thread *thread,
1205 struct perf_event_attr *attr, FILE *fp)
7cec0922
DA
1206{
1207 struct addr_location al;
a1a58707 1208 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
7cec0922
DA
1209
1210 if (!sample_addr_correlates_sym(attr))
a1a58707 1211 goto out;
7cec0922 1212
c2740a87 1213 thread__resolve(thread, &al, sample);
7cec0922
DA
1214
1215 if (PRINT_FIELD(SYM)) {
a1a58707 1216 printed += fprintf(fp, " ");
a978f2ab 1217 if (PRINT_FIELD(SYMOFFSET))
a1a58707 1218 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
a978f2ab 1219 else
a1a58707 1220 printed += symbol__fprintf_symname(al.sym, fp);
7cec0922
DA
1221 }
1222
1223 if (PRINT_FIELD(DSO)) {
a1a58707
ACM
1224 printed += fprintf(fp, " (");
1225 printed += map__fprintf_dsoname(al.map, fp);
1226 printed += fprintf(fp, ")");
7cec0922 1227 }
a1a58707
ACM
1228out:
1229 return printed;
7cec0922
DA
1230}
1231
99f753f0 1232static const char *resolve_branch_sym(struct perf_sample *sample,
32dcd021 1233 struct evsel *evsel,
99f753f0
AK
1234 struct thread *thread,
1235 struct addr_location *al,
1236 u64 *ip)
1237{
1238 struct addr_location addr_al;
1fc632ce 1239 struct perf_event_attr *attr = &evsel->core.attr;
99f753f0
AK
1240 const char *name = NULL;
1241
1242 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1243 if (sample_addr_correlates_sym(attr)) {
1244 thread__resolve(thread, &addr_al, sample);
1245 if (addr_al.sym)
1246 name = addr_al.sym->name;
1247 else
1248 *ip = sample->addr;
1249 } else {
1250 *ip = sample->addr;
1251 }
1252 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1253 if (al->sym)
1254 name = al->sym->name;
1255 else
1256 *ip = sample->ip;
1257 }
1258 return name;
1259}
1260
a1a58707 1261static int perf_sample__fprintf_callindent(struct perf_sample *sample,
32dcd021 1262 struct evsel *evsel,
a1a58707
ACM
1263 struct thread *thread,
1264 struct addr_location *al, FILE *fp)
e216708d 1265{
1fc632ce 1266 struct perf_event_attr *attr = &evsel->core.attr;
256d92bc 1267 size_t depth = thread_stack__depth(thread, sample->cpu);
e216708d
AH
1268 const char *name = NULL;
1269 static int spacing;
1270 int len = 0;
a78cdee6 1271 int dlen = 0;
e216708d
AH
1272 u64 ip = 0;
1273
1274 /*
1275 * The 'return' has already been popped off the stack so the depth has
1276 * to be adjusted to match the 'call'.
1277 */
1278 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1279 depth += 1;
1280
99f753f0 1281 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
e216708d 1282
a78cdee6
AK
1283 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1284 dlen += fprintf(fp, "(");
1285 dlen += map__fprintf_dsoname(al->map, fp);
1286 dlen += fprintf(fp, ")\t");
1287 }
1288
e216708d 1289 if (name)
a1a58707 1290 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
e216708d 1291 else if (ip)
a1a58707 1292 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
e216708d
AH
1293
1294 if (len < 0)
a1a58707 1295 return len;
e216708d
AH
1296
1297 /*
1298 * Try to keep the output length from changing frequently so that the
1299 * output lines up more nicely.
1300 */
1301 if (len > spacing || (len && len < spacing - 52))
1302 spacing = round_up(len + 4, 32);
1303
1304 if (len < spacing)
a1a58707
ACM
1305 len += fprintf(fp, "%*s", spacing - len, "");
1306
a78cdee6 1307 return len + dlen;
e216708d
AH
1308}
1309
3ab481a1
AK
1310__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1311 struct thread *thread __maybe_unused,
1312 struct machine *machine __maybe_unused)
1313{
1314}
1315
a1a58707
ACM
1316static int perf_sample__fprintf_insn(struct perf_sample *sample,
1317 struct perf_event_attr *attr,
1318 struct thread *thread,
1319 struct machine *machine, FILE *fp)
224e2c97 1320{
a1a58707
ACM
1321 int printed = 0;
1322
3ab481a1
AK
1323 if (sample->insn_len == 0 && native_arch)
1324 arch_fetch_insn(sample, thread, machine);
1325
224e2c97 1326 if (PRINT_FIELD(INSNLEN))
a1a58707 1327 printed += fprintf(fp, " ilen: %d", sample->insn_len);
3ab481a1 1328 if (PRINT_FIELD(INSN) && sample->insn_len) {
224e2c97
AK
1329 int i;
1330
a1a58707 1331 printed += fprintf(fp, " insn:");
224e2c97 1332 for (i = 0; i < sample->insn_len; i++)
a1a58707 1333 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
224e2c97 1334 }
48d02a1d 1335 if (PRINT_FIELD(BRSTACKINSN))
a1a58707
ACM
1336 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1337
1338 return printed;
224e2c97
AK
1339}
1340
68fb45bf
AH
1341static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1342 struct perf_event_attr *attr, FILE *fp)
1343{
1344 unsigned int ipc;
1345
1346 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1347 return 0;
1348
1349 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1350
1351 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1352 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1353}
1354
a1a58707 1355static int perf_sample__fprintf_bts(struct perf_sample *sample,
32dcd021 1356 struct evsel *evsel,
a1a58707
ACM
1357 struct thread *thread,
1358 struct addr_location *al,
1359 struct machine *machine, FILE *fp)
95582596 1360{
1fc632ce 1361 struct perf_event_attr *attr = &evsel->core.attr;
1405720d 1362 unsigned int type = output_type(attr->type);
8066be5f 1363 bool print_srcline_last = false;
a1a58707 1364 int printed = 0;
95582596 1365
e216708d 1366 if (PRINT_FIELD(CALLINDENT))
a1a58707 1367 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
e216708d 1368
95582596
AN
1369 /* print branch_from information */
1370 if (PRINT_FIELD(IP)) {
1405720d 1371 unsigned int print_opts = output[type].print_ip_opts;
e557b674 1372 struct callchain_cursor *cursor = NULL;
8066be5f 1373
6f736735 1374 if (symbol_conf.use_callchain && sample->callchain &&
e557b674 1375 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
6f736735 1376 sample, NULL, NULL, scripting_max_stack) == 0)
e557b674 1377 cursor = &callchain_cursor;
6f736735
ACM
1378
1379 if (cursor == NULL) {
a1a58707 1380 printed += fprintf(fp, " ");
e20ab86e 1381 if (print_opts & EVSEL__PRINT_SRCLINE) {
8066be5f 1382 print_srcline_last = true;
e20ab86e 1383 print_opts &= ~EVSEL__PRINT_SRCLINE;
8066be5f 1384 }
6f736735 1385 } else
a1a58707 1386 printed += fprintf(fp, "\n");
6f736735 1387
9620bc36
ACM
1388 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor,
1389 symbol_conf.bt_stop_list, fp);
95582596
AN
1390 }
1391
95582596 1392 /* print branch_to information */
243be3dd 1393 if (PRINT_FIELD(ADDR) ||
1fc632ce 1394 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
1405720d 1395 !output[type].user_set)) {
a1a58707
ACM
1396 printed += fprintf(fp, " => ");
1397 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
578bea40 1398 }
95582596 1399
68fb45bf
AH
1400 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1401
8066be5f 1402 if (print_srcline_last)
a1a58707 1403 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
224e2c97 1404
a1a58707 1405 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
dd2e18e9
AK
1406 printed += fprintf(fp, "\n");
1407 if (PRINT_FIELD(SRCCODE)) {
1408 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1409 &thread->srccode_state);
1410 if (ret) {
1411 printed += ret;
1412 printed += printf("\n");
1413 }
1414 }
1415 return printed;
95582596
AN
1416}
1417
055cd33d
AH
1418static struct {
1419 u32 flags;
1420 const char *name;
1421} sample_flags[] = {
1422 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1423 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1424 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1425 {PERF_IP_FLAG_BRANCH, "jmp"},
1426 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1427 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1428 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1429 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1430 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1431 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1432 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1433 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1434 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1435 {0, NULL}
1436};
1437
62cb1b88
AH
1438static const char *sample_flags_to_name(u32 flags)
1439{
1440 int i;
1441
1442 for (i = 0; sample_flags[i].name ; i++) {
1443 if (sample_flags[i].flags == flags)
1444 return sample_flags[i].name;
1445 }
1446
1447 return NULL;
1448}
1449
a1a58707 1450static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
400ea6d3
AH
1451{
1452 const char *chars = PERF_IP_FLAG_CHARS;
1453 const int n = strlen(PERF_IP_FLAG_CHARS);
055cd33d
AH
1454 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1455 const char *name = NULL;
400ea6d3
AH
1456 char str[33];
1457 int i, pos = 0;
1458
62cb1b88
AH
1459 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1460 if (name)
1461 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1462
1463 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1464 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1465 if (name)
1466 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1467 }
1468
1469 if (flags & PERF_IP_FLAG_TRACE_END) {
1470 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1471 if (name)
1472 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
055cd33d
AH
1473 }
1474
400ea6d3
AH
1475 for (i = 0; i < n; i++, flags >>= 1) {
1476 if (flags & 1)
1477 str[pos++] = chars[i];
1478 }
1479 for (; i < 32; i++, flags >>= 1) {
1480 if (flags & 1)
1481 str[pos++] = '?';
1482 }
1483 str[pos] = 0;
055cd33d 1484
62cb1b88 1485 return fprintf(fp, " %-19s ", str);
400ea6d3
AH
1486}
1487
30372f04
WN
1488struct printer_data {
1489 int line_no;
1490 bool hit_nul;
1491 bool is_printable;
1492};
1493
923d0c9a
ACM
1494static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1495 unsigned int val,
1496 void *extra, FILE *fp)
30372f04
WN
1497{
1498 unsigned char ch = (unsigned char)val;
1499 struct printer_data *printer_data = extra;
923d0c9a 1500 int printed = 0;
30372f04
WN
1501
1502 switch (op) {
1503 case BINARY_PRINT_DATA_BEGIN:
923d0c9a 1504 printed += fprintf(fp, "\n");
30372f04
WN
1505 break;
1506 case BINARY_PRINT_LINE_BEGIN:
923d0c9a 1507 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
30372f04
WN
1508 " ");
1509 break;
1510 case BINARY_PRINT_ADDR:
923d0c9a 1511 printed += fprintf(fp, " %04x:", val);
30372f04
WN
1512 break;
1513 case BINARY_PRINT_NUM_DATA:
923d0c9a 1514 printed += fprintf(fp, " %02x", val);
30372f04
WN
1515 break;
1516 case BINARY_PRINT_NUM_PAD:
923d0c9a 1517 printed += fprintf(fp, " ");
30372f04
WN
1518 break;
1519 case BINARY_PRINT_SEP:
923d0c9a 1520 printed += fprintf(fp, " ");
30372f04
WN
1521 break;
1522 case BINARY_PRINT_CHAR_DATA:
1523 if (printer_data->hit_nul && ch)
1524 printer_data->is_printable = false;
1525
1526 if (!isprint(ch)) {
923d0c9a 1527 printed += fprintf(fp, "%c", '.');
30372f04
WN
1528
1529 if (!printer_data->is_printable)
1530 break;
1531
1532 if (ch == '\0')
1533 printer_data->hit_nul = true;
1534 else
1535 printer_data->is_printable = false;
1536 } else {
923d0c9a 1537 printed += fprintf(fp, "%c", ch);
30372f04
WN
1538 }
1539 break;
1540 case BINARY_PRINT_CHAR_PAD:
923d0c9a 1541 printed += fprintf(fp, " ");
30372f04
WN
1542 break;
1543 case BINARY_PRINT_LINE_END:
923d0c9a 1544 printed += fprintf(fp, "\n");
30372f04
WN
1545 printer_data->line_no++;
1546 break;
1547 case BINARY_PRINT_DATA_END:
1548 default:
1549 break;
1550 }
923d0c9a
ACM
1551
1552 return printed;
30372f04
WN
1553}
1554
a1a58707 1555static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
30372f04
WN
1556{
1557 unsigned int nr_bytes = sample->raw_size;
1558 struct printer_data printer_data = {0, false, true};
a1a58707
ACM
1559 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1560 sample__fprintf_bpf_output, &printer_data, fp);
30372f04
WN
1561
1562 if (printer_data.is_printable && printer_data.hit_nul)
a1a58707
ACM
1563 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1564
1565 return printed;
30372f04
WN
1566}
1567
a1a58707 1568static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
65c5e18f
AH
1569{
1570 if (len > 0 && len < spacing)
a1a58707
ACM
1571 return fprintf(fp, "%*s", spacing - len, "");
1572
1573 return 0;
65c5e18f
AH
1574}
1575
a1a58707 1576static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
65c5e18f 1577{
a1a58707 1578 return perf_sample__fprintf_spacing(len, 34, fp);
65c5e18f
AH
1579}
1580
a1a58707 1581static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
65c5e18f
AH
1582{
1583 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1584 int len;
1585
1586 if (perf_sample__bad_synth_size(sample, *data))
a1a58707 1587 return 0;
65c5e18f 1588
a1a58707 1589 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
65c5e18f 1590 data->ip, le64_to_cpu(data->payload));
a1a58707 1591 return len + perf_sample__fprintf_pt_spacing(len, fp);
65c5e18f
AH
1592}
1593
a1a58707 1594static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
65c5e18f
AH
1595{
1596 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1597 int len;
1598
1599 if (perf_sample__bad_synth_size(sample, *data))
a1a58707 1600 return 0;
65c5e18f 1601
a1a58707
ACM
1602 len = fprintf(fp, " hints: %#x extensions: %#x ",
1603 data->hints, data->extensions);
1604 return len + perf_sample__fprintf_pt_spacing(len, fp);
65c5e18f
AH
1605}
1606
a1a58707 1607static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
65c5e18f
AH
1608{
1609 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1610 int len;
1611
1612 if (perf_sample__bad_synth_size(sample, *data))
a1a58707 1613 return 0;
65c5e18f 1614
a1a58707
ACM
1615 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1616 data->hw, data->cstate, data->subcstate);
1617 return len + perf_sample__fprintf_pt_spacing(len, fp);
65c5e18f
AH
1618}
1619
a1a58707 1620static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
65c5e18f
AH
1621{
1622 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1623 int len;
1624
1625 if (perf_sample__bad_synth_size(sample, *data))
a1a58707 1626 return 0;
65c5e18f 1627
a1a58707
ACM
1628 len = fprintf(fp, " IP: %u ", data->ip);
1629 return len + perf_sample__fprintf_pt_spacing(len, fp);
65c5e18f
AH
1630}
1631
a1a58707 1632static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
65c5e18f
AH
1633{
1634 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1635 int len;
1636
1637 if (perf_sample__bad_synth_size(sample, *data))
a1a58707 1638 return 0;
65c5e18f 1639
a1a58707 1640 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
65c5e18f
AH
1641 data->deepest_cstate, data->last_cstate,
1642 data->wake_reason);
a1a58707 1643 return len + perf_sample__fprintf_pt_spacing(len, fp);
65c5e18f
AH
1644}
1645
a1a58707 1646static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
65c5e18f
AH
1647{
1648 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1649 unsigned int percent, freq;
1650 int len;
1651
1652 if (perf_sample__bad_synth_size(sample, *data))
a1a58707 1653 return 0;
65c5e18f
AH
1654
1655 freq = (le32_to_cpu(data->freq) + 500) / 1000;
a1a58707 1656 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
65c5e18f
AH
1657 if (data->max_nonturbo) {
1658 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
a1a58707 1659 len += fprintf(fp, "(%3u%%) ", percent);
65c5e18f 1660 }
a1a58707 1661 return len + perf_sample__fprintf_pt_spacing(len, fp);
65c5e18f
AH
1662}
1663
a1a58707 1664static int perf_sample__fprintf_synth(struct perf_sample *sample,
32dcd021 1665 struct evsel *evsel, FILE *fp)
47e78084 1666{
1fc632ce 1667 switch (evsel->core.attr.config) {
65c5e18f 1668 case PERF_SYNTH_INTEL_PTWRITE:
a1a58707 1669 return perf_sample__fprintf_synth_ptwrite(sample, fp);
65c5e18f 1670 case PERF_SYNTH_INTEL_MWAIT:
a1a58707 1671 return perf_sample__fprintf_synth_mwait(sample, fp);
65c5e18f 1672 case PERF_SYNTH_INTEL_PWRE:
a1a58707 1673 return perf_sample__fprintf_synth_pwre(sample, fp);
65c5e18f 1674 case PERF_SYNTH_INTEL_EXSTOP:
a1a58707 1675 return perf_sample__fprintf_synth_exstop(sample, fp);
65c5e18f 1676 case PERF_SYNTH_INTEL_PWRX:
a1a58707 1677 return perf_sample__fprintf_synth_pwrx(sample, fp);
65c5e18f 1678 case PERF_SYNTH_INTEL_CBR:
a1a58707 1679 return perf_sample__fprintf_synth_cbr(sample, fp);
47e78084
AH
1680 default:
1681 break;
1682 }
a1a58707
ACM
1683
1684 return 0;
47e78084
AH
1685}
1686
809e9423
JO
1687struct perf_script {
1688 struct perf_tool tool;
1689 struct perf_session *session;
1690 bool show_task_events;
1691 bool show_mmap_events;
1692 bool show_switch_events;
96a44bbc 1693 bool show_namespace_events;
3d7c27b6 1694 bool show_lost_events;
3233b37a 1695 bool show_round_events;
490c8cc9 1696 bool show_bpf_events;
cfc8874a 1697 bool allocated;
a14390fd 1698 bool per_event_dump;
f90a2417 1699 struct evswitch evswitch;
f854839b 1700 struct perf_cpu_map *cpus;
9749b90e 1701 struct perf_thread_map *threads;
9cdbc409 1702 int name_width;
a91f4c47 1703 const char *time_str;
cc2ef584
JY
1704 struct perf_time_interval *ptime_range;
1705 int range_size;
2ab046cd 1706 int range_num;
809e9423
JO
1707};
1708
63503dba 1709static int perf_evlist__max_name_len(struct evlist *evlist)
9cdbc409 1710{
32dcd021 1711 struct evsel *evsel;
9cdbc409
JO
1712 int max = 0;
1713
e5cadb93 1714 evlist__for_each_entry(evlist, evsel) {
9cdbc409
JO
1715 int len = strlen(perf_evsel__name(evsel));
1716
1717 max = MAX(len, max);
1718 }
1719
1720 return max;
1721}
1722
a1a58707 1723static int data_src__fprintf(u64 data_src, FILE *fp)
c19ac912
JO
1724{
1725 struct mem_info mi = { .data_src.val = data_src };
1726 char decode[100];
1727 char out[100];
1728 static int maxlen;
1729 int len;
1730
1731 perf_script__meminfo_scnprintf(decode, 100, &mi);
1732
1733 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1734 if (maxlen < len)
1735 maxlen = len;
1736
a1a58707 1737 return fprintf(fp, "%-*s", maxlen, out);
c19ac912
JO
1738}
1739
4bd1bef8
AK
1740struct metric_ctx {
1741 struct perf_sample *sample;
1742 struct thread *thread;
32dcd021 1743 struct evsel *evsel;
4bd1bef8
AK
1744 FILE *fp;
1745};
1746
6ca9a082
JO
1747static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1748 void *ctx, const char *color,
4bd1bef8
AK
1749 const char *fmt,
1750 const char *unit, double val)
1751{
1752 struct metric_ctx *mctx = ctx;
1753
1754 if (!fmt)
1755 return;
1756 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
28a0b398 1757 PERF_RECORD_SAMPLE, mctx->fp);
4bd1bef8
AK
1758 fputs("\tmetric: ", mctx->fp);
1759 if (color)
1760 color_fprintf(mctx->fp, color, fmt, val);
1761 else
1762 printf(fmt, val);
1763 fprintf(mctx->fp, " %s\n", unit);
1764}
1765
6ca9a082
JO
1766static void script_new_line(struct perf_stat_config *config __maybe_unused,
1767 void *ctx)
4bd1bef8
AK
1768{
1769 struct metric_ctx *mctx = ctx;
1770
1771 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
28a0b398 1772 PERF_RECORD_SAMPLE, mctx->fp);
4bd1bef8
AK
1773 fputs("\tmetric: ", mctx->fp);
1774}
1775
1776static void perf_sample__fprint_metric(struct perf_script *script,
1777 struct thread *thread,
32dcd021 1778 struct evsel *evsel,
4bd1bef8
AK
1779 struct perf_sample *sample,
1780 FILE *fp)
1781{
1782 struct perf_stat_output_ctx ctx = {
1783 .print_metric = script_print_metric,
1784 .new_line = script_new_line,
1785 .ctx = &(struct metric_ctx) {
1786 .sample = sample,
1787 .thread = thread,
1788 .evsel = evsel,
1789 .fp = fp,
1790 },
1791 .force_header = false,
1792 };
32dcd021 1793 struct evsel *ev2;
4bd1bef8
AK
1794 u64 val;
1795
4bd1bef8
AK
1796 if (!evsel->stats)
1797 perf_evlist__alloc_stats(script->session->evlist, false);
1798 if (evsel_script(evsel->leader)->gnum++ == 0)
1799 perf_stat__reset_shadow_stats();
1800 val = sample->period * evsel->scale;
1801 perf_stat__update_shadow_stats(evsel,
1802 val,
1fcd0394
JY
1803 sample->cpu,
1804 &rt_stat);
4bd1bef8 1805 evsel_script(evsel)->val = val;
5643b1a5 1806 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
4bd1bef8 1807 for_each_group_member (ev2, evsel->leader) {
6ca9a082 1808 perf_stat__print_shadow_stats(&stat_config, ev2,
4bd1bef8
AK
1809 evsel_script(ev2)->val,
1810 sample->cpu,
1811 &ctx,
e0128b30
JY
1812 NULL,
1813 &rt_stat);
4bd1bef8
AK
1814 }
1815 evsel_script(evsel->leader)->gnum = 0;
1816 }
1817}
1818
99f753f0 1819static bool show_event(struct perf_sample *sample,
32dcd021 1820 struct evsel *evsel,
99f753f0
AK
1821 struct thread *thread,
1822 struct addr_location *al)
1823{
256d92bc 1824 int depth = thread_stack__depth(thread, sample->cpu);
99f753f0
AK
1825
1826 if (!symbol_conf.graph_function)
1827 return true;
1828
1829 if (thread->filter) {
1830 if (depth <= thread->filter_entry_depth) {
1831 thread->filter = false;
1832 return false;
1833 }
1834 return true;
1835 } else {
1836 const char *s = symbol_conf.graph_function;
1837 u64 ip;
1838 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1839 &ip);
1840 unsigned nlen;
1841
1842 if (!name)
1843 return false;
1844 nlen = strlen(name);
1845 while (*s) {
1846 unsigned len = strcspn(s, ",");
1847 if (nlen == len && !strncmp(name, s, len)) {
1848 thread->filter = true;
1849 thread->filter_entry_depth = depth;
1850 return true;
1851 }
1852 s += len;
1853 if (*s == ',')
1854 s++;
1855 }
1856 return false;
1857 }
1858}
1859
a3dff304 1860static void process_event(struct perf_script *script,
32dcd021 1861 struct perf_sample *sample, struct evsel *evsel,
48d02a1d
AK
1862 struct addr_location *al,
1863 struct machine *machine)
be6d842a 1864{
f9d5d549 1865 struct thread *thread = al->thread;
1fc632ce 1866 struct perf_event_attr *attr = &evsel->core.attr;
1405720d 1867 unsigned int type = output_type(attr->type);
32dcd021 1868 struct evsel_script *es = evsel->priv;
642ee1c6 1869 FILE *fp = es->fp;
1424dc96 1870
1405720d 1871 if (output[type].fields == 0)
1424dc96
DA
1872 return;
1873
99f753f0
AK
1874 if (!show_event(sample, evsel, thread, al))
1875 return;
1876
8829e56f
ACM
1877 if (evswitch__discard(&script->evswitch, evsel))
1878 return;
dd41f660 1879
642ee1c6
ACM
1880 ++es->samples;
1881
28a0b398
JO
1882 perf_sample__fprintf_start(sample, thread, evsel,
1883 PERF_RECORD_SAMPLE, fp);
745f43e3 1884
535aeaae 1885 if (PRINT_FIELD(PERIOD))
69c71252 1886 fprintf(fp, "%10" PRIu64 " ", sample->period);
535aeaae 1887
e944d3d7
NK
1888 if (PRINT_FIELD(EVNAME)) {
1889 const char *evname = perf_evsel__name(evsel);
9cdbc409
JO
1890
1891 if (!script->name_width)
1892 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1893
69c71252 1894 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
e944d3d7
NK
1895 }
1896
400ea6d3 1897 if (print_flags)
a1a58707 1898 perf_sample__fprintf_flags(sample->flags, fp);
400ea6d3 1899
95582596 1900 if (is_bts_event(attr)) {
a1a58707 1901 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
95582596
AN
1902 return;
1903 }
1904
96167167 1905 if (PRINT_FIELD(TRACE) && sample->raw_data) {
894f3f17
ACM
1906 event_format__fprintf(evsel->tp_format, sample->cpu,
1907 sample->raw_data, sample->raw_size, fp);
1908 }
47e78084
AH
1909
1910 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
a1a58707 1911 perf_sample__fprintf_synth(sample, evsel, fp);
47e78084 1912
7cec0922 1913 if (PRINT_FIELD(ADDR))
a1a58707 1914 perf_sample__fprintf_addr(sample, thread, attr, fp);
7cec0922 1915
94ddddfa 1916 if (PRINT_FIELD(DATA_SRC))
a1a58707 1917 data_src__fprintf(sample->data_src, fp);
94ddddfa
JO
1918
1919 if (PRINT_FIELD(WEIGHT))
a1a58707 1920 fprintf(fp, "%16" PRIu64, sample->weight);
94ddddfa 1921
787bef17 1922 if (PRINT_FIELD(IP)) {
e557b674 1923 struct callchain_cursor *cursor = NULL;
6f736735 1924
92231521 1925 if (symbol_conf.use_callchain && sample->callchain &&
e557b674 1926 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
6f736735 1927 sample, NULL, NULL, scripting_max_stack) == 0)
e557b674 1928 cursor = &callchain_cursor;
a6ffaf91 1929
a1a58707 1930 fputc(cursor ? '\n' : ' ', fp);
9620bc36
ACM
1931 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor,
1932 symbol_conf.bt_stop_list, fp);
c0230b2b
DA
1933 }
1934
fc36f948 1935 if (PRINT_FIELD(IREGS))
a1a58707 1936 perf_sample__fprintf_iregs(sample, attr, fp);
fc36f948 1937
b1491ace 1938 if (PRINT_FIELD(UREGS))
a1a58707 1939 perf_sample__fprintf_uregs(sample, attr, fp);
b1491ace 1940
dc323ce8 1941 if (PRINT_FIELD(BRSTACK))
a1a58707 1942 perf_sample__fprintf_brstack(sample, thread, attr, fp);
dc323ce8 1943 else if (PRINT_FIELD(BRSTACKSYM))
a1a58707 1944 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
106dacd8 1945 else if (PRINT_FIELD(BRSTACKOFF))
a1a58707 1946 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
dc323ce8 1947
30372f04 1948 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
a1a58707
ACM
1949 perf_sample__fprintf_bpf_output(sample, fp);
1950 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
49d58f04
KL
1951
1952 if (PRINT_FIELD(PHYS_ADDR))
69c71252 1953 fprintf(fp, "%16" PRIx64, sample->phys_addr);
68fb45bf
AH
1954
1955 perf_sample__fprintf_ipc(sample, attr, fp);
1956
69c71252 1957 fprintf(fp, "\n");
4bd1bef8 1958
dd2e18e9
AK
1959 if (PRINT_FIELD(SRCCODE)) {
1960 if (map__fprintf_srccode(al->map, al->addr, stdout,
1961 &thread->srccode_state))
1962 printf("\n");
1963 }
1964
4bd1bef8
AK
1965 if (PRINT_FIELD(METRIC))
1966 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
7ee40678
MW
1967
1968 if (verbose)
1969 fflush(fp);
be6d842a
DA
1970}
1971
956ffd02
TZ
1972static struct scripting_ops *scripting_ops;
1973
32dcd021 1974static void __process_stat(struct evsel *counter, u64 tstamp)
36e33c53 1975{
a2f354e3 1976 int nthreads = perf_thread_map__nr(counter->core.threads);
36e33c53
JO
1977 int ncpus = perf_evsel__nr_cpus(counter);
1978 int cpu, thread;
1979 static int header_printed;
1980
648b5af3 1981 if (counter->core.system_wide)
36e33c53
JO
1982 nthreads = 1;
1983
1984 if (!header_printed) {
1985 printf("%3s %8s %15s %15s %15s %15s %s\n",
1986 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1987 header_printed = 1;
1988 }
1989
1990 for (thread = 0; thread < nthreads; thread++) {
1991 for (cpu = 0; cpu < ncpus; cpu++) {
1992 struct perf_counts_values *counts;
1993
1994 counts = perf_counts(counter->counts, cpu, thread);
1995
1996 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
d400bd3a 1997 counter->core.cpus->map[cpu],
a2f354e3 1998 perf_thread_map__pid(counter->core.threads, thread),
36e33c53
JO
1999 counts->val,
2000 counts->ena,
2001 counts->run,
2002 tstamp,
2003 perf_evsel__name(counter));
2004 }
2005 }
2006}
2007
32dcd021 2008static void process_stat(struct evsel *counter, u64 tstamp)
e099eba8
JO
2009{
2010 if (scripting_ops && scripting_ops->process_stat)
2011 scripting_ops->process_stat(&stat_config, counter, tstamp);
36e33c53
JO
2012 else
2013 __process_stat(counter, tstamp);
e099eba8
JO
2014}
2015
2016static void process_stat_interval(u64 tstamp)
2017{
2018 if (scripting_ops && scripting_ops->process_stat_interval)
2019 scripting_ops->process_stat_interval(tstamp);
2020}
2021
956ffd02
TZ
2022static void setup_scripting(void)
2023{
16c632de 2024 setup_perl_scripting();
7e4b21b8 2025 setup_python_scripting();
956ffd02
TZ
2026}
2027
d445dd2a
AH
2028static int flush_scripting(void)
2029{
2aaecfc5 2030 return scripting_ops ? scripting_ops->flush_script() : 0;
d445dd2a
AH
2031}
2032
956ffd02
TZ
2033static int cleanup_scripting(void)
2034{
133dc4c3 2035 pr_debug("\nperf script stopped\n");
3824a4e8 2036
2aaecfc5 2037 return scripting_ops ? scripting_ops->stop_script() : 0;
956ffd02
TZ
2038}
2039
e87e5481
AK
2040static bool filter_cpu(struct perf_sample *sample)
2041{
2042 if (cpu_list)
2043 return !test_bit(sample->cpu, cpu_bitmap);
2044 return false;
2045}
2046
809e9423 2047static int process_sample_event(struct perf_tool *tool,
d20deb64 2048 union perf_event *event,
8115d60c 2049 struct perf_sample *sample,
32dcd021 2050 struct evsel *evsel,
743eb868 2051 struct machine *machine)
5f9c39dc 2052{
809e9423 2053 struct perf_script *scr = container_of(tool, struct perf_script, tool);
e7984b7b 2054 struct addr_location al;
5f9c39dc 2055
2ab046cd
JY
2056 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2057 sample->time)) {
a91f4c47 2058 return 0;
2ab046cd 2059 }
a91f4c47 2060
1424dc96
DA
2061 if (debug_mode) {
2062 if (sample->time < last_timestamp) {
2063 pr_err("Samples misordered, previous: %" PRIu64
2064 " this: %" PRIu64 "\n", last_timestamp,
2065 sample->time);
2066 nr_unordered++;
e1889d75 2067 }
1424dc96
DA
2068 last_timestamp = sample->time;
2069 return 0;
5f9c39dc 2070 }
5d67be97 2071
bb3eb566 2072 if (machine__resolve(machine, &al, sample) < 0) {
e7984b7b
DA
2073 pr_err("problem processing %d event, skipping it.\n",
2074 event->header.type);
2075 return -1;
2076 }
2077
2078 if (al.filtered)
b91fc39f 2079 goto out_put;
e7984b7b 2080
e87e5481 2081 if (filter_cpu(sample))
b91fc39f 2082 goto out_put;
5d67be97 2083
2aaecfc5
JO
2084 if (scripting_ops)
2085 scripting_ops->process_event(event, sample, evsel, &al);
2086 else
48d02a1d 2087 process_event(scr, sample, evsel, &al, machine);
2aaecfc5 2088
b91fc39f
ACM
2089out_put:
2090 addr_location__put(&al);
5f9c39dc
FW
2091 return 0;
2092}
2093
7ea95727 2094static int process_attr(struct perf_tool *tool, union perf_event *event,
63503dba 2095 struct evlist **pevlist)
7ea95727
AH
2096{
2097 struct perf_script *scr = container_of(tool, struct perf_script, tool);
63503dba 2098 struct evlist *evlist;
32dcd021 2099 struct evsel *evsel, *pos;
7ea95727 2100 int err;
32dcd021 2101 static struct evsel_script *es;
7ea95727
AH
2102
2103 err = perf_event__process_attr(tool, event, pevlist);
2104 if (err)
2105 return err;
2106
2107 evlist = *pevlist;
515dbe48 2108 evsel = evlist__last(*pevlist);
7ea95727 2109
a3af66f5
RB
2110 if (!evsel->priv) {
2111 if (scr->per_event_dump) {
2112 evsel->priv = perf_evsel_script__new(evsel,
2113 scr->session->data);
2114 } else {
2115 es = zalloc(sizeof(*es));
2116 if (!es)
2117 return -ENOMEM;
2118 es->fp = stdout;
2119 evsel->priv = es;
2120 }
2121 }
2122
1fc632ce
JO
2123 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2124 evsel->core.attr.type != PERF_TYPE_SYNTH)
7ea95727
AH
2125 return 0;
2126
e5cadb93 2127 evlist__for_each_entry(evlist, pos) {
1fc632ce 2128 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
7ea95727
AH
2129 return 0;
2130 }
2131
1fc632ce 2132 set_print_ip_opts(&evsel->core.attr);
7ea95727 2133
1fc632ce 2134 if (evsel->core.attr.sample_type)
d2b5a315
JO
2135 err = perf_evsel__check_attr(evsel, scr->session);
2136
2137 return err;
7ea95727
AH
2138}
2139
ad7ebb9a
NK
2140static int process_comm_event(struct perf_tool *tool,
2141 union perf_event *event,
2142 struct perf_sample *sample,
2143 struct machine *machine)
2144{
2145 struct thread *thread;
2146 struct perf_script *script = container_of(tool, struct perf_script, tool);
2147 struct perf_session *session = script->session;
32dcd021 2148 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ad7ebb9a
NK
2149 int ret = -1;
2150
2151 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2152 if (thread == NULL) {
2153 pr_debug("problem processing COMM event, skipping it.\n");
2154 return -1;
2155 }
2156
2157 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2158 goto out;
2159
1fc632ce 2160 if (!evsel->core.attr.sample_id_all) {
ad7ebb9a
NK
2161 sample->cpu = 0;
2162 sample->time = 0;
2163 sample->tid = event->comm.tid;
2164 sample->pid = event->comm.pid;
2165 }
e87e5481
AK
2166 if (!filter_cpu(sample)) {
2167 perf_sample__fprintf_start(sample, thread, evsel,
28a0b398 2168 PERF_RECORD_COMM, stdout);
e87e5481
AK
2169 perf_event__fprintf(event, stdout);
2170 }
ad7ebb9a 2171 ret = 0;
ad7ebb9a 2172out:
b91fc39f 2173 thread__put(thread);
ad7ebb9a
NK
2174 return ret;
2175}
2176
96a44bbc
HB
2177static int process_namespaces_event(struct perf_tool *tool,
2178 union perf_event *event,
2179 struct perf_sample *sample,
2180 struct machine *machine)
2181{
2182 struct thread *thread;
2183 struct perf_script *script = container_of(tool, struct perf_script, tool);
2184 struct perf_session *session = script->session;
32dcd021 2185 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
96a44bbc
HB
2186 int ret = -1;
2187
2188 thread = machine__findnew_thread(machine, event->namespaces.pid,
2189 event->namespaces.tid);
2190 if (thread == NULL) {
2191 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2192 return -1;
2193 }
2194
2195 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2196 goto out;
2197
1fc632ce 2198 if (!evsel->core.attr.sample_id_all) {
96a44bbc
HB
2199 sample->cpu = 0;
2200 sample->time = 0;
2201 sample->tid = event->namespaces.tid;
2202 sample->pid = event->namespaces.pid;
2203 }
e87e5481
AK
2204 if (!filter_cpu(sample)) {
2205 perf_sample__fprintf_start(sample, thread, evsel,
2206 PERF_RECORD_NAMESPACES, stdout);
2207 perf_event__fprintf(event, stdout);
2208 }
96a44bbc
HB
2209 ret = 0;
2210out:
2211 thread__put(thread);
2212 return ret;
2213}
2214
ad7ebb9a
NK
2215static int process_fork_event(struct perf_tool *tool,
2216 union perf_event *event,
2217 struct perf_sample *sample,
2218 struct machine *machine)
2219{
2220 struct thread *thread;
2221 struct perf_script *script = container_of(tool, struct perf_script, tool);
2222 struct perf_session *session = script->session;
32dcd021 2223 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ad7ebb9a
NK
2224
2225 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2226 return -1;
2227
2228 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2229 if (thread == NULL) {
2230 pr_debug("problem processing FORK event, skipping it.\n");
2231 return -1;
2232 }
2233
1fc632ce 2234 if (!evsel->core.attr.sample_id_all) {
ad7ebb9a
NK
2235 sample->cpu = 0;
2236 sample->time = event->fork.time;
2237 sample->tid = event->fork.tid;
2238 sample->pid = event->fork.pid;
2239 }
e87e5481
AK
2240 if (!filter_cpu(sample)) {
2241 perf_sample__fprintf_start(sample, thread, evsel,
2242 PERF_RECORD_FORK, stdout);
2243 perf_event__fprintf(event, stdout);
2244 }
b91fc39f 2245 thread__put(thread);
ad7ebb9a
NK
2246
2247 return 0;
2248}
2249static int process_exit_event(struct perf_tool *tool,
2250 union perf_event *event,
2251 struct perf_sample *sample,
2252 struct machine *machine)
2253{
b91fc39f 2254 int err = 0;
ad7ebb9a
NK
2255 struct thread *thread;
2256 struct perf_script *script = container_of(tool, struct perf_script, tool);
2257 struct perf_session *session = script->session;
32dcd021 2258 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ad7ebb9a
NK
2259
2260 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2261 if (thread == NULL) {
2262 pr_debug("problem processing EXIT event, skipping it.\n");
2263 return -1;
2264 }
2265
1fc632ce 2266 if (!evsel->core.attr.sample_id_all) {
ad7ebb9a
NK
2267 sample->cpu = 0;
2268 sample->time = 0;
53ff6bc3
AH
2269 sample->tid = event->fork.tid;
2270 sample->pid = event->fork.pid;
ad7ebb9a 2271 }
e87e5481
AK
2272 if (!filter_cpu(sample)) {
2273 perf_sample__fprintf_start(sample, thread, evsel,
2274 PERF_RECORD_EXIT, stdout);
2275 perf_event__fprintf(event, stdout);
2276 }
ad7ebb9a
NK
2277
2278 if (perf_event__process_exit(tool, event, sample, machine) < 0)
b91fc39f 2279 err = -1;
ad7ebb9a 2280
b91fc39f
ACM
2281 thread__put(thread);
2282 return err;
ad7ebb9a
NK
2283}
2284
ba1ddf42
NK
2285static int process_mmap_event(struct perf_tool *tool,
2286 union perf_event *event,
2287 struct perf_sample *sample,
2288 struct machine *machine)
2289{
2290 struct thread *thread;
2291 struct perf_script *script = container_of(tool, struct perf_script, tool);
2292 struct perf_session *session = script->session;
32dcd021 2293 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ba1ddf42
NK
2294
2295 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2296 return -1;
2297
2298 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2299 if (thread == NULL) {
2300 pr_debug("problem processing MMAP event, skipping it.\n");
2301 return -1;
2302 }
2303
1fc632ce 2304 if (!evsel->core.attr.sample_id_all) {
ba1ddf42
NK
2305 sample->cpu = 0;
2306 sample->time = 0;
2307 sample->tid = event->mmap.tid;
2308 sample->pid = event->mmap.pid;
2309 }
e87e5481
AK
2310 if (!filter_cpu(sample)) {
2311 perf_sample__fprintf_start(sample, thread, evsel,
2312 PERF_RECORD_MMAP, stdout);
2313 perf_event__fprintf(event, stdout);
2314 }
b91fc39f 2315 thread__put(thread);
ba1ddf42
NK
2316 return 0;
2317}
2318
2319static int process_mmap2_event(struct perf_tool *tool,
2320 union perf_event *event,
2321 struct perf_sample *sample,
2322 struct machine *machine)
2323{
2324 struct thread *thread;
2325 struct perf_script *script = container_of(tool, struct perf_script, tool);
2326 struct perf_session *session = script->session;
32dcd021 2327 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
ba1ddf42
NK
2328
2329 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2330 return -1;
2331
2332 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2333 if (thread == NULL) {
2334 pr_debug("problem processing MMAP2 event, skipping it.\n");
2335 return -1;
2336 }
2337
1fc632ce 2338 if (!evsel->core.attr.sample_id_all) {
ba1ddf42
NK
2339 sample->cpu = 0;
2340 sample->time = 0;
2341 sample->tid = event->mmap2.tid;
2342 sample->pid = event->mmap2.pid;
2343 }
e87e5481
AK
2344 if (!filter_cpu(sample)) {
2345 perf_sample__fprintf_start(sample, thread, evsel,
2346 PERF_RECORD_MMAP2, stdout);
2347 perf_event__fprintf(event, stdout);
2348 }
b91fc39f 2349 thread__put(thread);
ba1ddf42
NK
2350 return 0;
2351}
2352
7c14898b
AH
2353static int process_switch_event(struct perf_tool *tool,
2354 union perf_event *event,
2355 struct perf_sample *sample,
2356 struct machine *machine)
2357{
2358 struct thread *thread;
2359 struct perf_script *script = container_of(tool, struct perf_script, tool);
2360 struct perf_session *session = script->session;
32dcd021 2361 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
7c14898b
AH
2362
2363 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2364 return -1;
2365
5bf83c29
AH
2366 if (scripting_ops && scripting_ops->process_switch)
2367 scripting_ops->process_switch(event, sample, machine);
2368
2369 if (!script->show_switch_events)
2370 return 0;
2371
7c14898b
AH
2372 thread = machine__findnew_thread(machine, sample->pid,
2373 sample->tid);
2374 if (thread == NULL) {
2375 pr_debug("problem processing SWITCH event, skipping it.\n");
2376 return -1;
2377 }
2378
e87e5481
AK
2379 if (!filter_cpu(sample)) {
2380 perf_sample__fprintf_start(sample, thread, evsel,
2381 PERF_RECORD_SWITCH, stdout);
2382 perf_event__fprintf(event, stdout);
2383 }
7c14898b
AH
2384 thread__put(thread);
2385 return 0;
2386}
2387
3d7c27b6
JO
2388static int
2389process_lost_event(struct perf_tool *tool,
2390 union perf_event *event,
2391 struct perf_sample *sample,
2392 struct machine *machine)
2393{
2394 struct perf_script *script = container_of(tool, struct perf_script, tool);
2395 struct perf_session *session = script->session;
32dcd021 2396 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
3d7c27b6
JO
2397 struct thread *thread;
2398
2399 thread = machine__findnew_thread(machine, sample->pid,
2400 sample->tid);
2401 if (thread == NULL)
2402 return -1;
2403
e87e5481
AK
2404 if (!filter_cpu(sample)) {
2405 perf_sample__fprintf_start(sample, thread, evsel,
2406 PERF_RECORD_LOST, stdout);
2407 perf_event__fprintf(event, stdout);
2408 }
3d7c27b6
JO
2409 thread__put(thread);
2410 return 0;
2411}
2412
3233b37a
JO
2413static int
2414process_finished_round_event(struct perf_tool *tool __maybe_unused,
2415 union perf_event *event,
2416 struct ordered_events *oe __maybe_unused)
2417
2418{
2419 perf_event__fprintf(event, stdout);
2420 return 0;
2421}
2422
490c8cc9
JO
2423static int
2424process_bpf_events(struct perf_tool *tool __maybe_unused,
2425 union perf_event *event,
2426 struct perf_sample *sample,
2427 struct machine *machine)
2428{
2429 struct thread *thread;
2430 struct perf_script *script = container_of(tool, struct perf_script, tool);
2431 struct perf_session *session = script->session;
32dcd021 2432 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
490c8cc9
JO
2433
2434 if (machine__process_ksymbol(machine, event, sample) < 0)
2435 return -1;
2436
1fc632ce 2437 if (!evsel->core.attr.sample_id_all) {
490c8cc9
JO
2438 perf_event__fprintf(event, stdout);
2439 return 0;
2440 }
2441
2442 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2443 if (thread == NULL) {
2444 pr_debug("problem processing MMAP event, skipping it.\n");
2445 return -1;
2446 }
2447
2448 if (!filter_cpu(sample)) {
2449 perf_sample__fprintf_start(sample, thread, evsel,
2450 event->header.type, stdout);
2451 perf_event__fprintf(event, stdout);
2452 }
2453
2454 thread__put(thread);
2455 return 0;
2456}
2457
1d037ca1 2458static void sig_handler(int sig __maybe_unused)
c239da3b
TZ
2459{
2460 session_done = 1;
2461}
2462
a14390fd
ACM
2463static void perf_script__fclose_per_event_dump(struct perf_script *script)
2464{
63503dba 2465 struct evlist *evlist = script->session->evlist;
32dcd021 2466 struct evsel *evsel;
a14390fd
ACM
2467
2468 evlist__for_each_entry(evlist, evsel) {
2469 if (!evsel->priv)
2470 break;
642ee1c6 2471 perf_evsel_script__delete(evsel->priv);
a14390fd
ACM
2472 evsel->priv = NULL;
2473 }
2474}
2475
2476static int perf_script__fopen_per_event_dump(struct perf_script *script)
2477{
32dcd021 2478 struct evsel *evsel;
a14390fd
ACM
2479
2480 evlist__for_each_entry(script->session->evlist, evsel) {
fa48c892
ACM
2481 /*
2482 * Already setup? I.e. we may be called twice in cases like
2483 * Intel PT, one for the intel_pt// and dummy events, then
2484 * for the evsels syntheized from the auxtrace info.
2485 *
2486 * Ses perf_script__process_auxtrace_info.
2487 */
2488 if (evsel->priv != NULL)
2489 continue;
2490
8ceb41d7 2491 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
a14390fd
ACM
2492 if (evsel->priv == NULL)
2493 goto out_err_fclose;
2494 }
2495
2496 return 0;
2497
2498out_err_fclose:
2499 perf_script__fclose_per_event_dump(script);
2500 return -1;
2501}
2502
2503static int perf_script__setup_per_event_dump(struct perf_script *script)
2504{
32dcd021
JO
2505 struct evsel *evsel;
2506 static struct evsel_script es_stdout;
a14390fd
ACM
2507
2508 if (script->per_event_dump)
2509 return perf_script__fopen_per_event_dump(script);
2510
642ee1c6
ACM
2511 es_stdout.fp = stdout;
2512
a14390fd 2513 evlist__for_each_entry(script->session->evlist, evsel)
642ee1c6 2514 evsel->priv = &es_stdout;
a14390fd
ACM
2515
2516 return 0;
2517}
2518
642ee1c6
ACM
2519static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2520{
32dcd021 2521 struct evsel *evsel;
642ee1c6
ACM
2522
2523 evlist__for_each_entry(script->session->evlist, evsel) {
32dcd021 2524 struct evsel_script *es = evsel->priv;
642ee1c6
ACM
2525
2526 perf_evsel_script__fprintf(es, stdout);
2527 perf_evsel_script__delete(es);
2528 evsel->priv = NULL;
2529 }
2530}
2531
6f3e5eda 2532static int __cmd_script(struct perf_script *script)
5f9c39dc 2533{
6fcf7ddb
FW
2534 int ret;
2535
c239da3b
TZ
2536 signal(SIGINT, sig_handler);
2537
8bf8c6da
TJ
2538 perf_stat__init_shadow_stats();
2539
ad7ebb9a
NK
2540 /* override event processing functions */
2541 if (script->show_task_events) {
2542 script->tool.comm = process_comm_event;
2543 script->tool.fork = process_fork_event;
2544 script->tool.exit = process_exit_event;
2545 }
ba1ddf42
NK
2546 if (script->show_mmap_events) {
2547 script->tool.mmap = process_mmap_event;
2548 script->tool.mmap2 = process_mmap2_event;
2549 }
5bf83c29 2550 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
7c14898b 2551 script->tool.context_switch = process_switch_event;
96a44bbc
HB
2552 if (script->show_namespace_events)
2553 script->tool.namespaces = process_namespaces_event;
3d7c27b6
JO
2554 if (script->show_lost_events)
2555 script->tool.lost = process_lost_event;
3233b37a
JO
2556 if (script->show_round_events) {
2557 script->tool.ordered_events = false;
2558 script->tool.finished_round = process_finished_round_event;
2559 }
490c8cc9 2560 if (script->show_bpf_events) {
3f604b5f
ACM
2561 script->tool.ksymbol = process_bpf_events;
2562 script->tool.bpf = process_bpf_events;
490c8cc9 2563 }
ad7ebb9a 2564
a14390fd
ACM
2565 if (perf_script__setup_per_event_dump(script)) {
2566 pr_err("Couldn't create the per event dump files\n");
2567 return -1;
2568 }
2569
b7b61cbe 2570 ret = perf_session__process_events(script->session);
6fcf7ddb 2571
a14390fd 2572 if (script->per_event_dump)
642ee1c6 2573 perf_script__exit_per_event_dump_stats(script);
a14390fd 2574
6d8afb56 2575 if (debug_mode)
9486aa38 2576 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
6fcf7ddb
FW
2577
2578 return ret;
5f9c39dc
FW
2579}
2580
956ffd02
TZ
2581struct script_spec {
2582 struct list_head node;
2583 struct scripting_ops *ops;
2584 char spec[0];
2585};
2586
eccdfe2d 2587static LIST_HEAD(script_specs);
956ffd02
TZ
2588
2589static struct script_spec *script_spec__new(const char *spec,
2590 struct scripting_ops *ops)
2591{
2592 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2593
2594 if (s != NULL) {
2595 strcpy(s->spec, spec);
2596 s->ops = ops;
2597 }
2598
2599 return s;
2600}
2601
956ffd02
TZ
2602static void script_spec__add(struct script_spec *s)
2603{
2604 list_add_tail(&s->node, &script_specs);
2605}
2606
2607static struct script_spec *script_spec__find(const char *spec)
2608{
2609 struct script_spec *s;
2610
2611 list_for_each_entry(s, &script_specs, node)
2612 if (strcasecmp(s->spec, spec) == 0)
2613 return s;
2614 return NULL;
2615}
2616
956ffd02
TZ
2617int script_spec_register(const char *spec, struct scripting_ops *ops)
2618{
2619 struct script_spec *s;
2620
2621 s = script_spec__find(spec);
2622 if (s)
2623 return -1;
2624
8560bae0 2625 s = script_spec__new(spec, ops);
956ffd02
TZ
2626 if (!s)
2627 return -1;
8560bae0
TS
2628 else
2629 script_spec__add(s);
956ffd02
TZ
2630
2631 return 0;
2632}
2633
2634static struct scripting_ops *script_spec__lookup(const char *spec)
2635{
2636 struct script_spec *s = script_spec__find(spec);
2637 if (!s)
2638 return NULL;
2639
2640 return s->ops;
2641}
2642
2643static void list_available_languages(void)
2644{
2645 struct script_spec *s;
2646
2647 fprintf(stderr, "\n");
2648 fprintf(stderr, "Scripting language extensions (used in "
133dc4c3 2649 "perf script -s [spec:]script.[spec]):\n\n");
956ffd02
TZ
2650
2651 list_for_each_entry(s, &script_specs, node)
2652 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2653
2654 fprintf(stderr, "\n");
2655}
2656
1d037ca1
IT
2657static int parse_scriptname(const struct option *opt __maybe_unused,
2658 const char *str, int unset __maybe_unused)
956ffd02
TZ
2659{
2660 char spec[PATH_MAX];
2661 const char *script, *ext;
2662 int len;
2663
f526d68b 2664 if (strcmp(str, "lang") == 0) {
956ffd02 2665 list_available_languages();
f526d68b 2666 exit(0);
956ffd02
TZ
2667 }
2668
2669 script = strchr(str, ':');
2670 if (script) {
2671 len = script - str;
2672 if (len >= PATH_MAX) {
2673 fprintf(stderr, "invalid language specifier");
2674 return -1;
2675 }
2676 strncpy(spec, str, len);
2677 spec[len] = '\0';
2678 scripting_ops = script_spec__lookup(spec);
2679 if (!scripting_ops) {
2680 fprintf(stderr, "invalid language specifier");
2681 return -1;
2682 }
2683 script++;
2684 } else {
2685 script = str;
d1e95bb5 2686 ext = strrchr(script, '.');
956ffd02
TZ
2687 if (!ext) {
2688 fprintf(stderr, "invalid script extension");
2689 return -1;
2690 }
2691 scripting_ops = script_spec__lookup(++ext);
2692 if (!scripting_ops) {
2693 fprintf(stderr, "invalid script extension");
2694 return -1;
2695 }
2696 }
2697
2698 script_name = strdup(script);
2699
2700 return 0;
2701}
2702
1d037ca1
IT
2703static int parse_output_fields(const struct option *opt __maybe_unused,
2704 const char *arg, int unset __maybe_unused)
745f43e3 2705{
49346e85 2706 char *tok, *strtok_saveptr = NULL;
50ca19ae 2707 int i, imax = ARRAY_SIZE(all_output_options);
2c9e45f7 2708 int j;
745f43e3
DA
2709 int rc = 0;
2710 char *str = strdup(arg);
1424dc96 2711 int type = -1;
36ce5651 2712 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
745f43e3
DA
2713
2714 if (!str)
2715 return -ENOMEM;
2716
2c9e45f7
DA
2717 /* first word can state for which event type the user is specifying
2718 * the fields. If no type exists, the specified fields apply to all
2719 * event types found in the file minus the invalid fields for a type.
1424dc96 2720 */
2c9e45f7
DA
2721 tok = strchr(str, ':');
2722 if (tok) {
2723 *tok = '\0';
2724 tok++;
2725 if (!strcmp(str, "hw"))
2726 type = PERF_TYPE_HARDWARE;
2727 else if (!strcmp(str, "sw"))
2728 type = PERF_TYPE_SOFTWARE;
2729 else if (!strcmp(str, "trace"))
2730 type = PERF_TYPE_TRACEPOINT;
0817a6a3
AS
2731 else if (!strcmp(str, "raw"))
2732 type = PERF_TYPE_RAW;
27cfef00
WN
2733 else if (!strcmp(str, "break"))
2734 type = PERF_TYPE_BREAKPOINT;
1405720d
AH
2735 else if (!strcmp(str, "synth"))
2736 type = OUTPUT_TYPE_SYNTH;
2c9e45f7
DA
2737 else {
2738 fprintf(stderr, "Invalid event type in field string.\n");
38efb539
RR
2739 rc = -EINVAL;
2740 goto out;
2c9e45f7
DA
2741 }
2742
2743 if (output[type].user_set)
2744 pr_warning("Overriding previous field request for %s events.\n",
2745 event_type(type));
2746
6ef362fd
JO
2747 /* Don't override defaults for +- */
2748 if (strchr(tok, '+') || strchr(tok, '-'))
2749 goto parse;
2750
2c9e45f7
DA
2751 output[type].fields = 0;
2752 output[type].user_set = true;
9cbdb702 2753 output[type].wildcard_set = false;
2c9e45f7
DA
2754
2755 } else {
2756 tok = str;
2757 if (strlen(str) == 0) {
2758 fprintf(stderr,
2759 "Cannot set fields to 'none' for all event types.\n");
2760 rc = -EINVAL;
2761 goto out;
2762 }
2763
36ce5651
AK
2764 /* Don't override defaults for +- */
2765 if (strchr(str, '+') || strchr(str, '-'))
2766 goto parse;
2767
2c9e45f7
DA
2768 if (output_set_by_user())
2769 pr_warning("Overriding previous field request for all events.\n");
2770
1405720d 2771 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2c9e45f7
DA
2772 output[j].fields = 0;
2773 output[j].user_set = true;
9cbdb702 2774 output[j].wildcard_set = true;
2c9e45f7 2775 }
745f43e3
DA
2776 }
2777
36ce5651 2778parse:
49346e85 2779 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
36ce5651
AK
2780 if (*tok == '+') {
2781 if (change == SET)
2782 goto out_badmix;
2783 change = ADD;
2784 tok++;
2785 } else if (*tok == '-') {
2786 if (change == SET)
2787 goto out_badmix;
2788 change = REMOVE;
2789 tok++;
2790 } else {
2791 if (change != SET && change != DEFAULT)
2792 goto out_badmix;
2793 change = SET;
2794 }
2795
745f43e3 2796 for (i = 0; i < imax; ++i) {
2c9e45f7 2797 if (strcmp(tok, all_output_options[i].str) == 0)
745f43e3 2798 break;
745f43e3 2799 }
400ea6d3 2800 if (i == imax && strcmp(tok, "flags") == 0) {
36ce5651 2801 print_flags = change == REMOVE ? false : true;
400ea6d3
AH
2802 continue;
2803 }
745f43e3 2804 if (i == imax) {
2c9e45f7 2805 fprintf(stderr, "Invalid field requested.\n");
745f43e3 2806 rc = -EINVAL;
2c9e45f7 2807 goto out;
745f43e3
DA
2808 }
2809
2c9e45f7
DA
2810 if (type == -1) {
2811 /* add user option to all events types for
2812 * which it is valid
2813 */
1405720d 2814 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2c9e45f7
DA
2815 if (output[j].invalid_fields & all_output_options[i].field) {
2816 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2817 all_output_options[i].str, event_type(j));
36ce5651 2818 } else {
4b6ac811 2819 if (change == REMOVE) {
36ce5651 2820 output[j].fields &= ~all_output_options[i].field;
4b6ac811
AK
2821 output[j].user_set_fields &= ~all_output_options[i].field;
2822 } else {
36ce5651 2823 output[j].fields |= all_output_options[i].field;
4b6ac811
AK
2824 output[j].user_set_fields |= all_output_options[i].field;
2825 }
37fed3de
AK
2826 output[j].user_set = true;
2827 output[j].wildcard_set = true;
36ce5651 2828 }
2c9e45f7
DA
2829 }
2830 } else {
2831 if (output[type].invalid_fields & all_output_options[i].field) {
2832 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2833 all_output_options[i].str, event_type(type));
2834
2835 rc = -EINVAL;
2836 goto out;
2837 }
6ef362fd
JO
2838 if (change == REMOVE)
2839 output[type].fields &= ~all_output_options[i].field;
2840 else
2841 output[type].fields |= all_output_options[i].field;
37fed3de
AK
2842 output[type].user_set = true;
2843 output[type].wildcard_set = true;
2c9e45f7 2844 }
745f43e3
DA
2845 }
2846
2c9e45f7
DA
2847 if (type >= 0) {
2848 if (output[type].fields == 0) {
2849 pr_debug("No fields requested for %s type. "
2850 "Events will not be displayed.\n", event_type(type));
2851 }
2852 }
36ce5651 2853 goto out;
745f43e3 2854
36ce5651
AK
2855out_badmix:
2856 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2857 rc = -EINVAL;
2c9e45f7 2858out:
745f43e3
DA
2859 free(str);
2860 return rc;
2861}
2862
a5e8e825
ACM
2863#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2864 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2865 if ((lang_dirent->d_type == DT_DIR || \
2866 (lang_dirent->d_type == DT_UNKNOWN && \
2867 is_directory(scripts_path, lang_dirent))) && \
2868 (strcmp(lang_dirent->d_name, ".")) && \
2869 (strcmp(lang_dirent->d_name, "..")))
2870
2871#define for_each_script(lang_path, lang_dir, script_dirent) \
2872 while ((script_dirent = readdir(lang_dir)) != NULL) \
2873 if (script_dirent->d_type != DT_DIR && \
2874 (script_dirent->d_type != DT_UNKNOWN || \
2875 !is_directory(lang_path, script_dirent)))
4b9c0c59
TZ
2876
2877
2878#define RECORD_SUFFIX "-record"
2879#define REPORT_SUFFIX "-report"
2880
2881struct script_desc {
2882 struct list_head node;
2883 char *name;
2884 char *half_liner;
2885 char *args;
2886};
2887
eccdfe2d 2888static LIST_HEAD(script_descs);
4b9c0c59
TZ
2889
2890static struct script_desc *script_desc__new(const char *name)
2891{
2892 struct script_desc *s = zalloc(sizeof(*s));
2893
b5b87312 2894 if (s != NULL && name)
4b9c0c59
TZ
2895 s->name = strdup(name);
2896
2897 return s;
2898}
2899
2900static void script_desc__delete(struct script_desc *s)
2901{
74cf249d
ACM
2902 zfree(&s->name);
2903 zfree(&s->half_liner);
2904 zfree(&s->args);
4b9c0c59
TZ
2905 free(s);
2906}
2907
2908static void script_desc__add(struct script_desc *s)
2909{
2910 list_add_tail(&s->node, &script_descs);
2911}
2912
2913static struct script_desc *script_desc__find(const char *name)
2914{
2915 struct script_desc *s;
2916
2917 list_for_each_entry(s, &script_descs, node)
2918 if (strcasecmp(s->name, name) == 0)
2919 return s;
2920 return NULL;
2921}
2922
2923static struct script_desc *script_desc__findnew(const char *name)
2924{
2925 struct script_desc *s = script_desc__find(name);
2926
2927 if (s)
2928 return s;
2929
2930 s = script_desc__new(name);
2931 if (!s)
2ec5cab6 2932 return NULL;
4b9c0c59
TZ
2933
2934 script_desc__add(s);
2935
2936 return s;
4b9c0c59
TZ
2937}
2938
965bb6be 2939static const char *ends_with(const char *str, const char *suffix)
4b9c0c59
TZ
2940{
2941 size_t suffix_len = strlen(suffix);
965bb6be 2942 const char *p = str;
4b9c0c59
TZ
2943
2944 if (strlen(str) > suffix_len) {
2945 p = str + strlen(str) - suffix_len;
2946 if (!strncmp(p, suffix, suffix_len))
2947 return p;
2948 }
2949
2950 return NULL;
2951}
2952
4b9c0c59
TZ
2953static int read_script_info(struct script_desc *desc, const char *filename)
2954{
2955 char line[BUFSIZ], *p;
2956 FILE *fp;
2957
2958 fp = fopen(filename, "r");
2959 if (!fp)
2960 return -1;
2961
2962 while (fgets(line, sizeof(line), fp)) {
32858480 2963 p = skip_spaces(line);
4b9c0c59
TZ
2964 if (strlen(p) == 0)
2965 continue;
2966 if (*p != '#')
2967 continue;
2968 p++;
2969 if (strlen(p) && *p == '!')
2970 continue;
2971
32858480 2972 p = skip_spaces(p);
4b9c0c59
TZ
2973 if (strlen(p) && p[strlen(p) - 1] == '\n')
2974 p[strlen(p) - 1] = '\0';
2975
2976 if (!strncmp(p, "description:", strlen("description:"))) {
2977 p += strlen("description:");
32858480 2978 desc->half_liner = strdup(skip_spaces(p));
4b9c0c59
TZ
2979 continue;
2980 }
2981
2982 if (!strncmp(p, "args:", strlen("args:"))) {
2983 p += strlen("args:");
32858480 2984 desc->args = strdup(skip_spaces(p));
4b9c0c59
TZ
2985 continue;
2986 }
2987 }
2988
2989 fclose(fp);
2990
2991 return 0;
2992}
2993
38efb539
RR
2994static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2995{
2996 char *script_root, *str;
2997
2998 script_root = strdup(script_dirent->d_name);
2999 if (!script_root)
3000 return NULL;
3001
3002 str = (char *)ends_with(script_root, suffix);
3003 if (!str) {
3004 free(script_root);
3005 return NULL;
3006 }
3007
3008 *str = '\0';
3009 return script_root;
3010}
3011
1d037ca1
IT
3012static int list_available_scripts(const struct option *opt __maybe_unused,
3013 const char *s __maybe_unused,
3014 int unset __maybe_unused)
4b9c0c59 3015{
a5e8e825 3016 struct dirent *script_dirent, *lang_dirent;
4b9c0c59
TZ
3017 char scripts_path[MAXPATHLEN];
3018 DIR *scripts_dir, *lang_dir;
3019 char script_path[MAXPATHLEN];
3020 char lang_path[MAXPATHLEN];
3021 struct script_desc *desc;
3022 char first_half[BUFSIZ];
3023 char *script_root;
4b9c0c59 3024
46113a54 3025 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
4b9c0c59
TZ
3026
3027 scripts_dir = opendir(scripts_path);
88ded4d8
HK
3028 if (!scripts_dir) {
3029 fprintf(stdout,
3030 "open(%s) failed.\n"
3031 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
3032 scripts_path);
3033 exit(-1);
3034 }
4b9c0c59 3035
a5e8e825 3036 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
77f18153
JO
3037 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3038 lang_dirent->d_name);
4b9c0c59
TZ
3039 lang_dir = opendir(lang_path);
3040 if (!lang_dir)
3041 continue;
3042
a5e8e825
ACM
3043 for_each_script(lang_path, lang_dir, script_dirent) {
3044 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
38efb539 3045 if (script_root) {
4b9c0c59 3046 desc = script_desc__findnew(script_root);
77f18153
JO
3047 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3048 lang_path, script_dirent->d_name);
4b9c0c59 3049 read_script_info(desc, script_path);
38efb539 3050 free(script_root);
4b9c0c59 3051 }
4b9c0c59
TZ
3052 }
3053 }
3054
3055 fprintf(stdout, "List of available trace scripts:\n");
3056 list_for_each_entry(desc, &script_descs, node) {
3057 sprintf(first_half, "%s %s", desc->name,
3058 desc->args ? desc->args : "");
3059 fprintf(stdout, " %-36s %s\n", first_half,
3060 desc->half_liner ? desc->half_liner : "");
3061 }
3062
3063 exit(0);
3064}
3065
49e639e2
FT
3066/*
3067 * Some scripts specify the required events in their "xxx-record" file,
3068 * this function will check if the events in perf.data match those
3069 * mentioned in the "xxx-record".
3070 *
3071 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3072 * which is covered well now. And new parsing code should be added to
3073 * cover the future complexing formats like event groups etc.
3074 */
3075static int check_ev_match(char *dir_name, char *scriptname,
3076 struct perf_session *session)
3077{
3078 char filename[MAXPATHLEN], evname[128];
3079 char line[BUFSIZ], *p;
32dcd021 3080 struct evsel *pos;
49e639e2
FT
3081 int match, len;
3082 FILE *fp;
3083
77f18153 3084 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
49e639e2
FT
3085
3086 fp = fopen(filename, "r");
3087 if (!fp)
3088 return -1;
3089
3090 while (fgets(line, sizeof(line), fp)) {
32858480 3091 p = skip_spaces(line);
49e639e2
FT
3092 if (*p == '#')
3093 continue;
3094
3095 while (strlen(p)) {
3096 p = strstr(p, "-e");
3097 if (!p)
3098 break;
3099
3100 p += 2;
32858480 3101 p = skip_spaces(p);
49e639e2
FT
3102 len = strcspn(p, " \t");
3103 if (!len)
3104 break;
3105
3106 snprintf(evname, len + 1, "%s", p);
3107
3108 match = 0;
e5cadb93 3109 evlist__for_each_entry(session->evlist, pos) {
49e639e2
FT
3110 if (!strcmp(perf_evsel__name(pos), evname)) {
3111 match = 1;
3112 break;
3113 }
3114 }
3115
3116 if (!match) {
3117 fclose(fp);
3118 return -1;
3119 }
3120 }
3121 }
3122
3123 fclose(fp);
3124 return 0;
3125}
3126
e5f3705e
FT
3127/*
3128 * Return -1 if none is found, otherwise the actual scripts number.
3129 *
3130 * Currently the only user of this function is the script browser, which
3131 * will list all statically runnable scripts, select one, execute it and
3132 * show the output in a perf browser.
3133 */
905e4aff
AK
3134int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3135 int pathlen)
e5f3705e 3136{
a5e8e825 3137 struct dirent *script_dirent, *lang_dirent;
49e639e2 3138 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
e5f3705e 3139 DIR *scripts_dir, *lang_dir;
49e639e2 3140 struct perf_session *session;
8ceb41d7 3141 struct perf_data data = {
2d4f2799
JO
3142 .path = input_name,
3143 .mode = PERF_DATA_MODE_READ,
f5fc1412 3144 };
e5f3705e
FT
3145 char *temp;
3146 int i = 0;
3147
8ceb41d7 3148 session = perf_session__new(&data, false, NULL);
6ef81c55
MI
3149 if (IS_ERR(session))
3150 return PTR_ERR(session);
49e639e2 3151
46113a54 3152 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
e5f3705e
FT
3153
3154 scripts_dir = opendir(scripts_path);
49e639e2
FT
3155 if (!scripts_dir) {
3156 perf_session__delete(session);
e5f3705e 3157 return -1;
49e639e2 3158 }
e5f3705e 3159
a5e8e825 3160 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
77f18153
JO
3161 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3162 lang_dirent->d_name);
90ce61b9 3163#ifndef HAVE_LIBPERL_SUPPORT
e5f3705e
FT
3164 if (strstr(lang_path, "perl"))
3165 continue;
3166#endif
90ce61b9 3167#ifndef HAVE_LIBPYTHON_SUPPORT
e5f3705e
FT
3168 if (strstr(lang_path, "python"))
3169 continue;
3170#endif
3171
3172 lang_dir = opendir(lang_path);
3173 if (!lang_dir)
3174 continue;
3175
a5e8e825 3176 for_each_script(lang_path, lang_dir, script_dirent) {
e5f3705e 3177 /* Skip those real time scripts: xxxtop.p[yl] */
a5e8e825 3178 if (strstr(script_dirent->d_name, "top."))
e5f3705e 3179 continue;
905e4aff
AK
3180 if (i >= num)
3181 break;
3182 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3183 lang_path,
a5e8e825
ACM
3184 script_dirent->d_name);
3185 temp = strchr(script_dirent->d_name, '.');
e5f3705e 3186 snprintf(scripts_array[i],
a5e8e825
ACM
3187 (temp - script_dirent->d_name) + 1,
3188 "%s", script_dirent->d_name);
49e639e2
FT
3189
3190 if (check_ev_match(lang_path,
3191 scripts_array[i], session))
3192 continue;
3193
e5f3705e
FT
3194 i++;
3195 }
49e639e2 3196 closedir(lang_dir);
e5f3705e
FT
3197 }
3198
49e639e2
FT
3199 closedir(scripts_dir);
3200 perf_session__delete(session);
e5f3705e
FT
3201 return i;
3202}
3203
3875294f
TZ
3204static char *get_script_path(const char *script_root, const char *suffix)
3205{
a5e8e825 3206 struct dirent *script_dirent, *lang_dirent;
3875294f
TZ
3207 char scripts_path[MAXPATHLEN];
3208 char script_path[MAXPATHLEN];
3209 DIR *scripts_dir, *lang_dir;
3210 char lang_path[MAXPATHLEN];
38efb539 3211 char *__script_root;
3875294f 3212
46113a54 3213 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3875294f
TZ
3214
3215 scripts_dir = opendir(scripts_path);
3216 if (!scripts_dir)
3217 return NULL;
3218
a5e8e825 3219 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
77f18153
JO
3220 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3221 lang_dirent->d_name);
3875294f
TZ
3222 lang_dir = opendir(lang_path);
3223 if (!lang_dir)
3224 continue;
3225
a5e8e825
ACM
3226 for_each_script(lang_path, lang_dir, script_dirent) {
3227 __script_root = get_script_root(script_dirent, suffix);
38efb539
RR
3228 if (__script_root && !strcmp(script_root, __script_root)) {
3229 free(__script_root);
946ef2a2
NK
3230 closedir(lang_dir);
3231 closedir(scripts_dir);
77f18153
JO
3232 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3233 lang_path, script_dirent->d_name);
38efb539 3234 return strdup(script_path);
3875294f
TZ
3235 }
3236 free(__script_root);
3237 }
946ef2a2 3238 closedir(lang_dir);
3875294f 3239 }
946ef2a2 3240 closedir(scripts_dir);
3875294f 3241
38efb539 3242 return NULL;
3875294f
TZ
3243}
3244
b5b87312
TZ
3245static bool is_top_script(const char *script_path)
3246{
965bb6be 3247 return ends_with(script_path, "top") == NULL ? false : true;
b5b87312
TZ
3248}
3249
3250static int has_required_arg(char *script_path)
3251{
3252 struct script_desc *desc;
3253 int n_args = 0;
3254 char *p;
3255
3256 desc = script_desc__new(NULL);
3257
3258 if (read_script_info(desc, script_path))
3259 goto out;
3260
3261 if (!desc->args)
3262 goto out;
3263
3264 for (p = desc->args; *p; p++)
3265 if (*p == '<')
3266 n_args++;
3267out:
3268 script_desc__delete(desc);
3269
3270 return n_args;
3271}
3272
69b6470e
ACM
3273static int have_cmd(int argc, const char **argv)
3274{
3275 char **__argv = malloc(sizeof(const char *) * argc);
3276
3277 if (!__argv) {
3278 pr_err("malloc failed\n");
3279 return -1;
3280 }
3281
3282 memcpy(__argv, argv, sizeof(const char *) * argc);
3283 argc = parse_options(argc, (const char **)__argv, record_options,
3284 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3285 free(__argv);
5f9c39dc 3286
69b6470e
ACM
3287 system_wide = (argc == 0);
3288
3289 return 0;
3290}
3291
7322d6c9
JO
3292static void script__setup_sample_type(struct perf_script *script)
3293{
3294 struct perf_session *session = script->session;
3295 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3296
3297 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3298 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
eabad8c6 3299 (sample_type & PERF_SAMPLE_STACK_USER)) {
7322d6c9 3300 callchain_param.record_mode = CALLCHAIN_DWARF;
eabad8c6
ACM
3301 dwarf_callchain_users = true;
3302 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
7322d6c9
JO
3303 callchain_param.record_mode = CALLCHAIN_LBR;
3304 else
3305 callchain_param.record_mode = CALLCHAIN_FP;
3306 }
3307}
3308
89f1688a
JO
3309static int process_stat_round_event(struct perf_session *session,
3310 union perf_event *event)
e099eba8 3311{
72932371 3312 struct perf_record_stat_round *round = &event->stat_round;
32dcd021 3313 struct evsel *counter;
e099eba8 3314
e5cadb93 3315 evlist__for_each_entry(session->evlist, counter) {
e099eba8
JO
3316 perf_stat_process_counter(&stat_config, counter);
3317 process_stat(counter, round->time);
3318 }
3319
3320 process_stat_interval(round->time);
3321 return 0;
3322}
3323
89f1688a
JO
3324static int process_stat_config_event(struct perf_session *session __maybe_unused,
3325 union perf_event *event)
91a2c3d5
JO
3326{
3327 perf_event__read_stat_config(&stat_config, &event->stat_config);
3328 return 0;
3329}
3330
cfc8874a
JO
3331static int set_maps(struct perf_script *script)
3332{
63503dba 3333 struct evlist *evlist = script->session->evlist;
cfc8874a
JO
3334
3335 if (!script->cpus || !script->threads)
3336 return 0;
3337
3338 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3339 return -EINVAL;
3340
453fa030 3341 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
cfc8874a
JO
3342
3343 if (perf_evlist__alloc_stats(evlist, true))
3344 return -ENOMEM;
3345
3346 script->allocated = true;
3347 return 0;
3348}
3349
3350static
89f1688a
JO
3351int process_thread_map_event(struct perf_session *session,
3352 union perf_event *event)
cfc8874a 3353{
89f1688a 3354 struct perf_tool *tool = session->tool;
cfc8874a
JO
3355 struct perf_script *script = container_of(tool, struct perf_script, tool);
3356
3357 if (script->threads) {
3358 pr_warning("Extra thread map event, ignoring.\n");
3359 return 0;
3360 }
3361
3362 script->threads = thread_map__new_event(&event->thread_map);
3363 if (!script->threads)
3364 return -ENOMEM;
3365
3366 return set_maps(script);
3367}
3368
3369static
89f1688a
JO
3370int process_cpu_map_event(struct perf_session *session,
3371 union perf_event *event)
cfc8874a 3372{
89f1688a 3373 struct perf_tool *tool = session->tool;
cfc8874a
JO
3374 struct perf_script *script = container_of(tool, struct perf_script, tool);
3375
3376 if (script->cpus) {
3377 pr_warning("Extra cpu map event, ignoring.\n");
3378 return 0;
3379 }
3380
3381 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3382 if (!script->cpus)
3383 return -ENOMEM;
3384
3385 return set_maps(script);
3386}
3387
89f1688a
JO
3388static int process_feature_event(struct perf_session *session,
3389 union perf_event *event)
92ead7ee
RB
3390{
3391 if (event->feat.feat_id < HEADER_LAST_FEATURE)
89f1688a 3392 return perf_event__process_feature(session, event);
92ead7ee
RB
3393 return 0;
3394}
3395
fa48c892 3396#ifdef HAVE_AUXTRACE_SUPPORT
89f1688a
JO
3397static int perf_script__process_auxtrace_info(struct perf_session *session,
3398 union perf_event *event)
fa48c892 3399{
89f1688a
JO
3400 struct perf_tool *tool = session->tool;
3401
3402 int ret = perf_event__process_auxtrace_info(session, event);
fa48c892
ACM
3403
3404 if (ret == 0) {
3405 struct perf_script *script = container_of(tool, struct perf_script, tool);
3406
3407 ret = perf_script__setup_per_event_dump(script);
3408 }
3409
3410 return ret;
3411}
3412#else
3413#define perf_script__process_auxtrace_info 0
3414#endif
3415
b585ebdb
AK
3416static int parse_insn_trace(const struct option *opt __maybe_unused,
3417 const char *str __maybe_unused,
3418 int unset __maybe_unused)
3419{
3420 parse_output_fields(NULL, "+insn,-event,-period", 0);
3421 itrace_parse_synth_opts(opt, "i0ns", 0);
52bab886 3422 symbol_conf.nanosecs = true;
b585ebdb
AK
3423 return 0;
3424}
3425
3426static int parse_xed(const struct option *opt __maybe_unused,
3427 const char *str __maybe_unused,
3428 int unset __maybe_unused)
3429{
3430 force_pager("xed -F insn: -A -64 | less");
3431 return 0;
3432}
3433
d1b1552e
AK
3434static int parse_call_trace(const struct option *opt __maybe_unused,
3435 const char *str __maybe_unused,
3436 int unset __maybe_unused)
3437{
3438 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3439 itrace_parse_synth_opts(opt, "cewp", 0);
52bab886 3440 symbol_conf.nanosecs = true;
1c492422 3441 symbol_conf.pad_output_len_dso = 50;
d1b1552e
AK
3442 return 0;
3443}
3444
3445static int parse_callret_trace(const struct option *opt __maybe_unused,
3446 const char *str __maybe_unused,
3447 int unset __maybe_unused)
3448{
3449 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3450 itrace_parse_synth_opts(opt, "crewp", 0);
52bab886 3451 symbol_conf.nanosecs = true;
d1b1552e
AK
3452 return 0;
3453}
3454
b0ad8ea6 3455int cmd_script(int argc, const char **argv)
69b6470e
ACM
3456{
3457 bool show_full_info = false;
e90debdd
JO
3458 bool header = false;
3459 bool header_only = false;
6cc870f0 3460 bool script_started = false;
69b6470e
ACM
3461 char *rec_script_path = NULL;
3462 char *rep_script_path = NULL;
3463 struct perf_session *session;
4eb06815
AK
3464 struct itrace_synth_opts itrace_synth_opts = {
3465 .set = false,
3466 .default_no_sample = true,
3467 };
3ab481a1 3468 struct utsname uts;
69b6470e
ACM
3469 char *script_path = NULL;
3470 const char **__argv;
6cc870f0 3471 int i, j, err = 0;
6f3e5eda
AH
3472 struct perf_script script = {
3473 .tool = {
3474 .sample = process_sample_event,
3475 .mmap = perf_event__process_mmap,
3476 .mmap2 = perf_event__process_mmap2,
3477 .comm = perf_event__process_comm,
f3b3614a 3478 .namespaces = perf_event__process_namespaces,
6f3e5eda
AH
3479 .exit = perf_event__process_exit,
3480 .fork = perf_event__process_fork,
7ea95727 3481 .attr = process_attr,
91daee30 3482 .event_update = perf_event__process_event_update,
6f3e5eda 3483 .tracing_data = perf_event__process_tracing_data,
92ead7ee 3484 .feature = process_feature_event,
6f3e5eda 3485 .build_id = perf_event__process_build_id,
7a680eb9 3486 .id_index = perf_event__process_id_index,
fa48c892 3487 .auxtrace_info = perf_script__process_auxtrace_info,
7a680eb9
AH
3488 .auxtrace = perf_event__process_auxtrace,
3489 .auxtrace_error = perf_event__process_auxtrace_error,
e099eba8
JO
3490 .stat = perf_event__process_stat_event,
3491 .stat_round = process_stat_round_event,
91a2c3d5 3492 .stat_config = process_stat_config_event,
cfc8874a
JO
3493 .thread_map = process_thread_map_event,
3494 .cpu_map = process_cpu_map_event,
0a8cb85c 3495 .ordered_events = true,
6f3e5eda
AH
3496 .ordering_requires_timestamps = true,
3497 },
3498 };
8ceb41d7 3499 struct perf_data data = {
06af0f2c
YS
3500 .mode = PERF_DATA_MODE_READ,
3501 };
69b6470e 3502 const struct option options[] = {
5f9c39dc
FW
3503 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3504 "dump raw trace in ASCII"),
c0555642 3505 OPT_INCR('v', "verbose", &verbose,
69b6470e 3506 "be more verbose (show symbol address, etc)"),
4b9c0c59 3507 OPT_BOOLEAN('L', "Latency", &latency_format,
cda48461 3508 "show latency attributes (irqs/preemption disabled, etc)"),
4b9c0c59
TZ
3509 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3510 list_available_scripts),
956ffd02
TZ
3511 OPT_CALLBACK('s', "script", NULL, "name",
3512 "script file name (lang:script name, script name, or *)",
3513 parse_scriptname),
3514 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
133dc4c3 3515 "generate perf-script.xx script in specified language"),
69b6470e 3516 OPT_STRING('i', "input", &input_name, "file", "input file name"),
ffabd99e
FW
3517 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3518 "do various checks like samples ordering and lost events"),
e90debdd
JO
3519 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3520 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
c0230b2b
DA
3521 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3522 "file", "vmlinux pathname"),
3523 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3524 "file", "kallsyms pathname"),
3525 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3526 "When printing symbols do not display call chain"),
a7066709
HK
3527 OPT_CALLBACK(0, "symfs", NULL, "directory",
3528 "Look for files with symbols relative to this directory",
3529 symbol__config_symfs),
06af0f2c 3530 OPT_CALLBACK('F', "fields", NULL, "str",
a978f2ab 3531 "comma separated output fields prepend with 'type:'. "
36ce5651 3532 "+field to add and -field to remove."
1405720d 3533 "Valid types: hw,sw,trace,raw,synth. "
a978f2ab 3534 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
10e9cec9
RB
3535 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3536 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
68fb45bf 3537 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
48d02a1d 3538 parse_output_fields),
317df650 3539 OPT_BOOLEAN('a', "all-cpus", &system_wide,
69b6470e 3540 "system-wide collection from all CPUs"),
36385be5
FT
3541 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3542 "only consider these symbols"),
b585ebdb
AK
3543 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3544 "Decode instructions from itrace", parse_insn_trace),
3545 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3546 "Run xed disassembler on output", parse_xed),
d1b1552e
AK
3547 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3548 "Decode calls from from itrace", parse_call_trace),
3549 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3550 "Decode calls and returns from itrace", parse_callret_trace),
99f753f0
AK
3551 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3552 "Only print symbols and callees with --call-trace/--call-ret-trace"),
64eff7d9
DA
3553 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3554 "Stop display of callgraph at these symbols"),
c8e66720 3555 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
e7984b7b
DA
3556 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3557 "only display events for these comms"),
e03eaa40
DA
3558 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3559 "only consider symbols in these pids"),
3560 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3561 "only consider symbols in these tids"),
6125cc8d
ACM
3562 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3563 "Set the maximum stack depth when parsing the callchain, "
3564 "anything beyond the specified depth will be ignored. "
4cb93446 3565 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
90b10f47 3566 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
26567ed7 3567 OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
fbe96f29
SE
3568 OPT_BOOLEAN('I', "show-info", &show_full_info,
3569 "display extended information from perf.data file"),
0bc8d205
AN
3570 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3571 "Show the path of [kernel.kallsyms]"),
ad7ebb9a
NK
3572 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3573 "Show the fork/comm/exit events"),
ba1ddf42
NK
3574 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3575 "Show the mmap events"),
7c14898b
AH
3576 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3577 "Show context switch events (if recorded)"),
96a44bbc
HB
3578 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3579 "Show namespace events (if recorded)"),
3d7c27b6
JO
3580 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3581 "Show lost events (if recorded)"),
3233b37a
JO
3582 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3583 "Show round events (if recorded)"),
490c8cc9
JO
3584 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3585 "Show bpf related events (if recorded)"),
a14390fd
ACM
3586 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3587 "Dump trace output to files named by the monitored events"),
be3d466c 3588 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
48d02a1d
AK
3589 OPT_INTEGER(0, "max-blocks", &max_blocks,
3590 "Maximum number of code blocks to dump with brstackinsn"),
52bab886 3591 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
83e19860 3592 "Use 9 decimal places when displaying time"),
7a680eb9 3593 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
c12e039d 3594 "Instruction Tracing options\n" ITRACE_HELP,
7a680eb9 3595 itrace_parse_synth_opts),
a9710ba0
AK
3596 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3597 "Show full source file name path for source lines"),
77e0070d
MD
3598 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3599 "Enable symbol demangling"),
3600 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3601 "Enable kernel symbol demangling"),
a91f4c47
DA
3602 OPT_STRING(0, "time", &script.time_str, "str",
3603 "Time span of interest (start,stop)"),
325fbff5
NK
3604 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3605 "Show inline function"),
15a108af
ACM
3606 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3607 "guest mount directory under which every guest os"
3608 " instance has a subdir"),
3609 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3610 "file", "file saving guest os vmlinux"),
3611 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3612 "file", "file saving guest os /proc/kallsyms"),
3613 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3614 "file", "file saving guest os /proc/modules"),
add3a719 3615 OPTS_EVSWITCH(&script.evswitch),
1909629f 3616 OPT_END()
69b6470e 3617 };
40cae2b7
YS
3618 const char * const script_subcommands[] = { "record", "report", NULL };
3619 const char *script_usage[] = {
69b6470e
ACM
3620 "perf script [<options>]",
3621 "perf script [<options>] record <script> [<record-options>] <command>",
3622 "perf script [<options>] report <script> [script-args]",
3623 "perf script [<options>] <script> [<record-options>] <command>",
3624 "perf script [<options>] <top-script> [script-args]",
3625 NULL
3626 };
3875294f 3627
0a7c74ea
ACM
3628 perf_set_singlethreaded();
3629
b5b87312
TZ
3630 setup_scripting();
3631
40cae2b7 3632 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
b5b87312
TZ
3633 PARSE_OPT_STOP_AT_NON_OPTION);
3634
15a108af
ACM
3635 if (symbol_conf.guestmount ||
3636 symbol_conf.default_guest_vmlinux_name ||
3637 symbol_conf.default_guest_kallsyms ||
3638 symbol_conf.default_guest_modules) {
3639 /*
3640 * Enable guest sample processing.
3641 */
3642 perf_guest = true;
3643 }
3644
2d4f2799
JO
3645 data.path = input_name;
3646 data.force = symbol_conf.force;
f5fc1412 3647
b5b87312
TZ
3648 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3649 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3650 if (!rec_script_path)
b0ad8ea6 3651 return cmd_record(argc, argv);
3875294f
TZ
3652 }
3653
b5b87312
TZ
3654 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3655 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3656 if (!rep_script_path) {
3875294f 3657 fprintf(stderr,
b5b87312 3658 "Please specify a valid report script"
133dc4c3 3659 "(see 'perf script -l' for listing)\n");
3875294f
TZ
3660 return -1;
3661 }
3875294f
TZ
3662 }
3663
26567ed7
HPP
3664 if (reltime && deltatime) {
3665 fprintf(stderr,
3666 "reltime and deltatime - the two don't get along well. "
3667 "Please limit to --reltime or --deltatime.\n");
3668 return -1;
3669 }
3670
3c5b645f
AH
3671 if (itrace_synth_opts.callchain &&
3672 itrace_synth_opts.callchain_sz > scripting_max_stack)
3673 scripting_max_stack = itrace_synth_opts.callchain_sz;
3674
44e668c6 3675 /* make sure PERF_EXEC_PATH is set for scripts */
46113a54 3676 set_argv_exec_path(get_argv_exec_path());
44e668c6 3677
b5b87312 3678 if (argc && !script_name && !rec_script_path && !rep_script_path) {
a0cccc2e 3679 int live_pipe[2];
b5b87312 3680 int rep_args;
a0cccc2e
TZ
3681 pid_t pid;
3682
b5b87312
TZ
3683 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3684 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3685
3686 if (!rec_script_path && !rep_script_path) {
c7118369
NK
3687 usage_with_options_msg(script_usage, options,
3688 "Couldn't find script `%s'\n\n See perf"
133dc4c3 3689 " script -l for available scripts.\n", argv[0]);
a0cccc2e
TZ
3690 }
3691
b5b87312
TZ
3692 if (is_top_script(argv[0])) {
3693 rep_args = argc - 1;
3694 } else {
3695 int rec_args;
3696
3697 rep_args = has_required_arg(rep_script_path);
3698 rec_args = (argc - 1) - rep_args;
3699 if (rec_args < 0) {
c7118369
NK
3700 usage_with_options_msg(script_usage, options,
3701 "`%s' script requires options."
133dc4c3 3702 "\n\n See perf script -l for available "
b5b87312 3703 "scripts and options.\n", argv[0]);
b5b87312 3704 }
a0cccc2e
TZ
3705 }
3706
3707 if (pipe(live_pipe) < 0) {
3708 perror("failed to create pipe");
d54b1a9e 3709 return -1;
a0cccc2e
TZ
3710 }
3711
3712 pid = fork();
3713 if (pid < 0) {
3714 perror("failed to fork");
d54b1a9e 3715 return -1;
a0cccc2e
TZ
3716 }
3717
3718 if (!pid) {
b5b87312
TZ
3719 j = 0;
3720
a0cccc2e
TZ
3721 dup2(live_pipe[1], 1);
3722 close(live_pipe[0]);
3723
317df650
RR
3724 if (is_top_script(argv[0])) {
3725 system_wide = true;
3726 } else if (!system_wide) {
d54b1a9e
DA
3727 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3728 err = -1;
3729 goto out;
3730 }
317df650 3731 }
b5b87312
TZ
3732
3733 __argv = malloc((argc + 6) * sizeof(const char *));
d54b1a9e
DA
3734 if (!__argv) {
3735 pr_err("malloc failed\n");
3736 err = -ENOMEM;
3737 goto out;
3738 }
e8719adf 3739
b5b87312
TZ
3740 __argv[j++] = "/bin/sh";
3741 __argv[j++] = rec_script_path;
3742 if (system_wide)
3743 __argv[j++] = "-a";
3744 __argv[j++] = "-q";
3745 __argv[j++] = "-o";
3746 __argv[j++] = "-";
3747 for (i = rep_args + 1; i < argc; i++)
3748 __argv[j++] = argv[i];
3749 __argv[j++] = NULL;
a0cccc2e
TZ
3750
3751 execvp("/bin/sh", (char **)__argv);
e8719adf 3752 free(__argv);
a0cccc2e
TZ
3753 exit(-1);
3754 }
3755
3756 dup2(live_pipe[0], 0);
3757 close(live_pipe[1]);
3758
b5b87312 3759 __argv = malloc((argc + 4) * sizeof(const char *));
d54b1a9e
DA
3760 if (!__argv) {
3761 pr_err("malloc failed\n");
3762 err = -ENOMEM;
3763 goto out;
3764 }
3765
b5b87312
TZ
3766 j = 0;
3767 __argv[j++] = "/bin/sh";
3768 __argv[j++] = rep_script_path;
3769 for (i = 1; i < rep_args + 1; i++)
3770 __argv[j++] = argv[i];
3771 __argv[j++] = "-i";
3772 __argv[j++] = "-";
3773 __argv[j++] = NULL;
a0cccc2e
TZ
3774
3775 execvp("/bin/sh", (char **)__argv);
e8719adf 3776 free(__argv);
a0cccc2e
TZ
3777 exit(-1);
3778 }
3779
b5b87312
TZ
3780 if (rec_script_path)
3781 script_path = rec_script_path;
3782 if (rep_script_path)
3783 script_path = rep_script_path;
34c86ea9 3784
b5b87312 3785 if (script_path) {
b5b87312 3786 j = 0;
3875294f 3787
317df650
RR
3788 if (!rec_script_path)
3789 system_wide = false;
d54b1a9e
DA
3790 else if (!system_wide) {
3791 if (have_cmd(argc - 1, &argv[1]) != 0) {
3792 err = -1;
3793 goto out;
3794 }
3795 }
34c86ea9 3796
b5b87312 3797 __argv = malloc((argc + 2) * sizeof(const char *));
d54b1a9e
DA
3798 if (!__argv) {
3799 pr_err("malloc failed\n");
3800 err = -ENOMEM;
3801 goto out;
3802 }
3803
34c86ea9
TZ
3804 __argv[j++] = "/bin/sh";
3805 __argv[j++] = script_path;
3806 if (system_wide)
3807 __argv[j++] = "-a";
b5b87312 3808 for (i = 2; i < argc; i++)
34c86ea9
TZ
3809 __argv[j++] = argv[i];
3810 __argv[j++] = NULL;
3875294f
TZ
3811
3812 execvp("/bin/sh", (char **)__argv);
e8719adf 3813 free(__argv);
3875294f
TZ
3814 exit(-1);
3815 }
956ffd02 3816
c1c9b969 3817 if (!script_name) {
cf4fee50 3818 setup_pager();
c1c9b969
MW
3819 use_browser = 0;
3820 }
5f9c39dc 3821
8ceb41d7 3822 session = perf_session__new(&data, false, &script.tool);
6ef81c55
MI
3823 if (IS_ERR(session))
3824 return PTR_ERR(session);
d8f66248 3825
e90debdd 3826 if (header || header_only) {
114f709e 3827 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
e90debdd
JO
3828 perf_session__fprintf_info(session, stdout, show_full_info);
3829 if (header_only)
6cc870f0 3830 goto out_delete;
e90debdd 3831 }
114f709e
DCC
3832 if (show_full_info)
3833 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
e90debdd 3834
0a7e6d1b 3835 if (symbol__init(&session->header.env) < 0)
38520dc3
NK
3836 goto out_delete;
3837
3ab481a1 3838 uname(&uts);
9d49169c
SL
3839 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3840 !strcmp(uts.machine, session->header.env.arch) ||
3ab481a1
AK
3841 (!strcmp(uts.machine, "x86_64") &&
3842 !strcmp(session->header.env.arch, "i386")))
3843 native_arch = true;
3844
6f3e5eda 3845 script.session = session;
7322d6c9 3846 script__setup_sample_type(&script);
6f3e5eda 3847
99f753f0
AK
3848 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3849 symbol_conf.graph_function)
e216708d
AH
3850 itrace_synth_opts.thread_stack = true;
3851
7a680eb9
AH
3852 session->itrace_synth_opts = &itrace_synth_opts;
3853
5d67be97 3854 if (cpu_list) {
6cc870f0
NK
3855 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3856 if (err < 0)
3857 goto out_delete;
644e0840 3858 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
5d67be97
AB
3859 }
3860
1424dc96 3861 if (!no_callchain)
c0230b2b
DA
3862 symbol_conf.use_callchain = true;
3863 else
3864 symbol_conf.use_callchain = false;
3865
9ee67421 3866 if (session->tevent.pevent &&
ece2a4f4
TSV
3867 tep_set_function_resolver(session->tevent.pevent,
3868 machine__resolve_kernel_addr,
3869 &session->machines.host) < 0) {
ccb3a829 3870 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
db49bc15
CJ
3871 err = -1;
3872 goto out_delete;
ccb3a829
ACM
3873 }
3874
956ffd02
TZ
3875 if (generate_script_lang) {
3876 struct stat perf_stat;
745f43e3
DA
3877 int input;
3878
2c9e45f7 3879 if (output_set_by_user()) {
745f43e3
DA
3880 fprintf(stderr,
3881 "custom fields not supported for generated scripts");
6cc870f0
NK
3882 err = -EINVAL;
3883 goto out_delete;
745f43e3 3884 }
956ffd02 3885
2d4f2799 3886 input = open(data.path, O_RDONLY); /* input_name */
956ffd02 3887 if (input < 0) {
6cc870f0 3888 err = -errno;
956ffd02 3889 perror("failed to open file");
6cc870f0 3890 goto out_delete;
956ffd02
TZ
3891 }
3892
3893 err = fstat(input, &perf_stat);
3894 if (err < 0) {
3895 perror("failed to stat file");
6cc870f0 3896 goto out_delete;
956ffd02
TZ
3897 }
3898
3899 if (!perf_stat.st_size) {
3900 fprintf(stderr, "zero-sized file, nothing to do!\n");
6cc870f0 3901 goto out_delete;
956ffd02
TZ
3902 }
3903
3904 scripting_ops = script_spec__lookup(generate_script_lang);
3905 if (!scripting_ops) {
3906 fprintf(stderr, "invalid language specifier");
6cc870f0
NK
3907 err = -ENOENT;
3908 goto out_delete;
956ffd02
TZ
3909 }
3910
29f5ffd3 3911 err = scripting_ops->generate_script(session->tevent.pevent,
da378962 3912 "perf-script");
6cc870f0 3913 goto out_delete;
956ffd02
TZ
3914 }
3915
3916 if (script_name) {
586bc5cc 3917 err = scripting_ops->start_script(script_name, argc, argv);
956ffd02 3918 if (err)
6cc870f0 3919 goto out_delete;
133dc4c3 3920 pr_debug("perf script started with script %s\n\n", script_name);
6cc870f0 3921 script_started = true;
956ffd02
TZ
3922 }
3923
9cbdb702
DA
3924
3925 err = perf_session__check_output_opt(session);
3926 if (err < 0)
6cc870f0 3927 goto out_delete;
9cbdb702 3928
284c4e18 3929 if (script.time_str) {
b3509b6e 3930 err = perf_time__parse_for_ranges_reltime(script.time_str, session,
284c4e18
JY
3931 &script.ptime_range,
3932 &script.range_size,
b3509b6e
AK
3933 &script.range_num,
3934 reltime);
284c4e18 3935 if (err < 0)
2ab046cd 3936 goto out_delete;
400ae981
AH
3937
3938 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3939 script.ptime_range,
3940 script.range_num);
a91f4c47
DA
3941 }
3942
124e02be
ACM
3943 err = evswitch__init(&script.evswitch, session->evlist, stderr);
3944 if (err)
3945 goto out_delete;
dd41f660 3946
6f3e5eda 3947 err = __cmd_script(&script);
956ffd02 3948
d445dd2a
AH
3949 flush_scripting();
3950
6cc870f0 3951out_delete:
400ae981
AH
3952 if (script.ptime_range) {
3953 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
284c4e18 3954 zfree(&script.ptime_range);
400ae981 3955 }
cc2ef584 3956
cfc8874a 3957 perf_evlist__free_stats(session->evlist);
d8f66248 3958 perf_session__delete(session);
6cc870f0
NK
3959
3960 if (script_started)
3961 cleanup_scripting();
956ffd02
TZ
3962out:
3963 return err;
5f9c39dc 3964}