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