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