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