blob: 5a7dfc5691a171926e97d18ee5f9fa9c880aa9a0 [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 Meloa3dff302016-03-23 11:55:24 -0300503static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200504 struct perf_event_attr *attr)
505{
506 struct regs_dump *regs = &sample->intr_regs;
507 uint64_t mask = attr->sample_regs_intr;
508 unsigned i = 0, r;
509
510 if (!regs)
511 return;
512
513 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
514 u64 val = regs->regs[i++];
515 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
516 }
517}
518
Andi Kleenb1491ac2017-09-05 11:40:57 -0700519static void print_sample_uregs(struct perf_sample *sample,
520 struct perf_event_attr *attr)
521{
522 struct regs_dump *regs = &sample->user_regs;
523 uint64_t mask = attr->sample_regs_user;
524 unsigned i = 0, r;
525
526 if (!regs || !regs->regs)
527 return;
528
529 printf(" ABI:%" PRIu64 " ", regs->abi);
530
531 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
532 u64 val = regs->regs[i++];
533 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
534 }
535}
536
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300537static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700538 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300539 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700540{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300541 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700542 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700543 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700544
David Ahern745f43e2011-03-09 22:23:26 -0700545 if (PRINT_FIELD(COMM)) {
546 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200547 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600548 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200549 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700550 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200551 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700552 }
David Ahernc70c94b2011-03-09 22:23:25 -0700553
David Ahern745f43e2011-03-09 22:23:26 -0700554 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
555 printf("%5d/%-5d ", sample->pid, sample->tid);
556 else if (PRINT_FIELD(PID))
557 printf("%5d ", sample->pid);
558 else if (PRINT_FIELD(TID))
559 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700560
David Ahern745f43e2011-03-09 22:23:26 -0700561 if (PRINT_FIELD(CPU)) {
562 if (latency_format)
563 printf("%3d ", sample->cpu);
564 else
565 printf("[%03d] ", sample->cpu);
566 }
David Ahernc70c94b2011-03-09 22:23:25 -0700567
David Ahern745f43e2011-03-09 22:23:26 -0700568 if (PRINT_FIELD(TIME)) {
569 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300570 secs = nsecs / NSEC_PER_SEC;
571 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900572
Adrian Hunter83e19862015-09-25 16:15:36 +0300573 if (nanosecs)
574 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900575 else {
576 char sample_time[32];
577 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
578 printf("%12s: ", sample_time);
579 }
David Ahern745f43e2011-03-09 22:23:26 -0700580 }
David Ahernc70c94b2011-03-09 22:23:25 -0700581}
582
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200583static inline char
584mispred_str(struct branch_entry *br)
585{
586 if (!(br->flags.mispred || br->flags.predicted))
587 return '-';
588
589 return br->flags.predicted ? 'P' : 'M';
590}
591
Mark Santaniello55b9b502017-06-19 09:38:24 -0700592static void print_sample_brstack(struct perf_sample *sample,
593 struct thread *thread,
594 struct perf_event_attr *attr)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200595{
596 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700597 struct addr_location alf, alt;
598 u64 i, from, to;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200599
600 if (!(br && br->nr))
601 return;
602
603 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700604 from = br->entries[i].from;
605 to = br->entries[i].to;
606
607 if (PRINT_FIELD(DSO)) {
608 memset(&alf, 0, sizeof(alf));
609 memset(&alt, 0, sizeof(alt));
610 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
611 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
612 }
613
Mark Santanielloe9516c02017-10-06 01:07:22 -0700614 printf(" 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700615 if (PRINT_FIELD(DSO)) {
616 printf("(");
617 map__fprintf_dsoname(alf.map, stdout);
618 printf(")");
619 }
620
621 printf("/0x%"PRIx64, to);
622 if (PRINT_FIELD(DSO)) {
623 printf("(");
624 map__fprintf_dsoname(alt.map, stdout);
625 printf(")");
626 }
627
628 printf("/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200629 mispred_str( br->entries + i),
630 br->entries[i].flags.in_tx? 'X' : '-',
631 br->entries[i].flags.abort? 'A' : '-',
632 br->entries[i].flags.cycles);
633 }
634}
635
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300636static void print_sample_brstacksym(struct perf_sample *sample,
Mark Santaniello55b9b502017-06-19 09:38:24 -0700637 struct thread *thread,
638 struct perf_event_attr *attr)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200639{
640 struct branch_stack *br = sample->branch_stack;
641 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200642 u64 i, from, to;
643
644 if (!(br && br->nr))
645 return;
646
647 for (i = 0; i < br->nr; i++) {
648
649 memset(&alf, 0, sizeof(alf));
650 memset(&alt, 0, sizeof(alt));
651 from = br->entries[i].from;
652 to = br->entries[i].to;
653
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300654 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200655 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300656 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200657
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300658 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200659 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300660 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200661
662 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700663 if (PRINT_FIELD(DSO)) {
664 printf("(");
665 map__fprintf_dsoname(alf.map, stdout);
666 printf(")");
667 }
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200668 putchar('/');
669 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700670 if (PRINT_FIELD(DSO)) {
671 printf("(");
672 map__fprintf_dsoname(alt.map, stdout);
673 printf(")");
674 }
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200675 printf("/%c/%c/%c/%d ",
676 mispred_str( br->entries + i),
677 br->entries[i].flags.in_tx? 'X' : '-',
678 br->entries[i].flags.abort? 'A' : '-',
679 br->entries[i].flags.cycles);
680 }
681}
682
Mark Santaniello106dacd2017-06-19 09:38:25 -0700683static void print_sample_brstackoff(struct perf_sample *sample,
684 struct thread *thread,
685 struct perf_event_attr *attr)
686{
687 struct branch_stack *br = sample->branch_stack;
688 struct addr_location alf, alt;
689 u64 i, from, to;
690
691 if (!(br && br->nr))
692 return;
693
694 for (i = 0; i < br->nr; i++) {
695
696 memset(&alf, 0, sizeof(alf));
697 memset(&alt, 0, sizeof(alt));
698 from = br->entries[i].from;
699 to = br->entries[i].to;
700
701 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
702 if (alf.map && !alf.map->dso->adjust_symbols)
703 from = map__map_ip(alf.map, from);
704
705 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
706 if (alt.map && !alt.map->dso->adjust_symbols)
707 to = map__map_ip(alt.map, to);
708
Mark Santanielloe9516c02017-10-06 01:07:22 -0700709 printf(" 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700710 if (PRINT_FIELD(DSO)) {
711 printf("(");
712 map__fprintf_dsoname(alf.map, stdout);
713 printf(")");
714 }
715 printf("/0x%"PRIx64, to);
716 if (PRINT_FIELD(DSO)) {
717 printf("(");
718 map__fprintf_dsoname(alt.map, stdout);
719 printf(")");
720 }
721 printf("/%c/%c/%c/%d ",
722 mispred_str(br->entries + i),
723 br->entries[i].flags.in_tx ? 'X' : '-',
724 br->entries[i].flags.abort ? 'A' : '-',
725 br->entries[i].flags.cycles);
726 }
727}
Andi Kleen48d02a12017-02-23 15:46:34 -0800728#define MAXBB 16384UL
729
730static int grab_bb(u8 *buffer, u64 start, u64 end,
731 struct machine *machine, struct thread *thread,
732 bool *is64bit, u8 *cpumode, bool last)
733{
734 long offset, len;
735 struct addr_location al;
736 bool kernel;
737
738 if (!start || !end)
739 return 0;
740
741 kernel = machine__kernel_ip(machine, start);
742 if (kernel)
743 *cpumode = PERF_RECORD_MISC_KERNEL;
744 else
745 *cpumode = PERF_RECORD_MISC_USER;
746
747 /*
748 * Block overlaps between kernel and user.
749 * This can happen due to ring filtering
750 * On Intel CPUs the entry into the kernel is filtered,
751 * but the exit is not. Let the caller patch it up.
752 */
753 if (kernel != machine__kernel_ip(machine, end)) {
754 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
755 start, end);
756 return -ENXIO;
757 }
758
759 memset(&al, 0, sizeof(al));
760 if (end - start > MAXBB - MAXINSN) {
761 if (last)
762 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
763 else
764 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
765 return 0;
766 }
767
768 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
769 if (!al.map || !al.map->dso) {
770 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
771 return 0;
772 }
773 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
774 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
775 return 0;
776 }
777
778 /* Load maps to ensure dso->is_64_bit has been updated */
779 map__load(al.map);
780
781 offset = al.map->map_ip(al.map, start);
782 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
783 end - start + MAXINSN);
784
785 *is64bit = al.map->dso->is_64_bit;
786 if (len <= 0)
787 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
788 start, end);
789 return len;
790}
791
792static void print_jump(uint64_t ip, struct branch_entry *en,
793 struct perf_insn *x, u8 *inbuf, int len,
794 int insn)
795{
796 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
797 ip,
798 dump_insn(x, ip, inbuf, len, NULL),
799 en->flags.predicted ? " PRED" : "",
800 en->flags.mispred ? " MISPRED" : "",
801 en->flags.in_tx ? " INTX" : "",
802 en->flags.abort ? " ABORT" : "");
803 if (en->flags.cycles) {
804 printf(" %d cycles", en->flags.cycles);
805 if (insn)
806 printf(" %.2f IPC", (float)insn / en->flags.cycles);
807 }
808 putchar('\n');
809}
810
811static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
812 uint64_t addr, struct symbol **lastsym,
813 struct perf_event_attr *attr)
814{
815 struct addr_location al;
816 int off;
817
818 memset(&al, 0, sizeof(al));
819
820 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
821 if (!al.map)
822 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
823 addr, &al);
824 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
825 return;
826
827 al.cpu = cpu;
828 al.sym = NULL;
829 if (al.map)
830 al.sym = map__find_symbol(al.map, al.addr);
831
832 if (!al.sym)
833 return;
834
835 if (al.addr < al.sym->end)
836 off = al.addr - al.sym->start;
837 else
838 off = al.addr - al.map->start - al.sym->start;
839 printf("\t%s", al.sym->name);
840 if (off)
841 printf("%+d", off);
842 putchar(':');
843 if (PRINT_FIELD(SRCLINE))
844 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
845 putchar('\n');
846 *lastsym = al.sym;
847}
848
849static void print_sample_brstackinsn(struct perf_sample *sample,
850 struct thread *thread,
851 struct perf_event_attr *attr,
852 struct machine *machine)
853{
854 struct branch_stack *br = sample->branch_stack;
855 u64 start, end;
856 int i, insn, len, nr, ilen;
857 struct perf_insn x;
858 u8 buffer[MAXBB];
859 unsigned off;
860 struct symbol *lastsym = NULL;
861
862 if (!(br && br->nr))
863 return;
864 nr = br->nr;
865 if (max_blocks && nr > max_blocks + 1)
866 nr = max_blocks + 1;
867
868 x.thread = thread;
869 x.cpu = sample->cpu;
870
871 putchar('\n');
872
873 /* Handle first from jump, of which we don't know the entry. */
874 len = grab_bb(buffer, br->entries[nr-1].from,
875 br->entries[nr-1].from,
876 machine, thread, &x.is64bit, &x.cpumode, false);
877 if (len > 0) {
878 print_ip_sym(thread, x.cpumode, x.cpu,
879 br->entries[nr - 1].from, &lastsym, attr);
880 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
881 &x, buffer, len, 0);
882 }
883
884 /* Print all blocks */
885 for (i = nr - 2; i >= 0; i--) {
886 if (br->entries[i].from || br->entries[i].to)
887 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
888 br->entries[i].from,
889 br->entries[i].to);
890 start = br->entries[i + 1].to;
891 end = br->entries[i].from;
892
893 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
894 /* Patch up missing kernel transfers due to ring filters */
895 if (len == -ENXIO && i > 0) {
896 end = br->entries[--i].from;
897 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
898 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
899 }
900 if (len <= 0)
901 continue;
902
903 insn = 0;
904 for (off = 0;; off += ilen) {
905 uint64_t ip = start + off;
906
907 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
908 if (ip == end) {
909 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
910 break;
911 } else {
912 printf("\t%016" PRIx64 "\t%s\n", ip,
913 dump_insn(&x, ip, buffer + off, len - off, &ilen));
914 if (ilen == 0)
915 break;
916 insn++;
917 }
918 }
919 }
920
921 /*
922 * Hit the branch? In this case we are already done, and the target
923 * has not been executed yet.
924 */
925 if (br->entries[0].from == sample->ip)
926 return;
927 if (br->entries[0].flags.abort)
928 return;
929
930 /*
931 * Print final block upto sample
932 */
933 start = br->entries[0].to;
934 end = sample->ip;
935 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
936 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
937 if (len <= 0) {
938 /* Print at least last IP if basic block did not work */
939 len = grab_bb(buffer, sample->ip, sample->ip,
940 machine, thread, &x.is64bit, &x.cpumode, false);
941 if (len <= 0)
942 return;
943
944 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
945 dump_insn(&x, sample->ip, buffer, len, NULL));
946 return;
947 }
948 for (off = 0; off <= end - start; off += ilen) {
949 printf("\t%016" PRIx64 "\t%s\n", start + off,
950 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
951 if (ilen == 0)
952 break;
953 }
954}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200955
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300956static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600957 struct thread *thread,
958 struct perf_event_attr *attr)
959{
960 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600961
962 printf("%16" PRIx64, sample->addr);
963
964 if (!sample_addr_correlates_sym(attr))
965 return;
966
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300967 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600968
969 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900970 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900971 if (PRINT_FIELD(SYMOFFSET))
972 symbol__fprintf_symname_offs(al.sym, &al, stdout);
973 else
974 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600975 }
976
977 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900978 printf(" (");
979 map__fprintf_dsoname(al.map, stdout);
980 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600981 }
982}
983
Adrian Huntere2167082016-06-23 16:40:58 +0300984static void print_sample_callindent(struct perf_sample *sample,
985 struct perf_evsel *evsel,
986 struct thread *thread,
987 struct addr_location *al)
988{
989 struct perf_event_attr *attr = &evsel->attr;
990 size_t depth = thread_stack__depth(thread);
991 struct addr_location addr_al;
992 const char *name = NULL;
993 static int spacing;
994 int len = 0;
995 u64 ip = 0;
996
997 /*
998 * The 'return' has already been popped off the stack so the depth has
999 * to be adjusted to match the 'call'.
1000 */
1001 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
1002 depth += 1;
1003
1004 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
1005 if (sample_addr_correlates_sym(attr)) {
1006 thread__resolve(thread, &addr_al, sample);
1007 if (addr_al.sym)
1008 name = addr_al.sym->name;
1009 else
1010 ip = sample->addr;
1011 } else {
1012 ip = sample->addr;
1013 }
1014 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
1015 if (al->sym)
1016 name = al->sym->name;
1017 else
1018 ip = sample->ip;
1019 }
1020
1021 if (name)
1022 len = printf("%*s%s", (int)depth * 4, "", name);
1023 else if (ip)
1024 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
1025
1026 if (len < 0)
1027 return;
1028
1029 /*
1030 * Try to keep the output length from changing frequently so that the
1031 * output lines up more nicely.
1032 */
1033 if (len > spacing || (len && len < spacing - 52))
1034 spacing = round_up(len + 4, 32);
1035
1036 if (len < spacing)
1037 printf("%*s", spacing - len, "");
1038}
1039
Andi Kleen224e2c92016-10-07 16:42:27 +03001040static void print_insn(struct perf_sample *sample,
Andi Kleen48d02a12017-02-23 15:46:34 -08001041 struct perf_event_attr *attr,
1042 struct thread *thread,
1043 struct machine *machine)
Andi Kleen224e2c92016-10-07 16:42:27 +03001044{
1045 if (PRINT_FIELD(INSNLEN))
1046 printf(" ilen: %d", sample->insn_len);
1047 if (PRINT_FIELD(INSN)) {
1048 int i;
1049
1050 printf(" insn:");
1051 for (i = 0; i < sample->insn_len; i++)
1052 printf(" %02x", (unsigned char)sample->insn[i]);
1053 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001054 if (PRINT_FIELD(BRSTACKINSN))
1055 print_sample_brstackinsn(sample, thread, attr, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +03001056}
1057
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001058static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +09001059 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001060 struct thread *thread,
Andi Kleen48d02a12017-02-23 15:46:34 -08001061 struct addr_location *al,
1062 struct machine *machine)
Akihiro Nagai95582592012-01-30 13:43:09 +09001063{
1064 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001065 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001066 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +09001067
Adrian Huntere2167082016-06-23 16:40:58 +03001068 if (PRINT_FIELD(CALLINDENT))
1069 print_sample_callindent(sample, evsel, thread, al);
1070
Akihiro Nagai95582592012-01-30 13:43:09 +09001071 /* print branch_from information */
1072 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001073 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001074 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001075
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001076 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001077 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001078 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001079 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001080
1081 if (cursor == NULL) {
1082 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001083 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001084 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001085 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001086 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001087 } else
1088 putchar('\n');
1089
1090 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +09001091 }
1092
Akihiro Nagai95582592012-01-30 13:43:09 +09001093 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001094 if (PRINT_FIELD(ADDR) ||
1095 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001096 !output[type].user_set)) {
Adrian Hunter578bea42014-07-22 16:17:16 +03001097 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001098 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +03001099 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001100
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001101 if (print_srcline_last)
1102 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
1103
Andi Kleen48d02a12017-02-23 15:46:34 -08001104 print_insn(sample, attr, thread, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +03001105
Akihiro Nagai95582592012-01-30 13:43:09 +09001106 printf("\n");
1107}
1108
Adrian Hunter055cd332016-06-23 16:40:56 +03001109static struct {
1110 u32 flags;
1111 const char *name;
1112} sample_flags[] = {
1113 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1114 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1115 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1116 {PERF_IP_FLAG_BRANCH, "jmp"},
1117 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1118 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1119 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1120 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1121 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1122 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1123 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1124 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1125 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1126 {0, NULL}
1127};
1128
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001129static void print_sample_flags(u32 flags)
1130{
1131 const char *chars = PERF_IP_FLAG_CHARS;
1132 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001133 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1134 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001135 char str[33];
1136 int i, pos = 0;
1137
Adrian Hunter055cd332016-06-23 16:40:56 +03001138 for (i = 0; sample_flags[i].name ; i++) {
1139 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1140 name = sample_flags[i].name;
1141 break;
1142 }
1143 }
1144
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001145 for (i = 0; i < n; i++, flags >>= 1) {
1146 if (flags & 1)
1147 str[pos++] = chars[i];
1148 }
1149 for (; i < 32; i++, flags >>= 1) {
1150 if (flags & 1)
1151 str[pos++] = '?';
1152 }
1153 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001154
1155 if (name)
1156 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
1157 else
1158 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001159}
1160
Wang Nan30372f02016-02-24 11:20:45 +00001161struct printer_data {
1162 int line_no;
1163 bool hit_nul;
1164 bool is_printable;
1165};
1166
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001167static int sample__fprintf_bpf_output(enum binary_printer_ops op,
1168 unsigned int val,
1169 void *extra, FILE *fp)
Wang Nan30372f02016-02-24 11:20:45 +00001170{
1171 unsigned char ch = (unsigned char)val;
1172 struct printer_data *printer_data = extra;
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001173 int printed = 0;
Wang Nan30372f02016-02-24 11:20:45 +00001174
1175 switch (op) {
1176 case BINARY_PRINT_DATA_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001177 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001178 break;
1179 case BINARY_PRINT_LINE_BEGIN:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001180 printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
Wang Nan30372f02016-02-24 11:20:45 +00001181 " ");
1182 break;
1183 case BINARY_PRINT_ADDR:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001184 printed += fprintf(fp, " %04x:", val);
Wang Nan30372f02016-02-24 11:20:45 +00001185 break;
1186 case BINARY_PRINT_NUM_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001187 printed += fprintf(fp, " %02x", val);
Wang Nan30372f02016-02-24 11:20:45 +00001188 break;
1189 case BINARY_PRINT_NUM_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001190 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001191 break;
1192 case BINARY_PRINT_SEP:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001193 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001194 break;
1195 case BINARY_PRINT_CHAR_DATA:
1196 if (printer_data->hit_nul && ch)
1197 printer_data->is_printable = false;
1198
1199 if (!isprint(ch)) {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001200 printed += fprintf(fp, "%c", '.');
Wang Nan30372f02016-02-24 11:20:45 +00001201
1202 if (!printer_data->is_printable)
1203 break;
1204
1205 if (ch == '\0')
1206 printer_data->hit_nul = true;
1207 else
1208 printer_data->is_printable = false;
1209 } else {
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001210 printed += fprintf(fp, "%c", ch);
Wang Nan30372f02016-02-24 11:20:45 +00001211 }
1212 break;
1213 case BINARY_PRINT_CHAR_PAD:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001214 printed += fprintf(fp, " ");
Wang Nan30372f02016-02-24 11:20:45 +00001215 break;
1216 case BINARY_PRINT_LINE_END:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001217 printed += fprintf(fp, "\n");
Wang Nan30372f02016-02-24 11:20:45 +00001218 printer_data->line_no++;
1219 break;
1220 case BINARY_PRINT_DATA_END:
1221 default:
1222 break;
1223 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001224
1225 return printed;
Wang Nan30372f02016-02-24 11:20:45 +00001226}
1227
1228static void print_sample_bpf_output(struct perf_sample *sample)
1229{
1230 unsigned int nr_bytes = sample->raw_size;
1231 struct printer_data printer_data = {0, false, true};
1232
1233 print_binary(sample->raw_data, nr_bytes, 8,
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03001234 sample__fprintf_bpf_output, &printer_data);
Wang Nan30372f02016-02-24 11:20:45 +00001235
1236 if (printer_data.is_printable && printer_data.hit_nul)
1237 printf("%17s \"%s\"\n", "BPF string:",
1238 (char *)(sample->raw_data));
1239}
1240
Adrian Hunter65c5e182017-06-30 11:36:42 +03001241static void print_sample_spacing(int len, int spacing)
1242{
1243 if (len > 0 && len < spacing)
1244 printf("%*s", spacing - len, "");
1245}
1246
1247static void print_sample_pt_spacing(int len)
1248{
1249 print_sample_spacing(len, 34);
1250}
1251
1252static void print_sample_synth_ptwrite(struct perf_sample *sample)
1253{
1254 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1255 int len;
1256
1257 if (perf_sample__bad_synth_size(sample, *data))
1258 return;
1259
1260 len = printf(" IP: %u payload: %#" PRIx64 " ",
1261 data->ip, le64_to_cpu(data->payload));
1262 print_sample_pt_spacing(len);
1263}
1264
1265static void print_sample_synth_mwait(struct perf_sample *sample)
1266{
1267 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1268 int len;
1269
1270 if (perf_sample__bad_synth_size(sample, *data))
1271 return;
1272
1273 len = printf(" hints: %#x extensions: %#x ",
1274 data->hints, data->extensions);
1275 print_sample_pt_spacing(len);
1276}
1277
1278static void print_sample_synth_pwre(struct perf_sample *sample)
1279{
1280 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1281 int len;
1282
1283 if (perf_sample__bad_synth_size(sample, *data))
1284 return;
1285
1286 len = printf(" hw: %u cstate: %u sub-cstate: %u ",
1287 data->hw, data->cstate, data->subcstate);
1288 print_sample_pt_spacing(len);
1289}
1290
1291static void print_sample_synth_exstop(struct perf_sample *sample)
1292{
1293 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1294 int len;
1295
1296 if (perf_sample__bad_synth_size(sample, *data))
1297 return;
1298
1299 len = printf(" IP: %u ", data->ip);
1300 print_sample_pt_spacing(len);
1301}
1302
1303static void print_sample_synth_pwrx(struct perf_sample *sample)
1304{
1305 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1306 int len;
1307
1308 if (perf_sample__bad_synth_size(sample, *data))
1309 return;
1310
1311 len = printf(" deepest cstate: %u last cstate: %u wake reason: %#x ",
1312 data->deepest_cstate, data->last_cstate,
1313 data->wake_reason);
1314 print_sample_pt_spacing(len);
1315}
1316
1317static void print_sample_synth_cbr(struct perf_sample *sample)
1318{
1319 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1320 unsigned int percent, freq;
1321 int len;
1322
1323 if (perf_sample__bad_synth_size(sample, *data))
1324 return;
1325
1326 freq = (le32_to_cpu(data->freq) + 500) / 1000;
1327 len = printf(" cbr: %2u freq: %4u MHz ", data->cbr, freq);
1328 if (data->max_nonturbo) {
1329 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1330 len += printf("(%3u%%) ", percent);
1331 }
1332 print_sample_pt_spacing(len);
1333}
1334
1335static void print_sample_synth(struct perf_sample *sample,
Adrian Hunter47e78082017-05-26 11:17:22 +03001336 struct perf_evsel *evsel)
1337{
1338 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001339 case PERF_SYNTH_INTEL_PTWRITE:
1340 print_sample_synth_ptwrite(sample);
1341 break;
1342 case PERF_SYNTH_INTEL_MWAIT:
1343 print_sample_synth_mwait(sample);
1344 break;
1345 case PERF_SYNTH_INTEL_PWRE:
1346 print_sample_synth_pwre(sample);
1347 break;
1348 case PERF_SYNTH_INTEL_EXSTOP:
1349 print_sample_synth_exstop(sample);
1350 break;
1351 case PERF_SYNTH_INTEL_PWRX:
1352 print_sample_synth_pwrx(sample);
1353 break;
1354 case PERF_SYNTH_INTEL_CBR:
1355 print_sample_synth_cbr(sample);
1356 break;
Adrian Hunter47e78082017-05-26 11:17:22 +03001357 default:
1358 break;
1359 }
1360}
1361
Jiri Olsa809e9422015-11-26 18:55:21 +01001362struct perf_script {
1363 struct perf_tool tool;
1364 struct perf_session *session;
1365 bool show_task_events;
1366 bool show_mmap_events;
1367 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301368 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001369 bool allocated;
1370 struct cpu_map *cpus;
1371 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001372 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001373 const char *time_str;
1374 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001375};
1376
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001377static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1378{
1379 struct perf_evsel *evsel;
1380 int max = 0;
1381
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001382 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001383 int len = strlen(perf_evsel__name(evsel));
1384
1385 max = MAX(len, max);
1386 }
1387
1388 return max;
1389}
1390
Jiri Olsac19ac912016-02-24 09:46:54 +01001391static size_t data_src__printf(u64 data_src)
1392{
1393 struct mem_info mi = { .data_src.val = data_src };
1394 char decode[100];
1395 char out[100];
1396 static int maxlen;
1397 int len;
1398
1399 perf_script__meminfo_scnprintf(decode, 100, &mi);
1400
1401 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1402 if (maxlen < len)
1403 maxlen = len;
1404
1405 return printf("%-*s", maxlen, out);
1406}
1407
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001408static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001409 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001410 struct addr_location *al,
1411 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001412{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001413 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001414 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001415 unsigned int type = output_type(attr->type);
David Ahern1424dc92011-03-09 22:23:28 -07001416
Adrian Hunter14057202017-06-21 13:17:19 +03001417 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001418 return;
1419
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001420 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001421
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001422 if (PRINT_FIELD(PERIOD))
1423 printf("%10" PRIu64 " ", sample->period);
1424
Namhyung Kime944d3d2013-11-18 14:34:52 +09001425 if (PRINT_FIELD(EVNAME)) {
1426 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001427
1428 if (!script->name_width)
1429 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1430
1431 printf("%*s: ", script->name_width,
1432 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001433 }
1434
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001435 if (print_flags)
1436 print_sample_flags(sample->flags);
1437
Akihiro Nagai95582592012-01-30 13:43:09 +09001438 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001439 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001440 return;
1441 }
1442
David Ahern745f43e2011-03-09 22:23:26 -07001443 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001444 event_format__print(evsel->tp_format, sample->cpu,
1445 sample->raw_data, sample->raw_size);
Adrian Hunter47e78082017-05-26 11:17:22 +03001446
1447 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
1448 print_sample_synth(sample, evsel);
1449
David Ahern7cec0922011-05-30 13:08:23 -06001450 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001451 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001452
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001453 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001454 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001455
1456 if (PRINT_FIELD(WEIGHT))
1457 printf("%16" PRIu64, sample->weight);
1458
David Ahern787bef12011-05-27 14:28:43 -06001459 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001460 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001461
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001462 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001463 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001464 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001465 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001466
1467 putchar(cursor ? '\n' : ' ');
Adrian Hunter14057202017-06-21 13:17:19 +03001468 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001469 }
1470
Stephane Eranianfc36f942015-08-31 18:41:10 +02001471 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001472 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001473
Andi Kleenb1491ac2017-09-05 11:40:57 -07001474 if (PRINT_FIELD(UREGS))
1475 print_sample_uregs(sample, attr);
1476
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001477 if (PRINT_FIELD(BRSTACK))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001478 print_sample_brstack(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001479 else if (PRINT_FIELD(BRSTACKSYM))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001480 print_sample_brstacksym(sample, thread, attr);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001481 else if (PRINT_FIELD(BRSTACKOFF))
1482 print_sample_brstackoff(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001483
Wang Nan30372f02016-02-24 11:20:45 +00001484 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1485 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001486 print_insn(sample, attr, thread, machine);
Kan Liang49d58f02017-08-29 13:11:11 -04001487
1488 if (PRINT_FIELD(PHYS_ADDR))
1489 printf("%16" PRIx64, sample->phys_addr);
David Ahernc70c94b2011-03-09 22:23:25 -07001490 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001491}
1492
Tom Zanussi956ffd02009-11-25 01:15:46 -06001493static struct scripting_ops *scripting_ops;
1494
Jiri Olsa36e33c52016-01-06 11:49:56 +01001495static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1496{
1497 int nthreads = thread_map__nr(counter->threads);
1498 int ncpus = perf_evsel__nr_cpus(counter);
1499 int cpu, thread;
1500 static int header_printed;
1501
1502 if (counter->system_wide)
1503 nthreads = 1;
1504
1505 if (!header_printed) {
1506 printf("%3s %8s %15s %15s %15s %15s %s\n",
1507 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1508 header_printed = 1;
1509 }
1510
1511 for (thread = 0; thread < nthreads; thread++) {
1512 for (cpu = 0; cpu < ncpus; cpu++) {
1513 struct perf_counts_values *counts;
1514
1515 counts = perf_counts(counter->counts, cpu, thread);
1516
1517 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1518 counter->cpus->map[cpu],
1519 thread_map__pid(counter->threads, thread),
1520 counts->val,
1521 counts->ena,
1522 counts->run,
1523 tstamp,
1524 perf_evsel__name(counter));
1525 }
1526 }
1527}
1528
Jiri Olsae099eba2016-01-05 22:09:09 +01001529static void process_stat(struct perf_evsel *counter, u64 tstamp)
1530{
1531 if (scripting_ops && scripting_ops->process_stat)
1532 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001533 else
1534 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001535}
1536
1537static void process_stat_interval(u64 tstamp)
1538{
1539 if (scripting_ops && scripting_ops->process_stat_interval)
1540 scripting_ops->process_stat_interval(tstamp);
1541}
1542
Tom Zanussi956ffd02009-11-25 01:15:46 -06001543static void setup_scripting(void)
1544{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001545 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001546 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001547}
1548
Adrian Hunterd445dd22014-08-15 22:08:37 +03001549static int flush_scripting(void)
1550{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001551 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001552}
1553
Tom Zanussi956ffd02009-11-25 01:15:46 -06001554static int cleanup_scripting(void)
1555{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001556 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001557
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001558 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001559}
1560
Jiri Olsa809e9422015-11-26 18:55:21 +01001561static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001562 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001563 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001564 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001565 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001566{
Jiri Olsa809e9422015-11-26 18:55:21 +01001567 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001568 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001569
David Aherna91f4c42016-11-29 10:15:43 -07001570 if (perf_time__skip_sample(&scr->ptime, sample->time))
1571 return 0;
1572
David Ahern1424dc92011-03-09 22:23:28 -07001573 if (debug_mode) {
1574 if (sample->time < last_timestamp) {
1575 pr_err("Samples misordered, previous: %" PRIu64
1576 " this: %" PRIu64 "\n", last_timestamp,
1577 sample->time);
1578 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001579 }
David Ahern1424dc92011-03-09 22:23:28 -07001580 last_timestamp = sample->time;
1581 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001582 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001583
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001584 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001585 pr_err("problem processing %d event, skipping it.\n",
1586 event->header.type);
1587 return -1;
1588 }
1589
1590 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001591 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001592
Anton Blanchard5d67be92011-07-04 21:57:50 +10001593 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001594 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001595
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001596 if (scripting_ops)
1597 scripting_ops->process_event(event, sample, evsel, &al);
1598 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001599 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001600
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001601out_put:
1602 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001603 return 0;
1604}
1605
Adrian Hunter7ea95722013-11-01 15:51:30 +02001606static int process_attr(struct perf_tool *tool, union perf_event *event,
1607 struct perf_evlist **pevlist)
1608{
1609 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1610 struct perf_evlist *evlist;
1611 struct perf_evsel *evsel, *pos;
1612 int err;
1613
1614 err = perf_event__process_attr(tool, event, pevlist);
1615 if (err)
1616 return err;
1617
1618 evlist = *pevlist;
1619 evsel = perf_evlist__last(*pevlist);
1620
Adrian Hunter14057202017-06-21 13:17:19 +03001621 if (evsel->attr.type >= PERF_TYPE_MAX &&
1622 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001623 return 0;
1624
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001625 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001626 if (pos->attr.type == evsel->attr.type && pos != evsel)
1627 return 0;
1628 }
1629
1630 set_print_ip_opts(&evsel->attr);
1631
Jiri Olsad2b5a312015-10-16 12:41:25 +02001632 if (evsel->attr.sample_type)
1633 err = perf_evsel__check_attr(evsel, scr->session);
1634
1635 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001636}
1637
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001638static int process_comm_event(struct perf_tool *tool,
1639 union perf_event *event,
1640 struct perf_sample *sample,
1641 struct machine *machine)
1642{
1643 struct thread *thread;
1644 struct perf_script *script = container_of(tool, struct perf_script, tool);
1645 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001646 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001647 int ret = -1;
1648
1649 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1650 if (thread == NULL) {
1651 pr_debug("problem processing COMM event, skipping it.\n");
1652 return -1;
1653 }
1654
1655 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1656 goto out;
1657
1658 if (!evsel->attr.sample_id_all) {
1659 sample->cpu = 0;
1660 sample->time = 0;
1661 sample->tid = event->comm.tid;
1662 sample->pid = event->comm.pid;
1663 }
1664 print_sample_start(sample, thread, evsel);
1665 perf_event__fprintf(event, stdout);
1666 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001667out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001668 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001669 return ret;
1670}
1671
Hari Bathini96a44bb2017-03-08 02:12:06 +05301672static int process_namespaces_event(struct perf_tool *tool,
1673 union perf_event *event,
1674 struct perf_sample *sample,
1675 struct machine *machine)
1676{
1677 struct thread *thread;
1678 struct perf_script *script = container_of(tool, struct perf_script, tool);
1679 struct perf_session *session = script->session;
1680 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1681 int ret = -1;
1682
1683 thread = machine__findnew_thread(machine, event->namespaces.pid,
1684 event->namespaces.tid);
1685 if (thread == NULL) {
1686 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1687 return -1;
1688 }
1689
1690 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1691 goto out;
1692
1693 if (!evsel->attr.sample_id_all) {
1694 sample->cpu = 0;
1695 sample->time = 0;
1696 sample->tid = event->namespaces.tid;
1697 sample->pid = event->namespaces.pid;
1698 }
1699 print_sample_start(sample, thread, evsel);
1700 perf_event__fprintf(event, stdout);
1701 ret = 0;
1702out:
1703 thread__put(thread);
1704 return ret;
1705}
1706
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001707static int process_fork_event(struct perf_tool *tool,
1708 union perf_event *event,
1709 struct perf_sample *sample,
1710 struct machine *machine)
1711{
1712 struct thread *thread;
1713 struct perf_script *script = container_of(tool, struct perf_script, tool);
1714 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001715 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001716
1717 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1718 return -1;
1719
1720 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1721 if (thread == NULL) {
1722 pr_debug("problem processing FORK event, skipping it.\n");
1723 return -1;
1724 }
1725
1726 if (!evsel->attr.sample_id_all) {
1727 sample->cpu = 0;
1728 sample->time = event->fork.time;
1729 sample->tid = event->fork.tid;
1730 sample->pid = event->fork.pid;
1731 }
1732 print_sample_start(sample, thread, evsel);
1733 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001734 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001735
1736 return 0;
1737}
1738static int process_exit_event(struct perf_tool *tool,
1739 union perf_event *event,
1740 struct perf_sample *sample,
1741 struct machine *machine)
1742{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001743 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001744 struct thread *thread;
1745 struct perf_script *script = container_of(tool, struct perf_script, tool);
1746 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001747 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001748
1749 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1750 if (thread == NULL) {
1751 pr_debug("problem processing EXIT event, skipping it.\n");
1752 return -1;
1753 }
1754
1755 if (!evsel->attr.sample_id_all) {
1756 sample->cpu = 0;
1757 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001758 sample->tid = event->fork.tid;
1759 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001760 }
1761 print_sample_start(sample, thread, evsel);
1762 perf_event__fprintf(event, stdout);
1763
1764 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001765 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001766
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001767 thread__put(thread);
1768 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001769}
1770
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001771static int process_mmap_event(struct perf_tool *tool,
1772 union perf_event *event,
1773 struct perf_sample *sample,
1774 struct machine *machine)
1775{
1776 struct thread *thread;
1777 struct perf_script *script = container_of(tool, struct perf_script, tool);
1778 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001779 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001780
1781 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1782 return -1;
1783
1784 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1785 if (thread == NULL) {
1786 pr_debug("problem processing MMAP event, skipping it.\n");
1787 return -1;
1788 }
1789
1790 if (!evsel->attr.sample_id_all) {
1791 sample->cpu = 0;
1792 sample->time = 0;
1793 sample->tid = event->mmap.tid;
1794 sample->pid = event->mmap.pid;
1795 }
1796 print_sample_start(sample, thread, evsel);
1797 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001798 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001799 return 0;
1800}
1801
1802static int process_mmap2_event(struct perf_tool *tool,
1803 union perf_event *event,
1804 struct perf_sample *sample,
1805 struct machine *machine)
1806{
1807 struct thread *thread;
1808 struct perf_script *script = container_of(tool, struct perf_script, tool);
1809 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001810 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001811
1812 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1813 return -1;
1814
1815 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1816 if (thread == NULL) {
1817 pr_debug("problem processing MMAP2 event, skipping it.\n");
1818 return -1;
1819 }
1820
1821 if (!evsel->attr.sample_id_all) {
1822 sample->cpu = 0;
1823 sample->time = 0;
1824 sample->tid = event->mmap2.tid;
1825 sample->pid = event->mmap2.pid;
1826 }
1827 print_sample_start(sample, thread, evsel);
1828 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001829 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001830 return 0;
1831}
1832
Adrian Hunter7c148982015-07-21 12:44:06 +03001833static int process_switch_event(struct perf_tool *tool,
1834 union perf_event *event,
1835 struct perf_sample *sample,
1836 struct machine *machine)
1837{
1838 struct thread *thread;
1839 struct perf_script *script = container_of(tool, struct perf_script, tool);
1840 struct perf_session *session = script->session;
1841 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1842
1843 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1844 return -1;
1845
1846 thread = machine__findnew_thread(machine, sample->pid,
1847 sample->tid);
1848 if (thread == NULL) {
1849 pr_debug("problem processing SWITCH event, skipping it.\n");
1850 return -1;
1851 }
1852
1853 print_sample_start(sample, thread, evsel);
1854 perf_event__fprintf(event, stdout);
1855 thread__put(thread);
1856 return 0;
1857}
1858
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001859static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001860{
1861 session_done = 1;
1862}
1863
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001864static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001865{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001866 int ret;
1867
Tom Zanussic239da32010-04-01 23:59:18 -05001868 signal(SIGINT, sig_handler);
1869
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001870 /* override event processing functions */
1871 if (script->show_task_events) {
1872 script->tool.comm = process_comm_event;
1873 script->tool.fork = process_fork_event;
1874 script->tool.exit = process_exit_event;
1875 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001876 if (script->show_mmap_events) {
1877 script->tool.mmap = process_mmap_event;
1878 script->tool.mmap2 = process_mmap2_event;
1879 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001880 if (script->show_switch_events)
1881 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301882 if (script->show_namespace_events)
1883 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001884
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001885 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001886
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001887 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001888 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001889
1890 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001891}
1892
Tom Zanussi956ffd02009-11-25 01:15:46 -06001893struct script_spec {
1894 struct list_head node;
1895 struct scripting_ops *ops;
1896 char spec[0];
1897};
1898
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001899static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001900
1901static struct script_spec *script_spec__new(const char *spec,
1902 struct scripting_ops *ops)
1903{
1904 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1905
1906 if (s != NULL) {
1907 strcpy(s->spec, spec);
1908 s->ops = ops;
1909 }
1910
1911 return s;
1912}
1913
Tom Zanussi956ffd02009-11-25 01:15:46 -06001914static void script_spec__add(struct script_spec *s)
1915{
1916 list_add_tail(&s->node, &script_specs);
1917}
1918
1919static struct script_spec *script_spec__find(const char *spec)
1920{
1921 struct script_spec *s;
1922
1923 list_for_each_entry(s, &script_specs, node)
1924 if (strcasecmp(s->spec, spec) == 0)
1925 return s;
1926 return NULL;
1927}
1928
Tom Zanussi956ffd02009-11-25 01:15:46 -06001929int script_spec_register(const char *spec, struct scripting_ops *ops)
1930{
1931 struct script_spec *s;
1932
1933 s = script_spec__find(spec);
1934 if (s)
1935 return -1;
1936
Taeung Song8560bae2016-02-26 00:13:10 +09001937 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001938 if (!s)
1939 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001940 else
1941 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001942
1943 return 0;
1944}
1945
1946static struct scripting_ops *script_spec__lookup(const char *spec)
1947{
1948 struct script_spec *s = script_spec__find(spec);
1949 if (!s)
1950 return NULL;
1951
1952 return s->ops;
1953}
1954
1955static void list_available_languages(void)
1956{
1957 struct script_spec *s;
1958
1959 fprintf(stderr, "\n");
1960 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001961 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001962
1963 list_for_each_entry(s, &script_specs, node)
1964 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1965
1966 fprintf(stderr, "\n");
1967}
1968
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001969static int parse_scriptname(const struct option *opt __maybe_unused,
1970 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001971{
1972 char spec[PATH_MAX];
1973 const char *script, *ext;
1974 int len;
1975
Tom Zanussif526d682010-01-27 02:27:52 -06001976 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001977 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001978 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001979 }
1980
1981 script = strchr(str, ':');
1982 if (script) {
1983 len = script - str;
1984 if (len >= PATH_MAX) {
1985 fprintf(stderr, "invalid language specifier");
1986 return -1;
1987 }
1988 strncpy(spec, str, len);
1989 spec[len] = '\0';
1990 scripting_ops = script_spec__lookup(spec);
1991 if (!scripting_ops) {
1992 fprintf(stderr, "invalid language specifier");
1993 return -1;
1994 }
1995 script++;
1996 } else {
1997 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001998 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001999 if (!ext) {
2000 fprintf(stderr, "invalid script extension");
2001 return -1;
2002 }
2003 scripting_ops = script_spec__lookup(++ext);
2004 if (!scripting_ops) {
2005 fprintf(stderr, "invalid script extension");
2006 return -1;
2007 }
2008 }
2009
2010 script_name = strdup(script);
2011
2012 return 0;
2013}
2014
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002015static int parse_output_fields(const struct option *opt __maybe_unused,
2016 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002017{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002018 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002019 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002020 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002021 int rc = 0;
2022 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002023 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002024 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002025
2026 if (!str)
2027 return -ENOMEM;
2028
David Ahern2c9e45f72011-03-17 10:03:21 -06002029 /* first word can state for which event type the user is specifying
2030 * the fields. If no type exists, the specified fields apply to all
2031 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002032 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002033 tok = strchr(str, ':');
2034 if (tok) {
2035 *tok = '\0';
2036 tok++;
2037 if (!strcmp(str, "hw"))
2038 type = PERF_TYPE_HARDWARE;
2039 else if (!strcmp(str, "sw"))
2040 type = PERF_TYPE_SOFTWARE;
2041 else if (!strcmp(str, "trace"))
2042 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002043 else if (!strcmp(str, "raw"))
2044 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002045 else if (!strcmp(str, "break"))
2046 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002047 else if (!strcmp(str, "synth"))
2048 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002049 else {
2050 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002051 rc = -EINVAL;
2052 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002053 }
2054
2055 if (output[type].user_set)
2056 pr_warning("Overriding previous field request for %s events.\n",
2057 event_type(type));
2058
2059 output[type].fields = 0;
2060 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002061 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002062
2063 } else {
2064 tok = str;
2065 if (strlen(str) == 0) {
2066 fprintf(stderr,
2067 "Cannot set fields to 'none' for all event types.\n");
2068 rc = -EINVAL;
2069 goto out;
2070 }
2071
Andi Kleen36ce5652017-06-02 08:48:10 -07002072 /* Don't override defaults for +- */
2073 if (strchr(str, '+') || strchr(str, '-'))
2074 goto parse;
2075
David Ahern2c9e45f72011-03-17 10:03:21 -06002076 if (output_set_by_user())
2077 pr_warning("Overriding previous field request for all events.\n");
2078
Adrian Hunter14057202017-06-21 13:17:19 +03002079 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002080 output[j].fields = 0;
2081 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002082 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002083 }
David Ahern1424dc92011-03-09 22:23:28 -07002084 }
2085
Andi Kleen36ce5652017-06-02 08:48:10 -07002086parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002087 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002088 if (*tok == '+') {
2089 if (change == SET)
2090 goto out_badmix;
2091 change = ADD;
2092 tok++;
2093 } else if (*tok == '-') {
2094 if (change == SET)
2095 goto out_badmix;
2096 change = REMOVE;
2097 tok++;
2098 } else {
2099 if (change != SET && change != DEFAULT)
2100 goto out_badmix;
2101 change = SET;
2102 }
2103
David Ahern745f43e2011-03-09 22:23:26 -07002104 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002105 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002106 break;
David Ahern745f43e2011-03-09 22:23:26 -07002107 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002108 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002109 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002110 continue;
2111 }
David Ahern745f43e2011-03-09 22:23:26 -07002112 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002113 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002114 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002115 goto out;
2116 }
2117
2118 if (type == -1) {
2119 /* add user option to all events types for
2120 * which it is valid
2121 */
Adrian Hunter14057202017-06-21 13:17:19 +03002122 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002123 if (output[j].invalid_fields & all_output_options[i].field) {
2124 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2125 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002126 } else {
2127 if (change == REMOVE)
2128 output[j].fields &= ~all_output_options[i].field;
2129 else
2130 output[j].fields |= all_output_options[i].field;
2131 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002132 }
2133 } else {
2134 if (output[type].invalid_fields & all_output_options[i].field) {
2135 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2136 all_output_options[i].str, event_type(type));
2137
2138 rc = -EINVAL;
2139 goto out;
2140 }
2141 output[type].fields |= all_output_options[i].field;
2142 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002143 }
2144
2145 if (type >= 0) {
2146 if (output[type].fields == 0) {
2147 pr_debug("No fields requested for %s type. "
2148 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002149 }
David Ahern1424dc92011-03-09 22:23:28 -07002150 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002151 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002152
Andi Kleen36ce5652017-06-02 08:48:10 -07002153out_badmix:
2154 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2155 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002156out:
David Ahern745f43e2011-03-09 22:23:26 -07002157 free(str);
2158 return rc;
2159}
2160
Shawn Bohrer008f29d2010-11-21 10:09:39 -06002161/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2162static int is_directory(const char *base_path, const struct dirent *dent)
2163{
2164 char path[PATH_MAX];
2165 struct stat st;
2166
2167 sprintf(path, "%s/%s", base_path, dent->d_name);
2168 if (stat(path, &st))
2169 return 0;
2170
2171 return S_ISDIR(st.st_mode);
2172}
2173
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002174#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2175 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2176 if ((lang_dirent->d_type == DT_DIR || \
2177 (lang_dirent->d_type == DT_UNKNOWN && \
2178 is_directory(scripts_path, lang_dirent))) && \
2179 (strcmp(lang_dirent->d_name, ".")) && \
2180 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002181
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002182#define for_each_script(lang_path, lang_dir, script_dirent) \
2183 while ((script_dirent = readdir(lang_dir)) != NULL) \
2184 if (script_dirent->d_type != DT_DIR && \
2185 (script_dirent->d_type != DT_UNKNOWN || \
2186 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002187
2188
2189#define RECORD_SUFFIX "-record"
2190#define REPORT_SUFFIX "-report"
2191
2192struct script_desc {
2193 struct list_head node;
2194 char *name;
2195 char *half_liner;
2196 char *args;
2197};
2198
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002199static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002200
2201static struct script_desc *script_desc__new(const char *name)
2202{
2203 struct script_desc *s = zalloc(sizeof(*s));
2204
Tom Zanussib5b87312010-11-10 08:16:51 -06002205 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002206 s->name = strdup(name);
2207
2208 return s;
2209}
2210
2211static void script_desc__delete(struct script_desc *s)
2212{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002213 zfree(&s->name);
2214 zfree(&s->half_liner);
2215 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002216 free(s);
2217}
2218
2219static void script_desc__add(struct script_desc *s)
2220{
2221 list_add_tail(&s->node, &script_descs);
2222}
2223
2224static struct script_desc *script_desc__find(const char *name)
2225{
2226 struct script_desc *s;
2227
2228 list_for_each_entry(s, &script_descs, node)
2229 if (strcasecmp(s->name, name) == 0)
2230 return s;
2231 return NULL;
2232}
2233
2234static struct script_desc *script_desc__findnew(const char *name)
2235{
2236 struct script_desc *s = script_desc__find(name);
2237
2238 if (s)
2239 return s;
2240
2241 s = script_desc__new(name);
2242 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002243 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002244
2245 script_desc__add(s);
2246
2247 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002248}
2249
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002250static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002251{
2252 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002253 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002254
2255 if (strlen(str) > suffix_len) {
2256 p = str + strlen(str) - suffix_len;
2257 if (!strncmp(p, suffix, suffix_len))
2258 return p;
2259 }
2260
2261 return NULL;
2262}
2263
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002264static int read_script_info(struct script_desc *desc, const char *filename)
2265{
2266 char line[BUFSIZ], *p;
2267 FILE *fp;
2268
2269 fp = fopen(filename, "r");
2270 if (!fp)
2271 return -1;
2272
2273 while (fgets(line, sizeof(line), fp)) {
2274 p = ltrim(line);
2275 if (strlen(p) == 0)
2276 continue;
2277 if (*p != '#')
2278 continue;
2279 p++;
2280 if (strlen(p) && *p == '!')
2281 continue;
2282
2283 p = ltrim(p);
2284 if (strlen(p) && p[strlen(p) - 1] == '\n')
2285 p[strlen(p) - 1] = '\0';
2286
2287 if (!strncmp(p, "description:", strlen("description:"))) {
2288 p += strlen("description:");
2289 desc->half_liner = strdup(ltrim(p));
2290 continue;
2291 }
2292
2293 if (!strncmp(p, "args:", strlen("args:"))) {
2294 p += strlen("args:");
2295 desc->args = strdup(ltrim(p));
2296 continue;
2297 }
2298 }
2299
2300 fclose(fp);
2301
2302 return 0;
2303}
2304
Robert Richter38efb532011-11-25 11:38:40 +01002305static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2306{
2307 char *script_root, *str;
2308
2309 script_root = strdup(script_dirent->d_name);
2310 if (!script_root)
2311 return NULL;
2312
2313 str = (char *)ends_with(script_root, suffix);
2314 if (!str) {
2315 free(script_root);
2316 return NULL;
2317 }
2318
2319 *str = '\0';
2320 return script_root;
2321}
2322
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002323static int list_available_scripts(const struct option *opt __maybe_unused,
2324 const char *s __maybe_unused,
2325 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002326{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002327 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002328 char scripts_path[MAXPATHLEN];
2329 DIR *scripts_dir, *lang_dir;
2330 char script_path[MAXPATHLEN];
2331 char lang_path[MAXPATHLEN];
2332 struct script_desc *desc;
2333 char first_half[BUFSIZ];
2334 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002335
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002336 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002337
2338 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002339 if (!scripts_dir) {
2340 fprintf(stdout,
2341 "open(%s) failed.\n"
2342 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2343 scripts_path);
2344 exit(-1);
2345 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002346
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002347 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002348 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002349 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002350 lang_dir = opendir(lang_path);
2351 if (!lang_dir)
2352 continue;
2353
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002354 for_each_script(lang_path, lang_dir, script_dirent) {
2355 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002356 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002357 desc = script_desc__findnew(script_root);
2358 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002359 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002360 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002361 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002362 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002363 }
2364 }
2365
2366 fprintf(stdout, "List of available trace scripts:\n");
2367 list_for_each_entry(desc, &script_descs, node) {
2368 sprintf(first_half, "%s %s", desc->name,
2369 desc->args ? desc->args : "");
2370 fprintf(stdout, " %-36s %s\n", first_half,
2371 desc->half_liner ? desc->half_liner : "");
2372 }
2373
2374 exit(0);
2375}
2376
Feng Tange5f37052012-09-07 16:42:26 +08002377/*
Feng Tang49e639e2012-10-30 11:56:03 +08002378 * Some scripts specify the required events in their "xxx-record" file,
2379 * this function will check if the events in perf.data match those
2380 * mentioned in the "xxx-record".
2381 *
2382 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2383 * which is covered well now. And new parsing code should be added to
2384 * cover the future complexing formats like event groups etc.
2385 */
2386static int check_ev_match(char *dir_name, char *scriptname,
2387 struct perf_session *session)
2388{
2389 char filename[MAXPATHLEN], evname[128];
2390 char line[BUFSIZ], *p;
2391 struct perf_evsel *pos;
2392 int match, len;
2393 FILE *fp;
2394
2395 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2396
2397 fp = fopen(filename, "r");
2398 if (!fp)
2399 return -1;
2400
2401 while (fgets(line, sizeof(line), fp)) {
2402 p = ltrim(line);
2403 if (*p == '#')
2404 continue;
2405
2406 while (strlen(p)) {
2407 p = strstr(p, "-e");
2408 if (!p)
2409 break;
2410
2411 p += 2;
2412 p = ltrim(p);
2413 len = strcspn(p, " \t");
2414 if (!len)
2415 break;
2416
2417 snprintf(evname, len + 1, "%s", p);
2418
2419 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002420 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002421 if (!strcmp(perf_evsel__name(pos), evname)) {
2422 match = 1;
2423 break;
2424 }
2425 }
2426
2427 if (!match) {
2428 fclose(fp);
2429 return -1;
2430 }
2431 }
2432 }
2433
2434 fclose(fp);
2435 return 0;
2436}
2437
2438/*
Feng Tange5f37052012-09-07 16:42:26 +08002439 * Return -1 if none is found, otherwise the actual scripts number.
2440 *
2441 * Currently the only user of this function is the script browser, which
2442 * will list all statically runnable scripts, select one, execute it and
2443 * show the output in a perf browser.
2444 */
2445int find_scripts(char **scripts_array, char **scripts_path_array)
2446{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002447 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002448 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002449 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002450 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002451 struct perf_data_file file = {
2452 .path = input_name,
2453 .mode = PERF_DATA_MODE_READ,
2454 };
Feng Tange5f37052012-09-07 16:42:26 +08002455 char *temp;
2456 int i = 0;
2457
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002458 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002459 if (!session)
2460 return -1;
2461
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002462 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002463
2464 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002465 if (!scripts_dir) {
2466 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002467 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002468 }
Feng Tange5f37052012-09-07 16:42:26 +08002469
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002470 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002471 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002472 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002473#ifdef NO_LIBPERL
2474 if (strstr(lang_path, "perl"))
2475 continue;
2476#endif
2477#ifdef NO_LIBPYTHON
2478 if (strstr(lang_path, "python"))
2479 continue;
2480#endif
2481
2482 lang_dir = opendir(lang_path);
2483 if (!lang_dir)
2484 continue;
2485
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002486 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002487 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002488 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002489 continue;
2490 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002491 script_dirent->d_name);
2492 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002493 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002494 (temp - script_dirent->d_name) + 1,
2495 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002496
2497 if (check_ev_match(lang_path,
2498 scripts_array[i], session))
2499 continue;
2500
Feng Tange5f37052012-09-07 16:42:26 +08002501 i++;
2502 }
Feng Tang49e639e2012-10-30 11:56:03 +08002503 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002504 }
2505
Feng Tang49e639e2012-10-30 11:56:03 +08002506 closedir(scripts_dir);
2507 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002508 return i;
2509}
2510
Tom Zanussi38752942009-12-15 02:53:39 -06002511static char *get_script_path(const char *script_root, const char *suffix)
2512{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002513 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002514 char scripts_path[MAXPATHLEN];
2515 char script_path[MAXPATHLEN];
2516 DIR *scripts_dir, *lang_dir;
2517 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002518 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002519
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002520 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002521
2522 scripts_dir = opendir(scripts_path);
2523 if (!scripts_dir)
2524 return NULL;
2525
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002526 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002527 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002528 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002529 lang_dir = opendir(lang_path);
2530 if (!lang_dir)
2531 continue;
2532
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002533 for_each_script(lang_path, lang_dir, script_dirent) {
2534 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002535 if (__script_root && !strcmp(script_root, __script_root)) {
2536 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002537 closedir(lang_dir);
2538 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002539 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002540 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002541 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002542 }
2543 free(__script_root);
2544 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002545 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002546 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002547 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002548
Robert Richter38efb532011-11-25 11:38:40 +01002549 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002550}
2551
Tom Zanussib5b87312010-11-10 08:16:51 -06002552static bool is_top_script(const char *script_path)
2553{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002554 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002555}
2556
2557static int has_required_arg(char *script_path)
2558{
2559 struct script_desc *desc;
2560 int n_args = 0;
2561 char *p;
2562
2563 desc = script_desc__new(NULL);
2564
2565 if (read_script_info(desc, script_path))
2566 goto out;
2567
2568 if (!desc->args)
2569 goto out;
2570
2571 for (p = desc->args; *p; p++)
2572 if (*p == '<')
2573 n_args++;
2574out:
2575 script_desc__delete(desc);
2576
2577 return n_args;
2578}
2579
David Ahernd54b1a92012-08-26 12:24:46 -06002580static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002581{
2582 char **__argv = malloc(sizeof(const char *) * argc);
2583
David Ahernd54b1a92012-08-26 12:24:46 -06002584 if (!__argv) {
2585 pr_err("malloc failed\n");
2586 return -1;
2587 }
2588
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002589 memcpy(__argv, argv, sizeof(const char *) * argc);
2590 argc = parse_options(argc, (const char **)__argv, record_options,
2591 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2592 free(__argv);
2593
David Ahernd54b1a92012-08-26 12:24:46 -06002594 system_wide = (argc == 0);
2595
2596 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002597}
2598
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002599static void script__setup_sample_type(struct perf_script *script)
2600{
2601 struct perf_session *session = script->session;
2602 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2603
2604 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2605 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2606 (sample_type & PERF_SAMPLE_STACK_USER))
2607 callchain_param.record_mode = CALLCHAIN_DWARF;
2608 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2609 callchain_param.record_mode = CALLCHAIN_LBR;
2610 else
2611 callchain_param.record_mode = CALLCHAIN_FP;
2612 }
2613}
2614
Jiri Olsae099eba2016-01-05 22:09:09 +01002615static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2616 union perf_event *event,
2617 struct perf_session *session)
2618{
2619 struct stat_round_event *round = &event->stat_round;
2620 struct perf_evsel *counter;
2621
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002622 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002623 perf_stat_process_counter(&stat_config, counter);
2624 process_stat(counter, round->time);
2625 }
2626
2627 process_stat_interval(round->time);
2628 return 0;
2629}
2630
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002631static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2632 union perf_event *event,
2633 struct perf_session *session __maybe_unused)
2634{
2635 perf_event__read_stat_config(&stat_config, &event->stat_config);
2636 return 0;
2637}
2638
Jiri Olsacfc88742016-01-05 22:09:06 +01002639static int set_maps(struct perf_script *script)
2640{
2641 struct perf_evlist *evlist = script->session->evlist;
2642
2643 if (!script->cpus || !script->threads)
2644 return 0;
2645
2646 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2647 return -EINVAL;
2648
2649 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2650
2651 if (perf_evlist__alloc_stats(evlist, true))
2652 return -ENOMEM;
2653
2654 script->allocated = true;
2655 return 0;
2656}
2657
2658static
2659int process_thread_map_event(struct perf_tool *tool,
2660 union perf_event *event,
2661 struct perf_session *session __maybe_unused)
2662{
2663 struct perf_script *script = container_of(tool, struct perf_script, tool);
2664
2665 if (script->threads) {
2666 pr_warning("Extra thread map event, ignoring.\n");
2667 return 0;
2668 }
2669
2670 script->threads = thread_map__new_event(&event->thread_map);
2671 if (!script->threads)
2672 return -ENOMEM;
2673
2674 return set_maps(script);
2675}
2676
2677static
2678int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2679 union perf_event *event,
2680 struct perf_session *session __maybe_unused)
2681{
2682 struct perf_script *script = container_of(tool, struct perf_script, tool);
2683
2684 if (script->cpus) {
2685 pr_warning("Extra cpu map event, ignoring.\n");
2686 return 0;
2687 }
2688
2689 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2690 if (!script->cpus)
2691 return -ENOMEM;
2692
2693 return set_maps(script);
2694}
2695
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002696int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002697{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002698 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002699 bool header = false;
2700 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002701 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002702 char *rec_script_path = NULL;
2703 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002704 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002705 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002706 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002707 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002708 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002709 struct perf_script script = {
2710 .tool = {
2711 .sample = process_sample_event,
2712 .mmap = perf_event__process_mmap,
2713 .mmap2 = perf_event__process_mmap2,
2714 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302715 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002716 .exit = perf_event__process_exit,
2717 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002718 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002719 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002720 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002721 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002722 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002723 .id_index = perf_event__process_id_index,
2724 .auxtrace_info = perf_event__process_auxtrace_info,
2725 .auxtrace = perf_event__process_auxtrace,
2726 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002727 .stat = perf_event__process_stat_event,
2728 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002729 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002730 .thread_map = process_thread_map_event,
2731 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002732 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002733 .ordering_requires_timestamps = true,
2734 },
2735 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002736 struct perf_data_file file = {
2737 .mode = PERF_DATA_MODE_READ,
2738 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002739 const struct option options[] = {
2740 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2741 "dump raw trace in ASCII"),
2742 OPT_INCR('v', "verbose", &verbose,
2743 "be more verbose (show symbol address, etc)"),
2744 OPT_BOOLEAN('L', "Latency", &latency_format,
2745 "show latency attributes (irqs/preemption disabled, etc)"),
2746 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2747 list_available_scripts),
2748 OPT_CALLBACK('s', "script", NULL, "name",
2749 "script file name (lang:script name, script name, or *)",
2750 parse_scriptname),
2751 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2752 "generate perf-script.xx script in specified language"),
2753 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2754 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2755 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002756 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2757 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002758 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2759 "file", "vmlinux pathname"),
2760 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2761 "file", "kallsyms pathname"),
2762 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2763 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002764 OPT_CALLBACK(0, "symfs", NULL, "directory",
2765 "Look for files with symbols relative to this directory",
2766 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002767 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002768 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002769 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03002770 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002771 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07002772 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04002773 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08002774 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002775 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2776 "system-wide collection from all CPUs"),
2777 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2778 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002779 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2780 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002781 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2782 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2783 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002784 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2785 "only consider symbols in these pids"),
2786 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2787 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002788 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2789 "Set the maximum stack depth when parsing the callchain, "
2790 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002791 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002792 OPT_BOOLEAN('I', "show-info", &show_full_info,
2793 "display extended information from perf.data file"),
2794 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2795 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002796 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2797 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002798 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2799 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002800 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2801 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302802 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2803 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002804 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002805 OPT_INTEGER(0, "max-blocks", &max_blocks,
2806 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002807 OPT_BOOLEAN(0, "ns", &nanosecs,
2808 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002809 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2810 "Instruction Tracing options",
2811 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002812 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2813 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002814 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2815 "Enable symbol demangling"),
2816 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2817 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002818 OPT_STRING(0, "time", &script.time_str, "str",
2819 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002820 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2821 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002822 OPT_END()
2823 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002824 const char * const script_subcommands[] = { "record", "report", NULL };
2825 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002826 "perf script [<options>]",
2827 "perf script [<options>] record <script> [<record-options>] <command>",
2828 "perf script [<options>] report <script> [script-args]",
2829 "perf script [<options>] <script> [<record-options>] <command>",
2830 "perf script [<options>] <top-script> [script-args]",
2831 NULL
2832 };
Tom Zanussi38752942009-12-15 02:53:39 -06002833
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03002834 perf_set_singlethreaded();
2835
Tom Zanussib5b87312010-11-10 08:16:51 -06002836 setup_scripting();
2837
Yunlong Song40cae2b2015-03-18 21:35:54 +08002838 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002839 PARSE_OPT_STOP_AT_NON_OPTION);
2840
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002841 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002842 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002843
Tom Zanussib5b87312010-11-10 08:16:51 -06002844 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2845 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2846 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002847 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002848 }
2849
Tom Zanussib5b87312010-11-10 08:16:51 -06002850 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2851 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2852 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002853 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002854 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002855 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002856 return -1;
2857 }
Tom Zanussi38752942009-12-15 02:53:39 -06002858 }
2859
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002860 if (itrace_synth_opts.callchain &&
2861 itrace_synth_opts.callchain_sz > scripting_max_stack)
2862 scripting_max_stack = itrace_synth_opts.callchain_sz;
2863
Ben Hutchings44e668c2010-10-10 16:10:03 +01002864 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002865 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002866
Tom Zanussib5b87312010-11-10 08:16:51 -06002867 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002868 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002869 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002870 pid_t pid;
2871
Tom Zanussib5b87312010-11-10 08:16:51 -06002872 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2873 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2874
2875 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002876 usage_with_options_msg(script_usage, options,
2877 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002878 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002879 }
2880
Tom Zanussib5b87312010-11-10 08:16:51 -06002881 if (is_top_script(argv[0])) {
2882 rep_args = argc - 1;
2883 } else {
2884 int rec_args;
2885
2886 rep_args = has_required_arg(rep_script_path);
2887 rec_args = (argc - 1) - rep_args;
2888 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002889 usage_with_options_msg(script_usage, options,
2890 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002891 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002892 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002893 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002894 }
2895
2896 if (pipe(live_pipe) < 0) {
2897 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002898 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002899 }
2900
2901 pid = fork();
2902 if (pid < 0) {
2903 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002904 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002905 }
2906
2907 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002908 j = 0;
2909
Tom Zanussia0cccc22010-04-01 23:59:25 -05002910 dup2(live_pipe[1], 1);
2911 close(live_pipe[0]);
2912
Robert Richter317df652011-11-25 15:05:25 +01002913 if (is_top_script(argv[0])) {
2914 system_wide = true;
2915 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002916 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2917 err = -1;
2918 goto out;
2919 }
Robert Richter317df652011-11-25 15:05:25 +01002920 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002921
2922 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002923 if (!__argv) {
2924 pr_err("malloc failed\n");
2925 err = -ENOMEM;
2926 goto out;
2927 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002928
Tom Zanussib5b87312010-11-10 08:16:51 -06002929 __argv[j++] = "/bin/sh";
2930 __argv[j++] = rec_script_path;
2931 if (system_wide)
2932 __argv[j++] = "-a";
2933 __argv[j++] = "-q";
2934 __argv[j++] = "-o";
2935 __argv[j++] = "-";
2936 for (i = rep_args + 1; i < argc; i++)
2937 __argv[j++] = argv[i];
2938 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002939
2940 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002941 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002942 exit(-1);
2943 }
2944
2945 dup2(live_pipe[0], 0);
2946 close(live_pipe[1]);
2947
Tom Zanussib5b87312010-11-10 08:16:51 -06002948 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002949 if (!__argv) {
2950 pr_err("malloc failed\n");
2951 err = -ENOMEM;
2952 goto out;
2953 }
2954
Tom Zanussib5b87312010-11-10 08:16:51 -06002955 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002956 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002957 __argv[j++] = rep_script_path;
2958 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002959 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002960 __argv[j++] = "-i";
2961 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002962 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002963
2964 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002965 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002966 exit(-1);
2967 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002968
Tom Zanussib5b87312010-11-10 08:16:51 -06002969 if (rec_script_path)
2970 script_path = rec_script_path;
2971 if (rep_script_path)
2972 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002973
Tom Zanussib5b87312010-11-10 08:16:51 -06002974 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002975 j = 0;
2976
Robert Richter317df652011-11-25 15:05:25 +01002977 if (!rec_script_path)
2978 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002979 else if (!system_wide) {
2980 if (have_cmd(argc - 1, &argv[1]) != 0) {
2981 err = -1;
2982 goto out;
2983 }
2984 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002985
2986 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002987 if (!__argv) {
2988 pr_err("malloc failed\n");
2989 err = -ENOMEM;
2990 goto out;
2991 }
2992
Tom Zanussib5b87312010-11-10 08:16:51 -06002993 __argv[j++] = "/bin/sh";
2994 __argv[j++] = script_path;
2995 if (system_wide)
2996 __argv[j++] = "-a";
2997 for (i = 2; i < argc; i++)
2998 __argv[j++] = argv[i];
2999 __argv[j++] = NULL;
3000
3001 execvp("/bin/sh", (char **)__argv);
3002 free(__argv);
3003 exit(-1);
3004 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003005
Tom Zanussicf4fee52010-03-03 01:04:33 -06003006 if (!script_name)
3007 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003008
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003009 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003010 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003011 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003012
Jiri Olsae90debd2013-12-09 11:02:50 +01003013 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003014 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003015 perf_session__fprintf_info(session, stdout, show_full_info);
3016 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003017 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003018 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003019 if (show_full_info)
3020 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003021
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003022 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003023 goto out_delete;
3024
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003025 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003026 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003027
Adrian Huntere2167082016-06-23 16:40:58 +03003028 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3029 itrace_synth_opts.thread_stack = true;
3030
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003031 session->itrace_synth_opts = &itrace_synth_opts;
3032
Anton Blanchard5d67be92011-07-04 21:57:50 +10003033 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003034 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3035 if (err < 0)
3036 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003037 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003038 }
3039
David Ahern1424dc92011-03-09 22:23:28 -07003040 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003041 symbol_conf.use_callchain = true;
3042 else
3043 symbol_conf.use_callchain = false;
3044
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003045 if (session->tevent.pevent &&
3046 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003047 machine__resolve_kernel_addr,
3048 &session->machines.host) < 0) {
3049 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3050 return -1;
3051 }
3052
Tom Zanussi956ffd02009-11-25 01:15:46 -06003053 if (generate_script_lang) {
3054 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003055 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003056
David Ahern2c9e45f72011-03-17 10:03:21 -06003057 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003058 fprintf(stderr,
3059 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003060 err = -EINVAL;
3061 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003062 }
3063
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003064 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003065 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003066 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003067 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003068 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003069 }
3070
3071 err = fstat(input, &perf_stat);
3072 if (err < 0) {
3073 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003074 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003075 }
3076
3077 if (!perf_stat.st_size) {
3078 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003079 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003080 }
3081
3082 scripting_ops = script_spec__lookup(generate_script_lang);
3083 if (!scripting_ops) {
3084 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003085 err = -ENOENT;
3086 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003087 }
3088
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003089 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003090 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003091 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003092 }
3093
3094 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003095 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003096 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003097 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003098 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003099 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003100 }
3101
David Ahern9cbdb702011-04-06 21:54:20 -06003102
3103 err = perf_session__check_output_opt(session);
3104 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003105 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003106
David Aherna91f4c42016-11-29 10:15:43 -07003107 /* needs to be parsed after looking up reference time */
3108 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3109 pr_err("Invalid time string\n");
3110 return -EINVAL;
3111 }
3112
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003113 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003114
Adrian Hunterd445dd22014-08-15 22:08:37 +03003115 flush_scripting();
3116
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003117out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003118 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003119 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003120
3121 if (script_started)
3122 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003123out:
3124 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003125}