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