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