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