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