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