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