Merge tag 'for-linus-20171120' of git://git.infradead.org/linux-mtd
[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                 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
1959                 if (evsel->priv == NULL)
1960                         goto out_err_fclose;
1961         }
1962
1963         return 0;
1964
1965 out_err_fclose:
1966         perf_script__fclose_per_event_dump(script);
1967         return -1;
1968 }
1969
1970 static int perf_script__setup_per_event_dump(struct perf_script *script)
1971 {
1972         struct perf_evsel *evsel;
1973         static struct perf_evsel_script es_stdout;
1974
1975         if (script->per_event_dump)
1976                 return perf_script__fopen_per_event_dump(script);
1977
1978         es_stdout.fp = stdout;
1979
1980         evlist__for_each_entry(script->session->evlist, evsel)
1981                 evsel->priv = &es_stdout;
1982
1983         return 0;
1984 }
1985
1986 static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
1987 {
1988         struct perf_evsel *evsel;
1989
1990         evlist__for_each_entry(script->session->evlist, evsel) {
1991                 struct perf_evsel_script *es = evsel->priv;
1992
1993                 perf_evsel_script__fprintf(es, stdout);
1994                 perf_evsel_script__delete(es);
1995                 evsel->priv = NULL;
1996         }
1997 }
1998
1999 static int __cmd_script(struct perf_script *script)
2000 {
2001         int ret;
2002
2003         signal(SIGINT, sig_handler);
2004
2005         /* override event processing functions */
2006         if (script->show_task_events) {
2007                 script->tool.comm = process_comm_event;
2008                 script->tool.fork = process_fork_event;
2009                 script->tool.exit = process_exit_event;
2010         }
2011         if (script->show_mmap_events) {
2012                 script->tool.mmap = process_mmap_event;
2013                 script->tool.mmap2 = process_mmap2_event;
2014         }
2015         if (script->show_switch_events)
2016                 script->tool.context_switch = process_switch_event;
2017         if (script->show_namespace_events)
2018                 script->tool.namespaces = process_namespaces_event;
2019
2020         if (perf_script__setup_per_event_dump(script)) {
2021                 pr_err("Couldn't create the per event dump files\n");
2022                 return -1;
2023         }
2024
2025         ret = perf_session__process_events(script->session);
2026
2027         if (script->per_event_dump)
2028                 perf_script__exit_per_event_dump_stats(script);
2029
2030         if (debug_mode)
2031                 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
2032
2033         return ret;
2034 }
2035
2036 struct script_spec {
2037         struct list_head        node;
2038         struct scripting_ops    *ops;
2039         char                    spec[0];
2040 };
2041
2042 static LIST_HEAD(script_specs);
2043
2044 static struct script_spec *script_spec__new(const char *spec,
2045                                             struct scripting_ops *ops)
2046 {
2047         struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2048
2049         if (s != NULL) {
2050                 strcpy(s->spec, spec);
2051                 s->ops = ops;
2052         }
2053
2054         return s;
2055 }
2056
2057 static void script_spec__add(struct script_spec *s)
2058 {
2059         list_add_tail(&s->node, &script_specs);
2060 }
2061
2062 static struct script_spec *script_spec__find(const char *spec)
2063 {
2064         struct script_spec *s;
2065
2066         list_for_each_entry(s, &script_specs, node)
2067                 if (strcasecmp(s->spec, spec) == 0)
2068                         return s;
2069         return NULL;
2070 }
2071
2072 int script_spec_register(const char *spec, struct scripting_ops *ops)
2073 {
2074         struct script_spec *s;
2075
2076         s = script_spec__find(spec);
2077         if (s)
2078                 return -1;
2079
2080         s = script_spec__new(spec, ops);
2081         if (!s)
2082                 return -1;
2083         else
2084                 script_spec__add(s);
2085
2086         return 0;
2087 }
2088
2089 static struct scripting_ops *script_spec__lookup(const char *spec)
2090 {
2091         struct script_spec *s = script_spec__find(spec);
2092         if (!s)
2093                 return NULL;
2094
2095         return s->ops;
2096 }
2097
2098 static void list_available_languages(void)
2099 {
2100         struct script_spec *s;
2101
2102         fprintf(stderr, "\n");
2103         fprintf(stderr, "Scripting language extensions (used in "
2104                 "perf script -s [spec:]script.[spec]):\n\n");
2105
2106         list_for_each_entry(s, &script_specs, node)
2107                 fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name);
2108
2109         fprintf(stderr, "\n");
2110 }
2111
2112 static int parse_scriptname(const struct option *opt __maybe_unused,
2113                             const char *str, int unset __maybe_unused)
2114 {
2115         char spec[PATH_MAX];
2116         const char *script, *ext;
2117         int len;
2118
2119         if (strcmp(str, "lang") == 0) {
2120                 list_available_languages();
2121                 exit(0);
2122         }
2123
2124         script = strchr(str, ':');
2125         if (script) {
2126                 len = script - str;
2127                 if (len >= PATH_MAX) {
2128                         fprintf(stderr, "invalid language specifier");
2129                         return -1;
2130                 }
2131                 strncpy(spec, str, len);
2132                 spec[len] = '\0';
2133                 scripting_ops = script_spec__lookup(spec);
2134                 if (!scripting_ops) {
2135                         fprintf(stderr, "invalid language specifier");
2136                         return -1;
2137                 }
2138                 script++;
2139         } else {
2140                 script = str;
2141                 ext = strrchr(script, '.');
2142                 if (!ext) {
2143                         fprintf(stderr, "invalid script extension");
2144                         return -1;
2145                 }
2146                 scripting_ops = script_spec__lookup(++ext);
2147                 if (!scripting_ops) {
2148                         fprintf(stderr, "invalid script extension");
2149                         return -1;
2150                 }
2151         }
2152
2153         script_name = strdup(script);
2154
2155         return 0;
2156 }
2157
2158 static int parse_output_fields(const struct option *opt __maybe_unused,
2159                             const char *arg, int unset __maybe_unused)
2160 {
2161         char *tok, *strtok_saveptr = NULL;
2162         int i, imax = ARRAY_SIZE(all_output_options);
2163         int j;
2164         int rc = 0;
2165         char *str = strdup(arg);
2166         int type = -1;
2167         enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
2168
2169         if (!str)
2170                 return -ENOMEM;
2171
2172         /* first word can state for which event type the user is specifying
2173          * the fields. If no type exists, the specified fields apply to all
2174          * event types found in the file minus the invalid fields for a type.
2175          */
2176         tok = strchr(str, ':');
2177         if (tok) {
2178                 *tok = '\0';
2179                 tok++;
2180                 if (!strcmp(str, "hw"))
2181                         type = PERF_TYPE_HARDWARE;
2182                 else if (!strcmp(str, "sw"))
2183                         type = PERF_TYPE_SOFTWARE;
2184                 else if (!strcmp(str, "trace"))
2185                         type = PERF_TYPE_TRACEPOINT;
2186                 else if (!strcmp(str, "raw"))
2187                         type = PERF_TYPE_RAW;
2188                 else if (!strcmp(str, "break"))
2189                         type = PERF_TYPE_BREAKPOINT;
2190                 else if (!strcmp(str, "synth"))
2191                         type = OUTPUT_TYPE_SYNTH;
2192                 else {
2193                         fprintf(stderr, "Invalid event type in field string.\n");
2194                         rc = -EINVAL;
2195                         goto out;
2196                 }
2197
2198                 if (output[type].user_set)
2199                         pr_warning("Overriding previous field request for %s events.\n",
2200                                    event_type(type));
2201
2202                 output[type].fields = 0;
2203                 output[type].user_set = true;
2204                 output[type].wildcard_set = false;
2205
2206         } else {
2207                 tok = str;
2208                 if (strlen(str) == 0) {
2209                         fprintf(stderr,
2210                                 "Cannot set fields to 'none' for all event types.\n");
2211                         rc = -EINVAL;
2212                         goto out;
2213                 }
2214
2215                 /* Don't override defaults for +- */
2216                 if (strchr(str, '+') || strchr(str, '-'))
2217                         goto parse;
2218
2219                 if (output_set_by_user())
2220                         pr_warning("Overriding previous field request for all events.\n");
2221
2222                 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2223                         output[j].fields = 0;
2224                         output[j].user_set = true;
2225                         output[j].wildcard_set = true;
2226                 }
2227         }
2228
2229 parse:
2230         for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
2231                 if (*tok == '+') {
2232                         if (change == SET)
2233                                 goto out_badmix;
2234                         change = ADD;
2235                         tok++;
2236                 } else if (*tok == '-') {
2237                         if (change == SET)
2238                                 goto out_badmix;
2239                         change = REMOVE;
2240                         tok++;
2241                 } else {
2242                         if (change != SET && change != DEFAULT)
2243                                 goto out_badmix;
2244                         change = SET;
2245                 }
2246
2247                 for (i = 0; i < imax; ++i) {
2248                         if (strcmp(tok, all_output_options[i].str) == 0)
2249                                 break;
2250                 }
2251                 if (i == imax && strcmp(tok, "flags") == 0) {
2252                         print_flags = change == REMOVE ? false : true;
2253                         continue;
2254                 }
2255                 if (i == imax) {
2256                         fprintf(stderr, "Invalid field requested.\n");
2257                         rc = -EINVAL;
2258                         goto out;
2259                 }
2260
2261                 if (type == -1) {
2262                         /* add user option to all events types for
2263                          * which it is valid
2264                          */
2265                         for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
2266                                 if (output[j].invalid_fields & all_output_options[i].field) {
2267                                         pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2268                                                    all_output_options[i].str, event_type(j));
2269                                 } else {
2270                                         if (change == REMOVE)
2271                                                 output[j].fields &= ~all_output_options[i].field;
2272                                         else
2273                                                 output[j].fields |= all_output_options[i].field;
2274                                 }
2275                         }
2276                 } else {
2277                         if (output[type].invalid_fields & all_output_options[i].field) {
2278                                 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2279                                          all_output_options[i].str, event_type(type));
2280
2281                                 rc = -EINVAL;
2282                                 goto out;
2283                         }
2284                         output[type].fields |= all_output_options[i].field;
2285                 }
2286         }
2287
2288         if (type >= 0) {
2289                 if (output[type].fields == 0) {
2290                         pr_debug("No fields requested for %s type. "
2291                                  "Events will not be displayed.\n", event_type(type));
2292                 }
2293         }
2294         goto out;
2295
2296 out_badmix:
2297         fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2298         rc = -EINVAL;
2299 out:
2300         free(str);
2301         return rc;
2302 }
2303
2304 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2305 static int is_directory(const char *base_path, const struct dirent *dent)
2306 {
2307         char path[PATH_MAX];
2308         struct stat st;
2309
2310         sprintf(path, "%s/%s", base_path, dent->d_name);
2311         if (stat(path, &st))
2312                 return 0;
2313
2314         return S_ISDIR(st.st_mode);
2315 }
2316
2317 #define for_each_lang(scripts_path, scripts_dir, lang_dirent)           \
2318         while ((lang_dirent = readdir(scripts_dir)) != NULL)            \
2319                 if ((lang_dirent->d_type == DT_DIR ||                   \
2320                      (lang_dirent->d_type == DT_UNKNOWN &&              \
2321                       is_directory(scripts_path, lang_dirent))) &&      \
2322                     (strcmp(lang_dirent->d_name, ".")) &&               \
2323                     (strcmp(lang_dirent->d_name, "..")))
2324
2325 #define for_each_script(lang_path, lang_dir, script_dirent)             \
2326         while ((script_dirent = readdir(lang_dir)) != NULL)             \
2327                 if (script_dirent->d_type != DT_DIR &&                  \
2328                     (script_dirent->d_type != DT_UNKNOWN ||             \
2329                      !is_directory(lang_path, script_dirent)))
2330
2331
2332 #define RECORD_SUFFIX                   "-record"
2333 #define REPORT_SUFFIX                   "-report"
2334
2335 struct script_desc {
2336         struct list_head        node;
2337         char                    *name;
2338         char                    *half_liner;
2339         char                    *args;
2340 };
2341
2342 static LIST_HEAD(script_descs);
2343
2344 static struct script_desc *script_desc__new(const char *name)
2345 {
2346         struct script_desc *s = zalloc(sizeof(*s));
2347
2348         if (s != NULL && name)
2349                 s->name = strdup(name);
2350
2351         return s;
2352 }
2353
2354 static void script_desc__delete(struct script_desc *s)
2355 {
2356         zfree(&s->name);
2357         zfree(&s->half_liner);
2358         zfree(&s->args);
2359         free(s);
2360 }
2361
2362 static void script_desc__add(struct script_desc *s)
2363 {
2364         list_add_tail(&s->node, &script_descs);
2365 }
2366
2367 static struct script_desc *script_desc__find(const char *name)
2368 {
2369         struct script_desc *s;
2370
2371         list_for_each_entry(s, &script_descs, node)
2372                 if (strcasecmp(s->name, name) == 0)
2373                         return s;
2374         return NULL;
2375 }
2376
2377 static struct script_desc *script_desc__findnew(const char *name)
2378 {
2379         struct script_desc *s = script_desc__find(name);
2380
2381         if (s)
2382                 return s;
2383
2384         s = script_desc__new(name);
2385         if (!s)
2386                 return NULL;
2387
2388         script_desc__add(s);
2389
2390         return s;
2391 }
2392
2393 static const char *ends_with(const char *str, const char *suffix)
2394 {
2395         size_t suffix_len = strlen(suffix);
2396         const char *p = str;
2397
2398         if (strlen(str) > suffix_len) {
2399                 p = str + strlen(str) - suffix_len;
2400                 if (!strncmp(p, suffix, suffix_len))
2401                         return p;
2402         }
2403
2404         return NULL;
2405 }
2406
2407 static int read_script_info(struct script_desc *desc, const char *filename)
2408 {
2409         char line[BUFSIZ], *p;
2410         FILE *fp;
2411
2412         fp = fopen(filename, "r");
2413         if (!fp)
2414                 return -1;
2415
2416         while (fgets(line, sizeof(line), fp)) {
2417                 p = ltrim(line);
2418                 if (strlen(p) == 0)
2419                         continue;
2420                 if (*p != '#')
2421                         continue;
2422                 p++;
2423                 if (strlen(p) && *p == '!')
2424                         continue;
2425
2426                 p = ltrim(p);
2427                 if (strlen(p) && p[strlen(p) - 1] == '\n')
2428                         p[strlen(p) - 1] = '\0';
2429
2430                 if (!strncmp(p, "description:", strlen("description:"))) {
2431                         p += strlen("description:");
2432                         desc->half_liner = strdup(ltrim(p));
2433                         continue;
2434                 }
2435
2436                 if (!strncmp(p, "args:", strlen("args:"))) {
2437                         p += strlen("args:");
2438                         desc->args = strdup(ltrim(p));
2439                         continue;
2440                 }
2441         }
2442
2443         fclose(fp);
2444
2445         return 0;
2446 }
2447
2448 static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2449 {
2450         char *script_root, *str;
2451
2452         script_root = strdup(script_dirent->d_name);
2453         if (!script_root)
2454                 return NULL;
2455
2456         str = (char *)ends_with(script_root, suffix);
2457         if (!str) {
2458                 free(script_root);
2459                 return NULL;
2460         }
2461
2462         *str = '\0';
2463         return script_root;
2464 }
2465
2466 static int list_available_scripts(const struct option *opt __maybe_unused,
2467                                   const char *s __maybe_unused,
2468                                   int unset __maybe_unused)
2469 {
2470         struct dirent *script_dirent, *lang_dirent;
2471         char scripts_path[MAXPATHLEN];
2472         DIR *scripts_dir, *lang_dir;
2473         char script_path[MAXPATHLEN];
2474         char lang_path[MAXPATHLEN];
2475         struct script_desc *desc;
2476         char first_half[BUFSIZ];
2477         char *script_root;
2478
2479         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2480
2481         scripts_dir = opendir(scripts_path);
2482         if (!scripts_dir) {
2483                 fprintf(stdout,
2484                         "open(%s) failed.\n"
2485                         "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2486                         scripts_path);
2487                 exit(-1);
2488         }
2489
2490         for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2491                 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2492                          lang_dirent->d_name);
2493                 lang_dir = opendir(lang_path);
2494                 if (!lang_dir)
2495                         continue;
2496
2497                 for_each_script(lang_path, lang_dir, script_dirent) {
2498                         script_root = get_script_root(script_dirent, REPORT_SUFFIX);
2499                         if (script_root) {
2500                                 desc = script_desc__findnew(script_root);
2501                                 snprintf(script_path, MAXPATHLEN, "%s/%s",
2502                                          lang_path, script_dirent->d_name);
2503                                 read_script_info(desc, script_path);
2504                                 free(script_root);
2505                         }
2506                 }
2507         }
2508
2509         fprintf(stdout, "List of available trace scripts:\n");
2510         list_for_each_entry(desc, &script_descs, node) {
2511                 sprintf(first_half, "%s %s", desc->name,
2512                         desc->args ? desc->args : "");
2513                 fprintf(stdout, "  %-36s %s\n", first_half,
2514                         desc->half_liner ? desc->half_liner : "");
2515         }
2516
2517         exit(0);
2518 }
2519
2520 /*
2521  * Some scripts specify the required events in their "xxx-record" file,
2522  * this function will check if the events in perf.data match those
2523  * mentioned in the "xxx-record".
2524  *
2525  * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2526  * which is covered well now. And new parsing code should be added to
2527  * cover the future complexing formats like event groups etc.
2528  */
2529 static int check_ev_match(char *dir_name, char *scriptname,
2530                         struct perf_session *session)
2531 {
2532         char filename[MAXPATHLEN], evname[128];
2533         char line[BUFSIZ], *p;
2534         struct perf_evsel *pos;
2535         int match, len;
2536         FILE *fp;
2537
2538         sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2539
2540         fp = fopen(filename, "r");
2541         if (!fp)
2542                 return -1;
2543
2544         while (fgets(line, sizeof(line), fp)) {
2545                 p = ltrim(line);
2546                 if (*p == '#')
2547                         continue;
2548
2549                 while (strlen(p)) {
2550                         p = strstr(p, "-e");
2551                         if (!p)
2552                                 break;
2553
2554                         p += 2;
2555                         p = ltrim(p);
2556                         len = strcspn(p, " \t");
2557                         if (!len)
2558                                 break;
2559
2560                         snprintf(evname, len + 1, "%s", p);
2561
2562                         match = 0;
2563                         evlist__for_each_entry(session->evlist, pos) {
2564                                 if (!strcmp(perf_evsel__name(pos), evname)) {
2565                                         match = 1;
2566                                         break;
2567                                 }
2568                         }
2569
2570                         if (!match) {
2571                                 fclose(fp);
2572                                 return -1;
2573                         }
2574                 }
2575         }
2576
2577         fclose(fp);
2578         return 0;
2579 }
2580
2581 /*
2582  * Return -1 if none is found, otherwise the actual scripts number.
2583  *
2584  * Currently the only user of this function is the script browser, which
2585  * will list all statically runnable scripts, select one, execute it and
2586  * show the output in a perf browser.
2587  */
2588 int find_scripts(char **scripts_array, char **scripts_path_array)
2589 {
2590         struct dirent *script_dirent, *lang_dirent;
2591         char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
2592         DIR *scripts_dir, *lang_dir;
2593         struct perf_session *session;
2594         struct perf_data data = {
2595                 .file      = {
2596                         .path = input_name,
2597                 },
2598                 .mode      = PERF_DATA_MODE_READ,
2599         };
2600         char *temp;
2601         int i = 0;
2602
2603         session = perf_session__new(&data, false, NULL);
2604         if (!session)
2605                 return -1;
2606
2607         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2608
2609         scripts_dir = opendir(scripts_path);
2610         if (!scripts_dir) {
2611                 perf_session__delete(session);
2612                 return -1;
2613         }
2614
2615         for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2616                 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
2617                          lang_dirent->d_name);
2618 #ifdef NO_LIBPERL
2619                 if (strstr(lang_path, "perl"))
2620                         continue;
2621 #endif
2622 #ifdef NO_LIBPYTHON
2623                 if (strstr(lang_path, "python"))
2624                         continue;
2625 #endif
2626
2627                 lang_dir = opendir(lang_path);
2628                 if (!lang_dir)
2629                         continue;
2630
2631                 for_each_script(lang_path, lang_dir, script_dirent) {
2632                         /* Skip those real time scripts: xxxtop.p[yl] */
2633                         if (strstr(script_dirent->d_name, "top."))
2634                                 continue;
2635                         sprintf(scripts_path_array[i], "%s/%s", lang_path,
2636                                 script_dirent->d_name);
2637                         temp = strchr(script_dirent->d_name, '.');
2638                         snprintf(scripts_array[i],
2639                                 (temp - script_dirent->d_name) + 1,
2640                                 "%s", script_dirent->d_name);
2641
2642                         if (check_ev_match(lang_path,
2643                                         scripts_array[i], session))
2644                                 continue;
2645
2646                         i++;
2647                 }
2648                 closedir(lang_dir);
2649         }
2650
2651         closedir(scripts_dir);
2652         perf_session__delete(session);
2653         return i;
2654 }
2655
2656 static char *get_script_path(const char *script_root, const char *suffix)
2657 {
2658         struct dirent *script_dirent, *lang_dirent;
2659         char scripts_path[MAXPATHLEN];
2660         char script_path[MAXPATHLEN];
2661         DIR *scripts_dir, *lang_dir;
2662         char lang_path[MAXPATHLEN];
2663         char *__script_root;
2664
2665         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
2666
2667         scripts_dir = opendir(scripts_path);
2668         if (!scripts_dir)
2669                 return NULL;
2670
2671         for_each_lang(scripts_path, scripts_dir, lang_dirent) {
2672                 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2673                          lang_dirent->d_name);
2674                 lang_dir = opendir(lang_path);
2675                 if (!lang_dir)
2676                         continue;
2677
2678                 for_each_script(lang_path, lang_dir, script_dirent) {
2679                         __script_root = get_script_root(script_dirent, suffix);
2680                         if (__script_root && !strcmp(script_root, __script_root)) {
2681                                 free(__script_root);
2682                                 closedir(lang_dir);
2683                                 closedir(scripts_dir);
2684                                 snprintf(script_path, MAXPATHLEN, "%s/%s",
2685                                          lang_path, script_dirent->d_name);
2686                                 return strdup(script_path);
2687                         }
2688                         free(__script_root);
2689                 }
2690                 closedir(lang_dir);
2691         }
2692         closedir(scripts_dir);
2693
2694         return NULL;
2695 }
2696
2697 static bool is_top_script(const char *script_path)
2698 {
2699         return ends_with(script_path, "top") == NULL ? false : true;
2700 }
2701
2702 static int has_required_arg(char *script_path)
2703 {
2704         struct script_desc *desc;
2705         int n_args = 0;
2706         char *p;
2707
2708         desc = script_desc__new(NULL);
2709
2710         if (read_script_info(desc, script_path))
2711                 goto out;
2712
2713         if (!desc->args)
2714                 goto out;
2715
2716         for (p = desc->args; *p; p++)
2717                 if (*p == '<')
2718                         n_args++;
2719 out:
2720         script_desc__delete(desc);
2721
2722         return n_args;
2723 }
2724
2725 static int have_cmd(int argc, const char **argv)
2726 {
2727         char **__argv = malloc(sizeof(const char *) * argc);
2728
2729         if (!__argv) {
2730                 pr_err("malloc failed\n");
2731                 return -1;
2732         }
2733
2734         memcpy(__argv, argv, sizeof(const char *) * argc);
2735         argc = parse_options(argc, (const char **)__argv, record_options,
2736                              NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2737         free(__argv);
2738
2739         system_wide = (argc == 0);
2740
2741         return 0;
2742 }
2743
2744 static void script__setup_sample_type(struct perf_script *script)
2745 {
2746         struct perf_session *session = script->session;
2747         u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2748
2749         if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2750                 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2751                     (sample_type & PERF_SAMPLE_STACK_USER))
2752                         callchain_param.record_mode = CALLCHAIN_DWARF;
2753                 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2754                         callchain_param.record_mode = CALLCHAIN_LBR;
2755                 else
2756                         callchain_param.record_mode = CALLCHAIN_FP;
2757         }
2758 }
2759
2760 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2761                                     union perf_event *event,
2762                                     struct perf_session *session)
2763 {
2764         struct stat_round_event *round = &event->stat_round;
2765         struct perf_evsel *counter;
2766
2767         evlist__for_each_entry(session->evlist, counter) {
2768                 perf_stat_process_counter(&stat_config, counter);
2769                 process_stat(counter, round->time);
2770         }
2771
2772         process_stat_interval(round->time);
2773         return 0;
2774 }
2775
2776 static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2777                                      union perf_event *event,
2778                                      struct perf_session *session __maybe_unused)
2779 {
2780         perf_event__read_stat_config(&stat_config, &event->stat_config);
2781         return 0;
2782 }
2783
2784 static int set_maps(struct perf_script *script)
2785 {
2786         struct perf_evlist *evlist = script->session->evlist;
2787
2788         if (!script->cpus || !script->threads)
2789                 return 0;
2790
2791         if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2792                 return -EINVAL;
2793
2794         perf_evlist__set_maps(evlist, script->cpus, script->threads);
2795
2796         if (perf_evlist__alloc_stats(evlist, true))
2797                 return -ENOMEM;
2798
2799         script->allocated = true;
2800         return 0;
2801 }
2802
2803 static
2804 int process_thread_map_event(struct perf_tool *tool,
2805                              union perf_event *event,
2806                              struct perf_session *session __maybe_unused)
2807 {
2808         struct perf_script *script = container_of(tool, struct perf_script, tool);
2809
2810         if (script->threads) {
2811                 pr_warning("Extra thread map event, ignoring.\n");
2812                 return 0;
2813         }
2814
2815         script->threads = thread_map__new_event(&event->thread_map);
2816         if (!script->threads)
2817                 return -ENOMEM;
2818
2819         return set_maps(script);
2820 }
2821
2822 static
2823 int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2824                           union perf_event *event,
2825                           struct perf_session *session __maybe_unused)
2826 {
2827         struct perf_script *script = container_of(tool, struct perf_script, tool);
2828
2829         if (script->cpus) {
2830                 pr_warning("Extra cpu map event, ignoring.\n");
2831                 return 0;
2832         }
2833
2834         script->cpus = cpu_map__new_data(&event->cpu_map.data);
2835         if (!script->cpus)
2836                 return -ENOMEM;
2837
2838         return set_maps(script);
2839 }
2840
2841 int cmd_script(int argc, const char **argv)
2842 {
2843         bool show_full_info = false;
2844         bool header = false;
2845         bool header_only = false;
2846         bool script_started = false;
2847         char *rec_script_path = NULL;
2848         char *rep_script_path = NULL;
2849         struct perf_session *session;
2850         struct itrace_synth_opts itrace_synth_opts = { .set = false, };
2851         char *script_path = NULL;
2852         const char **__argv;
2853         int i, j, err = 0;
2854         struct perf_script script = {
2855                 .tool = {
2856                         .sample          = process_sample_event,
2857                         .mmap            = perf_event__process_mmap,
2858                         .mmap2           = perf_event__process_mmap2,
2859                         .comm            = perf_event__process_comm,
2860                         .namespaces      = perf_event__process_namespaces,
2861                         .exit            = perf_event__process_exit,
2862                         .fork            = perf_event__process_fork,
2863                         .attr            = process_attr,
2864                         .event_update   = perf_event__process_event_update,
2865                         .tracing_data    = perf_event__process_tracing_data,
2866                         .feature         = perf_event__process_feature,
2867                         .build_id        = perf_event__process_build_id,
2868                         .id_index        = perf_event__process_id_index,
2869                         .auxtrace_info   = perf_event__process_auxtrace_info,
2870                         .auxtrace        = perf_event__process_auxtrace,
2871                         .auxtrace_error  = perf_event__process_auxtrace_error,
2872                         .stat            = perf_event__process_stat_event,
2873                         .stat_round      = process_stat_round_event,
2874                         .stat_config     = process_stat_config_event,
2875                         .thread_map      = process_thread_map_event,
2876                         .cpu_map         = process_cpu_map_event,
2877                         .ordered_events  = true,
2878                         .ordering_requires_timestamps = true,
2879                 },
2880         };
2881         struct perf_data data = {
2882                 .mode = PERF_DATA_MODE_READ,
2883         };
2884         const struct option options[] = {
2885         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2886                     "dump raw trace in ASCII"),
2887         OPT_INCR('v', "verbose", &verbose,
2888                  "be more verbose (show symbol address, etc)"),
2889         OPT_BOOLEAN('L', "Latency", &latency_format,
2890                     "show latency attributes (irqs/preemption disabled, etc)"),
2891         OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2892                            list_available_scripts),
2893         OPT_CALLBACK('s', "script", NULL, "name",
2894                      "script file name (lang:script name, script name, or *)",
2895                      parse_scriptname),
2896         OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2897                    "generate perf-script.xx script in specified language"),
2898         OPT_STRING('i', "input", &input_name, "file", "input file name"),
2899         OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2900                    "do various checks like samples ordering and lost events"),
2901         OPT_BOOLEAN(0, "header", &header, "Show data header."),
2902         OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
2903         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2904                    "file", "vmlinux pathname"),
2905         OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2906                    "file", "kallsyms pathname"),
2907         OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2908                     "When printing symbols do not display call chain"),
2909         OPT_CALLBACK(0, "symfs", NULL, "directory",
2910                      "Look for files with symbols relative to this directory",
2911                      symbol__config_symfs),
2912         OPT_CALLBACK('F', "fields", NULL, "str",
2913                      "comma separated output fields prepend with 'type:'. "
2914                      "+field to add and -field to remove."
2915                      "Valid types: hw,sw,trace,raw,synth. "
2916                      "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
2917                      "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
2918                      "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
2919                      parse_output_fields),
2920         OPT_BOOLEAN('a', "all-cpus", &system_wide,
2921                     "system-wide collection from all CPUs"),
2922         OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2923                    "only consider these symbols"),
2924         OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2925                    "Stop display of callgraph at these symbols"),
2926         OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2927         OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2928                    "only display events for these comms"),
2929         OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2930                    "only consider symbols in these pids"),
2931         OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2932                    "only consider symbols in these tids"),
2933         OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2934                      "Set the maximum stack depth when parsing the callchain, "
2935                      "anything beyond the specified depth will be ignored. "
2936                      "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
2937         OPT_BOOLEAN('I', "show-info", &show_full_info,
2938                     "display extended information from perf.data file"),
2939         OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2940                     "Show the path of [kernel.kallsyms]"),
2941         OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2942                     "Show the fork/comm/exit events"),
2943         OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2944                     "Show the mmap events"),
2945         OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2946                     "Show context switch events (if recorded)"),
2947         OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2948                     "Show namespace events (if recorded)"),
2949         OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
2950                     "Dump trace output to files named by the monitored events"),
2951         OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
2952         OPT_INTEGER(0, "max-blocks", &max_blocks,
2953                     "Maximum number of code blocks to dump with brstackinsn"),
2954         OPT_BOOLEAN(0, "ns", &nanosecs,
2955                     "Use 9 decimal places when displaying time"),
2956         OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2957                             "Instruction Tracing options",
2958                             itrace_parse_synth_opts),
2959         OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2960                         "Show full source file name path for source lines"),
2961         OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2962                         "Enable symbol demangling"),
2963         OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2964                         "Enable kernel symbol demangling"),
2965         OPT_STRING(0, "time", &script.time_str, "str",
2966                    "Time span of interest (start,stop)"),
2967         OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2968                     "Show inline function"),
2969         OPT_END()
2970         };
2971         const char * const script_subcommands[] = { "record", "report", NULL };
2972         const char *script_usage[] = {
2973                 "perf script [<options>]",
2974                 "perf script [<options>] record <script> [<record-options>] <command>",
2975                 "perf script [<options>] report <script> [script-args]",
2976                 "perf script [<options>] <script> [<record-options>] <command>",
2977                 "perf script [<options>] <top-script> [script-args]",
2978                 NULL
2979         };
2980
2981         perf_set_singlethreaded();
2982
2983         setup_scripting();
2984
2985         argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
2986                              PARSE_OPT_STOP_AT_NON_OPTION);
2987
2988         data.file.path = input_name;
2989         data.force     = symbol_conf.force;
2990
2991         if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2992                 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2993                 if (!rec_script_path)
2994                         return cmd_record(argc, argv);
2995         }
2996
2997         if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2998                 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2999                 if (!rep_script_path) {
3000                         fprintf(stderr,
3001                                 "Please specify a valid report script"
3002                                 "(see 'perf script -l' for listing)\n");
3003                         return -1;
3004                 }
3005         }
3006
3007         if (itrace_synth_opts.callchain &&
3008             itrace_synth_opts.callchain_sz > scripting_max_stack)
3009                 scripting_max_stack = itrace_synth_opts.callchain_sz;
3010
3011         /* make sure PERF_EXEC_PATH is set for scripts */
3012         set_argv_exec_path(get_argv_exec_path());
3013
3014         if (argc && !script_name && !rec_script_path && !rep_script_path) {
3015                 int live_pipe[2];
3016                 int rep_args;
3017                 pid_t pid;
3018
3019                 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3020                 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3021
3022                 if (!rec_script_path && !rep_script_path) {
3023                         usage_with_options_msg(script_usage, options,
3024                                 "Couldn't find script `%s'\n\n See perf"
3025                                 " script -l for available scripts.\n", argv[0]);
3026                 }
3027
3028                 if (is_top_script(argv[0])) {
3029                         rep_args = argc - 1;
3030                 } else {
3031                         int rec_args;
3032
3033                         rep_args = has_required_arg(rep_script_path);
3034                         rec_args = (argc - 1) - rep_args;
3035                         if (rec_args < 0) {
3036                                 usage_with_options_msg(script_usage, options,
3037                                         "`%s' script requires options."
3038                                         "\n\n See perf script -l for available "
3039                                         "scripts and options.\n", argv[0]);
3040                         }
3041                 }
3042
3043                 if (pipe(live_pipe) < 0) {
3044                         perror("failed to create pipe");
3045                         return -1;
3046                 }
3047
3048                 pid = fork();
3049                 if (pid < 0) {
3050                         perror("failed to fork");
3051                         return -1;
3052                 }
3053
3054                 if (!pid) {
3055                         j = 0;
3056
3057                         dup2(live_pipe[1], 1);
3058                         close(live_pipe[0]);
3059
3060                         if (is_top_script(argv[0])) {
3061                                 system_wide = true;
3062                         } else if (!system_wide) {
3063                                 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3064                                         err = -1;
3065                                         goto out;
3066                                 }
3067                         }
3068
3069                         __argv = malloc((argc + 6) * sizeof(const char *));
3070                         if (!__argv) {
3071                                 pr_err("malloc failed\n");
3072                                 err = -ENOMEM;
3073                                 goto out;
3074                         }
3075
3076                         __argv[j++] = "/bin/sh";
3077                         __argv[j++] = rec_script_path;
3078                         if (system_wide)
3079                                 __argv[j++] = "-a";
3080                         __argv[j++] = "-q";
3081                         __argv[j++] = "-o";
3082                         __argv[j++] = "-";
3083                         for (i = rep_args + 1; i < argc; i++)
3084                                 __argv[j++] = argv[i];
3085                         __argv[j++] = NULL;
3086
3087                         execvp("/bin/sh", (char **)__argv);
3088                         free(__argv);
3089                         exit(-1);
3090                 }
3091
3092                 dup2(live_pipe[0], 0);
3093                 close(live_pipe[1]);
3094
3095                 __argv = malloc((argc + 4) * sizeof(const char *));
3096                 if (!__argv) {
3097                         pr_err("malloc failed\n");
3098                         err = -ENOMEM;
3099                         goto out;
3100                 }
3101
3102                 j = 0;
3103                 __argv[j++] = "/bin/sh";
3104                 __argv[j++] = rep_script_path;
3105                 for (i = 1; i < rep_args + 1; i++)
3106                         __argv[j++] = argv[i];
3107                 __argv[j++] = "-i";
3108                 __argv[j++] = "-";
3109                 __argv[j++] = NULL;
3110
3111                 execvp("/bin/sh", (char **)__argv);
3112                 free(__argv);
3113                 exit(-1);
3114         }
3115
3116         if (rec_script_path)
3117                 script_path = rec_script_path;
3118         if (rep_script_path)
3119                 script_path = rep_script_path;
3120
3121         if (script_path) {
3122                 j = 0;
3123
3124                 if (!rec_script_path)
3125                         system_wide = false;
3126                 else if (!system_wide) {
3127                         if (have_cmd(argc - 1, &argv[1]) != 0) {
3128                                 err = -1;
3129                                 goto out;
3130                         }
3131                 }
3132
3133                 __argv = malloc((argc + 2) * sizeof(const char *));
3134                 if (!__argv) {
3135                         pr_err("malloc failed\n");
3136                         err = -ENOMEM;
3137                         goto out;
3138                 }
3139
3140                 __argv[j++] = "/bin/sh";
3141                 __argv[j++] = script_path;
3142                 if (system_wide)
3143                         __argv[j++] = "-a";
3144                 for (i = 2; i < argc; i++)
3145                         __argv[j++] = argv[i];
3146                 __argv[j++] = NULL;
3147
3148                 execvp("/bin/sh", (char **)__argv);
3149                 free(__argv);
3150                 exit(-1);
3151         }
3152
3153         if (!script_name)
3154                 setup_pager();
3155
3156         session = perf_session__new(&data, false, &script.tool);
3157         if (session == NULL)
3158                 return -1;
3159
3160         if (header || header_only) {
3161                 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
3162                 perf_session__fprintf_info(session, stdout, show_full_info);
3163                 if (header_only)
3164                         goto out_delete;
3165         }
3166         if (show_full_info)
3167                 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
3168
3169         if (symbol__init(&session->header.env) < 0)
3170                 goto out_delete;
3171
3172         script.session = session;
3173         script__setup_sample_type(&script);
3174
3175         if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3176                 itrace_synth_opts.thread_stack = true;
3177
3178         session->itrace_synth_opts = &itrace_synth_opts;
3179
3180         if (cpu_list) {
3181                 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3182                 if (err < 0)
3183                         goto out_delete;
3184                 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
3185         }
3186
3187         if (!no_callchain)
3188                 symbol_conf.use_callchain = true;
3189         else
3190                 symbol_conf.use_callchain = false;
3191
3192         if (session->tevent.pevent &&
3193             pevent_set_function_resolver(session->tevent.pevent,
3194                                          machine__resolve_kernel_addr,
3195                                          &session->machines.host) < 0) {
3196                 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3197                 err = -1;
3198                 goto out_delete;
3199         }
3200
3201         if (generate_script_lang) {
3202                 struct stat perf_stat;
3203                 int input;
3204
3205                 if (output_set_by_user()) {
3206                         fprintf(stderr,
3207                                 "custom fields not supported for generated scripts");
3208                         err = -EINVAL;
3209                         goto out_delete;
3210                 }
3211
3212                 input = open(data.file.path, O_RDONLY); /* input_name */
3213                 if (input < 0) {
3214                         err = -errno;
3215                         perror("failed to open file");
3216                         goto out_delete;
3217                 }
3218
3219                 err = fstat(input, &perf_stat);
3220                 if (err < 0) {
3221                         perror("failed to stat file");
3222                         goto out_delete;
3223                 }
3224
3225                 if (!perf_stat.st_size) {
3226                         fprintf(stderr, "zero-sized file, nothing to do!\n");
3227                         goto out_delete;
3228                 }
3229
3230                 scripting_ops = script_spec__lookup(generate_script_lang);
3231                 if (!scripting_ops) {
3232                         fprintf(stderr, "invalid language specifier");
3233                         err = -ENOENT;
3234                         goto out_delete;
3235                 }
3236
3237                 err = scripting_ops->generate_script(session->tevent.pevent,
3238                                                      "perf-script");
3239                 goto out_delete;
3240         }
3241
3242         if (script_name) {
3243                 err = scripting_ops->start_script(script_name, argc, argv);
3244                 if (err)
3245                         goto out_delete;
3246                 pr_debug("perf script started with script %s\n\n", script_name);
3247                 script_started = true;
3248         }
3249
3250
3251         err = perf_session__check_output_opt(session);
3252         if (err < 0)
3253                 goto out_delete;
3254
3255         /* needs to be parsed after looking up reference time */
3256         if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3257                 pr_err("Invalid time string\n");
3258                 err = -EINVAL;
3259                 goto out_delete;
3260         }
3261
3262         err = __cmd_script(&script);
3263
3264         flush_scripting();
3265
3266 out_delete:
3267         perf_evlist__free_stats(session->evlist);
3268         perf_session__delete(session);
3269
3270         if (script_started)
3271                 cleanup_scripting();
3272 out:
3273         return err;
3274 }