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