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