blob: 725dbd3dd1044102194e1f89076e3ee072576a46 [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
614 printf("0x%"PRIx64, from);
615 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
709 printf("0x%"PRIx64, from);
710 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
1167static void
1168print_sample_bpf_output_printer(enum binary_printer_ops op,
1169 unsigned int val,
1170 void *extra)
1171{
1172 unsigned char ch = (unsigned char)val;
1173 struct printer_data *printer_data = extra;
1174
1175 switch (op) {
1176 case BINARY_PRINT_DATA_BEGIN:
1177 printf("\n");
1178 break;
1179 case BINARY_PRINT_LINE_BEGIN:
1180 printf("%17s", !printer_data->line_no ? "BPF output:" :
1181 " ");
1182 break;
1183 case BINARY_PRINT_ADDR:
1184 printf(" %04x:", val);
1185 break;
1186 case BINARY_PRINT_NUM_DATA:
1187 printf(" %02x", val);
1188 break;
1189 case BINARY_PRINT_NUM_PAD:
1190 printf(" ");
1191 break;
1192 case BINARY_PRINT_SEP:
1193 printf(" ");
1194 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)) {
1200 printf("%c", '.');
1201
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 {
1210 printf("%c", ch);
1211 }
1212 break;
1213 case BINARY_PRINT_CHAR_PAD:
1214 printf(" ");
1215 break;
1216 case BINARY_PRINT_LINE_END:
1217 printf("\n");
1218 printer_data->line_no++;
1219 break;
1220 case BINARY_PRINT_DATA_END:
1221 default:
1222 break;
1223 }
1224}
1225
1226static void print_sample_bpf_output(struct perf_sample *sample)
1227{
1228 unsigned int nr_bytes = sample->raw_size;
1229 struct printer_data printer_data = {0, false, true};
1230
1231 print_binary(sample->raw_data, nr_bytes, 8,
1232 print_sample_bpf_output_printer, &printer_data);
1233
1234 if (printer_data.is_printable && printer_data.hit_nul)
1235 printf("%17s \"%s\"\n", "BPF string:",
1236 (char *)(sample->raw_data));
1237}
1238
Adrian Hunter65c5e182017-06-30 11:36:42 +03001239static void print_sample_spacing(int len, int spacing)
1240{
1241 if (len > 0 && len < spacing)
1242 printf("%*s", spacing - len, "");
1243}
1244
1245static void print_sample_pt_spacing(int len)
1246{
1247 print_sample_spacing(len, 34);
1248}
1249
1250static void print_sample_synth_ptwrite(struct perf_sample *sample)
1251{
1252 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1253 int len;
1254
1255 if (perf_sample__bad_synth_size(sample, *data))
1256 return;
1257
1258 len = printf(" IP: %u payload: %#" PRIx64 " ",
1259 data->ip, le64_to_cpu(data->payload));
1260 print_sample_pt_spacing(len);
1261}
1262
1263static void print_sample_synth_mwait(struct perf_sample *sample)
1264{
1265 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1266 int len;
1267
1268 if (perf_sample__bad_synth_size(sample, *data))
1269 return;
1270
1271 len = printf(" hints: %#x extensions: %#x ",
1272 data->hints, data->extensions);
1273 print_sample_pt_spacing(len);
1274}
1275
1276static void print_sample_synth_pwre(struct perf_sample *sample)
1277{
1278 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1279 int len;
1280
1281 if (perf_sample__bad_synth_size(sample, *data))
1282 return;
1283
1284 len = printf(" hw: %u cstate: %u sub-cstate: %u ",
1285 data->hw, data->cstate, data->subcstate);
1286 print_sample_pt_spacing(len);
1287}
1288
1289static void print_sample_synth_exstop(struct perf_sample *sample)
1290{
1291 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1292 int len;
1293
1294 if (perf_sample__bad_synth_size(sample, *data))
1295 return;
1296
1297 len = printf(" IP: %u ", data->ip);
1298 print_sample_pt_spacing(len);
1299}
1300
1301static void print_sample_synth_pwrx(struct perf_sample *sample)
1302{
1303 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1304 int len;
1305
1306 if (perf_sample__bad_synth_size(sample, *data))
1307 return;
1308
1309 len = printf(" deepest cstate: %u last cstate: %u wake reason: %#x ",
1310 data->deepest_cstate, data->last_cstate,
1311 data->wake_reason);
1312 print_sample_pt_spacing(len);
1313}
1314
1315static void print_sample_synth_cbr(struct perf_sample *sample)
1316{
1317 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1318 unsigned int percent, freq;
1319 int len;
1320
1321 if (perf_sample__bad_synth_size(sample, *data))
1322 return;
1323
1324 freq = (le32_to_cpu(data->freq) + 500) / 1000;
1325 len = printf(" cbr: %2u freq: %4u MHz ", data->cbr, freq);
1326 if (data->max_nonturbo) {
1327 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1328 len += printf("(%3u%%) ", percent);
1329 }
1330 print_sample_pt_spacing(len);
1331}
1332
1333static void print_sample_synth(struct perf_sample *sample,
Adrian Hunter47e78082017-05-26 11:17:22 +03001334 struct perf_evsel *evsel)
1335{
1336 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001337 case PERF_SYNTH_INTEL_PTWRITE:
1338 print_sample_synth_ptwrite(sample);
1339 break;
1340 case PERF_SYNTH_INTEL_MWAIT:
1341 print_sample_synth_mwait(sample);
1342 break;
1343 case PERF_SYNTH_INTEL_PWRE:
1344 print_sample_synth_pwre(sample);
1345 break;
1346 case PERF_SYNTH_INTEL_EXSTOP:
1347 print_sample_synth_exstop(sample);
1348 break;
1349 case PERF_SYNTH_INTEL_PWRX:
1350 print_sample_synth_pwrx(sample);
1351 break;
1352 case PERF_SYNTH_INTEL_CBR:
1353 print_sample_synth_cbr(sample);
1354 break;
Adrian Hunter47e78082017-05-26 11:17:22 +03001355 default:
1356 break;
1357 }
1358}
1359
Jiri Olsa809e9422015-11-26 18:55:21 +01001360struct perf_script {
1361 struct perf_tool tool;
1362 struct perf_session *session;
1363 bool show_task_events;
1364 bool show_mmap_events;
1365 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301366 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001367 bool allocated;
1368 struct cpu_map *cpus;
1369 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001370 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001371 const char *time_str;
1372 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001373};
1374
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001375static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1376{
1377 struct perf_evsel *evsel;
1378 int max = 0;
1379
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001380 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001381 int len = strlen(perf_evsel__name(evsel));
1382
1383 max = MAX(len, max);
1384 }
1385
1386 return max;
1387}
1388
Jiri Olsac19ac912016-02-24 09:46:54 +01001389static size_t data_src__printf(u64 data_src)
1390{
1391 struct mem_info mi = { .data_src.val = data_src };
1392 char decode[100];
1393 char out[100];
1394 static int maxlen;
1395 int len;
1396
1397 perf_script__meminfo_scnprintf(decode, 100, &mi);
1398
1399 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1400 if (maxlen < len)
1401 maxlen = len;
1402
1403 return printf("%-*s", maxlen, out);
1404}
1405
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001406static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001407 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001408 struct addr_location *al,
1409 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001410{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001411 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001412 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001413 unsigned int type = output_type(attr->type);
David Ahern1424dc92011-03-09 22:23:28 -07001414
Adrian Hunter14057202017-06-21 13:17:19 +03001415 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001416 return;
1417
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001418 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001419
Jiri Olsa535aeaae2014-08-25 16:45:42 +02001420 if (PRINT_FIELD(PERIOD))
1421 printf("%10" PRIu64 " ", sample->period);
1422
Namhyung Kime944d3d2013-11-18 14:34:52 +09001423 if (PRINT_FIELD(EVNAME)) {
1424 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001425
1426 if (!script->name_width)
1427 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1428
1429 printf("%*s: ", script->name_width,
1430 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001431 }
1432
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001433 if (print_flags)
1434 print_sample_flags(sample->flags);
1435
Akihiro Nagai95582592012-01-30 13:43:09 +09001436 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001437 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001438 return;
1439 }
1440
David Ahern745f43e2011-03-09 22:23:26 -07001441 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001442 event_format__print(evsel->tp_format, sample->cpu,
1443 sample->raw_data, sample->raw_size);
Adrian Hunter47e78082017-05-26 11:17:22 +03001444
1445 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
1446 print_sample_synth(sample, evsel);
1447
David Ahern7cec0922011-05-30 13:08:23 -06001448 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001449 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001450
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001451 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001452 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001453
1454 if (PRINT_FIELD(WEIGHT))
1455 printf("%16" PRIu64, sample->weight);
1456
David Ahern787bef12011-05-27 14:28:43 -06001457 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001458 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001459
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001460 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001461 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001462 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001463 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001464
1465 putchar(cursor ? '\n' : ' ');
Adrian Hunter14057202017-06-21 13:17:19 +03001466 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001467 }
1468
Stephane Eranianfc36f942015-08-31 18:41:10 +02001469 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001470 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001471
Andi Kleenb1491ac2017-09-05 11:40:57 -07001472 if (PRINT_FIELD(UREGS))
1473 print_sample_uregs(sample, attr);
1474
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001475 if (PRINT_FIELD(BRSTACK))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001476 print_sample_brstack(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001477 else if (PRINT_FIELD(BRSTACKSYM))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001478 print_sample_brstacksym(sample, thread, attr);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001479 else if (PRINT_FIELD(BRSTACKOFF))
1480 print_sample_brstackoff(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001481
Wang Nan30372f02016-02-24 11:20:45 +00001482 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1483 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001484 print_insn(sample, attr, thread, machine);
Kan Liang49d58f02017-08-29 13:11:11 -04001485
1486 if (PRINT_FIELD(PHYS_ADDR))
1487 printf("%16" PRIx64, sample->phys_addr);
David Ahernc70c94b2011-03-09 22:23:25 -07001488 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001489}
1490
Tom Zanussi956ffd02009-11-25 01:15:46 -06001491static struct scripting_ops *scripting_ops;
1492
Jiri Olsa36e33c52016-01-06 11:49:56 +01001493static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1494{
1495 int nthreads = thread_map__nr(counter->threads);
1496 int ncpus = perf_evsel__nr_cpus(counter);
1497 int cpu, thread;
1498 static int header_printed;
1499
1500 if (counter->system_wide)
1501 nthreads = 1;
1502
1503 if (!header_printed) {
1504 printf("%3s %8s %15s %15s %15s %15s %s\n",
1505 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1506 header_printed = 1;
1507 }
1508
1509 for (thread = 0; thread < nthreads; thread++) {
1510 for (cpu = 0; cpu < ncpus; cpu++) {
1511 struct perf_counts_values *counts;
1512
1513 counts = perf_counts(counter->counts, cpu, thread);
1514
1515 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1516 counter->cpus->map[cpu],
1517 thread_map__pid(counter->threads, thread),
1518 counts->val,
1519 counts->ena,
1520 counts->run,
1521 tstamp,
1522 perf_evsel__name(counter));
1523 }
1524 }
1525}
1526
Jiri Olsae099eba2016-01-05 22:09:09 +01001527static void process_stat(struct perf_evsel *counter, u64 tstamp)
1528{
1529 if (scripting_ops && scripting_ops->process_stat)
1530 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001531 else
1532 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001533}
1534
1535static void process_stat_interval(u64 tstamp)
1536{
1537 if (scripting_ops && scripting_ops->process_stat_interval)
1538 scripting_ops->process_stat_interval(tstamp);
1539}
1540
Tom Zanussi956ffd02009-11-25 01:15:46 -06001541static void setup_scripting(void)
1542{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001543 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001544 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001545}
1546
Adrian Hunterd445dd22014-08-15 22:08:37 +03001547static int flush_scripting(void)
1548{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001549 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001550}
1551
Tom Zanussi956ffd02009-11-25 01:15:46 -06001552static int cleanup_scripting(void)
1553{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001554 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001555
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001556 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001557}
1558
Jiri Olsa809e9422015-11-26 18:55:21 +01001559static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001560 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001561 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001562 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001563 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001564{
Jiri Olsa809e9422015-11-26 18:55:21 +01001565 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001566 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001567
David Aherna91f4c42016-11-29 10:15:43 -07001568 if (perf_time__skip_sample(&scr->ptime, sample->time))
1569 return 0;
1570
David Ahern1424dc92011-03-09 22:23:28 -07001571 if (debug_mode) {
1572 if (sample->time < last_timestamp) {
1573 pr_err("Samples misordered, previous: %" PRIu64
1574 " this: %" PRIu64 "\n", last_timestamp,
1575 sample->time);
1576 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001577 }
David Ahern1424dc92011-03-09 22:23:28 -07001578 last_timestamp = sample->time;
1579 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001580 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001581
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001582 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001583 pr_err("problem processing %d event, skipping it.\n",
1584 event->header.type);
1585 return -1;
1586 }
1587
1588 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001589 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001590
Anton Blanchard5d67be92011-07-04 21:57:50 +10001591 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001592 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001593
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001594 if (scripting_ops)
1595 scripting_ops->process_event(event, sample, evsel, &al);
1596 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001597 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001598
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001599out_put:
1600 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001601 return 0;
1602}
1603
Adrian Hunter7ea95722013-11-01 15:51:30 +02001604static int process_attr(struct perf_tool *tool, union perf_event *event,
1605 struct perf_evlist **pevlist)
1606{
1607 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1608 struct perf_evlist *evlist;
1609 struct perf_evsel *evsel, *pos;
1610 int err;
1611
1612 err = perf_event__process_attr(tool, event, pevlist);
1613 if (err)
1614 return err;
1615
1616 evlist = *pevlist;
1617 evsel = perf_evlist__last(*pevlist);
1618
Adrian Hunter14057202017-06-21 13:17:19 +03001619 if (evsel->attr.type >= PERF_TYPE_MAX &&
1620 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001621 return 0;
1622
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001623 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001624 if (pos->attr.type == evsel->attr.type && pos != evsel)
1625 return 0;
1626 }
1627
1628 set_print_ip_opts(&evsel->attr);
1629
Jiri Olsad2b5a312015-10-16 12:41:25 +02001630 if (evsel->attr.sample_type)
1631 err = perf_evsel__check_attr(evsel, scr->session);
1632
1633 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001634}
1635
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001636static int process_comm_event(struct perf_tool *tool,
1637 union perf_event *event,
1638 struct perf_sample *sample,
1639 struct machine *machine)
1640{
1641 struct thread *thread;
1642 struct perf_script *script = container_of(tool, struct perf_script, tool);
1643 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001644 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001645 int ret = -1;
1646
1647 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1648 if (thread == NULL) {
1649 pr_debug("problem processing COMM event, skipping it.\n");
1650 return -1;
1651 }
1652
1653 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1654 goto out;
1655
1656 if (!evsel->attr.sample_id_all) {
1657 sample->cpu = 0;
1658 sample->time = 0;
1659 sample->tid = event->comm.tid;
1660 sample->pid = event->comm.pid;
1661 }
1662 print_sample_start(sample, thread, evsel);
1663 perf_event__fprintf(event, stdout);
1664 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001665out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001666 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001667 return ret;
1668}
1669
Hari Bathini96a44bb2017-03-08 02:12:06 +05301670static int process_namespaces_event(struct perf_tool *tool,
1671 union perf_event *event,
1672 struct perf_sample *sample,
1673 struct machine *machine)
1674{
1675 struct thread *thread;
1676 struct perf_script *script = container_of(tool, struct perf_script, tool);
1677 struct perf_session *session = script->session;
1678 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1679 int ret = -1;
1680
1681 thread = machine__findnew_thread(machine, event->namespaces.pid,
1682 event->namespaces.tid);
1683 if (thread == NULL) {
1684 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1685 return -1;
1686 }
1687
1688 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1689 goto out;
1690
1691 if (!evsel->attr.sample_id_all) {
1692 sample->cpu = 0;
1693 sample->time = 0;
1694 sample->tid = event->namespaces.tid;
1695 sample->pid = event->namespaces.pid;
1696 }
1697 print_sample_start(sample, thread, evsel);
1698 perf_event__fprintf(event, stdout);
1699 ret = 0;
1700out:
1701 thread__put(thread);
1702 return ret;
1703}
1704
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001705static int process_fork_event(struct perf_tool *tool,
1706 union perf_event *event,
1707 struct perf_sample *sample,
1708 struct machine *machine)
1709{
1710 struct thread *thread;
1711 struct perf_script *script = container_of(tool, struct perf_script, tool);
1712 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001713 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001714
1715 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1716 return -1;
1717
1718 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1719 if (thread == NULL) {
1720 pr_debug("problem processing FORK event, skipping it.\n");
1721 return -1;
1722 }
1723
1724 if (!evsel->attr.sample_id_all) {
1725 sample->cpu = 0;
1726 sample->time = event->fork.time;
1727 sample->tid = event->fork.tid;
1728 sample->pid = event->fork.pid;
1729 }
1730 print_sample_start(sample, thread, evsel);
1731 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001732 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001733
1734 return 0;
1735}
1736static int process_exit_event(struct perf_tool *tool,
1737 union perf_event *event,
1738 struct perf_sample *sample,
1739 struct machine *machine)
1740{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001741 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001742 struct thread *thread;
1743 struct perf_script *script = container_of(tool, struct perf_script, tool);
1744 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001745 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001746
1747 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1748 if (thread == NULL) {
1749 pr_debug("problem processing EXIT event, skipping it.\n");
1750 return -1;
1751 }
1752
1753 if (!evsel->attr.sample_id_all) {
1754 sample->cpu = 0;
1755 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001756 sample->tid = event->fork.tid;
1757 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001758 }
1759 print_sample_start(sample, thread, evsel);
1760 perf_event__fprintf(event, stdout);
1761
1762 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001763 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001764
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001765 thread__put(thread);
1766 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001767}
1768
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001769static int process_mmap_event(struct perf_tool *tool,
1770 union perf_event *event,
1771 struct perf_sample *sample,
1772 struct machine *machine)
1773{
1774 struct thread *thread;
1775 struct perf_script *script = container_of(tool, struct perf_script, tool);
1776 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001777 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001778
1779 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1780 return -1;
1781
1782 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1783 if (thread == NULL) {
1784 pr_debug("problem processing MMAP event, skipping it.\n");
1785 return -1;
1786 }
1787
1788 if (!evsel->attr.sample_id_all) {
1789 sample->cpu = 0;
1790 sample->time = 0;
1791 sample->tid = event->mmap.tid;
1792 sample->pid = event->mmap.pid;
1793 }
1794 print_sample_start(sample, thread, evsel);
1795 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001796 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001797 return 0;
1798}
1799
1800static int process_mmap2_event(struct perf_tool *tool,
1801 union perf_event *event,
1802 struct perf_sample *sample,
1803 struct machine *machine)
1804{
1805 struct thread *thread;
1806 struct perf_script *script = container_of(tool, struct perf_script, tool);
1807 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001808 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001809
1810 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1811 return -1;
1812
1813 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1814 if (thread == NULL) {
1815 pr_debug("problem processing MMAP2 event, skipping it.\n");
1816 return -1;
1817 }
1818
1819 if (!evsel->attr.sample_id_all) {
1820 sample->cpu = 0;
1821 sample->time = 0;
1822 sample->tid = event->mmap2.tid;
1823 sample->pid = event->mmap2.pid;
1824 }
1825 print_sample_start(sample, thread, evsel);
1826 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001827 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001828 return 0;
1829}
1830
Adrian Hunter7c148982015-07-21 12:44:06 +03001831static int process_switch_event(struct perf_tool *tool,
1832 union perf_event *event,
1833 struct perf_sample *sample,
1834 struct machine *machine)
1835{
1836 struct thread *thread;
1837 struct perf_script *script = container_of(tool, struct perf_script, tool);
1838 struct perf_session *session = script->session;
1839 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1840
1841 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1842 return -1;
1843
1844 thread = machine__findnew_thread(machine, sample->pid,
1845 sample->tid);
1846 if (thread == NULL) {
1847 pr_debug("problem processing SWITCH event, skipping it.\n");
1848 return -1;
1849 }
1850
1851 print_sample_start(sample, thread, evsel);
1852 perf_event__fprintf(event, stdout);
1853 thread__put(thread);
1854 return 0;
1855}
1856
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001857static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001858{
1859 session_done = 1;
1860}
1861
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001862static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001863{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001864 int ret;
1865
Tom Zanussic239da32010-04-01 23:59:18 -05001866 signal(SIGINT, sig_handler);
1867
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001868 /* override event processing functions */
1869 if (script->show_task_events) {
1870 script->tool.comm = process_comm_event;
1871 script->tool.fork = process_fork_event;
1872 script->tool.exit = process_exit_event;
1873 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001874 if (script->show_mmap_events) {
1875 script->tool.mmap = process_mmap_event;
1876 script->tool.mmap2 = process_mmap2_event;
1877 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001878 if (script->show_switch_events)
1879 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301880 if (script->show_namespace_events)
1881 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001882
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001883 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001884
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001885 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001886 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001887
1888 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001889}
1890
Tom Zanussi956ffd02009-11-25 01:15:46 -06001891struct script_spec {
1892 struct list_head node;
1893 struct scripting_ops *ops;
1894 char spec[0];
1895};
1896
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001897static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001898
1899static struct script_spec *script_spec__new(const char *spec,
1900 struct scripting_ops *ops)
1901{
1902 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1903
1904 if (s != NULL) {
1905 strcpy(s->spec, spec);
1906 s->ops = ops;
1907 }
1908
1909 return s;
1910}
1911
Tom Zanussi956ffd02009-11-25 01:15:46 -06001912static void script_spec__add(struct script_spec *s)
1913{
1914 list_add_tail(&s->node, &script_specs);
1915}
1916
1917static struct script_spec *script_spec__find(const char *spec)
1918{
1919 struct script_spec *s;
1920
1921 list_for_each_entry(s, &script_specs, node)
1922 if (strcasecmp(s->spec, spec) == 0)
1923 return s;
1924 return NULL;
1925}
1926
Tom Zanussi956ffd02009-11-25 01:15:46 -06001927int script_spec_register(const char *spec, struct scripting_ops *ops)
1928{
1929 struct script_spec *s;
1930
1931 s = script_spec__find(spec);
1932 if (s)
1933 return -1;
1934
Taeung Song8560bae2016-02-26 00:13:10 +09001935 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001936 if (!s)
1937 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001938 else
1939 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001940
1941 return 0;
1942}
1943
1944static struct scripting_ops *script_spec__lookup(const char *spec)
1945{
1946 struct script_spec *s = script_spec__find(spec);
1947 if (!s)
1948 return NULL;
1949
1950 return s->ops;
1951}
1952
1953static void list_available_languages(void)
1954{
1955 struct script_spec *s;
1956
1957 fprintf(stderr, "\n");
1958 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001959 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001960
1961 list_for_each_entry(s, &script_specs, node)
1962 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1963
1964 fprintf(stderr, "\n");
1965}
1966
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001967static int parse_scriptname(const struct option *opt __maybe_unused,
1968 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001969{
1970 char spec[PATH_MAX];
1971 const char *script, *ext;
1972 int len;
1973
Tom Zanussif526d682010-01-27 02:27:52 -06001974 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001975 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001976 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001977 }
1978
1979 script = strchr(str, ':');
1980 if (script) {
1981 len = script - str;
1982 if (len >= PATH_MAX) {
1983 fprintf(stderr, "invalid language specifier");
1984 return -1;
1985 }
1986 strncpy(spec, str, len);
1987 spec[len] = '\0';
1988 scripting_ops = script_spec__lookup(spec);
1989 if (!scripting_ops) {
1990 fprintf(stderr, "invalid language specifier");
1991 return -1;
1992 }
1993 script++;
1994 } else {
1995 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001996 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001997 if (!ext) {
1998 fprintf(stderr, "invalid script extension");
1999 return -1;
2000 }
2001 scripting_ops = script_spec__lookup(++ext);
2002 if (!scripting_ops) {
2003 fprintf(stderr, "invalid script extension");
2004 return -1;
2005 }
2006 }
2007
2008 script_name = strdup(script);
2009
2010 return 0;
2011}
2012
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002013static int parse_output_fields(const struct option *opt __maybe_unused,
2014 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07002015{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002016 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05002017 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06002018 int j;
David Ahern745f43e2011-03-09 22:23:26 -07002019 int rc = 0;
2020 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07002021 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07002022 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07002023
2024 if (!str)
2025 return -ENOMEM;
2026
David Ahern2c9e45f72011-03-17 10:03:21 -06002027 /* first word can state for which event type the user is specifying
2028 * the fields. If no type exists, the specified fields apply to all
2029 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002030 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002031 tok = strchr(str, ':');
2032 if (tok) {
2033 *tok = '\0';
2034 tok++;
2035 if (!strcmp(str, "hw"))
2036 type = PERF_TYPE_HARDWARE;
2037 else if (!strcmp(str, "sw"))
2038 type = PERF_TYPE_SOFTWARE;
2039 else if (!strcmp(str, "trace"))
2040 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002041 else if (!strcmp(str, "raw"))
2042 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002043 else if (!strcmp(str, "break"))
2044 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002045 else if (!strcmp(str, "synth"))
2046 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002047 else {
2048 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002049 rc = -EINVAL;
2050 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002051 }
2052
2053 if (output[type].user_set)
2054 pr_warning("Overriding previous field request for %s events.\n",
2055 event_type(type));
2056
2057 output[type].fields = 0;
2058 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002059 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002060
2061 } else {
2062 tok = str;
2063 if (strlen(str) == 0) {
2064 fprintf(stderr,
2065 "Cannot set fields to 'none' for all event types.\n");
2066 rc = -EINVAL;
2067 goto out;
2068 }
2069
Andi Kleen36ce5652017-06-02 08:48:10 -07002070 /* Don't override defaults for +- */
2071 if (strchr(str, '+') || strchr(str, '-'))
2072 goto parse;
2073
David Ahern2c9e45f72011-03-17 10:03:21 -06002074 if (output_set_by_user())
2075 pr_warning("Overriding previous field request for all events.\n");
2076
Adrian Hunter14057202017-06-21 13:17:19 +03002077 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002078 output[j].fields = 0;
2079 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002080 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002081 }
David Ahern1424dc92011-03-09 22:23:28 -07002082 }
2083
Andi Kleen36ce5652017-06-02 08:48:10 -07002084parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002085 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002086 if (*tok == '+') {
2087 if (change == SET)
2088 goto out_badmix;
2089 change = ADD;
2090 tok++;
2091 } else if (*tok == '-') {
2092 if (change == SET)
2093 goto out_badmix;
2094 change = REMOVE;
2095 tok++;
2096 } else {
2097 if (change != SET && change != DEFAULT)
2098 goto out_badmix;
2099 change = SET;
2100 }
2101
David Ahern745f43e2011-03-09 22:23:26 -07002102 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002103 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002104 break;
David Ahern745f43e2011-03-09 22:23:26 -07002105 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002106 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002107 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002108 continue;
2109 }
David Ahern745f43e2011-03-09 22:23:26 -07002110 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002111 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002112 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002113 goto out;
2114 }
2115
2116 if (type == -1) {
2117 /* add user option to all events types for
2118 * which it is valid
2119 */
Adrian Hunter14057202017-06-21 13:17:19 +03002120 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002121 if (output[j].invalid_fields & all_output_options[i].field) {
2122 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2123 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002124 } else {
2125 if (change == REMOVE)
2126 output[j].fields &= ~all_output_options[i].field;
2127 else
2128 output[j].fields |= all_output_options[i].field;
2129 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002130 }
2131 } else {
2132 if (output[type].invalid_fields & all_output_options[i].field) {
2133 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2134 all_output_options[i].str, event_type(type));
2135
2136 rc = -EINVAL;
2137 goto out;
2138 }
2139 output[type].fields |= all_output_options[i].field;
2140 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002141 }
2142
2143 if (type >= 0) {
2144 if (output[type].fields == 0) {
2145 pr_debug("No fields requested for %s type. "
2146 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002147 }
David Ahern1424dc92011-03-09 22:23:28 -07002148 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002149 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002150
Andi Kleen36ce5652017-06-02 08:48:10 -07002151out_badmix:
2152 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2153 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002154out:
David Ahern745f43e2011-03-09 22:23:26 -07002155 free(str);
2156 return rc;
2157}
2158
Shawn Bohrer008f29d2010-11-21 10:09:39 -06002159/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2160static int is_directory(const char *base_path, const struct dirent *dent)
2161{
2162 char path[PATH_MAX];
2163 struct stat st;
2164
2165 sprintf(path, "%s/%s", base_path, dent->d_name);
2166 if (stat(path, &st))
2167 return 0;
2168
2169 return S_ISDIR(st.st_mode);
2170}
2171
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002172#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2173 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2174 if ((lang_dirent->d_type == DT_DIR || \
2175 (lang_dirent->d_type == DT_UNKNOWN && \
2176 is_directory(scripts_path, lang_dirent))) && \
2177 (strcmp(lang_dirent->d_name, ".")) && \
2178 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002179
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002180#define for_each_script(lang_path, lang_dir, script_dirent) \
2181 while ((script_dirent = readdir(lang_dir)) != NULL) \
2182 if (script_dirent->d_type != DT_DIR && \
2183 (script_dirent->d_type != DT_UNKNOWN || \
2184 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002185
2186
2187#define RECORD_SUFFIX "-record"
2188#define REPORT_SUFFIX "-report"
2189
2190struct script_desc {
2191 struct list_head node;
2192 char *name;
2193 char *half_liner;
2194 char *args;
2195};
2196
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002197static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002198
2199static struct script_desc *script_desc__new(const char *name)
2200{
2201 struct script_desc *s = zalloc(sizeof(*s));
2202
Tom Zanussib5b87312010-11-10 08:16:51 -06002203 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002204 s->name = strdup(name);
2205
2206 return s;
2207}
2208
2209static void script_desc__delete(struct script_desc *s)
2210{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002211 zfree(&s->name);
2212 zfree(&s->half_liner);
2213 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002214 free(s);
2215}
2216
2217static void script_desc__add(struct script_desc *s)
2218{
2219 list_add_tail(&s->node, &script_descs);
2220}
2221
2222static struct script_desc *script_desc__find(const char *name)
2223{
2224 struct script_desc *s;
2225
2226 list_for_each_entry(s, &script_descs, node)
2227 if (strcasecmp(s->name, name) == 0)
2228 return s;
2229 return NULL;
2230}
2231
2232static struct script_desc *script_desc__findnew(const char *name)
2233{
2234 struct script_desc *s = script_desc__find(name);
2235
2236 if (s)
2237 return s;
2238
2239 s = script_desc__new(name);
2240 if (!s)
Dan Carpenter2ec5cab62017-07-22 10:36:10 +03002241 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002242
2243 script_desc__add(s);
2244
2245 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002246}
2247
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002248static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002249{
2250 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002251 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002252
2253 if (strlen(str) > suffix_len) {
2254 p = str + strlen(str) - suffix_len;
2255 if (!strncmp(p, suffix, suffix_len))
2256 return p;
2257 }
2258
2259 return NULL;
2260}
2261
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002262static int read_script_info(struct script_desc *desc, const char *filename)
2263{
2264 char line[BUFSIZ], *p;
2265 FILE *fp;
2266
2267 fp = fopen(filename, "r");
2268 if (!fp)
2269 return -1;
2270
2271 while (fgets(line, sizeof(line), fp)) {
2272 p = ltrim(line);
2273 if (strlen(p) == 0)
2274 continue;
2275 if (*p != '#')
2276 continue;
2277 p++;
2278 if (strlen(p) && *p == '!')
2279 continue;
2280
2281 p = ltrim(p);
2282 if (strlen(p) && p[strlen(p) - 1] == '\n')
2283 p[strlen(p) - 1] = '\0';
2284
2285 if (!strncmp(p, "description:", strlen("description:"))) {
2286 p += strlen("description:");
2287 desc->half_liner = strdup(ltrim(p));
2288 continue;
2289 }
2290
2291 if (!strncmp(p, "args:", strlen("args:"))) {
2292 p += strlen("args:");
2293 desc->args = strdup(ltrim(p));
2294 continue;
2295 }
2296 }
2297
2298 fclose(fp);
2299
2300 return 0;
2301}
2302
Robert Richter38efb532011-11-25 11:38:40 +01002303static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2304{
2305 char *script_root, *str;
2306
2307 script_root = strdup(script_dirent->d_name);
2308 if (!script_root)
2309 return NULL;
2310
2311 str = (char *)ends_with(script_root, suffix);
2312 if (!str) {
2313 free(script_root);
2314 return NULL;
2315 }
2316
2317 *str = '\0';
2318 return script_root;
2319}
2320
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002321static int list_available_scripts(const struct option *opt __maybe_unused,
2322 const char *s __maybe_unused,
2323 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002324{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002325 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002326 char scripts_path[MAXPATHLEN];
2327 DIR *scripts_dir, *lang_dir;
2328 char script_path[MAXPATHLEN];
2329 char lang_path[MAXPATHLEN];
2330 struct script_desc *desc;
2331 char first_half[BUFSIZ];
2332 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002333
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002334 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002335
2336 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002337 if (!scripts_dir) {
2338 fprintf(stdout,
2339 "open(%s) failed.\n"
2340 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2341 scripts_path);
2342 exit(-1);
2343 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002344
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002345 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002346 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002347 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002348 lang_dir = opendir(lang_path);
2349 if (!lang_dir)
2350 continue;
2351
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002352 for_each_script(lang_path, lang_dir, script_dirent) {
2353 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002354 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002355 desc = script_desc__findnew(script_root);
2356 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002357 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002358 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002359 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002360 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002361 }
2362 }
2363
2364 fprintf(stdout, "List of available trace scripts:\n");
2365 list_for_each_entry(desc, &script_descs, node) {
2366 sprintf(first_half, "%s %s", desc->name,
2367 desc->args ? desc->args : "");
2368 fprintf(stdout, " %-36s %s\n", first_half,
2369 desc->half_liner ? desc->half_liner : "");
2370 }
2371
2372 exit(0);
2373}
2374
Feng Tange5f37052012-09-07 16:42:26 +08002375/*
Feng Tang49e639e2012-10-30 11:56:03 +08002376 * Some scripts specify the required events in their "xxx-record" file,
2377 * this function will check if the events in perf.data match those
2378 * mentioned in the "xxx-record".
2379 *
2380 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2381 * which is covered well now. And new parsing code should be added to
2382 * cover the future complexing formats like event groups etc.
2383 */
2384static int check_ev_match(char *dir_name, char *scriptname,
2385 struct perf_session *session)
2386{
2387 char filename[MAXPATHLEN], evname[128];
2388 char line[BUFSIZ], *p;
2389 struct perf_evsel *pos;
2390 int match, len;
2391 FILE *fp;
2392
2393 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2394
2395 fp = fopen(filename, "r");
2396 if (!fp)
2397 return -1;
2398
2399 while (fgets(line, sizeof(line), fp)) {
2400 p = ltrim(line);
2401 if (*p == '#')
2402 continue;
2403
2404 while (strlen(p)) {
2405 p = strstr(p, "-e");
2406 if (!p)
2407 break;
2408
2409 p += 2;
2410 p = ltrim(p);
2411 len = strcspn(p, " \t");
2412 if (!len)
2413 break;
2414
2415 snprintf(evname, len + 1, "%s", p);
2416
2417 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002418 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002419 if (!strcmp(perf_evsel__name(pos), evname)) {
2420 match = 1;
2421 break;
2422 }
2423 }
2424
2425 if (!match) {
2426 fclose(fp);
2427 return -1;
2428 }
2429 }
2430 }
2431
2432 fclose(fp);
2433 return 0;
2434}
2435
2436/*
Feng Tange5f37052012-09-07 16:42:26 +08002437 * Return -1 if none is found, otherwise the actual scripts number.
2438 *
2439 * Currently the only user of this function is the script browser, which
2440 * will list all statically runnable scripts, select one, execute it and
2441 * show the output in a perf browser.
2442 */
2443int find_scripts(char **scripts_array, char **scripts_path_array)
2444{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002445 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002446 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002447 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002448 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002449 struct perf_data_file file = {
2450 .path = input_name,
2451 .mode = PERF_DATA_MODE_READ,
2452 };
Feng Tange5f37052012-09-07 16:42:26 +08002453 char *temp;
2454 int i = 0;
2455
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002456 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002457 if (!session)
2458 return -1;
2459
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002460 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002461
2462 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002463 if (!scripts_dir) {
2464 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002465 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002466 }
Feng Tange5f37052012-09-07 16:42:26 +08002467
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002468 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002469 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002470 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002471#ifdef NO_LIBPERL
2472 if (strstr(lang_path, "perl"))
2473 continue;
2474#endif
2475#ifdef NO_LIBPYTHON
2476 if (strstr(lang_path, "python"))
2477 continue;
2478#endif
2479
2480 lang_dir = opendir(lang_path);
2481 if (!lang_dir)
2482 continue;
2483
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002484 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002485 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002486 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002487 continue;
2488 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002489 script_dirent->d_name);
2490 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002491 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002492 (temp - script_dirent->d_name) + 1,
2493 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002494
2495 if (check_ev_match(lang_path,
2496 scripts_array[i], session))
2497 continue;
2498
Feng Tange5f37052012-09-07 16:42:26 +08002499 i++;
2500 }
Feng Tang49e639e2012-10-30 11:56:03 +08002501 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002502 }
2503
Feng Tang49e639e2012-10-30 11:56:03 +08002504 closedir(scripts_dir);
2505 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002506 return i;
2507}
2508
Tom Zanussi38752942009-12-15 02:53:39 -06002509static char *get_script_path(const char *script_root, const char *suffix)
2510{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002511 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002512 char scripts_path[MAXPATHLEN];
2513 char script_path[MAXPATHLEN];
2514 DIR *scripts_dir, *lang_dir;
2515 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002516 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002517
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002518 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002519
2520 scripts_dir = opendir(scripts_path);
2521 if (!scripts_dir)
2522 return NULL;
2523
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002524 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002525 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002526 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002527 lang_dir = opendir(lang_path);
2528 if (!lang_dir)
2529 continue;
2530
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002531 for_each_script(lang_path, lang_dir, script_dirent) {
2532 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002533 if (__script_root && !strcmp(script_root, __script_root)) {
2534 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002535 closedir(lang_dir);
2536 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002537 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002538 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002539 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002540 }
2541 free(__script_root);
2542 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002543 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002544 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002545 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002546
Robert Richter38efb532011-11-25 11:38:40 +01002547 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002548}
2549
Tom Zanussib5b87312010-11-10 08:16:51 -06002550static bool is_top_script(const char *script_path)
2551{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002552 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002553}
2554
2555static int has_required_arg(char *script_path)
2556{
2557 struct script_desc *desc;
2558 int n_args = 0;
2559 char *p;
2560
2561 desc = script_desc__new(NULL);
2562
2563 if (read_script_info(desc, script_path))
2564 goto out;
2565
2566 if (!desc->args)
2567 goto out;
2568
2569 for (p = desc->args; *p; p++)
2570 if (*p == '<')
2571 n_args++;
2572out:
2573 script_desc__delete(desc);
2574
2575 return n_args;
2576}
2577
David Ahernd54b1a92012-08-26 12:24:46 -06002578static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002579{
2580 char **__argv = malloc(sizeof(const char *) * argc);
2581
David Ahernd54b1a92012-08-26 12:24:46 -06002582 if (!__argv) {
2583 pr_err("malloc failed\n");
2584 return -1;
2585 }
2586
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002587 memcpy(__argv, argv, sizeof(const char *) * argc);
2588 argc = parse_options(argc, (const char **)__argv, record_options,
2589 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2590 free(__argv);
2591
David Ahernd54b1a92012-08-26 12:24:46 -06002592 system_wide = (argc == 0);
2593
2594 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002595}
2596
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002597static void script__setup_sample_type(struct perf_script *script)
2598{
2599 struct perf_session *session = script->session;
2600 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2601
2602 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2603 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
2604 (sample_type & PERF_SAMPLE_STACK_USER))
2605 callchain_param.record_mode = CALLCHAIN_DWARF;
2606 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
2607 callchain_param.record_mode = CALLCHAIN_LBR;
2608 else
2609 callchain_param.record_mode = CALLCHAIN_FP;
2610 }
2611}
2612
Jiri Olsae099eba2016-01-05 22:09:09 +01002613static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2614 union perf_event *event,
2615 struct perf_session *session)
2616{
2617 struct stat_round_event *round = &event->stat_round;
2618 struct perf_evsel *counter;
2619
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002620 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002621 perf_stat_process_counter(&stat_config, counter);
2622 process_stat(counter, round->time);
2623 }
2624
2625 process_stat_interval(round->time);
2626 return 0;
2627}
2628
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002629static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2630 union perf_event *event,
2631 struct perf_session *session __maybe_unused)
2632{
2633 perf_event__read_stat_config(&stat_config, &event->stat_config);
2634 return 0;
2635}
2636
Jiri Olsacfc88742016-01-05 22:09:06 +01002637static int set_maps(struct perf_script *script)
2638{
2639 struct perf_evlist *evlist = script->session->evlist;
2640
2641 if (!script->cpus || !script->threads)
2642 return 0;
2643
2644 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2645 return -EINVAL;
2646
2647 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2648
2649 if (perf_evlist__alloc_stats(evlist, true))
2650 return -ENOMEM;
2651
2652 script->allocated = true;
2653 return 0;
2654}
2655
2656static
2657int process_thread_map_event(struct perf_tool *tool,
2658 union perf_event *event,
2659 struct perf_session *session __maybe_unused)
2660{
2661 struct perf_script *script = container_of(tool, struct perf_script, tool);
2662
2663 if (script->threads) {
2664 pr_warning("Extra thread map event, ignoring.\n");
2665 return 0;
2666 }
2667
2668 script->threads = thread_map__new_event(&event->thread_map);
2669 if (!script->threads)
2670 return -ENOMEM;
2671
2672 return set_maps(script);
2673}
2674
2675static
2676int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2677 union perf_event *event,
2678 struct perf_session *session __maybe_unused)
2679{
2680 struct perf_script *script = container_of(tool, struct perf_script, tool);
2681
2682 if (script->cpus) {
2683 pr_warning("Extra cpu map event, ignoring.\n");
2684 return 0;
2685 }
2686
2687 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2688 if (!script->cpus)
2689 return -ENOMEM;
2690
2691 return set_maps(script);
2692}
2693
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002694int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002695{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002696 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002697 bool header = false;
2698 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002699 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002700 char *rec_script_path = NULL;
2701 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002702 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002703 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002704 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002705 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002706 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002707 struct perf_script script = {
2708 .tool = {
2709 .sample = process_sample_event,
2710 .mmap = perf_event__process_mmap,
2711 .mmap2 = perf_event__process_mmap2,
2712 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302713 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002714 .exit = perf_event__process_exit,
2715 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002716 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002717 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002718 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002719 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002720 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002721 .id_index = perf_event__process_id_index,
2722 .auxtrace_info = perf_event__process_auxtrace_info,
2723 .auxtrace = perf_event__process_auxtrace,
2724 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002725 .stat = perf_event__process_stat_event,
2726 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002727 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002728 .thread_map = process_thread_map_event,
2729 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002730 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002731 .ordering_requires_timestamps = true,
2732 },
2733 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002734 struct perf_data_file file = {
2735 .mode = PERF_DATA_MODE_READ,
2736 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002737 const struct option options[] = {
2738 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2739 "dump raw trace in ASCII"),
2740 OPT_INCR('v', "verbose", &verbose,
2741 "be more verbose (show symbol address, etc)"),
2742 OPT_BOOLEAN('L', "Latency", &latency_format,
2743 "show latency attributes (irqs/preemption disabled, etc)"),
2744 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2745 list_available_scripts),
2746 OPT_CALLBACK('s', "script", NULL, "name",
2747 "script file name (lang:script name, script name, or *)",
2748 parse_scriptname),
2749 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2750 "generate perf-script.xx script in specified language"),
2751 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2752 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2753 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002754 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2755 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002756 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2757 "file", "vmlinux pathname"),
2758 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2759 "file", "kallsyms pathname"),
2760 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2761 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002762 OPT_CALLBACK(0, "symfs", NULL, "directory",
2763 "Look for files with symbols relative to this directory",
2764 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002765 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002766 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002767 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03002768 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002769 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Andi Kleenb1491ac2017-09-05 11:40:57 -07002770 "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04002771 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08002772 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002773 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2774 "system-wide collection from all CPUs"),
2775 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2776 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002777 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2778 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002779 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2780 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2781 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002782 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2783 "only consider symbols in these pids"),
2784 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2785 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002786 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2787 "Set the maximum stack depth when parsing the callchain, "
2788 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002789 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002790 OPT_BOOLEAN('I', "show-info", &show_full_info,
2791 "display extended information from perf.data file"),
2792 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2793 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002794 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2795 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002796 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2797 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002798 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2799 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302800 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2801 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002802 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002803 OPT_INTEGER(0, "max-blocks", &max_blocks,
2804 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002805 OPT_BOOLEAN(0, "ns", &nanosecs,
2806 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002807 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2808 "Instruction Tracing options",
2809 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002810 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2811 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002812 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2813 "Enable symbol demangling"),
2814 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2815 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002816 OPT_STRING(0, "time", &script.time_str, "str",
2817 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002818 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2819 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002820 OPT_END()
2821 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002822 const char * const script_subcommands[] = { "record", "report", NULL };
2823 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002824 "perf script [<options>]",
2825 "perf script [<options>] record <script> [<record-options>] <command>",
2826 "perf script [<options>] report <script> [script-args]",
2827 "perf script [<options>] <script> [<record-options>] <command>",
2828 "perf script [<options>] <top-script> [script-args]",
2829 NULL
2830 };
Tom Zanussi38752942009-12-15 02:53:39 -06002831
Tom Zanussib5b87312010-11-10 08:16:51 -06002832 setup_scripting();
2833
Yunlong Song40cae2b2015-03-18 21:35:54 +08002834 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002835 PARSE_OPT_STOP_AT_NON_OPTION);
2836
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002837 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002838 file.force = symbol_conf.force;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002839
Tom Zanussib5b87312010-11-10 08:16:51 -06002840 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2841 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2842 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002843 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002844 }
2845
Tom Zanussib5b87312010-11-10 08:16:51 -06002846 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2847 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2848 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002849 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002850 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002851 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002852 return -1;
2853 }
Tom Zanussi38752942009-12-15 02:53:39 -06002854 }
2855
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002856 if (itrace_synth_opts.callchain &&
2857 itrace_synth_opts.callchain_sz > scripting_max_stack)
2858 scripting_max_stack = itrace_synth_opts.callchain_sz;
2859
Ben Hutchings44e668c2010-10-10 16:10:03 +01002860 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002861 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002862
Tom Zanussib5b87312010-11-10 08:16:51 -06002863 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002864 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002865 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002866 pid_t pid;
2867
Tom Zanussib5b87312010-11-10 08:16:51 -06002868 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2869 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2870
2871 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002872 usage_with_options_msg(script_usage, options,
2873 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002874 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002875 }
2876
Tom Zanussib5b87312010-11-10 08:16:51 -06002877 if (is_top_script(argv[0])) {
2878 rep_args = argc - 1;
2879 } else {
2880 int rec_args;
2881
2882 rep_args = has_required_arg(rep_script_path);
2883 rec_args = (argc - 1) - rep_args;
2884 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002885 usage_with_options_msg(script_usage, options,
2886 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002887 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002888 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002889 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002890 }
2891
2892 if (pipe(live_pipe) < 0) {
2893 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002894 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002895 }
2896
2897 pid = fork();
2898 if (pid < 0) {
2899 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002900 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002901 }
2902
2903 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002904 j = 0;
2905
Tom Zanussia0cccc22010-04-01 23:59:25 -05002906 dup2(live_pipe[1], 1);
2907 close(live_pipe[0]);
2908
Robert Richter317df652011-11-25 15:05:25 +01002909 if (is_top_script(argv[0])) {
2910 system_wide = true;
2911 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002912 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2913 err = -1;
2914 goto out;
2915 }
Robert Richter317df652011-11-25 15:05:25 +01002916 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002917
2918 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002919 if (!__argv) {
2920 pr_err("malloc failed\n");
2921 err = -ENOMEM;
2922 goto out;
2923 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002924
Tom Zanussib5b87312010-11-10 08:16:51 -06002925 __argv[j++] = "/bin/sh";
2926 __argv[j++] = rec_script_path;
2927 if (system_wide)
2928 __argv[j++] = "-a";
2929 __argv[j++] = "-q";
2930 __argv[j++] = "-o";
2931 __argv[j++] = "-";
2932 for (i = rep_args + 1; i < argc; i++)
2933 __argv[j++] = argv[i];
2934 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002935
2936 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002937 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002938 exit(-1);
2939 }
2940
2941 dup2(live_pipe[0], 0);
2942 close(live_pipe[1]);
2943
Tom Zanussib5b87312010-11-10 08:16:51 -06002944 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002945 if (!__argv) {
2946 pr_err("malloc failed\n");
2947 err = -ENOMEM;
2948 goto out;
2949 }
2950
Tom Zanussib5b87312010-11-10 08:16:51 -06002951 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002952 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002953 __argv[j++] = rep_script_path;
2954 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002955 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002956 __argv[j++] = "-i";
2957 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002958 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002959
2960 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002961 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002962 exit(-1);
2963 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002964
Tom Zanussib5b87312010-11-10 08:16:51 -06002965 if (rec_script_path)
2966 script_path = rec_script_path;
2967 if (rep_script_path)
2968 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002969
Tom Zanussib5b87312010-11-10 08:16:51 -06002970 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002971 j = 0;
2972
Robert Richter317df652011-11-25 15:05:25 +01002973 if (!rec_script_path)
2974 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002975 else if (!system_wide) {
2976 if (have_cmd(argc - 1, &argv[1]) != 0) {
2977 err = -1;
2978 goto out;
2979 }
2980 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002981
2982 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002983 if (!__argv) {
2984 pr_err("malloc failed\n");
2985 err = -ENOMEM;
2986 goto out;
2987 }
2988
Tom Zanussib5b87312010-11-10 08:16:51 -06002989 __argv[j++] = "/bin/sh";
2990 __argv[j++] = script_path;
2991 if (system_wide)
2992 __argv[j++] = "-a";
2993 for (i = 2; i < argc; i++)
2994 __argv[j++] = argv[i];
2995 __argv[j++] = NULL;
2996
2997 execvp("/bin/sh", (char **)__argv);
2998 free(__argv);
2999 exit(-1);
3000 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003001
Tom Zanussicf4fee52010-03-03 01:04:33 -06003002 if (!script_name)
3003 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003004
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003005 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003006 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003007 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003008
Jiri Olsae90debd2013-12-09 11:02:50 +01003009 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003010 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01003011 perf_session__fprintf_info(session, stdout, show_full_info);
3012 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003013 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01003014 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07003015 if (show_full_info)
3016 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01003017
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003018 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09003019 goto out_delete;
3020
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003021 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02003022 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003023
Adrian Huntere2167082016-06-23 16:40:58 +03003024 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
3025 itrace_synth_opts.thread_stack = true;
3026
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003027 session->itrace_synth_opts = &itrace_synth_opts;
3028
Anton Blanchard5d67be92011-07-04 21:57:50 +10003029 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003030 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3031 if (err < 0)
3032 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003033 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003034 }
3035
David Ahern1424dc92011-03-09 22:23:28 -07003036 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003037 symbol_conf.use_callchain = true;
3038 else
3039 symbol_conf.use_callchain = false;
3040
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003041 if (session->tevent.pevent &&
3042 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003043 machine__resolve_kernel_addr,
3044 &session->machines.host) < 0) {
3045 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3046 return -1;
3047 }
3048
Tom Zanussi956ffd02009-11-25 01:15:46 -06003049 if (generate_script_lang) {
3050 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003051 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003052
David Ahern2c9e45f72011-03-17 10:03:21 -06003053 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003054 fprintf(stderr,
3055 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003056 err = -EINVAL;
3057 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003058 }
3059
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003060 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003061 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003062 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003063 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003064 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003065 }
3066
3067 err = fstat(input, &perf_stat);
3068 if (err < 0) {
3069 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003070 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003071 }
3072
3073 if (!perf_stat.st_size) {
3074 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003075 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003076 }
3077
3078 scripting_ops = script_spec__lookup(generate_script_lang);
3079 if (!scripting_ops) {
3080 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003081 err = -ENOENT;
3082 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003083 }
3084
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003085 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003086 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003087 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003088 }
3089
3090 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003091 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003092 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003093 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003094 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003095 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003096 }
3097
David Ahern9cbdb702011-04-06 21:54:20 -06003098
3099 err = perf_session__check_output_opt(session);
3100 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003101 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003102
David Aherna91f4c42016-11-29 10:15:43 -07003103 /* needs to be parsed after looking up reference time */
3104 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3105 pr_err("Invalid time string\n");
3106 return -EINVAL;
3107 }
3108
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003109 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003110
Adrian Hunterd445dd22014-08-15 22:08:37 +03003111 flush_scripting();
3112
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003113out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003114 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003115 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003116
3117 if (script_started)
3118 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003119out:
3120 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003121}