blob: a90bc0b81e70d107940c0fa7050a70e19850bb8d [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,
David Ahern745f43e2011-03-09 22:23:26 -070061};
62
63struct output_option {
64 const char *str;
65 enum perf_output_field field;
66} all_output_options[] = {
67 {.str = "comm", .field = PERF_OUTPUT_COMM},
68 {.str = "tid", .field = PERF_OUTPUT_TID},
69 {.str = "pid", .field = PERF_OUTPUT_PID},
70 {.str = "time", .field = PERF_OUTPUT_TIME},
71 {.str = "cpu", .field = PERF_OUTPUT_CPU},
72 {.str = "event", .field = PERF_OUTPUT_EVNAME},
73 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060074 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070075 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060076 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060077 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090078 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020079 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaae2014-08-25 16:45:42 +020080 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020081 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +020082 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
83 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
David Ahern745f43e2011-03-09 22:23:26 -070084};
85
86/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060087static struct {
88 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060089 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040090 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060091 u64 fields;
92 u64 invalid_fields;
93} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070094
David Ahern2c9e45f72011-03-17 10:03:21 -060095 [PERF_TYPE_HARDWARE] = {
96 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070097
David Ahern2c9e45f72011-03-17 10:03:21 -060098 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
99 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600100 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200101 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
102 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600103
104 .invalid_fields = PERF_OUTPUT_TRACE,
105 },
106
107 [PERF_TYPE_SOFTWARE] = {
108 .user_set = false,
109
110 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
111 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600112 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200113 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
114 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600115
116 .invalid_fields = PERF_OUTPUT_TRACE,
117 },
118
119 [PERF_TYPE_TRACEPOINT] = {
120 .user_set = false,
121
122 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
123 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
124 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
125 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700126
127 [PERF_TYPE_RAW] = {
128 .user_set = false,
129
130 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
131 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600132 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200133 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
134 PERF_OUTPUT_PERIOD,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700135
136 .invalid_fields = PERF_OUTPUT_TRACE,
137 },
Wang Nan27cfef02015-12-08 02:25:43 +0000138
139 [PERF_TYPE_BREAKPOINT] = {
140 .user_set = false,
141
142 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
143 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
144 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
145 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
146 PERF_OUTPUT_PERIOD,
147
148 .invalid_fields = PERF_OUTPUT_TRACE,
149 },
David Ahern1424dc92011-03-09 22:23:28 -0700150};
David Ahern745f43e2011-03-09 22:23:26 -0700151
David Ahern2c9e45f72011-03-17 10:03:21 -0600152static bool output_set_by_user(void)
153{
154 int j;
155 for (j = 0; j < PERF_TYPE_MAX; ++j) {
156 if (output[j].user_set)
157 return true;
158 }
159 return false;
160}
David Ahern745f43e2011-03-09 22:23:26 -0700161
David Ahern9cbdb702011-04-06 21:54:20 -0600162static const char *output_field2str(enum perf_output_field field)
163{
164 int i, imax = ARRAY_SIZE(all_output_options);
165 const char *str = "";
166
167 for (i = 0; i < imax; ++i) {
168 if (all_output_options[i].field == field) {
169 str = all_output_options[i].str;
170 break;
171 }
172 }
173 return str;
174}
175
David Ahern2c9e45f72011-03-17 10:03:21 -0600176#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700177
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300178static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
179 u64 sample_type, const char *sample_msg,
180 enum perf_output_field field,
181 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700182{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300183 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600184 int type = attr->type;
185 const char *evname;
186
187 if (attr->sample_type & sample_type)
188 return 0;
189
190 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300191 if (allow_user_set)
192 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300193 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600194 pr_err("Samples for '%s' event do not have %s attribute set. "
195 "Cannot print '%s' field.\n",
196 evname, sample_msg, output_field2str(field));
197 return -1;
198 }
199
200 /* user did not ask for it explicitly so remove from the default list */
201 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300202 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600203 pr_debug("Samples for '%s' event do not have %s attribute set. "
204 "Skipping '%s' field.\n",
205 evname, sample_msg, output_field2str(field));
206
207 return 0;
208}
209
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300210static int perf_evsel__check_stype(struct perf_evsel *evsel,
211 u64 sample_type, const char *sample_msg,
212 enum perf_output_field field)
213{
214 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
215 false);
216}
217
David Ahern9cbdb702011-04-06 21:54:20 -0600218static int perf_evsel__check_attr(struct perf_evsel *evsel,
219 struct perf_session *session)
220{
221 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300222 bool allow_user_set;
223
224 allow_user_set = perf_header__has_feat(&session->header,
225 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600226
David Ahern1424dc92011-03-09 22:23:28 -0700227 if (PRINT_FIELD(TRACE) &&
228 !perf_session__has_traces(session, "record -R"))
229 return -EINVAL;
230
David Ahern787bef12011-05-27 14:28:43 -0600231 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300232 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
233 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700234 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700235 }
David Ahern7cec0922011-05-30 13:08:23 -0600236
237 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300238 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
239 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600240 return -EINVAL;
241
242 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
243 pr_err("Display of symbols requested but neither sample IP nor "
244 "sample address\nis selected. Hence, no addresses to convert "
245 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600246 return -EINVAL;
247 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900248 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
249 pr_err("Display of offsets requested but symbol is not"
250 "selected.\n");
251 return -EINVAL;
252 }
David Ahern7cec0922011-05-30 13:08:23 -0600253 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
254 pr_err("Display of DSO requested but neither sample IP nor "
255 "sample address\nis selected. Hence, no addresses to convert "
256 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600257 return -EINVAL;
258 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200259 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
260 pr_err("Display of source line number requested but sample IP is not\n"
261 "selected. Hence, no address to lookup the source line number.\n");
262 return -EINVAL;
263 }
David Ahern1424dc92011-03-09 22:23:28 -0700264
265 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300266 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
267 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700268 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700269
270 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300271 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
272 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700273 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700274
275 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300276 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
277 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700278 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600279
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200280 if (PRINT_FIELD(PERIOD) &&
281 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
282 PERF_OUTPUT_PERIOD))
283 return -EINVAL;
284
Stephane Eranianfc36f942015-08-31 18:41:10 +0200285 if (PRINT_FIELD(IREGS) &&
286 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
287 PERF_OUTPUT_IREGS))
288 return -EINVAL;
289
David Ahern9cbdb702011-04-06 21:54:20 -0600290 return 0;
291}
292
Adrian Hunter7ea95722013-11-01 15:51:30 +0200293static void set_print_ip_opts(struct perf_event_attr *attr)
294{
295 unsigned int type = attr->type;
296
297 output[type].print_ip_opts = 0;
298 if (PRINT_FIELD(IP))
299 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
300
301 if (PRINT_FIELD(SYM))
302 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
303
304 if (PRINT_FIELD(DSO))
305 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
306
307 if (PRINT_FIELD(SYMOFFSET))
308 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200309
310 if (PRINT_FIELD(SRCLINE))
311 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200312}
313
David Ahern9cbdb702011-04-06 21:54:20 -0600314/*
315 * verify all user requested events exist and the samples
316 * have the expected data
317 */
318static int perf_session__check_output_opt(struct perf_session *session)
319{
320 int j;
321 struct perf_evsel *evsel;
322
323 for (j = 0; j < PERF_TYPE_MAX; ++j) {
324 evsel = perf_session__find_first_evtype(session, j);
325
326 /*
327 * even if fields is set to 0 (ie., show nothing) event must
328 * exist if user explicitly includes it on the command line
329 */
330 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
331 pr_err("%s events do not exist. "
332 "Remove corresponding -f option to proceed.\n",
333 event_type(j));
334 return -1;
335 }
336
337 if (evsel && output[j].fields &&
338 perf_evsel__check_attr(evsel, session))
339 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400340
341 if (evsel == NULL)
342 continue;
343
Adrian Hunter7ea95722013-11-01 15:51:30 +0200344 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700345 }
346
Adrian Hunter98526ee2014-07-31 09:00:59 +0300347 if (!no_callchain) {
348 bool use_callchain = false;
349
350 evlist__for_each(session->evlist, evsel) {
351 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
352 use_callchain = true;
353 break;
354 }
355 }
356 if (!use_callchain)
357 symbol_conf.use_callchain = false;
358 }
359
David Ahern80b8b492013-11-19 21:07:37 -0700360 /*
361 * set default for tracepoints to print symbols only
362 * if callchains are present
363 */
364 if (symbol_conf.use_callchain &&
365 !output[PERF_TYPE_TRACEPOINT].user_set) {
366 struct perf_event_attr *attr;
367
368 j = PERF_TYPE_TRACEPOINT;
369 evsel = perf_session__find_first_evtype(session, j);
370 if (evsel == NULL)
371 goto out;
372
373 attr = &evsel->attr;
374
375 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
376 output[j].fields |= PERF_OUTPUT_IP;
377 output[j].fields |= PERF_OUTPUT_SYM;
378 output[j].fields |= PERF_OUTPUT_DSO;
379 set_print_ip_opts(attr);
380 }
381 }
382
383out:
David Ahern1424dc92011-03-09 22:23:28 -0700384 return 0;
385}
David Ahern745f43e2011-03-09 22:23:26 -0700386
Stephane Eranianfc36f942015-08-31 18:41:10 +0200387static void print_sample_iregs(union perf_event *event __maybe_unused,
388 struct perf_sample *sample,
389 struct thread *thread __maybe_unused,
390 struct perf_event_attr *attr)
391{
392 struct regs_dump *regs = &sample->intr_regs;
393 uint64_t mask = attr->sample_regs_intr;
394 unsigned i = 0, r;
395
396 if (!regs)
397 return;
398
399 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
400 u64 val = regs->regs[i++];
401 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
402 }
403}
404
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300405static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700406 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300407 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700408{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300409 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700410 unsigned long secs;
411 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700412 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700413
David Ahern745f43e2011-03-09 22:23:26 -0700414 if (PRINT_FIELD(COMM)) {
415 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200416 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600417 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200418 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700419 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200420 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700421 }
David Ahernc70c94b2011-03-09 22:23:25 -0700422
David Ahern745f43e2011-03-09 22:23:26 -0700423 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
424 printf("%5d/%-5d ", sample->pid, sample->tid);
425 else if (PRINT_FIELD(PID))
426 printf("%5d ", sample->pid);
427 else if (PRINT_FIELD(TID))
428 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700429
David Ahern745f43e2011-03-09 22:23:26 -0700430 if (PRINT_FIELD(CPU)) {
431 if (latency_format)
432 printf("%3d ", sample->cpu);
433 else
434 printf("[%03d] ", sample->cpu);
435 }
David Ahernc70c94b2011-03-09 22:23:25 -0700436
David Ahern745f43e2011-03-09 22:23:26 -0700437 if (PRINT_FIELD(TIME)) {
438 nsecs = sample->time;
439 secs = nsecs / NSECS_PER_SEC;
440 nsecs -= secs * NSECS_PER_SEC;
441 usecs = nsecs / NSECS_PER_USEC;
Adrian Hunter83e19862015-09-25 16:15:36 +0300442 if (nanosecs)
443 printf("%5lu.%09llu: ", secs, nsecs);
444 else
445 printf("%5lu.%06lu: ", secs, usecs);
David Ahern745f43e2011-03-09 22:23:26 -0700446 }
David Ahernc70c94b2011-03-09 22:23:25 -0700447}
448
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200449static inline char
450mispred_str(struct branch_entry *br)
451{
452 if (!(br->flags.mispred || br->flags.predicted))
453 return '-';
454
455 return br->flags.predicted ? 'P' : 'M';
456}
457
458static void print_sample_brstack(union perf_event *event __maybe_unused,
459 struct perf_sample *sample,
460 struct thread *thread __maybe_unused,
461 struct perf_event_attr *attr __maybe_unused)
462{
463 struct branch_stack *br = sample->branch_stack;
464 u64 i;
465
466 if (!(br && br->nr))
467 return;
468
469 for (i = 0; i < br->nr; i++) {
470 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
471 br->entries[i].from,
472 br->entries[i].to,
473 mispred_str( br->entries + i),
474 br->entries[i].flags.in_tx? 'X' : '-',
475 br->entries[i].flags.abort? 'A' : '-',
476 br->entries[i].flags.cycles);
477 }
478}
479
480static void print_sample_brstacksym(union perf_event *event __maybe_unused,
481 struct perf_sample *sample,
482 struct thread *thread __maybe_unused,
483 struct perf_event_attr *attr __maybe_unused)
484{
485 struct branch_stack *br = sample->branch_stack;
486 struct addr_location alf, alt;
487 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
488 u64 i, from, to;
489
490 if (!(br && br->nr))
491 return;
492
493 for (i = 0; i < br->nr; i++) {
494
495 memset(&alf, 0, sizeof(alf));
496 memset(&alt, 0, sizeof(alt));
497 from = br->entries[i].from;
498 to = br->entries[i].to;
499
500 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
501 if (alf.map)
502 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
503
504 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
505 if (alt.map)
506 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
507
508 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
509 putchar('/');
510 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
511 printf("/%c/%c/%c/%d ",
512 mispred_str( br->entries + i),
513 br->entries[i].flags.in_tx? 'X' : '-',
514 br->entries[i].flags.abort? 'A' : '-',
515 br->entries[i].flags.cycles);
516 }
517}
518
519
David Ahern7cec0922011-05-30 13:08:23 -0600520static void print_sample_addr(union perf_event *event,
521 struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600522 struct thread *thread,
523 struct perf_event_attr *attr)
524{
525 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600526
527 printf("%16" PRIx64, sample->addr);
528
529 if (!sample_addr_correlates_sym(attr))
530 return;
531
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300532 perf_event__preprocess_sample_addr(event, sample, thread, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600533
534 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900535 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900536 if (PRINT_FIELD(SYMOFFSET))
537 symbol__fprintf_symname_offs(al.sym, &al, stdout);
538 else
539 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600540 }
541
542 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900543 printf(" (");
544 map__fprintf_dsoname(al.map, stdout);
545 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600546 }
547}
548
Akihiro Nagai95582592012-01-30 13:43:09 +0900549static void print_sample_bts(union perf_event *event,
550 struct perf_sample *sample,
551 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200552 struct thread *thread,
553 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900554{
555 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300556 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900557
558 /* print branch_from information */
559 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300560 unsigned int print_opts = output[attr->type].print_ip_opts;
561
562 if (symbol_conf.use_callchain && sample->callchain) {
Akihiro Nagai95582592012-01-30 13:43:09 +0900563 printf("\n");
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300564 } else {
565 printf(" ");
566 if (print_opts & PRINT_IP_OPT_SRCLINE) {
567 print_srcline_last = true;
568 print_opts &= ~PRINT_IP_OPT_SRCLINE;
569 }
570 }
571 perf_evsel__print_ip(evsel, sample, al, print_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300572 scripting_max_stack);
Akihiro Nagai95582592012-01-30 13:43:09 +0900573 }
574
Akihiro Nagai95582592012-01-30 13:43:09 +0900575 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300576 if (PRINT_FIELD(ADDR) ||
577 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300578 !output[attr->type].user_set)) {
579 printf(" => ");
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300580 print_sample_addr(event, sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300581 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900582
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300583 if (print_srcline_last)
584 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
585
Akihiro Nagai95582592012-01-30 13:43:09 +0900586 printf("\n");
587}
588
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300589static void print_sample_flags(u32 flags)
590{
591 const char *chars = PERF_IP_FLAG_CHARS;
592 const int n = strlen(PERF_IP_FLAG_CHARS);
593 char str[33];
594 int i, pos = 0;
595
596 for (i = 0; i < n; i++, flags >>= 1) {
597 if (flags & 1)
598 str[pos++] = chars[i];
599 }
600 for (; i < 32; i++, flags >>= 1) {
601 if (flags & 1)
602 str[pos++] = '?';
603 }
604 str[pos] = 0;
605 printf(" %-4s ", str);
606}
607
Jiri Olsa809e9422015-11-26 18:55:21 +0100608struct perf_script {
609 struct perf_tool tool;
610 struct perf_session *session;
611 bool show_task_events;
612 bool show_mmap_events;
613 bool show_switch_events;
Jiri Olsacfc88742016-01-05 22:09:06 +0100614 bool allocated;
615 struct cpu_map *cpus;
616 struct thread_map *threads;
Jiri Olsa809e9422015-11-26 18:55:21 +0100617};
618
619static void process_event(struct perf_script *script __maybe_unused, union perf_event *event,
620 struct perf_sample *sample, struct perf_evsel *evsel,
621 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700622{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300623 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300624 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700625
David Ahern2c9e45f72011-03-17 10:03:21 -0600626 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700627 return;
628
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300629 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700630
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200631 if (PRINT_FIELD(PERIOD))
632 printf("%10" PRIu64 " ", sample->period);
633
Namhyung Kime944d3d2013-11-18 14:34:52 +0900634 if (PRINT_FIELD(EVNAME)) {
635 const char *evname = perf_evsel__name(evsel);
636 printf("%s: ", evname ? evname : "[unknown]");
637 }
638
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300639 if (print_flags)
640 print_sample_flags(sample->flags);
641
Akihiro Nagai95582592012-01-30 13:43:09 +0900642 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300643 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900644 return;
645 }
646
David Ahern745f43e2011-03-09 22:23:26 -0700647 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300648 event_format__print(evsel->tp_format, sample->cpu,
649 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600650 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300651 print_sample_addr(event, sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600652
David Ahern787bef12011-05-27 14:28:43 -0600653 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700654 if (!symbol_conf.use_callchain)
655 printf(" ");
656 else
657 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400658
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300659 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400660 output[attr->type].print_ip_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300661 scripting_max_stack);
David Ahernc0230b22011-03-09 22:23:27 -0700662 }
663
Stephane Eranianfc36f942015-08-31 18:41:10 +0200664 if (PRINT_FIELD(IREGS))
665 print_sample_iregs(event, sample, thread, attr);
666
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200667 if (PRINT_FIELD(BRSTACK))
668 print_sample_brstack(event, sample, thread, attr);
669 else if (PRINT_FIELD(BRSTACKSYM))
670 print_sample_brstacksym(event, sample, thread, attr);
671
David Ahernc70c94b2011-03-09 22:23:25 -0700672 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700673}
674
Tom Zanussi956ffd02009-11-25 01:15:46 -0600675static struct scripting_ops *scripting_ops;
676
677static void setup_scripting(void)
678{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600679 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600680 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600681}
682
Adrian Hunterd445dd22014-08-15 22:08:37 +0300683static int flush_scripting(void)
684{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100685 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300686}
687
Tom Zanussi956ffd02009-11-25 01:15:46 -0600688static int cleanup_scripting(void)
689{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100690 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500691
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100692 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600693}
694
Jiri Olsa809e9422015-11-26 18:55:21 +0100695static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200696 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200697 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300698 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200699 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200700{
Jiri Olsa809e9422015-11-26 18:55:21 +0100701 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -0700702 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200703
David Ahern1424dc92011-03-09 22:23:28 -0700704 if (debug_mode) {
705 if (sample->time < last_timestamp) {
706 pr_err("Samples misordered, previous: %" PRIu64
707 " this: %" PRIu64 "\n", last_timestamp,
708 sample->time);
709 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200710 }
David Ahern1424dc92011-03-09 22:23:28 -0700711 last_timestamp = sample->time;
712 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200713 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000714
Adrian Huntere44baa32013-08-08 14:32:25 +0300715 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700716 pr_err("problem processing %d event, skipping it.\n",
717 event->header.type);
718 return -1;
719 }
720
721 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300722 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700723
Anton Blanchard5d67be92011-07-04 21:57:50 +1000724 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300725 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000726
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100727 if (scripting_ops)
728 scripting_ops->process_event(event, sample, evsel, &al);
729 else
Jiri Olsa809e9422015-11-26 18:55:21 +0100730 process_event(scr, event, sample, evsel, &al);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100731
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300732out_put:
733 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200734 return 0;
735}
736
Adrian Hunter7ea95722013-11-01 15:51:30 +0200737static int process_attr(struct perf_tool *tool, union perf_event *event,
738 struct perf_evlist **pevlist)
739{
740 struct perf_script *scr = container_of(tool, struct perf_script, tool);
741 struct perf_evlist *evlist;
742 struct perf_evsel *evsel, *pos;
743 int err;
744
745 err = perf_event__process_attr(tool, event, pevlist);
746 if (err)
747 return err;
748
749 evlist = *pevlist;
750 evsel = perf_evlist__last(*pevlist);
751
752 if (evsel->attr.type >= PERF_TYPE_MAX)
753 return 0;
754
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300755 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200756 if (pos->attr.type == evsel->attr.type && pos != evsel)
757 return 0;
758 }
759
760 set_print_ip_opts(&evsel->attr);
761
Jiri Olsad2b5a312015-10-16 12:41:25 +0200762 if (evsel->attr.sample_type)
763 err = perf_evsel__check_attr(evsel, scr->session);
764
765 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200766}
767
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900768static int process_comm_event(struct perf_tool *tool,
769 union perf_event *event,
770 struct perf_sample *sample,
771 struct machine *machine)
772{
773 struct thread *thread;
774 struct perf_script *script = container_of(tool, struct perf_script, tool);
775 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300776 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900777 int ret = -1;
778
779 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
780 if (thread == NULL) {
781 pr_debug("problem processing COMM event, skipping it.\n");
782 return -1;
783 }
784
785 if (perf_event__process_comm(tool, event, sample, machine) < 0)
786 goto out;
787
788 if (!evsel->attr.sample_id_all) {
789 sample->cpu = 0;
790 sample->time = 0;
791 sample->tid = event->comm.tid;
792 sample->pid = event->comm.pid;
793 }
794 print_sample_start(sample, thread, evsel);
795 perf_event__fprintf(event, stdout);
796 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900797out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300798 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900799 return ret;
800}
801
802static int process_fork_event(struct perf_tool *tool,
803 union perf_event *event,
804 struct perf_sample *sample,
805 struct machine *machine)
806{
807 struct thread *thread;
808 struct perf_script *script = container_of(tool, struct perf_script, tool);
809 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300810 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900811
812 if (perf_event__process_fork(tool, event, sample, machine) < 0)
813 return -1;
814
815 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
816 if (thread == NULL) {
817 pr_debug("problem processing FORK event, skipping it.\n");
818 return -1;
819 }
820
821 if (!evsel->attr.sample_id_all) {
822 sample->cpu = 0;
823 sample->time = event->fork.time;
824 sample->tid = event->fork.tid;
825 sample->pid = event->fork.pid;
826 }
827 print_sample_start(sample, thread, evsel);
828 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300829 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900830
831 return 0;
832}
833static int process_exit_event(struct perf_tool *tool,
834 union perf_event *event,
835 struct perf_sample *sample,
836 struct machine *machine)
837{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300838 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900839 struct thread *thread;
840 struct perf_script *script = container_of(tool, struct perf_script, tool);
841 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300842 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900843
844 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
845 if (thread == NULL) {
846 pr_debug("problem processing EXIT event, skipping it.\n");
847 return -1;
848 }
849
850 if (!evsel->attr.sample_id_all) {
851 sample->cpu = 0;
852 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +0300853 sample->tid = event->fork.tid;
854 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900855 }
856 print_sample_start(sample, thread, evsel);
857 perf_event__fprintf(event, stdout);
858
859 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300860 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900861
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300862 thread__put(thread);
863 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900864}
865
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900866static int process_mmap_event(struct perf_tool *tool,
867 union perf_event *event,
868 struct perf_sample *sample,
869 struct machine *machine)
870{
871 struct thread *thread;
872 struct perf_script *script = container_of(tool, struct perf_script, tool);
873 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300874 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900875
876 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
877 return -1;
878
879 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
880 if (thread == NULL) {
881 pr_debug("problem processing MMAP event, skipping it.\n");
882 return -1;
883 }
884
885 if (!evsel->attr.sample_id_all) {
886 sample->cpu = 0;
887 sample->time = 0;
888 sample->tid = event->mmap.tid;
889 sample->pid = event->mmap.pid;
890 }
891 print_sample_start(sample, thread, evsel);
892 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300893 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900894 return 0;
895}
896
897static int process_mmap2_event(struct perf_tool *tool,
898 union perf_event *event,
899 struct perf_sample *sample,
900 struct machine *machine)
901{
902 struct thread *thread;
903 struct perf_script *script = container_of(tool, struct perf_script, tool);
904 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300905 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900906
907 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
908 return -1;
909
910 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
911 if (thread == NULL) {
912 pr_debug("problem processing MMAP2 event, skipping it.\n");
913 return -1;
914 }
915
916 if (!evsel->attr.sample_id_all) {
917 sample->cpu = 0;
918 sample->time = 0;
919 sample->tid = event->mmap2.tid;
920 sample->pid = event->mmap2.pid;
921 }
922 print_sample_start(sample, thread, evsel);
923 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300924 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900925 return 0;
926}
927
Adrian Hunter7c148982015-07-21 12:44:06 +0300928static int process_switch_event(struct perf_tool *tool,
929 union perf_event *event,
930 struct perf_sample *sample,
931 struct machine *machine)
932{
933 struct thread *thread;
934 struct perf_script *script = container_of(tool, struct perf_script, tool);
935 struct perf_session *session = script->session;
936 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
937
938 if (perf_event__process_switch(tool, event, sample, machine) < 0)
939 return -1;
940
941 thread = machine__findnew_thread(machine, sample->pid,
942 sample->tid);
943 if (thread == NULL) {
944 pr_debug("problem processing SWITCH event, skipping it.\n");
945 return -1;
946 }
947
948 print_sample_start(sample, thread, evsel);
949 perf_event__fprintf(event, stdout);
950 thread__put(thread);
951 return 0;
952}
953
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300954static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500955{
956 session_done = 1;
957}
958
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300959static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200960{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200961 int ret;
962
Tom Zanussic239da32010-04-01 23:59:18 -0500963 signal(SIGINT, sig_handler);
964
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900965 /* override event processing functions */
966 if (script->show_task_events) {
967 script->tool.comm = process_comm_event;
968 script->tool.fork = process_fork_event;
969 script->tool.exit = process_exit_event;
970 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900971 if (script->show_mmap_events) {
972 script->tool.mmap = process_mmap_event;
973 script->tool.mmap2 = process_mmap2_event;
974 }
Adrian Hunter7c148982015-07-21 12:44:06 +0300975 if (script->show_switch_events)
976 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900977
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300978 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200979
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200980 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200981 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200982
983 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200984}
985
Tom Zanussi956ffd02009-11-25 01:15:46 -0600986struct script_spec {
987 struct list_head node;
988 struct scripting_ops *ops;
989 char spec[0];
990};
991
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200992static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600993
994static struct script_spec *script_spec__new(const char *spec,
995 struct scripting_ops *ops)
996{
997 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
998
999 if (s != NULL) {
1000 strcpy(s->spec, spec);
1001 s->ops = ops;
1002 }
1003
1004 return s;
1005}
1006
Tom Zanussi956ffd02009-11-25 01:15:46 -06001007static void script_spec__add(struct script_spec *s)
1008{
1009 list_add_tail(&s->node, &script_specs);
1010}
1011
1012static struct script_spec *script_spec__find(const char *spec)
1013{
1014 struct script_spec *s;
1015
1016 list_for_each_entry(s, &script_specs, node)
1017 if (strcasecmp(s->spec, spec) == 0)
1018 return s;
1019 return NULL;
1020}
1021
1022static struct script_spec *script_spec__findnew(const char *spec,
1023 struct scripting_ops *ops)
1024{
1025 struct script_spec *s = script_spec__find(spec);
1026
1027 if (s)
1028 return s;
1029
1030 s = script_spec__new(spec, ops);
1031 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +09001032 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001033
1034 script_spec__add(s);
1035
1036 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001037}
1038
1039int script_spec_register(const char *spec, struct scripting_ops *ops)
1040{
1041 struct script_spec *s;
1042
1043 s = script_spec__find(spec);
1044 if (s)
1045 return -1;
1046
1047 s = script_spec__findnew(spec, ops);
1048 if (!s)
1049 return -1;
1050
1051 return 0;
1052}
1053
1054static struct scripting_ops *script_spec__lookup(const char *spec)
1055{
1056 struct script_spec *s = script_spec__find(spec);
1057 if (!s)
1058 return NULL;
1059
1060 return s->ops;
1061}
1062
1063static void list_available_languages(void)
1064{
1065 struct script_spec *s;
1066
1067 fprintf(stderr, "\n");
1068 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001069 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001070
1071 list_for_each_entry(s, &script_specs, node)
1072 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1073
1074 fprintf(stderr, "\n");
1075}
1076
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001077static int parse_scriptname(const struct option *opt __maybe_unused,
1078 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001079{
1080 char spec[PATH_MAX];
1081 const char *script, *ext;
1082 int len;
1083
Tom Zanussif526d682010-01-27 02:27:52 -06001084 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001085 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001086 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001087 }
1088
1089 script = strchr(str, ':');
1090 if (script) {
1091 len = script - str;
1092 if (len >= PATH_MAX) {
1093 fprintf(stderr, "invalid language specifier");
1094 return -1;
1095 }
1096 strncpy(spec, str, len);
1097 spec[len] = '\0';
1098 scripting_ops = script_spec__lookup(spec);
1099 if (!scripting_ops) {
1100 fprintf(stderr, "invalid language specifier");
1101 return -1;
1102 }
1103 script++;
1104 } else {
1105 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001106 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001107 if (!ext) {
1108 fprintf(stderr, "invalid script extension");
1109 return -1;
1110 }
1111 scripting_ops = script_spec__lookup(++ext);
1112 if (!scripting_ops) {
1113 fprintf(stderr, "invalid script extension");
1114 return -1;
1115 }
1116 }
1117
1118 script_name = strdup(script);
1119
1120 return 0;
1121}
1122
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001123static int parse_output_fields(const struct option *opt __maybe_unused,
1124 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001125{
1126 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001127 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001128 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001129 int rc = 0;
1130 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001131 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001132
1133 if (!str)
1134 return -ENOMEM;
1135
David Ahern2c9e45f72011-03-17 10:03:21 -06001136 /* first word can state for which event type the user is specifying
1137 * the fields. If no type exists, the specified fields apply to all
1138 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001139 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001140 tok = strchr(str, ':');
1141 if (tok) {
1142 *tok = '\0';
1143 tok++;
1144 if (!strcmp(str, "hw"))
1145 type = PERF_TYPE_HARDWARE;
1146 else if (!strcmp(str, "sw"))
1147 type = PERF_TYPE_SOFTWARE;
1148 else if (!strcmp(str, "trace"))
1149 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001150 else if (!strcmp(str, "raw"))
1151 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001152 else if (!strcmp(str, "break"))
1153 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001154 else {
1155 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001156 rc = -EINVAL;
1157 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001158 }
1159
1160 if (output[type].user_set)
1161 pr_warning("Overriding previous field request for %s events.\n",
1162 event_type(type));
1163
1164 output[type].fields = 0;
1165 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001166 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001167
1168 } else {
1169 tok = str;
1170 if (strlen(str) == 0) {
1171 fprintf(stderr,
1172 "Cannot set fields to 'none' for all event types.\n");
1173 rc = -EINVAL;
1174 goto out;
1175 }
1176
1177 if (output_set_by_user())
1178 pr_warning("Overriding previous field request for all events.\n");
1179
1180 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1181 output[j].fields = 0;
1182 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001183 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001184 }
David Ahern1424dc92011-03-09 22:23:28 -07001185 }
1186
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001187 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001188 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001189 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001190 break;
David Ahern745f43e2011-03-09 22:23:26 -07001191 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001192 if (i == imax && strcmp(tok, "flags") == 0) {
1193 print_flags = true;
1194 continue;
1195 }
David Ahern745f43e2011-03-09 22:23:26 -07001196 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001197 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001198 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001199 goto out;
1200 }
1201
1202 if (type == -1) {
1203 /* add user option to all events types for
1204 * which it is valid
1205 */
1206 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1207 if (output[j].invalid_fields & all_output_options[i].field) {
1208 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1209 all_output_options[i].str, event_type(j));
1210 } else
1211 output[j].fields |= all_output_options[i].field;
1212 }
1213 } else {
1214 if (output[type].invalid_fields & all_output_options[i].field) {
1215 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1216 all_output_options[i].str, event_type(type));
1217
1218 rc = -EINVAL;
1219 goto out;
1220 }
1221 output[type].fields |= all_output_options[i].field;
1222 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001223 }
1224
1225 if (type >= 0) {
1226 if (output[type].fields == 0) {
1227 pr_debug("No fields requested for %s type. "
1228 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001229 }
David Ahern1424dc92011-03-09 22:23:28 -07001230 }
David Ahern745f43e2011-03-09 22:23:26 -07001231
David Ahern2c9e45f72011-03-17 10:03:21 -06001232out:
David Ahern745f43e2011-03-09 22:23:26 -07001233 free(str);
1234 return rc;
1235}
1236
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001237/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1238static int is_directory(const char *base_path, const struct dirent *dent)
1239{
1240 char path[PATH_MAX];
1241 struct stat st;
1242
1243 sprintf(path, "%s/%s", base_path, dent->d_name);
1244 if (stat(path, &st))
1245 return 0;
1246
1247 return S_ISDIR(st.st_mode);
1248}
1249
1250#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001251 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1252 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001253 if ((lang_dirent.d_type == DT_DIR || \
1254 (lang_dirent.d_type == DT_UNKNOWN && \
1255 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001256 (strcmp(lang_dirent.d_name, ".")) && \
1257 (strcmp(lang_dirent.d_name, "..")))
1258
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001259#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001260 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1261 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001262 if (script_dirent.d_type != DT_DIR && \
1263 (script_dirent.d_type != DT_UNKNOWN || \
1264 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001265
1266
1267#define RECORD_SUFFIX "-record"
1268#define REPORT_SUFFIX "-report"
1269
1270struct script_desc {
1271 struct list_head node;
1272 char *name;
1273 char *half_liner;
1274 char *args;
1275};
1276
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001277static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001278
1279static struct script_desc *script_desc__new(const char *name)
1280{
1281 struct script_desc *s = zalloc(sizeof(*s));
1282
Tom Zanussib5b87312010-11-10 08:16:51 -06001283 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001284 s->name = strdup(name);
1285
1286 return s;
1287}
1288
1289static void script_desc__delete(struct script_desc *s)
1290{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001291 zfree(&s->name);
1292 zfree(&s->half_liner);
1293 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001294 free(s);
1295}
1296
1297static void script_desc__add(struct script_desc *s)
1298{
1299 list_add_tail(&s->node, &script_descs);
1300}
1301
1302static struct script_desc *script_desc__find(const char *name)
1303{
1304 struct script_desc *s;
1305
1306 list_for_each_entry(s, &script_descs, node)
1307 if (strcasecmp(s->name, name) == 0)
1308 return s;
1309 return NULL;
1310}
1311
1312static struct script_desc *script_desc__findnew(const char *name)
1313{
1314 struct script_desc *s = script_desc__find(name);
1315
1316 if (s)
1317 return s;
1318
1319 s = script_desc__new(name);
1320 if (!s)
1321 goto out_delete_desc;
1322
1323 script_desc__add(s);
1324
1325 return s;
1326
1327out_delete_desc:
1328 script_desc__delete(s);
1329
1330 return NULL;
1331}
1332
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001333static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001334{
1335 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001336 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001337
1338 if (strlen(str) > suffix_len) {
1339 p = str + strlen(str) - suffix_len;
1340 if (!strncmp(p, suffix, suffix_len))
1341 return p;
1342 }
1343
1344 return NULL;
1345}
1346
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001347static int read_script_info(struct script_desc *desc, const char *filename)
1348{
1349 char line[BUFSIZ], *p;
1350 FILE *fp;
1351
1352 fp = fopen(filename, "r");
1353 if (!fp)
1354 return -1;
1355
1356 while (fgets(line, sizeof(line), fp)) {
1357 p = ltrim(line);
1358 if (strlen(p) == 0)
1359 continue;
1360 if (*p != '#')
1361 continue;
1362 p++;
1363 if (strlen(p) && *p == '!')
1364 continue;
1365
1366 p = ltrim(p);
1367 if (strlen(p) && p[strlen(p) - 1] == '\n')
1368 p[strlen(p) - 1] = '\0';
1369
1370 if (!strncmp(p, "description:", strlen("description:"))) {
1371 p += strlen("description:");
1372 desc->half_liner = strdup(ltrim(p));
1373 continue;
1374 }
1375
1376 if (!strncmp(p, "args:", strlen("args:"))) {
1377 p += strlen("args:");
1378 desc->args = strdup(ltrim(p));
1379 continue;
1380 }
1381 }
1382
1383 fclose(fp);
1384
1385 return 0;
1386}
1387
Robert Richter38efb532011-11-25 11:38:40 +01001388static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1389{
1390 char *script_root, *str;
1391
1392 script_root = strdup(script_dirent->d_name);
1393 if (!script_root)
1394 return NULL;
1395
1396 str = (char *)ends_with(script_root, suffix);
1397 if (!str) {
1398 free(script_root);
1399 return NULL;
1400 }
1401
1402 *str = '\0';
1403 return script_root;
1404}
1405
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001406static int list_available_scripts(const struct option *opt __maybe_unused,
1407 const char *s __maybe_unused,
1408 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001409{
1410 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1411 char scripts_path[MAXPATHLEN];
1412 DIR *scripts_dir, *lang_dir;
1413 char script_path[MAXPATHLEN];
1414 char lang_path[MAXPATHLEN];
1415 struct script_desc *desc;
1416 char first_half[BUFSIZ];
1417 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001418
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001419 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001420
1421 scripts_dir = opendir(scripts_path);
1422 if (!scripts_dir)
1423 return -1;
1424
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001425 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001426 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1427 lang_dirent.d_name);
1428 lang_dir = opendir(lang_path);
1429 if (!lang_dir)
1430 continue;
1431
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001432 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001433 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1434 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001435 desc = script_desc__findnew(script_root);
1436 snprintf(script_path, MAXPATHLEN, "%s/%s",
1437 lang_path, script_dirent.d_name);
1438 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001439 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001440 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001441 }
1442 }
1443
1444 fprintf(stdout, "List of available trace scripts:\n");
1445 list_for_each_entry(desc, &script_descs, node) {
1446 sprintf(first_half, "%s %s", desc->name,
1447 desc->args ? desc->args : "");
1448 fprintf(stdout, " %-36s %s\n", first_half,
1449 desc->half_liner ? desc->half_liner : "");
1450 }
1451
1452 exit(0);
1453}
1454
Feng Tange5f37052012-09-07 16:42:26 +08001455/*
Feng Tang49e639e2012-10-30 11:56:03 +08001456 * Some scripts specify the required events in their "xxx-record" file,
1457 * this function will check if the events in perf.data match those
1458 * mentioned in the "xxx-record".
1459 *
1460 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1461 * which is covered well now. And new parsing code should be added to
1462 * cover the future complexing formats like event groups etc.
1463 */
1464static int check_ev_match(char *dir_name, char *scriptname,
1465 struct perf_session *session)
1466{
1467 char filename[MAXPATHLEN], evname[128];
1468 char line[BUFSIZ], *p;
1469 struct perf_evsel *pos;
1470 int match, len;
1471 FILE *fp;
1472
1473 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1474
1475 fp = fopen(filename, "r");
1476 if (!fp)
1477 return -1;
1478
1479 while (fgets(line, sizeof(line), fp)) {
1480 p = ltrim(line);
1481 if (*p == '#')
1482 continue;
1483
1484 while (strlen(p)) {
1485 p = strstr(p, "-e");
1486 if (!p)
1487 break;
1488
1489 p += 2;
1490 p = ltrim(p);
1491 len = strcspn(p, " \t");
1492 if (!len)
1493 break;
1494
1495 snprintf(evname, len + 1, "%s", p);
1496
1497 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001498 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001499 if (!strcmp(perf_evsel__name(pos), evname)) {
1500 match = 1;
1501 break;
1502 }
1503 }
1504
1505 if (!match) {
1506 fclose(fp);
1507 return -1;
1508 }
1509 }
1510 }
1511
1512 fclose(fp);
1513 return 0;
1514}
1515
1516/*
Feng Tange5f37052012-09-07 16:42:26 +08001517 * Return -1 if none is found, otherwise the actual scripts number.
1518 *
1519 * Currently the only user of this function is the script browser, which
1520 * will list all statically runnable scripts, select one, execute it and
1521 * show the output in a perf browser.
1522 */
1523int find_scripts(char **scripts_array, char **scripts_path_array)
1524{
1525 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001526 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001527 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001528 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001529 struct perf_data_file file = {
1530 .path = input_name,
1531 .mode = PERF_DATA_MODE_READ,
1532 };
Feng Tange5f37052012-09-07 16:42:26 +08001533 char *temp;
1534 int i = 0;
1535
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001536 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001537 if (!session)
1538 return -1;
1539
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001540 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08001541
1542 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001543 if (!scripts_dir) {
1544 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001545 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001546 }
Feng Tange5f37052012-09-07 16:42:26 +08001547
1548 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1549 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1550 lang_dirent.d_name);
1551#ifdef NO_LIBPERL
1552 if (strstr(lang_path, "perl"))
1553 continue;
1554#endif
1555#ifdef NO_LIBPYTHON
1556 if (strstr(lang_path, "python"))
1557 continue;
1558#endif
1559
1560 lang_dir = opendir(lang_path);
1561 if (!lang_dir)
1562 continue;
1563
1564 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1565 /* Skip those real time scripts: xxxtop.p[yl] */
1566 if (strstr(script_dirent.d_name, "top."))
1567 continue;
1568 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1569 script_dirent.d_name);
1570 temp = strchr(script_dirent.d_name, '.');
1571 snprintf(scripts_array[i],
1572 (temp - script_dirent.d_name) + 1,
1573 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001574
1575 if (check_ev_match(lang_path,
1576 scripts_array[i], session))
1577 continue;
1578
Feng Tange5f37052012-09-07 16:42:26 +08001579 i++;
1580 }
Feng Tang49e639e2012-10-30 11:56:03 +08001581 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001582 }
1583
Feng Tang49e639e2012-10-30 11:56:03 +08001584 closedir(scripts_dir);
1585 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001586 return i;
1587}
1588
Tom Zanussi38752942009-12-15 02:53:39 -06001589static char *get_script_path(const char *script_root, const char *suffix)
1590{
1591 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1592 char scripts_path[MAXPATHLEN];
1593 char script_path[MAXPATHLEN];
1594 DIR *scripts_dir, *lang_dir;
1595 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001596 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001597
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001598 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06001599
1600 scripts_dir = opendir(scripts_path);
1601 if (!scripts_dir)
1602 return NULL;
1603
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001604 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001605 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1606 lang_dirent.d_name);
1607 lang_dir = opendir(lang_path);
1608 if (!lang_dir)
1609 continue;
1610
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001611 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001612 __script_root = get_script_root(&script_dirent, suffix);
1613 if (__script_root && !strcmp(script_root, __script_root)) {
1614 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001615 closedir(lang_dir);
1616 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001617 snprintf(script_path, MAXPATHLEN, "%s/%s",
1618 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001619 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001620 }
1621 free(__script_root);
1622 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001623 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001624 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001625 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001626
Robert Richter38efb532011-11-25 11:38:40 +01001627 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001628}
1629
Tom Zanussib5b87312010-11-10 08:16:51 -06001630static bool is_top_script(const char *script_path)
1631{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001632 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001633}
1634
1635static int has_required_arg(char *script_path)
1636{
1637 struct script_desc *desc;
1638 int n_args = 0;
1639 char *p;
1640
1641 desc = script_desc__new(NULL);
1642
1643 if (read_script_info(desc, script_path))
1644 goto out;
1645
1646 if (!desc->args)
1647 goto out;
1648
1649 for (p = desc->args; *p; p++)
1650 if (*p == '<')
1651 n_args++;
1652out:
1653 script_desc__delete(desc);
1654
1655 return n_args;
1656}
1657
David Ahernd54b1a92012-08-26 12:24:46 -06001658static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001659{
1660 char **__argv = malloc(sizeof(const char *) * argc);
1661
David Ahernd54b1a92012-08-26 12:24:46 -06001662 if (!__argv) {
1663 pr_err("malloc failed\n");
1664 return -1;
1665 }
1666
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001667 memcpy(__argv, argv, sizeof(const char *) * argc);
1668 argc = parse_options(argc, (const char **)__argv, record_options,
1669 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1670 free(__argv);
1671
David Ahernd54b1a92012-08-26 12:24:46 -06001672 system_wide = (argc == 0);
1673
1674 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001675}
1676
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001677static void script__setup_sample_type(struct perf_script *script)
1678{
1679 struct perf_session *session = script->session;
1680 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1681
1682 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1683 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1684 (sample_type & PERF_SAMPLE_STACK_USER))
1685 callchain_param.record_mode = CALLCHAIN_DWARF;
1686 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1687 callchain_param.record_mode = CALLCHAIN_LBR;
1688 else
1689 callchain_param.record_mode = CALLCHAIN_FP;
1690 }
1691}
1692
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001693static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1694 union perf_event *event,
1695 struct perf_session *session __maybe_unused)
1696{
1697 perf_event__read_stat_config(&stat_config, &event->stat_config);
1698 return 0;
1699}
1700
Jiri Olsacfc88742016-01-05 22:09:06 +01001701static int set_maps(struct perf_script *script)
1702{
1703 struct perf_evlist *evlist = script->session->evlist;
1704
1705 if (!script->cpus || !script->threads)
1706 return 0;
1707
1708 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1709 return -EINVAL;
1710
1711 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1712
1713 if (perf_evlist__alloc_stats(evlist, true))
1714 return -ENOMEM;
1715
1716 script->allocated = true;
1717 return 0;
1718}
1719
1720static
1721int process_thread_map_event(struct perf_tool *tool,
1722 union perf_event *event,
1723 struct perf_session *session __maybe_unused)
1724{
1725 struct perf_script *script = container_of(tool, struct perf_script, tool);
1726
1727 if (script->threads) {
1728 pr_warning("Extra thread map event, ignoring.\n");
1729 return 0;
1730 }
1731
1732 script->threads = thread_map__new_event(&event->thread_map);
1733 if (!script->threads)
1734 return -ENOMEM;
1735
1736 return set_maps(script);
1737}
1738
1739static
1740int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1741 union perf_event *event,
1742 struct perf_session *session __maybe_unused)
1743{
1744 struct perf_script *script = container_of(tool, struct perf_script, tool);
1745
1746 if (script->cpus) {
1747 pr_warning("Extra cpu map event, ignoring.\n");
1748 return 0;
1749 }
1750
1751 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1752 if (!script->cpus)
1753 return -ENOMEM;
1754
1755 return set_maps(script);
1756}
1757
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001758int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001759{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001760 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001761 bool header = false;
1762 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001763 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001764 char *rec_script_path = NULL;
1765 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001766 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001767 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001768 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001769 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001770 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001771 struct perf_script script = {
1772 .tool = {
1773 .sample = process_sample_event,
1774 .mmap = perf_event__process_mmap,
1775 .mmap2 = perf_event__process_mmap2,
1776 .comm = perf_event__process_comm,
1777 .exit = perf_event__process_exit,
1778 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001779 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001780 .tracing_data = perf_event__process_tracing_data,
1781 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001782 .id_index = perf_event__process_id_index,
1783 .auxtrace_info = perf_event__process_auxtrace_info,
1784 .auxtrace = perf_event__process_auxtrace,
1785 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001786 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01001787 .thread_map = process_thread_map_event,
1788 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001789 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001790 .ordering_requires_timestamps = true,
1791 },
1792 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001793 struct perf_data_file file = {
1794 .mode = PERF_DATA_MODE_READ,
1795 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001796 const struct option options[] = {
1797 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1798 "dump raw trace in ASCII"),
1799 OPT_INCR('v', "verbose", &verbose,
1800 "be more verbose (show symbol address, etc)"),
1801 OPT_BOOLEAN('L', "Latency", &latency_format,
1802 "show latency attributes (irqs/preemption disabled, etc)"),
1803 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1804 list_available_scripts),
1805 OPT_CALLBACK('s', "script", NULL, "name",
1806 "script file name (lang:script name, script name, or *)",
1807 parse_scriptname),
1808 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1809 "generate perf-script.xx script in specified language"),
1810 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1811 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1812 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001813 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1814 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001815 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1816 "file", "vmlinux pathname"),
1817 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1818 "file", "kallsyms pathname"),
1819 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1820 "When printing symbols do not display call chain"),
1821 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1822 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001823 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001824 "comma separated output fields prepend with 'type:'. "
1825 "Valid types: hw,sw,trace,raw. "
1826 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001827 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001828 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1829 "system-wide collection from all CPUs"),
1830 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1831 "only consider these symbols"),
1832 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1833 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1834 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06001835 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1836 "only consider symbols in these pids"),
1837 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1838 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001839 OPT_BOOLEAN('I', "show-info", &show_full_info,
1840 "display extended information from perf.data file"),
1841 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1842 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001843 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1844 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001845 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1846 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03001847 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1848 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001849 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03001850 OPT_BOOLEAN(0, "ns", &nanosecs,
1851 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001852 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1853 "Instruction Tracing options",
1854 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07001855 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1856 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07001857 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1858 "Enable symbol demangling"),
1859 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1860 "Enable kernel symbol demangling"),
1861
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001862 OPT_END()
1863 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08001864 const char * const script_subcommands[] = { "record", "report", NULL };
1865 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001866 "perf script [<options>]",
1867 "perf script [<options>] record <script> [<record-options>] <command>",
1868 "perf script [<options>] report <script> [script-args]",
1869 "perf script [<options>] <script> [<record-options>] <command>",
1870 "perf script [<options>] <top-script> [script-args]",
1871 NULL
1872 };
Tom Zanussi38752942009-12-15 02:53:39 -06001873
Tom Zanussib5b87312010-11-10 08:16:51 -06001874 setup_scripting();
1875
Yunlong Song40cae2b2015-03-18 21:35:54 +08001876 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001877 PARSE_OPT_STOP_AT_NON_OPTION);
1878
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001879 file.path = input_name;
1880
Tom Zanussib5b87312010-11-10 08:16:51 -06001881 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1882 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1883 if (!rec_script_path)
1884 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001885 }
1886
Tom Zanussib5b87312010-11-10 08:16:51 -06001887 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1888 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1889 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001890 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001891 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001892 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001893 return -1;
1894 }
Tom Zanussi38752942009-12-15 02:53:39 -06001895 }
1896
Adrian Hunter3c5b6452015-09-25 16:15:51 +03001897 if (itrace_synth_opts.callchain &&
1898 itrace_synth_opts.callchain_sz > scripting_max_stack)
1899 scripting_max_stack = itrace_synth_opts.callchain_sz;
1900
Ben Hutchings44e668c2010-10-10 16:10:03 +01001901 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001902 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01001903
Tom Zanussib5b87312010-11-10 08:16:51 -06001904 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001905 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001906 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001907 pid_t pid;
1908
Tom Zanussib5b87312010-11-10 08:16:51 -06001909 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1910 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1911
1912 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001913 usage_with_options_msg(script_usage, options,
1914 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001915 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001916 }
1917
Tom Zanussib5b87312010-11-10 08:16:51 -06001918 if (is_top_script(argv[0])) {
1919 rep_args = argc - 1;
1920 } else {
1921 int rec_args;
1922
1923 rep_args = has_required_arg(rep_script_path);
1924 rec_args = (argc - 1) - rep_args;
1925 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001926 usage_with_options_msg(script_usage, options,
1927 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001928 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001929 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06001930 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001931 }
1932
1933 if (pipe(live_pipe) < 0) {
1934 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001935 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001936 }
1937
1938 pid = fork();
1939 if (pid < 0) {
1940 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001941 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001942 }
1943
1944 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001945 j = 0;
1946
Tom Zanussia0cccc22010-04-01 23:59:25 -05001947 dup2(live_pipe[1], 1);
1948 close(live_pipe[0]);
1949
Robert Richter317df652011-11-25 15:05:25 +01001950 if (is_top_script(argv[0])) {
1951 system_wide = true;
1952 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001953 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1954 err = -1;
1955 goto out;
1956 }
Robert Richter317df652011-11-25 15:05:25 +01001957 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001958
1959 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001960 if (!__argv) {
1961 pr_err("malloc failed\n");
1962 err = -ENOMEM;
1963 goto out;
1964 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001965
Tom Zanussib5b87312010-11-10 08:16:51 -06001966 __argv[j++] = "/bin/sh";
1967 __argv[j++] = rec_script_path;
1968 if (system_wide)
1969 __argv[j++] = "-a";
1970 __argv[j++] = "-q";
1971 __argv[j++] = "-o";
1972 __argv[j++] = "-";
1973 for (i = rep_args + 1; i < argc; i++)
1974 __argv[j++] = argv[i];
1975 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001976
1977 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001978 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001979 exit(-1);
1980 }
1981
1982 dup2(live_pipe[0], 0);
1983 close(live_pipe[1]);
1984
Tom Zanussib5b87312010-11-10 08:16:51 -06001985 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001986 if (!__argv) {
1987 pr_err("malloc failed\n");
1988 err = -ENOMEM;
1989 goto out;
1990 }
1991
Tom Zanussib5b87312010-11-10 08:16:51 -06001992 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001993 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001994 __argv[j++] = rep_script_path;
1995 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001996 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001997 __argv[j++] = "-i";
1998 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001999 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002000
2001 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002002 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002003 exit(-1);
2004 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002005
Tom Zanussib5b87312010-11-10 08:16:51 -06002006 if (rec_script_path)
2007 script_path = rec_script_path;
2008 if (rep_script_path)
2009 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002010
Tom Zanussib5b87312010-11-10 08:16:51 -06002011 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002012 j = 0;
2013
Robert Richter317df652011-11-25 15:05:25 +01002014 if (!rec_script_path)
2015 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002016 else if (!system_wide) {
2017 if (have_cmd(argc - 1, &argv[1]) != 0) {
2018 err = -1;
2019 goto out;
2020 }
2021 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002022
2023 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002024 if (!__argv) {
2025 pr_err("malloc failed\n");
2026 err = -ENOMEM;
2027 goto out;
2028 }
2029
Tom Zanussib5b87312010-11-10 08:16:51 -06002030 __argv[j++] = "/bin/sh";
2031 __argv[j++] = script_path;
2032 if (system_wide)
2033 __argv[j++] = "-a";
2034 for (i = 2; i < argc; i++)
2035 __argv[j++] = argv[i];
2036 __argv[j++] = NULL;
2037
2038 execvp("/bin/sh", (char **)__argv);
2039 free(__argv);
2040 exit(-1);
2041 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002042
Tom Zanussicf4fee52010-03-03 01:04:33 -06002043 if (!script_name)
2044 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002045
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002046 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002047 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002048 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002049
Jiri Olsae90debd2013-12-09 11:02:50 +01002050 if (header || header_only) {
2051 perf_session__fprintf_info(session, stdout, show_full_info);
2052 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002053 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002054 }
2055
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002056 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002057 goto out_delete;
2058
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002059 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002060 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002061
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002062 session->itrace_synth_opts = &itrace_synth_opts;
2063
Anton Blanchard5d67be92011-07-04 21:57:50 +10002064 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002065 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2066 if (err < 0)
2067 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002068 }
2069
David Ahern1424dc92011-03-09 22:23:28 -07002070 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002071 symbol_conf.use_callchain = true;
2072 else
2073 symbol_conf.use_callchain = false;
2074
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002075 if (session->tevent.pevent &&
2076 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002077 machine__resolve_kernel_addr,
2078 &session->machines.host) < 0) {
2079 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2080 return -1;
2081 }
2082
Tom Zanussi956ffd02009-11-25 01:15:46 -06002083 if (generate_script_lang) {
2084 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002085 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002086
David Ahern2c9e45f72011-03-17 10:03:21 -06002087 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002088 fprintf(stderr,
2089 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002090 err = -EINVAL;
2091 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002092 }
2093
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002094 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002095 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002096 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002097 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002098 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002099 }
2100
2101 err = fstat(input, &perf_stat);
2102 if (err < 0) {
2103 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002104 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002105 }
2106
2107 if (!perf_stat.st_size) {
2108 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002109 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002110 }
2111
2112 scripting_ops = script_spec__lookup(generate_script_lang);
2113 if (!scripting_ops) {
2114 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002115 err = -ENOENT;
2116 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002117 }
2118
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002119 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002120 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002121 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002122 }
2123
2124 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002125 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002126 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002127 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002128 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002129 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002130 }
2131
David Ahern9cbdb702011-04-06 21:54:20 -06002132
2133 err = perf_session__check_output_opt(session);
2134 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002135 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002136
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002137 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002138
Adrian Hunterd445dd22014-08-15 22:08:37 +03002139 flush_scripting();
2140
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002141out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002142 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002143 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002144
2145 if (script_started)
2146 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002147out:
2148 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002149}