blob: 066b4bf73780c96292a0c9a7aeefb41ed10dc95f [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
Andrea Gelminib7eead82010-08-05 15:51:38 +02003#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06006#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
Adrian Huntere2167082016-06-23 16:40:58 +030024#include "util/thread-stack.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100025#include <linux/bitmap.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030026#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030027#include <linux/time64.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010028#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010029#include "util/mem-events.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020030
Tom Zanussi956ffd02009-11-25 01:15:46 -060031static char const *script_name;
32static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020033static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020034static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020035static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070036static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090037static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010038static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030039static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030040static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100041static const char *cpu_list;
42static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010043static struct perf_stat_config stat_config;
Tom Zanussi956ffd02009-11-25 01:15:46 -060044
Adrian Hunter44cbe722015-09-25 16:15:50 +030045unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030046
David Ahern745f43e2011-03-09 22:23:26 -070047enum perf_output_field {
48 PERF_OUTPUT_COMM = 1U << 0,
49 PERF_OUTPUT_TID = 1U << 1,
50 PERF_OUTPUT_PID = 1U << 2,
51 PERF_OUTPUT_TIME = 1U << 3,
52 PERF_OUTPUT_CPU = 1U << 4,
53 PERF_OUTPUT_EVNAME = 1U << 5,
54 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060055 PERF_OUTPUT_IP = 1U << 7,
56 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060057 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060058 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090059 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020060 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020061 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020062 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020063 PERF_OUTPUT_BRSTACK = 1U << 15,
64 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010065 PERF_OUTPUT_DATA_SRC = 1U << 17,
66 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000067 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
Adrian Huntere2167082016-06-23 16:40:58 +030068 PERF_OUTPUT_CALLINDENT = 1U << 20,
Andi Kleen224e2c92016-10-07 16:42:27 +030069 PERF_OUTPUT_INSN = 1U << 21,
70 PERF_OUTPUT_INSNLEN = 1U << 22,
David Ahern745f43e2011-03-09 22:23:26 -070071};
72
73struct output_option {
74 const char *str;
75 enum perf_output_field field;
76} all_output_options[] = {
77 {.str = "comm", .field = PERF_OUTPUT_COMM},
78 {.str = "tid", .field = PERF_OUTPUT_TID},
79 {.str = "pid", .field = PERF_OUTPUT_PID},
80 {.str = "time", .field = PERF_OUTPUT_TIME},
81 {.str = "cpu", .field = PERF_OUTPUT_CPU},
82 {.str = "event", .field = PERF_OUTPUT_EVNAME},
83 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060084 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070085 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060086 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060087 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090088 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020089 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +020090 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020091 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +020092 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
93 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +010094 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
95 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +000096 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
Adrian Huntere2167082016-06-23 16:40:58 +030097 {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
Andi Kleen224e2c92016-10-07 16:42:27 +030098 {.str = "insn", .field = PERF_OUTPUT_INSN},
99 {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
David Ahern745f43e2011-03-09 22:23:26 -0700100};
101
102/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -0600103static struct {
104 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -0600105 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -0400106 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -0600107 u64 fields;
108 u64 invalid_fields;
109} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700110
David Ahern2c9e45f72011-03-17 10:03:21 -0600111 [PERF_TYPE_HARDWARE] = {
112 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700113
David Ahern2c9e45f72011-03-17 10:03:21 -0600114 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
115 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600116 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200117 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
118 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600119
Wang Nan30372f02016-02-24 11:20:45 +0000120 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600121 },
122
123 [PERF_TYPE_SOFTWARE] = {
124 .user_set = false,
125
126 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
127 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600128 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200129 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000130 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600131
132 .invalid_fields = PERF_OUTPUT_TRACE,
133 },
134
135 [PERF_TYPE_TRACEPOINT] = {
136 .user_set = false,
137
138 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
139 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000140 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600141 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700142
143 [PERF_TYPE_RAW] = {
144 .user_set = false,
145
146 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
147 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600148 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200149 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100150 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
151 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700152
Wang Nan30372f02016-02-24 11:20:45 +0000153 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700154 },
Wang Nan27cfef02015-12-08 02:25:43 +0000155
156 [PERF_TYPE_BREAKPOINT] = {
157 .user_set = false,
158
159 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
160 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
161 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
162 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
163 PERF_OUTPUT_PERIOD,
164
Wang Nan30372f02016-02-24 11:20:45 +0000165 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000166 },
David Ahern1424dc92011-03-09 22:23:28 -0700167};
David Ahern745f43e2011-03-09 22:23:26 -0700168
David Ahern2c9e45f72011-03-17 10:03:21 -0600169static bool output_set_by_user(void)
170{
171 int j;
172 for (j = 0; j < PERF_TYPE_MAX; ++j) {
173 if (output[j].user_set)
174 return true;
175 }
176 return false;
177}
David Ahern745f43e2011-03-09 22:23:26 -0700178
David Ahern9cbdb702011-04-06 21:54:20 -0600179static const char *output_field2str(enum perf_output_field field)
180{
181 int i, imax = ARRAY_SIZE(all_output_options);
182 const char *str = "";
183
184 for (i = 0; i < imax; ++i) {
185 if (all_output_options[i].field == field) {
186 str = all_output_options[i].str;
187 break;
188 }
189 }
190 return str;
191}
192
David Ahern2c9e45f72011-03-17 10:03:21 -0600193#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700194
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300195static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
196 u64 sample_type, const char *sample_msg,
197 enum perf_output_field field,
198 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700199{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300200 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600201 int type = attr->type;
202 const char *evname;
203
204 if (attr->sample_type & sample_type)
205 return 0;
206
207 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300208 if (allow_user_set)
209 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300210 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600211 pr_err("Samples for '%s' event do not have %s attribute set. "
212 "Cannot print '%s' field.\n",
213 evname, sample_msg, output_field2str(field));
214 return -1;
215 }
216
217 /* user did not ask for it explicitly so remove from the default list */
218 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300219 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600220 pr_debug("Samples for '%s' event do not have %s attribute set. "
221 "Skipping '%s' field.\n",
222 evname, sample_msg, output_field2str(field));
223
224 return 0;
225}
226
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300227static int perf_evsel__check_stype(struct perf_evsel *evsel,
228 u64 sample_type, const char *sample_msg,
229 enum perf_output_field field)
230{
231 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
232 false);
233}
234
David Ahern9cbdb702011-04-06 21:54:20 -0600235static int perf_evsel__check_attr(struct perf_evsel *evsel,
236 struct perf_session *session)
237{
238 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300239 bool allow_user_set;
240
Jiri Olsae099eba2016-01-05 22:09:09 +0100241 if (perf_header__has_feat(&session->header, HEADER_STAT))
242 return 0;
243
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300244 allow_user_set = perf_header__has_feat(&session->header,
245 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600246
David Ahern1424dc92011-03-09 22:23:28 -0700247 if (PRINT_FIELD(TRACE) &&
248 !perf_session__has_traces(session, "record -R"))
249 return -EINVAL;
250
David Ahern787bef12011-05-27 14:28:43 -0600251 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300252 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
253 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700254 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700255 }
David Ahern7cec0922011-05-30 13:08:23 -0600256
257 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300258 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
259 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600260 return -EINVAL;
261
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100262 if (PRINT_FIELD(DATA_SRC) &&
263 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
264 PERF_OUTPUT_DATA_SRC))
265 return -EINVAL;
266
267 if (PRINT_FIELD(WEIGHT) &&
268 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
269 PERF_OUTPUT_WEIGHT))
270 return -EINVAL;
271
David Ahern7cec0922011-05-30 13:08:23 -0600272 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
273 pr_err("Display of symbols requested but neither sample IP nor "
274 "sample address\nis selected. Hence, no addresses to convert "
275 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600276 return -EINVAL;
277 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900278 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
279 pr_err("Display of offsets requested but symbol is not"
280 "selected.\n");
281 return -EINVAL;
282 }
David Ahern7cec0922011-05-30 13:08:23 -0600283 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
284 pr_err("Display of DSO requested but neither sample IP nor "
285 "sample address\nis selected. Hence, no addresses to convert "
286 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600287 return -EINVAL;
288 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200289 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
290 pr_err("Display of source line number requested but sample IP is not\n"
291 "selected. Hence, no address to lookup the source line number.\n");
292 return -EINVAL;
293 }
David Ahern1424dc92011-03-09 22:23:28 -0700294
295 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300296 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
297 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700298 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700299
300 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300301 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
302 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700303 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700304
305 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300306 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
307 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700308 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600309
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200310 if (PRINT_FIELD(PERIOD) &&
311 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
312 PERF_OUTPUT_PERIOD))
313 return -EINVAL;
314
Stephane Eranianfc36f942015-08-31 18:41:10 +0200315 if (PRINT_FIELD(IREGS) &&
316 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
317 PERF_OUTPUT_IREGS))
318 return -EINVAL;
319
David Ahern9cbdb702011-04-06 21:54:20 -0600320 return 0;
321}
322
Adrian Hunter7ea95722013-11-01 15:51:30 +0200323static void set_print_ip_opts(struct perf_event_attr *attr)
324{
325 unsigned int type = attr->type;
326
327 output[type].print_ip_opts = 0;
328 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300329 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200330
331 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300332 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200333
334 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300335 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200336
337 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300338 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200339
340 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300341 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200342}
343
David Ahern9cbdb702011-04-06 21:54:20 -0600344/*
345 * verify all user requested events exist and the samples
346 * have the expected data
347 */
348static int perf_session__check_output_opt(struct perf_session *session)
349{
He Kuang40f20e52016-05-16 04:51:19 +0000350 unsigned int j;
David Ahern9cbdb702011-04-06 21:54:20 -0600351 struct perf_evsel *evsel;
352
353 for (j = 0; j < PERF_TYPE_MAX; ++j) {
354 evsel = perf_session__find_first_evtype(session, j);
355
356 /*
357 * even if fields is set to 0 (ie., show nothing) event must
358 * exist if user explicitly includes it on the command line
359 */
360 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
361 pr_err("%s events do not exist. "
362 "Remove corresponding -f option to proceed.\n",
363 event_type(j));
364 return -1;
365 }
366
367 if (evsel && output[j].fields &&
368 perf_evsel__check_attr(evsel, session))
369 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400370
371 if (evsel == NULL)
372 continue;
373
Adrian Hunter7ea95722013-11-01 15:51:30 +0200374 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700375 }
376
Adrian Hunter98526ee2014-07-31 09:00:59 +0300377 if (!no_callchain) {
378 bool use_callchain = false;
He Kuang71ac8992016-08-04 11:25:43 +0000379 bool not_pipe = false;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300380
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300381 evlist__for_each_entry(session->evlist, evsel) {
He Kuang71ac8992016-08-04 11:25:43 +0000382 not_pipe = true;
Adrian Hunter98526ee2014-07-31 09:00:59 +0300383 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
384 use_callchain = true;
385 break;
386 }
387 }
He Kuang71ac8992016-08-04 11:25:43 +0000388 if (not_pipe && !use_callchain)
Adrian Hunter98526ee2014-07-31 09:00:59 +0300389 symbol_conf.use_callchain = false;
390 }
391
David Ahern80b8b492013-11-19 21:07:37 -0700392 /*
393 * set default for tracepoints to print symbols only
394 * if callchains are present
395 */
396 if (symbol_conf.use_callchain &&
397 !output[PERF_TYPE_TRACEPOINT].user_set) {
398 struct perf_event_attr *attr;
399
400 j = PERF_TYPE_TRACEPOINT;
David Ahern80b8b492013-11-19 21:07:37 -0700401
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300402 evlist__for_each_entry(session->evlist, evsel) {
He Kuang40f20e52016-05-16 04:51:19 +0000403 if (evsel->attr.type != j)
404 continue;
David Ahern80b8b492013-11-19 21:07:37 -0700405
He Kuang40f20e52016-05-16 04:51:19 +0000406 attr = &evsel->attr;
407
408 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
409 output[j].fields |= PERF_OUTPUT_IP;
410 output[j].fields |= PERF_OUTPUT_SYM;
411 output[j].fields |= PERF_OUTPUT_DSO;
412 set_print_ip_opts(attr);
413 goto out;
414 }
David Ahern80b8b492013-11-19 21:07:37 -0700415 }
416 }
417
418out:
David Ahern1424dc92011-03-09 22:23:28 -0700419 return 0;
420}
David Ahern745f43e2011-03-09 22:23:26 -0700421
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300422static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200423 struct perf_event_attr *attr)
424{
425 struct regs_dump *regs = &sample->intr_regs;
426 uint64_t mask = attr->sample_regs_intr;
427 unsigned i = 0, r;
428
429 if (!regs)
430 return;
431
432 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
433 u64 val = regs->regs[i++];
434 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
435 }
436}
437
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300438static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700439 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300440 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700441{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300442 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700443 unsigned long secs;
David Ahern745f43e2011-03-09 22:23:26 -0700444 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700445
David Ahern745f43e2011-03-09 22:23:26 -0700446 if (PRINT_FIELD(COMM)) {
447 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200448 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600449 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200450 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700451 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200452 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700453 }
David Ahernc70c94b2011-03-09 22:23:25 -0700454
David Ahern745f43e2011-03-09 22:23:26 -0700455 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
456 printf("%5d/%-5d ", sample->pid, sample->tid);
457 else if (PRINT_FIELD(PID))
458 printf("%5d ", sample->pid);
459 else if (PRINT_FIELD(TID))
460 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700461
David Ahern745f43e2011-03-09 22:23:26 -0700462 if (PRINT_FIELD(CPU)) {
463 if (latency_format)
464 printf("%3d ", sample->cpu);
465 else
466 printf("[%03d] ", sample->cpu);
467 }
David Ahernc70c94b2011-03-09 22:23:25 -0700468
David Ahern745f43e2011-03-09 22:23:26 -0700469 if (PRINT_FIELD(TIME)) {
470 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300471 secs = nsecs / NSEC_PER_SEC;
472 nsecs -= secs * NSEC_PER_SEC;
Namhyung Kim99620a52016-10-24 11:02:45 +0900473
Adrian Hunter83e19862015-09-25 16:15:36 +0300474 if (nanosecs)
475 printf("%5lu.%09llu: ", secs, nsecs);
Namhyung Kim99620a52016-10-24 11:02:45 +0900476 else {
477 char sample_time[32];
478 timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
479 printf("%12s: ", sample_time);
480 }
David Ahern745f43e2011-03-09 22:23:26 -0700481 }
David Ahernc70c94b2011-03-09 22:23:25 -0700482}
483
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200484static inline char
485mispred_str(struct branch_entry *br)
486{
487 if (!(br->flags.mispred || br->flags.predicted))
488 return '-';
489
490 return br->flags.predicted ? 'P' : 'M';
491}
492
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300493static void print_sample_brstack(struct perf_sample *sample)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200494{
495 struct branch_stack *br = sample->branch_stack;
496 u64 i;
497
498 if (!(br && br->nr))
499 return;
500
501 for (i = 0; i < br->nr; i++) {
502 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
503 br->entries[i].from,
504 br->entries[i].to,
505 mispred_str( br->entries + i),
506 br->entries[i].flags.in_tx? 'X' : '-',
507 br->entries[i].flags.abort? 'A' : '-',
508 br->entries[i].flags.cycles);
509 }
510}
511
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300512static void print_sample_brstacksym(struct perf_sample *sample,
513 struct thread *thread)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200514{
515 struct branch_stack *br = sample->branch_stack;
516 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200517 u64 i, from, to;
518
519 if (!(br && br->nr))
520 return;
521
522 for (i = 0; i < br->nr; i++) {
523
524 memset(&alf, 0, sizeof(alf));
525 memset(&alt, 0, sizeof(alt));
526 from = br->entries[i].from;
527 to = br->entries[i].to;
528
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300529 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200530 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300531 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200532
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300533 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200534 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300535 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200536
537 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
538 putchar('/');
539 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
540 printf("/%c/%c/%c/%d ",
541 mispred_str( br->entries + i),
542 br->entries[i].flags.in_tx? 'X' : '-',
543 br->entries[i].flags.abort? 'A' : '-',
544 br->entries[i].flags.cycles);
545 }
546}
547
548
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300549static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600550 struct thread *thread,
551 struct perf_event_attr *attr)
552{
553 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600554
555 printf("%16" PRIx64, sample->addr);
556
557 if (!sample_addr_correlates_sym(attr))
558 return;
559
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300560 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600561
562 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900563 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900564 if (PRINT_FIELD(SYMOFFSET))
565 symbol__fprintf_symname_offs(al.sym, &al, stdout);
566 else
567 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600568 }
569
570 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900571 printf(" (");
572 map__fprintf_dsoname(al.map, stdout);
573 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600574 }
575}
576
Adrian Huntere2167082016-06-23 16:40:58 +0300577static void print_sample_callindent(struct perf_sample *sample,
578 struct perf_evsel *evsel,
579 struct thread *thread,
580 struct addr_location *al)
581{
582 struct perf_event_attr *attr = &evsel->attr;
583 size_t depth = thread_stack__depth(thread);
584 struct addr_location addr_al;
585 const char *name = NULL;
586 static int spacing;
587 int len = 0;
588 u64 ip = 0;
589
590 /*
591 * The 'return' has already been popped off the stack so the depth has
592 * to be adjusted to match the 'call'.
593 */
594 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
595 depth += 1;
596
597 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
598 if (sample_addr_correlates_sym(attr)) {
599 thread__resolve(thread, &addr_al, sample);
600 if (addr_al.sym)
601 name = addr_al.sym->name;
602 else
603 ip = sample->addr;
604 } else {
605 ip = sample->addr;
606 }
607 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
608 if (al->sym)
609 name = al->sym->name;
610 else
611 ip = sample->ip;
612 }
613
614 if (name)
615 len = printf("%*s%s", (int)depth * 4, "", name);
616 else if (ip)
617 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
618
619 if (len < 0)
620 return;
621
622 /*
623 * Try to keep the output length from changing frequently so that the
624 * output lines up more nicely.
625 */
626 if (len > spacing || (len && len < spacing - 52))
627 spacing = round_up(len + 4, 32);
628
629 if (len < spacing)
630 printf("%*s", spacing - len, "");
631}
632
Andi Kleen224e2c92016-10-07 16:42:27 +0300633static void print_insn(struct perf_sample *sample,
634 struct perf_event_attr *attr)
635{
636 if (PRINT_FIELD(INSNLEN))
637 printf(" ilen: %d", sample->insn_len);
638 if (PRINT_FIELD(INSN)) {
639 int i;
640
641 printf(" insn:");
642 for (i = 0; i < sample->insn_len; i++)
643 printf(" %02x", (unsigned char)sample->insn[i]);
644 }
645}
646
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300647static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900648 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200649 struct thread *thread,
650 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900651{
652 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300653 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900654
Adrian Huntere2167082016-06-23 16:40:58 +0300655 if (PRINT_FIELD(CALLINDENT))
656 print_sample_callindent(sample, evsel, thread, al);
657
Akihiro Nagai95582592012-01-30 13:43:09 +0900658 /* print branch_from information */
659 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300660 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700661 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300662
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300663 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700664 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300665 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700666 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300667
668 if (cursor == NULL) {
669 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300670 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300671 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300672 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300673 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300674 } else
675 putchar('\n');
676
677 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900678 }
679
Akihiro Nagai95582592012-01-30 13:43:09 +0900680 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300681 if (PRINT_FIELD(ADDR) ||
682 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300683 !output[attr->type].user_set)) {
684 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300685 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300686 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900687
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300688 if (print_srcline_last)
689 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
690
Andi Kleen224e2c92016-10-07 16:42:27 +0300691 print_insn(sample, attr);
692
Akihiro Nagai95582592012-01-30 13:43:09 +0900693 printf("\n");
694}
695
Adrian Hunter055cd332016-06-23 16:40:56 +0300696static struct {
697 u32 flags;
698 const char *name;
699} sample_flags[] = {
700 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
701 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
702 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
703 {PERF_IP_FLAG_BRANCH, "jmp"},
704 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
705 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
706 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
707 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
708 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
709 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
710 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
711 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
712 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
713 {0, NULL}
714};
715
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300716static void print_sample_flags(u32 flags)
717{
718 const char *chars = PERF_IP_FLAG_CHARS;
719 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +0300720 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
721 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300722 char str[33];
723 int i, pos = 0;
724
Adrian Hunter055cd332016-06-23 16:40:56 +0300725 for (i = 0; sample_flags[i].name ; i++) {
726 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
727 name = sample_flags[i].name;
728 break;
729 }
730 }
731
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300732 for (i = 0; i < n; i++, flags >>= 1) {
733 if (flags & 1)
734 str[pos++] = chars[i];
735 }
736 for (; i < 32; i++, flags >>= 1) {
737 if (flags & 1)
738 str[pos++] = '?';
739 }
740 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +0300741
742 if (name)
743 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
744 else
745 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300746}
747
Wang Nan30372f02016-02-24 11:20:45 +0000748struct printer_data {
749 int line_no;
750 bool hit_nul;
751 bool is_printable;
752};
753
754static void
755print_sample_bpf_output_printer(enum binary_printer_ops op,
756 unsigned int val,
757 void *extra)
758{
759 unsigned char ch = (unsigned char)val;
760 struct printer_data *printer_data = extra;
761
762 switch (op) {
763 case BINARY_PRINT_DATA_BEGIN:
764 printf("\n");
765 break;
766 case BINARY_PRINT_LINE_BEGIN:
767 printf("%17s", !printer_data->line_no ? "BPF output:" :
768 " ");
769 break;
770 case BINARY_PRINT_ADDR:
771 printf(" %04x:", val);
772 break;
773 case BINARY_PRINT_NUM_DATA:
774 printf(" %02x", val);
775 break;
776 case BINARY_PRINT_NUM_PAD:
777 printf(" ");
778 break;
779 case BINARY_PRINT_SEP:
780 printf(" ");
781 break;
782 case BINARY_PRINT_CHAR_DATA:
783 if (printer_data->hit_nul && ch)
784 printer_data->is_printable = false;
785
786 if (!isprint(ch)) {
787 printf("%c", '.');
788
789 if (!printer_data->is_printable)
790 break;
791
792 if (ch == '\0')
793 printer_data->hit_nul = true;
794 else
795 printer_data->is_printable = false;
796 } else {
797 printf("%c", ch);
798 }
799 break;
800 case BINARY_PRINT_CHAR_PAD:
801 printf(" ");
802 break;
803 case BINARY_PRINT_LINE_END:
804 printf("\n");
805 printer_data->line_no++;
806 break;
807 case BINARY_PRINT_DATA_END:
808 default:
809 break;
810 }
811}
812
813static void print_sample_bpf_output(struct perf_sample *sample)
814{
815 unsigned int nr_bytes = sample->raw_size;
816 struct printer_data printer_data = {0, false, true};
817
818 print_binary(sample->raw_data, nr_bytes, 8,
819 print_sample_bpf_output_printer, &printer_data);
820
821 if (printer_data.is_printable && printer_data.hit_nul)
822 printf("%17s \"%s\"\n", "BPF string:",
823 (char *)(sample->raw_data));
824}
825
Jiri Olsa809e9422015-11-26 18:55:21 +0100826struct perf_script {
827 struct perf_tool tool;
828 struct perf_session *session;
829 bool show_task_events;
830 bool show_mmap_events;
831 bool show_switch_events;
Jiri Olsacfc88742016-01-05 22:09:06 +0100832 bool allocated;
833 struct cpu_map *cpus;
834 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100835 int name_width;
Jiri Olsa809e9422015-11-26 18:55:21 +0100836};
837
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100838static int perf_evlist__max_name_len(struct perf_evlist *evlist)
839{
840 struct perf_evsel *evsel;
841 int max = 0;
842
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300843 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100844 int len = strlen(perf_evsel__name(evsel));
845
846 max = MAX(len, max);
847 }
848
849 return max;
850}
851
Jiri Olsac19ac912016-02-24 09:46:54 +0100852static size_t data_src__printf(u64 data_src)
853{
854 struct mem_info mi = { .data_src.val = data_src };
855 char decode[100];
856 char out[100];
857 static int maxlen;
858 int len;
859
860 perf_script__meminfo_scnprintf(decode, 100, &mi);
861
862 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
863 if (maxlen < len)
864 maxlen = len;
865
866 return printf("%-*s", maxlen, out);
867}
868
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300869static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +0100870 struct perf_sample *sample, struct perf_evsel *evsel,
871 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700872{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300873 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300874 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700875
David Ahern2c9e45f72011-03-17 10:03:21 -0600876 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700877 return;
878
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300879 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700880
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200881 if (PRINT_FIELD(PERIOD))
882 printf("%10" PRIu64 " ", sample->period);
883
Namhyung Kime944d3d2013-11-18 14:34:52 +0900884 if (PRINT_FIELD(EVNAME)) {
885 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100886
887 if (!script->name_width)
888 script->name_width = perf_evlist__max_name_len(script->session->evlist);
889
890 printf("%*s: ", script->name_width,
891 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +0900892 }
893
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300894 if (print_flags)
895 print_sample_flags(sample->flags);
896
Akihiro Nagai95582592012-01-30 13:43:09 +0900897 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300898 print_sample_bts(sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900899 return;
900 }
901
David Ahern745f43e2011-03-09 22:23:26 -0700902 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300903 event_format__print(evsel->tp_format, sample->cpu,
904 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600905 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300906 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600907
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100908 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +0100909 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100910
911 if (PRINT_FIELD(WEIGHT))
912 printf("%16" PRIu64, sample->weight);
913
David Ahern787bef12011-05-27 14:28:43 -0600914 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -0700915 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -0400916
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -0300917 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700918 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300919 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700920 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300921
922 putchar(cursor ? '\n' : ' ');
923 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -0700924 }
925
Stephane Eranianfc36f942015-08-31 18:41:10 +0200926 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300927 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200928
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200929 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300930 print_sample_brstack(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200931 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300932 print_sample_brstacksym(sample, thread);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200933
Wang Nan30372f02016-02-24 11:20:45 +0000934 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
935 print_sample_bpf_output(sample);
Andi Kleen224e2c92016-10-07 16:42:27 +0300936 print_insn(sample, attr);
David Ahernc70c94b2011-03-09 22:23:25 -0700937 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700938}
939
Tom Zanussi956ffd02009-11-25 01:15:46 -0600940static struct scripting_ops *scripting_ops;
941
Jiri Olsa36e33c52016-01-06 11:49:56 +0100942static void __process_stat(struct perf_evsel *counter, u64 tstamp)
943{
944 int nthreads = thread_map__nr(counter->threads);
945 int ncpus = perf_evsel__nr_cpus(counter);
946 int cpu, thread;
947 static int header_printed;
948
949 if (counter->system_wide)
950 nthreads = 1;
951
952 if (!header_printed) {
953 printf("%3s %8s %15s %15s %15s %15s %s\n",
954 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
955 header_printed = 1;
956 }
957
958 for (thread = 0; thread < nthreads; thread++) {
959 for (cpu = 0; cpu < ncpus; cpu++) {
960 struct perf_counts_values *counts;
961
962 counts = perf_counts(counter->counts, cpu, thread);
963
964 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
965 counter->cpus->map[cpu],
966 thread_map__pid(counter->threads, thread),
967 counts->val,
968 counts->ena,
969 counts->run,
970 tstamp,
971 perf_evsel__name(counter));
972 }
973 }
974}
975
Jiri Olsae099eba2016-01-05 22:09:09 +0100976static void process_stat(struct perf_evsel *counter, u64 tstamp)
977{
978 if (scripting_ops && scripting_ops->process_stat)
979 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +0100980 else
981 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +0100982}
983
984static void process_stat_interval(u64 tstamp)
985{
986 if (scripting_ops && scripting_ops->process_stat_interval)
987 scripting_ops->process_stat_interval(tstamp);
988}
989
Tom Zanussi956ffd02009-11-25 01:15:46 -0600990static void setup_scripting(void)
991{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600992 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600993 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600994}
995
Adrian Hunterd445dd22014-08-15 22:08:37 +0300996static int flush_scripting(void)
997{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100998 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300999}
1000
Tom Zanussi956ffd02009-11-25 01:15:46 -06001001static int cleanup_scripting(void)
1002{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001003 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001004
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001005 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001006}
1007
Jiri Olsa809e9422015-11-26 18:55:21 +01001008static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001009 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001010 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001011 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001012 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001013{
Jiri Olsa809e9422015-11-26 18:55:21 +01001014 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001015 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001016
David Ahern1424dc92011-03-09 22:23:28 -07001017 if (debug_mode) {
1018 if (sample->time < last_timestamp) {
1019 pr_err("Samples misordered, previous: %" PRIu64
1020 " this: %" PRIu64 "\n", last_timestamp,
1021 sample->time);
1022 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001023 }
David Ahern1424dc92011-03-09 22:23:28 -07001024 last_timestamp = sample->time;
1025 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001026 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001027
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001028 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001029 pr_err("problem processing %d event, skipping it.\n",
1030 event->header.type);
1031 return -1;
1032 }
1033
1034 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001035 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001036
Anton Blanchard5d67be92011-07-04 21:57:50 +10001037 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001038 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001039
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001040 if (scripting_ops)
1041 scripting_ops->process_event(event, sample, evsel, &al);
1042 else
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001043 process_event(scr, sample, evsel, &al);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001044
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001045out_put:
1046 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001047 return 0;
1048}
1049
Adrian Hunter7ea95722013-11-01 15:51:30 +02001050static int process_attr(struct perf_tool *tool, union perf_event *event,
1051 struct perf_evlist **pevlist)
1052{
1053 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1054 struct perf_evlist *evlist;
1055 struct perf_evsel *evsel, *pos;
1056 int err;
1057
1058 err = perf_event__process_attr(tool, event, pevlist);
1059 if (err)
1060 return err;
1061
1062 evlist = *pevlist;
1063 evsel = perf_evlist__last(*pevlist);
1064
1065 if (evsel->attr.type >= PERF_TYPE_MAX)
1066 return 0;
1067
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001068 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001069 if (pos->attr.type == evsel->attr.type && pos != evsel)
1070 return 0;
1071 }
1072
1073 set_print_ip_opts(&evsel->attr);
1074
Jiri Olsad2b5a312015-10-16 12:41:25 +02001075 if (evsel->attr.sample_type)
1076 err = perf_evsel__check_attr(evsel, scr->session);
1077
1078 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001079}
1080
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001081static int process_comm_event(struct perf_tool *tool,
1082 union perf_event *event,
1083 struct perf_sample *sample,
1084 struct machine *machine)
1085{
1086 struct thread *thread;
1087 struct perf_script *script = container_of(tool, struct perf_script, tool);
1088 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001089 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001090 int ret = -1;
1091
1092 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1093 if (thread == NULL) {
1094 pr_debug("problem processing COMM event, skipping it.\n");
1095 return -1;
1096 }
1097
1098 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1099 goto out;
1100
1101 if (!evsel->attr.sample_id_all) {
1102 sample->cpu = 0;
1103 sample->time = 0;
1104 sample->tid = event->comm.tid;
1105 sample->pid = event->comm.pid;
1106 }
1107 print_sample_start(sample, thread, evsel);
1108 perf_event__fprintf(event, stdout);
1109 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001110out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001111 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001112 return ret;
1113}
1114
1115static int process_fork_event(struct perf_tool *tool,
1116 union perf_event *event,
1117 struct perf_sample *sample,
1118 struct machine *machine)
1119{
1120 struct thread *thread;
1121 struct perf_script *script = container_of(tool, struct perf_script, tool);
1122 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001123 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001124
1125 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1126 return -1;
1127
1128 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1129 if (thread == NULL) {
1130 pr_debug("problem processing FORK event, skipping it.\n");
1131 return -1;
1132 }
1133
1134 if (!evsel->attr.sample_id_all) {
1135 sample->cpu = 0;
1136 sample->time = event->fork.time;
1137 sample->tid = event->fork.tid;
1138 sample->pid = event->fork.pid;
1139 }
1140 print_sample_start(sample, thread, evsel);
1141 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001142 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001143
1144 return 0;
1145}
1146static int process_exit_event(struct perf_tool *tool,
1147 union perf_event *event,
1148 struct perf_sample *sample,
1149 struct machine *machine)
1150{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001151 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001152 struct thread *thread;
1153 struct perf_script *script = container_of(tool, struct perf_script, tool);
1154 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001155 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001156
1157 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1158 if (thread == NULL) {
1159 pr_debug("problem processing EXIT event, skipping it.\n");
1160 return -1;
1161 }
1162
1163 if (!evsel->attr.sample_id_all) {
1164 sample->cpu = 0;
1165 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001166 sample->tid = event->fork.tid;
1167 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001168 }
1169 print_sample_start(sample, thread, evsel);
1170 perf_event__fprintf(event, stdout);
1171
1172 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001173 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001174
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001175 thread__put(thread);
1176 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001177}
1178
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001179static int process_mmap_event(struct perf_tool *tool,
1180 union perf_event *event,
1181 struct perf_sample *sample,
1182 struct machine *machine)
1183{
1184 struct thread *thread;
1185 struct perf_script *script = container_of(tool, struct perf_script, tool);
1186 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001187 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001188
1189 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1190 return -1;
1191
1192 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1193 if (thread == NULL) {
1194 pr_debug("problem processing MMAP event, skipping it.\n");
1195 return -1;
1196 }
1197
1198 if (!evsel->attr.sample_id_all) {
1199 sample->cpu = 0;
1200 sample->time = 0;
1201 sample->tid = event->mmap.tid;
1202 sample->pid = event->mmap.pid;
1203 }
1204 print_sample_start(sample, thread, evsel);
1205 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001206 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001207 return 0;
1208}
1209
1210static int process_mmap2_event(struct perf_tool *tool,
1211 union perf_event *event,
1212 struct perf_sample *sample,
1213 struct machine *machine)
1214{
1215 struct thread *thread;
1216 struct perf_script *script = container_of(tool, struct perf_script, tool);
1217 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001218 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001219
1220 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1221 return -1;
1222
1223 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1224 if (thread == NULL) {
1225 pr_debug("problem processing MMAP2 event, skipping it.\n");
1226 return -1;
1227 }
1228
1229 if (!evsel->attr.sample_id_all) {
1230 sample->cpu = 0;
1231 sample->time = 0;
1232 sample->tid = event->mmap2.tid;
1233 sample->pid = event->mmap2.pid;
1234 }
1235 print_sample_start(sample, thread, evsel);
1236 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001237 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001238 return 0;
1239}
1240
Adrian Hunter7c148982015-07-21 12:44:06 +03001241static int process_switch_event(struct perf_tool *tool,
1242 union perf_event *event,
1243 struct perf_sample *sample,
1244 struct machine *machine)
1245{
1246 struct thread *thread;
1247 struct perf_script *script = container_of(tool, struct perf_script, tool);
1248 struct perf_session *session = script->session;
1249 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1250
1251 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1252 return -1;
1253
1254 thread = machine__findnew_thread(machine, sample->pid,
1255 sample->tid);
1256 if (thread == NULL) {
1257 pr_debug("problem processing SWITCH event, skipping it.\n");
1258 return -1;
1259 }
1260
1261 print_sample_start(sample, thread, evsel);
1262 perf_event__fprintf(event, stdout);
1263 thread__put(thread);
1264 return 0;
1265}
1266
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001267static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001268{
1269 session_done = 1;
1270}
1271
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001272static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001273{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001274 int ret;
1275
Tom Zanussic239da32010-04-01 23:59:18 -05001276 signal(SIGINT, sig_handler);
1277
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001278 /* override event processing functions */
1279 if (script->show_task_events) {
1280 script->tool.comm = process_comm_event;
1281 script->tool.fork = process_fork_event;
1282 script->tool.exit = process_exit_event;
1283 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001284 if (script->show_mmap_events) {
1285 script->tool.mmap = process_mmap_event;
1286 script->tool.mmap2 = process_mmap2_event;
1287 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001288 if (script->show_switch_events)
1289 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001290
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001291 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001292
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001293 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001294 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001295
1296 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001297}
1298
Tom Zanussi956ffd02009-11-25 01:15:46 -06001299struct script_spec {
1300 struct list_head node;
1301 struct scripting_ops *ops;
1302 char spec[0];
1303};
1304
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001305static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001306
1307static struct script_spec *script_spec__new(const char *spec,
1308 struct scripting_ops *ops)
1309{
1310 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1311
1312 if (s != NULL) {
1313 strcpy(s->spec, spec);
1314 s->ops = ops;
1315 }
1316
1317 return s;
1318}
1319
Tom Zanussi956ffd02009-11-25 01:15:46 -06001320static void script_spec__add(struct script_spec *s)
1321{
1322 list_add_tail(&s->node, &script_specs);
1323}
1324
1325static struct script_spec *script_spec__find(const char *spec)
1326{
1327 struct script_spec *s;
1328
1329 list_for_each_entry(s, &script_specs, node)
1330 if (strcasecmp(s->spec, spec) == 0)
1331 return s;
1332 return NULL;
1333}
1334
Tom Zanussi956ffd02009-11-25 01:15:46 -06001335int script_spec_register(const char *spec, struct scripting_ops *ops)
1336{
1337 struct script_spec *s;
1338
1339 s = script_spec__find(spec);
1340 if (s)
1341 return -1;
1342
Taeung Song8560bae2016-02-26 00:13:10 +09001343 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001344 if (!s)
1345 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001346 else
1347 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001348
1349 return 0;
1350}
1351
1352static struct scripting_ops *script_spec__lookup(const char *spec)
1353{
1354 struct script_spec *s = script_spec__find(spec);
1355 if (!s)
1356 return NULL;
1357
1358 return s->ops;
1359}
1360
1361static void list_available_languages(void)
1362{
1363 struct script_spec *s;
1364
1365 fprintf(stderr, "\n");
1366 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001367 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001368
1369 list_for_each_entry(s, &script_specs, node)
1370 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1371
1372 fprintf(stderr, "\n");
1373}
1374
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001375static int parse_scriptname(const struct option *opt __maybe_unused,
1376 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001377{
1378 char spec[PATH_MAX];
1379 const char *script, *ext;
1380 int len;
1381
Tom Zanussif526d682010-01-27 02:27:52 -06001382 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001383 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001384 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001385 }
1386
1387 script = strchr(str, ':');
1388 if (script) {
1389 len = script - str;
1390 if (len >= PATH_MAX) {
1391 fprintf(stderr, "invalid language specifier");
1392 return -1;
1393 }
1394 strncpy(spec, str, len);
1395 spec[len] = '\0';
1396 scripting_ops = script_spec__lookup(spec);
1397 if (!scripting_ops) {
1398 fprintf(stderr, "invalid language specifier");
1399 return -1;
1400 }
1401 script++;
1402 } else {
1403 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001404 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001405 if (!ext) {
1406 fprintf(stderr, "invalid script extension");
1407 return -1;
1408 }
1409 scripting_ops = script_spec__lookup(++ext);
1410 if (!scripting_ops) {
1411 fprintf(stderr, "invalid script extension");
1412 return -1;
1413 }
1414 }
1415
1416 script_name = strdup(script);
1417
1418 return 0;
1419}
1420
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001421static int parse_output_fields(const struct option *opt __maybe_unused,
1422 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001423{
1424 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001425 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001426 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001427 int rc = 0;
1428 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001429 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001430
1431 if (!str)
1432 return -ENOMEM;
1433
David Ahern2c9e45f72011-03-17 10:03:21 -06001434 /* first word can state for which event type the user is specifying
1435 * the fields. If no type exists, the specified fields apply to all
1436 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001437 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001438 tok = strchr(str, ':');
1439 if (tok) {
1440 *tok = '\0';
1441 tok++;
1442 if (!strcmp(str, "hw"))
1443 type = PERF_TYPE_HARDWARE;
1444 else if (!strcmp(str, "sw"))
1445 type = PERF_TYPE_SOFTWARE;
1446 else if (!strcmp(str, "trace"))
1447 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001448 else if (!strcmp(str, "raw"))
1449 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001450 else if (!strcmp(str, "break"))
1451 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001452 else {
1453 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001454 rc = -EINVAL;
1455 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001456 }
1457
1458 if (output[type].user_set)
1459 pr_warning("Overriding previous field request for %s events.\n",
1460 event_type(type));
1461
1462 output[type].fields = 0;
1463 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001464 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001465
1466 } else {
1467 tok = str;
1468 if (strlen(str) == 0) {
1469 fprintf(stderr,
1470 "Cannot set fields to 'none' for all event types.\n");
1471 rc = -EINVAL;
1472 goto out;
1473 }
1474
1475 if (output_set_by_user())
1476 pr_warning("Overriding previous field request for all events.\n");
1477
1478 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1479 output[j].fields = 0;
1480 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001481 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001482 }
David Ahern1424dc92011-03-09 22:23:28 -07001483 }
1484
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001485 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001486 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001487 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001488 break;
David Ahern745f43e2011-03-09 22:23:26 -07001489 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001490 if (i == imax && strcmp(tok, "flags") == 0) {
1491 print_flags = true;
1492 continue;
1493 }
David Ahern745f43e2011-03-09 22:23:26 -07001494 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001495 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001496 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001497 goto out;
1498 }
1499
1500 if (type == -1) {
1501 /* add user option to all events types for
1502 * which it is valid
1503 */
1504 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1505 if (output[j].invalid_fields & all_output_options[i].field) {
1506 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1507 all_output_options[i].str, event_type(j));
1508 } else
1509 output[j].fields |= all_output_options[i].field;
1510 }
1511 } else {
1512 if (output[type].invalid_fields & all_output_options[i].field) {
1513 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1514 all_output_options[i].str, event_type(type));
1515
1516 rc = -EINVAL;
1517 goto out;
1518 }
1519 output[type].fields |= all_output_options[i].field;
1520 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001521 }
1522
1523 if (type >= 0) {
1524 if (output[type].fields == 0) {
1525 pr_debug("No fields requested for %s type. "
1526 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001527 }
David Ahern1424dc92011-03-09 22:23:28 -07001528 }
David Ahern745f43e2011-03-09 22:23:26 -07001529
David Ahern2c9e45f72011-03-17 10:03:21 -06001530out:
David Ahern745f43e2011-03-09 22:23:26 -07001531 free(str);
1532 return rc;
1533}
1534
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001535/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1536static int is_directory(const char *base_path, const struct dirent *dent)
1537{
1538 char path[PATH_MAX];
1539 struct stat st;
1540
1541 sprintf(path, "%s/%s", base_path, dent->d_name);
1542 if (stat(path, &st))
1543 return 0;
1544
1545 return S_ISDIR(st.st_mode);
1546}
1547
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001548#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1549 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1550 if ((lang_dirent->d_type == DT_DIR || \
1551 (lang_dirent->d_type == DT_UNKNOWN && \
1552 is_directory(scripts_path, lang_dirent))) && \
1553 (strcmp(lang_dirent->d_name, ".")) && \
1554 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001555
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001556#define for_each_script(lang_path, lang_dir, script_dirent) \
1557 while ((script_dirent = readdir(lang_dir)) != NULL) \
1558 if (script_dirent->d_type != DT_DIR && \
1559 (script_dirent->d_type != DT_UNKNOWN || \
1560 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001561
1562
1563#define RECORD_SUFFIX "-record"
1564#define REPORT_SUFFIX "-report"
1565
1566struct script_desc {
1567 struct list_head node;
1568 char *name;
1569 char *half_liner;
1570 char *args;
1571};
1572
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001573static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001574
1575static struct script_desc *script_desc__new(const char *name)
1576{
1577 struct script_desc *s = zalloc(sizeof(*s));
1578
Tom Zanussib5b87312010-11-10 08:16:51 -06001579 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001580 s->name = strdup(name);
1581
1582 return s;
1583}
1584
1585static void script_desc__delete(struct script_desc *s)
1586{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001587 zfree(&s->name);
1588 zfree(&s->half_liner);
1589 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001590 free(s);
1591}
1592
1593static void script_desc__add(struct script_desc *s)
1594{
1595 list_add_tail(&s->node, &script_descs);
1596}
1597
1598static struct script_desc *script_desc__find(const char *name)
1599{
1600 struct script_desc *s;
1601
1602 list_for_each_entry(s, &script_descs, node)
1603 if (strcasecmp(s->name, name) == 0)
1604 return s;
1605 return NULL;
1606}
1607
1608static struct script_desc *script_desc__findnew(const char *name)
1609{
1610 struct script_desc *s = script_desc__find(name);
1611
1612 if (s)
1613 return s;
1614
1615 s = script_desc__new(name);
1616 if (!s)
1617 goto out_delete_desc;
1618
1619 script_desc__add(s);
1620
1621 return s;
1622
1623out_delete_desc:
1624 script_desc__delete(s);
1625
1626 return NULL;
1627}
1628
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001629static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001630{
1631 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001632 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001633
1634 if (strlen(str) > suffix_len) {
1635 p = str + strlen(str) - suffix_len;
1636 if (!strncmp(p, suffix, suffix_len))
1637 return p;
1638 }
1639
1640 return NULL;
1641}
1642
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001643static int read_script_info(struct script_desc *desc, const char *filename)
1644{
1645 char line[BUFSIZ], *p;
1646 FILE *fp;
1647
1648 fp = fopen(filename, "r");
1649 if (!fp)
1650 return -1;
1651
1652 while (fgets(line, sizeof(line), fp)) {
1653 p = ltrim(line);
1654 if (strlen(p) == 0)
1655 continue;
1656 if (*p != '#')
1657 continue;
1658 p++;
1659 if (strlen(p) && *p == '!')
1660 continue;
1661
1662 p = ltrim(p);
1663 if (strlen(p) && p[strlen(p) - 1] == '\n')
1664 p[strlen(p) - 1] = '\0';
1665
1666 if (!strncmp(p, "description:", strlen("description:"))) {
1667 p += strlen("description:");
1668 desc->half_liner = strdup(ltrim(p));
1669 continue;
1670 }
1671
1672 if (!strncmp(p, "args:", strlen("args:"))) {
1673 p += strlen("args:");
1674 desc->args = strdup(ltrim(p));
1675 continue;
1676 }
1677 }
1678
1679 fclose(fp);
1680
1681 return 0;
1682}
1683
Robert Richter38efb532011-11-25 11:38:40 +01001684static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1685{
1686 char *script_root, *str;
1687
1688 script_root = strdup(script_dirent->d_name);
1689 if (!script_root)
1690 return NULL;
1691
1692 str = (char *)ends_with(script_root, suffix);
1693 if (!str) {
1694 free(script_root);
1695 return NULL;
1696 }
1697
1698 *str = '\0';
1699 return script_root;
1700}
1701
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001702static int list_available_scripts(const struct option *opt __maybe_unused,
1703 const char *s __maybe_unused,
1704 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001705{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001706 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001707 char scripts_path[MAXPATHLEN];
1708 DIR *scripts_dir, *lang_dir;
1709 char script_path[MAXPATHLEN];
1710 char lang_path[MAXPATHLEN];
1711 struct script_desc *desc;
1712 char first_half[BUFSIZ];
1713 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001714
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001715 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001716
1717 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00001718 if (!scripts_dir) {
1719 fprintf(stdout,
1720 "open(%s) failed.\n"
1721 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
1722 scripts_path);
1723 exit(-1);
1724 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001725
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001726 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001727 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001728 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001729 lang_dir = opendir(lang_path);
1730 if (!lang_dir)
1731 continue;
1732
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001733 for_each_script(lang_path, lang_dir, script_dirent) {
1734 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01001735 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001736 desc = script_desc__findnew(script_root);
1737 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001738 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001739 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001740 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001741 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001742 }
1743 }
1744
1745 fprintf(stdout, "List of available trace scripts:\n");
1746 list_for_each_entry(desc, &script_descs, node) {
1747 sprintf(first_half, "%s %s", desc->name,
1748 desc->args ? desc->args : "");
1749 fprintf(stdout, " %-36s %s\n", first_half,
1750 desc->half_liner ? desc->half_liner : "");
1751 }
1752
1753 exit(0);
1754}
1755
Feng Tange5f37052012-09-07 16:42:26 +08001756/*
Feng Tang49e639e2012-10-30 11:56:03 +08001757 * Some scripts specify the required events in their "xxx-record" file,
1758 * this function will check if the events in perf.data match those
1759 * mentioned in the "xxx-record".
1760 *
1761 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1762 * which is covered well now. And new parsing code should be added to
1763 * cover the future complexing formats like event groups etc.
1764 */
1765static int check_ev_match(char *dir_name, char *scriptname,
1766 struct perf_session *session)
1767{
1768 char filename[MAXPATHLEN], evname[128];
1769 char line[BUFSIZ], *p;
1770 struct perf_evsel *pos;
1771 int match, len;
1772 FILE *fp;
1773
1774 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1775
1776 fp = fopen(filename, "r");
1777 if (!fp)
1778 return -1;
1779
1780 while (fgets(line, sizeof(line), fp)) {
1781 p = ltrim(line);
1782 if (*p == '#')
1783 continue;
1784
1785 while (strlen(p)) {
1786 p = strstr(p, "-e");
1787 if (!p)
1788 break;
1789
1790 p += 2;
1791 p = ltrim(p);
1792 len = strcspn(p, " \t");
1793 if (!len)
1794 break;
1795
1796 snprintf(evname, len + 1, "%s", p);
1797
1798 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001799 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001800 if (!strcmp(perf_evsel__name(pos), evname)) {
1801 match = 1;
1802 break;
1803 }
1804 }
1805
1806 if (!match) {
1807 fclose(fp);
1808 return -1;
1809 }
1810 }
1811 }
1812
1813 fclose(fp);
1814 return 0;
1815}
1816
1817/*
Feng Tange5f37052012-09-07 16:42:26 +08001818 * Return -1 if none is found, otherwise the actual scripts number.
1819 *
1820 * Currently the only user of this function is the script browser, which
1821 * will list all statically runnable scripts, select one, execute it and
1822 * show the output in a perf browser.
1823 */
1824int find_scripts(char **scripts_array, char **scripts_path_array)
1825{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001826 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001827 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001828 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001829 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001830 struct perf_data_file file = {
1831 .path = input_name,
1832 .mode = PERF_DATA_MODE_READ,
1833 };
Feng Tange5f37052012-09-07 16:42:26 +08001834 char *temp;
1835 int i = 0;
1836
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001837 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001838 if (!session)
1839 return -1;
1840
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001841 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08001842
1843 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001844 if (!scripts_dir) {
1845 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001846 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001847 }
Feng Tange5f37052012-09-07 16:42:26 +08001848
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001849 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08001850 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001851 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08001852#ifdef NO_LIBPERL
1853 if (strstr(lang_path, "perl"))
1854 continue;
1855#endif
1856#ifdef NO_LIBPYTHON
1857 if (strstr(lang_path, "python"))
1858 continue;
1859#endif
1860
1861 lang_dir = opendir(lang_path);
1862 if (!lang_dir)
1863 continue;
1864
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001865 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08001866 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001867 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08001868 continue;
1869 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001870 script_dirent->d_name);
1871 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08001872 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001873 (temp - script_dirent->d_name) + 1,
1874 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001875
1876 if (check_ev_match(lang_path,
1877 scripts_array[i], session))
1878 continue;
1879
Feng Tange5f37052012-09-07 16:42:26 +08001880 i++;
1881 }
Feng Tang49e639e2012-10-30 11:56:03 +08001882 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001883 }
1884
Feng Tang49e639e2012-10-30 11:56:03 +08001885 closedir(scripts_dir);
1886 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001887 return i;
1888}
1889
Tom Zanussi38752942009-12-15 02:53:39 -06001890static char *get_script_path(const char *script_root, const char *suffix)
1891{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001892 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06001893 char scripts_path[MAXPATHLEN];
1894 char script_path[MAXPATHLEN];
1895 DIR *scripts_dir, *lang_dir;
1896 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001897 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001898
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001899 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06001900
1901 scripts_dir = opendir(scripts_path);
1902 if (!scripts_dir)
1903 return NULL;
1904
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001905 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06001906 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001907 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06001908 lang_dir = opendir(lang_path);
1909 if (!lang_dir)
1910 continue;
1911
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001912 for_each_script(lang_path, lang_dir, script_dirent) {
1913 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01001914 if (__script_root && !strcmp(script_root, __script_root)) {
1915 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001916 closedir(lang_dir);
1917 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001918 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001919 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001920 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001921 }
1922 free(__script_root);
1923 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001924 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001925 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001926 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001927
Robert Richter38efb532011-11-25 11:38:40 +01001928 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001929}
1930
Tom Zanussib5b87312010-11-10 08:16:51 -06001931static bool is_top_script(const char *script_path)
1932{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001933 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001934}
1935
1936static int has_required_arg(char *script_path)
1937{
1938 struct script_desc *desc;
1939 int n_args = 0;
1940 char *p;
1941
1942 desc = script_desc__new(NULL);
1943
1944 if (read_script_info(desc, script_path))
1945 goto out;
1946
1947 if (!desc->args)
1948 goto out;
1949
1950 for (p = desc->args; *p; p++)
1951 if (*p == '<')
1952 n_args++;
1953out:
1954 script_desc__delete(desc);
1955
1956 return n_args;
1957}
1958
David Ahernd54b1a92012-08-26 12:24:46 -06001959static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001960{
1961 char **__argv = malloc(sizeof(const char *) * argc);
1962
David Ahernd54b1a92012-08-26 12:24:46 -06001963 if (!__argv) {
1964 pr_err("malloc failed\n");
1965 return -1;
1966 }
1967
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001968 memcpy(__argv, argv, sizeof(const char *) * argc);
1969 argc = parse_options(argc, (const char **)__argv, record_options,
1970 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1971 free(__argv);
1972
David Ahernd54b1a92012-08-26 12:24:46 -06001973 system_wide = (argc == 0);
1974
1975 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001976}
1977
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001978static void script__setup_sample_type(struct perf_script *script)
1979{
1980 struct perf_session *session = script->session;
1981 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1982
1983 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1984 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1985 (sample_type & PERF_SAMPLE_STACK_USER))
1986 callchain_param.record_mode = CALLCHAIN_DWARF;
1987 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1988 callchain_param.record_mode = CALLCHAIN_LBR;
1989 else
1990 callchain_param.record_mode = CALLCHAIN_FP;
1991 }
1992}
1993
Jiri Olsae099eba2016-01-05 22:09:09 +01001994static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1995 union perf_event *event,
1996 struct perf_session *session)
1997{
1998 struct stat_round_event *round = &event->stat_round;
1999 struct perf_evsel *counter;
2000
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002001 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002002 perf_stat_process_counter(&stat_config, counter);
2003 process_stat(counter, round->time);
2004 }
2005
2006 process_stat_interval(round->time);
2007 return 0;
2008}
2009
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002010static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2011 union perf_event *event,
2012 struct perf_session *session __maybe_unused)
2013{
2014 perf_event__read_stat_config(&stat_config, &event->stat_config);
2015 return 0;
2016}
2017
Jiri Olsacfc88742016-01-05 22:09:06 +01002018static int set_maps(struct perf_script *script)
2019{
2020 struct perf_evlist *evlist = script->session->evlist;
2021
2022 if (!script->cpus || !script->threads)
2023 return 0;
2024
2025 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2026 return -EINVAL;
2027
2028 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2029
2030 if (perf_evlist__alloc_stats(evlist, true))
2031 return -ENOMEM;
2032
2033 script->allocated = true;
2034 return 0;
2035}
2036
2037static
2038int process_thread_map_event(struct perf_tool *tool,
2039 union perf_event *event,
2040 struct perf_session *session __maybe_unused)
2041{
2042 struct perf_script *script = container_of(tool, struct perf_script, tool);
2043
2044 if (script->threads) {
2045 pr_warning("Extra thread map event, ignoring.\n");
2046 return 0;
2047 }
2048
2049 script->threads = thread_map__new_event(&event->thread_map);
2050 if (!script->threads)
2051 return -ENOMEM;
2052
2053 return set_maps(script);
2054}
2055
2056static
2057int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2058 union perf_event *event,
2059 struct perf_session *session __maybe_unused)
2060{
2061 struct perf_script *script = container_of(tool, struct perf_script, tool);
2062
2063 if (script->cpus) {
2064 pr_warning("Extra cpu map event, ignoring.\n");
2065 return 0;
2066 }
2067
2068 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2069 if (!script->cpus)
2070 return -ENOMEM;
2071
2072 return set_maps(script);
2073}
2074
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002075int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002076{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002077 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002078 bool header = false;
2079 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002080 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002081 char *rec_script_path = NULL;
2082 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002083 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002084 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002085 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002086 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002087 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002088 struct perf_script script = {
2089 .tool = {
2090 .sample = process_sample_event,
2091 .mmap = perf_event__process_mmap,
2092 .mmap2 = perf_event__process_mmap2,
2093 .comm = perf_event__process_comm,
2094 .exit = perf_event__process_exit,
2095 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002096 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002097 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002098 .tracing_data = perf_event__process_tracing_data,
2099 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002100 .id_index = perf_event__process_id_index,
2101 .auxtrace_info = perf_event__process_auxtrace_info,
2102 .auxtrace = perf_event__process_auxtrace,
2103 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002104 .stat = perf_event__process_stat_event,
2105 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002106 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002107 .thread_map = process_thread_map_event,
2108 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002109 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002110 .ordering_requires_timestamps = true,
2111 },
2112 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002113 struct perf_data_file file = {
2114 .mode = PERF_DATA_MODE_READ,
2115 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002116 const struct option options[] = {
2117 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2118 "dump raw trace in ASCII"),
2119 OPT_INCR('v', "verbose", &verbose,
2120 "be more verbose (show symbol address, etc)"),
2121 OPT_BOOLEAN('L', "Latency", &latency_format,
2122 "show latency attributes (irqs/preemption disabled, etc)"),
2123 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2124 list_available_scripts),
2125 OPT_CALLBACK('s', "script", NULL, "name",
2126 "script file name (lang:script name, script name, or *)",
2127 parse_scriptname),
2128 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2129 "generate perf-script.xx script in specified language"),
2130 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2131 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2132 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002133 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2134 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002135 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2136 "file", "vmlinux pathname"),
2137 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2138 "file", "kallsyms pathname"),
2139 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2140 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002141 OPT_CALLBACK(0, "symfs", NULL, "directory",
2142 "Look for files with symbols relative to this directory",
2143 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002144 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002145 "comma separated output fields prepend with 'type:'. "
2146 "Valid types: hw,sw,trace,raw. "
2147 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002148 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen224e2c92016-10-07 16:42:27 +03002149 "bpf-output,callindent,insn,insnlen", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002150 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2151 "system-wide collection from all CPUs"),
2152 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2153 "only consider these symbols"),
David Ahern64eff7d2016-11-25 13:00:21 -07002154 OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
2155 "Stop display of callgraph at these symbols"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002156 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2157 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2158 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002159 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2160 "only consider symbols in these pids"),
2161 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2162 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002163 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2164 "Set the maximum stack depth when parsing the callchain, "
2165 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002166 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002167 OPT_BOOLEAN('I', "show-info", &show_full_info,
2168 "display extended information from perf.data file"),
2169 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2170 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002171 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2172 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002173 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2174 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002175 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2176 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08002177 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002178 OPT_BOOLEAN(0, "ns", &nanosecs,
2179 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002180 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2181 "Instruction Tracing options",
2182 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002183 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2184 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002185 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2186 "Enable symbol demangling"),
2187 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2188 "Enable kernel symbol demangling"),
2189
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002190 OPT_END()
2191 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002192 const char * const script_subcommands[] = { "record", "report", NULL };
2193 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002194 "perf script [<options>]",
2195 "perf script [<options>] record <script> [<record-options>] <command>",
2196 "perf script [<options>] report <script> [script-args]",
2197 "perf script [<options>] <script> [<record-options>] <command>",
2198 "perf script [<options>] <top-script> [script-args]",
2199 NULL
2200 };
Tom Zanussi38752942009-12-15 02:53:39 -06002201
Tom Zanussib5b87312010-11-10 08:16:51 -06002202 setup_scripting();
2203
Yunlong Song40cae2b2015-03-18 21:35:54 +08002204 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002205 PARSE_OPT_STOP_AT_NON_OPTION);
2206
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002207 file.path = input_name;
2208
Tom Zanussib5b87312010-11-10 08:16:51 -06002209 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2210 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2211 if (!rec_script_path)
2212 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06002213 }
2214
Tom Zanussib5b87312010-11-10 08:16:51 -06002215 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2216 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2217 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002218 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002219 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002220 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002221 return -1;
2222 }
Tom Zanussi38752942009-12-15 02:53:39 -06002223 }
2224
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002225 if (itrace_synth_opts.callchain &&
2226 itrace_synth_opts.callchain_sz > scripting_max_stack)
2227 scripting_max_stack = itrace_synth_opts.callchain_sz;
2228
Ben Hutchings44e668c2010-10-10 16:10:03 +01002229 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002230 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002231
Tom Zanussib5b87312010-11-10 08:16:51 -06002232 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002233 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002234 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002235 pid_t pid;
2236
Tom Zanussib5b87312010-11-10 08:16:51 -06002237 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2238 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2239
2240 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002241 usage_with_options_msg(script_usage, options,
2242 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002243 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002244 }
2245
Tom Zanussib5b87312010-11-10 08:16:51 -06002246 if (is_top_script(argv[0])) {
2247 rep_args = argc - 1;
2248 } else {
2249 int rec_args;
2250
2251 rep_args = has_required_arg(rep_script_path);
2252 rec_args = (argc - 1) - rep_args;
2253 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002254 usage_with_options_msg(script_usage, options,
2255 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002256 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002257 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002258 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002259 }
2260
2261 if (pipe(live_pipe) < 0) {
2262 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002263 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002264 }
2265
2266 pid = fork();
2267 if (pid < 0) {
2268 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002269 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002270 }
2271
2272 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002273 j = 0;
2274
Tom Zanussia0cccc22010-04-01 23:59:25 -05002275 dup2(live_pipe[1], 1);
2276 close(live_pipe[0]);
2277
Robert Richter317df652011-11-25 15:05:25 +01002278 if (is_top_script(argv[0])) {
2279 system_wide = true;
2280 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002281 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2282 err = -1;
2283 goto out;
2284 }
Robert Richter317df652011-11-25 15:05:25 +01002285 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002286
2287 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002288 if (!__argv) {
2289 pr_err("malloc failed\n");
2290 err = -ENOMEM;
2291 goto out;
2292 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002293
Tom Zanussib5b87312010-11-10 08:16:51 -06002294 __argv[j++] = "/bin/sh";
2295 __argv[j++] = rec_script_path;
2296 if (system_wide)
2297 __argv[j++] = "-a";
2298 __argv[j++] = "-q";
2299 __argv[j++] = "-o";
2300 __argv[j++] = "-";
2301 for (i = rep_args + 1; i < argc; i++)
2302 __argv[j++] = argv[i];
2303 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002304
2305 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002306 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002307 exit(-1);
2308 }
2309
2310 dup2(live_pipe[0], 0);
2311 close(live_pipe[1]);
2312
Tom Zanussib5b87312010-11-10 08:16:51 -06002313 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002314 if (!__argv) {
2315 pr_err("malloc failed\n");
2316 err = -ENOMEM;
2317 goto out;
2318 }
2319
Tom Zanussib5b87312010-11-10 08:16:51 -06002320 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002321 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002322 __argv[j++] = rep_script_path;
2323 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002324 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002325 __argv[j++] = "-i";
2326 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002327 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002328
2329 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002330 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002331 exit(-1);
2332 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002333
Tom Zanussib5b87312010-11-10 08:16:51 -06002334 if (rec_script_path)
2335 script_path = rec_script_path;
2336 if (rep_script_path)
2337 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002338
Tom Zanussib5b87312010-11-10 08:16:51 -06002339 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002340 j = 0;
2341
Robert Richter317df652011-11-25 15:05:25 +01002342 if (!rec_script_path)
2343 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002344 else if (!system_wide) {
2345 if (have_cmd(argc - 1, &argv[1]) != 0) {
2346 err = -1;
2347 goto out;
2348 }
2349 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002350
2351 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002352 if (!__argv) {
2353 pr_err("malloc failed\n");
2354 err = -ENOMEM;
2355 goto out;
2356 }
2357
Tom Zanussib5b87312010-11-10 08:16:51 -06002358 __argv[j++] = "/bin/sh";
2359 __argv[j++] = script_path;
2360 if (system_wide)
2361 __argv[j++] = "-a";
2362 for (i = 2; i < argc; i++)
2363 __argv[j++] = argv[i];
2364 __argv[j++] = NULL;
2365
2366 execvp("/bin/sh", (char **)__argv);
2367 free(__argv);
2368 exit(-1);
2369 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002370
Tom Zanussicf4fee52010-03-03 01:04:33 -06002371 if (!script_name)
2372 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002373
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002374 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002375 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002376 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002377
Jiri Olsae90debd2013-12-09 11:02:50 +01002378 if (header || header_only) {
2379 perf_session__fprintf_info(session, stdout, show_full_info);
2380 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002381 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002382 }
2383
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002384 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002385 goto out_delete;
2386
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002387 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002388 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002389
Adrian Huntere2167082016-06-23 16:40:58 +03002390 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2391 itrace_synth_opts.thread_stack = true;
2392
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002393 session->itrace_synth_opts = &itrace_synth_opts;
2394
Anton Blanchard5d67be92011-07-04 21:57:50 +10002395 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002396 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2397 if (err < 0)
2398 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002399 }
2400
David Ahern1424dc92011-03-09 22:23:28 -07002401 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002402 symbol_conf.use_callchain = true;
2403 else
2404 symbol_conf.use_callchain = false;
2405
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002406 if (session->tevent.pevent &&
2407 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002408 machine__resolve_kernel_addr,
2409 &session->machines.host) < 0) {
2410 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2411 return -1;
2412 }
2413
Tom Zanussi956ffd02009-11-25 01:15:46 -06002414 if (generate_script_lang) {
2415 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002416 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002417
David Ahern2c9e45f72011-03-17 10:03:21 -06002418 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002419 fprintf(stderr,
2420 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002421 err = -EINVAL;
2422 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002423 }
2424
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002425 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002426 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002427 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002428 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002429 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002430 }
2431
2432 err = fstat(input, &perf_stat);
2433 if (err < 0) {
2434 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002435 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002436 }
2437
2438 if (!perf_stat.st_size) {
2439 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002440 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002441 }
2442
2443 scripting_ops = script_spec__lookup(generate_script_lang);
2444 if (!scripting_ops) {
2445 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002446 err = -ENOENT;
2447 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002448 }
2449
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002450 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002451 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002452 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002453 }
2454
2455 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002456 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002457 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002458 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002459 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002460 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002461 }
2462
David Ahern9cbdb702011-04-06 21:54:20 -06002463
2464 err = perf_session__check_output_opt(session);
2465 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002466 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002467
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002468 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002469
Adrian Hunterd445dd22014-08-15 22:08:37 +03002470 flush_scripting();
2471
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002472out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002473 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002474 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002475
2476 if (script_started)
2477 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002478out:
2479 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002480}