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