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