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