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