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