]>
Commit | Line | Data |
---|---|---|
5f9c39dc FW |
1 | #include "builtin.h" |
2 | ||
b7eead86 | 3 | #include "perf.h" |
5f9c39dc | 4 | #include "util/cache.h" |
b7eead86 AG |
5 | #include "util/debug.h" |
6 | #include "util/exec_cmd.h" | |
7 | #include "util/header.h" | |
8 | #include "util/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" |
5d67be97 | 21 | #include <linux/bitmap.h> |
5f9c39dc | 22 | |
956ffd02 TZ |
23 | static char const *script_name; |
24 | static char const *generate_script_lang; | |
ffabd99e | 25 | static bool debug_mode; |
e1889d75 | 26 | static u64 last_timestamp; |
6fcf7ddb | 27 | static u64 nr_unordered; |
c0230b2b | 28 | static bool no_callchain; |
47390ae2 | 29 | static bool latency_format; |
317df650 | 30 | static bool system_wide; |
400ea6d3 | 31 | static bool print_flags; |
83e19860 | 32 | static bool nanosecs; |
5d67be97 AB |
33 | static const char *cpu_list; |
34 | static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); | |
956ffd02 | 35 | |
745f43e3 DA |
36 | enum perf_output_field { |
37 | PERF_OUTPUT_COMM = 1U << 0, | |
38 | PERF_OUTPUT_TID = 1U << 1, | |
39 | PERF_OUTPUT_PID = 1U << 2, | |
40 | PERF_OUTPUT_TIME = 1U << 3, | |
41 | PERF_OUTPUT_CPU = 1U << 4, | |
42 | PERF_OUTPUT_EVNAME = 1U << 5, | |
43 | PERF_OUTPUT_TRACE = 1U << 6, | |
787bef17 DA |
44 | PERF_OUTPUT_IP = 1U << 7, |
45 | PERF_OUTPUT_SYM = 1U << 8, | |
610723f2 | 46 | PERF_OUTPUT_DSO = 1U << 9, |
7cec0922 | 47 | PERF_OUTPUT_ADDR = 1U << 10, |
a978f2ab | 48 | PERF_OUTPUT_SYMOFFSET = 1U << 11, |
cc8fae1d | 49 | PERF_OUTPUT_SRCLINE = 1U << 12, |
535aeaae | 50 | PERF_OUTPUT_PERIOD = 1U << 13, |
fc36f948 | 51 | PERF_OUTPUT_IREGS = 1U << 14, |
745f43e3 DA |
52 | }; |
53 | ||
54 | struct output_option { | |
55 | const char *str; | |
56 | enum perf_output_field field; | |
57 | } all_output_options[] = { | |
58 | {.str = "comm", .field = PERF_OUTPUT_COMM}, | |
59 | {.str = "tid", .field = PERF_OUTPUT_TID}, | |
60 | {.str = "pid", .field = PERF_OUTPUT_PID}, | |
61 | {.str = "time", .field = PERF_OUTPUT_TIME}, | |
62 | {.str = "cpu", .field = PERF_OUTPUT_CPU}, | |
63 | {.str = "event", .field = PERF_OUTPUT_EVNAME}, | |
64 | {.str = "trace", .field = PERF_OUTPUT_TRACE}, | |
787bef17 | 65 | {.str = "ip", .field = PERF_OUTPUT_IP}, |
c0230b2b | 66 | {.str = "sym", .field = PERF_OUTPUT_SYM}, |
610723f2 | 67 | {.str = "dso", .field = PERF_OUTPUT_DSO}, |
7cec0922 | 68 | {.str = "addr", .field = PERF_OUTPUT_ADDR}, |
a978f2ab | 69 | {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET}, |
cc8fae1d | 70 | {.str = "srcline", .field = PERF_OUTPUT_SRCLINE}, |
535aeaae | 71 | {.str = "period", .field = PERF_OUTPUT_PERIOD}, |
fc36f948 | 72 | {.str = "iregs", .field = PERF_OUTPUT_IREGS}, |
745f43e3 DA |
73 | }; |
74 | ||
75 | /* default set to maintain compatibility with current format */ | |
2c9e45f7 DA |
76 | static struct { |
77 | bool user_set; | |
9cbdb702 | 78 | bool wildcard_set; |
a6ffaf91 | 79 | unsigned int print_ip_opts; |
2c9e45f7 DA |
80 | u64 fields; |
81 | u64 invalid_fields; | |
82 | } output[PERF_TYPE_MAX] = { | |
83 | ||
84 | [PERF_TYPE_HARDWARE] = { | |
85 | .user_set = false, | |
86 | ||
87 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
88 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
787bef17 | 89 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
e8564b71 JO |
90 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
91 | PERF_OUTPUT_PERIOD, | |
2c9e45f7 DA |
92 | |
93 | .invalid_fields = PERF_OUTPUT_TRACE, | |
94 | }, | |
95 | ||
96 | [PERF_TYPE_SOFTWARE] = { | |
97 | .user_set = false, | |
98 | ||
99 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
100 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
787bef17 | 101 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
e8564b71 JO |
102 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
103 | PERF_OUTPUT_PERIOD, | |
2c9e45f7 DA |
104 | |
105 | .invalid_fields = PERF_OUTPUT_TRACE, | |
106 | }, | |
107 | ||
108 | [PERF_TYPE_TRACEPOINT] = { | |
109 | .user_set = false, | |
110 | ||
111 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
112 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
113 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE, | |
114 | }, | |
0817a6a3 AS |
115 | |
116 | [PERF_TYPE_RAW] = { | |
117 | .user_set = false, | |
118 | ||
119 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
120 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
787bef17 | 121 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
e8564b71 JO |
122 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO | |
123 | PERF_OUTPUT_PERIOD, | |
0817a6a3 AS |
124 | |
125 | .invalid_fields = PERF_OUTPUT_TRACE, | |
126 | }, | |
1424dc96 | 127 | }; |
745f43e3 | 128 | |
2c9e45f7 DA |
129 | static bool output_set_by_user(void) |
130 | { | |
131 | int j; | |
132 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
133 | if (output[j].user_set) | |
134 | return true; | |
135 | } | |
136 | return false; | |
137 | } | |
745f43e3 | 138 | |
9cbdb702 DA |
139 | static const char *output_field2str(enum perf_output_field field) |
140 | { | |
141 | int i, imax = ARRAY_SIZE(all_output_options); | |
142 | const char *str = ""; | |
143 | ||
144 | for (i = 0; i < imax; ++i) { | |
145 | if (all_output_options[i].field == field) { | |
146 | str = all_output_options[i].str; | |
147 | break; | |
148 | } | |
149 | } | |
150 | return str; | |
151 | } | |
152 | ||
2c9e45f7 | 153 | #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x) |
1424dc96 | 154 | |
6d5cdd64 AH |
155 | static int perf_evsel__do_check_stype(struct perf_evsel *evsel, |
156 | u64 sample_type, const char *sample_msg, | |
157 | enum perf_output_field field, | |
158 | bool allow_user_set) | |
1424dc96 | 159 | { |
5bff01f6 | 160 | struct perf_event_attr *attr = &evsel->attr; |
9cbdb702 DA |
161 | int type = attr->type; |
162 | const char *evname; | |
163 | ||
164 | if (attr->sample_type & sample_type) | |
165 | return 0; | |
166 | ||
167 | if (output[type].user_set) { | |
6d5cdd64 AH |
168 | if (allow_user_set) |
169 | return 0; | |
5bff01f6 | 170 | evname = perf_evsel__name(evsel); |
9cbdb702 DA |
171 | pr_err("Samples for '%s' event do not have %s attribute set. " |
172 | "Cannot print '%s' field.\n", | |
173 | evname, sample_msg, output_field2str(field)); | |
174 | return -1; | |
175 | } | |
176 | ||
177 | /* user did not ask for it explicitly so remove from the default list */ | |
178 | output[type].fields &= ~field; | |
5bff01f6 | 179 | evname = perf_evsel__name(evsel); |
9cbdb702 DA |
180 | pr_debug("Samples for '%s' event do not have %s attribute set. " |
181 | "Skipping '%s' field.\n", | |
182 | evname, sample_msg, output_field2str(field)); | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
6d5cdd64 AH |
187 | static int perf_evsel__check_stype(struct perf_evsel *evsel, |
188 | u64 sample_type, const char *sample_msg, | |
189 | enum perf_output_field field) | |
190 | { | |
191 | return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field, | |
192 | false); | |
193 | } | |
194 | ||
9cbdb702 DA |
195 | static int perf_evsel__check_attr(struct perf_evsel *evsel, |
196 | struct perf_session *session) | |
197 | { | |
198 | struct perf_event_attr *attr = &evsel->attr; | |
6d5cdd64 AH |
199 | bool allow_user_set; |
200 | ||
201 | allow_user_set = perf_header__has_feat(&session->header, | |
202 | HEADER_AUXTRACE); | |
9cbdb702 | 203 | |
1424dc96 DA |
204 | if (PRINT_FIELD(TRACE) && |
205 | !perf_session__has_traces(session, "record -R")) | |
206 | return -EINVAL; | |
207 | ||
787bef17 | 208 | if (PRINT_FIELD(IP)) { |
5bff01f6 ACM |
209 | if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", |
210 | PERF_OUTPUT_IP)) | |
1424dc96 | 211 | return -EINVAL; |
1424dc96 | 212 | } |
7cec0922 DA |
213 | |
214 | if (PRINT_FIELD(ADDR) && | |
6d5cdd64 AH |
215 | perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", |
216 | PERF_OUTPUT_ADDR, allow_user_set)) | |
7cec0922 DA |
217 | return -EINVAL; |
218 | ||
219 | if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { | |
220 | pr_err("Display of symbols requested but neither sample IP nor " | |
221 | "sample address\nis selected. Hence, no addresses to convert " | |
222 | "to symbols.\n"); | |
787bef17 DA |
223 | return -EINVAL; |
224 | } | |
a978f2ab AN |
225 | if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) { |
226 | pr_err("Display of offsets requested but symbol is not" | |
227 | "selected.\n"); | |
228 | return -EINVAL; | |
229 | } | |
7cec0922 DA |
230 | if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { |
231 | pr_err("Display of DSO requested but neither sample IP nor " | |
232 | "sample address\nis selected. Hence, no addresses to convert " | |
233 | "to DSO.\n"); | |
610723f2 DA |
234 | return -EINVAL; |
235 | } | |
cc8fae1d AH |
236 | if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) { |
237 | pr_err("Display of source line number requested but sample IP is not\n" | |
238 | "selected. Hence, no address to lookup the source line number.\n"); | |
239 | return -EINVAL; | |
240 | } | |
1424dc96 DA |
241 | |
242 | if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) && | |
5bff01f6 ACM |
243 | perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", |
244 | PERF_OUTPUT_TID|PERF_OUTPUT_PID)) | |
1424dc96 | 245 | return -EINVAL; |
1424dc96 DA |
246 | |
247 | if (PRINT_FIELD(TIME) && | |
5bff01f6 ACM |
248 | perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", |
249 | PERF_OUTPUT_TIME)) | |
1424dc96 | 250 | return -EINVAL; |
1424dc96 DA |
251 | |
252 | if (PRINT_FIELD(CPU) && | |
6d5cdd64 AH |
253 | perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", |
254 | PERF_OUTPUT_CPU, allow_user_set)) | |
1424dc96 | 255 | return -EINVAL; |
9cbdb702 | 256 | |
535aeaae JO |
257 | if (PRINT_FIELD(PERIOD) && |
258 | perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD", | |
259 | PERF_OUTPUT_PERIOD)) | |
260 | return -EINVAL; | |
261 | ||
fc36f948 SE |
262 | if (PRINT_FIELD(IREGS) && |
263 | perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", | |
264 | PERF_OUTPUT_IREGS)) | |
265 | return -EINVAL; | |
266 | ||
9cbdb702 DA |
267 | return 0; |
268 | } | |
269 | ||
7ea95727 AH |
270 | static void set_print_ip_opts(struct perf_event_attr *attr) |
271 | { | |
272 | unsigned int type = attr->type; | |
273 | ||
274 | output[type].print_ip_opts = 0; | |
275 | if (PRINT_FIELD(IP)) | |
276 | output[type].print_ip_opts |= PRINT_IP_OPT_IP; | |
277 | ||
278 | if (PRINT_FIELD(SYM)) | |
279 | output[type].print_ip_opts |= PRINT_IP_OPT_SYM; | |
280 | ||
281 | if (PRINT_FIELD(DSO)) | |
282 | output[type].print_ip_opts |= PRINT_IP_OPT_DSO; | |
283 | ||
284 | if (PRINT_FIELD(SYMOFFSET)) | |
285 | output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET; | |
cc8fae1d AH |
286 | |
287 | if (PRINT_FIELD(SRCLINE)) | |
288 | output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE; | |
7ea95727 AH |
289 | } |
290 | ||
9cbdb702 DA |
291 | /* |
292 | * verify all user requested events exist and the samples | |
293 | * have the expected data | |
294 | */ | |
295 | static int perf_session__check_output_opt(struct perf_session *session) | |
296 | { | |
297 | int j; | |
298 | struct perf_evsel *evsel; | |
299 | ||
300 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
301 | evsel = perf_session__find_first_evtype(session, j); | |
302 | ||
303 | /* | |
304 | * even if fields is set to 0 (ie., show nothing) event must | |
305 | * exist if user explicitly includes it on the command line | |
306 | */ | |
307 | if (!evsel && output[j].user_set && !output[j].wildcard_set) { | |
308 | pr_err("%s events do not exist. " | |
309 | "Remove corresponding -f option to proceed.\n", | |
310 | event_type(j)); | |
311 | return -1; | |
312 | } | |
313 | ||
314 | if (evsel && output[j].fields && | |
315 | perf_evsel__check_attr(evsel, session)) | |
316 | return -1; | |
a6ffaf91 DA |
317 | |
318 | if (evsel == NULL) | |
319 | continue; | |
320 | ||
7ea95727 | 321 | set_print_ip_opts(&evsel->attr); |
1424dc96 DA |
322 | } |
323 | ||
98526ee7 AH |
324 | if (!no_callchain) { |
325 | bool use_callchain = false; | |
326 | ||
327 | evlist__for_each(session->evlist, evsel) { | |
328 | if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { | |
329 | use_callchain = true; | |
330 | break; | |
331 | } | |
332 | } | |
333 | if (!use_callchain) | |
334 | symbol_conf.use_callchain = false; | |
335 | } | |
336 | ||
80b8b496 DA |
337 | /* |
338 | * set default for tracepoints to print symbols only | |
339 | * if callchains are present | |
340 | */ | |
341 | if (symbol_conf.use_callchain && | |
342 | !output[PERF_TYPE_TRACEPOINT].user_set) { | |
343 | struct perf_event_attr *attr; | |
344 | ||
345 | j = PERF_TYPE_TRACEPOINT; | |
346 | evsel = perf_session__find_first_evtype(session, j); | |
347 | if (evsel == NULL) | |
348 | goto out; | |
349 | ||
350 | attr = &evsel->attr; | |
351 | ||
352 | if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) { | |
353 | output[j].fields |= PERF_OUTPUT_IP; | |
354 | output[j].fields |= PERF_OUTPUT_SYM; | |
355 | output[j].fields |= PERF_OUTPUT_DSO; | |
356 | set_print_ip_opts(attr); | |
357 | } | |
358 | } | |
359 | ||
360 | out: | |
1424dc96 DA |
361 | return 0; |
362 | } | |
745f43e3 | 363 | |
fc36f948 SE |
364 | static void print_sample_iregs(union perf_event *event __maybe_unused, |
365 | struct perf_sample *sample, | |
366 | struct thread *thread __maybe_unused, | |
367 | struct perf_event_attr *attr) | |
368 | { | |
369 | struct regs_dump *regs = &sample->intr_regs; | |
370 | uint64_t mask = attr->sample_regs_intr; | |
371 | unsigned i = 0, r; | |
372 | ||
373 | if (!regs) | |
374 | return; | |
375 | ||
376 | for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) { | |
377 | u64 val = regs->regs[i++]; | |
378 | printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val); | |
379 | } | |
380 | } | |
381 | ||
fcf65bf1 | 382 | static void print_sample_start(struct perf_sample *sample, |
1424dc96 | 383 | struct thread *thread, |
5bff01f6 | 384 | struct perf_evsel *evsel) |
c70c94b4 | 385 | { |
5bff01f6 | 386 | struct perf_event_attr *attr = &evsel->attr; |
c70c94b4 DA |
387 | unsigned long secs; |
388 | unsigned long usecs; | |
745f43e3 DA |
389 | unsigned long long nsecs; |
390 | ||
391 | if (PRINT_FIELD(COMM)) { | |
392 | if (latency_format) | |
b9c5143a | 393 | printf("%8.8s ", thread__comm_str(thread)); |
787bef17 | 394 | else if (PRINT_FIELD(IP) && symbol_conf.use_callchain) |
b9c5143a | 395 | printf("%s ", thread__comm_str(thread)); |
745f43e3 | 396 | else |
b9c5143a | 397 | printf("%16s ", thread__comm_str(thread)); |
745f43e3 | 398 | } |
c70c94b4 | 399 | |
745f43e3 DA |
400 | if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) |
401 | printf("%5d/%-5d ", sample->pid, sample->tid); | |
402 | else if (PRINT_FIELD(PID)) | |
403 | printf("%5d ", sample->pid); | |
404 | else if (PRINT_FIELD(TID)) | |
405 | printf("%5d ", sample->tid); | |
406 | ||
407 | if (PRINT_FIELD(CPU)) { | |
408 | if (latency_format) | |
409 | printf("%3d ", sample->cpu); | |
410 | else | |
411 | printf("[%03d] ", sample->cpu); | |
412 | } | |
c70c94b4 | 413 | |
745f43e3 DA |
414 | if (PRINT_FIELD(TIME)) { |
415 | nsecs = sample->time; | |
416 | secs = nsecs / NSECS_PER_SEC; | |
417 | nsecs -= secs * NSECS_PER_SEC; | |
418 | usecs = nsecs / NSECS_PER_USEC; | |
83e19860 AH |
419 | if (nanosecs) |
420 | printf("%5lu.%09llu: ", secs, nsecs); | |
421 | else | |
422 | printf("%5lu.%06lu: ", secs, usecs); | |
745f43e3 | 423 | } |
c70c94b4 DA |
424 | } |
425 | ||
7cec0922 DA |
426 | static void print_sample_addr(union perf_event *event, |
427 | struct perf_sample *sample, | |
7cec0922 DA |
428 | struct thread *thread, |
429 | struct perf_event_attr *attr) | |
430 | { | |
431 | struct addr_location al; | |
7cec0922 DA |
432 | |
433 | printf("%16" PRIx64, sample->addr); | |
434 | ||
435 | if (!sample_addr_correlates_sym(attr)) | |
436 | return; | |
437 | ||
bb871a9c | 438 | perf_event__preprocess_sample_addr(event, sample, thread, &al); |
7cec0922 DA |
439 | |
440 | if (PRINT_FIELD(SYM)) { | |
547a92e0 | 441 | printf(" "); |
a978f2ab AN |
442 | if (PRINT_FIELD(SYMOFFSET)) |
443 | symbol__fprintf_symname_offs(al.sym, &al, stdout); | |
444 | else | |
445 | symbol__fprintf_symname(al.sym, stdout); | |
7cec0922 DA |
446 | } |
447 | ||
448 | if (PRINT_FIELD(DSO)) { | |
547a92e0 AN |
449 | printf(" ("); |
450 | map__fprintf_dsoname(al.map, stdout); | |
451 | printf(")"); | |
7cec0922 DA |
452 | } |
453 | } | |
454 | ||
95582596 AN |
455 | static void print_sample_bts(union perf_event *event, |
456 | struct perf_sample *sample, | |
457 | struct perf_evsel *evsel, | |
a2cb3cf2 AH |
458 | struct thread *thread, |
459 | struct addr_location *al) | |
95582596 AN |
460 | { |
461 | struct perf_event_attr *attr = &evsel->attr; | |
8066be5f | 462 | bool print_srcline_last = false; |
95582596 AN |
463 | |
464 | /* print branch_from information */ | |
465 | if (PRINT_FIELD(IP)) { | |
8066be5f AH |
466 | unsigned int print_opts = output[attr->type].print_ip_opts; |
467 | ||
468 | if (symbol_conf.use_callchain && sample->callchain) { | |
95582596 | 469 | printf("\n"); |
8066be5f AH |
470 | } else { |
471 | printf(" "); | |
472 | if (print_opts & PRINT_IP_OPT_SRCLINE) { | |
473 | print_srcline_last = true; | |
474 | print_opts &= ~PRINT_IP_OPT_SRCLINE; | |
475 | } | |
476 | } | |
477 | perf_evsel__print_ip(evsel, sample, al, print_opts, | |
307cbb92 | 478 | PERF_MAX_STACK_DEPTH); |
95582596 AN |
479 | } |
480 | ||
95582596 | 481 | /* print branch_to information */ |
243be3dd AH |
482 | if (PRINT_FIELD(ADDR) || |
483 | ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) && | |
578bea40 AH |
484 | !output[attr->type].user_set)) { |
485 | printf(" => "); | |
bb871a9c | 486 | print_sample_addr(event, sample, thread, attr); |
578bea40 | 487 | } |
95582596 | 488 | |
8066be5f AH |
489 | if (print_srcline_last) |
490 | map__fprintf_srcline(al->map, al->addr, "\n ", stdout); | |
491 | ||
95582596 AN |
492 | printf("\n"); |
493 | } | |
494 | ||
400ea6d3 AH |
495 | static void print_sample_flags(u32 flags) |
496 | { | |
497 | const char *chars = PERF_IP_FLAG_CHARS; | |
498 | const int n = strlen(PERF_IP_FLAG_CHARS); | |
499 | char str[33]; | |
500 | int i, pos = 0; | |
501 | ||
502 | for (i = 0; i < n; i++, flags >>= 1) { | |
503 | if (flags & 1) | |
504 | str[pos++] = chars[i]; | |
505 | } | |
506 | for (; i < 32; i++, flags >>= 1) { | |
507 | if (flags & 1) | |
508 | str[pos++] = '?'; | |
509 | } | |
510 | str[pos] = 0; | |
511 | printf(" %-4s ", str); | |
512 | } | |
513 | ||
97822433 | 514 | static void process_event(union perf_event *event, struct perf_sample *sample, |
f9d5d549 | 515 | struct perf_evsel *evsel, struct addr_location *al) |
be6d842a | 516 | { |
f9d5d549 | 517 | struct thread *thread = al->thread; |
9e69c210 | 518 | struct perf_event_attr *attr = &evsel->attr; |
1424dc96 | 519 | |
2c9e45f7 | 520 | if (output[attr->type].fields == 0) |
1424dc96 DA |
521 | return; |
522 | ||
fcf65bf1 | 523 | print_sample_start(sample, thread, evsel); |
745f43e3 | 524 | |
535aeaae JO |
525 | if (PRINT_FIELD(PERIOD)) |
526 | printf("%10" PRIu64 " ", sample->period); | |
527 | ||
e944d3d7 NK |
528 | if (PRINT_FIELD(EVNAME)) { |
529 | const char *evname = perf_evsel__name(evsel); | |
530 | printf("%s: ", evname ? evname : "[unknown]"); | |
531 | } | |
532 | ||
400ea6d3 AH |
533 | if (print_flags) |
534 | print_sample_flags(sample->flags); | |
535 | ||
95582596 | 536 | if (is_bts_event(attr)) { |
cc22e575 | 537 | print_sample_bts(event, sample, evsel, thread, al); |
95582596 AN |
538 | return; |
539 | } | |
540 | ||
745f43e3 | 541 | if (PRINT_FIELD(TRACE)) |
fcf65bf1 ACM |
542 | event_format__print(evsel->tp_format, sample->cpu, |
543 | sample->raw_data, sample->raw_size); | |
7cec0922 | 544 | if (PRINT_FIELD(ADDR)) |
bb871a9c | 545 | print_sample_addr(event, sample, thread, attr); |
7cec0922 | 546 | |
787bef17 | 547 | if (PRINT_FIELD(IP)) { |
c0230b2b DA |
548 | if (!symbol_conf.use_callchain) |
549 | printf(" "); | |
550 | else | |
551 | printf("\n"); | |
a6ffaf91 | 552 | |
cc22e575 | 553 | perf_evsel__print_ip(evsel, sample, al, |
307cbb92 DA |
554 | output[attr->type].print_ip_opts, |
555 | PERF_MAX_STACK_DEPTH); | |
c0230b2b DA |
556 | } |
557 | ||
fc36f948 SE |
558 | if (PRINT_FIELD(IREGS)) |
559 | print_sample_iregs(event, sample, thread, attr); | |
560 | ||
c70c94b4 | 561 | printf("\n"); |
be6d842a DA |
562 | } |
563 | ||
1d037ca1 IT |
564 | static int default_start_script(const char *script __maybe_unused, |
565 | int argc __maybe_unused, | |
566 | const char **argv __maybe_unused) | |
956ffd02 TZ |
567 | { |
568 | return 0; | |
569 | } | |
570 | ||
d445dd2a AH |
571 | static int default_flush_script(void) |
572 | { | |
573 | return 0; | |
574 | } | |
575 | ||
956ffd02 TZ |
576 | static int default_stop_script(void) |
577 | { | |
578 | return 0; | |
579 | } | |
580 | ||
1d037ca1 IT |
581 | static int default_generate_script(struct pevent *pevent __maybe_unused, |
582 | const char *outfile __maybe_unused) | |
956ffd02 TZ |
583 | { |
584 | return 0; | |
585 | } | |
586 | ||
587 | static struct scripting_ops default_scripting_ops = { | |
588 | .start_script = default_start_script, | |
d445dd2a | 589 | .flush_script = default_flush_script, |
956ffd02 | 590 | .stop_script = default_stop_script, |
be6d842a | 591 | .process_event = process_event, |
956ffd02 TZ |
592 | .generate_script = default_generate_script, |
593 | }; | |
594 | ||
595 | static struct scripting_ops *scripting_ops; | |
596 | ||
597 | static void setup_scripting(void) | |
598 | { | |
16c632de | 599 | setup_perl_scripting(); |
7e4b21b8 | 600 | setup_python_scripting(); |
16c632de | 601 | |
956ffd02 TZ |
602 | scripting_ops = &default_scripting_ops; |
603 | } | |
604 | ||
d445dd2a AH |
605 | static int flush_scripting(void) |
606 | { | |
607 | return scripting_ops->flush_script(); | |
608 | } | |
609 | ||
956ffd02 TZ |
610 | static int cleanup_scripting(void) |
611 | { | |
133dc4c3 | 612 | pr_debug("\nperf script stopped\n"); |
3824a4e8 | 613 | |
956ffd02 TZ |
614 | return scripting_ops->stop_script(); |
615 | } | |
616 | ||
1d037ca1 | 617 | static int process_sample_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 618 | union perf_event *event, |
8115d60c | 619 | struct perf_sample *sample, |
9e69c210 | 620 | struct perf_evsel *evsel, |
743eb868 | 621 | struct machine *machine) |
5f9c39dc | 622 | { |
e7984b7b | 623 | struct addr_location al; |
5f9c39dc | 624 | |
1424dc96 DA |
625 | if (debug_mode) { |
626 | if (sample->time < last_timestamp) { | |
627 | pr_err("Samples misordered, previous: %" PRIu64 | |
628 | " this: %" PRIu64 "\n", last_timestamp, | |
629 | sample->time); | |
630 | nr_unordered++; | |
e1889d75 | 631 | } |
1424dc96 DA |
632 | last_timestamp = sample->time; |
633 | return 0; | |
5f9c39dc | 634 | } |
5d67be97 | 635 | |
e44baa3e | 636 | if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) { |
e7984b7b DA |
637 | pr_err("problem processing %d event, skipping it.\n", |
638 | event->header.type); | |
639 | return -1; | |
640 | } | |
641 | ||
642 | if (al.filtered) | |
b91fc39f | 643 | goto out_put; |
e7984b7b | 644 | |
5d67be97 | 645 | if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) |
b91fc39f | 646 | goto out_put; |
5d67be97 | 647 | |
f9d5d549 | 648 | scripting_ops->process_event(event, sample, evsel, &al); |
b91fc39f ACM |
649 | out_put: |
650 | addr_location__put(&al); | |
5f9c39dc FW |
651 | return 0; |
652 | } | |
653 | ||
6f3e5eda AH |
654 | struct perf_script { |
655 | struct perf_tool tool; | |
656 | struct perf_session *session; | |
ad7ebb9a | 657 | bool show_task_events; |
ba1ddf42 | 658 | bool show_mmap_events; |
7c14898b | 659 | bool show_switch_events; |
016e92fb FW |
660 | }; |
661 | ||
7ea95727 AH |
662 | static int process_attr(struct perf_tool *tool, union perf_event *event, |
663 | struct perf_evlist **pevlist) | |
664 | { | |
665 | struct perf_script *scr = container_of(tool, struct perf_script, tool); | |
666 | struct perf_evlist *evlist; | |
667 | struct perf_evsel *evsel, *pos; | |
668 | int err; | |
669 | ||
670 | err = perf_event__process_attr(tool, event, pevlist); | |
671 | if (err) | |
672 | return err; | |
673 | ||
674 | evlist = *pevlist; | |
675 | evsel = perf_evlist__last(*pevlist); | |
676 | ||
677 | if (evsel->attr.type >= PERF_TYPE_MAX) | |
678 | return 0; | |
679 | ||
0050f7aa | 680 | evlist__for_each(evlist, pos) { |
7ea95727 AH |
681 | if (pos->attr.type == evsel->attr.type && pos != evsel) |
682 | return 0; | |
683 | } | |
684 | ||
685 | set_print_ip_opts(&evsel->attr); | |
686 | ||
687 | return perf_evsel__check_attr(evsel, scr->session); | |
688 | } | |
689 | ||
ad7ebb9a NK |
690 | static int process_comm_event(struct perf_tool *tool, |
691 | union perf_event *event, | |
692 | struct perf_sample *sample, | |
693 | struct machine *machine) | |
694 | { | |
695 | struct thread *thread; | |
696 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
697 | struct perf_session *session = script->session; | |
06b234ec | 698 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ad7ebb9a NK |
699 | int ret = -1; |
700 | ||
701 | thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid); | |
702 | if (thread == NULL) { | |
703 | pr_debug("problem processing COMM event, skipping it.\n"); | |
704 | return -1; | |
705 | } | |
706 | ||
707 | if (perf_event__process_comm(tool, event, sample, machine) < 0) | |
708 | goto out; | |
709 | ||
710 | if (!evsel->attr.sample_id_all) { | |
711 | sample->cpu = 0; | |
712 | sample->time = 0; | |
713 | sample->tid = event->comm.tid; | |
714 | sample->pid = event->comm.pid; | |
715 | } | |
716 | print_sample_start(sample, thread, evsel); | |
717 | perf_event__fprintf(event, stdout); | |
718 | ret = 0; | |
ad7ebb9a | 719 | out: |
b91fc39f | 720 | thread__put(thread); |
ad7ebb9a NK |
721 | return ret; |
722 | } | |
723 | ||
724 | static int process_fork_event(struct perf_tool *tool, | |
725 | union perf_event *event, | |
726 | struct perf_sample *sample, | |
727 | struct machine *machine) | |
728 | { | |
729 | struct thread *thread; | |
730 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
731 | struct perf_session *session = script->session; | |
06b234ec | 732 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ad7ebb9a NK |
733 | |
734 | if (perf_event__process_fork(tool, event, sample, machine) < 0) | |
735 | return -1; | |
736 | ||
737 | thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); | |
738 | if (thread == NULL) { | |
739 | pr_debug("problem processing FORK event, skipping it.\n"); | |
740 | return -1; | |
741 | } | |
742 | ||
743 | if (!evsel->attr.sample_id_all) { | |
744 | sample->cpu = 0; | |
745 | sample->time = event->fork.time; | |
746 | sample->tid = event->fork.tid; | |
747 | sample->pid = event->fork.pid; | |
748 | } | |
749 | print_sample_start(sample, thread, evsel); | |
750 | perf_event__fprintf(event, stdout); | |
b91fc39f | 751 | thread__put(thread); |
ad7ebb9a NK |
752 | |
753 | return 0; | |
754 | } | |
755 | static int process_exit_event(struct perf_tool *tool, | |
756 | union perf_event *event, | |
757 | struct perf_sample *sample, | |
758 | struct machine *machine) | |
759 | { | |
b91fc39f | 760 | int err = 0; |
ad7ebb9a NK |
761 | struct thread *thread; |
762 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
763 | struct perf_session *session = script->session; | |
06b234ec | 764 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ad7ebb9a NK |
765 | |
766 | thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); | |
767 | if (thread == NULL) { | |
768 | pr_debug("problem processing EXIT event, skipping it.\n"); | |
769 | return -1; | |
770 | } | |
771 | ||
772 | if (!evsel->attr.sample_id_all) { | |
773 | sample->cpu = 0; | |
774 | sample->time = 0; | |
53ff6bc3 AH |
775 | sample->tid = event->fork.tid; |
776 | sample->pid = event->fork.pid; | |
ad7ebb9a NK |
777 | } |
778 | print_sample_start(sample, thread, evsel); | |
779 | perf_event__fprintf(event, stdout); | |
780 | ||
781 | if (perf_event__process_exit(tool, event, sample, machine) < 0) | |
b91fc39f | 782 | err = -1; |
ad7ebb9a | 783 | |
b91fc39f ACM |
784 | thread__put(thread); |
785 | return err; | |
ad7ebb9a NK |
786 | } |
787 | ||
ba1ddf42 NK |
788 | static int process_mmap_event(struct perf_tool *tool, |
789 | union perf_event *event, | |
790 | struct perf_sample *sample, | |
791 | struct machine *machine) | |
792 | { | |
793 | struct thread *thread; | |
794 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
795 | struct perf_session *session = script->session; | |
06b234ec | 796 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ba1ddf42 NK |
797 | |
798 | if (perf_event__process_mmap(tool, event, sample, machine) < 0) | |
799 | return -1; | |
800 | ||
801 | thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid); | |
802 | if (thread == NULL) { | |
803 | pr_debug("problem processing MMAP event, skipping it.\n"); | |
804 | return -1; | |
805 | } | |
806 | ||
807 | if (!evsel->attr.sample_id_all) { | |
808 | sample->cpu = 0; | |
809 | sample->time = 0; | |
810 | sample->tid = event->mmap.tid; | |
811 | sample->pid = event->mmap.pid; | |
812 | } | |
813 | print_sample_start(sample, thread, evsel); | |
814 | perf_event__fprintf(event, stdout); | |
b91fc39f | 815 | thread__put(thread); |
ba1ddf42 NK |
816 | return 0; |
817 | } | |
818 | ||
819 | static int process_mmap2_event(struct perf_tool *tool, | |
820 | union perf_event *event, | |
821 | struct perf_sample *sample, | |
822 | struct machine *machine) | |
823 | { | |
824 | struct thread *thread; | |
825 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
826 | struct perf_session *session = script->session; | |
06b234ec | 827 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
ba1ddf42 NK |
828 | |
829 | if (perf_event__process_mmap2(tool, event, sample, machine) < 0) | |
830 | return -1; | |
831 | ||
832 | thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid); | |
833 | if (thread == NULL) { | |
834 | pr_debug("problem processing MMAP2 event, skipping it.\n"); | |
835 | return -1; | |
836 | } | |
837 | ||
838 | if (!evsel->attr.sample_id_all) { | |
839 | sample->cpu = 0; | |
840 | sample->time = 0; | |
841 | sample->tid = event->mmap2.tid; | |
842 | sample->pid = event->mmap2.pid; | |
843 | } | |
844 | print_sample_start(sample, thread, evsel); | |
845 | perf_event__fprintf(event, stdout); | |
b91fc39f | 846 | thread__put(thread); |
ba1ddf42 NK |
847 | return 0; |
848 | } | |
849 | ||
7c14898b AH |
850 | static int process_switch_event(struct perf_tool *tool, |
851 | union perf_event *event, | |
852 | struct perf_sample *sample, | |
853 | struct machine *machine) | |
854 | { | |
855 | struct thread *thread; | |
856 | struct perf_script *script = container_of(tool, struct perf_script, tool); | |
857 | struct perf_session *session = script->session; | |
858 | struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); | |
859 | ||
860 | if (perf_event__process_switch(tool, event, sample, machine) < 0) | |
861 | return -1; | |
862 | ||
863 | thread = machine__findnew_thread(machine, sample->pid, | |
864 | sample->tid); | |
865 | if (thread == NULL) { | |
866 | pr_debug("problem processing SWITCH event, skipping it.\n"); | |
867 | return -1; | |
868 | } | |
869 | ||
870 | print_sample_start(sample, thread, evsel); | |
871 | perf_event__fprintf(event, stdout); | |
872 | thread__put(thread); | |
873 | return 0; | |
874 | } | |
875 | ||
1d037ca1 | 876 | static void sig_handler(int sig __maybe_unused) |
c239da3b TZ |
877 | { |
878 | session_done = 1; | |
879 | } | |
880 | ||
6f3e5eda | 881 | static int __cmd_script(struct perf_script *script) |
5f9c39dc | 882 | { |
6fcf7ddb FW |
883 | int ret; |
884 | ||
c239da3b TZ |
885 | signal(SIGINT, sig_handler); |
886 | ||
ad7ebb9a NK |
887 | /* override event processing functions */ |
888 | if (script->show_task_events) { | |
889 | script->tool.comm = process_comm_event; | |
890 | script->tool.fork = process_fork_event; | |
891 | script->tool.exit = process_exit_event; | |
892 | } | |
ba1ddf42 NK |
893 | if (script->show_mmap_events) { |
894 | script->tool.mmap = process_mmap_event; | |
895 | script->tool.mmap2 = process_mmap2_event; | |
896 | } | |
7c14898b AH |
897 | if (script->show_switch_events) |
898 | script->tool.context_switch = process_switch_event; | |
ad7ebb9a | 899 | |
b7b61cbe | 900 | ret = perf_session__process_events(script->session); |
6fcf7ddb | 901 | |
6d8afb56 | 902 | if (debug_mode) |
9486aa38 | 903 | pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); |
6fcf7ddb FW |
904 | |
905 | return ret; | |
5f9c39dc FW |
906 | } |
907 | ||
956ffd02 TZ |
908 | struct script_spec { |
909 | struct list_head node; | |
910 | struct scripting_ops *ops; | |
911 | char spec[0]; | |
912 | }; | |
913 | ||
eccdfe2d | 914 | static LIST_HEAD(script_specs); |
956ffd02 TZ |
915 | |
916 | static struct script_spec *script_spec__new(const char *spec, | |
917 | struct scripting_ops *ops) | |
918 | { | |
919 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); | |
920 | ||
921 | if (s != NULL) { | |
922 | strcpy(s->spec, spec); | |
923 | s->ops = ops; | |
924 | } | |
925 | ||
926 | return s; | |
927 | } | |
928 | ||
956ffd02 TZ |
929 | static void script_spec__add(struct script_spec *s) |
930 | { | |
931 | list_add_tail(&s->node, &script_specs); | |
932 | } | |
933 | ||
934 | static struct script_spec *script_spec__find(const char *spec) | |
935 | { | |
936 | struct script_spec *s; | |
937 | ||
938 | list_for_each_entry(s, &script_specs, node) | |
939 | if (strcasecmp(s->spec, spec) == 0) | |
940 | return s; | |
941 | return NULL; | |
942 | } | |
943 | ||
944 | static struct script_spec *script_spec__findnew(const char *spec, | |
945 | struct scripting_ops *ops) | |
946 | { | |
947 | struct script_spec *s = script_spec__find(spec); | |
948 | ||
949 | if (s) | |
950 | return s; | |
951 | ||
952 | s = script_spec__new(spec, ops); | |
953 | if (!s) | |
466e2876 | 954 | return NULL; |
956ffd02 TZ |
955 | |
956 | script_spec__add(s); | |
957 | ||
958 | return s; | |
956ffd02 TZ |
959 | } |
960 | ||
961 | int script_spec_register(const char *spec, struct scripting_ops *ops) | |
962 | { | |
963 | struct script_spec *s; | |
964 | ||
965 | s = script_spec__find(spec); | |
966 | if (s) | |
967 | return -1; | |
968 | ||
969 | s = script_spec__findnew(spec, ops); | |
970 | if (!s) | |
971 | return -1; | |
972 | ||
973 | return 0; | |
974 | } | |
975 | ||
976 | static struct scripting_ops *script_spec__lookup(const char *spec) | |
977 | { | |
978 | struct script_spec *s = script_spec__find(spec); | |
979 | if (!s) | |
980 | return NULL; | |
981 | ||
982 | return s->ops; | |
983 | } | |
984 | ||
985 | static void list_available_languages(void) | |
986 | { | |
987 | struct script_spec *s; | |
988 | ||
989 | fprintf(stderr, "\n"); | |
990 | fprintf(stderr, "Scripting language extensions (used in " | |
133dc4c3 | 991 | "perf script -s [spec:]script.[spec]):\n\n"); |
956ffd02 TZ |
992 | |
993 | list_for_each_entry(s, &script_specs, node) | |
994 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); | |
995 | ||
996 | fprintf(stderr, "\n"); | |
997 | } | |
998 | ||
1d037ca1 IT |
999 | static int parse_scriptname(const struct option *opt __maybe_unused, |
1000 | const char *str, int unset __maybe_unused) | |
956ffd02 TZ |
1001 | { |
1002 | char spec[PATH_MAX]; | |
1003 | const char *script, *ext; | |
1004 | int len; | |
1005 | ||
f526d68b | 1006 | if (strcmp(str, "lang") == 0) { |
956ffd02 | 1007 | list_available_languages(); |
f526d68b | 1008 | exit(0); |
956ffd02 TZ |
1009 | } |
1010 | ||
1011 | script = strchr(str, ':'); | |
1012 | if (script) { | |
1013 | len = script - str; | |
1014 | if (len >= PATH_MAX) { | |
1015 | fprintf(stderr, "invalid language specifier"); | |
1016 | return -1; | |
1017 | } | |
1018 | strncpy(spec, str, len); | |
1019 | spec[len] = '\0'; | |
1020 | scripting_ops = script_spec__lookup(spec); | |
1021 | if (!scripting_ops) { | |
1022 | fprintf(stderr, "invalid language specifier"); | |
1023 | return -1; | |
1024 | } | |
1025 | script++; | |
1026 | } else { | |
1027 | script = str; | |
d1e95bb5 | 1028 | ext = strrchr(script, '.'); |
956ffd02 TZ |
1029 | if (!ext) { |
1030 | fprintf(stderr, "invalid script extension"); | |
1031 | return -1; | |
1032 | } | |
1033 | scripting_ops = script_spec__lookup(++ext); | |
1034 | if (!scripting_ops) { | |
1035 | fprintf(stderr, "invalid script extension"); | |
1036 | return -1; | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | script_name = strdup(script); | |
1041 | ||
1042 | return 0; | |
1043 | } | |
1044 | ||
1d037ca1 IT |
1045 | static int parse_output_fields(const struct option *opt __maybe_unused, |
1046 | const char *arg, int unset __maybe_unused) | |
745f43e3 DA |
1047 | { |
1048 | char *tok; | |
50ca19ae | 1049 | int i, imax = ARRAY_SIZE(all_output_options); |
2c9e45f7 | 1050 | int j; |
745f43e3 DA |
1051 | int rc = 0; |
1052 | char *str = strdup(arg); | |
1424dc96 | 1053 | int type = -1; |
745f43e3 DA |
1054 | |
1055 | if (!str) | |
1056 | return -ENOMEM; | |
1057 | ||
2c9e45f7 DA |
1058 | /* first word can state for which event type the user is specifying |
1059 | * the fields. If no type exists, the specified fields apply to all | |
1060 | * event types found in the file minus the invalid fields for a type. | |
1424dc96 | 1061 | */ |
2c9e45f7 DA |
1062 | tok = strchr(str, ':'); |
1063 | if (tok) { | |
1064 | *tok = '\0'; | |
1065 | tok++; | |
1066 | if (!strcmp(str, "hw")) | |
1067 | type = PERF_TYPE_HARDWARE; | |
1068 | else if (!strcmp(str, "sw")) | |
1069 | type = PERF_TYPE_SOFTWARE; | |
1070 | else if (!strcmp(str, "trace")) | |
1071 | type = PERF_TYPE_TRACEPOINT; | |
0817a6a3 AS |
1072 | else if (!strcmp(str, "raw")) |
1073 | type = PERF_TYPE_RAW; | |
2c9e45f7 DA |
1074 | else { |
1075 | fprintf(stderr, "Invalid event type in field string.\n"); | |
38efb539 RR |
1076 | rc = -EINVAL; |
1077 | goto out; | |
2c9e45f7 DA |
1078 | } |
1079 | ||
1080 | if (output[type].user_set) | |
1081 | pr_warning("Overriding previous field request for %s events.\n", | |
1082 | event_type(type)); | |
1083 | ||
1084 | output[type].fields = 0; | |
1085 | output[type].user_set = true; | |
9cbdb702 | 1086 | output[type].wildcard_set = false; |
2c9e45f7 DA |
1087 | |
1088 | } else { | |
1089 | tok = str; | |
1090 | if (strlen(str) == 0) { | |
1091 | fprintf(stderr, | |
1092 | "Cannot set fields to 'none' for all event types.\n"); | |
1093 | rc = -EINVAL; | |
1094 | goto out; | |
1095 | } | |
1096 | ||
1097 | if (output_set_by_user()) | |
1098 | pr_warning("Overriding previous field request for all events.\n"); | |
1099 | ||
1100 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
1101 | output[j].fields = 0; | |
1102 | output[j].user_set = true; | |
9cbdb702 | 1103 | output[j].wildcard_set = true; |
2c9e45f7 | 1104 | } |
745f43e3 DA |
1105 | } |
1106 | ||
400ea6d3 | 1107 | for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) { |
745f43e3 | 1108 | for (i = 0; i < imax; ++i) { |
2c9e45f7 | 1109 | if (strcmp(tok, all_output_options[i].str) == 0) |
745f43e3 | 1110 | break; |
745f43e3 | 1111 | } |
400ea6d3 AH |
1112 | if (i == imax && strcmp(tok, "flags") == 0) { |
1113 | print_flags = true; | |
1114 | continue; | |
1115 | } | |
745f43e3 | 1116 | if (i == imax) { |
2c9e45f7 | 1117 | fprintf(stderr, "Invalid field requested.\n"); |
745f43e3 | 1118 | rc = -EINVAL; |
2c9e45f7 | 1119 | goto out; |
745f43e3 DA |
1120 | } |
1121 | ||
2c9e45f7 DA |
1122 | if (type == -1) { |
1123 | /* add user option to all events types for | |
1124 | * which it is valid | |
1125 | */ | |
1126 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
1127 | if (output[j].invalid_fields & all_output_options[i].field) { | |
1128 | pr_warning("\'%s\' not valid for %s events. Ignoring.\n", | |
1129 | all_output_options[i].str, event_type(j)); | |
1130 | } else | |
1131 | output[j].fields |= all_output_options[i].field; | |
1132 | } | |
1133 | } else { | |
1134 | if (output[type].invalid_fields & all_output_options[i].field) { | |
1135 | fprintf(stderr, "\'%s\' not valid for %s events.\n", | |
1136 | all_output_options[i].str, event_type(type)); | |
1137 | ||
1138 | rc = -EINVAL; | |
1139 | goto out; | |
1140 | } | |
1141 | output[type].fields |= all_output_options[i].field; | |
1142 | } | |
745f43e3 DA |
1143 | } |
1144 | ||
2c9e45f7 DA |
1145 | if (type >= 0) { |
1146 | if (output[type].fields == 0) { | |
1147 | pr_debug("No fields requested for %s type. " | |
1148 | "Events will not be displayed.\n", event_type(type)); | |
1149 | } | |
1150 | } | |
745f43e3 | 1151 | |
2c9e45f7 | 1152 | out: |
745f43e3 DA |
1153 | free(str); |
1154 | return rc; | |
1155 | } | |
1156 | ||
008f29d3 SB |
1157 | /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */ |
1158 | static int is_directory(const char *base_path, const struct dirent *dent) | |
1159 | { | |
1160 | char path[PATH_MAX]; | |
1161 | struct stat st; | |
1162 | ||
1163 | sprintf(path, "%s/%s", base_path, dent->d_name); | |
1164 | if (stat(path, &st)) | |
1165 | return 0; | |
1166 | ||
1167 | return S_ISDIR(st.st_mode); | |
1168 | } | |
1169 | ||
1170 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\ | |
4b9c0c59 TZ |
1171 | while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ |
1172 | lang_next) \ | |
008f29d3 SB |
1173 | if ((lang_dirent.d_type == DT_DIR || \ |
1174 | (lang_dirent.d_type == DT_UNKNOWN && \ | |
1175 | is_directory(scripts_path, &lang_dirent))) && \ | |
4b9c0c59 TZ |
1176 | (strcmp(lang_dirent.d_name, ".")) && \ |
1177 | (strcmp(lang_dirent.d_name, ".."))) | |
1178 | ||
008f29d3 | 1179 | #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\ |
4b9c0c59 TZ |
1180 | while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ |
1181 | script_next) \ | |
008f29d3 SB |
1182 | if (script_dirent.d_type != DT_DIR && \ |
1183 | (script_dirent.d_type != DT_UNKNOWN || \ | |
1184 | !is_directory(lang_path, &script_dirent))) | |
4b9c0c59 TZ |
1185 | |
1186 | ||
1187 | #define RECORD_SUFFIX "-record" | |
1188 | #define REPORT_SUFFIX "-report" | |
1189 | ||
1190 | struct script_desc { | |
1191 | struct list_head node; | |
1192 | char *name; | |
1193 | char *half_liner; | |
1194 | char *args; | |
1195 | }; | |
1196 | ||
eccdfe2d | 1197 | static LIST_HEAD(script_descs); |
4b9c0c59 TZ |
1198 | |
1199 | static struct script_desc *script_desc__new(const char *name) | |
1200 | { | |
1201 | struct script_desc *s = zalloc(sizeof(*s)); | |
1202 | ||
b5b87312 | 1203 | if (s != NULL && name) |
4b9c0c59 TZ |
1204 | s->name = strdup(name); |
1205 | ||
1206 | return s; | |
1207 | } | |
1208 | ||
1209 | static void script_desc__delete(struct script_desc *s) | |
1210 | { | |
74cf249d ACM |
1211 | zfree(&s->name); |
1212 | zfree(&s->half_liner); | |
1213 | zfree(&s->args); | |
4b9c0c59 TZ |
1214 | free(s); |
1215 | } | |
1216 | ||
1217 | static void script_desc__add(struct script_desc *s) | |
1218 | { | |
1219 | list_add_tail(&s->node, &script_descs); | |
1220 | } | |
1221 | ||
1222 | static struct script_desc *script_desc__find(const char *name) | |
1223 | { | |
1224 | struct script_desc *s; | |
1225 | ||
1226 | list_for_each_entry(s, &script_descs, node) | |
1227 | if (strcasecmp(s->name, name) == 0) | |
1228 | return s; | |
1229 | return NULL; | |
1230 | } | |
1231 | ||
1232 | static struct script_desc *script_desc__findnew(const char *name) | |
1233 | { | |
1234 | struct script_desc *s = script_desc__find(name); | |
1235 | ||
1236 | if (s) | |
1237 | return s; | |
1238 | ||
1239 | s = script_desc__new(name); | |
1240 | if (!s) | |
1241 | goto out_delete_desc; | |
1242 | ||
1243 | script_desc__add(s); | |
1244 | ||
1245 | return s; | |
1246 | ||
1247 | out_delete_desc: | |
1248 | script_desc__delete(s); | |
1249 | ||
1250 | return NULL; | |
1251 | } | |
1252 | ||
965bb6be | 1253 | static const char *ends_with(const char *str, const char *suffix) |
4b9c0c59 TZ |
1254 | { |
1255 | size_t suffix_len = strlen(suffix); | |
965bb6be | 1256 | const char *p = str; |
4b9c0c59 TZ |
1257 | |
1258 | if (strlen(str) > suffix_len) { | |
1259 | p = str + strlen(str) - suffix_len; | |
1260 | if (!strncmp(p, suffix, suffix_len)) | |
1261 | return p; | |
1262 | } | |
1263 | ||
1264 | return NULL; | |
1265 | } | |
1266 | ||
4b9c0c59 TZ |
1267 | static int read_script_info(struct script_desc *desc, const char *filename) |
1268 | { | |
1269 | char line[BUFSIZ], *p; | |
1270 | FILE *fp; | |
1271 | ||
1272 | fp = fopen(filename, "r"); | |
1273 | if (!fp) | |
1274 | return -1; | |
1275 | ||
1276 | while (fgets(line, sizeof(line), fp)) { | |
1277 | p = ltrim(line); | |
1278 | if (strlen(p) == 0) | |
1279 | continue; | |
1280 | if (*p != '#') | |
1281 | continue; | |
1282 | p++; | |
1283 | if (strlen(p) && *p == '!') | |
1284 | continue; | |
1285 | ||
1286 | p = ltrim(p); | |
1287 | if (strlen(p) && p[strlen(p) - 1] == '\n') | |
1288 | p[strlen(p) - 1] = '\0'; | |
1289 | ||
1290 | if (!strncmp(p, "description:", strlen("description:"))) { | |
1291 | p += strlen("description:"); | |
1292 | desc->half_liner = strdup(ltrim(p)); | |
1293 | continue; | |
1294 | } | |
1295 | ||
1296 | if (!strncmp(p, "args:", strlen("args:"))) { | |
1297 | p += strlen("args:"); | |
1298 | desc->args = strdup(ltrim(p)); | |
1299 | continue; | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | fclose(fp); | |
1304 | ||
1305 | return 0; | |
1306 | } | |
1307 | ||
38efb539 RR |
1308 | static char *get_script_root(struct dirent *script_dirent, const char *suffix) |
1309 | { | |
1310 | char *script_root, *str; | |
1311 | ||
1312 | script_root = strdup(script_dirent->d_name); | |
1313 | if (!script_root) | |
1314 | return NULL; | |
1315 | ||
1316 | str = (char *)ends_with(script_root, suffix); | |
1317 | if (!str) { | |
1318 | free(script_root); | |
1319 | return NULL; | |
1320 | } | |
1321 | ||
1322 | *str = '\0'; | |
1323 | return script_root; | |
1324 | } | |
1325 | ||
1d037ca1 IT |
1326 | static int list_available_scripts(const struct option *opt __maybe_unused, |
1327 | const char *s __maybe_unused, | |
1328 | int unset __maybe_unused) | |
4b9c0c59 TZ |
1329 | { |
1330 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
1331 | char scripts_path[MAXPATHLEN]; | |
1332 | DIR *scripts_dir, *lang_dir; | |
1333 | char script_path[MAXPATHLEN]; | |
1334 | char lang_path[MAXPATHLEN]; | |
1335 | struct script_desc *desc; | |
1336 | char first_half[BUFSIZ]; | |
1337 | char *script_root; | |
4b9c0c59 TZ |
1338 | |
1339 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | |
1340 | ||
1341 | scripts_dir = opendir(scripts_path); | |
1342 | if (!scripts_dir) | |
1343 | return -1; | |
1344 | ||
008f29d3 | 1345 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
4b9c0c59 TZ |
1346 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
1347 | lang_dirent.d_name); | |
1348 | lang_dir = opendir(lang_path); | |
1349 | if (!lang_dir) | |
1350 | continue; | |
1351 | ||
008f29d3 | 1352 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
38efb539 RR |
1353 | script_root = get_script_root(&script_dirent, REPORT_SUFFIX); |
1354 | if (script_root) { | |
4b9c0c59 TZ |
1355 | desc = script_desc__findnew(script_root); |
1356 | snprintf(script_path, MAXPATHLEN, "%s/%s", | |
1357 | lang_path, script_dirent.d_name); | |
1358 | read_script_info(desc, script_path); | |
38efb539 | 1359 | free(script_root); |
4b9c0c59 | 1360 | } |
4b9c0c59 TZ |
1361 | } |
1362 | } | |
1363 | ||
1364 | fprintf(stdout, "List of available trace scripts:\n"); | |
1365 | list_for_each_entry(desc, &script_descs, node) { | |
1366 | sprintf(first_half, "%s %s", desc->name, | |
1367 | desc->args ? desc->args : ""); | |
1368 | fprintf(stdout, " %-36s %s\n", first_half, | |
1369 | desc->half_liner ? desc->half_liner : ""); | |
1370 | } | |
1371 | ||
1372 | exit(0); | |
1373 | } | |
1374 | ||
49e639e2 FT |
1375 | /* |
1376 | * Some scripts specify the required events in their "xxx-record" file, | |
1377 | * this function will check if the events in perf.data match those | |
1378 | * mentioned in the "xxx-record". | |
1379 | * | |
1380 | * Fixme: All existing "xxx-record" are all in good formats "-e event ", | |
1381 | * which is covered well now. And new parsing code should be added to | |
1382 | * cover the future complexing formats like event groups etc. | |
1383 | */ | |
1384 | static int check_ev_match(char *dir_name, char *scriptname, | |
1385 | struct perf_session *session) | |
1386 | { | |
1387 | char filename[MAXPATHLEN], evname[128]; | |
1388 | char line[BUFSIZ], *p; | |
1389 | struct perf_evsel *pos; | |
1390 | int match, len; | |
1391 | FILE *fp; | |
1392 | ||
1393 | sprintf(filename, "%s/bin/%s-record", dir_name, scriptname); | |
1394 | ||
1395 | fp = fopen(filename, "r"); | |
1396 | if (!fp) | |
1397 | return -1; | |
1398 | ||
1399 | while (fgets(line, sizeof(line), fp)) { | |
1400 | p = ltrim(line); | |
1401 | if (*p == '#') | |
1402 | continue; | |
1403 | ||
1404 | while (strlen(p)) { | |
1405 | p = strstr(p, "-e"); | |
1406 | if (!p) | |
1407 | break; | |
1408 | ||
1409 | p += 2; | |
1410 | p = ltrim(p); | |
1411 | len = strcspn(p, " \t"); | |
1412 | if (!len) | |
1413 | break; | |
1414 | ||
1415 | snprintf(evname, len + 1, "%s", p); | |
1416 | ||
1417 | match = 0; | |
0050f7aa | 1418 | evlist__for_each(session->evlist, pos) { |
49e639e2 FT |
1419 | if (!strcmp(perf_evsel__name(pos), evname)) { |
1420 | match = 1; | |
1421 | break; | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | if (!match) { | |
1426 | fclose(fp); | |
1427 | return -1; | |
1428 | } | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | fclose(fp); | |
1433 | return 0; | |
1434 | } | |
1435 | ||
e5f3705e FT |
1436 | /* |
1437 | * Return -1 if none is found, otherwise the actual scripts number. | |
1438 | * | |
1439 | * Currently the only user of this function is the script browser, which | |
1440 | * will list all statically runnable scripts, select one, execute it and | |
1441 | * show the output in a perf browser. | |
1442 | */ | |
1443 | int find_scripts(char **scripts_array, char **scripts_path_array) | |
1444 | { | |
1445 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
49e639e2 | 1446 | char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN]; |
e5f3705e | 1447 | DIR *scripts_dir, *lang_dir; |
49e639e2 | 1448 | struct perf_session *session; |
f5fc1412 JO |
1449 | struct perf_data_file file = { |
1450 | .path = input_name, | |
1451 | .mode = PERF_DATA_MODE_READ, | |
1452 | }; | |
e5f3705e FT |
1453 | char *temp; |
1454 | int i = 0; | |
1455 | ||
f5fc1412 | 1456 | session = perf_session__new(&file, false, NULL); |
49e639e2 FT |
1457 | if (!session) |
1458 | return -1; | |
1459 | ||
e5f3705e FT |
1460 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); |
1461 | ||
1462 | scripts_dir = opendir(scripts_path); | |
49e639e2 FT |
1463 | if (!scripts_dir) { |
1464 | perf_session__delete(session); | |
e5f3705e | 1465 | return -1; |
49e639e2 | 1466 | } |
e5f3705e FT |
1467 | |
1468 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { | |
1469 | snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, | |
1470 | lang_dirent.d_name); | |
1471 | #ifdef NO_LIBPERL | |
1472 | if (strstr(lang_path, "perl")) | |
1473 | continue; | |
1474 | #endif | |
1475 | #ifdef NO_LIBPYTHON | |
1476 | if (strstr(lang_path, "python")) | |
1477 | continue; | |
1478 | #endif | |
1479 | ||
1480 | lang_dir = opendir(lang_path); | |
1481 | if (!lang_dir) | |
1482 | continue; | |
1483 | ||
1484 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { | |
1485 | /* Skip those real time scripts: xxxtop.p[yl] */ | |
1486 | if (strstr(script_dirent.d_name, "top.")) | |
1487 | continue; | |
1488 | sprintf(scripts_path_array[i], "%s/%s", lang_path, | |
1489 | script_dirent.d_name); | |
1490 | temp = strchr(script_dirent.d_name, '.'); | |
1491 | snprintf(scripts_array[i], | |
1492 | (temp - script_dirent.d_name) + 1, | |
1493 | "%s", script_dirent.d_name); | |
49e639e2 FT |
1494 | |
1495 | if (check_ev_match(lang_path, | |
1496 | scripts_array[i], session)) | |
1497 | continue; | |
1498 | ||
e5f3705e FT |
1499 | i++; |
1500 | } | |
49e639e2 | 1501 | closedir(lang_dir); |
e5f3705e FT |
1502 | } |
1503 | ||
49e639e2 FT |
1504 | closedir(scripts_dir); |
1505 | perf_session__delete(session); | |
e5f3705e FT |
1506 | return i; |
1507 | } | |
1508 | ||
3875294f TZ |
1509 | static char *get_script_path(const char *script_root, const char *suffix) |
1510 | { | |
1511 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
1512 | char scripts_path[MAXPATHLEN]; | |
1513 | char script_path[MAXPATHLEN]; | |
1514 | DIR *scripts_dir, *lang_dir; | |
1515 | char lang_path[MAXPATHLEN]; | |
38efb539 | 1516 | char *__script_root; |
3875294f TZ |
1517 | |
1518 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | |
1519 | ||
1520 | scripts_dir = opendir(scripts_path); | |
1521 | if (!scripts_dir) | |
1522 | return NULL; | |
1523 | ||
008f29d3 | 1524 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
3875294f TZ |
1525 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
1526 | lang_dirent.d_name); | |
1527 | lang_dir = opendir(lang_path); | |
1528 | if (!lang_dir) | |
1529 | continue; | |
1530 | ||
008f29d3 | 1531 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
38efb539 RR |
1532 | __script_root = get_script_root(&script_dirent, suffix); |
1533 | if (__script_root && !strcmp(script_root, __script_root)) { | |
1534 | free(__script_root); | |
946ef2a2 NK |
1535 | closedir(lang_dir); |
1536 | closedir(scripts_dir); | |
3875294f TZ |
1537 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
1538 | lang_path, script_dirent.d_name); | |
38efb539 | 1539 | return strdup(script_path); |
3875294f TZ |
1540 | } |
1541 | free(__script_root); | |
1542 | } | |
946ef2a2 | 1543 | closedir(lang_dir); |
3875294f | 1544 | } |
946ef2a2 | 1545 | closedir(scripts_dir); |
3875294f | 1546 | |
38efb539 | 1547 | return NULL; |
3875294f TZ |
1548 | } |
1549 | ||
b5b87312 TZ |
1550 | static bool is_top_script(const char *script_path) |
1551 | { | |
965bb6be | 1552 | return ends_with(script_path, "top") == NULL ? false : true; |
b5b87312 TZ |
1553 | } |
1554 | ||
1555 | static int has_required_arg(char *script_path) | |
1556 | { | |
1557 | struct script_desc *desc; | |
1558 | int n_args = 0; | |
1559 | char *p; | |
1560 | ||
1561 | desc = script_desc__new(NULL); | |
1562 | ||
1563 | if (read_script_info(desc, script_path)) | |
1564 | goto out; | |
1565 | ||
1566 | if (!desc->args) | |
1567 | goto out; | |
1568 | ||
1569 | for (p = desc->args; *p; p++) | |
1570 | if (*p == '<') | |
1571 | n_args++; | |
1572 | out: | |
1573 | script_desc__delete(desc); | |
1574 | ||
1575 | return n_args; | |
1576 | } | |
1577 | ||
69b6470e ACM |
1578 | static int have_cmd(int argc, const char **argv) |
1579 | { | |
1580 | char **__argv = malloc(sizeof(const char *) * argc); | |
1581 | ||
1582 | if (!__argv) { | |
1583 | pr_err("malloc failed\n"); | |
1584 | return -1; | |
1585 | } | |
1586 | ||
1587 | memcpy(__argv, argv, sizeof(const char *) * argc); | |
1588 | argc = parse_options(argc, (const char **)__argv, record_options, | |
1589 | NULL, PARSE_OPT_STOP_AT_NON_OPTION); | |
1590 | free(__argv); | |
5f9c39dc | 1591 | |
69b6470e ACM |
1592 | system_wide = (argc == 0); |
1593 | ||
1594 | return 0; | |
1595 | } | |
1596 | ||
7322d6c9 JO |
1597 | static void script__setup_sample_type(struct perf_script *script) |
1598 | { | |
1599 | struct perf_session *session = script->session; | |
1600 | u64 sample_type = perf_evlist__combined_sample_type(session->evlist); | |
1601 | ||
1602 | if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) { | |
1603 | if ((sample_type & PERF_SAMPLE_REGS_USER) && | |
1604 | (sample_type & PERF_SAMPLE_STACK_USER)) | |
1605 | callchain_param.record_mode = CALLCHAIN_DWARF; | |
1606 | else if (sample_type & PERF_SAMPLE_BRANCH_STACK) | |
1607 | callchain_param.record_mode = CALLCHAIN_LBR; | |
1608 | else | |
1609 | callchain_param.record_mode = CALLCHAIN_FP; | |
1610 | } | |
1611 | } | |
1612 | ||
69b6470e ACM |
1613 | int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused) |
1614 | { | |
1615 | bool show_full_info = false; | |
e90debdd JO |
1616 | bool header = false; |
1617 | bool header_only = false; | |
6cc870f0 | 1618 | bool script_started = false; |
69b6470e ACM |
1619 | char *rec_script_path = NULL; |
1620 | char *rep_script_path = NULL; | |
1621 | struct perf_session *session; | |
7a680eb9 | 1622 | struct itrace_synth_opts itrace_synth_opts = { .set = false, }; |
69b6470e ACM |
1623 | char *script_path = NULL; |
1624 | const char **__argv; | |
6cc870f0 | 1625 | int i, j, err = 0; |
6f3e5eda AH |
1626 | struct perf_script script = { |
1627 | .tool = { | |
1628 | .sample = process_sample_event, | |
1629 | .mmap = perf_event__process_mmap, | |
1630 | .mmap2 = perf_event__process_mmap2, | |
1631 | .comm = perf_event__process_comm, | |
1632 | .exit = perf_event__process_exit, | |
1633 | .fork = perf_event__process_fork, | |
7ea95727 | 1634 | .attr = process_attr, |
6f3e5eda AH |
1635 | .tracing_data = perf_event__process_tracing_data, |
1636 | .build_id = perf_event__process_build_id, | |
7a680eb9 AH |
1637 | .id_index = perf_event__process_id_index, |
1638 | .auxtrace_info = perf_event__process_auxtrace_info, | |
1639 | .auxtrace = perf_event__process_auxtrace, | |
1640 | .auxtrace_error = perf_event__process_auxtrace_error, | |
0a8cb85c | 1641 | .ordered_events = true, |
6f3e5eda AH |
1642 | .ordering_requires_timestamps = true, |
1643 | }, | |
1644 | }; | |
06af0f2c YS |
1645 | struct perf_data_file file = { |
1646 | .mode = PERF_DATA_MODE_READ, | |
1647 | }; | |
69b6470e | 1648 | const struct option options[] = { |
5f9c39dc FW |
1649 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
1650 | "dump raw trace in ASCII"), | |
c0555642 | 1651 | OPT_INCR('v', "verbose", &verbose, |
69b6470e | 1652 | "be more verbose (show symbol address, etc)"), |
4b9c0c59 | 1653 | OPT_BOOLEAN('L', "Latency", &latency_format, |
cda48461 | 1654 | "show latency attributes (irqs/preemption disabled, etc)"), |
4b9c0c59 TZ |
1655 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", |
1656 | list_available_scripts), | |
956ffd02 TZ |
1657 | OPT_CALLBACK('s', "script", NULL, "name", |
1658 | "script file name (lang:script name, script name, or *)", | |
1659 | parse_scriptname), | |
1660 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", | |
133dc4c3 | 1661 | "generate perf-script.xx script in specified language"), |
69b6470e | 1662 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
ffabd99e FW |
1663 | OPT_BOOLEAN('d', "debug-mode", &debug_mode, |
1664 | "do various checks like samples ordering and lost events"), | |
e90debdd JO |
1665 | OPT_BOOLEAN(0, "header", &header, "Show data header."), |
1666 | OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."), | |
c0230b2b DA |
1667 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
1668 | "file", "vmlinux pathname"), | |
1669 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, | |
1670 | "file", "kallsyms pathname"), | |
1671 | OPT_BOOLEAN('G', "hide-call-graph", &no_callchain, | |
1672 | "When printing symbols do not display call chain"), | |
1673 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", | |
1674 | "Look for files with symbols relative to this directory"), | |
06af0f2c | 1675 | OPT_CALLBACK('F', "fields", NULL, "str", |
a978f2ab AN |
1676 | "comma separated output fields prepend with 'type:'. " |
1677 | "Valid types: hw,sw,trace,raw. " | |
1678 | "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso," | |
fc36f948 | 1679 | "addr,symoff,period,iregs,flags", parse_output_fields), |
317df650 | 1680 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
69b6470e | 1681 | "system-wide collection from all CPUs"), |
36385be5 FT |
1682 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", |
1683 | "only consider these symbols"), | |
c8e66720 | 1684 | OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"), |
e7984b7b DA |
1685 | OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", |
1686 | "only display events for these comms"), | |
e03eaa40 DA |
1687 | OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]", |
1688 | "only consider symbols in these pids"), | |
1689 | OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]", | |
1690 | "only consider symbols in these tids"), | |
fbe96f29 SE |
1691 | OPT_BOOLEAN('I', "show-info", &show_full_info, |
1692 | "display extended information from perf.data file"), | |
0bc8d205 AN |
1693 | OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path, |
1694 | "Show the path of [kernel.kallsyms]"), | |
ad7ebb9a NK |
1695 | OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events, |
1696 | "Show the fork/comm/exit events"), | |
ba1ddf42 NK |
1697 | OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events, |
1698 | "Show the mmap events"), | |
7c14898b AH |
1699 | OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events, |
1700 | "Show context switch events (if recorded)"), | |
06af0f2c | 1701 | OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"), |
83e19860 AH |
1702 | OPT_BOOLEAN(0, "ns", &nanosecs, |
1703 | "Use 9 decimal places when displaying time"), | |
7a680eb9 AH |
1704 | OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", |
1705 | "Instruction Tracing options", | |
1706 | itrace_parse_synth_opts), | |
a9710ba0 AK |
1707 | OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename, |
1708 | "Show full source file name path for source lines"), | |
77e0070d MD |
1709 | OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, |
1710 | "Enable symbol demangling"), | |
1711 | OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, | |
1712 | "Enable kernel symbol demangling"), | |
1713 | ||
1909629f | 1714 | OPT_END() |
69b6470e | 1715 | }; |
40cae2b7 YS |
1716 | const char * const script_subcommands[] = { "record", "report", NULL }; |
1717 | const char *script_usage[] = { | |
69b6470e ACM |
1718 | "perf script [<options>]", |
1719 | "perf script [<options>] record <script> [<record-options>] <command>", | |
1720 | "perf script [<options>] report <script> [script-args]", | |
1721 | "perf script [<options>] <script> [<record-options>] <command>", | |
1722 | "perf script [<options>] <top-script> [script-args]", | |
1723 | NULL | |
1724 | }; | |
3875294f | 1725 | |
b5b87312 TZ |
1726 | setup_scripting(); |
1727 | ||
40cae2b7 | 1728 | argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage, |
b5b87312 TZ |
1729 | PARSE_OPT_STOP_AT_NON_OPTION); |
1730 | ||
f5fc1412 JO |
1731 | file.path = input_name; |
1732 | ||
b5b87312 TZ |
1733 | if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { |
1734 | rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); | |
1735 | if (!rec_script_path) | |
1736 | return cmd_record(argc, argv, NULL); | |
3875294f TZ |
1737 | } |
1738 | ||
b5b87312 TZ |
1739 | if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { |
1740 | rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); | |
1741 | if (!rep_script_path) { | |
3875294f | 1742 | fprintf(stderr, |
b5b87312 | 1743 | "Please specify a valid report script" |
133dc4c3 | 1744 | "(see 'perf script -l' for listing)\n"); |
3875294f TZ |
1745 | return -1; |
1746 | } | |
3875294f TZ |
1747 | } |
1748 | ||
44e668c6 BH |
1749 | /* make sure PERF_EXEC_PATH is set for scripts */ |
1750 | perf_set_argv_exec_path(perf_exec_path()); | |
1751 | ||
b5b87312 | 1752 | if (argc && !script_name && !rec_script_path && !rep_script_path) { |
a0cccc2e | 1753 | int live_pipe[2]; |
b5b87312 | 1754 | int rep_args; |
a0cccc2e TZ |
1755 | pid_t pid; |
1756 | ||
b5b87312 TZ |
1757 | rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); |
1758 | rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); | |
1759 | ||
1760 | if (!rec_script_path && !rep_script_path) { | |
1761 | fprintf(stderr, " Couldn't find script %s\n\n See perf" | |
133dc4c3 IM |
1762 | " script -l for available scripts.\n", argv[0]); |
1763 | usage_with_options(script_usage, options); | |
a0cccc2e TZ |
1764 | } |
1765 | ||
b5b87312 TZ |
1766 | if (is_top_script(argv[0])) { |
1767 | rep_args = argc - 1; | |
1768 | } else { | |
1769 | int rec_args; | |
1770 | ||
1771 | rep_args = has_required_arg(rep_script_path); | |
1772 | rec_args = (argc - 1) - rep_args; | |
1773 | if (rec_args < 0) { | |
1774 | fprintf(stderr, " %s script requires options." | |
133dc4c3 | 1775 | "\n\n See perf script -l for available " |
b5b87312 | 1776 | "scripts and options.\n", argv[0]); |
133dc4c3 | 1777 | usage_with_options(script_usage, options); |
b5b87312 | 1778 | } |
a0cccc2e TZ |
1779 | } |
1780 | ||
1781 | if (pipe(live_pipe) < 0) { | |
1782 | perror("failed to create pipe"); | |
d54b1a9e | 1783 | return -1; |
a0cccc2e TZ |
1784 | } |
1785 | ||
1786 | pid = fork(); | |
1787 | if (pid < 0) { | |
1788 | perror("failed to fork"); | |
d54b1a9e | 1789 | return -1; |
a0cccc2e TZ |
1790 | } |
1791 | ||
1792 | if (!pid) { | |
b5b87312 TZ |
1793 | j = 0; |
1794 | ||
a0cccc2e TZ |
1795 | dup2(live_pipe[1], 1); |
1796 | close(live_pipe[0]); | |
1797 | ||
317df650 RR |
1798 | if (is_top_script(argv[0])) { |
1799 | system_wide = true; | |
1800 | } else if (!system_wide) { | |
d54b1a9e DA |
1801 | if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) { |
1802 | err = -1; | |
1803 | goto out; | |
1804 | } | |
317df650 | 1805 | } |
b5b87312 TZ |
1806 | |
1807 | __argv = malloc((argc + 6) * sizeof(const char *)); | |
d54b1a9e DA |
1808 | if (!__argv) { |
1809 | pr_err("malloc failed\n"); | |
1810 | err = -ENOMEM; | |
1811 | goto out; | |
1812 | } | |
e8719adf | 1813 | |
b5b87312 TZ |
1814 | __argv[j++] = "/bin/sh"; |
1815 | __argv[j++] = rec_script_path; | |
1816 | if (system_wide) | |
1817 | __argv[j++] = "-a"; | |
1818 | __argv[j++] = "-q"; | |
1819 | __argv[j++] = "-o"; | |
1820 | __argv[j++] = "-"; | |
1821 | for (i = rep_args + 1; i < argc; i++) | |
1822 | __argv[j++] = argv[i]; | |
1823 | __argv[j++] = NULL; | |
a0cccc2e TZ |
1824 | |
1825 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 1826 | free(__argv); |
a0cccc2e TZ |
1827 | exit(-1); |
1828 | } | |
1829 | ||
1830 | dup2(live_pipe[0], 0); | |
1831 | close(live_pipe[1]); | |
1832 | ||
b5b87312 | 1833 | __argv = malloc((argc + 4) * sizeof(const char *)); |
d54b1a9e DA |
1834 | if (!__argv) { |
1835 | pr_err("malloc failed\n"); | |
1836 | err = -ENOMEM; | |
1837 | goto out; | |
1838 | } | |
1839 | ||
b5b87312 TZ |
1840 | j = 0; |
1841 | __argv[j++] = "/bin/sh"; | |
1842 | __argv[j++] = rep_script_path; | |
1843 | for (i = 1; i < rep_args + 1; i++) | |
1844 | __argv[j++] = argv[i]; | |
1845 | __argv[j++] = "-i"; | |
1846 | __argv[j++] = "-"; | |
1847 | __argv[j++] = NULL; | |
a0cccc2e TZ |
1848 | |
1849 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 1850 | free(__argv); |
a0cccc2e TZ |
1851 | exit(-1); |
1852 | } | |
1853 | ||
b5b87312 TZ |
1854 | if (rec_script_path) |
1855 | script_path = rec_script_path; | |
1856 | if (rep_script_path) | |
1857 | script_path = rep_script_path; | |
34c86ea9 | 1858 | |
b5b87312 | 1859 | if (script_path) { |
b5b87312 | 1860 | j = 0; |
3875294f | 1861 | |
317df650 RR |
1862 | if (!rec_script_path) |
1863 | system_wide = false; | |
d54b1a9e DA |
1864 | else if (!system_wide) { |
1865 | if (have_cmd(argc - 1, &argv[1]) != 0) { | |
1866 | err = -1; | |
1867 | goto out; | |
1868 | } | |
1869 | } | |
34c86ea9 | 1870 | |
b5b87312 | 1871 | __argv = malloc((argc + 2) * sizeof(const char *)); |
d54b1a9e DA |
1872 | if (!__argv) { |
1873 | pr_err("malloc failed\n"); | |
1874 | err = -ENOMEM; | |
1875 | goto out; | |
1876 | } | |
1877 | ||
34c86ea9 TZ |
1878 | __argv[j++] = "/bin/sh"; |
1879 | __argv[j++] = script_path; | |
1880 | if (system_wide) | |
1881 | __argv[j++] = "-a"; | |
b5b87312 | 1882 | for (i = 2; i < argc; i++) |
34c86ea9 TZ |
1883 | __argv[j++] = argv[i]; |
1884 | __argv[j++] = NULL; | |
3875294f TZ |
1885 | |
1886 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 1887 | free(__argv); |
3875294f TZ |
1888 | exit(-1); |
1889 | } | |
956ffd02 | 1890 | |
cf4fee50 TZ |
1891 | if (!script_name) |
1892 | setup_pager(); | |
5f9c39dc | 1893 | |
6f3e5eda | 1894 | session = perf_session__new(&file, false, &script.tool); |
d8f66248 | 1895 | if (session == NULL) |
52e02834 | 1896 | return -1; |
d8f66248 | 1897 | |
e90debdd JO |
1898 | if (header || header_only) { |
1899 | perf_session__fprintf_info(session, stdout, show_full_info); | |
1900 | if (header_only) | |
6cc870f0 | 1901 | goto out_delete; |
e90debdd JO |
1902 | } |
1903 | ||
0a7e6d1b | 1904 | if (symbol__init(&session->header.env) < 0) |
38520dc3 NK |
1905 | goto out_delete; |
1906 | ||
6f3e5eda | 1907 | script.session = session; |
7322d6c9 | 1908 | script__setup_sample_type(&script); |
6f3e5eda | 1909 | |
7a680eb9 AH |
1910 | session->itrace_synth_opts = &itrace_synth_opts; |
1911 | ||
5d67be97 | 1912 | if (cpu_list) { |
6cc870f0 NK |
1913 | err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap); |
1914 | if (err < 0) | |
1915 | goto out_delete; | |
5d67be97 AB |
1916 | } |
1917 | ||
1424dc96 | 1918 | if (!no_callchain) |
c0230b2b DA |
1919 | symbol_conf.use_callchain = true; |
1920 | else | |
1921 | symbol_conf.use_callchain = false; | |
1922 | ||
9ee67421 ACM |
1923 | if (session->tevent.pevent && |
1924 | pevent_set_function_resolver(session->tevent.pevent, | |
ccb3a829 ACM |
1925 | machine__resolve_kernel_addr, |
1926 | &session->machines.host) < 0) { | |
1927 | pr_err("%s: failed to set libtraceevent function resolver\n", __func__); | |
1928 | return -1; | |
1929 | } | |
1930 | ||
956ffd02 TZ |
1931 | if (generate_script_lang) { |
1932 | struct stat perf_stat; | |
745f43e3 DA |
1933 | int input; |
1934 | ||
2c9e45f7 | 1935 | if (output_set_by_user()) { |
745f43e3 DA |
1936 | fprintf(stderr, |
1937 | "custom fields not supported for generated scripts"); | |
6cc870f0 NK |
1938 | err = -EINVAL; |
1939 | goto out_delete; | |
745f43e3 | 1940 | } |
956ffd02 | 1941 | |
cc9784bd | 1942 | input = open(file.path, O_RDONLY); /* input_name */ |
956ffd02 | 1943 | if (input < 0) { |
6cc870f0 | 1944 | err = -errno; |
956ffd02 | 1945 | perror("failed to open file"); |
6cc870f0 | 1946 | goto out_delete; |
956ffd02 TZ |
1947 | } |
1948 | ||
1949 | err = fstat(input, &perf_stat); | |
1950 | if (err < 0) { | |
1951 | perror("failed to stat file"); | |
6cc870f0 | 1952 | goto out_delete; |
956ffd02 TZ |
1953 | } |
1954 | ||
1955 | if (!perf_stat.st_size) { | |
1956 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
6cc870f0 | 1957 | goto out_delete; |
956ffd02 TZ |
1958 | } |
1959 | ||
1960 | scripting_ops = script_spec__lookup(generate_script_lang); | |
1961 | if (!scripting_ops) { | |
1962 | fprintf(stderr, "invalid language specifier"); | |
6cc870f0 NK |
1963 | err = -ENOENT; |
1964 | goto out_delete; | |
956ffd02 TZ |
1965 | } |
1966 | ||
29f5ffd3 | 1967 | err = scripting_ops->generate_script(session->tevent.pevent, |
da378962 | 1968 | "perf-script"); |
6cc870f0 | 1969 | goto out_delete; |
956ffd02 TZ |
1970 | } |
1971 | ||
1972 | if (script_name) { | |
586bc5cc | 1973 | err = scripting_ops->start_script(script_name, argc, argv); |
956ffd02 | 1974 | if (err) |
6cc870f0 | 1975 | goto out_delete; |
133dc4c3 | 1976 | pr_debug("perf script started with script %s\n\n", script_name); |
6cc870f0 | 1977 | script_started = true; |
956ffd02 TZ |
1978 | } |
1979 | ||
9cbdb702 DA |
1980 | |
1981 | err = perf_session__check_output_opt(session); | |
1982 | if (err < 0) | |
6cc870f0 | 1983 | goto out_delete; |
9cbdb702 | 1984 | |
6f3e5eda | 1985 | err = __cmd_script(&script); |
956ffd02 | 1986 | |
d445dd2a AH |
1987 | flush_scripting(); |
1988 | ||
6cc870f0 | 1989 | out_delete: |
d8f66248 | 1990 | perf_session__delete(session); |
6cc870f0 NK |
1991 | |
1992 | if (script_started) | |
1993 | cleanup_scripting(); | |
956ffd02 TZ |
1994 | out: |
1995 | return err; | |
5f9c39dc | 1996 | } |