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