projects/linux·blob master · refs

4629 lines · 125.0 KiB

1// SPDX-License-Identifier: GPL-2.0
2#include <errno.h>
3#include <inttypes.h>
4#include <signal.h>
5#include <stdio.h>
6
7#include <dirent.h>
8#include <fcntl.h>
9#include <linux/bitmap.h>
10#include <linux/compiler.h>
11#include <linux/ctype.h>
12#include <linux/err.h>
13#include <linux/kernel.h>
14#include <linux/stringify.h>
15#include <linux/time64.h>
16#include <linux/unaligned.h>
17#include <linux/zalloc.h>
18#include <sys/param.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <sys/utsname.h>
22#include <unistd.h>
23
24#include <perf/evlist.h>
25#include <subcmd/exec-cmd.h>
26#include <subcmd/pager.h>
27#include <subcmd/parse-options.h>
28
29#include "asm/bug.h"
30#include "builtin.h"
31#include "perf.h"
32#include "print_binary.h"
33#include "print_insn.h"
34#include "ui/ui.h"
35#include "util/annotate.h"
36#include "util/auxtrace.h"
37#include "util/cgroup.h"
38#include "util/color.h"
39#include "util/counts.h"
40#include "util/cpumap.h"
41#include "util/data.h"
42#include "util/debug.h"
43#include "util/dlfilter.h"
44#include "util/dso.h"
45#include "util/dump-insn.h"
46#include "util/env.h"
47#include "util/event.h"
48#include "util/evlist.h"
49#include "util/evsel.h"
50#include "util/evsel_fprintf.h"
51#include "util/evswitch.h"
52#include "util/header.h"
53#include "util/map.h"
54#include "util/mem-events.h"
55#include "util/mem-info.h"
56#include "util/metricgroup.h"
57#include "util/path.h"
58#include "util/perf_regs.h"
59#include "util/pmu.h"
60#include "util/record.h"
61#include "util/session.h"
62#include "util/sort.h"
63#include "util/srcline.h"
64#include "util/stat.h"
65#include "util/string2.h"
66#include "util/symbol.h"
67#include "util/thread-stack.h"
68#include "util/thread.h"
69#include "util/thread_map.h"
70#include "util/time-utils.h"
71#include "util/tool.h"
72#include "util/trace-event.h"
73#include "util/unwind.h"
74#include "util/util.h"
75
76#ifdef HAVE_LIBTRACEEVENT
77#include <event-parse.h>
78#endif
79
80static char const *script_name;
81static char const *generate_script_lang;
82static bool reltime;
83static bool deltatime;
84static u64 initial_time;
85static u64 previous_time;
86static bool debug_mode;
87static u64 last_timestamp;
88static u64 nr_unordered;
89static bool no_callchain;
90static bool latency_format;
91static bool system_wide;
92static bool print_flags;
93static const char *cpu_list;
94static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
95static int max_blocks;
96static struct dlfilter *dlfilter;
97static int dlargc;
98static char **dlargv;
99
100enum perf_output_field {
101 PERF_OUTPUT_COMM = 1ULL << 0,
102 PERF_OUTPUT_TID = 1ULL << 1,
103 PERF_OUTPUT_PID = 1ULL << 2,
104 PERF_OUTPUT_TIME = 1ULL << 3,
105 PERF_OUTPUT_CPU = 1ULL << 4,
106 PERF_OUTPUT_EVNAME = 1ULL << 5,
107 PERF_OUTPUT_TRACE = 1ULL << 6,
108 PERF_OUTPUT_IP = 1ULL << 7,
109 PERF_OUTPUT_SYM = 1ULL << 8,
110 PERF_OUTPUT_DSO = 1ULL << 9,
111 PERF_OUTPUT_ADDR = 1ULL << 10,
112 PERF_OUTPUT_SYMOFFSET = 1ULL << 11,
113 PERF_OUTPUT_SRCLINE = 1ULL << 12,
114 PERF_OUTPUT_PERIOD = 1ULL << 13,
115 PERF_OUTPUT_IREGS = 1ULL << 14,
116 PERF_OUTPUT_BRSTACK = 1ULL << 15,
117 PERF_OUTPUT_BRSTACKSYM = 1ULL << 16,
118 PERF_OUTPUT_DATA_SRC = 1ULL << 17,
119 PERF_OUTPUT_WEIGHT = 1ULL << 18,
120 PERF_OUTPUT_BPF_OUTPUT = 1ULL << 19,
121 PERF_OUTPUT_CALLINDENT = 1ULL << 20,
122 PERF_OUTPUT_INSN = 1ULL << 21,
123 PERF_OUTPUT_INSNLEN = 1ULL << 22,
124 PERF_OUTPUT_BRSTACKINSN = 1ULL << 23,
125 PERF_OUTPUT_BRSTACKOFF = 1ULL << 24,
126 PERF_OUTPUT_SYNTH = 1ULL << 25,
127 PERF_OUTPUT_PHYS_ADDR = 1ULL << 26,
128 PERF_OUTPUT_UREGS = 1ULL << 27,
129 PERF_OUTPUT_METRIC = 1ULL << 28,
130 PERF_OUTPUT_MISC = 1ULL << 29,
131 PERF_OUTPUT_SRCCODE = 1ULL << 30,
132 PERF_OUTPUT_IPC = 1ULL << 31,
133 PERF_OUTPUT_TOD = 1ULL << 32,
134 PERF_OUTPUT_DATA_PAGE_SIZE = 1ULL << 33,
135 PERF_OUTPUT_CODE_PAGE_SIZE = 1ULL << 34,
136 PERF_OUTPUT_INS_LAT = 1ULL << 35,
137 PERF_OUTPUT_BRSTACKINSNLEN = 1ULL << 36,
138 PERF_OUTPUT_MACHINE_PID = 1ULL << 37,
139 PERF_OUTPUT_VCPU = 1ULL << 38,
140 PERF_OUTPUT_CGROUP = 1ULL << 39,
141 PERF_OUTPUT_RETIRE_LAT = 1ULL << 40,
142 PERF_OUTPUT_DSOFF = 1ULL << 41,
143 PERF_OUTPUT_DISASM = 1ULL << 42,
144 PERF_OUTPUT_BRSTACKDISASM = 1ULL << 43,
145 PERF_OUTPUT_BRCNTR = 1ULL << 44,
146};
147
148struct perf_script {
149 struct perf_tool tool;
150 struct perf_session *session;
151 bool show_task_events;
152 bool show_mmap_events;
153 bool show_switch_events;
154 bool show_namespace_events;
155 bool show_lost_events;
156 bool show_round_events;
157 bool show_bpf_events;
158 bool show_cgroup_events;
159 bool show_text_poke_events;
160 bool allocated;
161 bool per_event_dump;
162 bool stitch_lbr;
163 struct evswitch evswitch;
164 struct perf_cpu_map *cpus;
165 struct perf_thread_map *threads;
166 int name_width;
167 const char *time_str;
168 struct perf_time_interval *ptime_range;
169 int range_size;
170 int range_num;
171};
172
173static struct output_option {
174 const char *str;
175 enum perf_output_field field;
176} all_output_options[] = {
177 {.str = "comm", .field = PERF_OUTPUT_COMM},
178 {.str = "tid", .field = PERF_OUTPUT_TID},
179 {.str = "pid", .field = PERF_OUTPUT_PID},
180 {.str = "time", .field = PERF_OUTPUT_TIME},
181 {.str = "cpu", .field = PERF_OUTPUT_CPU},
182 {.str = "event", .field = PERF_OUTPUT_EVNAME},
183 {.str = "trace", .field = PERF_OUTPUT_TRACE},
184 {.str = "ip", .field = PERF_OUTPUT_IP},
185 {.str = "sym", .field = PERF_OUTPUT_SYM},
186 {.str = "dso", .field = PERF_OUTPUT_DSO},
187 {.str = "dsoff", .field = PERF_OUTPUT_DSOFF},
188 {.str = "addr", .field = PERF_OUTPUT_ADDR},
189 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
190 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
191 {.str = "period", .field = PERF_OUTPUT_PERIOD},
192 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
193 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
194 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
195 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
196 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
197 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
198 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
199 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
200 {.str = "insn", .field = PERF_OUTPUT_INSN},
201 {.str = "disasm", .field = PERF_OUTPUT_DISASM},
202 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
203 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
204 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
205 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
206 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
207 {.str = "metric", .field = PERF_OUTPUT_METRIC},
208 {.str = "misc", .field = PERF_OUTPUT_MISC},
209 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
210 {.str = "ipc", .field = PERF_OUTPUT_IPC},
211 {.str = "tod", .field = PERF_OUTPUT_TOD},
212 {.str = "data_page_size", .field = PERF_OUTPUT_DATA_PAGE_SIZE},
213 {.str = "code_page_size", .field = PERF_OUTPUT_CODE_PAGE_SIZE},
214 {.str = "ins_lat", .field = PERF_OUTPUT_INS_LAT},
215 {.str = "brstackinsnlen", .field = PERF_OUTPUT_BRSTACKINSNLEN},
216 {.str = "machine_pid", .field = PERF_OUTPUT_MACHINE_PID},
217 {.str = "vcpu", .field = PERF_OUTPUT_VCPU},
218 {.str = "cgroup", .field = PERF_OUTPUT_CGROUP},
219 {.str = "retire_lat", .field = PERF_OUTPUT_RETIRE_LAT},
220 {.str = "brstackdisasm", .field = PERF_OUTPUT_BRSTACKDISASM},
221 {.str = "brcntr", .field = PERF_OUTPUT_BRCNTR},
222};
223
224enum {
225 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
226 OUTPUT_TYPE_OTHER,
227 OUTPUT_TYPE_MAX
228};
229
230// We need to refactor the evsel->priv use in 'perf script' to allow for
231// using that area, that is being used only in some cases.
232#define OUTPUT_TYPE_UNSET -1
233
234/* default set to maintain compatibility with current format */
235static struct {
236 bool user_set;
237 bool wildcard_set;
238 unsigned int print_ip_opts;
239 u64 fields;
240 u64 invalid_fields;
241 u64 user_set_fields;
242 u64 user_unset_fields;
243} output[OUTPUT_TYPE_MAX] = {
244
245 [PERF_TYPE_HARDWARE] = {
246 .user_set = false,
247
248 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
249 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
250 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
251 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
252 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
253
254 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
255 },
256
257 [PERF_TYPE_SOFTWARE] = {
258 .user_set = false,
259
260 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
261 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
262 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
263 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
264 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
265 PERF_OUTPUT_BPF_OUTPUT,
266
267 .invalid_fields = PERF_OUTPUT_TRACE,
268 },
269
270 [PERF_TYPE_TRACEPOINT] = {
271 .user_set = false,
272
273 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
274 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
275 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
276 },
277
278 [PERF_TYPE_HW_CACHE] = {
279 .user_set = false,
280
281 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
282 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
283 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
284 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
285 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
286
287 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
288 },
289
290 [PERF_TYPE_RAW] = {
291 .user_set = false,
292
293 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
294 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
295 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
296 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
297 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
298 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
299 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR |
300 PERF_OUTPUT_DATA_PAGE_SIZE | PERF_OUTPUT_CODE_PAGE_SIZE |
301 PERF_OUTPUT_INS_LAT | PERF_OUTPUT_RETIRE_LAT,
302
303 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
304 },
305
306 [PERF_TYPE_BREAKPOINT] = {
307 .user_set = false,
308
309 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
310 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
311 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
312 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
313 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
314
315 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
316 },
317
318 [OUTPUT_TYPE_SYNTH] = {
319 .user_set = false,
320
321 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
322 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
323 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
324 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
325 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
326
327 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
328 },
329
330 [OUTPUT_TYPE_OTHER] = {
331 .user_set = false,
332
333 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
334 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
335 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
336 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
337 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
338
339 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
340 },
341};
342
343struct evsel_script {
344 char *filename;
345 FILE *fp;
346 u64 samples;
347};
348
349static struct evsel_script *evsel_script__new(struct evsel *evsel, struct perf_data *data)
350{
351 struct evsel_script *es = zalloc(sizeof(*es));
352
353 if (es != NULL) {
354 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, evsel__name(evsel)) < 0)
355 goto out_free;
356 es->fp = fopen(es->filename, "w");
357 if (es->fp == NULL)
358 goto out_free_filename;
359 }
360
361 return es;
362out_free_filename:
363 zfree(&es->filename);
364out_free:
365 free(es);
366 return NULL;
367}
368
369static void evsel_script__delete(struct evsel_script *es)
370{
371 zfree(&es->filename);
372 fclose(es->fp);
373 es->fp = NULL;
374 free(es);
375}
376
377static int evsel_script__fprintf(struct evsel_script *es, FILE *fp)
378{
379 struct stat st;
380
381 fstat(fileno(es->fp), &st);
382 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
383 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
384}
385
386static inline int output_type(unsigned int type)
387{
388 switch (type) {
389 case PERF_TYPE_SYNTH:
390 return OUTPUT_TYPE_SYNTH;
391 default:
392 if (type < PERF_TYPE_MAX)
393 return type;
394 }
395
396 return OUTPUT_TYPE_OTHER;
397}
398
399static inline int evsel__output_type(struct evsel *evsel)
400{
401 int type = evsel->script_output_type;
402
403 if (type == OUTPUT_TYPE_UNSET) {
404 type = output_type(evsel->core.attr.type);
405 if (type == OUTPUT_TYPE_OTHER) {
406 struct perf_pmu *pmu = evsel__find_pmu(evsel);
407
408 if (pmu && pmu->is_core)
409 type = PERF_TYPE_RAW;
410 }
411 evsel->script_output_type = type;
412 }
413
414 return type;
415}
416
417static bool output_set_by_user(void)
418{
419 int j;
420 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
421 if (output[j].user_set)
422 return true;
423 }
424 return false;
425}
426
427static const char *output_field2str(enum perf_output_field field)
428{
429 int i, imax = ARRAY_SIZE(all_output_options);
430 const char *str = "";
431
432 for (i = 0; i < imax; ++i) {
433 if (all_output_options[i].field == field) {
434 str = all_output_options[i].str;
435 break;
436 }
437 }
438 return str;
439}
440
441#define PRINT_FIELD(x) (output[evsel__output_type(evsel)].fields & PERF_OUTPUT_##x)
442
443static int evsel__do_check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
444 enum perf_output_field field, bool allow_user_set)
445{
446 struct perf_event_attr *attr = &evsel->core.attr;
447 int type = evsel__output_type(evsel);
448 const char *evname;
449
450 if (attr->sample_type & sample_type)
451 return 0;
452
453 if (output[type].user_set_fields & field) {
454 if (allow_user_set)
455 return 0;
456 evname = evsel__name(evsel);
457 pr_err("Samples for '%s' event do not have %s attribute set. "
458 "Cannot print '%s' field.\n",
459 evname, sample_msg, output_field2str(field));
460 return -1;
461 }
462
463 /* user did not ask for it explicitly so remove from the default list */
464 output[type].fields &= ~field;
465 evname = evsel__name(evsel);
466 pr_debug("Samples for '%s' event do not have %s attribute set. "
467 "Skipping '%s' field.\n",
468 evname, sample_msg, output_field2str(field));
469
470 return 0;
471}
472
473static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
474 enum perf_output_field field)
475{
476 return evsel__do_check_stype(evsel, sample_type, sample_msg, field, false);
477}
478
479static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
480{
481 bool allow_user_set;
482
483 if (evsel__is_dummy_event(evsel))
484 return 0;
485
486 if (perf_header__has_feat(&session->header, HEADER_STAT))
487 return 0;
488
489 allow_user_set = perf_header__has_feat(&session->header,
490 HEADER_AUXTRACE);
491
492 if (PRINT_FIELD(TRACE) &&
493 !perf_session__has_traces(session, "record -R"))
494 return -EINVAL;
495
496 if (PRINT_FIELD(IP)) {
497 if (evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", PERF_OUTPUT_IP))
498 return -EINVAL;
499 }
500
501 if (PRINT_FIELD(ADDR) &&
502 evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", PERF_OUTPUT_ADDR, allow_user_set))
503 return -EINVAL;
504
505 if (PRINT_FIELD(DATA_SRC) &&
506 evsel__do_check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", PERF_OUTPUT_DATA_SRC, allow_user_set))
507 return -EINVAL;
508
509 if (PRINT_FIELD(WEIGHT) &&
510 evsel__do_check_stype(evsel, PERF_SAMPLE_WEIGHT_TYPE, "WEIGHT", PERF_OUTPUT_WEIGHT, allow_user_set))
511 return -EINVAL;
512
513 if (PRINT_FIELD(SYM) &&
514 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
515 pr_err("Display of symbols requested but neither sample IP nor "
516 "sample address\navailable. Hence, no addresses to convert "
517 "to symbols.\n");
518 return -EINVAL;
519 }
520 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
521 pr_err("Display of offsets requested but symbol is not"
522 "selected.\n");
523 return -EINVAL;
524 }
525 if (PRINT_FIELD(DSO) &&
526 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
527 pr_err("Display of DSO requested but no address to convert.\n");
528 return -EINVAL;
529 }
530 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
531 pr_err("Display of source line number requested but sample IP is not\n"
532 "selected. Hence, no address to lookup the source line number.\n");
533 return -EINVAL;
534 }
535 if ((PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN) || PRINT_FIELD(BRSTACKDISASM))
536 && !allow_user_set &&
537 !(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY)) {
538 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
539 "Hint: run 'perf record -b ...'\n");
540 return -EINVAL;
541 }
542 if (PRINT_FIELD(BRCNTR) &&
543 !(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_COUNTERS)) {
544 pr_err("Display of branch counter requested but it's not enabled\n"
545 "Hint: run 'perf record -j any,counter ...'\n");
546 return -EINVAL;
547 }
548 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
549 evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", PERF_OUTPUT_TID|PERF_OUTPUT_PID))
550 return -EINVAL;
551
552 if (PRINT_FIELD(TIME) &&
553 evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", PERF_OUTPUT_TIME))
554 return -EINVAL;
555
556 if (PRINT_FIELD(CPU) &&
557 evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", PERF_OUTPUT_CPU, allow_user_set))
558 return -EINVAL;
559
560 if (PRINT_FIELD(IREGS) &&
561 evsel__do_check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", PERF_OUTPUT_IREGS, allow_user_set))
562 return -EINVAL;
563
564 if (PRINT_FIELD(UREGS) &&
565 evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS", PERF_OUTPUT_UREGS))
566 return -EINVAL;
567
568 if (PRINT_FIELD(PHYS_ADDR) &&
569 evsel__do_check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR", PERF_OUTPUT_PHYS_ADDR, allow_user_set))
570 return -EINVAL;
571
572 if (PRINT_FIELD(DATA_PAGE_SIZE) &&
573 evsel__check_stype(evsel, PERF_SAMPLE_DATA_PAGE_SIZE, "DATA_PAGE_SIZE", PERF_OUTPUT_DATA_PAGE_SIZE))
574 return -EINVAL;
575
576 if (PRINT_FIELD(CODE_PAGE_SIZE) &&
577 evsel__check_stype(evsel, PERF_SAMPLE_CODE_PAGE_SIZE, "CODE_PAGE_SIZE", PERF_OUTPUT_CODE_PAGE_SIZE))
578 return -EINVAL;
579
580 if (PRINT_FIELD(INS_LAT) &&
581 evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT_STRUCT, "WEIGHT_STRUCT", PERF_OUTPUT_INS_LAT))
582 return -EINVAL;
583
584 if (PRINT_FIELD(CGROUP) &&
585 evsel__check_stype(evsel, PERF_SAMPLE_CGROUP, "CGROUP", PERF_OUTPUT_CGROUP)) {
586 pr_err("Hint: run 'perf record --all-cgroups ...'\n");
587 return -EINVAL;
588 }
589
590 if (PRINT_FIELD(RETIRE_LAT) &&
591 evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT_STRUCT, "WEIGHT_STRUCT", PERF_OUTPUT_RETIRE_LAT))
592 return -EINVAL;
593
594 return 0;
595}
596
597static void evsel__set_print_ip_opts(struct evsel *evsel)
598{
599 unsigned int type = evsel__output_type(evsel);
600
601 output[type].print_ip_opts = 0;
602 if (PRINT_FIELD(IP))
603 output[type].print_ip_opts |= EVSEL__PRINT_IP;
604
605 if (PRINT_FIELD(SYM))
606 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
607
608 if (PRINT_FIELD(DSO))
609 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
610
611 if (PRINT_FIELD(DSOFF))
612 output[type].print_ip_opts |= EVSEL__PRINT_DSOFF;
613
614 if (PRINT_FIELD(SYMOFFSET))
615 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
616
617 if (PRINT_FIELD(SRCLINE))
618 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
619}
620
621static struct evsel *find_first_output_type(struct evlist *evlist,
622 unsigned int type)
623{
624 struct evsel *evsel;
625
626 evlist__for_each_entry(evlist, evsel) {
627 if (evsel__is_dummy_event(evsel))
628 continue;
629 if (evsel__output_type(evsel) == (int)type)
630 return evsel;
631 }
632 return NULL;
633}
634
635/*
636 * verify all user requested events exist and the samples
637 * have the expected data
638 */
639static int perf_session__check_output_opt(struct perf_session *session)
640{
641 bool tod = false;
642 unsigned int j;
643 struct evsel *evsel;
644
645 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
646 evsel = find_first_output_type(session->evlist, j);
647
648 /*
649 * even if fields is set to 0 (ie., show nothing) event must
650 * exist if user explicitly includes it on the command line
651 */
652 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
653 j != OUTPUT_TYPE_SYNTH) {
654 pr_err("%s events do not exist. "
655 "Remove corresponding -F option to proceed.\n",
656 event_type(j));
657 return -1;
658 }
659
660 if (evsel && output[j].fields &&
661 evsel__check_attr(evsel, session))
662 return -1;
663
664 if (evsel == NULL)
665 continue;
666
667 /* 'dsoff' implys 'dso' field */
668 if (output[j].fields & PERF_OUTPUT_DSOFF)
669 output[j].fields |= PERF_OUTPUT_DSO;
670
671 evsel__set_print_ip_opts(evsel);
672 tod |= output[j].fields & PERF_OUTPUT_TOD;
673 }
674
675 if (!no_callchain) {
676 bool use_callchain = false;
677 bool not_pipe = false;
678
679 evlist__for_each_entry(session->evlist, evsel) {
680 not_pipe = true;
681 if (evsel__has_callchain(evsel) || evsel__is_offcpu_event(evsel)) {
682 use_callchain = true;
683 break;
684 }
685 }
686 if (not_pipe && !use_callchain)
687 symbol_conf.use_callchain = false;
688 }
689
690 /*
691 * set default for tracepoints to print symbols only
692 * if callchains are present
693 */
694 if (symbol_conf.use_callchain &&
695 !output[PERF_TYPE_TRACEPOINT].user_set) {
696 j = PERF_TYPE_TRACEPOINT;
697
698 evlist__for_each_entry(session->evlist, evsel) {
699 if (evsel->core.attr.type != j)
700 continue;
701
702 if (evsel__has_callchain(evsel)) {
703 output[j].fields |= PERF_OUTPUT_IP;
704 output[j].fields |= PERF_OUTPUT_SYM;
705 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
706 output[j].fields |= PERF_OUTPUT_DSO;
707 evsel__set_print_ip_opts(evsel);
708 goto out;
709 }
710 }
711 }
712
713 if (tod && !perf_session__env(session)->clock.enabled) {
714 pr_err("Can't provide 'tod' time, missing clock data. "
715 "Please record with -k/--clockid option.\n");
716 return -1;
717 }
718out:
719 return 0;
720}
721
722static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
723 uint16_t e_machine, uint32_t e_flags,
724 FILE *fp)
725{
726 unsigned i = 0, r;
727 int printed = 0;
728
729 if (!regs || !regs->regs)
730 return 0;
731
732 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
733
734 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
735 u64 val = regs->regs[i++];
736 printed += fprintf(fp, "%5s:0x%"PRIx64" ",
737 perf_reg_name(r, e_machine, e_flags),
738 val);
739 }
740
741 return printed;
742}
743
744#define DEFAULT_TOD_FMT "%F %H:%M:%S"
745
746static char*
747tod_scnprintf(struct perf_script *script, char *buf, int buflen,
748 u64 timestamp)
749{
750 u64 tod_ns, clockid_ns;
751 struct perf_env *env;
752 unsigned long nsec;
753 struct tm ltime;
754 char date[64];
755 time_t sec;
756
757 buf[0] = '\0';
758 if (buflen < 64 || !script)
759 return buf;
760
761 env = perf_session__env(script->session);
762 if (!env->clock.enabled) {
763 scnprintf(buf, buflen, "disabled");
764 return buf;
765 }
766
767 clockid_ns = env->clock.clockid_ns;
768 tod_ns = env->clock.tod_ns;
769
770 if (timestamp > clockid_ns)
771 tod_ns += timestamp - clockid_ns;
772 else
773 tod_ns -= clockid_ns - timestamp;
774
775 sec = (time_t) (tod_ns / NSEC_PER_SEC);
776 nsec = tod_ns - sec * NSEC_PER_SEC;
777
778 if (localtime_r(&sec, &ltime) == NULL) {
779 scnprintf(buf, buflen, "failed");
780 } else {
781 strftime(date, sizeof(date), DEFAULT_TOD_FMT, &ltime);
782
783 if (symbol_conf.nanosecs) {
784 snprintf(buf, buflen, "%s.%09lu", date, nsec);
785 } else {
786 snprintf(buf, buflen, "%s.%06lu",
787 date, nsec / NSEC_PER_USEC);
788 }
789 }
790
791 return buf;
792}
793
794static int perf_sample__fprintf_iregs(struct perf_sample *sample,
795 struct perf_event_attr *attr,
796 uint16_t e_machine,
797 uint32_t e_flags,
798 FILE *fp)
799{
800 if (!sample->intr_regs)
801 return 0;
802
803 return perf_sample__fprintf_regs(perf_sample__intr_regs(sample),
804 attr->sample_regs_intr, e_machine, e_flags, fp);
805}
806
807static int perf_sample__fprintf_uregs(struct perf_sample *sample,
808 struct perf_event_attr *attr,
809 uint16_t e_machine,
810 uint32_t e_flags,
811 FILE *fp)
812{
813 if (!sample->user_regs)
814 return 0;
815
816 return perf_sample__fprintf_regs(perf_sample__user_regs(sample),
817 attr->sample_regs_user, e_machine, e_flags, fp);
818}
819
820static int perf_sample__fprintf_start(struct perf_script *script,
821 struct perf_sample *sample,
822 struct thread *thread,
823 struct evsel *evsel,
824 u32 type, FILE *fp)
825{
826 unsigned long secs;
827 unsigned long long nsecs;
828 int printed = 0;
829 char tstr[128];
830
831 /*
832 * Print the branch counter's abbreviation list,
833 * if the branch counter is available.
834 */
835 if (PRINT_FIELD(BRCNTR) && !verbose) {
836 char *buf;
837
838 if (!annotation_br_cntr_abbr_list(&buf, evsel, true)) {
839 printed += fprintf(stdout, "%s", buf);
840 free(buf);
841 }
842 }
843
844 if (PRINT_FIELD(MACHINE_PID) && sample->machine_pid)
845 printed += fprintf(fp, "VM:%5d ", sample->machine_pid);
846
847 /* Print VCPU only for guest events i.e. with machine_pid */
848 if (PRINT_FIELD(VCPU) && sample->machine_pid)
849 printed += fprintf(fp, "VCPU:%03d ", sample->vcpu);
850
851 if (PRINT_FIELD(COMM)) {
852 const char *comm = thread ? thread__comm_str(thread) : ":-1";
853
854 if (latency_format)
855 printed += fprintf(fp, "%8.8s ", comm);
856 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
857 printed += fprintf(fp, "%s ", comm);
858 else
859 printed += fprintf(fp, "%16s ", comm);
860 }
861
862 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
863 printed += fprintf(fp, "%7d/%-7d ", sample->pid, sample->tid);
864 else if (PRINT_FIELD(PID))
865 printed += fprintf(fp, "%7d ", sample->pid);
866 else if (PRINT_FIELD(TID))
867 printed += fprintf(fp, "%7d ", sample->tid);
868
869 if (PRINT_FIELD(CPU)) {
870 if (latency_format)
871 printed += fprintf(fp, "%3d ", sample->cpu);
872 else
873 printed += fprintf(fp, "[%03d] ", sample->cpu);
874 }
875
876 if (PRINT_FIELD(MISC)) {
877 int ret = 0;
878
879 #define has(m) \
880 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
881
882 if (has(KERNEL))
883 ret += fprintf(fp, "K");
884 if (has(USER))
885 ret += fprintf(fp, "U");
886 if (has(HYPERVISOR))
887 ret += fprintf(fp, "H");
888 if (has(GUEST_KERNEL))
889 ret += fprintf(fp, "G");
890 if (has(GUEST_USER))
891 ret += fprintf(fp, "g");
892
893 switch (type) {
894 case PERF_RECORD_MMAP:
895 case PERF_RECORD_MMAP2:
896 if (has(MMAP_DATA))
897 ret += fprintf(fp, "M");
898 break;
899 case PERF_RECORD_COMM:
900 if (has(COMM_EXEC))
901 ret += fprintf(fp, "E");
902 break;
903 case PERF_RECORD_SWITCH:
904 case PERF_RECORD_SWITCH_CPU_WIDE:
905 if (has(SWITCH_OUT)) {
906 ret += fprintf(fp, "S");
907 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
908 ret += fprintf(fp, "p");
909 }
910 default:
911 break;
912 }
913
914 #undef has
915
916 ret += fprintf(fp, "%*s", 6 - ret, " ");
917 printed += ret;
918 }
919
920 if (PRINT_FIELD(TOD)) {
921 tod_scnprintf(script, tstr, sizeof(tstr), sample->time);
922 printed += fprintf(fp, "%s ", tstr);
923 }
924
925 if (PRINT_FIELD(TIME)) {
926 u64 t = sample->time;
927 if (reltime) {
928 if (!initial_time)
929 initial_time = sample->time;
930 t = sample->time - initial_time;
931 } else if (deltatime) {
932 if (previous_time)
933 t = sample->time - previous_time;
934 else {
935 t = 0;
936 }
937 previous_time = sample->time;
938 }
939 nsecs = t;
940 secs = nsecs / NSEC_PER_SEC;
941 nsecs -= secs * NSEC_PER_SEC;
942
943 if (symbol_conf.nanosecs)
944 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
945 else {
946 char sample_time[32];
947 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
948 printed += fprintf(fp, "%12s: ", sample_time);
949 }
950 }
951
952 return printed;
953}
954
955static inline size_t
956bstack_event_str(struct branch_entry *br, char *buf, size_t sz)
957{
958 if (!(br->flags.mispred || br->flags.predicted || br->flags.not_taken))
959 return snprintf(buf, sz, "-");
960
961 return snprintf(buf, sz, "%s%s",
962 br->flags.predicted ? "P" : "M",
963 br->flags.not_taken ? "N" : "");
964}
965
966static int print_bstack_flags(FILE *fp, struct branch_entry *br)
967{
968 char events[16] = { 0 };
969 size_t pos;
970
971 pos = bstack_event_str(br, events, sizeof(events));
972 return fprintf(fp, "/%s/%c/%c/%d/%s/%s ",
973 pos < 0 ? "-" : events,
974 br->flags.in_tx ? 'X' : '-',
975 br->flags.abort ? 'A' : '-',
976 br->flags.cycles,
977 get_branch_type(br),
978 br->flags.spec ? branch_spec_desc(br->flags.spec) : "-");
979}
980
981static int perf_sample__fprintf_brstack(struct perf_sample *sample,
982 struct thread *thread,
983 struct evsel *evsel, FILE *fp)
984{
985 struct branch_stack *br = sample->branch_stack;
986 struct branch_entry *entries = perf_sample__branch_entries(sample);
987 u64 i, from, to;
988 int printed = 0;
989
990 if (!(br && br->nr))
991 return 0;
992
993 for (i = 0; i < br->nr; i++) {
994 from = entries[i].from;
995 to = entries[i].to;
996
997 printed += fprintf(fp, " 0x%"PRIx64, from);
998 if (PRINT_FIELD(DSO)) {
999 struct addr_location alf, alt;
1000
1001 addr_location__init(&alf);
1002 addr_location__init(&alt);
1003 thread__find_map_fb(thread, sample->cpumode, from, &alf);
1004 thread__find_map_fb(thread, sample->cpumode, to, &alt);
1005
1006 printed += map__fprintf_dsoname_dsoff(alf.map, PRINT_FIELD(DSOFF), alf.addr, fp);
1007 printed += fprintf(fp, "/0x%"PRIx64, to);
1008 printed += map__fprintf_dsoname_dsoff(alt.map, PRINT_FIELD(DSOFF), alt.addr, fp);
1009 addr_location__exit(&alt);
1010 addr_location__exit(&alf);
1011 } else
1012 printed += fprintf(fp, "/0x%"PRIx64, to);
1013
1014 printed += print_bstack_flags(fp, entries + i);
1015 }
1016
1017 return printed;
1018}
1019
1020static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
1021 struct thread *thread,
1022 struct evsel *evsel, FILE *fp)
1023{
1024 struct branch_stack *br = sample->branch_stack;
1025 struct branch_entry *entries = perf_sample__branch_entries(sample);
1026 u64 i, from, to;
1027 int printed = 0;
1028
1029 if (!(br && br->nr))
1030 return 0;
1031
1032 for (i = 0; i < br->nr; i++) {
1033 struct addr_location alf, alt;
1034
1035 addr_location__init(&alf);
1036 addr_location__init(&alt);
1037 from = entries[i].from;
1038 to = entries[i].to;
1039
1040 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
1041 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
1042
1043 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
1044 if (PRINT_FIELD(DSO))
1045 printed += map__fprintf_dsoname_dsoff(alf.map, PRINT_FIELD(DSOFF), alf.addr, fp);
1046 printed += fprintf(fp, "%c", '/');
1047 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
1048 if (PRINT_FIELD(DSO))
1049 printed += map__fprintf_dsoname_dsoff(alt.map, PRINT_FIELD(DSOFF), alt.addr, fp);
1050 printed += print_bstack_flags(fp, entries + i);
1051 addr_location__exit(&alt);
1052 addr_location__exit(&alf);
1053 }
1054
1055 return printed;
1056}
1057
1058static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
1059 struct thread *thread,
1060 struct evsel *evsel, FILE *fp)
1061{
1062 struct branch_stack *br = sample->branch_stack;
1063 struct branch_entry *entries = perf_sample__branch_entries(sample);
1064 u64 i, from, to;
1065 int printed = 0;
1066
1067 if (!(br && br->nr))
1068 return 0;
1069
1070 for (i = 0; i < br->nr; i++) {
1071 struct addr_location alf, alt;
1072
1073 addr_location__init(&alf);
1074 addr_location__init(&alt);
1075 from = entries[i].from;
1076 to = entries[i].to;
1077
1078 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
1079 !dso__adjust_symbols(map__dso(alf.map)))
1080 from = map__dso_map_ip(alf.map, from);
1081
1082 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
1083 !dso__adjust_symbols(map__dso(alt.map)))
1084 to = map__dso_map_ip(alt.map, to);
1085
1086 printed += fprintf(fp, " 0x%"PRIx64, from);
1087 if (PRINT_FIELD(DSO))
1088 printed += map__fprintf_dsoname_dsoff(alf.map, PRINT_FIELD(DSOFF), alf.addr, fp);
1089 printed += fprintf(fp, "/0x%"PRIx64, to);
1090 if (PRINT_FIELD(DSO))
1091 printed += map__fprintf_dsoname_dsoff(alt.map, PRINT_FIELD(DSOFF), alt.addr, fp);
1092 printed += print_bstack_flags(fp, entries + i);
1093 addr_location__exit(&alt);
1094 addr_location__exit(&alf);
1095 }
1096
1097 return printed;
1098}
1099#define MAXBB 16384UL
1100
1101static int grab_bb(u8 *buffer, u64 start, u64 end,
1102 struct machine *machine, struct thread *thread,
1103 bool *is64bit, u8 *cpumode, bool last)
1104{
1105 long offset, len;
1106 struct addr_location al;
1107 bool kernel;
1108 struct dso *dso;
1109 int ret = 0;
1110
1111 if (!start || !end)
1112 return 0;
1113
1114 kernel = machine__kernel_ip(machine, start);
1115 if (kernel)
1116 *cpumode = PERF_RECORD_MISC_KERNEL;
1117 else
1118 *cpumode = PERF_RECORD_MISC_USER;
1119
1120 /*
1121 * Block overlaps between kernel and user.
1122 * This can happen due to ring filtering
1123 * On Intel CPUs the entry into the kernel is filtered,
1124 * but the exit is not. Let the caller patch it up.
1125 */
1126 if (kernel != machine__kernel_ip(machine, end)) {
1127 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
1128 return -ENXIO;
1129 }
1130
1131 if (end - start > MAXBB - MAXINSN) {
1132 if (last)
1133 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
1134 else
1135 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
1136 return 0;
1137 }
1138
1139 addr_location__init(&al);
1140 if (!thread__find_map(thread, *cpumode, start, &al) || (dso = map__dso(al.map)) == NULL) {
1141 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
1142 goto out;
1143 }
1144 if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR) {
1145 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
1146 goto out;
1147 }
1148
1149 /* Load maps to ensure dso->is_64_bit has been updated */
1150 map__load(al.map);
1151
1152 offset = map__map_ip(al.map, start);
1153 len = dso__data_read_offset(dso, machine, offset, (u8 *)buffer,
1154 end - start + MAXINSN);
1155
1156 *is64bit = dso__is_64_bit(dso);
1157 if (len <= 0)
1158 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
1159 start, end);
1160 ret = len;
1161out:
1162 addr_location__exit(&al);
1163 return ret;
1164}
1165
1166static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srccode_state *state)
1167{
1168 char *srcfile;
1169 int ret = 0;
1170 unsigned line;
1171 int len;
1172 char *srccode;
1173 struct dso *dso;
1174
1175 if (!map || (dso = map__dso(map)) == NULL)
1176 return 0;
1177 srcfile = get_srcline_split(dso,
1178 map__rip_2objdump(map, addr),
1179 &line);
1180 if (!srcfile)
1181 return 0;
1182
1183 /* Avoid redundant printing */
1184 if (state &&
1185 state->srcfile &&
1186 !strcmp(state->srcfile, srcfile) &&
1187 state->line == line) {
1188 free(srcfile);
1189 return 0;
1190 }
1191
1192 srccode = find_sourceline(srcfile, line, &len);
1193 if (!srccode)
1194 goto out_free_line;
1195
1196 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
1197
1198 if (state) {
1199 state->srcfile = srcfile;
1200 state->line = line;
1201 }
1202 return ret;
1203
1204out_free_line:
1205 free(srcfile);
1206 return ret;
1207}
1208
1209static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
1210{
1211 struct addr_location al;
1212 int ret = 0;
1213
1214 addr_location__init(&al);
1215 thread__find_map(thread, cpumode, addr, &al);
1216 if (!al.map)
1217 goto out;
1218 ret = map__fprintf_srccode(al.map, al.addr, stdout,
1219 thread__srccode_state(thread));
1220 if (ret)
1221 ret += printf("\n");
1222out:
1223 addr_location__exit(&al);
1224 return ret;
1225}
1226
1227static int any_dump_insn(struct evsel *evsel __maybe_unused,
1228 struct perf_insn *x, uint64_t ip,
1229 u8 *inbuf, int inlen, int *lenp,
1230 FILE *fp)
1231{
1232 if (PRINT_FIELD(BRSTACKDISASM)) {
1233 int printed = fprintf_insn_asm(x->machine, x->thread, x->cpumode, x->is64bit,
1234 (uint8_t *)inbuf, inlen, ip, lenp,
1235 PRINT_INSN_IMM_HEX, fp);
1236
1237 if (printed > 0)
1238 return printed;
1239 }
1240 return fprintf(fp, "%s", dump_insn(x, ip, inbuf, inlen, lenp));
1241}
1242
1243static int add_padding(FILE *fp, int printed, int padding)
1244{
1245 if (printed >= 0 && printed < padding)
1246 printed += fprintf(fp, "%*s", padding - printed, "");
1247 return printed;
1248}
1249
1250static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
1251 struct perf_insn *x, u8 *inbuf, int len,
1252 int insn, FILE *fp, int *total_cycles,
1253 struct evsel *evsel,
1254 struct thread *thread,
1255 u64 br_cntr)
1256{
1257 int ilen = 0;
1258 int printed = fprintf(fp, "\t%016" PRIx64 "\t", ip);
1259
1260 printed += add_padding(fp, any_dump_insn(evsel, x, ip, inbuf, len, &ilen, fp), 30);
1261 printed += fprintf(fp, "\t");
1262
1263 if (PRINT_FIELD(BRSTACKINSNLEN))
1264 printed += fprintf(fp, "ilen: %d\t", ilen);
1265
1266 if (PRINT_FIELD(SRCLINE)) {
1267 struct addr_location al;
1268
1269 addr_location__init(&al);
1270 thread__find_map(thread, x->cpumode, ip, &al);
1271 printed += map__fprintf_srcline(al.map, al.addr, " srcline: ", fp);
1272 printed += fprintf(fp, "\t");
1273 addr_location__exit(&al);
1274 }
1275
1276 if (PRINT_FIELD(BRCNTR)) {
1277 struct evsel *pos = evsel__leader(evsel);
1278 unsigned int i = 0, j, num, mask, width, numprinted = 0;
1279
1280 perf_env__find_br_cntr_info(evsel__env(evsel), NULL, &width);
1281 mask = (1L << width) - 1;
1282 printed += fprintf(fp, "\t# br_cntr: ");
1283 evlist__for_each_entry_from(evsel->evlist, pos) {
1284 if (!(pos->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS))
1285 continue;
1286 if (evsel__leader(pos) != evsel__leader(evsel))
1287 break;
1288
1289 num = (br_cntr >> (i++ * width)) & mask;
1290 numprinted += num;
1291 if (!verbose) {
1292 for (j = 0; j < num; j++)
1293 printed += fprintf(fp, "%s", pos->abbr_name);
1294 if (mask && (num == mask))
1295 printed += fprintf(fp, "+");
1296 } else {
1297 printed += fprintf(fp, "%s %d%s", pos->name,
1298 num, mask && (num == mask) ? "+ " : " ");
1299 }
1300 }
1301 if (numprinted == 0 && !verbose)
1302 printed += fprintf(fp, "-");
1303 printed += fprintf(fp, " ");
1304 }
1305
1306 printed += fprintf(fp, "%s%s%s%s%s",
1307 !PRINT_FIELD(BRCNTR) ? "#" : "",
1308 en->flags.predicted ? " PRED" : "",
1309 en->flags.mispred ? " MISPRED" : "",
1310 en->flags.in_tx ? " INTX" : "",
1311 en->flags.abort ? " ABORT" : "");
1312 if (en->flags.cycles) {
1313 *total_cycles += en->flags.cycles;
1314 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
1315 if (insn)
1316 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
1317 }
1318
1319 return printed + fprintf(fp, "\n");
1320}
1321
1322static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
1323 u8 cpumode, int cpu, struct symbol **lastsym,
1324 struct evsel *evsel, FILE *fp)
1325{
1326 struct addr_location al;
1327 int off, printed = 0, ret = 0;
1328
1329 addr_location__init(&al);
1330 thread__find_map(thread, cpumode, addr, &al);
1331
1332 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
1333 goto out;
1334
1335 al.cpu = cpu;
1336 al.sym = NULL;
1337 if (al.map)
1338 al.sym = map__find_symbol(al.map, al.addr);
1339
1340 if (!al.sym)
1341 goto out;
1342
1343 if (al.addr < al.sym->end)
1344 off = al.addr - al.sym->start;
1345 else
1346 off = al.addr - map__start(al.map) - al.sym->start;
1347 printed += fprintf(fp, "\t%s", al.sym->name);
1348 if (off)
1349 printed += fprintf(fp, "%+d", off);
1350 printed += fprintf(fp, ":");
1351 if (PRINT_FIELD(SRCLINE))
1352 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
1353 printed += fprintf(fp, "\n");
1354 *lastsym = al.sym;
1355
1356 ret = printed;
1357out:
1358 addr_location__exit(&al);
1359 return ret;
1360}
1361
1362static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1363 struct evsel *evsel,
1364 struct thread *thread,
1365 struct perf_event_attr *attr,
1366 struct machine *machine, FILE *fp)
1367{
1368 struct branch_stack *br = sample->branch_stack;
1369 struct branch_entry *entries = perf_sample__branch_entries(sample);
1370 u64 start, end;
1371 int i, insn, len, nr, ilen, printed = 0;
1372 struct perf_insn x;
1373 u8 buffer[MAXBB];
1374 unsigned off;
1375 struct symbol *lastsym = NULL;
1376 int total_cycles = 0;
1377 u64 br_cntr = 0;
1378
1379 if (!(br && br->nr))
1380 return 0;
1381 nr = br->nr;
1382 if (max_blocks && nr > max_blocks + 1)
1383 nr = max_blocks + 1;
1384
1385 x.thread = thread;
1386 x.machine = machine;
1387 x.cpu = sample->cpu;
1388
1389 if (PRINT_FIELD(BRCNTR) && sample->branch_stack_cntr)
1390 br_cntr = sample->branch_stack_cntr[nr - 1];
1391
1392 printed += fprintf(fp, "%c", '\n');
1393
1394 /* Handle first from jump, of which we don't know the entry. */
1395 len = grab_bb(buffer, entries[nr-1].from,
1396 entries[nr-1].from,
1397 machine, thread, &x.is64bit, &x.cpumode, false);
1398 if (len > 0) {
1399 printed += ip__fprintf_sym(entries[nr - 1].from, thread,
1400 x.cpumode, x.cpu, &lastsym, evsel, fp);
1401 printed += ip__fprintf_jump(entries[nr - 1].from, &entries[nr - 1],
1402 &x, buffer, len, 0, fp, &total_cycles,
1403 evsel, thread, br_cntr);
1404 if (PRINT_FIELD(SRCCODE))
1405 printed += print_srccode(thread, x.cpumode, entries[nr - 1].from);
1406 }
1407
1408 /* Print all blocks */
1409 for (i = nr - 2; i >= 0; i--) {
1410 if (entries[i].from || entries[i].to)
1411 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1412 entries[i].from,
1413 entries[i].to);
1414 start = entries[i + 1].to;
1415 end = entries[i].from;
1416
1417 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1418 /* Patch up missing kernel transfers due to ring filters */
1419 if (len == -ENXIO && i > 0) {
1420 end = entries[--i].from;
1421 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1422 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1423 }
1424 if (len <= 0)
1425 continue;
1426
1427 insn = 0;
1428 for (off = 0; off < (unsigned)len; off += ilen) {
1429 uint64_t ip = start + off;
1430
1431 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, evsel, fp);
1432 if (ip == end) {
1433 if (PRINT_FIELD(BRCNTR) && sample->branch_stack_cntr)
1434 br_cntr = sample->branch_stack_cntr[i];
1435 printed += ip__fprintf_jump(ip, &entries[i], &x, buffer + off, len - off, ++insn, fp,
1436 &total_cycles, evsel, thread, br_cntr);
1437 if (PRINT_FIELD(SRCCODE))
1438 printed += print_srccode(thread, x.cpumode, ip);
1439 break;
1440 } else {
1441 ilen = 0;
1442 printed += fprintf(fp, "\t%016" PRIx64 "\t", ip);
1443 printed += any_dump_insn(evsel, &x, ip, buffer + off, len - off, &ilen, fp);
1444 if (PRINT_FIELD(BRSTACKINSNLEN))
1445 printed += fprintf(fp, "\tilen: %d", ilen);
1446 printed += fprintf(fp, "\n");
1447 if (ilen == 0)
1448 break;
1449 if (PRINT_FIELD(SRCCODE))
1450 print_srccode(thread, x.cpumode, ip);
1451 insn++;
1452 }
1453 }
1454 if (off != end - start)
1455 printed += fprintf(fp, "\tmismatch of LBR data and executable\n");
1456 }
1457
1458 /*
1459 * Hit the branch? In this case we are already done, and the target
1460 * has not been executed yet.
1461 */
1462 if (entries[0].from == sample->ip)
1463 goto out;
1464 if (entries[0].flags.abort)
1465 goto out;
1466
1467 /*
1468 * Print final block up to sample
1469 *
1470 * Due to pipeline delays the LBRs might be missing a branch
1471 * or two, which can result in very large or negative blocks
1472 * between final branch and sample. When this happens just
1473 * continue walking after the last TO.
1474 */
1475 start = entries[0].to;
1476 end = sample->ip;
1477 if (end < start) {
1478 /* Missing jump. Scan 128 bytes for the next branch */
1479 end = start + 128;
1480 }
1481 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
1482 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, evsel, fp);
1483 if (len <= 0) {
1484 /* Print at least last IP if basic block did not work */
1485 len = grab_bb(buffer, sample->ip, sample->ip,
1486 machine, thread, &x.is64bit, &x.cpumode, false);
1487 if (len <= 0)
1488 goto out;
1489 ilen = 0;
1490 printed += fprintf(fp, "\t%016" PRIx64 "\t", sample->ip);
1491 printed += any_dump_insn(evsel, &x, sample->ip, buffer, len, &ilen, fp);
1492 if (PRINT_FIELD(BRSTACKINSNLEN))
1493 printed += fprintf(fp, "\tilen: %d", ilen);
1494 printed += fprintf(fp, "\n");
1495 if (PRINT_FIELD(SRCCODE))
1496 print_srccode(thread, x.cpumode, sample->ip);
1497 goto out;
1498 }
1499 for (off = 0; off <= end - start; off += ilen) {
1500 ilen = 0;
1501 printed += fprintf(fp, "\t%016" PRIx64 "\t", start + off);
1502 printed += any_dump_insn(evsel, &x, start + off, buffer + off, len - off, &ilen, fp);
1503 if (PRINT_FIELD(BRSTACKINSNLEN))
1504 printed += fprintf(fp, "\tilen: %d", ilen);
1505 printed += fprintf(fp, "\n");
1506 if (ilen == 0)
1507 break;
1508 if ((attr->branch_sample_type == 0 || attr->branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
1509 && arch_is_uncond_branch(buffer + off, len - off, x.is64bit)
1510 && start + off != sample->ip) {
1511 /*
1512 * Hit a missing branch. Just stop.
1513 */
1514 printed += fprintf(fp, "\t... not reaching sample ...\n");
1515 break;
1516 }
1517 if (PRINT_FIELD(SRCCODE))
1518 print_srccode(thread, x.cpumode, start + off);
1519 }
1520out:
1521 return printed;
1522}
1523
1524static int perf_sample__fprintf_addr(struct perf_sample *sample,
1525 struct thread *thread,
1526 struct evsel *evsel, FILE *fp)
1527{
1528 struct addr_location al;
1529 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
1530
1531 addr_location__init(&al);
1532 if (!sample_addr_correlates_sym(&evsel->core.attr))
1533 goto out;
1534
1535 thread__resolve(thread, &al, sample);
1536
1537 if (PRINT_FIELD(SYM)) {
1538 printed += fprintf(fp, " ");
1539 if (PRINT_FIELD(SYMOFFSET))
1540 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
1541 else
1542 printed += symbol__fprintf_symname(al.sym, fp);
1543 }
1544
1545 if (PRINT_FIELD(DSO))
1546 printed += map__fprintf_dsoname_dsoff(al.map, PRINT_FIELD(DSOFF), al.addr, fp);
1547out:
1548 addr_location__exit(&al);
1549 return printed;
1550}
1551
1552static const char *resolve_branch_sym(struct perf_sample *sample,
1553 struct evsel *evsel,
1554 struct thread *thread,
1555 struct addr_location *al,
1556 struct addr_location *addr_al,
1557 u64 *ip)
1558{
1559 const char *name = NULL;
1560
1561 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1562 if (sample_addr_correlates_sym(&evsel->core.attr)) {
1563 if (!addr_al->thread)
1564 thread__resolve(thread, addr_al, sample);
1565 if (addr_al->sym)
1566 name = addr_al->sym->name;
1567 else
1568 *ip = sample->addr;
1569 } else {
1570 *ip = sample->addr;
1571 }
1572 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1573 if (al->sym)
1574 name = al->sym->name;
1575 else
1576 *ip = sample->ip;
1577 }
1578 return name;
1579}
1580
1581static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1582 struct evsel *evsel,
1583 struct thread *thread,
1584 struct addr_location *al,
1585 struct addr_location *addr_al,
1586 FILE *fp)
1587{
1588 size_t depth = thread_stack__depth(thread, sample->cpu);
1589 const char *name = NULL;
1590 static int spacing;
1591 int len = 0;
1592 int dlen = 0;
1593 u64 ip = 0;
1594
1595 /*
1596 * The 'return' has already been popped off the stack so the depth has
1597 * to be adjusted to match the 'call'.
1598 */
1599 if (thread__ts(thread) && sample->flags & PERF_IP_FLAG_RETURN)
1600 depth += 1;
1601
1602 name = resolve_branch_sym(sample, evsel, thread, al, addr_al, &ip);
1603
1604 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1605 dlen += fprintf(fp, "(");
1606 dlen += map__fprintf_dsoname(al->map, fp);
1607 dlen += fprintf(fp, ")\t");
1608 }
1609
1610 if (name)
1611 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
1612 else if (ip)
1613 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
1614
1615 if (len < 0)
1616 return len;
1617
1618 /*
1619 * Try to keep the output length from changing frequently so that the
1620 * output lines up more nicely.
1621 */
1622 if (len > spacing || (len && len < spacing - 52))
1623 spacing = round_up(len + 4, 32);
1624
1625 if (len < spacing)
1626 len += fprintf(fp, "%*s", spacing - len, "");
1627
1628 return len + dlen;
1629}
1630
1631static int perf_sample__fprintf_insn(struct perf_sample *sample,
1632 struct evsel *evsel,
1633 struct perf_event_attr *attr,
1634 struct thread *thread,
1635 struct machine *machine, FILE *fp,
1636 struct addr_location *al)
1637{
1638 int printed = 0;
1639
1640 perf_sample__fetch_insn(sample, thread, machine);
1641
1642 if (PRINT_FIELD(INSNLEN))
1643 printed += fprintf(fp, " ilen: %d", sample->insn_len);
1644 if (PRINT_FIELD(INSN) && sample->insn_len) {
1645 printed += fprintf(fp, " insn: ");
1646 printed += sample__fprintf_insn_raw(sample, fp);
1647 }
1648 if (PRINT_FIELD(DISASM) && sample->insn_len) {
1649 printed += fprintf(fp, "\t\t");
1650 printed += sample__fprintf_insn_asm(sample, thread, machine, fp, al);
1651 }
1652 if (PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN) || PRINT_FIELD(BRSTACKDISASM))
1653 printed += perf_sample__fprintf_brstackinsn(sample, evsel, thread, attr, machine, fp);
1654
1655 return printed;
1656}
1657
1658static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1659 struct evsel *evsel, FILE *fp)
1660{
1661 unsigned int ipc;
1662
1663 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1664 return 0;
1665
1666 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1667
1668 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1669 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1670}
1671
1672static int perf_sample__fprintf_bts(struct perf_sample *sample,
1673 struct evsel *evsel,
1674 struct thread *thread,
1675 struct addr_location *al,
1676 struct addr_location *addr_al,
1677 struct machine *machine, FILE *fp)
1678{
1679 struct perf_event_attr *attr = &evsel->core.attr;
1680 unsigned int type = evsel__output_type(evsel);
1681 bool print_srcline_last = false;
1682 int printed = 0;
1683
1684 if (PRINT_FIELD(CALLINDENT))
1685 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, addr_al, fp);
1686
1687 /* print branch_from information */
1688 if (PRINT_FIELD(IP)) {
1689 unsigned int print_opts = output[type].print_ip_opts;
1690 struct callchain_cursor *cursor = NULL;
1691
1692 if (symbol_conf.use_callchain && sample->callchain) {
1693 cursor = get_tls_callchain_cursor();
1694 if (thread__resolve_callchain(al->thread, cursor,
1695 sample, NULL, NULL,
1696 scripting_max_stack))
1697 cursor = NULL;
1698 }
1699 if (cursor == NULL) {
1700 printed += fprintf(fp, " ");
1701 if (print_opts & EVSEL__PRINT_SRCLINE) {
1702 print_srcline_last = true;
1703 print_opts &= ~EVSEL__PRINT_SRCLINE;
1704 }
1705 } else
1706 printed += fprintf(fp, "\n");
1707
1708 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor,
1709 symbol_conf.bt_stop_list, fp);
1710 }
1711
1712 /* print branch_to information */
1713 if (PRINT_FIELD(ADDR) ||
1714 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
1715 !output[type].user_set)) {
1716 printed += fprintf(fp, " => ");
1717 printed += perf_sample__fprintf_addr(sample, thread, evsel, fp);
1718 }
1719
1720 printed += perf_sample__fprintf_ipc(sample, evsel, fp);
1721
1722 if (print_srcline_last)
1723 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
1724
1725 printed += perf_sample__fprintf_insn(sample, evsel, attr, thread, machine, fp, al);
1726 printed += fprintf(fp, "\n");
1727 if (PRINT_FIELD(SRCCODE)) {
1728 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1729 thread__srccode_state(thread));
1730 if (ret) {
1731 printed += ret;
1732 printed += printf("\n");
1733 }
1734 }
1735 return printed;
1736}
1737
1738static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1739{
1740 char str[SAMPLE_FLAGS_BUF_SIZE];
1741 int ret;
1742
1743 ret = perf_sample__sprintf_flags(flags, str, sizeof(str));
1744 if (ret < 0)
1745 return fprintf(fp, " raw flags:0x%-*x ",
1746 SAMPLE_FLAGS_STR_ALIGNED_SIZE - 12, flags);
1747
1748 return fprintf(fp, " %-*s ", SAMPLE_FLAGS_STR_ALIGNED_SIZE, str);
1749}
1750
1751struct printer_data {
1752 int line_no;
1753 bool hit_nul;
1754 bool is_printable;
1755};
1756
1757static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1758 unsigned int val,
1759 void *extra, FILE *fp)
1760{
1761 unsigned char ch = (unsigned char)val;
1762 struct printer_data *printer_data = extra;
1763 int printed = 0;
1764
1765 switch (op) {
1766 case BINARY_PRINT_DATA_BEGIN:
1767 printed += fprintf(fp, "\n");
1768 break;
1769 case BINARY_PRINT_LINE_BEGIN:
1770 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
1771 " ");
1772 break;
1773 case BINARY_PRINT_ADDR:
1774 printed += fprintf(fp, " %04x:", val);
1775 break;
1776 case BINARY_PRINT_NUM_DATA:
1777 printed += fprintf(fp, " %02x", val);
1778 break;
1779 case BINARY_PRINT_NUM_PAD:
1780 printed += fprintf(fp, " ");
1781 break;
1782 case BINARY_PRINT_SEP:
1783 printed += fprintf(fp, " ");
1784 break;
1785 case BINARY_PRINT_CHAR_DATA:
1786 if (printer_data->hit_nul && ch)
1787 printer_data->is_printable = false;
1788
1789 if (!isprint(ch)) {
1790 printed += fprintf(fp, "%c", '.');
1791
1792 if (!printer_data->is_printable)
1793 break;
1794
1795 if (ch == '\0')
1796 printer_data->hit_nul = true;
1797 else
1798 printer_data->is_printable = false;
1799 } else {
1800 printed += fprintf(fp, "%c", ch);
1801 }
1802 break;
1803 case BINARY_PRINT_CHAR_PAD:
1804 printed += fprintf(fp, " ");
1805 break;
1806 case BINARY_PRINT_LINE_END:
1807 printed += fprintf(fp, "\n");
1808 printer_data->line_no++;
1809 break;
1810 case BINARY_PRINT_DATA_END:
1811 default:
1812 break;
1813 }
1814
1815 return printed;
1816}
1817
1818static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
1819{
1820 unsigned int nr_bytes = sample->raw_size;
1821 struct printer_data printer_data = {0, false, true};
1822 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1823 sample__fprintf_bpf_output, &printer_data, fp);
1824
1825 if (printer_data.is_printable && printer_data.hit_nul)
1826 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1827
1828 return printed;
1829}
1830
1831static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
1832{
1833 if (len > 0 && len < spacing)
1834 return fprintf(fp, "%*s", spacing - len, "");
1835
1836 return 0;
1837}
1838
1839static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
1840{
1841 return perf_sample__fprintf_spacing(len, 34, fp);
1842}
1843
1844/* If a value contains only printable ASCII characters padded with NULLs */
1845static bool ptw_is_prt(u64 val)
1846{
1847 char c;
1848 u32 i;
1849
1850 for (i = 0; i < sizeof(val); i++) {
1851 c = ((char *)&val)[i];
1852 if (!c)
1853 break;
1854 if (!isprint(c) || !isascii(c))
1855 return false;
1856 }
1857 for (; i < sizeof(val); i++) {
1858 c = ((char *)&val)[i];
1859 if (c)
1860 return false;
1861 }
1862 return true;
1863}
1864
1865static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
1866{
1867 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1868 char str[sizeof(u64) + 1] = "";
1869 int len;
1870 u64 val;
1871
1872 if (perf_sample__bad_synth_size(sample, *data))
1873 return 0;
1874
1875 val = le64_to_cpu(data->payload);
1876 if (ptw_is_prt(val)) {
1877 memcpy(str, &val, sizeof(val));
1878 str[sizeof(val)] = 0;
1879 }
1880 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " %s ",
1881 data->ip, val, str);
1882 return len + perf_sample__fprintf_pt_spacing(len, fp);
1883}
1884
1885static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
1886{
1887 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1888 int len;
1889
1890 if (perf_sample__bad_synth_size(sample, *data))
1891 return 0;
1892
1893 len = fprintf(fp, " hints: %#x extensions: %#x ",
1894 data->hints, data->extensions);
1895 return len + perf_sample__fprintf_pt_spacing(len, fp);
1896}
1897
1898static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
1899{
1900 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1901 int len;
1902
1903 if (perf_sample__bad_synth_size(sample, *data))
1904 return 0;
1905
1906 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1907 data->hw, data->cstate, data->subcstate);
1908 return len + perf_sample__fprintf_pt_spacing(len, fp);
1909}
1910
1911static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
1912{
1913 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1914 int len;
1915
1916 if (perf_sample__bad_synth_size(sample, *data))
1917 return 0;
1918
1919 len = fprintf(fp, " IP: %u ", data->ip);
1920 return len + perf_sample__fprintf_pt_spacing(len, fp);
1921}
1922
1923static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
1924{
1925 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1926 int len;
1927
1928 if (perf_sample__bad_synth_size(sample, *data))
1929 return 0;
1930
1931 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1932 data->deepest_cstate, data->last_cstate,
1933 data->wake_reason);
1934 return len + perf_sample__fprintf_pt_spacing(len, fp);
1935}
1936
1937static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
1938{
1939 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1940 unsigned int percent, freq;
1941 int len;
1942
1943 if (perf_sample__bad_synth_size(sample, *data))
1944 return 0;
1945
1946 freq = (le32_to_cpu(data->freq) + 500) / 1000;
1947 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
1948 if (data->max_nonturbo) {
1949 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1950 len += fprintf(fp, "(%3u%%) ", percent);
1951 }
1952 return len + perf_sample__fprintf_pt_spacing(len, fp);
1953}
1954
1955static int perf_sample__fprintf_synth_psb(struct perf_sample *sample, FILE *fp)
1956{
1957 struct perf_synth_intel_psb *data = perf_sample__synth_ptr(sample);
1958 int len;
1959
1960 if (perf_sample__bad_synth_size(sample, *data))
1961 return 0;
1962
1963 len = fprintf(fp, " psb offs: %#" PRIx64, data->offset);
1964 return len + perf_sample__fprintf_pt_spacing(len, fp);
1965}
1966
1967/* Intel PT Event Trace */
1968static int perf_sample__fprintf_synth_evt(struct perf_sample *sample, FILE *fp)
1969{
1970 struct perf_synth_intel_evt *data = perf_sample__synth_ptr(sample);
1971 const char *cfe[32] = {NULL, "INTR", "IRET", "SMI", "RSM", "SIPI",
1972 "INIT", "VMENTRY", "VMEXIT", "VMEXIT_INTR",
1973 "SHUTDOWN", NULL, "UINTR", "UIRET"};
1974 const char *evd[64] = {"PFA", "VMXQ", "VMXR"};
1975 const char *s;
1976 int len, i;
1977
1978 if (perf_sample__bad_synth_size(sample, *data))
1979 return 0;
1980
1981 s = cfe[data->type];
1982 if (s) {
1983 len = fprintf(fp, " cfe: %s IP: %d vector: %u",
1984 s, data->ip, data->vector);
1985 } else {
1986 len = fprintf(fp, " cfe: %u IP: %d vector: %u",
1987 data->type, data->ip, data->vector);
1988 }
1989 for (i = 0; i < data->evd_cnt; i++) {
1990 unsigned int et = data->evd[i].evd_type & 0x3f;
1991
1992 s = evd[et];
1993 if (s) {
1994 len += fprintf(fp, " %s: %#" PRIx64,
1995 s, data->evd[i].payload);
1996 } else {
1997 len += fprintf(fp, " EVD_%u: %#" PRIx64,
1998 et, data->evd[i].payload);
1999 }
2000 }
2001 return len + perf_sample__fprintf_pt_spacing(len, fp);
2002}
2003
2004static int perf_sample__fprintf_synth_iflag_chg(struct perf_sample *sample, FILE *fp)
2005{
2006 struct perf_synth_intel_iflag_chg *data = perf_sample__synth_ptr(sample);
2007 int len;
2008
2009 if (perf_sample__bad_synth_size(sample, *data))
2010 return 0;
2011
2012 len = fprintf(fp, " IFLAG: %d->%d %s branch", !data->iflag, data->iflag,
2013 data->via_branch ? "via" : "non");
2014 return len + perf_sample__fprintf_pt_spacing(len, fp);
2015}
2016
2017static int perf_sample__fprintf_synth_vpadtl(struct perf_sample *data, FILE *fp)
2018{
2019 struct powerpc_vpadtl_entry *dtl = (struct powerpc_vpadtl_entry *)data->raw_data;
2020 int len;
2021
2022 len = fprintf(fp, "timebase: %" PRIu64 " dispatch_reason:%s, preempt_reason:%s,\n"
2023 "enqueue_to_dispatch_time:%d, ready_to_enqueue_time:%d,"
2024 "waiting_to_ready_time:%d, processor_id: %d",
2025 get_unaligned_be64(&dtl->timebase),
2026 dispatch_reasons[dtl->dispatch_reason],
2027 preempt_reasons[dtl->preempt_reason],
2028 be32_to_cpu(dtl->enqueue_to_dispatch_time),
2029 be32_to_cpu(dtl->ready_to_enqueue_time),
2030 be32_to_cpu(dtl->waiting_to_ready_time),
2031 be16_to_cpu(dtl->processor_id));
2032
2033 return len;
2034}
2035
2036static int perf_sample__fprintf_synth(struct perf_sample *sample,
2037 struct evsel *evsel, FILE *fp)
2038{
2039 switch (evsel->core.attr.config) {
2040 case PERF_SYNTH_INTEL_PTWRITE:
2041 return perf_sample__fprintf_synth_ptwrite(sample, fp);
2042 case PERF_SYNTH_INTEL_MWAIT:
2043 return perf_sample__fprintf_synth_mwait(sample, fp);
2044 case PERF_SYNTH_INTEL_PWRE:
2045 return perf_sample__fprintf_synth_pwre(sample, fp);
2046 case PERF_SYNTH_INTEL_EXSTOP:
2047 return perf_sample__fprintf_synth_exstop(sample, fp);
2048 case PERF_SYNTH_INTEL_PWRX:
2049 return perf_sample__fprintf_synth_pwrx(sample, fp);
2050 case PERF_SYNTH_INTEL_CBR:
2051 return perf_sample__fprintf_synth_cbr(sample, fp);
2052 case PERF_SYNTH_INTEL_PSB:
2053 return perf_sample__fprintf_synth_psb(sample, fp);
2054 case PERF_SYNTH_INTEL_EVT:
2055 return perf_sample__fprintf_synth_evt(sample, fp);
2056 case PERF_SYNTH_INTEL_IFLAG_CHG:
2057 return perf_sample__fprintf_synth_iflag_chg(sample, fp);
2058 case PERF_SYNTH_POWERPC_VPA_DTL:
2059 return perf_sample__fprintf_synth_vpadtl(sample, fp);
2060 default:
2061 break;
2062 }
2063
2064 return 0;
2065}
2066
2067static int evlist__max_name_len(struct evlist *evlist)
2068{
2069 struct evsel *evsel;
2070 int max = 0;
2071
2072 evlist__for_each_entry(evlist, evsel) {
2073 int len = strlen(evsel__name(evsel));
2074
2075 max = MAX(len, max);
2076 }
2077
2078 return max;
2079}
2080
2081static int data_src__fprintf(u64 data_src, FILE *fp)
2082{
2083 struct mem_info *mi = mem_info__new();
2084 char decode[100];
2085 char out[100];
2086 static int maxlen;
2087 int len;
2088
2089 if (!mi)
2090 return -ENOMEM;
2091
2092 mem_info__data_src(mi)->val = data_src;
2093 perf_script__meminfo_scnprintf(decode, 100, mi);
2094 mem_info__put(mi);
2095
2096 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
2097 if (maxlen < len)
2098 maxlen = len;
2099
2100 return fprintf(fp, "%-*s", maxlen, out);
2101}
2102
2103struct metric_ctx {
2104 struct perf_sample *sample;
2105 struct thread *thread;
2106 struct evsel *evsel;
2107 FILE *fp;
2108};
2109
2110static void script_print_metric(struct perf_stat_config *config __maybe_unused,
2111 void *ctx, enum metric_threshold_classify thresh,
2112 const char *fmt, const char *unit, double val)
2113{
2114 struct metric_ctx *mctx = ctx;
2115 const char *color = metric_threshold_classify__color(thresh);
2116
2117 if (!fmt)
2118 return;
2119 perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
2120 PERF_RECORD_SAMPLE, mctx->fp);
2121 fputs("\tmetric: ", mctx->fp);
2122 if (color)
2123 color_fprintf(mctx->fp, color, fmt, val);
2124 else
2125 printf(fmt, val);
2126 fprintf(mctx->fp, " %s\n", unit);
2127}
2128
2129static void script_new_line(struct perf_stat_config *config __maybe_unused,
2130 void *ctx)
2131{
2132 struct metric_ctx *mctx = ctx;
2133
2134 perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
2135 PERF_RECORD_SAMPLE, mctx->fp);
2136 fputs("\tmetric: ", mctx->fp);
2137}
2138
2139struct script_find_metrics_args {
2140 struct evlist *evlist;
2141 bool system_wide;
2142};
2143
2144static struct evsel *map_metric_evsel_to_script_evsel(struct evlist *script_evlist,
2145 struct evsel *metric_evsel)
2146{
2147 struct evsel *script_evsel;
2148
2149 evlist__for_each_entry(script_evlist, script_evsel) {
2150 /* Skip if perf_event_attr differ. */
2151 if (metric_evsel->core.attr.type != script_evsel->core.attr.type)
2152 continue;
2153 if (metric_evsel->core.attr.config != script_evsel->core.attr.config)
2154 continue;
2155 /* Skip if the script event has a metric_id that doesn't match. */
2156 if (script_evsel->metric_id &&
2157 strcmp(evsel__metric_id(metric_evsel), evsel__metric_id(script_evsel))) {
2158 pr_debug("Skipping matching evsel due to differing metric ids '%s' vs '%s'\n",
2159 evsel__metric_id(metric_evsel), evsel__metric_id(script_evsel));
2160 continue;
2161 }
2162 return script_evsel;
2163 }
2164 return NULL;
2165}
2166
2167static int script_find_metrics(const struct pmu_metric *pm,
2168 const struct pmu_metrics_table *table __maybe_unused,
2169 void *data)
2170{
2171 struct script_find_metrics_args *args = data;
2172 struct evlist *script_evlist = args->evlist;
2173 struct evlist *metric_evlist = evlist__new();
2174 struct evsel *metric_evsel;
2175 int ret = metricgroup__parse_groups(metric_evlist,
2176 /*pmu=*/"all",
2177 /*cputype_filter=*/false,
2178 pm->metric_name,
2179 /*metric_no_group=*/false,
2180 /*metric_no_merge=*/false,
2181 /*metric_no_threshold=*/true,
2182 /*user_requested_cpu_list=*/NULL,
2183 args->system_wide,
2184 /*hardware_aware_grouping=*/false);
2185
2186 if (ret) {
2187 /* Metric parsing failed but continue the search. */
2188 goto out;
2189 }
2190
2191 /*
2192 * Check the script_evlist has an entry for each metric_evlist entry. If
2193 * the script evsel was already set up avoid changing data that may
2194 * break it.
2195 */
2196 evlist__for_each_entry(metric_evlist, metric_evsel) {
2197 struct evsel *script_evsel =
2198 map_metric_evsel_to_script_evsel(script_evlist, metric_evsel);
2199 struct evsel *new_metric_leader;
2200
2201 if (!script_evsel) {
2202 pr_debug("Skipping metric '%s' as evsel '%s' / '%s' is missing\n",
2203 pm->metric_name, evsel__name(metric_evsel),
2204 evsel__metric_id(metric_evsel));
2205 goto out;
2206 }
2207
2208 if (script_evsel->metric_leader == NULL)
2209 continue;
2210
2211 if (metric_evsel->metric_leader == metric_evsel) {
2212 new_metric_leader = script_evsel;
2213 } else {
2214 new_metric_leader =
2215 map_metric_evsel_to_script_evsel(script_evlist,
2216 metric_evsel->metric_leader);
2217 }
2218 /* Mismatching evsel leaders. */
2219 if (script_evsel->metric_leader != new_metric_leader) {
2220 pr_debug("Skipping metric '%s' due to mismatching evsel metric leaders '%s' vs '%s'\n",
2221 pm->metric_name, evsel__metric_id(metric_evsel),
2222 evsel__metric_id(script_evsel));
2223 goto out;
2224 }
2225 }
2226 /*
2227 * Metric events match those in the script evlist, copy metric evsel
2228 * data into the script evlist.
2229 */
2230 evlist__for_each_entry(metric_evlist, metric_evsel) {
2231 struct evsel *script_evsel =
2232 map_metric_evsel_to_script_evsel(script_evlist, metric_evsel);
2233 struct metric_event *metric_me =
2234 metricgroup__lookup(evlist__metric_events(metric_evlist),
2235 metric_evsel,
2236 /*create=*/false);
2237
2238 if (script_evsel->metric_id == NULL) {
2239 script_evsel->metric_id = metric_evsel->metric_id;
2240 metric_evsel->metric_id = NULL;
2241 }
2242
2243 if (script_evsel->metric_leader == NULL) {
2244 if (metric_evsel->metric_leader == metric_evsel) {
2245 script_evsel->metric_leader = script_evsel;
2246 } else {
2247 script_evsel->metric_leader =
2248 map_metric_evsel_to_script_evsel(script_evlist,
2249 metric_evsel->metric_leader);
2250 }
2251 }
2252
2253 if (metric_me) {
2254 struct metric_expr *expr;
2255 struct metric_event *script_me =
2256 metricgroup__lookup(evlist__metric_events(script_evlist),
2257 script_evsel,
2258 /*create=*/true);
2259
2260 if (!script_me) {
2261 /*
2262 * As the metric_expr is created, the only
2263 * failure is a lack of memory.
2264 */
2265 goto out;
2266 }
2267 list_splice_init(&metric_me->head, &script_me->head);
2268 list_for_each_entry(expr, &script_me->head, nd) {
2269 for (int i = 0; expr->metric_events[i]; i++) {
2270 expr->metric_events[i] =
2271 map_metric_evsel_to_script_evsel(script_evlist,
2272 expr->metric_events[i]);
2273 }
2274 }
2275 }
2276 }
2277 pr_debug("Found metric '%s' whose evsels match those of in the perf data\n",
2278 pm->metric_name);
2279out:
2280 evlist__put(metric_evlist);
2281 return 0;
2282}
2283
2284static struct aggr_cpu_id script_aggr_cpu_id_get(struct perf_stat_config *config __maybe_unused,
2285 struct perf_cpu cpu)
2286{
2287 return aggr_cpu_id__global(cpu, /*data=*/NULL);
2288}
2289
2290static void perf_sample__fprint_metric(struct thread *thread,
2291 struct evsel *evsel,
2292 struct perf_sample *sample,
2293 FILE *fp)
2294{
2295 static bool init_metrics;
2296 struct perf_stat_output_ctx ctx = {
2297 .print_metric = script_print_metric,
2298 .new_line = script_new_line,
2299 .ctx = &(struct metric_ctx) {
2300 .sample = sample,
2301 .thread = thread,
2302 .evsel = evsel,
2303 .fp = fp,
2304 },
2305 .force_header = false,
2306 };
2307 struct perf_counts_values *count, *old_count;
2308 int cpu_map_idx, thread_map_idx, aggr_idx;
2309 struct evsel *pos;
2310
2311 if (!init_metrics) {
2312 /* One time initialization of stat_config and metric data. */
2313 struct script_find_metrics_args args = {
2314 .evlist = evsel->evlist,
2315 .system_wide = perf_thread_map__pid(evsel->core.threads, /*idx=*/0) == -1,
2316
2317 };
2318 if (!stat_config.output)
2319 stat_config.output = stdout;
2320
2321 if (!stat_config.aggr_map) {
2322 /* TODO: currently only global aggregation is supported. */
2323 assert(stat_config.aggr_mode == AGGR_GLOBAL);
2324 stat_config.aggr_get_id = script_aggr_cpu_id_get;
2325 stat_config.aggr_map =
2326 cpu_aggr_map__new(evlist__core(evsel->evlist)->user_requested_cpus,
2327 aggr_cpu_id__global, /*data=*/NULL,
2328 /*needs_sort=*/false);
2329 }
2330
2331 metricgroup__for_each_metric(pmu_metrics_table__find(), script_find_metrics, &args);
2332 init_metrics = true;
2333 }
2334
2335 if (!evsel->stats) {
2336 if (evlist__alloc_stats(&stat_config, evsel->evlist, /*alloc_raw=*/true) < 0)
2337 return;
2338 }
2339 if (!evsel->stats->aggr) {
2340 if (evlist__alloc_aggr_stats(evsel->evlist, stat_config.aggr_map->nr) < 0)
2341 return;
2342 }
2343
2344 /* Update the evsel's count using the sample's data. */
2345 cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, (struct perf_cpu){sample->cpu});
2346 if (cpu_map_idx < 0) {
2347 /* Missing CPU, check for any CPU. */
2348 if (perf_cpu_map__cpu(evsel->core.cpus, /*idx=*/0).cpu == -1 ||
2349 sample->cpu == (u32)-1) {
2350 /* Place the counts in the which ever CPU is first in the map. */
2351 cpu_map_idx = 0;
2352 } else {
2353 pr_info("Missing CPU map entry for CPU %d\n", sample->cpu);
2354 return;
2355 }
2356 }
2357 thread_map_idx = perf_thread_map__idx(evsel->core.threads, sample->tid);
2358 if (thread_map_idx < 0) {
2359 /* Missing thread, check for any thread. */
2360 if (perf_thread_map__pid(evsel->core.threads, /*idx=*/0) == -1 ||
2361 sample->tid == (u32)-1) {
2362 /* Place the counts in the which ever thread is first in the map. */
2363 thread_map_idx = 0;
2364 } else {
2365 pr_info("Missing thread map entry for thread %d\n", sample->tid);
2366 return;
2367 }
2368 }
2369 count = perf_counts(evsel->counts, cpu_map_idx, thread_map_idx);
2370 old_count = perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread_map_idx);
2371 count->val = old_count->val + sample->period;
2372 count->run = old_count->run + 1;
2373 count->ena = old_count->ena + 1;
2374
2375 /* Update the aggregated stats. */
2376 perf_stat_process_counter(&stat_config, evsel);
2377
2378 /* Display all metrics. */
2379 evlist__for_each_entry(evsel->evlist, pos) {
2380 cpu_aggr_map__for_each_idx(aggr_idx, stat_config.aggr_map) {
2381 perf_stat__print_shadow_stats(&stat_config, pos,
2382 aggr_idx,
2383 &ctx);
2384 }
2385 }
2386}
2387
2388static bool show_event(struct perf_sample *sample,
2389 struct evsel *evsel,
2390 struct thread *thread,
2391 struct addr_location *al,
2392 struct addr_location *addr_al)
2393{
2394 int depth = thread_stack__depth(thread, sample->cpu);
2395
2396 if (!symbol_conf.graph_function)
2397 return true;
2398
2399 if (thread__filter(thread)) {
2400 if (depth <= thread__filter_entry_depth(thread)) {
2401 thread__set_filter(thread, false);
2402 return false;
2403 }
2404 return true;
2405 } else {
2406 const char *s = symbol_conf.graph_function;
2407 u64 ip;
2408 const char *name = resolve_branch_sym(sample, evsel, thread, al, addr_al,
2409 &ip);
2410 unsigned nlen;
2411
2412 if (!name)
2413 return false;
2414 nlen = strlen(name);
2415 while (*s) {
2416 unsigned len = strcspn(s, ",");
2417 if (nlen == len && !strncmp(name, s, len)) {
2418 thread__set_filter(thread, true);
2419 thread__set_filter_entry_depth(thread, depth);
2420 return true;
2421 }
2422 s += len;
2423 if (*s == ',')
2424 s++;
2425 }
2426 return false;
2427 }
2428}
2429
2430static void process_event(struct perf_script *script,
2431 struct perf_sample *sample,
2432 struct addr_location *al,
2433 struct addr_location *addr_al,
2434 struct machine *machine)
2435{
2436 struct thread *thread = al->thread;
2437 struct evsel *evsel = sample->evsel;
2438 struct perf_event_attr *attr = &evsel->core.attr;
2439 unsigned int type = evsel__output_type(evsel);
2440 struct evsel_script *es = evsel->priv;
2441 FILE *fp = es->fp;
2442 char str[PAGE_SIZE_NAME_LEN];
2443 uint32_t e_flags;
2444
2445 if (output[type].fields == 0)
2446 return;
2447
2448 ++es->samples;
2449
2450 perf_sample__fprintf_start(script, sample, thread, evsel,
2451 PERF_RECORD_SAMPLE, fp);
2452
2453 if (PRINT_FIELD(PERIOD))
2454 fprintf(fp, "%10" PRIu64 " ", sample->period);
2455
2456 if (PRINT_FIELD(EVNAME)) {
2457 const char *evname = evsel__name(evsel);
2458
2459 if (!script->name_width)
2460 script->name_width = evlist__max_name_len(script->session->evlist);
2461
2462 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
2463 }
2464
2465 if (print_flags)
2466 perf_sample__fprintf_flags(sample->flags, fp);
2467
2468 if (is_bts_event(attr)) {
2469 perf_sample__fprintf_bts(sample, evsel, thread, al, addr_al, machine, fp);
2470 return;
2471 }
2472#ifdef HAVE_LIBTRACEEVENT
2473 if (PRINT_FIELD(TRACE) && sample->raw_data) {
2474 const struct tep_event *tp_format = evsel__tp_format(evsel);
2475
2476 if (tp_format) {
2477 event_format__fprintf(tp_format, sample->cpu,
2478 sample->raw_data, sample->raw_size,
2479 fp);
2480 }
2481 }
2482#endif
2483 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
2484 perf_sample__fprintf_synth(sample, evsel, fp);
2485
2486 if (PRINT_FIELD(ADDR))
2487 perf_sample__fprintf_addr(sample, thread, evsel, fp);
2488
2489 if (PRINT_FIELD(DATA_SRC))
2490 data_src__fprintf(sample->data_src, fp);
2491
2492 if (PRINT_FIELD(WEIGHT))
2493 fprintf(fp, "%16" PRIu64, sample->weight);
2494
2495 if (PRINT_FIELD(INS_LAT))
2496 fprintf(fp, "%16" PRIu16, sample->ins_lat);
2497
2498 if (PRINT_FIELD(RETIRE_LAT))
2499 fprintf(fp, "%16" PRIu16, sample->weight3);
2500
2501 if (PRINT_FIELD(CGROUP)) {
2502 const char *cgrp_name;
2503 struct cgroup *cgrp = cgroup__find(machine->env,
2504 sample->cgroup);
2505 if (cgrp != NULL)
2506 cgrp_name = cgrp->name;
2507 else
2508 cgrp_name = "unknown";
2509 fprintf(fp, " %s", cgrp_name);
2510 }
2511
2512 if (PRINT_FIELD(IP)) {
2513 struct callchain_cursor *cursor = NULL;
2514
2515 if (script->stitch_lbr)
2516 thread__set_lbr_stitch_enable(al->thread, true);
2517
2518 if (symbol_conf.use_callchain && sample->callchain) {
2519 cursor = get_tls_callchain_cursor();
2520 if (thread__resolve_callchain(al->thread, cursor,
2521 sample, NULL, NULL,
2522 scripting_max_stack))
2523 cursor = NULL;
2524 }
2525 fputc(cursor ? '\n' : ' ', fp);
2526 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor,
2527 symbol_conf.bt_stop_list, fp);
2528 }
2529
2530 if (PRINT_FIELD(IREGS)) {
2531 perf_sample__fprintf_iregs(sample, attr,
2532 thread__e_machine(thread, machine, &e_flags),
2533 e_flags,
2534 fp);
2535 }
2536
2537 if (PRINT_FIELD(UREGS)) {
2538 perf_sample__fprintf_uregs(sample, attr,
2539 thread__e_machine(thread, machine, &e_flags),
2540 e_flags,
2541 fp);
2542 }
2543
2544 if (PRINT_FIELD(BRSTACK))
2545 perf_sample__fprintf_brstack(sample, thread, evsel, fp);
2546 else if (PRINT_FIELD(BRSTACKSYM))
2547 perf_sample__fprintf_brstacksym(sample, thread, evsel, fp);
2548 else if (PRINT_FIELD(BRSTACKOFF))
2549 perf_sample__fprintf_brstackoff(sample, thread, evsel, fp);
2550
2551 if (evsel__is_bpf_output(evsel) && !evsel__is_offcpu_event(evsel) && PRINT_FIELD(BPF_OUTPUT))
2552 perf_sample__fprintf_bpf_output(sample, fp);
2553 perf_sample__fprintf_insn(sample, evsel, attr, thread, machine, fp, al);
2554
2555 if (PRINT_FIELD(PHYS_ADDR))
2556 fprintf(fp, "%16" PRIx64, sample->phys_addr);
2557
2558 if (PRINT_FIELD(DATA_PAGE_SIZE))
2559 fprintf(fp, " %s", get_page_size_name(sample->data_page_size, str));
2560
2561 if (PRINT_FIELD(CODE_PAGE_SIZE))
2562 fprintf(fp, " %s", get_page_size_name(sample->code_page_size, str));
2563
2564 perf_sample__fprintf_ipc(sample, evsel, fp);
2565
2566 fprintf(fp, "\n");
2567
2568 if (PRINT_FIELD(SRCCODE)) {
2569 if (map__fprintf_srccode(al->map, al->addr, stdout,
2570 thread__srccode_state(thread)))
2571 printf("\n");
2572 }
2573
2574 if (PRINT_FIELD(METRIC))
2575 perf_sample__fprint_metric(thread, evsel, sample, fp);
2576
2577 if (verbose > 0)
2578 fflush(fp);
2579}
2580
2581static struct scripting_ops *scripting_ops;
2582
2583static void __process_stat(struct evsel *counter, u64 tstamp)
2584{
2585 int nthreads = perf_thread_map__nr(counter->core.threads);
2586 struct perf_cpu cpu;
2587 static int header_printed;
2588
2589 if (!header_printed) {
2590 printf("%3s %8s %15s %15s %15s %15s %s\n",
2591 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
2592 header_printed = 1;
2593 }
2594
2595 for (int thread = 0; thread < nthreads; thread++) {
2596 unsigned int idx;
2597
2598 perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
2599 struct perf_counts_values *counts;
2600
2601 counts = perf_counts(counter->counts, idx, thread);
2602
2603 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
2604 cpu.cpu,
2605 perf_thread_map__pid(counter->core.threads, thread),
2606 counts->val,
2607 counts->ena,
2608 counts->run,
2609 tstamp,
2610 evsel__name(counter));
2611 }
2612 }
2613}
2614
2615static void process_stat(struct evsel *counter, u64 tstamp)
2616{
2617 if (scripting_ops && scripting_ops->process_stat)
2618 scripting_ops->process_stat(&stat_config, counter, tstamp);
2619 else
2620 __process_stat(counter, tstamp);
2621}
2622
2623static void process_stat_interval(u64 tstamp)
2624{
2625 if (scripting_ops && scripting_ops->process_stat_interval)
2626 scripting_ops->process_stat_interval(tstamp);
2627}
2628
2629static void setup_scripting(void)
2630{
2631#ifdef HAVE_LIBTRACEEVENT
2632 setup_perl_scripting();
2633#endif
2634 setup_python_scripting();
2635}
2636
2637static int flush_scripting(void)
2638{
2639 return scripting_ops ? scripting_ops->flush_script() : 0;
2640}
2641
2642static int cleanup_scripting(void)
2643{
2644 pr_debug("\nperf script stopped\n");
2645
2646 return scripting_ops ? scripting_ops->stop_script() : 0;
2647}
2648
2649static bool filter_cpu(struct perf_sample *sample)
2650{
2651 if (cpu_list && sample->cpu != (u32)-1 && sample->cpu < MAX_NR_CPUS)
2652 return !test_bit(sample->cpu, cpu_bitmap);
2653 return false;
2654}
2655
2656static int process_sample_event(const struct perf_tool *tool,
2657 union perf_event *event,
2658 struct perf_sample *sample,
2659 struct machine *machine)
2660{
2661 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2662 struct evsel *evsel = sample->evsel;
2663 struct addr_location al;
2664 struct addr_location addr_al;
2665 int ret = 0;
2666
2667 /* Set thread to NULL to indicate addr_al and al are not initialized */
2668 addr_location__init(&al);
2669 addr_location__init(&addr_al);
2670
2671 ret = dlfilter__filter_event_early(dlfilter, event, sample, evsel, machine, &al, &addr_al);
2672 if (ret) {
2673 if (ret > 0)
2674 ret = 0;
2675 goto out_put;
2676 }
2677
2678 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2679 sample->time)) {
2680 goto out_put;
2681 }
2682
2683 if (debug_mode) {
2684 if (sample->time < last_timestamp) {
2685 pr_err("Samples misordered, previous: %" PRIu64
2686 " this: %" PRIu64 "\n", last_timestamp,
2687 sample->time);
2688 nr_unordered++;
2689 }
2690 last_timestamp = sample->time;
2691 goto out_put;
2692 }
2693
2694 if (filter_cpu(sample))
2695 goto out_put;
2696
2697 if (!al.thread && machine__resolve(machine, &al, sample) < 0) {
2698 pr_err("problem processing %s (%u) event at offset %#" PRIx64 ", skipping it.\n",
2699 perf_event__name(event->header.type), event->header.type,
2700 sample->file_offset);
2701 ret = -1;
2702 goto out_put;
2703 }
2704
2705 if (al.filtered)
2706 goto out_put;
2707
2708 if (!show_event(sample, evsel, al.thread, &al, &addr_al))
2709 goto out_put;
2710
2711 if (evswitch__discard(&scr->evswitch, evsel))
2712 goto out_put;
2713
2714 ret = dlfilter__filter_event(dlfilter, event, sample, evsel, machine, &al, &addr_al);
2715 if (ret) {
2716 if (ret > 0)
2717 ret = 0;
2718 goto out_put;
2719 }
2720
2721 if (scripting_ops) {
2722 struct addr_location *addr_al_ptr = NULL;
2723
2724 if ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
2725 sample_addr_correlates_sym(&evsel->core.attr)) {
2726 if (!addr_al.thread)
2727 thread__resolve(al.thread, &addr_al, sample);
2728 addr_al_ptr = &addr_al;
2729 }
2730 scripting_ops->process_event(event, sample, &al, addr_al_ptr);
2731 } else {
2732 process_event(scr, sample, &al, &addr_al, machine);
2733 }
2734
2735out_put:
2736 addr_location__exit(&addr_al);
2737 addr_location__exit(&al);
2738 return ret;
2739}
2740
2741static int process_deferred_sample_event(const struct perf_tool *tool,
2742 union perf_event *event,
2743 struct perf_sample *sample,
2744 struct machine *machine)
2745{
2746 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2747 struct evsel *evsel = sample->evsel;
2748 struct perf_event_attr *attr = &evsel->core.attr;
2749 struct evsel_script *es = evsel->priv;
2750 unsigned int type = output_type(attr->type);
2751 struct addr_location al;
2752 FILE *fp = es->fp;
2753 int ret = 0;
2754
2755 if (output[type].fields == 0)
2756 return 0;
2757
2758 /* Set thread to NULL to indicate addr_al and al are not initialized */
2759 addr_location__init(&al);
2760
2761 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
2762 sample->time)) {
2763 goto out_put;
2764 }
2765
2766 if (debug_mode) {
2767 if (sample->time < last_timestamp) {
2768 pr_err("Samples misordered, previous: %" PRIu64
2769 " this: %" PRIu64 "\n", last_timestamp,
2770 sample->time);
2771 nr_unordered++;
2772 }
2773 last_timestamp = sample->time;
2774 goto out_put;
2775 }
2776
2777 if (filter_cpu(sample))
2778 goto out_put;
2779
2780 if (machine__resolve(machine, &al, sample) < 0) {
2781 pr_err("problem processing %s (%u) event at offset %#" PRIx64 ", skipping it.\n",
2782 perf_event__name(event->header.type), event->header.type,
2783 sample->file_offset);
2784 ret = -1;
2785 goto out_put;
2786 }
2787
2788 if (al.filtered)
2789 goto out_put;
2790
2791 if (!show_event(sample, evsel, al.thread, &al, NULL))
2792 goto out_put;
2793
2794 if (evswitch__discard(&scr->evswitch, evsel))
2795 goto out_put;
2796
2797 perf_sample__fprintf_start(scr, sample, al.thread, evsel,
2798 PERF_RECORD_CALLCHAIN_DEFERRED, fp);
2799 fprintf(fp, "DEFERRED CALLCHAIN [cookie: %llx]",
2800 (unsigned long long)event->callchain_deferred.cookie);
2801
2802 if (PRINT_FIELD(IP)) {
2803 struct callchain_cursor *cursor = NULL;
2804
2805 if (symbol_conf.use_callchain && sample->callchain) {
2806 cursor = get_tls_callchain_cursor();
2807 if (thread__resolve_callchain(al.thread, cursor,
2808 sample, NULL, NULL,
2809 scripting_max_stack)) {
2810 pr_info("cannot resolve deferred callchains\n");
2811 cursor = NULL;
2812 }
2813 }
2814
2815 fputc(cursor ? '\n' : ' ', fp);
2816 sample__fprintf_sym(sample, &al, 0, output[type].print_ip_opts,
2817 cursor, symbol_conf.bt_stop_list, fp);
2818 }
2819
2820 fprintf(fp, "\n");
2821
2822 if (verbose > 0)
2823 fflush(fp);
2824
2825out_put:
2826 addr_location__exit(&al);
2827 return ret;
2828}
2829
2830// Used when scr->per_event_dump is not set
2831static struct evsel_script es_stdout;
2832
2833static int process_attr(const struct perf_tool *tool, union perf_event *event,
2834 struct evlist **pevlist)
2835{
2836 struct perf_script *scr = container_of(tool, struct perf_script, tool);
2837 struct evlist *evlist;
2838 struct evsel *evsel, *pos;
2839 uint16_t e_machine;
2840 u64 sample_type;
2841 int err;
2842
2843 err = perf_event__process_attr(tool, event, pevlist);
2844 if (err)
2845 return err;
2846
2847 evlist = *pevlist;
2848 evsel = evlist__last(*pevlist);
2849
2850 if (!evsel->priv) {
2851 if (scr->per_event_dump) {
2852 evsel->priv = evsel_script__new(evsel, scr->session->data);
2853 if (!evsel->priv)
2854 return -ENOMEM;
2855 } else { // Replicate what is done in perf_script__setup_per_event_dump()
2856 es_stdout.fp = stdout;
2857 evsel->priv = &es_stdout;
2858 }
2859 }
2860
2861 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2862 evsel->core.attr.type != PERF_TYPE_SYNTH)
2863 return 0;
2864
2865 evlist__for_each_entry(evlist, pos) {
2866 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
2867 return 0;
2868 }
2869
2870 if (evsel->core.attr.sample_type) {
2871 err = evsel__check_attr(evsel, scr->session);
2872 if (err)
2873 return err;
2874 }
2875
2876 /*
2877 * Check if we need to enable callchains based
2878 * on events sample_type.
2879 */
2880 sample_type = evlist__combined_sample_type(evlist);
2881 e_machine = perf_session__e_machine(evsel__session(evsel), /*e_flags=*/NULL);
2882 callchain_param_setup(sample_type, e_machine);
2883
2884 /* Enable fields for callchain entries */
2885 if (symbol_conf.use_callchain &&
2886 (sample_type & PERF_SAMPLE_CALLCHAIN ||
2887 sample_type & PERF_SAMPLE_BRANCH_STACK ||
2888 (sample_type & PERF_SAMPLE_REGS_USER &&
2889 sample_type & PERF_SAMPLE_STACK_USER))) {
2890 int type = evsel__output_type(evsel);
2891
2892 if (!(output[type].user_unset_fields & PERF_OUTPUT_IP))
2893 output[type].fields |= PERF_OUTPUT_IP;
2894 if (!(output[type].user_unset_fields & PERF_OUTPUT_SYM))
2895 output[type].fields |= PERF_OUTPUT_SYM;
2896 }
2897 evsel__set_print_ip_opts(evsel);
2898 return 0;
2899}
2900
2901static int print_event_with_time(const struct perf_tool *tool,
2902 union perf_event *event,
2903 struct perf_sample *sample,
2904 struct machine *machine,
2905 pid_t pid, pid_t tid, u64 timestamp)
2906{
2907 struct perf_script *script = container_of(tool, struct perf_script, tool);
2908 struct perf_session *session = script->session;
2909 struct evsel *evsel = sample->evsel;
2910 struct thread *thread = NULL;
2911
2912 if (!evsel)
2913 evsel = evlist__id2evsel(session->evlist, sample->id);
2914
2915 if (evsel && !evsel->core.attr.sample_id_all) {
2916 sample->cpu = 0;
2917 sample->time = timestamp;
2918 sample->pid = pid;
2919 sample->tid = tid;
2920 }
2921
2922 if (filter_cpu(sample))
2923 return 0;
2924
2925 if (tid != -1)
2926 thread = machine__findnew_thread(machine, pid, tid);
2927
2928 if (evsel) {
2929 struct evsel *saved_evsel = sample->evsel;
2930
2931 sample->evsel = evsel;
2932 perf_sample__fprintf_start(script, sample, thread, evsel,
2933 event->header.type, stdout);
2934 sample->evsel = saved_evsel;
2935 }
2936
2937 perf_event__fprintf(event, machine, stdout);
2938
2939 thread__put(thread);
2940
2941 return 0;
2942}
2943
2944static int print_event(const struct perf_tool *tool, union perf_event *event,
2945 struct perf_sample *sample, struct machine *machine,
2946 pid_t pid, pid_t tid)
2947{
2948 return print_event_with_time(tool, event, sample, machine, pid, tid, 0);
2949}
2950
2951static int process_comm_event(const struct perf_tool *tool,
2952 union perf_event *event,
2953 struct perf_sample *sample,
2954 struct machine *machine)
2955{
2956 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2957 return -1;
2958
2959 return print_event(tool, event, sample, machine, event->comm.pid,
2960 event->comm.tid);
2961}
2962
2963static int process_namespaces_event(const struct perf_tool *tool,
2964 union perf_event *event,
2965 struct perf_sample *sample,
2966 struct machine *machine)
2967{
2968 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2969 return -1;
2970
2971 return print_event(tool, event, sample, machine, event->namespaces.pid,
2972 event->namespaces.tid);
2973}
2974
2975static int process_cgroup_event(const struct perf_tool *tool,
2976 union perf_event *event,
2977 struct perf_sample *sample,
2978 struct machine *machine)
2979{
2980 if (perf_event__process_cgroup(tool, event, sample, machine) < 0)
2981 return -1;
2982
2983 return print_event(tool, event, sample, machine, sample->pid,
2984 sample->tid);
2985}
2986
2987static int process_fork_event(const struct perf_tool *tool,
2988 union perf_event *event,
2989 struct perf_sample *sample,
2990 struct machine *machine)
2991{
2992 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2993 return -1;
2994
2995 return print_event_with_time(tool, event, sample, machine,
2996 event->fork.pid, event->fork.tid,
2997 event->fork.time);
2998}
2999static int process_exit_event(const struct perf_tool *tool,
3000 union perf_event *event,
3001 struct perf_sample *sample,
3002 struct machine *machine)
3003{
3004 /* Print before 'exit' deletes anything */
3005 if (print_event_with_time(tool, event, sample, machine, event->fork.pid,
3006 event->fork.tid, event->fork.time))
3007 return -1;
3008
3009 return perf_event__process_exit(tool, event, sample, machine);
3010}
3011
3012static int process_mmap_event(const struct perf_tool *tool,
3013 union perf_event *event,
3014 struct perf_sample *sample,
3015 struct machine *machine)
3016{
3017 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
3018 return -1;
3019
3020 return print_event(tool, event, sample, machine, event->mmap.pid,
3021 event->mmap.tid);
3022}
3023
3024static int process_mmap2_event(const struct perf_tool *tool,
3025 union perf_event *event,
3026 struct perf_sample *sample,
3027 struct machine *machine)
3028{
3029 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
3030 return -1;
3031
3032 return print_event(tool, event, sample, machine, event->mmap2.pid,
3033 event->mmap2.tid);
3034}
3035
3036static int process_switch_event(const struct perf_tool *tool,
3037 union perf_event *event,
3038 struct perf_sample *sample,
3039 struct machine *machine)
3040{
3041 struct perf_script *script = container_of(tool, struct perf_script, tool);
3042
3043 if (perf_event__process_switch(tool, event, sample, machine) < 0)
3044 return -1;
3045
3046 if (scripting_ops && scripting_ops->process_switch && !filter_cpu(sample))
3047 scripting_ops->process_switch(event, sample, machine);
3048
3049 if (!script->show_switch_events)
3050 return 0;
3051
3052 return print_event(tool, event, sample, machine, sample->pid,
3053 sample->tid);
3054}
3055
3056static int process_auxtrace_error(const struct perf_tool *tool,
3057 struct perf_session *session,
3058 union perf_event *event)
3059{
3060 if (scripting_ops && scripting_ops->process_auxtrace_error) {
3061 scripting_ops->process_auxtrace_error(session, event);
3062 return 0;
3063 }
3064
3065 return perf_event__process_auxtrace_error(tool, session, event);
3066}
3067
3068static int
3069process_lost_event(const struct perf_tool *tool,
3070 union perf_event *event,
3071 struct perf_sample *sample,
3072 struct machine *machine)
3073{
3074 return print_event(tool, event, sample, machine, sample->pid,
3075 sample->tid);
3076}
3077
3078static int
3079process_throttle_event(const struct perf_tool *tool __maybe_unused,
3080 union perf_event *event,
3081 struct perf_sample *sample,
3082 struct machine *machine)
3083{
3084 if (scripting_ops && scripting_ops->process_throttle)
3085 scripting_ops->process_throttle(event, sample, machine);
3086 return 0;
3087}
3088
3089static int
3090process_finished_round_event(const struct perf_tool *tool __maybe_unused,
3091 union perf_event *event,
3092 struct ordered_events *oe __maybe_unused)
3093
3094{
3095 perf_event__fprintf(event, NULL, stdout);
3096 return 0;
3097}
3098
3099static int
3100process_bpf_events(const struct perf_tool *tool __maybe_unused,
3101 union perf_event *event,
3102 struct perf_sample *sample,
3103 struct machine *machine)
3104{
3105 if (machine__process_ksymbol(machine, event, sample) < 0)
3106 return -1;
3107
3108 return print_event(tool, event, sample, machine, sample->pid,
3109 sample->tid);
3110}
3111
3112static int
3113process_bpf_metadata_event(const struct perf_tool *tool __maybe_unused,
3114 struct perf_session *session __maybe_unused,
3115 union perf_event *event)
3116{
3117 perf_event__fprintf(event, NULL, stdout);
3118 return 0;
3119}
3120
3121static int process_text_poke_events(const struct perf_tool *tool,
3122 union perf_event *event,
3123 struct perf_sample *sample,
3124 struct machine *machine)
3125{
3126 if (perf_event__process_text_poke(tool, event, sample, machine) < 0)
3127 return -1;
3128
3129 return print_event(tool, event, sample, machine, sample->pid,
3130 sample->tid);
3131}
3132
3133static void sig_handler(int sig __maybe_unused)
3134{
3135 session_done = 1;
3136}
3137
3138static void perf_script__fclose_per_event_dump(struct perf_script *script)
3139{
3140 struct evlist *evlist = script->session->evlist;
3141 struct evsel *evsel;
3142
3143 evlist__for_each_entry(evlist, evsel) {
3144 if (!evsel->priv)
3145 break;
3146 evsel_script__delete(evsel->priv);
3147 evsel->priv = NULL;
3148 }
3149}
3150
3151static int perf_script__fopen_per_event_dump(struct perf_script *script)
3152{
3153 struct evsel *evsel;
3154
3155 evlist__for_each_entry(script->session->evlist, evsel) {
3156 /*
3157 * Already setup? I.e. we may be called twice in cases like
3158 * Intel PT, one for the intel_pt// and dummy events, then
3159 * for the evsels synthesized from the auxtrace info.
3160 *
3161 * Ses perf_script__process_auxtrace_info.
3162 */
3163 if (evsel->priv != NULL)
3164 continue;
3165
3166 evsel->priv = evsel_script__new(evsel, script->session->data);
3167 if (evsel->priv == NULL)
3168 goto out_err_fclose;
3169 }
3170
3171 return 0;
3172
3173out_err_fclose:
3174 perf_script__fclose_per_event_dump(script);
3175 return -1;
3176}
3177
3178static int perf_script__setup_per_event_dump(struct perf_script *script)
3179{
3180 struct evsel *evsel;
3181
3182 if (script->per_event_dump)
3183 return perf_script__fopen_per_event_dump(script);
3184
3185 es_stdout.fp = stdout;
3186
3187 evlist__for_each_entry(script->session->evlist, evsel)
3188 evsel->priv = &es_stdout;
3189
3190 return 0;
3191}
3192
3193static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
3194{
3195 struct evsel *evsel;
3196
3197 evlist__for_each_entry(script->session->evlist, evsel) {
3198 struct evsel_script *es = evsel->priv;
3199
3200 evsel_script__fprintf(es, stdout);
3201 evsel_script__delete(es);
3202 evsel->priv = NULL;
3203 }
3204}
3205
3206static void perf_script__exit(struct perf_script *script)
3207{
3208 perf_thread_map__put(script->threads);
3209 perf_cpu_map__put(script->cpus);
3210}
3211
3212static int __cmd_script(struct perf_script *script)
3213{
3214 int ret;
3215
3216 signal(SIGINT, sig_handler);
3217
3218 /* override event processing functions */
3219 if (script->show_task_events) {
3220 script->tool.comm = process_comm_event;
3221 script->tool.fork = process_fork_event;
3222 script->tool.exit = process_exit_event;
3223 }
3224 if (script->show_mmap_events) {
3225 script->tool.mmap = process_mmap_event;
3226 script->tool.mmap2 = process_mmap2_event;
3227 }
3228 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
3229 script->tool.context_switch = process_switch_event;
3230 if (scripting_ops && scripting_ops->process_auxtrace_error)
3231 script->tool.auxtrace_error = process_auxtrace_error;
3232 if (script->show_namespace_events)
3233 script->tool.namespaces = process_namespaces_event;
3234 if (script->show_cgroup_events)
3235 script->tool.cgroup = process_cgroup_event;
3236 if (script->show_lost_events)
3237 script->tool.lost = process_lost_event;
3238 if (script->show_round_events) {
3239 script->tool.ordered_events = false;
3240 script->tool.finished_round = process_finished_round_event;
3241 }
3242 if (script->show_bpf_events) {
3243 script->tool.ksymbol = process_bpf_events;
3244 script->tool.bpf = process_bpf_events;
3245 script->tool.bpf_metadata = process_bpf_metadata_event;
3246 }
3247 if (script->show_text_poke_events) {
3248 script->tool.ksymbol = process_bpf_events;
3249 script->tool.text_poke = process_text_poke_events;
3250 }
3251
3252 if (perf_script__setup_per_event_dump(script)) {
3253 pr_err("Couldn't create the per event dump files\n");
3254 return -1;
3255 }
3256
3257 ret = perf_session__process_events(script->session);
3258
3259 if (script->per_event_dump)
3260 perf_script__exit_per_event_dump_stats(script);
3261
3262 if (debug_mode)
3263 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
3264
3265 return ret;
3266}
3267
3268static int list_available_languages_cb(struct scripting_ops *ops, const char *spec)
3269{
3270 fprintf(stderr, " %-42s [%s]\n", spec, ops->name);
3271 return 0;
3272}
3273
3274static void list_available_languages(void)
3275{
3276 fprintf(stderr, "\n");
3277 fprintf(stderr, "Scripting language extensions (used in "
3278 "perf script -s [spec:]script.[spec]):\n\n");
3279 script_spec__for_each(&list_available_languages_cb);
3280 fprintf(stderr, "\n");
3281}
3282
3283/* Find script file relative to current directory or exec path */
3284static char *find_script(const char *script)
3285{
3286 char path[PATH_MAX];
3287
3288 if (!scripting_ops) {
3289 const char *ext = strrchr(script, '.');
3290
3291 if (!ext)
3292 return NULL;
3293
3294 scripting_ops = script_spec__lookup(++ext);
3295 if (!scripting_ops)
3296 return NULL;
3297 }
3298
3299 if (access(script, R_OK)) {
3300 char *exec_path = get_argv_exec_path();
3301
3302 if (!exec_path)
3303 return NULL;
3304 snprintf(path, sizeof(path), "%s/scripts/%s/%s",
3305 exec_path, scripting_ops->dirname, script);
3306 free(exec_path);
3307 script = path;
3308 if (access(script, R_OK))
3309 return NULL;
3310 }
3311 return strdup(script);
3312}
3313
3314static int parse_scriptname(const struct option *opt __maybe_unused,
3315 const char *str, int unset __maybe_unused)
3316{
3317 char spec[PATH_MAX];
3318 const char *script, *ext;
3319 int len;
3320
3321 if (strcmp(str, "lang") == 0) {
3322 list_available_languages();
3323 exit(0);
3324 }
3325
3326 script = strchr(str, ':');
3327 if (script) {
3328 len = script - str;
3329 if (len >= PATH_MAX) {
3330 fprintf(stderr, "invalid language specifier");
3331 return -1;
3332 }
3333 strncpy(spec, str, len);
3334 spec[len] = '\0';
3335 scripting_ops = script_spec__lookup(spec);
3336 if (!scripting_ops) {
3337 fprintf(stderr, "invalid language specifier");
3338 return -1;
3339 }
3340 script++;
3341 } else {
3342 script = str;
3343 ext = strrchr(script, '.');
3344 if (!ext) {
3345 fprintf(stderr, "invalid script extension");
3346 return -1;
3347 }
3348 scripting_ops = script_spec__lookup(++ext);
3349 if (!scripting_ops) {
3350 fprintf(stderr, "invalid script extension");
3351 return -1;
3352 }
3353 }
3354
3355 script_name = find_script(script);
3356 if (!script_name)
3357 script_name = strdup(script);
3358
3359 return 0;
3360}
3361
3362static int parse_output_fields(const struct option *opt __maybe_unused,
3363 const char *arg, int unset __maybe_unused)
3364{
3365 char *tok, *strtok_saveptr = NULL;
3366 int i, imax = ARRAY_SIZE(all_output_options);
3367 int j;
3368 int rc = 0;
3369 char *str = strdup(arg);
3370 int type = -1;
3371 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
3372
3373 if (!str)
3374 return -ENOMEM;
3375
3376 /* first word can state for which event type the user is specifying
3377 * the fields. If no type exists, the specified fields apply to all
3378 * event types found in the file minus the invalid fields for a type.
3379 */
3380 tok = strchr(str, ':');
3381 if (tok) {
3382 *tok = '\0';
3383 tok++;
3384 if (!strcmp(str, "hw"))
3385 type = PERF_TYPE_HARDWARE;
3386 else if (!strcmp(str, "sw"))
3387 type = PERF_TYPE_SOFTWARE;
3388 else if (!strcmp(str, "trace"))
3389 type = PERF_TYPE_TRACEPOINT;
3390 else if (!strcmp(str, "raw"))
3391 type = PERF_TYPE_RAW;
3392 else if (!strcmp(str, "break"))
3393 type = PERF_TYPE_BREAKPOINT;
3394 else if (!strcmp(str, "synth"))
3395 type = OUTPUT_TYPE_SYNTH;
3396 else {
3397 fprintf(stderr, "Invalid event type in field string.\n");
3398 rc = -EINVAL;
3399 goto out;
3400 }
3401
3402 if (output[type].user_set)
3403 pr_warning("Overriding previous field request for %s events.\n",
3404 event_type(type));
3405
3406 /* Don't override defaults for +- */
3407 if (strchr(tok, '+') || strchr(tok, '-'))
3408 goto parse;
3409
3410 output[type].fields = 0;
3411 output[type].user_set = true;
3412 output[type].wildcard_set = false;
3413
3414 } else {
3415 tok = str;
3416 if (strlen(str) == 0) {
3417 fprintf(stderr,
3418 "Cannot set fields to 'none' for all event types.\n");
3419 rc = -EINVAL;
3420 goto out;
3421 }
3422
3423 /* Don't override defaults for +- */
3424 if (strchr(str, '+') || strchr(str, '-'))
3425 goto parse;
3426
3427 if (output_set_by_user())
3428 pr_warning("Overriding previous field request for all events.\n");
3429
3430 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
3431 output[j].fields = 0;
3432 output[j].user_set = true;
3433 output[j].wildcard_set = true;
3434 }
3435 }
3436
3437parse:
3438 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
3439 if (*tok == '+') {
3440 if (change == SET)
3441 goto out_badmix;
3442 change = ADD;
3443 tok++;
3444 } else if (*tok == '-') {
3445 if (change == SET)
3446 goto out_badmix;
3447 change = REMOVE;
3448 tok++;
3449 } else {
3450 if (change != SET && change != DEFAULT)
3451 goto out_badmix;
3452 change = SET;
3453 }
3454
3455 for (i = 0; i < imax; ++i) {
3456 if (strcmp(tok, all_output_options[i].str) == 0)
3457 break;
3458 }
3459 if (i == imax && strcmp(tok, "flags") == 0) {
3460 print_flags = change != REMOVE;
3461 continue;
3462 }
3463 if (i == imax) {
3464 fprintf(stderr, "Invalid field requested.\n");
3465 rc = -EINVAL;
3466 goto out;
3467 }
3468#ifndef HAVE_LIBCAPSTONE_SUPPORT
3469 if (change != REMOVE && strcmp(tok, "disasm") == 0) {
3470 fprintf(stderr, "Field \"disasm\" requires perf to be built with libcapstone support.\n");
3471 rc = -EINVAL;
3472 goto out;
3473 }
3474#endif
3475
3476 if (type == -1) {
3477 /* add user option to all events types for
3478 * which it is valid
3479 */
3480 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
3481 if (output[j].invalid_fields & all_output_options[i].field) {
3482 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
3483 all_output_options[i].str, event_type(j));
3484 } else {
3485 if (change == REMOVE) {
3486 output[j].fields &= ~all_output_options[i].field;
3487 output[j].user_set_fields &= ~all_output_options[i].field;
3488 output[j].user_unset_fields |= all_output_options[i].field;
3489 } else {
3490 output[j].fields |= all_output_options[i].field;
3491 output[j].user_set_fields |= all_output_options[i].field;
3492 output[j].user_unset_fields &= ~all_output_options[i].field;
3493 }
3494 output[j].user_set = true;
3495 output[j].wildcard_set = true;
3496 }
3497 }
3498 } else {
3499 if (output[type].invalid_fields & all_output_options[i].field) {
3500 fprintf(stderr, "\'%s\' not valid for %s events.\n",
3501 all_output_options[i].str, event_type(type));
3502
3503 rc = -EINVAL;
3504 goto out;
3505 }
3506 if (change == REMOVE)
3507 output[type].fields &= ~all_output_options[i].field;
3508 else
3509 output[type].fields |= all_output_options[i].field;
3510 output[type].user_set = true;
3511 output[type].wildcard_set = true;
3512 }
3513 }
3514
3515 if (type >= 0) {
3516 if (output[type].fields == 0) {
3517 pr_debug("No fields requested for %s type. "
3518 "Events will not be displayed.\n", event_type(type));
3519 }
3520 }
3521 goto out;
3522
3523out_badmix:
3524 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
3525 rc = -EINVAL;
3526out:
3527 free(str);
3528 return rc;
3529}
3530
3531#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
3532 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
3533 if ((lang_dirent->d_type == DT_DIR || \
3534 (lang_dirent->d_type == DT_UNKNOWN && \
3535 is_directory(scripts_path, lang_dirent))) && \
3536 (strcmp(lang_dirent->d_name, ".")) && \
3537 (strcmp(lang_dirent->d_name, "..")))
3538
3539#define for_each_script(lang_path, lang_dir, script_dirent) \
3540 while ((script_dirent = readdir(lang_dir)) != NULL) \
3541 if (script_dirent->d_type != DT_DIR && \
3542 (script_dirent->d_type != DT_UNKNOWN || \
3543 !is_directory(lang_path, script_dirent)))
3544
3545
3546#define RECORD_SUFFIX "-record"
3547#define REPORT_SUFFIX "-report"
3548
3549struct script_desc {
3550 struct list_head node;
3551 char *name;
3552 char *half_liner;
3553 char *args;
3554};
3555
3556static LIST_HEAD(script_descs);
3557
3558static struct script_desc *script_desc__new(const char *name)
3559{
3560 struct script_desc *s = zalloc(sizeof(*s));
3561
3562 if (s != NULL && name)
3563 s->name = strdup(name);
3564
3565 return s;
3566}
3567
3568static void script_desc__delete(struct script_desc *s)
3569{
3570 zfree(&s->name);
3571 zfree(&s->half_liner);
3572 zfree(&s->args);
3573 free(s);
3574}
3575
3576static void script_desc__add(struct script_desc *s)
3577{
3578 list_add_tail(&s->node, &script_descs);
3579}
3580
3581static struct script_desc *script_desc__find(const char *name)
3582{
3583 struct script_desc *s;
3584
3585 list_for_each_entry(s, &script_descs, node)
3586 if (strcasecmp(s->name, name) == 0)
3587 return s;
3588 return NULL;
3589}
3590
3591static struct script_desc *script_desc__findnew(const char *name)
3592{
3593 struct script_desc *s = script_desc__find(name);
3594
3595 if (s)
3596 return s;
3597
3598 s = script_desc__new(name);
3599 if (!s)
3600 return NULL;
3601
3602 script_desc__add(s);
3603
3604 return s;
3605}
3606
3607static const char *ends_with(const char *str, const char *suffix)
3608{
3609 size_t suffix_len = strlen(suffix);
3610 const char *p = str;
3611
3612 if (strlen(str) > suffix_len) {
3613 p = str + strlen(str) - suffix_len;
3614 if (!strncmp(p, suffix, suffix_len))
3615 return p;
3616 }
3617
3618 return NULL;
3619}
3620
3621static int read_script_info(struct script_desc *desc, const char *filename)
3622{
3623 char line[BUFSIZ], *p;
3624 FILE *fp;
3625
3626 fp = fopen(filename, "r");
3627 if (!fp)
3628 return -1;
3629
3630 while (fgets(line, sizeof(line), fp)) {
3631 p = skip_spaces(line);
3632 if (strlen(p) == 0)
3633 continue;
3634 if (*p != '#')
3635 continue;
3636 p++;
3637 if (strlen(p) && *p == '!')
3638 continue;
3639
3640 p = skip_spaces(p);
3641 if (strlen(p) && p[strlen(p) - 1] == '\n')
3642 p[strlen(p) - 1] = '\0';
3643
3644 if (!strncmp(p, "description:", strlen("description:"))) {
3645 p += strlen("description:");
3646 desc->half_liner = strdup(skip_spaces(p));
3647 continue;
3648 }
3649
3650 if (!strncmp(p, "args:", strlen("args:"))) {
3651 p += strlen("args:");
3652 desc->args = strdup(skip_spaces(p));
3653 continue;
3654 }
3655 }
3656
3657 fclose(fp);
3658
3659 return 0;
3660}
3661
3662static char *get_script_root(struct dirent *script_dirent, const char *suffix)
3663{
3664 char *script_root, *str;
3665
3666 script_root = strdup(script_dirent->d_name);
3667 if (!script_root)
3668 return NULL;
3669
3670 str = (char *)ends_with(script_root, suffix);
3671 if (!str) {
3672 free(script_root);
3673 return NULL;
3674 }
3675
3676 *str = '\0';
3677 return script_root;
3678}
3679
3680static int list_available_scripts(const struct option *opt __maybe_unused,
3681 const char *s __maybe_unused,
3682 int unset __maybe_unused)
3683{
3684 struct dirent *script_dirent, *lang_dirent;
3685 char *buf, *scripts_path, *script_path, *lang_path, *first_half;
3686 DIR *scripts_dir, *lang_dir;
3687 struct script_desc *desc;
3688 char *script_root;
3689
3690 buf = malloc(3 * MAXPATHLEN + BUFSIZ);
3691 if (!buf) {
3692 pr_err("malloc failed\n");
3693 exit(-1);
3694 }
3695 scripts_path = buf;
3696 script_path = buf + MAXPATHLEN;
3697 lang_path = buf + 2 * MAXPATHLEN;
3698 first_half = buf + 3 * MAXPATHLEN;
3699
3700 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3701
3702 scripts_dir = opendir(scripts_path);
3703 if (!scripts_dir) {
3704 fprintf(stdout,
3705 "open(%s) failed.\n"
3706 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
3707 scripts_path);
3708 free(buf);
3709 exit(-1);
3710 }
3711
3712 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3713 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3714 lang_dirent->d_name);
3715 lang_dir = opendir(lang_path);
3716 if (!lang_dir)
3717 continue;
3718
3719 for_each_script(lang_path, lang_dir, script_dirent) {
3720 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
3721 if (script_root) {
3722 desc = script_desc__findnew(script_root);
3723 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3724 lang_path, script_dirent->d_name);
3725 read_script_info(desc, script_path);
3726 free(script_root);
3727 }
3728 }
3729 }
3730
3731 fprintf(stdout, "List of available trace scripts:\n");
3732 list_for_each_entry(desc, &script_descs, node) {
3733 sprintf(first_half, "%s %s", desc->name,
3734 desc->args ? desc->args : "");
3735 fprintf(stdout, " %-36s %s\n", first_half,
3736 desc->half_liner ? desc->half_liner : "");
3737 }
3738
3739 free(buf);
3740 exit(0);
3741}
3742
3743static int add_dlarg(const struct option *opt __maybe_unused,
3744 const char *s, int unset __maybe_unused)
3745{
3746 char *arg = strdup(s);
3747 void *a;
3748
3749 if (!arg)
3750 return -1;
3751
3752 a = realloc(dlargv, sizeof(dlargv[0]) * (dlargc + 1));
3753 if (!a) {
3754 free(arg);
3755 return -1;
3756 }
3757
3758 dlargv = a;
3759 dlargv[dlargc++] = arg;
3760
3761 return 0;
3762}
3763
3764static void free_dlarg(void)
3765{
3766 while (dlargc--)
3767 free(dlargv[dlargc]);
3768 free(dlargv);
3769}
3770
3771static char *get_script_path(const char *script_root, const char *suffix)
3772{
3773 struct dirent *script_dirent, *lang_dirent;
3774 char scripts_path[MAXPATHLEN];
3775 char script_path[MAXPATHLEN];
3776 DIR *scripts_dir, *lang_dir;
3777 char lang_path[MAXPATHLEN];
3778 char *__script_root;
3779
3780 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
3781
3782 scripts_dir = opendir(scripts_path);
3783 if (!scripts_dir)
3784 return NULL;
3785
3786 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
3787 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3788 lang_dirent->d_name);
3789 lang_dir = opendir(lang_path);
3790 if (!lang_dir)
3791 continue;
3792
3793 for_each_script(lang_path, lang_dir, script_dirent) {
3794 __script_root = get_script_root(script_dirent, suffix);
3795 if (__script_root && !strcmp(script_root, __script_root)) {
3796 free(__script_root);
3797 closedir(scripts_dir);
3798 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3799 lang_path, script_dirent->d_name);
3800 closedir(lang_dir);
3801 return strdup(script_path);
3802 }
3803 free(__script_root);
3804 }
3805 closedir(lang_dir);
3806 }
3807 closedir(scripts_dir);
3808
3809 return NULL;
3810}
3811
3812static bool is_top_script(const char *script_path)
3813{
3814 return ends_with(script_path, "top") != NULL;
3815}
3816
3817static int has_required_arg(char *script_path)
3818{
3819 struct script_desc *desc;
3820 int n_args = 0;
3821 char *p;
3822
3823 desc = script_desc__new(NULL);
3824
3825 if (read_script_info(desc, script_path))
3826 goto out;
3827
3828 if (!desc->args)
3829 goto out;
3830
3831 for (p = desc->args; *p; p++)
3832 if (*p == '<')
3833 n_args++;
3834out:
3835 script_desc__delete(desc);
3836
3837 return n_args;
3838}
3839
3840static int have_cmd(int argc, const char **argv)
3841{
3842 char **__argv = calloc(argc, sizeof(const char *));
3843
3844 if (!__argv) {
3845 pr_err("malloc failed\n");
3846 return -1;
3847 }
3848
3849 memcpy(__argv, argv, sizeof(const char *) * argc);
3850 argc = parse_options(argc, (const char **)__argv, record_options,
3851 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3852 free(__argv);
3853
3854 system_wide = (argc == 0);
3855
3856 return 0;
3857}
3858
3859static void script__setup_sample_type(struct perf_script *script)
3860{
3861 struct perf_session *session = script->session;
3862 u64 sample_type = evlist__combined_sample_type(session->evlist);
3863
3864 callchain_param_setup(sample_type, perf_session__e_machine(session, /*e_flags=*/NULL));
3865
3866 if (script->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) {
3867 pr_warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
3868 "Please apply --call-graph lbr when recording.\n");
3869 script->stitch_lbr = false;
3870 }
3871}
3872
3873static int process_stat_round_event(const struct perf_tool *tool __maybe_unused,
3874 struct perf_session *session,
3875 union perf_event *event)
3876{
3877 struct perf_record_stat_round *round = &event->stat_round;
3878 struct evsel *counter;
3879
3880 evlist__for_each_entry(session->evlist, counter) {
3881 perf_stat_process_counter(&stat_config, counter);
3882 process_stat(counter, round->time);
3883 }
3884
3885 process_stat_interval(round->time);
3886 return 0;
3887}
3888
3889static int process_stat_config_event(const struct perf_tool *tool __maybe_unused,
3890 struct perf_session *session __maybe_unused,
3891 union perf_event *event)
3892{
3893 perf_event__read_stat_config(&stat_config, &event->stat_config);
3894
3895 /*
3896 * Aggregation modes are not used since post-processing scripts are
3897 * supposed to take care of such requirements
3898 */
3899 stat_config.aggr_mode = AGGR_NONE;
3900
3901 return 0;
3902}
3903
3904static int set_maps(struct perf_script *script)
3905{
3906 struct evlist *evlist = script->session->evlist;
3907
3908 if (!script->cpus || !script->threads)
3909 return 0;
3910
3911 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3912 return -EINVAL;
3913
3914 perf_evlist__set_maps(evlist__core(evlist), script->cpus, script->threads);
3915
3916 if (evlist__alloc_stats(&stat_config, evlist, /*alloc_raw=*/true))
3917 return -ENOMEM;
3918
3919 script->allocated = true;
3920 return 0;
3921}
3922
3923static
3924int process_thread_map_event(const struct perf_tool *tool,
3925 struct perf_session *session __maybe_unused,
3926 union perf_event *event)
3927{
3928 struct perf_script *script = container_of(tool, struct perf_script, tool);
3929
3930 if (dump_trace)
3931 perf_event__fprintf_thread_map(event, stdout);
3932
3933 if (script->threads) {
3934 pr_warning("Extra thread map event, ignoring.\n");
3935 return 0;
3936 }
3937
3938 script->threads = thread_map__new_event(&event->thread_map);
3939 if (!script->threads)
3940 return -ENOMEM;
3941
3942 return set_maps(script);
3943}
3944
3945static
3946int process_cpu_map_event(const struct perf_tool *tool,
3947 struct perf_session *session __maybe_unused,
3948 union perf_event *event)
3949{
3950 struct perf_script *script = container_of(tool, struct perf_script, tool);
3951
3952 if (dump_trace)
3953 perf_event__fprintf_cpu_map(event, stdout);
3954
3955 if (script->cpus) {
3956 pr_warning("Extra cpu map event, ignoring.\n");
3957 return 0;
3958 }
3959
3960 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3961 if (!script->cpus)
3962 return -ENOMEM;
3963
3964 return set_maps(script);
3965}
3966
3967static int perf_script__process_auxtrace_info(const struct perf_tool *tool,
3968 struct perf_session *session,
3969 union perf_event *event)
3970{
3971 int ret = perf_event__process_auxtrace_info(tool, session, event);
3972
3973 if (ret == 0) {
3974 struct perf_script *script = container_of(tool, struct perf_script, tool);
3975
3976 ret = perf_script__setup_per_event_dump(script);
3977 }
3978
3979 return ret;
3980}
3981
3982static int parse_insn_trace(const struct option *opt __maybe_unused,
3983 const char *str, int unset __maybe_unused)
3984{
3985 const char *fields = "+insn,-event,-period";
3986 int ret;
3987
3988 if (str) {
3989 if (strcmp(str, "disasm") == 0)
3990 fields = "+disasm,-event,-period";
3991 else if (strlen(str) != 0 && strcmp(str, "raw") != 0) {
3992 fprintf(stderr, "Only accept raw|disasm\n");
3993 return -EINVAL;
3994 }
3995 }
3996
3997 ret = parse_output_fields(NULL, fields, 0);
3998 if (ret < 0)
3999 return ret;
4000
4001 itrace_parse_synth_opts(opt, "i0nse", 0);
4002 symbol_conf.nanosecs = true;
4003 return 0;
4004}
4005
4006static int parse_xed(const struct option *opt __maybe_unused,
4007 const char *str __maybe_unused,
4008 int unset __maybe_unused)
4009{
4010 if (isatty(1))
4011 force_pager("xed -F insn: -A -64 | less");
4012 else
4013 force_pager("xed -F insn: -A -64");
4014 return 0;
4015}
4016
4017static int parse_call_trace(const struct option *opt __maybe_unused,
4018 const char *str __maybe_unused,
4019 int unset __maybe_unused)
4020{
4021 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
4022 itrace_parse_synth_opts(opt, "cewp", 0);
4023 symbol_conf.nanosecs = true;
4024 symbol_conf.pad_output_len_dso = 50;
4025 return 0;
4026}
4027
4028static int parse_callret_trace(const struct option *opt __maybe_unused,
4029 const char *str __maybe_unused,
4030 int unset __maybe_unused)
4031{
4032 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
4033 itrace_parse_synth_opts(opt, "crewp", 0);
4034 symbol_conf.nanosecs = true;
4035 return 0;
4036}
4037
4038int cmd_script(int argc, const char **argv)
4039{
4040 bool show_full_info = false;
4041 bool header = false;
4042 bool header_only = false;
4043 bool script_started = false;
4044 bool unsorted_dump = false;
4045 bool merge_deferred_callchains = true;
4046 char *rec_script_path = NULL;
4047 char *rep_script_path = NULL;
4048 struct perf_session *session;
4049 struct itrace_synth_opts itrace_synth_opts = {
4050 .set = false,
4051 .default_no_sample = true,
4052 };
4053 char *script_path = NULL;
4054 const char *dlfilter_file = NULL;
4055 const char **__argv;
4056 int i, j, err = 0;
4057 struct perf_script script = {};
4058 struct perf_data data = {
4059 .mode = PERF_DATA_MODE_READ,
4060 };
4061 const struct option options[] = {
4062 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
4063 "dump raw trace in ASCII"),
4064 OPT_BOOLEAN(0, "dump-unsorted-raw-trace", &unsorted_dump,
4065 "dump unsorted raw trace in ASCII"),
4066 OPT_INCR('v', "verbose", &verbose,
4067 "be more verbose (show symbol address, etc)"),
4068 OPT_BOOLEAN('L', "Latency", &latency_format,
4069 "show latency attributes (irqs/preemption disabled, etc)"),
4070 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
4071 list_available_scripts),
4072 OPT_CALLBACK_NOOPT(0, "list-dlfilters", NULL, NULL, "list available dlfilters",
4073 list_available_dlfilters),
4074 OPT_CALLBACK('s', "script", NULL, "name",
4075 "script file name (lang:script name, script name, or *)",
4076 parse_scriptname),
4077 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
4078 "generate perf-script.xx script in specified language"),
4079 OPT_STRING(0, "dlfilter", &dlfilter_file, "file", "filter .so file name"),
4080 OPT_CALLBACK(0, "dlarg", NULL, "argument", "filter argument",
4081 add_dlarg),
4082 OPT_STRING('i', "input", &input_name, "file", "input file name"),
4083 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
4084 "do various checks like samples ordering and lost events"),
4085 OPT_BOOLEAN(0, "header", &header, "Show data header."),
4086 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
4087 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
4088 "file", "vmlinux pathname"),
4089 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
4090 "file", "kallsyms pathname"),
4091 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
4092 "When printing symbols do not display call chain"),
4093 OPT_CALLBACK(0, "symfs", NULL, "directory[,layout]", SYMFS_HELP,
4094 symbol__config_symfs),
4095 OPT_CALLBACK('F', "fields", NULL, "str",
4096 "comma separated output fields prepend with 'type:'. "
4097 "+field to add and -field to remove."
4098 "Valid types: hw,sw,trace,raw,synth. "
4099 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,dsoff,"
4100 "addr,symoff,srcline,period,iregs,uregs,brstack,"
4101 "brstacksym,flags,data_src,weight,bpf-output,brstackinsn,"
4102 "brstackinsnlen,brstackdisasm,brstackoff,callindent,insn,disasm,insnlen,synth,"
4103 "phys_addr,metric,misc,srccode,ipc,tod,data_page_size,"
4104 "code_page_size,ins_lat,machine_pid,vcpu,cgroup,retire_lat,"
4105 "brcntr",
4106 parse_output_fields),
4107 OPT_BOOLEAN('a', "all-cpus", &system_wide,
4108 "system-wide collection from all CPUs"),
4109 OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
4110 "only consider symbols in these DSOs"),
4111 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
4112 "only consider these symbols"),
4113 OPT_INTEGER(0, "addr-range", &symbol_conf.addr_range,
4114 "Use with -S to list traced records within address range"),
4115 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, "raw|disasm",
4116 "Decode instructions from itrace", parse_insn_trace),
4117 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
4118 "Run xed disassembler on output", parse_xed),
4119 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
4120 "Decode calls from itrace", parse_call_trace),
4121 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
4122 "Decode calls and returns from itrace", parse_callret_trace),
4123 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
4124 "Only print symbols and callees with --call-trace/--call-ret-trace"),
4125 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
4126 "Stop display of callgraph at these symbols"),
4127 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
4128 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
4129 "only display events for these comms"),
4130 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
4131 "only consider symbols in these pids"),
4132 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
4133 "only consider symbols in these tids"),
4134 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
4135 "Set the maximum stack depth when parsing the callchain, "
4136 "anything beyond the specified depth will be ignored. "
4137 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
4138 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
4139 OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
4140 OPT_BOOLEAN('I', "show-info", &show_full_info,
4141 "display extended information from perf.data file"),
4142 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
4143 "Show the path of [kernel.kallsyms]"),
4144 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
4145 "Show the fork/comm/exit events"),
4146 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
4147 "Show the mmap events"),
4148 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
4149 "Show context switch events (if recorded)"),
4150 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
4151 "Show namespace events (if recorded)"),
4152 OPT_BOOLEAN('\0', "show-cgroup-events", &script.show_cgroup_events,
4153 "Show cgroup events (if recorded)"),
4154 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
4155 "Show lost events (if recorded)"),
4156 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
4157 "Show round events (if recorded)"),
4158 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
4159 "Show bpf related events (if recorded)"),
4160 OPT_BOOLEAN('\0', "show-text-poke-events", &script.show_text_poke_events,
4161 "Show text poke related events (if recorded)"),
4162 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
4163 "Dump trace output to files named by the monitored events"),
4164 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
4165 OPT_INTEGER(0, "max-blocks", &max_blocks,
4166 "Maximum number of code blocks to dump with brstackinsn"),
4167 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
4168 "Use 9 decimal places when displaying time"),
4169 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
4170 "Instruction Tracing options\n" ITRACE_HELP,
4171 itrace_parse_synth_opts),
4172 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
4173 "Show full source file name path for source lines"),
4174 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
4175 "Enable symbol demangling"),
4176 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
4177 "Enable kernel symbol demangling"),
4178 OPT_CALLBACK(0, "unwind-style", NULL, "unwind style",
4179 "unwind styles (libdw,libunwind)",
4180 unwind__option),
4181 OPT_STRING(0, "addr2line", &symbol_conf.addr2line_path, "path",
4182 "addr2line binary to use for line numbers"),
4183 OPT_STRING(0, "time", &script.time_str, "str",
4184 "Time span of interest (start,stop)"),
4185 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
4186 "Show inline function"),
4187 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
4188 "guest mount directory under which every guest os"
4189 " instance has a subdir"),
4190 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
4191 "file", "file saving guest os vmlinux"),
4192 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
4193 "file", "file saving guest os /proc/kallsyms"),
4194 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
4195 "file", "file saving guest os /proc/modules"),
4196 OPT_BOOLEAN(0, "guest-code", &symbol_conf.guest_code,
4197 "Guest code can be found in hypervisor process"),
4198 OPT_BOOLEAN('\0', "stitch-lbr", &script.stitch_lbr,
4199 "Enable LBR callgraph stitching approach"),
4200 OPT_BOOLEAN('\0', "merge-callchains", &merge_deferred_callchains,
4201 "Enable merge deferred user callchains"),
4202 OPTS_EVSWITCH(&script.evswitch),
4203 OPT_END()
4204 };
4205 const char * const script_subcommands[] = { "record", "report", NULL };
4206 const char *script_usage[] = {
4207 "perf script [<options>]",
4208 "perf script [<options>] record <script> [<record-options>] <command>",
4209 "perf script [<options>] report <script> [script-args]",
4210 "perf script [<options>] <script> [<record-options>] <command>",
4211 "perf script [<options>] <top-script> [script-args]",
4212 NULL
4213 };
4214 struct perf_env *env;
4215
4216 perf_set_singlethreaded();
4217
4218 setup_scripting();
4219
4220 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
4221 PARSE_OPT_STOP_AT_NON_OPTION);
4222
4223 if (symbol_conf.guestmount ||
4224 symbol_conf.default_guest_vmlinux_name ||
4225 symbol_conf.default_guest_kallsyms ||
4226 symbol_conf.default_guest_modules ||
4227 symbol_conf.guest_code) {
4228 /*
4229 * Enable guest sample processing.
4230 */
4231 perf_guest = true;
4232 }
4233
4234 data.path = input_name;
4235 data.force = symbol_conf.force;
4236
4237 if (unsorted_dump)
4238 dump_trace = true;
4239
4240 if (symbol__validate_sym_arguments())
4241 return -1;
4242
4243 if (argc > 1 && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
4244 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
4245 if (!rec_script_path)
4246 return cmd_record(argc, argv);
4247 }
4248
4249 if (argc > 1 && strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
4250 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
4251 if (!rep_script_path) {
4252 fprintf(stderr,
4253 "Please specify a valid report script"
4254 "(see 'perf script -l' for listing)\n");
4255 return -1;
4256 }
4257 }
4258
4259 if (reltime && deltatime) {
4260 fprintf(stderr,
4261 "reltime and deltatime - the two don't get along well. "
4262 "Please limit to --reltime or --deltatime.\n");
4263 return -1;
4264 }
4265
4266 if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
4267 itrace_synth_opts.callchain_sz > scripting_max_stack)
4268 scripting_max_stack = itrace_synth_opts.callchain_sz;
4269
4270 /* make sure PERF_EXEC_PATH is set for scripts */
4271 set_argv_exec_path(get_argv_exec_path());
4272
4273 if (argc && !script_name && !rec_script_path && !rep_script_path) {
4274 int live_pipe[2];
4275 int rep_args;
4276 pid_t pid;
4277
4278 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
4279 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
4280
4281 if (!rec_script_path && !rep_script_path) {
4282 script_name = find_script(argv[0]);
4283 if (script_name) {
4284 argc -= 1;
4285 argv += 1;
4286 goto script_found;
4287 }
4288 usage_with_options_msg(script_usage, options,
4289 "Couldn't find script `%s'\n\n See perf"
4290 " script -l for available scripts.\n", argv[0]);
4291 }
4292
4293 if (is_top_script(argv[0])) {
4294 rep_args = argc - 1;
4295 } else {
4296 int rec_args;
4297
4298 rep_args = has_required_arg(rep_script_path);
4299 rec_args = (argc - 1) - rep_args;
4300 if (rec_args < 0) {
4301 usage_with_options_msg(script_usage, options,
4302 "`%s' script requires options."
4303 "\n\n See perf script -l for available "
4304 "scripts and options.\n", argv[0]);
4305 }
4306 }
4307
4308 if (pipe(live_pipe) < 0) {
4309 perror("failed to create pipe");
4310 return -1;
4311 }
4312
4313 pid = fork();
4314 if (pid < 0) {
4315 perror("failed to fork");
4316 return -1;
4317 }
4318
4319 if (!pid) {
4320 j = 0;
4321
4322 dup2(live_pipe[1], 1);
4323 close(live_pipe[0]);
4324
4325 if (is_top_script(argv[0])) {
4326 system_wide = true;
4327 } else if (!system_wide) {
4328 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
4329 err = -1;
4330 goto out;
4331 }
4332 }
4333
4334 __argv = calloc(argc + 6, sizeof(const char *));
4335 if (!__argv) {
4336 pr_err("malloc failed\n");
4337 err = -ENOMEM;
4338 goto out;
4339 }
4340
4341 __argv[j++] = "/bin/sh";
4342 __argv[j++] = rec_script_path;
4343 if (system_wide)
4344 __argv[j++] = "-a";
4345 __argv[j++] = "-q";
4346 __argv[j++] = "-o";
4347 __argv[j++] = "-";
4348 for (i = rep_args + 1; i < argc; i++)
4349 __argv[j++] = argv[i];
4350 __argv[j++] = NULL;
4351
4352 execvp("/bin/sh", (char **)__argv);
4353 free(__argv);
4354 exit(-1);
4355 }
4356
4357 dup2(live_pipe[0], 0);
4358 close(live_pipe[1]);
4359
4360 __argv = calloc(argc + 4, sizeof(const char *));
4361 if (!__argv) {
4362 pr_err("malloc failed\n");
4363 err = -ENOMEM;
4364 goto out;
4365 }
4366
4367 j = 0;
4368 __argv[j++] = "/bin/sh";
4369 __argv[j++] = rep_script_path;
4370 for (i = 1; i < rep_args + 1; i++)
4371 __argv[j++] = argv[i];
4372 __argv[j++] = "-i";
4373 __argv[j++] = "-";
4374 __argv[j++] = NULL;
4375
4376 execvp("/bin/sh", (char **)__argv);
4377 free(__argv);
4378 exit(-1);
4379 }
4380script_found:
4381 if (rec_script_path)
4382 script_path = rec_script_path;
4383 if (rep_script_path)
4384 script_path = rep_script_path;
4385
4386 if (script_path) {
4387 j = 0;
4388
4389 if (!rec_script_path)
4390 system_wide = false;
4391 else if (!system_wide) {
4392 if (have_cmd(argc - 1, &argv[1]) != 0) {
4393 err = -1;
4394 goto out;
4395 }
4396 }
4397
4398 __argv = calloc(argc + 2, sizeof(const char *));
4399 if (!__argv) {
4400 pr_err("malloc failed\n");
4401 err = -ENOMEM;
4402 goto out;
4403 }
4404
4405 __argv[j++] = "/bin/sh";
4406 __argv[j++] = script_path;
4407 if (system_wide)
4408 __argv[j++] = "-a";
4409 for (i = 2; i < argc; i++)
4410 __argv[j++] = argv[i];
4411 __argv[j++] = NULL;
4412
4413 execvp("/bin/sh", (char **)__argv);
4414 free(__argv);
4415 exit(-1);
4416 }
4417
4418 if (dlfilter_file) {
4419 dlfilter = dlfilter__new(dlfilter_file, dlargc, dlargv);
4420 if (!dlfilter)
4421 return -1;
4422 }
4423
4424 if (!script_name) {
4425 setup_pager();
4426 use_browser = 0;
4427 }
4428
4429 perf_tool__init(&script.tool, !unsorted_dump);
4430 script.tool.sample = process_sample_event;
4431 script.tool.callchain_deferred = process_deferred_sample_event;
4432 script.tool.mmap = perf_event__process_mmap;
4433 script.tool.mmap2 = perf_event__process_mmap2;
4434 script.tool.comm = perf_event__process_comm;
4435 script.tool.namespaces = perf_event__process_namespaces;
4436 script.tool.cgroup = perf_event__process_cgroup;
4437 script.tool.exit = perf_event__process_exit;
4438 script.tool.fork = perf_event__process_fork;
4439 script.tool.attr = process_attr;
4440 script.tool.event_update = perf_event__process_event_update;
4441#ifdef HAVE_LIBTRACEEVENT
4442 script.tool.tracing_data = perf_event__process_tracing_data;
4443#endif
4444 script.tool.feature = perf_event__process_feature;
4445 script.tool.build_id = perf_event__process_build_id;
4446 script.tool.id_index = perf_event__process_id_index;
4447 script.tool.auxtrace_info = perf_script__process_auxtrace_info;
4448 script.tool.auxtrace = perf_event__process_auxtrace;
4449 script.tool.auxtrace_error = perf_event__process_auxtrace_error;
4450 script.tool.stat = perf_event__process_stat_event;
4451 script.tool.stat_round = process_stat_round_event;
4452 script.tool.stat_config = process_stat_config_event;
4453 script.tool.thread_map = process_thread_map_event;
4454 script.tool.cpu_map = process_cpu_map_event;
4455 script.tool.throttle = process_throttle_event;
4456 script.tool.unthrottle = process_throttle_event;
4457 script.tool.ordering_requires_timestamps = true;
4458 script.tool.merge_deferred_callchains = merge_deferred_callchains;
4459 session = perf_session__new(&data, &script.tool);
4460 if (IS_ERR(session))
4461 return PTR_ERR(session);
4462
4463 env = perf_session__env(session);
4464 if (header || header_only) {
4465 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
4466 perf_session__fprintf_info(session, stdout, show_full_info);
4467 if (header_only)
4468 goto out_delete;
4469 }
4470 if (show_full_info)
4471 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
4472
4473 if (symbol__init(env) < 0)
4474 goto out_delete;
4475
4476 script.session = session;
4477 script__setup_sample_type(&script);
4478
4479 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
4480 symbol_conf.graph_function)
4481 itrace_synth_opts.thread_stack = true;
4482
4483 session->itrace_synth_opts = &itrace_synth_opts;
4484
4485 if (cpu_list) {
4486 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
4487 if (err < 0)
4488 goto out_delete;
4489 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
4490 }
4491
4492 if (!no_callchain)
4493 symbol_conf.use_callchain = true;
4494 else
4495 symbol_conf.use_callchain = false;
4496
4497#ifdef HAVE_LIBTRACEEVENT
4498 if (session->tevent.pevent &&
4499 tep_set_function_resolver(session->tevent.pevent,
4500 machine__resolve_kernel_addr,
4501 &session->machines.host) < 0) {
4502 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
4503 err = -1;
4504 goto out_delete;
4505 }
4506#endif
4507 if (generate_script_lang) {
4508 struct stat perf_stat;
4509 int input;
4510 char *filename = strdup("perf-script");
4511
4512 if (output_set_by_user()) {
4513 fprintf(stderr,
4514 "custom fields not supported for generated scripts");
4515 err = -EINVAL;
4516 goto out_delete;
4517 }
4518
4519 input = open(data.path, O_RDONLY); /* input_name */
4520 if (input < 0) {
4521 err = -errno;
4522 perror("failed to open file");
4523 goto out_delete;
4524 }
4525
4526 err = fstat(input, &perf_stat);
4527 if (err < 0) {
4528 perror("failed to stat file");
4529 goto out_delete;
4530 }
4531
4532 if (!perf_stat.st_size) {
4533 fprintf(stderr, "zero-sized file, nothing to do!\n");
4534 goto out_delete;
4535 }
4536
4537 scripting_ops = script_spec__lookup(generate_script_lang);
4538 if (!scripting_ops && ends_with(generate_script_lang, ".py")) {
4539 scripting_ops = script_spec__lookup("python");
4540 free(filename);
4541 filename = strdup(generate_script_lang);
4542 filename[strlen(filename) - 3] = '\0';
4543 } else if (!scripting_ops && ends_with(generate_script_lang, ".pl")) {
4544 scripting_ops = script_spec__lookup("perl");
4545 free(filename);
4546 filename = strdup(generate_script_lang);
4547 filename[strlen(filename) - 3] = '\0';
4548 }
4549 if (!scripting_ops) {
4550 fprintf(stderr, "invalid language specifier '%s'\n", generate_script_lang);
4551 err = -ENOENT;
4552 goto out_delete;
4553 }
4554 if (!filename) {
4555 err = -ENOMEM;
4556 goto out_delete;
4557 }
4558#ifdef HAVE_LIBTRACEEVENT
4559 err = scripting_ops->generate_script(session->tevent.pevent, filename);
4560#else
4561 err = scripting_ops->generate_script(NULL, filename);
4562#endif
4563 free(filename);
4564 goto out_delete;
4565 }
4566
4567 err = dlfilter__start(dlfilter, session);
4568 if (err)
4569 goto out_delete;
4570
4571 if (script_name) {
4572 err = scripting_ops->start_script(script_name, argc, argv, session);
4573 if (err)
4574 goto out_delete;
4575 pr_debug("perf script started with script %s\n\n", script_name);
4576 script_started = true;
4577 }
4578
4579
4580 err = perf_session__check_output_opt(session);
4581 if (err < 0)
4582 goto out_delete;
4583
4584 if (script.time_str) {
4585 err = perf_time__parse_for_ranges_reltime(script.time_str, session,
4586 &script.ptime_range,
4587 &script.range_size,
4588 &script.range_num,
4589 reltime);
4590 if (err < 0)
4591 goto out_delete;
4592
4593 itrace_synth_opts__set_time_range(&itrace_synth_opts,
4594 script.ptime_range,
4595 script.range_num);
4596 }
4597
4598 err = evswitch__init(&script.evswitch, session->evlist, stderr);
4599 if (err)
4600 goto out_delete;
4601
4602 if (zstd_init(&(session->zstd_data), 0) < 0)
4603 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
4604
4605 err = __cmd_script(&script);
4606
4607 flush_scripting();
4608
4609 if (verbose > 2 || debug_kmaps)
4610 perf_session__dump_kmaps(session);
4611
4612out_delete:
4613 if (script.ptime_range) {
4614 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
4615 zfree(&script.ptime_range);
4616 }
4617
4618 zstd_fini(&(session->zstd_data));
4619 evlist__free_stats(session->evlist);
4620 perf_session__delete(session);
4621 perf_script__exit(&script);
4622
4623 if (script_started)
4624 cleanup_scripting();
4625 dlfilter__cleanup(dlfilter);
4626 free_dlarg();
4627out:
4628 return err;
4629}