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