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