blob: 615fdc63452ee94a6bd55b0779d7aad547faaf4d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002#include "builtin.h"
3
Andrea Gelminib7eead82010-08-05 15:51:38 +02004#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02005#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02006#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06007#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02008#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06009#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +020010#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020011#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020012#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020013#include "util/symbol.h"
14#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010015#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020016#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070017#include "util/evlist.h"
18#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080019#include "util/sort.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020020#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030021#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010022#include "util/cpumap.h"
23#include "util/thread_map.h"
24#include "util/stat.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030025#include "util/string2.h"
Adrian Huntere2167082016-06-23 16:40:58 +030026#include "util/thread-stack.h"
David Aherna91f4c42016-11-29 10:15:43 -070027#include "util/time-utils.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030028#include "print_binary.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100029#include <linux/bitmap.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030030#include <linux/kernel.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030031#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030032#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010033#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010034#include "util/mem-events.h"
Andi Kleen48d02a12017-02-23 15:46:34 -080035#include "util/dump-insn.h"
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -030036#include <dirent.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030037#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030038#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030039#include <signal.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030040#include <sys/param.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030041#include <sys/types.h>
42#include <sys/stat.h>
43#include <unistd.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020044
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030045#include "sane_ctype.h"
46
Tom Zanussi956ffd02009-11-25 01:15:46 -060047static char const *script_name;
48static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020049static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020050static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020051static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070052static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090053static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010054static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030055static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030056static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100057static const char *cpu_list;
58static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010059static struct perf_stat_config stat_config;
Andi Kleen48d02a12017-02-23 15:46:34 -080060static int max_blocks;
Tom Zanussi956ffd02009-11-25 01:15:46 -060061
Adrian Hunter44cbe722015-09-25 16:15:50 +030062unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030063
David Ahern745f43e2011-03-09 22:23:26 -070064enum perf_output_field {
65 PERF_OUTPUT_COMM = 1U << 0,
66 PERF_OUTPUT_TID = 1U << 1,
67 PERF_OUTPUT_PID = 1U << 2,
68 PERF_OUTPUT_TIME = 1U << 3,
69 PERF_OUTPUT_CPU = 1U << 4,
70 PERF_OUTPUT_EVNAME = 1U << 5,
71 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060072 PERF_OUTPUT_IP = 1U << 7,
73 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060074 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060075 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090076 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020077 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaa2014-08-25 16:45:42 +020078 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020079 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020080 PERF_OUTPUT_BRSTACK = 1U << 15,
81 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010082 PERF_OUTPUT_DATA_SRC = 1U << 17,
83 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000084 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030085 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030086 PERF_OUTPUT_INSN = 1U << 21,
87 PERF_OUTPUT_INSNLEN = 1U << 22,
Andi Kleen48d02a12017-02-23 15:46:34 -080088 PERF_OUTPUT_BRSTACKINSN = 1U << 23,
Mark Santaniello106dacd2017-06-19 09:38:25 -070089 PERF_OUTPUT_BRSTACKOFF = 1U << 24,
Adrian Hunter47e78082017-05-26 11:17:22 +030090 PERF_OUTPUT_SYNTH = 1U << 25,
Kan Liang49d58f02017-08-29 13:11:11 -040091 PERF_OUTPUT_PHYS_ADDR = 1U << 26,
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 Olsa535aeaa2014-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},
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200113 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
114 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100115 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
116 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +0000117 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +0300118 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +0300119 {.str = "insn", .field = PERF_OUTPUT_INSN},
120 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
Andi Kleen48d02a12017-02-23 15:46:34 -0800121 {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
Mark Santaniello106dacd2017-06-19 09:38:25 -0700122 {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
Adrian Hunter47e78082017-05-26 11:17:22 +0300123 {.str = "synth", .field = PERF_OUTPUT_SYNTH},
Kan Liang49d58f02017-08-29 13:11:11 -0400124 {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
David Ahern745f43e2011-03-09 22:23:26 -0700125};
126
Adrian Hunter14057202017-06-21 13:17:19 +0300127enum {
128 OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
129 OUTPUT_TYPE_MAX
130};
131
David Ahern745f43e2011-03-09 22:23:26 -0700132/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600133static struct {
134 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600135 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400136 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600137 u64 fields;
138 u64 invalid_fields;
Adrian Hunter14057202017-06-21 13:17:19 +0300139} output[OUTPUT_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700140
David Ahern2c9e45f72011-03-17 10:03:21 -0600141 [PERF_TYPE_HARDWARE] = {
142 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700143
David Ahern2c9e45f72011-03-17 10:03:21 -0600144 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
145 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600146 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200147 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
148 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600149
Wang Nan30372f02016-02-24 11:20:45 +0000150 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600151 },
152
153 [PERF_TYPE_SOFTWARE] = {
154 .user_set = false,
155
156 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
157 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600158 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200159 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000160 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600161
162 .invalid_fields = PERF_OUTPUT_TRACE,
163 },
164
165 [PERF_TYPE_TRACEPOINT] = {
166 .user_set = false,
167
168 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
169 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000170 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600171 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700172
173 [PERF_TYPE_RAW] = {
174 .user_set = false,
175
176 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
177 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600178 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200179 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100180 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
Kan Liang49d58f02017-08-29 13:11:11 -0400181 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
182 PERF_OUTPUT_PHYS_ADDR,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700183
Wang Nan30372f02016-02-24 11:20:45 +0000184 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700185 },
Wang Nan27cfef02015-12-08 02:25:43 +0000186
187 [PERF_TYPE_BREAKPOINT] = {
188 .user_set = false,
189
190 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
191 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
192 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
193 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
194 PERF_OUTPUT_PERIOD,
195
Wang Nan30372f02016-02-24 11:20:45 +0000196 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000197 },
Adrian Hunter14057202017-06-21 13:17:19 +0300198
199 [OUTPUT_TYPE_SYNTH] = {
200 .user_set = false,
201
202 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
203 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
204 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Adrian Hunter47e78082017-05-26 11:17:22 +0300205 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
206 PERF_OUTPUT_SYNTH,
Adrian Hunter14057202017-06-21 13:17:19 +0300207
208 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
209 },
David Ahern1424dc92011-03-09 22:23:28 -0700210};
David Ahern745f43e2011-03-09 22:23:26 -0700211
Adrian Hunter14057202017-06-21 13:17:19 +0300212static inline int output_type(unsigned int type)
213{
214 switch (type) {
215 case PERF_TYPE_SYNTH:
216 return OUTPUT_TYPE_SYNTH;
217 default:
218 return type;
219 }
220}
221
222static inline unsigned int attr_type(unsigned int type)
223{
224 switch (type) {
225 case OUTPUT_TYPE_SYNTH:
226 return PERF_TYPE_SYNTH;
227 default:
228 return type;
229 }
230}
231
David Ahern2c9e45f72011-03-17 10:03:21 -0600232static bool output_set_by_user(void)
233{
234 int j;
Adrian Hunter14057202017-06-21 13:17:19 +0300235 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600236 if (output[j].user_set)
237 return true;
238 }
239 return false;
240}
David Ahern745f43e2011-03-09 22:23:26 -0700241
David Ahern9cbdb702011-04-06 21:54:20 -0600242static const char *output_field2str(enum perf_output_field field)
243{
244 int i, imax = ARRAY_SIZE(all_output_options);
245 const char *str = "";
246
247 for (i = 0; i < imax; ++i) {
248 if (all_output_options[i].field == field) {
249 str = all_output_options[i].str;
250 break;
251 }
252 }
253 return str;
254}
255
Adrian Hunter14057202017-06-21 13:17:19 +0300256#define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700257
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300258static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
259 u64 sample_type, const char *sample_msg,
260 enum perf_output_field field,
261 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700262{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300263 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +0300264 int type = output_type(attr->type);
David Ahern9cbdb702011-04-06 21:54:20 -0600265 const char *evname;
266
267 if (attr->sample_type & sample_type)
268 return 0;
269
270 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300271 if (allow_user_set)
272 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300273 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600274 pr_err("Samples for '%s' event do not have %s attribute set. "
275 "Cannot print '%s' field.\n",
276 evname, sample_msg, output_field2str(field));
277 return -1;
278 }
279
280 /* user did not ask for it explicitly so remove from the default list */
281 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300282 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600283 pr_debug("Samples for '%s' event do not have %s attribute set. "
284 "Skipping '%s' field.\n",
285 evname, sample_msg, output_field2str(field));
286
287 return 0;
288}
289
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300290static int perf_evsel__check_stype(struct perf_evsel *evsel,
291 u64 sample_type, const char *sample_msg,
292 enum perf_output_field field)
293{
294 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
295 false);
296}
297
David Ahern9cbdb702011-04-06 21:54:20 -0600298static int perf_evsel__check_attr(struct perf_evsel *evsel,
299 struct perf_session *session)
300{
301 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300302 bool allow_user_set;
303
Jiri Olsae099eba2016-01-05 22:09:09 +0100304 if (perf_header__has_feat(&session->header, HEADER_STAT))
305 return 0;
306
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300307 allow_user_set = perf_header__has_feat(&session->header,
308 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600309
David Ahern1424dc92011-03-09 22:23:28 -0700310 if (PRINT_FIELD(TRACE) &&
311 !perf_session__has_traces(session, "record -R"))
312 return -EINVAL;
313
David Ahern787bef12011-05-27 14:28:43 -0600314 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300315 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
316 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700317 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700318 }
David Ahern7cec0922011-05-30 13:08:23 -0600319
320 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300321 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
322 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600323 return -EINVAL;
324
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100325 if (PRINT_FIELD(DATA_SRC) &&
326 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
327 PERF_OUTPUT_DATA_SRC))
328 return -EINVAL;
329
330 if (PRINT_FIELD(WEIGHT) &&
331 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
332 PERF_OUTPUT_WEIGHT))
333 return -EINVAL;
334
David Ahern7cec0922011-05-30 13:08:23 -0600335 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
336 pr_err("Display of symbols requested but neither sample IP nor "
337 "sample address\nis selected. Hence, no addresses to convert "
338 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600339 return -EINVAL;
340 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900341 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
342 pr_err("Display of offsets requested but symbol is not"
343 "selected.\n");
344 return -EINVAL;
345 }
Mark Santaniello55b9b502017-06-19 09:38:24 -0700346 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
Mark Santaniello106dacd2017-06-19 09:38:25 -0700347 !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
348 pr_err("Display of DSO requested but no address to convert. Select\n"
349 "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
David Ahern610723f2011-05-27 14:28:44 -0600350 return -EINVAL;
351 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200352 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
353 pr_err("Display of source line number requested but sample IP is not\n"
354 "selected. Hence, no address to lookup the source line number.\n");
355 return -EINVAL;
356 }
Andi Kleen48d02a12017-02-23 15:46:34 -0800357 if (PRINT_FIELD(BRSTACKINSN) &&
358 !(perf_evlist__combined_branch_type(session->evlist) &
359 PERF_SAMPLE_BRANCH_ANY)) {
360 pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
361 "Hint: run 'perf record -b ...'\n");
362 return -EINVAL;
363 }
David Ahern1424dc92011-03-09 22:23:28 -0700364 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300365 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
366 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700367 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700368
369 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300370 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
371 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700372 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700373
374 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300375 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
376 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700377 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600378
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200379 if (PRINT_FIELD(PERIOD) &&
380 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
381 PERF_OUTPUT_PERIOD))
382 return -EINVAL;
383
Stephane Eranianfc36f942015-08-31 18:41:10 +0200384 if (PRINT_FIELD(IREGS) &&
385 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
386 PERF_OUTPUT_IREGS))
387 return -EINVAL;
388
Kan Liang49d58f02017-08-29 13:11:11 -0400389 if (PRINT_FIELD(PHYS_ADDR) &&
390 perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
391 PERF_OUTPUT_PHYS_ADDR))
392 return -EINVAL;
393
David Ahern9cbdb702011-04-06 21:54:20 -0600394 return 0;
395}
396
Adrian Hunter7ea95722013-11-01 15:51:30 +0200397static void set_print_ip_opts(struct perf_event_attr *attr)
398{
Adrian Hunter14057202017-06-21 13:17:19 +0300399 unsigned int type = output_type(attr->type);
Adrian Hunter7ea95722013-11-01 15:51:30 +0200400
401 output[type].print_ip_opts = 0;
402 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300403 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200404
405 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300406 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200407
408 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300409 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200410
411 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300412 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200413
414 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300415 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200416}
417
David Ahern9cbdb702011-04-06 21:54:20 -0600418/*
419 * verify all user requested events exist and the samples
420 * have the expected data
421 */
422static int perf_session__check_output_opt(struct perf_session *session)
423{
He Kuang40f20e52016-05-16 04:51:19 +0000424 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600425 struct perf_evsel *evsel;
426
Adrian Hunter14057202017-06-21 13:17:19 +0300427 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
428 evsel = perf_session__find_first_evtype(session, attr_type(j));
David Ahern9cbdb702011-04-06 21:54:20 -0600429
430 /*
431 * even if fields is set to 0 (ie., show nothing) event must
432 * exist if user explicitly includes it on the command line
433 */
Adrian Hunter14057202017-06-21 13:17:19 +0300434 if (!evsel && output[j].user_set && !output[j].wildcard_set &&
435 j != OUTPUT_TYPE_SYNTH) {
David Ahern9cbdb702011-04-06 21:54:20 -0600436 pr_err("%s events do not exist. "
Adrian Hunter701516a2017-05-26 11:17:20 +0300437 "Remove corresponding -F option to proceed.\n",
David Ahern9cbdb702011-04-06 21:54:20 -0600438 event_type(j));
439 return -1;
440 }
441
442 if (evsel && output[j].fields &&
443 perf_evsel__check_attr(evsel, session))
444 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400445
446 if (evsel == NULL)
447 continue;
448
Adrian Hunter7ea95722013-11-01 15:51:30 +0200449 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700450 }
451
Adrian Hunter98526ee2014-07-31 09:00:59 +0300452 if (!no_callchain) {
453 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000454 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300455
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300456 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000457 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300458 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
459 use_callchain = true;
460 break;
461 }
462 }
He Kuang71ac8992016-08-04 11:25:43 +0000463 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300464 symbol_conf.use_callchain = false;
465 }
466
David Ahern80b8b492013-11-19 21:07:37 -0700467 /*
468 * set default for tracepoints to print symbols only
469 * if callchains are present
470 */
471 if (symbol_conf.use_callchain &&
472 !output[PERF_TYPE_TRACEPOINT].user_set) {
473 struct perf_event_attr *attr;
474
475 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700476
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300477 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000478 if (evsel->attr.type != j)
479 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700480
He Kuang40f20e52016-05-16 04:51:19 +0000481 attr = &evsel->attr;
482
483 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
484 output[j].fields |= PERF_OUTPUT_IP;
485 output[j].fields |= PERF_OUTPUT_SYM;
486 output[j].fields |= PERF_OUTPUT_DSO;
487 set_print_ip_opts(attr);
488 goto out;
489 }
David Ahern80b8b492013-11-19 21:07:37 -0700490 }
491 }
492
493out:
David Ahern1424dc92011-03-09 22:23:28 -0700494 return 0;
495}
David Ahern745f43e2011-03-09 22:23:26 -0700496
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300497static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200498 struct perf_event_attr *attr)
499{
500 struct regs_dump *regs = &sample->intr_regs;
501 uint64_t mask = attr->sample_regs_intr;
502 unsigned i = 0, r;
503
504 if (!regs)
505 return;
506
507 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
508 u64 val = regs->regs[i++];
509 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
510 }
511}
512
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300513static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700514 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300515 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700516{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300517 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700518 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700519 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700520
David Ahern745f43e2011-03-09 22:23:26 -0700521 if (PRINT_FIELD(COMM)) {
522 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200523 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600524 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200525 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700526 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200527 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700528 }
David Ahernc70c94b2011-03-09 22:23:25 -0700529
David Ahern745f43e2011-03-09 22:23:26 -0700530 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
531 printf("%5d/%-5d ", sample->pid, sample->tid);
532 else if (PRINT_FIELD(PID))
533 printf("%5d ", sample->pid);
534 else if (PRINT_FIELD(TID))
535 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700536
David Ahern745f43e2011-03-09 22:23:26 -0700537 if (PRINT_FIELD(CPU)) {
538 if (latency_format)
539 printf("%3d ", sample->cpu);
540 else
541 printf("[%03d] ", sample->cpu);
542 }
David Ahernc70c94b2011-03-09 22:23:25 -0700543
David Ahern745f43e2011-03-09 22:23:26 -0700544 if (PRINT_FIELD(TIME)) {
545 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300546 secs = nsecs / NSEC_PER_SEC;
547 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900548
Adrian Hunter83e19862015-09-25 16:15:36 +0300549 if (nanosecs)
550 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900551 else {
552 char sample_time[32];
553 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
554 printf("%12s: ", sample_time);
555 }
David Ahern745f43e2011-03-09 22:23:26 -0700556 }
David Ahernc70c94b2011-03-09 22:23:25 -0700557}
558
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200559static inline char
560mispred_str(struct branch_entry *br)
561{
562 if (!(br->flags.mispred || br->flags.predicted))
563 return '-';
564
565 return br->flags.predicted ? 'P' : 'M';
566}
567
Mark Santaniello55b9b502017-06-19 09:38:24 -0700568static void print_sample_brstack(struct perf_sample *sample,
569 struct thread *thread,
570 struct perf_event_attr *attr)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200571{
572 struct branch_stack *br = sample->branch_stack;
Mark Santaniello55b9b502017-06-19 09:38:24 -0700573 struct addr_location alf, alt;
574 u64 i, from, to;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200575
576 if (!(br && br->nr))
577 return;
578
579 for (i = 0; i < br->nr; i++) {
Mark Santaniello55b9b502017-06-19 09:38:24 -0700580 from = br->entries[i].from;
581 to = br->entries[i].to;
582
583 if (PRINT_FIELD(DSO)) {
584 memset(&alf, 0, sizeof(alf));
585 memset(&alt, 0, sizeof(alt));
586 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
587 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
588 }
589
Mark Santanielloe9516c02017-10-06 01:07:22 -0700590 printf(" 0x%"PRIx64, from);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700591 if (PRINT_FIELD(DSO)) {
592 printf("(");
593 map__fprintf_dsoname(alf.map, stdout);
594 printf(")");
595 }
596
597 printf("/0x%"PRIx64, to);
598 if (PRINT_FIELD(DSO)) {
599 printf("(");
600 map__fprintf_dsoname(alt.map, stdout);
601 printf(")");
602 }
603
604 printf("/%c/%c/%c/%d ",
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200605 mispred_str( br->entries + i),
606 br->entries[i].flags.in_tx? 'X' : '-',
607 br->entries[i].flags.abort? 'A' : '-',
608 br->entries[i].flags.cycles);
609 }
610}
611
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300612static void print_sample_brstacksym(struct perf_sample *sample,
Mark Santaniello55b9b502017-06-19 09:38:24 -0700613 struct thread *thread,
614 struct perf_event_attr *attr)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200615{
616 struct branch_stack *br = sample->branch_stack;
617 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200618 u64 i, from, to;
619
620 if (!(br && br->nr))
621 return;
622
623 for (i = 0; i < br->nr; i++) {
624
625 memset(&alf, 0, sizeof(alf));
626 memset(&alt, 0, sizeof(alt));
627 from = br->entries[i].from;
628 to = br->entries[i].to;
629
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300630 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200631 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300632 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200633
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300634 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200635 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300636 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200637
638 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700639 if (PRINT_FIELD(DSO)) {
640 printf("(");
641 map__fprintf_dsoname(alf.map, stdout);
642 printf(")");
643 }
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200644 putchar('/');
645 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
Mark Santaniello55b9b502017-06-19 09:38:24 -0700646 if (PRINT_FIELD(DSO)) {
647 printf("(");
648 map__fprintf_dsoname(alt.map, stdout);
649 printf(")");
650 }
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200651 printf("/%c/%c/%c/%d ",
652 mispred_str( br->entries + i),
653 br->entries[i].flags.in_tx? 'X' : '-',
654 br->entries[i].flags.abort? 'A' : '-',
655 br->entries[i].flags.cycles);
656 }
657}
658
Mark Santaniello106dacd2017-06-19 09:38:25 -0700659static void print_sample_brstackoff(struct perf_sample *sample,
660 struct thread *thread,
661 struct perf_event_attr *attr)
662{
663 struct branch_stack *br = sample->branch_stack;
664 struct addr_location alf, alt;
665 u64 i, from, to;
666
667 if (!(br && br->nr))
668 return;
669
670 for (i = 0; i < br->nr; i++) {
671
672 memset(&alf, 0, sizeof(alf));
673 memset(&alt, 0, sizeof(alt));
674 from = br->entries[i].from;
675 to = br->entries[i].to;
676
677 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
678 if (alf.map && !alf.map->dso->adjust_symbols)
679 from = map__map_ip(alf.map, from);
680
681 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
682 if (alt.map && !alt.map->dso->adjust_symbols)
683 to = map__map_ip(alt.map, to);
684
Mark Santanielloe9516c02017-10-06 01:07:22 -0700685 printf(" 0x%"PRIx64, from);
Mark Santaniello106dacd2017-06-19 09:38:25 -0700686 if (PRINT_FIELD(DSO)) {
687 printf("(");
688 map__fprintf_dsoname(alf.map, stdout);
689 printf(")");
690 }
691 printf("/0x%"PRIx64, to);
692 if (PRINT_FIELD(DSO)) {
693 printf("(");
694 map__fprintf_dsoname(alt.map, stdout);
695 printf(")");
696 }
697 printf("/%c/%c/%c/%d ",
698 mispred_str(br->entries + i),
699 br->entries[i].flags.in_tx ? 'X' : '-',
700 br->entries[i].flags.abort ? 'A' : '-',
701 br->entries[i].flags.cycles);
702 }
703}
Andi Kleen48d02a12017-02-23 15:46:34 -0800704#define MAXBB 16384UL
705
706static int grab_bb(u8 *buffer, u64 start, u64 end,
707 struct machine *machine, struct thread *thread,
708 bool *is64bit, u8 *cpumode, bool last)
709{
710 long offset, len;
711 struct addr_location al;
712 bool kernel;
713
714 if (!start || !end)
715 return 0;
716
717 kernel = machine__kernel_ip(machine, start);
718 if (kernel)
719 *cpumode = PERF_RECORD_MISC_KERNEL;
720 else
721 *cpumode = PERF_RECORD_MISC_USER;
722
723 /*
724 * Block overlaps between kernel and user.
725 * This can happen due to ring filtering
726 * On Intel CPUs the entry into the kernel is filtered,
727 * but the exit is not. Let the caller patch it up.
728 */
729 if (kernel != machine__kernel_ip(machine, end)) {
730 printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
731 start, end);
732 return -ENXIO;
733 }
734
735 memset(&al, 0, sizeof(al));
736 if (end - start > MAXBB - MAXINSN) {
737 if (last)
738 printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
739 else
740 printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
741 return 0;
742 }
743
744 thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
745 if (!al.map || !al.map->dso) {
746 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
747 return 0;
748 }
749 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
750 printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
751 return 0;
752 }
753
754 /* Load maps to ensure dso->is_64_bit has been updated */
755 map__load(al.map);
756
757 offset = al.map->map_ip(al.map, start);
758 len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
759 end - start + MAXINSN);
760
761 *is64bit = al.map->dso->is_64_bit;
762 if (len <= 0)
763 printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
764 start, end);
765 return len;
766}
767
768static void print_jump(uint64_t ip, struct branch_entry *en,
769 struct perf_insn *x, u8 *inbuf, int len,
770 int insn)
771{
772 printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
773 ip,
774 dump_insn(x, ip, inbuf, len, NULL),
775 en->flags.predicted ? " PRED" : "",
776 en->flags.mispred ? " MISPRED" : "",
777 en->flags.in_tx ? " INTX" : "",
778 en->flags.abort ? " ABORT" : "");
779 if (en->flags.cycles) {
780 printf(" %d cycles", en->flags.cycles);
781 if (insn)
782 printf(" %.2f IPC", (float)insn / en->flags.cycles);
783 }
784 putchar('\n');
785}
786
787static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
788 uint64_t addr, struct symbol **lastsym,
789 struct perf_event_attr *attr)
790{
791 struct addr_location al;
792 int off;
793
794 memset(&al, 0, sizeof(al));
795
796 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
797 if (!al.map)
798 thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
799 addr, &al);
800 if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
801 return;
802
803 al.cpu = cpu;
804 al.sym = NULL;
805 if (al.map)
806 al.sym = map__find_symbol(al.map, al.addr);
807
808 if (!al.sym)
809 return;
810
811 if (al.addr < al.sym->end)
812 off = al.addr - al.sym->start;
813 else
814 off = al.addr - al.map->start - al.sym->start;
815 printf("\t%s", al.sym->name);
816 if (off)
817 printf("%+d", off);
818 putchar(':');
819 if (PRINT_FIELD(SRCLINE))
820 map__fprintf_srcline(al.map, al.addr, "\t", stdout);
821 putchar('\n');
822 *lastsym = al.sym;
823}
824
825static void print_sample_brstackinsn(struct perf_sample *sample,
826 struct thread *thread,
827 struct perf_event_attr *attr,
828 struct machine *machine)
829{
830 struct branch_stack *br = sample->branch_stack;
831 u64 start, end;
832 int i, insn, len, nr, ilen;
833 struct perf_insn x;
834 u8 buffer[MAXBB];
835 unsigned off;
836 struct symbol *lastsym = NULL;
837
838 if (!(br && br->nr))
839 return;
840 nr = br->nr;
841 if (max_blocks && nr > max_blocks + 1)
842 nr = max_blocks + 1;
843
844 x.thread = thread;
845 x.cpu = sample->cpu;
846
847 putchar('\n');
848
849 /* Handle first from jump, of which we don't know the entry. */
850 len = grab_bb(buffer, br->entries[nr-1].from,
851 br->entries[nr-1].from,
852 machine, thread, &x.is64bit, &x.cpumode, false);
853 if (len > 0) {
854 print_ip_sym(thread, x.cpumode, x.cpu,
855 br->entries[nr - 1].from, &lastsym, attr);
856 print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
857 &x, buffer, len, 0);
858 }
859
860 /* Print all blocks */
861 for (i = nr - 2; i >= 0; i--) {
862 if (br->entries[i].from || br->entries[i].to)
863 pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
864 br->entries[i].from,
865 br->entries[i].to);
866 start = br->entries[i + 1].to;
867 end = br->entries[i].from;
868
869 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
870 /* Patch up missing kernel transfers due to ring filters */
871 if (len == -ENXIO && i > 0) {
872 end = br->entries[--i].from;
873 pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
874 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
875 }
876 if (len <= 0)
877 continue;
878
879 insn = 0;
880 for (off = 0;; off += ilen) {
881 uint64_t ip = start + off;
882
883 print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
884 if (ip == end) {
885 print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
886 break;
887 } else {
888 printf("\t%016" PRIx64 "\t%s\n", ip,
889 dump_insn(&x, ip, buffer + off, len - off, &ilen));
890 if (ilen == 0)
891 break;
892 insn++;
893 }
894 }
895 }
896
897 /*
898 * Hit the branch? In this case we are already done, and the target
899 * has not been executed yet.
900 */
901 if (br->entries[0].from == sample->ip)
902 return;
903 if (br->entries[0].flags.abort)
904 return;
905
906 /*
907 * Print final block upto sample
908 */
909 start = br->entries[0].to;
910 end = sample->ip;
911 len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
912 print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
913 if (len <= 0) {
914 /* Print at least last IP if basic block did not work */
915 len = grab_bb(buffer, sample->ip, sample->ip,
916 machine, thread, &x.is64bit, &x.cpumode, false);
917 if (len <= 0)
918 return;
919
920 printf("\t%016" PRIx64 "\t%s\n", sample->ip,
921 dump_insn(&x, sample->ip, buffer, len, NULL));
922 return;
923 }
924 for (off = 0; off <= end - start; off += ilen) {
925 printf("\t%016" PRIx64 "\t%s\n", start + off,
926 dump_insn(&x, start + off, buffer + off, len - off, &ilen));
927 if (ilen == 0)
928 break;
929 }
930}
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200931
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300932static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600933 struct thread *thread,
934 struct perf_event_attr *attr)
935{
936 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600937
938 printf("%16" PRIx64, sample->addr);
939
940 if (!sample_addr_correlates_sym(attr))
941 return;
942
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300943 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600944
945 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900946 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900947 if (PRINT_FIELD(SYMOFFSET))
948 symbol__fprintf_symname_offs(al.sym, &al, stdout);
949 else
950 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600951 }
952
953 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900954 printf(" (");
955 map__fprintf_dsoname(al.map, stdout);
956 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600957 }
958}
959
Adrian Huntere2167082016-06-23 16:40:58 +0300960static void print_sample_callindent(struct perf_sample *sample,
961 struct perf_evsel *evsel,
962 struct thread *thread,
963 struct addr_location *al)
964{
965 struct perf_event_attr *attr = &evsel->attr;
966 size_t depth = thread_stack__depth(thread);
967 struct addr_location addr_al;
968 const char *name = NULL;
969 static int spacing;
970 int len = 0;
971 u64 ip = 0;
972
973 /*
974 * The 'return' has already been popped off the stack so the depth has
975 * to be adjusted to match the 'call'.
976 */
977 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
978 depth += 1;
979
980 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
981 if (sample_addr_correlates_sym(attr)) {
982 thread__resolve(thread, &addr_al, sample);
983 if (addr_al.sym)
984 name = addr_al.sym->name;
985 else
986 ip = sample->addr;
987 } else {
988 ip = sample->addr;
989 }
990 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
991 if (al->sym)
992 name = al->sym->name;
993 else
994 ip = sample->ip;
995 }
996
997 if (name)
998 len = printf("%*s%s", (int)depth * 4, "", name);
999 else if (ip)
1000 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
1001
1002 if (len < 0)
1003 return;
1004
1005 /*
1006 * Try to keep the output length from changing frequently so that the
1007 * output lines up more nicely.
1008 */
1009 if (len > spacing || (len && len < spacing - 52))
1010 spacing = round_up(len + 4, 32);
1011
1012 if (len < spacing)
1013 printf("%*s", spacing - len, "");
1014}
1015
Andi Kleen224e2c92016-10-07 16:42:27 +03001016static void print_insn(struct perf_sample *sample,
Andi Kleen48d02a12017-02-23 15:46:34 -08001017 struct perf_event_attr *attr,
1018 struct thread *thread,
1019 struct machine *machine)
Andi Kleen224e2c92016-10-07 16:42:27 +03001020{
1021 if (PRINT_FIELD(INSNLEN))
1022 printf(" ilen: %d", sample->insn_len);
1023 if (PRINT_FIELD(INSN)) {
1024 int i;
1025
1026 printf(" insn:");
1027 for (i = 0; i < sample->insn_len; i++)
1028 printf(" %02x", (unsigned char)sample->insn[i]);
1029 }
Andi Kleen48d02a12017-02-23 15:46:34 -08001030 if (PRINT_FIELD(BRSTACKINSN))
1031 print_sample_brstackinsn(sample, thread, attr, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +03001032}
1033
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001034static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +09001035 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +02001036 struct thread *thread,
Andi Kleen48d02a12017-02-23 15:46:34 -08001037 struct addr_location *al,
1038 struct machine *machine)
Akihiro Nagai95582592012-01-30 13:43:09 +09001039{
1040 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001041 unsigned int type = output_type(attr->type);
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001042 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +09001043
Adrian Huntere2167082016-06-23 16:40:58 +03001044 if (PRINT_FIELD(CALLINDENT))
1045 print_sample_callindent(sample, evsel, thread, al);
1046
Akihiro Nagai95582592012-01-30 13:43:09 +09001047 /* print branch_from information */
1048 if (PRINT_FIELD(IP)) {
Adrian Hunter14057202017-06-21 13:17:19 +03001049 unsigned int print_opts = output[type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -07001050 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001051
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001052 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001053 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001054 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001055 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001056
1057 if (cursor == NULL) {
1058 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001059 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001060 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001061 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001062 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001063 } else
1064 putchar('\n');
1065
1066 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +09001067 }
1068
Akihiro Nagai95582592012-01-30 13:43:09 +09001069 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +03001070 if (PRINT_FIELD(ADDR) ||
1071 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter14057202017-06-21 13:17:19 +03001072 !output[type].user_set)) {
Adrian Hunter578bea42014-07-22 16:17:16 +03001073 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001074 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +03001075 }
Akihiro Nagai95582592012-01-30 13:43:09 +09001076
Adrian Hunter8066be5f2014-07-22 16:17:15 +03001077 if (print_srcline_last)
1078 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
1079
Andi Kleen48d02a12017-02-23 15:46:34 -08001080 print_insn(sample, attr, thread, machine);
Andi Kleen224e2c92016-10-07 16:42:27 +03001081
Akihiro Nagai95582592012-01-30 13:43:09 +09001082 printf("\n");
1083}
1084
Adrian Hunter055cd332016-06-23 16:40:56 +03001085static struct {
1086 u32 flags;
1087 const char *name;
1088} sample_flags[] = {
1089 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
1090 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
1091 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
1092 {PERF_IP_FLAG_BRANCH, "jmp"},
1093 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
1094 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
1095 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
1096 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
1097 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
1098 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
1099 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
1100 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
1101 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
1102 {0, NULL}
1103};
1104
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001105static void print_sample_flags(u32 flags)
1106{
1107 const char *chars = PERF_IP_FLAG_CHARS;
1108 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +03001109 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
1110 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001111 char str[33];
1112 int i, pos = 0;
1113
Adrian Hunter055cd332016-06-23 16:40:56 +03001114 for (i = 0; sample_flags[i].name ; i++) {
1115 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
1116 name = sample_flags[i].name;
1117 break;
1118 }
1119 }
1120
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001121 for (i = 0; i < n; i++, flags >>= 1) {
1122 if (flags & 1)
1123 str[pos++] = chars[i];
1124 }
1125 for (; i < 32; i++, flags >>= 1) {
1126 if (flags & 1)
1127 str[pos++] = '?';
1128 }
1129 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +03001130
1131 if (name)
1132 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
1133 else
1134 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001135}
1136
Wang Nan30372f02016-02-24 11:20:45 +00001137struct printer_data {
1138 int line_no;
1139 bool hit_nul;
1140 bool is_printable;
1141};
1142
1143static void
1144print_sample_bpf_output_printer(enum binary_printer_ops op,
1145 unsigned int val,
1146 void *extra)
1147{
1148 unsigned char ch = (unsigned char)val;
1149 struct printer_data *printer_data = extra;
1150
1151 switch (op) {
1152 case BINARY_PRINT_DATA_BEGIN:
1153 printf("\n");
1154 break;
1155 case BINARY_PRINT_LINE_BEGIN:
1156 printf("%17s", !printer_data->line_no ? "BPF output:" :
1157 " ");
1158 break;
1159 case BINARY_PRINT_ADDR:
1160 printf(" %04x:", val);
1161 break;
1162 case BINARY_PRINT_NUM_DATA:
1163 printf(" %02x", val);
1164 break;
1165 case BINARY_PRINT_NUM_PAD:
1166 printf(" ");
1167 break;
1168 case BINARY_PRINT_SEP:
1169 printf(" ");
1170 break;
1171 case BINARY_PRINT_CHAR_DATA:
1172 if (printer_data->hit_nul && ch)
1173 printer_data->is_printable = false;
1174
1175 if (!isprint(ch)) {
1176 printf("%c", '.');
1177
1178 if (!printer_data->is_printable)
1179 break;
1180
1181 if (ch == '\0')
1182 printer_data->hit_nul = true;
1183 else
1184 printer_data->is_printable = false;
1185 } else {
1186 printf("%c", ch);
1187 }
1188 break;
1189 case BINARY_PRINT_CHAR_PAD:
1190 printf(" ");
1191 break;
1192 case BINARY_PRINT_LINE_END:
1193 printf("\n");
1194 printer_data->line_no++;
1195 break;
1196 case BINARY_PRINT_DATA_END:
1197 default:
1198 break;
1199 }
1200}
1201
1202static void print_sample_bpf_output(struct perf_sample *sample)
1203{
1204 unsigned int nr_bytes = sample->raw_size;
1205 struct printer_data printer_data = {0, false, true};
1206
1207 print_binary(sample->raw_data, nr_bytes, 8,
1208 print_sample_bpf_output_printer, &printer_data);
1209
1210 if (printer_data.is_printable && printer_data.hit_nul)
1211 printf("%17s \"%s\"\n", "BPF string:",
1212 (char *)(sample->raw_data));
1213}
1214
Adrian Hunter65c5e182017-06-30 11:36:42 +03001215static void print_sample_spacing(int len, int spacing)
1216{
1217 if (len > 0 && len < spacing)
1218 printf("%*s", spacing - len, "");
1219}
1220
1221static void print_sample_pt_spacing(int len)
1222{
1223 print_sample_spacing(len, 34);
1224}
1225
1226static void print_sample_synth_ptwrite(struct perf_sample *sample)
1227{
1228 struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
1229 int len;
1230
1231 if (perf_sample__bad_synth_size(sample, *data))
1232 return;
1233
1234 len = printf(" IP: %u payload: %#" PRIx64 " ",
1235 data->ip, le64_to_cpu(data->payload));
1236 print_sample_pt_spacing(len);
1237}
1238
1239static void print_sample_synth_mwait(struct perf_sample *sample)
1240{
1241 struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
1242 int len;
1243
1244 if (perf_sample__bad_synth_size(sample, *data))
1245 return;
1246
1247 len = printf(" hints: %#x extensions: %#x ",
1248 data->hints, data->extensions);
1249 print_sample_pt_spacing(len);
1250}
1251
1252static void print_sample_synth_pwre(struct perf_sample *sample)
1253{
1254 struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
1255 int len;
1256
1257 if (perf_sample__bad_synth_size(sample, *data))
1258 return;
1259
1260 len = printf(" hw: %u cstate: %u sub-cstate: %u ",
1261 data->hw, data->cstate, data->subcstate);
1262 print_sample_pt_spacing(len);
1263}
1264
1265static void print_sample_synth_exstop(struct perf_sample *sample)
1266{
1267 struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
1268 int len;
1269
1270 if (perf_sample__bad_synth_size(sample, *data))
1271 return;
1272
1273 len = printf(" IP: %u ", data->ip);
1274 print_sample_pt_spacing(len);
1275}
1276
1277static void print_sample_synth_pwrx(struct perf_sample *sample)
1278{
1279 struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
1280 int len;
1281
1282 if (perf_sample__bad_synth_size(sample, *data))
1283 return;
1284
1285 len = printf(" deepest cstate: %u last cstate: %u wake reason: %#x ",
1286 data->deepest_cstate, data->last_cstate,
1287 data->wake_reason);
1288 print_sample_pt_spacing(len);
1289}
1290
1291static void print_sample_synth_cbr(struct perf_sample *sample)
1292{
1293 struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
1294 unsigned int percent, freq;
1295 int len;
1296
1297 if (perf_sample__bad_synth_size(sample, *data))
1298 return;
1299
1300 freq = (le32_to_cpu(data->freq) + 500) / 1000;
1301 len = printf(" cbr: %2u freq: %4u MHz ", data->cbr, freq);
1302 if (data->max_nonturbo) {
1303 percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
1304 len += printf("(%3u%%) ", percent);
1305 }
1306 print_sample_pt_spacing(len);
1307}
1308
1309static void print_sample_synth(struct perf_sample *sample,
Adrian Hunter47e78082017-05-26 11:17:22 +03001310 struct perf_evsel *evsel)
1311{
1312 switch (evsel->attr.config) {
Adrian Hunter65c5e182017-06-30 11:36:42 +03001313 case PERF_SYNTH_INTEL_PTWRITE:
1314 print_sample_synth_ptwrite(sample);
1315 break;
1316 case PERF_SYNTH_INTEL_MWAIT:
1317 print_sample_synth_mwait(sample);
1318 break;
1319 case PERF_SYNTH_INTEL_PWRE:
1320 print_sample_synth_pwre(sample);
1321 break;
1322 case PERF_SYNTH_INTEL_EXSTOP:
1323 print_sample_synth_exstop(sample);
1324 break;
1325 case PERF_SYNTH_INTEL_PWRX:
1326 print_sample_synth_pwrx(sample);
1327 break;
1328 case PERF_SYNTH_INTEL_CBR:
1329 print_sample_synth_cbr(sample);
1330 break;
Adrian Hunter47e78082017-05-26 11:17:22 +03001331 default:
1332 break;
1333 }
1334}
1335
Jiri Olsa809e9422015-11-26 18:55:21 +01001336struct perf_script {
1337 struct perf_tool tool;
1338 struct perf_session *session;
1339 bool show_task_events;
1340 bool show_mmap_events;
1341 bool show_switch_events;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301342 bool show_namespace_events;
Jiri Olsacfc88742016-01-05 22:09:06 +01001343 bool allocated;
1344 struct cpu_map *cpus;
1345 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001346 int name_width;
David Aherna91f4c42016-11-29 10:15:43 -07001347 const char *time_str;
1348 struct perf_time_interval ptime;
Jiri Olsa809e9422015-11-26 18:55:21 +01001349};
1350
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001351static int perf_evlist__max_name_len(struct perf_evlist *evlist)
1352{
1353 struct perf_evsel *evsel;
1354 int max = 0;
1355
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001356 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001357 int len = strlen(perf_evsel__name(evsel));
1358
1359 max = MAX(len, max);
1360 }
1361
1362 return max;
1363}
1364
Jiri Olsac19ac912016-02-24 09:46:54 +01001365static size_t data_src__printf(u64 data_src)
1366{
1367 struct mem_info mi = { .data_src.val = data_src };
1368 char decode[100];
1369 char out[100];
1370 static int maxlen;
1371 int len;
1372
1373 perf_script__meminfo_scnprintf(decode, 100, &mi);
1374
1375 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
1376 if (maxlen < len)
1377 maxlen = len;
1378
1379 return printf("%-*s", maxlen, out);
1380}
1381
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001382static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +01001383 struct perf_sample *sample, struct perf_evsel *evsel,
Andi Kleen48d02a12017-02-23 15:46:34 -08001384 struct addr_location *al,
1385 struct machine *machine)
David Ahernbe6d8422011-03-09 22:23:23 -07001386{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -03001387 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001388 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter14057202017-06-21 13:17:19 +03001389 unsigned int type = output_type(attr->type);
David Ahern1424dc92011-03-09 22:23:28 -07001390
Adrian Hunter14057202017-06-21 13:17:19 +03001391 if (output[type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -07001392 return;
1393
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001394 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -07001395
Jiri Olsa535aeaa2014-08-25 16:45:42 +02001396 if (PRINT_FIELD(PERIOD))
1397 printf("%10" PRIu64 " ", sample->period);
1398
Namhyung Kime944d3d2013-11-18 14:34:52 +09001399 if (PRINT_FIELD(EVNAME)) {
1400 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +01001401
1402 if (!script->name_width)
1403 script->name_width = perf_evlist__max_name_len(script->session->evlist);
1404
1405 printf("%*s: ", script->name_width,
1406 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +09001407 }
1408
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001409 if (print_flags)
1410 print_sample_flags(sample->flags);
1411
Akihiro Nagai95582592012-01-30 13:43:09 +09001412 if (is_bts_event(attr)) {
Andi Kleen48d02a12017-02-23 15:46:34 -08001413 print_sample_bts(sample, evsel, thread, al, machine);
Akihiro Nagai95582592012-01-30 13:43:09 +09001414 return;
1415 }
1416
David Ahern745f43e2011-03-09 22:23:26 -07001417 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03001418 event_format__print(evsel->tp_format, sample->cpu,
1419 sample->raw_data, sample->raw_size);
Adrian Hunter47e78082017-05-26 11:17:22 +03001420
1421 if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
1422 print_sample_synth(sample, evsel);
1423
David Ahern7cec0922011-05-30 13:08:23 -06001424 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001425 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -06001426
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001427 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +01001428 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +01001429
1430 if (PRINT_FIELD(WEIGHT))
1431 printf("%16" PRIu64, sample->weight);
1432
David Ahern787bef12011-05-27 14:28:43 -06001433 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -07001434 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -04001435
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -03001436 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -07001437 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001438 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -07001439 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -03001440
1441 putchar(cursor ? '\n' : ' ');
Adrian Hunter14057202017-06-21 13:17:19 +03001442 sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -07001443 }
1444
Stephane Eranianfc36f942015-08-31 18:41:10 +02001445 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001446 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +02001447
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001448 if (PRINT_FIELD(BRSTACK))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001449 print_sample_brstack(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001450 else if (PRINT_FIELD(BRSTACKSYM))
Mark Santaniello55b9b502017-06-19 09:38:24 -07001451 print_sample_brstacksym(sample, thread, attr);
Mark Santaniello106dacd2017-06-19 09:38:25 -07001452 else if (PRINT_FIELD(BRSTACKOFF))
1453 print_sample_brstackoff(sample, thread, attr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001454
Wang Nan30372f02016-02-24 11:20:45 +00001455 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
1456 print_sample_bpf_output(sample);
Andi Kleen48d02a12017-02-23 15:46:34 -08001457 print_insn(sample, attr, thread, machine);
Kan Liang49d58f02017-08-29 13:11:11 -04001458
1459 if (PRINT_FIELD(PHYS_ADDR))
1460 printf("%16" PRIx64, sample->phys_addr);
David Ahernc70c94b2011-03-09 22:23:25 -07001461 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -07001462}
1463
Tom Zanussi956ffd02009-11-25 01:15:46 -06001464static struct scripting_ops *scripting_ops;
1465
Jiri Olsa36e33c52016-01-06 11:49:56 +01001466static void __process_stat(struct perf_evsel *counter, u64 tstamp)
1467{
1468 int nthreads = thread_map__nr(counter->threads);
1469 int ncpus = perf_evsel__nr_cpus(counter);
1470 int cpu, thread;
1471 static int header_printed;
1472
1473 if (counter->system_wide)
1474 nthreads = 1;
1475
1476 if (!header_printed) {
1477 printf("%3s %8s %15s %15s %15s %15s %s\n",
1478 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
1479 header_printed = 1;
1480 }
1481
1482 for (thread = 0; thread < nthreads; thread++) {
1483 for (cpu = 0; cpu < ncpus; cpu++) {
1484 struct perf_counts_values *counts;
1485
1486 counts = perf_counts(counter->counts, cpu, thread);
1487
1488 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
1489 counter->cpus->map[cpu],
1490 thread_map__pid(counter->threads, thread),
1491 counts->val,
1492 counts->ena,
1493 counts->run,
1494 tstamp,
1495 perf_evsel__name(counter));
1496 }
1497 }
1498}
1499
Jiri Olsae099eba2016-01-05 22:09:09 +01001500static void process_stat(struct perf_evsel *counter, u64 tstamp)
1501{
1502 if (scripting_ops && scripting_ops->process_stat)
1503 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +01001504 else
1505 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +01001506}
1507
1508static void process_stat_interval(u64 tstamp)
1509{
1510 if (scripting_ops && scripting_ops->process_stat_interval)
1511 scripting_ops->process_stat_interval(tstamp);
1512}
1513
Tom Zanussi956ffd02009-11-25 01:15:46 -06001514static void setup_scripting(void)
1515{
Tom Zanussi16c632d2009-11-25 01:15:48 -06001516 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -06001517 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001518}
1519
Adrian Hunterd445dd22014-08-15 22:08:37 +03001520static int flush_scripting(void)
1521{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001522 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +03001523}
1524
Tom Zanussi956ffd02009-11-25 01:15:46 -06001525static int cleanup_scripting(void)
1526{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001527 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001528
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001529 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001530}
1531
Jiri Olsa809e9422015-11-26 18:55:21 +01001532static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001533 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001534 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001535 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001536 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001537{
Jiri Olsa809e9422015-11-26 18:55:21 +01001538 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001539 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001540
David Aherna91f4c42016-11-29 10:15:43 -07001541 if (perf_time__skip_sample(&scr->ptime, sample->time))
1542 return 0;
1543
David Ahern1424dc92011-03-09 22:23:28 -07001544 if (debug_mode) {
1545 if (sample->time < last_timestamp) {
1546 pr_err("Samples misordered, previous: %" PRIu64
1547 " this: %" PRIu64 "\n", last_timestamp,
1548 sample->time);
1549 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001550 }
David Ahern1424dc92011-03-09 22:23:28 -07001551 last_timestamp = sample->time;
1552 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001553 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001554
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001555 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001556 pr_err("problem processing %d event, skipping it.\n",
1557 event->header.type);
1558 return -1;
1559 }
1560
1561 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001562 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001563
Anton Blanchard5d67be92011-07-04 21:57:50 +10001564 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001565 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001566
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001567 if (scripting_ops)
1568 scripting_ops->process_event(event, sample, evsel, &al);
1569 else
Andi Kleen48d02a12017-02-23 15:46:34 -08001570 process_event(scr, sample, evsel, &al, machine);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001571
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001572out_put:
1573 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001574 return 0;
1575}
1576
Adrian Hunter7ea95722013-11-01 15:51:30 +02001577static int process_attr(struct perf_tool *tool, union perf_event *event,
1578 struct perf_evlist **pevlist)
1579{
1580 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1581 struct perf_evlist *evlist;
1582 struct perf_evsel *evsel, *pos;
1583 int err;
1584
1585 err = perf_event__process_attr(tool, event, pevlist);
1586 if (err)
1587 return err;
1588
1589 evlist = *pevlist;
1590 evsel = perf_evlist__last(*pevlist);
1591
Adrian Hunter14057202017-06-21 13:17:19 +03001592 if (evsel->attr.type >= PERF_TYPE_MAX &&
1593 evsel->attr.type != PERF_TYPE_SYNTH)
Adrian Hunter7ea95722013-11-01 15:51:30 +02001594 return 0;
1595
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001596 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001597 if (pos->attr.type == evsel->attr.type && pos != evsel)
1598 return 0;
1599 }
1600
1601 set_print_ip_opts(&evsel->attr);
1602
Jiri Olsad2b5a312015-10-16 12:41:25 +02001603 if (evsel->attr.sample_type)
1604 err = perf_evsel__check_attr(evsel, scr->session);
1605
1606 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001607}
1608
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001609static int process_comm_event(struct perf_tool *tool,
1610 union perf_event *event,
1611 struct perf_sample *sample,
1612 struct machine *machine)
1613{
1614 struct thread *thread;
1615 struct perf_script *script = container_of(tool, struct perf_script, tool);
1616 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001617 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001618 int ret = -1;
1619
1620 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1621 if (thread == NULL) {
1622 pr_debug("problem processing COMM event, skipping it.\n");
1623 return -1;
1624 }
1625
1626 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1627 goto out;
1628
1629 if (!evsel->attr.sample_id_all) {
1630 sample->cpu = 0;
1631 sample->time = 0;
1632 sample->tid = event->comm.tid;
1633 sample->pid = event->comm.pid;
1634 }
1635 print_sample_start(sample, thread, evsel);
1636 perf_event__fprintf(event, stdout);
1637 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001638out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001639 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001640 return ret;
1641}
1642
Hari Bathini96a44bb2017-03-08 02:12:06 +05301643static int process_namespaces_event(struct perf_tool *tool,
1644 union perf_event *event,
1645 struct perf_sample *sample,
1646 struct machine *machine)
1647{
1648 struct thread *thread;
1649 struct perf_script *script = container_of(tool, struct perf_script, tool);
1650 struct perf_session *session = script->session;
1651 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1652 int ret = -1;
1653
1654 thread = machine__findnew_thread(machine, event->namespaces.pid,
1655 event->namespaces.tid);
1656 if (thread == NULL) {
1657 pr_debug("problem processing NAMESPACES event, skipping it.\n");
1658 return -1;
1659 }
1660
1661 if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
1662 goto out;
1663
1664 if (!evsel->attr.sample_id_all) {
1665 sample->cpu = 0;
1666 sample->time = 0;
1667 sample->tid = event->namespaces.tid;
1668 sample->pid = event->namespaces.pid;
1669 }
1670 print_sample_start(sample, thread, evsel);
1671 perf_event__fprintf(event, stdout);
1672 ret = 0;
1673out:
1674 thread__put(thread);
1675 return ret;
1676}
1677
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001678static int process_fork_event(struct perf_tool *tool,
1679 union perf_event *event,
1680 struct perf_sample *sample,
1681 struct machine *machine)
1682{
1683 struct thread *thread;
1684 struct perf_script *script = container_of(tool, struct perf_script, tool);
1685 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001686 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001687
1688 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1689 return -1;
1690
1691 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1692 if (thread == NULL) {
1693 pr_debug("problem processing FORK event, skipping it.\n");
1694 return -1;
1695 }
1696
1697 if (!evsel->attr.sample_id_all) {
1698 sample->cpu = 0;
1699 sample->time = event->fork.time;
1700 sample->tid = event->fork.tid;
1701 sample->pid = event->fork.pid;
1702 }
1703 print_sample_start(sample, thread, evsel);
1704 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001705 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001706
1707 return 0;
1708}
1709static int process_exit_event(struct perf_tool *tool,
1710 union perf_event *event,
1711 struct perf_sample *sample,
1712 struct machine *machine)
1713{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001714 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001715 struct thread *thread;
1716 struct perf_script *script = container_of(tool, struct perf_script, tool);
1717 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001718 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001719
1720 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1721 if (thread == NULL) {
1722 pr_debug("problem processing EXIT event, skipping it.\n");
1723 return -1;
1724 }
1725
1726 if (!evsel->attr.sample_id_all) {
1727 sample->cpu = 0;
1728 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001729 sample->tid = event->fork.tid;
1730 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001731 }
1732 print_sample_start(sample, thread, evsel);
1733 perf_event__fprintf(event, stdout);
1734
1735 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001736 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001737
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001738 thread__put(thread);
1739 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001740}
1741
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001742static int process_mmap_event(struct perf_tool *tool,
1743 union perf_event *event,
1744 struct perf_sample *sample,
1745 struct machine *machine)
1746{
1747 struct thread *thread;
1748 struct perf_script *script = container_of(tool, struct perf_script, tool);
1749 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001750 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001751
1752 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1753 return -1;
1754
1755 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1756 if (thread == NULL) {
1757 pr_debug("problem processing MMAP event, skipping it.\n");
1758 return -1;
1759 }
1760
1761 if (!evsel->attr.sample_id_all) {
1762 sample->cpu = 0;
1763 sample->time = 0;
1764 sample->tid = event->mmap.tid;
1765 sample->pid = event->mmap.pid;
1766 }
1767 print_sample_start(sample, thread, evsel);
1768 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001769 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001770 return 0;
1771}
1772
1773static int process_mmap2_event(struct perf_tool *tool,
1774 union perf_event *event,
1775 struct perf_sample *sample,
1776 struct machine *machine)
1777{
1778 struct thread *thread;
1779 struct perf_script *script = container_of(tool, struct perf_script, tool);
1780 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001781 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001782
1783 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1784 return -1;
1785
1786 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1787 if (thread == NULL) {
1788 pr_debug("problem processing MMAP2 event, skipping it.\n");
1789 return -1;
1790 }
1791
1792 if (!evsel->attr.sample_id_all) {
1793 sample->cpu = 0;
1794 sample->time = 0;
1795 sample->tid = event->mmap2.tid;
1796 sample->pid = event->mmap2.pid;
1797 }
1798 print_sample_start(sample, thread, evsel);
1799 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001800 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001801 return 0;
1802}
1803
Adrian Hunter7c148982015-07-21 12:44:06 +03001804static int process_switch_event(struct perf_tool *tool,
1805 union perf_event *event,
1806 struct perf_sample *sample,
1807 struct machine *machine)
1808{
1809 struct thread *thread;
1810 struct perf_script *script = container_of(tool, struct perf_script, tool);
1811 struct perf_session *session = script->session;
1812 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1813
1814 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1815 return -1;
1816
1817 thread = machine__findnew_thread(machine, sample->pid,
1818 sample->tid);
1819 if (thread == NULL) {
1820 pr_debug("problem processing SWITCH event, skipping it.\n");
1821 return -1;
1822 }
1823
1824 print_sample_start(sample, thread, evsel);
1825 perf_event__fprintf(event, stdout);
1826 thread__put(thread);
1827 return 0;
1828}
1829
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001830static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001831{
1832 session_done = 1;
1833}
1834
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001835static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001836{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001837 int ret;
1838
Tom Zanussic239da32010-04-01 23:59:18 -05001839 signal(SIGINT, sig_handler);
1840
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001841 /* override event processing functions */
1842 if (script->show_task_events) {
1843 script->tool.comm = process_comm_event;
1844 script->tool.fork = process_fork_event;
1845 script->tool.exit = process_exit_event;
1846 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001847 if (script->show_mmap_events) {
1848 script->tool.mmap = process_mmap_event;
1849 script->tool.mmap2 = process_mmap2_event;
1850 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001851 if (script->show_switch_events)
1852 script->tool.context_switch = process_switch_event;
Hari Bathini96a44bb2017-03-08 02:12:06 +05301853 if (script->show_namespace_events)
1854 script->tool.namespaces = process_namespaces_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001855
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001856 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001857
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001858 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001859 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001860
1861 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001862}
1863
Tom Zanussi956ffd02009-11-25 01:15:46 -06001864struct script_spec {
1865 struct list_head node;
1866 struct scripting_ops *ops;
1867 char spec[0];
1868};
1869
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001870static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001871
1872static struct script_spec *script_spec__new(const char *spec,
1873 struct scripting_ops *ops)
1874{
1875 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1876
1877 if (s != NULL) {
1878 strcpy(s->spec, spec);
1879 s->ops = ops;
1880 }
1881
1882 return s;
1883}
1884
Tom Zanussi956ffd02009-11-25 01:15:46 -06001885static void script_spec__add(struct script_spec *s)
1886{
1887 list_add_tail(&s->node, &script_specs);
1888}
1889
1890static struct script_spec *script_spec__find(const char *spec)
1891{
1892 struct script_spec *s;
1893
1894 list_for_each_entry(s, &script_specs, node)
1895 if (strcasecmp(s->spec, spec) == 0)
1896 return s;
1897 return NULL;
1898}
1899
Tom Zanussi956ffd02009-11-25 01:15:46 -06001900int script_spec_register(const char *spec, struct scripting_ops *ops)
1901{
1902 struct script_spec *s;
1903
1904 s = script_spec__find(spec);
1905 if (s)
1906 return -1;
1907
Taeung Song8560bae2016-02-26 00:13:10 +09001908 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001909 if (!s)
1910 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001911 else
1912 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001913
1914 return 0;
1915}
1916
1917static struct scripting_ops *script_spec__lookup(const char *spec)
1918{
1919 struct script_spec *s = script_spec__find(spec);
1920 if (!s)
1921 return NULL;
1922
1923 return s->ops;
1924}
1925
1926static void list_available_languages(void)
1927{
1928 struct script_spec *s;
1929
1930 fprintf(stderr, "\n");
1931 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001932 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001933
1934 list_for_each_entry(s, &script_specs, node)
1935 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1936
1937 fprintf(stderr, "\n");
1938}
1939
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001940static int parse_scriptname(const struct option *opt __maybe_unused,
1941 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001942{
1943 char spec[PATH_MAX];
1944 const char *script, *ext;
1945 int len;
1946
Tom Zanussif526d682010-01-27 02:27:52 -06001947 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001948 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001949 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001950 }
1951
1952 script = strchr(str, ':');
1953 if (script) {
1954 len = script - str;
1955 if (len >= PATH_MAX) {
1956 fprintf(stderr, "invalid language specifier");
1957 return -1;
1958 }
1959 strncpy(spec, str, len);
1960 spec[len] = '\0';
1961 scripting_ops = script_spec__lookup(spec);
1962 if (!scripting_ops) {
1963 fprintf(stderr, "invalid language specifier");
1964 return -1;
1965 }
1966 script++;
1967 } else {
1968 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001969 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001970 if (!ext) {
1971 fprintf(stderr, "invalid script extension");
1972 return -1;
1973 }
1974 scripting_ops = script_spec__lookup(++ext);
1975 if (!scripting_ops) {
1976 fprintf(stderr, "invalid script extension");
1977 return -1;
1978 }
1979 }
1980
1981 script_name = strdup(script);
1982
1983 return 0;
1984}
1985
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001986static int parse_output_fields(const struct option *opt __maybe_unused,
1987 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001988{
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03001989 char *tok, *strtok_saveptr = NULL;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001990 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001991 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001992 int rc = 0;
1993 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001994 int type = -1;
Andi Kleen36ce5652017-06-02 08:48:10 -07001995 enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
David Ahern745f43e2011-03-09 22:23:26 -07001996
1997 if (!str)
1998 return -ENOMEM;
1999
David Ahern2c9e45f72011-03-17 10:03:21 -06002000 /* first word can state for which event type the user is specifying
2001 * the fields. If no type exists, the specified fields apply to all
2002 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07002003 */
David Ahern2c9e45f72011-03-17 10:03:21 -06002004 tok = strchr(str, ':');
2005 if (tok) {
2006 *tok = '\0';
2007 tok++;
2008 if (!strcmp(str, "hw"))
2009 type = PERF_TYPE_HARDWARE;
2010 else if (!strcmp(str, "sw"))
2011 type = PERF_TYPE_SOFTWARE;
2012 else if (!strcmp(str, "trace"))
2013 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07002014 else if (!strcmp(str, "raw"))
2015 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00002016 else if (!strcmp(str, "break"))
2017 type = PERF_TYPE_BREAKPOINT;
Adrian Hunter14057202017-06-21 13:17:19 +03002018 else if (!strcmp(str, "synth"))
2019 type = OUTPUT_TYPE_SYNTH;
David Ahern2c9e45f72011-03-17 10:03:21 -06002020 else {
2021 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01002022 rc = -EINVAL;
2023 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06002024 }
2025
2026 if (output[type].user_set)
2027 pr_warning("Overriding previous field request for %s events.\n",
2028 event_type(type));
2029
2030 output[type].fields = 0;
2031 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002032 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06002033
2034 } else {
2035 tok = str;
2036 if (strlen(str) == 0) {
2037 fprintf(stderr,
2038 "Cannot set fields to 'none' for all event types.\n");
2039 rc = -EINVAL;
2040 goto out;
2041 }
2042
Andi Kleen36ce5652017-06-02 08:48:10 -07002043 /* Don't override defaults for +- */
2044 if (strchr(str, '+') || strchr(str, '-'))
2045 goto parse;
2046
David Ahern2c9e45f72011-03-17 10:03:21 -06002047 if (output_set_by_user())
2048 pr_warning("Overriding previous field request for all events.\n");
2049
Adrian Hunter14057202017-06-21 13:17:19 +03002050 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002051 output[j].fields = 0;
2052 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06002053 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06002054 }
David Ahern1424dc92011-03-09 22:23:28 -07002055 }
2056
Andi Kleen36ce5652017-06-02 08:48:10 -07002057parse:
Arnaldo Carvalho de Melo49346e82017-04-05 11:43:41 -03002058 for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002059 if (*tok == '+') {
2060 if (change == SET)
2061 goto out_badmix;
2062 change = ADD;
2063 tok++;
2064 } else if (*tok == '-') {
2065 if (change == SET)
2066 goto out_badmix;
2067 change = REMOVE;
2068 tok++;
2069 } else {
2070 if (change != SET && change != DEFAULT)
2071 goto out_badmix;
2072 change = SET;
2073 }
2074
David Ahern745f43e2011-03-09 22:23:26 -07002075 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002076 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07002077 break;
David Ahern745f43e2011-03-09 22:23:26 -07002078 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002079 if (i == imax && strcmp(tok, "flags") == 0) {
Andi Kleen36ce5652017-06-02 08:48:10 -07002080 print_flags = change == REMOVE ? false : true;
Adrian Hunter400ea6d2015-04-09 18:54:05 +03002081 continue;
2082 }
David Ahern745f43e2011-03-09 22:23:26 -07002083 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002084 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07002085 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002086 goto out;
2087 }
2088
2089 if (type == -1) {
2090 /* add user option to all events types for
2091 * which it is valid
2092 */
Adrian Hunter14057202017-06-21 13:17:19 +03002093 for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
David Ahern2c9e45f72011-03-17 10:03:21 -06002094 if (output[j].invalid_fields & all_output_options[i].field) {
2095 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
2096 all_output_options[i].str, event_type(j));
Andi Kleen36ce5652017-06-02 08:48:10 -07002097 } else {
2098 if (change == REMOVE)
2099 output[j].fields &= ~all_output_options[i].field;
2100 else
2101 output[j].fields |= all_output_options[i].field;
2102 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002103 }
2104 } else {
2105 if (output[type].invalid_fields & all_output_options[i].field) {
2106 fprintf(stderr, "\'%s\' not valid for %s events.\n",
2107 all_output_options[i].str, event_type(type));
2108
2109 rc = -EINVAL;
2110 goto out;
2111 }
2112 output[type].fields |= all_output_options[i].field;
2113 }
David Ahern2c9e45f72011-03-17 10:03:21 -06002114 }
2115
2116 if (type >= 0) {
2117 if (output[type].fields == 0) {
2118 pr_debug("No fields requested for %s type. "
2119 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07002120 }
David Ahern1424dc92011-03-09 22:23:28 -07002121 }
Andi Kleen36ce5652017-06-02 08:48:10 -07002122 goto out;
David Ahern745f43e2011-03-09 22:23:26 -07002123
Andi Kleen36ce5652017-06-02 08:48:10 -07002124out_badmix:
2125 fprintf(stderr, "Cannot mix +-field with overridden fields\n");
2126 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06002127out:
David Ahern745f43e2011-03-09 22:23:26 -07002128 free(str);
2129 return rc;
2130}
2131
Shawn Bohrer008f29d2010-11-21 10:09:39 -06002132/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
2133static int is_directory(const char *base_path, const struct dirent *dent)
2134{
2135 char path[PATH_MAX];
2136 struct stat st;
2137
2138 sprintf(path, "%s/%s", base_path, dent->d_name);
2139 if (stat(path, &st))
2140 return 0;
2141
2142 return S_ISDIR(st.st_mode);
2143}
2144
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002145#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
2146 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
2147 if ((lang_dirent->d_type == DT_DIR || \
2148 (lang_dirent->d_type == DT_UNKNOWN && \
2149 is_directory(scripts_path, lang_dirent))) && \
2150 (strcmp(lang_dirent->d_name, ".")) && \
2151 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002152
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002153#define for_each_script(lang_path, lang_dir, script_dirent) \
2154 while ((script_dirent = readdir(lang_dir)) != NULL) \
2155 if (script_dirent->d_type != DT_DIR && \
2156 (script_dirent->d_type != DT_UNKNOWN || \
2157 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002158
2159
2160#define RECORD_SUFFIX "-record"
2161#define REPORT_SUFFIX "-report"
2162
2163struct script_desc {
2164 struct list_head node;
2165 char *name;
2166 char *half_liner;
2167 char *args;
2168};
2169
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02002170static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002171
2172static struct script_desc *script_desc__new(const char *name)
2173{
2174 struct script_desc *s = zalloc(sizeof(*s));
2175
Tom Zanussib5b87312010-11-10 08:16:51 -06002176 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002177 s->name = strdup(name);
2178
2179 return s;
2180}
2181
2182static void script_desc__delete(struct script_desc *s)
2183{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002184 zfree(&s->name);
2185 zfree(&s->half_liner);
2186 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002187 free(s);
2188}
2189
2190static void script_desc__add(struct script_desc *s)
2191{
2192 list_add_tail(&s->node, &script_descs);
2193}
2194
2195static struct script_desc *script_desc__find(const char *name)
2196{
2197 struct script_desc *s;
2198
2199 list_for_each_entry(s, &script_descs, node)
2200 if (strcasecmp(s->name, name) == 0)
2201 return s;
2202 return NULL;
2203}
2204
2205static struct script_desc *script_desc__findnew(const char *name)
2206{
2207 struct script_desc *s = script_desc__find(name);
2208
2209 if (s)
2210 return s;
2211
2212 s = script_desc__new(name);
2213 if (!s)
Dan Carpenter2ec5cab2017-07-22 10:36:10 +03002214 return NULL;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002215
2216 script_desc__add(s);
2217
2218 return s;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002219}
2220
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002221static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002222{
2223 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002224 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002225
2226 if (strlen(str) > suffix_len) {
2227 p = str + strlen(str) - suffix_len;
2228 if (!strncmp(p, suffix, suffix_len))
2229 return p;
2230 }
2231
2232 return NULL;
2233}
2234
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002235static int read_script_info(struct script_desc *desc, const char *filename)
2236{
2237 char line[BUFSIZ], *p;
2238 FILE *fp;
2239
2240 fp = fopen(filename, "r");
2241 if (!fp)
2242 return -1;
2243
2244 while (fgets(line, sizeof(line), fp)) {
2245 p = ltrim(line);
2246 if (strlen(p) == 0)
2247 continue;
2248 if (*p != '#')
2249 continue;
2250 p++;
2251 if (strlen(p) && *p == '!')
2252 continue;
2253
2254 p = ltrim(p);
2255 if (strlen(p) && p[strlen(p) - 1] == '\n')
2256 p[strlen(p) - 1] = '\0';
2257
2258 if (!strncmp(p, "description:", strlen("description:"))) {
2259 p += strlen("description:");
2260 desc->half_liner = strdup(ltrim(p));
2261 continue;
2262 }
2263
2264 if (!strncmp(p, "args:", strlen("args:"))) {
2265 p += strlen("args:");
2266 desc->args = strdup(ltrim(p));
2267 continue;
2268 }
2269 }
2270
2271 fclose(fp);
2272
2273 return 0;
2274}
2275
Robert Richter38efb532011-11-25 11:38:40 +01002276static char *get_script_root(struct dirent *script_dirent, const char *suffix)
2277{
2278 char *script_root, *str;
2279
2280 script_root = strdup(script_dirent->d_name);
2281 if (!script_root)
2282 return NULL;
2283
2284 str = (char *)ends_with(script_root, suffix);
2285 if (!str) {
2286 free(script_root);
2287 return NULL;
2288 }
2289
2290 *str = '\0';
2291 return script_root;
2292}
2293
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002294static int list_available_scripts(const struct option *opt __maybe_unused,
2295 const char *s __maybe_unused,
2296 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002297{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002298 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002299 char scripts_path[MAXPATHLEN];
2300 DIR *scripts_dir, *lang_dir;
2301 char script_path[MAXPATHLEN];
2302 char lang_path[MAXPATHLEN];
2303 struct script_desc *desc;
2304 char first_half[BUFSIZ];
2305 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002306
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002307 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002308
2309 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00002310 if (!scripts_dir) {
2311 fprintf(stdout,
2312 "open(%s) failed.\n"
2313 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
2314 scripts_path);
2315 exit(-1);
2316 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002317
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002318 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002319 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002320 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002321 lang_dir = opendir(lang_path);
2322 if (!lang_dir)
2323 continue;
2324
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002325 for_each_script(lang_path, lang_dir, script_dirent) {
2326 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01002327 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002328 desc = script_desc__findnew(script_root);
2329 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002330 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002331 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01002332 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002333 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06002334 }
2335 }
2336
2337 fprintf(stdout, "List of available trace scripts:\n");
2338 list_for_each_entry(desc, &script_descs, node) {
2339 sprintf(first_half, "%s %s", desc->name,
2340 desc->args ? desc->args : "");
2341 fprintf(stdout, " %-36s %s\n", first_half,
2342 desc->half_liner ? desc->half_liner : "");
2343 }
2344
2345 exit(0);
2346}
2347
Feng Tange5f37052012-09-07 16:42:26 +08002348/*
Feng Tang49e639e2012-10-30 11:56:03 +08002349 * Some scripts specify the required events in their "xxx-record" file,
2350 * this function will check if the events in perf.data match those
2351 * mentioned in the "xxx-record".
2352 *
2353 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
2354 * which is covered well now. And new parsing code should be added to
2355 * cover the future complexing formats like event groups etc.
2356 */
2357static int check_ev_match(char *dir_name, char *scriptname,
2358 struct perf_session *session)
2359{
2360 char filename[MAXPATHLEN], evname[128];
2361 char line[BUFSIZ], *p;
2362 struct perf_evsel *pos;
2363 int match, len;
2364 FILE *fp;
2365
2366 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
2367
2368 fp = fopen(filename, "r");
2369 if (!fp)
2370 return -1;
2371
2372 while (fgets(line, sizeof(line), fp)) {
2373 p = ltrim(line);
2374 if (*p == '#')
2375 continue;
2376
2377 while (strlen(p)) {
2378 p = strstr(p, "-e");
2379 if (!p)
2380 break;
2381
2382 p += 2;
2383 p = ltrim(p);
2384 len = strcspn(p, " \t");
2385 if (!len)
2386 break;
2387
2388 snprintf(evname, len + 1, "%s", p);
2389
2390 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002391 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08002392 if (!strcmp(perf_evsel__name(pos), evname)) {
2393 match = 1;
2394 break;
2395 }
2396 }
2397
2398 if (!match) {
2399 fclose(fp);
2400 return -1;
2401 }
2402 }
2403 }
2404
2405 fclose(fp);
2406 return 0;
2407}
2408
2409/*
Feng Tange5f37052012-09-07 16:42:26 +08002410 * Return -1 if none is found, otherwise the actual scripts number.
2411 *
2412 * Currently the only user of this function is the script browser, which
2413 * will list all statically runnable scripts, select one, execute it and
2414 * show the output in a perf browser.
2415 */
2416int find_scripts(char **scripts_array, char **scripts_path_array)
2417{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002418 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08002419 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08002420 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08002421 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002422 struct perf_data_file file = {
2423 .path = input_name,
2424 .mode = PERF_DATA_MODE_READ,
2425 };
Feng Tange5f37052012-09-07 16:42:26 +08002426 char *temp;
2427 int i = 0;
2428
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002429 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08002430 if (!session)
2431 return -1;
2432
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002433 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08002434
2435 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08002436 if (!scripts_dir) {
2437 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002438 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08002439 }
Feng Tange5f37052012-09-07 16:42:26 +08002440
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002441 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002442 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002443 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08002444#ifdef NO_LIBPERL
2445 if (strstr(lang_path, "perl"))
2446 continue;
2447#endif
2448#ifdef NO_LIBPYTHON
2449 if (strstr(lang_path, "python"))
2450 continue;
2451#endif
2452
2453 lang_dir = opendir(lang_path);
2454 if (!lang_dir)
2455 continue;
2456
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002457 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08002458 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002459 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08002460 continue;
2461 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002462 script_dirent->d_name);
2463 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08002464 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002465 (temp - script_dirent->d_name) + 1,
2466 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08002467
2468 if (check_ev_match(lang_path,
2469 scripts_array[i], session))
2470 continue;
2471
Feng Tange5f37052012-09-07 16:42:26 +08002472 i++;
2473 }
Feng Tang49e639e2012-10-30 11:56:03 +08002474 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08002475 }
2476
Feng Tang49e639e2012-10-30 11:56:03 +08002477 closedir(scripts_dir);
2478 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08002479 return i;
2480}
2481
Tom Zanussi38752942009-12-15 02:53:39 -06002482static char *get_script_path(const char *script_root, const char *suffix)
2483{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002484 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06002485 char scripts_path[MAXPATHLEN];
2486 char script_path[MAXPATHLEN];
2487 DIR *scripts_dir, *lang_dir;
2488 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01002489 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06002490
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002491 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06002492
2493 scripts_dir = opendir(scripts_path);
2494 if (!scripts_dir)
2495 return NULL;
2496
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002497 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06002498 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002499 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06002500 lang_dir = opendir(lang_path);
2501 if (!lang_dir)
2502 continue;
2503
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002504 for_each_script(lang_path, lang_dir, script_dirent) {
2505 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01002506 if (__script_root && !strcmp(script_root, __script_root)) {
2507 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002508 closedir(lang_dir);
2509 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002510 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03002511 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01002512 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06002513 }
2514 free(__script_root);
2515 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002516 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002517 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09002518 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06002519
Robert Richter38efb532011-11-25 11:38:40 +01002520 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002521}
2522
Tom Zanussib5b87312010-11-10 08:16:51 -06002523static bool is_top_script(const char *script_path)
2524{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02002525 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06002526}
2527
2528static int has_required_arg(char *script_path)
2529{
2530 struct script_desc *desc;
2531 int n_args = 0;
2532 char *p;
2533
2534 desc = script_desc__new(NULL);
2535
2536 if (read_script_info(desc, script_path))
2537 goto out;
2538
2539 if (!desc->args)
2540 goto out;
2541
2542 for (p = desc->args; *p; p++)
2543 if (*p == '<')
2544 n_args++;
2545out:
2546 script_desc__delete(desc);
2547
2548 return n_args;
2549}
2550
David Ahernd54b1a92012-08-26 12:24:46 -06002551static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002552{
2553 char **__argv = malloc(sizeof(const char *) * argc);
2554
David Ahernd54b1a92012-08-26 12:24:46 -06002555 if (!__argv) {
2556 pr_err("malloc failed\n");
2557 return -1;
2558 }
2559
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002560 memcpy(__argv, argv, sizeof(const char *) * argc);
2561 argc = parse_options(argc, (const char **)__argv, record_options,
2562 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
2563 free(__argv);
2564
David Ahernd54b1a92012-08-26 12:24:46 -06002565 system_wide = (argc == 0);
2566
2567 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002568}
2569
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002570static void script__setup_sample_type(struct perf_script *script)
2571{
2572 struct perf_session *session = script->session;
2573 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
2574
2575 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
2576 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
Arnaldo Carvalho de Melo0eda4d02018-01-15 16:48:46 -03002577 (sample_type & PERF_SAMPLE_STACK_USER)) {
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002578 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Melo0eda4d02018-01-15 16:48:46 -03002579 dwarf_callchain_users = true;
2580 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002581 callchain_param.record_mode = CALLCHAIN_LBR;
2582 else
2583 callchain_param.record_mode = CALLCHAIN_FP;
2584 }
2585}
2586
Jiri Olsae099eba2016-01-05 22:09:09 +01002587static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2588 union perf_event *event,
2589 struct perf_session *session)
2590{
2591 struct stat_round_event *round = &event->stat_round;
2592 struct perf_evsel *counter;
2593
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002594 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002595 perf_stat_process_counter(&stat_config, counter);
2596 process_stat(counter, round->time);
2597 }
2598
2599 process_stat_interval(round->time);
2600 return 0;
2601}
2602
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002603static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2604 union perf_event *event,
2605 struct perf_session *session __maybe_unused)
2606{
2607 perf_event__read_stat_config(&stat_config, &event->stat_config);
2608 return 0;
2609}
2610
Jiri Olsacfc88742016-01-05 22:09:06 +01002611static int set_maps(struct perf_script *script)
2612{
2613 struct perf_evlist *evlist = script->session->evlist;
2614
2615 if (!script->cpus || !script->threads)
2616 return 0;
2617
2618 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2619 return -EINVAL;
2620
2621 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2622
2623 if (perf_evlist__alloc_stats(evlist, true))
2624 return -ENOMEM;
2625
2626 script->allocated = true;
2627 return 0;
2628}
2629
2630static
2631int process_thread_map_event(struct perf_tool *tool,
2632 union perf_event *event,
2633 struct perf_session *session __maybe_unused)
2634{
2635 struct perf_script *script = container_of(tool, struct perf_script, tool);
2636
2637 if (script->threads) {
2638 pr_warning("Extra thread map event, ignoring.\n");
2639 return 0;
2640 }
2641
2642 script->threads = thread_map__new_event(&event->thread_map);
2643 if (!script->threads)
2644 return -ENOMEM;
2645
2646 return set_maps(script);
2647}
2648
2649static
2650int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2651 union perf_event *event,
2652 struct perf_session *session __maybe_unused)
2653{
2654 struct perf_script *script = container_of(tool, struct perf_script, tool);
2655
2656 if (script->cpus) {
2657 pr_warning("Extra cpu map event, ignoring.\n");
2658 return 0;
2659 }
2660
2661 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2662 if (!script->cpus)
2663 return -ENOMEM;
2664
2665 return set_maps(script);
2666}
2667
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002668int cmd_script(int argc, const char **argv)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002669{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002670 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002671 bool header = false;
2672 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002673 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002674 char *rec_script_path = NULL;
2675 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002676 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002677 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002678 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002679 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002680 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002681 struct perf_script script = {
2682 .tool = {
2683 .sample = process_sample_event,
2684 .mmap = perf_event__process_mmap,
2685 .mmap2 = perf_event__process_mmap2,
2686 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +05302687 .namespaces = perf_event__process_namespaces,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002688 .exit = perf_event__process_exit,
2689 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002690 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002691 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002692 .tracing_data = perf_event__process_tracing_data,
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002693 .feature = perf_event__process_feature,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002694 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002695 .id_index = perf_event__process_id_index,
2696 .auxtrace_info = perf_event__process_auxtrace_info,
2697 .auxtrace = perf_event__process_auxtrace,
2698 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002699 .stat = perf_event__process_stat_event,
2700 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002701 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002702 .thread_map = process_thread_map_event,
2703 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002704 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002705 .ordering_requires_timestamps = true,
2706 },
2707 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002708 struct perf_data_file file = {
2709 .mode = PERF_DATA_MODE_READ,
2710 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002711 const struct option options[] = {
2712 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2713 "dump raw trace in ASCII"),
2714 OPT_INCR('v', "verbose", &verbose,
2715 "be more verbose (show symbol address, etc)"),
2716 OPT_BOOLEAN('L', "Latency", &latency_format,
2717 "show latency attributes (irqs/preemption disabled, etc)"),
2718 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2719 list_available_scripts),
2720 OPT_CALLBACK('s', "script", NULL, "name",
2721 "script file name (lang:script name, script name, or *)",
2722 parse_scriptname),
2723 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2724 "generate perf-script.xx script in specified language"),
2725 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2726 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2727 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002728 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2729 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002730 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2731 "file", "vmlinux pathname"),
2732 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2733 "file", "kallsyms pathname"),
2734 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2735 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002736 OPT_CALLBACK(0, "symfs", NULL, "directory",
2737 "Look for files with symbols relative to this directory",
2738 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002739 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002740 "comma separated output fields prepend with 'type:'. "
Andi Kleen36ce5652017-06-02 08:48:10 -07002741 "+field to add and -field to remove."
Adrian Hunter14057202017-06-21 13:17:19 +03002742 "Valid types: hw,sw,trace,raw,synth. "
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002743 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002744 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Kan Liang49d58f02017-08-29 13:11:11 -04002745 "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
Andi Kleen48d02a12017-02-23 15:46:34 -08002746 parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002747 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2748 "system-wide collection from all CPUs"),
2749 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2750 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002751 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2752 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002753 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2754 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2755 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002756 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2757 "only consider symbols in these pids"),
2758 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2759 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002760 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2761 "Set the maximum stack depth when parsing the callchain, "
2762 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002763 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002764 OPT_BOOLEAN('I', "show-info", &show_full_info,
2765 "display extended information from perf.data file"),
2766 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2767 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002768 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2769 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002770 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2771 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002772 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2773 "Show context switch events (if recorded)"),
Hari Bathini96a44bb2017-03-08 02:12:06 +05302774 OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
2775 "Show namespace events (if recorded)"),
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002776 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
Andi Kleen48d02a12017-02-23 15:46:34 -08002777 OPT_INTEGER(0, "max-blocks", &max_blocks,
2778 "Maximum number of code blocks to dump with brstackinsn"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002779 OPT_BOOLEAN(0, "ns", &nanosecs,
2780 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002781 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2782 "Instruction Tracing options",
2783 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002784 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2785 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002786 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2787 "Enable symbol demangling"),
2788 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2789 "Enable kernel symbol demangling"),
David Aherna91f4c42016-11-29 10:15:43 -07002790 OPT_STRING(0, "time", &script.time_str, "str",
2791 "Time span of interest (start,stop)"),
Namhyung Kim325fbff2017-05-24 15:21:26 +09002792 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
2793 "Show inline function"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002794 OPT_END()
2795 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002796 const char * const script_subcommands[] = { "record", "report", NULL };
2797 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002798 "perf script [<options>]",
2799 "perf script [<options>] record <script> [<record-options>] <command>",
2800 "perf script [<options>] report <script> [script-args]",
2801 "perf script [<options>] <script> [<record-options>] <command>",
2802 "perf script [<options>] <top-script> [script-args]",
2803 NULL
2804 };
Tom Zanussi38752942009-12-15 02:53:39 -06002805
Tom Zanussib5b87312010-11-10 08:16:51 -06002806 setup_scripting();
2807
Yunlong Song40cae2b2015-03-18 21:35:54 +08002808 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002809 PARSE_OPT_STOP_AT_NON_OPTION);
2810
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002811 file.path = input_name;
Yannick Brosseaube3d4662017-01-13 13:25:27 -05002812 file.force = symbol_conf.force;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002813
Tom Zanussib5b87312010-11-10 08:16:51 -06002814 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2815 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2816 if (!rec_script_path)
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002817 return cmd_record(argc, argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002818 }
2819
Tom Zanussib5b87312010-11-10 08:16:51 -06002820 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2821 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2822 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002823 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002824 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002825 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002826 return -1;
2827 }
Tom Zanussi38752942009-12-15 02:53:39 -06002828 }
2829
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002830 if (itrace_synth_opts.callchain &&
2831 itrace_synth_opts.callchain_sz > scripting_max_stack)
2832 scripting_max_stack = itrace_synth_opts.callchain_sz;
2833
Ben Hutchings44e668c2010-10-10 16:10:03 +01002834 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002835 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002836
Tom Zanussib5b87312010-11-10 08:16:51 -06002837 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002838 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002839 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002840 pid_t pid;
2841
Tom Zanussib5b87312010-11-10 08:16:51 -06002842 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2843 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2844
2845 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002846 usage_with_options_msg(script_usage, options,
2847 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002848 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002849 }
2850
Tom Zanussib5b87312010-11-10 08:16:51 -06002851 if (is_top_script(argv[0])) {
2852 rep_args = argc - 1;
2853 } else {
2854 int rec_args;
2855
2856 rep_args = has_required_arg(rep_script_path);
2857 rec_args = (argc - 1) - rep_args;
2858 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002859 usage_with_options_msg(script_usage, options,
2860 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002861 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002862 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002863 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002864 }
2865
2866 if (pipe(live_pipe) < 0) {
2867 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002868 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002869 }
2870
2871 pid = fork();
2872 if (pid < 0) {
2873 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002874 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002875 }
2876
2877 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002878 j = 0;
2879
Tom Zanussia0cccc22010-04-01 23:59:25 -05002880 dup2(live_pipe[1], 1);
2881 close(live_pipe[0]);
2882
Robert Richter317df652011-11-25 15:05:25 +01002883 if (is_top_script(argv[0])) {
2884 system_wide = true;
2885 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002886 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2887 err = -1;
2888 goto out;
2889 }
Robert Richter317df652011-11-25 15:05:25 +01002890 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002891
2892 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002893 if (!__argv) {
2894 pr_err("malloc failed\n");
2895 err = -ENOMEM;
2896 goto out;
2897 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002898
Tom Zanussib5b87312010-11-10 08:16:51 -06002899 __argv[j++] = "/bin/sh";
2900 __argv[j++] = rec_script_path;
2901 if (system_wide)
2902 __argv[j++] = "-a";
2903 __argv[j++] = "-q";
2904 __argv[j++] = "-o";
2905 __argv[j++] = "-";
2906 for (i = rep_args + 1; i < argc; i++)
2907 __argv[j++] = argv[i];
2908 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002909
2910 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002911 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002912 exit(-1);
2913 }
2914
2915 dup2(live_pipe[0], 0);
2916 close(live_pipe[1]);
2917
Tom Zanussib5b87312010-11-10 08:16:51 -06002918 __argv = malloc((argc + 4) * 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 }
2924
Tom Zanussib5b87312010-11-10 08:16:51 -06002925 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002926 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002927 __argv[j++] = rep_script_path;
2928 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002929 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002930 __argv[j++] = "-i";
2931 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002932 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002933
2934 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002935 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002936 exit(-1);
2937 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002938
Tom Zanussib5b87312010-11-10 08:16:51 -06002939 if (rec_script_path)
2940 script_path = rec_script_path;
2941 if (rep_script_path)
2942 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002943
Tom Zanussib5b87312010-11-10 08:16:51 -06002944 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002945 j = 0;
2946
Robert Richter317df652011-11-25 15:05:25 +01002947 if (!rec_script_path)
2948 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002949 else if (!system_wide) {
2950 if (have_cmd(argc - 1, &argv[1]) != 0) {
2951 err = -1;
2952 goto out;
2953 }
2954 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002955
2956 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002957 if (!__argv) {
2958 pr_err("malloc failed\n");
2959 err = -ENOMEM;
2960 goto out;
2961 }
2962
Tom Zanussib5b87312010-11-10 08:16:51 -06002963 __argv[j++] = "/bin/sh";
2964 __argv[j++] = script_path;
2965 if (system_wide)
2966 __argv[j++] = "-a";
2967 for (i = 2; i < argc; i++)
2968 __argv[j++] = argv[i];
2969 __argv[j++] = NULL;
2970
2971 execvp("/bin/sh", (char **)__argv);
2972 free(__argv);
2973 exit(-1);
2974 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002975
Tom Zanussicf4fee52010-03-03 01:04:33 -06002976 if (!script_name)
2977 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002978
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002979 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002980 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002981 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002982
Jiri Olsae90debd2013-12-09 11:02:50 +01002983 if (header || header_only) {
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07002984 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
Jiri Olsae90debd2013-12-09 11:02:50 +01002985 perf_session__fprintf_info(session, stdout, show_full_info);
2986 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002987 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002988 }
David Carrillo-Cisneros114f7092017-07-17 21:25:47 -07002989 if (show_full_info)
2990 script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
Jiri Olsae90debd2013-12-09 11:02:50 +01002991
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002992 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002993 goto out_delete;
2994
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002995 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002996 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002997
Adrian Huntere2167082016-06-23 16:40:58 +03002998 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2999 itrace_synth_opts.thread_stack = true;
3000
Adrian Hunter7a680eb2015-04-09 18:53:56 +03003001 session->itrace_synth_opts = &itrace_synth_opts;
3002
Anton Blanchard5d67be92011-07-04 21:57:50 +10003003 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003004 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
3005 if (err < 0)
3006 goto out_delete;
Adrian Hunter644e0842017-05-26 11:17:38 +03003007 itrace_synth_opts.cpu_bitmap = cpu_bitmap;
Anton Blanchard5d67be92011-07-04 21:57:50 +10003008 }
3009
David Ahern1424dc92011-03-09 22:23:28 -07003010 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07003011 symbol_conf.use_callchain = true;
3012 else
3013 symbol_conf.use_callchain = false;
3014
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03003015 if (session->tevent.pevent &&
3016 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03003017 machine__resolve_kernel_addr,
3018 &session->machines.host) < 0) {
3019 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
3020 return -1;
3021 }
3022
Tom Zanussi956ffd02009-11-25 01:15:46 -06003023 if (generate_script_lang) {
3024 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07003025 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003026
David Ahern2c9e45f72011-03-17 10:03:21 -06003027 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07003028 fprintf(stderr,
3029 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003030 err = -EINVAL;
3031 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07003032 }
3033
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003034 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06003035 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003036 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003037 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003038 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003039 }
3040
3041 err = fstat(input, &perf_stat);
3042 if (err < 0) {
3043 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003044 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003045 }
3046
3047 if (!perf_stat.st_size) {
3048 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003049 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003050 }
3051
3052 scripting_ops = script_spec__lookup(generate_script_lang);
3053 if (!scripting_ops) {
3054 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003055 err = -ENOENT;
3056 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003057 }
3058
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003059 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003060 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003061 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003062 }
3063
3064 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06003065 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003066 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003067 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01003068 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003069 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06003070 }
3071
David Ahern9cbdb702011-04-06 21:54:20 -06003072
3073 err = perf_session__check_output_opt(session);
3074 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003075 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06003076
David Aherna91f4c42016-11-29 10:15:43 -07003077 /* needs to be parsed after looking up reference time */
3078 if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
3079 pr_err("Invalid time string\n");
3080 return -EINVAL;
3081 }
3082
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03003083 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06003084
Adrian Hunterd445dd22014-08-15 22:08:37 +03003085 flush_scripting();
3086
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003087out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01003088 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02003089 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09003090
3091 if (script_started)
3092 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06003093out:
3094 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02003095}