blob: f4caf48982457e764b15f9dc569f007391c6d609 [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
Andrea Gelminib7eead82010-08-05 15:51:38 +02003#include "perf.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02004#include "util/cache.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +02005#include "util/debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06006#include <subcmd/exec-cmd.h>
Andrea Gelminib7eead82010-08-05 15:51:38 +02007#include "util/header.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Jiri Olsacfc88742016-01-05 22:09:06 +010021#include "util/cpumap.h"
22#include "util/thread_map.h"
23#include "util/stat.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100024#include <linux/bitmap.h>
Jiri Olsacfc88742016-01-05 22:09:06 +010025#include "asm/bug.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020026
Tom Zanussi956ffd02009-11-25 01:15:46 -060027static char const *script_name;
28static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020029static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020030static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020031static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070032static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090033static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010034static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030035static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030036static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100037static const char *cpu_list;
38static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Jiri Olsa91a2c3d2016-01-05 22:09:07 +010039static struct perf_stat_config stat_config;
Tom Zanussi956ffd02009-11-25 01:15:46 -060040
Adrian Hunter44cbe722015-09-25 16:15:50 +030041unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030042
David Ahern745f43e2011-03-09 22:23:26 -070043enum perf_output_field {
44 PERF_OUTPUT_COMM = 1U << 0,
45 PERF_OUTPUT_TID = 1U << 1,
46 PERF_OUTPUT_PID = 1U << 2,
47 PERF_OUTPUT_TIME = 1U << 3,
48 PERF_OUTPUT_CPU = 1U << 4,
49 PERF_OUTPUT_EVNAME = 1U << 5,
50 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060051 PERF_OUTPUT_IP = 1U << 7,
52 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060053 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060054 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090055 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020056 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaae2014-08-25 16:45:42 +020057 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020058 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020059 PERF_OUTPUT_BRSTACK = 1U << 15,
60 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
Jiri Olsa94ddddf2016-02-15 09:34:51 +010061 PERF_OUTPUT_DATA_SRC = 1U << 17,
62 PERF_OUTPUT_WEIGHT = 1U << 18,
David Ahern745f43e2011-03-09 22:23:26 -070063};
64
65struct output_option {
66 const char *str;
67 enum perf_output_field field;
68} all_output_options[] = {
69 {.str = "comm", .field = PERF_OUTPUT_COMM},
70 {.str = "tid", .field = PERF_OUTPUT_TID},
71 {.str = "pid", .field = PERF_OUTPUT_PID},
72 {.str = "time", .field = PERF_OUTPUT_TIME},
73 {.str = "cpu", .field = PERF_OUTPUT_CPU},
74 {.str = "event", .field = PERF_OUTPUT_EVNAME},
75 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060076 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070077 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060078 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060079 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090080 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020081 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +020082 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020083 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +020084 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
85 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
Jiri Olsa94ddddf2016-02-15 09:34:51 +010086 {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
87 {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
David Ahern745f43e2011-03-09 22:23:26 -070088};
89
90/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060091static struct {
92 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060093 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040094 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060095 u64 fields;
96 u64 invalid_fields;
97} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070098
David Ahern2c9e45f72011-03-17 10:03:21 -060099 [PERF_TYPE_HARDWARE] = {
100 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -0700101
David Ahern2c9e45f72011-03-17 10:03:21 -0600102 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
103 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600104 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200105 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
106 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600107
108 .invalid_fields = PERF_OUTPUT_TRACE,
109 },
110
111 [PERF_TYPE_SOFTWARE] = {
112 .user_set = false,
113
114 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
115 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600116 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200117 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
118 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600119
120 .invalid_fields = PERF_OUTPUT_TRACE,
121 },
122
123 [PERF_TYPE_TRACEPOINT] = {
124 .user_set = false,
125
126 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
127 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
128 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
129 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700130
131 [PERF_TYPE_RAW] = {
132 .user_set = false,
133
134 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
135 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600136 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200137 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
Jiri Olsaff7b1912016-02-15 09:34:52 +0100138 PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
139 PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700140
141 .invalid_fields = PERF_OUTPUT_TRACE,
142 },
Wang Nan27cfef02015-12-08 02:25:43 +0000143
144 [PERF_TYPE_BREAKPOINT] = {
145 .user_set = false,
146
147 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
148 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
149 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
150 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
151 PERF_OUTPUT_PERIOD,
152
153 .invalid_fields = PERF_OUTPUT_TRACE,
154 },
David Ahern1424dc92011-03-09 22:23:28 -0700155};
David Ahern745f43e2011-03-09 22:23:26 -0700156
David Ahern2c9e45f72011-03-17 10:03:21 -0600157static bool output_set_by_user(void)
158{
159 int j;
160 for (j = 0; j < PERF_TYPE_MAX; ++j) {
161 if (output[j].user_set)
162 return true;
163 }
164 return false;
165}
David Ahern745f43e2011-03-09 22:23:26 -0700166
David Ahern9cbdb702011-04-06 21:54:20 -0600167static const char *output_field2str(enum perf_output_field field)
168{
169 int i, imax = ARRAY_SIZE(all_output_options);
170 const char *str = "";
171
172 for (i = 0; i < imax; ++i) {
173 if (all_output_options[i].field == field) {
174 str = all_output_options[i].str;
175 break;
176 }
177 }
178 return str;
179}
180
David Ahern2c9e45f72011-03-17 10:03:21 -0600181#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700182
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300183static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
184 u64 sample_type, const char *sample_msg,
185 enum perf_output_field field,
186 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700187{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300188 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600189 int type = attr->type;
190 const char *evname;
191
192 if (attr->sample_type & sample_type)
193 return 0;
194
195 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300196 if (allow_user_set)
197 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300198 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600199 pr_err("Samples for '%s' event do not have %s attribute set. "
200 "Cannot print '%s' field.\n",
201 evname, sample_msg, output_field2str(field));
202 return -1;
203 }
204
205 /* user did not ask for it explicitly so remove from the default list */
206 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300207 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600208 pr_debug("Samples for '%s' event do not have %s attribute set. "
209 "Skipping '%s' field.\n",
210 evname, sample_msg, output_field2str(field));
211
212 return 0;
213}
214
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300215static int perf_evsel__check_stype(struct perf_evsel *evsel,
216 u64 sample_type, const char *sample_msg,
217 enum perf_output_field field)
218{
219 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
220 false);
221}
222
David Ahern9cbdb702011-04-06 21:54:20 -0600223static int perf_evsel__check_attr(struct perf_evsel *evsel,
224 struct perf_session *session)
225{
226 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300227 bool allow_user_set;
228
Jiri Olsae099eba2016-01-05 22:09:09 +0100229 if (perf_header__has_feat(&session->header, HEADER_STAT))
230 return 0;
231
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300232 allow_user_set = perf_header__has_feat(&session->header,
233 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600234
David Ahern1424dc92011-03-09 22:23:28 -0700235 if (PRINT_FIELD(TRACE) &&
236 !perf_session__has_traces(session, "record -R"))
237 return -EINVAL;
238
David Ahern787bef12011-05-27 14:28:43 -0600239 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300240 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
241 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700242 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700243 }
David Ahern7cec0922011-05-30 13:08:23 -0600244
245 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300246 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
247 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600248 return -EINVAL;
249
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100250 if (PRINT_FIELD(DATA_SRC) &&
251 perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
252 PERF_OUTPUT_DATA_SRC))
253 return -EINVAL;
254
255 if (PRINT_FIELD(WEIGHT) &&
256 perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
257 PERF_OUTPUT_WEIGHT))
258 return -EINVAL;
259
David Ahern7cec0922011-05-30 13:08:23 -0600260 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
261 pr_err("Display of symbols requested but neither sample IP nor "
262 "sample address\nis selected. Hence, no addresses to convert "
263 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600264 return -EINVAL;
265 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900266 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
267 pr_err("Display of offsets requested but symbol is not"
268 "selected.\n");
269 return -EINVAL;
270 }
David Ahern7cec0922011-05-30 13:08:23 -0600271 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
272 pr_err("Display of DSO requested but neither sample IP nor "
273 "sample address\nis selected. Hence, no addresses to convert "
274 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600275 return -EINVAL;
276 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200277 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
278 pr_err("Display of source line number requested but sample IP is not\n"
279 "selected. Hence, no address to lookup the source line number.\n");
280 return -EINVAL;
281 }
David Ahern1424dc92011-03-09 22:23:28 -0700282
283 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300284 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
285 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700286 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700287
288 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300289 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
290 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700291 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700292
293 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300294 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
295 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700296 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600297
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200298 if (PRINT_FIELD(PERIOD) &&
299 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
300 PERF_OUTPUT_PERIOD))
301 return -EINVAL;
302
Stephane Eranianfc36f942015-08-31 18:41:10 +0200303 if (PRINT_FIELD(IREGS) &&
304 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
305 PERF_OUTPUT_IREGS))
306 return -EINVAL;
307
David Ahern9cbdb702011-04-06 21:54:20 -0600308 return 0;
309}
310
Adrian Hunter7ea95722013-11-01 15:51:30 +0200311static void set_print_ip_opts(struct perf_event_attr *attr)
312{
313 unsigned int type = attr->type;
314
315 output[type].print_ip_opts = 0;
316 if (PRINT_FIELD(IP))
317 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
318
319 if (PRINT_FIELD(SYM))
320 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
321
322 if (PRINT_FIELD(DSO))
323 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
324
325 if (PRINT_FIELD(SYMOFFSET))
326 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200327
328 if (PRINT_FIELD(SRCLINE))
329 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200330}
331
David Ahern9cbdb702011-04-06 21:54:20 -0600332/*
333 * verify all user requested events exist and the samples
334 * have the expected data
335 */
336static int perf_session__check_output_opt(struct perf_session *session)
337{
338 int j;
339 struct perf_evsel *evsel;
340
341 for (j = 0; j < PERF_TYPE_MAX; ++j) {
342 evsel = perf_session__find_first_evtype(session, j);
343
344 /*
345 * even if fields is set to 0 (ie., show nothing) event must
346 * exist if user explicitly includes it on the command line
347 */
348 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
349 pr_err("%s events do not exist. "
350 "Remove corresponding -f option to proceed.\n",
351 event_type(j));
352 return -1;
353 }
354
355 if (evsel && output[j].fields &&
356 perf_evsel__check_attr(evsel, session))
357 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400358
359 if (evsel == NULL)
360 continue;
361
Adrian Hunter7ea95722013-11-01 15:51:30 +0200362 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700363 }
364
Adrian Hunter98526ee2014-07-31 09:00:59 +0300365 if (!no_callchain) {
366 bool use_callchain = false;
367
368 evlist__for_each(session->evlist, evsel) {
369 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
370 use_callchain = true;
371 break;
372 }
373 }
374 if (!use_callchain)
375 symbol_conf.use_callchain = false;
376 }
377
David Ahern80b8b492013-11-19 21:07:37 -0700378 /*
379 * set default for tracepoints to print symbols only
380 * if callchains are present
381 */
382 if (symbol_conf.use_callchain &&
383 !output[PERF_TYPE_TRACEPOINT].user_set) {
384 struct perf_event_attr *attr;
385
386 j = PERF_TYPE_TRACEPOINT;
387 evsel = perf_session__find_first_evtype(session, j);
388 if (evsel == NULL)
389 goto out;
390
391 attr = &evsel->attr;
392
393 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
394 output[j].fields |= PERF_OUTPUT_IP;
395 output[j].fields |= PERF_OUTPUT_SYM;
396 output[j].fields |= PERF_OUTPUT_DSO;
397 set_print_ip_opts(attr);
398 }
399 }
400
401out:
David Ahern1424dc92011-03-09 22:23:28 -0700402 return 0;
403}
David Ahern745f43e2011-03-09 22:23:26 -0700404
Stephane Eranianfc36f942015-08-31 18:41:10 +0200405static void print_sample_iregs(union perf_event *event __maybe_unused,
406 struct perf_sample *sample,
407 struct thread *thread __maybe_unused,
408 struct perf_event_attr *attr)
409{
410 struct regs_dump *regs = &sample->intr_regs;
411 uint64_t mask = attr->sample_regs_intr;
412 unsigned i = 0, r;
413
414 if (!regs)
415 return;
416
417 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
418 u64 val = regs->regs[i++];
419 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
420 }
421}
422
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300423static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700424 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300425 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700426{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300427 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700428 unsigned long secs;
429 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700430 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700431
David Ahern745f43e2011-03-09 22:23:26 -0700432 if (PRINT_FIELD(COMM)) {
433 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200434 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600435 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200436 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700437 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200438 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700439 }
David Ahernc70c94b2011-03-09 22:23:25 -0700440
David Ahern745f43e2011-03-09 22:23:26 -0700441 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
442 printf("%5d/%-5d ", sample->pid, sample->tid);
443 else if (PRINT_FIELD(PID))
444 printf("%5d ", sample->pid);
445 else if (PRINT_FIELD(TID))
446 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700447
David Ahern745f43e2011-03-09 22:23:26 -0700448 if (PRINT_FIELD(CPU)) {
449 if (latency_format)
450 printf("%3d ", sample->cpu);
451 else
452 printf("[%03d] ", sample->cpu);
453 }
David Ahernc70c94b2011-03-09 22:23:25 -0700454
David Ahern745f43e2011-03-09 22:23:26 -0700455 if (PRINT_FIELD(TIME)) {
456 nsecs = sample->time;
457 secs = nsecs / NSECS_PER_SEC;
458 nsecs -= secs * NSECS_PER_SEC;
459 usecs = nsecs / NSECS_PER_USEC;
Adrian Hunter83e19862015-09-25 16:15:36 +0300460 if (nanosecs)
461 printf("%5lu.%09llu: ", secs, nsecs);
462 else
463 printf("%5lu.%06lu: ", secs, usecs);
David Ahern745f43e2011-03-09 22:23:26 -0700464 }
David Ahernc70c94b2011-03-09 22:23:25 -0700465}
466
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200467static inline char
468mispred_str(struct branch_entry *br)
469{
470 if (!(br->flags.mispred || br->flags.predicted))
471 return '-';
472
473 return br->flags.predicted ? 'P' : 'M';
474}
475
476static void print_sample_brstack(union perf_event *event __maybe_unused,
477 struct perf_sample *sample,
478 struct thread *thread __maybe_unused,
479 struct perf_event_attr *attr __maybe_unused)
480{
481 struct branch_stack *br = sample->branch_stack;
482 u64 i;
483
484 if (!(br && br->nr))
485 return;
486
487 for (i = 0; i < br->nr; i++) {
488 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
489 br->entries[i].from,
490 br->entries[i].to,
491 mispred_str( br->entries + i),
492 br->entries[i].flags.in_tx? 'X' : '-',
493 br->entries[i].flags.abort? 'A' : '-',
494 br->entries[i].flags.cycles);
495 }
496}
497
498static void print_sample_brstacksym(union perf_event *event __maybe_unused,
499 struct perf_sample *sample,
500 struct thread *thread __maybe_unused,
501 struct perf_event_attr *attr __maybe_unused)
502{
503 struct branch_stack *br = sample->branch_stack;
504 struct addr_location alf, alt;
505 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
506 u64 i, from, to;
507
508 if (!(br && br->nr))
509 return;
510
511 for (i = 0; i < br->nr; i++) {
512
513 memset(&alf, 0, sizeof(alf));
514 memset(&alt, 0, sizeof(alt));
515 from = br->entries[i].from;
516 to = br->entries[i].to;
517
518 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
519 if (alf.map)
520 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
521
522 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
523 if (alt.map)
524 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
525
526 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
527 putchar('/');
528 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
529 printf("/%c/%c/%c/%d ",
530 mispred_str( br->entries + i),
531 br->entries[i].flags.in_tx? 'X' : '-',
532 br->entries[i].flags.abort? 'A' : '-',
533 br->entries[i].flags.cycles);
534 }
535}
536
537
David Ahern7cec0922011-05-30 13:08:23 -0600538static void print_sample_addr(union perf_event *event,
539 struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600540 struct thread *thread,
541 struct perf_event_attr *attr)
542{
543 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600544
545 printf("%16" PRIx64, sample->addr);
546
547 if (!sample_addr_correlates_sym(attr))
548 return;
549
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300550 perf_event__preprocess_sample_addr(event, sample, thread, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600551
552 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900553 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900554 if (PRINT_FIELD(SYMOFFSET))
555 symbol__fprintf_symname_offs(al.sym, &al, stdout);
556 else
557 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600558 }
559
560 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900561 printf(" (");
562 map__fprintf_dsoname(al.map, stdout);
563 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600564 }
565}
566
Akihiro Nagai95582592012-01-30 13:43:09 +0900567static void print_sample_bts(union perf_event *event,
568 struct perf_sample *sample,
569 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200570 struct thread *thread,
571 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900572{
573 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300574 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900575
576 /* print branch_from information */
577 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300578 unsigned int print_opts = output[attr->type].print_ip_opts;
579
580 if (symbol_conf.use_callchain && sample->callchain) {
Akihiro Nagai95582592012-01-30 13:43:09 +0900581 printf("\n");
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300582 } else {
583 printf(" ");
584 if (print_opts & PRINT_IP_OPT_SRCLINE) {
585 print_srcline_last = true;
586 print_opts &= ~PRINT_IP_OPT_SRCLINE;
587 }
588 }
589 perf_evsel__print_ip(evsel, sample, al, print_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300590 scripting_max_stack);
Akihiro Nagai95582592012-01-30 13:43:09 +0900591 }
592
Akihiro Nagai95582592012-01-30 13:43:09 +0900593 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300594 if (PRINT_FIELD(ADDR) ||
595 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300596 !output[attr->type].user_set)) {
597 printf(" => ");
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300598 print_sample_addr(event, sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300599 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900600
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300601 if (print_srcline_last)
602 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
603
Akihiro Nagai95582592012-01-30 13:43:09 +0900604 printf("\n");
605}
606
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300607static void print_sample_flags(u32 flags)
608{
609 const char *chars = PERF_IP_FLAG_CHARS;
610 const int n = strlen(PERF_IP_FLAG_CHARS);
611 char str[33];
612 int i, pos = 0;
613
614 for (i = 0; i < n; i++, flags >>= 1) {
615 if (flags & 1)
616 str[pos++] = chars[i];
617 }
618 for (; i < 32; i++, flags >>= 1) {
619 if (flags & 1)
620 str[pos++] = '?';
621 }
622 str[pos] = 0;
623 printf(" %-4s ", str);
624}
625
Jiri Olsa809e9422015-11-26 18:55:21 +0100626struct perf_script {
627 struct perf_tool tool;
628 struct perf_session *session;
629 bool show_task_events;
630 bool show_mmap_events;
631 bool show_switch_events;
Jiri Olsacfc88742016-01-05 22:09:06 +0100632 bool allocated;
633 struct cpu_map *cpus;
634 struct thread_map *threads;
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100635 int name_width;
Jiri Olsa809e9422015-11-26 18:55:21 +0100636};
637
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100638static int perf_evlist__max_name_len(struct perf_evlist *evlist)
639{
640 struct perf_evsel *evsel;
641 int max = 0;
642
643 evlist__for_each(evlist, evsel) {
644 int len = strlen(perf_evsel__name(evsel));
645
646 max = MAX(len, max);
647 }
648
649 return max;
650}
651
652static void process_event(struct perf_script *script, union perf_event *event,
Jiri Olsa809e9422015-11-26 18:55:21 +0100653 struct perf_sample *sample, struct perf_evsel *evsel,
654 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700655{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300656 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300657 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700658
David Ahern2c9e45f72011-03-17 10:03:21 -0600659 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700660 return;
661
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300662 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700663
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200664 if (PRINT_FIELD(PERIOD))
665 printf("%10" PRIu64 " ", sample->period);
666
Namhyung Kime944d3d2013-11-18 14:34:52 +0900667 if (PRINT_FIELD(EVNAME)) {
668 const char *evname = perf_evsel__name(evsel);
Jiri Olsa9cdbc402016-01-07 10:14:05 +0100669
670 if (!script->name_width)
671 script->name_width = perf_evlist__max_name_len(script->session->evlist);
672
673 printf("%*s: ", script->name_width,
674 evname ? evname : "[unknown]");
Namhyung Kime944d3d2013-11-18 14:34:52 +0900675 }
676
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300677 if (print_flags)
678 print_sample_flags(sample->flags);
679
Akihiro Nagai95582592012-01-30 13:43:09 +0900680 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300681 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900682 return;
683 }
684
David Ahern745f43e2011-03-09 22:23:26 -0700685 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300686 event_format__print(evsel->tp_format, sample->cpu,
687 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600688 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300689 print_sample_addr(event, sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600690
Jiri Olsa94ddddf2016-02-15 09:34:51 +0100691 if (PRINT_FIELD(DATA_SRC))
692 printf("%16" PRIx64, sample->data_src);
693
694 if (PRINT_FIELD(WEIGHT))
695 printf("%16" PRIu64, sample->weight);
696
David Ahern787bef12011-05-27 14:28:43 -0600697 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700698 if (!symbol_conf.use_callchain)
699 printf(" ");
700 else
701 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400702
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300703 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400704 output[attr->type].print_ip_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300705 scripting_max_stack);
David Ahernc0230b22011-03-09 22:23:27 -0700706 }
707
Stephane Eranianfc36f942015-08-31 18:41:10 +0200708 if (PRINT_FIELD(IREGS))
709 print_sample_iregs(event, sample, thread, attr);
710
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200711 if (PRINT_FIELD(BRSTACK))
712 print_sample_brstack(event, sample, thread, attr);
713 else if (PRINT_FIELD(BRSTACKSYM))
714 print_sample_brstacksym(event, sample, thread, attr);
715
David Ahernc70c94b2011-03-09 22:23:25 -0700716 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700717}
718
Tom Zanussi956ffd02009-11-25 01:15:46 -0600719static struct scripting_ops *scripting_ops;
720
Jiri Olsa36e33c52016-01-06 11:49:56 +0100721static void __process_stat(struct perf_evsel *counter, u64 tstamp)
722{
723 int nthreads = thread_map__nr(counter->threads);
724 int ncpus = perf_evsel__nr_cpus(counter);
725 int cpu, thread;
726 static int header_printed;
727
728 if (counter->system_wide)
729 nthreads = 1;
730
731 if (!header_printed) {
732 printf("%3s %8s %15s %15s %15s %15s %s\n",
733 "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
734 header_printed = 1;
735 }
736
737 for (thread = 0; thread < nthreads; thread++) {
738 for (cpu = 0; cpu < ncpus; cpu++) {
739 struct perf_counts_values *counts;
740
741 counts = perf_counts(counter->counts, cpu, thread);
742
743 printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
744 counter->cpus->map[cpu],
745 thread_map__pid(counter->threads, thread),
746 counts->val,
747 counts->ena,
748 counts->run,
749 tstamp,
750 perf_evsel__name(counter));
751 }
752 }
753}
754
Jiri Olsae099eba2016-01-05 22:09:09 +0100755static void process_stat(struct perf_evsel *counter, u64 tstamp)
756{
757 if (scripting_ops && scripting_ops->process_stat)
758 scripting_ops->process_stat(&stat_config, counter, tstamp);
Jiri Olsa36e33c52016-01-06 11:49:56 +0100759 else
760 __process_stat(counter, tstamp);
Jiri Olsae099eba2016-01-05 22:09:09 +0100761}
762
763static void process_stat_interval(u64 tstamp)
764{
765 if (scripting_ops && scripting_ops->process_stat_interval)
766 scripting_ops->process_stat_interval(tstamp);
767}
768
Tom Zanussi956ffd02009-11-25 01:15:46 -0600769static void setup_scripting(void)
770{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600771 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600772 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600773}
774
Adrian Hunterd445dd22014-08-15 22:08:37 +0300775static int flush_scripting(void)
776{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100777 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300778}
779
Tom Zanussi956ffd02009-11-25 01:15:46 -0600780static int cleanup_scripting(void)
781{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100782 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500783
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100784 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600785}
786
Jiri Olsa809e9422015-11-26 18:55:21 +0100787static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200788 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200789 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300790 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200791 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200792{
Jiri Olsa809e9422015-11-26 18:55:21 +0100793 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -0700794 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200795
David Ahern1424dc92011-03-09 22:23:28 -0700796 if (debug_mode) {
797 if (sample->time < last_timestamp) {
798 pr_err("Samples misordered, previous: %" PRIu64
799 " this: %" PRIu64 "\n", last_timestamp,
800 sample->time);
801 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200802 }
David Ahern1424dc92011-03-09 22:23:28 -0700803 last_timestamp = sample->time;
804 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200805 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000806
Adrian Huntere44baa32013-08-08 14:32:25 +0300807 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700808 pr_err("problem processing %d event, skipping it.\n",
809 event->header.type);
810 return -1;
811 }
812
813 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300814 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700815
Anton Blanchard5d67be92011-07-04 21:57:50 +1000816 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300817 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000818
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100819 if (scripting_ops)
820 scripting_ops->process_event(event, sample, evsel, &al);
821 else
Jiri Olsa809e9422015-11-26 18:55:21 +0100822 process_event(scr, event, sample, evsel, &al);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100823
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300824out_put:
825 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200826 return 0;
827}
828
Adrian Hunter7ea95722013-11-01 15:51:30 +0200829static int process_attr(struct perf_tool *tool, union perf_event *event,
830 struct perf_evlist **pevlist)
831{
832 struct perf_script *scr = container_of(tool, struct perf_script, tool);
833 struct perf_evlist *evlist;
834 struct perf_evsel *evsel, *pos;
835 int err;
836
837 err = perf_event__process_attr(tool, event, pevlist);
838 if (err)
839 return err;
840
841 evlist = *pevlist;
842 evsel = perf_evlist__last(*pevlist);
843
844 if (evsel->attr.type >= PERF_TYPE_MAX)
845 return 0;
846
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300847 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200848 if (pos->attr.type == evsel->attr.type && pos != evsel)
849 return 0;
850 }
851
852 set_print_ip_opts(&evsel->attr);
853
Jiri Olsad2b5a312015-10-16 12:41:25 +0200854 if (evsel->attr.sample_type)
855 err = perf_evsel__check_attr(evsel, scr->session);
856
857 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200858}
859
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900860static int process_comm_event(struct perf_tool *tool,
861 union perf_event *event,
862 struct perf_sample *sample,
863 struct machine *machine)
864{
865 struct thread *thread;
866 struct perf_script *script = container_of(tool, struct perf_script, tool);
867 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300868 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900869 int ret = -1;
870
871 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
872 if (thread == NULL) {
873 pr_debug("problem processing COMM event, skipping it.\n");
874 return -1;
875 }
876
877 if (perf_event__process_comm(tool, event, sample, machine) < 0)
878 goto out;
879
880 if (!evsel->attr.sample_id_all) {
881 sample->cpu = 0;
882 sample->time = 0;
883 sample->tid = event->comm.tid;
884 sample->pid = event->comm.pid;
885 }
886 print_sample_start(sample, thread, evsel);
887 perf_event__fprintf(event, stdout);
888 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900889out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300890 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900891 return ret;
892}
893
894static int process_fork_event(struct perf_tool *tool,
895 union perf_event *event,
896 struct perf_sample *sample,
897 struct machine *machine)
898{
899 struct thread *thread;
900 struct perf_script *script = container_of(tool, struct perf_script, tool);
901 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300902 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900903
904 if (perf_event__process_fork(tool, event, sample, machine) < 0)
905 return -1;
906
907 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
908 if (thread == NULL) {
909 pr_debug("problem processing FORK event, skipping it.\n");
910 return -1;
911 }
912
913 if (!evsel->attr.sample_id_all) {
914 sample->cpu = 0;
915 sample->time = event->fork.time;
916 sample->tid = event->fork.tid;
917 sample->pid = event->fork.pid;
918 }
919 print_sample_start(sample, thread, evsel);
920 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300921 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900922
923 return 0;
924}
925static int process_exit_event(struct perf_tool *tool,
926 union perf_event *event,
927 struct perf_sample *sample,
928 struct machine *machine)
929{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300930 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900931 struct thread *thread;
932 struct perf_script *script = container_of(tool, struct perf_script, tool);
933 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300934 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900935
936 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
937 if (thread == NULL) {
938 pr_debug("problem processing EXIT event, skipping it.\n");
939 return -1;
940 }
941
942 if (!evsel->attr.sample_id_all) {
943 sample->cpu = 0;
944 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +0300945 sample->tid = event->fork.tid;
946 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900947 }
948 print_sample_start(sample, thread, evsel);
949 perf_event__fprintf(event, stdout);
950
951 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300952 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900953
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300954 thread__put(thread);
955 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900956}
957
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900958static int process_mmap_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 Kimba1ddf42013-11-26 17:54:26 +0900967
968 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
969 return -1;
970
971 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
972 if (thread == NULL) {
973 pr_debug("problem processing MMAP event, skipping it.\n");
974 return -1;
975 }
976
977 if (!evsel->attr.sample_id_all) {
978 sample->cpu = 0;
979 sample->time = 0;
980 sample->tid = event->mmap.tid;
981 sample->pid = event->mmap.pid;
982 }
983 print_sample_start(sample, thread, evsel);
984 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300985 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900986 return 0;
987}
988
989static int process_mmap2_event(struct perf_tool *tool,
990 union perf_event *event,
991 struct perf_sample *sample,
992 struct machine *machine)
993{
994 struct thread *thread;
995 struct perf_script *script = container_of(tool, struct perf_script, tool);
996 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300997 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900998
999 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
1000 return -1;
1001
1002 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
1003 if (thread == NULL) {
1004 pr_debug("problem processing MMAP2 event, skipping it.\n");
1005 return -1;
1006 }
1007
1008 if (!evsel->attr.sample_id_all) {
1009 sample->cpu = 0;
1010 sample->time = 0;
1011 sample->tid = event->mmap2.tid;
1012 sample->pid = event->mmap2.pid;
1013 }
1014 print_sample_start(sample, thread, evsel);
1015 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001016 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001017 return 0;
1018}
1019
Adrian Hunter7c148982015-07-21 12:44:06 +03001020static int process_switch_event(struct perf_tool *tool,
1021 union perf_event *event,
1022 struct perf_sample *sample,
1023 struct machine *machine)
1024{
1025 struct thread *thread;
1026 struct perf_script *script = container_of(tool, struct perf_script, tool);
1027 struct perf_session *session = script->session;
1028 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1029
1030 if (perf_event__process_switch(tool, event, sample, machine) < 0)
1031 return -1;
1032
1033 thread = machine__findnew_thread(machine, sample->pid,
1034 sample->tid);
1035 if (thread == NULL) {
1036 pr_debug("problem processing SWITCH event, skipping it.\n");
1037 return -1;
1038 }
1039
1040 print_sample_start(sample, thread, evsel);
1041 perf_event__fprintf(event, stdout);
1042 thread__put(thread);
1043 return 0;
1044}
1045
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001046static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -05001047{
1048 session_done = 1;
1049}
1050
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001051static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001052{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001053 int ret;
1054
Tom Zanussic239da32010-04-01 23:59:18 -05001055 signal(SIGINT, sig_handler);
1056
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001057 /* override event processing functions */
1058 if (script->show_task_events) {
1059 script->tool.comm = process_comm_event;
1060 script->tool.fork = process_fork_event;
1061 script->tool.exit = process_exit_event;
1062 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001063 if (script->show_mmap_events) {
1064 script->tool.mmap = process_mmap_event;
1065 script->tool.mmap2 = process_mmap2_event;
1066 }
Adrian Hunter7c148982015-07-21 12:44:06 +03001067 if (script->show_switch_events)
1068 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001069
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03001070 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001071
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -02001072 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001073 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +02001074
1075 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001076}
1077
Tom Zanussi956ffd02009-11-25 01:15:46 -06001078struct script_spec {
1079 struct list_head node;
1080 struct scripting_ops *ops;
1081 char spec[0];
1082};
1083
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001084static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001085
1086static struct script_spec *script_spec__new(const char *spec,
1087 struct scripting_ops *ops)
1088{
1089 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1090
1091 if (s != NULL) {
1092 strcpy(s->spec, spec);
1093 s->ops = ops;
1094 }
1095
1096 return s;
1097}
1098
Tom Zanussi956ffd02009-11-25 01:15:46 -06001099static void script_spec__add(struct script_spec *s)
1100{
1101 list_add_tail(&s->node, &script_specs);
1102}
1103
1104static struct script_spec *script_spec__find(const char *spec)
1105{
1106 struct script_spec *s;
1107
1108 list_for_each_entry(s, &script_specs, node)
1109 if (strcasecmp(s->spec, spec) == 0)
1110 return s;
1111 return NULL;
1112}
1113
1114static struct script_spec *script_spec__findnew(const char *spec,
1115 struct scripting_ops *ops)
1116{
1117 struct script_spec *s = script_spec__find(spec);
1118
1119 if (s)
1120 return s;
1121
1122 s = script_spec__new(spec, ops);
1123 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +09001124 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001125
1126 script_spec__add(s);
1127
1128 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001129}
1130
1131int script_spec_register(const char *spec, struct scripting_ops *ops)
1132{
1133 struct script_spec *s;
1134
1135 s = script_spec__find(spec);
1136 if (s)
1137 return -1;
1138
1139 s = script_spec__findnew(spec, ops);
1140 if (!s)
1141 return -1;
1142
1143 return 0;
1144}
1145
1146static struct scripting_ops *script_spec__lookup(const char *spec)
1147{
1148 struct script_spec *s = script_spec__find(spec);
1149 if (!s)
1150 return NULL;
1151
1152 return s->ops;
1153}
1154
1155static void list_available_languages(void)
1156{
1157 struct script_spec *s;
1158
1159 fprintf(stderr, "\n");
1160 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001161 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001162
1163 list_for_each_entry(s, &script_specs, node)
1164 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1165
1166 fprintf(stderr, "\n");
1167}
1168
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001169static int parse_scriptname(const struct option *opt __maybe_unused,
1170 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001171{
1172 char spec[PATH_MAX];
1173 const char *script, *ext;
1174 int len;
1175
Tom Zanussif526d682010-01-27 02:27:52 -06001176 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001177 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001178 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001179 }
1180
1181 script = strchr(str, ':');
1182 if (script) {
1183 len = script - str;
1184 if (len >= PATH_MAX) {
1185 fprintf(stderr, "invalid language specifier");
1186 return -1;
1187 }
1188 strncpy(spec, str, len);
1189 spec[len] = '\0';
1190 scripting_ops = script_spec__lookup(spec);
1191 if (!scripting_ops) {
1192 fprintf(stderr, "invalid language specifier");
1193 return -1;
1194 }
1195 script++;
1196 } else {
1197 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001198 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001199 if (!ext) {
1200 fprintf(stderr, "invalid script extension");
1201 return -1;
1202 }
1203 scripting_ops = script_spec__lookup(++ext);
1204 if (!scripting_ops) {
1205 fprintf(stderr, "invalid script extension");
1206 return -1;
1207 }
1208 }
1209
1210 script_name = strdup(script);
1211
1212 return 0;
1213}
1214
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001215static int parse_output_fields(const struct option *opt __maybe_unused,
1216 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001217{
1218 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001219 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001220 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001221 int rc = 0;
1222 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001223 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001224
1225 if (!str)
1226 return -ENOMEM;
1227
David Ahern2c9e45f72011-03-17 10:03:21 -06001228 /* first word can state for which event type the user is specifying
1229 * the fields. If no type exists, the specified fields apply to all
1230 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001231 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001232 tok = strchr(str, ':');
1233 if (tok) {
1234 *tok = '\0';
1235 tok++;
1236 if (!strcmp(str, "hw"))
1237 type = PERF_TYPE_HARDWARE;
1238 else if (!strcmp(str, "sw"))
1239 type = PERF_TYPE_SOFTWARE;
1240 else if (!strcmp(str, "trace"))
1241 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001242 else if (!strcmp(str, "raw"))
1243 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001244 else if (!strcmp(str, "break"))
1245 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001246 else {
1247 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001248 rc = -EINVAL;
1249 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001250 }
1251
1252 if (output[type].user_set)
1253 pr_warning("Overriding previous field request for %s events.\n",
1254 event_type(type));
1255
1256 output[type].fields = 0;
1257 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001258 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001259
1260 } else {
1261 tok = str;
1262 if (strlen(str) == 0) {
1263 fprintf(stderr,
1264 "Cannot set fields to 'none' for all event types.\n");
1265 rc = -EINVAL;
1266 goto out;
1267 }
1268
1269 if (output_set_by_user())
1270 pr_warning("Overriding previous field request for all events.\n");
1271
1272 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1273 output[j].fields = 0;
1274 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001275 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001276 }
David Ahern1424dc92011-03-09 22:23:28 -07001277 }
1278
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001279 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001280 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001281 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001282 break;
David Ahern745f43e2011-03-09 22:23:26 -07001283 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001284 if (i == imax && strcmp(tok, "flags") == 0) {
1285 print_flags = true;
1286 continue;
1287 }
David Ahern745f43e2011-03-09 22:23:26 -07001288 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001289 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001290 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001291 goto out;
1292 }
1293
1294 if (type == -1) {
1295 /* add user option to all events types for
1296 * which it is valid
1297 */
1298 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1299 if (output[j].invalid_fields & all_output_options[i].field) {
1300 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1301 all_output_options[i].str, event_type(j));
1302 } else
1303 output[j].fields |= all_output_options[i].field;
1304 }
1305 } else {
1306 if (output[type].invalid_fields & all_output_options[i].field) {
1307 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1308 all_output_options[i].str, event_type(type));
1309
1310 rc = -EINVAL;
1311 goto out;
1312 }
1313 output[type].fields |= all_output_options[i].field;
1314 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001315 }
1316
1317 if (type >= 0) {
1318 if (output[type].fields == 0) {
1319 pr_debug("No fields requested for %s type. "
1320 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001321 }
David Ahern1424dc92011-03-09 22:23:28 -07001322 }
David Ahern745f43e2011-03-09 22:23:26 -07001323
David Ahern2c9e45f72011-03-17 10:03:21 -06001324out:
David Ahern745f43e2011-03-09 22:23:26 -07001325 free(str);
1326 return rc;
1327}
1328
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001329/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1330static int is_directory(const char *base_path, const struct dirent *dent)
1331{
1332 char path[PATH_MAX];
1333 struct stat st;
1334
1335 sprintf(path, "%s/%s", base_path, dent->d_name);
1336 if (stat(path, &st))
1337 return 0;
1338
1339 return S_ISDIR(st.st_mode);
1340}
1341
1342#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001343 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1344 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001345 if ((lang_dirent.d_type == DT_DIR || \
1346 (lang_dirent.d_type == DT_UNKNOWN && \
1347 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001348 (strcmp(lang_dirent.d_name, ".")) && \
1349 (strcmp(lang_dirent.d_name, "..")))
1350
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001351#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001352 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1353 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001354 if (script_dirent.d_type != DT_DIR && \
1355 (script_dirent.d_type != DT_UNKNOWN || \
1356 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001357
1358
1359#define RECORD_SUFFIX "-record"
1360#define REPORT_SUFFIX "-report"
1361
1362struct script_desc {
1363 struct list_head node;
1364 char *name;
1365 char *half_liner;
1366 char *args;
1367};
1368
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001369static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001370
1371static struct script_desc *script_desc__new(const char *name)
1372{
1373 struct script_desc *s = zalloc(sizeof(*s));
1374
Tom Zanussib5b87312010-11-10 08:16:51 -06001375 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001376 s->name = strdup(name);
1377
1378 return s;
1379}
1380
1381static void script_desc__delete(struct script_desc *s)
1382{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001383 zfree(&s->name);
1384 zfree(&s->half_liner);
1385 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001386 free(s);
1387}
1388
1389static void script_desc__add(struct script_desc *s)
1390{
1391 list_add_tail(&s->node, &script_descs);
1392}
1393
1394static struct script_desc *script_desc__find(const char *name)
1395{
1396 struct script_desc *s;
1397
1398 list_for_each_entry(s, &script_descs, node)
1399 if (strcasecmp(s->name, name) == 0)
1400 return s;
1401 return NULL;
1402}
1403
1404static struct script_desc *script_desc__findnew(const char *name)
1405{
1406 struct script_desc *s = script_desc__find(name);
1407
1408 if (s)
1409 return s;
1410
1411 s = script_desc__new(name);
1412 if (!s)
1413 goto out_delete_desc;
1414
1415 script_desc__add(s);
1416
1417 return s;
1418
1419out_delete_desc:
1420 script_desc__delete(s);
1421
1422 return NULL;
1423}
1424
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001425static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001426{
1427 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001428 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001429
1430 if (strlen(str) > suffix_len) {
1431 p = str + strlen(str) - suffix_len;
1432 if (!strncmp(p, suffix, suffix_len))
1433 return p;
1434 }
1435
1436 return NULL;
1437}
1438
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001439static int read_script_info(struct script_desc *desc, const char *filename)
1440{
1441 char line[BUFSIZ], *p;
1442 FILE *fp;
1443
1444 fp = fopen(filename, "r");
1445 if (!fp)
1446 return -1;
1447
1448 while (fgets(line, sizeof(line), fp)) {
1449 p = ltrim(line);
1450 if (strlen(p) == 0)
1451 continue;
1452 if (*p != '#')
1453 continue;
1454 p++;
1455 if (strlen(p) && *p == '!')
1456 continue;
1457
1458 p = ltrim(p);
1459 if (strlen(p) && p[strlen(p) - 1] == '\n')
1460 p[strlen(p) - 1] = '\0';
1461
1462 if (!strncmp(p, "description:", strlen("description:"))) {
1463 p += strlen("description:");
1464 desc->half_liner = strdup(ltrim(p));
1465 continue;
1466 }
1467
1468 if (!strncmp(p, "args:", strlen("args:"))) {
1469 p += strlen("args:");
1470 desc->args = strdup(ltrim(p));
1471 continue;
1472 }
1473 }
1474
1475 fclose(fp);
1476
1477 return 0;
1478}
1479
Robert Richter38efb532011-11-25 11:38:40 +01001480static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1481{
1482 char *script_root, *str;
1483
1484 script_root = strdup(script_dirent->d_name);
1485 if (!script_root)
1486 return NULL;
1487
1488 str = (char *)ends_with(script_root, suffix);
1489 if (!str) {
1490 free(script_root);
1491 return NULL;
1492 }
1493
1494 *str = '\0';
1495 return script_root;
1496}
1497
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001498static int list_available_scripts(const struct option *opt __maybe_unused,
1499 const char *s __maybe_unused,
1500 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001501{
1502 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1503 char scripts_path[MAXPATHLEN];
1504 DIR *scripts_dir, *lang_dir;
1505 char script_path[MAXPATHLEN];
1506 char lang_path[MAXPATHLEN];
1507 struct script_desc *desc;
1508 char first_half[BUFSIZ];
1509 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001510
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001511 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001512
1513 scripts_dir = opendir(scripts_path);
1514 if (!scripts_dir)
1515 return -1;
1516
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001517 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001518 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1519 lang_dirent.d_name);
1520 lang_dir = opendir(lang_path);
1521 if (!lang_dir)
1522 continue;
1523
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001524 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001525 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1526 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001527 desc = script_desc__findnew(script_root);
1528 snprintf(script_path, MAXPATHLEN, "%s/%s",
1529 lang_path, script_dirent.d_name);
1530 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001531 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001532 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001533 }
1534 }
1535
1536 fprintf(stdout, "List of available trace scripts:\n");
1537 list_for_each_entry(desc, &script_descs, node) {
1538 sprintf(first_half, "%s %s", desc->name,
1539 desc->args ? desc->args : "");
1540 fprintf(stdout, " %-36s %s\n", first_half,
1541 desc->half_liner ? desc->half_liner : "");
1542 }
1543
1544 exit(0);
1545}
1546
Feng Tange5f37052012-09-07 16:42:26 +08001547/*
Feng Tang49e639e2012-10-30 11:56:03 +08001548 * Some scripts specify the required events in their "xxx-record" file,
1549 * this function will check if the events in perf.data match those
1550 * mentioned in the "xxx-record".
1551 *
1552 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1553 * which is covered well now. And new parsing code should be added to
1554 * cover the future complexing formats like event groups etc.
1555 */
1556static int check_ev_match(char *dir_name, char *scriptname,
1557 struct perf_session *session)
1558{
1559 char filename[MAXPATHLEN], evname[128];
1560 char line[BUFSIZ], *p;
1561 struct perf_evsel *pos;
1562 int match, len;
1563 FILE *fp;
1564
1565 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1566
1567 fp = fopen(filename, "r");
1568 if (!fp)
1569 return -1;
1570
1571 while (fgets(line, sizeof(line), fp)) {
1572 p = ltrim(line);
1573 if (*p == '#')
1574 continue;
1575
1576 while (strlen(p)) {
1577 p = strstr(p, "-e");
1578 if (!p)
1579 break;
1580
1581 p += 2;
1582 p = ltrim(p);
1583 len = strcspn(p, " \t");
1584 if (!len)
1585 break;
1586
1587 snprintf(evname, len + 1, "%s", p);
1588
1589 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001590 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001591 if (!strcmp(perf_evsel__name(pos), evname)) {
1592 match = 1;
1593 break;
1594 }
1595 }
1596
1597 if (!match) {
1598 fclose(fp);
1599 return -1;
1600 }
1601 }
1602 }
1603
1604 fclose(fp);
1605 return 0;
1606}
1607
1608/*
Feng Tange5f37052012-09-07 16:42:26 +08001609 * Return -1 if none is found, otherwise the actual scripts number.
1610 *
1611 * Currently the only user of this function is the script browser, which
1612 * will list all statically runnable scripts, select one, execute it and
1613 * show the output in a perf browser.
1614 */
1615int find_scripts(char **scripts_array, char **scripts_path_array)
1616{
1617 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001618 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001619 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001620 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001621 struct perf_data_file file = {
1622 .path = input_name,
1623 .mode = PERF_DATA_MODE_READ,
1624 };
Feng Tange5f37052012-09-07 16:42:26 +08001625 char *temp;
1626 int i = 0;
1627
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001628 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001629 if (!session)
1630 return -1;
1631
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001632 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08001633
1634 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001635 if (!scripts_dir) {
1636 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001637 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001638 }
Feng Tange5f37052012-09-07 16:42:26 +08001639
1640 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1641 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1642 lang_dirent.d_name);
1643#ifdef NO_LIBPERL
1644 if (strstr(lang_path, "perl"))
1645 continue;
1646#endif
1647#ifdef NO_LIBPYTHON
1648 if (strstr(lang_path, "python"))
1649 continue;
1650#endif
1651
1652 lang_dir = opendir(lang_path);
1653 if (!lang_dir)
1654 continue;
1655
1656 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1657 /* Skip those real time scripts: xxxtop.p[yl] */
1658 if (strstr(script_dirent.d_name, "top."))
1659 continue;
1660 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1661 script_dirent.d_name);
1662 temp = strchr(script_dirent.d_name, '.');
1663 snprintf(scripts_array[i],
1664 (temp - script_dirent.d_name) + 1,
1665 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001666
1667 if (check_ev_match(lang_path,
1668 scripts_array[i], session))
1669 continue;
1670
Feng Tange5f37052012-09-07 16:42:26 +08001671 i++;
1672 }
Feng Tang49e639e2012-10-30 11:56:03 +08001673 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001674 }
1675
Feng Tang49e639e2012-10-30 11:56:03 +08001676 closedir(scripts_dir);
1677 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001678 return i;
1679}
1680
Tom Zanussi38752942009-12-15 02:53:39 -06001681static char *get_script_path(const char *script_root, const char *suffix)
1682{
1683 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1684 char scripts_path[MAXPATHLEN];
1685 char script_path[MAXPATHLEN];
1686 DIR *scripts_dir, *lang_dir;
1687 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001688 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001689
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001690 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06001691
1692 scripts_dir = opendir(scripts_path);
1693 if (!scripts_dir)
1694 return NULL;
1695
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001696 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001697 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1698 lang_dirent.d_name);
1699 lang_dir = opendir(lang_path);
1700 if (!lang_dir)
1701 continue;
1702
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001703 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001704 __script_root = get_script_root(&script_dirent, suffix);
1705 if (__script_root && !strcmp(script_root, __script_root)) {
1706 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001707 closedir(lang_dir);
1708 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001709 snprintf(script_path, MAXPATHLEN, "%s/%s",
1710 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001711 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001712 }
1713 free(__script_root);
1714 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001715 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001716 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001717 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001718
Robert Richter38efb532011-11-25 11:38:40 +01001719 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001720}
1721
Tom Zanussib5b87312010-11-10 08:16:51 -06001722static bool is_top_script(const char *script_path)
1723{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001724 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001725}
1726
1727static int has_required_arg(char *script_path)
1728{
1729 struct script_desc *desc;
1730 int n_args = 0;
1731 char *p;
1732
1733 desc = script_desc__new(NULL);
1734
1735 if (read_script_info(desc, script_path))
1736 goto out;
1737
1738 if (!desc->args)
1739 goto out;
1740
1741 for (p = desc->args; *p; p++)
1742 if (*p == '<')
1743 n_args++;
1744out:
1745 script_desc__delete(desc);
1746
1747 return n_args;
1748}
1749
David Ahernd54b1a92012-08-26 12:24:46 -06001750static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001751{
1752 char **__argv = malloc(sizeof(const char *) * argc);
1753
David Ahernd54b1a92012-08-26 12:24:46 -06001754 if (!__argv) {
1755 pr_err("malloc failed\n");
1756 return -1;
1757 }
1758
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001759 memcpy(__argv, argv, sizeof(const char *) * argc);
1760 argc = parse_options(argc, (const char **)__argv, record_options,
1761 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1762 free(__argv);
1763
David Ahernd54b1a92012-08-26 12:24:46 -06001764 system_wide = (argc == 0);
1765
1766 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001767}
1768
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001769static void script__setup_sample_type(struct perf_script *script)
1770{
1771 struct perf_session *session = script->session;
1772 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1773
1774 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1775 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1776 (sample_type & PERF_SAMPLE_STACK_USER))
1777 callchain_param.record_mode = CALLCHAIN_DWARF;
1778 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1779 callchain_param.record_mode = CALLCHAIN_LBR;
1780 else
1781 callchain_param.record_mode = CALLCHAIN_FP;
1782 }
1783}
1784
Jiri Olsae099eba2016-01-05 22:09:09 +01001785static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1786 union perf_event *event,
1787 struct perf_session *session)
1788{
1789 struct stat_round_event *round = &event->stat_round;
1790 struct perf_evsel *counter;
1791
1792 evlist__for_each(session->evlist, counter) {
1793 perf_stat_process_counter(&stat_config, counter);
1794 process_stat(counter, round->time);
1795 }
1796
1797 process_stat_interval(round->time);
1798 return 0;
1799}
1800
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001801static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1802 union perf_event *event,
1803 struct perf_session *session __maybe_unused)
1804{
1805 perf_event__read_stat_config(&stat_config, &event->stat_config);
1806 return 0;
1807}
1808
Jiri Olsacfc88742016-01-05 22:09:06 +01001809static int set_maps(struct perf_script *script)
1810{
1811 struct perf_evlist *evlist = script->session->evlist;
1812
1813 if (!script->cpus || !script->threads)
1814 return 0;
1815
1816 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1817 return -EINVAL;
1818
1819 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1820
1821 if (perf_evlist__alloc_stats(evlist, true))
1822 return -ENOMEM;
1823
1824 script->allocated = true;
1825 return 0;
1826}
1827
1828static
1829int process_thread_map_event(struct perf_tool *tool,
1830 union perf_event *event,
1831 struct perf_session *session __maybe_unused)
1832{
1833 struct perf_script *script = container_of(tool, struct perf_script, tool);
1834
1835 if (script->threads) {
1836 pr_warning("Extra thread map event, ignoring.\n");
1837 return 0;
1838 }
1839
1840 script->threads = thread_map__new_event(&event->thread_map);
1841 if (!script->threads)
1842 return -ENOMEM;
1843
1844 return set_maps(script);
1845}
1846
1847static
1848int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1849 union perf_event *event,
1850 struct perf_session *session __maybe_unused)
1851{
1852 struct perf_script *script = container_of(tool, struct perf_script, tool);
1853
1854 if (script->cpus) {
1855 pr_warning("Extra cpu map event, ignoring.\n");
1856 return 0;
1857 }
1858
1859 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1860 if (!script->cpus)
1861 return -ENOMEM;
1862
1863 return set_maps(script);
1864}
1865
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001866int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001867{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001868 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001869 bool header = false;
1870 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001871 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001872 char *rec_script_path = NULL;
1873 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001874 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001875 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001876 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001877 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001878 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001879 struct perf_script script = {
1880 .tool = {
1881 .sample = process_sample_event,
1882 .mmap = perf_event__process_mmap,
1883 .mmap2 = perf_event__process_mmap2,
1884 .comm = perf_event__process_comm,
1885 .exit = perf_event__process_exit,
1886 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001887 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001888 .tracing_data = perf_event__process_tracing_data,
1889 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001890 .id_index = perf_event__process_id_index,
1891 .auxtrace_info = perf_event__process_auxtrace_info,
1892 .auxtrace = perf_event__process_auxtrace,
1893 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01001894 .stat = perf_event__process_stat_event,
1895 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001896 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01001897 .thread_map = process_thread_map_event,
1898 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001899 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001900 .ordering_requires_timestamps = true,
1901 },
1902 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001903 struct perf_data_file file = {
1904 .mode = PERF_DATA_MODE_READ,
1905 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001906 const struct option options[] = {
1907 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1908 "dump raw trace in ASCII"),
1909 OPT_INCR('v', "verbose", &verbose,
1910 "be more verbose (show symbol address, etc)"),
1911 OPT_BOOLEAN('L', "Latency", &latency_format,
1912 "show latency attributes (irqs/preemption disabled, etc)"),
1913 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1914 list_available_scripts),
1915 OPT_CALLBACK('s', "script", NULL, "name",
1916 "script file name (lang:script name, script name, or *)",
1917 parse_scriptname),
1918 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1919 "generate perf-script.xx script in specified language"),
1920 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1921 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1922 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001923 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1924 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001925 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1926 "file", "vmlinux pathname"),
1927 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1928 "file", "kallsyms pathname"),
1929 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1930 "When printing symbols do not display call chain"),
1931 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1932 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001933 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001934 "comma separated output fields prepend with 'type:'. "
1935 "Valid types: hw,sw,trace,raw. "
1936 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001937 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001938 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1939 "system-wide collection from all CPUs"),
1940 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1941 "only consider these symbols"),
1942 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1943 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1944 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06001945 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1946 "only consider symbols in these pids"),
1947 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1948 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001949 OPT_BOOLEAN('I', "show-info", &show_full_info,
1950 "display extended information from perf.data file"),
1951 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1952 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001953 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1954 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001955 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1956 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03001957 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1958 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001959 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03001960 OPT_BOOLEAN(0, "ns", &nanosecs,
1961 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001962 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1963 "Instruction Tracing options",
1964 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07001965 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1966 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07001967 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1968 "Enable symbol demangling"),
1969 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1970 "Enable kernel symbol demangling"),
1971
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001972 OPT_END()
1973 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08001974 const char * const script_subcommands[] = { "record", "report", NULL };
1975 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001976 "perf script [<options>]",
1977 "perf script [<options>] record <script> [<record-options>] <command>",
1978 "perf script [<options>] report <script> [script-args]",
1979 "perf script [<options>] <script> [<record-options>] <command>",
1980 "perf script [<options>] <top-script> [script-args]",
1981 NULL
1982 };
Tom Zanussi38752942009-12-15 02:53:39 -06001983
Tom Zanussib5b87312010-11-10 08:16:51 -06001984 setup_scripting();
1985
Yunlong Song40cae2b2015-03-18 21:35:54 +08001986 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001987 PARSE_OPT_STOP_AT_NON_OPTION);
1988
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001989 file.path = input_name;
1990
Tom Zanussib5b87312010-11-10 08:16:51 -06001991 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1992 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1993 if (!rec_script_path)
1994 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001995 }
1996
Tom Zanussib5b87312010-11-10 08:16:51 -06001997 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1998 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1999 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06002000 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06002001 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002002 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06002003 return -1;
2004 }
Tom Zanussi38752942009-12-15 02:53:39 -06002005 }
2006
Adrian Hunter3c5b6452015-09-25 16:15:51 +03002007 if (itrace_synth_opts.callchain &&
2008 itrace_synth_opts.callchain_sz > scripting_max_stack)
2009 scripting_max_stack = itrace_synth_opts.callchain_sz;
2010
Ben Hutchings44e668c2010-10-10 16:10:03 +01002011 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06002012 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01002013
Tom Zanussib5b87312010-11-10 08:16:51 -06002014 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05002015 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06002016 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002017 pid_t pid;
2018
Tom Zanussib5b87312010-11-10 08:16:51 -06002019 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
2020 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
2021
2022 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002023 usage_with_options_msg(script_usage, options,
2024 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002025 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002026 }
2027
Tom Zanussib5b87312010-11-10 08:16:51 -06002028 if (is_top_script(argv[0])) {
2029 rep_args = argc - 1;
2030 } else {
2031 int rec_args;
2032
2033 rep_args = has_required_arg(rep_script_path);
2034 rec_args = (argc - 1) - rep_args;
2035 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09002036 usage_with_options_msg(script_usage, options,
2037 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002038 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06002039 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06002040 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05002041 }
2042
2043 if (pipe(live_pipe) < 0) {
2044 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06002045 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002046 }
2047
2048 pid = fork();
2049 if (pid < 0) {
2050 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06002051 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002052 }
2053
2054 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002055 j = 0;
2056
Tom Zanussia0cccc22010-04-01 23:59:25 -05002057 dup2(live_pipe[1], 1);
2058 close(live_pipe[0]);
2059
Robert Richter317df652011-11-25 15:05:25 +01002060 if (is_top_script(argv[0])) {
2061 system_wide = true;
2062 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06002063 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
2064 err = -1;
2065 goto out;
2066 }
Robert Richter317df652011-11-25 15:05:25 +01002067 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002068
2069 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002070 if (!__argv) {
2071 pr_err("malloc failed\n");
2072 err = -ENOMEM;
2073 goto out;
2074 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06002075
Tom Zanussib5b87312010-11-10 08:16:51 -06002076 __argv[j++] = "/bin/sh";
2077 __argv[j++] = rec_script_path;
2078 if (system_wide)
2079 __argv[j++] = "-a";
2080 __argv[j++] = "-q";
2081 __argv[j++] = "-o";
2082 __argv[j++] = "-";
2083 for (i = rep_args + 1; i < argc; i++)
2084 __argv[j++] = argv[i];
2085 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002086
2087 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002088 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002089 exit(-1);
2090 }
2091
2092 dup2(live_pipe[0], 0);
2093 close(live_pipe[1]);
2094
Tom Zanussib5b87312010-11-10 08:16:51 -06002095 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002096 if (!__argv) {
2097 pr_err("malloc failed\n");
2098 err = -ENOMEM;
2099 goto out;
2100 }
2101
Tom Zanussib5b87312010-11-10 08:16:51 -06002102 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002103 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002104 __argv[j++] = rep_script_path;
2105 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002106 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002107 __argv[j++] = "-i";
2108 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002109 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002110
2111 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002112 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002113 exit(-1);
2114 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002115
Tom Zanussib5b87312010-11-10 08:16:51 -06002116 if (rec_script_path)
2117 script_path = rec_script_path;
2118 if (rep_script_path)
2119 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002120
Tom Zanussib5b87312010-11-10 08:16:51 -06002121 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002122 j = 0;
2123
Robert Richter317df652011-11-25 15:05:25 +01002124 if (!rec_script_path)
2125 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002126 else if (!system_wide) {
2127 if (have_cmd(argc - 1, &argv[1]) != 0) {
2128 err = -1;
2129 goto out;
2130 }
2131 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002132
2133 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002134 if (!__argv) {
2135 pr_err("malloc failed\n");
2136 err = -ENOMEM;
2137 goto out;
2138 }
2139
Tom Zanussib5b87312010-11-10 08:16:51 -06002140 __argv[j++] = "/bin/sh";
2141 __argv[j++] = script_path;
2142 if (system_wide)
2143 __argv[j++] = "-a";
2144 for (i = 2; i < argc; i++)
2145 __argv[j++] = argv[i];
2146 __argv[j++] = NULL;
2147
2148 execvp("/bin/sh", (char **)__argv);
2149 free(__argv);
2150 exit(-1);
2151 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002152
Tom Zanussicf4fee52010-03-03 01:04:33 -06002153 if (!script_name)
2154 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002155
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002156 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002157 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002158 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002159
Jiri Olsae90debd2013-12-09 11:02:50 +01002160 if (header || header_only) {
2161 perf_session__fprintf_info(session, stdout, show_full_info);
2162 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002163 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002164 }
2165
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002166 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002167 goto out_delete;
2168
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002169 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002170 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002171
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002172 session->itrace_synth_opts = &itrace_synth_opts;
2173
Anton Blanchard5d67be92011-07-04 21:57:50 +10002174 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002175 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2176 if (err < 0)
2177 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002178 }
2179
David Ahern1424dc92011-03-09 22:23:28 -07002180 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002181 symbol_conf.use_callchain = true;
2182 else
2183 symbol_conf.use_callchain = false;
2184
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002185 if (session->tevent.pevent &&
2186 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002187 machine__resolve_kernel_addr,
2188 &session->machines.host) < 0) {
2189 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2190 return -1;
2191 }
2192
Tom Zanussi956ffd02009-11-25 01:15:46 -06002193 if (generate_script_lang) {
2194 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002195 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002196
David Ahern2c9e45f72011-03-17 10:03:21 -06002197 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002198 fprintf(stderr,
2199 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002200 err = -EINVAL;
2201 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002202 }
2203
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002204 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002205 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002206 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002207 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002208 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002209 }
2210
2211 err = fstat(input, &perf_stat);
2212 if (err < 0) {
2213 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002214 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002215 }
2216
2217 if (!perf_stat.st_size) {
2218 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002219 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002220 }
2221
2222 scripting_ops = script_spec__lookup(generate_script_lang);
2223 if (!scripting_ops) {
2224 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002225 err = -ENOENT;
2226 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002227 }
2228
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002229 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002230 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002231 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002232 }
2233
2234 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002235 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002236 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002237 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002238 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002239 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002240 }
2241
David Ahern9cbdb702011-04-06 21:54:20 -06002242
2243 err = perf_session__check_output_opt(session);
2244 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002245 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002246
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002247 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002248
Adrian Hunterd445dd22014-08-15 22:08:37 +03002249 flush_scripting();
2250
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002251out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002252 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002253 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002254
2255 if (script_started)
2256 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002257out:
2258 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002259}