blob: 9b93ddeaeafa704b69f3686b55694227aec1e8bb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.h"
Arnaldo Carvalho de Melo0f31c012019-08-21 14:00:17 -03006#include "util/counts.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02009#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060010#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020011#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020012#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020013#include "util/tool.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010014#include "util/map.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020015#include "util/symbol.h"
16#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010017#include "util/trace-event.h"
David Ahern1424dc92011-03-09 22:23:28 -070018#include "util/evlist.h"
19#include "util/evsel.h"
Arnaldo Carvalho de Melod2360442019-08-15 10:37:24 -030020#include "util/evswitch.h"
Feng Tang36385be2012-09-07 16:42:24 +080021#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020022#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030023#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010024#include "util/cpumap.h"
25#include "util/thread_map.h"
26#include "util/stat.h"
Andi Kleen4bd1bef2017-11-17 13:43:00 -080027#include "util/color.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030028#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030029#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070030#include "util/time-utils.h"
Jiri Olsa06c3f2a2017-12-06 18:45:35 +010031#include "util/path.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030032#include "print_binary.h"
Andi Kleen3ab481a2019-03-05 06:47:45 -080033#include "archinsn.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100034#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030035#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030036#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030037#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030038#include <linux/zalloc.h>
Andi Kleen3ab481a2019-03-05 06:47:45 -080039#include <sys/utsname.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010040#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010041#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080042#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030043#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030044#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030045#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030046#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030047#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030048#include <sys/types.h>
49#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030050#include <fcntl.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030051#include <unistd.h>
Andi Kleenb585ebd2018-09-20 11:05:36 -070052#include <subcmd/pager.h>
Jiri Olsa453fa032019-07-21 13:24:43 +020053#include <perf/evlist.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020054
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030055#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030056
Tom Zanussi956ffd02009-11-25 01:15:46 -060057static char const *script_name;
58static char const *generate_script_lang;
Andi Kleen90b10f42019-03-14 15:50:00 -070059static bool reltime;
60static u64 initial_time;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020061static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020062static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020063static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070064static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090065static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010066static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030067static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100068static const char *cpu_list;
69static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010070static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080071static int max_blocks;
Andi Kleen3ab481a2019-03-05 06:47:45 -080072static bool native_arch;
Tom Zanussi956ffd02009-11-25 01:15:46 -060073
Adrian Hunter44cbe722015-09-25 16:15:50 +030074unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030075
David Ahern745f43e2011-03-09 22:23:26 -070076enum perf_output_field {
77 PERF_OUTPUT_COMM = 1U << 0,
78 PERF_OUTPUT_TID = 1U << 1,
79 PERF_OUTPUT_PID = 1U << 2,
80 PERF_OUTPUT_TIME = 1U << 3,
81 PERF_OUTPUT_CPU = 1U << 4,
82 PERF_OUTPUT_EVNAME = 1U << 5,
83 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060084 PERF_OUTPUT_IP = 1U << 7,
85 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060086 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060087 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090088 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020089 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020090 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020091 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020092 PERF_OUTPUT_BRSTACK = 1U << 15,
93 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010094 PERF_OUTPUT_DATA_SRC = 1U << 17,
95 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000096 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030097 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030098 PERF_OUTPUT_INSN = 1U << 21,
99 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -0800100 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -0700101 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +0300102 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -0400103 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -0700104 PERF_OUTPUT_UREGS = 1U << 27,
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800105 PERF_OUTPUT_METRIC = 1U << 28,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100106 PERF_OUTPUT_MISC = 1U << 29,
Andi Kleendd2e18e2018-12-03 16:18:48 -0800107 PERF_OUTPUT_SRCCODE = 1U << 30,
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300108 PERF_OUTPUT_IPC = 1U << 31,
David Ahern745f43e2011-03-09 22:23:26 -0700109};
110
111struct output_option {
112 const char *str;
113 enum perf_output_field field;
114} all_output_options[] = {
115 {.str = "comm", .field = PERF_OUTPUT_COMM},
116 {.str = "tid", .field = PERF_OUTPUT_TID},
117 {.str = "pid", .field = PERF_OUTPUT_PID},
118 {.str = "time", .field = PERF_OUTPUT_TIME},
119 {.str = "cpu", .field = PERF_OUTPUT_CPU},
120 {.str = "event", .field = PERF_OUTPUT_EVNAME},
121 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600122 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700123 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600124 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600125 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900126 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200127 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200128 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200129 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700130 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200131 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
132 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100133 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
134 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000135 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300136 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300137 {.str = "insn", .field = PERF_OUTPUT_INSN},
138 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800139 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700140 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300141 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400142 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800143 {.str = "metric", .field = PERF_OUTPUT_METRIC},
Jiri Olsa28a0b392018-01-07 17:03:52 +0100144 {.str = "misc", .field = PERF_OUTPUT_MISC},
Andi Kleendd2e18e2018-12-03 16:18:48 -0800145 {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
Adrian Hunter68fb45b2019-05-20 14:37:14 +0300146 {.str = "ipc", .field = PERF_OUTPUT_IPC},
David Ahern745f43e2011-03-09 22:23:26 -0700147};
148
Adrian Hunter14057202017-06-21 13:17:19 +0300149enum {
150 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
151 OUTPUT_TYPE_MAX
152};
153
David Ahern745f43e2011-03-09 22:23:26 -0700154/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600155static struct {
156 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600157 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400158 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600159 u64 fields;
160 u64 invalid_fields;
Andi Kleen4b6ac812019-02-24 07:37:12 -0800161 u64 user_set_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300162} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700163
David Ahern2c9e45f72011-03-17 10:03:21 -0600164 [PERF_TYPE_HARDWARE] = {
165 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700166
David Ahern2c9e45f72011-03-17 10:03:21 -0600167 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
168 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600169 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530170 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
171 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600172
Wang Nan30372f02016-02-24 11:20:45 +0000173 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600174 },
175
176 [PERF_TYPE_SOFTWARE] = {
177 .user_set = false,
178
179 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
180 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600181 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530182 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
183 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
184 PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600185
186 .invalid_fields = PERF_OUTPUT_TRACE,
187 },
188
189 [PERF_TYPE_TRACEPOINT] = {
190 .user_set = false,
191
192 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
193 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000194 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600195 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700196
Seeteena Thoufeekfad76d42018-06-08 16:32:28 +0530197 [PERF_TYPE_HW_CACHE] = {
198 .user_set = false,
199
200 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
201 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
202 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
203 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
204 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
205
206 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
207 },
208
Arun Sharma0817a6a2011-04-14 10:38:18 -0700209 [PERF_TYPE_RAW] = {
210 .user_set = false,
211
212 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
213 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600214 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530215 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
216 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
217 PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
218 PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700219
Wang Nan30372f02016-02-24 11:20:45 +0000220 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700221 },
Wang Nan27cfef02015-12-08 02:25:43 +0000222
223 [PERF_TYPE_BREAKPOINT] = {
224 .user_set = false,
225
226 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
227 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
228 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530229 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
230 PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
Wang Nan27cfef02015-12-08 02:25:43 +0000231
Wang Nan30372f02016-02-24 11:20:45 +0000232 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000233 },
Adrian Hunter14057202017-06-21 13:17:19 +0300234
235 [OUTPUT_TYPE_SYNTH] = {
236 .user_set = false,
237
238 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
239 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
240 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Sandipan Das7903a702018-05-17 12:03:26 +0530241 PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
242 PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300243
244 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
245 },
David Ahern1424dc92011-03-09 22:23:28 -0700246};
David Ahern745f43e2011-03-09 22:23:26 -0700247
Jiri Olsa32dcd022019-07-21 13:23:51 +0200248struct evsel_script {
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300249 char *filename;
250 FILE *fp;
251 u64 samples;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800252 /* For metric output */
253 u64 val;
254 int gnum;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300255};
256
Jiri Olsa32dcd022019-07-21 13:23:51 +0200257static inline struct evsel_script *evsel_script(struct evsel *evsel)
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800258{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200259 return (struct evsel_script *)evsel->priv;
Andi Kleen4bd1bef2017-11-17 13:43:00 -0800260}
261
Jiri Olsa32dcd022019-07-21 13:23:51 +0200262static struct evsel_script *perf_evsel_script__new(struct evsel *evsel,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100263 struct perf_data *data)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300264{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200265 struct evsel_script *es = zalloc(sizeof(*es));
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300266
267 if (es != NULL) {
Jiri Olsaeae8ad82017-01-23 22:25:41 +0100268 if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300269 goto out_free;
270 es->fp = fopen(es->filename, "w");
271 if (es->fp == NULL)
272 goto out_free_filename;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300273 }
274
275 return es;
276out_free_filename:
277 zfree(&es->filename);
278out_free:
279 free(es);
280 return NULL;
281}
282
Jiri Olsa32dcd022019-07-21 13:23:51 +0200283static void perf_evsel_script__delete(struct evsel_script *es)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300284{
285 zfree(&es->filename);
286 fclose(es->fp);
287 es->fp = NULL;
288 free(es);
289}
290
Jiri Olsa32dcd022019-07-21 13:23:51 +0200291static int perf_evsel_script__fprintf(struct evsel_script *es, FILE *fp)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -0300292{
293 struct stat st;
294
295 fstat(fileno(es->fp), &st);
296 return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
297 st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
298}
299
Adrian Hunter14057202017-06-21 13:17:19 +0300300static inline int output_type(unsigned int type)
301{
302 switch (type) {
303 case PERF_TYPE_SYNTH:
304 return OUTPUT_TYPE_SYNTH;
305 default:
306 return type;
307 }
308}
309
310static inline unsigned int attr_type(unsigned int type)
311{
312 switch (type) {
313 case OUTPUT_TYPE_SYNTH:
314 return PERF_TYPE_SYNTH;
315 default:
316 return type;
317 }
318}
319
David Ahern2c9e45f72011-03-17 10:03:21 -0600320static bool output_set_by_user(void)
321{
322 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300323 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600324 if (output[j].user_set)
325 return true;
326 }
327 return false;
328}
David Ahern745f43e2011-03-09 22:23:26 -0700329
David Ahern9cbdb702011-04-06 21:54:20 -0600330static const char *output_field2str(enum perf_output_field field)
331{
332 int i, imax = ARRAY_SIZE(all_output_options);
333 const char *str = "";
334
335 for (i = 0; i < imax; ++i) {
336 if (all_output_options[i].field == field) {
337 str = all_output_options[i].str;
338 break;
339 }
340 }
341 return str;
342}
343
Adrian Hunter14057202017-06-21 13:17:19 +0300344#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700345
Jiri Olsa32dcd022019-07-21 13:23:51 +0200346static int perf_evsel__do_check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300347 u64 sample_type, const char *sample_msg,
348 enum perf_output_field field,
349 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700350{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200351 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300352 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600353 const char *evname;
354
355 if (attr->sample_type & sample_type)
356 return 0;
357
Andi Kleen4b6ac812019-02-24 07:37:12 -0800358 if (output[type].user_set_fields & field) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300359 if (allow_user_set)
360 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300361 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600362 pr_err("Samples for '%s' event do not have %s attribute set. "
363 "Cannot print '%s' field.\n",
364 evname, sample_msg, output_field2str(field));
365 return -1;
366 }
367
368 /* user did not ask for it explicitly so remove from the default list */
369 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300370 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600371 pr_debug("Samples for '%s' event do not have %s attribute set. "
372 "Skipping '%s' field.\n",
373 evname, sample_msg, output_field2str(field));
374
375 return 0;
376}
377
Jiri Olsa32dcd022019-07-21 13:23:51 +0200378static int perf_evsel__check_stype(struct evsel *evsel,
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300379 u64 sample_type, const char *sample_msg,
380 enum perf_output_field field)
381{
382 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
383 false);
384}
385
Jiri Olsa32dcd022019-07-21 13:23:51 +0200386static int perf_evsel__check_attr(struct evsel *evsel,
David Ahern9cbdb702011-04-06 21:54:20 -0600387 struct perf_session *session)
388{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200389 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300390 bool allow_user_set;
391
Jiri Olsae099eba2016-01-05 22:09:09 +0100392 if (perf_header__has_feat(&session->header, HEADER_STAT))
393 return 0;
394
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300395 allow_user_set = perf_header__has_feat(&session->header,
396 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600397
David Ahern1424dc92011-03-09 22:23:28 -0700398 if (PRINT_FIELD(TRACE) &&
399 !perf_session__has_traces(session, "record -R"))
400 return -EINVAL;
401
David Ahern787bef12011-05-27 14:28:43 -0600402 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300403 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
404 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700405 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700406 }
David Ahern7cec0922011-05-30 13:08:23 -0600407
408 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300409 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
410 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600411 return -EINVAL;
412
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100413 if (PRINT_FIELD(DATA_SRC) &&
414 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
415 PERF_OUTPUT_DATA_SRC))
416 return -EINVAL;
417
418 if (PRINT_FIELD(WEIGHT) &&
419 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
420 PERF_OUTPUT_WEIGHT))
421 return -EINVAL;
422
Andi Kleen37fed3d2018-09-18 05:32:09 -0700423 if (PRINT_FIELD(SYM) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200424 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
David Ahern7cec0922011-05-30 13:08:23 -0600425 pr_err("Display of symbols requested but neither sample IP nor "
Andi Kleen37fed3d2018-09-18 05:32:09 -0700426 "sample address\navailable. Hence, no addresses to convert "
David Ahern7cec0922011-05-30 13:08:23 -0600427 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600428 return -EINVAL;
429 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900430 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
431 pr_err("Display of offsets requested but symbol is not"
432 "selected.\n");
433 return -EINVAL;
434 }
Andi Kleen37fed3d2018-09-18 05:32:09 -0700435 if (PRINT_FIELD(DSO) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200436 !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
Andi Kleen37fed3d2018-09-18 05:32:09 -0700437 pr_err("Display of DSO requested but no address to convert.\n");
David Ahern610723f2011-05-27 14:28:44 -0600438 return -EINVAL;
439 }
Andi Kleendd2e18e2018-12-03 16:18:48 -0800440 if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
Adrian Huntercc8fae12013-12-06 09:42:57 +0200441 pr_err("Display of source line number requested but sample IP is not\n"
442 "selected. Hence, no address to lookup the source line number.\n");
443 return -EINVAL;
444 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800445 if (PRINT_FIELD(BRSTACKINSN) &&
446 !(perf_evlist__combined_branch_type(session->evlist) &
447 PERF_SAMPLE_BRANCH_ANY)) {
448 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
449 "Hint: run 'perf record -b ...'\n");
450 return -EINVAL;
451 }
David Ahern1424dc92011-03-09 22:23:28 -0700452 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300453 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
454 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700455 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700456
457 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300458 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
459 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700460 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700461
462 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300463 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
464 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700465 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600466
Stephane Eranianfc36f942015-08-31 18:41:10 +0200467 if (PRINT_FIELD(IREGS) &&
468 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
469 PERF_OUTPUT_IREGS))
470 return -EINVAL;
471
Andi Kleenb1491ac2017-09-05 11:40:57 -0700472 if (PRINT_FIELD(UREGS) &&
473 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
474 PERF_OUTPUT_UREGS))
475 return -EINVAL;
476
Kan Liang49d58f02017-08-29 13:11:11 -0400477 if (PRINT_FIELD(PHYS_ADDR) &&
478 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
479 PERF_OUTPUT_PHYS_ADDR))
480 return -EINVAL;
481
David Ahern9cbdb702011-04-06 21:54:20 -0600482 return 0;
483}
484
Adrian Hunter7ea95722013-11-01 15:51:30 +0200485static void set_print_ip_opts(struct perf_event_attr *attr)
486{
Adrian Hunter14057202017-06-21 13:17:19 +0300487 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200488
489 output[type].print_ip_opts = 0;
490 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300491 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200492
493 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300494 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200495
496 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300497 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200498
499 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300500 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200501
502 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300503 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200504}
505
David Ahern9cbdb702011-04-06 21:54:20 -0600506/*
507 * verify all user requested events exist and the samples
508 * have the expected data
509 */
510static int perf_session__check_output_opt(struct perf_session *session)
511{
He Kuang40f20e52016-05-16 04:51:19 +0000512 unsigned int j;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200513 struct evsel *evsel;
David Ahern9cbdb702011-04-06 21:54:20 -0600514
Adrian Hunter14057202017-06-21 13:17:19 +0300515 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
516 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600517
518 /*
519 * even if fields is set to 0 (ie., show nothing) event must
520 * exist if user explicitly includes it on the command line
521 */
Adrian Hunter14057202017-06-21 13:17:19 +0300522 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
523 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600524 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300525 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600526 event_type(j));
527 return -1;
528 }
529
530 if (evsel && output[j].fields &&
531 perf_evsel__check_attr(evsel, session))
532 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400533
534 if (evsel == NULL)
535 continue;
536
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200537 set_print_ip_opts(&evsel->core.attr);
David Ahern1424dc92011-03-09 22:23:28 -0700538 }
539
Adrian Hunter98526ee2014-07-31 09:00:59 +0300540 if (!no_callchain) {
541 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000542 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300543
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300544 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000545 not_pipe = true;
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300546 if (evsel__has_callchain(evsel)) {
Adrian Hunter98526ee2014-07-31 09:00:59 +0300547 use_callchain = true;
548 break;
549 }
550 }
He Kuang71ac8992016-08-04 11:25:43 +0000551 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300552 symbol_conf.use_callchain = false;
553 }
554
David Ahern80b8b492013-11-19 21:07:37 -0700555 /*
556 * set default for tracepoints to print symbols only
557 * if callchains are present
558 */
559 if (symbol_conf.use_callchain &&
560 !output[PERF_TYPE_TRACEPOINT].user_set) {
David Ahern80b8b492013-11-19 21:07:37 -0700561 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700562
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300563 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200564 if (evsel->core.attr.type != j)
He Kuang40f20e52016-05-16 04:51:19 +0000565 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700566
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -0300567 if (evsel__has_callchain(evsel)) {
He Kuang40f20e52016-05-16 04:51:19 +0000568 output[j].fields |= PERF_OUTPUT_IP;
569 output[j].fields |= PERF_OUTPUT_SYM;
Sandipan Das7903a702018-05-17 12:03:26 +0530570 output[j].fields |= PERF_OUTPUT_SYMOFFSET;
He Kuang40f20e52016-05-16 04:51:19 +0000571 output[j].fields |= PERF_OUTPUT_DSO;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200572 set_print_ip_opts(&evsel->core.attr);
He Kuang40f20e52016-05-16 04:51:19 +0000573 goto out;
574 }
David Ahern80b8b492013-11-19 21:07:37 -0700575 }
576 }
577
578out:
David Ahern1424dc92011-03-09 22:23:28 -0700579 return 0;
580}
David Ahern745f43e2011-03-09 22:23:26 -0700581
Milian Wolff9add8fe2018-11-07 23:34:37 +0100582static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
583 FILE *fp
584)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200585{
Andi Kleenb1491ac2017-09-05 11:40:57 -0700586 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300587 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700588
589 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300590 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700591
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300592 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700593
594 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
595 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300596 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700597 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300598
Milian Wolffb07d16f2018-11-07 10:37:05 +0100599 fprintf(fp, "\n");
600
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300601 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700602}
603
Milian Wolff9add8fe2018-11-07 23:34:37 +0100604static int perf_sample__fprintf_iregs(struct perf_sample *sample,
605 struct perf_event_attr *attr, FILE *fp)
606{
607 return perf_sample__fprintf_regs(&sample->intr_regs,
608 attr->sample_regs_intr, fp);
609}
610
611static int perf_sample__fprintf_uregs(struct perf_sample *sample,
612 struct perf_event_attr *attr, FILE *fp)
613{
614 return perf_sample__fprintf_regs(&sample->user_regs,
615 attr->sample_regs_user, fp);
616}
617
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300618static int perf_sample__fprintf_start(struct perf_sample *sample,
619 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200620 struct evsel *evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +0100621 u32 type, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700622{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200623 struct perf_event_attr *attr = &evsel->core.attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700624 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700625 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300626 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700627
David Ahern745f43e2011-03-09 22:23:26 -0700628 if (PRINT_FIELD(COMM)) {
629 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300630 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
Arnaldo Carvalho de Melob8798332018-06-04 10:34:20 -0300631 else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300632 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700633 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300634 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700635 }
David Ahernc70c94b2011-03-09 22:23:25 -0700636
David Ahern745f43e2011-03-09 22:23:26 -0700637 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300638 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700639 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300640 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700641 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300642 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700643
David Ahern745f43e2011-03-09 22:23:26 -0700644 if (PRINT_FIELD(CPU)) {
645 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300646 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700647 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300648 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700649 }
David Ahernc70c94b2011-03-09 22:23:25 -0700650
Jiri Olsa28a0b392018-01-07 17:03:52 +0100651 if (PRINT_FIELD(MISC)) {
652 int ret = 0;
653
654 #define has(m) \
655 (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
656
657 if (has(KERNEL))
658 ret += fprintf(fp, "K");
659 if (has(USER))
660 ret += fprintf(fp, "U");
661 if (has(HYPERVISOR))
662 ret += fprintf(fp, "H");
663 if (has(GUEST_KERNEL))
664 ret += fprintf(fp, "G");
665 if (has(GUEST_USER))
666 ret += fprintf(fp, "g");
667
668 switch (type) {
669 case PERF_RECORD_MMAP:
670 case PERF_RECORD_MMAP2:
671 if (has(MMAP_DATA))
672 ret += fprintf(fp, "M");
673 break;
674 case PERF_RECORD_COMM:
675 if (has(COMM_EXEC))
676 ret += fprintf(fp, "E");
677 break;
678 case PERF_RECORD_SWITCH:
679 case PERF_RECORD_SWITCH_CPU_WIDE:
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300680 if (has(SWITCH_OUT)) {
Jiri Olsa28a0b392018-01-07 17:03:52 +0100681 ret += fprintf(fp, "S");
Alexey Budankovbf30cc12018-04-09 10:26:46 +0300682 if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
683 ret += fprintf(fp, "p");
684 }
Jiri Olsa28a0b392018-01-07 17:03:52 +0100685 default:
686 break;
687 }
688
689 #undef has
690
691 ret += fprintf(fp, "%*s", 6 - ret, " ");
692 printed += ret;
693 }
694
David Ahern745f43e2011-03-09 22:23:26 -0700695 if (PRINT_FIELD(TIME)) {
Andi Kleen90b10f42019-03-14 15:50:00 -0700696 u64 t = sample->time;
697 if (reltime) {
698 if (!initial_time)
699 initial_time = sample->time;
700 t = sample->time - initial_time;
701 }
702 nsecs = t;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300703 secs = nsecs / NSEC_PER_SEC;
704 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900705
Andi Kleen52bab882019-03-05 06:47:47 -0800706 if (symbol_conf.nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300707 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900708 else {
709 char sample_time[32];
Andi Kleen90b10f42019-03-14 15:50:00 -0700710 timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300711 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900712 }
David Ahern745f43e2011-03-09 22:23:26 -0700713 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300714
715 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700716}
717
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200718static inline char
719mispred_str(struct branch_entry *br)
720{
721 if (!(br->flags.mispred || br->flags.predicted))
722 return '-';
723
724 return br->flags.predicted ? 'P' : 'M';
725}
726
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300727static int perf_sample__fprintf_brstack(struct perf_sample *sample,
728 struct thread *thread,
729 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200730{
731 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700732 struct addr_location alf, alt;
733 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300734 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200735
736 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300737 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200738
739 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700740 from = br->entries[i].from;
741 to = br->entries[i].to;
742
743 if (PRINT_FIELD(DSO)) {
744 memset(&alf, 0, sizeof(alf));
745 memset(&alt, 0, sizeof(alt));
Adrian Hunter692d0e62018-11-06 23:07:12 +0200746 thread__find_map_fb(thread, sample->cpumode, from, &alf);
747 thread__find_map_fb(thread, sample->cpumode, to, &alt);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700748 }
749
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300750 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700751 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300752 printed += fprintf(fp, "(");
753 printed += map__fprintf_dsoname(alf.map, fp);
754 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700755 }
756
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300757 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700758 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300759 printed += fprintf(fp, "(");
760 printed += map__fprintf_dsoname(alt.map, fp);
761 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700762 }
763
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300764 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200765 mispred_str( br->entries + i),
766 br->entries[i].flags.in_tx? 'X' : '-',
767 br->entries[i].flags.abort? 'A' : '-',
768 br->entries[i].flags.cycles);
769 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300770
771 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200772}
773
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300774static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
775 struct thread *thread,
776 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200777{
778 struct branch_stack *br = sample->branch_stack;
779 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200780 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300781 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200782
783 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300784 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200785
786 for (i = 0; i < br->nr; i++) {
787
788 memset(&alf, 0, sizeof(alf));
789 memset(&alt, 0, sizeof(alt));
790 from = br->entries[i].from;
791 to = br->entries[i].to;
792
Adrian Hunter692d0e62018-11-06 23:07:12 +0200793 thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
794 thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200795
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300796 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700797 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300798 printed += fprintf(fp, "(");
799 printed += map__fprintf_dsoname(alf.map, fp);
800 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700801 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300802 printed += fprintf(fp, "%c", '/');
803 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700804 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300805 printed += fprintf(fp, "(");
806 printed += map__fprintf_dsoname(alt.map, fp);
807 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700808 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300809 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200810 mispred_str( br->entries + i),
811 br->entries[i].flags.in_tx? 'X' : '-',
812 br->entries[i].flags.abort? 'A' : '-',
813 br->entries[i].flags.cycles);
814 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300815
816 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200817}
818
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300819static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
820 struct thread *thread,
821 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700822{
823 struct branch_stack *br = sample->branch_stack;
824 struct addr_location alf, alt;
825 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300826 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700827
828 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300829 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700830
831 for (i = 0; i < br->nr; i++) {
832
833 memset(&alf, 0, sizeof(alf));
834 memset(&alt, 0, sizeof(alt));
835 from = br->entries[i].from;
836 to = br->entries[i].to;
837
Adrian Hunter692d0e62018-11-06 23:07:12 +0200838 if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300839 !alf.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700840 from = map__map_ip(alf.map, from);
841
Adrian Hunter692d0e62018-11-06 23:07:12 +0200842 if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300843 !alt.map->dso->adjust_symbols)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700844 to = map__map_ip(alt.map, to);
845
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300846 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700847 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300848 printed += fprintf(fp, "(");
849 printed += map__fprintf_dsoname(alf.map, fp);
850 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700851 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300852 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700853 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300854 printed += fprintf(fp, "(");
855 printed += map__fprintf_dsoname(alt.map, fp);
856 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700857 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300858 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700859 mispred_str(br->entries + i),
860 br->entries[i].flags.in_tx ? 'X' : '-',
861 br->entries[i].flags.abort ? 'A' : '-',
862 br->entries[i].flags.cycles);
863 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300864
865 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700866}
Andi Kleen48d02a12017-02-23 15:46:34 -0800867#define MAXBB 16384UL
868
869static int grab_bb(u8 *buffer, u64 start, u64 end,
870 struct machine *machine, struct thread *thread,
871 bool *is64bit, u8 *cpumode, bool last)
872{
873 long offset, len;
874 struct addr_location al;
875 bool kernel;
876
877 if (!start || !end)
878 return 0;
879
880 kernel = machine__kernel_ip(machine, start);
881 if (kernel)
882 *cpumode = PERF_RECORD_MISC_KERNEL;
883 else
884 *cpumode = PERF_RECORD_MISC_USER;
885
886 /*
887 * Block overlaps between kernel and user.
888 * This can happen due to ring filtering
889 * On Intel CPUs the entry into the kernel is filtered,
890 * but the exit is not. Let the caller patch it up.
891 */
892 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300893 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800894 return -ENXIO;
895 }
896
897 memset(&al, 0, sizeof(al));
898 if (end - start > MAXBB - MAXINSN) {
899 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300900 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800901 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300902 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800903 return 0;
904 }
905
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300906 if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300907 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800908 return 0;
909 }
910 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300911 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800912 return 0;
913 }
914
915 /* Load maps to ensure dso->is_64_bit has been updated */
916 map__load(al.map);
917
918 offset = al.map->map_ip(al.map, start);
919 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
920 end - start + MAXINSN);
921
922 *is64bit = al.map->dso->is_64_bit;
923 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300924 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800925 start, end);
926 return len;
927}
928
Andi Kleendd2e18e2018-12-03 16:18:48 -0800929static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
930{
931 struct addr_location al;
932 int ret = 0;
933
934 memset(&al, 0, sizeof(al));
935 thread__find_map(thread, cpumode, addr, &al);
936 if (!al.map)
937 return 0;
938 ret = map__fprintf_srccode(al.map, al.addr, stdout,
939 &thread->srccode_state);
940 if (ret)
941 ret += printf("\n");
942 return ret;
943}
944
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300945static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
946 struct perf_insn *x, u8 *inbuf, int len,
Andi Kleenfe571202018-09-24 10:07:32 -0700947 int insn, FILE *fp, int *total_cycles)
Andi Kleen48d02a12017-02-23 15:46:34 -0800948{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300949 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
950 dump_insn(x, ip, inbuf, len, NULL),
951 en->flags.predicted ? " PRED" : "",
952 en->flags.mispred ? " MISPRED" : "",
953 en->flags.in_tx ? " INTX" : "",
954 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800955 if (en->flags.cycles) {
Andi Kleenfe571202018-09-24 10:07:32 -0700956 *total_cycles += en->flags.cycles;
957 printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800958 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300959 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800960 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300961 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800962}
963
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300964static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
965 u8 cpumode, int cpu, struct symbol **lastsym,
966 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800967{
968 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300969 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800970
971 memset(&al, 0, sizeof(al));
972
Arnaldo Carvalho de Melo404eb5a2018-04-26 09:34:37 -0300973 thread__find_map(thread, cpumode, addr, &al);
974
Andi Kleen48d02a12017-02-23 15:46:34 -0800975 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300976 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800977
978 al.cpu = cpu;
979 al.sym = NULL;
980 if (al.map)
981 al.sym = map__find_symbol(al.map, al.addr);
982
983 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300984 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800985
986 if (al.addr < al.sym->end)
987 off = al.addr - al.sym->start;
988 else
989 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300990 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800991 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300992 printed += fprintf(fp, "%+d", off);
993 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800994 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300995 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
996 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800997 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300998
999 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001000}
1001
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001002static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
1003 struct thread *thread,
1004 struct perf_event_attr *attr,
1005 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -08001006{
1007 struct branch_stack *br = sample->branch_stack;
1008 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001009 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001010 struct perf_insn x;
1011 u8 buffer[MAXBB];
1012 unsigned off;
1013 struct symbol *lastsym = NULL;
Andi Kleenfe571202018-09-24 10:07:32 -07001014 int total_cycles = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001015
1016 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001017 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -08001018 nr = br->nr;
1019 if (max_blocks && nr > max_blocks + 1)
1020 nr = max_blocks + 1;
1021
1022 x.thread = thread;
1023 x.cpu = sample->cpu;
1024
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001025 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -08001026
1027 /* Handle first from jump, of which we don't know the entry. */
1028 len = grab_bb(buffer, br->entries[nr-1].from,
1029 br->entries[nr-1].from,
1030 machine, thread, &x.is64bit, &x.cpumode, false);
1031 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001032 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
1033 x.cpumode, x.cpu, &lastsym, attr, fp);
1034 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
Andi Kleenfe571202018-09-24 10:07:32 -07001035 &x, buffer, len, 0, fp, &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001036 if (PRINT_FIELD(SRCCODE))
1037 printed += print_srccode(thread, x.cpumode, br->entries[nr - 1].from);
Andi Kleen48d02a12017-02-23 15:46:34 -08001038 }
1039
1040 /* Print all blocks */
1041 for (i = nr - 2; i >= 0; i--) {
1042 if (br->entries[i].from || br->entries[i].to)
1043 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
1044 br->entries[i].from,
1045 br->entries[i].to);
1046 start = br->entries[i + 1].to;
1047 end = br->entries[i].from;
1048
1049 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1050 /* Patch up missing kernel transfers due to ring filters */
1051 if (len == -ENXIO && i > 0) {
1052 end = br->entries[--i].from;
1053 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
1054 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
1055 }
1056 if (len <= 0)
1057 continue;
1058
1059 insn = 0;
1060 for (off = 0;; off += ilen) {
1061 uint64_t ip = start + off;
1062
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001063 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001064 if (ip == end) {
Andi Kleendde4e732019-07-11 11:19:21 -07001065 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, ++insn, fp,
Andi Kleenfe571202018-09-24 10:07:32 -07001066 &total_cycles);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001067 if (PRINT_FIELD(SRCCODE))
1068 printed += print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001069 break;
1070 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001071 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
1072 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001073 if (ilen == 0)
1074 break;
Andi Kleendd2e18e2018-12-03 16:18:48 -08001075 if (PRINT_FIELD(SRCCODE))
1076 print_srccode(thread, x.cpumode, ip);
Andi Kleen48d02a12017-02-23 15:46:34 -08001077 insn++;
1078 }
1079 }
1080 }
1081
1082 /*
1083 * Hit the branch? In this case we are already done, and the target
1084 * has not been executed yet.
1085 */
1086 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001087 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001088 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001089 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001090
1091 /*
1092 * Print final block upto sample
Andi Kleen61f61152018-11-19 21:06:17 -08001093 *
1094 * Due to pipeline delays the LBRs might be missing a branch
1095 * or two, which can result in very large or negative blocks
1096 * between final branch and sample. When this happens just
1097 * continue walking after the last TO until we hit a branch.
Andi Kleen48d02a12017-02-23 15:46:34 -08001098 */
1099 start = br->entries[0].to;
1100 end = sample->ip;
Andi Kleen61f61152018-11-19 21:06:17 -08001101 if (end < start) {
1102 /* Missing jump. Scan 128 bytes for the next branch */
1103 end = start + 128;
1104 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001105 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001106 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -08001107 if (len <= 0) {
1108 /* Print at least last IP if basic block did not work */
1109 len = grab_bb(buffer, sample->ip, sample->ip,
1110 machine, thread, &x.is64bit, &x.cpumode, false);
1111 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001112 goto out;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001113 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -08001114 dump_insn(&x, sample->ip, buffer, len, NULL));
Andi Kleendd2e18e2018-12-03 16:18:48 -08001115 if (PRINT_FIELD(SRCCODE))
1116 print_srccode(thread, x.cpumode, sample->ip);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001117 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -08001118 }
1119 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001120 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
1121 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -08001122 if (ilen == 0)
1123 break;
Andi Kleen61f61152018-11-19 21:06:17 -08001124 if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
1125 /*
1126 * Hit a missing branch. Just stop.
1127 */
1128 printed += fprintf(fp, "\t... not reaching sample ...\n");
1129 break;
1130 }
Andi Kleendd2e18e2018-12-03 16:18:48 -08001131 if (PRINT_FIELD(SRCCODE))
1132 print_srccode(thread, x.cpumode, start + off);
Andi Kleen48d02a12017-02-23 15:46:34 -08001133 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001134out:
1135 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -08001136}
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001137
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001138static int perf_sample__fprintf_addr(struct perf_sample *sample,
1139 struct thread *thread,
1140 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -06001141{
1142 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001143 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -06001144
1145 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001146 goto out;
David Ahern7cec0922011-05-30 13:08:23 -06001147
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -03001148 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -06001149
1150 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001151 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001152 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001153 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001154 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001155 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001156 }
1157
1158 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001159 printed += fprintf(fp, " (");
1160 printed += map__fprintf_dsoname(al.map, fp);
1161 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001162 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001163out:
1164 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001165}
1166
Andi Kleen99f753f2018-09-20 11:05:39 -07001167static const char *resolve_branch_sym(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001168 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001169 struct thread *thread,
1170 struct addr_location *al,
1171 u64 *ip)
1172{
1173 struct addr_location addr_al;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001174 struct perf_event_attr *attr = &evsel->core.attr;
Andi Kleen99f753f2018-09-20 11:05:39 -07001175 const char *name = NULL;
1176
1177 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1178 if (sample_addr_correlates_sym(attr)) {
1179 thread__resolve(thread, &addr_al, sample);
1180 if (addr_al.sym)
1181 name = addr_al.sym->name;
1182 else
1183 *ip = sample->addr;
1184 } else {
1185 *ip = sample->addr;
1186 }
1187 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1188 if (al->sym)
1189 name = al->sym->name;
1190 else
1191 *ip = sample->ip;
1192 }
1193 return name;
1194}
1195
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001196static int perf_sample__fprintf_callindent(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001197 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001198 struct thread *thread,
1199 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001200{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001201 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter256d92b2018-12-21 14:06:19 +02001202 size_t depth = thread_stack__depth(thread, sample->cpu);
Adrian Huntere2167082016-06-23 16:40:58 +03001203 const char *name = NULL;
1204 static int spacing;
1205 int len = 0;
Andi Kleena78cdee2018-09-18 05:32:10 -07001206 int dlen = 0;
Adrian Huntere2167082016-06-23 16:40:58 +03001207 u64 ip = 0;
1208
1209 /*
1210 * The 'return' has already been popped off the stack so the depth has
1211 * to be adjusted to match the 'call'.
1212 */
1213 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1214 depth += 1;
1215
Andi Kleen99f753f2018-09-20 11:05:39 -07001216 name = resolve_branch_sym(sample, evsel, thread, al, &ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001217
Andi Kleena78cdee2018-09-18 05:32:10 -07001218 if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
1219 dlen += fprintf(fp, "(");
1220 dlen += map__fprintf_dsoname(al->map, fp);
1221 dlen += fprintf(fp, ")\t");
1222 }
1223
Adrian Huntere2167082016-06-23 16:40:58 +03001224 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001225 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001226 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001227 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001228
1229 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001230 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001231
1232 /*
1233 * Try to keep the output length from changing frequently so that the
1234 * output lines up more nicely.
1235 */
1236 if (len > spacing || (len && len < spacing - 52))
1237 spacing = round_up(len + 4, 32);
1238
1239 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001240 len += fprintf(fp, "%*s", spacing - len, "");
1241
Andi Kleena78cdee2018-09-18 05:32:10 -07001242 return len + dlen;
Adrian Huntere2167082016-06-23 16:40:58 +03001243}
1244
Andi Kleen3ab481a2019-03-05 06:47:45 -08001245__weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
1246 struct thread *thread __maybe_unused,
1247 struct machine *machine __maybe_unused)
1248{
1249}
1250
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001251static int perf_sample__fprintf_insn(struct perf_sample *sample,
1252 struct perf_event_attr *attr,
1253 struct thread *thread,
1254 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001255{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001256 int printed = 0;
1257
Andi Kleen3ab481a2019-03-05 06:47:45 -08001258 if (sample->insn_len == 0 && native_arch)
1259 arch_fetch_insn(sample, thread, machine);
1260
Andi Kleen224e2c92016-10-07 16:42:27 +03001261 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001262 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen3ab481a2019-03-05 06:47:45 -08001263 if (PRINT_FIELD(INSN) && sample->insn_len) {
Andi Kleen224e2c92016-10-07 16:42:27 +03001264 int i;
1265
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001266 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001267 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001268 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001269 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001270 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001271 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1272
1273 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001274}
1275
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001276static int perf_sample__fprintf_ipc(struct perf_sample *sample,
1277 struct perf_event_attr *attr, FILE *fp)
1278{
1279 unsigned int ipc;
1280
1281 if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
1282 return 0;
1283
1284 ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
1285
1286 return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
1287 ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
1288}
1289
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001290static int perf_sample__fprintf_bts(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001291 struct evsel *evsel,
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001292 struct thread *thread,
1293 struct addr_location *al,
1294 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001295{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001296 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001297 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001298 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001299 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001300
Adrian Huntere2167082016-06-23 16:40:58 +03001301 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001302 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001303
Akihiro Nagai95582592012-01-30 13:43:09 +09001304 /* print branch_from information */
1305 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001306 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001307 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001308
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001309 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001310 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001311 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001312 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001313
1314 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001315 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001316 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001317 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001318 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001319 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001320 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001321 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001322
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001323 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001324 }
1325
Akihiro Nagai95582592012-01-30 13:43:09 +09001326 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001327 if (PRINT_FIELD(ADDR) ||
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001328 ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001329 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001330 printed += fprintf(fp, " => ");
1331 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001332 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001333
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001334 printed += perf_sample__fprintf_ipc(sample, attr, fp);
1335
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001336 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001337 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001338
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001339 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Andi Kleendd2e18e2018-12-03 16:18:48 -08001340 printed += fprintf(fp, "\n");
1341 if (PRINT_FIELD(SRCCODE)) {
1342 int ret = map__fprintf_srccode(al->map, al->addr, stdout,
1343 &thread->srccode_state);
1344 if (ret) {
1345 printed += ret;
1346 printed += printf("\n");
1347 }
1348 }
1349 return printed;
Akihiro Nagai95582592012-01-30 13:43:09 +09001350}
1351
Adrian Hunter055cd332016-06-23 16:40:56 +03001352static struct {
1353 u32 flags;
1354 const char *name;
1355} sample_flags[] = {
1356 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1357 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1358 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1359 {PERF_IP_FLAG_BRANCH, "jmp"},
1360 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1361 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1362 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1363 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1364 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1366 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1367 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1368 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1369 {0, NULL}
1370};
1371
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001372static const char *sample_flags_to_name(u32 flags)
1373{
1374 int i;
1375
1376 for (i = 0; sample_flags[i].name ; i++) {
1377 if (sample_flags[i].flags == flags)
1378 return sample_flags[i].name;
1379 }
1380
1381 return NULL;
1382}
1383
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001384static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001385{
1386 const char *chars = PERF_IP_FLAG_CHARS;
1387 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001388 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1389 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001390 char str[33];
1391 int i, pos = 0;
1392
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001393 name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
1394 if (name)
1395 return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
1396
1397 if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
1398 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
1399 if (name)
1400 return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
1401 }
1402
1403 if (flags & PERF_IP_FLAG_TRACE_END) {
1404 name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
1405 if (name)
1406 return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
Adrian Hunter055cd332016-06-23 16:40:56 +03001407 }
1408
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001409 for (i = 0; i < n; i++, flags >>= 1) {
1410 if (flags & 1)
1411 str[pos++] = chars[i];
1412 }
1413 for (; i < 32; i++, flags >>= 1) {
1414 if (flags & 1)
1415 str[pos++] = '?';
1416 }
1417 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001418
Adrian Hunter62cb1b82018-09-20 16:00:43 +03001419 return fprintf(fp, " %-19s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001420}
1421
Wang Nan30372f02016-02-24 11:20:45 +00001422struct printer_data {
1423 int line_no;
1424 bool hit_nul;
1425 bool is_printable;
1426};
1427
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001428static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1429 unsigned int val,
1430 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001431{
1432 unsigned char ch = (unsigned char)val;
1433 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001434 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001435
1436 switch (op) {
1437 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001438 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001439 break;
1440 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001441 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001442 " ");
1443 break;
1444 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001445 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001446 break;
1447 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001448 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001449 break;
1450 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001451 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001452 break;
1453 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001454 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001455 break;
1456 case BINARY_PRINT_CHAR_DATA:
1457 if (printer_data->hit_nul && ch)
1458 printer_data->is_printable = false;
1459
1460 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001461 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001462
1463 if (!printer_data->is_printable)
1464 break;
1465
1466 if (ch == '\0')
1467 printer_data->hit_nul = true;
1468 else
1469 printer_data->is_printable = false;
1470 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001471 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001472 }
1473 break;
1474 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001475 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001476 break;
1477 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001478 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001479 printer_data->line_no++;
1480 break;
1481 case BINARY_PRINT_DATA_END:
1482 default:
1483 break;
1484 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001485
1486 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001487}
1488
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001489static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001490{
1491 unsigned int nr_bytes = sample->raw_size;
1492 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001493 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1494 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001495
1496 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001497 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1498
1499 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001500}
1501
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001502static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001503{
1504 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001505 return fprintf(fp, "%*s", spacing - len, "");
1506
1507 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001508}
1509
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001510static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001511{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001512 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001513}
1514
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001515static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001516{
1517 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1518 int len;
1519
1520 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001521 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001522
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001523 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001524 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001525 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001526}
1527
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001528static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001529{
1530 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1531 int len;
1532
1533 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001534 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001535
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001536 len = fprintf(fp, " hints: %#x extensions: %#x ",
1537 data->hints, data->extensions);
1538 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001539}
1540
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001541static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001542{
1543 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1544 int len;
1545
1546 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001547 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001548
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001549 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1550 data->hw, data->cstate, data->subcstate);
1551 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001552}
1553
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001554static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001555{
1556 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1557 int len;
1558
1559 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001560 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001561
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001562 len = fprintf(fp, " IP: %u ", data->ip);
1563 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001564}
1565
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001566static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001567{
1568 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1569 int len;
1570
1571 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001572 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001573
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001574 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001575 data->deepest_cstate, data->last_cstate,
1576 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001577 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001578}
1579
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001580static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001581{
1582 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1583 unsigned int percent, freq;
1584 int len;
1585
1586 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001587 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001588
1589 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001590 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001591 if (data->max_nonturbo) {
1592 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001593 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001594 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001595 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001596}
1597
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001598static int perf_sample__fprintf_synth(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001599 struct evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001600{
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001601 switch (evsel->core.attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001602 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001603 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001604 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001605 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001606 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001607 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001608 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001609 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001610 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001611 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001612 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001613 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001614 default:
1615 break;
1616 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001617
1618 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001619}
1620
Jiri Olsa809e9422015-11-26 18:55:21 +01001621struct perf_script {
1622 struct perf_tool tool;
1623 struct perf_session *session;
1624 bool show_task_events;
1625 bool show_mmap_events;
1626 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301627 bool show_namespace_events;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01001628 bool show_lost_events;
Jiri Olsa3233b372018-02-06 19:17:59 +01001629 bool show_round_events;
Jiri Olsa490c8cc2019-05-08 15:20:08 +02001630 bool show_bpf_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001631 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001632 bool per_event_dump;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001633 struct evswitch evswitch;
Jiri Olsaf8548392019-07-21 13:23:49 +02001634 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +02001635 struct perf_thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001636 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001637 const char *time_str;
Jin Yaocc2ef582018-01-10 23:00:33 +08001638 struct perf_time_interval *ptime_range;
1639 int range_size;
Jin Yao2ab046c2017-12-08 21:13:46 +08001640 int range_num;
Jiri Olsa809e9422015-11-26 18:55:21 +01001641};
1642
Jiri Olsa63503db2019-07-21 13:23:52 +02001643static int perf_evlist__max_name_len(struct evlist *evlist)
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001644{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001645 struct evsel *evsel;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001646 int max = 0;
1647
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001648 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001649 int len = strlen(perf_evsel__name(evsel));
1650
1651 max = MAX(len, max);
1652 }
1653
1654 return max;
1655}
1656
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001657static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001658{
1659 struct mem_info mi = { .data_src.val = data_src };
1660 char decode[100];
1661 char out[100];
1662 static int maxlen;
1663 int len;
1664
1665 perf_script__meminfo_scnprintf(decode, 100, &mi);
1666
1667 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1668 if (maxlen < len)
1669 maxlen = len;
1670
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001671 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001672}
1673
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001674struct metric_ctx {
1675 struct perf_sample *sample;
1676 struct thread *thread;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001677 struct evsel *evsel;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001678 FILE *fp;
1679};
1680
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001681static void script_print_metric(struct perf_stat_config *config __maybe_unused,
1682 void *ctx, const char *color,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001683 const char *fmt,
1684 const char *unit, double val)
1685{
1686 struct metric_ctx *mctx = ctx;
1687
1688 if (!fmt)
1689 return;
1690 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001691 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001692 fputs("\tmetric: ", mctx->fp);
1693 if (color)
1694 color_fprintf(mctx->fp, color, fmt, val);
1695 else
1696 printf(fmt, val);
1697 fprintf(mctx->fp, " %s\n", unit);
1698}
1699
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001700static void script_new_line(struct perf_stat_config *config __maybe_unused,
1701 void *ctx)
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001702{
1703 struct metric_ctx *mctx = ctx;
1704
1705 perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01001706 PERF_RECORD_SAMPLE, mctx->fp);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001707 fputs("\tmetric: ", mctx->fp);
1708}
1709
1710static void perf_sample__fprint_metric(struct perf_script *script,
1711 struct thread *thread,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001712 struct evsel *evsel,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001713 struct perf_sample *sample,
1714 FILE *fp)
1715{
1716 struct perf_stat_output_ctx ctx = {
1717 .print_metric = script_print_metric,
1718 .new_line = script_new_line,
1719 .ctx = &(struct metric_ctx) {
1720 .sample = sample,
1721 .thread = thread,
1722 .evsel = evsel,
1723 .fp = fp,
1724 },
1725 .force_header = false,
1726 };
Jiri Olsa32dcd022019-07-21 13:23:51 +02001727 struct evsel *ev2;
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001728 u64 val;
1729
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001730 if (!evsel->stats)
1731 perf_evlist__alloc_stats(script->session->evlist, false);
1732 if (evsel_script(evsel->leader)->gnum++ == 0)
1733 perf_stat__reset_shadow_stats();
1734 val = sample->period * evsel->scale;
1735 perf_stat__update_shadow_stats(evsel,
1736 val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001737 sample->cpu,
1738 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001739 evsel_script(evsel)->val = val;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001740 if (evsel_script(evsel->leader)->gnum == evsel->leader->core.nr_members) {
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001741 for_each_group_member (ev2, evsel->leader) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001742 perf_stat__print_shadow_stats(&stat_config, ev2,
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001743 evsel_script(ev2)->val,
1744 sample->cpu,
1745 &ctx,
Jin Yaoe0128b32017-12-05 22:03:05 +08001746 NULL,
1747 &rt_stat);
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001748 }
1749 evsel_script(evsel->leader)->gnum = 0;
1750 }
1751}
1752
Andi Kleen99f753f2018-09-20 11:05:39 -07001753static bool show_event(struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001754 struct evsel *evsel,
Andi Kleen99f753f2018-09-20 11:05:39 -07001755 struct thread *thread,
1756 struct addr_location *al)
1757{
Adrian Hunter256d92b2018-12-21 14:06:19 +02001758 int depth = thread_stack__depth(thread, sample->cpu);
Andi Kleen99f753f2018-09-20 11:05:39 -07001759
1760 if (!symbol_conf.graph_function)
1761 return true;
1762
1763 if (thread->filter) {
1764 if (depth <= thread->filter_entry_depth) {
1765 thread->filter = false;
1766 return false;
1767 }
1768 return true;
1769 } else {
1770 const char *s = symbol_conf.graph_function;
1771 u64 ip;
1772 const char *name = resolve_branch_sym(sample, evsel, thread, al,
1773 &ip);
1774 unsigned nlen;
1775
1776 if (!name)
1777 return false;
1778 nlen = strlen(name);
1779 while (*s) {
1780 unsigned len = strcspn(s, ",");
1781 if (nlen == len && !strncmp(name, s, len)) {
1782 thread->filter = true;
1783 thread->filter_entry_depth = depth;
1784 return true;
1785 }
1786 s += len;
1787 if (*s == ',')
1788 s++;
1789 }
1790 return false;
1791 }
1792}
1793
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001794static void process_event(struct perf_script *script,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001795 struct perf_sample *sample, struct evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001796 struct addr_location *al,
1797 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001798{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001799 struct thread *thread = al->thread;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001800 struct perf_event_attr *attr = &evsel->core.attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001801 unsigned int type = output_type(attr->type);
Jiri Olsa32dcd022019-07-21 13:23:51 +02001802 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001803 FILE *fp = es->fp;
David Ahern1424dc92011-03-09 22:23:28 -07001804
Adrian Hunter14057202017-06-21 13:17:19 +03001805 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001806 return;
1807
Andi Kleen99f753f2018-09-20 11:05:39 -07001808 if (!show_event(sample, evsel, thread, al))
1809 return;
1810
Arnaldo Carvalho de Melo8829e562019-08-15 11:00:11 -03001811 if (evswitch__discard(&script->evswitch, evsel))
1812 return;
Arnaldo Carvalho de Melof90a2412019-08-14 16:20:13 -03001813
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03001814 ++es->samples;
1815
Jiri Olsa28a0b392018-01-07 17:03:52 +01001816 perf_sample__fprintf_start(sample, thread, evsel,
1817 PERF_RECORD_SAMPLE, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001818
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001819 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001820 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001821
Namhyung Kime944d3d2013-11-18 14:34:52 +09001822 if (PRINT_FIELD(EVNAME)) {
1823 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001824
1825 if (!script->name_width)
1826 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1827
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001828 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001829 }
1830
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001831 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001832 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001833
Akihiro Nagai95582592012-01-30 13:43:09 +09001834 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001835 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001836 return;
1837 }
1838
Andi Kleen96167162019-01-17 11:48:34 -08001839 if (PRINT_FIELD(TRACE) && sample->raw_data) {
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001840 event_format__fprintf(evsel->tp_format, sample->cpu,
1841 sample->raw_data, sample->raw_size, fp);
1842 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001843
1844 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001845 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001846
David Ahern7cec0922011-05-30 13:08:23 -06001847 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001848 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001849
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001850 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001851 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001852
1853 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001854 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001855
David Ahern787bef12011-05-27 14:28:43 -06001856 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001857 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001858
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001859 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001860 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001861 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001862 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001863
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001864 fputc(cursor ? '\n' : ' ', fp);
1865 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001866 }
1867
Stephane Eranianfc36f942015-08-31 18:41:10 +02001868 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001869 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001870
Andi Kleenb1491ac2017-09-05 11:40:57 -07001871 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001872 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001873
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001874 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001875 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001876 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001877 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001878 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001879 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001880
Wang Nan30372f02016-02-24 11:20:45 +00001881 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001882 perf_sample__fprintf_bpf_output(sample, fp);
1883 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001884
1885 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001886 fprintf(fp, "%16" PRIx64, sample->phys_addr);
Adrian Hunter68fb45b2019-05-20 14:37:14 +03001887
1888 perf_sample__fprintf_ipc(sample, attr, fp);
1889
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001890 fprintf(fp, "\n");
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001891
Andi Kleendd2e18e2018-12-03 16:18:48 -08001892 if (PRINT_FIELD(SRCCODE)) {
1893 if (map__fprintf_srccode(al->map, al->addr, stdout,
1894 &thread->srccode_state))
1895 printf("\n");
1896 }
1897
Andi Kleen4bd1bef2017-11-17 13:43:00 -08001898 if (PRINT_FIELD(METRIC))
1899 perf_sample__fprint_metric(script, thread, evsel, sample, fp);
Milian Wolff7ee40672018-10-21 21:14:24 +02001900
1901 if (verbose)
1902 fflush(fp);
David Ahernbe6d8422011-03-09 22:23:23 -07001903}
1904
Tom Zanussi956ffd02009-11-25 01:15:46 -06001905static struct scripting_ops *scripting_ops;
1906
Jiri Olsa32dcd022019-07-21 13:23:51 +02001907static void __process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsa36e33c52016-01-06 11:49:56 +01001908{
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001909 int nthreads = perf_thread_map__nr(counter->core.threads);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001910 int ncpus = perf_evsel__nr_cpus(counter);
1911 int cpu, thread;
1912 static int header_printed;
1913
1914 if (counter->system_wide)
1915 nthreads = 1;
1916
1917 if (!header_printed) {
1918 printf("%3s %8s %15s %15s %15s %15s %s\n",
1919 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1920 header_printed = 1;
1921 }
1922
1923 for (thread = 0; thread < nthreads; thread++) {
1924 for (cpu = 0; cpu < ncpus; cpu++) {
1925 struct perf_counts_values *counts;
1926
1927 counts = perf_counts(counter->counts, cpu, thread);
1928
1929 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
Jiri Olsad400bd32019-07-21 13:24:37 +02001930 counter->core.cpus->map[cpu],
Jiri Olsaa2f354e2019-08-22 13:11:41 +02001931 perf_thread_map__pid(counter->core.threads, thread),
Jiri Olsa36e33c52016-01-06 11:49:56 +01001932 counts->val,
1933 counts->ena,
1934 counts->run,
1935 tstamp,
1936 perf_evsel__name(counter));
1937 }
1938 }
1939}
1940
Jiri Olsa32dcd022019-07-21 13:23:51 +02001941static void process_stat(struct evsel *counter, u64 tstamp)
Jiri Olsae099eba2016-01-05 22:09:09 +01001942{
1943 if (scripting_ops && scripting_ops->process_stat)
1944 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001945 else
1946 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001947}
1948
1949static void process_stat_interval(u64 tstamp)
1950{
1951 if (scripting_ops && scripting_ops->process_stat_interval)
1952 scripting_ops->process_stat_interval(tstamp);
1953}
1954
Tom Zanussi956ffd02009-11-25 01:15:46 -06001955static void setup_scripting(void)
1956{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001957 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001958 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001959}
1960
Adrian Hunterd445dd22014-08-15 22:08:37 +03001961static int flush_scripting(void)
1962{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001963 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001964}
1965
Tom Zanussi956ffd02009-11-25 01:15:46 -06001966static int cleanup_scripting(void)
1967{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001968 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001969
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001970 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001971}
1972
Andi Kleene87e5482019-03-11 07:44:52 -07001973static bool filter_cpu(struct perf_sample *sample)
1974{
1975 if (cpu_list)
1976 return !test_bit(sample->cpu, cpu_bitmap);
1977 return false;
1978}
1979
Jiri Olsa809e9422015-11-26 18:55:21 +01001980static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001981 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001982 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001983 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001984 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001985{
Jiri Olsa809e9422015-11-26 18:55:21 +01001986 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001987 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001988
Jin Yao2ab046c2017-12-08 21:13:46 +08001989 if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
1990 sample->time)) {
David Aherna91f4c42016-11-29 10:15:43 -07001991 return 0;
Jin Yao2ab046c2017-12-08 21:13:46 +08001992 }
David Aherna91f4c42016-11-29 10:15:43 -07001993
David Ahern1424dc92011-03-09 22:23:28 -07001994 if (debug_mode) {
1995 if (sample->time < last_timestamp) {
1996 pr_err("Samples misordered, previous: %" PRIu64
1997 " this: %" PRIu64 "\n", last_timestamp,
1998 sample->time);
1999 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02002000 }
David Ahern1424dc92011-03-09 22:23:28 -07002001 last_timestamp = sample->time;
2002 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002003 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10002004
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03002005 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07002006 pr_err("problem processing %d event, skipping it.\n",
2007 event->header.type);
2008 return -1;
2009 }
2010
2011 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002012 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07002013
Andi Kleene87e5482019-03-11 07:44:52 -07002014 if (filter_cpu(sample))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002015 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002016
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002017 if (scripting_ops)
2018 scripting_ops->process_event(event, sample, evsel, &al);
2019 else
Andi Kleen48d02a12017-02-23 15:46:34 -08002020 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01002021
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002022out_put:
2023 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002024 return 0;
2025}
2026
Adrian Hunter7ea95722013-11-01 15:51:30 +02002027static int process_attr(struct perf_tool *tool, union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02002028 struct evlist **pevlist)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002029{
2030 struct perf_script *scr = container_of(tool, struct perf_script, tool);
Jiri Olsa63503db2019-07-21 13:23:52 +02002031 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002032 struct evsel *evsel, *pos;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002033 int err;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002034 static struct evsel_script *es;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002035
2036 err = perf_event__process_attr(tool, event, pevlist);
2037 if (err)
2038 return err;
2039
2040 evlist = *pevlist;
2041 evsel = perf_evlist__last(*pevlist);
2042
Ravi Bangoriaa3af66f2018-06-25 18:12:19 +05302043 if (!evsel->priv) {
2044 if (scr->per_event_dump) {
2045 evsel->priv = perf_evsel_script__new(evsel,
2046 scr->session->data);
2047 } else {
2048 es = zalloc(sizeof(*es));
2049 if (!es)
2050 return -ENOMEM;
2051 es->fp = stdout;
2052 evsel->priv = es;
2053 }
2054 }
2055
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002056 if (evsel->core.attr.type >= PERF_TYPE_MAX &&
2057 evsel->core.attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002058 return 0;
2059
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002060 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002061 if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
Adrian Hunter7ea95722013-11-01 15:51:30 +02002062 return 0;
2063 }
2064
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002065 set_print_ip_opts(&evsel->core.attr);
Adrian Hunter7ea95722013-11-01 15:51:30 +02002066
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002067 if (evsel->core.attr.sample_type)
Jiri Olsad2b5a312015-10-16 12:41:25 +02002068 err = perf_evsel__check_attr(evsel, scr->session);
2069
2070 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02002071}
2072
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002073static int process_comm_event(struct perf_tool *tool,
2074 union perf_event *event,
2075 struct perf_sample *sample,
2076 struct machine *machine)
2077{
2078 struct thread *thread;
2079 struct perf_script *script = container_of(tool, struct perf_script, tool);
2080 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002081 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002082 int ret = -1;
2083
2084 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
2085 if (thread == NULL) {
2086 pr_debug("problem processing COMM event, skipping it.\n");
2087 return -1;
2088 }
2089
2090 if (perf_event__process_comm(tool, event, sample, machine) < 0)
2091 goto out;
2092
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002093 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002094 sample->cpu = 0;
2095 sample->time = 0;
2096 sample->tid = event->comm.tid;
2097 sample->pid = event->comm.pid;
2098 }
Andi Kleene87e5482019-03-11 07:44:52 -07002099 if (!filter_cpu(sample)) {
2100 perf_sample__fprintf_start(sample, thread, evsel,
Jiri Olsa28a0b392018-01-07 17:03:52 +01002101 PERF_RECORD_COMM, stdout);
Andi Kleene87e5482019-03-11 07:44:52 -07002102 perf_event__fprintf(event, stdout);
2103 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002104 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002105out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002106 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002107 return ret;
2108}
2109
Hari Bathini96a44bb2017-03-08 02:12:06 +05302110static int process_namespaces_event(struct perf_tool *tool,
2111 union perf_event *event,
2112 struct perf_sample *sample,
2113 struct machine *machine)
2114{
2115 struct thread *thread;
2116 struct perf_script *script = container_of(tool, struct perf_script, tool);
2117 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002118 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Hari Bathini96a44bb2017-03-08 02:12:06 +05302119 int ret = -1;
2120
2121 thread = machine__findnew_thread(machine, event->namespaces.pid,
2122 event->namespaces.tid);
2123 if (thread == NULL) {
2124 pr_debug("problem processing NAMESPACES event, skipping it.\n");
2125 return -1;
2126 }
2127
2128 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
2129 goto out;
2130
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002131 if (!evsel->core.attr.sample_id_all) {
Hari Bathini96a44bb2017-03-08 02:12:06 +05302132 sample->cpu = 0;
2133 sample->time = 0;
2134 sample->tid = event->namespaces.tid;
2135 sample->pid = event->namespaces.pid;
2136 }
Andi Kleene87e5482019-03-11 07:44:52 -07002137 if (!filter_cpu(sample)) {
2138 perf_sample__fprintf_start(sample, thread, evsel,
2139 PERF_RECORD_NAMESPACES, stdout);
2140 perf_event__fprintf(event, stdout);
2141 }
Hari Bathini96a44bb2017-03-08 02:12:06 +05302142 ret = 0;
2143out:
2144 thread__put(thread);
2145 return ret;
2146}
2147
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002148static int process_fork_event(struct perf_tool *tool,
2149 union perf_event *event,
2150 struct perf_sample *sample,
2151 struct machine *machine)
2152{
2153 struct thread *thread;
2154 struct perf_script *script = container_of(tool, struct perf_script, tool);
2155 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002156 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002157
2158 if (perf_event__process_fork(tool, event, sample, machine) < 0)
2159 return -1;
2160
2161 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2162 if (thread == NULL) {
2163 pr_debug("problem processing FORK event, skipping it.\n");
2164 return -1;
2165 }
2166
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002167 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002168 sample->cpu = 0;
2169 sample->time = event->fork.time;
2170 sample->tid = event->fork.tid;
2171 sample->pid = event->fork.pid;
2172 }
Andi Kleene87e5482019-03-11 07:44:52 -07002173 if (!filter_cpu(sample)) {
2174 perf_sample__fprintf_start(sample, thread, evsel,
2175 PERF_RECORD_FORK, stdout);
2176 perf_event__fprintf(event, stdout);
2177 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002178 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002179
2180 return 0;
2181}
2182static int process_exit_event(struct perf_tool *tool,
2183 union perf_event *event,
2184 struct perf_sample *sample,
2185 struct machine *machine)
2186{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002187 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002188 struct thread *thread;
2189 struct perf_script *script = container_of(tool, struct perf_script, tool);
2190 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002191 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002192
2193 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
2194 if (thread == NULL) {
2195 pr_debug("problem processing EXIT event, skipping it.\n");
2196 return -1;
2197 }
2198
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002199 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002200 sample->cpu = 0;
2201 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002202 sample->tid = event->fork.tid;
2203 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002204 }
Andi Kleene87e5482019-03-11 07:44:52 -07002205 if (!filter_cpu(sample)) {
2206 perf_sample__fprintf_start(sample, thread, evsel,
2207 PERF_RECORD_EXIT, stdout);
2208 perf_event__fprintf(event, stdout);
2209 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002210
2211 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002212 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002213
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002214 thread__put(thread);
2215 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002216}
2217
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002218static int process_mmap_event(struct perf_tool *tool,
2219 union perf_event *event,
2220 struct perf_sample *sample,
2221 struct machine *machine)
2222{
2223 struct thread *thread;
2224 struct perf_script *script = container_of(tool, struct perf_script, tool);
2225 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002226 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002227
2228 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
2229 return -1;
2230
2231 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
2232 if (thread == NULL) {
2233 pr_debug("problem processing MMAP event, skipping it.\n");
2234 return -1;
2235 }
2236
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002237 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002238 sample->cpu = 0;
2239 sample->time = 0;
2240 sample->tid = event->mmap.tid;
2241 sample->pid = event->mmap.pid;
2242 }
Andi Kleene87e5482019-03-11 07:44:52 -07002243 if (!filter_cpu(sample)) {
2244 perf_sample__fprintf_start(sample, thread, evsel,
2245 PERF_RECORD_MMAP, stdout);
2246 perf_event__fprintf(event, stdout);
2247 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002248 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002249 return 0;
2250}
2251
2252static int process_mmap2_event(struct perf_tool *tool,
2253 union perf_event *event,
2254 struct perf_sample *sample,
2255 struct machine *machine)
2256{
2257 struct thread *thread;
2258 struct perf_script *script = container_of(tool, struct perf_script, tool);
2259 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002260 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002261
2262 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
2263 return -1;
2264
2265 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
2266 if (thread == NULL) {
2267 pr_debug("problem processing MMAP2 event, skipping it.\n");
2268 return -1;
2269 }
2270
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002271 if (!evsel->core.attr.sample_id_all) {
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002272 sample->cpu = 0;
2273 sample->time = 0;
2274 sample->tid = event->mmap2.tid;
2275 sample->pid = event->mmap2.pid;
2276 }
Andi Kleene87e5482019-03-11 07:44:52 -07002277 if (!filter_cpu(sample)) {
2278 perf_sample__fprintf_start(sample, thread, evsel,
2279 PERF_RECORD_MMAP2, stdout);
2280 perf_event__fprintf(event, stdout);
2281 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002282 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002283 return 0;
2284}
2285
Adrian Hunter7c148982015-07-21 12:44:06 +03002286static int process_switch_event(struct perf_tool *tool,
2287 union perf_event *event,
2288 struct perf_sample *sample,
2289 struct machine *machine)
2290{
2291 struct thread *thread;
2292 struct perf_script *script = container_of(tool, struct perf_script, tool);
2293 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002294 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Adrian Hunter7c148982015-07-21 12:44:06 +03002295
2296 if (perf_event__process_switch(tool, event, sample, machine) < 0)
2297 return -1;
2298
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002299 if (scripting_ops && scripting_ops->process_switch)
2300 scripting_ops->process_switch(event, sample, machine);
2301
2302 if (!script->show_switch_events)
2303 return 0;
2304
Adrian Hunter7c148982015-07-21 12:44:06 +03002305 thread = machine__findnew_thread(machine, sample->pid,
2306 sample->tid);
2307 if (thread == NULL) {
2308 pr_debug("problem processing SWITCH event, skipping it.\n");
2309 return -1;
2310 }
2311
Andi Kleene87e5482019-03-11 07:44:52 -07002312 if (!filter_cpu(sample)) {
2313 perf_sample__fprintf_start(sample, thread, evsel,
2314 PERF_RECORD_SWITCH, stdout);
2315 perf_event__fprintf(event, stdout);
2316 }
Adrian Hunter7c148982015-07-21 12:44:06 +03002317 thread__put(thread);
2318 return 0;
2319}
2320
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002321static int
2322process_lost_event(struct perf_tool *tool,
2323 union perf_event *event,
2324 struct perf_sample *sample,
2325 struct machine *machine)
2326{
2327 struct perf_script *script = container_of(tool, struct perf_script, tool);
2328 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002329 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002330 struct thread *thread;
2331
2332 thread = machine__findnew_thread(machine, sample->pid,
2333 sample->tid);
2334 if (thread == NULL)
2335 return -1;
2336
Andi Kleene87e5482019-03-11 07:44:52 -07002337 if (!filter_cpu(sample)) {
2338 perf_sample__fprintf_start(sample, thread, evsel,
2339 PERF_RECORD_LOST, stdout);
2340 perf_event__fprintf(event, stdout);
2341 }
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002342 thread__put(thread);
2343 return 0;
2344}
2345
Jiri Olsa3233b372018-02-06 19:17:59 +01002346static int
2347process_finished_round_event(struct perf_tool *tool __maybe_unused,
2348 union perf_event *event,
2349 struct ordered_events *oe __maybe_unused)
2350
2351{
2352 perf_event__fprintf(event, stdout);
2353 return 0;
2354}
2355
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002356static int
2357process_bpf_events(struct perf_tool *tool __maybe_unused,
2358 union perf_event *event,
2359 struct perf_sample *sample,
2360 struct machine *machine)
2361{
2362 struct thread *thread;
2363 struct perf_script *script = container_of(tool, struct perf_script, tool);
2364 struct perf_session *session = script->session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002365 struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002366
2367 if (machine__process_ksymbol(machine, event, sample) < 0)
2368 return -1;
2369
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002370 if (!evsel->core.attr.sample_id_all) {
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002371 perf_event__fprintf(event, stdout);
2372 return 0;
2373 }
2374
2375 thread = machine__findnew_thread(machine, sample->pid, sample->tid);
2376 if (thread == NULL) {
2377 pr_debug("problem processing MMAP event, skipping it.\n");
2378 return -1;
2379 }
2380
2381 if (!filter_cpu(sample)) {
2382 perf_sample__fprintf_start(sample, thread, evsel,
2383 event->header.type, stdout);
2384 perf_event__fprintf(event, stdout);
2385 }
2386
2387 thread__put(thread);
2388 return 0;
2389}
2390
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002391static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05002392{
2393 session_done = 1;
2394}
2395
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002396static void perf_script__fclose_per_event_dump(struct perf_script *script)
2397{
Jiri Olsa63503db2019-07-21 13:23:52 +02002398 struct evlist *evlist = script->session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002399 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002400
2401 evlist__for_each_entry(evlist, evsel) {
2402 if (!evsel->priv)
2403 break;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002404 perf_evsel_script__delete(evsel->priv);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002405 evsel->priv = NULL;
2406 }
2407}
2408
2409static int perf_script__fopen_per_event_dump(struct perf_script *script)
2410{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002411 struct evsel *evsel;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002412
2413 evlist__for_each_entry(script->session->evlist, evsel) {
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03002414 /*
2415 * Already setup? I.e. we may be called twice in cases like
2416 * Intel PT, one for the intel_pt// and dummy events, then
2417 * for the evsels syntheized from the auxtrace info.
2418 *
2419 * Ses perf_script__process_auxtrace_info.
2420 */
2421 if (evsel->priv != NULL)
2422 continue;
2423
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002424 evsel->priv = perf_evsel_script__new(evsel, script->session->data);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002425 if (evsel->priv == NULL)
2426 goto out_err_fclose;
2427 }
2428
2429 return 0;
2430
2431out_err_fclose:
2432 perf_script__fclose_per_event_dump(script);
2433 return -1;
2434}
2435
2436static int perf_script__setup_per_event_dump(struct perf_script *script)
2437{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002438 struct evsel *evsel;
2439 static struct evsel_script es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002440
2441 if (script->per_event_dump)
2442 return perf_script__fopen_per_event_dump(script);
2443
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002444 es_stdout.fp = stdout;
2445
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002446 evlist__for_each_entry(script->session->evlist, evsel)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002447 evsel->priv = &es_stdout;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002448
2449 return 0;
2450}
2451
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002452static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
2453{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002454 struct evsel *evsel;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002455
2456 evlist__for_each_entry(script->session->evlist, evsel) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002457 struct evsel_script *es = evsel->priv;
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002458
2459 perf_evsel_script__fprintf(es, stdout);
2460 perf_evsel_script__delete(es);
2461 evsel->priv = NULL;
2462 }
2463}
2464
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002465static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002466{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002467 int ret;
2468
Tom Zanussic239da32010-04-01 23:59:18 -05002469 signal(SIGINT, sig_handler);
2470
Tony Jones8bf8c6d2019-01-20 11:14:14 -08002471 perf_stat__init_shadow_stats();
2472
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002473 /* override event processing functions */
2474 if (script->show_task_events) {
2475 script->tool.comm = process_comm_event;
2476 script->tool.fork = process_fork_event;
2477 script->tool.exit = process_exit_event;
2478 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002479 if (script->show_mmap_events) {
2480 script->tool.mmap = process_mmap_event;
2481 script->tool.mmap2 = process_mmap2_event;
2482 }
Adrian Hunter5bf83c22019-07-10 11:58:06 +03002483 if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
Adrian Hunter7c148982015-07-21 12:44:06 +03002484 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05302485 if (script->show_namespace_events)
2486 script->tool.namespaces = process_namespaces_event;
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01002487 if (script->show_lost_events)
2488 script->tool.lost = process_lost_event;
Jiri Olsa3233b372018-02-06 19:17:59 +01002489 if (script->show_round_events) {
2490 script->tool.ordered_events = false;
2491 script->tool.finished_round = process_finished_round_event;
2492 }
Jiri Olsa490c8cc2019-05-08 15:20:08 +02002493 if (script->show_bpf_events) {
2494 script->tool.ksymbol = process_bpf_events;
2495 script->tool.bpf_event = process_bpf_events;
2496 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002497
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002498 if (perf_script__setup_per_event_dump(script)) {
2499 pr_err("Couldn't create the per event dump files\n");
2500 return -1;
2501 }
2502
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002503 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002504
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002505 if (script->per_event_dump)
Arnaldo Carvalho de Melo642ee1c2017-10-30 13:11:15 -03002506 perf_script__exit_per_event_dump_stats(script);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002507
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02002508 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002509 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02002510
2511 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002512}
2513
Tom Zanussi956ffd02009-11-25 01:15:46 -06002514struct script_spec {
2515 struct list_head node;
2516 struct scripting_ops *ops;
2517 char spec[0];
2518};
2519
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002520static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002521
2522static struct script_spec *script_spec__new(const char *spec,
2523 struct scripting_ops *ops)
2524{
2525 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
2526
2527 if (s != NULL) {
2528 strcpy(s->spec, spec);
2529 s->ops = ops;
2530 }
2531
2532 return s;
2533}
2534
Tom Zanussi956ffd02009-11-25 01:15:46 -06002535static void script_spec__add(struct script_spec *s)
2536{
2537 list_add_tail(&s->node, &script_specs);
2538}
2539
2540static struct script_spec *script_spec__find(const char *spec)
2541{
2542 struct script_spec *s;
2543
2544 list_for_each_entry(s, &script_specs, node)
2545 if (strcasecmp(s->spec, spec) == 0)
2546 return s;
2547 return NULL;
2548}
2549
Tom Zanussi956ffd02009-11-25 01:15:46 -06002550int script_spec_register(const char *spec, struct scripting_ops *ops)
2551{
2552 struct script_spec *s;
2553
2554 s = script_spec__find(spec);
2555 if (s)
2556 return -1;
2557
Taeung Song8560bae2016-02-26 00:13:10 +09002558 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002559 if (!s)
2560 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002561 else
2562 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002563
2564 return 0;
2565}
2566
2567static struct scripting_ops *script_spec__lookup(const char *spec)
2568{
2569 struct script_spec *s = script_spec__find(spec);
2570 if (!s)
2571 return NULL;
2572
2573 return s->ops;
2574}
2575
2576static void list_available_languages(void)
2577{
2578 struct script_spec *s;
2579
2580 fprintf(stderr, "\n");
2581 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002582 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002583
2584 list_for_each_entry(s, &script_specs, node)
2585 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2586
2587 fprintf(stderr, "\n");
2588}
2589
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002590static int parse_scriptname(const struct option *opt __maybe_unused,
2591 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002592{
2593 char spec[PATH_MAX];
2594 const char *script, *ext;
2595 int len;
2596
Tom Zanussif526d682010-01-27 02:27:52 -06002597 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002598 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002599 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002600 }
2601
2602 script = strchr(str, ':');
2603 if (script) {
2604 len = script - str;
2605 if (len >= PATH_MAX) {
2606 fprintf(stderr, "invalid language specifier");
2607 return -1;
2608 }
2609 strncpy(spec, str, len);
2610 spec[len] = '\0';
2611 scripting_ops = script_spec__lookup(spec);
2612 if (!scripting_ops) {
2613 fprintf(stderr, "invalid language specifier");
2614 return -1;
2615 }
2616 script++;
2617 } else {
2618 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002619 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002620 if (!ext) {
2621 fprintf(stderr, "invalid script extension");
2622 return -1;
2623 }
2624 scripting_ops = script_spec__lookup(++ext);
2625 if (!scripting_ops) {
2626 fprintf(stderr, "invalid script extension");
2627 return -1;
2628 }
2629 }
2630
2631 script_name = strdup(script);
2632
2633 return 0;
2634}
2635
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002636static int parse_output_fields(const struct option *opt __maybe_unused,
2637 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002638{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002639 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002640 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002641 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002642 int rc = 0;
2643 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002644 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002645 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002646
2647 if (!str)
2648 return -ENOMEM;
2649
David Ahern2c9e45f72011-03-17 10:03:21 -06002650 /* first word can state for which event type the user is specifying
2651 * the fields. If no type exists, the specified fields apply to all
2652 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002653 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002654 tok = strchr(str, ':');
2655 if (tok) {
2656 *tok = '\0';
2657 tok++;
2658 if (!strcmp(str, "hw"))
2659 type = PERF_TYPE_HARDWARE;
2660 else if (!strcmp(str, "sw"))
2661 type = PERF_TYPE_SOFTWARE;
2662 else if (!strcmp(str, "trace"))
2663 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002664 else if (!strcmp(str, "raw"))
2665 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002666 else if (!strcmp(str, "break"))
2667 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002668 else if (!strcmp(str, "synth"))
2669 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002670 else {
2671 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002672 rc = -EINVAL;
2673 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002674 }
2675
2676 if (output[type].user_set)
2677 pr_warning("Overriding previous field request for %s events.\n",
2678 event_type(type));
2679
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002680 /* Don't override defaults for +- */
2681 if (strchr(tok, '+') || strchr(tok, '-'))
2682 goto parse;
2683
David Ahern2c9e45f72011-03-17 10:03:21 -06002684 output[type].fields = 0;
2685 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002686 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002687
2688 } else {
2689 tok = str;
2690 if (strlen(str) == 0) {
2691 fprintf(stderr,
2692 "Cannot set fields to 'none' for all event types.\n");
2693 rc = -EINVAL;
2694 goto out;
2695 }
2696
Andi Kleen36ce5652017-06-02 08:48:10 -07002697 /* Don't override defaults for +- */
2698 if (strchr(str, '+') || strchr(str, '-'))
2699 goto parse;
2700
David Ahern2c9e45f72011-03-17 10:03:21 -06002701 if (output_set_by_user())
2702 pr_warning("Overriding previous field request for all events.\n");
2703
Adrian Hunter14057202017-06-21 13:17:19 +03002704 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002705 output[j].fields = 0;
2706 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002707 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002708 }
David Ahern1424dc92011-03-09 22:23:28 -07002709 }
2710
Andi Kleen36ce5652017-06-02 08:48:10 -07002711parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002712 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002713 if (*tok == '+') {
2714 if (change == SET)
2715 goto out_badmix;
2716 change = ADD;
2717 tok++;
2718 } else if (*tok == '-') {
2719 if (change == SET)
2720 goto out_badmix;
2721 change = REMOVE;
2722 tok++;
2723 } else {
2724 if (change != SET && change != DEFAULT)
2725 goto out_badmix;
2726 change = SET;
2727 }
2728
David Ahern745f43e2011-03-09 22:23:26 -07002729 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002730 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002731 break;
David Ahern745f43e2011-03-09 22:23:26 -07002732 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002733 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002734 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002735 continue;
2736 }
David Ahern745f43e2011-03-09 22:23:26 -07002737 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002738 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002739 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002740 goto out;
2741 }
2742
2743 if (type == -1) {
2744 /* add user option to all events types for
2745 * which it is valid
2746 */
Adrian Hunter14057202017-06-21 13:17:19 +03002747 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002748 if (output[j].invalid_fields & all_output_options[i].field) {
2749 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2750 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002751 } else {
Andi Kleen4b6ac812019-02-24 07:37:12 -08002752 if (change == REMOVE) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002753 output[j].fields &= ~all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002754 output[j].user_set_fields &= ~all_output_options[i].field;
2755 } else {
Andi Kleen36ce5652017-06-02 08:48:10 -07002756 output[j].fields |= all_output_options[i].field;
Andi Kleen4b6ac812019-02-24 07:37:12 -08002757 output[j].user_set_fields |= all_output_options[i].field;
2758 }
Andi Kleen37fed3d2018-09-18 05:32:09 -07002759 output[j].user_set = true;
2760 output[j].wildcard_set = true;
Andi Kleen36ce5652017-06-02 08:48:10 -07002761 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002762 }
2763 } else {
2764 if (output[type].invalid_fields & all_output_options[i].field) {
2765 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2766 all_output_options[i].str, event_type(type));
2767
2768 rc = -EINVAL;
2769 goto out;
2770 }
Jiri Olsa6ef362f2019-02-20 13:27:57 +01002771 if (change == REMOVE)
2772 output[type].fields &= ~all_output_options[i].field;
2773 else
2774 output[type].fields |= all_output_options[i].field;
Andi Kleen37fed3d2018-09-18 05:32:09 -07002775 output[type].user_set = true;
2776 output[type].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002777 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002778 }
2779
2780 if (type >= 0) {
2781 if (output[type].fields == 0) {
2782 pr_debug("No fields requested for %s type. "
2783 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002784 }
David Ahern1424dc92011-03-09 22:23:28 -07002785 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002786 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002787
Andi Kleen36ce5652017-06-02 08:48:10 -07002788out_badmix:
2789 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2790 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002791out:
David Ahern745f43e2011-03-09 22:23:26 -07002792 free(str);
2793 return rc;
2794}
2795
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002796#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2797 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2798 if ((lang_dirent->d_type == DT_DIR || \
2799 (lang_dirent->d_type == DT_UNKNOWN && \
2800 is_directory(scripts_path, lang_dirent))) && \
2801 (strcmp(lang_dirent->d_name, ".")) && \
2802 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002803
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002804#define for_each_script(lang_path, lang_dir, script_dirent) \
2805 while ((script_dirent = readdir(lang_dir)) != NULL) \
2806 if (script_dirent->d_type != DT_DIR && \
2807 (script_dirent->d_type != DT_UNKNOWN || \
2808 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002809
2810
2811#define RECORD_SUFFIX "-record"
2812#define REPORT_SUFFIX "-report"
2813
2814struct script_desc {
2815 struct list_head node;
2816 char *name;
2817 char *half_liner;
2818 char *args;
2819};
2820
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002821static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002822
2823static struct script_desc *script_desc__new(const char *name)
2824{
2825 struct script_desc *s = zalloc(sizeof(*s));
2826
Tom Zanussib5b87312010-11-10 08:16:51 -06002827 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002828 s->name = strdup(name);
2829
2830 return s;
2831}
2832
2833static void script_desc__delete(struct script_desc *s)
2834{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002835 zfree(&s->name);
2836 zfree(&s->half_liner);
2837 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002838 free(s);
2839}
2840
2841static void script_desc__add(struct script_desc *s)
2842{
2843 list_add_tail(&s->node, &script_descs);
2844}
2845
2846static struct script_desc *script_desc__find(const char *name)
2847{
2848 struct script_desc *s;
2849
2850 list_for_each_entry(s, &script_descs, node)
2851 if (strcasecmp(s->name, name) == 0)
2852 return s;
2853 return NULL;
2854}
2855
2856static struct script_desc *script_desc__findnew(const char *name)
2857{
2858 struct script_desc *s = script_desc__find(name);
2859
2860 if (s)
2861 return s;
2862
2863 s = script_desc__new(name);
2864 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002865 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002866
2867 script_desc__add(s);
2868
2869 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002870}
2871
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002872static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002873{
2874 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002875 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002876
2877 if (strlen(str) > suffix_len) {
2878 p = str + strlen(str) - suffix_len;
2879 if (!strncmp(p, suffix, suffix_len))
2880 return p;
2881 }
2882
2883 return NULL;
2884}
2885
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002886static int read_script_info(struct script_desc *desc, const char *filename)
2887{
2888 char line[BUFSIZ], *p;
2889 FILE *fp;
2890
2891 fp = fopen(filename, "r");
2892 if (!fp)
2893 return -1;
2894
2895 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002896 p = skip_spaces(line);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002897 if (strlen(p) == 0)
2898 continue;
2899 if (*p != '#')
2900 continue;
2901 p++;
2902 if (strlen(p) && *p == '!')
2903 continue;
2904
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002905 p = skip_spaces(p);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002906 if (strlen(p) && p[strlen(p) - 1] == '\n')
2907 p[strlen(p) - 1] = '\0';
2908
2909 if (!strncmp(p, "description:", strlen("description:"))) {
2910 p += strlen("description:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002911 desc->half_liner = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002912 continue;
2913 }
2914
2915 if (!strncmp(p, "args:", strlen("args:"))) {
2916 p += strlen("args:");
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03002917 desc->args = strdup(skip_spaces(p));
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002918 continue;
2919 }
2920 }
2921
2922 fclose(fp);
2923
2924 return 0;
2925}
2926
Robert Richter38efb532011-11-25 11:38:40 +01002927static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2928{
2929 char *script_root, *str;
2930
2931 script_root = strdup(script_dirent->d_name);
2932 if (!script_root)
2933 return NULL;
2934
2935 str = (char *)ends_with(script_root, suffix);
2936 if (!str) {
2937 free(script_root);
2938 return NULL;
2939 }
2940
2941 *str = '\0';
2942 return script_root;
2943}
2944
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002945static int list_available_scripts(const struct option *opt __maybe_unused,
2946 const char *s __maybe_unused,
2947 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002948{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002949 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002950 char scripts_path[MAXPATHLEN];
2951 DIR *scripts_dir, *lang_dir;
2952 char script_path[MAXPATHLEN];
2953 char lang_path[MAXPATHLEN];
2954 struct script_desc *desc;
2955 char first_half[BUFSIZ];
2956 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002957
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002958 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002959
2960 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002961 if (!scripts_dir) {
2962 fprintf(stdout,
2963 "open(%s) failed.\n"
2964 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2965 scripts_path);
2966 exit(-1);
2967 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002968
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002969 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01002970 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
2971 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002972 lang_dir = opendir(lang_path);
2973 if (!lang_dir)
2974 continue;
2975
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002976 for_each_script(lang_path, lang_dir, script_dirent) {
2977 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002978 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002979 desc = script_desc__findnew(script_root);
Jiri Olsa77f18152018-03-19 09:29:01 +01002980 scnprintf(script_path, MAXPATHLEN, "%s/%s",
2981 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002982 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002983 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002984 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002985 }
2986 }
2987
2988 fprintf(stdout, "List of available trace scripts:\n");
2989 list_for_each_entry(desc, &script_descs, node) {
2990 sprintf(first_half, "%s %s", desc->name,
2991 desc->args ? desc->args : "");
2992 fprintf(stdout, " %-36s %s\n", first_half,
2993 desc->half_liner ? desc->half_liner : "");
2994 }
2995
2996 exit(0);
2997}
2998
Feng Tange5f37052012-09-07 16:42:26 +08002999/*
Feng Tang49e639e2012-10-30 11:56:03 +08003000 * Some scripts specify the required events in their "xxx-record" file,
3001 * this function will check if the events in perf.data match those
3002 * mentioned in the "xxx-record".
3003 *
3004 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
3005 * which is covered well now. And new parsing code should be added to
3006 * cover the future complexing formats like event groups etc.
3007 */
3008static int check_ev_match(char *dir_name, char *scriptname,
3009 struct perf_session *session)
3010{
3011 char filename[MAXPATHLEN], evname[128];
3012 char line[BUFSIZ], *p;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003013 struct evsel *pos;
Feng Tang49e639e2012-10-30 11:56:03 +08003014 int match, len;
3015 FILE *fp;
3016
Jiri Olsa77f18152018-03-19 09:29:01 +01003017 scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
Feng Tang49e639e2012-10-30 11:56:03 +08003018
3019 fp = fopen(filename, "r");
3020 if (!fp)
3021 return -1;
3022
3023 while (fgets(line, sizeof(line), fp)) {
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003024 p = skip_spaces(line);
Feng Tang49e639e2012-10-30 11:56:03 +08003025 if (*p == '#')
3026 continue;
3027
3028 while (strlen(p)) {
3029 p = strstr(p, "-e");
3030 if (!p)
3031 break;
3032
3033 p += 2;
Arnaldo Carvalho de Melo32858482019-06-26 11:42:03 -03003034 p = skip_spaces(p);
Feng Tang49e639e2012-10-30 11:56:03 +08003035 len = strcspn(p, " \t");
3036 if (!len)
3037 break;
3038
3039 snprintf(evname, len + 1, "%s", p);
3040
3041 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003042 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08003043 if (!strcmp(perf_evsel__name(pos), evname)) {
3044 match = 1;
3045 break;
3046 }
3047 }
3048
3049 if (!match) {
3050 fclose(fp);
3051 return -1;
3052 }
3053 }
3054 }
3055
3056 fclose(fp);
3057 return 0;
3058}
3059
3060/*
Feng Tange5f37052012-09-07 16:42:26 +08003061 * Return -1 if none is found, otherwise the actual scripts number.
3062 *
3063 * Currently the only user of this function is the script browser, which
3064 * will list all statically runnable scripts, select one, execute it and
3065 * show the output in a perf browser.
3066 */
Andi Kleen905e4af2019-03-11 07:45:01 -07003067int find_scripts(char **scripts_array, char **scripts_path_array, int num,
3068 int pathlen)
Feng Tange5f37052012-09-07 16:42:26 +08003069{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003070 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08003071 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08003072 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08003073 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003074 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003075 .path = input_name,
3076 .mode = PERF_DATA_MODE_READ,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003077 };
Feng Tange5f37052012-09-07 16:42:26 +08003078 char *temp;
3079 int i = 0;
3080
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003081 session = perf_session__new(&data, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08003082 if (!session)
3083 return -1;
3084
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003085 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08003086
3087 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08003088 if (!scripts_dir) {
3089 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003090 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08003091 }
Feng Tange5f37052012-09-07 16:42:26 +08003092
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003093 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003094 scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
3095 lang_dirent->d_name);
Jin Yao90ce61b2018-04-09 18:26:47 +08003096#ifndef HAVE_LIBPERL_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003097 if (strstr(lang_path, "perl"))
3098 continue;
3099#endif
Jin Yao90ce61b2018-04-09 18:26:47 +08003100#ifndef HAVE_LIBPYTHON_SUPPORT
Feng Tange5f37052012-09-07 16:42:26 +08003101 if (strstr(lang_path, "python"))
3102 continue;
3103#endif
3104
3105 lang_dir = opendir(lang_path);
3106 if (!lang_dir)
3107 continue;
3108
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003109 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08003110 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003111 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08003112 continue;
Andi Kleen905e4af2019-03-11 07:45:01 -07003113 if (i >= num)
3114 break;
3115 snprintf(scripts_path_array[i], pathlen, "%s/%s",
3116 lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003117 script_dirent->d_name);
3118 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08003119 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003120 (temp - script_dirent->d_name) + 1,
3121 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08003122
3123 if (check_ev_match(lang_path,
3124 scripts_array[i], session))
3125 continue;
3126
Feng Tange5f37052012-09-07 16:42:26 +08003127 i++;
3128 }
Feng Tang49e639e2012-10-30 11:56:03 +08003129 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08003130 }
3131
Feng Tang49e639e2012-10-30 11:56:03 +08003132 closedir(scripts_dir);
3133 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08003134 return i;
3135}
3136
Tom Zanussi38752942009-12-15 02:53:39 -06003137static char *get_script_path(const char *script_root, const char *suffix)
3138{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003139 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06003140 char scripts_path[MAXPATHLEN];
3141 char script_path[MAXPATHLEN];
3142 DIR *scripts_dir, *lang_dir;
3143 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01003144 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06003145
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003146 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06003147
3148 scripts_dir = opendir(scripts_path);
3149 if (!scripts_dir)
3150 return NULL;
3151
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003152 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Jiri Olsa77f18152018-03-19 09:29:01 +01003153 scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
3154 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06003155 lang_dir = opendir(lang_path);
3156 if (!lang_dir)
3157 continue;
3158
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03003159 for_each_script(lang_path, lang_dir, script_dirent) {
3160 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01003161 if (__script_root && !strcmp(script_root, __script_root)) {
3162 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003163 closedir(lang_dir);
3164 closedir(scripts_dir);
Jiri Olsa77f18152018-03-19 09:29:01 +01003165 scnprintf(script_path, MAXPATHLEN, "%s/%s",
3166 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01003167 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06003168 }
3169 free(__script_root);
3170 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003171 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003172 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09003173 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06003174
Robert Richter38efb532011-11-25 11:38:40 +01003175 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003176}
3177
Tom Zanussib5b87312010-11-10 08:16:51 -06003178static bool is_top_script(const char *script_path)
3179{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02003180 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06003181}
3182
3183static int has_required_arg(char *script_path)
3184{
3185 struct script_desc *desc;
3186 int n_args = 0;
3187 char *p;
3188
3189 desc = script_desc__new(NULL);
3190
3191 if (read_script_info(desc, script_path))
3192 goto out;
3193
3194 if (!desc->args)
3195 goto out;
3196
3197 for (p = desc->args; *p; p++)
3198 if (*p == '<')
3199 n_args++;
3200out:
3201 script_desc__delete(desc);
3202
3203 return n_args;
3204}
3205
David Ahernd54b1a92012-08-26 12:24:46 -06003206static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003207{
3208 char **__argv = malloc(sizeof(const char *) * argc);
3209
David Ahernd54b1a92012-08-26 12:24:46 -06003210 if (!__argv) {
3211 pr_err("malloc failed\n");
3212 return -1;
3213 }
3214
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003215 memcpy(__argv, argv, sizeof(const char *) * argc);
3216 argc = parse_options(argc, (const char **)__argv, record_options,
3217 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
3218 free(__argv);
3219
David Ahernd54b1a92012-08-26 12:24:46 -06003220 system_wide = (argc == 0);
3221
3222 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003223}
3224
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003225static void script__setup_sample_type(struct perf_script *script)
3226{
3227 struct perf_session *session = script->session;
3228 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
3229
3230 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
3231 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003232 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003233 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -03003234 dwarf_callchain_users = true;
3235 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003236 callchain_param.record_mode = CALLCHAIN_LBR;
3237 else
3238 callchain_param.record_mode = CALLCHAIN_FP;
3239 }
3240}
3241
Jiri Olsa89f16882018-09-13 14:54:03 +02003242static int process_stat_round_event(struct perf_session *session,
3243 union perf_event *event)
Jiri Olsae099eba2016-01-05 22:09:09 +01003244{
3245 struct stat_round_event *round = &event->stat_round;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003246 struct evsel *counter;
Jiri Olsae099eba2016-01-05 22:09:09 +01003247
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003248 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01003249 perf_stat_process_counter(&stat_config, counter);
3250 process_stat(counter, round->time);
3251 }
3252
3253 process_stat_interval(round->time);
3254 return 0;
3255}
3256
Jiri Olsa89f16882018-09-13 14:54:03 +02003257static int process_stat_config_event(struct perf_session *session __maybe_unused,
3258 union perf_event *event)
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003259{
3260 perf_event__read_stat_config(&stat_config, &event->stat_config);
3261 return 0;
3262}
3263
Jiri Olsacfc88742016-01-05 22:09:06 +01003264static int set_maps(struct perf_script *script)
3265{
Jiri Olsa63503db2019-07-21 13:23:52 +02003266 struct evlist *evlist = script->session->evlist;
Jiri Olsacfc88742016-01-05 22:09:06 +01003267
3268 if (!script->cpus || !script->threads)
3269 return 0;
3270
3271 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
3272 return -EINVAL;
3273
Jiri Olsa453fa032019-07-21 13:24:43 +02003274 perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
Jiri Olsacfc88742016-01-05 22:09:06 +01003275
3276 if (perf_evlist__alloc_stats(evlist, true))
3277 return -ENOMEM;
3278
3279 script->allocated = true;
3280 return 0;
3281}
3282
3283static
Jiri Olsa89f16882018-09-13 14:54:03 +02003284int process_thread_map_event(struct perf_session *session,
3285 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003286{
Jiri Olsa89f16882018-09-13 14:54:03 +02003287 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003288 struct perf_script *script = container_of(tool, struct perf_script, tool);
3289
3290 if (script->threads) {
3291 pr_warning("Extra thread map event, ignoring.\n");
3292 return 0;
3293 }
3294
3295 script->threads = thread_map__new_event(&event->thread_map);
3296 if (!script->threads)
3297 return -ENOMEM;
3298
3299 return set_maps(script);
3300}
3301
3302static
Jiri Olsa89f16882018-09-13 14:54:03 +02003303int process_cpu_map_event(struct perf_session *session,
3304 union perf_event *event)
Jiri Olsacfc88742016-01-05 22:09:06 +01003305{
Jiri Olsa89f16882018-09-13 14:54:03 +02003306 struct perf_tool *tool = session->tool;
Jiri Olsacfc88742016-01-05 22:09:06 +01003307 struct perf_script *script = container_of(tool, struct perf_script, tool);
3308
3309 if (script->cpus) {
3310 pr_warning("Extra cpu map event, ignoring.\n");
3311 return 0;
3312 }
3313
3314 script->cpus = cpu_map__new_data(&event->cpu_map.data);
3315 if (!script->cpus)
3316 return -ENOMEM;
3317
3318 return set_maps(script);
3319}
3320
Jiri Olsa89f16882018-09-13 14:54:03 +02003321static int process_feature_event(struct perf_session *session,
3322 union perf_event *event)
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303323{
3324 if (event->feat.feat_id < HEADER_LAST_FEATURE)
Jiri Olsa89f16882018-09-13 14:54:03 +02003325 return perf_event__process_feature(session, event);
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303326 return 0;
3327}
3328
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003329#ifdef HAVE_AUXTRACE_SUPPORT
Jiri Olsa89f16882018-09-13 14:54:03 +02003330static int perf_script__process_auxtrace_info(struct perf_session *session,
3331 union perf_event *event)
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003332{
Jiri Olsa89f16882018-09-13 14:54:03 +02003333 struct perf_tool *tool = session->tool;
3334
3335 int ret = perf_event__process_auxtrace_info(session, event);
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003336
3337 if (ret == 0) {
3338 struct perf_script *script = container_of(tool, struct perf_script, tool);
3339
3340 ret = perf_script__setup_per_event_dump(script);
3341 }
3342
3343 return ret;
3344}
3345#else
3346#define perf_script__process_auxtrace_info 0
3347#endif
3348
Andi Kleenb585ebd2018-09-20 11:05:36 -07003349static int parse_insn_trace(const struct option *opt __maybe_unused,
3350 const char *str __maybe_unused,
3351 int unset __maybe_unused)
3352{
3353 parse_output_fields(NULL, "+insn,-event,-period", 0);
3354 itrace_parse_synth_opts(opt, "i0ns", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003355 symbol_conf.nanosecs = true;
Andi Kleenb585ebd2018-09-20 11:05:36 -07003356 return 0;
3357}
3358
3359static int parse_xed(const struct option *opt __maybe_unused,
3360 const char *str __maybe_unused,
3361 int unset __maybe_unused)
3362{
3363 force_pager("xed -F insn: -A -64 | less");
3364 return 0;
3365}
3366
Andi Kleend1b15522018-09-20 11:05:38 -07003367static int parse_call_trace(const struct option *opt __maybe_unused,
3368 const char *str __maybe_unused,
3369 int unset __maybe_unused)
3370{
3371 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
3372 itrace_parse_synth_opts(opt, "cewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003373 symbol_conf.nanosecs = true;
Jiri Olsa1c492422019-05-08 15:20:05 +02003374 symbol_conf.pad_output_len_dso = 50;
Andi Kleend1b15522018-09-20 11:05:38 -07003375 return 0;
3376}
3377
3378static int parse_callret_trace(const struct option *opt __maybe_unused,
3379 const char *str __maybe_unused,
3380 int unset __maybe_unused)
3381{
3382 parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
3383 itrace_parse_synth_opts(opt, "crewp", 0);
Andi Kleen52bab882019-03-05 06:47:47 -08003384 symbol_conf.nanosecs = true;
Andi Kleend1b15522018-09-20 11:05:38 -07003385 return 0;
3386}
3387
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003388int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003389{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003390 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01003391 bool header = false;
3392 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003393 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06003394 char *rec_script_path = NULL;
3395 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003396 struct perf_session *session;
Andi Kleen4eb06812018-09-20 11:05:37 -07003397 struct itrace_synth_opts itrace_synth_opts = {
3398 .set = false,
3399 .default_no_sample = true,
3400 };
Andi Kleen3ab481a2019-03-05 06:47:45 -08003401 struct utsname uts;
Tom Zanussib5b87312010-11-10 08:16:51 -06003402 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003403 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003404 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003405 struct perf_script script = {
3406 .tool = {
3407 .sample = process_sample_event,
3408 .mmap = perf_event__process_mmap,
3409 .mmap2 = perf_event__process_mmap2,
3410 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05303411 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003412 .exit = perf_event__process_exit,
3413 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02003414 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02003415 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003416 .tracing_data = perf_event__process_tracing_data,
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303417 .feature = process_feature_event,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003418 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003419 .id_index = perf_event__process_id_index,
Arnaldo Carvalho de Melofa48c892017-11-09 16:04:26 -03003420 .auxtrace_info = perf_script__process_auxtrace_info,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003421 .auxtrace = perf_event__process_auxtrace,
3422 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01003423 .stat = perf_event__process_stat_event,
3424 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01003425 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01003426 .thread_map = process_thread_map_event,
3427 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003428 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003429 .ordering_requires_timestamps = true,
3430 },
3431 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003432 struct perf_data data = {
Yunlong Song06af0f22015-04-02 21:47:16 +08003433 .mode = PERF_DATA_MODE_READ,
3434 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003435 const struct option options[] = {
3436 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
3437 "dump raw trace in ASCII"),
3438 OPT_INCR('v', "verbose", &verbose,
3439 "be more verbose (show symbol address, etc)"),
3440 OPT_BOOLEAN('L', "Latency", &latency_format,
3441 "show latency attributes (irqs/preemption disabled, etc)"),
3442 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
3443 list_available_scripts),
3444 OPT_CALLBACK('s', "script", NULL, "name",
3445 "script file name (lang:script name, script name, or *)",
3446 parse_scriptname),
3447 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
3448 "generate perf-script.xx script in specified language"),
3449 OPT_STRING('i', "input", &input_name, "file", "input file name"),
3450 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
3451 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01003452 OPT_BOOLEAN(0, "header", &header, "Show data header."),
3453 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003454 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
3455 "file", "vmlinux pathname"),
3456 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
3457 "file", "kallsyms pathname"),
3458 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
3459 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00003460 OPT_CALLBACK(0, "symfs", NULL, "directory",
3461 "Look for files with symbols relative to this directory",
3462 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08003463 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003464 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07003465 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03003466 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003467 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Ravi Bangoria10e9cec2018-06-25 18:12:18 +05303468 "addr,symoff,srcline,period,iregs,uregs,brstack,"
3469 "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
Adrian Hunter68fb45b2019-05-20 14:37:14 +03003470 "callindent,insn,insnlen,synth,phys_addr,metric,misc,ipc",
Andi Kleen48d02a12017-02-23 15:46:34 -08003471 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003472 OPT_BOOLEAN('a', "all-cpus", &system_wide,
3473 "system-wide collection from all CPUs"),
3474 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
3475 "only consider these symbols"),
Andi Kleenb585ebd2018-09-20 11:05:36 -07003476 OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
3477 "Decode instructions from itrace", parse_insn_trace),
3478 OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
3479 "Run xed disassembler on output", parse_xed),
Andi Kleend1b15522018-09-20 11:05:38 -07003480 OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
3481 "Decode calls from from itrace", parse_call_trace),
3482 OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
3483 "Decode calls and returns from itrace", parse_callret_trace),
Andi Kleen99f753f2018-09-20 11:05:39 -07003484 OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
3485 "Only print symbols and callees with --call-trace/--call-ret-trace"),
David Ahern64eff7d2016-11-25 13:00:21 -07003486 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
3487 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003488 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
3489 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
3490 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06003491 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
3492 "only consider symbols in these pids"),
3493 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
3494 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03003495 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
3496 "Set the maximum stack depth when parsing the callchain, "
3497 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003498 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Andi Kleen90b10f42019-03-14 15:50:00 -07003499 OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003500 OPT_BOOLEAN('I', "show-info", &show_full_info,
3501 "display extended information from perf.data file"),
3502 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
3503 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09003504 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
3505 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09003506 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
3507 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03003508 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
3509 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05303510 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
3511 "Show namespace events (if recorded)"),
Jiri Olsa3d7c27b2018-01-07 17:03:53 +01003512 OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
3513 "Show lost events (if recorded)"),
Jiri Olsa3233b372018-02-06 19:17:59 +01003514 OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
3515 "Show round events (if recorded)"),
Jiri Olsa490c8cc2019-05-08 15:20:08 +02003516 OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
3517 "Show bpf related events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03003518 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
3519 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05003520 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08003521 OPT_INTEGER(0, "max-blocks", &max_blocks,
3522 "Maximum number of code blocks to dump with brstackinsn"),
Andi Kleen52bab882019-03-05 06:47:47 -08003523 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
Adrian Hunter83e19862015-09-25 16:15:36 +03003524 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003525 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
Andi Kleenc12e0392018-09-13 20:10:31 -07003526 "Instruction Tracing options\n" ITRACE_HELP,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003527 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07003528 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
3529 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07003530 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
3531 "Enable symbol demangling"),
3532 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
3533 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07003534 OPT_STRING(0, "time", &script.time_str, "str",
3535 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09003536 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
3537 "Show inline function"),
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003538 OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
3539 "guest mount directory under which every guest os"
3540 " instance has a subdir"),
3541 OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
3542 "file", "file saving guest os vmlinux"),
3543 OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
3544 "file", "file saving guest os /proc/kallsyms"),
3545 OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
3546 "file", "file saving guest os /proc/modules"),
Arnaldo Carvalho de Meloadd3a712019-08-15 11:21:21 -03003547 OPTS_EVSWITCH(&script.evswitch),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003548 OPT_END()
3549 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08003550 const char * const script_subcommands[] = { "record", "report", NULL };
3551 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03003552 "perf script [<options>]",
3553 "perf script [<options>] record <script> [<record-options>] <command>",
3554 "perf script [<options>] report <script> [script-args]",
3555 "perf script [<options>] <script> [<record-options>] <command>",
3556 "perf script [<options>] <top-script> [script-args]",
3557 NULL
3558 };
Tom Zanussi38752942009-12-15 02:53:39 -06003559
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03003560 perf_set_singlethreaded();
3561
Tom Zanussib5b87312010-11-10 08:16:51 -06003562 setup_scripting();
3563
Yunlong Song40cae2b2015-03-18 21:35:54 +08003564 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06003565 PARSE_OPT_STOP_AT_NON_OPTION);
3566
Arnaldo Carvalho de Melo15a108a2019-06-28 17:16:58 -03003567 if (symbol_conf.guestmount ||
3568 symbol_conf.default_guest_vmlinux_name ||
3569 symbol_conf.default_guest_kallsyms ||
3570 symbol_conf.default_guest_modules) {
3571 /*
3572 * Enable guest sample processing.
3573 */
3574 perf_guest = true;
3575 }
3576
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003577 data.path = input_name;
3578 data.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003579
Tom Zanussib5b87312010-11-10 08:16:51 -06003580 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
3581 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
3582 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003583 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003584 }
3585
Tom Zanussib5b87312010-11-10 08:16:51 -06003586 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
3587 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
3588 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06003589 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06003590 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003591 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06003592 return -1;
3593 }
Tom Zanussi38752942009-12-15 02:53:39 -06003594 }
3595
Andi Kleen90b10f42019-03-14 15:50:00 -07003596 if (script.time_str && reltime) {
3597 fprintf(stderr, "Don't combine --reltime with --time\n");
3598 return -1;
3599 }
3600
Adrian Hunter3c5b6452015-09-25 16:15:51 +03003601 if (itrace_synth_opts.callchain &&
3602 itrace_synth_opts.callchain_sz > scripting_max_stack)
3603 scripting_max_stack = itrace_synth_opts.callchain_sz;
3604
Ben Hutchings44e668c2010-10-10 16:10:03 +01003605 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06003606 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01003607
Tom Zanussib5b87312010-11-10 08:16:51 -06003608 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05003609 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06003610 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003611 pid_t pid;
3612
Tom Zanussib5b87312010-11-10 08:16:51 -06003613 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
3614 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
3615
3616 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003617 usage_with_options_msg(script_usage, options,
3618 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003619 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003620 }
3621
Tom Zanussib5b87312010-11-10 08:16:51 -06003622 if (is_top_script(argv[0])) {
3623 rep_args = argc - 1;
3624 } else {
3625 int rec_args;
3626
3627 rep_args = has_required_arg(rep_script_path);
3628 rec_args = (argc - 1) - rep_args;
3629 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09003630 usage_with_options_msg(script_usage, options,
3631 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003632 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06003633 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06003634 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05003635 }
3636
3637 if (pipe(live_pipe) < 0) {
3638 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06003639 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003640 }
3641
3642 pid = fork();
3643 if (pid < 0) {
3644 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06003645 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003646 }
3647
3648 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003649 j = 0;
3650
Tom Zanussia0cccc22010-04-01 23:59:25 -05003651 dup2(live_pipe[1], 1);
3652 close(live_pipe[0]);
3653
Robert Richter317df652011-11-25 15:05:25 +01003654 if (is_top_script(argv[0])) {
3655 system_wide = true;
3656 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06003657 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3658 err = -1;
3659 goto out;
3660 }
Robert Richter317df652011-11-25 15:05:25 +01003661 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003662
3663 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003664 if (!__argv) {
3665 pr_err("malloc failed\n");
3666 err = -ENOMEM;
3667 goto out;
3668 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003669
Tom Zanussib5b87312010-11-10 08:16:51 -06003670 __argv[j++] = "/bin/sh";
3671 __argv[j++] = rec_script_path;
3672 if (system_wide)
3673 __argv[j++] = "-a";
3674 __argv[j++] = "-q";
3675 __argv[j++] = "-o";
3676 __argv[j++] = "-";
3677 for (i = rep_args + 1; i < argc; i++)
3678 __argv[j++] = argv[i];
3679 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003680
3681 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003682 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003683 exit(-1);
3684 }
3685
3686 dup2(live_pipe[0], 0);
3687 close(live_pipe[1]);
3688
Tom Zanussib5b87312010-11-10 08:16:51 -06003689 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003690 if (!__argv) {
3691 pr_err("malloc failed\n");
3692 err = -ENOMEM;
3693 goto out;
3694 }
3695
Tom Zanussib5b87312010-11-10 08:16:51 -06003696 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003697 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003698 __argv[j++] = rep_script_path;
3699 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003700 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003701 __argv[j++] = "-i";
3702 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003703 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003704
3705 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003706 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003707 exit(-1);
3708 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003709
Tom Zanussib5b87312010-11-10 08:16:51 -06003710 if (rec_script_path)
3711 script_path = rec_script_path;
3712 if (rep_script_path)
3713 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003714
Tom Zanussib5b87312010-11-10 08:16:51 -06003715 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003716 j = 0;
3717
Robert Richter317df652011-11-25 15:05:25 +01003718 if (!rec_script_path)
3719 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003720 else if (!system_wide) {
3721 if (have_cmd(argc - 1, &argv[1]) != 0) {
3722 err = -1;
3723 goto out;
3724 }
3725 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003726
3727 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003728 if (!__argv) {
3729 pr_err("malloc failed\n");
3730 err = -ENOMEM;
3731 goto out;
3732 }
3733
Tom Zanussib5b87312010-11-10 08:16:51 -06003734 __argv[j++] = "/bin/sh";
3735 __argv[j++] = script_path;
3736 if (system_wide)
3737 __argv[j++] = "-a";
3738 for (i = 2; i < argc; i++)
3739 __argv[j++] = argv[i];
3740 __argv[j++] = NULL;
3741
3742 execvp("/bin/sh", (char **)__argv);
3743 free(__argv);
3744 exit(-1);
3745 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003746
Milian Wolffc1c9b962018-10-21 21:14:23 +02003747 if (!script_name) {
Tom Zanussicf4fee52010-03-03 01:04:33 -06003748 setup_pager();
Milian Wolffc1c9b962018-10-21 21:14:23 +02003749 use_browser = 0;
3750 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003751
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003752 session = perf_session__new(&data, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003753 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003754 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003755
Jiri Olsae90debd2013-12-09 11:02:50 +01003756 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003757 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003758 perf_session__fprintf_info(session, stdout, show_full_info);
3759 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003760 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003761 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003762 if (show_full_info)
3763 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003764
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003765 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003766 goto out_delete;
3767
Andi Kleen3ab481a2019-03-05 06:47:45 -08003768 uname(&uts);
Song Liu9d491692019-06-20 18:44:38 -07003769 if (data.is_pipe || /* assume pipe_mode indicates native_arch */
3770 !strcmp(uts.machine, session->header.env.arch) ||
Andi Kleen3ab481a2019-03-05 06:47:45 -08003771 (!strcmp(uts.machine, "x86_64") &&
3772 !strcmp(session->header.env.arch, "i386")))
3773 native_arch = true;
3774
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003775 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003776 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003777
Andi Kleen99f753f2018-09-20 11:05:39 -07003778 if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
3779 symbol_conf.graph_function)
Adrian Huntere2167082016-06-23 16:40:58 +03003780 itrace_synth_opts.thread_stack = true;
3781
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003782 session->itrace_synth_opts = &itrace_synth_opts;
3783
Anton Blanchard5d67be92011-07-04 21:57:50 +10003784 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003785 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3786 if (err < 0)
3787 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003788 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003789 }
3790
David Ahern1424dc92011-03-09 22:23:28 -07003791 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003792 symbol_conf.use_callchain = true;
3793 else
3794 symbol_conf.use_callchain = false;
3795
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003796 if (session->tevent.pevent &&
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -04003797 tep_set_function_resolver(session->tevent.pevent,
3798 machine__resolve_kernel_addr,
3799 &session->machines.host) < 0) {
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003800 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003801 err = -1;
3802 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003803 }
3804
Tom Zanussi956ffd02009-11-25 01:15:46 -06003805 if (generate_script_lang) {
3806 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003807 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003808
David Ahern2c9e45f72011-03-17 10:03:21 -06003809 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003810 fprintf(stderr,
3811 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003812 err = -EINVAL;
3813 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003814 }
3815
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003816 input = open(data.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003817 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003818 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003819 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003820 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003821 }
3822
3823 err = fstat(input, &perf_stat);
3824 if (err < 0) {
3825 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003826 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003827 }
3828
3829 if (!perf_stat.st_size) {
3830 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003831 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003832 }
3833
3834 scripting_ops = script_spec__lookup(generate_script_lang);
3835 if (!scripting_ops) {
3836 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003837 err = -ENOENT;
3838 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003839 }
3840
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003841 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003842 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003843 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003844 }
3845
3846 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003847 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003848 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003849 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003850 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003851 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003852 }
3853
David Ahern9cbdb702011-04-06 21:54:20 -06003854
3855 err = perf_session__check_output_opt(session);
3856 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003857 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003858
Jin Yao284c4e12019-03-01 18:13:06 +08003859 if (script.time_str) {
3860 err = perf_time__parse_for_ranges(script.time_str, session,
3861 &script.ptime_range,
3862 &script.range_size,
3863 &script.range_num);
3864 if (err < 0)
Jin Yao2ab046c2017-12-08 21:13:46 +08003865 goto out_delete;
Adrian Hunter400ae982019-06-04 16:00:00 +03003866
3867 itrace_synth_opts__set_time_range(&itrace_synth_opts,
3868 script.ptime_range,
3869 script.range_num);
David Aherna91f4c42016-11-29 10:15:43 -07003870 }
3871
Arnaldo Carvalho de Melo124e02b2019-08-15 11:31:29 -03003872 err = evswitch__init(&script.evswitch, session->evlist, stderr);
3873 if (err)
3874 goto out_delete;
Arnaldo Carvalho de Melodd41f662019-08-14 16:51:28 -03003875
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003876 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003877
Adrian Hunterd445dd22014-08-15 22:08:37 +03003878 flush_scripting();
3879
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003880out_delete:
Adrian Hunter400ae982019-06-04 16:00:00 +03003881 if (script.ptime_range) {
3882 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
Jin Yao284c4e12019-03-01 18:13:06 +08003883 zfree(&script.ptime_range);
Adrian Hunter400ae982019-06-04 16:00:00 +03003884 }
Jin Yaocc2ef582018-01-10 23:00:33 +08003885
Jiri Olsacfc88742016-01-05 22:09:06 +01003886 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003887 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003888
3889 if (script_started)
3890 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003891out:
3892 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003893}