Commit | Line | Data |
---|---|---|
5f9c39dc FW |
1 | #include "builtin.h" |
2 | ||
b7eead86 | 3 | #include "perf.h" |
5f9c39dc | 4 | #include "util/cache.h" |
b7eead86 | 5 | #include "util/debug.h" |
4b6ab94e | 6 | #include <subcmd/exec-cmd.h> |
b7eead86 | 7 | #include "util/header.h" |
4b6ab94e | 8 | #include <subcmd/parse-options.h> |
fc36f948 | 9 | #include "util/perf_regs.h" |
b7eead86 | 10 | #include "util/session.h" |
45694aa7 | 11 | #include "util/tool.h" |
5f9c39dc FW |
12 | #include "util/symbol.h" |
13 | #include "util/thread.h" | |
cf72344d | 14 | #include "util/trace-event.h" |
b7eead86 | 15 | #include "util/util.h" |
1424dc96 DA |
16 | #include "util/evlist.h" |
17 | #include "util/evsel.h" | |
36385be5 | 18 | #include "util/sort.h" |
f5fc1412 | 19 | #include "util/data.h" |
7a680eb9 | 20 | #include "util/auxtrace.h" |
cfc8874a JO |
21 | #include "util/cpumap.h" |
22 | #include "util/thread_map.h" | |
23 | #include "util/stat.h" | |
e216708d | 24 | #include "util/thread-stack.h" |
5d67be97 | 25 | #include <linux/bitmap.h> |
6125cc8d | 26 | #include <linux/stringify.h> |
bd48c63e | 27 | #include <linux/time64.h> |
cfc8874a | 28 | #include "asm/bug.h" |
c19ac912 | 29 | #include "util/mem-events.h" |
5f9c39dc | 30 | |
956ffd02 TZ |
31 | static char const *script_name; |
32 | static char const *generate_script_lang; | |
ffabd99e | 33 | static bool debug_mode; |
e1889d75 | 34 | static u64 last_timestamp; |
6fcf7ddb | 35 | static u64 nr_unordered; |
c0230b2b | 36 | static bool no_callchain; |
47390ae2 | 37 | static bool latency_format; |
317df650 | 38 | static bool system_wide; |
400ea6d3 | 39 | static bool print_flags; |
83e19860 | 40 | static bool nanosecs; |
5d67be97 AB |
41 | static const char *cpu_list; |
42 | static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); | |
91a2c3d5 | 43 | static struct perf_stat_config stat_config; |
956ffd02 | 44 | |
44cbe729 | 45 | unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH; |
03cd1fed | 46 | |
745f43e3 DA |
47 | enum perf_output_field { |
48 | PERF_OUTPUT_COMM = 1U << 0, | |
49 | PERF_OUTPUT_TID = 1U << 1, | |
50 | PERF_OUTPUT_PID = 1U << 2, | |
51 | PERF_OUTPUT_TIME = 1U << 3, | |
52 | PERF_OUTPUT_CPU = 1U << 4, | |
53 | PERF_OUTPUT_EVNAME = 1U << 5, | |
54 | PERF_OUTPUT_TRACE = 1U << 6, | |
787bef17 DA |
55 | PERF_OUTPUT_IP = 1U << 7, |
56 | PERF_OUTPUT_SYM = 1U << 8, | |
610723f2 | 57 | PERF_OUTPUT_DSO = 1U << 9, |
7cec0922 | 58 | PERF_OUTPUT_ADDR = 1U << 10, |
a978f2ab | 59 | PERF_OUTPUT_SYMOFFSET = 1U << 11, |
cc8fae1d | 60 | PERF_OUTPUT_SRCLINE = 1U << 12, |
535aeaae | 61 | PERF_OUTPUT_PERIOD = 1U << 13, |
fc36f948 | 62 | PERF_OUTPUT_IREGS = 1U << 14, |
dc323ce8 SE |
63 | PERF_OUTPUT_BRSTACK = 1U << 15, |
64 | PERF_OUTPUT_BRSTACKSYM = 1U << 16, | |
94ddddfa JO |
65 | PERF_OUTPUT_DATA_SRC = 1U << 17, |
66 | PERF_OUTPUT_WEIGHT = 1U << 18, | |
30372f04 | 67 | PERF_OUTPUT_BPF_OUTPUT = 1U << 19, |
e216708d | 68 | PERF_OUTPUT_CALLINDENT = 1U << 20, |
745f43e3 DA |
69 | }; |
70 | ||
71 | struct output_option { | |
72 | const char *str; | |
73 | enum perf_output_field field; | |
74 | } all_output_options[] = { | |
75 | {.str = "comm", .field = PERF_OUTPUT_COMM}, | |
76 | {.str = "tid", .field = PERF_OUTPUT_TID}, | |
77 | {.str = "pid", .field = PERF_OUTPUT_PID}, | |
78 | {.str = "time", .field = PERF_OUTPUT_TIME}, | |
79 | {.str = "cpu", .field = PERF_OUTPUT_CPU}, | |
80 | {.str = "event", .field = PERF_OUTPUT_EVNAME}, | |
81 | {.str = "trace", .field = PERF_OUTPUT_TRACE}, | |
787bef17 | 82 | {.str = "ip", .field = PERF_OUTPUT_IP}, |
c0230b2b | 83 | {.str = "sym", .field = PERF_OUTPUT_SYM}, |
610723f2 | 84 | {.str = "dso", .field = PERF_OUTPUT_DSO}, |
7cec0922 | 85 | {.str = "addr", .field = PERF_OUTPUT_ADDR}, |
a978f2ab | 86 | {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET}, |
cc8fae1d | 87 | {.str = "srcline", .field = PERF_OUTPUT_SRCLINE}, |
535aeaae | 88 | {.str = "period", .field = PERF_OUTPUT_PERIOD}, |
fc36f948 | 89 | {.str = "iregs", .field = PERF_OUTPUT_IREGS}, |
dc323ce8 SE |
90 | {.str = "brstack", .field = PERF_OUTPUT_BRSTACK}, |
91 | {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM}, | |
94ddddfa JO |
92 | {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC}, |
93 | {.str = "weight", .field = PERF_OUTPUT_WEIGHT}, | |
30372f04 | 94 | {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT}, |
e216708d | 95 | {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT}, |
745f43e3 DA |
96 | }; |
97 | ||
98 | /* default set to maintain compatibility with current format */ | |
2c9e45f7 DA |
99 | static struct { |
100 | bool user_set; | |
9cbdb702 | 101 | bool wildcard_set; |
a6ffaf91 | 102 | unsigned int print_ip_opts; |
2c9e45f7 DA |
103 | u64 fields; |
104 | u64 invalid_fields; | |
105 | } output[PERF_TYPE_MAX] = { | |
106 | ||
107 | [PERF_TYPE_HARDWARE] = { | |
108 | .user_set = false, | |
109 | ||
110 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
111 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
787bef17 | 112 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
e8564b71 JO |
113 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
114 | PERF_OUTPUT_PERIOD, | |
2c9e45f7 | 115 | |
30372f04 | 116 | .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, |
2c9e45f7 DA |
117 | }, |
118 | ||
119 | [PERF_TYPE_SOFTWARE] = { | |
120 | .user_set = false, | |
121 | ||
122 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
123 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
787bef17 | 124 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
e8564b71 | 125 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
30372f04 | 126 | PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT, |
2c9e45f7 DA |
127 | |
128 | .invalid_fields = PERF_OUTPUT_TRACE, | |
129 | }, | |
130 | ||
131 | [PERF_TYPE_TRACEPOINT] = { | |
132 | .user_set = false, | |
133 | ||
134 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
135 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
30372f04 | 136 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE |
2c9e45f7 | 137 | }, |
0817a6a3 AS |
138 | |
139 | [PERF_TYPE_RAW] = { | |
140 | .user_set = false, | |
141 | ||
142 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
143 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
787bef17 | 144 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
e8564b71 | 145 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
ff7b1915 JO |
146 | PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR | |
147 | PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT, | |
0817a6a3 | 148 | |
30372f04 | 149 | .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, |
0817a6a3 | 150 | }, |
27cfef00 WN |
151 | |
152 | [PERF_TYPE_BREAKPOINT] = { | |
153 | .user_set = false, | |
154 | ||
155 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
156 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
157 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | | |
158 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | | |
159 | PERF_OUTPUT_PERIOD, | |
160 | ||
30372f04 | 161 | .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT, |
27cfef00 | 162 | }, |
1424dc96 | 163 | }; |
745f43e3 | 164 | |
2c9e45f7 DA |
165 | static bool output_set_by_user(void) |
166 | { | |
167 | int j; | |
168 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
169 | if (output[j].user_set) | |
170 | return true; | |
171 | } | |
172 | return false; | |
173 | } | |
745f43e3 | 174 | |
9cbdb702 DA |
175 | static const char *output_field2str(enum perf_output_field field) |
176 | { | |
177 | int i, imax = ARRAY_SIZE(all_output_options); | |
178 | const char *str = ""; | |
179 | ||
180 | for (i = 0; i < imax; ++i) { | |
181 | if (all_output_options[i].field == field) { | |
182 | str = all_output_options[i].str; | |
183 | break; | |
184 | } | |
185 | } | |
186 | return str; | |
187 | } | |
188 | ||
2c9e45f7 | 189 | #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x) |
1424dc96 | 190 | |
6d5cdd64 AH |
191 | static int perf_evsel__do_check_stype(struct perf_evsel *evsel, |
192 | u64 sample_type, const char *sample_msg, | |
193 | enum perf_output_field field, | |
194 | bool allow_user_set) | |
1424dc96 | 195 | { |
5bff01f6 | 196 | struct perf_event_attr *attr = &evsel->attr; |
9cbdb702 DA |
197 | int type = attr->type; |
198 | const char *evname; | |
199 | ||
200 | if (attr->sample_type & sample_type) | |
201 | return 0; | |
202 | ||
203 | if (output[type].user_set) { | |
6d5cdd64 AH |
204 | if (allow_user_set) |
205 | return 0; | |
5bff01f6 | 206 | evname = perf_evsel__name(evsel); |
9cbdb702 DA |
207 | pr_err("Samples for '%s' event do not have %s attribute set. " |
208 | "Cannot print '%s' field.\n", | |
209 | evname, sample_msg, output_field2str(field)); | |
210 | return -1; | |
211 | } | |
212 | ||
213 | /* user did not ask for it explicitly so remove from the default list */ | |
214 | output[type].fields &= ~field; | |
5bff01f6 | 215 | evname = perf_evsel__name(evsel); |
9cbdb702 DA |
216 | pr_debug("Samples for '%s' event do not have %s attribute set. " |
217 | "Skipping '%s' field.\n", | |
218 | evname, sample_msg, output_field2str(field)); | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
6d5cdd64 AH |
223 | static int perf_evsel__check_stype(struct perf_evsel *evsel, |
224 | u64 sample_type, const char *sample_msg, | |
225 | enum perf_output_field field) | |
226 | { | |
227 | return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field, | |
228 | false); | |
229 | } | |
230 | ||
9cbdb702 DA |
231 | static int perf_evsel__check_attr(struct perf_evsel *evsel, |
232 | struct perf_session *session) | |
233 | { | |
234 | struct perf_event_attr *attr = &evsel->attr; | |
6d5cdd64 AH |
235 | bool allow_user_set; |
236 | ||
e099eba8 JO |
237 | if (perf_header__has_feat(&session->header, HEADER_STAT)) |
238 | return 0; | |
239 | ||
6d5cdd64 AH |
240 | allow_user_set = perf_header__has_feat(&session->header, |
241 | HEADER_AUXTRACE); | |
9cbdb702 | 242 | |
1424dc96 DA |
243 | if (PRINT_FIELD(TRACE) && |
244 | !perf_session__has_traces(session, "record -R")) | |
245 | return -EINVAL; | |
246 | ||
787bef17 | 247 | if (PRINT_FIELD(IP)) { |
5bff01f6 ACM |
248 | if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", |
249 | PERF_OUTPUT_IP)) | |
1424dc96 | 250 | return -EINVAL; |
1424dc96 | 251 | } |
7cec0922 DA |
252 | |
253 | if (PRINT_FIELD(ADDR) && | |
6d5cdd64 AH |
254 | perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", |
255 | PERF_OUTPUT_ADDR, allow_user_set)) | |
7cec0922 DA |
256 | return -EINVAL; |
257 | ||
94ddddfa JO |
258 | if (PRINT_FIELD(DATA_SRC) && |
259 | perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", | |
260 | PERF_OUTPUT_DATA_SRC)) | |
261 | return -EINVAL; | |
262 | ||
263 | if (PRINT_FIELD(WEIGHT) && | |
264 | perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT", | |
265 | PERF_OUTPUT_WEIGHT)) | |
266 | return -EINVAL; | |
267 | ||
7cec0922 DA |
268 | if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { |
269 | pr_err("Display of symbols requested but neither sample IP nor " | |
270 | "sample address\nis selected. Hence, no addresses to convert " | |
271 | "to symbols.\n"); | |
787bef17 DA |
272 | return -EINVAL; |
273 | } | |
a978f2ab AN |
274 | if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) { |
275 | pr_err("Display of offsets requested but symbol is not" | |
276 | "selected.\n"); | |
277 | return -EINVAL; | |
278 | } | |
7cec0922 DA |
279 | if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { |
280 | pr_err("Display of DSO requested but neither sample IP nor " | |
281 | "sample address\nis selected. Hence, no addresses to convert " | |
282 | "to DSO.\n"); | |
610723f2 DA |
283 | return -EINVAL; |
284 | } | |
cc8fae1d AH |
285 | if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) { |
286 | pr_err("Display of source line number requested but sample IP is not\n" | |
287 | "selected. Hence, no address to lookup the source line number.\n"); | |
288 | return -EINVAL; | |
289 | } | |
1424dc96 DA |
290 | |
291 | if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) && | |
5bff01f6 ACM |
292 | perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", |
293 | PERF_OUTPUT_TID|PERF_OUTPUT_PID)) | |
1424dc96 | 294 | return -EINVAL; |
1424dc96 DA |
295 | |
296 | if (PRINT_FIELD(TIME) && | |
5bff01f6 ACM |
297 | perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", |
298 | PERF_OUTPUT_TIME)) | |
1424dc96 | 299 | return -EINVAL; |
1424dc96 DA |
300 | |
301 | if (PRINT_FIELD(CPU) && | |
6d5cdd64 AH |
302 | perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", |
303 | PERF_OUTPUT_CPU, allow_user_set)) | |
1424dc96 | 304 | return -EINVAL; |
9cbdb702 | 305 | |
535aeaae JO |
306 | if (PRINT_FIELD(PERIOD) && |
307 | perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD", | |
308 | PERF_OUTPUT_PERIOD)) | |
309 | return -EINVAL; | |
310 | ||
fc36f948 SE |
311 | if (PRINT_FIELD(IREGS) && |
312 | perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", | |
313 | PERF_OUTPUT_IREGS)) | |
314 | return -EINVAL; | |
315 | ||
9cbdb702 DA |
316 | return 0; |
317 | } | |
318 | ||
7ea95727 AH |
319 | static void set_print_ip_opts(struct perf_event_attr *attr) |
320 | { | |
321 | unsigned int type = attr->type; | |
322 | ||
323 | output[type].print_ip_opts = 0; | |
324 | if (PRINT_FIELD(IP)) | |
e20ab86e | 325 | output[type].print_ip_opts |= EVSEL__PRINT_IP; |
7ea95727 AH |
326 | |
327 | if (PRINT_FIELD(SYM)) | |
e20ab86e | 328 | output[type].print_ip_opts |= EVSEL__PRINT_SYM; |
7ea95727 AH |
329 | |
330 | if (PRINT_FIELD(DSO)) | |
e20ab86e | 331 | output[type].print_ip_opts |= EVSEL__PRINT_DSO; |
7ea95727 AH |
332 | |
333 | if (PRINT_FIELD(SYMOFFSET)) | |
e20ab86e | 334 | output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET; |
cc8fae1d AH |
335 | |
336 | if (PRINT_FIELD(SRCLINE)) | |
e20ab86e | 337 | output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE; |
7ea95727 AH |
338 | } |
339 | ||
9cbdb702 DA |
340 | /* |
341 | * verify all user requested events exist and the samples | |
342 | * have the expected data | |
343 | */ | |
344 | static int perf_session__check_output_opt(struct perf_session *session) | |
345 | { | |
40f20e50 | 346 | unsigned int j; |
9cbdb702 DA |
347 | struct perf_evsel *evsel; |
348 | ||
349 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
350 | evsel = perf_session__find_first_evtype(session, j); | |
351 | ||
352 | /* | |
353 | * even if fields is set to 0 (ie., show nothing) event must | |
354 | * exist if user explicitly includes it on the command line | |
355 | */ | |
356 | if (!evsel && output[j].user_set && !output[j].wildcard_set) { | |
357 | pr_err("%s events do not exist. " | |
358 | "Remove corresponding -f option to proceed.\n", | |
359 | event_type(j)); | |
360 | return -1; | |
361 | } | |
362 | ||
363 | if (evsel && output[j].fields && | |
364 | perf_evsel__check_attr(evsel, session)) | |
365 | return -1; | |
a6ffaf91 DA |
366 | |
367 | if (evsel == NULL) | |
368 | continue; | |
369 | ||
7ea95727 | 370 | set_print_ip_opts(&evsel->attr); |
1424dc96 DA |
371 | } |
372 | ||
98526ee7 AH |
373 | if (!no_callchain) { |
374 | bool use_callchain = false; | |
71ac899b | 375 | bool not_pipe = false; |
98526ee7 | 376 | |
e5cadb93 | 377 | evlist__for_each_entry(session->evlist, evsel) { |
71ac899b | 378 | not_pipe = true; |
98526ee7 AH |
379 | if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { |
380 | use_callchain = true; | |
381 | break; | |
382 | } | |
383 | } | |
71ac899b | 384 | if (not_pipe && !use_callchain) |
98526ee7 AH |
385 | symbol_conf.use_callchain = false; |
386 | } | |
387 | ||
80b8b496 DA |
388 | /* |
389 | * set default for tracepoints to print symbols only | |
390 | * if callchains are present | |
391 | */ | |
392 | if (symbol_conf.use_callchain && | |
393 | !output[PERF_TYPE_TRACEPOINT].user_set) { | |
394 | struct perf_event_attr *attr; | |
395 | ||
396 | j = PERF_TYPE_TRACEPOINT; | |
80b8b496 | 397 | |
e5cadb93 | 398 | evlist__for_each_entry(session->evlist, evsel) { |
40f20e50 HK |
399 | if (evsel->attr.type != j) |
400 | continue; | |
401 | ||
402 | attr = &evsel->attr; | |
80b8b496 | 403 | |
40f20e50 HK |
404 | if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) { |
405 | output[j].fields |= PERF_OUTPUT_IP; | |
406 | output[j].fields |= PERF_OUTPUT_SYM; | |
407 | output[j].fields |= PERF_OUTPUT_DSO; | |
408 | set_print_ip_opts(attr); | |
409 | goto out; | |
410 | } | |
80b8b496 DA |
411 | } |
412 | } | |
413 | ||
414 | out: | |
1424dc96 DA |
415 | return 0; |
416 | } | |
745f43e3 | 417 | |
a3dff304 | 418 | static void print_sample_iregs(struct perf_sample *sample, |
fc36f948 SE |
419 | struct perf_event_attr *attr) |
420 | { | |
421 | struct regs_dump *regs = &sample->intr_regs; | |
422 | uint64_t mask = attr->sample_regs_intr; | |
423 | unsigned i = 0, r; | |
424 | ||
425 | if (!regs) | |
426 | return; | |
427 | ||
428 | for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) { | |
429 | u64 val = regs->regs[i++]; | |
430 | printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val); | |
431 | } | |
432 | } | |
433 | ||
fcf65bf1 | 434 | static void print_sample_start(struct perf_sample *sample, |
1424dc96 | 435 | struct thread *thread, |
5bff01f6 | 436 | struct perf_evsel *evsel) |
c70c94b4 | 437 | { |
5bff01f6 | 438 | struct perf_event_attr *attr = &evsel->attr; |
c70c94b4 DA |
439 | unsigned long secs; |
440 | unsigned long usecs; | |
745f43e3 DA |
441 | unsigned long long nsecs; |
442 | ||
443 | if (PRINT_FIELD(COMM)) { | |
444 | if (latency_format) | |
b9c5143a | 445 | printf("%8.8s ", thread__comm_str(thread)); |
787bef17 | 446 | else if (PRINT_FIELD(IP) && symbol_conf.use_callchain) |
b9c5143a | 447 | printf("%s ", thread__comm_str(thread)); |
745f43e3 | 448 | else |
b9c5143a | 449 | printf("%16s ", thread__comm_str(thread)); |
745f43e3 | 450 | } |
c70c94b4 | 451 | |
745f43e3 DA |
452 | if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) |
453 | printf("%5d/%-5d ", sample->pid, sample->tid); | |
454 | else if (PRINT_FIELD(PID)) | |
455 | printf("%5d ", sample->pid); | |
456 | else if (PRINT_FIELD(TID)) | |
457 | printf("%5d ", sample->tid); | |
458 | ||
459 | if (PRINT_FIELD(CPU)) { | |
460 | if (latency_format) | |
461 | printf("%3d ", sample->cpu); | |
462 | else | |
463 | printf("[%03d] ", sample->cpu); | |
464 | } | |
c70c94b4 | 465 | |
745f43e3 DA |
466 | if (PRINT_FIELD(TIME)) { |
467 | nsecs = sample->time; | |
bd48c63e ACM |
468 | secs = nsecs / NSEC_PER_SEC; |
469 | nsecs -= secs * NSEC_PER_SEC; | |
470 | usecs = nsecs / NSEC_PER_USEC; | |
83e19860 AH |
471 | if (nanosecs) |
472 | printf("%5lu.%09llu: ", secs, nsecs); | |
473 | else | |
474 | printf("%5lu.%06lu: ", secs, usecs); | |
745f43e3 | 475 | } |
c70c94b4 DA |
476 | } |
477 | ||
dc323ce8 SE |
478 | static inline char |
479 | mispred_str(struct branch_entry *br) | |
480 | { | |
481 | if (!(br->flags.mispred || br->flags.predicted)) | |
482 | return '-'; | |
483 | ||
484 | return br->flags.predicted ? 'P' : 'M'; | |
485 | } | |
486 | ||
a3dff304 | 487 | static void print_sample_brstack(struct perf_sample *sample) |
dc323ce8 SE |
488 | { |
489 | struct branch_stack *br = sample->branch_stack; | |
490 | u64 i; | |
491 | ||
492 | if (!(br && br->nr)) | |
493 | return; | |
494 | ||
495 | for (i = 0; i < br->nr; i++) { | |
496 | printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ", | |
497 | br->entries[i].from, | |
498 | br->entries[i].to, | |
499 | mispred_str( br->entries + i), | |
500 | br->entries[i].flags.in_tx? 'X' : '-', | |
501 | br->entries[i].flags.abort? 'A' : '-', | |
502 | br->entries[i].flags.cycles); | |
503 | } | |
504 | } | |
505 | ||
a3dff304 ACM |
506 | static void print_sample_brstacksym(struct perf_sample *sample, |
507 | struct thread *thread) | |
dc323ce8 SE |
508 | { |
509 | struct branch_stack *br = sample->branch_stack; | |
510 | struct addr_location alf, alt; | |
dc323ce8 SE |
511 | u64 i, from, to; |
512 | ||
513 | if (!(br && br->nr)) | |
514 | return; | |
515 | ||
516 | for (i = 0; i < br->nr; i++) { | |
517 | ||
518 | memset(&alf, 0, sizeof(alf)); | |
519 | memset(&alt, 0, sizeof(alt)); | |
520 | from = br->entries[i].from; | |
521 | to = br->entries[i].to; | |
522 | ||
473398a2 | 523 | thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf); |
dc323ce8 | 524 | if (alf.map) |
be39db9f | 525 | alf.sym = map__find_symbol(alf.map, alf.addr); |
dc323ce8 | 526 | |
473398a2 | 527 | thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt); |
dc323ce8 | 528 | if (alt.map) |
be39db9f | 529 | alt.sym = map__find_symbol(alt.map, alt.addr); |
dc323ce8 SE |
530 | |
531 | symbol__fprintf_symname_offs(alf.sym, &alf, stdout); | |
532 | putchar('/'); | |
533 | symbol__fprintf_symname_offs(alt.sym, &alt, stdout); | |
534 | printf("/%c/%c/%c/%d ", | |
535 | mispred_str( br->entries + i), | |
536 | br->entries[i].flags.in_tx? 'X' : '-', | |
537 | br->entries[i].flags.abort? 'A' : '-', | |
538 | br->entries[i].flags.cycles); | |
539 | } | |
540 | } | |
541 | ||
542 | ||
a3dff304 | 543 | static void print_sample_addr(struct perf_sample *sample, |
7cec0922 DA |
544 | struct thread *thread, |
545 | struct perf_event_attr *attr) | |
546 | { | |
547 | struct addr_location al; | |
7cec0922 DA |
548 | |
549 | printf("%16" PRIx64, sample->addr); | |
550 | ||
551 | if (!sample_addr_correlates_sym(attr)) | |
552 | return; | |
553 | ||
c2740a87 | 554 | thread__resolve(thread, &al, sample); |
7cec0922 DA |
555 | |
556 | if (PRINT_FIELD(SYM)) { | |
547a92e0 | 557 | printf(" "); |
a978f2ab AN |
558 | if (PRINT_FIELD(SYMOFFSET)) |
559 | symbol__fprintf_symname_offs(al.sym, &al, stdout); | |
560 | else | |
561 | symbol__fprintf_symname(al.sym, stdout); | |
7cec0922 DA |
562 | } |
563 | ||
564 | if (PRINT_FIELD(DSO)) { | |
547a92e0 AN |
565 | printf(" ("); |
566 | map__fprintf_dsoname(al.map, stdout); | |
567 | printf(")"); | |
7cec0922 DA |
568 | } |
569 | } | |
570 | ||
e216708d AH |
571 | static void print_sample_callindent(struct perf_sample *sample, |
572 | struct perf_evsel *evsel, | |
573 | struct thread *thread, | |
574 | struct addr_location *al) | |
575 | { | |
576 | struct perf_event_attr *attr = &evsel->attr; | |
577 | size_t depth = thread_stack__depth(thread); | |
578 | struct addr_location addr_al; | |
579 | const char *name = NULL; | |
580 | static int spacing; | |
581 | int len = 0; | |
582 | u64 ip = 0; | |
583 | ||
584 | /* | |
585 | * The 'return' has already been popped off the stack so the depth has | |
586 | * to be adjusted to match the 'call'. | |
587 | */ | |
588 | if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN) | |
589 | depth += 1; | |
590 | ||
591 | if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) { | |
592 | if (sample_addr_correlates_sym(attr)) { | |
593 | thread__resolve(thread, &addr_al, sample); | |
594 | if (addr_al.sym) | |
595 | name = addr_al.sym->name; | |
596 | else | |
597 | ip = sample->addr; | |
598 | } else { | |
599 | ip = sample->addr; | |
600 | } | |
601 | } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) { | |
602 | if (al->sym) | |
603 | name = al->sym->name; | |
604 | else | |
605 | ip = sample->ip; | |
606 | } | |
607 | ||
608 | if (name) | |
609 | len = printf("%*s%s", (int)depth * 4, "", name); | |
610 | else if (ip) | |
611 | len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip); | |
612 | ||
613 | if (len < 0) | |
614 | return; | |
615 | ||
616 | /* | |
617 | * Try to keep the output length from changing frequently so that the | |
618 | * output lines up more nicely. | |
619 | */ | |
620 | if (len > spacing || (len && len < spacing - 52)) | |
621 | spacing = round_up(len + 4, 32); | |
622 | ||
623 | if (len < spacing) | |
624 | printf("%*s", spacing - len, ""); | |
625 | } | |
626 | ||
a3dff304 | 627 | static void print_sample_bts(struct perf_sample *sample, |
95582596 | 628 | struct perf_evsel *evsel, |
a2cb3cf2 AH |
629 | struct thread *thread, |
630 | struct addr_location *al) | |
95582596 AN |
631 | { |
632 | struct perf_event_attr *attr = &evsel->attr; | |
8066be5f | 633 | bool print_srcline_last = false; |
95582596 | 634 | |
e216708d AH |
635 | if (PRINT_FIELD(CALLINDENT)) |
636 | print_sample_callindent(sample, evsel, thread, al); | |
637 | ||
95582596 AN |
638 | /* print branch_from information */ |
639 | if (PRINT_FIELD(IP)) { | |
8066be5f | 640 | unsigned int print_opts = output[attr->type].print_ip_opts; |
e557b674 | 641 | struct callchain_cursor *cursor = NULL; |
8066be5f | 642 | |
6f736735 | 643 | if (symbol_conf.use_callchain && sample->callchain && |
e557b674 | 644 | thread__resolve_callchain(al->thread, &callchain_cursor, evsel, |
6f736735 | 645 | sample, NULL, NULL, scripting_max_stack) == 0) |
e557b674 | 646 | cursor = &callchain_cursor; |
6f736735 ACM |
647 | |
648 | if (cursor == NULL) { | |
649 | putchar(' '); | |
e20ab86e | 650 | if (print_opts & EVSEL__PRINT_SRCLINE) { |
8066be5f | 651 | print_srcline_last = true; |
e20ab86e | 652 | print_opts &= ~EVSEL__PRINT_SRCLINE; |
8066be5f | 653 | } |
6f736735 ACM |
654 | } else |
655 | putchar('\n'); | |
656 | ||
657 | sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout); | |
95582596 AN |
658 | } |
659 | ||
95582596 | 660 | /* print branch_to information */ |
243be3dd AH |
661 | if (PRINT_FIELD(ADDR) || |
662 | ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) && | |
578bea40 AH |
663 | !output[attr->type].user_set)) { |
664 | printf(" => "); | |
a3dff304 | 665 | print_sample_addr(sample, thread, attr); |
578bea40 | 666 | } |
95582596 | 667 | |
8066be5f AH |
668 | if (print_srcline_last) |
669 | map__fprintf_srcline(al->map, al->addr, "\n ", stdout); | |
670 | ||
95582596 AN |
671 | printf("\n"); |
672 | } | |
673 | ||
055cd33d AH |
674 | static struct { |
675 | u32 flags; | |
676 | const char *name; | |
677 | } sample_flags[] = { | |
678 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"}, | |
679 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"}, | |
680 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"}, | |
681 | {PERF_IP_FLAG_BRANCH, "jmp"}, | |
682 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"}, | |
683 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"}, | |
684 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"}, | |
685 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"}, | |
686 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"}, | |
687 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"}, | |
688 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"}, | |
689 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"}, | |
690 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"}, | |
691 | {0, NULL} | |
692 | }; | |
693 | ||
400ea6d3 AH |
694 | static void print_sample_flags(u32 flags) |
695 | { | |
696 | const char *chars = PERF_IP_FLAG_CHARS; | |
697 | const int n = strlen(PERF_IP_FLAG_CHARS); | |
055cd33d AH |
698 | bool in_tx = flags & PERF_IP_FLAG_IN_TX; |
699 | const char *name = NULL; | |
400ea6d3 AH |
700 | char str[33]; |
701 | int i, pos = 0; | |
702 | ||
055cd33d AH |
703 | for (i = 0; sample_flags[i].name ; i++) { |
704 | if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) { | |
705 | name = sample_flags[i].name; | |
706 | break; | |
707 | } | |
708 | } | |
709 | ||
400ea6d3 AH |
710 | for (i = 0; i < n; i++, flags >>= 1) { |
711 | if (flags & 1) | |
712 | str[pos++] = chars[i]; | |
713 | } | |
714 | for (; i < 32; i++, flags >>= 1) { | |
715 | if (flags & 1) | |
716 | str[pos++] = '?'; | |
717 | } | |
718 | str[pos] = 0; | |
055cd33d AH |
719 | |
720 | if (name) | |
721 | printf(" %-7s%4s ", name, in_tx ? "(x)" : ""); | |
722 | else | |
723 | printf(" %-11s ", str); | |
400ea6d3 AH |
724 | } |
725 | ||
30372f04 WN |
726 | struct printer_data { |
727 | int line_no; | |
728 | bool hit_nul; | |
729 | bool is_printable; | |
730 | }; | |
731 | ||
732 | static void | |
733 | print_sample_bpf_output_printer(enum binary_printer_ops op, | |
734 | unsigned int val, | |
735 | void *extra) | |
736 | { | |
737 | unsigned char ch = (unsigned char)val; | |
738 | struct printer_data *printer_data = extra; | |
739 | ||
740 | switch (op) { | |
741 | case BINARY_PRINT_DATA_BEGIN: | |
742 | printf("\n"); | |
743 | break; | |
744 | case BINARY_PRINT_LINE_BEGIN: | |
745 | printf("%17s", !printer_data->line_no ? "BPF output:" : | |
746 | " "); | |
747 | break; | |
748 | case BINARY_PRINT_ADDR: | |
749 | printf(" %04x:", val); | |
750 | break; | |
751 | case BINARY_PRINT_NUM_DATA: | |
752 | printf(" %02x", val); | |
753 | break; | |
754 | case BINARY_PRINT_NUM_PAD: | |
755 | printf(" "); | |
756 | break; | |
757 | case BINARY_PRINT_SEP: | |
758 | printf(" "); | |
759 | break; | |
760 | case BINARY_PRINT_CHAR_DATA: | |
761 | if (printer_data->hit_nul && ch) | |
762 | printer_data->is_printable = false; | |
763 | ||
764 | if (!isprint(ch)) { | |
765 | printf("%c", '.'); | |
766 | ||
767 | if (!printer_data->is_printable) | |
768 | break; | |
769 | ||
770 | if (ch == '\0') | |
771 | printer_data->hit_nul = true; | |
772 | else | |
773 | printer_data->is_printable = false; | |
774 | } else { | |
775 | printf("%c", ch); | |
776 | } | |
777 | break; | |
778 | case BINARY_PRINT_CHAR_PAD: | |
779 | printf(" "); | |
780 | break; | |
781 | case BINARY_PRINT_LINE_END: | |
782 | printf("\n"); | |
783 | printer_data->line_no++; | |
784 | break; | |
785 | case BINARY_PRINT_DATA_END: | |
786 | default: | |
787 | break; | |
788 | } | |
789 | } | |
790 | ||
791 | static void print_sample_bpf_output(struct perf_sample *sample) | |
792 | { | |
793 | unsigned int nr_bytes = sample->raw_size; | |
794 | struct printer_data printer_data = {0, false, true}; | |
795 | ||
796 | print_binary(sample->raw_data, nr_bytes, 8, | |
797 | print_sample_bpf_output_printer, &printer_data); | |
798 | ||
799 | if (printer_data.is_printable && printer_data.hit_nul) | |
800 | printf("%17s \"%s\"\n", "BPF string:", | |
801 | (char *)(sample->raw_data)); | |
802 | } | |
803 | ||
809e9423 JO |
804 | struct perf_script { |
805 | struct perf_tool tool; | |
806 | struct perf_session *session; | |
807 | bool show_task_events; | |
808 | bool show_mmap_events; | |
809 | bool show_switch_events; | |
cfc8874a JO |
810 | bool allocated; |
811 | struct cpu_map *cpus; | |
812 | struct thread_map *threads; | |
9cdbc409 | 813 | int name_width; |
809e9423 JO |
814 | }; |
815 | ||
9cdbc409 JO |
816 | static int perf_evlist__max_name_len(struct perf_evlist *evlist) |
817 | { | |
818 | struct perf_evsel *evsel; | |
819 | int max = 0; | |
820 | ||
e5cadb93 | 821 | evlist__for_each_entry(evlist, evsel) { |
9cdbc409 JO |
822 | int len = strlen(perf_evsel__name(evsel)); |
823 | ||
824 | max = MAX(len, max); | |
825 | } | |
826 | ||
827 | return max; | |
828 | } | |
829 | ||
c19ac912 JO |
830 | static size_t data_src__printf(u64 data_src) |
831 | { | |
832 | struct mem_info mi = { .data_src.val = data_src }; | |
833 | char decode[100]; | |
834 | char out[100]; | |
835 | static int maxlen; | |
836 | int len; | |
837 | ||
838 | perf_script__meminfo_scnprintf(decode, 100, &mi); | |
839 | ||
840 | len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode); | |
841 | if (maxlen < len) | |
842 | maxlen = len; | |
843 | ||
844 | return printf("%-*s", maxlen, out); | |
845 | } | |
846 | ||
a3dff304 | 847 | static void process_event(struct perf_script *script, |
809e9423 JO |
848 | struct perf_sample *sample, struct perf_evsel *evsel, |
849 | struct addr_location *al) | |
be6d842a | 850 | { |
f9d5d549 | 851 | struct thread *thread = al->thread; |
9e69c210 | 852 | struct perf_event_attr *attr = &evsel->attr; |
1424dc96 | 853 | |
2c9e45f7 | 854 | if (output[attr->type].fields == 0) |
1424dc96 DA |
855 | return; |
856 | ||
fcf65bf1 | 857 | print_sample_start(sample, thread, evsel); |
745f43e3 | 858 | |
535aeaae JO |
859 | if (PRINT_FIELD(PERIOD)) |
860 | printf("%10" PRIu64 " ", sample->period); | |
861 | ||
e944d3d7 NK |
862 | if (PRINT_FIELD(EVNAME)) { |
863 | const char *evname = perf_evsel__name(evsel); | |
9cdbc409 JO |
864 | |
865 | if (!script->name_width) | |
866 | script->name_width = perf_evlist__max_name_len(script->session->evlist); | |
867 | ||
868 | printf("%*s: ", script->name_width, | |
869 | evname ? evname : "[unknown]"); | |
e944d3d7 NK |
870 | } |
871 | ||
400ea6d3 AH |
872 | if (print_flags) |
873 | print_sample_flags(sample->flags); | |
874 | ||
95582596 | 875 | if (is_bts_event(attr)) { |
a3dff304 | 876 | print_sample_bts(sample, evsel, thread, al); |
95582596 AN |
877 | return; |
878 | } | |
879 | ||
745f43e3 | 880 | if (PRINT_FIELD(TRACE)) |
fcf65bf1 ACM |
881 | event_format__print(evsel->tp_format, sample->cpu, |
882 | sample->raw_data, sample->raw_size); | |
7cec0922 | 883 | if (PRINT_FIELD(ADDR)) |
a3dff304 | 884 | print_sample_addr(sample, thread, attr); |
7cec0922 | 885 | |
94ddddfa | 886 | if (PRINT_FIELD(DATA_SRC)) |
c19ac912 | 887 | data_src__printf(sample->data_src); |
94ddddfa JO |
888 | |
889 | if (PRINT_FIELD(WEIGHT)) | |
890 | printf("%16" PRIu64, sample->weight); | |
891 | ||
787bef17 | 892 | if (PRINT_FIELD(IP)) { |
e557b674 | 893 | struct callchain_cursor *cursor = NULL; |
6f736735 | 894 | |
92231521 | 895 | if (symbol_conf.use_callchain && sample->callchain && |
e557b674 | 896 | thread__resolve_callchain(al->thread, &callchain_cursor, evsel, |
6f736735 | 897 | sample, NULL, NULL, scripting_max_stack) == 0) |
e557b674 | 898 | cursor = &callchain_cursor; |
a6ffaf91 | 899 | |
6f736735 ACM |
900 | putchar(cursor ? '\n' : ' '); |
901 | sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout); | |
c0230b2b DA |
902 | } |
903 | ||
fc36f948 | 904 | if (PRINT_FIELD(IREGS)) |
a3dff304 | 905 | print_sample_iregs(sample, attr); |
fc36f948 | 906 | |
dc323ce8 | 907 | if (PRINT_FIELD(BRSTACK)) |
a3dff304 | 908 | print_sample_brstack(sample); |
dc323ce8 | 909 | else if (PRINT_FIELD(BRSTACKSYM)) |
a3dff304 | 910 | print_sample_brstacksym(sample, thread); |
dc323ce8 | 911 | |
30372f04 WN |
912 | if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT)) |
913 | print_sample_bpf_output(sample); | |
914 | ||
c70c94b4 | 915 | printf("\n"); |
be6d842a DA |
916 | } |
917 | ||
956ffd02 TZ |
918 | static struct scripting_ops *scripting_ops; |
919 | ||
36e33c53 JO |
920 | static void __process_stat(struct perf_evsel *counter, u64 tstamp) |
921 | { | |
922 | int nthreads = thread_map__nr(counter->threads); | |
923 | int ncpus = perf_evsel__nr_cpus(counter); | |
924 | int cpu, thread; | |
925 | static int header_printed; | |
926 | ||
927 | if (counter->system_wide) | |
928 | nthreads = 1; | |
929 | ||
930 | if (!header_printed) { | |
931 | printf("%3s %8s %15s %15s %15s %15s %s\n", | |
932 | "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT"); | |
933 | header_printed = 1; | |
934 | } | |
935 | ||
936 | for (thread = 0; thread < nthreads; thread++) { | |
937 | for (cpu = 0; cpu < ncpus; cpu++) { | |
938 | struct perf_counts_values *counts; | |
939 | ||
940 | counts = perf_counts(counter->counts, cpu, thread); | |
941 | ||
942 | printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n", | |
943 | counter->cpus->map[cpu], | |
944 | thread_map__pid(counter->threads, thread), | |
945 | counts->val, | |
946 | counts->ena, | |
947 | counts->run, | |
948 | tstamp, | |
949 | perf_evsel__name(counter)); | |
950 | } | |
951 | } | |
952 | } | |
953 | ||
e099eba8 JO |
954 | static void process_stat(struct perf_evsel *counter, u64 tstamp) |
955 | { | |
956 | if (scripting_ops && scripting_ops->process_stat) | |
957 | scripting_ops->process_stat(&stat_config, counter, tstamp); | |
36e33c53 JO |
958 | else |
959 | __process_stat(counter, tstamp); | |
e099eba8 JO |
960 | } |
961 | ||
962 | static void process_stat_interval(u64 tstamp) | |
963 | { | |
964 | if (scripting_ops && scripting_ops->process_stat_interval) | |
965 | scripting_ops->process_stat_interval(tstamp); | |
966 | } | |
967 | ||
956ffd02 TZ |
968 | static void setup_scripting(void) |
969 | { | |
16c632de | 970 | setup_perl_scripting(); |
7e4b21b8 | 971 | setup_python_scripting(); |
956ffd02 TZ |
972 | } |
973 | ||
d445dd2a AH |
974 | static int flush_scripting(void) |
975 | { | |
2aaecfc5 | 976 | return scripting_ops ? scripting_ops->flush_script() : 0; |
d445dd2a AH |
977 | } |
978 | ||
956ffd02 TZ |
979 | static int cleanup_scripting(void) |
980 | { | |
133dc4c3 | 981 | pr_debug("\nperf script stopped\n"); |
3824a4e8 | 982 | |
2aaecfc5 | 983 | return scripting_ops ? scripting_ops->stop_script() : 0; |
956ffd02 TZ |
984 | } |
985 | ||
809e9423 | 986 | static int process_sample_event(struct perf_tool *tool, |
d20deb64 | 987 | union perf_event *event, |
8115d60c | 988 | struct perf_sample *sample, |
9e69c210 | 989 | struct perf_evsel *evsel, |
743eb868 | 990 | struct machine *machine) |
5f9c39dc | 991 | { |
809e9423 | 992 | struct perf_script *scr = container_of(tool, struct perf_script, tool); |
e7984b7b | 993 | struct addr_location al; |
5f9c39dc | 994 | |
1424dc96 DA |
995 | if (debug_mode) { |
996 | if (sample->time < last_timestamp) { | |
997 | pr_err("Samples misordered, previous: %" PRIu64 | |
998 | " this: %" PRIu64 "\n", last_timestamp, | |
999 | sample->time); | |
1000 | nr_unordered++; | |
e1889d75 | 1001 | } |
1424dc96 DA |
1002 | last_timestamp = sample->time; |
1003 | return 0; | |
5f9c39dc | 1004 | } |
5d67be97 | 1005 | |
bb3eb566 | 1006 | if (machine__resolve(machine, &al, sample) < 0) { |
e7984b7b DA |
1007 | pr_err("problem processing %d event, skipping it.\n", |
1008 | event->header.type); | |
1009 | return -1; | |
1010 | } | |
1011 | ||
1012 | if (al.filtered) | |
b91fc39f | 1013 | goto out_put; |
e7984b7b | 1014 | |
5d67be97 | 1015 | if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) |
b91fc39f | 1016 | goto out_put; |
5d67be97 | 1017 | |
2aaecfc5 JO |
1018 | if (scripting_ops) |
1019 | scripting_ops->process_event(event, sample, evsel, &al); | |
1020 | else | |
a3dff304 | 1021 | process_event(scr, sample, evsel, &al); |
2aaecfc5 | 1022 | |
b91fc39f ACM |
1023 | out_put: |
1024 | addr_location__put(&al); | |
5f9c39dc FW |
1025 | return 0; |
1026 | } | |
1027 | ||
7ea95727 AH |
1028 | static int process_attr(struct perf_tool *tool, union perf_event *event, |
1029 | struct perf_evlist **pevlist) | |
1030 | { | |
1031 | struct perf_script *scr = container_of(tool, struct perf_script, tool); | |
1032 | struct perf_evlist *evlist; | |
1033 | struct perf_evsel *evsel, *pos; | |
1034 | int err; | |
1035 | ||
1036 | err = perf_event__process_attr(tool, event, pevlist); | |
1037 | if (err) | |
1038 | return err; | |
1039 | ||
1040 | evlist = *pevlist; | |
1041 | evsel = perf_evlist__last(*pevlist); | |
1042 | ||
1043 | if (evsel->attr.type >= PERF_TYPE_MAX) | |
1044 | return 0; | |
1045 | ||
e5cadb93 | 1046 | evlist__for_each_entry(evlist, pos) { |
7ea95727 AH |
1047 | if (pos->attr.type == evsel->attr.type && pos != evsel) |
1048 | return 0; | |
1049 | } | |
1050 | ||
1051 | set_print_ip_opts(&evsel->attr); | |
1052 | ||
d2b5a315 JO |
1053 | if (evsel->attr.sample_type) |
1054 | err = perf_evsel__check_attr(evsel, scr->session); | |
1055 | ||
1056 | return err; | |
7ea95727 AH |
1057 | } |
1058 | ||
ad7ebb9a NK |
1059 | static int process_comm_event(struct perf_tool *tool, |
1060 | union perf_event *event, | |
1061 | struct perf_sample *sample, | |
1062 | struct machine *machine) | |
1063 | { | |
1064 | struct thread *thread; | |
1065 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
1066 | struct perf_session *session = script->session; | |
06b234ec | 1067 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ad7ebb9a NK |
1068 | int ret = -1; |
1069 | ||
1070 | thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid); | |
1071 | if (thread == NULL) { | |
1072 | pr_debug("problem processing COMM event, skipping it.\n"); | |
1073 | return -1; | |
1074 | } | |
1075 | ||
1076 | if (perf_event__process_comm(tool, event, sample, machine) < 0) | |
1077 | goto out; | |
1078 | ||
1079 | if (!evsel->attr.sample_id_all) { | |
1080 | sample->cpu = 0; | |
1081 | sample->time = 0; | |
1082 | sample->tid = event->comm.tid; | |
1083 | sample->pid = event->comm.pid; | |
1084 | } | |
1085 | print_sample_start(sample, thread, evsel); | |
1086 | perf_event__fprintf(event, stdout); | |
1087 | ret = 0; | |
ad7ebb9a | 1088 | out: |
b91fc39f | 1089 | thread__put(thread); |
ad7ebb9a NK |
1090 | return ret; |
1091 | } | |
1092 | ||
1093 | static int process_fork_event(struct perf_tool *tool, | |
1094 | union perf_event *event, | |
1095 | struct perf_sample *sample, | |
1096 | struct machine *machine) | |
1097 | { | |
1098 | struct thread *thread; | |
1099 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
1100 | struct perf_session *session = script->session; | |
06b234ec | 1101 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ad7ebb9a NK |
1102 | |
1103 | if (perf_event__process_fork(tool, event, sample, machine) < 0) | |
1104 | return -1; | |
1105 | ||
1106 | thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); | |
1107 | if (thread == NULL) { | |
1108 | pr_debug("problem processing FORK event, skipping it.\n"); | |
1109 | return -1; | |
1110 | } | |
1111 | ||
1112 | if (!evsel->attr.sample_id_all) { | |
1113 | sample->cpu = 0; | |
1114 | sample->time = event->fork.time; | |
1115 | sample->tid = event->fork.tid; | |
1116 | sample->pid = event->fork.pid; | |
1117 | } | |
1118 | print_sample_start(sample, thread, evsel); | |
1119 | perf_event__fprintf(event, stdout); | |
b91fc39f | 1120 | thread__put(thread); |
ad7ebb9a NK |
1121 | |
1122 | return 0; | |
1123 | } | |
1124 | static int process_exit_event(struct perf_tool *tool, | |
1125 | union perf_event *event, | |
1126 | struct perf_sample *sample, | |
1127 | struct machine *machine) | |
1128 | { | |
b91fc39f | 1129 | int err = 0; |
ad7ebb9a NK |
1130 | struct thread *thread; |
1131 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
1132 | struct perf_session *session = script->session; | |
06b234ec | 1133 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ad7ebb9a NK |
1134 | |
1135 | thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); | |
1136 | if (thread == NULL) { | |
1137 | pr_debug("problem processing EXIT event, skipping it.\n"); | |
1138 | return -1; | |
1139 | } | |
1140 | ||
1141 | if (!evsel->attr.sample_id_all) { | |
1142 | sample->cpu = 0; | |
1143 | sample->time = 0; | |
53ff6bc3 AH |
1144 | sample->tid = event->fork.tid; |
1145 | sample->pid = event->fork.pid; | |
ad7ebb9a NK |
1146 | } |
1147 | print_sample_start(sample, thread, evsel); | |
1148 | perf_event__fprintf(event, stdout); | |
1149 | ||
1150 | if (perf_event__process_exit(tool, event, sample, machine) < 0) | |
b91fc39f | 1151 | err = -1; |
ad7ebb9a | 1152 | |
b91fc39f ACM |
1153 | thread__put(thread); |
1154 | return err; | |
ad7ebb9a NK |
1155 | } |
1156 | ||
ba1ddf42 NK |
1157 | static int process_mmap_event(struct perf_tool *tool, |
1158 | union perf_event *event, | |
1159 | struct perf_sample *sample, | |
1160 | struct machine *machine) | |
1161 | { | |
1162 | struct thread *thread; | |
1163 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
1164 | struct perf_session *session = script->session; | |
06b234ec | 1165 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ba1ddf42 NK |
1166 | |
1167 | if (perf_event__process_mmap(tool, event, sample, machine) < 0) | |
1168 | return -1; | |
1169 | ||
1170 | thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid); | |
1171 | if (thread == NULL) { | |
1172 | pr_debug("problem processing MMAP event, skipping it.\n"); | |
1173 | return -1; | |
1174 | } | |
1175 | ||
1176 | if (!evsel->attr.sample_id_all) { | |
1177 | sample->cpu = 0; | |
1178 | sample->time = 0; | |
1179 | sample->tid = event->mmap.tid; | |
1180 | sample->pid = event->mmap.pid; | |
1181 | } | |
1182 | print_sample_start(sample, thread, evsel); | |
1183 | perf_event__fprintf(event, stdout); | |
b91fc39f | 1184 | thread__put(thread); |
ba1ddf42 NK |
1185 | return 0; |
1186 | } | |
1187 | ||
1188 | static int process_mmap2_event(struct perf_tool *tool, | |
1189 | union perf_event *event, | |
1190 | struct perf_sample *sample, | |
1191 | struct machine *machine) | |
1192 | { | |
1193 | struct thread *thread; | |
1194 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
1195 | struct perf_session *session = script->session; | |
06b234ec | 1196 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ba1ddf42 NK |
1197 | |
1198 | if (perf_event__process_mmap2(tool, event, sample, machine) < 0) | |
1199 | return -1; | |
1200 | ||
1201 | thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid); | |
1202 | if (thread == NULL) { | |
1203 | pr_debug("problem processing MMAP2 event, skipping it.\n"); | |
1204 | return -1; | |
1205 | } | |
1206 | ||
1207 | if (!evsel->attr.sample_id_all) { | |
1208 | sample->cpu = 0; | |
1209 | sample->time = 0; | |
1210 | sample->tid = event->mmap2.tid; | |
1211 | sample->pid = event->mmap2.pid; | |
1212 | } | |
1213 | print_sample_start(sample, thread, evsel); | |
1214 | perf_event__fprintf(event, stdout); | |
b91fc39f | 1215 | thread__put(thread); |
ba1ddf42 NK |
1216 | return 0; |
1217 | } | |
1218 | ||
7c14898b AH |
1219 | static int process_switch_event(struct perf_tool *tool, |
1220 | union perf_event *event, | |
1221 | struct perf_sample *sample, | |
1222 | struct machine *machine) | |
1223 | { | |
1224 | struct thread *thread; | |
1225 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
1226 | struct perf_session *session = script->session; | |
1227 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); | |
1228 | ||
1229 | if (perf_event__process_switch(tool, event, sample, machine) < 0) | |
1230 | return -1; | |
1231 | ||
1232 | thread = machine__findnew_thread(machine, sample->pid, | |
1233 | sample->tid); | |
1234 | if (thread == NULL) { | |
1235 | pr_debug("problem processing SWITCH event, skipping it.\n"); | |
1236 | return -1; | |
1237 | } | |
1238 | ||
1239 | print_sample_start(sample, thread, evsel); | |
1240 | perf_event__fprintf(event, stdout); | |
1241 | thread__put(thread); | |
1242 | return 0; | |
1243 | } | |
1244 | ||
1d037ca1 | 1245 | static void sig_handler(int sig __maybe_unused) |
c239da3b TZ |
1246 | { |
1247 | session_done = 1; | |
1248 | } | |
1249 | ||
6f3e5eda | 1250 | static int __cmd_script(struct perf_script *script) |
5f9c39dc | 1251 | { |
6fcf7ddb FW |
1252 | int ret; |
1253 | ||
c239da3b TZ |
1254 | signal(SIGINT, sig_handler); |
1255 | ||
ad7ebb9a NK |
1256 | /* override event processing functions */ |
1257 | if (script->show_task_events) { | |
1258 | script->tool.comm = process_comm_event; | |
1259 | script->tool.fork = process_fork_event; | |
1260 | script->tool.exit = process_exit_event; | |
1261 | } | |
ba1ddf42 NK |
1262 | if (script->show_mmap_events) { |
1263 | script->tool.mmap = process_mmap_event; | |
1264 | script->tool.mmap2 = process_mmap2_event; | |
1265 | } | |
7c14898b AH |
1266 | if (script->show_switch_events) |
1267 | script->tool.context_switch = process_switch_event; | |
ad7ebb9a | 1268 | |
b7b61cbe | 1269 | ret = perf_session__process_events(script->session); |
6fcf7ddb | 1270 | |
6d8afb56 | 1271 | if (debug_mode) |
9486aa38 | 1272 | pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); |
6fcf7ddb FW |
1273 | |
1274 | return ret; | |
5f9c39dc FW |
1275 | } |
1276 | ||
956ffd02 TZ |
1277 | struct script_spec { |
1278 | struct list_head node; | |
1279 | struct scripting_ops *ops; | |
1280 | char spec[0]; | |
1281 | }; | |
1282 | ||
eccdfe2d | 1283 | static LIST_HEAD(script_specs); |
956ffd02 TZ |
1284 | |
1285 | static struct script_spec *script_spec__new(const char *spec, | |
1286 | struct scripting_ops *ops) | |
1287 | { | |
1288 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); | |
1289 | ||
1290 | if (s != NULL) { | |
1291 | strcpy(s->spec, spec); | |
1292 | s->ops = ops; | |
1293 | } | |
1294 | ||
1295 | return s; | |
1296 | } | |
1297 | ||
956ffd02 TZ |
1298 | static void script_spec__add(struct script_spec *s) |
1299 | { | |
1300 | list_add_tail(&s->node, &script_specs); | |
1301 | } | |
1302 | ||
1303 | static struct script_spec *script_spec__find(const char *spec) | |
1304 | { | |
1305 | struct script_spec *s; | |
1306 | ||
1307 | list_for_each_entry(s, &script_specs, node) | |
1308 | if (strcasecmp(s->spec, spec) == 0) | |
1309 | return s; | |
1310 | return NULL; | |
1311 | } | |
1312 | ||
956ffd02 TZ |
1313 | int script_spec_register(const char *spec, struct scripting_ops *ops) |
1314 | { | |
1315 | struct script_spec *s; | |
1316 | ||
1317 | s = script_spec__find(spec); | |
1318 | if (s) | |
1319 | return -1; | |
1320 | ||
8560bae0 | 1321 | s = script_spec__new(spec, ops); |
956ffd02 TZ |
1322 | if (!s) |
1323 | return -1; | |
8560bae0 TS |
1324 | else |
1325 | script_spec__add(s); | |
956ffd02 TZ |
1326 | |
1327 | return 0; | |
1328 | } | |
1329 | ||
1330 | static struct scripting_ops *script_spec__lookup(const char *spec) | |
1331 | { | |
1332 | struct script_spec *s = script_spec__find(spec); | |
1333 | if (!s) | |
1334 | return NULL; | |
1335 | ||
1336 | return s->ops; | |
1337 | } | |
1338 | ||
1339 | static void list_available_languages(void) | |
1340 | { | |
1341 | struct script_spec *s; | |
1342 | ||
1343 | fprintf(stderr, "\n"); | |
1344 | fprintf(stderr, "Scripting language extensions (used in " | |
133dc4c3 | 1345 | "perf script -s [spec:]script.[spec]):\n\n"); |
956ffd02 TZ |
1346 | |
1347 | list_for_each_entry(s, &script_specs, node) | |
1348 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); | |
1349 | ||
1350 | fprintf(stderr, "\n"); | |
1351 | } | |
1352 | ||
1d037ca1 IT |
1353 | static int parse_scriptname(const struct option *opt __maybe_unused, |
1354 | const char *str, int unset __maybe_unused) | |
956ffd02 TZ |
1355 | { |
1356 | char spec[PATH_MAX]; | |
1357 | const char *script, *ext; | |
1358 | int len; | |
1359 | ||
f526d68b | 1360 | if (strcmp(str, "lang") == 0) { |
956ffd02 | 1361 | list_available_languages(); |
f526d68b | 1362 | exit(0); |
956ffd02 TZ |
1363 | } |
1364 | ||
1365 | script = strchr(str, ':'); | |
1366 | if (script) { | |
1367 | len = script - str; | |
1368 | if (len >= PATH_MAX) { | |
1369 | fprintf(stderr, "invalid language specifier"); | |
1370 | return -1; | |
1371 | } | |
1372 | strncpy(spec, str, len); | |
1373 | spec[len] = '\0'; | |
1374 | scripting_ops = script_spec__lookup(spec); | |
1375 | if (!scripting_ops) { | |
1376 | fprintf(stderr, "invalid language specifier"); | |
1377 | return -1; | |
1378 | } | |
1379 | script++; | |
1380 | } else { | |
1381 | script = str; | |
d1e95bb5 | 1382 | ext = strrchr(script, '.'); |
956ffd02 TZ |
1383 | if (!ext) { |
1384 | fprintf(stderr, "invalid script extension"); | |
1385 | return -1; | |
1386 | } | |
1387 | scripting_ops = script_spec__lookup(++ext); | |
1388 | if (!scripting_ops) { | |
1389 | fprintf(stderr, "invalid script extension"); | |
1390 | return -1; | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | script_name = strdup(script); | |
1395 | ||
1396 | return 0; | |
1397 | } | |
1398 | ||
1d037ca1 IT |
1399 | static int parse_output_fields(const struct option *opt __maybe_unused, |
1400 | const char *arg, int unset __maybe_unused) | |
745f43e3 DA |
1401 | { |
1402 | char *tok; | |
50ca19ae | 1403 | int i, imax = ARRAY_SIZE(all_output_options); |
2c9e45f7 | 1404 | int j; |
745f43e3 DA |
1405 | int rc = 0; |
1406 | char *str = strdup(arg); | |
1424dc96 | 1407 | int type = -1; |
745f43e3 DA |
1408 | |
1409 | if (!str) | |
1410 | return -ENOMEM; | |
1411 | ||
2c9e45f7 DA |
1412 | /* first word can state for which event type the user is specifying |
1413 | * the fields. If no type exists, the specified fields apply to all | |
1414 | * event types found in the file minus the invalid fields for a type. | |
1424dc96 | 1415 | */ |
2c9e45f7 DA |
1416 | tok = strchr(str, ':'); |
1417 | if (tok) { | |
1418 | *tok = '\0'; | |
1419 | tok++; | |
1420 | if (!strcmp(str, "hw")) | |
1421 | type = PERF_TYPE_HARDWARE; | |
1422 | else if (!strcmp(str, "sw")) | |
1423 | type = PERF_TYPE_SOFTWARE; | |
1424 | else if (!strcmp(str, "trace")) | |
1425 | type = PERF_TYPE_TRACEPOINT; | |
0817a6a3 AS |
1426 | else if (!strcmp(str, "raw")) |
1427 | type = PERF_TYPE_RAW; | |
27cfef00 WN |
1428 | else if (!strcmp(str, "break")) |
1429 | type = PERF_TYPE_BREAKPOINT; | |
2c9e45f7 DA |
1430 | else { |
1431 | fprintf(stderr, "Invalid event type in field string.\n"); | |
38efb539 RR |
1432 | rc = -EINVAL; |
1433 | goto out; | |
2c9e45f7 DA |
1434 | } |
1435 | ||
1436 | if (output[type].user_set) | |
1437 | pr_warning("Overriding previous field request for %s events.\n", | |
1438 | event_type(type)); | |
1439 | ||
1440 | output[type].fields = 0; | |
1441 | output[type].user_set = true; | |
9cbdb702 | 1442 | output[type].wildcard_set = false; |
2c9e45f7 DA |
1443 | |
1444 | } else { | |
1445 | tok = str; | |
1446 | if (strlen(str) == 0) { | |
1447 | fprintf(stderr, | |
1448 | "Cannot set fields to 'none' for all event types.\n"); | |
1449 | rc = -EINVAL; | |
1450 | goto out; | |
1451 | } | |
1452 | ||
1453 | if (output_set_by_user()) | |
1454 | pr_warning("Overriding previous field request for all events.\n"); | |
1455 | ||
1456 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
1457 | output[j].fields = 0; | |
1458 | output[j].user_set = true; | |
9cbdb702 | 1459 | output[j].wildcard_set = true; |
2c9e45f7 | 1460 | } |
745f43e3 DA |
1461 | } |
1462 | ||
400ea6d3 | 1463 | for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) { |
745f43e3 | 1464 | for (i = 0; i < imax; ++i) { |
2c9e45f7 | 1465 | if (strcmp(tok, all_output_options[i].str) == 0) |
745f43e3 | 1466 | break; |
745f43e3 | 1467 | } |
400ea6d3 AH |
1468 | if (i == imax && strcmp(tok, "flags") == 0) { |
1469 | print_flags = true; | |
1470 | continue; | |
1471 | } | |
745f43e3 | 1472 | if (i == imax) { |
2c9e45f7 | 1473 | fprintf(stderr, "Invalid field requested.\n"); |
745f43e3 | 1474 | rc = -EINVAL; |
2c9e45f7 | 1475 | goto out; |
745f43e3 DA |
1476 | } |
1477 | ||
2c9e45f7 DA |
1478 | if (type == -1) { |
1479 | /* add user option to all events types for | |
1480 | * which it is valid | |
1481 | */ | |
1482 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
1483 | if (output[j].invalid_fields & all_output_options[i].field) { | |
1484 | pr_warning("\'%s\' not valid for %s events. Ignoring.\n", | |
1485 | all_output_options[i].str, event_type(j)); | |
1486 | } else | |
1487 | output[j].fields |= all_output_options[i].field; | |
1488 | } | |
1489 | } else { | |
1490 | if (output[type].invalid_fields & all_output_options[i].field) { | |
1491 | fprintf(stderr, "\'%s\' not valid for %s events.\n", | |
1492 | all_output_options[i].str, event_type(type)); | |
1493 | ||
1494 | rc = -EINVAL; | |
1495 | goto out; | |
1496 | } | |
1497 | output[type].fields |= all_output_options[i].field; | |
1498 | } | |
745f43e3 DA |
1499 | } |
1500 | ||
2c9e45f7 DA |
1501 | if (type >= 0) { |
1502 | if (output[type].fields == 0) { | |
1503 | pr_debug("No fields requested for %s type. " | |
1504 | "Events will not be displayed.\n", event_type(type)); | |
1505 | } | |
1506 | } | |
745f43e3 | 1507 | |
2c9e45f7 | 1508 | out: |
745f43e3 DA |
1509 | free(str); |
1510 | return rc; | |
1511 | } | |
1512 | ||
008f29d3 SB |
1513 | /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */ |
1514 | static int is_directory(const char *base_path, const struct dirent *dent) | |
1515 | { | |
1516 | char path[PATH_MAX]; | |
1517 | struct stat st; | |
1518 | ||
1519 | sprintf(path, "%s/%s", base_path, dent->d_name); | |
1520 | if (stat(path, &st)) | |
1521 | return 0; | |
1522 | ||
1523 | return S_ISDIR(st.st_mode); | |
1524 | } | |
1525 | ||
a5e8e825 ACM |
1526 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \ |
1527 | while ((lang_dirent = readdir(scripts_dir)) != NULL) \ | |
1528 | if ((lang_dirent->d_type == DT_DIR || \ | |
1529 | (lang_dirent->d_type == DT_UNKNOWN && \ | |
1530 | is_directory(scripts_path, lang_dirent))) && \ | |
1531 | (strcmp(lang_dirent->d_name, ".")) && \ | |
1532 | (strcmp(lang_dirent->d_name, ".."))) | |
1533 | ||
1534 | #define for_each_script(lang_path, lang_dir, script_dirent) \ | |
1535 | while ((script_dirent = readdir(lang_dir)) != NULL) \ | |
1536 | if (script_dirent->d_type != DT_DIR && \ | |
1537 | (script_dirent->d_type != DT_UNKNOWN || \ | |
1538 | !is_directory(lang_path, script_dirent))) | |
4b9c0c59 TZ |
1539 | |
1540 | ||
1541 | #define RECORD_SUFFIX "-record" | |
1542 | #define REPORT_SUFFIX "-report" | |
1543 | ||
1544 | struct script_desc { | |
1545 | struct list_head node; | |
1546 | char *name; | |
1547 | char *half_liner; | |
1548 | char *args; | |
1549 | }; | |
1550 | ||
eccdfe2d | 1551 | static LIST_HEAD(script_descs); |
4b9c0c59 TZ |
1552 | |
1553 | static struct script_desc *script_desc__new(const char *name) | |
1554 | { | |
1555 | struct script_desc *s = zalloc(sizeof(*s)); | |
1556 | ||
b5b87312 | 1557 | if (s != NULL && name) |
4b9c0c59 TZ |
1558 | s->name = strdup(name); |
1559 | ||
1560 | return s; | |
1561 | } | |
1562 | ||
1563 | static void script_desc__delete(struct script_desc *s) | |
1564 | { | |
74cf249d ACM |
1565 | zfree(&s->name); |
1566 | zfree(&s->half_liner); | |
1567 | zfree(&s->args); | |
4b9c0c59 TZ |
1568 | free(s); |
1569 | } | |
1570 | ||
1571 | static void script_desc__add(struct script_desc *s) | |
1572 | { | |
1573 | list_add_tail(&s->node, &script_descs); | |
1574 | } | |
1575 | ||
1576 | static struct script_desc *script_desc__find(const char *name) | |
1577 | { | |
1578 | struct script_desc *s; | |
1579 | ||
1580 | list_for_each_entry(s, &script_descs, node) | |
1581 | if (strcasecmp(s->name, name) == 0) | |
1582 | return s; | |
1583 | return NULL; | |
1584 | } | |
1585 | ||
1586 | static struct script_desc *script_desc__findnew(const char *name) | |
1587 | { | |
1588 | struct script_desc *s = script_desc__find(name); | |
1589 | ||
1590 | if (s) | |
1591 | return s; | |
1592 | ||
1593 | s = script_desc__new(name); | |
1594 | if (!s) | |
1595 | goto out_delete_desc; | |
1596 | ||
1597 | script_desc__add(s); | |
1598 | ||
1599 | return s; | |
1600 | ||
1601 | out_delete_desc: | |
1602 | script_desc__delete(s); | |
1603 | ||
1604 | return NULL; | |
1605 | } | |
1606 | ||
965bb6be | 1607 | static const char *ends_with(const char *str, const char *suffix) |
4b9c0c59 TZ |
1608 | { |
1609 | size_t suffix_len = strlen(suffix); | |
965bb6be | 1610 | const char *p = str; |
4b9c0c59 TZ |
1611 | |
1612 | if (strlen(str) > suffix_len) { | |
1613 | p = str + strlen(str) - suffix_len; | |
1614 | if (!strncmp(p, suffix, suffix_len)) | |
1615 | return p; | |
1616 | } | |
1617 | ||
1618 | return NULL; | |
1619 | } | |
1620 | ||
4b9c0c59 TZ |
1621 | static int read_script_info(struct script_desc *desc, const char *filename) |
1622 | { | |
1623 | char line[BUFSIZ], *p; | |
1624 | FILE *fp; | |
1625 | ||
1626 | fp = fopen(filename, "r"); | |
1627 | if (!fp) | |
1628 | return -1; | |
1629 | ||
1630 | while (fgets(line, sizeof(line), fp)) { | |
1631 | p = ltrim(line); | |
1632 | if (strlen(p) == 0) | |
1633 | continue; | |
1634 | if (*p != '#') | |
1635 | continue; | |
1636 | p++; | |
1637 | if (strlen(p) && *p == '!') | |
1638 | continue; | |
1639 | ||
1640 | p = ltrim(p); | |
1641 | if (strlen(p) && p[strlen(p) - 1] == '\n') | |
1642 | p[strlen(p) - 1] = '\0'; | |
1643 | ||
1644 | if (!strncmp(p, "description:", strlen("description:"))) { | |
1645 | p += strlen("description:"); | |
1646 | desc->half_liner = strdup(ltrim(p)); | |
1647 | continue; | |
1648 | } | |
1649 | ||
1650 | if (!strncmp(p, "args:", strlen("args:"))) { | |
1651 | p += strlen("args:"); | |
1652 | desc->args = strdup(ltrim(p)); | |
1653 | continue; | |
1654 | } | |
1655 | } | |
1656 | ||
1657 | fclose(fp); | |
1658 | ||
1659 | return 0; | |
1660 | } | |
1661 | ||
38efb539 RR |
1662 | static char *get_script_root(struct dirent *script_dirent, const char *suffix) |
1663 | { | |
1664 | char *script_root, *str; | |
1665 | ||
1666 | script_root = strdup(script_dirent->d_name); | |
1667 | if (!script_root) | |
1668 | return NULL; | |
1669 | ||
1670 | str = (char *)ends_with(script_root, suffix); | |
1671 | if (!str) { | |
1672 | free(script_root); | |
1673 | return NULL; | |
1674 | } | |
1675 | ||
1676 | *str = '\0'; | |
1677 | return script_root; | |
1678 | } | |
1679 | ||
1d037ca1 IT |
1680 | static int list_available_scripts(const struct option *opt __maybe_unused, |
1681 | const char *s __maybe_unused, | |
1682 | int unset __maybe_unused) | |
4b9c0c59 | 1683 | { |
a5e8e825 | 1684 | struct dirent *script_dirent, *lang_dirent; |
4b9c0c59 TZ |
1685 | char scripts_path[MAXPATHLEN]; |
1686 | DIR *scripts_dir, *lang_dir; | |
1687 | char script_path[MAXPATHLEN]; | |
1688 | char lang_path[MAXPATHLEN]; | |
1689 | struct script_desc *desc; | |
1690 | char first_half[BUFSIZ]; | |
1691 | char *script_root; | |
4b9c0c59 | 1692 | |
46113a54 | 1693 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); |
4b9c0c59 TZ |
1694 | |
1695 | scripts_dir = opendir(scripts_path); | |
88ded4d8 HK |
1696 | if (!scripts_dir) { |
1697 | fprintf(stdout, | |
1698 | "open(%s) failed.\n" | |
1699 | "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n", | |
1700 | scripts_path); | |
1701 | exit(-1); | |
1702 | } | |
4b9c0c59 | 1703 | |
a5e8e825 | 1704 | for_each_lang(scripts_path, scripts_dir, lang_dirent) { |
4b9c0c59 | 1705 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
a5e8e825 | 1706 | lang_dirent->d_name); |
4b9c0c59 TZ |
1707 | lang_dir = opendir(lang_path); |
1708 | if (!lang_dir) | |
1709 | continue; | |
1710 | ||
a5e8e825 ACM |
1711 | for_each_script(lang_path, lang_dir, script_dirent) { |
1712 | script_root = get_script_root(script_dirent, REPORT_SUFFIX); | |
38efb539 | 1713 | if (script_root) { |
4b9c0c59 TZ |
1714 | desc = script_desc__findnew(script_root); |
1715 | snprintf(script_path, MAXPATHLEN, "%s/%s", | |
a5e8e825 | 1716 | lang_path, script_dirent->d_name); |
4b9c0c59 | 1717 | read_script_info(desc, script_path); |
38efb539 | 1718 | free(script_root); |
4b9c0c59 | 1719 | } |
4b9c0c59 TZ |
1720 | } |
1721 | } | |
1722 | ||
1723 | fprintf(stdout, "List of available trace scripts:\n"); | |
1724 | list_for_each_entry(desc, &script_descs, node) { | |
1725 | sprintf(first_half, "%s %s", desc->name, | |
1726 | desc->args ? desc->args : ""); | |
1727 | fprintf(stdout, " %-36s %s\n", first_half, | |
1728 | desc->half_liner ? desc->half_liner : ""); | |
1729 | } | |
1730 | ||
1731 | exit(0); | |
1732 | } | |
1733 | ||
49e639e2 FT |
1734 | /* |
1735 | * Some scripts specify the required events in their "xxx-record" file, | |
1736 | * this function will check if the events in perf.data match those | |
1737 | * mentioned in the "xxx-record". | |
1738 | * | |
1739 | * Fixme: All existing "xxx-record" are all in good formats "-e event ", | |
1740 | * which is covered well now. And new parsing code should be added to | |
1741 | * cover the future complexing formats like event groups etc. | |
1742 | */ | |
1743 | static int check_ev_match(char *dir_name, char *scriptname, | |
1744 | struct perf_session *session) | |
1745 | { | |
1746 | char filename[MAXPATHLEN], evname[128]; | |
1747 | char line[BUFSIZ], *p; | |
1748 | struct perf_evsel *pos; | |
1749 | int match, len; | |
1750 | FILE *fp; | |
1751 | ||
1752 | sprintf(filename, "%s/bin/%s-record", dir_name, scriptname); | |
1753 | ||
1754 | fp = fopen(filename, "r"); | |
1755 | if (!fp) | |
1756 | return -1; | |
1757 | ||
1758 | while (fgets(line, sizeof(line), fp)) { | |
1759 | p = ltrim(line); | |
1760 | if (*p == '#') | |
1761 | continue; | |
1762 | ||
1763 | while (strlen(p)) { | |
1764 | p = strstr(p, "-e"); | |
1765 | if (!p) | |
1766 | break; | |
1767 | ||
1768 | p += 2; | |
1769 | p = ltrim(p); | |
1770 | len = strcspn(p, " \t"); | |
1771 | if (!len) | |
1772 | break; | |
1773 | ||
1774 | snprintf(evname, len + 1, "%s", p); | |
1775 | ||
1776 | match = 0; | |
e5cadb93 | 1777 | evlist__for_each_entry(session->evlist, pos) { |
49e639e2 FT |
1778 | if (!strcmp(perf_evsel__name(pos), evname)) { |
1779 | match = 1; | |
1780 | break; | |
1781 | } | |
1782 | } | |
1783 | ||
1784 | if (!match) { | |
1785 | fclose(fp); | |
1786 | return -1; | |
1787 | } | |
1788 | } | |
1789 | } | |
1790 | ||
1791 | fclose(fp); | |
1792 | return 0; | |
1793 | } | |
1794 | ||
e5f3705e FT |
1795 | /* |
1796 | * Return -1 if none is found, otherwise the actual scripts number. | |
1797 | * | |
1798 | * Currently the only user of this function is the script browser, which | |
1799 | * will list all statically runnable scripts, select one, execute it and | |
1800 | * show the output in a perf browser. | |
1801 | */ | |
1802 | int find_scripts(char **scripts_array, char **scripts_path_array) | |
1803 | { | |
a5e8e825 | 1804 | struct dirent *script_dirent, *lang_dirent; |
49e639e2 | 1805 | char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN]; |
e5f3705e | 1806 | DIR *scripts_dir, *lang_dir; |
49e639e2 | 1807 | struct perf_session *session; |
f5fc1412 JO |
1808 | struct perf_data_file file = { |
1809 | .path = input_name, | |
1810 | .mode = PERF_DATA_MODE_READ, | |
1811 | }; | |
e5f3705e FT |
1812 | char *temp; |
1813 | int i = 0; | |
1814 | ||
f5fc1412 | 1815 | session = perf_session__new(&file, false, NULL); |
49e639e2 FT |
1816 | if (!session) |
1817 | return -1; | |
1818 | ||
46113a54 | 1819 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); |
e5f3705e FT |
1820 | |
1821 | scripts_dir = opendir(scripts_path); | |
49e639e2 FT |
1822 | if (!scripts_dir) { |
1823 | perf_session__delete(session); | |
e5f3705e | 1824 | return -1; |
49e639e2 | 1825 | } |
e5f3705e | 1826 | |
a5e8e825 | 1827 | for_each_lang(scripts_path, scripts_dir, lang_dirent) { |
e5f3705e | 1828 | snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, |
a5e8e825 | 1829 | lang_dirent->d_name); |
e5f3705e FT |
1830 | #ifdef NO_LIBPERL |
1831 | if (strstr(lang_path, "perl")) | |
1832 | continue; | |
1833 | #endif | |
1834 | #ifdef NO_LIBPYTHON | |
1835 | if (strstr(lang_path, "python")) | |
1836 | continue; | |
1837 | #endif | |
1838 | ||
1839 | lang_dir = opendir(lang_path); | |
1840 | if (!lang_dir) | |
1841 | continue; | |
1842 | ||
a5e8e825 | 1843 | for_each_script(lang_path, lang_dir, script_dirent) { |
e5f3705e | 1844 | /* Skip those real time scripts: xxxtop.p[yl] */ |
a5e8e825 | 1845 | if (strstr(script_dirent->d_name, "top.")) |
e5f3705e FT |
1846 | continue; |
1847 | sprintf(scripts_path_array[i], "%s/%s", lang_path, | |
a5e8e825 ACM |
1848 | script_dirent->d_name); |
1849 | temp = strchr(script_dirent->d_name, '.'); | |
e5f3705e | 1850 | snprintf(scripts_array[i], |
a5e8e825 ACM |
1851 | (temp - script_dirent->d_name) + 1, |
1852 | "%s", script_dirent->d_name); | |
49e639e2 FT |
1853 | |
1854 | if (check_ev_match(lang_path, | |
1855 | scripts_array[i], session)) | |
1856 | continue; | |
1857 | ||
e5f3705e FT |
1858 | i++; |
1859 | } | |
49e639e2 | 1860 | closedir(lang_dir); |
e5f3705e FT |
1861 | } |
1862 | ||
49e639e2 FT |
1863 | closedir(scripts_dir); |
1864 | perf_session__delete(session); | |
e5f3705e FT |
1865 | return i; |
1866 | } | |
1867 | ||
3875294f TZ |
1868 | static char *get_script_path(const char *script_root, const char *suffix) |
1869 | { | |
a5e8e825 | 1870 | struct dirent *script_dirent, *lang_dirent; |
3875294f TZ |
1871 | char scripts_path[MAXPATHLEN]; |
1872 | char script_path[MAXPATHLEN]; | |
1873 | DIR *scripts_dir, *lang_dir; | |
1874 | char lang_path[MAXPATHLEN]; | |
38efb539 | 1875 | char *__script_root; |
3875294f | 1876 | |
46113a54 | 1877 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); |
3875294f TZ |
1878 | |
1879 | scripts_dir = opendir(scripts_path); | |
1880 | if (!scripts_dir) | |
1881 | return NULL; | |
1882 | ||
a5e8e825 | 1883 | for_each_lang(scripts_path, scripts_dir, lang_dirent) { |
3875294f | 1884 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
a5e8e825 | 1885 | lang_dirent->d_name); |
3875294f TZ |
1886 | lang_dir = opendir(lang_path); |
1887 | if (!lang_dir) | |
1888 | continue; | |
1889 | ||
a5e8e825 ACM |
1890 | for_each_script(lang_path, lang_dir, script_dirent) { |
1891 | __script_root = get_script_root(script_dirent, suffix); | |
38efb539 RR |
1892 | if (__script_root && !strcmp(script_root, __script_root)) { |
1893 | free(__script_root); | |
946ef2a2 NK |
1894 | closedir(lang_dir); |
1895 | closedir(scripts_dir); | |
3875294f | 1896 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
a5e8e825 | 1897 | lang_path, script_dirent->d_name); |
38efb539 | 1898 | return strdup(script_path); |
3875294f TZ |
1899 | } |
1900 | free(__script_root); | |
1901 | } | |
946ef2a2 | 1902 | closedir(lang_dir); |
3875294f | 1903 | } |
946ef2a2 | 1904 | closedir(scripts_dir); |
3875294f | 1905 | |
38efb539 | 1906 | return NULL; |
3875294f TZ |
1907 | } |
1908 | ||
b5b87312 TZ |
1909 | static bool is_top_script(const char *script_path) |
1910 | { | |
965bb6be | 1911 | return ends_with(script_path, "top") == NULL ? false : true; |
b5b87312 TZ |
1912 | } |
1913 | ||
1914 | static int has_required_arg(char *script_path) | |
1915 | { | |
1916 | struct script_desc *desc; | |
1917 | int n_args = 0; | |
1918 | char *p; | |
1919 | ||
1920 | desc = script_desc__new(NULL); | |
1921 | ||
1922 | if (read_script_info(desc, script_path)) | |
1923 | goto out; | |
1924 | ||
1925 | if (!desc->args) | |
1926 | goto out; | |
1927 | ||
1928 | for (p = desc->args; *p; p++) | |
1929 | if (*p == '<') | |
1930 | n_args++; | |
1931 | out: | |
1932 | script_desc__delete(desc); | |
1933 | ||
1934 | return n_args; | |
1935 | } | |
1936 | ||
69b6470e ACM |
1937 | static int have_cmd(int argc, const char **argv) |
1938 | { | |
1939 | char **__argv = malloc(sizeof(const char *) * argc); | |
1940 | ||
1941 | if (!__argv) { | |
1942 | pr_err("malloc failed\n"); | |
1943 | return -1; | |
1944 | } | |
1945 | ||
1946 | memcpy(__argv, argv, sizeof(const char *) * argc); | |
1947 | argc = parse_options(argc, (const char **)__argv, record_options, | |
1948 | NULL, PARSE_OPT_STOP_AT_NON_OPTION); | |
1949 | free(__argv); | |
5f9c39dc | 1950 | |
69b6470e ACM |
1951 | system_wide = (argc == 0); |
1952 | ||
1953 | return 0; | |
1954 | } | |
1955 | ||
7322d6c9 JO |
1956 | static void script__setup_sample_type(struct perf_script *script) |
1957 | { | |
1958 | struct perf_session *session = script->session; | |
1959 | u64 sample_type = perf_evlist__combined_sample_type(session->evlist); | |
1960 | ||
1961 | if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) { | |
1962 | if ((sample_type & PERF_SAMPLE_REGS_USER) && | |
1963 | (sample_type & PERF_SAMPLE_STACK_USER)) | |
1964 | callchain_param.record_mode = CALLCHAIN_DWARF; | |
1965 | else if (sample_type & PERF_SAMPLE_BRANCH_STACK) | |
1966 | callchain_param.record_mode = CALLCHAIN_LBR; | |
1967 | else | |
1968 | callchain_param.record_mode = CALLCHAIN_FP; | |
1969 | } | |
1970 | } | |
1971 | ||
e099eba8 JO |
1972 | static int process_stat_round_event(struct perf_tool *tool __maybe_unused, |
1973 | union perf_event *event, | |
1974 | struct perf_session *session) | |
1975 | { | |
1976 | struct stat_round_event *round = &event->stat_round; | |
1977 | struct perf_evsel *counter; | |
1978 | ||
e5cadb93 | 1979 | evlist__for_each_entry(session->evlist, counter) { |
e099eba8 JO |
1980 | perf_stat_process_counter(&stat_config, counter); |
1981 | process_stat(counter, round->time); | |
1982 | } | |
1983 | ||
1984 | process_stat_interval(round->time); | |
1985 | return 0; | |
1986 | } | |
1987 | ||
91a2c3d5 JO |
1988 | static int process_stat_config_event(struct perf_tool *tool __maybe_unused, |
1989 | union perf_event *event, | |
1990 | struct perf_session *session __maybe_unused) | |
1991 | { | |
1992 | perf_event__read_stat_config(&stat_config, &event->stat_config); | |
1993 | return 0; | |
1994 | } | |
1995 | ||
cfc8874a JO |
1996 | static int set_maps(struct perf_script *script) |
1997 | { | |
1998 | struct perf_evlist *evlist = script->session->evlist; | |
1999 | ||
2000 | if (!script->cpus || !script->threads) | |
2001 | return 0; | |
2002 | ||
2003 | if (WARN_ONCE(script->allocated, "stats double allocation\n")) | |
2004 | return -EINVAL; | |
2005 | ||
2006 | perf_evlist__set_maps(evlist, script->cpus, script->threads); | |
2007 | ||
2008 | if (perf_evlist__alloc_stats(evlist, true)) | |
2009 | return -ENOMEM; | |
2010 | ||
2011 | script->allocated = true; | |
2012 | return 0; | |
2013 | } | |
2014 | ||
2015 | static | |
2016 | int process_thread_map_event(struct perf_tool *tool, | |
2017 | union perf_event *event, | |
2018 | struct perf_session *session __maybe_unused) | |
2019 | { | |
2020 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
2021 | ||
2022 | if (script->threads) { | |
2023 | pr_warning("Extra thread map event, ignoring.\n"); | |
2024 | return 0; | |
2025 | } | |
2026 | ||
2027 | script->threads = thread_map__new_event(&event->thread_map); | |
2028 | if (!script->threads) | |
2029 | return -ENOMEM; | |
2030 | ||
2031 | return set_maps(script); | |
2032 | } | |
2033 | ||
2034 | static | |
2035 | int process_cpu_map_event(struct perf_tool *tool __maybe_unused, | |
2036 | union perf_event *event, | |
2037 | struct perf_session *session __maybe_unused) | |
2038 | { | |
2039 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
2040 | ||
2041 | if (script->cpus) { | |
2042 | pr_warning("Extra cpu map event, ignoring.\n"); | |
2043 | return 0; | |
2044 | } | |
2045 | ||
2046 | script->cpus = cpu_map__new_data(&event->cpu_map.data); | |
2047 | if (!script->cpus) | |
2048 | return -ENOMEM; | |
2049 | ||
2050 | return set_maps(script); | |
2051 | } | |
2052 | ||
69b6470e ACM |
2053 | int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused) |
2054 | { | |
2055 | bool show_full_info = false; | |
e90debdd JO |
2056 | bool header = false; |
2057 | bool header_only = false; | |
6cc870f0 | 2058 | bool script_started = false; |
69b6470e ACM |
2059 | char *rec_script_path = NULL; |
2060 | char *rep_script_path = NULL; | |
2061 | struct perf_session *session; | |
7a680eb9 | 2062 | struct itrace_synth_opts itrace_synth_opts = { .set = false, }; |
69b6470e ACM |
2063 | char *script_path = NULL; |
2064 | const char **__argv; | |
6cc870f0 | 2065 | int i, j, err = 0; |
6f3e5eda AH |
2066 | struct perf_script script = { |
2067 | .tool = { | |
2068 | .sample = process_sample_event, | |
2069 | .mmap = perf_event__process_mmap, | |
2070 | .mmap2 = perf_event__process_mmap2, | |
2071 | .comm = perf_event__process_comm, | |
2072 | .exit = perf_event__process_exit, | |
2073 | .fork = perf_event__process_fork, | |
7ea95727 | 2074 | .attr = process_attr, |
91daee30 | 2075 | .event_update = perf_event__process_event_update, |
6f3e5eda AH |
2076 | .tracing_data = perf_event__process_tracing_data, |
2077 | .build_id = perf_event__process_build_id, | |
7a680eb9 AH |
2078 | .id_index = perf_event__process_id_index, |
2079 | .auxtrace_info = perf_event__process_auxtrace_info, | |
2080 | .auxtrace = perf_event__process_auxtrace, | |
2081 | .auxtrace_error = perf_event__process_auxtrace_error, | |
e099eba8 JO |
2082 | .stat = perf_event__process_stat_event, |
2083 | .stat_round = process_stat_round_event, | |
91a2c3d5 | 2084 | .stat_config = process_stat_config_event, |
cfc8874a JO |
2085 | .thread_map = process_thread_map_event, |
2086 | .cpu_map = process_cpu_map_event, | |
0a8cb85c | 2087 | .ordered_events = true, |
6f3e5eda AH |
2088 | .ordering_requires_timestamps = true, |
2089 | }, | |
2090 | }; | |
06af0f2c YS |
2091 | struct perf_data_file file = { |
2092 | .mode = PERF_DATA_MODE_READ, | |
2093 | }; | |
69b6470e | 2094 | const struct option options[] = { |
5f9c39dc FW |
2095 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
2096 | "dump raw trace in ASCII"), | |
c0555642 | 2097 | OPT_INCR('v', "verbose", &verbose, |
69b6470e | 2098 | "be more verbose (show symbol address, etc)"), |
4b9c0c59 | 2099 | OPT_BOOLEAN('L', "Latency", &latency_format, |
cda48461 | 2100 | "show latency attributes (irqs/preemption disabled, etc)"), |
4b9c0c59 TZ |
2101 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", |
2102 | list_available_scripts), | |
956ffd02 TZ |
2103 | OPT_CALLBACK('s', "script", NULL, "name", |
2104 | "script file name (lang:script name, script name, or *)", | |
2105 | parse_scriptname), | |
2106 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", | |
133dc4c3 | 2107 | "generate perf-script.xx script in specified language"), |
69b6470e | 2108 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
ffabd99e FW |
2109 | OPT_BOOLEAN('d', "debug-mode", &debug_mode, |
2110 | "do various checks like samples ordering and lost events"), | |
e90debdd JO |
2111 | OPT_BOOLEAN(0, "header", &header, "Show data header."), |
2112 | OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."), | |
c0230b2b DA |
2113 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
2114 | "file", "vmlinux pathname"), | |
2115 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, | |
2116 | "file", "kallsyms pathname"), | |
2117 | OPT_BOOLEAN('G', "hide-call-graph", &no_callchain, | |
2118 | "When printing symbols do not display call chain"), | |
a7066709 HK |
2119 | OPT_CALLBACK(0, "symfs", NULL, "directory", |
2120 | "Look for files with symbols relative to this directory", | |
2121 | symbol__config_symfs), | |
06af0f2c | 2122 | OPT_CALLBACK('F', "fields", NULL, "str", |
a978f2ab AN |
2123 | "comma separated output fields prepend with 'type:'. " |
2124 | "Valid types: hw,sw,trace,raw. " | |
2125 | "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso," | |
e216708d | 2126 | "addr,symoff,period,iregs,brstack,brstacksym,flags," |
bcdc09af | 2127 | "bpf-output,callindent", parse_output_fields), |
317df650 | 2128 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
69b6470e | 2129 | "system-wide collection from all CPUs"), |
36385be5 FT |
2130 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", |
2131 | "only consider these symbols"), | |
c8e66720 | 2132 | OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"), |
e7984b7b DA |
2133 | OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", |
2134 | "only display events for these comms"), | |
e03eaa40 DA |
2135 | OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]", |
2136 | "only consider symbols in these pids"), | |
2137 | OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]", | |
2138 | "only consider symbols in these tids"), | |
6125cc8d ACM |
2139 | OPT_UINTEGER(0, "max-stack", &scripting_max_stack, |
2140 | "Set the maximum stack depth when parsing the callchain, " | |
2141 | "anything beyond the specified depth will be ignored. " | |
4cb93446 | 2142 | "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), |
fbe96f29 SE |
2143 | OPT_BOOLEAN('I', "show-info", &show_full_info, |
2144 | "display extended information from perf.data file"), | |
0bc8d205 AN |
2145 | OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path, |
2146 | "Show the path of [kernel.kallsyms]"), | |
ad7ebb9a NK |
2147 | OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events, |
2148 | "Show the fork/comm/exit events"), | |
ba1ddf42 NK |
2149 | OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events, |
2150 | "Show the mmap events"), | |
7c14898b AH |
2151 | OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events, |
2152 | "Show context switch events (if recorded)"), | |
06af0f2c | 2153 | OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"), |
83e19860 AH |
2154 | OPT_BOOLEAN(0, "ns", &nanosecs, |
2155 | "Use 9 decimal places when displaying time"), | |
7a680eb9 AH |
2156 | OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", |
2157 | "Instruction Tracing options", | |
2158 | itrace_parse_synth_opts), | |
a9710ba0 AK |
2159 | OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename, |
2160 | "Show full source file name path for source lines"), | |
77e0070d MD |
2161 | OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, |
2162 | "Enable symbol demangling"), | |
2163 | OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, | |
2164 | "Enable kernel symbol demangling"), | |
2165 | ||
1909629f | 2166 | OPT_END() |
69b6470e | 2167 | }; |
40cae2b7 YS |
2168 | const char * const script_subcommands[] = { "record", "report", NULL }; |
2169 | const char *script_usage[] = { | |
69b6470e ACM |
2170 | "perf script [<options>]", |
2171 | "perf script [<options>] record <script> [<record-options>] <command>", | |
2172 | "perf script [<options>] report <script> [script-args]", | |
2173 | "perf script [<options>] <script> [<record-options>] <command>", | |
2174 | "perf script [<options>] <top-script> [script-args]", | |
2175 | NULL | |
2176 | }; | |
3875294f | 2177 | |
b5b87312 TZ |
2178 | setup_scripting(); |
2179 | ||
40cae2b7 | 2180 | argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage, |
b5b87312 TZ |
2181 | PARSE_OPT_STOP_AT_NON_OPTION); |
2182 | ||
f5fc1412 JO |
2183 | file.path = input_name; |
2184 | ||
b5b87312 TZ |
2185 | if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { |
2186 | rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); | |
2187 | if (!rec_script_path) | |
2188 | return cmd_record(argc, argv, NULL); | |
3875294f TZ |
2189 | } |
2190 | ||
b5b87312 TZ |
2191 | if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { |
2192 | rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); | |
2193 | if (!rep_script_path) { | |
3875294f | 2194 | fprintf(stderr, |
b5b87312 | 2195 | "Please specify a valid report script" |
133dc4c3 | 2196 | "(see 'perf script -l' for listing)\n"); |
3875294f TZ |
2197 | return -1; |
2198 | } | |
3875294f TZ |
2199 | } |
2200 | ||
3c5b645f AH |
2201 | if (itrace_synth_opts.callchain && |
2202 | itrace_synth_opts.callchain_sz > scripting_max_stack) | |
2203 | scripting_max_stack = itrace_synth_opts.callchain_sz; | |
2204 | ||
44e668c6 | 2205 | /* make sure PERF_EXEC_PATH is set for scripts */ |
46113a54 | 2206 | set_argv_exec_path(get_argv_exec_path()); |
44e668c6 | 2207 | |
b5b87312 | 2208 | if (argc && !script_name && !rec_script_path && !rep_script_path) { |
a0cccc2e | 2209 | int live_pipe[2]; |
b5b87312 | 2210 | int rep_args; |
a0cccc2e TZ |
2211 | pid_t pid; |
2212 | ||
b5b87312 TZ |
2213 | rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); |
2214 | rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); | |
2215 | ||
2216 | if (!rec_script_path && !rep_script_path) { | |
c7118369 NK |
2217 | usage_with_options_msg(script_usage, options, |
2218 | "Couldn't find script `%s'\n\n See perf" | |
133dc4c3 | 2219 | " script -l for available scripts.\n", argv[0]); |
a0cccc2e TZ |
2220 | } |
2221 | ||
b5b87312 TZ |
2222 | if (is_top_script(argv[0])) { |
2223 | rep_args = argc - 1; | |
2224 | } else { | |
2225 | int rec_args; | |
2226 | ||
2227 | rep_args = has_required_arg(rep_script_path); | |
2228 | rec_args = (argc - 1) - rep_args; | |
2229 | if (rec_args < 0) { | |
c7118369 NK |
2230 | usage_with_options_msg(script_usage, options, |
2231 | "`%s' script requires options." | |
133dc4c3 | 2232 | "\n\n See perf script -l for available " |
b5b87312 | 2233 | "scripts and options.\n", argv[0]); |
b5b87312 | 2234 | } |
a0cccc2e TZ |
2235 | } |
2236 | ||
2237 | if (pipe(live_pipe) < 0) { | |
2238 | perror("failed to create pipe"); | |
d54b1a9e | 2239 | return -1; |
a0cccc2e TZ |
2240 | } |
2241 | ||
2242 | pid = fork(); | |
2243 | if (pid < 0) { | |
2244 | perror("failed to fork"); | |
d54b1a9e | 2245 | return -1; |
a0cccc2e TZ |
2246 | } |
2247 | ||
2248 | if (!pid) { | |
b5b87312 TZ |
2249 | j = 0; |
2250 | ||
a0cccc2e TZ |
2251 | dup2(live_pipe[1], 1); |
2252 | close(live_pipe[0]); | |
2253 | ||
317df650 RR |
2254 | if (is_top_script(argv[0])) { |
2255 | system_wide = true; | |
2256 | } else if (!system_wide) { | |
d54b1a9e DA |
2257 | if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) { |
2258 | err = -1; | |
2259 | goto out; | |
2260 | } | |
317df650 | 2261 | } |
b5b87312 TZ |
2262 | |
2263 | __argv = malloc((argc + 6) * sizeof(const char *)); | |
d54b1a9e DA |
2264 | if (!__argv) { |
2265 | pr_err("malloc failed\n"); | |
2266 | err = -ENOMEM; | |
2267 | goto out; | |
2268 | } | |
e8719adf | 2269 | |
b5b87312 TZ |
2270 | __argv[j++] = "/bin/sh"; |
2271 | __argv[j++] = rec_script_path; | |
2272 | if (system_wide) | |
2273 | __argv[j++] = "-a"; | |
2274 | __argv[j++] = "-q"; | |
2275 | __argv[j++] = "-o"; | |
2276 | __argv[j++] = "-"; | |
2277 | for (i = rep_args + 1; i < argc; i++) | |
2278 | __argv[j++] = argv[i]; | |
2279 | __argv[j++] = NULL; | |
a0cccc2e TZ |
2280 | |
2281 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 2282 | free(__argv); |
a0cccc2e TZ |
2283 | exit(-1); |
2284 | } | |
2285 | ||
2286 | dup2(live_pipe[0], 0); | |
2287 | close(live_pipe[1]); | |
2288 | ||
b5b87312 | 2289 | __argv = malloc((argc + 4) * sizeof(const char *)); |
d54b1a9e DA |
2290 | if (!__argv) { |
2291 | pr_err("malloc failed\n"); | |
2292 | err = -ENOMEM; | |
2293 | goto out; | |
2294 | } | |
2295 | ||
b5b87312 TZ |
2296 | j = 0; |
2297 | __argv[j++] = "/bin/sh"; | |
2298 | __argv[j++] = rep_script_path; | |
2299 | for (i = 1; i < rep_args + 1; i++) | |
2300 | __argv[j++] = argv[i]; | |
2301 | __argv[j++] = "-i"; | |
2302 | __argv[j++] = "-"; | |
2303 | __argv[j++] = NULL; | |
a0cccc2e TZ |
2304 | |
2305 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 2306 | free(__argv); |
a0cccc2e TZ |
2307 | exit(-1); |
2308 | } | |
2309 | ||
b5b87312 TZ |
2310 | if (rec_script_path) |
2311 | script_path = rec_script_path; | |
2312 | if (rep_script_path) | |
2313 | script_path = rep_script_path; | |
34c86ea9 | 2314 | |
b5b87312 | 2315 | if (script_path) { |
b5b87312 | 2316 | j = 0; |
3875294f | 2317 | |
317df650 RR |
2318 | if (!rec_script_path) |
2319 | system_wide = false; | |
d54b1a9e DA |
2320 | else if (!system_wide) { |
2321 | if (have_cmd(argc - 1, &argv[1]) != 0) { | |
2322 | err = -1; | |
2323 | goto out; | |
2324 | } | |
2325 | } | |
34c86ea9 | 2326 | |
b5b87312 | 2327 | __argv = malloc((argc + 2) * sizeof(const char *)); |
d54b1a9e DA |
2328 | if (!__argv) { |
2329 | pr_err("malloc failed\n"); | |
2330 | err = -ENOMEM; | |
2331 | goto out; | |
2332 | } | |
2333 | ||
34c86ea9 TZ |
2334 | __argv[j++] = "/bin/sh"; |
2335 | __argv[j++] = script_path; | |
2336 | if (system_wide) | |
2337 | __argv[j++] = "-a"; | |
b5b87312 | 2338 | for (i = 2; i < argc; i++) |
34c86ea9 TZ |
2339 | __argv[j++] = argv[i]; |
2340 | __argv[j++] = NULL; | |
3875294f TZ |
2341 | |
2342 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 2343 | free(__argv); |
3875294f TZ |
2344 | exit(-1); |
2345 | } | |
956ffd02 | 2346 | |
cf4fee50 TZ |
2347 | if (!script_name) |
2348 | setup_pager(); | |
5f9c39dc | 2349 | |
6f3e5eda | 2350 | session = perf_session__new(&file, false, &script.tool); |
d8f66248 | 2351 | if (session == NULL) |
52e02834 | 2352 | return -1; |
d8f66248 | 2353 | |
e90debdd JO |
2354 | if (header || header_only) { |
2355 | perf_session__fprintf_info(session, stdout, show_full_info); | |
2356 | if (header_only) | |
6cc870f0 | 2357 | goto out_delete; |
e90debdd JO |
2358 | } |
2359 | ||
0a7e6d1b | 2360 | if (symbol__init(&session->header.env) < 0) |
38520dc3 NK |
2361 | goto out_delete; |
2362 | ||
6f3e5eda | 2363 | script.session = session; |
7322d6c9 | 2364 | script__setup_sample_type(&script); |
6f3e5eda | 2365 | |
e216708d AH |
2366 | if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) |
2367 | itrace_synth_opts.thread_stack = true; | |
2368 | ||
7a680eb9 AH |
2369 | session->itrace_synth_opts = &itrace_synth_opts; |
2370 | ||
5d67be97 | 2371 | if (cpu_list) { |
6cc870f0 NK |
2372 | err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap); |
2373 | if (err < 0) | |
2374 | goto out_delete; | |
5d67be97 AB |
2375 | } |
2376 | ||
1424dc96 | 2377 | if (!no_callchain) |
c0230b2b DA |
2378 | symbol_conf.use_callchain = true; |
2379 | else | |
2380 | symbol_conf.use_callchain = false; | |
2381 | ||
9ee67421 ACM |
2382 | if (session->tevent.pevent && |
2383 | pevent_set_function_resolver(session->tevent.pevent, | |
ccb3a829 ACM |
2384 | machine__resolve_kernel_addr, |
2385 | &session->machines.host) < 0) { | |
2386 | pr_err("%s: failed to set libtraceevent function resolver\n", __func__); | |
2387 | return -1; | |
2388 | } | |
2389 | ||
956ffd02 TZ |
2390 | if (generate_script_lang) { |
2391 | struct stat perf_stat; | |
745f43e3 DA |
2392 | int input; |
2393 | ||
2c9e45f7 | 2394 | if (output_set_by_user()) { |
745f43e3 DA |
2395 | fprintf(stderr, |
2396 | "custom fields not supported for generated scripts"); | |
6cc870f0 NK |
2397 | err = -EINVAL; |
2398 | goto out_delete; | |
745f43e3 | 2399 | } |
956ffd02 | 2400 | |
cc9784bd | 2401 | input = open(file.path, O_RDONLY); /* input_name */ |
956ffd02 | 2402 | if (input < 0) { |
6cc870f0 | 2403 | err = -errno; |
956ffd02 | 2404 | perror("failed to open file"); |
6cc870f0 | 2405 | goto out_delete; |
956ffd02 TZ |
2406 | } |
2407 | ||
2408 | err = fstat(input, &perf_stat); | |
2409 | if (err < 0) { | |
2410 | perror("failed to stat file"); | |
6cc870f0 | 2411 | goto out_delete; |
956ffd02 TZ |
2412 | } |
2413 | ||
2414 | if (!perf_stat.st_size) { | |
2415 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
6cc870f0 | 2416 | goto out_delete; |
956ffd02 TZ |
2417 | } |
2418 | ||
2419 | scripting_ops = script_spec__lookup(generate_script_lang); | |
2420 | if (!scripting_ops) { | |
2421 | fprintf(stderr, "invalid language specifier"); | |
6cc870f0 NK |
2422 | err = -ENOENT; |
2423 | goto out_delete; | |
956ffd02 TZ |
2424 | } |
2425 | ||
29f5ffd3 | 2426 | err = scripting_ops->generate_script(session->tevent.pevent, |
da378962 | 2427 | "perf-script"); |
6cc870f0 | 2428 | goto out_delete; |
956ffd02 TZ |
2429 | } |
2430 | ||
2431 | if (script_name) { | |
586bc5cc | 2432 | err = scripting_ops->start_script(script_name, argc, argv); |
956ffd02 | 2433 | if (err) |
6cc870f0 | 2434 | goto out_delete; |
133dc4c3 | 2435 | pr_debug("perf script started with script %s\n\n", script_name); |
6cc870f0 | 2436 | script_started = true; |
956ffd02 TZ |
2437 | } |
2438 | ||
9cbdb702 DA |
2439 | |
2440 | err = perf_session__check_output_opt(session); | |
2441 | if (err < 0) | |
6cc870f0 | 2442 | goto out_delete; |
9cbdb702 | 2443 | |
6f3e5eda | 2444 | err = __cmd_script(&script); |
956ffd02 | 2445 | |
d445dd2a AH |
2446 | flush_scripting(); |
2447 | ||
6cc870f0 | 2448 | out_delete: |
cfc8874a | 2449 | perf_evlist__free_stats(session->evlist); |
d8f66248 | 2450 | perf_session__delete(session); |
6cc870f0 NK |
2451 | |
2452 | if (script_started) | |
2453 | cleanup_scripting(); | |
956ffd02 TZ |
2454 | out: |
2455 | return err; | |
5f9c39dc | 2456 | } |