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