~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/builtin-script.c

Version: ~ [ linux-6.1-rc7 ] ~ [ linux-6.0.10 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.80 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.156 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.225 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.267 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.300 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.334 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp