Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / tools / perf / builtin-script.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "builtin.h"
3
4 #include "perf.h"
5 #include "util/cache.h"
6 #include "util/debug.h"
7 #include <subcmd/exec-cmd.h>
8 #include "util/header.h"
9 #include <subcmd/parse-options.h>
10 #include "util/perf_regs.h"
11 #include "util/session.h"
12 #include "util/tool.h"
13 #include "util/symbol.h"
14 #include "util/thread.h"
15 #include "util/trace-event.h"
16 #include "util/util.h"
17 #include "util/evlist.h"
18 #include "util/evsel.h"
19 #include "util/sort.h"
20 #include "util/data.h"
21 #include "util/auxtrace.h"
22 #include "util/cpumap.h"
23 #include "util/thread_map.h"
24 #include "util/stat.h"
25 #include "util/string2.h"
26 #include "util/thread-stack.h"
27 #include "util/time-utils.h"
28 #include "print_binary.h"
29 #include <linux/bitmap.h>
30 #include <linux/kernel.h>
31 #include <linux/stringify.h>
32 #include <linux/time64.h>
33 #include "asm/bug.h"
34 #include "util/mem-events.h"
35 #include "util/dump-insn.h"
36 #include <dirent.h>
37 #include <errno.h>
38 #include <inttypes.h>
39 #include <signal.h>
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44
45 #include "sane_ctype.h"
46
47 static char const               *script_name;
48 static char const               *generate_script_lang;
49 static bool                     debug_mode;
50 static u64                      last_timestamp;
51 static u64                      nr_unordered;
52 static bool                     no_callchain;
53 static bool                     latency_format;
54 static bool                     system_wide;
55 static bool                     print_flags;
56 static bool                     nanosecs;
57 static const char               *cpu_list;
58 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
59 static struct perf_stat_config  stat_config;
60 static int                      max_blocks;
61
62 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
63
64 enum perf_output_field {
65         PERF_OUTPUT_COMM            = 1U << 0,
66         PERF_OUTPUT_TID             = 1U << 1,
67         PERF_OUTPUT_PID             = 1U << 2,
68         PERF_OUTPUT_TIME            = 1U << 3,
69         PERF_OUTPUT_CPU             = 1U << 4,
70         PERF_OUTPUT_EVNAME          = 1U << 5,
71         PERF_OUTPUT_TRACE           = 1U << 6,
72         PERF_OUTPUT_IP              = 1U << 7,
73         PERF_OUTPUT_SYM             = 1U << 8,
74         PERF_OUTPUT_DSO             = 1U << 9,
75         PERF_OUTPUT_ADDR            = 1U << 10,
76         PERF_OUTPUT_SYMOFFSET       = 1U << 11,
77         PERF_OUTPUT_SRCLINE         = 1U << 12,
78         PERF_OUTPUT_PERIOD          = 1U << 13,
79         PERF_OUTPUT_IREGS           = 1U << 14,
80         PERF_OUTPUT_BRSTACK         = 1U << 15,
81         PERF_OUTPUT_BRSTACKSYM      = 1U << 16,
82         PERF_OUTPUT_DATA_SRC        = 1U << 17,
83         PERF_OUTPUT_WEIGHT          = 1U << 18,
84         PERF_OUTPUT_BPF_OUTPUT      = 1U << 19,
85         PERF_OUTPUT_CALLINDENT      = 1U << 20,
86         PERF_OUTPUT_INSN            = 1U << 21,
87         PERF_OUTPUT_INSNLEN         = 1U << 22,
88         PERF_OUTPUT_BRSTACKINSN     = 1U << 23,
89         PERF_OUTPUT_BRSTACKOFF      = 1U << 24,
90         PERF_OUTPUT_SYNTH           = 1U << 25,
91         PERF_OUTPUT_PHYS_ADDR       = 1U << 26,
92         PERF_OUTPUT_UREGS           = 1U << 27,
93 };
94
95 struct output_option {
96         const char *str;
97         enum perf_output_field field;
98 } all_output_options[] = {
99         {.str = "comm",  .field = PERF_OUTPUT_COMM},
100         {.str = "tid",   .field = PERF_OUTPUT_TID},
101         {.str = "pid",   .field = PERF_OUTPUT_PID},
102         {.str = "time",  .field = PERF_OUTPUT_TIME},
103         {.str = "cpu",   .field = PERF_OUTPUT_CPU},
104         {.str = "event", .field = PERF_OUTPUT_EVNAME},
105         {.str = "trace", .field = PERF_OUTPUT_TRACE},
106         {.str = "ip",    .field = PERF_OUTPUT_IP},
107         {.str = "sym",   .field = PERF_OUTPUT_SYM},
108         {.str = "dso",   .field = PERF_OUTPUT_DSO},
109         {.str = "addr",  .field = PERF_OUTPUT_ADDR},
110         {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
111         {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
112         {.str = "period", .field = PERF_OUTPUT_PERIOD},
113         {.str = "iregs", .field = PERF_OUTPUT_IREGS},
114         {.str = "uregs", .field = PERF_OUTPUT_UREGS},
115         {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
116         {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
117         {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
118         {.str = "weight",   .field = PERF_OUTPUT_WEIGHT},
119         {.str = "bpf-output",   .field = PERF_OUTPUT_BPF_OUTPUT},
120         {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
121         {.str = "insn", .field = PERF_OUTPUT_INSN},
122         {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
123         {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
124         {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
125         {.str = "synth", .field = PERF_OUTPUT_SYNTH},
126         {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
127 };
128
129 enum {
130         OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
131         OUTPUT_TYPE_MAX
132 };
133
134 /* default set to maintain compatibility with current format */
135 static struct {
136         bool user_set;
137         bool wildcard_set;
138         unsigned int print_ip_opts;
139         u64 fields;
140         u64 invalid_fields;
141 } output[OUTPUT_TYPE_MAX] = {
142
143         [PERF_TYPE_HARDWARE] = {
144                 .user_set = false,
145
146                 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
147                               PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
148                               PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
149                               PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
150                               PERF_OUTPUT_PERIOD,
151
152                 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
153         },
154
155         [PERF_TYPE_SOFTWARE] = {
156                 .user_set = false,
157
158                 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
159                               PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
160                               PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
161                               PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
162                               PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
163
164                 .invalid_fields = PERF_OUTPUT_TRACE,
165         },
166
167         [PERF_TYPE_TRACEPOINT] = {
168                 .user_set = false,
169
170                 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
171                                   PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
172                                   PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
173         },
174
175         [PERF_TYPE_RAW] = {
176                 .user_set = false,
177
178                 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
179                               PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
180                               PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
181                               PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
182                               PERF_OUTPUT_PERIOD |  PERF_OUTPUT_ADDR |
183                               PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
184                               PERF_OUTPUT_PHYS_ADDR,
185
186                 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
187         },
188
189         [PERF_TYPE_BREAKPOINT] = {
190                 .user_set = false,
191
192                 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
193                               PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
194                               PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
195                               PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
196                               PERF_OUTPUT_PERIOD,
197
198                 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
199         },
200
201         [OUTPUT_TYPE_SYNTH] = {
202                 .user_set = false,
203
204                 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
205                               PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
206                               PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
207                               PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
208                               PERF_OUTPUT_SYNTH,
209
210                 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
211         },
212 };
213
214 struct perf_evsel_script {
215        char *filename;
216        FILE *fp;
217        u64  samples;
218 };
219
220 static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
221                                                         struct perf_data *data)
222 {
223         struct perf_evsel_script *es = malloc(sizeof(*es));
224
225         if (es != NULL) {
226                 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
227                         goto out_free;
228                 es->fp = fopen(es->filename, "w");
229                 if (es->fp == NULL)
230                         goto out_free_filename;
231                 es->samples = 0;
232         }
233
234         return es;
235 out_free_filename:
236         zfree(&es->filename);
237 out_free:
238         free(es);
239         return NULL;
240 }
241
242 static void perf_evsel_script__delete(struct perf_evsel_script *es)
243 {
244         zfree(&es->filename);
245         fclose(es->fp);
246         es->fp = NULL;
247         free(es);
248 }
249
250 static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
251 {
252         struct stat st;
253
254         fstat(fileno(es->fp), &st);
255         return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
256                        st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
257 }
258
259 static inline int output_type(unsigned int type)
260 {
261         switch (type) {
262         case PERF_TYPE_SYNTH:
263                 return OUTPUT_TYPE_SYNTH;
264         default:
265                 return type;
266         }
267 }
268
269 static inline unsigned int attr_type(unsigned int type)
270 {
271         switch (type) {
272         case OUTPUT_TYPE_SYNTH:
273                 return PERF_TYPE_SYNTH;
274         default:
275                 return type;
276         }
277 }
278
279 static bool output_set_by_user(void)
280 {
281         int j;
282         for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
283                 if (output[j].user_set)
284                         return true;
285         }
286         return false;
287 }
288
289 static const char *output_field2str(enum perf_output_field field)
290 {
291         int i, imax = ARRAY_SIZE(all_output_options);
292         const char *str = "";
293
294         for (i = 0; i < imax; ++i) {
295                 if (all_output_options[i].field == field) {
296                         str = all_output_options[i].str;
297                         break;
298                 }
299         }
300         return str;
301 }
302
303 #define PRINT_FIELD(x)  (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
304
305 static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
306                                       u64 sample_type, const char *sample_msg,
307                                       enum perf_output_field field,
308                                       bool allow_user_set)
309 {
310         struct perf_event_attr *attr = &evsel->attr;
311         int type = output_type(attr->type);
312         const char *evname;
313
314         if (attr->sample_type & sample_type)
315                 return 0;
316
317         if (output[type].user_set) {
318                 if (allow_user_set)
319                         return 0;
320                 evname = perf_evsel__name(evsel);
321                 pr_err("Samples for '%s' event do not have %s attribute set. "
322                        "Cannot print '%s' field.\n",
323                        evname, sample_msg, output_field2str(field));
324                 return -1;
325         }
326
327         /* user did not ask for it explicitly so remove from the default list */
328         output[type].fields &= ~field;
329         evname = perf_evsel__name(evsel);
330         pr_debug("Samples for '%s' event do not have %s attribute set. "
331                  "Skipping '%s' field.\n",
332                  evname, sample_msg, output_field2str(field));
333
334         return 0;
335 }
336
337 static int perf_evsel__check_stype(struct perf_evsel *evsel,
338                                    u64 sample_type, const char *sample_msg,
339                                    enum perf_output_field field)
340 {
341         return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
342                                           false);
343 }
344
345 static int perf_evsel__check_attr(struct perf_evsel *evsel,
346                                   struct perf_session *session)
347 {
348         struct perf_event_attr *attr = &evsel->attr;
349         bool allow_user_set;
350
351         if (perf_header__has_feat(&session->header, HEADER_STAT))
352                 return 0;
353
354         allow_user_set = perf_header__has_feat(&session->header,
355                                                HEADER_AUXTRACE);
356
357         if (PRINT_FIELD(TRACE) &&
358                 !perf_session__has_traces(session, "record -R"))
359                 return -EINVAL;
360
361         if (PRINT_FIELD(IP)) {
362                 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
363                                             PERF_OUTPUT_IP))
364                         return -EINVAL;
365         }
366
367         if (PRINT_FIELD(ADDR) &&
368                 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
369                                            PERF_OUTPUT_ADDR, allow_user_set))
370                 return -EINVAL;
371
372         if (PRINT_FIELD(DATA_SRC) &&
373                 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
374                                         PERF_OUTPUT_DATA_SRC))
375                 return -EINVAL;
376
377         if (PRINT_FIELD(WEIGHT) &&
378                 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
379                                         PERF_OUTPUT_WEIGHT))
380                 return -EINVAL;
381
382         if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
383                 pr_err("Display of symbols requested but neither sample IP nor "
384                            "sample address\nis selected. Hence, no addresses to convert "
385                        "to symbols.\n");
386                 return -EINVAL;
387         }
388         if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
389                 pr_err("Display of offsets requested but symbol is not"
390                        "selected.\n");
391                 return -EINVAL;
392         }
393         if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
394             !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
395                 pr_err("Display of DSO requested but no address to convert.  Select\n"
396                        "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
397                 return -EINVAL;
398         }
399         if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
400                 pr_err("Display of source line number requested but sample IP is not\n"
401                        "selected. Hence, no address to lookup the source line number.\n");
402                 return -EINVAL;
403         }
404         if (PRINT_FIELD(BRSTACKINSN) &&
405             !(perf_evlist__combined_branch_type(session->evlist) &
406               PERF_SAMPLE_BRANCH_ANY)) {
407                 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
408                        "Hint: run 'perf record -b ...'\n");
409                 return -EINVAL;
410         }
411         if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
412                 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
413                                         PERF_OUTPUT_TID|PERF_OUTPUT_PID))
414                 return -EINVAL;
415
416         if (PRINT_FIELD(TIME) &&
417                 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
418                                         PERF_OUTPUT_TIME))
419                 return -EINVAL;
420
421         if (PRINT_FIELD(CPU) &&
422                 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
423                                            PERF_OUTPUT_CPU, allow_user_set))
424                 return -EINVAL;
425
426         if (PRINT_FIELD(PERIOD) &&
427                 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
428                                         PERF_OUTPUT_PERIOD))
429                 return -EINVAL;
430
431         if (PRINT_FIELD(IREGS) &&
432                 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
433                                         PERF_OUTPUT_IREGS))
434                 return -EINVAL;
435
436         if (PRINT_FIELD(UREGS) &&
437                 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
438                                         PERF_OUTPUT_UREGS))
439                 return -EINVAL;
440
441         if (PRINT_FIELD(PHYS_ADDR) &&
442                 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
443                                         PERF_OUTPUT_PHYS_ADDR))
444                 return -EINVAL;
445
446         return 0;
447 }
448
449 static void set_print_ip_opts(struct perf_event_attr *attr)
450 {
451         unsigned int type = output_type(attr->type);
452
453         output[type].print_ip_opts = 0;
454         if (PRINT_FIELD(IP))
455                 output[type].print_ip_opts |= EVSEL__PRINT_IP;
456
457         if (PRINT_FIELD(SYM))
458                 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
459
460         if (PRINT_FIELD(DSO))
461                 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
462
463         if (PRINT_FIELD(SYMOFFSET))
464                 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
465
466         if (PRINT_FIELD(SRCLINE))
467                 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
468 }
469
470 /*
471  * verify all user requested events exist and the samples
472  * have the expected data
473  */
474 static int perf_session__check_output_opt(struct perf_session *session)
475 {
476         unsigned int j;
477         struct perf_evsel *evsel;
478
479         for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
480                 evsel = perf_session__find_first_evtype(session, attr_type(j));
481
482                 /*
483                  * even if fields is set to 0 (ie., show nothing) event must
484                  * exist if user explicitly includes it on the command line
485                  */
486                 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
487                     j != OUTPUT_TYPE_SYNTH) {
488                         pr_err("%s events do not exist. "
489                                "Remove corresponding -F option to proceed.\n",
490                                event_type(j));
491                         return -1;
492                 }
493
494                 if (evsel && output[j].fields &&
495                         perf_evsel__check_attr(evsel, session))
496                         return -1;
497
498                 if (evsel == NULL)
499                         continue;
500
501                 set_print_ip_opts(&evsel->attr);
502         }
503
504         if (!no_callchain) {
505                 bool use_callchain = false;
506                 bool not_pipe = false;
507
508                 evlist__for_each_entry(session->evlist, evsel) {
509                         not_pipe = true;
510                         if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
511                                 use_callchain = true;
512                                 break;
513                         }
514                 }
515                 if (not_pipe && !use_callchain)
516                         symbol_conf.use_callchain = false;
517         }
518
519         /*
520          * set default for tracepoints to print symbols only
521          * if callchains are present
522          */
523         if (symbol_conf.use_callchain &&
524             !output[PERF_TYPE_TRACEPOINT].user_set) {
525                 struct perf_event_attr *attr;
526
527                 j = PERF_TYPE_TRACEPOINT;
528
529                 evlist__for_each_entry(session->evlist, evsel) {
530                         if (evsel->attr.type != j)
531                                 continue;
532
533                         attr = &evsel->attr;
534
535                         if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
536                                 output[j].fields |= PERF_OUTPUT_IP;
537                                 output[j].fields |= PERF_OUTPUT_SYM;
538                                 output[j].fields |= PERF_OUTPUT_DSO;
539                                 set_print_ip_opts(attr);
540                                 goto out;
541                         }
542                 }
543         }
544
545 out:
546         return 0;
547 }
548
549 static int perf_sample__fprintf_iregs(struct perf_sample *sample,
550                                       struct perf_event_attr *attr, FILE *fp)
551 {
552         struct regs_dump *regs = &sample->intr_regs;
553         uint64_t mask = attr->sample_regs_intr;
554         unsigned i = 0, r;
555         int printed = 0;
556
557         if (!regs)
558                 return 0;
559
560         for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
561                 u64 val = regs->regs[i++];
562                 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
563         }
564
565         return printed;
566 }
567
568 static int perf_sample__fprintf_uregs(struct perf_sample *sample,
569                                       struct perf_event_attr *attr, FILE *fp)
570 {
571         struct regs_dump *regs = &sample->user_regs;
572         uint64_t mask = attr->sample_regs_user;
573         unsigned i = 0, r;
574         int printed = 0;
575
576         if (!regs || !regs->regs)
577                 return 0;
578
579         printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
580
581         for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
582                 u64 val = regs->regs[i++];
583                 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
584         }
585
586         return printed;
587 }
588
589 static int perf_sample__fprintf_start(struct perf_sample *sample,
590                                       struct thread *thread,
591                                       struct perf_evsel *evsel, FILE *fp)
592 {
593         struct perf_event_attr *attr = &evsel->attr;
594         unsigned long secs;
595         unsigned long long nsecs;
596         int printed = 0;
597
598         if (PRINT_FIELD(COMM)) {
599                 if (latency_format)
600                         printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
601                 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
602                         printed += fprintf(fp, "%s ", thread__comm_str(thread));
603                 else
604                         printed += fprintf(fp, "%16s ", thread__comm_str(thread));
605         }
606
607         if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
608                 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
609         else if (PRINT_FIELD(PID))
610                 printed += fprintf(fp, "%5d ", sample->pid);
611         else if (PRINT_FIELD(TID))
612                 printed += fprintf(fp, "%5d ", sample->tid);
613
614         if (PRINT_FIELD(CPU)) {
615                 if (latency_format)
616                         printed += fprintf(fp, "%3d ", sample->cpu);
617                 else
618                         printed += fprintf(fp, "[%03d] ", sample->cpu);
619         }
620
621         if (PRINT_FIELD(TIME)) {
622                 nsecs = sample->time;
623                 secs = nsecs / NSEC_PER_SEC;
624                 nsecs -= secs * NSEC_PER_SEC;
625
626                 if (nanosecs)
627                         printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
628                 else {
629                         char sample_time[32];
630                         timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
631                         printed += fprintf(fp, "%12s: ", sample_time);
632                 }
633         }
634
635         return printed;
636 }
637
638 static inline char
639 mispred_str(struct branch_entry *br)
640 {
641         if (!(br->flags.mispred  || br->flags.predicted))
642                 return '-';
643
644         return br->flags.predicted ? 'P' : 'M';
645 }
646
647 static int perf_sample__fprintf_brstack(struct perf_sample *sample,
648                                         struct thread *thread,
649                                         struct perf_event_attr *attr, FILE *fp)
650 {
651         struct branch_stack *br = sample->branch_stack;
652         struct addr_location alf, alt;
653         u64 i, from, to;
654         int printed = 0;
655
656         if (!(br && br->nr))
657                 return 0;
658
659         for (i = 0; i < br->nr; i++) {
660                 from = br->entries[i].from;
661                 to   = br->entries[i].to;
662
663                 if (PRINT_FIELD(DSO)) {
664                         memset(&alf, 0, sizeof(alf));
665                         memset(&alt, 0, sizeof(alt));
666                         thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
667                         thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
668                 }
669
670                 printed += fprintf(fp, " 0x%"PRIx64, from);
671                 if (PRINT_FIELD(DSO)) {
672                         printed += fprintf(fp, "(");
673                         printed += map__fprintf_dsoname(alf.map, fp);
674                         printed += fprintf(fp, ")");
675                 }
676
677                 printed += fprintf(fp, "/0x%"PRIx64, to);
678                 if (PRINT_FIELD(DSO)) {
679                         printed += fprintf(fp, "(");
680                         printed += map__fprintf_dsoname(alt.map, fp);
681                         printed += fprintf(fp, ")");
682                 }
683
684                 printed += fprintf(fp, "/%c/%c/%c/%d ",
685                         mispred_str( br->entries + i),
686                         br->entries[i].flags.in_tx? 'X' : '-',
687                         br->entries[i].flags.abort? 'A' : '-',
688                         br->entries[i].flags.cycles);
689         }
690
691         return printed;
692 }
693
694 static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
695                                            struct thread *thread,
696                                            struct perf_event_attr *attr, FILE *fp)
697 {
698         struct branch_stack *br = sample->branch_stack;
699         struct addr_location alf, alt;
700         u64 i, from, to;
701         int printed = 0;
702
703         if (!(br && br->nr))
704                 return 0;
705
706         for (i = 0; i < br->nr; i++) {
707
708                 memset(&alf, 0, sizeof(alf));
709                 memset(&alt, 0, sizeof(alt));
710                 from = br->entries[i].from;
711                 to   = br->entries[i].to;
712
713                 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
714                 if (alf.map)
715                         alf.sym = map__find_symbol(alf.map, alf.addr);
716
717                 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
718                 if (alt.map)
719                         alt.sym = map__find_symbol(alt.map, alt.addr);
720
721                 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
722                 if (PRINT_FIELD(DSO)) {
723                         printed += fprintf(fp, "(");
724                         printed += map__fprintf_dsoname(alf.map, fp);
725                         printed += fprintf(fp, ")");
726                 }
727                 printed += fprintf(fp, "%c", '/');
728                 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
729                 if (PRINT_FIELD(DSO)) {
730                         printed += fprintf(fp, "(");
731                         printed += map__fprintf_dsoname(alt.map, fp);
732                         printed += fprintf(fp, ")");
733                 }
734                 printed += fprintf(fp, "/%c/%c/%c/%d ",
735                         mispred_str( br->entries + i),
736                         br->entries[i].flags.in_tx? 'X' : '-',
737                         br->entries[i].flags.abort? 'A' : '-',
738                         br->entries[i].flags.cycles);
739         }
740
741         return printed;
742 }
743
744 static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
745                                            struct thread *thread,
746                                            struct perf_event_attr *attr, FILE *fp)
747 {
748         struct branch_stack *br = sample->branch_stack;
749         struct addr_location alf, alt;
750         u64 i, from, to;
751         int printed = 0;
752
753         if (!(br && br->nr))
754                 return 0;
755
756         for (i = 0; i < br->nr; i++) {
757
758                 memset(&alf, 0, sizeof(alf));
759                 memset(&alt, 0, sizeof(alt));
760                 from = br->entries[i].from;
761                 to   = br->entries[i].to;
762
763                 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
764                 if (alf.map && !alf.map->dso->adjust_symbols)
765                         from = map__map_ip(alf.map, from);
766
767                 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
768                 if (alt.map && !alt.map->dso->adjust_symbols)
769                         to = map__map_ip(alt.map, to);
770
771                 printed += fprintf(fp, " 0x%"PRIx64, from);
772                 if (PRINT_FIELD(DSO)) {
773                         printed += fprintf(fp, "(");
774                         printed += map__fprintf_dsoname(alf.map, fp);
775                         printed += fprintf(fp, ")");
776                 }
777                 printed += fprintf(fp, "/0x%"PRIx64, to);
778                 if (PRINT_FIELD(DSO)) {
779                         printed += fprintf(fp, "(");
780                         printed += map__fprintf_dsoname(alt.map, fp);
781                         printed += fprintf(fp, ")");
782                 }
783                 printed += fprintf(fp, "/%c/%c/%c/%d ",
784                         mispred_str(br->entries + i),
785                         br->entries[i].flags.in_tx ? 'X' : '-',
786                         br->entries[i].flags.abort ? 'A' : '-',
787                         br->entries[i].flags.cycles);
788         }
789
790         return printed;
791 }
792 #define MAXBB 16384UL
793
794 static int grab_bb(u8 *buffer, u64 start, u64 end,
795                     struct machine *machine, struct thread *thread,
796                     bool *is64bit, u8 *cpumode, bool last)
797 {
798         long offset, len;
799         struct addr_location al;
800         bool kernel;
801
802         if (!start || !end)
803                 return 0;
804
805         kernel = machine__kernel_ip(machine, start);
806         if (kernel)
807                 *cpumode = PERF_RECORD_MISC_KERNEL;
808         else
809                 *cpumode = PERF_RECORD_MISC_USER;
810
811         /*
812          * Block overlaps between kernel and user.
813          * This can happen due to ring filtering
814          * On Intel CPUs the entry into the kernel is filtered,
815          * but the exit is not. Let the caller patch it up.
816          */
817         if (kernel != machine__kernel_ip(machine, end)) {
818                 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
819                 return -ENXIO;
820         }
821
822         memset(&al, 0, sizeof(al));
823         if (end - start > MAXBB - MAXINSN) {
824                 if (last)
825                         pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
826                 else
827                         pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
828                 return 0;
829         }
830
831         thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
832         if (!al.map || !al.map->dso) {
833                 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
834                 return 0;
835         }
836         if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
837                 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
838                 return 0;
839         }
840
841         /* Load maps to ensure dso->is_64_bit has been updated */
842         map__load(al.map);
843
844         offset = al.map->map_ip(al.map, start);
845         len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
846                                     end - start + MAXINSN);
847
848         *is64bit = al.map->dso->is_64_bit;
849         if (len <= 0)
850                 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
851                         start, end);
852         return len;
853 }
854
855 static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
856                             struct perf_insn *x, u8 *inbuf, int len,
857                             int insn, FILE *fp)
858 {
859         int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
860                               dump_insn(x, ip, inbuf, len, NULL),
861                               en->flags.predicted ? " PRED" : "",
862                               en->flags.mispred ? " MISPRED" : "",
863                               en->flags.in_tx ? " INTX" : "",
864                               en->flags.abort ? " ABORT" : "");
865         if (en->flags.cycles) {
866                 printed += fprintf(fp, " %d cycles", en->flags.cycles);
867                 if (insn)
868                         printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
869         }
870         return printed + fprintf(fp, "\n");
871 }
872
873 static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
874                            u8 cpumode, int cpu, struct symbol **lastsym,
875                            struct perf_event_attr *attr, FILE *fp)
876 {
877         struct addr_location al;
878         int off, printed = 0;
879
880         memset(&al, 0, sizeof(al));
881
882         thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
883         if (!al.map)
884                 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
885                                       addr, &al);
886         if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
887                 return 0;
888
889         al.cpu = cpu;
890         al.sym = NULL;
891         if (al.map)
892                 al.sym = map__find_symbol(al.map, al.addr);
893
894         if (!al.sym)
895                 return 0;
896
897         if (al.addr < al.sym->end)
898                 off = al.addr - al.sym->start;
899         else
900                 off = al.addr - al.map->start - al.sym->start;
901         printed += fprintf(fp, "\t%s", al.sym->name);
902         if (off)
903                 printed += fprintf(fp, "%+d", off);
904         printed += fprintf(fp, ":");
905         if (PRINT_FIELD(SRCLINE))
906                 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
907         printed += fprintf(fp, "\n");
908         *lastsym = al.sym;
909
910         return printed;
911 }
912
913 static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
914                                             struct thread *thread,
915                                             struct perf_event_attr *attr,
916                                             struct machine *machine, FILE *fp)
917 {
918         struct branch_stack *br = sample->branch_stack;
919         u64 start, end;
920         int i, insn, len, nr, ilen, printed = 0;
921         struct perf_insn x;
922         u8 buffer[MAXBB];
923         unsigned off;
924         struct symbol *lastsym = NULL;
925
926         if (!(br && br->nr))
927                 return 0;
928         nr = br->nr;
929         if (max_blocks && nr > max_blocks + 1)
930                 nr = max_blocks + 1;
931
932         x.thread = thread;
933         x.cpu = sample->cpu;
934
935         printed += fprintf(fp, "%c", '\n');
936
937         /* Handle first from jump, of which we don't know the entry. */
938         len = grab_bb(buffer, br->entries[nr-1].from,
939                         br->entries[nr-1].from,
940                         machine, thread, &x.is64bit, &x.cpumode, false);
941         if (len > 0) {
942                 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
943                                            x.cpumode, x.cpu, &lastsym, attr, fp);
944                 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
945                                             &x, buffer, len, 0, fp);
946         }
947
948         /* Print all blocks */
949         for (i = nr - 2; i >= 0; i--) {
950                 if (br->entries[i].from || br->entries[i].to)
951                         pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
952                                  br->entries[i].from,
953                                  br->entries[i].to);
954                 start = br->entries[i + 1].to;
955                 end   = br->entries[i].from;
956
957                 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
958                 /* Patch up missing kernel transfers due to ring filters */
959                 if (len == -ENXIO && i > 0) {
960                         end = br->entries[--i].from;
961                         pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
962                         len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
963                 }
964                 if (len <= 0)
965                         continue;
966
967                 insn = 0;
968                 for (off = 0;; off += ilen) {
969                         uint64_t ip = start + off;
970
971                         printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
972                         if (ip == end) {
973                                 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
974                                 break;
975                         } else {
976                                 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
977                                                    dump_insn(&x, ip, buffer + off, len - off, &ilen));
978                                 if (ilen == 0)
979                                         break;
980                                 insn++;
981                         }
982                 }
983         }
984
985         /*
986          * Hit the branch? In this case we are already done, and the target
987          * has not been executed yet.
988          */
989         if (br->entries[0].from == sample->ip)
990                 goto out;
991         if (br->entries[0].flags.abort)
992                 goto out;
993
994         /*
995          * Print final block upto sample
996          */
997         start = br->entries[0].to;
998         end = sample->ip;
999         len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
1000         printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
1001         if (len <= 0) {
1002                 /* Print at least last IP if basic block did not work */
1003                 len = grab_bb(buffer, sample->ip, sample->ip,
1004                               machine, thread, &x.is64bit, &x.cpumode, false);
1005                 if (len <= 0)
1006                         goto out;
1007
1008                 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
1009                         dump_insn(&x, sample->ip, buffer, len, NULL));
1010                 goto out;
1011         }
1012         for (off = 0; off <= end - start; off += ilen) {
1013                 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1014                                    dump_insn(&x, start + off, buffer + off, len - off, &ilen));
1015                 if (ilen == 0)
1016                         break;
1017         }
1018 out:
1019         return printed;
1020 }
1021
1022 static int perf_sample__fprintf_addr(struct perf_sample *sample,
1023                                      struct thread *thread,
1024                                      struct perf_event_attr *attr, FILE *fp)
1025 {
1026         struct addr_location al;
1027         int printed = fprintf(fp, "%16" PRIx64, sample->addr);
1028
1029         if (!sample_addr_correlates_sym(attr))
1030                 goto out;
1031
1032         thread__resolve(thread, &al, sample);
1033
1034         if (PRINT_FIELD(SYM)) {
1035                 printed += fprintf(fp, " ");
1036                 if (PRINT_FIELD(SYMOFFSET))
1037                         printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
1038                 else
1039                         printed += symbol__fprintf_symname(al.sym, fp);
1040         }
1041
1042         if (PRINT_FIELD(DSO)) {
1043                 printed += fprintf(fp, " (");
1044                 printed += map__fprintf_dsoname(al.map, fp);
1045                 printed += fprintf(fp, ")");
1046         }
1047 out:
1048         return printed;
1049 }
1050
1051 static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1052                                            struct perf_evsel *evsel,
1053                                            struct thread *thread,
1054                                            struct addr_location *al, FILE *fp)
1055 {
1056         struct perf_event_attr *attr = &evsel->attr;
1057         size_t depth = thread_stack__depth(thread);
1058         struct addr_location addr_al;
1059         const char *name = NULL;
1060         static int spacing;
1061         int len = 0;
1062         u64 ip = 0;
1063
1064         /*
1065          * The 'return' has already been popped off the stack so the depth has
1066          * to be adjusted to match the 'call'.
1067          */
1068         if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1069                 depth += 1;
1070
1071         if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1072                 if (sample_addr_correlates_sym(attr)) {
1073                         thread__resolve(thread, &addr_al, sample);
1074                         if (addr_al.sym)
1075                                 name = addr_al.sym->name;
1076                         else
1077                                 ip = sample->addr;
1078                 } else {
1079                         ip = sample->addr;
1080                 }
1081         } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1082                 if (al->sym)
1083                         name = al->sym->name;
1084                 else
1085                         ip = sample->ip;
1086         }
1087
1088         if (name)
1089                 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
1090         else if (ip)
1091                 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
1092
1093         if (len < 0)
1094                 return len;
1095
1096         /*
1097          * Try to keep the output length from changing frequently so that the
1098          * output lines up more nicely.
1099          */
1100         if (len > spacing || (len && len < spacing - 52))
1101                 spacing = round_up(len + 4, 32);
1102
1103         if (len < spacing)
1104                 len += fprintf(fp, "%*s", spacing - len, "");
1105
1106         return len;
1107 }
1108
1109 static int perf_sample__fprintf_insn(struct perf_sample *sample,
1110                                      struct perf_event_attr *attr,
1111                                      struct thread *thread,
1112                                      struct machine *machine, FILE *fp)
1113 {
1114         int printed = 0;
1115
1116         if (PRINT_FIELD(INSNLEN))
1117                 printed += fprintf(fp, " ilen: %d", sample->insn_len);
1118         if (PRINT_FIELD(INSN)) {
1119                 int i;
1120
1121                 printed += fprintf(fp, " insn:");
1122                 for (i = 0; i < sample->insn_len; i++)
1123                         printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
1124         }
1125         if (PRINT_FIELD(BRSTACKINSN))
1126                 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1127
1128         return printed;
1129 }
1130
1131 static int perf_sample__fprintf_bts(struct perf_sample *sample,
1132                                     struct perf_evsel *evsel,
1133                                     struct thread *thread,
1134                                     struct addr_location *al,
1135                                     struct machine *machine, FILE *fp)
1136 {
1137         struct perf_event_attr *attr = &evsel->attr;
1138         unsigned int type = output_type(attr->type);
1139         bool print_srcline_last = false;
1140         int printed = 0;
1141
1142         if (PRINT_FIELD(CALLINDENT))
1143                 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
1144
1145         /* print branch_from information */
1146         if (PRINT_FIELD(IP)) {
1147                 unsigned int print_opts = output[type].print_ip_opts;
1148                 struct callchain_cursor *cursor = NULL;
1149
1150                 if (symbol_conf.use_callchain && sample->callchain &&
1151                     thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1152                                               sample, NULL, NULL, scripting_max_stack) == 0)
1153                         cursor = &callchain_cursor;
1154
1155                 if (cursor == NULL) {
1156                         printed += fprintf(fp, " ");
1157                         if (print_opts & EVSEL__PRINT_SRCLINE) {
1158                                 print_srcline_last = true;
1159                                 print_opts &= ~EVSEL__PRINT_SRCLINE;
1160                         }
1161                 } else
1162                         printed += fprintf(fp, "\n");
1163
1164                 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
1165         }
1166
1167         /* print branch_to information */
1168         if (PRINT_FIELD(ADDR) ||
1169             ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
1170              !output[type].user_set)) {
1171                 printed += fprintf(fp, " => ");
1172                 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
1173         }
1174
1175         if (print_srcline_last)
1176                 printed += map__fprintf_srcline(al->map, al->addr, "\n  ", fp);
1177
1178         printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1179         return printed + fprintf(fp, "\n");
1180 }
1181
1182 static struct {
1183         u32 flags;
1184         const char *name;
1185 } sample_flags[] = {
1186         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1187         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1188         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1189         {PERF_IP_FLAG_BRANCH, "jmp"},
1190         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1191         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1192         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1193         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1194         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1195         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1196         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1197         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1198         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1199         {0, NULL}
1200 };
1201
1202 static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
1203 {
1204         const char *chars = PERF_IP_FLAG_CHARS;
1205         const int n = strlen(PERF_IP_FLAG_CHARS);
1206         bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1207         const char *name = NULL;
1208         char str[33];
1209         int i, pos = 0;
1210
1211         for (i = 0; sample_flags[i].name ; i++) {
1212                 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1213                         name = sample_flags[i].name;
1214                         break;
1215                 }
1216         }
1217
1218         for (i = 0; i < n; i++, flags >>= 1) {
1219                 if (flags & 1)
1220                         str[pos++] = chars[i];
1221         }
1222         for (; i < 32; i++, flags >>= 1) {
1223                 if (flags & 1)
1224                         str[pos++] = '?';
1225         }
1226         str[pos] = 0;
1227
1228         if (name)
1229                 return fprintf(fp, "  %-7s%4s ", name, in_tx ? "(x)" : "");
1230
1231         return fprintf(fp, "  %-11s ", str);
1232 }
1233
1234 struct printer_data {
1235         int line_no;
1236         bool hit_nul;
1237         bool is_printable;
1238 };
1239
1240 static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1241                                       unsigned int val,
1242                                       void *extra, FILE *fp)
1243 {
1244         unsigned char ch = (unsigned char)val;
1245         struct printer_data *printer_data = extra;
1246         int printed = 0;
1247
1248         switch (op) {
1249         case BINARY_PRINT_DATA_BEGIN:
1250                 printed += fprintf(fp, "\n");
1251                 break;
1252         case BINARY_PRINT_LINE_BEGIN:
1253                 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
1254                                                         "           ");
1255                 break;
1256         case BINARY_PRINT_ADDR:
1257                 printed += fprintf(fp, " %04x:", val);
1258                 break;
1259         case BINARY_PRINT_NUM_DATA:
1260                 printed += fprintf(fp, " %02x", val);
1261                 break;
1262         case BINARY_PRINT_NUM_PAD:
1263                 printed += fprintf(fp, "   ");
1264                 break;
1265         case BINARY_PRINT_SEP:
1266                 printed += fprintf(fp, "  ");
1267                 break;
1268         case BINARY_PRINT_CHAR_DATA:
1269                 if (printer_data->hit_nul && ch)
1270                         printer_data->is_printable = false;
1271
1272                 if (!isprint(ch)) {
1273                         printed += fprintf(fp, "%c", '.');
1274
1275                         if (!printer_data->is_printable)
1276                                 break;
1277
1278                         if (ch == '\0')
1279                                 printer_data->hit_nul = true;
1280                         else
1281                                 printer_data->is_printable = false;
1282                 } else {
1283                         printed += fprintf(fp, "%c", ch);
1284                 }
1285                 break;
1286         case BINARY_PRINT_CHAR_PAD:
1287                 printed += fprintf(fp, " ");
1288                 break;
1289         case BINARY_PRINT_LINE_END:
1290                 printed += fprintf(fp, "\n");
1291                 printer_data->line_no++;
1292                 break;
1293         case BINARY_PRINT_DATA_END:
1294         default:
1295                 break;
1296         }
1297
1298         return printed;
1299 }
1300
1301 static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
1302 {
1303         unsigned int nr_bytes = sample->raw_size;
1304         struct printer_data printer_data = {0, false, true};
1305         int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1306                                       sample__fprintf_bpf_output, &printer_data, fp);
1307
1308         if (printer_data.is_printable && printer_data.hit_nul)
1309                 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1310
1311         return printed;
1312 }
1313
1314 static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
1315 {
1316         if (len > 0 && len < spacing)
1317                 return fprintf(fp, "%*s", spacing - len, "");
1318
1319         return 0;
1320 }
1321
1322 static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
1323 {
1324         return perf_sample__fprintf_spacing(len, 34, fp);
1325 }
1326
1327 static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
1328 {
1329         struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1330         int len;
1331
1332         if (perf_sample__bad_synth_size(sample, *data))
1333                 return 0;
1334
1335         len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
1336                      data->ip, le64_to_cpu(data->payload));
1337         return len + perf_sample__fprintf_pt_spacing(len, fp);
1338 }
1339
1340 static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
1341 {
1342         struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1343         int len;
1344
1345         if (perf_sample__bad_synth_size(sample, *data))
1346                 return 0;
1347
1348         len = fprintf(fp, " hints: %#x extensions: %#x ",
1349                       data->hints, data->extensions);
1350         return len + perf_sample__fprintf_pt_spacing(len, fp);
1351 }
1352
1353 static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
1354 {
1355         struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1356         int len;
1357
1358         if (perf_sample__bad_synth_size(sample, *data))
1359                 return 0;
1360
1361         len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1362                       data->hw, data->cstate, data->subcstate);
1363         return len + perf_sample__fprintf_pt_spacing(len, fp);
1364 }
1365
1366 static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
1367 {
1368         struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1369         int len;
1370
1371         if (perf_sample__bad_synth_size(sample, *data))
1372                 return 0;
1373
1374         len = fprintf(fp, " IP: %u ", data->ip);
1375         return len + perf_sample__fprintf_pt_spacing(len, fp);
1376 }
1377
1378 static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
1379 {
1380         struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1381         int len;
1382
1383         if (perf_sample__bad_synth_size(sample, *data))
1384                 return 0;
1385
1386         len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
1387                      data->deepest_cstate, data->last_cstate,
1388                      data->wake_reason);
1389         return len + perf_sample__fprintf_pt_spacing(len, fp);
1390 }
1391
1392 static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
1393 {
1394         struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1395         unsigned int percent, freq;
1396         int len;
1397
1398         if (perf_sample__bad_synth_size(sample, *data))
1399                 return 0;
1400
1401         freq = (le32_to_cpu(data->freq) + 500) / 1000;
1402         len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
1403         if (data->max_nonturbo) {
1404                 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1405                 len += fprintf(fp, "(%3u%%) ", percent);
1406         }
1407         return len + perf_sample__fprintf_pt_spacing(len, fp);
1408 }
1409
1410 static int perf_sample__fprintf_synth(struct perf_sample *sample,
1411                                       struct perf_evsel *evsel, FILE *fp)
1412 {
1413         switch (evsel->attr.config) {
1414         case PERF_SYNTH_INTEL_PTWRITE:
1415                 return perf_sample__fprintf_synth_ptwrite(sample, fp);
1416         case PERF_SYNTH_INTEL_MWAIT:
1417                 return perf_sample__fprintf_synth_mwait(sample, fp);
1418         case PERF_SYNTH_INTEL_PWRE:
1419                 return perf_sample__fprintf_synth_pwre(sample, fp);
1420         case PERF_SYNTH_INTEL_EXSTOP:
1421                 return perf_sample__fprintf_synth_exstop(sample, fp);
1422         case PERF_SYNTH_INTEL_PWRX:
1423                 return perf_sample__fprintf_synth_pwrx(sample, fp);
1424         case PERF_SYNTH_INTEL_CBR:
1425                 return perf_sample__fprintf_synth_cbr(sample, fp);
1426         default:
1427                 break;
1428         }
1429
1430         return 0;
1431 }
1432
1433 struct perf_script {
1434         struct perf_tool        tool;
1435         struct perf_session     *session;
1436         bool                    show_task_events;
1437         bool                    show_mmap_events;
1438         bool                    show_switch_events;
1439         bool                    show_namespace_events;
1440         bool                    allocated;
1441         bool                    per_event_dump;
1442         struct cpu_map          *cpus;
1443         struct thread_map       *threads;
1444         int                     name_width;
1445         const char              *time_str;
1446         struct perf_time_interval ptime;
1447 };
1448
1449 static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1450 {
1451         struct perf_evsel *evsel;
1452         int max = 0;
1453
1454         evlist__for_each_entry(evlist, evsel) {
1455                 int len = strlen(perf_evsel__name(evsel));
1456
1457                 max = MAX(len, max);
1458         }
1459
1460         return max;
1461 }
1462
1463 static int data_src__fprintf(u64 data_src, FILE *fp)
1464 {
1465         struct mem_info mi = { .data_src.val = data_src };
1466         char decode[100];
1467         char out[100];
1468         static int maxlen;
1469         int len;
1470
1471         perf_script__meminfo_scnprintf(decode, 100, &mi);
1472
1473         len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1474         if (maxlen < len)
1475                 maxlen = len;
1476
1477         return fprintf(fp, "%-*s", maxlen, out);
1478 }
1479
1480 static void process_event(struct perf_script *script,
1481                           struct perf_sample *sample, struct perf_evsel *evsel,
1482                           struct addr_location *al,
1483                           struct machine *machine)
1484 {
1485         struct thread *thread = al->thread;
1486         struct perf_event_attr *attr = &evsel->attr;
1487         unsigned int type = output_type(attr->type);
1488         struct perf_evsel_script *es = evsel->priv;
1489         FILE *fp = es->fp;
1490
1491         if (output[type].fields == 0)
1492                 return;
1493
1494         ++es->samples;
1495
1496         perf_sample__fprintf_start(sample, thread, evsel, fp);
1497
1498         if (PRINT_FIELD(PERIOD))
1499                 fprintf(fp, "%10" PRIu64 " ", sample->period);
1500
1501         if (PRINT_FIELD(EVNAME)) {
1502                 const char *evname = perf_evsel__name(evsel);
1503
1504                 if (!script->name_width)
1505                         script->name_width = perf_evlist__max_name_len(script->session->evlist);
1506
1507                 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
1508         }
1509
1510         if (print_flags)
1511                 perf_sample__fprintf_flags(sample->flags, fp);
1512
1513         if (is_bts_event(attr)) {
1514                 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
1515                 return;
1516         }
1517
1518         if (PRINT_FIELD(TRACE)) {
1519                 event_format__fprintf(evsel->tp_format, sample->cpu,
1520                                       sample->raw_data, sample->raw_size, fp);
1521         }
1522
1523         if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
1524                 perf_sample__fprintf_synth(sample, evsel, fp);
1525
1526         if (PRINT_FIELD(ADDR))
1527                 perf_sample__fprintf_addr(sample, thread, attr, fp);
1528
1529         if (PRINT_FIELD(DATA_SRC))
1530                 data_src__fprintf(sample->data_src, fp);
1531
1532         if (PRINT_FIELD(WEIGHT))
1533                 fprintf(fp, "%16" PRIu64, sample->weight);
1534
1535         if (PRINT_FIELD(IP)) {
1536                 struct callchain_cursor *cursor = NULL;
1537
1538                 if (symbol_conf.use_callchain && sample->callchain &&
1539                     thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
1540                                               sample, NULL, NULL, scripting_max_stack) == 0)
1541                         cursor = &callchain_cursor;
1542
1543                 fputc(cursor ? '\n' : ' ', fp);
1544                 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
1545         }
1546
1547         if (PRINT_FIELD(IREGS))
1548                 perf_sample__fprintf_iregs(sample, attr, fp);
1549
1550         if (PRINT_FIELD(UREGS))
1551                 perf_sample__fprintf_uregs(sample, attr, fp);
1552
1553         if (PRINT_FIELD(BRSTACK))
1554                 perf_sample__fprintf_brstack(sample, thread, attr, fp);
1555         else if (PRINT_FIELD(BRSTACKSYM))
1556                 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
1557         else if (PRINT_FIELD(BRSTACKOFF))
1558                 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
1559
1560         if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1561                 perf_sample__fprintf_bpf_output(sample, fp);
1562         perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1563
1564         if (PRINT_FIELD(PHYS_ADDR))
1565                 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1566         fprintf(fp, "\n");
1567 }
1568
1569 static struct scripting_ops     *scripting_ops;
1570
1571 static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1572 {
1573         int nthreads = thread_map__nr(counter->threads);
1574         int ncpus = perf_evsel__nr_cpus(counter);
1575         int cpu, thread;
1576         static int header_printed;
1577
1578         if (counter->system_wide)
1579                 nthreads = 1;
1580
1581         if (!header_printed) {
1582                 printf("%3s %8s %15s %15s %15s %15s %s\n",
1583                        "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1584                 header_printed = 1;
1585         }
1586
1587         for (thread = 0; thread < nthreads; thread++) {
1588                 for (cpu = 0; cpu < ncpus; cpu++) {
1589                         struct perf_counts_values *counts;
1590
1591                         counts = perf_counts(counter->counts, cpu, thread);
1592
1593                         printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1594                                 counter->cpus->map[cpu],
1595                                 thread_map__pid(counter->threads, thread),
1596                                 counts->val,
1597                                 counts->ena,
1598                                 counts->run,
1599                                 tstamp,
1600                                 perf_evsel__name(counter));
1601                 }
1602         }
1603 }
1604
1605 static void process_stat(struct perf_evsel *counter, u64 tstamp)
1606 {
1607         if (scripting_ops && scripting_ops->process_stat)
1608                 scripting_ops->process_stat(&stat_config, counter, tstamp);
1609         else
1610                 __process_stat(counter, tstamp);
1611 }
1612
1613 static void process_stat_interval(u64 tstamp)
1614 {
1615         if (scripting_ops && scripting_ops->process_stat_interval)
1616                 scripting_ops->process_stat_interval(tstamp);
1617 }
1618
1619 static void setup_scripting(void)
1620 {
1621         setup_perl_scripting();
1622         setup_python_scripting();
1623 }
1624
1625 static int flush_scripting(void)
1626 {
1627         return scripting_ops ? scripting_ops->flush_script() : 0;
1628 }
1629
1630 static int cleanup_scripting(void)
1631 {
1632         pr_debug("\nperf script stopped\n");
1633
1634         return scripting_ops ? scripting_ops->stop_script() : 0;
1635 }
1636
1637 static int process_sample_event(struct perf_tool *tool,
1638                                 union perf_event *event,
1639                                 struct perf_sample *sample,
1640                                 struct perf_evsel *evsel,
1641                                 struct machine *machine)
1642 {
1643         struct perf_script *scr = container_of(tool, struct perf_script, tool);
1644         struct addr_location al;
1645
1646         if (perf_time__skip_sample(&scr->ptime, sample->time))
1647                 return 0;
1648
1649         if (debug_mode) {
1650                 if (sample->time < last_timestamp) {
1651                         pr_err("Samples misordered, previous: %" PRIu64
1652                                 " this: %" PRIu64 "\n", last_timestamp,
1653                                 sample->time);
1654                         nr_unordered++;
1655                 }
1656                 last_timestamp = sample->time;
1657                 return 0;
1658         }
1659
1660         if (machine__resolve(machine, &al, sample) < 0) {
1661                 pr_err("problem processing %d event, skipping it.\n",
1662                        event->header.type);
1663                 return -1;
1664         }
1665
1666         if (al.filtered)
1667                 goto out_put;
1668
1669         if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
1670                 goto out_put;
1671
1672         if (scripting_ops)
1673                 scripting_ops->process_event(event, sample, evsel, &al);
1674         else
1675                 process_event(scr, sample, evsel, &al, machine);
1676
1677 out_put:
1678         addr_location__put(&al);
1679         return 0;
1680 }
1681
1682 static int process_attr(struct perf_tool *tool, union perf_event *event,
1683                         struct perf_evlist **pevlist)
1684 {
1685         struct perf_script *scr = container_of(tool, struct perf_script, tool);
1686         struct perf_evlist *evlist;
1687         struct perf_evsel *evsel, *pos;
1688         int err;
1689
1690         err = perf_event__process_attr(tool, event, pevlist);
1691         if (err)
1692                 return err;
1693
1694         evlist = *pevlist;
1695         evsel = perf_evlist__last(*pevlist);
1696
1697         if (evsel->attr.type >= PERF_TYPE_MAX &&
1698             evsel->attr.type != PERF_TYPE_SYNTH)
1699                 return 0;
1700
1701         evlist__for_each_entry(evlist, pos) {
1702                 if (pos->attr.type == evsel->attr.type && pos != evsel)
1703                         return 0;
1704         }
1705
1706         set_print_ip_opts(&evsel->attr);
1707
1708         if (evsel->attr.sample_type)
1709                 err = perf_evsel__check_attr(evsel, scr->session);
1710
1711         return err;
1712 }
1713
1714 static int process_comm_event(struct perf_tool *tool,
1715                               union perf_event *event,
1716                               struct perf_sample *sample,
1717                               struct machine *machine)
1718 {
1719         struct thread *thread;
1720         struct perf_script *script = container_of(tool, struct perf_script, tool);
1721         struct perf_session *session = script->session;
1722         struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1723         int ret = -1;
1724
1725         thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1726         if (thread == NULL) {
1727                 pr_debug("problem processing COMM event, skipping it.\n");
1728                 return -1;
1729         }
1730
1731         if (perf_event__process_comm(tool, event, sample, machine) < 0)
1732                 goto out;
1733
1734         if (!evsel->attr.sample_id_all) {
1735                 sample->cpu = 0;
1736                 sample->time = 0;
1737                 sample->tid = event->comm.tid;
1738                 sample->pid = event->comm.pid;
1739         }
1740         perf_sample__fprintf_start(sample, thread, evsel, stdout);
1741         perf_event__fprintf(event, stdout);
1742         ret = 0;
1743 out:
1744         thread__put(thread);
1745         return ret;
1746 }
1747
1748 static int process_namespaces_event(struct perf_tool *tool,
1749                                     union perf_event *event,
1750                                     struct perf_sample *sample,
1751                                     struct machine *machine)
1752 {
1753         struct thread *thread;
1754         struct perf_script *script = container_of(tool, struct perf_script, tool);
1755         struct perf_session *session = script->session;
1756         struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1757         int ret = -1;
1758
1759         thread = machine__findnew_thread(machine, event->namespaces.pid,
1760                                          event->namespaces.tid);
1761         if (thread == NULL) {
1762                 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1763                 return -1;
1764         }
1765
1766         if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1767                 goto out;
1768
1769         if (!evsel->attr.sample_id_all) {
1770                 sample->cpu = 0;
1771                 sample->time = 0;
1772                 sample->tid = event->namespaces.tid;
1773                 sample->pid = event->namespaces.pid;
1774         }
1775         perf_sample__fprintf_start(sample, thread, evsel, stdout);
1776         perf_event__fprintf(event, stdout);
1777         ret = 0;
1778 out:
1779         thread__put(thread);
1780         return ret;
1781 }
1782
1783 static int process_fork_event(struct perf_tool *tool,
1784                               union perf_event *event,
1785                               struct perf_sample *sample,
1786                               struct machine *machine)
1787 {
1788         struct thread *thread;
1789         struct perf_script *script = container_of(tool, struct perf_script, tool);
1790         struct perf_session *session = script->session;
1791         struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1792
1793         if (perf_event__process_fork(tool, event, sample, machine) < 0)
1794                 return -1;
1795
1796         thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1797         if (thread == NULL) {
1798                 pr_debug("problem processing FORK event, skipping it.\n");
1799                 return -1;
1800         }
1801
1802         if (!evsel->attr.sample_id_all) {
1803                 sample->cpu = 0;
1804                 sample->time = event->fork.time;
1805                 sample->tid = event->fork.tid;
1806                 sample->pid = event->fork.pid;
1807         }
1808         perf_sample__fprintf_start(sample, thread, evsel, stdout);
1809         perf_event__fprintf(event, stdout);
1810         thread__put(thread);
1811
1812         return 0;
1813 }
1814 static int process_exit_event(struct perf_tool *tool,
1815                               union perf_event *event,
1816                               struct perf_sample *sample,
1817                               struct machine *machine)
1818 {
1819         int err = 0;
1820         struct thread *thread;
1821         struct perf_script *script = container_of(tool, struct perf_script, tool);
1822         struct perf_session *session = script->session;
1823         struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1824
1825         thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1826         if (thread == NULL) {
1827                 pr_debug("problem processing EXIT event, skipping it.\n");
1828                 return -1;
1829         }
1830
1831         if (!evsel->attr.sample_id_all) {
1832                 sample->cpu = 0;
1833                 sample->time = 0;
1834                 sample->tid = event->fork.tid;
1835                 sample->pid = event->fork.pid;
1836         }
1837         perf_sample__fprintf_start(sample, thread, evsel, stdout);
1838         perf_event__fprintf(event, stdout);
1839
1840         if (perf_event__process_exit(tool, event, sample, machine) < 0)
1841                 err = -1;
1842
1843         thread__put(thread);
1844         return err;
1845 }
1846
1847 static int process_mmap_event(struct perf_tool *tool,
1848                               union perf_event *event,
1849                               struct perf_sample *sample,
1850                               struct machine *machine)
1851 {
1852         struct thread *thread;
1853         struct perf_script *script = container_of(tool, struct perf_script, tool);
1854         struct perf_session *session = script->session;
1855         struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1856
1857         if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1858                 return -1;
1859
1860         thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1861         if (thread == NULL) {
1862                 pr_debug("problem processing MMAP event, skipping it.\n");
1863                 return -1;
1864         }
1865
1866         if (!evsel->attr.sample_id_all) {
1867                 sample->cpu = 0;
1868                 sample->time = 0;
1869                 sample->tid = event->mmap.tid;
1870                 sample->pid = event->mmap.pid;
1871         }
1872         perf_sample__fprintf_start(sample, thread, evsel, stdout);
1873         perf_event__fprintf(event, stdout);
1874         thread__put(thread);
1875         return 0;
1876 }
1877
1878 static int process_mmap2_event(struct perf_tool *tool,
1879                               union perf_event *event,
1880                               struct perf_sample *sample,
1881                               struct machine *machine)
1882 {
1883         struct thread *thread;
1884         struct perf_script *script = container_of(tool, struct perf_script, tool);
1885         struct perf_session *session = script->session;
1886         struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1887
1888         if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1889                 return -1;
1890
1891         thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1892         if (thread == NULL) {
1893                 pr_debug("problem processing MMAP2 event, skipping it.\n");
1894                 return -1;
1895         }
1896
1897         if (!evsel->attr.sample_id_all) {
1898                 sample->cpu = 0;
1899                 sample->time = 0;
1900                 sample->tid = event->mmap2.tid;
1901                 sample->pid = event->mmap2.pid;
1902         }
1903         perf_sample__fprintf_start(sample, thread, evsel, stdout);
1904         perf_event__fprintf(event, stdout);
1905         thread__put(thread);
1906         return 0;
1907 }
1908
1909 static int process_switch_event(struct perf_tool *tool,
1910                                 union perf_event *event,
1911                                 struct perf_sample *sample,
1912                                 struct machine *machine)
1913 {
1914         struct thread *thread;
1915         struct perf_script *script = container_of(tool, struct perf_script, tool);
1916         struct perf_session *session = script->session;
1917         struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1918
1919         if (perf_event__process_switch(tool, event, sample, machine) < 0)
1920                 return -1;
1921
1922         thread = machine__findnew_thread(machine, sample->pid,
1923                                          sample->tid);
1924         if (thread == NULL) {
1925                 pr_debug("problem processing SWITCH event, skipping it.\n");
1926                 return -1;
1927         }
1928
1929         perf_sample__fprintf_start(sample, thread, evsel, stdout);
1930         perf_event__fprintf(event, stdout);
1931         thread__put(thread);
1932         return 0;
1933 }
1934
1935 static void sig_handler(int sig __maybe_unused)
1936 {
1937         session_done = 1;
1938 }
1939
1940 static void perf_script__fclose_per_event_dump(struct perf_script *script)
1941 {
1942         struct perf_evlist *evlist = script->session->evlist;
1943         struct perf_evsel *evsel;
1944
1945         evlist__for_each_entry(evlist, evsel) {
1946                 if (!evsel->priv)
1947                         break;
1948                 perf_evsel_script__delete(evsel->priv);
1949                 evsel->priv = NULL;
1950         }
1951 }
1952
1953 static int perf_script__fopen_per_event_dump(struct perf_script *script)
1954 {
1955         struct perf_evsel *evsel;
1956
1957         evlist__for_each_entry(script->session->evlist, evsel) {
1958                 /*
1959                  * Already setup? I.e. we may be called twice in cases like
1960                  * Intel PT, one for the intel_pt// and dummy events, then
1961                  * for the evsels syntheized from the auxtrace info.
1962                  *
1963                  * Ses perf_script__process_auxtrace_info.
1964                  */
1965                 if (evsel->priv != NULL)
1966                         continue;
1967
1968                 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
1969                 if (evsel->priv == NULL)
1970                         goto out_err_fclose;
1971         }
1972
1973         return 0;
1974
1975 out_err_fclose:
1976         perf_script__fclose_per_event_dump(script);
1977         return -1;
1978 }
1979
1980 static int perf_script__setup_per_event_dump(struct perf_script *script)
1981 {
1982         struct perf_evsel *evsel;
1983         static struct perf_evsel_script es_stdout;
1984
1985         if (script->per_event_dump)
1986                 return perf_script__fopen_per_event_dump(script);
1987
1988         es_stdout.fp = stdout;
1989
1990         evlist__for_each_entry(script->session->evlist, evsel)
1991                 evsel->priv = &es_stdout;
1992
1993         return 0;
1994 }
1995
1996 static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
1997 {
1998         struct perf_evsel *evsel;
1999
2000         evlist__for_each_entry(script->session->evlist, evsel) {
2001                 struct perf_evsel_script *es = evsel->priv;
2002
2003                 perf_evsel_script__fprintf(es, stdout);
2004                 perf_evsel_script__delete(es);
2005                 evsel->priv = NULL;
2006         }
2007 }
2008
2009 static int __cmd_script(struct perf_script *script)
2010 {
2011         int ret;
2012
2013         signal(SIGINT, sig_handler);
2014
2015         /* override event processing functions */
2016         if (script->show_task_events) {
2017                 script->tool.comm = process_comm_event;
2018                 script->tool.fork = process_fork_event;
2019                 script->tool.exit = process_exit_event;
2020         }
2021         if (script->show_mmap_events) {
2022                 script->tool.mmap = process_mmap_event;
2023                 script->tool.mmap2 = process_mmap2_event;
2024         }
2025         if (script->show_switch_events)
2026                 script->tool.context_switch = process_switch_event;
2027         if (script->show_namespace_events)
2028                 script->tool.namespaces = process_namespaces_event;
2029
2030         if (perf_script__setup_per_event_dump(script)) {
2031                 pr_err("Couldn't create the per event dump files\n");
2032                 return -1;
2033         }
2034
2035         ret = perf_session__process_events(script->session);
2036
2037         if (script->per_event_dump)
2038                 perf_script__exit_per_event_dump_stats(script);
2039
2040         if (debug_mode)
2041                 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
2042
2043         return ret;
2044 }
2045
2046 struct script_spec {
2047         struct list_head        node;
2048         struct scripting_ops    *ops;
2049         char                    spec[0];
2050 };
2051
2052 static LIST_HEAD(script_specs);
2053
2054 static struct script_spec *script_spec__new(const char *spec,
2055                                             struct scripting_ops *ops)
2056 {
2057         struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2058
2059         if (s != NULL) {
2060                 strcpy(s->spec, spec);
2061                 s->ops = ops;
2062         }
2063
2064         return s;
2065 }
2066
2067 static void script_spec__add(struct script_spec *s)
2068 {
2069         list_add_tail(&s->node, &script_specs);
2070 }
2071
2072 static struct script_spec *script_spec__find(const char *spec)
2073 {
2074         struct script_spec *s;
2075
2076         list_for_each_entry(s, &script_specs, node)
2077                 if (strcasecmp(s->spec, spec) == 0)
2078                         return s;
2079         return NULL;
2080 }
2081
2082 int script_spec_register(const char *spec, struct scripting_ops *ops)
2083 {
2084         struct script_spec *s;
2085
2086         s = script_spec__find(spec);
2087         if (s)
2088                 return -1;
2089
2090         s = script_spec__new(spec, ops);
2091         if (!s)
2092                 return -1;
2093         else
2094                 script_spec__add(s);
2095
2096         return 0;
2097 }
2098
2099 static struct scripting_ops *script_spec__lookup(const char *spec)
2100 {
2101         struct script_spec *s = script_spec__find(spec);
2102         if (!s)
2103                 return NULL;
2104
2105         return s->ops;
2106 }
2107
2108 static void list_available_languages(void)
2109 {
2110         struct script_spec *s;
2111
2112         fprintf(stderr, "\n");
2113         fprintf(stderr, "Scripting language extensions (used in "
2114                 "perf script -s [spec:]script.[spec]):\n\n");
2115
2116         list_for_each_entry(s, &script_specs, node)
2117                 fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name);
2118
2119         fprintf(stderr, "\n");
2120 }
2121
2122 static int parse_scriptname(const struct option *opt __maybe_unused,
2123                             const char *str, int unset __maybe_unused)
2124 {
2125         char spec[PATH_MAX];
2126         const char *script, *ext;
2127         int len;
2128
2129         if (strcmp(str, "lang") == 0) {
2130                 list_available_languages();
2131                 exit(0);
2132         }
2133
2134         script = strchr(str, ':');
2135         if (script) {
2136                 len = script - str;
2137                 if (len >= PATH_MAX) {
2138                         fprintf(stderr, "invalid language specifier");
2139                         return -1;
2140                 }
2141                 strncpy(spec, str, len);
2142                 spec[len] = '\0';
2143                 scripting_ops = script_spec__lookup(spec);
2144                 if (!scripting_ops) {
2145                         fprintf(stderr, "invalid language specifier");
2146                         return -1;
2147                 }
2148                 script++;
2149         } else {
2150                 script = str;
2151                 ext = strrchr(script, '.');
2152                 if (!ext) {
2153                         fprintf(stderr, "invalid script extension");
2154                         return -1;
2155                 }
2156                 scripting_ops = script_spec__lookup(++ext);
2157                 if (!scripting_ops) {
2158                         fprintf(stderr, "invalid script extension");
2159                         return -1;
2160                 }
2161         }
2162
2163         script_name = strdup(script);
2164
2165         return 0;
2166 }
2167
2168 static int parse_output_fields(const struct option *opt __maybe_unused,
2169                             const char *arg, int unset __maybe_unused)
2170 {
2171         char *tok, *strtok_saveptr = NULL;
2172         int i, imax = ARRAY_SIZE(all_output_options);
2173         int j;
2174         int rc = 0;
2175         char *str = strdup(arg);
2176         int type = -1;
2177         enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
2178
2179         if (!str)
2180                 return -ENOMEM;
2181
2182         /* first word can state for which event type the user is specifying
2183          * the fields. If no type exists, the specified fields apply to all
2184          * event types found in the file minus the invalid fields for a type.
2185          */
2186         tok = strchr(str, ':');
2187         if (tok) {
2188                 *tok = '\0';
2189                 tok++;
2190                 if (!strcmp(str, "hw"))
2191                         type = PERF_TYPE_HARDWARE;
2192                 else if (!strcmp(str, "sw"))
2193                         type = PERF_TYPE_SOFTWARE;
2194                 else if (!strcmp(str, "trace"))
2195                         type = PERF_TYPE_TRACEPOINT;
2196                 else if (!strcmp(str, "raw"))
2197                         type = PERF_TYPE_RAW;
2198                 else if (!strcmp(str, "break"))
2199                         type = PERF_TYPE_BREAKPOINT;
2200                 else if (!strcmp(str, "synth"))
2201                         type = OUTPUT_TYPE_SYNTH;
2202                 else {
2203                         fprintf(stderr, "Invalid event type in field string.\n");
2204                         rc = -EINVAL;
2205                         goto out;
2206                 }
2207
2208                 if (output[type].user_set)
2209                         pr_warning("Overriding previous field request for %s events.\n",
2210                                    event_type(type));
2211
2212                 output[type].fields = 0;
2213                 output[type].user_set = true;
2214                 output[type].wildcard_set = false;
2215
2216         } else {
2217                 tok = str;
2218                 if (strlen(str) == 0) {
2219                         fprintf(stderr,
2220                                 "Cannot set fields to 'none' for all event types.\n");
2221                         rc = -EINVAL;
2222                         goto out;
2223                 }
2224
2225                 /* Don't override defaults for +- */
2226                 if (strchr(str, '+') || strchr(str, '-'))
2227                         goto parse;
2228
2229                 if (output_set_by_user())
2230                         pr_warning("Overriding previous field request for all events.\n");
2231
2232                 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2233                         output[j].fields = 0;
2234                         output[j].user_set = true;
2235                         output[j].wildcard_set = true;
2236                 }
2237         }
2238
2239 parse:
2240         for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
2241                 if (*tok == '+') {
2242                         if (change == SET)
2243                                 goto out_badmix;
2244                         change = ADD;
2245                         tok++;
2246                 } else if (*tok == '-') {
2247                         if (change == SET)
2248                                 goto out_badmix;
2249                         change = REMOVE;
2250                         tok++;
2251                 } else {
2252                         if (change != SET && change != DEFAULT)
2253                                 goto out_badmix;
2254                         change = SET;
2255                 }
2256
2257                 for (i = 0; i < imax; ++i) {
2258                         if (strcmp(tok, all_output_options[i].str) == 0)
2259                                 break;
2260                 }
2261                 if (i == imax && strcmp(tok, "flags") == 0) {
2262                         print_flags = change == REMOVE ? false : true;
2263                         continue;
2264                 }
2265                 if (i == imax) {
2266                         fprintf(stderr, "Invalid field requested.\n");
2267                         rc = -EINVAL;
2268                         goto out;
2269                 }
2270
2271                 if (type == -1) {
2272                         /* add user option to all events types for
2273                          * which it is valid
2274                          */
2275                         for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2276                                 if (output[j].invalid_fields & all_output_options[i].field) {
2277                                         pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2278                                                    all_output_options[i].str, event_type(j));
2279                                 } else {
2280                                         if (change == REMOVE)
2281                                                 output[j].fields &= ~all_output_options[i].field;
2282                                         else
2283                                                 output[j].fields |= all_output_options[i].field;
2284                                 }
2285                         }
2286                 } else {
2287                         if (output[type].invalid_fields & all_output_options[i].field) {
2288                                 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2289                                          all_output_options[i].str, event_type(type));
2290
2291                                 rc = -EINVAL;
2292                                 goto out;
2293                         }
2294                         output[type].fields |= all_output_options[i].field;
2295                 }
2296         }
2297
2298         if (type >= 0) {
2299                 if (output[type].fields == 0) {
2300                         pr_debug("No fields requested for %s type. "
2301                                  "Events will not be displayed.\n", event_type(type));
2302                 }
2303         }
2304         goto out;
2305
2306 out_badmix:
2307         fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2308         rc = -EINVAL;
2309 out:
2310         free(str);
2311         return rc;
2312 }
2313
2314 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2315 static int is_directory(const char *base_path, const struct dirent *dent)
2316 {
2317         char path[PATH_MAX];
2318         struct stat st;
2319
2320         sprintf(path, "%s/%s", base_path, dent->d_name);
2321         if (stat(path, &st))
2322                 return 0;
2323
2324         return S_ISDIR(st.st_mode);
2325 }
2326
2327 #define for_each_lang(scripts_path, scripts_dir, lang_dirent)           \
2328         while ((lang_dirent = readdir(scripts_dir)) != NULL)            \
2329                 if ((lang_dirent->d_type == DT_DIR ||                   \
2330                      (lang_dirent->d_type == DT_UNKNOWN &&              \
2331                       is_directory(scripts_path, lang_dirent))) &&      \
2332                     (strcmp(lang_dirent->d_name, ".")) &&               \
2333                     (strcmp(lang_dirent->d_name, "..")))
2334
2335 #define for_each_script(lang_path, lang_dir, script_dirent)             \
2336         while ((script_dirent = readdir(lang_dir)) != NULL)             \
2337                 if (script_dirent->d_type != DT_DIR &&                  \
2338                     (script_dirent->d_type != DT_UNKNOWN ||             \
2339                      !is_directory(lang_path, script_dirent)))
2340
2341
2342 #define RECORD_SUFFIX                   "-record"
2343 #define REPORT_SUFFIX                   "-report"
2344
2345 struct script_desc {
2346         struct list_head        node;
2347         char                    *name;
2348         char                    *half_liner;
2349         char                    *args;
2350 };
2351
2352 static LIST_HEAD(script_descs);
2353
2354 static struct script_desc *script_desc__new(const char *name)
2355 {
2356         struct script_desc *s = zalloc(sizeof(*s));
2357
2358         if (s != NULL && name)
2359                 s->name = strdup(name);
2360
2361         return s;
2362 }
2363
2364 static void script_desc__delete(struct script_desc *s)
2365 {
2366         zfree(&s->name);
2367         zfree(&s->half_liner);
2368         zfree(&s->args);
2369         free(s);
2370 }
2371
2372 static void script_desc__add(struct script_desc *s)
2373 {
2374         list_add_tail(&s->node, &script_descs);
2375 }
2376
2377 static struct script_desc *script_desc__find(const char *name)
2378 {
2379         struct script_desc *s;
2380
2381         list_for_each_entry(s, &script_descs, node)
2382                 if (strcasecmp(s->name, name) == 0)
2383                         return s;
2384         return NULL;
2385 }
2386
2387 static struct script_desc *script_desc__findnew(const char *name)
2388 {
2389         struct script_desc *s = script_desc__find(name);
2390
2391         if (s)
2392                 return s;
2393
2394         s = script_desc__new(name);
2395         if (!s)
2396                 return NULL;
2397
2398         script_desc__add(s);
2399
2400         return s;
2401 }
2402
2403 static const char *ends_with(const char *str, const char *suffix)
2404 {
2405         size_t suffix_len = strlen(suffix);
2406         const char *p = str;
2407
2408         if (strlen(str) > suffix_len) {
2409                 p = str + strlen(str) - suffix_len;
2410                 if (!strncmp(p, suffix, suffix_len))
2411                         return p;
2412         }
2413
2414         return NULL;
2415 }
2416
2417 static int read_script_info(struct script_desc *desc, const char *filename)
2418 {
2419         char line[BUFSIZ], *p;
2420         FILE *fp;
2421
2422         fp = fopen(filename, "r");
2423         if (!fp)
2424                 return -1;
2425
2426         while (fgets(line, sizeof(line), fp)) {
2427                 p = ltrim(line);
2428                 if (strlen(p) == 0)
2429                         continue;
2430                 if (*p != '#')
2431                         continue;
2432                 p++;
2433                 if (strlen(p) && *p == '!')
2434                         continue;
2435
2436                 p = ltrim(p);
2437                 if (strlen(p) && p[strlen(p) - 1] == '\n')
2438                         p[strlen(p) - 1] = '\0';
2439
2440                 if (!strncmp(p, "description:", strlen("description:"))) {
2441                         p += strlen("description:");
2442                         desc->half_liner = strdup(ltrim(p));
2443                         continue;
2444                 }
2445
2446                 if (!strncmp(p, "args:", strlen("args:"))) {
2447                         p += strlen("args:");
2448                         desc->args = strdup(ltrim(p));
2449                         continue;
2450                 }
2451         }
2452
2453         fclose(fp);
2454
2455         return 0;
2456 }
2457
2458 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2459 {
2460         char *script_root, *str;
2461
2462         script_root = strdup(script_dirent->d_name);
2463         if (!script_root)
2464                 return NULL;
2465
2466         str = (char *)ends_with(script_root, suffix);
2467         if (!str) {
2468                 free(script_root);
2469                 return NULL;
2470         }
2471
2472         *str = '\0';
2473         return script_root;
2474 }
2475
2476 static int list_available_scripts(const struct option *opt __maybe_unused,
2477                                   const char *s __maybe_unused,
2478                                   int unset __maybe_unused)
2479 {
2480         struct dirent *script_dirent, *lang_dirent;
2481         char scripts_path[MAXPATHLEN];
2482         DIR *scripts_dir, *lang_dir;
2483         char script_path[MAXPATHLEN];
2484         char lang_path[MAXPATHLEN];
2485         struct script_desc *desc;
2486         char first_half[BUFSIZ];
2487         char *script_root;
2488
2489         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2490
2491         scripts_dir = opendir(scripts_path);
2492         if (!scripts_dir) {
2493                 fprintf(stdout,
2494                         "open(%s) failed.\n"
2495                         "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2496                         scripts_path);
2497                 exit(-1);
2498         }
2499
2500         for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2501                 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2502                          lang_dirent->d_name);
2503                 lang_dir = opendir(lang_path);
2504                 if (!lang_dir)
2505                         continue;
2506
2507                 for_each_script(lang_path, lang_dir, script_dirent) {
2508                         script_root = get_script_root(script_dirent, REPORT_SUFFIX);
2509                         if (script_root) {
2510                                 desc = script_desc__findnew(script_root);
2511                                 snprintf(script_path, MAXPATHLEN, "%s/%s",
2512                                          lang_path, script_dirent->d_name);
2513                                 read_script_info(desc, script_path);
2514                                 free(script_root);
2515                         }
2516                 }
2517         }
2518
2519         fprintf(stdout, "List of available trace scripts:\n");
2520         list_for_each_entry(desc, &script_descs, node) {
2521                 sprintf(first_half, "%s %s", desc->name,
2522                         desc->args ? desc->args : "");
2523                 fprintf(stdout, "  %-36s %s\n", first_half,
2524                         desc->half_liner ? desc->half_liner : "");
2525         }
2526
2527         exit(0);
2528 }
2529
2530 /*
2531  * Some scripts specify the required events in their "xxx-record" file,
2532  * this function will check if the events in perf.data match those
2533  * mentioned in the "xxx-record".
2534  *
2535  * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2536  * which is covered well now. And new parsing code should be added to
2537  * cover the future complexing formats like event groups etc.
2538  */
2539 static int check_ev_match(char *dir_name, char *scriptname,
2540                         struct perf_session *session)
2541 {
2542         char filename[MAXPATHLEN], evname[128];
2543         char line[BUFSIZ], *p;
2544         struct perf_evsel *pos;
2545         int match, len;
2546         FILE *fp;
2547
2548         sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2549
2550         fp = fopen(filename, "r");
2551         if (!fp)
2552                 return -1;
2553
2554         while (fgets(line, sizeof(line), fp)) {
2555                 p = ltrim(line);
2556                 if (*p == '#')
2557                         continue;
2558
2559                 while (strlen(p)) {
2560                         p = strstr(p, "-e");
2561                         if (!p)
2562                                 break;
2563
2564                         p += 2;
2565                         p = ltrim(p);
2566                         len = strcspn(p, " \t");
2567                         if (!len)
2568                                 break;
2569
2570                         snprintf(evname, len + 1, "%s", p);
2571
2572                         match = 0;
2573                         evlist__for_each_entry(session->evlist, pos) {
2574                                 if (!strcmp(perf_evsel__name(pos), evname)) {
2575                                         match = 1;
2576                                         break;
2577                                 }
2578                         }
2579
2580                         if (!match) {
2581                                 fclose(fp);
2582                                 return -1;
2583                         }
2584                 }
2585         }
2586
2587         fclose(fp);
2588         return 0;
2589 }
2590
2591 /*
2592  * Return -1 if none is found, otherwise the actual scripts number.
2593  *
2594  * Currently the only user of this function is the script browser, which
2595  * will list all statically runnable scripts, select one, execute it and
2596  * show the output in a perf browser.
2597  */
2598 int find_scripts(char **scripts_array, char **scripts_path_array)
2599 {
2600         struct dirent *script_dirent, *lang_dirent;
2601         char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
2602         DIR *scripts_dir, *lang_dir;
2603         struct perf_session *session;
2604         struct perf_data data = {
2605                 .file      = {
2606                         .path = input_name,
2607                 },
2608                 .mode      = PERF_DATA_MODE_READ,
2609         };
2610         char *temp;
2611         int i = 0;
2612
2613         session = perf_session__new(&data, false, NULL);
2614         if (!session)
2615                 return -1;
2616
2617         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2618
2619         scripts_dir = opendir(scripts_path);
2620         if (!scripts_dir) {
2621                 perf_session__delete(session);
2622                 return -1;
2623         }
2624
2625         for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2626                 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2627                          lang_dirent->d_name);
2628 #ifdef NO_LIBPERL
2629                 if (strstr(lang_path, "perl"))
2630                         continue;
2631 #endif
2632 #ifdef NO_LIBPYTHON
2633                 if (strstr(lang_path, "python"))
2634                         continue;
2635 #endif
2636
2637                 lang_dir = opendir(lang_path);
2638                 if (!lang_dir)
2639                         continue;
2640
2641                 for_each_script(lang_path, lang_dir, script_dirent) {
2642                         /* Skip those real time scripts: xxxtop.p[yl] */
2643                         if (strstr(script_dirent->d_name, "top."))
2644                                 continue;
2645                         sprintf(scripts_path_array[i], "%s/%s", lang_path,
2646                                 script_dirent->d_name);
2647                         temp = strchr(script_dirent->d_name, '.');
2648                         snprintf(scripts_array[i],
2649                                 (temp - script_dirent->d_name) + 1,
2650                                 "%s", script_dirent->d_name);
2651
2652                         if (check_ev_match(lang_path,
2653                                         scripts_array[i], session))
2654                                 continue;
2655
2656                         i++;
2657                 }
2658                 closedir(lang_dir);
2659         }
2660
2661         closedir(scripts_dir);
2662         perf_session__delete(session);
2663         return i;
2664 }
2665
2666 static char *get_script_path(const char *script_root, const char *suffix)
2667 {
2668         struct dirent *script_dirent, *lang_dirent;
2669         char scripts_path[MAXPATHLEN];
2670         char script_path[MAXPATHLEN];
2671         DIR *scripts_dir, *lang_dir;
2672         char lang_path[MAXPATHLEN];
2673         char *__script_root;
2674
2675         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2676
2677         scripts_dir = opendir(scripts_path);
2678         if (!scripts_dir)
2679                 return NULL;
2680
2681         for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2682                 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2683                          lang_dirent->d_name);
2684                 lang_dir = opendir(lang_path);
2685                 if (!lang_dir)
2686                         continue;
2687
2688                 for_each_script(lang_path, lang_dir, script_dirent) {
2689                         __script_root = get_script_root(script_dirent, suffix);
2690                         if (__script_root && !strcmp(script_root, __script_root)) {
2691                                 free(__script_root);
2692                                 closedir(lang_dir);
2693                                 closedir(scripts_dir);
2694                                 snprintf(script_path, MAXPATHLEN, "%s/%s",
2695                                          lang_path, script_dirent->d_name);
2696                                 return strdup(script_path);
2697                         }
2698                         free(__script_root);
2699                 }
2700                 closedir(lang_dir);
2701         }
2702         closedir(scripts_dir);
2703
2704         return NULL;
2705 }
2706
2707 static bool is_top_script(const char *script_path)
2708 {
2709         return ends_with(script_path, "top") == NULL ? false : true;
2710 }
2711
2712 static int has_required_arg(char *script_path)
2713 {
2714         struct script_desc *desc;
2715         int n_args = 0;
2716         char *p;
2717
2718         desc = script_desc__new(NULL);
2719
2720         if (read_script_info(desc, script_path))
2721                 goto out;
2722
2723         if (!desc->args)
2724                 goto out;
2725
2726         for (p = desc->args; *p; p++)
2727                 if (*p == '<')
2728                         n_args++;
2729 out:
2730         script_desc__delete(desc);
2731
2732         return n_args;
2733 }
2734
2735 static int have_cmd(int argc, const char **argv)
2736 {
2737         char **__argv = malloc(sizeof(const char *) * argc);
2738
2739         if (!__argv) {
2740                 pr_err("malloc failed\n");
2741                 return -1;
2742         }
2743
2744         memcpy(__argv, argv, sizeof(const char *) * argc);
2745         argc = parse_options(argc, (const char **)__argv, record_options,
2746                              NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2747         free(__argv);
2748
2749         system_wide = (argc == 0);
2750
2751         return 0;
2752 }
2753
2754 static void script__setup_sample_type(struct perf_script *script)
2755 {
2756         struct perf_session *session = script->session;
2757         u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2758
2759         if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2760                 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2761                     (sample_type & PERF_SAMPLE_STACK_USER))
2762                         callchain_param.record_mode = CALLCHAIN_DWARF;
2763                 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2764                         callchain_param.record_mode = CALLCHAIN_LBR;
2765                 else
2766                         callchain_param.record_mode = CALLCHAIN_FP;
2767         }
2768 }
2769
2770 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2771                                     union perf_event *event,
2772                                     struct perf_session *session)
2773 {
2774         struct stat_round_event *round = &event->stat_round;
2775         struct perf_evsel *counter;
2776
2777         evlist__for_each_entry(session->evlist, counter) {
2778                 perf_stat_process_counter(&stat_config, counter);
2779                 process_stat(counter, round->time);
2780         }
2781
2782         process_stat_interval(round->time);
2783         return 0;
2784 }
2785
2786 static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2787                                      union perf_event *event,
2788                                      struct perf_session *session __maybe_unused)
2789 {
2790         perf_event__read_stat_config(&stat_config, &event->stat_config);
2791         return 0;
2792 }
2793
2794 static int set_maps(struct perf_script *script)
2795 {
2796         struct perf_evlist *evlist = script->session->evlist;
2797
2798         if (!script->cpus || !script->threads)
2799                 return 0;
2800
2801         if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2802                 return -EINVAL;
2803
2804         perf_evlist__set_maps(evlist, script->cpus, script->threads);
2805
2806         if (perf_evlist__alloc_stats(evlist, true))
2807                 return -ENOMEM;
2808
2809         script->allocated = true;
2810         return 0;
2811 }
2812
2813 static
2814 int process_thread_map_event(struct perf_tool *tool,
2815                              union perf_event *event,
2816                              struct perf_session *session __maybe_unused)
2817 {
2818         struct perf_script *script = container_of(tool, struct perf_script, tool);
2819
2820         if (script->threads) {
2821                 pr_warning("Extra thread map event, ignoring.\n");
2822                 return 0;
2823         }
2824
2825         script->threads = thread_map__new_event(&event->thread_map);
2826         if (!script->threads)
2827                 return -ENOMEM;
2828
2829         return set_maps(script);
2830 }
2831
2832 static
2833 int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2834                           union perf_event *event,
2835                           struct perf_session *session __maybe_unused)
2836 {
2837         struct perf_script *script = container_of(tool, struct perf_script, tool);
2838
2839         if (script->cpus) {
2840                 pr_warning("Extra cpu map event, ignoring.\n");
2841                 return 0;
2842         }
2843
2844         script->cpus = cpu_map__new_data(&event->cpu_map.data);
2845         if (!script->cpus)
2846                 return -ENOMEM;
2847
2848         return set_maps(script);
2849 }
2850
2851 #ifdef HAVE_AUXTRACE_SUPPORT
2852 static int perf_script__process_auxtrace_info(struct perf_tool *tool,
2853                                               union perf_event *event,
2854                                               struct perf_session *session)
2855 {
2856         int ret = perf_event__process_auxtrace_info(tool, event, session);
2857
2858         if (ret == 0) {
2859                 struct perf_script *script = container_of(tool, struct perf_script, tool);
2860
2861                 ret = perf_script__setup_per_event_dump(script);
2862         }
2863
2864         return ret;
2865 }
2866 #else
2867 #define perf_script__process_auxtrace_info 0
2868 #endif
2869
2870 int cmd_script(int argc, const char **argv)
2871 {
2872         bool show_full_info = false;
2873         bool header = false;
2874         bool header_only = false;
2875         bool script_started = false;
2876         char *rec_script_path = NULL;
2877         char *rep_script_path = NULL;
2878         struct perf_session *session;
2879         struct itrace_synth_opts itrace_synth_opts = { .set = false, };
2880         char *script_path = NULL;
2881         const char **__argv;
2882         int i, j, err = 0;
2883         struct perf_script script = {
2884                 .tool = {
2885                         .sample          = process_sample_event,
2886                         .mmap            = perf_event__process_mmap,
2887                         .mmap2           = perf_event__process_mmap2,
2888                         .comm            = perf_event__process_comm,
2889                         .namespaces      = perf_event__process_namespaces,
2890                         .exit            = perf_event__process_exit,
2891                         .fork            = perf_event__process_fork,
2892                         .attr            = process_attr,
2893                         .event_update   = perf_event__process_event_update,
2894                         .tracing_data    = perf_event__process_tracing_data,
2895                         .feature         = perf_event__process_feature,
2896                         .build_id        = perf_event__process_build_id,
2897                         .id_index        = perf_event__process_id_index,
2898                         .auxtrace_info   = perf_script__process_auxtrace_info,
2899                         .auxtrace        = perf_event__process_auxtrace,
2900                         .auxtrace_error  = perf_event__process_auxtrace_error,
2901                         .stat            = perf_event__process_stat_event,
2902                         .stat_round      = process_stat_round_event,
2903                         .stat_config     = process_stat_config_event,
2904                         .thread_map      = process_thread_map_event,
2905                         .cpu_map         = process_cpu_map_event,
2906                         .ordered_events  = true,
2907                         .ordering_requires_timestamps = true,
2908                 },
2909         };
2910         struct perf_data data = {
2911                 .mode = PERF_DATA_MODE_READ,
2912         };
2913         const struct option options[] = {
2914         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2915                     "dump raw trace in ASCII"),
2916         OPT_INCR('v', "verbose", &verbose,
2917                  "be more verbose (show symbol address, etc)"),
2918         OPT_BOOLEAN('L', "Latency", &latency_format,
2919                     "show latency attributes (irqs/preemption disabled, etc)"),
2920         OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2921                            list_available_scripts),
2922         OPT_CALLBACK('s', "script", NULL, "name",
2923                      "script file name (lang:script name, script name, or *)",
2924                      parse_scriptname),
2925         OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2926                    "generate perf-script.xx script in specified language"),
2927         OPT_STRING('i', "input", &input_name, "file", "input file name"),
2928         OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2929                    "do various checks like samples ordering and lost events"),
2930         OPT_BOOLEAN(0, "header", &header, "Show data header."),
2931         OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
2932         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2933                    "file", "vmlinux pathname"),
2934         OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2935                    "file", "kallsyms pathname"),
2936         OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2937                     "When printing symbols do not display call chain"),
2938         OPT_CALLBACK(0, "symfs", NULL, "directory",
2939                      "Look for files with symbols relative to this directory",
2940                      symbol__config_symfs),
2941         OPT_CALLBACK('F', "fields", NULL, "str",
2942                      "comma separated output fields prepend with 'type:'. "
2943                      "+field to add and -field to remove."
2944                      "Valid types: hw,sw,trace,raw,synth. "
2945                      "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2946                      "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
2947                      "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
2948                      parse_output_fields),
2949         OPT_BOOLEAN('a', "all-cpus", &system_wide,
2950                     "system-wide collection from all CPUs"),
2951         OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2952                    "only consider these symbols"),
2953         OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2954                    "Stop display of callgraph at these symbols"),
2955         OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2956         OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2957                    "only display events for these comms"),
2958         OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2959                    "only consider symbols in these pids"),
2960         OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2961                    "only consider symbols in these tids"),
2962         OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2963                      "Set the maximum stack depth when parsing the callchain, "
2964                      "anything beyond the specified depth will be ignored. "
2965                      "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
2966         OPT_BOOLEAN('I', "show-info", &show_full_info,
2967                     "display extended information from perf.data file"),
2968         OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2969                     "Show the path of [kernel.kallsyms]"),
2970         OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2971                     "Show the fork/comm/exit events"),
2972         OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2973                     "Show the mmap events"),
2974         OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2975                     "Show context switch events (if recorded)"),
2976         OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2977                     "Show namespace events (if recorded)"),
2978         OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
2979                     "Dump trace output to files named by the monitored events"),
2980         OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2981         OPT_INTEGER(0, "max-blocks", &max_blocks,
2982                     "Maximum number of code blocks to dump with brstackinsn"),
2983         OPT_BOOLEAN(0, "ns", &nanosecs,
2984                     "Use 9 decimal places when displaying time"),
2985         OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2986                             "Instruction Tracing options",
2987                             itrace_parse_synth_opts),
2988         OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2989                         "Show full source file name path for source lines"),
2990         OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2991                         "Enable symbol demangling"),
2992         OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2993                         "Enable kernel symbol demangling"),
2994         OPT_STRING(0, "time", &script.time_str, "str",
2995                    "Time span of interest (start,stop)"),
2996         OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2997                     "Show inline function"),
2998         OPT_END()
2999         };
3000         const char * const script_subcommands[] = { "record", "report", NULL };
3001         const char *script_usage[] = {
3002                 "perf script [<options>]",
3003                 "perf script [<options>] record <script> [<record-options>] <command>",
3004                 "perf script [<options>] report <script> [script-args]",
3005                 "perf script [<options>] <script> [<record-options>] <command>",
3006                 "perf script [<options>] <top-script> [script-args]",
3007                 NULL
3008         };
3009
3010         perf_set_singlethreaded();
3011
3012         setup_scripting();
3013
3014         argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
3015                              PARSE_OPT_STOP_AT_NON_OPTION);
3016
3017         data.file.path = input_name;
3018         data.force     = symbol_conf.force;
3019
3020         if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3021                 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3022                 if (!rec_script_path)
3023                         return cmd_record(argc, argv);
3024         }
3025
3026         if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3027                 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3028                 if (!rep_script_path) {
3029                         fprintf(stderr,
3030                                 "Please specify a valid report script"
3031                                 "(see 'perf script -l' for listing)\n");
3032                         return -1;
3033                 }
3034         }
3035
3036         if (itrace_synth_opts.callchain &&
3037             itrace_synth_opts.callchain_sz > scripting_max_stack)
3038                 scripting_max_stack = itrace_synth_opts.callchain_sz;
3039
3040         /* make sure PERF_EXEC_PATH is set for scripts */
3041         set_argv_exec_path(get_argv_exec_path());
3042
3043         if (argc && !script_name && !rec_script_path && !rep_script_path) {
3044                 int live_pipe[2];
3045                 int rep_args;
3046                 pid_t pid;
3047
3048                 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3049                 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3050
3051                 if (!rec_script_path && !rep_script_path) {
3052                         usage_with_options_msg(script_usage, options,
3053                                 "Couldn't find script `%s'\n\n See perf"
3054                                 " script -l for available scripts.\n", argv[0]);
3055                 }
3056
3057                 if (is_top_script(argv[0])) {
3058                         rep_args = argc - 1;
3059                 } else {
3060                         int rec_args;
3061
3062                         rep_args = has_required_arg(rep_script_path);
3063                         rec_args = (argc - 1) - rep_args;
3064                         if (rec_args < 0) {
3065                                 usage_with_options_msg(script_usage, options,
3066                                         "`%s' script requires options."
3067                                         "\n\n See perf script -l for available "
3068                                         "scripts and options.\n", argv[0]);
3069                         }
3070                 }
3071
3072                 if (pipe(live_pipe) < 0) {
3073                         perror("failed to create pipe");
3074                         return -1;
3075                 }
3076
3077                 pid = fork();
3078                 if (pid < 0) {
3079                         perror("failed to fork");
3080                         return -1;
3081                 }
3082
3083                 if (!pid) {
3084                         j = 0;
3085
3086                         dup2(live_pipe[1], 1);
3087                         close(live_pipe[0]);
3088
3089                         if (is_top_script(argv[0])) {
3090                                 system_wide = true;
3091                         } else if (!system_wide) {
3092                                 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3093                                         err = -1;
3094                                         goto out;
3095                                 }
3096                         }
3097
3098                         __argv = malloc((argc + 6) * sizeof(const char *));
3099                         if (!__argv) {
3100                                 pr_err("malloc failed\n");
3101                                 err = -ENOMEM;
3102                                 goto out;
3103                         }
3104
3105                         __argv[j++] = "/bin/sh";
3106                         __argv[j++] = rec_script_path;
3107                         if (system_wide)
3108                                 __argv[j++] = "-a";
3109                         __argv[j++] = "-q";
3110                         __argv[j++] = "-o";
3111                         __argv[j++] = "-";
3112                         for (i = rep_args + 1; i < argc; i++)
3113                                 __argv[j++] = argv[i];
3114                         __argv[j++] = NULL;
3115
3116                         execvp("/bin/sh", (char **)__argv);
3117                         free(__argv);
3118                         exit(-1);
3119                 }
3120
3121                 dup2(live_pipe[0], 0);
3122                 close(live_pipe[1]);
3123
3124                 __argv = malloc((argc + 4) * sizeof(const char *));
3125                 if (!__argv) {
3126                         pr_err("malloc failed\n");
3127                         err = -ENOMEM;
3128                         goto out;
3129                 }
3130
3131                 j = 0;
3132                 __argv[j++] = "/bin/sh";
3133                 __argv[j++] = rep_script_path;
3134                 for (i = 1; i < rep_args + 1; i++)
3135                         __argv[j++] = argv[i];
3136                 __argv[j++] = "-i";
3137                 __argv[j++] = "-";
3138                 __argv[j++] = NULL;
3139
3140                 execvp("/bin/sh", (char **)__argv);
3141                 free(__argv);
3142                 exit(-1);
3143         }
3144
3145         if (rec_script_path)
3146                 script_path = rec_script_path;
3147         if (rep_script_path)
3148                 script_path = rep_script_path;
3149
3150         if (script_path) {
3151                 j = 0;
3152
3153                 if (!rec_script_path)
3154                         system_wide = false;
3155                 else if (!system_wide) {
3156                         if (have_cmd(argc - 1, &argv[1]) != 0) {
3157                                 err = -1;
3158                                 goto out;
3159                         }
3160                 }
3161
3162                 __argv = malloc((argc + 2) * sizeof(const char *));
3163                 if (!__argv) {
3164                         pr_err("malloc failed\n");
3165                         err = -ENOMEM;
3166                         goto out;
3167                 }
3168
3169                 __argv[j++] = "/bin/sh";
3170                 __argv[j++] = script_path;
3171                 if (system_wide)
3172                         __argv[j++] = "-a";
3173                 for (i = 2; i < argc; i++)
3174                         __argv[j++] = argv[i];
3175                 __argv[j++] = NULL;
3176
3177                 execvp("/bin/sh", (char **)__argv);
3178                 free(__argv);
3179                 exit(-1);
3180         }
3181
3182         if (!script_name)
3183                 setup_pager();
3184
3185         session = perf_session__new(&data, false, &script.tool);
3186         if (session == NULL)
3187                 return -1;
3188
3189         if (header || header_only) {
3190                 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
3191                 perf_session__fprintf_info(session, stdout, show_full_info);
3192                 if (header_only)
3193                         goto out_delete;
3194         }
3195         if (show_full_info)
3196                 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
3197
3198         if (symbol__init(&session->header.env) < 0)
3199                 goto out_delete;
3200
3201         script.session = session;
3202         script__setup_sample_type(&script);
3203
3204         if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3205                 itrace_synth_opts.thread_stack = true;
3206
3207         session->itrace_synth_opts = &itrace_synth_opts;
3208
3209         if (cpu_list) {
3210                 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3211                 if (err < 0)
3212                         goto out_delete;
3213                 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
3214         }
3215
3216         if (!no_callchain)
3217                 symbol_conf.use_callchain = true;
3218         else
3219                 symbol_conf.use_callchain = false;
3220
3221         if (session->tevent.pevent &&
3222             pevent_set_function_resolver(session->tevent.pevent,
3223                                          machine__resolve_kernel_addr,
3224                                          &session->machines.host) < 0) {
3225                 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3226                 err = -1;
3227                 goto out_delete;
3228         }
3229
3230         if (generate_script_lang) {
3231                 struct stat perf_stat;
3232                 int input;
3233
3234                 if (output_set_by_user()) {
3235                         fprintf(stderr,
3236                                 "custom fields not supported for generated scripts");
3237                         err = -EINVAL;
3238                         goto out_delete;
3239                 }
3240
3241                 input = open(data.file.path, O_RDONLY); /* input_name */
3242                 if (input < 0) {
3243                         err = -errno;
3244                         perror("failed to open file");
3245                         goto out_delete;
3246                 }
3247
3248                 err = fstat(input, &perf_stat);
3249                 if (err < 0) {
3250                         perror("failed to stat file");
3251                         goto out_delete;
3252                 }
3253
3254                 if (!perf_stat.st_size) {
3255                         fprintf(stderr, "zero-sized file, nothing to do!\n");
3256                         goto out_delete;
3257                 }
3258
3259                 scripting_ops = script_spec__lookup(generate_script_lang);
3260                 if (!scripting_ops) {
3261                         fprintf(stderr, "invalid language specifier");
3262                         err = -ENOENT;
3263                         goto out_delete;
3264                 }
3265
3266                 err = scripting_ops->generate_script(session->tevent.pevent,
3267                                                      "perf-script");
3268                 goto out_delete;
3269         }
3270
3271         if (script_name) {
3272                 err = scripting_ops->start_script(script_name, argc, argv);
3273                 if (err)
3274                         goto out_delete;
3275                 pr_debug("perf script started with script %s\n\n", script_name);
3276                 script_started = true;
3277         }
3278
3279
3280         err = perf_session__check_output_opt(session);
3281         if (err < 0)
3282                 goto out_delete;
3283
3284         /* needs to be parsed after looking up reference time */
3285         if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3286                 pr_err("Invalid time string\n");
3287                 err = -EINVAL;
3288                 goto out_delete;
3289         }
3290
3291         err = __cmd_script(&script);
3292
3293         flush_scripting();
3294
3295 out_delete:
3296         perf_evlist__free_stats(session->evlist);
3297         perf_session__delete(session);
3298
3299         if (script_started)
3300                 cleanup_scripting();
3301 out:
3302         return err;
3303 }