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