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