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