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