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