]>
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" | |
9 | #include "util/session.h" | |
45694aa7 | 10 | #include "util/tool.h" |
5f9c39dc FW |
11 | #include "util/symbol.h" |
12 | #include "util/thread.h" | |
cf72344d | 13 | #include "util/trace-event.h" |
b7eead86 | 14 | #include "util/util.h" |
1424dc96 DA |
15 | #include "util/evlist.h" |
16 | #include "util/evsel.h" | |
36385be5 | 17 | #include "util/sort.h" |
f5fc1412 | 18 | #include "util/data.h" |
5d67be97 | 19 | #include <linux/bitmap.h> |
5f9c39dc | 20 | |
956ffd02 TZ |
21 | static char const *script_name; |
22 | static char const *generate_script_lang; | |
ffabd99e | 23 | static bool debug_mode; |
e1889d75 | 24 | static u64 last_timestamp; |
6fcf7ddb | 25 | static u64 nr_unordered; |
34c86ea9 | 26 | extern const struct option record_options[]; |
c0230b2b | 27 | static bool no_callchain; |
47390ae2 | 28 | static bool latency_format; |
317df650 | 29 | static bool system_wide; |
5d67be97 AB |
30 | static const char *cpu_list; |
31 | static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); | |
956ffd02 | 32 | |
745f43e3 DA |
33 | enum perf_output_field { |
34 | PERF_OUTPUT_COMM = 1U << 0, | |
35 | PERF_OUTPUT_TID = 1U << 1, | |
36 | PERF_OUTPUT_PID = 1U << 2, | |
37 | PERF_OUTPUT_TIME = 1U << 3, | |
38 | PERF_OUTPUT_CPU = 1U << 4, | |
39 | PERF_OUTPUT_EVNAME = 1U << 5, | |
40 | PERF_OUTPUT_TRACE = 1U << 6, | |
787bef17 DA |
41 | PERF_OUTPUT_IP = 1U << 7, |
42 | PERF_OUTPUT_SYM = 1U << 8, | |
610723f2 | 43 | PERF_OUTPUT_DSO = 1U << 9, |
7cec0922 | 44 | PERF_OUTPUT_ADDR = 1U << 10, |
a978f2ab | 45 | PERF_OUTPUT_SYMOFFSET = 1U << 11, |
745f43e3 DA |
46 | }; |
47 | ||
48 | struct output_option { | |
49 | const char *str; | |
50 | enum perf_output_field field; | |
51 | } all_output_options[] = { | |
52 | {.str = "comm", .field = PERF_OUTPUT_COMM}, | |
53 | {.str = "tid", .field = PERF_OUTPUT_TID}, | |
54 | {.str = "pid", .field = PERF_OUTPUT_PID}, | |
55 | {.str = "time", .field = PERF_OUTPUT_TIME}, | |
56 | {.str = "cpu", .field = PERF_OUTPUT_CPU}, | |
57 | {.str = "event", .field = PERF_OUTPUT_EVNAME}, | |
58 | {.str = "trace", .field = PERF_OUTPUT_TRACE}, | |
787bef17 | 59 | {.str = "ip", .field = PERF_OUTPUT_IP}, |
c0230b2b | 60 | {.str = "sym", .field = PERF_OUTPUT_SYM}, |
610723f2 | 61 | {.str = "dso", .field = PERF_OUTPUT_DSO}, |
7cec0922 | 62 | {.str = "addr", .field = PERF_OUTPUT_ADDR}, |
a978f2ab | 63 | {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET}, |
745f43e3 DA |
64 | }; |
65 | ||
66 | /* default set to maintain compatibility with current format */ | |
2c9e45f7 DA |
67 | static struct { |
68 | bool user_set; | |
9cbdb702 | 69 | bool wildcard_set; |
a6ffaf91 | 70 | unsigned int print_ip_opts; |
2c9e45f7 DA |
71 | u64 fields; |
72 | u64 invalid_fields; | |
73 | } output[PERF_TYPE_MAX] = { | |
74 | ||
75 | [PERF_TYPE_HARDWARE] = { | |
76 | .user_set = false, | |
77 | ||
78 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
79 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
787bef17 | 80 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
610723f2 | 81 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO, |
2c9e45f7 DA |
82 | |
83 | .invalid_fields = PERF_OUTPUT_TRACE, | |
84 | }, | |
85 | ||
86 | [PERF_TYPE_SOFTWARE] = { | |
87 | .user_set = false, | |
88 | ||
89 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
90 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
787bef17 | 91 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
610723f2 | 92 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO, |
2c9e45f7 DA |
93 | |
94 | .invalid_fields = PERF_OUTPUT_TRACE, | |
95 | }, | |
96 | ||
97 | [PERF_TYPE_TRACEPOINT] = { | |
98 | .user_set = false, | |
99 | ||
100 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
101 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
102 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE, | |
103 | }, | |
0817a6a3 AS |
104 | |
105 | [PERF_TYPE_RAW] = { | |
106 | .user_set = false, | |
107 | ||
108 | .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | |
109 | PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | |
787bef17 | 110 | PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP | |
610723f2 | 111 | PERF_OUTPUT_SYM | PERF_OUTPUT_DSO, |
0817a6a3 AS |
112 | |
113 | .invalid_fields = PERF_OUTPUT_TRACE, | |
114 | }, | |
1424dc96 | 115 | }; |
745f43e3 | 116 | |
2c9e45f7 DA |
117 | static bool output_set_by_user(void) |
118 | { | |
119 | int j; | |
120 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
121 | if (output[j].user_set) | |
122 | return true; | |
123 | } | |
124 | return false; | |
125 | } | |
745f43e3 | 126 | |
9cbdb702 DA |
127 | static const char *output_field2str(enum perf_output_field field) |
128 | { | |
129 | int i, imax = ARRAY_SIZE(all_output_options); | |
130 | const char *str = ""; | |
131 | ||
132 | for (i = 0; i < imax; ++i) { | |
133 | if (all_output_options[i].field == field) { | |
134 | str = all_output_options[i].str; | |
135 | break; | |
136 | } | |
137 | } | |
138 | return str; | |
139 | } | |
140 | ||
2c9e45f7 | 141 | #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x) |
1424dc96 | 142 | |
5bff01f6 ACM |
143 | static int perf_evsel__check_stype(struct perf_evsel *evsel, |
144 | u64 sample_type, const char *sample_msg, | |
145 | enum perf_output_field field) | |
1424dc96 | 146 | { |
5bff01f6 | 147 | struct perf_event_attr *attr = &evsel->attr; |
9cbdb702 DA |
148 | int type = attr->type; |
149 | const char *evname; | |
150 | ||
151 | if (attr->sample_type & sample_type) | |
152 | return 0; | |
153 | ||
154 | if (output[type].user_set) { | |
5bff01f6 | 155 | evname = perf_evsel__name(evsel); |
9cbdb702 DA |
156 | pr_err("Samples for '%s' event do not have %s attribute set. " |
157 | "Cannot print '%s' field.\n", | |
158 | evname, sample_msg, output_field2str(field)); | |
159 | return -1; | |
160 | } | |
161 | ||
162 | /* user did not ask for it explicitly so remove from the default list */ | |
163 | output[type].fields &= ~field; | |
5bff01f6 | 164 | evname = perf_evsel__name(evsel); |
9cbdb702 DA |
165 | pr_debug("Samples for '%s' event do not have %s attribute set. " |
166 | "Skipping '%s' field.\n", | |
167 | evname, sample_msg, output_field2str(field)); | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | static int perf_evsel__check_attr(struct perf_evsel *evsel, | |
173 | struct perf_session *session) | |
174 | { | |
175 | struct perf_event_attr *attr = &evsel->attr; | |
176 | ||
1424dc96 DA |
177 | if (PRINT_FIELD(TRACE) && |
178 | !perf_session__has_traces(session, "record -R")) | |
179 | return -EINVAL; | |
180 | ||
787bef17 | 181 | if (PRINT_FIELD(IP)) { |
5bff01f6 ACM |
182 | if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", |
183 | PERF_OUTPUT_IP)) | |
1424dc96 | 184 | return -EINVAL; |
9cbdb702 | 185 | |
1424dc96 | 186 | if (!no_callchain && |
9cbdb702 | 187 | !(attr->sample_type & PERF_SAMPLE_CALLCHAIN)) |
1424dc96 DA |
188 | symbol_conf.use_callchain = false; |
189 | } | |
7cec0922 DA |
190 | |
191 | if (PRINT_FIELD(ADDR) && | |
5bff01f6 ACM |
192 | perf_evsel__check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", |
193 | PERF_OUTPUT_ADDR)) | |
7cec0922 DA |
194 | return -EINVAL; |
195 | ||
196 | if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { | |
197 | pr_err("Display of symbols requested but neither sample IP nor " | |
198 | "sample address\nis selected. Hence, no addresses to convert " | |
199 | "to symbols.\n"); | |
787bef17 DA |
200 | return -EINVAL; |
201 | } | |
a978f2ab AN |
202 | if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) { |
203 | pr_err("Display of offsets requested but symbol is not" | |
204 | "selected.\n"); | |
205 | return -EINVAL; | |
206 | } | |
7cec0922 DA |
207 | if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { |
208 | pr_err("Display of DSO requested but neither sample IP nor " | |
209 | "sample address\nis selected. Hence, no addresses to convert " | |
210 | "to DSO.\n"); | |
610723f2 DA |
211 | return -EINVAL; |
212 | } | |
1424dc96 DA |
213 | |
214 | if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) && | |
5bff01f6 ACM |
215 | perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", |
216 | PERF_OUTPUT_TID|PERF_OUTPUT_PID)) | |
1424dc96 | 217 | return -EINVAL; |
1424dc96 DA |
218 | |
219 | if (PRINT_FIELD(TIME) && | |
5bff01f6 ACM |
220 | perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", |
221 | PERF_OUTPUT_TIME)) | |
1424dc96 | 222 | return -EINVAL; |
1424dc96 DA |
223 | |
224 | if (PRINT_FIELD(CPU) && | |
5bff01f6 ACM |
225 | perf_evsel__check_stype(evsel, PERF_SAMPLE_CPU, "CPU", |
226 | PERF_OUTPUT_CPU)) | |
1424dc96 | 227 | return -EINVAL; |
9cbdb702 DA |
228 | |
229 | return 0; | |
230 | } | |
231 | ||
232 | /* | |
233 | * verify all user requested events exist and the samples | |
234 | * have the expected data | |
235 | */ | |
236 | static int perf_session__check_output_opt(struct perf_session *session) | |
237 | { | |
238 | int j; | |
239 | struct perf_evsel *evsel; | |
a6ffaf91 | 240 | struct perf_event_attr *attr; |
9cbdb702 DA |
241 | |
242 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
243 | evsel = perf_session__find_first_evtype(session, j); | |
244 | ||
245 | /* | |
246 | * even if fields is set to 0 (ie., show nothing) event must | |
247 | * exist if user explicitly includes it on the command line | |
248 | */ | |
249 | if (!evsel && output[j].user_set && !output[j].wildcard_set) { | |
250 | pr_err("%s events do not exist. " | |
251 | "Remove corresponding -f option to proceed.\n", | |
252 | event_type(j)); | |
253 | return -1; | |
254 | } | |
255 | ||
256 | if (evsel && output[j].fields && | |
257 | perf_evsel__check_attr(evsel, session)) | |
258 | return -1; | |
a6ffaf91 DA |
259 | |
260 | if (evsel == NULL) | |
261 | continue; | |
262 | ||
263 | attr = &evsel->attr; | |
264 | ||
265 | output[j].print_ip_opts = 0; | |
266 | if (PRINT_FIELD(IP)) | |
267 | output[j].print_ip_opts |= PRINT_IP_OPT_IP; | |
268 | ||
269 | if (PRINT_FIELD(SYM)) | |
270 | output[j].print_ip_opts |= PRINT_IP_OPT_SYM; | |
271 | ||
272 | if (PRINT_FIELD(DSO)) | |
273 | output[j].print_ip_opts |= PRINT_IP_OPT_DSO; | |
274 | ||
275 | if (PRINT_FIELD(SYMOFFSET)) | |
276 | output[j].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET; | |
1424dc96 DA |
277 | } |
278 | ||
279 | return 0; | |
280 | } | |
745f43e3 | 281 | |
fcf65bf1 | 282 | static void print_sample_start(struct perf_sample *sample, |
1424dc96 | 283 | struct thread *thread, |
5bff01f6 | 284 | struct perf_evsel *evsel) |
c70c94b4 | 285 | { |
5bff01f6 | 286 | struct perf_event_attr *attr = &evsel->attr; |
c70c94b4 DA |
287 | const char *evname = NULL; |
288 | unsigned long secs; | |
289 | unsigned long usecs; | |
745f43e3 DA |
290 | unsigned long long nsecs; |
291 | ||
292 | if (PRINT_FIELD(COMM)) { | |
293 | if (latency_format) | |
294 | printf("%8.8s ", thread->comm); | |
787bef17 | 295 | else if (PRINT_FIELD(IP) && symbol_conf.use_callchain) |
c0230b2b | 296 | printf("%s ", thread->comm); |
745f43e3 DA |
297 | else |
298 | printf("%16s ", thread->comm); | |
299 | } | |
c70c94b4 | 300 | |
745f43e3 DA |
301 | if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) |
302 | printf("%5d/%-5d ", sample->pid, sample->tid); | |
303 | else if (PRINT_FIELD(PID)) | |
304 | printf("%5d ", sample->pid); | |
305 | else if (PRINT_FIELD(TID)) | |
306 | printf("%5d ", sample->tid); | |
307 | ||
308 | if (PRINT_FIELD(CPU)) { | |
309 | if (latency_format) | |
310 | printf("%3d ", sample->cpu); | |
311 | else | |
312 | printf("[%03d] ", sample->cpu); | |
313 | } | |
c70c94b4 | 314 | |
745f43e3 DA |
315 | if (PRINT_FIELD(TIME)) { |
316 | nsecs = sample->time; | |
317 | secs = nsecs / NSECS_PER_SEC; | |
318 | nsecs -= secs * NSECS_PER_SEC; | |
319 | usecs = nsecs / NSECS_PER_USEC; | |
320 | printf("%5lu.%06lu: ", secs, usecs); | |
321 | } | |
c70c94b4 | 322 | |
745f43e3 | 323 | if (PRINT_FIELD(EVNAME)) { |
fcf65bf1 | 324 | evname = perf_evsel__name(evsel); |
547a92e0 | 325 | printf("%s: ", evname ? evname : "[unknown]"); |
745f43e3 | 326 | } |
c70c94b4 DA |
327 | } |
328 | ||
95582596 AN |
329 | static bool is_bts_event(struct perf_event_attr *attr) |
330 | { | |
331 | return ((attr->type == PERF_TYPE_HARDWARE) && | |
332 | (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && | |
333 | (attr->sample_period == 1)); | |
334 | } | |
335 | ||
7cec0922 DA |
336 | static bool sample_addr_correlates_sym(struct perf_event_attr *attr) |
337 | { | |
338 | if ((attr->type == PERF_TYPE_SOFTWARE) && | |
339 | ((attr->config == PERF_COUNT_SW_PAGE_FAULTS) || | |
340 | (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN) || | |
341 | (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))) | |
342 | return true; | |
343 | ||
95582596 AN |
344 | if (is_bts_event(attr)) |
345 | return true; | |
346 | ||
7cec0922 DA |
347 | return false; |
348 | } | |
349 | ||
350 | static void print_sample_addr(union perf_event *event, | |
351 | struct perf_sample *sample, | |
743eb868 | 352 | struct machine *machine, |
7cec0922 DA |
353 | struct thread *thread, |
354 | struct perf_event_attr *attr) | |
355 | { | |
356 | struct addr_location al; | |
357 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | |
7cec0922 DA |
358 | |
359 | printf("%16" PRIx64, sample->addr); | |
360 | ||
361 | if (!sample_addr_correlates_sym(attr)) | |
362 | return; | |
363 | ||
743eb868 | 364 | thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION, |
326f59bf | 365 | sample->addr, &al); |
7cec0922 | 366 | if (!al.map) |
743eb868 | 367 | thread__find_addr_map(thread, machine, cpumode, MAP__VARIABLE, |
326f59bf | 368 | sample->addr, &al); |
7cec0922 DA |
369 | |
370 | al.cpu = sample->cpu; | |
371 | al.sym = NULL; | |
372 | ||
373 | if (al.map) | |
374 | al.sym = map__find_symbol(al.map, al.addr, NULL); | |
375 | ||
376 | if (PRINT_FIELD(SYM)) { | |
547a92e0 | 377 | printf(" "); |
a978f2ab AN |
378 | if (PRINT_FIELD(SYMOFFSET)) |
379 | symbol__fprintf_symname_offs(al.sym, &al, stdout); | |
380 | else | |
381 | symbol__fprintf_symname(al.sym, stdout); | |
7cec0922 DA |
382 | } |
383 | ||
384 | if (PRINT_FIELD(DSO)) { | |
547a92e0 AN |
385 | printf(" ("); |
386 | map__fprintf_dsoname(al.map, stdout); | |
387 | printf(")"); | |
7cec0922 DA |
388 | } |
389 | } | |
390 | ||
95582596 AN |
391 | static void print_sample_bts(union perf_event *event, |
392 | struct perf_sample *sample, | |
393 | struct perf_evsel *evsel, | |
394 | struct machine *machine, | |
395 | struct thread *thread) | |
396 | { | |
397 | struct perf_event_attr *attr = &evsel->attr; | |
398 | ||
399 | /* print branch_from information */ | |
400 | if (PRINT_FIELD(IP)) { | |
401 | if (!symbol_conf.use_callchain) | |
402 | printf(" "); | |
403 | else | |
404 | printf("\n"); | |
71ad0f5e | 405 | perf_evsel__print_ip(evsel, event, sample, machine, |
307cbb92 DA |
406 | output[attr->type].print_ip_opts, |
407 | PERF_MAX_STACK_DEPTH); | |
95582596 AN |
408 | } |
409 | ||
410 | printf(" => "); | |
411 | ||
412 | /* print branch_to information */ | |
243be3dd AH |
413 | if (PRINT_FIELD(ADDR) || |
414 | ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) && | |
415 | !output[attr->type].user_set)) | |
95582596 AN |
416 | print_sample_addr(event, sample, machine, thread, attr); |
417 | ||
418 | printf("\n"); | |
419 | } | |
420 | ||
97822433 ACM |
421 | static void process_event(union perf_event *event, struct perf_sample *sample, |
422 | struct perf_evsel *evsel, struct machine *machine, | |
2eaa1b40 DA |
423 | struct thread *thread, |
424 | struct addr_location *al __maybe_unused) | |
be6d842a | 425 | { |
9e69c210 | 426 | struct perf_event_attr *attr = &evsel->attr; |
1424dc96 | 427 | |
2c9e45f7 | 428 | if (output[attr->type].fields == 0) |
1424dc96 DA |
429 | return; |
430 | ||
fcf65bf1 | 431 | print_sample_start(sample, thread, evsel); |
745f43e3 | 432 | |
95582596 AN |
433 | if (is_bts_event(attr)) { |
434 | print_sample_bts(event, sample, evsel, machine, thread); | |
435 | return; | |
436 | } | |
437 | ||
745f43e3 | 438 | if (PRINT_FIELD(TRACE)) |
fcf65bf1 ACM |
439 | event_format__print(evsel->tp_format, sample->cpu, |
440 | sample->raw_data, sample->raw_size); | |
7cec0922 | 441 | if (PRINT_FIELD(ADDR)) |
743eb868 | 442 | print_sample_addr(event, sample, machine, thread, attr); |
7cec0922 | 443 | |
787bef17 | 444 | if (PRINT_FIELD(IP)) { |
c0230b2b DA |
445 | if (!symbol_conf.use_callchain) |
446 | printf(" "); | |
447 | else | |
448 | printf("\n"); | |
a6ffaf91 | 449 | |
71ad0f5e | 450 | perf_evsel__print_ip(evsel, event, sample, machine, |
307cbb92 DA |
451 | output[attr->type].print_ip_opts, |
452 | PERF_MAX_STACK_DEPTH); | |
c0230b2b DA |
453 | } |
454 | ||
c70c94b4 | 455 | printf("\n"); |
be6d842a DA |
456 | } |
457 | ||
1d037ca1 IT |
458 | static int default_start_script(const char *script __maybe_unused, |
459 | int argc __maybe_unused, | |
460 | const char **argv __maybe_unused) | |
956ffd02 TZ |
461 | { |
462 | return 0; | |
463 | } | |
464 | ||
465 | static int default_stop_script(void) | |
466 | { | |
467 | return 0; | |
468 | } | |
469 | ||
1d037ca1 IT |
470 | static int default_generate_script(struct pevent *pevent __maybe_unused, |
471 | const char *outfile __maybe_unused) | |
956ffd02 TZ |
472 | { |
473 | return 0; | |
474 | } | |
475 | ||
476 | static struct scripting_ops default_scripting_ops = { | |
477 | .start_script = default_start_script, | |
478 | .stop_script = default_stop_script, | |
be6d842a | 479 | .process_event = process_event, |
956ffd02 TZ |
480 | .generate_script = default_generate_script, |
481 | }; | |
482 | ||
483 | static struct scripting_ops *scripting_ops; | |
484 | ||
485 | static void setup_scripting(void) | |
486 | { | |
16c632de | 487 | setup_perl_scripting(); |
7e4b21b8 | 488 | setup_python_scripting(); |
16c632de | 489 | |
956ffd02 TZ |
490 | scripting_ops = &default_scripting_ops; |
491 | } | |
492 | ||
493 | static int cleanup_scripting(void) | |
494 | { | |
133dc4c3 | 495 | pr_debug("\nperf script stopped\n"); |
3824a4e8 | 496 | |
956ffd02 TZ |
497 | return scripting_ops->stop_script(); |
498 | } | |
499 | ||
1d037ca1 | 500 | static int process_sample_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 501 | union perf_event *event, |
8115d60c | 502 | struct perf_sample *sample, |
9e69c210 | 503 | struct perf_evsel *evsel, |
743eb868 | 504 | struct machine *machine) |
5f9c39dc | 505 | { |
e7984b7b | 506 | struct addr_location al; |
ef89325f AH |
507 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
508 | sample->tid); | |
6ddf259d | 509 | |
5f9c39dc | 510 | if (thread == NULL) { |
6beba7ad ACM |
511 | pr_debug("problem processing %d event, skipping it.\n", |
512 | event->header.type); | |
5f9c39dc FW |
513 | return -1; |
514 | } | |
515 | ||
1424dc96 DA |
516 | if (debug_mode) { |
517 | if (sample->time < last_timestamp) { | |
518 | pr_err("Samples misordered, previous: %" PRIu64 | |
519 | " this: %" PRIu64 "\n", last_timestamp, | |
520 | sample->time); | |
521 | nr_unordered++; | |
e1889d75 | 522 | } |
1424dc96 DA |
523 | last_timestamp = sample->time; |
524 | return 0; | |
5f9c39dc | 525 | } |
5d67be97 | 526 | |
e44baa3e | 527 | if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) { |
e7984b7b DA |
528 | pr_err("problem processing %d event, skipping it.\n", |
529 | event->header.type); | |
530 | return -1; | |
531 | } | |
532 | ||
533 | if (al.filtered) | |
534 | return 0; | |
535 | ||
5d67be97 AB |
536 | if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) |
537 | return 0; | |
538 | ||
2eaa1b40 | 539 | scripting_ops->process_event(event, sample, evsel, machine, thread, &al); |
5f9c39dc | 540 | |
743eb868 | 541 | evsel->hists.stats.total_period += sample->period; |
5f9c39dc FW |
542 | return 0; |
543 | } | |
544 | ||
97822433 ACM |
545 | static struct perf_tool perf_script = { |
546 | .sample = process_sample_event, | |
547 | .mmap = perf_event__process_mmap, | |
5c5e854b | 548 | .mmap2 = perf_event__process_mmap2, |
97822433 | 549 | .comm = perf_event__process_comm, |
f62d3f0f ACM |
550 | .exit = perf_event__process_exit, |
551 | .fork = perf_event__process_fork, | |
97822433 | 552 | .attr = perf_event__process_attr, |
97822433 ACM |
553 | .tracing_data = perf_event__process_tracing_data, |
554 | .build_id = perf_event__process_build_id, | |
555 | .ordered_samples = true, | |
556 | .ordering_requires_timestamps = true, | |
016e92fb FW |
557 | }; |
558 | ||
1d037ca1 | 559 | static void sig_handler(int sig __maybe_unused) |
c239da3b TZ |
560 | { |
561 | session_done = 1; | |
562 | } | |
563 | ||
133dc4c3 | 564 | static int __cmd_script(struct perf_session *session) |
5f9c39dc | 565 | { |
6fcf7ddb FW |
566 | int ret; |
567 | ||
c239da3b TZ |
568 | signal(SIGINT, sig_handler); |
569 | ||
97822433 | 570 | ret = perf_session__process_events(session, &perf_script); |
6fcf7ddb | 571 | |
6d8afb56 | 572 | if (debug_mode) |
9486aa38 | 573 | pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); |
6fcf7ddb FW |
574 | |
575 | return ret; | |
5f9c39dc FW |
576 | } |
577 | ||
956ffd02 TZ |
578 | struct script_spec { |
579 | struct list_head node; | |
580 | struct scripting_ops *ops; | |
581 | char spec[0]; | |
582 | }; | |
583 | ||
eccdfe2d | 584 | static LIST_HEAD(script_specs); |
956ffd02 TZ |
585 | |
586 | static struct script_spec *script_spec__new(const char *spec, | |
587 | struct scripting_ops *ops) | |
588 | { | |
589 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); | |
590 | ||
591 | if (s != NULL) { | |
592 | strcpy(s->spec, spec); | |
593 | s->ops = ops; | |
594 | } | |
595 | ||
596 | return s; | |
597 | } | |
598 | ||
956ffd02 TZ |
599 | static void script_spec__add(struct script_spec *s) |
600 | { | |
601 | list_add_tail(&s->node, &script_specs); | |
602 | } | |
603 | ||
604 | static struct script_spec *script_spec__find(const char *spec) | |
605 | { | |
606 | struct script_spec *s; | |
607 | ||
608 | list_for_each_entry(s, &script_specs, node) | |
609 | if (strcasecmp(s->spec, spec) == 0) | |
610 | return s; | |
611 | return NULL; | |
612 | } | |
613 | ||
614 | static struct script_spec *script_spec__findnew(const char *spec, | |
615 | struct scripting_ops *ops) | |
616 | { | |
617 | struct script_spec *s = script_spec__find(spec); | |
618 | ||
619 | if (s) | |
620 | return s; | |
621 | ||
622 | s = script_spec__new(spec, ops); | |
623 | if (!s) | |
466e2876 | 624 | return NULL; |
956ffd02 TZ |
625 | |
626 | script_spec__add(s); | |
627 | ||
628 | return s; | |
956ffd02 TZ |
629 | } |
630 | ||
631 | int script_spec_register(const char *spec, struct scripting_ops *ops) | |
632 | { | |
633 | struct script_spec *s; | |
634 | ||
635 | s = script_spec__find(spec); | |
636 | if (s) | |
637 | return -1; | |
638 | ||
639 | s = script_spec__findnew(spec, ops); | |
640 | if (!s) | |
641 | return -1; | |
642 | ||
643 | return 0; | |
644 | } | |
645 | ||
646 | static struct scripting_ops *script_spec__lookup(const char *spec) | |
647 | { | |
648 | struct script_spec *s = script_spec__find(spec); | |
649 | if (!s) | |
650 | return NULL; | |
651 | ||
652 | return s->ops; | |
653 | } | |
654 | ||
655 | static void list_available_languages(void) | |
656 | { | |
657 | struct script_spec *s; | |
658 | ||
659 | fprintf(stderr, "\n"); | |
660 | fprintf(stderr, "Scripting language extensions (used in " | |
133dc4c3 | 661 | "perf script -s [spec:]script.[spec]):\n\n"); |
956ffd02 TZ |
662 | |
663 | list_for_each_entry(s, &script_specs, node) | |
664 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); | |
665 | ||
666 | fprintf(stderr, "\n"); | |
667 | } | |
668 | ||
1d037ca1 IT |
669 | static int parse_scriptname(const struct option *opt __maybe_unused, |
670 | const char *str, int unset __maybe_unused) | |
956ffd02 TZ |
671 | { |
672 | char spec[PATH_MAX]; | |
673 | const char *script, *ext; | |
674 | int len; | |
675 | ||
f526d68b | 676 | if (strcmp(str, "lang") == 0) { |
956ffd02 | 677 | list_available_languages(); |
f526d68b | 678 | exit(0); |
956ffd02 TZ |
679 | } |
680 | ||
681 | script = strchr(str, ':'); | |
682 | if (script) { | |
683 | len = script - str; | |
684 | if (len >= PATH_MAX) { | |
685 | fprintf(stderr, "invalid language specifier"); | |
686 | return -1; | |
687 | } | |
688 | strncpy(spec, str, len); | |
689 | spec[len] = '\0'; | |
690 | scripting_ops = script_spec__lookup(spec); | |
691 | if (!scripting_ops) { | |
692 | fprintf(stderr, "invalid language specifier"); | |
693 | return -1; | |
694 | } | |
695 | script++; | |
696 | } else { | |
697 | script = str; | |
d1e95bb5 | 698 | ext = strrchr(script, '.'); |
956ffd02 TZ |
699 | if (!ext) { |
700 | fprintf(stderr, "invalid script extension"); | |
701 | return -1; | |
702 | } | |
703 | scripting_ops = script_spec__lookup(++ext); | |
704 | if (!scripting_ops) { | |
705 | fprintf(stderr, "invalid script extension"); | |
706 | return -1; | |
707 | } | |
708 | } | |
709 | ||
710 | script_name = strdup(script); | |
711 | ||
712 | return 0; | |
713 | } | |
714 | ||
1d037ca1 IT |
715 | static int parse_output_fields(const struct option *opt __maybe_unused, |
716 | const char *arg, int unset __maybe_unused) | |
745f43e3 DA |
717 | { |
718 | char *tok; | |
50ca19ae | 719 | int i, imax = ARRAY_SIZE(all_output_options); |
2c9e45f7 | 720 | int j; |
745f43e3 DA |
721 | int rc = 0; |
722 | char *str = strdup(arg); | |
1424dc96 | 723 | int type = -1; |
745f43e3 DA |
724 | |
725 | if (!str) | |
726 | return -ENOMEM; | |
727 | ||
2c9e45f7 DA |
728 | /* first word can state for which event type the user is specifying |
729 | * the fields. If no type exists, the specified fields apply to all | |
730 | * event types found in the file minus the invalid fields for a type. | |
1424dc96 | 731 | */ |
2c9e45f7 DA |
732 | tok = strchr(str, ':'); |
733 | if (tok) { | |
734 | *tok = '\0'; | |
735 | tok++; | |
736 | if (!strcmp(str, "hw")) | |
737 | type = PERF_TYPE_HARDWARE; | |
738 | else if (!strcmp(str, "sw")) | |
739 | type = PERF_TYPE_SOFTWARE; | |
740 | else if (!strcmp(str, "trace")) | |
741 | type = PERF_TYPE_TRACEPOINT; | |
0817a6a3 AS |
742 | else if (!strcmp(str, "raw")) |
743 | type = PERF_TYPE_RAW; | |
2c9e45f7 DA |
744 | else { |
745 | fprintf(stderr, "Invalid event type in field string.\n"); | |
38efb539 RR |
746 | rc = -EINVAL; |
747 | goto out; | |
2c9e45f7 DA |
748 | } |
749 | ||
750 | if (output[type].user_set) | |
751 | pr_warning("Overriding previous field request for %s events.\n", | |
752 | event_type(type)); | |
753 | ||
754 | output[type].fields = 0; | |
755 | output[type].user_set = true; | |
9cbdb702 | 756 | output[type].wildcard_set = false; |
2c9e45f7 DA |
757 | |
758 | } else { | |
759 | tok = str; | |
760 | if (strlen(str) == 0) { | |
761 | fprintf(stderr, | |
762 | "Cannot set fields to 'none' for all event types.\n"); | |
763 | rc = -EINVAL; | |
764 | goto out; | |
765 | } | |
766 | ||
767 | if (output_set_by_user()) | |
768 | pr_warning("Overriding previous field request for all events.\n"); | |
769 | ||
770 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
771 | output[j].fields = 0; | |
772 | output[j].user_set = true; | |
9cbdb702 | 773 | output[j].wildcard_set = true; |
2c9e45f7 | 774 | } |
745f43e3 DA |
775 | } |
776 | ||
2c9e45f7 DA |
777 | tok = strtok(tok, ","); |
778 | while (tok) { | |
745f43e3 | 779 | for (i = 0; i < imax; ++i) { |
2c9e45f7 | 780 | if (strcmp(tok, all_output_options[i].str) == 0) |
745f43e3 | 781 | break; |
745f43e3 DA |
782 | } |
783 | if (i == imax) { | |
2c9e45f7 | 784 | fprintf(stderr, "Invalid field requested.\n"); |
745f43e3 | 785 | rc = -EINVAL; |
2c9e45f7 | 786 | goto out; |
745f43e3 DA |
787 | } |
788 | ||
2c9e45f7 DA |
789 | if (type == -1) { |
790 | /* add user option to all events types for | |
791 | * which it is valid | |
792 | */ | |
793 | for (j = 0; j < PERF_TYPE_MAX; ++j) { | |
794 | if (output[j].invalid_fields & all_output_options[i].field) { | |
795 | pr_warning("\'%s\' not valid for %s events. Ignoring.\n", | |
796 | all_output_options[i].str, event_type(j)); | |
797 | } else | |
798 | output[j].fields |= all_output_options[i].field; | |
799 | } | |
800 | } else { | |
801 | if (output[type].invalid_fields & all_output_options[i].field) { | |
802 | fprintf(stderr, "\'%s\' not valid for %s events.\n", | |
803 | all_output_options[i].str, event_type(type)); | |
804 | ||
805 | rc = -EINVAL; | |
806 | goto out; | |
807 | } | |
808 | output[type].fields |= all_output_options[i].field; | |
809 | } | |
810 | ||
811 | tok = strtok(NULL, ","); | |
745f43e3 DA |
812 | } |
813 | ||
2c9e45f7 DA |
814 | if (type >= 0) { |
815 | if (output[type].fields == 0) { | |
816 | pr_debug("No fields requested for %s type. " | |
817 | "Events will not be displayed.\n", event_type(type)); | |
818 | } | |
819 | } | |
745f43e3 | 820 | |
2c9e45f7 | 821 | out: |
745f43e3 DA |
822 | free(str); |
823 | return rc; | |
824 | } | |
825 | ||
008f29d3 SB |
826 | /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */ |
827 | static int is_directory(const char *base_path, const struct dirent *dent) | |
828 | { | |
829 | char path[PATH_MAX]; | |
830 | struct stat st; | |
831 | ||
832 | sprintf(path, "%s/%s", base_path, dent->d_name); | |
833 | if (stat(path, &st)) | |
834 | return 0; | |
835 | ||
836 | return S_ISDIR(st.st_mode); | |
837 | } | |
838 | ||
839 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\ | |
4b9c0c59 TZ |
840 | while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ |
841 | lang_next) \ | |
008f29d3 SB |
842 | if ((lang_dirent.d_type == DT_DIR || \ |
843 | (lang_dirent.d_type == DT_UNKNOWN && \ | |
844 | is_directory(scripts_path, &lang_dirent))) && \ | |
4b9c0c59 TZ |
845 | (strcmp(lang_dirent.d_name, ".")) && \ |
846 | (strcmp(lang_dirent.d_name, ".."))) | |
847 | ||
008f29d3 | 848 | #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\ |
4b9c0c59 TZ |
849 | while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ |
850 | script_next) \ | |
008f29d3 SB |
851 | if (script_dirent.d_type != DT_DIR && \ |
852 | (script_dirent.d_type != DT_UNKNOWN || \ | |
853 | !is_directory(lang_path, &script_dirent))) | |
4b9c0c59 TZ |
854 | |
855 | ||
856 | #define RECORD_SUFFIX "-record" | |
857 | #define REPORT_SUFFIX "-report" | |
858 | ||
859 | struct script_desc { | |
860 | struct list_head node; | |
861 | char *name; | |
862 | char *half_liner; | |
863 | char *args; | |
864 | }; | |
865 | ||
eccdfe2d | 866 | static LIST_HEAD(script_descs); |
4b9c0c59 TZ |
867 | |
868 | static struct script_desc *script_desc__new(const char *name) | |
869 | { | |
870 | struct script_desc *s = zalloc(sizeof(*s)); | |
871 | ||
b5b87312 | 872 | if (s != NULL && name) |
4b9c0c59 TZ |
873 | s->name = strdup(name); |
874 | ||
875 | return s; | |
876 | } | |
877 | ||
878 | static void script_desc__delete(struct script_desc *s) | |
879 | { | |
880 | free(s->name); | |
e8719adf TZ |
881 | free(s->half_liner); |
882 | free(s->args); | |
4b9c0c59 TZ |
883 | free(s); |
884 | } | |
885 | ||
886 | static void script_desc__add(struct script_desc *s) | |
887 | { | |
888 | list_add_tail(&s->node, &script_descs); | |
889 | } | |
890 | ||
891 | static struct script_desc *script_desc__find(const char *name) | |
892 | { | |
893 | struct script_desc *s; | |
894 | ||
895 | list_for_each_entry(s, &script_descs, node) | |
896 | if (strcasecmp(s->name, name) == 0) | |
897 | return s; | |
898 | return NULL; | |
899 | } | |
900 | ||
901 | static struct script_desc *script_desc__findnew(const char *name) | |
902 | { | |
903 | struct script_desc *s = script_desc__find(name); | |
904 | ||
905 | if (s) | |
906 | return s; | |
907 | ||
908 | s = script_desc__new(name); | |
909 | if (!s) | |
910 | goto out_delete_desc; | |
911 | ||
912 | script_desc__add(s); | |
913 | ||
914 | return s; | |
915 | ||
916 | out_delete_desc: | |
917 | script_desc__delete(s); | |
918 | ||
919 | return NULL; | |
920 | } | |
921 | ||
965bb6be | 922 | static const char *ends_with(const char *str, const char *suffix) |
4b9c0c59 TZ |
923 | { |
924 | size_t suffix_len = strlen(suffix); | |
965bb6be | 925 | const char *p = str; |
4b9c0c59 TZ |
926 | |
927 | if (strlen(str) > suffix_len) { | |
928 | p = str + strlen(str) - suffix_len; | |
929 | if (!strncmp(p, suffix, suffix_len)) | |
930 | return p; | |
931 | } | |
932 | ||
933 | return NULL; | |
934 | } | |
935 | ||
4b9c0c59 TZ |
936 | static int read_script_info(struct script_desc *desc, const char *filename) |
937 | { | |
938 | char line[BUFSIZ], *p; | |
939 | FILE *fp; | |
940 | ||
941 | fp = fopen(filename, "r"); | |
942 | if (!fp) | |
943 | return -1; | |
944 | ||
945 | while (fgets(line, sizeof(line), fp)) { | |
946 | p = ltrim(line); | |
947 | if (strlen(p) == 0) | |
948 | continue; | |
949 | if (*p != '#') | |
950 | continue; | |
951 | p++; | |
952 | if (strlen(p) && *p == '!') | |
953 | continue; | |
954 | ||
955 | p = ltrim(p); | |
956 | if (strlen(p) && p[strlen(p) - 1] == '\n') | |
957 | p[strlen(p) - 1] = '\0'; | |
958 | ||
959 | if (!strncmp(p, "description:", strlen("description:"))) { | |
960 | p += strlen("description:"); | |
961 | desc->half_liner = strdup(ltrim(p)); | |
962 | continue; | |
963 | } | |
964 | ||
965 | if (!strncmp(p, "args:", strlen("args:"))) { | |
966 | p += strlen("args:"); | |
967 | desc->args = strdup(ltrim(p)); | |
968 | continue; | |
969 | } | |
970 | } | |
971 | ||
972 | fclose(fp); | |
973 | ||
974 | return 0; | |
975 | } | |
976 | ||
38efb539 RR |
977 | static char *get_script_root(struct dirent *script_dirent, const char *suffix) |
978 | { | |
979 | char *script_root, *str; | |
980 | ||
981 | script_root = strdup(script_dirent->d_name); | |
982 | if (!script_root) | |
983 | return NULL; | |
984 | ||
985 | str = (char *)ends_with(script_root, suffix); | |
986 | if (!str) { | |
987 | free(script_root); | |
988 | return NULL; | |
989 | } | |
990 | ||
991 | *str = '\0'; | |
992 | return script_root; | |
993 | } | |
994 | ||
1d037ca1 IT |
995 | static int list_available_scripts(const struct option *opt __maybe_unused, |
996 | const char *s __maybe_unused, | |
997 | int unset __maybe_unused) | |
4b9c0c59 TZ |
998 | { |
999 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
1000 | char scripts_path[MAXPATHLEN]; | |
1001 | DIR *scripts_dir, *lang_dir; | |
1002 | char script_path[MAXPATHLEN]; | |
1003 | char lang_path[MAXPATHLEN]; | |
1004 | struct script_desc *desc; | |
1005 | char first_half[BUFSIZ]; | |
1006 | char *script_root; | |
4b9c0c59 TZ |
1007 | |
1008 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | |
1009 | ||
1010 | scripts_dir = opendir(scripts_path); | |
1011 | if (!scripts_dir) | |
1012 | return -1; | |
1013 | ||
008f29d3 | 1014 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
4b9c0c59 TZ |
1015 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
1016 | lang_dirent.d_name); | |
1017 | lang_dir = opendir(lang_path); | |
1018 | if (!lang_dir) | |
1019 | continue; | |
1020 | ||
008f29d3 | 1021 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
38efb539 RR |
1022 | script_root = get_script_root(&script_dirent, REPORT_SUFFIX); |
1023 | if (script_root) { | |
4b9c0c59 TZ |
1024 | desc = script_desc__findnew(script_root); |
1025 | snprintf(script_path, MAXPATHLEN, "%s/%s", | |
1026 | lang_path, script_dirent.d_name); | |
1027 | read_script_info(desc, script_path); | |
38efb539 | 1028 | free(script_root); |
4b9c0c59 | 1029 | } |
4b9c0c59 TZ |
1030 | } |
1031 | } | |
1032 | ||
1033 | fprintf(stdout, "List of available trace scripts:\n"); | |
1034 | list_for_each_entry(desc, &script_descs, node) { | |
1035 | sprintf(first_half, "%s %s", desc->name, | |
1036 | desc->args ? desc->args : ""); | |
1037 | fprintf(stdout, " %-36s %s\n", first_half, | |
1038 | desc->half_liner ? desc->half_liner : ""); | |
1039 | } | |
1040 | ||
1041 | exit(0); | |
1042 | } | |
1043 | ||
49e639e2 FT |
1044 | /* |
1045 | * Some scripts specify the required events in their "xxx-record" file, | |
1046 | * this function will check if the events in perf.data match those | |
1047 | * mentioned in the "xxx-record". | |
1048 | * | |
1049 | * Fixme: All existing "xxx-record" are all in good formats "-e event ", | |
1050 | * which is covered well now. And new parsing code should be added to | |
1051 | * cover the future complexing formats like event groups etc. | |
1052 | */ | |
1053 | static int check_ev_match(char *dir_name, char *scriptname, | |
1054 | struct perf_session *session) | |
1055 | { | |
1056 | char filename[MAXPATHLEN], evname[128]; | |
1057 | char line[BUFSIZ], *p; | |
1058 | struct perf_evsel *pos; | |
1059 | int match, len; | |
1060 | FILE *fp; | |
1061 | ||
1062 | sprintf(filename, "%s/bin/%s-record", dir_name, scriptname); | |
1063 | ||
1064 | fp = fopen(filename, "r"); | |
1065 | if (!fp) | |
1066 | return -1; | |
1067 | ||
1068 | while (fgets(line, sizeof(line), fp)) { | |
1069 | p = ltrim(line); | |
1070 | if (*p == '#') | |
1071 | continue; | |
1072 | ||
1073 | while (strlen(p)) { | |
1074 | p = strstr(p, "-e"); | |
1075 | if (!p) | |
1076 | break; | |
1077 | ||
1078 | p += 2; | |
1079 | p = ltrim(p); | |
1080 | len = strcspn(p, " \t"); | |
1081 | if (!len) | |
1082 | break; | |
1083 | ||
1084 | snprintf(evname, len + 1, "%s", p); | |
1085 | ||
1086 | match = 0; | |
1087 | list_for_each_entry(pos, | |
1088 | &session->evlist->entries, node) { | |
1089 | if (!strcmp(perf_evsel__name(pos), evname)) { | |
1090 | match = 1; | |
1091 | break; | |
1092 | } | |
1093 | } | |
1094 | ||
1095 | if (!match) { | |
1096 | fclose(fp); | |
1097 | return -1; | |
1098 | } | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | fclose(fp); | |
1103 | return 0; | |
1104 | } | |
1105 | ||
e5f3705e FT |
1106 | /* |
1107 | * Return -1 if none is found, otherwise the actual scripts number. | |
1108 | * | |
1109 | * Currently the only user of this function is the script browser, which | |
1110 | * will list all statically runnable scripts, select one, execute it and | |
1111 | * show the output in a perf browser. | |
1112 | */ | |
1113 | int find_scripts(char **scripts_array, char **scripts_path_array) | |
1114 | { | |
1115 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
49e639e2 | 1116 | char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN]; |
e5f3705e | 1117 | DIR *scripts_dir, *lang_dir; |
49e639e2 | 1118 | struct perf_session *session; |
f5fc1412 JO |
1119 | struct perf_data_file file = { |
1120 | .path = input_name, | |
1121 | .mode = PERF_DATA_MODE_READ, | |
1122 | }; | |
e5f3705e FT |
1123 | char *temp; |
1124 | int i = 0; | |
1125 | ||
f5fc1412 | 1126 | session = perf_session__new(&file, false, NULL); |
49e639e2 FT |
1127 | if (!session) |
1128 | return -1; | |
1129 | ||
e5f3705e FT |
1130 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); |
1131 | ||
1132 | scripts_dir = opendir(scripts_path); | |
49e639e2 FT |
1133 | if (!scripts_dir) { |
1134 | perf_session__delete(session); | |
e5f3705e | 1135 | return -1; |
49e639e2 | 1136 | } |
e5f3705e FT |
1137 | |
1138 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { | |
1139 | snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, | |
1140 | lang_dirent.d_name); | |
1141 | #ifdef NO_LIBPERL | |
1142 | if (strstr(lang_path, "perl")) | |
1143 | continue; | |
1144 | #endif | |
1145 | #ifdef NO_LIBPYTHON | |
1146 | if (strstr(lang_path, "python")) | |
1147 | continue; | |
1148 | #endif | |
1149 | ||
1150 | lang_dir = opendir(lang_path); | |
1151 | if (!lang_dir) | |
1152 | continue; | |
1153 | ||
1154 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { | |
1155 | /* Skip those real time scripts: xxxtop.p[yl] */ | |
1156 | if (strstr(script_dirent.d_name, "top.")) | |
1157 | continue; | |
1158 | sprintf(scripts_path_array[i], "%s/%s", lang_path, | |
1159 | script_dirent.d_name); | |
1160 | temp = strchr(script_dirent.d_name, '.'); | |
1161 | snprintf(scripts_array[i], | |
1162 | (temp - script_dirent.d_name) + 1, | |
1163 | "%s", script_dirent.d_name); | |
49e639e2 FT |
1164 | |
1165 | if (check_ev_match(lang_path, | |
1166 | scripts_array[i], session)) | |
1167 | continue; | |
1168 | ||
e5f3705e FT |
1169 | i++; |
1170 | } | |
49e639e2 | 1171 | closedir(lang_dir); |
e5f3705e FT |
1172 | } |
1173 | ||
49e639e2 FT |
1174 | closedir(scripts_dir); |
1175 | perf_session__delete(session); | |
e5f3705e FT |
1176 | return i; |
1177 | } | |
1178 | ||
3875294f TZ |
1179 | static char *get_script_path(const char *script_root, const char *suffix) |
1180 | { | |
1181 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
1182 | char scripts_path[MAXPATHLEN]; | |
1183 | char script_path[MAXPATHLEN]; | |
1184 | DIR *scripts_dir, *lang_dir; | |
1185 | char lang_path[MAXPATHLEN]; | |
38efb539 | 1186 | char *__script_root; |
3875294f TZ |
1187 | |
1188 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | |
1189 | ||
1190 | scripts_dir = opendir(scripts_path); | |
1191 | if (!scripts_dir) | |
1192 | return NULL; | |
1193 | ||
008f29d3 | 1194 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
3875294f TZ |
1195 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
1196 | lang_dirent.d_name); | |
1197 | lang_dir = opendir(lang_path); | |
1198 | if (!lang_dir) | |
1199 | continue; | |
1200 | ||
008f29d3 | 1201 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
38efb539 RR |
1202 | __script_root = get_script_root(&script_dirent, suffix); |
1203 | if (__script_root && !strcmp(script_root, __script_root)) { | |
1204 | free(__script_root); | |
946ef2a2 NK |
1205 | closedir(lang_dir); |
1206 | closedir(scripts_dir); | |
3875294f TZ |
1207 | snprintf(script_path, MAXPATHLEN, "%s/%s", |
1208 | lang_path, script_dirent.d_name); | |
38efb539 | 1209 | return strdup(script_path); |
3875294f TZ |
1210 | } |
1211 | free(__script_root); | |
1212 | } | |
946ef2a2 | 1213 | closedir(lang_dir); |
3875294f | 1214 | } |
946ef2a2 | 1215 | closedir(scripts_dir); |
3875294f | 1216 | |
38efb539 | 1217 | return NULL; |
3875294f TZ |
1218 | } |
1219 | ||
b5b87312 TZ |
1220 | static bool is_top_script(const char *script_path) |
1221 | { | |
965bb6be | 1222 | return ends_with(script_path, "top") == NULL ? false : true; |
b5b87312 TZ |
1223 | } |
1224 | ||
1225 | static int has_required_arg(char *script_path) | |
1226 | { | |
1227 | struct script_desc *desc; | |
1228 | int n_args = 0; | |
1229 | char *p; | |
1230 | ||
1231 | desc = script_desc__new(NULL); | |
1232 | ||
1233 | if (read_script_info(desc, script_path)) | |
1234 | goto out; | |
1235 | ||
1236 | if (!desc->args) | |
1237 | goto out; | |
1238 | ||
1239 | for (p = desc->args; *p; p++) | |
1240 | if (*p == '<') | |
1241 | n_args++; | |
1242 | out: | |
1243 | script_desc__delete(desc); | |
1244 | ||
1245 | return n_args; | |
1246 | } | |
1247 | ||
69b6470e ACM |
1248 | static int have_cmd(int argc, const char **argv) |
1249 | { | |
1250 | char **__argv = malloc(sizeof(const char *) * argc); | |
1251 | ||
1252 | if (!__argv) { | |
1253 | pr_err("malloc failed\n"); | |
1254 | return -1; | |
1255 | } | |
1256 | ||
1257 | memcpy(__argv, argv, sizeof(const char *) * argc); | |
1258 | argc = parse_options(argc, (const char **)__argv, record_options, | |
1259 | NULL, PARSE_OPT_STOP_AT_NON_OPTION); | |
1260 | free(__argv); | |
5f9c39dc | 1261 | |
69b6470e ACM |
1262 | system_wide = (argc == 0); |
1263 | ||
1264 | return 0; | |
1265 | } | |
1266 | ||
1267 | int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused) | |
1268 | { | |
1269 | bool show_full_info = false; | |
69b6470e ACM |
1270 | char *rec_script_path = NULL; |
1271 | char *rep_script_path = NULL; | |
1272 | struct perf_session *session; | |
1273 | char *script_path = NULL; | |
1274 | const char **__argv; | |
1275 | int i, j, err; | |
1276 | const struct option options[] = { | |
5f9c39dc FW |
1277 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
1278 | "dump raw trace in ASCII"), | |
c0555642 | 1279 | OPT_INCR('v', "verbose", &verbose, |
69b6470e | 1280 | "be more verbose (show symbol address, etc)"), |
4b9c0c59 | 1281 | OPT_BOOLEAN('L', "Latency", &latency_format, |
cda48461 | 1282 | "show latency attributes (irqs/preemption disabled, etc)"), |
4b9c0c59 TZ |
1283 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", |
1284 | list_available_scripts), | |
956ffd02 TZ |
1285 | OPT_CALLBACK('s', "script", NULL, "name", |
1286 | "script file name (lang:script name, script name, or *)", | |
1287 | parse_scriptname), | |
1288 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", | |
133dc4c3 | 1289 | "generate perf-script.xx script in specified language"), |
69b6470e | 1290 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
ffabd99e FW |
1291 | OPT_BOOLEAN('d', "debug-mode", &debug_mode, |
1292 | "do various checks like samples ordering and lost events"), | |
c0230b2b DA |
1293 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
1294 | "file", "vmlinux pathname"), | |
1295 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, | |
1296 | "file", "kallsyms pathname"), | |
1297 | OPT_BOOLEAN('G', "hide-call-graph", &no_callchain, | |
1298 | "When printing symbols do not display call chain"), | |
1299 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", | |
1300 | "Look for files with symbols relative to this directory"), | |
745f43e3 | 1301 | OPT_CALLBACK('f', "fields", NULL, "str", |
a978f2ab AN |
1302 | "comma separated output fields prepend with 'type:'. " |
1303 | "Valid types: hw,sw,trace,raw. " | |
1304 | "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso," | |
69b6470e | 1305 | "addr,symoff", parse_output_fields), |
317df650 | 1306 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
69b6470e | 1307 | "system-wide collection from all CPUs"), |
36385be5 FT |
1308 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", |
1309 | "only consider these symbols"), | |
c8e66720 | 1310 | OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"), |
e7984b7b DA |
1311 | OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", |
1312 | "only display events for these comms"), | |
fbe96f29 SE |
1313 | OPT_BOOLEAN('I', "show-info", &show_full_info, |
1314 | "display extended information from perf.data file"), | |
0bc8d205 AN |
1315 | OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path, |
1316 | "Show the path of [kernel.kallsyms]"), | |
1909629f | 1317 | OPT_END() |
69b6470e ACM |
1318 | }; |
1319 | const char * const script_usage[] = { | |
1320 | "perf script [<options>]", | |
1321 | "perf script [<options>] record <script> [<record-options>] <command>", | |
1322 | "perf script [<options>] report <script> [script-args]", | |
1323 | "perf script [<options>] <script> [<record-options>] <command>", | |
1324 | "perf script [<options>] <top-script> [script-args]", | |
1325 | NULL | |
1326 | }; | |
f5fc1412 JO |
1327 | struct perf_data_file file = { |
1328 | .mode = PERF_DATA_MODE_READ, | |
1329 | }; | |
3875294f | 1330 | |
b5b87312 TZ |
1331 | setup_scripting(); |
1332 | ||
133dc4c3 | 1333 | argc = parse_options(argc, argv, options, script_usage, |
b5b87312 TZ |
1334 | PARSE_OPT_STOP_AT_NON_OPTION); |
1335 | ||
f5fc1412 JO |
1336 | file.path = input_name; |
1337 | ||
b5b87312 TZ |
1338 | if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { |
1339 | rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); | |
1340 | if (!rec_script_path) | |
1341 | return cmd_record(argc, argv, NULL); | |
3875294f TZ |
1342 | } |
1343 | ||
b5b87312 TZ |
1344 | if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { |
1345 | rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); | |
1346 | if (!rep_script_path) { | |
3875294f | 1347 | fprintf(stderr, |
b5b87312 | 1348 | "Please specify a valid report script" |
133dc4c3 | 1349 | "(see 'perf script -l' for listing)\n"); |
3875294f TZ |
1350 | return -1; |
1351 | } | |
3875294f TZ |
1352 | } |
1353 | ||
44e668c6 BH |
1354 | /* make sure PERF_EXEC_PATH is set for scripts */ |
1355 | perf_set_argv_exec_path(perf_exec_path()); | |
1356 | ||
b5b87312 | 1357 | if (argc && !script_name && !rec_script_path && !rep_script_path) { |
a0cccc2e | 1358 | int live_pipe[2]; |
b5b87312 | 1359 | int rep_args; |
a0cccc2e TZ |
1360 | pid_t pid; |
1361 | ||
b5b87312 TZ |
1362 | rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); |
1363 | rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); | |
1364 | ||
1365 | if (!rec_script_path && !rep_script_path) { | |
1366 | fprintf(stderr, " Couldn't find script %s\n\n See perf" | |
133dc4c3 IM |
1367 | " script -l for available scripts.\n", argv[0]); |
1368 | usage_with_options(script_usage, options); | |
a0cccc2e TZ |
1369 | } |
1370 | ||
b5b87312 TZ |
1371 | if (is_top_script(argv[0])) { |
1372 | rep_args = argc - 1; | |
1373 | } else { | |
1374 | int rec_args; | |
1375 | ||
1376 | rep_args = has_required_arg(rep_script_path); | |
1377 | rec_args = (argc - 1) - rep_args; | |
1378 | if (rec_args < 0) { | |
1379 | fprintf(stderr, " %s script requires options." | |
133dc4c3 | 1380 | "\n\n See perf script -l for available " |
b5b87312 | 1381 | "scripts and options.\n", argv[0]); |
133dc4c3 | 1382 | usage_with_options(script_usage, options); |
b5b87312 | 1383 | } |
a0cccc2e TZ |
1384 | } |
1385 | ||
1386 | if (pipe(live_pipe) < 0) { | |
1387 | perror("failed to create pipe"); | |
d54b1a9e | 1388 | return -1; |
a0cccc2e TZ |
1389 | } |
1390 | ||
1391 | pid = fork(); | |
1392 | if (pid < 0) { | |
1393 | perror("failed to fork"); | |
d54b1a9e | 1394 | return -1; |
a0cccc2e TZ |
1395 | } |
1396 | ||
1397 | if (!pid) { | |
b5b87312 TZ |
1398 | j = 0; |
1399 | ||
a0cccc2e TZ |
1400 | dup2(live_pipe[1], 1); |
1401 | close(live_pipe[0]); | |
1402 | ||
317df650 RR |
1403 | if (is_top_script(argv[0])) { |
1404 | system_wide = true; | |
1405 | } else if (!system_wide) { | |
d54b1a9e DA |
1406 | if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) { |
1407 | err = -1; | |
1408 | goto out; | |
1409 | } | |
317df650 | 1410 | } |
b5b87312 TZ |
1411 | |
1412 | __argv = malloc((argc + 6) * sizeof(const char *)); | |
d54b1a9e DA |
1413 | if (!__argv) { |
1414 | pr_err("malloc failed\n"); | |
1415 | err = -ENOMEM; | |
1416 | goto out; | |
1417 | } | |
e8719adf | 1418 | |
b5b87312 TZ |
1419 | __argv[j++] = "/bin/sh"; |
1420 | __argv[j++] = rec_script_path; | |
1421 | if (system_wide) | |
1422 | __argv[j++] = "-a"; | |
1423 | __argv[j++] = "-q"; | |
1424 | __argv[j++] = "-o"; | |
1425 | __argv[j++] = "-"; | |
1426 | for (i = rep_args + 1; i < argc; i++) | |
1427 | __argv[j++] = argv[i]; | |
1428 | __argv[j++] = NULL; | |
a0cccc2e TZ |
1429 | |
1430 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 1431 | free(__argv); |
a0cccc2e TZ |
1432 | exit(-1); |
1433 | } | |
1434 | ||
1435 | dup2(live_pipe[0], 0); | |
1436 | close(live_pipe[1]); | |
1437 | ||
b5b87312 | 1438 | __argv = malloc((argc + 4) * sizeof(const char *)); |
d54b1a9e DA |
1439 | if (!__argv) { |
1440 | pr_err("malloc failed\n"); | |
1441 | err = -ENOMEM; | |
1442 | goto out; | |
1443 | } | |
1444 | ||
b5b87312 TZ |
1445 | j = 0; |
1446 | __argv[j++] = "/bin/sh"; | |
1447 | __argv[j++] = rep_script_path; | |
1448 | for (i = 1; i < rep_args + 1; i++) | |
1449 | __argv[j++] = argv[i]; | |
1450 | __argv[j++] = "-i"; | |
1451 | __argv[j++] = "-"; | |
1452 | __argv[j++] = NULL; | |
a0cccc2e TZ |
1453 | |
1454 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 1455 | free(__argv); |
a0cccc2e TZ |
1456 | exit(-1); |
1457 | } | |
1458 | ||
b5b87312 TZ |
1459 | if (rec_script_path) |
1460 | script_path = rec_script_path; | |
1461 | if (rep_script_path) | |
1462 | script_path = rep_script_path; | |
34c86ea9 | 1463 | |
b5b87312 | 1464 | if (script_path) { |
b5b87312 | 1465 | j = 0; |
3875294f | 1466 | |
317df650 RR |
1467 | if (!rec_script_path) |
1468 | system_wide = false; | |
d54b1a9e DA |
1469 | else if (!system_wide) { |
1470 | if (have_cmd(argc - 1, &argv[1]) != 0) { | |
1471 | err = -1; | |
1472 | goto out; | |
1473 | } | |
1474 | } | |
34c86ea9 | 1475 | |
b5b87312 | 1476 | __argv = malloc((argc + 2) * sizeof(const char *)); |
d54b1a9e DA |
1477 | if (!__argv) { |
1478 | pr_err("malloc failed\n"); | |
1479 | err = -ENOMEM; | |
1480 | goto out; | |
1481 | } | |
1482 | ||
34c86ea9 TZ |
1483 | __argv[j++] = "/bin/sh"; |
1484 | __argv[j++] = script_path; | |
1485 | if (system_wide) | |
1486 | __argv[j++] = "-a"; | |
b5b87312 | 1487 | for (i = 2; i < argc; i++) |
34c86ea9 TZ |
1488 | __argv[j++] = argv[i]; |
1489 | __argv[j++] = NULL; | |
3875294f TZ |
1490 | |
1491 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 1492 | free(__argv); |
3875294f TZ |
1493 | exit(-1); |
1494 | } | |
956ffd02 | 1495 | |
655000e7 ACM |
1496 | if (symbol__init() < 0) |
1497 | return -1; | |
cf4fee50 TZ |
1498 | if (!script_name) |
1499 | setup_pager(); | |
5f9c39dc | 1500 | |
f5fc1412 | 1501 | session = perf_session__new(&file, false, &perf_script); |
d8f66248 ACM |
1502 | if (session == NULL) |
1503 | return -ENOMEM; | |
1504 | ||
5d67be97 AB |
1505 | if (cpu_list) { |
1506 | if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap)) | |
1507 | return -1; | |
1508 | } | |
1509 | ||
bdb71db2 TZ |
1510 | if (!script_name && !generate_script_lang) |
1511 | perf_session__fprintf_info(session, stdout, show_full_info); | |
fbe96f29 | 1512 | |
1424dc96 | 1513 | if (!no_callchain) |
c0230b2b DA |
1514 | symbol_conf.use_callchain = true; |
1515 | else | |
1516 | symbol_conf.use_callchain = false; | |
1517 | ||
956ffd02 TZ |
1518 | if (generate_script_lang) { |
1519 | struct stat perf_stat; | |
745f43e3 DA |
1520 | int input; |
1521 | ||
2c9e45f7 | 1522 | if (output_set_by_user()) { |
745f43e3 DA |
1523 | fprintf(stderr, |
1524 | "custom fields not supported for generated scripts"); | |
1525 | return -1; | |
1526 | } | |
956ffd02 | 1527 | |
cc9784bd | 1528 | input = open(file.path, O_RDONLY); /* input_name */ |
956ffd02 TZ |
1529 | if (input < 0) { |
1530 | perror("failed to open file"); | |
d54b1a9e | 1531 | return -1; |
956ffd02 TZ |
1532 | } |
1533 | ||
1534 | err = fstat(input, &perf_stat); | |
1535 | if (err < 0) { | |
1536 | perror("failed to stat file"); | |
d54b1a9e | 1537 | return -1; |
956ffd02 TZ |
1538 | } |
1539 | ||
1540 | if (!perf_stat.st_size) { | |
1541 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
d54b1a9e | 1542 | return 0; |
956ffd02 TZ |
1543 | } |
1544 | ||
1545 | scripting_ops = script_spec__lookup(generate_script_lang); | |
1546 | if (!scripting_ops) { | |
1547 | fprintf(stderr, "invalid language specifier"); | |
1548 | return -1; | |
1549 | } | |
1550 | ||
da378962 ACM |
1551 | err = scripting_ops->generate_script(session->pevent, |
1552 | "perf-script"); | |
956ffd02 TZ |
1553 | goto out; |
1554 | } | |
1555 | ||
1556 | if (script_name) { | |
586bc5cc | 1557 | err = scripting_ops->start_script(script_name, argc, argv); |
956ffd02 TZ |
1558 | if (err) |
1559 | goto out; | |
133dc4c3 | 1560 | pr_debug("perf script started with script %s\n\n", script_name); |
956ffd02 TZ |
1561 | } |
1562 | ||
9cbdb702 DA |
1563 | |
1564 | err = perf_session__check_output_opt(session); | |
1565 | if (err < 0) | |
1566 | goto out; | |
1567 | ||
133dc4c3 | 1568 | err = __cmd_script(session); |
956ffd02 | 1569 | |
d8f66248 | 1570 | perf_session__delete(session); |
956ffd02 TZ |
1571 | cleanup_scripting(); |
1572 | out: | |
1573 | return err; | |
5f9c39dc | 1574 | } |