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