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