blob: 412fb6e65ac06dcdb7e00e667c9f12d0a8edbeda [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;
444 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700445 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700446
David Ahern745f43e2011-03-09 22:23:26 -0700447 if (PRINT_FIELD(COMM)) {
448 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200449 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600450 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200451 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700452 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200453 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700454 }
David Ahernc70c94b2011-03-09 22:23:25 -0700455
David Ahern745f43e2011-03-09 22:23:26 -0700456 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
457 printf("%5d/%-5d ", sample->pid, sample->tid);
458 else if (PRINT_FIELD(PID))
459 printf("%5d ", sample->pid);
460 else if (PRINT_FIELD(TID))
461 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700462
David Ahern745f43e2011-03-09 22:23:26 -0700463 if (PRINT_FIELD(CPU)) {
464 if (latency_format)
465 printf("%3d ", sample->cpu);
466 else
467 printf("[%03d] ", sample->cpu);
468 }
David Ahernc70c94b2011-03-09 22:23:25 -0700469
David Ahern745f43e2011-03-09 22:23:26 -0700470 if (PRINT_FIELD(TIME)) {
471 nsecs = sample->time;
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300472 secs = nsecs / NSEC_PER_SEC;
473 nsecs -= secs * NSEC_PER_SEC;
474 usecs = nsecs / NSEC_PER_USEC;
Adrian Hunter83e19862015-09-25 16:15:36 +0300475 if (nanosecs)
476 printf("%5lu.%09llu: ", secs, nsecs);
477 else
478 printf("%5lu.%06lu: ", secs, usecs);
David Ahern745f43e2011-03-09 22:23:26 -0700479 }
David Ahernc70c94b2011-03-09 22:23:25 -0700480}
481
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200482static inline char
483mispred_str(struct branch_entry *br)
484{
485 if (!(br->flags.mispred || br->flags.predicted))
486 return '-';
487
488 return br->flags.predicted ? 'P' : 'M';
489}
490
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300491static void print_sample_brstack(struct perf_sample *sample)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200492{
493 struct branch_stack *br = sample->branch_stack;
494 u64 i;
495
496 if (!(br && br->nr))
497 return;
498
499 for (i = 0; i < br->nr; i++) {
500 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
501 br->entries[i].from,
502 br->entries[i].to,
503 mispred_str( br->entries + i),
504 br->entries[i].flags.in_tx? 'X' : '-',
505 br->entries[i].flags.abort? 'A' : '-',
506 br->entries[i].flags.cycles);
507 }
508}
509
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300510static void print_sample_brstacksym(struct perf_sample *sample,
511 struct thread *thread)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200512{
513 struct branch_stack *br = sample->branch_stack;
514 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200515 u64 i, from, to;
516
517 if (!(br && br->nr))
518 return;
519
520 for (i = 0; i < br->nr; i++) {
521
522 memset(&alf, 0, sizeof(alf));
523 memset(&alt, 0, sizeof(alt));
524 from = br->entries[i].from;
525 to = br->entries[i].to;
526
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300527 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200528 if (alf.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300529 alf.sym = map__find_symbol(alf.map, alf.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200530
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300531 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200532 if (alt.map)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300533 alt.sym = map__find_symbol(alt.map, alt.addr);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200534
535 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
536 putchar('/');
537 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
538 printf("/%c/%c/%c/%d ",
539 mispred_str( br->entries + i),
540 br->entries[i].flags.in_tx? 'X' : '-',
541 br->entries[i].flags.abort? 'A' : '-',
542 br->entries[i].flags.cycles);
543 }
544}
545
546
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300547static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600548 struct thread *thread,
549 struct perf_event_attr *attr)
550{
551 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600552
553 printf("%16" PRIx64, sample->addr);
554
555 if (!sample_addr_correlates_sym(attr))
556 return;
557
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300558 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600559
560 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900561 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900562 if (PRINT_FIELD(SYMOFFSET))
563 symbol__fprintf_symname_offs(al.sym, &al, stdout);
564 else
565 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600566 }
567
568 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900569 printf(" (");
570 map__fprintf_dsoname(al.map, stdout);
571 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600572 }
573}
574
Adrian Huntere2167082016-06-23 16:40:58 +0300575static void print_sample_callindent(struct perf_sample *sample,
576 struct perf_evsel *evsel,
577 struct thread *thread,
578 struct addr_location *al)
579{
580 struct perf_event_attr *attr = &evsel->attr;
581 size_t depth = thread_stack__depth(thread);
582 struct addr_location addr_al;
583 const char *name = NULL;
584 static int spacing;
585 int len = 0;
586 u64 ip = 0;
587
588 /*
589 * The 'return' has already been popped off the stack so the depth has
590 * to be adjusted to match the 'call'.
591 */
592 if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
593 depth += 1;
594
595 if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
596 if (sample_addr_correlates_sym(attr)) {
597 thread__resolve(thread, &addr_al, sample);
598 if (addr_al.sym)
599 name = addr_al.sym->name;
600 else
601 ip = sample->addr;
602 } else {
603 ip = sample->addr;
604 }
605 } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
606 if (al->sym)
607 name = al->sym->name;
608 else
609 ip = sample->ip;
610 }
611
612 if (name)
613 len = printf("%*s%s", (int)depth * 4, "", name);
614 else if (ip)
615 len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
616
617 if (len < 0)
618 return;
619
620 /*
621 * Try to keep the output length from changing frequently so that the
622 * output lines up more nicely.
623 */
624 if (len > spacing || (len && len < spacing - 52))
625 spacing = round_up(len + 4, 32);
626
627 if (len < spacing)
628 printf("%*s", spacing - len, "");
629}
630
Andi Kleen224e2c92016-10-07 16:42:27 +0300631static void print_insn(struct perf_sample *sample,
632 struct perf_event_attr *attr)
633{
634 if (PRINT_FIELD(INSNLEN))
635 printf(" ilen: %d", sample->insn_len);
636 if (PRINT_FIELD(INSN)) {
637 int i;
638
639 printf(" insn:");
640 for (i = 0; i < sample->insn_len; i++)
641 printf(" %02x", (unsigned char)sample->insn[i]);
642 }
643}
644
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300645static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900646 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200647 struct thread *thread,
648 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900649{
650 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300651 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900652
Adrian Huntere2167082016-06-23 16:40:58 +0300653 if (PRINT_FIELD(CALLINDENT))
654 print_sample_callindent(sample, evsel, thread, al);
655
Akihiro Nagai95582592012-01-30 13:43:09 +0900656 /* print branch_from information */
657 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300658 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700659 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300660
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300661 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700662 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300663 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700664 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300665
666 if (cursor == NULL) {
667 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300668 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300669 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300670 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300671 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300672 } else
673 putchar('\n');
674
675 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900676 }
677
Akihiro Nagai95582592012-01-30 13:43:09 +0900678 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300679 if (PRINT_FIELD(ADDR) ||
680 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300681 !output[attr->type].user_set)) {
682 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300683 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300684 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900685
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300686 if (print_srcline_last)
687 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
688
Andi Kleen224e2c92016-10-07 16:42:27 +0300689 print_insn(sample, attr);
690
Akihiro Nagai95582592012-01-30 13:43:09 +0900691 printf("\n");
692}
693
Adrian Hunter055cd332016-06-23 16:40:56 +0300694static struct {
695 u32 flags;
696 const char *name;
697} sample_flags[] = {
698 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
699 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
700 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
701 {PERF_IP_FLAG_BRANCH, "jmp"},
702 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
703 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
704 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
705 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
706 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
707 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
708 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
709 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
710 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
711 {0, NULL}
712};
713
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300714static void print_sample_flags(u32 flags)
715{
716 const char *chars = PERF_IP_FLAG_CHARS;
717 const int n = strlen(PERF_IP_FLAG_CHARS);
Adrian Hunter055cd332016-06-23 16:40:56 +0300718 bool in_tx = flags & PERF_IP_FLAG_IN_TX;
719 const char *name = NULL;
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300720 char str[33];
721 int i, pos = 0;
722
Adrian Hunter055cd332016-06-23 16:40:56 +0300723 for (i = 0; sample_flags[i].name ; i++) {
724 if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
725 name = sample_flags[i].name;
726 break;
727 }
728 }
729
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300730 for (i = 0; i < n; i++, flags >>= 1) {
731 if (flags & 1)
732 str[pos++] = chars[i];
733 }
734 for (; i < 32; i++, flags >>= 1) {
735 if (flags & 1)
736 str[pos++] = '?';
737 }
738 str[pos] = 0;
Adrian Hunter055cd332016-06-23 16:40:56 +0300739
740 if (name)
741 printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
742 else
743 printf(" %-11s ", str);
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300744}
745
Wang Nan30372f02016-02-24 11:20:45 +0000746struct printer_data {
747 int line_no;
748 bool hit_nul;
749 bool is_printable;
750};
751
752static void
753print_sample_bpf_output_printer(enum binary_printer_ops op,
754 unsigned int val,
755 void *extra)
756{
757 unsigned char ch = (unsigned char)val;
758 struct printer_data *printer_data = extra;
759
760 switch (op) {
761 case BINARY_PRINT_DATA_BEGIN:
762 printf("\n");
763 break;
764 case BINARY_PRINT_LINE_BEGIN:
765 printf("%17s", !printer_data->line_no ? "BPF output:" :
766 " ");
767 break;
768 case BINARY_PRINT_ADDR:
769 printf(" %04x:", val);
770 break;
771 case BINARY_PRINT_NUM_DATA:
772 printf(" %02x", val);
773 break;
774 case BINARY_PRINT_NUM_PAD:
775 printf(" ");
776 break;
777 case BINARY_PRINT_SEP:
778 printf(" ");
779 break;
780 case BINARY_PRINT_CHAR_DATA:
781 if (printer_data->hit_nul && ch)
782 printer_data->is_printable = false;
783
784 if (!isprint(ch)) {
785 printf("%c", '.');
786
787 if (!printer_data->is_printable)
788 break;
789
790 if (ch == '\0')
791 printer_data->hit_nul = true;
792 else
793 printer_data->is_printable = false;
794 } else {
795 printf("%c", ch);
796 }
797 break;
798 case BINARY_PRINT_CHAR_PAD:
799 printf(" ");
800 break;
801 case BINARY_PRINT_LINE_END:
802 printf("\n");
803 printer_data->line_no++;
804 break;
805 case BINARY_PRINT_DATA_END:
806 default:
807 break;
808 }
809}
810
811static void print_sample_bpf_output(struct perf_sample *sample)
812{
813 unsigned int nr_bytes = sample->raw_size;
814 struct printer_data printer_data = {0, false, true};
815
816 print_binary(sample->raw_data, nr_bytes, 8,
817 print_sample_bpf_output_printer, &printer_data);
818
819 if (printer_data.is_printable && printer_data.hit_nul)
820 printf("%17s \"%s\"\n", "BPF string:",
821 (char *)(sample->raw_data));
822}
823
Jiri Olsa809e9422015-11-26 18:55:21 +0100824struct perf_script {
825 struct perf_tool tool;
826 struct perf_session *session;
827 bool show_task_events;
828 bool show_mmap_events;
829 bool show_switch_events;
Jiri Olsacfc88742016-01-05 22:09:06 +0100830 bool allocated;
831 struct cpu_map *cpus;
832 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100833 int name_width;
Jiri Olsa809e9422015-11-26 18:55:21 +0100834};
835
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100836static int perf_evlist__max_name_len(struct perf_evlist *evlist)
837{
838 struct perf_evsel *evsel;
839 int max = 0;
840
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300841 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100842 int len = strlen(perf_evsel__name(evsel));
843
844 max = MAX(len, max);
845 }
846
847 return max;
848}
849
Jiri Olsac19ac912016-02-24 09:46:54 +0100850static size_t data_src__printf(u64 data_src)
851{
852 struct mem_info mi = { .data_src.val = data_src };
853 char decode[100];
854 char out[100];
855 static int maxlen;
856 int len;
857
858 perf_script__meminfo_scnprintf(decode, 100, &mi);
859
860 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
861 if (maxlen < len)
862 maxlen = len;
863
864 return printf("%-*s", maxlen, out);
865}
866
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300867static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +0100868 struct perf_sample *sample, struct perf_evsel *evsel,
869 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700870{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300871 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300872 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700873
David Ahern2c9e45f72011-03-17 10:03:21 -0600874 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700875 return;
876
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300877 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700878
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200879 if (PRINT_FIELD(PERIOD))
880 printf("%10" PRIu64 " ", sample->period);
881
Namhyung Kime944d3d2013-11-18 14:34:52 +0900882 if (PRINT_FIELD(EVNAME)) {
883 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100884
885 if (!script->name_width)
886 script->name_width = perf_evlist__max_name_len(script->session->evlist);
887
888 printf("%*s: ", script->name_width,
889 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +0900890 }
891
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300892 if (print_flags)
893 print_sample_flags(sample->flags);
894
Akihiro Nagai95582592012-01-30 13:43:09 +0900895 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300896 print_sample_bts(sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900897 return;
898 }
899
David Ahern745f43e2011-03-09 22:23:26 -0700900 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300901 event_format__print(evsel->tp_format, sample->cpu,
902 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600903 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300904 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600905
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100906 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +0100907 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100908
909 if (PRINT_FIELD(WEIGHT))
910 printf("%16" PRIu64, sample->weight);
911
David Ahern787bef12011-05-27 14:28:43 -0600912 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -0700913 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -0400914
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -0300915 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700916 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300917 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700918 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300919
920 putchar(cursor ? '\n' : ' ');
921 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -0700922 }
923
Stephane Eranianfc36f942015-08-31 18:41:10 +0200924 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300925 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200926
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200927 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300928 print_sample_brstack(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200929 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300930 print_sample_brstacksym(sample, thread);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200931
Wang Nan30372f02016-02-24 11:20:45 +0000932 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
933 print_sample_bpf_output(sample);
Andi Kleen224e2c92016-10-07 16:42:27 +0300934 print_insn(sample, attr);
David Ahernc70c94b2011-03-09 22:23:25 -0700935 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700936}
937
Tom Zanussi956ffd02009-11-25 01:15:46 -0600938static struct scripting_ops *scripting_ops;
939
Jiri Olsa36e33c52016-01-06 11:49:56 +0100940static void __process_stat(struct perf_evsel *counter, u64 tstamp)
941{
942 int nthreads = thread_map__nr(counter->threads);
943 int ncpus = perf_evsel__nr_cpus(counter);
944 int cpu, thread;
945 static int header_printed;
946
947 if (counter->system_wide)
948 nthreads = 1;
949
950 if (!header_printed) {
951 printf("%3s %8s %15s %15s %15s %15s %s\n",
952 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
953 header_printed = 1;
954 }
955
956 for (thread = 0; thread < nthreads; thread++) {
957 for (cpu = 0; cpu < ncpus; cpu++) {
958 struct perf_counts_values *counts;
959
960 counts = perf_counts(counter->counts, cpu, thread);
961
962 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
963 counter->cpus->map[cpu],
964 thread_map__pid(counter->threads, thread),
965 counts->val,
966 counts->ena,
967 counts->run,
968 tstamp,
969 perf_evsel__name(counter));
970 }
971 }
972}
973
Jiri Olsae099eba2016-01-05 22:09:09 +0100974static void process_stat(struct perf_evsel *counter, u64 tstamp)
975{
976 if (scripting_ops && scripting_ops->process_stat)
977 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +0100978 else
979 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +0100980}
981
982static void process_stat_interval(u64 tstamp)
983{
984 if (scripting_ops && scripting_ops->process_stat_interval)
985 scripting_ops->process_stat_interval(tstamp);
986}
987
Tom Zanussi956ffd02009-11-25 01:15:46 -0600988static void setup_scripting(void)
989{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600990 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600991 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600992}
993
Adrian Hunterd445dd22014-08-15 22:08:37 +0300994static int flush_scripting(void)
995{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100996 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300997}
998
Tom Zanussi956ffd02009-11-25 01:15:46 -0600999static int cleanup_scripting(void)
1000{
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001001 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -05001002
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001003 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001004}
1005
Jiri Olsa809e9422015-11-26 18:55:21 +01001006static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02001007 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02001008 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -03001009 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02001010 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001011{
Jiri Olsa809e9422015-11-26 18:55:21 +01001012 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -07001013 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001014
David Ahern1424dc92011-03-09 22:23:28 -07001015 if (debug_mode) {
1016 if (sample->time < last_timestamp) {
1017 pr_err("Samples misordered, previous: %" PRIu64
1018 " this: %" PRIu64 "\n", last_timestamp,
1019 sample->time);
1020 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +02001021 }
David Ahern1424dc92011-03-09 22:23:28 -07001022 last_timestamp = sample->time;
1023 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001024 }
Anton Blanchard5d67be92011-07-04 21:57:50 +10001025
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -03001026 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -07001027 pr_err("problem processing %d event, skipping it.\n",
1028 event->header.type);
1029 return -1;
1030 }
1031
1032 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001033 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -07001034
Anton Blanchard5d67be92011-07-04 21:57:50 +10001035 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001036 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001037
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001038 if (scripting_ops)
1039 scripting_ops->process_event(event, sample, evsel, &al);
1040 else
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -03001041 process_event(scr, sample, evsel, &al);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +01001042
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001043out_put:
1044 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001045 return 0;
1046}
1047
Adrian Hunter7ea95722013-11-01 15:51:30 +02001048static int process_attr(struct perf_tool *tool, union perf_event *event,
1049 struct perf_evlist **pevlist)
1050{
1051 struct perf_script *scr = container_of(tool, struct perf_script, tool);
1052 struct perf_evlist *evlist;
1053 struct perf_evsel *evsel, *pos;
1054 int err;
1055
1056 err = perf_event__process_attr(tool, event, pevlist);
1057 if (err)
1058 return err;
1059
1060 evlist = *pevlist;
1061 evsel = perf_evlist__last(*pevlist);
1062
1063 if (evsel->attr.type >= PERF_TYPE_MAX)
1064 return 0;
1065
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001066 evlist__for_each_entry(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +02001067 if (pos->attr.type == evsel->attr.type && pos != evsel)
1068 return 0;
1069 }
1070
1071 set_print_ip_opts(&evsel->attr);
1072
Jiri Olsad2b5a312015-10-16 12:41:25 +02001073 if (evsel->attr.sample_type)
1074 err = perf_evsel__check_attr(evsel, scr->session);
1075
1076 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +02001077}
1078
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001079static int process_comm_event(struct perf_tool *tool,
1080 union perf_event *event,
1081 struct perf_sample *sample,
1082 struct machine *machine)
1083{
1084 struct thread *thread;
1085 struct perf_script *script = container_of(tool, struct perf_script, tool);
1086 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001087 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001088 int ret = -1;
1089
1090 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
1091 if (thread == NULL) {
1092 pr_debug("problem processing COMM event, skipping it.\n");
1093 return -1;
1094 }
1095
1096 if (perf_event__process_comm(tool, event, sample, machine) < 0)
1097 goto out;
1098
1099 if (!evsel->attr.sample_id_all) {
1100 sample->cpu = 0;
1101 sample->time = 0;
1102 sample->tid = event->comm.tid;
1103 sample->pid = event->comm.pid;
1104 }
1105 print_sample_start(sample, thread, evsel);
1106 perf_event__fprintf(event, stdout);
1107 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001108out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001109 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001110 return ret;
1111}
1112
1113static int process_fork_event(struct perf_tool *tool,
1114 union perf_event *event,
1115 struct perf_sample *sample,
1116 struct machine *machine)
1117{
1118 struct thread *thread;
1119 struct perf_script *script = container_of(tool, struct perf_script, tool);
1120 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001121 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001122
1123 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1124 return -1;
1125
1126 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1127 if (thread == NULL) {
1128 pr_debug("problem processing FORK event, skipping it.\n");
1129 return -1;
1130 }
1131
1132 if (!evsel->attr.sample_id_all) {
1133 sample->cpu = 0;
1134 sample->time = event->fork.time;
1135 sample->tid = event->fork.tid;
1136 sample->pid = event->fork.pid;
1137 }
1138 print_sample_start(sample, thread, evsel);
1139 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001140 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001141
1142 return 0;
1143}
1144static int process_exit_event(struct perf_tool *tool,
1145 union perf_event *event,
1146 struct perf_sample *sample,
1147 struct machine *machine)
1148{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001149 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001150 struct thread *thread;
1151 struct perf_script *script = container_of(tool, struct perf_script, tool);
1152 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001153 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001154
1155 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1156 if (thread == NULL) {
1157 pr_debug("problem processing EXIT event, skipping it.\n");
1158 return -1;
1159 }
1160
1161 if (!evsel->attr.sample_id_all) {
1162 sample->cpu = 0;
1163 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001164 sample->tid = event->fork.tid;
1165 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001166 }
1167 print_sample_start(sample, thread, evsel);
1168 perf_event__fprintf(event, stdout);
1169
1170 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001171 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001172
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001173 thread__put(thread);
1174 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001175}
1176
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001177static int process_mmap_event(struct perf_tool *tool,
1178 union perf_event *event,
1179 struct perf_sample *sample,
1180 struct machine *machine)
1181{
1182 struct thread *thread;
1183 struct perf_script *script = container_of(tool, struct perf_script, tool);
1184 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001185 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001186
1187 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1188 return -1;
1189
1190 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1191 if (thread == NULL) {
1192 pr_debug("problem processing MMAP event, skipping it.\n");
1193 return -1;
1194 }
1195
1196 if (!evsel->attr.sample_id_all) {
1197 sample->cpu = 0;
1198 sample->time = 0;
1199 sample->tid = event->mmap.tid;
1200 sample->pid = event->mmap.pid;
1201 }
1202 print_sample_start(sample, thread, evsel);
1203 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001204 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001205 return 0;
1206}
1207
1208static int process_mmap2_event(struct perf_tool *tool,
1209 union perf_event *event,
1210 struct perf_sample *sample,
1211 struct machine *machine)
1212{
1213 struct thread *thread;
1214 struct perf_script *script = container_of(tool, struct perf_script, tool);
1215 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001216 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001217
1218 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1219 return -1;
1220
1221 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1222 if (thread == NULL) {
1223 pr_debug("problem processing MMAP2 event, skipping it.\n");
1224 return -1;
1225 }
1226
1227 if (!evsel->attr.sample_id_all) {
1228 sample->cpu = 0;
1229 sample->time = 0;
1230 sample->tid = event->mmap2.tid;
1231 sample->pid = event->mmap2.pid;
1232 }
1233 print_sample_start(sample, thread, evsel);
1234 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001235 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001236 return 0;
1237}
1238
Adrian Hunter7c148982015-07-21 12:44:06 +03001239static int process_switch_event(struct perf_tool *tool,
1240 union perf_event *event,
1241 struct perf_sample *sample,
1242 struct machine *machine)
1243{
1244 struct thread *thread;
1245 struct perf_script *script = container_of(tool, struct perf_script, tool);
1246 struct perf_session *session = script->session;
1247 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1248
1249 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1250 return -1;
1251
1252 thread = machine__findnew_thread(machine, sample->pid,
1253 sample->tid);
1254 if (thread == NULL) {
1255 pr_debug("problem processing SWITCH event, skipping it.\n");
1256 return -1;
1257 }
1258
1259 print_sample_start(sample, thread, evsel);
1260 perf_event__fprintf(event, stdout);
1261 thread__put(thread);
1262 return 0;
1263}
1264
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001265static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001266{
1267 session_done = 1;
1268}
1269
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001270static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001271{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001272 int ret;
1273
Tom Zanussic239da32010-04-01 23:59:18 -05001274 signal(SIGINT, sig_handler);
1275
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001276 /* override event processing functions */
1277 if (script->show_task_events) {
1278 script->tool.comm = process_comm_event;
1279 script->tool.fork = process_fork_event;
1280 script->tool.exit = process_exit_event;
1281 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001282 if (script->show_mmap_events) {
1283 script->tool.mmap = process_mmap_event;
1284 script->tool.mmap2 = process_mmap2_event;
1285 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001286 if (script->show_switch_events)
1287 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001288
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001289 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001290
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001291 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001292 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001293
1294 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001295}
1296
Tom Zanussi956ffd02009-11-25 01:15:46 -06001297struct script_spec {
1298 struct list_head node;
1299 struct scripting_ops *ops;
1300 char spec[0];
1301};
1302
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001303static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001304
1305static struct script_spec *script_spec__new(const char *spec,
1306 struct scripting_ops *ops)
1307{
1308 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1309
1310 if (s != NULL) {
1311 strcpy(s->spec, spec);
1312 s->ops = ops;
1313 }
1314
1315 return s;
1316}
1317
Tom Zanussi956ffd02009-11-25 01:15:46 -06001318static void script_spec__add(struct script_spec *s)
1319{
1320 list_add_tail(&s->node, &script_specs);
1321}
1322
1323static struct script_spec *script_spec__find(const char *spec)
1324{
1325 struct script_spec *s;
1326
1327 list_for_each_entry(s, &script_specs, node)
1328 if (strcasecmp(s->spec, spec) == 0)
1329 return s;
1330 return NULL;
1331}
1332
Tom Zanussi956ffd02009-11-25 01:15:46 -06001333int script_spec_register(const char *spec, struct scripting_ops *ops)
1334{
1335 struct script_spec *s;
1336
1337 s = script_spec__find(spec);
1338 if (s)
1339 return -1;
1340
Taeung Song8560bae2016-02-26 00:13:10 +09001341 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001342 if (!s)
1343 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001344 else
1345 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001346
1347 return 0;
1348}
1349
1350static struct scripting_ops *script_spec__lookup(const char *spec)
1351{
1352 struct script_spec *s = script_spec__find(spec);
1353 if (!s)
1354 return NULL;
1355
1356 return s->ops;
1357}
1358
1359static void list_available_languages(void)
1360{
1361 struct script_spec *s;
1362
1363 fprintf(stderr, "\n");
1364 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001365 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001366
1367 list_for_each_entry(s, &script_specs, node)
1368 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1369
1370 fprintf(stderr, "\n");
1371}
1372
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001373static int parse_scriptname(const struct option *opt __maybe_unused,
1374 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001375{
1376 char spec[PATH_MAX];
1377 const char *script, *ext;
1378 int len;
1379
Tom Zanussif526d682010-01-27 02:27:52 -06001380 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001381 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001382 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001383 }
1384
1385 script = strchr(str, ':');
1386 if (script) {
1387 len = script - str;
1388 if (len >= PATH_MAX) {
1389 fprintf(stderr, "invalid language specifier");
1390 return -1;
1391 }
1392 strncpy(spec, str, len);
1393 spec[len] = '\0';
1394 scripting_ops = script_spec__lookup(spec);
1395 if (!scripting_ops) {
1396 fprintf(stderr, "invalid language specifier");
1397 return -1;
1398 }
1399 script++;
1400 } else {
1401 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001402 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001403 if (!ext) {
1404 fprintf(stderr, "invalid script extension");
1405 return -1;
1406 }
1407 scripting_ops = script_spec__lookup(++ext);
1408 if (!scripting_ops) {
1409 fprintf(stderr, "invalid script extension");
1410 return -1;
1411 }
1412 }
1413
1414 script_name = strdup(script);
1415
1416 return 0;
1417}
1418
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001419static int parse_output_fields(const struct option *opt __maybe_unused,
1420 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001421{
1422 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001423 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001424 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001425 int rc = 0;
1426 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001427 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001428
1429 if (!str)
1430 return -ENOMEM;
1431
David Ahern2c9e45f72011-03-17 10:03:21 -06001432 /* first word can state for which event type the user is specifying
1433 * the fields. If no type exists, the specified fields apply to all
1434 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001435 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001436 tok = strchr(str, ':');
1437 if (tok) {
1438 *tok = '\0';
1439 tok++;
1440 if (!strcmp(str, "hw"))
1441 type = PERF_TYPE_HARDWARE;
1442 else if (!strcmp(str, "sw"))
1443 type = PERF_TYPE_SOFTWARE;
1444 else if (!strcmp(str, "trace"))
1445 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001446 else if (!strcmp(str, "raw"))
1447 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001448 else if (!strcmp(str, "break"))
1449 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001450 else {
1451 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001452 rc = -EINVAL;
1453 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001454 }
1455
1456 if (output[type].user_set)
1457 pr_warning("Overriding previous field request for %s events.\n",
1458 event_type(type));
1459
1460 output[type].fields = 0;
1461 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001462 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001463
1464 } else {
1465 tok = str;
1466 if (strlen(str) == 0) {
1467 fprintf(stderr,
1468 "Cannot set fields to 'none' for all event types.\n");
1469 rc = -EINVAL;
1470 goto out;
1471 }
1472
1473 if (output_set_by_user())
1474 pr_warning("Overriding previous field request for all events.\n");
1475
1476 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1477 output[j].fields = 0;
1478 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001479 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001480 }
David Ahern1424dc92011-03-09 22:23:28 -07001481 }
1482
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001483 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001484 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001485 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001486 break;
David Ahern745f43e2011-03-09 22:23:26 -07001487 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001488 if (i == imax && strcmp(tok, "flags") == 0) {
1489 print_flags = true;
1490 continue;
1491 }
David Ahern745f43e2011-03-09 22:23:26 -07001492 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001493 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001494 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001495 goto out;
1496 }
1497
1498 if (type == -1) {
1499 /* add user option to all events types for
1500 * which it is valid
1501 */
1502 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1503 if (output[j].invalid_fields & all_output_options[i].field) {
1504 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1505 all_output_options[i].str, event_type(j));
1506 } else
1507 output[j].fields |= all_output_options[i].field;
1508 }
1509 } else {
1510 if (output[type].invalid_fields & all_output_options[i].field) {
1511 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1512 all_output_options[i].str, event_type(type));
1513
1514 rc = -EINVAL;
1515 goto out;
1516 }
1517 output[type].fields |= all_output_options[i].field;
1518 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001519 }
1520
1521 if (type >= 0) {
1522 if (output[type].fields == 0) {
1523 pr_debug("No fields requested for %s type. "
1524 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001525 }
David Ahern1424dc92011-03-09 22:23:28 -07001526 }
David Ahern745f43e2011-03-09 22:23:26 -07001527
David Ahern2c9e45f72011-03-17 10:03:21 -06001528out:
David Ahern745f43e2011-03-09 22:23:26 -07001529 free(str);
1530 return rc;
1531}
1532
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001533/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1534static int is_directory(const char *base_path, const struct dirent *dent)
1535{
1536 char path[PATH_MAX];
1537 struct stat st;
1538
1539 sprintf(path, "%s/%s", base_path, dent->d_name);
1540 if (stat(path, &st))
1541 return 0;
1542
1543 return S_ISDIR(st.st_mode);
1544}
1545
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001546#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1547 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1548 if ((lang_dirent->d_type == DT_DIR || \
1549 (lang_dirent->d_type == DT_UNKNOWN && \
1550 is_directory(scripts_path, lang_dirent))) && \
1551 (strcmp(lang_dirent->d_name, ".")) && \
1552 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001553
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001554#define for_each_script(lang_path, lang_dir, script_dirent) \
1555 while ((script_dirent = readdir(lang_dir)) != NULL) \
1556 if (script_dirent->d_type != DT_DIR && \
1557 (script_dirent->d_type != DT_UNKNOWN || \
1558 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001559
1560
1561#define RECORD_SUFFIX "-record"
1562#define REPORT_SUFFIX "-report"
1563
1564struct script_desc {
1565 struct list_head node;
1566 char *name;
1567 char *half_liner;
1568 char *args;
1569};
1570
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001571static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001572
1573static struct script_desc *script_desc__new(const char *name)
1574{
1575 struct script_desc *s = zalloc(sizeof(*s));
1576
Tom Zanussib5b87312010-11-10 08:16:51 -06001577 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001578 s->name = strdup(name);
1579
1580 return s;
1581}
1582
1583static void script_desc__delete(struct script_desc *s)
1584{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001585 zfree(&s->name);
1586 zfree(&s->half_liner);
1587 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001588 free(s);
1589}
1590
1591static void script_desc__add(struct script_desc *s)
1592{
1593 list_add_tail(&s->node, &script_descs);
1594}
1595
1596static struct script_desc *script_desc__find(const char *name)
1597{
1598 struct script_desc *s;
1599
1600 list_for_each_entry(s, &script_descs, node)
1601 if (strcasecmp(s->name, name) == 0)
1602 return s;
1603 return NULL;
1604}
1605
1606static struct script_desc *script_desc__findnew(const char *name)
1607{
1608 struct script_desc *s = script_desc__find(name);
1609
1610 if (s)
1611 return s;
1612
1613 s = script_desc__new(name);
1614 if (!s)
1615 goto out_delete_desc;
1616
1617 script_desc__add(s);
1618
1619 return s;
1620
1621out_delete_desc:
1622 script_desc__delete(s);
1623
1624 return NULL;
1625}
1626
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001627static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001628{
1629 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001630 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001631
1632 if (strlen(str) > suffix_len) {
1633 p = str + strlen(str) - suffix_len;
1634 if (!strncmp(p, suffix, suffix_len))
1635 return p;
1636 }
1637
1638 return NULL;
1639}
1640
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001641static int read_script_info(struct script_desc *desc, const char *filename)
1642{
1643 char line[BUFSIZ], *p;
1644 FILE *fp;
1645
1646 fp = fopen(filename, "r");
1647 if (!fp)
1648 return -1;
1649
1650 while (fgets(line, sizeof(line), fp)) {
1651 p = ltrim(line);
1652 if (strlen(p) == 0)
1653 continue;
1654 if (*p != '#')
1655 continue;
1656 p++;
1657 if (strlen(p) && *p == '!')
1658 continue;
1659
1660 p = ltrim(p);
1661 if (strlen(p) && p[strlen(p) - 1] == '\n')
1662 p[strlen(p) - 1] = '\0';
1663
1664 if (!strncmp(p, "description:", strlen("description:"))) {
1665 p += strlen("description:");
1666 desc->half_liner = strdup(ltrim(p));
1667 continue;
1668 }
1669
1670 if (!strncmp(p, "args:", strlen("args:"))) {
1671 p += strlen("args:");
1672 desc->args = strdup(ltrim(p));
1673 continue;
1674 }
1675 }
1676
1677 fclose(fp);
1678
1679 return 0;
1680}
1681
Robert Richter38efb532011-11-25 11:38:40 +01001682static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1683{
1684 char *script_root, *str;
1685
1686 script_root = strdup(script_dirent->d_name);
1687 if (!script_root)
1688 return NULL;
1689
1690 str = (char *)ends_with(script_root, suffix);
1691 if (!str) {
1692 free(script_root);
1693 return NULL;
1694 }
1695
1696 *str = '\0';
1697 return script_root;
1698}
1699
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001700static int list_available_scripts(const struct option *opt __maybe_unused,
1701 const char *s __maybe_unused,
1702 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001703{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001704 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001705 char scripts_path[MAXPATHLEN];
1706 DIR *scripts_dir, *lang_dir;
1707 char script_path[MAXPATHLEN];
1708 char lang_path[MAXPATHLEN];
1709 struct script_desc *desc;
1710 char first_half[BUFSIZ];
1711 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001712
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001713 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001714
1715 scripts_dir = opendir(scripts_path);
He Kuang88ded4d2016-08-04 11:25:42 +00001716 if (!scripts_dir) {
1717 fprintf(stdout,
1718 "open(%s) failed.\n"
1719 "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
1720 scripts_path);
1721 exit(-1);
1722 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001723
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001724 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001725 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001726 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001727 lang_dir = opendir(lang_path);
1728 if (!lang_dir)
1729 continue;
1730
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001731 for_each_script(lang_path, lang_dir, script_dirent) {
1732 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01001733 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001734 desc = script_desc__findnew(script_root);
1735 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001736 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001737 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001738 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001739 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001740 }
1741 }
1742
1743 fprintf(stdout, "List of available trace scripts:\n");
1744 list_for_each_entry(desc, &script_descs, node) {
1745 sprintf(first_half, "%s %s", desc->name,
1746 desc->args ? desc->args : "");
1747 fprintf(stdout, " %-36s %s\n", first_half,
1748 desc->half_liner ? desc->half_liner : "");
1749 }
1750
1751 exit(0);
1752}
1753
Feng Tange5f37052012-09-07 16:42:26 +08001754/*
Feng Tang49e639e2012-10-30 11:56:03 +08001755 * Some scripts specify the required events in their "xxx-record" file,
1756 * this function will check if the events in perf.data match those
1757 * mentioned in the "xxx-record".
1758 *
1759 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1760 * which is covered well now. And new parsing code should be added to
1761 * cover the future complexing formats like event groups etc.
1762 */
1763static int check_ev_match(char *dir_name, char *scriptname,
1764 struct perf_session *session)
1765{
1766 char filename[MAXPATHLEN], evname[128];
1767 char line[BUFSIZ], *p;
1768 struct perf_evsel *pos;
1769 int match, len;
1770 FILE *fp;
1771
1772 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1773
1774 fp = fopen(filename, "r");
1775 if (!fp)
1776 return -1;
1777
1778 while (fgets(line, sizeof(line), fp)) {
1779 p = ltrim(line);
1780 if (*p == '#')
1781 continue;
1782
1783 while (strlen(p)) {
1784 p = strstr(p, "-e");
1785 if (!p)
1786 break;
1787
1788 p += 2;
1789 p = ltrim(p);
1790 len = strcspn(p, " \t");
1791 if (!len)
1792 break;
1793
1794 snprintf(evname, len + 1, "%s", p);
1795
1796 match = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001797 evlist__for_each_entry(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001798 if (!strcmp(perf_evsel__name(pos), evname)) {
1799 match = 1;
1800 break;
1801 }
1802 }
1803
1804 if (!match) {
1805 fclose(fp);
1806 return -1;
1807 }
1808 }
1809 }
1810
1811 fclose(fp);
1812 return 0;
1813}
1814
1815/*
Feng Tange5f37052012-09-07 16:42:26 +08001816 * Return -1 if none is found, otherwise the actual scripts number.
1817 *
1818 * Currently the only user of this function is the script browser, which
1819 * will list all statically runnable scripts, select one, execute it and
1820 * show the output in a perf browser.
1821 */
1822int find_scripts(char **scripts_array, char **scripts_path_array)
1823{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001824 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001825 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001826 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001827 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001828 struct perf_data_file file = {
1829 .path = input_name,
1830 .mode = PERF_DATA_MODE_READ,
1831 };
Feng Tange5f37052012-09-07 16:42:26 +08001832 char *temp;
1833 int i = 0;
1834
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001835 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001836 if (!session)
1837 return -1;
1838
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001839 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08001840
1841 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001842 if (!scripts_dir) {
1843 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001844 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001845 }
Feng Tange5f37052012-09-07 16:42:26 +08001846
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001847 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08001848 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001849 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08001850#ifdef NO_LIBPERL
1851 if (strstr(lang_path, "perl"))
1852 continue;
1853#endif
1854#ifdef NO_LIBPYTHON
1855 if (strstr(lang_path, "python"))
1856 continue;
1857#endif
1858
1859 lang_dir = opendir(lang_path);
1860 if (!lang_dir)
1861 continue;
1862
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001863 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08001864 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001865 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08001866 continue;
1867 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001868 script_dirent->d_name);
1869 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08001870 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001871 (temp - script_dirent->d_name) + 1,
1872 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001873
1874 if (check_ev_match(lang_path,
1875 scripts_array[i], session))
1876 continue;
1877
Feng Tange5f37052012-09-07 16:42:26 +08001878 i++;
1879 }
Feng Tang49e639e2012-10-30 11:56:03 +08001880 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001881 }
1882
Feng Tang49e639e2012-10-30 11:56:03 +08001883 closedir(scripts_dir);
1884 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001885 return i;
1886}
1887
Tom Zanussi38752942009-12-15 02:53:39 -06001888static char *get_script_path(const char *script_root, const char *suffix)
1889{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001890 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06001891 char scripts_path[MAXPATHLEN];
1892 char script_path[MAXPATHLEN];
1893 DIR *scripts_dir, *lang_dir;
1894 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001895 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001896
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001897 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06001898
1899 scripts_dir = opendir(scripts_path);
1900 if (!scripts_dir)
1901 return NULL;
1902
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001903 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06001904 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001905 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06001906 lang_dir = opendir(lang_path);
1907 if (!lang_dir)
1908 continue;
1909
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001910 for_each_script(lang_path, lang_dir, script_dirent) {
1911 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01001912 if (__script_root && !strcmp(script_root, __script_root)) {
1913 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001914 closedir(lang_dir);
1915 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001916 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001917 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001918 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001919 }
1920 free(__script_root);
1921 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001922 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001923 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001924 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001925
Robert Richter38efb532011-11-25 11:38:40 +01001926 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001927}
1928
Tom Zanussib5b87312010-11-10 08:16:51 -06001929static bool is_top_script(const char *script_path)
1930{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001931 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001932}
1933
1934static int has_required_arg(char *script_path)
1935{
1936 struct script_desc *desc;
1937 int n_args = 0;
1938 char *p;
1939
1940 desc = script_desc__new(NULL);
1941
1942 if (read_script_info(desc, script_path))
1943 goto out;
1944
1945 if (!desc->args)
1946 goto out;
1947
1948 for (p = desc->args; *p; p++)
1949 if (*p == '<')
1950 n_args++;
1951out:
1952 script_desc__delete(desc);
1953
1954 return n_args;
1955}
1956
David Ahernd54b1a92012-08-26 12:24:46 -06001957static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001958{
1959 char **__argv = malloc(sizeof(const char *) * argc);
1960
David Ahernd54b1a92012-08-26 12:24:46 -06001961 if (!__argv) {
1962 pr_err("malloc failed\n");
1963 return -1;
1964 }
1965
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001966 memcpy(__argv, argv, sizeof(const char *) * argc);
1967 argc = parse_options(argc, (const char **)__argv, record_options,
1968 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1969 free(__argv);
1970
David Ahernd54b1a92012-08-26 12:24:46 -06001971 system_wide = (argc == 0);
1972
1973 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001974}
1975
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001976static void script__setup_sample_type(struct perf_script *script)
1977{
1978 struct perf_session *session = script->session;
1979 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1980
1981 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1982 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1983 (sample_type & PERF_SAMPLE_STACK_USER))
1984 callchain_param.record_mode = CALLCHAIN_DWARF;
1985 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1986 callchain_param.record_mode = CALLCHAIN_LBR;
1987 else
1988 callchain_param.record_mode = CALLCHAIN_FP;
1989 }
1990}
1991
Jiri Olsae099eba2016-01-05 22:09:09 +01001992static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1993 union perf_event *event,
1994 struct perf_session *session)
1995{
1996 struct stat_round_event *round = &event->stat_round;
1997 struct perf_evsel *counter;
1998
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001999 evlist__for_each_entry(session->evlist, counter) {
Jiri Olsae099eba2016-01-05 22:09:09 +01002000 perf_stat_process_counter(&stat_config, counter);
2001 process_stat(counter, round->time);
2002 }
2003
2004 process_stat_interval(round->time);
2005 return 0;
2006}
2007
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002008static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2009 union perf_event *event,
2010 struct perf_session *session __maybe_unused)
2011{
2012 perf_event__read_stat_config(&stat_config, &event->stat_config);
2013 return 0;
2014}
2015
Jiri Olsacfc88742016-01-05 22:09:06 +01002016static int set_maps(struct perf_script *script)
2017{
2018 struct perf_evlist *evlist = script->session->evlist;
2019
2020 if (!script->cpus || !script->threads)
2021 return 0;
2022
2023 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
2024 return -EINVAL;
2025
2026 perf_evlist__set_maps(evlist, script->cpus, script->threads);
2027
2028 if (perf_evlist__alloc_stats(evlist, true))
2029 return -ENOMEM;
2030
2031 script->allocated = true;
2032 return 0;
2033}
2034
2035static
2036int process_thread_map_event(struct perf_tool *tool,
2037 union perf_event *event,
2038 struct perf_session *session __maybe_unused)
2039{
2040 struct perf_script *script = container_of(tool, struct perf_script, tool);
2041
2042 if (script->threads) {
2043 pr_warning("Extra thread map event, ignoring.\n");
2044 return 0;
2045 }
2046
2047 script->threads = thread_map__new_event(&event->thread_map);
2048 if (!script->threads)
2049 return -ENOMEM;
2050
2051 return set_maps(script);
2052}
2053
2054static
2055int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2056 union perf_event *event,
2057 struct perf_session *session __maybe_unused)
2058{
2059 struct perf_script *script = container_of(tool, struct perf_script, tool);
2060
2061 if (script->cpus) {
2062 pr_warning("Extra cpu map event, ignoring.\n");
2063 return 0;
2064 }
2065
2066 script->cpus = cpu_map__new_data(&event->cpu_map.data);
2067 if (!script->cpus)
2068 return -ENOMEM;
2069
2070 return set_maps(script);
2071}
2072
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002073int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002074{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002075 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01002076 bool header = false;
2077 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002078 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06002079 char *rec_script_path = NULL;
2080 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002081 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002082 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06002083 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002084 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002085 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002086 struct perf_script script = {
2087 .tool = {
2088 .sample = process_sample_event,
2089 .mmap = perf_event__process_mmap,
2090 .mmap2 = perf_event__process_mmap2,
2091 .comm = perf_event__process_comm,
2092 .exit = perf_event__process_exit,
2093 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02002094 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02002095 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002096 .tracing_data = perf_event__process_tracing_data,
2097 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002098 .id_index = perf_event__process_id_index,
2099 .auxtrace_info = perf_event__process_auxtrace_info,
2100 .auxtrace = perf_event__process_auxtrace,
2101 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01002102 .stat = perf_event__process_stat_event,
2103 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01002104 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01002105 .thread_map = process_thread_map_event,
2106 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002107 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002108 .ordering_requires_timestamps = true,
2109 },
2110 };
Yunlong Song06af0f22015-04-02 21:47:16 +08002111 struct perf_data_file file = {
2112 .mode = PERF_DATA_MODE_READ,
2113 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002114 const struct option options[] = {
2115 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2116 "dump raw trace in ASCII"),
2117 OPT_INCR('v', "verbose", &verbose,
2118 "be more verbose (show symbol address, etc)"),
2119 OPT_BOOLEAN('L', "Latency", &latency_format,
2120 "show latency attributes (irqs/preemption disabled, etc)"),
2121 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
2122 list_available_scripts),
2123 OPT_CALLBACK('s', "script", NULL, "name",
2124 "script file name (lang:script name, script name, or *)",
2125 parse_scriptname),
2126 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2127 "generate perf-script.xx script in specified language"),
2128 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2129 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2130 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002131 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2132 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002133 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2134 "file", "vmlinux pathname"),
2135 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2136 "file", "kallsyms pathname"),
2137 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2138 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002139 OPT_CALLBACK(0, "symfs", NULL, "directory",
2140 "Look for files with symbols relative to this directory",
2141 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002142 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002143 "comma separated output fields prepend with 'type:'. "
2144 "Valid types: hw,sw,trace,raw. "
2145 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Huntere2167082016-06-23 16:40:58 +03002146 "addr,symoff,period,iregs,brstack,brstacksym,flags,"
Andi Kleen224e2c92016-10-07 16:42:27 +03002147 "bpf-output,callindent,insn,insnlen", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002148 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2149 "system-wide collection from all CPUs"),
2150 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2151 "only consider these symbols"),
2152 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2153 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2154 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002155 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2156 "only consider symbols in these pids"),
2157 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2158 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002159 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2160 "Set the maximum stack depth when parsing the callchain, "
2161 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002162 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002163 OPT_BOOLEAN('I', "show-info", &show_full_info,
2164 "display extended information from perf.data file"),
2165 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2166 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002167 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2168 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002169 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2170 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002171 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2172 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08002173 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002174 OPT_BOOLEAN(0, "ns", &nanosecs,
2175 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002176 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2177 "Instruction Tracing options",
2178 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002179 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2180 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002181 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2182 "Enable symbol demangling"),
2183 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2184 "Enable kernel symbol demangling"),
2185
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002186 OPT_END()
2187 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002188 const char * const script_subcommands[] = { "record", "report", NULL };
2189 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002190 "perf script [<options>]",
2191 "perf script [<options>] record <script> [<record-options>] <command>",
2192 "perf script [<options>] report <script> [script-args]",
2193 "perf script [<options>] <script> [<record-options>] <command>",
2194 "perf script [<options>] <top-script> [script-args]",
2195 NULL
2196 };
Tom Zanussi38752942009-12-15 02:53:39 -06002197
Tom Zanussib5b87312010-11-10 08:16:51 -06002198 setup_scripting();
2199
Yunlong Song40cae2b2015-03-18 21:35:54 +08002200 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002201 PARSE_OPT_STOP_AT_NON_OPTION);
2202
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002203 file.path = input_name;
2204
Tom Zanussib5b87312010-11-10 08:16:51 -06002205 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2206 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2207 if (!rec_script_path)
2208 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06002209 }
2210
Tom Zanussib5b87312010-11-10 08:16:51 -06002211 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2212 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2213 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002214 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002215 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002216 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002217 return -1;
2218 }
Tom Zanussi38752942009-12-15 02:53:39 -06002219 }
2220
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002221 if (itrace_synth_opts.callchain &&
2222 itrace_synth_opts.callchain_sz > scripting_max_stack)
2223 scripting_max_stack = itrace_synth_opts.callchain_sz;
2224
Ben Hutchings44e668c2010-10-10 16:10:03 +01002225 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002226 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002227
Tom Zanussib5b87312010-11-10 08:16:51 -06002228 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002229 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002230 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002231 pid_t pid;
2232
Tom Zanussib5b87312010-11-10 08:16:51 -06002233 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2234 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2235
2236 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002237 usage_with_options_msg(script_usage, options,
2238 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002239 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002240 }
2241
Tom Zanussib5b87312010-11-10 08:16:51 -06002242 if (is_top_script(argv[0])) {
2243 rep_args = argc - 1;
2244 } else {
2245 int rec_args;
2246
2247 rep_args = has_required_arg(rep_script_path);
2248 rec_args = (argc - 1) - rep_args;
2249 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002250 usage_with_options_msg(script_usage, options,
2251 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002252 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002253 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002254 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002255 }
2256
2257 if (pipe(live_pipe) < 0) {
2258 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002259 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002260 }
2261
2262 pid = fork();
2263 if (pid < 0) {
2264 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002265 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002266 }
2267
2268 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002269 j = 0;
2270
Tom Zanussia0cccc22010-04-01 23:59:25 -05002271 dup2(live_pipe[1], 1);
2272 close(live_pipe[0]);
2273
Robert Richter317df652011-11-25 15:05:25 +01002274 if (is_top_script(argv[0])) {
2275 system_wide = true;
2276 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002277 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2278 err = -1;
2279 goto out;
2280 }
Robert Richter317df652011-11-25 15:05:25 +01002281 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002282
2283 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002284 if (!__argv) {
2285 pr_err("malloc failed\n");
2286 err = -ENOMEM;
2287 goto out;
2288 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002289
Tom Zanussib5b87312010-11-10 08:16:51 -06002290 __argv[j++] = "/bin/sh";
2291 __argv[j++] = rec_script_path;
2292 if (system_wide)
2293 __argv[j++] = "-a";
2294 __argv[j++] = "-q";
2295 __argv[j++] = "-o";
2296 __argv[j++] = "-";
2297 for (i = rep_args + 1; i < argc; i++)
2298 __argv[j++] = argv[i];
2299 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002300
2301 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002302 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002303 exit(-1);
2304 }
2305
2306 dup2(live_pipe[0], 0);
2307 close(live_pipe[1]);
2308
Tom Zanussib5b87312010-11-10 08:16:51 -06002309 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002310 if (!__argv) {
2311 pr_err("malloc failed\n");
2312 err = -ENOMEM;
2313 goto out;
2314 }
2315
Tom Zanussib5b87312010-11-10 08:16:51 -06002316 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002317 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002318 __argv[j++] = rep_script_path;
2319 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002320 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002321 __argv[j++] = "-i";
2322 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002323 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002324
2325 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002326 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002327 exit(-1);
2328 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002329
Tom Zanussib5b87312010-11-10 08:16:51 -06002330 if (rec_script_path)
2331 script_path = rec_script_path;
2332 if (rep_script_path)
2333 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002334
Tom Zanussib5b87312010-11-10 08:16:51 -06002335 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002336 j = 0;
2337
Robert Richter317df652011-11-25 15:05:25 +01002338 if (!rec_script_path)
2339 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002340 else if (!system_wide) {
2341 if (have_cmd(argc - 1, &argv[1]) != 0) {
2342 err = -1;
2343 goto out;
2344 }
2345 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002346
2347 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002348 if (!__argv) {
2349 pr_err("malloc failed\n");
2350 err = -ENOMEM;
2351 goto out;
2352 }
2353
Tom Zanussib5b87312010-11-10 08:16:51 -06002354 __argv[j++] = "/bin/sh";
2355 __argv[j++] = script_path;
2356 if (system_wide)
2357 __argv[j++] = "-a";
2358 for (i = 2; i < argc; i++)
2359 __argv[j++] = argv[i];
2360 __argv[j++] = NULL;
2361
2362 execvp("/bin/sh", (char **)__argv);
2363 free(__argv);
2364 exit(-1);
2365 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002366
Tom Zanussicf4fee52010-03-03 01:04:33 -06002367 if (!script_name)
2368 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002369
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002370 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002371 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002372 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002373
Jiri Olsae90debd2013-12-09 11:02:50 +01002374 if (header || header_only) {
2375 perf_session__fprintf_info(session, stdout, show_full_info);
2376 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002377 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002378 }
2379
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002380 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002381 goto out_delete;
2382
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002383 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002384 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002385
Adrian Huntere2167082016-06-23 16:40:58 +03002386 if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
2387 itrace_synth_opts.thread_stack = true;
2388
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002389 session->itrace_synth_opts = &itrace_synth_opts;
2390
Anton Blanchard5d67be92011-07-04 21:57:50 +10002391 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002392 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2393 if (err < 0)
2394 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002395 }
2396
David Ahern1424dc92011-03-09 22:23:28 -07002397 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002398 symbol_conf.use_callchain = true;
2399 else
2400 symbol_conf.use_callchain = false;
2401
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002402 if (session->tevent.pevent &&
2403 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002404 machine__resolve_kernel_addr,
2405 &session->machines.host) < 0) {
2406 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2407 return -1;
2408 }
2409
Tom Zanussi956ffd02009-11-25 01:15:46 -06002410 if (generate_script_lang) {
2411 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002412 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002413
David Ahern2c9e45f72011-03-17 10:03:21 -06002414 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002415 fprintf(stderr,
2416 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002417 err = -EINVAL;
2418 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002419 }
2420
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002421 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002422 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002423 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002424 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002425 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002426 }
2427
2428 err = fstat(input, &perf_stat);
2429 if (err < 0) {
2430 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002431 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002432 }
2433
2434 if (!perf_stat.st_size) {
2435 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002436 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002437 }
2438
2439 scripting_ops = script_spec__lookup(generate_script_lang);
2440 if (!scripting_ops) {
2441 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002442 err = -ENOENT;
2443 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002444 }
2445
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002446 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002447 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002448 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002449 }
2450
2451 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002452 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002453 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002454 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002455 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002456 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002457 }
2458
David Ahern9cbdb702011-04-06 21:54:20 -06002459
2460 err = perf_session__check_output_opt(session);
2461 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002462 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002463
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002464 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002465
Adrian Hunterd445dd22014-08-15 22:08:37 +03002466 flush_scripting();
2467
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002468out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002469 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002470 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002471
2472 if (script_started)
2473 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002474out:
2475 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002476}