blob: e3ce2f34d3ad5276cc8f10d78b6590c41b56c179 [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 Olsaf5fc1412013-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"
Anton Blanchard5d67be92011-07-04 21:57:50 +100024#include <linux/bitmap.h>
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -030025#include <linux/stringify.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010026#include "asm/bug.h"
Jiri Olsac19ac912016-02-24 09:46:54 +010027#include "util/mem-events.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020028
Tom Zanussi956ffd02009-11-25 01:15:46 -060029static char const *script_name;
30static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020031static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020032static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020033static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070034static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090035static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010036static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030037static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030038static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100039static const char *cpu_list;
40static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010041static struct perf_stat_config stat_config;
Tom Zanussi956ffd02009-11-25 01:15:46 -060042
Adrian Hunter44cbe722015-09-25 16:15:50 +030043unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030044
David Ahern745f43e2011-03-09 22:23:26 -070045enum perf_output_field {
46 PERF_OUTPUT_COMM = 1U << 0,
47 PERF_OUTPUT_TID = 1U << 1,
48 PERF_OUTPUT_PID = 1U << 2,
49 PERF_OUTPUT_TIME = 1U << 3,
50 PERF_OUTPUT_CPU = 1U << 4,
51 PERF_OUTPUT_EVNAME = 1U << 5,
52 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060053 PERF_OUTPUT_IP = 1U << 7,
54 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060055 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060056 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090057 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020058 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaa2014-08-25 16:45:42 +020059 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020060 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020061 PERF_OUTPUT_BRSTACK = 1U << 15,
62 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010063 PERF_OUTPUT_DATA_SRC = 1U << 17,
64 PERF_OUTPUT_WEIGHT = 1U << 18,
Wang Nan30372f02016-02-24 11:20:45 +000065 PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
David Ahern745f43e2011-03-09 22:23:26 -070066};
67
68struct output_option {
69 const char *str;
70 enum perf_output_field field;
71} all_output_options[] = {
72 {.str = "comm", .field = PERF_OUTPUT_COMM},
73 {.str = "tid", .field = PERF_OUTPUT_TID},
74 {.str = "pid", .field = PERF_OUTPUT_PID},
75 {.str = "time", .field = PERF_OUTPUT_TIME},
76 {.str = "cpu", .field = PERF_OUTPUT_CPU},
77 {.str = "event", .field = PERF_OUTPUT_EVNAME},
78 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060079 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070080 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060081 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060082 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090083 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020084 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaa2014-08-25 16:45:42 +020085 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020086 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +020087 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
88 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +010089 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
90 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
Wang Nan30372f02016-02-24 11:20:45 +000091 {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
David Ahern745f43e2011-03-09 22:23:26 -070092};
93
94/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060095static struct {
96 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060097 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040098 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060099 u64 fields;
100 u64 invalid_fields;
101} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -0700102
David Ahern2c9e45f72011-03-17 10:03:21 -0600103 [PERF_TYPE_HARDWARE] = {
104 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700105
David Ahern2c9e45f72011-03-17 10:03:21 -0600106 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
107 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600108 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200109 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
110 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600111
Wang Nan30372f02016-02-24 11:20:45 +0000112 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600113 },
114
115 [PERF_TYPE_SOFTWARE] = {
116 .user_set = false,
117
118 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
119 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600120 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200121 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Wang Nan30372f02016-02-24 11:20:45 +0000122 PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
David Ahern2c9e45f72011-03-17 10:03:21 -0600123
124 .invalid_fields = PERF_OUTPUT_TRACE,
125 },
126
127 [PERF_TYPE_TRACEPOINT] = {
128 .user_set = false,
129
130 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
131 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
Wang Nan30372f02016-02-24 11:20:45 +0000132 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
David Ahern2c9e45f72011-03-17 10:03:21 -0600133 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700134
135 [PERF_TYPE_RAW] = {
136 .user_set = false,
137
138 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
139 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600140 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200141 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100142 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
143 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700144
Wang Nan30372f02016-02-24 11:20:45 +0000145 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700146 },
Wang Nan27cfef02015-12-08 02:25:43 +0000147
148 [PERF_TYPE_BREAKPOINT] = {
149 .user_set = false,
150
151 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
152 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
153 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
154 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
155 PERF_OUTPUT_PERIOD,
156
Wang Nan30372f02016-02-24 11:20:45 +0000157 .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
Wang Nan27cfef02015-12-08 02:25:43 +0000158 },
David Ahern1424dc92011-03-09 22:23:28 -0700159};
David Ahern745f43e2011-03-09 22:23:26 -0700160
David Ahern2c9e45f72011-03-17 10:03:21 -0600161static bool output_set_by_user(void)
162{
163 int j;
164 for (j = 0; j < PERF_TYPE_MAX; ++j) {
165 if (output[j].user_set)
166 return true;
167 }
168 return false;
169}
David Ahern745f43e2011-03-09 22:23:26 -0700170
David Ahern9cbdb702011-04-06 21:54:20 -0600171static const char *output_field2str(enum perf_output_field field)
172{
173 int i, imax = ARRAY_SIZE(all_output_options);
174 const char *str = "";
175
176 for (i = 0; i < imax; ++i) {
177 if (all_output_options[i].field == field) {
178 str = all_output_options[i].str;
179 break;
180 }
181 }
182 return str;
183}
184
David Ahern2c9e45f72011-03-17 10:03:21 -0600185#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700186
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300187static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
188 u64 sample_type, const char *sample_msg,
189 enum perf_output_field field,
190 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700191{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300192 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600193 int type = attr->type;
194 const char *evname;
195
196 if (attr->sample_type & sample_type)
197 return 0;
198
199 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300200 if (allow_user_set)
201 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300202 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600203 pr_err("Samples for '%s' event do not have %s attribute set. "
204 "Cannot print '%s' field.\n",
205 evname, sample_msg, output_field2str(field));
206 return -1;
207 }
208
209 /* user did not ask for it explicitly so remove from the default list */
210 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300211 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600212 pr_debug("Samples for '%s' event do not have %s attribute set. "
213 "Skipping '%s' field.\n",
214 evname, sample_msg, output_field2str(field));
215
216 return 0;
217}
218
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300219static int perf_evsel__check_stype(struct perf_evsel *evsel,
220 u64 sample_type, const char *sample_msg,
221 enum perf_output_field field)
222{
223 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
224 false);
225}
226
David Ahern9cbdb702011-04-06 21:54:20 -0600227static int perf_evsel__check_attr(struct perf_evsel *evsel,
228 struct perf_session *session)
229{
230 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300231 bool allow_user_set;
232
Jiri Olsae099eba2016-01-05 22:09:09 +0100233 if (perf_header__has_feat(&session->header, HEADER_STAT))
234 return 0;
235
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300236 allow_user_set = perf_header__has_feat(&session->header,
237 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600238
David Ahern1424dc92011-03-09 22:23:28 -0700239 if (PRINT_FIELD(TRACE) &&
240 !perf_session__has_traces(session, "record -R"))
241 return -EINVAL;
242
David Ahern787bef12011-05-27 14:28:43 -0600243 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300244 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
245 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700246 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700247 }
David Ahern7cec0922011-05-30 13:08:23 -0600248
249 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300250 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
251 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600252 return -EINVAL;
253
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100254 if (PRINT_FIELD(DATA_SRC) &&
255 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
256 PERF_OUTPUT_DATA_SRC))
257 return -EINVAL;
258
259 if (PRINT_FIELD(WEIGHT) &&
260 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
261 PERF_OUTPUT_WEIGHT))
262 return -EINVAL;
263
David Ahern7cec0922011-05-30 13:08:23 -0600264 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
265 pr_err("Display of symbols requested but neither sample IP nor "
266 "sample address\nis selected. Hence, no addresses to convert "
267 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600268 return -EINVAL;
269 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900270 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
271 pr_err("Display of offsets requested but symbol is not"
272 "selected.\n");
273 return -EINVAL;
274 }
David Ahern7cec0922011-05-30 13:08:23 -0600275 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
276 pr_err("Display of DSO requested but neither sample IP nor "
277 "sample address\nis selected. Hence, no addresses to convert "
278 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600279 return -EINVAL;
280 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200281 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
282 pr_err("Display of source line number requested but sample IP is not\n"
283 "selected. Hence, no address to lookup the source line number.\n");
284 return -EINVAL;
285 }
David Ahern1424dc92011-03-09 22:23:28 -0700286
287 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300288 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
289 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700290 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700291
292 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300293 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
294 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700295 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700296
297 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300298 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
299 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700300 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600301
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200302 if (PRINT_FIELD(PERIOD) &&
303 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
304 PERF_OUTPUT_PERIOD))
305 return -EINVAL;
306
Stephane Eranianfc36f942015-08-31 18:41:10 +0200307 if (PRINT_FIELD(IREGS) &&
308 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
309 PERF_OUTPUT_IREGS))
310 return -EINVAL;
311
David Ahern9cbdb702011-04-06 21:54:20 -0600312 return 0;
313}
314
Adrian Hunter7ea95722013-11-01 15:51:30 +0200315static void set_print_ip_opts(struct perf_event_attr *attr)
316{
317 unsigned int type = attr->type;
318
319 output[type].print_ip_opts = 0;
320 if (PRINT_FIELD(IP))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300321 output[type].print_ip_opts |= EVSEL__PRINT_IP;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200322
323 if (PRINT_FIELD(SYM))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300324 output[type].print_ip_opts |= EVSEL__PRINT_SYM;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200325
326 if (PRINT_FIELD(DSO))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300327 output[type].print_ip_opts |= EVSEL__PRINT_DSO;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200328
329 if (PRINT_FIELD(SYMOFFSET))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300330 output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200331
332 if (PRINT_FIELD(SRCLINE))
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300333 output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200334}
335
David Ahern9cbdb702011-04-06 21:54:20 -0600336/*
337 * verify all user requested events exist and the samples
338 * have the expected data
339 */
340static int perf_session__check_output_opt(struct perf_session *session)
341{
342 int j;
343 struct perf_evsel *evsel;
344
345 for (j = 0; j < PERF_TYPE_MAX; ++j) {
346 evsel = perf_session__find_first_evtype(session, j);
347
348 /*
349 * even if fields is set to 0 (ie., show nothing) event must
350 * exist if user explicitly includes it on the command line
351 */
352 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
353 pr_err("%s events do not exist. "
354 "Remove corresponding -f option to proceed.\n",
355 event_type(j));
356 return -1;
357 }
358
359 if (evsel && output[j].fields &&
360 perf_evsel__check_attr(evsel, session))
361 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400362
363 if (evsel == NULL)
364 continue;
365
Adrian Hunter7ea95722013-11-01 15:51:30 +0200366 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700367 }
368
Adrian Hunter98526ee2014-07-31 09:00:59 +0300369 if (!no_callchain) {
370 bool use_callchain = false;
371
372 evlist__for_each(session->evlist, evsel) {
373 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
374 use_callchain = true;
375 break;
376 }
377 }
378 if (!use_callchain)
379 symbol_conf.use_callchain = false;
380 }
381
David Ahern80b8b492013-11-19 21:07:37 -0700382 /*
383 * set default for tracepoints to print symbols only
384 * if callchains are present
385 */
386 if (symbol_conf.use_callchain &&
387 !output[PERF_TYPE_TRACEPOINT].user_set) {
388 struct perf_event_attr *attr;
389
390 j = PERF_TYPE_TRACEPOINT;
391 evsel = perf_session__find_first_evtype(session, j);
392 if (evsel == NULL)
393 goto out;
394
395 attr = &evsel->attr;
396
397 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
398 output[j].fields |= PERF_OUTPUT_IP;
399 output[j].fields |= PERF_OUTPUT_SYM;
400 output[j].fields |= PERF_OUTPUT_DSO;
401 set_print_ip_opts(attr);
402 }
403 }
404
405out:
David Ahern1424dc92011-03-09 22:23:28 -0700406 return 0;
407}
David Ahern745f43e2011-03-09 22:23:26 -0700408
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300409static void print_sample_iregs(struct perf_sample *sample,
Stephane Eranianfc36f942015-08-31 18:41:10 +0200410 struct perf_event_attr *attr)
411{
412 struct regs_dump *regs = &sample->intr_regs;
413 uint64_t mask = attr->sample_regs_intr;
414 unsigned i = 0, r;
415
416 if (!regs)
417 return;
418
419 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
420 u64 val = regs->regs[i++];
421 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
422 }
423}
424
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300425static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700426 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300427 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700428{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300429 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700430 unsigned long secs;
431 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700432 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700433
David Ahern745f43e2011-03-09 22:23:26 -0700434 if (PRINT_FIELD(COMM)) {
435 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200436 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600437 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200438 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700439 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200440 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700441 }
David Ahernc70c94b2011-03-09 22:23:25 -0700442
David Ahern745f43e2011-03-09 22:23:26 -0700443 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
444 printf("%5d/%-5d ", sample->pid, sample->tid);
445 else if (PRINT_FIELD(PID))
446 printf("%5d ", sample->pid);
447 else if (PRINT_FIELD(TID))
448 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700449
David Ahern745f43e2011-03-09 22:23:26 -0700450 if (PRINT_FIELD(CPU)) {
451 if (latency_format)
452 printf("%3d ", sample->cpu);
453 else
454 printf("[%03d] ", sample->cpu);
455 }
David Ahernc70c94b2011-03-09 22:23:25 -0700456
David Ahern745f43e2011-03-09 22:23:26 -0700457 if (PRINT_FIELD(TIME)) {
458 nsecs = sample->time;
459 secs = nsecs / NSECS_PER_SEC;
460 nsecs -= secs * NSECS_PER_SEC;
461 usecs = nsecs / NSECS_PER_USEC;
Adrian Hunter83e19862015-09-25 16:15:36 +0300462 if (nanosecs)
463 printf("%5lu.%09llu: ", secs, nsecs);
464 else
465 printf("%5lu.%06lu: ", secs, usecs);
David Ahern745f43e2011-03-09 22:23:26 -0700466 }
David Ahernc70c94b2011-03-09 22:23:25 -0700467}
468
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200469static inline char
470mispred_str(struct branch_entry *br)
471{
472 if (!(br->flags.mispred || br->flags.predicted))
473 return '-';
474
475 return br->flags.predicted ? 'P' : 'M';
476}
477
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300478static void print_sample_brstack(struct perf_sample *sample)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200479{
480 struct branch_stack *br = sample->branch_stack;
481 u64 i;
482
483 if (!(br && br->nr))
484 return;
485
486 for (i = 0; i < br->nr; i++) {
487 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
488 br->entries[i].from,
489 br->entries[i].to,
490 mispred_str( br->entries + i),
491 br->entries[i].flags.in_tx? 'X' : '-',
492 br->entries[i].flags.abort? 'A' : '-',
493 br->entries[i].flags.cycles);
494 }
495}
496
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300497static void print_sample_brstacksym(struct perf_sample *sample,
498 struct thread *thread)
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200499{
500 struct branch_stack *br = sample->branch_stack;
501 struct addr_location alf, alt;
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200502 u64 i, from, to;
503
504 if (!(br && br->nr))
505 return;
506
507 for (i = 0; i < br->nr; i++) {
508
509 memset(&alf, 0, sizeof(alf));
510 memset(&alt, 0, sizeof(alt));
511 from = br->entries[i].from;
512 to = br->entries[i].to;
513
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300514 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200515 if (alf.map)
516 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
517
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -0300518 thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200519 if (alt.map)
520 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
521
522 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
523 putchar('/');
524 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
525 printf("/%c/%c/%c/%d ",
526 mispred_str( br->entries + i),
527 br->entries[i].flags.in_tx? 'X' : '-',
528 br->entries[i].flags.abort? 'A' : '-',
529 br->entries[i].flags.cycles);
530 }
531}
532
533
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300534static void print_sample_addr(struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600535 struct thread *thread,
536 struct perf_event_attr *attr)
537{
538 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600539
540 printf("%16" PRIx64, sample->addr);
541
542 if (!sample_addr_correlates_sym(attr))
543 return;
544
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300545 thread__resolve(thread, &al, sample);
David Ahern7cec0922011-05-30 13:08:23 -0600546
547 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900548 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900549 if (PRINT_FIELD(SYMOFFSET))
550 symbol__fprintf_symname_offs(al.sym, &al, stdout);
551 else
552 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600553 }
554
555 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900556 printf(" (");
557 map__fprintf_dsoname(al.map, stdout);
558 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600559 }
560}
561
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300562static void print_sample_bts(struct perf_sample *sample,
Akihiro Nagai95582592012-01-30 13:43:09 +0900563 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200564 struct thread *thread,
565 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900566{
567 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300568 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900569
570 /* print branch_from information */
571 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300572 unsigned int print_opts = output[attr->type].print_ip_opts;
Chris Phlipote557b672016-04-19 19:32:11 -0700573 struct callchain_cursor *cursor = NULL;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300574
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300575 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700576 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300577 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700578 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300579
580 if (cursor == NULL) {
581 putchar(' ');
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300582 if (print_opts & EVSEL__PRINT_SRCLINE) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300583 print_srcline_last = true;
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -0300584 print_opts &= ~EVSEL__PRINT_SRCLINE;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300585 }
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300586 } else
587 putchar('\n');
588
589 sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
Akihiro Nagai95582592012-01-30 13:43:09 +0900590 }
591
Akihiro Nagai95582592012-01-30 13:43:09 +0900592 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300593 if (PRINT_FIELD(ADDR) ||
594 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300595 !output[attr->type].user_set)) {
596 printf(" => ");
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300597 print_sample_addr(sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300598 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900599
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300600 if (print_srcline_last)
601 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
602
Akihiro Nagai95582592012-01-30 13:43:09 +0900603 printf("\n");
604}
605
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300606static void print_sample_flags(u32 flags)
607{
608 const char *chars = PERF_IP_FLAG_CHARS;
609 const int n = strlen(PERF_IP_FLAG_CHARS);
610 char str[33];
611 int i, pos = 0;
612
613 for (i = 0; i < n; i++, flags >>= 1) {
614 if (flags & 1)
615 str[pos++] = chars[i];
616 }
617 for (; i < 32; i++, flags >>= 1) {
618 if (flags & 1)
619 str[pos++] = '?';
620 }
621 str[pos] = 0;
622 printf(" %-4s ", str);
623}
624
Wang Nan30372f02016-02-24 11:20:45 +0000625struct printer_data {
626 int line_no;
627 bool hit_nul;
628 bool is_printable;
629};
630
631static void
632print_sample_bpf_output_printer(enum binary_printer_ops op,
633 unsigned int val,
634 void *extra)
635{
636 unsigned char ch = (unsigned char)val;
637 struct printer_data *printer_data = extra;
638
639 switch (op) {
640 case BINARY_PRINT_DATA_BEGIN:
641 printf("\n");
642 break;
643 case BINARY_PRINT_LINE_BEGIN:
644 printf("%17s", !printer_data->line_no ? "BPF output:" :
645 " ");
646 break;
647 case BINARY_PRINT_ADDR:
648 printf(" %04x:", val);
649 break;
650 case BINARY_PRINT_NUM_DATA:
651 printf(" %02x", val);
652 break;
653 case BINARY_PRINT_NUM_PAD:
654 printf(" ");
655 break;
656 case BINARY_PRINT_SEP:
657 printf(" ");
658 break;
659 case BINARY_PRINT_CHAR_DATA:
660 if (printer_data->hit_nul && ch)
661 printer_data->is_printable = false;
662
663 if (!isprint(ch)) {
664 printf("%c", '.');
665
666 if (!printer_data->is_printable)
667 break;
668
669 if (ch == '\0')
670 printer_data->hit_nul = true;
671 else
672 printer_data->is_printable = false;
673 } else {
674 printf("%c", ch);
675 }
676 break;
677 case BINARY_PRINT_CHAR_PAD:
678 printf(" ");
679 break;
680 case BINARY_PRINT_LINE_END:
681 printf("\n");
682 printer_data->line_no++;
683 break;
684 case BINARY_PRINT_DATA_END:
685 default:
686 break;
687 }
688}
689
690static void print_sample_bpf_output(struct perf_sample *sample)
691{
692 unsigned int nr_bytes = sample->raw_size;
693 struct printer_data printer_data = {0, false, true};
694
695 print_binary(sample->raw_data, nr_bytes, 8,
696 print_sample_bpf_output_printer, &printer_data);
697
698 if (printer_data.is_printable && printer_data.hit_nul)
699 printf("%17s \"%s\"\n", "BPF string:",
700 (char *)(sample->raw_data));
701}
702
Jiri Olsa809e9422015-11-26 18:55:21 +0100703struct perf_script {
704 struct perf_tool tool;
705 struct perf_session *session;
706 bool show_task_events;
707 bool show_mmap_events;
708 bool show_switch_events;
Jiri Olsacfc88742016-01-05 22:09:06 +0100709 bool allocated;
710 struct cpu_map *cpus;
711 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100712 int name_width;
Jiri Olsa809e9422015-11-26 18:55:21 +0100713};
714
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100715static int perf_evlist__max_name_len(struct perf_evlist *evlist)
716{
717 struct perf_evsel *evsel;
718 int max = 0;
719
720 evlist__for_each(evlist, evsel) {
721 int len = strlen(perf_evsel__name(evsel));
722
723 max = MAX(len, max);
724 }
725
726 return max;
727}
728
Jiri Olsac19ac912016-02-24 09:46:54 +0100729static size_t data_src__printf(u64 data_src)
730{
731 struct mem_info mi = { .data_src.val = data_src };
732 char decode[100];
733 char out[100];
734 static int maxlen;
735 int len;
736
737 perf_script__meminfo_scnprintf(decode, 100, &mi);
738
739 len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
740 if (maxlen < len)
741 maxlen = len;
742
743 return printf("%-*s", maxlen, out);
744}
745
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300746static void process_event(struct perf_script *script,
Jiri Olsa809e9422015-11-26 18:55:21 +0100747 struct perf_sample *sample, struct perf_evsel *evsel,
748 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700749{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300750 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300751 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700752
David Ahern2c9e45f72011-03-17 10:03:21 -0600753 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700754 return;
755
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300756 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700757
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200758 if (PRINT_FIELD(PERIOD))
759 printf("%10" PRIu64 " ", sample->period);
760
Namhyung Kime944d3d2013-11-18 14:34:52 +0900761 if (PRINT_FIELD(EVNAME)) {
762 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100763
764 if (!script->name_width)
765 script->name_width = perf_evlist__max_name_len(script->session->evlist);
766
767 printf("%*s: ", script->name_width,
768 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +0900769 }
770
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300771 if (print_flags)
772 print_sample_flags(sample->flags);
773
Akihiro Nagai95582592012-01-30 13:43:09 +0900774 if (is_bts_event(attr)) {
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300775 print_sample_bts(sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900776 return;
777 }
778
David Ahern745f43e2011-03-09 22:23:26 -0700779 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300780 event_format__print(evsel->tp_format, sample->cpu,
781 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600782 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300783 print_sample_addr(sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600784
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100785 if (PRINT_FIELD(DATA_SRC))
Jiri Olsac19ac912016-02-24 09:46:54 +0100786 data_src__printf(sample->data_src);
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100787
788 if (PRINT_FIELD(WEIGHT))
789 printf("%16" PRIu64, sample->weight);
790
David Ahern787bef12011-05-27 14:28:43 -0600791 if (PRINT_FIELD(IP)) {
Chris Phlipote557b672016-04-19 19:32:11 -0700792 struct callchain_cursor *cursor = NULL;
David Aherna6ffaf92013-08-07 22:50:51 -0400793
Arnaldo Carvalho de Melo92231522016-04-18 11:31:46 -0300794 if (symbol_conf.use_callchain && sample->callchain &&
Chris Phlipote557b672016-04-19 19:32:11 -0700795 thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300796 sample, NULL, NULL, scripting_max_stack) == 0)
Chris Phlipote557b672016-04-19 19:32:11 -0700797 cursor = &callchain_cursor;
Arnaldo Carvalho de Melo6f736732016-04-14 17:45:51 -0300798
799 putchar(cursor ? '\n' : ' ');
800 sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
David Ahernc0230b22011-03-09 22:23:27 -0700801 }
802
Stephane Eranianfc36f942015-08-31 18:41:10 +0200803 if (PRINT_FIELD(IREGS))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300804 print_sample_iregs(sample, attr);
Stephane Eranianfc36f942015-08-31 18:41:10 +0200805
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200806 if (PRINT_FIELD(BRSTACK))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300807 print_sample_brstack(sample);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200808 else if (PRINT_FIELD(BRSTACKSYM))
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300809 print_sample_brstacksym(sample, thread);
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200810
Wang Nan30372f02016-02-24 11:20:45 +0000811 if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
812 print_sample_bpf_output(sample);
813
David Ahernc70c94b2011-03-09 22:23:25 -0700814 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700815}
816
Tom Zanussi956ffd02009-11-25 01:15:46 -0600817static struct scripting_ops *scripting_ops;
818
Jiri Olsa36e33c52016-01-06 11:49:56 +0100819static void __process_stat(struct perf_evsel *counter, u64 tstamp)
820{
821 int nthreads = thread_map__nr(counter->threads);
822 int ncpus = perf_evsel__nr_cpus(counter);
823 int cpu, thread;
824 static int header_printed;
825
826 if (counter->system_wide)
827 nthreads = 1;
828
829 if (!header_printed) {
830 printf("%3s %8s %15s %15s %15s %15s %s\n",
831 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
832 header_printed = 1;
833 }
834
835 for (thread = 0; thread < nthreads; thread++) {
836 for (cpu = 0; cpu < ncpus; cpu++) {
837 struct perf_counts_values *counts;
838
839 counts = perf_counts(counter->counts, cpu, thread);
840
841 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
842 counter->cpus->map[cpu],
843 thread_map__pid(counter->threads, thread),
844 counts->val,
845 counts->ena,
846 counts->run,
847 tstamp,
848 perf_evsel__name(counter));
849 }
850 }
851}
852
Jiri Olsae099eba2016-01-05 22:09:09 +0100853static void process_stat(struct perf_evsel *counter, u64 tstamp)
854{
855 if (scripting_ops && scripting_ops->process_stat)
856 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +0100857 else
858 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +0100859}
860
861static void process_stat_interval(u64 tstamp)
862{
863 if (scripting_ops && scripting_ops->process_stat_interval)
864 scripting_ops->process_stat_interval(tstamp);
865}
866
Tom Zanussi956ffd02009-11-25 01:15:46 -0600867static void setup_scripting(void)
868{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600869 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600870 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600871}
872
Adrian Hunterd445dd22014-08-15 22:08:37 +0300873static int flush_scripting(void)
874{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100875 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300876}
877
Tom Zanussi956ffd02009-11-25 01:15:46 -0600878static int cleanup_scripting(void)
879{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100880 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500881
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100882 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600883}
884
Jiri Olsa809e9422015-11-26 18:55:21 +0100885static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200886 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200887 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300888 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200889 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200890{
Jiri Olsa809e9422015-11-26 18:55:21 +0100891 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -0700892 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200893
David Ahern1424dc92011-03-09 22:23:28 -0700894 if (debug_mode) {
895 if (sample->time < last_timestamp) {
896 pr_err("Samples misordered, previous: %" PRIu64
897 " this: %" PRIu64 "\n", last_timestamp,
898 sample->time);
899 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200900 }
David Ahern1424dc92011-03-09 22:23:28 -0700901 last_timestamp = sample->time;
902 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200903 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000904
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300905 if (machine__resolve(machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700906 pr_err("problem processing %d event, skipping it.\n",
907 event->header.type);
908 return -1;
909 }
910
911 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300912 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700913
Anton Blanchard5d67be92011-07-04 21:57:50 +1000914 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300915 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000916
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100917 if (scripting_ops)
918 scripting_ops->process_event(event, sample, evsel, &al);
919 else
Arnaldo Carvalho de Meloa3dff302016-03-23 11:55:24 -0300920 process_event(scr, sample, evsel, &al);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100921
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300922out_put:
923 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200924 return 0;
925}
926
Adrian Hunter7ea95722013-11-01 15:51:30 +0200927static int process_attr(struct perf_tool *tool, union perf_event *event,
928 struct perf_evlist **pevlist)
929{
930 struct perf_script *scr = container_of(tool, struct perf_script, tool);
931 struct perf_evlist *evlist;
932 struct perf_evsel *evsel, *pos;
933 int err;
934
935 err = perf_event__process_attr(tool, event, pevlist);
936 if (err)
937 return err;
938
939 evlist = *pevlist;
940 evsel = perf_evlist__last(*pevlist);
941
942 if (evsel->attr.type >= PERF_TYPE_MAX)
943 return 0;
944
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300945 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200946 if (pos->attr.type == evsel->attr.type && pos != evsel)
947 return 0;
948 }
949
950 set_print_ip_opts(&evsel->attr);
951
Jiri Olsad2b5a312015-10-16 12:41:25 +0200952 if (evsel->attr.sample_type)
953 err = perf_evsel__check_attr(evsel, scr->session);
954
955 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200956}
957
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900958static int process_comm_event(struct perf_tool *tool,
959 union perf_event *event,
960 struct perf_sample *sample,
961 struct machine *machine)
962{
963 struct thread *thread;
964 struct perf_script *script = container_of(tool, struct perf_script, tool);
965 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300966 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900967 int ret = -1;
968
969 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
970 if (thread == NULL) {
971 pr_debug("problem processing COMM event, skipping it.\n");
972 return -1;
973 }
974
975 if (perf_event__process_comm(tool, event, sample, machine) < 0)
976 goto out;
977
978 if (!evsel->attr.sample_id_all) {
979 sample->cpu = 0;
980 sample->time = 0;
981 sample->tid = event->comm.tid;
982 sample->pid = event->comm.pid;
983 }
984 print_sample_start(sample, thread, evsel);
985 perf_event__fprintf(event, stdout);
986 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900987out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300988 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900989 return ret;
990}
991
992static int process_fork_event(struct perf_tool *tool,
993 union perf_event *event,
994 struct perf_sample *sample,
995 struct machine *machine)
996{
997 struct thread *thread;
998 struct perf_script *script = container_of(tool, struct perf_script, tool);
999 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001000 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001001
1002 if (perf_event__process_fork(tool, event, sample, machine) < 0)
1003 return -1;
1004
1005 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1006 if (thread == NULL) {
1007 pr_debug("problem processing FORK event, skipping it.\n");
1008 return -1;
1009 }
1010
1011 if (!evsel->attr.sample_id_all) {
1012 sample->cpu = 0;
1013 sample->time = event->fork.time;
1014 sample->tid = event->fork.tid;
1015 sample->pid = event->fork.pid;
1016 }
1017 print_sample_start(sample, thread, evsel);
1018 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001019 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001020
1021 return 0;
1022}
1023static int process_exit_event(struct perf_tool *tool,
1024 union perf_event *event,
1025 struct perf_sample *sample,
1026 struct machine *machine)
1027{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001028 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001029 struct thread *thread;
1030 struct perf_script *script = container_of(tool, struct perf_script, tool);
1031 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001032 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001033
1034 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
1035 if (thread == NULL) {
1036 pr_debug("problem processing EXIT event, skipping it.\n");
1037 return -1;
1038 }
1039
1040 if (!evsel->attr.sample_id_all) {
1041 sample->cpu = 0;
1042 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001043 sample->tid = event->fork.tid;
1044 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001045 }
1046 print_sample_start(sample, thread, evsel);
1047 perf_event__fprintf(event, stdout);
1048
1049 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001050 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001051
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001052 thread__put(thread);
1053 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001054}
1055
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001056static int process_mmap_event(struct perf_tool *tool,
1057 union perf_event *event,
1058 struct perf_sample *sample,
1059 struct machine *machine)
1060{
1061 struct thread *thread;
1062 struct perf_script *script = container_of(tool, struct perf_script, tool);
1063 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001064 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001065
1066 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
1067 return -1;
1068
1069 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
1070 if (thread == NULL) {
1071 pr_debug("problem processing MMAP event, skipping it.\n");
1072 return -1;
1073 }
1074
1075 if (!evsel->attr.sample_id_all) {
1076 sample->cpu = 0;
1077 sample->time = 0;
1078 sample->tid = event->mmap.tid;
1079 sample->pid = event->mmap.pid;
1080 }
1081 print_sample_start(sample, thread, evsel);
1082 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001083 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001084 return 0;
1085}
1086
1087static int process_mmap2_event(struct perf_tool *tool,
1088 union perf_event *event,
1089 struct perf_sample *sample,
1090 struct machine *machine)
1091{
1092 struct thread *thread;
1093 struct perf_script *script = container_of(tool, struct perf_script, tool);
1094 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +03001095 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001096
1097 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1098 return -1;
1099
1100 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1101 if (thread == NULL) {
1102 pr_debug("problem processing MMAP2 event, skipping it.\n");
1103 return -1;
1104 }
1105
1106 if (!evsel->attr.sample_id_all) {
1107 sample->cpu = 0;
1108 sample->time = 0;
1109 sample->tid = event->mmap2.tid;
1110 sample->pid = event->mmap2.pid;
1111 }
1112 print_sample_start(sample, thread, evsel);
1113 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001114 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001115 return 0;
1116}
1117
Adrian Hunter7c148982015-07-21 12:44:06 +03001118static int process_switch_event(struct perf_tool *tool,
1119 union perf_event *event,
1120 struct perf_sample *sample,
1121 struct machine *machine)
1122{
1123 struct thread *thread;
1124 struct perf_script *script = container_of(tool, struct perf_script, tool);
1125 struct perf_session *session = script->session;
1126 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1127
1128 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1129 return -1;
1130
1131 thread = machine__findnew_thread(machine, sample->pid,
1132 sample->tid);
1133 if (thread == NULL) {
1134 pr_debug("problem processing SWITCH event, skipping it.\n");
1135 return -1;
1136 }
1137
1138 print_sample_start(sample, thread, evsel);
1139 perf_event__fprintf(event, stdout);
1140 thread__put(thread);
1141 return 0;
1142}
1143
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001144static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001145{
1146 session_done = 1;
1147}
1148
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001149static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001150{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001151 int ret;
1152
Tom Zanussic239da32010-04-01 23:59:18 -05001153 signal(SIGINT, sig_handler);
1154
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001155 /* override event processing functions */
1156 if (script->show_task_events) {
1157 script->tool.comm = process_comm_event;
1158 script->tool.fork = process_fork_event;
1159 script->tool.exit = process_exit_event;
1160 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001161 if (script->show_mmap_events) {
1162 script->tool.mmap = process_mmap_event;
1163 script->tool.mmap2 = process_mmap2_event;
1164 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001165 if (script->show_switch_events)
1166 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001167
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001168 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001169
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001170 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001171 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001172
1173 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001174}
1175
Tom Zanussi956ffd02009-11-25 01:15:46 -06001176struct script_spec {
1177 struct list_head node;
1178 struct scripting_ops *ops;
1179 char spec[0];
1180};
1181
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001182static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001183
1184static struct script_spec *script_spec__new(const char *spec,
1185 struct scripting_ops *ops)
1186{
1187 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1188
1189 if (s != NULL) {
1190 strcpy(s->spec, spec);
1191 s->ops = ops;
1192 }
1193
1194 return s;
1195}
1196
Tom Zanussi956ffd02009-11-25 01:15:46 -06001197static void script_spec__add(struct script_spec *s)
1198{
1199 list_add_tail(&s->node, &script_specs);
1200}
1201
1202static struct script_spec *script_spec__find(const char *spec)
1203{
1204 struct script_spec *s;
1205
1206 list_for_each_entry(s, &script_specs, node)
1207 if (strcasecmp(s->spec, spec) == 0)
1208 return s;
1209 return NULL;
1210}
1211
Tom Zanussi956ffd02009-11-25 01:15:46 -06001212int script_spec_register(const char *spec, struct scripting_ops *ops)
1213{
1214 struct script_spec *s;
1215
1216 s = script_spec__find(spec);
1217 if (s)
1218 return -1;
1219
Taeung Song8560bae2016-02-26 00:13:10 +09001220 s = script_spec__new(spec, ops);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001221 if (!s)
1222 return -1;
Taeung Song8560bae2016-02-26 00:13:10 +09001223 else
1224 script_spec__add(s);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001225
1226 return 0;
1227}
1228
1229static struct scripting_ops *script_spec__lookup(const char *spec)
1230{
1231 struct script_spec *s = script_spec__find(spec);
1232 if (!s)
1233 return NULL;
1234
1235 return s->ops;
1236}
1237
1238static void list_available_languages(void)
1239{
1240 struct script_spec *s;
1241
1242 fprintf(stderr, "\n");
1243 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001244 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001245
1246 list_for_each_entry(s, &script_specs, node)
1247 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1248
1249 fprintf(stderr, "\n");
1250}
1251
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001252static int parse_scriptname(const struct option *opt __maybe_unused,
1253 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001254{
1255 char spec[PATH_MAX];
1256 const char *script, *ext;
1257 int len;
1258
Tom Zanussif526d682010-01-27 02:27:52 -06001259 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001260 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001261 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001262 }
1263
1264 script = strchr(str, ':');
1265 if (script) {
1266 len = script - str;
1267 if (len >= PATH_MAX) {
1268 fprintf(stderr, "invalid language specifier");
1269 return -1;
1270 }
1271 strncpy(spec, str, len);
1272 spec[len] = '\0';
1273 scripting_ops = script_spec__lookup(spec);
1274 if (!scripting_ops) {
1275 fprintf(stderr, "invalid language specifier");
1276 return -1;
1277 }
1278 script++;
1279 } else {
1280 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001281 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001282 if (!ext) {
1283 fprintf(stderr, "invalid script extension");
1284 return -1;
1285 }
1286 scripting_ops = script_spec__lookup(++ext);
1287 if (!scripting_ops) {
1288 fprintf(stderr, "invalid script extension");
1289 return -1;
1290 }
1291 }
1292
1293 script_name = strdup(script);
1294
1295 return 0;
1296}
1297
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001298static int parse_output_fields(const struct option *opt __maybe_unused,
1299 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001300{
1301 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001302 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001303 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001304 int rc = 0;
1305 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001306 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001307
1308 if (!str)
1309 return -ENOMEM;
1310
David Ahern2c9e45f72011-03-17 10:03:21 -06001311 /* first word can state for which event type the user is specifying
1312 * the fields. If no type exists, the specified fields apply to all
1313 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001314 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001315 tok = strchr(str, ':');
1316 if (tok) {
1317 *tok = '\0';
1318 tok++;
1319 if (!strcmp(str, "hw"))
1320 type = PERF_TYPE_HARDWARE;
1321 else if (!strcmp(str, "sw"))
1322 type = PERF_TYPE_SOFTWARE;
1323 else if (!strcmp(str, "trace"))
1324 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001325 else if (!strcmp(str, "raw"))
1326 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001327 else if (!strcmp(str, "break"))
1328 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001329 else {
1330 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001331 rc = -EINVAL;
1332 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001333 }
1334
1335 if (output[type].user_set)
1336 pr_warning("Overriding previous field request for %s events.\n",
1337 event_type(type));
1338
1339 output[type].fields = 0;
1340 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001341 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001342
1343 } else {
1344 tok = str;
1345 if (strlen(str) == 0) {
1346 fprintf(stderr,
1347 "Cannot set fields to 'none' for all event types.\n");
1348 rc = -EINVAL;
1349 goto out;
1350 }
1351
1352 if (output_set_by_user())
1353 pr_warning("Overriding previous field request for all events.\n");
1354
1355 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1356 output[j].fields = 0;
1357 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001358 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001359 }
David Ahern1424dc92011-03-09 22:23:28 -07001360 }
1361
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001362 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001363 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001364 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001365 break;
David Ahern745f43e2011-03-09 22:23:26 -07001366 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001367 if (i == imax && strcmp(tok, "flags") == 0) {
1368 print_flags = true;
1369 continue;
1370 }
David Ahern745f43e2011-03-09 22:23:26 -07001371 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001372 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001373 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001374 goto out;
1375 }
1376
1377 if (type == -1) {
1378 /* add user option to all events types for
1379 * which it is valid
1380 */
1381 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1382 if (output[j].invalid_fields & all_output_options[i].field) {
1383 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1384 all_output_options[i].str, event_type(j));
1385 } else
1386 output[j].fields |= all_output_options[i].field;
1387 }
1388 } else {
1389 if (output[type].invalid_fields & all_output_options[i].field) {
1390 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1391 all_output_options[i].str, event_type(type));
1392
1393 rc = -EINVAL;
1394 goto out;
1395 }
1396 output[type].fields |= all_output_options[i].field;
1397 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001398 }
1399
1400 if (type >= 0) {
1401 if (output[type].fields == 0) {
1402 pr_debug("No fields requested for %s type. "
1403 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001404 }
David Ahern1424dc92011-03-09 22:23:28 -07001405 }
David Ahern745f43e2011-03-09 22:23:26 -07001406
David Ahern2c9e45f72011-03-17 10:03:21 -06001407out:
David Ahern745f43e2011-03-09 22:23:26 -07001408 free(str);
1409 return rc;
1410}
1411
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001412/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1413static int is_directory(const char *base_path, const struct dirent *dent)
1414{
1415 char path[PATH_MAX];
1416 struct stat st;
1417
1418 sprintf(path, "%s/%s", base_path, dent->d_name);
1419 if (stat(path, &st))
1420 return 0;
1421
1422 return S_ISDIR(st.st_mode);
1423}
1424
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001425#define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
1426 while ((lang_dirent = readdir(scripts_dir)) != NULL) \
1427 if ((lang_dirent->d_type == DT_DIR || \
1428 (lang_dirent->d_type == DT_UNKNOWN && \
1429 is_directory(scripts_path, lang_dirent))) && \
1430 (strcmp(lang_dirent->d_name, ".")) && \
1431 (strcmp(lang_dirent->d_name, "..")))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001432
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001433#define for_each_script(lang_path, lang_dir, script_dirent) \
1434 while ((script_dirent = readdir(lang_dir)) != NULL) \
1435 if (script_dirent->d_type != DT_DIR && \
1436 (script_dirent->d_type != DT_UNKNOWN || \
1437 !is_directory(lang_path, script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001438
1439
1440#define RECORD_SUFFIX "-record"
1441#define REPORT_SUFFIX "-report"
1442
1443struct script_desc {
1444 struct list_head node;
1445 char *name;
1446 char *half_liner;
1447 char *args;
1448};
1449
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001450static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001451
1452static struct script_desc *script_desc__new(const char *name)
1453{
1454 struct script_desc *s = zalloc(sizeof(*s));
1455
Tom Zanussib5b87312010-11-10 08:16:51 -06001456 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001457 s->name = strdup(name);
1458
1459 return s;
1460}
1461
1462static void script_desc__delete(struct script_desc *s)
1463{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001464 zfree(&s->name);
1465 zfree(&s->half_liner);
1466 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001467 free(s);
1468}
1469
1470static void script_desc__add(struct script_desc *s)
1471{
1472 list_add_tail(&s->node, &script_descs);
1473}
1474
1475static struct script_desc *script_desc__find(const char *name)
1476{
1477 struct script_desc *s;
1478
1479 list_for_each_entry(s, &script_descs, node)
1480 if (strcasecmp(s->name, name) == 0)
1481 return s;
1482 return NULL;
1483}
1484
1485static struct script_desc *script_desc__findnew(const char *name)
1486{
1487 struct script_desc *s = script_desc__find(name);
1488
1489 if (s)
1490 return s;
1491
1492 s = script_desc__new(name);
1493 if (!s)
1494 goto out_delete_desc;
1495
1496 script_desc__add(s);
1497
1498 return s;
1499
1500out_delete_desc:
1501 script_desc__delete(s);
1502
1503 return NULL;
1504}
1505
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001506static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001507{
1508 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001509 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001510
1511 if (strlen(str) > suffix_len) {
1512 p = str + strlen(str) - suffix_len;
1513 if (!strncmp(p, suffix, suffix_len))
1514 return p;
1515 }
1516
1517 return NULL;
1518}
1519
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001520static int read_script_info(struct script_desc *desc, const char *filename)
1521{
1522 char line[BUFSIZ], *p;
1523 FILE *fp;
1524
1525 fp = fopen(filename, "r");
1526 if (!fp)
1527 return -1;
1528
1529 while (fgets(line, sizeof(line), fp)) {
1530 p = ltrim(line);
1531 if (strlen(p) == 0)
1532 continue;
1533 if (*p != '#')
1534 continue;
1535 p++;
1536 if (strlen(p) && *p == '!')
1537 continue;
1538
1539 p = ltrim(p);
1540 if (strlen(p) && p[strlen(p) - 1] == '\n')
1541 p[strlen(p) - 1] = '\0';
1542
1543 if (!strncmp(p, "description:", strlen("description:"))) {
1544 p += strlen("description:");
1545 desc->half_liner = strdup(ltrim(p));
1546 continue;
1547 }
1548
1549 if (!strncmp(p, "args:", strlen("args:"))) {
1550 p += strlen("args:");
1551 desc->args = strdup(ltrim(p));
1552 continue;
1553 }
1554 }
1555
1556 fclose(fp);
1557
1558 return 0;
1559}
1560
Robert Richter38efb532011-11-25 11:38:40 +01001561static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1562{
1563 char *script_root, *str;
1564
1565 script_root = strdup(script_dirent->d_name);
1566 if (!script_root)
1567 return NULL;
1568
1569 str = (char *)ends_with(script_root, suffix);
1570 if (!str) {
1571 free(script_root);
1572 return NULL;
1573 }
1574
1575 *str = '\0';
1576 return script_root;
1577}
1578
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001579static int list_available_scripts(const struct option *opt __maybe_unused,
1580 const char *s __maybe_unused,
1581 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001582{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001583 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001584 char scripts_path[MAXPATHLEN];
1585 DIR *scripts_dir, *lang_dir;
1586 char script_path[MAXPATHLEN];
1587 char lang_path[MAXPATHLEN];
1588 struct script_desc *desc;
1589 char first_half[BUFSIZ];
1590 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001591
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001592 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001593
1594 scripts_dir = opendir(scripts_path);
1595 if (!scripts_dir)
1596 return -1;
1597
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001598 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001599 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001600 lang_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001601 lang_dir = opendir(lang_path);
1602 if (!lang_dir)
1603 continue;
1604
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001605 for_each_script(lang_path, lang_dir, script_dirent) {
1606 script_root = get_script_root(script_dirent, REPORT_SUFFIX);
Robert Richter38efb532011-11-25 11:38:40 +01001607 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001608 desc = script_desc__findnew(script_root);
1609 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001610 lang_path, script_dirent->d_name);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001611 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001612 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001613 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001614 }
1615 }
1616
1617 fprintf(stdout, "List of available trace scripts:\n");
1618 list_for_each_entry(desc, &script_descs, node) {
1619 sprintf(first_half, "%s %s", desc->name,
1620 desc->args ? desc->args : "");
1621 fprintf(stdout, " %-36s %s\n", first_half,
1622 desc->half_liner ? desc->half_liner : "");
1623 }
1624
1625 exit(0);
1626}
1627
Feng Tange5f37052012-09-07 16:42:26 +08001628/*
Feng Tang49e639e2012-10-30 11:56:03 +08001629 * Some scripts specify the required events in their "xxx-record" file,
1630 * this function will check if the events in perf.data match those
1631 * mentioned in the "xxx-record".
1632 *
1633 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1634 * which is covered well now. And new parsing code should be added to
1635 * cover the future complexing formats like event groups etc.
1636 */
1637static int check_ev_match(char *dir_name, char *scriptname,
1638 struct perf_session *session)
1639{
1640 char filename[MAXPATHLEN], evname[128];
1641 char line[BUFSIZ], *p;
1642 struct perf_evsel *pos;
1643 int match, len;
1644 FILE *fp;
1645
1646 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1647
1648 fp = fopen(filename, "r");
1649 if (!fp)
1650 return -1;
1651
1652 while (fgets(line, sizeof(line), fp)) {
1653 p = ltrim(line);
1654 if (*p == '#')
1655 continue;
1656
1657 while (strlen(p)) {
1658 p = strstr(p, "-e");
1659 if (!p)
1660 break;
1661
1662 p += 2;
1663 p = ltrim(p);
1664 len = strcspn(p, " \t");
1665 if (!len)
1666 break;
1667
1668 snprintf(evname, len + 1, "%s", p);
1669
1670 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001671 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001672 if (!strcmp(perf_evsel__name(pos), evname)) {
1673 match = 1;
1674 break;
1675 }
1676 }
1677
1678 if (!match) {
1679 fclose(fp);
1680 return -1;
1681 }
1682 }
1683 }
1684
1685 fclose(fp);
1686 return 0;
1687}
1688
1689/*
Feng Tange5f37052012-09-07 16:42:26 +08001690 * Return -1 if none is found, otherwise the actual scripts number.
1691 *
1692 * Currently the only user of this function is the script browser, which
1693 * will list all statically runnable scripts, select one, execute it and
1694 * show the output in a perf browser.
1695 */
1696int find_scripts(char **scripts_array, char **scripts_path_array)
1697{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001698 struct dirent *script_dirent, *lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001699 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001700 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001701 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001702 struct perf_data_file file = {
1703 .path = input_name,
1704 .mode = PERF_DATA_MODE_READ,
1705 };
Feng Tange5f37052012-09-07 16:42:26 +08001706 char *temp;
1707 int i = 0;
1708
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001709 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001710 if (!session)
1711 return -1;
1712
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001713 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08001714
1715 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001716 if (!scripts_dir) {
1717 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001718 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001719 }
Feng Tange5f37052012-09-07 16:42:26 +08001720
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001721 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08001722 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001723 lang_dirent->d_name);
Feng Tange5f37052012-09-07 16:42:26 +08001724#ifdef NO_LIBPERL
1725 if (strstr(lang_path, "perl"))
1726 continue;
1727#endif
1728#ifdef NO_LIBPYTHON
1729 if (strstr(lang_path, "python"))
1730 continue;
1731#endif
1732
1733 lang_dir = opendir(lang_path);
1734 if (!lang_dir)
1735 continue;
1736
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001737 for_each_script(lang_path, lang_dir, script_dirent) {
Feng Tange5f37052012-09-07 16:42:26 +08001738 /* Skip those real time scripts: xxxtop.p[yl] */
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001739 if (strstr(script_dirent->d_name, "top."))
Feng Tange5f37052012-09-07 16:42:26 +08001740 continue;
1741 sprintf(scripts_path_array[i], "%s/%s", lang_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001742 script_dirent->d_name);
1743 temp = strchr(script_dirent->d_name, '.');
Feng Tange5f37052012-09-07 16:42:26 +08001744 snprintf(scripts_array[i],
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001745 (temp - script_dirent->d_name) + 1,
1746 "%s", script_dirent->d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001747
1748 if (check_ev_match(lang_path,
1749 scripts_array[i], session))
1750 continue;
1751
Feng Tange5f37052012-09-07 16:42:26 +08001752 i++;
1753 }
Feng Tang49e639e2012-10-30 11:56:03 +08001754 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001755 }
1756
Feng Tang49e639e2012-10-30 11:56:03 +08001757 closedir(scripts_dir);
1758 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001759 return i;
1760}
1761
Tom Zanussi38752942009-12-15 02:53:39 -06001762static char *get_script_path(const char *script_root, const char *suffix)
1763{
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001764 struct dirent *script_dirent, *lang_dirent;
Tom Zanussi38752942009-12-15 02:53:39 -06001765 char scripts_path[MAXPATHLEN];
1766 char script_path[MAXPATHLEN];
1767 DIR *scripts_dir, *lang_dir;
1768 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001769 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001770
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001771 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06001772
1773 scripts_dir = opendir(scripts_path);
1774 if (!scripts_dir)
1775 return NULL;
1776
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001777 for_each_lang(scripts_path, scripts_dir, lang_dirent) {
Tom Zanussi38752942009-12-15 02:53:39 -06001778 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001779 lang_dirent->d_name);
Tom Zanussi38752942009-12-15 02:53:39 -06001780 lang_dir = opendir(lang_path);
1781 if (!lang_dir)
1782 continue;
1783
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001784 for_each_script(lang_path, lang_dir, script_dirent) {
1785 __script_root = get_script_root(script_dirent, suffix);
Robert Richter38efb532011-11-25 11:38:40 +01001786 if (__script_root && !strcmp(script_root, __script_root)) {
1787 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001788 closedir(lang_dir);
1789 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001790 snprintf(script_path, MAXPATHLEN, "%s/%s",
Arnaldo Carvalho de Meloa5e8e822016-04-08 11:25:59 -03001791 lang_path, script_dirent->d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001792 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001793 }
1794 free(__script_root);
1795 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001796 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001797 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001798 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001799
Robert Richter38efb532011-11-25 11:38:40 +01001800 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001801}
1802
Tom Zanussib5b87312010-11-10 08:16:51 -06001803static bool is_top_script(const char *script_path)
1804{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001805 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001806}
1807
1808static int has_required_arg(char *script_path)
1809{
1810 struct script_desc *desc;
1811 int n_args = 0;
1812 char *p;
1813
1814 desc = script_desc__new(NULL);
1815
1816 if (read_script_info(desc, script_path))
1817 goto out;
1818
1819 if (!desc->args)
1820 goto out;
1821
1822 for (p = desc->args; *p; p++)
1823 if (*p == '<')
1824 n_args++;
1825out:
1826 script_desc__delete(desc);
1827
1828 return n_args;
1829}
1830
David Ahernd54b1a92012-08-26 12:24:46 -06001831static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001832{
1833 char **__argv = malloc(sizeof(const char *) * argc);
1834
David Ahernd54b1a92012-08-26 12:24:46 -06001835 if (!__argv) {
1836 pr_err("malloc failed\n");
1837 return -1;
1838 }
1839
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001840 memcpy(__argv, argv, sizeof(const char *) * argc);
1841 argc = parse_options(argc, (const char **)__argv, record_options,
1842 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1843 free(__argv);
1844
David Ahernd54b1a92012-08-26 12:24:46 -06001845 system_wide = (argc == 0);
1846
1847 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001848}
1849
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001850static void script__setup_sample_type(struct perf_script *script)
1851{
1852 struct perf_session *session = script->session;
1853 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1854
1855 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1856 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1857 (sample_type & PERF_SAMPLE_STACK_USER))
1858 callchain_param.record_mode = CALLCHAIN_DWARF;
1859 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1860 callchain_param.record_mode = CALLCHAIN_LBR;
1861 else
1862 callchain_param.record_mode = CALLCHAIN_FP;
1863 }
1864}
1865
Jiri Olsae099eba2016-01-05 22:09:09 +01001866static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1867 union perf_event *event,
1868 struct perf_session *session)
1869{
1870 struct stat_round_event *round = &event->stat_round;
1871 struct perf_evsel *counter;
1872
1873 evlist__for_each(session->evlist, counter) {
1874 perf_stat_process_counter(&stat_config, counter);
1875 process_stat(counter, round->time);
1876 }
1877
1878 process_stat_interval(round->time);
1879 return 0;
1880}
1881
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001882static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1883 union perf_event *event,
1884 struct perf_session *session __maybe_unused)
1885{
1886 perf_event__read_stat_config(&stat_config, &event->stat_config);
1887 return 0;
1888}
1889
Jiri Olsacfc88742016-01-05 22:09:06 +01001890static int set_maps(struct perf_script *script)
1891{
1892 struct perf_evlist *evlist = script->session->evlist;
1893
1894 if (!script->cpus || !script->threads)
1895 return 0;
1896
1897 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1898 return -EINVAL;
1899
1900 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1901
1902 if (perf_evlist__alloc_stats(evlist, true))
1903 return -ENOMEM;
1904
1905 script->allocated = true;
1906 return 0;
1907}
1908
1909static
1910int process_thread_map_event(struct perf_tool *tool,
1911 union perf_event *event,
1912 struct perf_session *session __maybe_unused)
1913{
1914 struct perf_script *script = container_of(tool, struct perf_script, tool);
1915
1916 if (script->threads) {
1917 pr_warning("Extra thread map event, ignoring.\n");
1918 return 0;
1919 }
1920
1921 script->threads = thread_map__new_event(&event->thread_map);
1922 if (!script->threads)
1923 return -ENOMEM;
1924
1925 return set_maps(script);
1926}
1927
1928static
1929int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1930 union perf_event *event,
1931 struct perf_session *session __maybe_unused)
1932{
1933 struct perf_script *script = container_of(tool, struct perf_script, tool);
1934
1935 if (script->cpus) {
1936 pr_warning("Extra cpu map event, ignoring.\n");
1937 return 0;
1938 }
1939
1940 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1941 if (!script->cpus)
1942 return -ENOMEM;
1943
1944 return set_maps(script);
1945}
1946
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001947int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001948{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001949 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001950 bool header = false;
1951 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001952 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001953 char *rec_script_path = NULL;
1954 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001955 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001956 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001957 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001958 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001959 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001960 struct perf_script script = {
1961 .tool = {
1962 .sample = process_sample_event,
1963 .mmap = perf_event__process_mmap,
1964 .mmap2 = perf_event__process_mmap2,
1965 .comm = perf_event__process_comm,
1966 .exit = perf_event__process_exit,
1967 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001968 .attr = process_attr,
Jiri Olsa91daee32016-04-07 09:11:13 +02001969 .event_update = perf_event__process_event_update,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001970 .tracing_data = perf_event__process_tracing_data,
1971 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001972 .id_index = perf_event__process_id_index,
1973 .auxtrace_info = perf_event__process_auxtrace_info,
1974 .auxtrace = perf_event__process_auxtrace,
1975 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01001976 .stat = perf_event__process_stat_event,
1977 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001978 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01001979 .thread_map = process_thread_map_event,
1980 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001981 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001982 .ordering_requires_timestamps = true,
1983 },
1984 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001985 struct perf_data_file file = {
1986 .mode = PERF_DATA_MODE_READ,
1987 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001988 const struct option options[] = {
1989 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1990 "dump raw trace in ASCII"),
1991 OPT_INCR('v', "verbose", &verbose,
1992 "be more verbose (show symbol address, etc)"),
1993 OPT_BOOLEAN('L', "Latency", &latency_format,
1994 "show latency attributes (irqs/preemption disabled, etc)"),
1995 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1996 list_available_scripts),
1997 OPT_CALLBACK('s', "script", NULL, "name",
1998 "script file name (lang:script name, script name, or *)",
1999 parse_scriptname),
2000 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
2001 "generate perf-script.xx script in specified language"),
2002 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2003 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
2004 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01002005 OPT_BOOLEAN(0, "header", &header, "Show data header."),
2006 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002007 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
2008 "file", "vmlinux pathname"),
2009 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2010 "file", "kallsyms pathname"),
2011 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
2012 "When printing symbols do not display call chain"),
He Kuanga7066702016-05-19 11:47:37 +00002013 OPT_CALLBACK(0, "symfs", NULL, "directory",
2014 "Look for files with symbols relative to this directory",
2015 symbol__config_symfs),
Yunlong Song06af0f22015-04-02 21:47:16 +08002016 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002017 "comma separated output fields prepend with 'type:'. "
2018 "Valid types: hw,sw,trace,raw. "
2019 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Stephane Eraniandc323ce2015-08-31 18:41:13 +02002020 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002021 OPT_BOOLEAN('a', "all-cpus", &system_wide,
2022 "system-wide collection from all CPUs"),
2023 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
2024 "only consider these symbols"),
2025 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
2026 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
2027 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06002028 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
2029 "only consider symbols in these pids"),
2030 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
2031 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo6125cc82016-04-14 18:15:18 -03002032 OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
2033 "Set the maximum stack depth when parsing the callchain, "
2034 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03002035 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002036 OPT_BOOLEAN('I', "show-info", &show_full_info,
2037 "display extended information from perf.data file"),
2038 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
2039 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09002040 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
2041 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09002042 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
2043 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03002044 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
2045 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08002046 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03002047 OPT_BOOLEAN(0, "ns", &nanosecs,
2048 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002049 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
2050 "Instruction Tracing options",
2051 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07002052 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
2053 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07002054 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
2055 "Enable symbol demangling"),
2056 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
2057 "Enable kernel symbol demangling"),
2058
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002059 OPT_END()
2060 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08002061 const char * const script_subcommands[] = { "record", "report", NULL };
2062 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03002063 "perf script [<options>]",
2064 "perf script [<options>] record <script> [<record-options>] <command>",
2065 "perf script [<options>] report <script> [script-args]",
2066 "perf script [<options>] <script> [<record-options>] <command>",
2067 "perf script [<options>] <top-script> [script-args]",
2068 NULL
2069 };
Tom Zanussi38752942009-12-15 02:53:39 -06002070
Tom Zanussib5b87312010-11-10 08:16:51 -06002071 setup_scripting();
2072
Yunlong Song40cae2b2015-03-18 21:35:54 +08002073 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06002074 PARSE_OPT_STOP_AT_NON_OPTION);
2075
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002076 file.path = input_name;
2077
Tom Zanussib5b87312010-11-10 08:16:51 -06002078 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
2079 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
2080 if (!rec_script_path)
2081 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06002082 }
2083
Tom Zanussib5b87312010-11-10 08:16:51 -06002084 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
2085 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
2086 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002087 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002088 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002089 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002090 return -1;
2091 }
Tom Zanussi38752942009-12-15 02:53:39 -06002092 }
2093
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002094 if (itrace_synth_opts.callchain &&
2095 itrace_synth_opts.callchain_sz > scripting_max_stack)
2096 scripting_max_stack = itrace_synth_opts.callchain_sz;
2097
Ben Hutchings44e668c2010-10-10 16:10:03 +01002098 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002099 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002100
Tom Zanussib5b87312010-11-10 08:16:51 -06002101 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002102 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002103 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002104 pid_t pid;
2105
Tom Zanussib5b87312010-11-10 08:16:51 -06002106 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2107 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2108
2109 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002110 usage_with_options_msg(script_usage, options,
2111 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002112 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002113 }
2114
Tom Zanussib5b87312010-11-10 08:16:51 -06002115 if (is_top_script(argv[0])) {
2116 rep_args = argc - 1;
2117 } else {
2118 int rec_args;
2119
2120 rep_args = has_required_arg(rep_script_path);
2121 rec_args = (argc - 1) - rep_args;
2122 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002123 usage_with_options_msg(script_usage, options,
2124 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002125 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002126 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002127 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002128 }
2129
2130 if (pipe(live_pipe) < 0) {
2131 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002132 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002133 }
2134
2135 pid = fork();
2136 if (pid < 0) {
2137 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002138 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002139 }
2140
2141 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002142 j = 0;
2143
Tom Zanussia0cccc22010-04-01 23:59:25 -05002144 dup2(live_pipe[1], 1);
2145 close(live_pipe[0]);
2146
Robert Richter317df652011-11-25 15:05:25 +01002147 if (is_top_script(argv[0])) {
2148 system_wide = true;
2149 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002150 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2151 err = -1;
2152 goto out;
2153 }
Robert Richter317df652011-11-25 15:05:25 +01002154 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002155
2156 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002157 if (!__argv) {
2158 pr_err("malloc failed\n");
2159 err = -ENOMEM;
2160 goto out;
2161 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002162
Tom Zanussib5b87312010-11-10 08:16:51 -06002163 __argv[j++] = "/bin/sh";
2164 __argv[j++] = rec_script_path;
2165 if (system_wide)
2166 __argv[j++] = "-a";
2167 __argv[j++] = "-q";
2168 __argv[j++] = "-o";
2169 __argv[j++] = "-";
2170 for (i = rep_args + 1; i < argc; i++)
2171 __argv[j++] = argv[i];
2172 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002173
2174 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002175 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002176 exit(-1);
2177 }
2178
2179 dup2(live_pipe[0], 0);
2180 close(live_pipe[1]);
2181
Tom Zanussib5b87312010-11-10 08:16:51 -06002182 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002183 if (!__argv) {
2184 pr_err("malloc failed\n");
2185 err = -ENOMEM;
2186 goto out;
2187 }
2188
Tom Zanussib5b87312010-11-10 08:16:51 -06002189 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002190 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002191 __argv[j++] = rep_script_path;
2192 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002193 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002194 __argv[j++] = "-i";
2195 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002196 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002197
2198 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002199 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002200 exit(-1);
2201 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002202
Tom Zanussib5b87312010-11-10 08:16:51 -06002203 if (rec_script_path)
2204 script_path = rec_script_path;
2205 if (rep_script_path)
2206 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002207
Tom Zanussib5b87312010-11-10 08:16:51 -06002208 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002209 j = 0;
2210
Robert Richter317df652011-11-25 15:05:25 +01002211 if (!rec_script_path)
2212 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002213 else if (!system_wide) {
2214 if (have_cmd(argc - 1, &argv[1]) != 0) {
2215 err = -1;
2216 goto out;
2217 }
2218 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002219
2220 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002221 if (!__argv) {
2222 pr_err("malloc failed\n");
2223 err = -ENOMEM;
2224 goto out;
2225 }
2226
Tom Zanussib5b87312010-11-10 08:16:51 -06002227 __argv[j++] = "/bin/sh";
2228 __argv[j++] = script_path;
2229 if (system_wide)
2230 __argv[j++] = "-a";
2231 for (i = 2; i < argc; i++)
2232 __argv[j++] = argv[i];
2233 __argv[j++] = NULL;
2234
2235 execvp("/bin/sh", (char **)__argv);
2236 free(__argv);
2237 exit(-1);
2238 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002239
Tom Zanussicf4fee52010-03-03 01:04:33 -06002240 if (!script_name)
2241 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002242
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002243 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002244 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002245 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002246
Jiri Olsae90debd2013-12-09 11:02:50 +01002247 if (header || header_only) {
2248 perf_session__fprintf_info(session, stdout, show_full_info);
2249 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002250 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002251 }
2252
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002253 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002254 goto out_delete;
2255
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002256 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002257 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002258
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002259 session->itrace_synth_opts = &itrace_synth_opts;
2260
Anton Blanchard5d67be92011-07-04 21:57:50 +10002261 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002262 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2263 if (err < 0)
2264 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002265 }
2266
David Ahern1424dc92011-03-09 22:23:28 -07002267 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002268 symbol_conf.use_callchain = true;
2269 else
2270 symbol_conf.use_callchain = false;
2271
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002272 if (session->tevent.pevent &&
2273 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002274 machine__resolve_kernel_addr,
2275 &session->machines.host) < 0) {
2276 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2277 return -1;
2278 }
2279
Tom Zanussi956ffd02009-11-25 01:15:46 -06002280 if (generate_script_lang) {
2281 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002282 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002283
David Ahern2c9e45f72011-03-17 10:03:21 -06002284 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002285 fprintf(stderr,
2286 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002287 err = -EINVAL;
2288 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002289 }
2290
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002291 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002292 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002293 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002294 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002295 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002296 }
2297
2298 err = fstat(input, &perf_stat);
2299 if (err < 0) {
2300 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002301 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002302 }
2303
2304 if (!perf_stat.st_size) {
2305 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002306 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002307 }
2308
2309 scripting_ops = script_spec__lookup(generate_script_lang);
2310 if (!scripting_ops) {
2311 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002312 err = -ENOENT;
2313 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002314 }
2315
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002316 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002317 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002318 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002319 }
2320
2321 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002322 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002323 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002324 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002325 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002326 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002327 }
2328
David Ahern9cbdb702011-04-06 21:54:20 -06002329
2330 err = perf_session__check_output_opt(session);
2331 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002332 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002333
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002334 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002335
Adrian Hunterd445dd22014-08-15 22:08:37 +03002336 flush_scripting();
2337
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002338out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002339 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002340 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002341
2342 if (script_started)
2343 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002344out:
2345 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002346}