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