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