blob: fb5e49b3bc449b113e3cbd1f4966caf026228889 [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
Andrea Gelminib7eead82010-08-05 15:51:38 +02003#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06006#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030024#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030025#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070026#include "util/time-utils.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030027#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100028#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030029#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030030#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030031#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010032#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010033#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080034#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030035#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030036#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030037#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030038#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030039#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030040#include <sys/types.h>
41#include <sys/stat.h>
42#include <unistd.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020043
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030044#include "sane_ctype.h"
45
Tom Zanussi956ffd02009-11-25 01:15:46 -060046static char const *script_name;
47static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020048static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020049static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020050static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070051static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090052static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010053static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030054static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030055static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100056static const char *cpu_list;
57static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010058static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080059static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060060
Adrian Hunter44cbe722015-09-25 16:15:50 +030061unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030062
David Ahern745f43e2011-03-09 22:23:26 -070063enum perf_output_field {
64 PERF_OUTPUT_COMM = 1U << 0,
65 PERF_OUTPUT_TID = 1U << 1,
66 PERF_OUTPUT_PID = 1U << 2,
67 PERF_OUTPUT_TIME = 1U << 3,
68 PERF_OUTPUT_CPU = 1U << 4,
69 PERF_OUTPUT_EVNAME = 1U << 5,
70 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060071 PERF_OUTPUT_IP = 1U << 7,
72 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060073 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060074 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090075 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020076 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020077 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020078 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020079 PERF_OUTPUT_BRSTACK = 1U << 15,
80 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010081 PERF_OUTPUT_DATA_SRC = 1U << 17,
82 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000083 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030084 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030085 PERF_OUTPUT_INSN = 1U << 21,
86 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080087 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070088 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030089 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040090 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
Andi Kleenb1491ac2017-09-05 11:40:57 -070091 PERF_OUTPUT_UREGS = 1U << 27,
David Ahern745f43e2011-03-09 22:23:26 -070092};
93
94struct output_option {
95 const char *str;
96 enum perf_output_field field;
97} all_output_options[] = {
98 {.str = "comm", .field = PERF_OUTPUT_COMM},
99 {.str = "tid", .field = PERF_OUTPUT_TID},
100 {.str = "pid", .field = PERF_OUTPUT_PID},
101 {.str = "time", .field = PERF_OUTPUT_TIME},
102 {.str = "cpu", .field = PERF_OUTPUT_CPU},
103 {.str = "event", .field = PERF_OUTPUT_EVNAME},
104 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -0600105 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -0700106 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -0600107 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -0600108 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900109 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +0200110 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200111 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +0200112 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Andi Kleenb1491ac2017-09-05 11:40:57 -0700113 {.str = "uregs", .field = PERF_OUTPUT_UREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200114 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
115 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100116 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
117 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000118 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300119 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300120 {.str = "insn", .field = PERF_OUTPUT_INSN},
121 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800122 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700123 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300124 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400125 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
David Ahern745f43e2011-03-09 22:23:26 -0700126};
127
Adrian Hunter14057202017-06-21 13:17:19 +0300128enum {
129 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
130 OUTPUT_TYPE_MAX
131};
132
David Ahern745f43e2011-03-09 22:23:26 -0700133/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600134static struct {
135 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600136 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400137 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600138 u64 fields;
139 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300140} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700141
David Ahern2c9e45f72011-03-17 10:03:21 -0600142 [PERF_TYPE_HARDWARE] = {
143 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700144
David Ahern2c9e45f72011-03-17 10:03:21 -0600145 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
146 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600147 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200148 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
149 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600150
Wang Nan30372f02016-02-24 11:20:45 +0000151 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600152 },
153
154 [PERF_TYPE_SOFTWARE] = {
155 .user_set = false,
156
157 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
158 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600159 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200160 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000161 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600162
163 .invalid_fields = PERF_OUTPUT_TRACE,
164 },
165
166 [PERF_TYPE_TRACEPOINT] = {
167 .user_set = false,
168
169 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
170 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000171 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600172 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700173
174 [PERF_TYPE_RAW] = {
175 .user_set = false,
176
177 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
178 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600179 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200180 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100181 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
Kan Liang49d58f02017-08-29 13:11:11 -0400182 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
183 PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700184
Wang Nan30372f02016-02-24 11:20:45 +0000185 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700186 },
Wang Nan27cfef02015-12-08 02:25:43 +0000187
188 [PERF_TYPE_BREAKPOINT] = {
189 .user_set = false,
190
191 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
192 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
193 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
194 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
195 PERF_OUTPUT_PERIOD,
196
Wang Nan30372f02016-02-24 11:20:45 +0000197 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000198 },
Adrian Hunter14057202017-06-21 13:17:19 +0300199
200 [OUTPUT_TYPE_SYNTH] = {
201 .user_set = false,
202
203 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
204 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
205 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Adrian Hunter47e78082017-05-26 11:17:22 +0300206 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
207 PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300208
209 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
210 },
David Ahern1424dc92011-03-09 22:23:28 -0700211};
David Ahern745f43e2011-03-09 22:23:26 -0700212
Adrian Hunter14057202017-06-21 13:17:19 +0300213static inline int output_type(unsigned int type)
214{
215 switch (type) {
216 case PERF_TYPE_SYNTH:
217 return OUTPUT_TYPE_SYNTH;
218 default:
219 return type;
220 }
221}
222
223static inline unsigned int attr_type(unsigned int type)
224{
225 switch (type) {
226 case OUTPUT_TYPE_SYNTH:
227 return PERF_TYPE_SYNTH;
228 default:
229 return type;
230 }
231}
232
David Ahern2c9e45f72011-03-17 10:03:21 -0600233static bool output_set_by_user(void)
234{
235 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300236 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600237 if (output[j].user_set)
238 return true;
239 }
240 return false;
241}
David Ahern745f43e2011-03-09 22:23:26 -0700242
David Ahern9cbdb702011-04-06 21:54:20 -0600243static const char *output_field2str(enum perf_output_field field)
244{
245 int i, imax = ARRAY_SIZE(all_output_options);
246 const char *str = "";
247
248 for (i = 0; i < imax; ++i) {
249 if (all_output_options[i].field == field) {
250 str = all_output_options[i].str;
251 break;
252 }
253 }
254 return str;
255}
256
Adrian Hunter14057202017-06-21 13:17:19 +0300257#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700258
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300259static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
260 u64 sample_type, const char *sample_msg,
261 enum perf_output_field field,
262 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700263{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300264 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300265 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600266 const char *evname;
267
268 if (attr->sample_type & sample_type)
269 return 0;
270
271 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300272 if (allow_user_set)
273 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300274 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600275 pr_err("Samples for '%s' event do not have %s attribute set. "
276 "Cannot print '%s' field.\n",
277 evname, sample_msg, output_field2str(field));
278 return -1;
279 }
280
281 /* user did not ask for it explicitly so remove from the default list */
282 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300283 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600284 pr_debug("Samples for '%s' event do not have %s attribute set. "
285 "Skipping '%s' field.\n",
286 evname, sample_msg, output_field2str(field));
287
288 return 0;
289}
290
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300291static int perf_evsel__check_stype(struct perf_evsel *evsel,
292 u64 sample_type, const char *sample_msg,
293 enum perf_output_field field)
294{
295 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
296 false);
297}
298
David Ahern9cbdb702011-04-06 21:54:20 -0600299static int perf_evsel__check_attr(struct perf_evsel *evsel,
300 struct perf_session *session)
301{
302 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300303 bool allow_user_set;
304
Jiri Olsae099eba2016-01-05 22:09:09 +0100305 if (perf_header__has_feat(&session->header, HEADER_STAT))
306 return 0;
307
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300308 allow_user_set = perf_header__has_feat(&session->header,
309 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600310
David Ahern1424dc92011-03-09 22:23:28 -0700311 if (PRINT_FIELD(TRACE) &&
312 !perf_session__has_traces(session, "record -R"))
313 return -EINVAL;
314
David Ahern787bef12011-05-27 14:28:43 -0600315 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300316 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
317 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700318 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700319 }
David Ahern7cec0922011-05-30 13:08:23 -0600320
321 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300322 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
323 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600324 return -EINVAL;
325
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100326 if (PRINT_FIELD(DATA_SRC) &&
327 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
328 PERF_OUTPUT_DATA_SRC))
329 return -EINVAL;
330
331 if (PRINT_FIELD(WEIGHT) &&
332 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
333 PERF_OUTPUT_WEIGHT))
334 return -EINVAL;
335
David Ahern7cec0922011-05-30 13:08:23 -0600336 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
337 pr_err("Display of symbols requested but neither sample IP nor "
338 "sample address\nis selected. Hence, no addresses to convert "
339 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600340 return -EINVAL;
341 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900342 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
343 pr_err("Display of offsets requested but symbol is not"
344 "selected.\n");
345 return -EINVAL;
346 }
Mark Santaniello55b9b502017-06-19 09:38:24 -0700347 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
Mark Santaniello106dacd2017-06-19 09:38:25 -0700348 !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
349 pr_err("Display of DSO requested but no address to convert. Select\n"
350 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
David Ahern610723f2011-05-27 14:28:44 -0600351 return -EINVAL;
352 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200353 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
354 pr_err("Display of source line number requested but sample IP is not\n"
355 "selected. Hence, no address to lookup the source line number.\n");
356 return -EINVAL;
357 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800358 if (PRINT_FIELD(BRSTACKINSN) &&
359 !(perf_evlist__combined_branch_type(session->evlist) &
360 PERF_SAMPLE_BRANCH_ANY)) {
361 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
362 "Hint: run 'perf record -b ...'\n");
363 return -EINVAL;
364 }
David Ahern1424dc92011-03-09 22:23:28 -0700365 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300366 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
367 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700368 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700369
370 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300371 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
372 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700373 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700374
375 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300376 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
377 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700378 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600379
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200380 if (PRINT_FIELD(PERIOD) &&
381 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
382 PERF_OUTPUT_PERIOD))
383 return -EINVAL;
384
Stephane Eranianfc36f942015-08-31 18:41:10 +0200385 if (PRINT_FIELD(IREGS) &&
386 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
387 PERF_OUTPUT_IREGS))
388 return -EINVAL;
389
Andi Kleenb1491ac2017-09-05 11:40:57 -0700390 if (PRINT_FIELD(UREGS) &&
391 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
392 PERF_OUTPUT_UREGS))
393 return -EINVAL;
394
Kan Liang49d58f02017-08-29 13:11:11 -0400395 if (PRINT_FIELD(PHYS_ADDR) &&
396 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
397 PERF_OUTPUT_PHYS_ADDR))
398 return -EINVAL;
399
David Ahern9cbdb702011-04-06 21:54:20 -0600400 return 0;
401}
402
Adrian Hunter7ea95722013-11-01 15:51:30 +0200403static void set_print_ip_opts(struct perf_event_attr *attr)
404{
Adrian Hunter14057202017-06-21 13:17:19 +0300405 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200406
407 output[type].print_ip_opts = 0;
408 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300409 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200410
411 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300412 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200413
414 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300415 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200416
417 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300418 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200419
420 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300421 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200422}
423
David Ahern9cbdb702011-04-06 21:54:20 -0600424/*
425 * verify all user requested events exist and the samples
426 * have the expected data
427 */
428static int perf_session__check_output_opt(struct perf_session *session)
429{
He Kuang40f20e52016-05-16 04:51:19 +0000430 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600431 struct perf_evsel *evsel;
432
Adrian Hunter14057202017-06-21 13:17:19 +0300433 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
434 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600435
436 /*
437 * even if fields is set to 0 (ie., show nothing) event must
438 * exist if user explicitly includes it on the command line
439 */
Adrian Hunter14057202017-06-21 13:17:19 +0300440 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
441 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600442 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300443 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600444 event_type(j));
445 return -1;
446 }
447
448 if (evsel && output[j].fields &&
449 perf_evsel__check_attr(evsel, session))
450 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400451
452 if (evsel == NULL)
453 continue;
454
Adrian Hunter7ea95722013-11-01 15:51:30 +0200455 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700456 }
457
Adrian Hunter98526ee2014-07-31 09:00:59 +0300458 if (!no_callchain) {
459 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000460 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300461
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300462 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000463 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300464 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
465 use_callchain = true;
466 break;
467 }
468 }
He Kuang71ac8992016-08-04 11:25:43 +0000469 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300470 symbol_conf.use_callchain = false;
471 }
472
David Ahern80b8b492013-11-19 21:07:37 -0700473 /*
474 * set default for tracepoints to print symbols only
475 * if callchains are present
476 */
477 if (symbol_conf.use_callchain &&
478 !output[PERF_TYPE_TRACEPOINT].user_set) {
479 struct perf_event_attr *attr;
480
481 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700482
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300483 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000484 if (evsel->attr.type != j)
485 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700486
He Kuang40f20e52016-05-16 04:51:19 +0000487 attr = &evsel->attr;
488
489 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
490 output[j].fields |= PERF_OUTPUT_IP;
491 output[j].fields |= PERF_OUTPUT_SYM;
492 output[j].fields |= PERF_OUTPUT_DSO;
493 set_print_ip_opts(attr);
494 goto out;
495 }
David Ahern80b8b492013-11-19 21:07:37 -0700496 }
497 }
498
499out:
David Ahern1424dc92011-03-09 22:23:28 -0700500 return 0;
501}
David Ahern745f43e2011-03-09 22:23:26 -0700502
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300503static int perf_sample__fprintf_iregs(struct perf_sample *sample,
504 struct perf_event_attr *attr, FILE *fp)
Stephane Eranianfc36f942015-08-31 18:41:10 +0200505{
506 struct regs_dump *regs = &sample->intr_regs;
507 uint64_t mask = attr->sample_regs_intr;
508 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300509 int printed = 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200510
511 if (!regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300512 return 0;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200513
514 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
515 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300516 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200517 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300518
519 return printed;
Stephane Eranianfc36f942015-08-31 18:41:10 +0200520}
521
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300522static int perf_sample__fprintf_uregs(struct perf_sample *sample,
523 struct perf_event_attr *attr, FILE *fp)
Andi Kleenb1491ac2017-09-05 11:40:57 -0700524{
525 struct regs_dump *regs = &sample->user_regs;
526 uint64_t mask = attr->sample_regs_user;
527 unsigned i = 0, r;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300528 int printed = 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700529
530 if (!regs || !regs->regs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300531 return 0;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700532
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300533 printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700534
535 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
536 u64 val = regs->regs[i++];
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300537 printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
Andi Kleenb1491ac2017-09-05 11:40:57 -0700538 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300539
540 return printed;
Andi Kleenb1491ac2017-09-05 11:40:57 -0700541}
542
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300543static int perf_sample__fprintf_start(struct perf_sample *sample,
544 struct thread *thread,
545 struct perf_evsel *evsel, FILE *fp)
David Ahernc70c94b2011-03-09 22:23:25 -0700546{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300547 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700548 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700549 unsigned long long nsecs;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300550 int printed = 0;
David Ahernc70c94b2011-03-09 22:23:25 -0700551
David Ahern745f43e2011-03-09 22:23:26 -0700552 if (PRINT_FIELD(COMM)) {
553 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300554 printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600555 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300556 printed += fprintf(fp, "%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700557 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300558 printed += fprintf(fp, "%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700559 }
David Ahernc70c94b2011-03-09 22:23:25 -0700560
David Ahern745f43e2011-03-09 22:23:26 -0700561 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300562 printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
David Ahern745f43e2011-03-09 22:23:26 -0700563 else if (PRINT_FIELD(PID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300564 printed += fprintf(fp, "%5d ", sample->pid);
David Ahern745f43e2011-03-09 22:23:26 -0700565 else if (PRINT_FIELD(TID))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300566 printed += fprintf(fp, "%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700567
David Ahern745f43e2011-03-09 22:23:26 -0700568 if (PRINT_FIELD(CPU)) {
569 if (latency_format)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300570 printed += fprintf(fp, "%3d ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700571 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300572 printed += fprintf(fp, "[%03d] ", sample->cpu);
David Ahern745f43e2011-03-09 22:23:26 -0700573 }
David Ahernc70c94b2011-03-09 22:23:25 -0700574
David Ahern745f43e2011-03-09 22:23:26 -0700575 if (PRINT_FIELD(TIME)) {
576 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300577 secs = nsecs / NSEC_PER_SEC;
578 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900579
Adrian Hunter83e19862015-09-25 16:15:36 +0300580 if (nanosecs)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300581 printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900582 else {
583 char sample_time[32];
584 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300585 printed += fprintf(fp, "%12s: ", sample_time);
Namhyung Kim99620a52016-10-24 11:02:45 +0900586 }
David Ahern745f43e2011-03-09 22:23:26 -0700587 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300588
589 return printed;
David Ahernc70c94b2011-03-09 22:23:25 -0700590}
591
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200592static inline char
593mispred_str(struct branch_entry *br)
594{
595 if (!(br->flags.mispred || br->flags.predicted))
596 return '-';
597
598 return br->flags.predicted ? 'P' : 'M';
599}
600
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300601static int perf_sample__fprintf_brstack(struct perf_sample *sample,
602 struct thread *thread,
603 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200604{
605 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700606 struct addr_location alf, alt;
607 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300608 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200609
610 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300611 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200612
613 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700614 from = br->entries[i].from;
615 to = br->entries[i].to;
616
617 if (PRINT_FIELD(DSO)) {
618 memset(&alf, 0, sizeof(alf));
619 memset(&alt, 0, sizeof(alt));
620 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
621 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
622 }
623
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300624 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700625 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300626 printed += fprintf(fp, "(");
627 printed += map__fprintf_dsoname(alf.map, fp);
628 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700629 }
630
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300631 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700632 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300633 printed += fprintf(fp, "(");
634 printed += map__fprintf_dsoname(alt.map, fp);
635 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700636 }
637
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300638 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200639 mispred_str( br->entries + i),
640 br->entries[i].flags.in_tx? 'X' : '-',
641 br->entries[i].flags.abort? 'A' : '-',
642 br->entries[i].flags.cycles);
643 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300644
645 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200646}
647
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300648static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
649 struct thread *thread,
650 struct perf_event_attr *attr, FILE *fp)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200651{
652 struct branch_stack *br = sample->branch_stack;
653 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200654 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300655 int printed = 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200656
657 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300658 return 0;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200659
660 for (i = 0; i < br->nr; i++) {
661
662 memset(&alf, 0, sizeof(alf));
663 memset(&alt, 0, sizeof(alt));
664 from = br->entries[i].from;
665 to = br->entries[i].to;
666
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300667 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200668 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300669 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200670
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300671 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200672 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300673 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200674
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300675 printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700676 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300677 printed += fprintf(fp, "(");
678 printed += map__fprintf_dsoname(alf.map, fp);
679 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700680 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300681 printed += fprintf(fp, "%c", '/');
682 printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700683 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300684 printed += fprintf(fp, "(");
685 printed += map__fprintf_dsoname(alt.map, fp);
686 printed += fprintf(fp, ")");
Mark Santaniello55b9b502017-06-19 09:38:24 -0700687 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300688 printed += fprintf(fp, "/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200689 mispred_str( br->entries + i),
690 br->entries[i].flags.in_tx? 'X' : '-',
691 br->entries[i].flags.abort? 'A' : '-',
692 br->entries[i].flags.cycles);
693 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300694
695 return printed;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200696}
697
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300698static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
699 struct thread *thread,
700 struct perf_event_attr *attr, FILE *fp)
Mark Santaniello106dacd2017-06-19 09:38:25 -0700701{
702 struct branch_stack *br = sample->branch_stack;
703 struct addr_location alf, alt;
704 u64 i, from, to;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300705 int printed = 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700706
707 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300708 return 0;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700709
710 for (i = 0; i < br->nr; i++) {
711
712 memset(&alf, 0, sizeof(alf));
713 memset(&alt, 0, sizeof(alt));
714 from = br->entries[i].from;
715 to = br->entries[i].to;
716
717 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
718 if (alf.map && !alf.map->dso->adjust_symbols)
719 from = map__map_ip(alf.map, from);
720
721 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
722 if (alt.map && !alt.map->dso->adjust_symbols)
723 to = map__map_ip(alt.map, to);
724
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300725 printed += fprintf(fp, " 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700726 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300727 printed += fprintf(fp, "(");
728 printed += map__fprintf_dsoname(alf.map, fp);
729 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700730 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300731 printed += fprintf(fp, "/0x%"PRIx64, to);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700732 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300733 printed += fprintf(fp, "(");
734 printed += map__fprintf_dsoname(alt.map, fp);
735 printed += fprintf(fp, ")");
Mark Santaniello106dacd2017-06-19 09:38:25 -0700736 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300737 printed += fprintf(fp, "/%c/%c/%c/%d ",
Mark Santaniello106dacd2017-06-19 09:38:25 -0700738 mispred_str(br->entries + i),
739 br->entries[i].flags.in_tx ? 'X' : '-',
740 br->entries[i].flags.abort ? 'A' : '-',
741 br->entries[i].flags.cycles);
742 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300743
744 return printed;
Mark Santaniello106dacd2017-06-19 09:38:25 -0700745}
Andi Kleen48d02a12017-02-23 15:46:34 -0800746#define MAXBB 16384UL
747
748static int grab_bb(u8 *buffer, u64 start, u64 end,
749 struct machine *machine, struct thread *thread,
750 bool *is64bit, u8 *cpumode, bool last)
751{
752 long offset, len;
753 struct addr_location al;
754 bool kernel;
755
756 if (!start || !end)
757 return 0;
758
759 kernel = machine__kernel_ip(machine, start);
760 if (kernel)
761 *cpumode = PERF_RECORD_MISC_KERNEL;
762 else
763 *cpumode = PERF_RECORD_MISC_USER;
764
765 /*
766 * Block overlaps between kernel and user.
767 * This can happen due to ring filtering
768 * On Intel CPUs the entry into the kernel is filtered,
769 * but the exit is not. Let the caller patch it up.
770 */
771 if (kernel != machine__kernel_ip(machine, end)) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300772 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800773 return -ENXIO;
774 }
775
776 memset(&al, 0, sizeof(al));
777 if (end - start > MAXBB - MAXINSN) {
778 if (last)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300779 pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800780 else
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300781 pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
Andi Kleen48d02a12017-02-23 15:46:34 -0800782 return 0;
783 }
784
785 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
786 if (!al.map || !al.map->dso) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300787 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800788 return 0;
789 }
790 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300791 pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
Andi Kleen48d02a12017-02-23 15:46:34 -0800792 return 0;
793 }
794
795 /* Load maps to ensure dso->is_64_bit has been updated */
796 map__load(al.map);
797
798 offset = al.map->map_ip(al.map, start);
799 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
800 end - start + MAXINSN);
801
802 *is64bit = al.map->dso->is_64_bit;
803 if (len <= 0)
Arnaldo Carvalho de Melo5ce2c5b2017-10-26 09:55:22 -0300804 pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
Andi Kleen48d02a12017-02-23 15:46:34 -0800805 start, end);
806 return len;
807}
808
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300809static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
810 struct perf_insn *x, u8 *inbuf, int len,
811 int insn, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800812{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300813 int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
814 dump_insn(x, ip, inbuf, len, NULL),
815 en->flags.predicted ? " PRED" : "",
816 en->flags.mispred ? " MISPRED" : "",
817 en->flags.in_tx ? " INTX" : "",
818 en->flags.abort ? " ABORT" : "");
Andi Kleen48d02a12017-02-23 15:46:34 -0800819 if (en->flags.cycles) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300820 printed += fprintf(fp, " %d cycles", en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800821 if (insn)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300822 printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
Andi Kleen48d02a12017-02-23 15:46:34 -0800823 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300824 return printed + fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800825}
826
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300827static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
828 u8 cpumode, int cpu, struct symbol **lastsym,
829 struct perf_event_attr *attr, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800830{
831 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300832 int off, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800833
834 memset(&al, 0, sizeof(al));
835
836 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
837 if (!al.map)
838 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
839 addr, &al);
840 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300841 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800842
843 al.cpu = cpu;
844 al.sym = NULL;
845 if (al.map)
846 al.sym = map__find_symbol(al.map, al.addr);
847
848 if (!al.sym)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300849 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800850
851 if (al.addr < al.sym->end)
852 off = al.addr - al.sym->start;
853 else
854 off = al.addr - al.map->start - al.sym->start;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300855 printed += fprintf(fp, "\t%s", al.sym->name);
Andi Kleen48d02a12017-02-23 15:46:34 -0800856 if (off)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300857 printed += fprintf(fp, "%+d", off);
858 printed += fprintf(fp, ":");
Andi Kleen48d02a12017-02-23 15:46:34 -0800859 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300860 printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
861 printed += fprintf(fp, "\n");
Andi Kleen48d02a12017-02-23 15:46:34 -0800862 *lastsym = al.sym;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300863
864 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800865}
866
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300867static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
868 struct thread *thread,
869 struct perf_event_attr *attr,
870 struct machine *machine, FILE *fp)
Andi Kleen48d02a12017-02-23 15:46:34 -0800871{
872 struct branch_stack *br = sample->branch_stack;
873 u64 start, end;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300874 int i, insn, len, nr, ilen, printed = 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800875 struct perf_insn x;
876 u8 buffer[MAXBB];
877 unsigned off;
878 struct symbol *lastsym = NULL;
879
880 if (!(br && br->nr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300881 return 0;
Andi Kleen48d02a12017-02-23 15:46:34 -0800882 nr = br->nr;
883 if (max_blocks && nr > max_blocks + 1)
884 nr = max_blocks + 1;
885
886 x.thread = thread;
887 x.cpu = sample->cpu;
888
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300889 printed += fprintf(fp, "%c", '\n');
Andi Kleen48d02a12017-02-23 15:46:34 -0800890
891 /* Handle first from jump, of which we don't know the entry. */
892 len = grab_bb(buffer, br->entries[nr-1].from,
893 br->entries[nr-1].from,
894 machine, thread, &x.is64bit, &x.cpumode, false);
895 if (len > 0) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300896 printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
897 x.cpumode, x.cpu, &lastsym, attr, fp);
898 printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
899 &x, buffer, len, 0, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800900 }
901
902 /* Print all blocks */
903 for (i = nr - 2; i >= 0; i--) {
904 if (br->entries[i].from || br->entries[i].to)
905 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
906 br->entries[i].from,
907 br->entries[i].to);
908 start = br->entries[i + 1].to;
909 end = br->entries[i].from;
910
911 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
912 /* Patch up missing kernel transfers due to ring filters */
913 if (len == -ENXIO && i > 0) {
914 end = br->entries[--i].from;
915 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
916 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
917 }
918 if (len <= 0)
919 continue;
920
921 insn = 0;
922 for (off = 0;; off += ilen) {
923 uint64_t ip = start + off;
924
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300925 printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800926 if (ip == end) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300927 printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800928 break;
929 } else {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300930 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
931 dump_insn(&x, ip, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -0800932 if (ilen == 0)
933 break;
934 insn++;
935 }
936 }
937 }
938
939 /*
940 * Hit the branch? In this case we are already done, and the target
941 * has not been executed yet.
942 */
943 if (br->entries[0].from == sample->ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300944 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800945 if (br->entries[0].flags.abort)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300946 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800947
948 /*
949 * Print final block upto sample
950 */
951 start = br->entries[0].to;
952 end = sample->ip;
953 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300954 printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
Andi Kleen48d02a12017-02-23 15:46:34 -0800955 if (len <= 0) {
956 /* Print at least last IP if basic block did not work */
957 len = grab_bb(buffer, sample->ip, sample->ip,
958 machine, thread, &x.is64bit, &x.cpumode, false);
959 if (len <= 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300960 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800961
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300962 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
Andi Kleen48d02a12017-02-23 15:46:34 -0800963 dump_insn(&x, sample->ip, buffer, len, NULL));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300964 goto out;
Andi Kleen48d02a12017-02-23 15:46:34 -0800965 }
966 for (off = 0; off <= end - start; off += ilen) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300967 printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
968 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
Andi Kleen48d02a12017-02-23 15:46:34 -0800969 if (ilen == 0)
970 break;
971 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300972out:
973 return printed;
Andi Kleen48d02a12017-02-23 15:46:34 -0800974}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200975
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300976static int perf_sample__fprintf_addr(struct perf_sample *sample,
977 struct thread *thread,
978 struct perf_event_attr *attr, FILE *fp)
David Ahern7cec0922011-05-30 13:08:23 -0600979{
980 struct addr_location al;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300981 int printed = fprintf(fp, "%16" PRIx64, sample->addr);
David Ahern7cec0922011-05-30 13:08:23 -0600982
983 if (!sample_addr_correlates_sym(attr))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300984 goto out;
David Ahern7cec0922011-05-30 13:08:23 -0600985
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300986 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600987
988 if (PRINT_FIELD(SYM)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300989 printed += fprintf(fp, " ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900990 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300991 printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900992 else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300993 printed += symbol__fprintf_symname(al.sym, fp);
David Ahern7cec0922011-05-30 13:08:23 -0600994 }
995
996 if (PRINT_FIELD(DSO)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -0300997 printed += fprintf(fp, " (");
998 printed += map__fprintf_dsoname(al.map, fp);
999 printed += fprintf(fp, ")");
David Ahern7cec0922011-05-30 13:08:23 -06001000 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001001out:
1002 return printed;
David Ahern7cec0922011-05-30 13:08:23 -06001003}
1004
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001005static int perf_sample__fprintf_callindent(struct perf_sample *sample,
1006 struct perf_evsel *evsel,
1007 struct thread *thread,
1008 struct addr_location *al, FILE *fp)
Adrian Huntere2167082016-06-23 16:40:58 +03001009{
1010 struct perf_event_attr *attr = &evsel->attr;
1011 size_t depth = thread_stack__depth(thread);
1012 struct addr_location addr_al;
1013 const char *name = NULL;
1014 static int spacing;
1015 int len = 0;
1016 u64 ip = 0;
1017
1018 /*
1019 * The 'return' has already been popped off the stack so the depth has
1020 * to be adjusted to match the 'call'.
1021 */
1022 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1023 depth += 1;
1024
1025 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1026 if (sample_addr_correlates_sym(attr)) {
1027 thread__resolve(thread, &addr_al, sample);
1028 if (addr_al.sym)
1029 name = addr_al.sym->name;
1030 else
1031 ip = sample->addr;
1032 } else {
1033 ip = sample->addr;
1034 }
1035 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1036 if (al->sym)
1037 name = al->sym->name;
1038 else
1039 ip = sample->ip;
1040 }
1041
1042 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001043 len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
Adrian Huntere2167082016-06-23 16:40:58 +03001044 else if (ip)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001045 len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
Adrian Huntere2167082016-06-23 16:40:58 +03001046
1047 if (len < 0)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001048 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001049
1050 /*
1051 * Try to keep the output length from changing frequently so that the
1052 * output lines up more nicely.
1053 */
1054 if (len > spacing || (len && len < spacing - 52))
1055 spacing = round_up(len + 4, 32);
1056
1057 if (len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001058 len += fprintf(fp, "%*s", spacing - len, "");
1059
1060 return len;
Adrian Huntere2167082016-06-23 16:40:58 +03001061}
1062
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001063static int perf_sample__fprintf_insn(struct perf_sample *sample,
1064 struct perf_event_attr *attr,
1065 struct thread *thread,
1066 struct machine *machine, FILE *fp)
Andi Kleen224e2c92016-10-07 16:42:27 +03001067{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001068 int printed = 0;
1069
Andi Kleen224e2c92016-10-07 16:42:27 +03001070 if (PRINT_FIELD(INSNLEN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001071 printed += fprintf(fp, " ilen: %d", sample->insn_len);
Andi Kleen224e2c92016-10-07 16:42:27 +03001072 if (PRINT_FIELD(INSN)) {
1073 int i;
1074
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001075 printed += fprintf(fp, " insn:");
Andi Kleen224e2c92016-10-07 16:42:27 +03001076 for (i = 0; i < sample->insn_len; i++)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001077 printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
Andi Kleen224e2c92016-10-07 16:42:27 +03001078 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001079 if (PRINT_FIELD(BRSTACKINSN))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001080 printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
1081
1082 return printed;
Andi Kleen224e2c92016-10-07 16:42:27 +03001083}
1084
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001085static int perf_sample__fprintf_bts(struct perf_sample *sample,
1086 struct perf_evsel *evsel,
1087 struct thread *thread,
1088 struct addr_location *al,
1089 struct machine *machine, FILE *fp)
Akihiro Nagai95582592012-01-30 13:43:09 +09001090{
1091 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001092 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001093 bool print_srcline_last = false;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001094 int printed = 0;
Akihiro Nagai95582592012-01-30 13:43:09 +09001095
Adrian Huntere2167082016-06-23 16:40:58 +03001096 if (PRINT_FIELD(CALLINDENT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001097 printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
Adrian Huntere2167082016-06-23 16:40:58 +03001098
Akihiro Nagai95582592012-01-30 13:43:09 +09001099 /* print branch_from information */
1100 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001101 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001102 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001103
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001104 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001105 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001106 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001107 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001108
1109 if (cursor == NULL) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001110 printed += fprintf(fp, " ");
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001111 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001112 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001113 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001114 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001115 } else
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001116 printed += fprintf(fp, "\n");
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001117
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001118 printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001119 }
1120
Akihiro Nagai95582592012-01-30 13:43:09 +09001121 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001122 if (PRINT_FIELD(ADDR) ||
1123 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001124 !output[type].user_set)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001125 printed += fprintf(fp, " => ");
1126 printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
Adrian Hunter578bea42014-07-22 16:17:16 +03001127 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001128
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001129 if (print_srcline_last)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001130 printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001131
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001132 printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
1133 return printed + fprintf(fp, "\n");
Akihiro Nagai95582592012-01-30 13:43:09 +09001134}
1135
Adrian Hunter055cd332016-06-23 16:40:56 +03001136static struct {
1137 u32 flags;
1138 const char *name;
1139} sample_flags[] = {
1140 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1141 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1142 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1143 {PERF_IP_FLAG_BRANCH, "jmp"},
1144 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1145 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1146 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1147 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1148 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1149 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1150 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1151 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1152 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1153 {0, NULL}
1154};
1155
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001156static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001157{
1158 const char *chars = PERF_IP_FLAG_CHARS;
1159 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001160 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1161 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001162 char str[33];
1163 int i, pos = 0;
1164
Adrian Hunter055cd332016-06-23 16:40:56 +03001165 for (i = 0; sample_flags[i].name ; i++) {
1166 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1167 name = sample_flags[i].name;
1168 break;
1169 }
1170 }
1171
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001172 for (i = 0; i < n; i++, flags >>= 1) {
1173 if (flags & 1)
1174 str[pos++] = chars[i];
1175 }
1176 for (; i < 32; i++, flags >>= 1) {
1177 if (flags & 1)
1178 str[pos++] = '?';
1179 }
1180 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001181
1182 if (name)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001183 return fprintf(fp, " %-7s%4s ", name, in_tx ? "(x)" : "");
1184
1185 return fprintf(fp, " %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001186}
1187
Wang Nan30372f02016-02-24 11:20:45 +00001188struct printer_data {
1189 int line_no;
1190 bool hit_nul;
1191 bool is_printable;
1192};
1193
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001194static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1195 unsigned int val,
1196 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001197{
1198 unsigned char ch = (unsigned char)val;
1199 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001200 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001201
1202 switch (op) {
1203 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001204 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001205 break;
1206 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001207 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001208 " ");
1209 break;
1210 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001211 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001212 break;
1213 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001214 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001215 break;
1216 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001217 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001218 break;
1219 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001220 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001221 break;
1222 case BINARY_PRINT_CHAR_DATA:
1223 if (printer_data->hit_nul && ch)
1224 printer_data->is_printable = false;
1225
1226 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001227 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001228
1229 if (!printer_data->is_printable)
1230 break;
1231
1232 if (ch == '\0')
1233 printer_data->hit_nul = true;
1234 else
1235 printer_data->is_printable = false;
1236 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001237 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001238 }
1239 break;
1240 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001241 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001242 break;
1243 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001244 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001245 printer_data->line_no++;
1246 break;
1247 case BINARY_PRINT_DATA_END:
1248 default:
1249 break;
1250 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001251
1252 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001253}
1254
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001255static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001256{
1257 unsigned int nr_bytes = sample->raw_size;
1258 struct printer_data printer_data = {0, false, true};
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001259 int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
1260 sample__fprintf_bpf_output, &printer_data, fp);
Wang Nan30372f02016-02-24 11:20:45 +00001261
1262 if (printer_data.is_printable && printer_data.hit_nul)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001263 printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
1264
1265 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001266}
1267
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001268static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001269{
1270 if (len > 0 && len < spacing)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001271 return fprintf(fp, "%*s", spacing - len, "");
1272
1273 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001274}
1275
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001276static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001277{
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001278 return perf_sample__fprintf_spacing(len, 34, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001279}
1280
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001281static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001282{
1283 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1284 int len;
1285
1286 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001287 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001288
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001289 len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001290 data->ip, le64_to_cpu(data->payload));
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001291 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001292}
1293
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001294static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001295{
1296 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1297 int len;
1298
1299 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001300 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001301
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001302 len = fprintf(fp, " hints: %#x extensions: %#x ",
1303 data->hints, data->extensions);
1304 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001305}
1306
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001307static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001308{
1309 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1310 int len;
1311
1312 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001313 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001314
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001315 len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
1316 data->hw, data->cstate, data->subcstate);
1317 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001318}
1319
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001320static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001321{
1322 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1323 int len;
1324
1325 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001326 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001327
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001328 len = fprintf(fp, " IP: %u ", data->ip);
1329 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001330}
1331
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001332static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001333{
1334 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1335 int len;
1336
1337 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001338 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001339
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001340 len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
Adrian Hunter65c5e182017-06-30 11:36:42 +03001341 data->deepest_cstate, data->last_cstate,
1342 data->wake_reason);
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001343 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001344}
1345
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001346static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
Adrian Hunter65c5e182017-06-30 11:36:42 +03001347{
1348 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1349 unsigned int percent, freq;
1350 int len;
1351
1352 if (perf_sample__bad_synth_size(sample, *data))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001353 return 0;
Adrian Hunter65c5e182017-06-30 11:36:42 +03001354
1355 freq = (le32_to_cpu(data->freq) + 500) / 1000;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001356 len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001357 if (data->max_nonturbo) {
1358 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001359 len += fprintf(fp, "(%3u%%) ", percent);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001360 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001361 return len + perf_sample__fprintf_pt_spacing(len, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001362}
1363
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001364static int perf_sample__fprintf_synth(struct perf_sample *sample,
1365 struct perf_evsel *evsel, FILE *fp)
Adrian Hunter47e78082017-05-26 11:17:22 +03001366{
1367 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001368 case PERF_SYNTH_INTEL_PTWRITE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001369 return perf_sample__fprintf_synth_ptwrite(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001370 case PERF_SYNTH_INTEL_MWAIT:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001371 return perf_sample__fprintf_synth_mwait(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001372 case PERF_SYNTH_INTEL_PWRE:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001373 return perf_sample__fprintf_synth_pwre(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001374 case PERF_SYNTH_INTEL_EXSTOP:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001375 return perf_sample__fprintf_synth_exstop(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001376 case PERF_SYNTH_INTEL_PWRX:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001377 return perf_sample__fprintf_synth_pwrx(sample, fp);
Adrian Hunter65c5e182017-06-30 11:36:42 +03001378 case PERF_SYNTH_INTEL_CBR:
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001379 return perf_sample__fprintf_synth_cbr(sample, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001380 default:
1381 break;
1382 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001383
1384 return 0;
Adrian Hunter47e78082017-05-26 11:17:22 +03001385}
1386
Jiri Olsa809e9422015-11-26 18:55:21 +01001387struct perf_script {
1388 struct perf_tool tool;
1389 struct perf_session *session;
1390 bool show_task_events;
1391 bool show_mmap_events;
1392 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301393 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001394 bool allocated;
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001395 bool per_event_dump;
Jiri Olsacfc88742016-01-05 22:09:06 +01001396 struct cpu_map *cpus;
1397 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001398 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001399 const char *time_str;
1400 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001401};
1402
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001403static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1404{
1405 struct perf_evsel *evsel;
1406 int max = 0;
1407
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001408 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001409 int len = strlen(perf_evsel__name(evsel));
1410
1411 max = MAX(len, max);
1412 }
1413
1414 return max;
1415}
1416
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001417static int data_src__fprintf(u64 data_src, FILE *fp)
Jiri Olsac19ac912016-02-24 09:46:54 +01001418{
1419 struct mem_info mi = { .data_src.val = data_src };
1420 char decode[100];
1421 char out[100];
1422 static int maxlen;
1423 int len;
1424
1425 perf_script__meminfo_scnprintf(decode, 100, &mi);
1426
1427 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1428 if (maxlen < len)
1429 maxlen = len;
1430
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001431 return fprintf(fp, "%-*s", maxlen, out);
Jiri Olsac19ac912016-02-24 09:46:54 +01001432}
1433
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001434static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001435 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001436 struct addr_location *al,
1437 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001438{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001439 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001440 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001441 unsigned int type = output_type(attr->type);
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001442 FILE *fp = evsel->priv;
David Ahern1424dc92011-03-09 22:23:28 -07001443
Adrian Hunter14057202017-06-21 13:17:19 +03001444 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001445 return;
1446
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001447 perf_sample__fprintf_start(sample, thread, evsel, fp);
David Ahern745f43e2011-03-09 22:23:26 -07001448
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001449 if (PRINT_FIELD(PERIOD))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001450 fprintf(fp, "%10" PRIu64 " ", sample->period);
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001451
Namhyung Kime944d3d2013-11-18 14:34:52 +09001452 if (PRINT_FIELD(EVNAME)) {
1453 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001454
1455 if (!script->name_width)
1456 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1457
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001458 fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001459 }
1460
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001461 if (print_flags)
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001462 perf_sample__fprintf_flags(sample->flags, fp);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001463
Akihiro Nagai95582592012-01-30 13:43:09 +09001464 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001465 perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
Akihiro Nagai95582592012-01-30 13:43:09 +09001466 return;
1467 }
1468
Arnaldo Carvalho de Melo894f3f12017-10-26 10:26:52 -03001469 if (PRINT_FIELD(TRACE)) {
1470 event_format__fprintf(evsel->tp_format, sample->cpu,
1471 sample->raw_data, sample->raw_size, fp);
1472 }
Adrian Hunter47e78082017-05-26 11:17:22 +03001473
1474 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001475 perf_sample__fprintf_synth(sample, evsel, fp);
Adrian Hunter47e78082017-05-26 11:17:22 +03001476
David Ahern7cec0922011-05-30 13:08:23 -06001477 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001478 perf_sample__fprintf_addr(sample, thread, attr, fp);
David Ahern7cec0922011-05-30 13:08:23 -06001479
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001480 if (PRINT_FIELD(DATA_SRC))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001481 data_src__fprintf(sample->data_src, fp);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001482
1483 if (PRINT_FIELD(WEIGHT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001484 fprintf(fp, "%16" PRIu64, sample->weight);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001485
David Ahern787bef12011-05-27 14:28:43 -06001486 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001487 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001488
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001489 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001490 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001491 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001492 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001493
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001494 fputc(cursor ? '\n' : ' ', fp);
1495 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
David Ahernc0230b22011-03-09 22:23:27 -07001496 }
1497
Stephane Eranianfc36f942015-08-31 18:41:10 +02001498 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001499 perf_sample__fprintf_iregs(sample, attr, fp);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001500
Andi Kleenb1491ac2017-09-05 11:40:57 -07001501 if (PRINT_FIELD(UREGS))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001502 perf_sample__fprintf_uregs(sample, attr, fp);
Andi Kleenb1491ac2017-09-05 11:40:57 -07001503
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001504 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001505 perf_sample__fprintf_brstack(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001506 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001507 perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001508 else if (PRINT_FIELD(BRSTACKOFF))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001509 perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001510
Wang Nan30372f02016-02-24 11:20:45 +00001511 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001512 perf_sample__fprintf_bpf_output(sample, fp);
1513 perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
Kan Liang49d58f02017-08-29 13:11:11 -04001514
1515 if (PRINT_FIELD(PHYS_ADDR))
Arnaldo Carvalho de Melo69c71252017-10-26 09:51:13 -03001516 fprintf(fp, "%16" PRIx64, sample->phys_addr);
1517 fprintf(fp, "\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001518}
1519
Tom Zanussi956ffd02009-11-25 01:15:46 -06001520static struct scripting_ops *scripting_ops;
1521
Jiri Olsa36e33c52016-01-06 11:49:56 +01001522static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1523{
1524 int nthreads = thread_map__nr(counter->threads);
1525 int ncpus = perf_evsel__nr_cpus(counter);
1526 int cpu, thread;
1527 static int header_printed;
1528
1529 if (counter->system_wide)
1530 nthreads = 1;
1531
1532 if (!header_printed) {
1533 printf("%3s %8s %15s %15s %15s %15s %s\n",
1534 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1535 header_printed = 1;
1536 }
1537
1538 for (thread = 0; thread < nthreads; thread++) {
1539 for (cpu = 0; cpu < ncpus; cpu++) {
1540 struct perf_counts_values *counts;
1541
1542 counts = perf_counts(counter->counts, cpu, thread);
1543
1544 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1545 counter->cpus->map[cpu],
1546 thread_map__pid(counter->threads, thread),
1547 counts->val,
1548 counts->ena,
1549 counts->run,
1550 tstamp,
1551 perf_evsel__name(counter));
1552 }
1553 }
1554}
1555
Jiri Olsae099eba2016-01-05 22:09:09 +01001556static void process_stat(struct perf_evsel *counter, u64 tstamp)
1557{
1558 if (scripting_ops && scripting_ops->process_stat)
1559 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001560 else
1561 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001562}
1563
1564static void process_stat_interval(u64 tstamp)
1565{
1566 if (scripting_ops && scripting_ops->process_stat_interval)
1567 scripting_ops->process_stat_interval(tstamp);
1568}
1569
Tom Zanussi956ffd02009-11-25 01:15:46 -06001570static void setup_scripting(void)
1571{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001572 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001573 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001574}
1575
Adrian Hunterd445dd22014-08-15 22:08:37 +03001576static int flush_scripting(void)
1577{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001578 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001579}
1580
Tom Zanussi956ffd02009-11-25 01:15:46 -06001581static int cleanup_scripting(void)
1582{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001583 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001584
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001585 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001586}
1587
Jiri Olsa809e9422015-11-26 18:55:21 +01001588static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001589 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001590 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001591 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001592 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001593{
Jiri Olsa809e9422015-11-26 18:55:21 +01001594 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001595 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001596
David Aherna91f4c42016-11-29 10:15:43 -07001597 if (perf_time__skip_sample(&scr->ptime, sample->time))
1598 return 0;
1599
David Ahern1424dc92011-03-09 22:23:28 -07001600 if (debug_mode) {
1601 if (sample->time < last_timestamp) {
1602 pr_err("Samples misordered, previous: %" PRIu64
1603 " this: %" PRIu64 "\n", last_timestamp,
1604 sample->time);
1605 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001606 }
David Ahern1424dc92011-03-09 22:23:28 -07001607 last_timestamp = sample->time;
1608 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001609 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001610
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001611 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001612 pr_err("problem processing %d event, skipping it.\n",
1613 event->header.type);
1614 return -1;
1615 }
1616
1617 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001618 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001619
Anton Blanchard5d67be92011-07-04 21:57:50 +10001620 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001621 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001622
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001623 if (scripting_ops)
1624 scripting_ops->process_event(event, sample, evsel, &al);
1625 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001626 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001627
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001628out_put:
1629 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001630 return 0;
1631}
1632
Adrian Hunter7ea95722013-11-01 15:51:30 +02001633static int process_attr(struct perf_tool *tool, union perf_event *event,
1634 struct perf_evlist **pevlist)
1635{
1636 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1637 struct perf_evlist *evlist;
1638 struct perf_evsel *evsel, *pos;
1639 int err;
1640
1641 err = perf_event__process_attr(tool, event, pevlist);
1642 if (err)
1643 return err;
1644
1645 evlist = *pevlist;
1646 evsel = perf_evlist__last(*pevlist);
1647
Adrian Hunter14057202017-06-21 13:17:19 +03001648 if (evsel->attr.type >= PERF_TYPE_MAX &&
1649 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001650 return 0;
1651
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001652 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001653 if (pos->attr.type == evsel->attr.type && pos != evsel)
1654 return 0;
1655 }
1656
1657 set_print_ip_opts(&evsel->attr);
1658
Jiri Olsad2b5a312015-10-16 12:41:25 +02001659 if (evsel->attr.sample_type)
1660 err = perf_evsel__check_attr(evsel, scr->session);
1661
1662 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001663}
1664
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001665static int process_comm_event(struct perf_tool *tool,
1666 union perf_event *event,
1667 struct perf_sample *sample,
1668 struct machine *machine)
1669{
1670 struct thread *thread;
1671 struct perf_script *script = container_of(tool, struct perf_script, tool);
1672 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001673 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001674 int ret = -1;
1675
1676 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1677 if (thread == NULL) {
1678 pr_debug("problem processing COMM event, skipping it.\n");
1679 return -1;
1680 }
1681
1682 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1683 goto out;
1684
1685 if (!evsel->attr.sample_id_all) {
1686 sample->cpu = 0;
1687 sample->time = 0;
1688 sample->tid = event->comm.tid;
1689 sample->pid = event->comm.pid;
1690 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001691 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001692 perf_event__fprintf(event, stdout);
1693 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001694out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001695 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001696 return ret;
1697}
1698
Hari Bathini96a44bb2017-03-08 02:12:06 +05301699static int process_namespaces_event(struct perf_tool *tool,
1700 union perf_event *event,
1701 struct perf_sample *sample,
1702 struct machine *machine)
1703{
1704 struct thread *thread;
1705 struct perf_script *script = container_of(tool, struct perf_script, tool);
1706 struct perf_session *session = script->session;
1707 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1708 int ret = -1;
1709
1710 thread = machine__findnew_thread(machine, event->namespaces.pid,
1711 event->namespaces.tid);
1712 if (thread == NULL) {
1713 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1714 return -1;
1715 }
1716
1717 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1718 goto out;
1719
1720 if (!evsel->attr.sample_id_all) {
1721 sample->cpu = 0;
1722 sample->time = 0;
1723 sample->tid = event->namespaces.tid;
1724 sample->pid = event->namespaces.pid;
1725 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001726 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Hari Bathini96a44bb2017-03-08 02:12:06 +05301727 perf_event__fprintf(event, stdout);
1728 ret = 0;
1729out:
1730 thread__put(thread);
1731 return ret;
1732}
1733
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001734static int process_fork_event(struct perf_tool *tool,
1735 union perf_event *event,
1736 struct perf_sample *sample,
1737 struct machine *machine)
1738{
1739 struct thread *thread;
1740 struct perf_script *script = container_of(tool, struct perf_script, tool);
1741 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001742 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001743
1744 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1745 return -1;
1746
1747 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1748 if (thread == NULL) {
1749 pr_debug("problem processing FORK event, skipping it.\n");
1750 return -1;
1751 }
1752
1753 if (!evsel->attr.sample_id_all) {
1754 sample->cpu = 0;
1755 sample->time = event->fork.time;
1756 sample->tid = event->fork.tid;
1757 sample->pid = event->fork.pid;
1758 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001759 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001760 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001761 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001762
1763 return 0;
1764}
1765static int process_exit_event(struct perf_tool *tool,
1766 union perf_event *event,
1767 struct perf_sample *sample,
1768 struct machine *machine)
1769{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001770 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001771 struct thread *thread;
1772 struct perf_script *script = container_of(tool, struct perf_script, tool);
1773 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001774 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001775
1776 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1777 if (thread == NULL) {
1778 pr_debug("problem processing EXIT event, skipping it.\n");
1779 return -1;
1780 }
1781
1782 if (!evsel->attr.sample_id_all) {
1783 sample->cpu = 0;
1784 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001785 sample->tid = event->fork.tid;
1786 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001787 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001788 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001789 perf_event__fprintf(event, stdout);
1790
1791 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001792 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001793
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001794 thread__put(thread);
1795 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001796}
1797
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001798static int process_mmap_event(struct perf_tool *tool,
1799 union perf_event *event,
1800 struct perf_sample *sample,
1801 struct machine *machine)
1802{
1803 struct thread *thread;
1804 struct perf_script *script = container_of(tool, struct perf_script, tool);
1805 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001806 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001807
1808 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1809 return -1;
1810
1811 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1812 if (thread == NULL) {
1813 pr_debug("problem processing MMAP event, skipping it.\n");
1814 return -1;
1815 }
1816
1817 if (!evsel->attr.sample_id_all) {
1818 sample->cpu = 0;
1819 sample->time = 0;
1820 sample->tid = event->mmap.tid;
1821 sample->pid = event->mmap.pid;
1822 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001823 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001824 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001825 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001826 return 0;
1827}
1828
1829static int process_mmap2_event(struct perf_tool *tool,
1830 union perf_event *event,
1831 struct perf_sample *sample,
1832 struct machine *machine)
1833{
1834 struct thread *thread;
1835 struct perf_script *script = container_of(tool, struct perf_script, tool);
1836 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001837 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001838
1839 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1840 return -1;
1841
1842 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1843 if (thread == NULL) {
1844 pr_debug("problem processing MMAP2 event, skipping it.\n");
1845 return -1;
1846 }
1847
1848 if (!evsel->attr.sample_id_all) {
1849 sample->cpu = 0;
1850 sample->time = 0;
1851 sample->tid = event->mmap2.tid;
1852 sample->pid = event->mmap2.pid;
1853 }
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001854 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001855 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001856 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001857 return 0;
1858}
1859
Adrian Hunter7c148982015-07-21 12:44:06 +03001860static int process_switch_event(struct perf_tool *tool,
1861 union perf_event *event,
1862 struct perf_sample *sample,
1863 struct machine *machine)
1864{
1865 struct thread *thread;
1866 struct perf_script *script = container_of(tool, struct perf_script, tool);
1867 struct perf_session *session = script->session;
1868 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1869
1870 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1871 return -1;
1872
1873 thread = machine__findnew_thread(machine, sample->pid,
1874 sample->tid);
1875 if (thread == NULL) {
1876 pr_debug("problem processing SWITCH event, skipping it.\n");
1877 return -1;
1878 }
1879
Arnaldo Carvalho de Meloa1a58702017-10-17 10:54:24 -03001880 perf_sample__fprintf_start(sample, thread, evsel, stdout);
Adrian Hunter7c148982015-07-21 12:44:06 +03001881 perf_event__fprintf(event, stdout);
1882 thread__put(thread);
1883 return 0;
1884}
1885
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001886static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001887{
1888 session_done = 1;
1889}
1890
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001891static void perf_script__fclose_per_event_dump(struct perf_script *script)
1892{
1893 struct perf_evlist *evlist = script->session->evlist;
1894 struct perf_evsel *evsel;
1895
1896 evlist__for_each_entry(evlist, evsel) {
1897 if (!evsel->priv)
1898 break;
1899 fclose(evsel->priv);
1900 evsel->priv = NULL;
1901 }
1902}
1903
1904static int perf_script__fopen_per_event_dump(struct perf_script *script)
1905{
1906 struct perf_evsel *evsel;
1907
1908 evlist__for_each_entry(script->session->evlist, evsel) {
1909 char filename[PATH_MAX];
1910 snprintf(filename, sizeof(filename), "%s.%s.dump",
1911 script->session->file->path, perf_evsel__name(evsel));
1912 evsel->priv = fopen(filename, "w");
1913 if (evsel->priv == NULL)
1914 goto out_err_fclose;
1915 }
1916
1917 return 0;
1918
1919out_err_fclose:
1920 perf_script__fclose_per_event_dump(script);
1921 return -1;
1922}
1923
1924static int perf_script__setup_per_event_dump(struct perf_script *script)
1925{
1926 struct perf_evsel *evsel;
1927
1928 if (script->per_event_dump)
1929 return perf_script__fopen_per_event_dump(script);
1930
1931 evlist__for_each_entry(script->session->evlist, evsel)
1932 evsel->priv = stdout;
1933
1934 return 0;
1935}
1936
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001937static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001938{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001939 int ret;
1940
Tom Zanussic239da32010-04-01 23:59:18 -05001941 signal(SIGINT, sig_handler);
1942
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001943 /* override event processing functions */
1944 if (script->show_task_events) {
1945 script->tool.comm = process_comm_event;
1946 script->tool.fork = process_fork_event;
1947 script->tool.exit = process_exit_event;
1948 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001949 if (script->show_mmap_events) {
1950 script->tool.mmap = process_mmap_event;
1951 script->tool.mmap2 = process_mmap2_event;
1952 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001953 if (script->show_switch_events)
1954 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301955 if (script->show_namespace_events)
1956 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001957
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001958 if (perf_script__setup_per_event_dump(script)) {
1959 pr_err("Couldn't create the per event dump files\n");
1960 return -1;
1961 }
1962
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001963 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001964
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03001965 if (script->per_event_dump)
1966 perf_script__fclose_per_event_dump(script);
1967
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001968 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001969 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001970
1971 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001972}
1973
Tom Zanussi956ffd02009-11-25 01:15:46 -06001974struct script_spec {
1975 struct list_head node;
1976 struct scripting_ops *ops;
1977 char spec[0];
1978};
1979
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001980static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001981
1982static struct script_spec *script_spec__new(const char *spec,
1983 struct scripting_ops *ops)
1984{
1985 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1986
1987 if (s != NULL) {
1988 strcpy(s->spec, spec);
1989 s->ops = ops;
1990 }
1991
1992 return s;
1993}
1994
Tom Zanussi956ffd02009-11-25 01:15:46 -06001995static void script_spec__add(struct script_spec *s)
1996{
1997 list_add_tail(&s->node, &script_specs);
1998}
1999
2000static struct script_spec *script_spec__find(const char *spec)
2001{
2002 struct script_spec *s;
2003
2004 list_for_each_entry(s, &script_specs, node)
2005 if (strcasecmp(s->spec, spec) == 0)
2006 return s;
2007 return NULL;
2008}
2009
Tom Zanussi956ffd02009-11-25 01:15:46 -06002010int script_spec_register(const char *spec, struct scripting_ops *ops)
2011{
2012 struct script_spec *s;
2013
2014 s = script_spec__find(spec);
2015 if (s)
2016 return -1;
2017
Taeung Song8560bae2016-02-26 00:13:10 +09002018 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002019 if (!s)
2020 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09002021 else
2022 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002023
2024 return 0;
2025}
2026
2027static struct scripting_ops *script_spec__lookup(const char *spec)
2028{
2029 struct script_spec *s = script_spec__find(spec);
2030 if (!s)
2031 return NULL;
2032
2033 return s->ops;
2034}
2035
2036static void list_available_languages(void)
2037{
2038 struct script_spec *s;
2039
2040 fprintf(stderr, "\n");
2041 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002042 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06002043
2044 list_for_each_entry(s, &script_specs, node)
2045 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
2046
2047 fprintf(stderr, "\n");
2048}
2049
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002050static int parse_scriptname(const struct option *opt __maybe_unused,
2051 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06002052{
2053 char spec[PATH_MAX];
2054 const char *script, *ext;
2055 int len;
2056
Tom Zanussif526d682010-01-27 02:27:52 -06002057 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06002058 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06002059 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002060 }
2061
2062 script = strchr(str, ':');
2063 if (script) {
2064 len = script - str;
2065 if (len >= PATH_MAX) {
2066 fprintf(stderr, "invalid language specifier");
2067 return -1;
2068 }
2069 strncpy(spec, str, len);
2070 spec[len] = '\0';
2071 scripting_ops = script_spec__lookup(spec);
2072 if (!scripting_ops) {
2073 fprintf(stderr, "invalid language specifier");
2074 return -1;
2075 }
2076 script++;
2077 } else {
2078 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01002079 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06002080 if (!ext) {
2081 fprintf(stderr, "invalid script extension");
2082 return -1;
2083 }
2084 scripting_ops = script_spec__lookup(++ext);
2085 if (!scripting_ops) {
2086 fprintf(stderr, "invalid script extension");
2087 return -1;
2088 }
2089 }
2090
2091 script_name = strdup(script);
2092
2093 return 0;
2094}
2095
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002096static int parse_output_fields(const struct option *opt __maybe_unused,
2097 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002098{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002099 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002100 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002101 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002102 int rc = 0;
2103 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002104 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002105 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002106
2107 if (!str)
2108 return -ENOMEM;
2109
David Ahern2c9e45f72011-03-17 10:03:21 -06002110 /* first word can state for which event type the user is specifying
2111 * the fields. If no type exists, the specified fields apply to all
2112 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002113 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002114 tok = strchr(str, ':');
2115 if (tok) {
2116 *tok = '\0';
2117 tok++;
2118 if (!strcmp(str, "hw"))
2119 type = PERF_TYPE_HARDWARE;
2120 else if (!strcmp(str, "sw"))
2121 type = PERF_TYPE_SOFTWARE;
2122 else if (!strcmp(str, "trace"))
2123 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002124 else if (!strcmp(str, "raw"))
2125 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002126 else if (!strcmp(str, "break"))
2127 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002128 else if (!strcmp(str, "synth"))
2129 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002130 else {
2131 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002132 rc = -EINVAL;
2133 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002134 }
2135
2136 if (output[type].user_set)
2137 pr_warning("Overriding previous field request for %s events.\n",
2138 event_type(type));
2139
2140 output[type].fields = 0;
2141 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002142 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002143
2144 } else {
2145 tok = str;
2146 if (strlen(str) == 0) {
2147 fprintf(stderr,
2148 "Cannot set fields to 'none' for all event types.\n");
2149 rc = -EINVAL;
2150 goto out;
2151 }
2152
Andi Kleen36ce5652017-06-02 08:48:10 -07002153 /* Don't override defaults for +- */
2154 if (strchr(str, '+') || strchr(str, '-'))
2155 goto parse;
2156
David Ahern2c9e45f72011-03-17 10:03:21 -06002157 if (output_set_by_user())
2158 pr_warning("Overriding previous field request for all events.\n");
2159
Adrian Hunter14057202017-06-21 13:17:19 +03002160 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002161 output[j].fields = 0;
2162 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002163 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002164 }
David Ahern1424dc92011-03-09 22:23:28 -07002165 }
2166
Andi Kleen36ce5652017-06-02 08:48:10 -07002167parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002168 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002169 if (*tok == '+') {
2170 if (change == SET)
2171 goto out_badmix;
2172 change = ADD;
2173 tok++;
2174 } else if (*tok == '-') {
2175 if (change == SET)
2176 goto out_badmix;
2177 change = REMOVE;
2178 tok++;
2179 } else {
2180 if (change != SET && change != DEFAULT)
2181 goto out_badmix;
2182 change = SET;
2183 }
2184
David Ahern745f43e2011-03-09 22:23:26 -07002185 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002186 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002187 break;
David Ahern745f43e2011-03-09 22:23:26 -07002188 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002189 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002190 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002191 continue;
2192 }
David Ahern745f43e2011-03-09 22:23:26 -07002193 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002194 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002195 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002196 goto out;
2197 }
2198
2199 if (type == -1) {
2200 /* add user option to all events types for
2201 * which it is valid
2202 */
Adrian Hunter14057202017-06-21 13:17:19 +03002203 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002204 if (output[j].invalid_fields & all_output_options[i].field) {
2205 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2206 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002207 } else {
2208 if (change == REMOVE)
2209 output[j].fields &= ~all_output_options[i].field;
2210 else
2211 output[j].fields |= all_output_options[i].field;
2212 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002213 }
2214 } else {
2215 if (output[type].invalid_fields & all_output_options[i].field) {
2216 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2217 all_output_options[i].str, event_type(type));
2218
2219 rc = -EINVAL;
2220 goto out;
2221 }
2222 output[type].fields |= all_output_options[i].field;
2223 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002224 }
2225
2226 if (type >= 0) {
2227 if (output[type].fields == 0) {
2228 pr_debug("No fields requested for %s type. "
2229 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002230 }
David Ahern1424dc92011-03-09 22:23:28 -07002231 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002232 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002233
Andi Kleen36ce5652017-06-02 08:48:10 -07002234out_badmix:
2235 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2236 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002237out:
David Ahern745f43e2011-03-09 22:23:26 -07002238 free(str);
2239 return rc;
2240}
2241
Shawn Bohrer008f29d2010-11-21 10:09:39 -06002242/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2243static int is_directory(const char *base_path, const struct dirent *dent)
2244{
2245 char path[PATH_MAX];
2246 struct stat st;
2247
2248 sprintf(path, "%s/%s", base_path, dent->d_name);
2249 if (stat(path, &st))
2250 return 0;
2251
2252 return S_ISDIR(st.st_mode);
2253}
2254
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002255#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2256 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2257 if ((lang_dirent->d_type == DT_DIR || \
2258 (lang_dirent->d_type == DT_UNKNOWN && \
2259 is_directory(scripts_path, lang_dirent))) && \
2260 (strcmp(lang_dirent->d_name, ".")) && \
2261 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002262
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002263#define for_each_script(lang_path, lang_dir, script_dirent) \
2264 while ((script_dirent = readdir(lang_dir)) != NULL) \
2265 if (script_dirent->d_type != DT_DIR && \
2266 (script_dirent->d_type != DT_UNKNOWN || \
2267 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002268
2269
2270#define RECORD_SUFFIX "-record"
2271#define REPORT_SUFFIX "-report"
2272
2273struct script_desc {
2274 struct list_head node;
2275 char *name;
2276 char *half_liner;
2277 char *args;
2278};
2279
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002280static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002281
2282static struct script_desc *script_desc__new(const char *name)
2283{
2284 struct script_desc *s = zalloc(sizeof(*s));
2285
Tom Zanussib5b87312010-11-10 08:16:51 -06002286 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002287 s->name = strdup(name);
2288
2289 return s;
2290}
2291
2292static void script_desc__delete(struct script_desc *s)
2293{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002294 zfree(&s->name);
2295 zfree(&s->half_liner);
2296 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002297 free(s);
2298}
2299
2300static void script_desc__add(struct script_desc *s)
2301{
2302 list_add_tail(&s->node, &script_descs);
2303}
2304
2305static struct script_desc *script_desc__find(const char *name)
2306{
2307 struct script_desc *s;
2308
2309 list_for_each_entry(s, &script_descs, node)
2310 if (strcasecmp(s->name, name) == 0)
2311 return s;
2312 return NULL;
2313}
2314
2315static struct script_desc *script_desc__findnew(const char *name)
2316{
2317 struct script_desc *s = script_desc__find(name);
2318
2319 if (s)
2320 return s;
2321
2322 s = script_desc__new(name);
2323 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002324 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002325
2326 script_desc__add(s);
2327
2328 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002329}
2330
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002331static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002332{
2333 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002334 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002335
2336 if (strlen(str) > suffix_len) {
2337 p = str + strlen(str) - suffix_len;
2338 if (!strncmp(p, suffix, suffix_len))
2339 return p;
2340 }
2341
2342 return NULL;
2343}
2344
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002345static int read_script_info(struct script_desc *desc, const char *filename)
2346{
2347 char line[BUFSIZ], *p;
2348 FILE *fp;
2349
2350 fp = fopen(filename, "r");
2351 if (!fp)
2352 return -1;
2353
2354 while (fgets(line, sizeof(line), fp)) {
2355 p = ltrim(line);
2356 if (strlen(p) == 0)
2357 continue;
2358 if (*p != '#')
2359 continue;
2360 p++;
2361 if (strlen(p) && *p == '!')
2362 continue;
2363
2364 p = ltrim(p);
2365 if (strlen(p) && p[strlen(p) - 1] == '\n')
2366 p[strlen(p) - 1] = '\0';
2367
2368 if (!strncmp(p, "description:", strlen("description:"))) {
2369 p += strlen("description:");
2370 desc->half_liner = strdup(ltrim(p));
2371 continue;
2372 }
2373
2374 if (!strncmp(p, "args:", strlen("args:"))) {
2375 p += strlen("args:");
2376 desc->args = strdup(ltrim(p));
2377 continue;
2378 }
2379 }
2380
2381 fclose(fp);
2382
2383 return 0;
2384}
2385
Robert Richter38efb532011-11-25 11:38:40 +01002386static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2387{
2388 char *script_root, *str;
2389
2390 script_root = strdup(script_dirent->d_name);
2391 if (!script_root)
2392 return NULL;
2393
2394 str = (char *)ends_with(script_root, suffix);
2395 if (!str) {
2396 free(script_root);
2397 return NULL;
2398 }
2399
2400 *str = '\0';
2401 return script_root;
2402}
2403
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002404static int list_available_scripts(const struct option *opt __maybe_unused,
2405 const char *s __maybe_unused,
2406 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002407{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002408 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002409 char scripts_path[MAXPATHLEN];
2410 DIR *scripts_dir, *lang_dir;
2411 char script_path[MAXPATHLEN];
2412 char lang_path[MAXPATHLEN];
2413 struct script_desc *desc;
2414 char first_half[BUFSIZ];
2415 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002416
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002417 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002418
2419 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002420 if (!scripts_dir) {
2421 fprintf(stdout,
2422 "open(%s) failed.\n"
2423 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2424 scripts_path);
2425 exit(-1);
2426 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002427
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002428 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002429 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002430 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002431 lang_dir = opendir(lang_path);
2432 if (!lang_dir)
2433 continue;
2434
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002435 for_each_script(lang_path, lang_dir, script_dirent) {
2436 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002437 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002438 desc = script_desc__findnew(script_root);
2439 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002440 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002441 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002442 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002443 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002444 }
2445 }
2446
2447 fprintf(stdout, "List of available trace scripts:\n");
2448 list_for_each_entry(desc, &script_descs, node) {
2449 sprintf(first_half, "%s %s", desc->name,
2450 desc->args ? desc->args : "");
2451 fprintf(stdout, " %-36s %s\n", first_half,
2452 desc->half_liner ? desc->half_liner : "");
2453 }
2454
2455 exit(0);
2456}
2457
Feng Tange5f37052012-09-07 16:42:26 +08002458/*
Feng Tang49e639e2012-10-30 11:56:03 +08002459 * Some scripts specify the required events in their "xxx-record" file,
2460 * this function will check if the events in perf.data match those
2461 * mentioned in the "xxx-record".
2462 *
2463 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2464 * which is covered well now. And new parsing code should be added to
2465 * cover the future complexing formats like event groups etc.
2466 */
2467static int check_ev_match(char *dir_name, char *scriptname,
2468 struct perf_session *session)
2469{
2470 char filename[MAXPATHLEN], evname[128];
2471 char line[BUFSIZ], *p;
2472 struct perf_evsel *pos;
2473 int match, len;
2474 FILE *fp;
2475
2476 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2477
2478 fp = fopen(filename, "r");
2479 if (!fp)
2480 return -1;
2481
2482 while (fgets(line, sizeof(line), fp)) {
2483 p = ltrim(line);
2484 if (*p == '#')
2485 continue;
2486
2487 while (strlen(p)) {
2488 p = strstr(p, "-e");
2489 if (!p)
2490 break;
2491
2492 p += 2;
2493 p = ltrim(p);
2494 len = strcspn(p, " \t");
2495 if (!len)
2496 break;
2497
2498 snprintf(evname, len + 1, "%s", p);
2499
2500 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002501 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002502 if (!strcmp(perf_evsel__name(pos), evname)) {
2503 match = 1;
2504 break;
2505 }
2506 }
2507
2508 if (!match) {
2509 fclose(fp);
2510 return -1;
2511 }
2512 }
2513 }
2514
2515 fclose(fp);
2516 return 0;
2517}
2518
2519/*
Feng Tange5f37052012-09-07 16:42:26 +08002520 * Return -1 if none is found, otherwise the actual scripts number.
2521 *
2522 * Currently the only user of this function is the script browser, which
2523 * will list all statically runnable scripts, select one, execute it and
2524 * show the output in a perf browser.
2525 */
2526int find_scripts(char **scripts_array, char **scripts_path_array)
2527{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002528 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002529 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002530 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002531 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002532 struct perf_data_file file = {
2533 .path = input_name,
2534 .mode = PERF_DATA_MODE_READ,
2535 };
Feng Tange5f37052012-09-07 16:42:26 +08002536 char *temp;
2537 int i = 0;
2538
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002539 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002540 if (!session)
2541 return -1;
2542
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002543 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002544
2545 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002546 if (!scripts_dir) {
2547 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002548 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002549 }
Feng Tange5f37052012-09-07 16:42:26 +08002550
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002551 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002552 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002553 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002554#ifdef NO_LIBPERL
2555 if (strstr(lang_path, "perl"))
2556 continue;
2557#endif
2558#ifdef NO_LIBPYTHON
2559 if (strstr(lang_path, "python"))
2560 continue;
2561#endif
2562
2563 lang_dir = opendir(lang_path);
2564 if (!lang_dir)
2565 continue;
2566
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002567 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002568 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002569 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002570 continue;
2571 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002572 script_dirent->d_name);
2573 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002574 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002575 (temp - script_dirent->d_name) + 1,
2576 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002577
2578 if (check_ev_match(lang_path,
2579 scripts_array[i], session))
2580 continue;
2581
Feng Tange5f37052012-09-07 16:42:26 +08002582 i++;
2583 }
Feng Tang49e639e2012-10-30 11:56:03 +08002584 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002585 }
2586
Feng Tang49e639e2012-10-30 11:56:03 +08002587 closedir(scripts_dir);
2588 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002589 return i;
2590}
2591
Tom Zanussi38752942009-12-15 02:53:39 -06002592static char *get_script_path(const char *script_root, const char *suffix)
2593{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002594 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002595 char scripts_path[MAXPATHLEN];
2596 char script_path[MAXPATHLEN];
2597 DIR *scripts_dir, *lang_dir;
2598 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002599 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002600
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002601 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002602
2603 scripts_dir = opendir(scripts_path);
2604 if (!scripts_dir)
2605 return NULL;
2606
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002607 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002608 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002609 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002610 lang_dir = opendir(lang_path);
2611 if (!lang_dir)
2612 continue;
2613
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002614 for_each_script(lang_path, lang_dir, script_dirent) {
2615 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002616 if (__script_root && !strcmp(script_root, __script_root)) {
2617 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002618 closedir(lang_dir);
2619 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002620 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002621 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002622 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002623 }
2624 free(__script_root);
2625 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002626 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002627 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002628 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002629
Robert Richter38efb532011-11-25 11:38:40 +01002630 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002631}
2632
Tom Zanussib5b87312010-11-10 08:16:51 -06002633static bool is_top_script(const char *script_path)
2634{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002635 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002636}
2637
2638static int has_required_arg(char *script_path)
2639{
2640 struct script_desc *desc;
2641 int n_args = 0;
2642 char *p;
2643
2644 desc = script_desc__new(NULL);
2645
2646 if (read_script_info(desc, script_path))
2647 goto out;
2648
2649 if (!desc->args)
2650 goto out;
2651
2652 for (p = desc->args; *p; p++)
2653 if (*p == '<')
2654 n_args++;
2655out:
2656 script_desc__delete(desc);
2657
2658 return n_args;
2659}
2660
David Ahernd54b1a92012-08-26 12:24:46 -06002661static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002662{
2663 char **__argv = malloc(sizeof(const char *) * argc);
2664
David Ahernd54b1a92012-08-26 12:24:46 -06002665 if (!__argv) {
2666 pr_err("malloc failed\n");
2667 return -1;
2668 }
2669
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002670 memcpy(__argv, argv, sizeof(const char *) * argc);
2671 argc = parse_options(argc, (const char **)__argv, record_options,
2672 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2673 free(__argv);
2674
David Ahernd54b1a92012-08-26 12:24:46 -06002675 system_wide = (argc == 0);
2676
2677 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002678}
2679
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002680static void script__setup_sample_type(struct perf_script *script)
2681{
2682 struct perf_session *session = script->session;
2683 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2684
2685 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2686 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2687 (sample_type & PERF_SAMPLE_STACK_USER))
2688 callchain_param.record_mode = CALLCHAIN_DWARF;
2689 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2690 callchain_param.record_mode = CALLCHAIN_LBR;
2691 else
2692 callchain_param.record_mode = CALLCHAIN_FP;
2693 }
2694}
2695
Jiri Olsae099eba2016-01-05 22:09:09 +01002696static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2697 union perf_event *event,
2698 struct perf_session *session)
2699{
2700 struct stat_round_event *round = &event->stat_round;
2701 struct perf_evsel *counter;
2702
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002703 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002704 perf_stat_process_counter(&stat_config, counter);
2705 process_stat(counter, round->time);
2706 }
2707
2708 process_stat_interval(round->time);
2709 return 0;
2710}
2711
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002712static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2713 union perf_event *event,
2714 struct perf_session *session __maybe_unused)
2715{
2716 perf_event__read_stat_config(&stat_config, &event->stat_config);
2717 return 0;
2718}
2719
Jiri Olsacfc88742016-01-05 22:09:06 +01002720static int set_maps(struct perf_script *script)
2721{
2722 struct perf_evlist *evlist = script->session->evlist;
2723
2724 if (!script->cpus || !script->threads)
2725 return 0;
2726
2727 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2728 return -EINVAL;
2729
2730 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2731
2732 if (perf_evlist__alloc_stats(evlist, true))
2733 return -ENOMEM;
2734
2735 script->allocated = true;
2736 return 0;
2737}
2738
2739static
2740int process_thread_map_event(struct perf_tool *tool,
2741 union perf_event *event,
2742 struct perf_session *session __maybe_unused)
2743{
2744 struct perf_script *script = container_of(tool, struct perf_script, tool);
2745
2746 if (script->threads) {
2747 pr_warning("Extra thread map event, ignoring.\n");
2748 return 0;
2749 }
2750
2751 script->threads = thread_map__new_event(&event->thread_map);
2752 if (!script->threads)
2753 return -ENOMEM;
2754
2755 return set_maps(script);
2756}
2757
2758static
2759int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2760 union perf_event *event,
2761 struct perf_session *session __maybe_unused)
2762{
2763 struct perf_script *script = container_of(tool, struct perf_script, tool);
2764
2765 if (script->cpus) {
2766 pr_warning("Extra cpu map event, ignoring.\n");
2767 return 0;
2768 }
2769
2770 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2771 if (!script->cpus)
2772 return -ENOMEM;
2773
2774 return set_maps(script);
2775}
2776
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002777int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002778{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002779 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002780 bool header = false;
2781 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002782 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002783 char *rec_script_path = NULL;
2784 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002785 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002786 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002787 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002788 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002789 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002790 struct perf_script script = {
2791 .tool = {
2792 .sample = process_sample_event,
2793 .mmap = perf_event__process_mmap,
2794 .mmap2 = perf_event__process_mmap2,
2795 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302796 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002797 .exit = perf_event__process_exit,
2798 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002799 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002800 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002801 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002802 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002803 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002804 .id_index = perf_event__process_id_index,
2805 .auxtrace_info = perf_event__process_auxtrace_info,
2806 .auxtrace = perf_event__process_auxtrace,
2807 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002808 .stat = perf_event__process_stat_event,
2809 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002810 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002811 .thread_map = process_thread_map_event,
2812 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002813 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002814 .ordering_requires_timestamps = true,
2815 },
2816 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002817 struct perf_data_file file = {
2818 .mode = PERF_DATA_MODE_READ,
2819 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002820 const struct option options[] = {
2821 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2822 "dump raw trace in ASCII"),
2823 OPT_INCR('v', "verbose", &verbose,
2824 "be more verbose (show symbol address, etc)"),
2825 OPT_BOOLEAN('L', "Latency", &latency_format,
2826 "show latency attributes (irqs/preemption disabled, etc)"),
2827 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2828 list_available_scripts),
2829 OPT_CALLBACK('s', "script", NULL, "name",
2830 "script file name (lang:script name, script name, or *)",
2831 parse_scriptname),
2832 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2833 "generate perf-script.xx script in specified language"),
2834 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2835 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2836 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002837 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2838 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002839 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2840 "file", "vmlinux pathname"),
2841 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2842 "file", "kallsyms pathname"),
2843 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2844 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002845 OPT_CALLBACK(0, "symfs", NULL, "directory",
2846 "Look for files with symbols relative to this directory",
2847 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002848 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002849 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002850 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03002851 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002852 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07002853 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04002854 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08002855 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002856 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2857 "system-wide collection from all CPUs"),
2858 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2859 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002860 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2861 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002862 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2863 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2864 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002865 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2866 "only consider symbols in these pids"),
2867 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2868 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002869 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2870 "Set the maximum stack depth when parsing the callchain, "
2871 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002872 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002873 OPT_BOOLEAN('I', "show-info", &show_full_info,
2874 "display extended information from perf.data file"),
2875 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2876 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002877 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2878 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002879 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2880 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002881 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2882 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302883 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2884 "Show namespace events (if recorded)"),
Arnaldo Carvalho de Meloa14390f2017-10-26 10:30:20 -03002885 OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
2886 "Dump trace output to files named by the monitored events"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002887 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002888 OPT_INTEGER(0, "max-blocks", &max_blocks,
2889 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002890 OPT_BOOLEAN(0, "ns", &nanosecs,
2891 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002892 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2893 "Instruction Tracing options",
2894 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002895 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2896 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002897 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2898 "Enable symbol demangling"),
2899 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2900 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002901 OPT_STRING(0, "time", &script.time_str, "str",
2902 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002903 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2904 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002905 OPT_END()
2906 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002907 const char * const script_subcommands[] = { "record", "report", NULL };
2908 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002909 "perf script [<options>]",
2910 "perf script [<options>] record <script> [<record-options>] <command>",
2911 "perf script [<options>] report <script> [script-args]",
2912 "perf script [<options>] <script> [<record-options>] <command>",
2913 "perf script [<options>] <top-script> [script-args]",
2914 NULL
2915 };
Tom Zanussi38752942009-12-15 02:53:39 -06002916
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03002917 perf_set_singlethreaded();
2918
Tom Zanussib5b87312010-11-10 08:16:51 -06002919 setup_scripting();
2920
Yunlong Song40cae2b2015-03-18 21:35:54 +08002921 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002922 PARSE_OPT_STOP_AT_NON_OPTION);
2923
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002924 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002925 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002926
Tom Zanussib5b87312010-11-10 08:16:51 -06002927 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2928 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2929 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002930 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002931 }
2932
Tom Zanussib5b87312010-11-10 08:16:51 -06002933 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2934 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2935 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002936 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002937 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002938 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002939 return -1;
2940 }
Tom Zanussi38752942009-12-15 02:53:39 -06002941 }
2942
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002943 if (itrace_synth_opts.callchain &&
2944 itrace_synth_opts.callchain_sz > scripting_max_stack)
2945 scripting_max_stack = itrace_synth_opts.callchain_sz;
2946
Ben Hutchings44e668c2010-10-10 16:10:03 +01002947 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002948 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002949
Tom Zanussib5b87312010-11-10 08:16:51 -06002950 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002951 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002952 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002953 pid_t pid;
2954
Tom Zanussib5b87312010-11-10 08:16:51 -06002955 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2956 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2957
2958 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002959 usage_with_options_msg(script_usage, options,
2960 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002961 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002962 }
2963
Tom Zanussib5b87312010-11-10 08:16:51 -06002964 if (is_top_script(argv[0])) {
2965 rep_args = argc - 1;
2966 } else {
2967 int rec_args;
2968
2969 rep_args = has_required_arg(rep_script_path);
2970 rec_args = (argc - 1) - rep_args;
2971 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002972 usage_with_options_msg(script_usage, options,
2973 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002974 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002975 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002976 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002977 }
2978
2979 if (pipe(live_pipe) < 0) {
2980 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002981 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002982 }
2983
2984 pid = fork();
2985 if (pid < 0) {
2986 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002987 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002988 }
2989
2990 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002991 j = 0;
2992
Tom Zanussia0cccc22010-04-01 23:59:25 -05002993 dup2(live_pipe[1], 1);
2994 close(live_pipe[0]);
2995
Robert Richter317df652011-11-25 15:05:25 +01002996 if (is_top_script(argv[0])) {
2997 system_wide = true;
2998 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002999 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
3000 err = -1;
3001 goto out;
3002 }
Robert Richter317df652011-11-25 15:05:25 +01003003 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003004
3005 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003006 if (!__argv) {
3007 pr_err("malloc failed\n");
3008 err = -ENOMEM;
3009 goto out;
3010 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06003011
Tom Zanussib5b87312010-11-10 08:16:51 -06003012 __argv[j++] = "/bin/sh";
3013 __argv[j++] = rec_script_path;
3014 if (system_wide)
3015 __argv[j++] = "-a";
3016 __argv[j++] = "-q";
3017 __argv[j++] = "-o";
3018 __argv[j++] = "-";
3019 for (i = rep_args + 1; i < argc; i++)
3020 __argv[j++] = argv[i];
3021 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05003022
3023 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003024 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05003025 exit(-1);
3026 }
3027
3028 dup2(live_pipe[0], 0);
3029 close(live_pipe[1]);
3030
Tom Zanussib5b87312010-11-10 08:16:51 -06003031 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003032 if (!__argv) {
3033 pr_err("malloc failed\n");
3034 err = -ENOMEM;
3035 goto out;
3036 }
3037
Tom Zanussib5b87312010-11-10 08:16:51 -06003038 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003039 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06003040 __argv[j++] = rep_script_path;
3041 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003042 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06003043 __argv[j++] = "-i";
3044 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06003045 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06003046
3047 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06003048 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06003049 exit(-1);
3050 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06003051
Tom Zanussib5b87312010-11-10 08:16:51 -06003052 if (rec_script_path)
3053 script_path = rec_script_path;
3054 if (rep_script_path)
3055 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003056
Tom Zanussib5b87312010-11-10 08:16:51 -06003057 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06003058 j = 0;
3059
Robert Richter317df652011-11-25 15:05:25 +01003060 if (!rec_script_path)
3061 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06003062 else if (!system_wide) {
3063 if (have_cmd(argc - 1, &argv[1]) != 0) {
3064 err = -1;
3065 goto out;
3066 }
3067 }
Tom Zanussib5b87312010-11-10 08:16:51 -06003068
3069 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06003070 if (!__argv) {
3071 pr_err("malloc failed\n");
3072 err = -ENOMEM;
3073 goto out;
3074 }
3075
Tom Zanussib5b87312010-11-10 08:16:51 -06003076 __argv[j++] = "/bin/sh";
3077 __argv[j++] = script_path;
3078 if (system_wide)
3079 __argv[j++] = "-a";
3080 for (i = 2; i < argc; i++)
3081 __argv[j++] = argv[i];
3082 __argv[j++] = NULL;
3083
3084 execvp("/bin/sh", (char **)__argv);
3085 free(__argv);
3086 exit(-1);
3087 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003088
Tom Zanussicf4fee52010-03-03 01:04:33 -06003089 if (!script_name)
3090 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003091
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003092 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003093 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003094 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003095
Jiri Olsae90debd2013-12-09 11:02:50 +01003096 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003097 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003098 perf_session__fprintf_info(session, stdout, show_full_info);
3099 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003100 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003101 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003102 if (show_full_info)
3103 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003104
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003105 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003106 goto out_delete;
3107
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003108 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003109 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003110
Adrian Huntere2167082016-06-23 16:40:58 +03003111 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3112 itrace_synth_opts.thread_stack = true;
3113
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003114 session->itrace_synth_opts = &itrace_synth_opts;
3115
Anton Blanchard5d67be92011-07-04 21:57:50 +10003116 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003117 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3118 if (err < 0)
3119 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003120 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003121 }
3122
David Ahern1424dc92011-03-09 22:23:28 -07003123 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003124 symbol_conf.use_callchain = true;
3125 else
3126 symbol_conf.use_callchain = false;
3127
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003128 if (session->tevent.pevent &&
3129 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003130 machine__resolve_kernel_addr,
3131 &session->machines.host) < 0) {
3132 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003133 err = -1;
3134 goto out_delete;
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003135 }
3136
Tom Zanussi956ffd02009-11-25 01:15:46 -06003137 if (generate_script_lang) {
3138 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003139 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003140
David Ahern2c9e45f72011-03-17 10:03:21 -06003141 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003142 fprintf(stderr,
3143 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003144 err = -EINVAL;
3145 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003146 }
3147
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003148 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003149 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003150 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003151 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003152 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003153 }
3154
3155 err = fstat(input, &perf_stat);
3156 if (err < 0) {
3157 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003158 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003159 }
3160
3161 if (!perf_stat.st_size) {
3162 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003163 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003164 }
3165
3166 scripting_ops = script_spec__lookup(generate_script_lang);
3167 if (!scripting_ops) {
3168 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003169 err = -ENOENT;
3170 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003171 }
3172
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003173 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003174 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003175 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003176 }
3177
3178 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003179 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003180 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003181 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003182 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003183 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003184 }
3185
David Ahern9cbdb702011-04-06 21:54:20 -06003186
3187 err = perf_session__check_output_opt(session);
3188 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003189 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003190
David Aherna91f4c42016-11-29 10:15:43 -07003191 /* needs to be parsed after looking up reference time */
3192 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3193 pr_err("Invalid time string\n");
Christophe JAILLETdb49bc12017-09-16 08:25:37 +02003194 err = -EINVAL;
3195 goto out_delete;
David Aherna91f4c42016-11-29 10:15:43 -07003196 }
3197
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003198 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003199
Adrian Hunterd445dd22014-08-15 22:08:37 +03003200 flush_scripting();
3201
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003202out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003203 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003204 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003205
3206 if (script_started)
3207 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003208out:
3209 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003210}