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