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