blob: 5e1865408aa5b61041ea031eb01cef195cb7e67e [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
Jiri Olsae099eba2016-01-05 22:09:09 +0100224 if (perf_header__has_feat(&session->header, HEADER_STAT))
225 return 0;
226
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300227 allow_user_set = perf_header__has_feat(&session->header,
228 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600229
David Ahern1424dc92011-03-09 22:23:28 -0700230 if (PRINT_FIELD(TRACE) &&
231 !perf_session__has_traces(session, "record -R"))
232 return -EINVAL;
233
David Ahern787bef12011-05-27 14:28:43 -0600234 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300235 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
236 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700237 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700238 }
David Ahern7cec0922011-05-30 13:08:23 -0600239
240 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300241 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
242 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600243 return -EINVAL;
244
245 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
246 pr_err("Display of symbols requested but neither sample IP nor "
247 "sample address\nis selected. Hence, no addresses to convert "
248 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600249 return -EINVAL;
250 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900251 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
252 pr_err("Display of offsets requested but symbol is not"
253 "selected.\n");
254 return -EINVAL;
255 }
David Ahern7cec0922011-05-30 13:08:23 -0600256 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
257 pr_err("Display of DSO requested but neither sample IP nor "
258 "sample address\nis selected. Hence, no addresses to convert "
259 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600260 return -EINVAL;
261 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200262 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
263 pr_err("Display of source line number requested but sample IP is not\n"
264 "selected. Hence, no address to lookup the source line number.\n");
265 return -EINVAL;
266 }
David Ahern1424dc92011-03-09 22:23:28 -0700267
268 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300269 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
270 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700271 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700272
273 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300274 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
275 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700276 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700277
278 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300279 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
280 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700281 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600282
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200283 if (PRINT_FIELD(PERIOD) &&
284 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
285 PERF_OUTPUT_PERIOD))
286 return -EINVAL;
287
Stephane Eranianfc36f942015-08-31 18:41:10 +0200288 if (PRINT_FIELD(IREGS) &&
289 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
290 PERF_OUTPUT_IREGS))
291 return -EINVAL;
292
David Ahern9cbdb702011-04-06 21:54:20 -0600293 return 0;
294}
295
Adrian Hunter7ea95722013-11-01 15:51:30 +0200296static void set_print_ip_opts(struct perf_event_attr *attr)
297{
298 unsigned int type = attr->type;
299
300 output[type].print_ip_opts = 0;
301 if (PRINT_FIELD(IP))
302 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
303
304 if (PRINT_FIELD(SYM))
305 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
306
307 if (PRINT_FIELD(DSO))
308 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
309
310 if (PRINT_FIELD(SYMOFFSET))
311 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200312
313 if (PRINT_FIELD(SRCLINE))
314 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200315}
316
David Ahern9cbdb702011-04-06 21:54:20 -0600317/*
318 * verify all user requested events exist and the samples
319 * have the expected data
320 */
321static int perf_session__check_output_opt(struct perf_session *session)
322{
323 int j;
324 struct perf_evsel *evsel;
325
326 for (j = 0; j < PERF_TYPE_MAX; ++j) {
327 evsel = perf_session__find_first_evtype(session, j);
328
329 /*
330 * even if fields is set to 0 (ie., show nothing) event must
331 * exist if user explicitly includes it on the command line
332 */
333 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
334 pr_err("%s events do not exist. "
335 "Remove corresponding -f option to proceed.\n",
336 event_type(j));
337 return -1;
338 }
339
340 if (evsel && output[j].fields &&
341 perf_evsel__check_attr(evsel, session))
342 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400343
344 if (evsel == NULL)
345 continue;
346
Adrian Hunter7ea95722013-11-01 15:51:30 +0200347 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700348 }
349
Adrian Hunter98526ee2014-07-31 09:00:59 +0300350 if (!no_callchain) {
351 bool use_callchain = false;
352
353 evlist__for_each(session->evlist, evsel) {
354 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
355 use_callchain = true;
356 break;
357 }
358 }
359 if (!use_callchain)
360 symbol_conf.use_callchain = false;
361 }
362
David Ahern80b8b492013-11-19 21:07:37 -0700363 /*
364 * set default for tracepoints to print symbols only
365 * if callchains are present
366 */
367 if (symbol_conf.use_callchain &&
368 !output[PERF_TYPE_TRACEPOINT].user_set) {
369 struct perf_event_attr *attr;
370
371 j = PERF_TYPE_TRACEPOINT;
372 evsel = perf_session__find_first_evtype(session, j);
373 if (evsel == NULL)
374 goto out;
375
376 attr = &evsel->attr;
377
378 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
379 output[j].fields |= PERF_OUTPUT_IP;
380 output[j].fields |= PERF_OUTPUT_SYM;
381 output[j].fields |= PERF_OUTPUT_DSO;
382 set_print_ip_opts(attr);
383 }
384 }
385
386out:
David Ahern1424dc92011-03-09 22:23:28 -0700387 return 0;
388}
David Ahern745f43e2011-03-09 22:23:26 -0700389
Stephane Eranianfc36f942015-08-31 18:41:10 +0200390static void print_sample_iregs(union perf_event *event __maybe_unused,
391 struct perf_sample *sample,
392 struct thread *thread __maybe_unused,
393 struct perf_event_attr *attr)
394{
395 struct regs_dump *regs = &sample->intr_regs;
396 uint64_t mask = attr->sample_regs_intr;
397 unsigned i = 0, r;
398
399 if (!regs)
400 return;
401
402 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
403 u64 val = regs->regs[i++];
404 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
405 }
406}
407
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300408static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700409 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300410 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700411{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300412 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700413 unsigned long secs;
414 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700415 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700416
David Ahern745f43e2011-03-09 22:23:26 -0700417 if (PRINT_FIELD(COMM)) {
418 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200419 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600420 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200421 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700422 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200423 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700424 }
David Ahernc70c94b2011-03-09 22:23:25 -0700425
David Ahern745f43e2011-03-09 22:23:26 -0700426 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
427 printf("%5d/%-5d ", sample->pid, sample->tid);
428 else if (PRINT_FIELD(PID))
429 printf("%5d ", sample->pid);
430 else if (PRINT_FIELD(TID))
431 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700432
David Ahern745f43e2011-03-09 22:23:26 -0700433 if (PRINT_FIELD(CPU)) {
434 if (latency_format)
435 printf("%3d ", sample->cpu);
436 else
437 printf("[%03d] ", sample->cpu);
438 }
David Ahernc70c94b2011-03-09 22:23:25 -0700439
David Ahern745f43e2011-03-09 22:23:26 -0700440 if (PRINT_FIELD(TIME)) {
441 nsecs = sample->time;
442 secs = nsecs / NSECS_PER_SEC;
443 nsecs -= secs * NSECS_PER_SEC;
444 usecs = nsecs / NSECS_PER_USEC;
Adrian Hunter83e19862015-09-25 16:15:36 +0300445 if (nanosecs)
446 printf("%5lu.%09llu: ", secs, nsecs);
447 else
448 printf("%5lu.%06lu: ", secs, usecs);
David Ahern745f43e2011-03-09 22:23:26 -0700449 }
David Ahernc70c94b2011-03-09 22:23:25 -0700450}
451
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200452static inline char
453mispred_str(struct branch_entry *br)
454{
455 if (!(br->flags.mispred || br->flags.predicted))
456 return '-';
457
458 return br->flags.predicted ? 'P' : 'M';
459}
460
461static void print_sample_brstack(union perf_event *event __maybe_unused,
462 struct perf_sample *sample,
463 struct thread *thread __maybe_unused,
464 struct perf_event_attr *attr __maybe_unused)
465{
466 struct branch_stack *br = sample->branch_stack;
467 u64 i;
468
469 if (!(br && br->nr))
470 return;
471
472 for (i = 0; i < br->nr; i++) {
473 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
474 br->entries[i].from,
475 br->entries[i].to,
476 mispred_str( br->entries + i),
477 br->entries[i].flags.in_tx? 'X' : '-',
478 br->entries[i].flags.abort? 'A' : '-',
479 br->entries[i].flags.cycles);
480 }
481}
482
483static void print_sample_brstacksym(union perf_event *event __maybe_unused,
484 struct perf_sample *sample,
485 struct thread *thread __maybe_unused,
486 struct perf_event_attr *attr __maybe_unused)
487{
488 struct branch_stack *br = sample->branch_stack;
489 struct addr_location alf, alt;
490 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
491 u64 i, from, to;
492
493 if (!(br && br->nr))
494 return;
495
496 for (i = 0; i < br->nr; i++) {
497
498 memset(&alf, 0, sizeof(alf));
499 memset(&alt, 0, sizeof(alt));
500 from = br->entries[i].from;
501 to = br->entries[i].to;
502
503 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
504 if (alf.map)
505 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
506
507 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
508 if (alt.map)
509 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
510
511 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
512 putchar('/');
513 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
514 printf("/%c/%c/%c/%d ",
515 mispred_str( br->entries + i),
516 br->entries[i].flags.in_tx? 'X' : '-',
517 br->entries[i].flags.abort? 'A' : '-',
518 br->entries[i].flags.cycles);
519 }
520}
521
522
David Ahern7cec0922011-05-30 13:08:23 -0600523static void print_sample_addr(union perf_event *event,
524 struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600525 struct thread *thread,
526 struct perf_event_attr *attr)
527{
528 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600529
530 printf("%16" PRIx64, sample->addr);
531
532 if (!sample_addr_correlates_sym(attr))
533 return;
534
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300535 perf_event__preprocess_sample_addr(event, sample, thread, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600536
537 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900538 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900539 if (PRINT_FIELD(SYMOFFSET))
540 symbol__fprintf_symname_offs(al.sym, &al, stdout);
541 else
542 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600543 }
544
545 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900546 printf(" (");
547 map__fprintf_dsoname(al.map, stdout);
548 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600549 }
550}
551
Akihiro Nagai95582592012-01-30 13:43:09 +0900552static void print_sample_bts(union perf_event *event,
553 struct perf_sample *sample,
554 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200555 struct thread *thread,
556 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900557{
558 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300559 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900560
561 /* print branch_from information */
562 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300563 unsigned int print_opts = output[attr->type].print_ip_opts;
564
565 if (symbol_conf.use_callchain && sample->callchain) {
Akihiro Nagai95582592012-01-30 13:43:09 +0900566 printf("\n");
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300567 } else {
568 printf(" ");
569 if (print_opts & PRINT_IP_OPT_SRCLINE) {
570 print_srcline_last = true;
571 print_opts &= ~PRINT_IP_OPT_SRCLINE;
572 }
573 }
574 perf_evsel__print_ip(evsel, sample, al, print_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300575 scripting_max_stack);
Akihiro Nagai95582592012-01-30 13:43:09 +0900576 }
577
Akihiro Nagai95582592012-01-30 13:43:09 +0900578 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300579 if (PRINT_FIELD(ADDR) ||
580 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300581 !output[attr->type].user_set)) {
582 printf(" => ");
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300583 print_sample_addr(event, sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300584 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900585
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300586 if (print_srcline_last)
587 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
588
Akihiro Nagai95582592012-01-30 13:43:09 +0900589 printf("\n");
590}
591
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300592static void print_sample_flags(u32 flags)
593{
594 const char *chars = PERF_IP_FLAG_CHARS;
595 const int n = strlen(PERF_IP_FLAG_CHARS);
596 char str[33];
597 int i, pos = 0;
598
599 for (i = 0; i < n; i++, flags >>= 1) {
600 if (flags & 1)
601 str[pos++] = chars[i];
602 }
603 for (; i < 32; i++, flags >>= 1) {
604 if (flags & 1)
605 str[pos++] = '?';
606 }
607 str[pos] = 0;
608 printf(" %-4s ", str);
609}
610
Jiri Olsa809e9422015-11-26 18:55:21 +0100611struct perf_script {
612 struct perf_tool tool;
613 struct perf_session *session;
614 bool show_task_events;
615 bool show_mmap_events;
616 bool show_switch_events;
Jiri Olsacfc88742016-01-05 22:09:06 +0100617 bool allocated;
618 struct cpu_map *cpus;
619 struct thread_map *threads;
Jiri Olsa809e9422015-11-26 18:55:21 +0100620};
621
622static void process_event(struct perf_script *script __maybe_unused, union perf_event *event,
623 struct perf_sample *sample, struct perf_evsel *evsel,
624 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700625{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300626 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300627 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700628
David Ahern2c9e45f72011-03-17 10:03:21 -0600629 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700630 return;
631
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300632 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700633
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200634 if (PRINT_FIELD(PERIOD))
635 printf("%10" PRIu64 " ", sample->period);
636
Namhyung Kime944d3d2013-11-18 14:34:52 +0900637 if (PRINT_FIELD(EVNAME)) {
638 const char *evname = perf_evsel__name(evsel);
639 printf("%s: ", evname ? evname : "[unknown]");
640 }
641
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300642 if (print_flags)
643 print_sample_flags(sample->flags);
644
Akihiro Nagai95582592012-01-30 13:43:09 +0900645 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300646 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900647 return;
648 }
649
David Ahern745f43e2011-03-09 22:23:26 -0700650 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300651 event_format__print(evsel->tp_format, sample->cpu,
652 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600653 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300654 print_sample_addr(event, sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600655
David Ahern787bef12011-05-27 14:28:43 -0600656 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700657 if (!symbol_conf.use_callchain)
658 printf(" ");
659 else
660 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400661
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300662 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400663 output[attr->type].print_ip_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300664 scripting_max_stack);
David Ahernc0230b22011-03-09 22:23:27 -0700665 }
666
Stephane Eranianfc36f942015-08-31 18:41:10 +0200667 if (PRINT_FIELD(IREGS))
668 print_sample_iregs(event, sample, thread, attr);
669
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200670 if (PRINT_FIELD(BRSTACK))
671 print_sample_brstack(event, sample, thread, attr);
672 else if (PRINT_FIELD(BRSTACKSYM))
673 print_sample_brstacksym(event, sample, thread, attr);
674
David Ahernc70c94b2011-03-09 22:23:25 -0700675 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700676}
677
Tom Zanussi956ffd02009-11-25 01:15:46 -0600678static struct scripting_ops *scripting_ops;
679
Jiri Olsae099eba2016-01-05 22:09:09 +0100680static void process_stat(struct perf_evsel *counter, u64 tstamp)
681{
682 if (scripting_ops && scripting_ops->process_stat)
683 scripting_ops->process_stat(&stat_config, counter, tstamp);
684}
685
686static void process_stat_interval(u64 tstamp)
687{
688 if (scripting_ops && scripting_ops->process_stat_interval)
689 scripting_ops->process_stat_interval(tstamp);
690}
691
Tom Zanussi956ffd02009-11-25 01:15:46 -0600692static void setup_scripting(void)
693{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600694 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600695 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600696}
697
Adrian Hunterd445dd22014-08-15 22:08:37 +0300698static int flush_scripting(void)
699{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100700 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300701}
702
Tom Zanussi956ffd02009-11-25 01:15:46 -0600703static int cleanup_scripting(void)
704{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100705 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500706
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100707 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600708}
709
Jiri Olsa809e9422015-11-26 18:55:21 +0100710static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200711 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200712 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300713 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200714 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200715{
Jiri Olsa809e9422015-11-26 18:55:21 +0100716 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -0700717 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200718
David Ahern1424dc92011-03-09 22:23:28 -0700719 if (debug_mode) {
720 if (sample->time < last_timestamp) {
721 pr_err("Samples misordered, previous: %" PRIu64
722 " this: %" PRIu64 "\n", last_timestamp,
723 sample->time);
724 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200725 }
David Ahern1424dc92011-03-09 22:23:28 -0700726 last_timestamp = sample->time;
727 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200728 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000729
Adrian Huntere44baa32013-08-08 14:32:25 +0300730 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700731 pr_err("problem processing %d event, skipping it.\n",
732 event->header.type);
733 return -1;
734 }
735
736 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300737 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700738
Anton Blanchard5d67be92011-07-04 21:57:50 +1000739 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300740 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000741
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100742 if (scripting_ops)
743 scripting_ops->process_event(event, sample, evsel, &al);
744 else
Jiri Olsa809e9422015-11-26 18:55:21 +0100745 process_event(scr, event, sample, evsel, &al);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100746
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300747out_put:
748 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200749 return 0;
750}
751
Adrian Hunter7ea95722013-11-01 15:51:30 +0200752static int process_attr(struct perf_tool *tool, union perf_event *event,
753 struct perf_evlist **pevlist)
754{
755 struct perf_script *scr = container_of(tool, struct perf_script, tool);
756 struct perf_evlist *evlist;
757 struct perf_evsel *evsel, *pos;
758 int err;
759
760 err = perf_event__process_attr(tool, event, pevlist);
761 if (err)
762 return err;
763
764 evlist = *pevlist;
765 evsel = perf_evlist__last(*pevlist);
766
767 if (evsel->attr.type >= PERF_TYPE_MAX)
768 return 0;
769
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300770 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200771 if (pos->attr.type == evsel->attr.type && pos != evsel)
772 return 0;
773 }
774
775 set_print_ip_opts(&evsel->attr);
776
Jiri Olsad2b5a312015-10-16 12:41:25 +0200777 if (evsel->attr.sample_type)
778 err = perf_evsel__check_attr(evsel, scr->session);
779
780 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200781}
782
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900783static int process_comm_event(struct perf_tool *tool,
784 union perf_event *event,
785 struct perf_sample *sample,
786 struct machine *machine)
787{
788 struct thread *thread;
789 struct perf_script *script = container_of(tool, struct perf_script, tool);
790 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300791 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900792 int ret = -1;
793
794 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
795 if (thread == NULL) {
796 pr_debug("problem processing COMM event, skipping it.\n");
797 return -1;
798 }
799
800 if (perf_event__process_comm(tool, event, sample, machine) < 0)
801 goto out;
802
803 if (!evsel->attr.sample_id_all) {
804 sample->cpu = 0;
805 sample->time = 0;
806 sample->tid = event->comm.tid;
807 sample->pid = event->comm.pid;
808 }
809 print_sample_start(sample, thread, evsel);
810 perf_event__fprintf(event, stdout);
811 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900812out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300813 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900814 return ret;
815}
816
817static int process_fork_event(struct perf_tool *tool,
818 union perf_event *event,
819 struct perf_sample *sample,
820 struct machine *machine)
821{
822 struct thread *thread;
823 struct perf_script *script = container_of(tool, struct perf_script, tool);
824 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300825 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900826
827 if (perf_event__process_fork(tool, event, sample, machine) < 0)
828 return -1;
829
830 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
831 if (thread == NULL) {
832 pr_debug("problem processing FORK event, skipping it.\n");
833 return -1;
834 }
835
836 if (!evsel->attr.sample_id_all) {
837 sample->cpu = 0;
838 sample->time = event->fork.time;
839 sample->tid = event->fork.tid;
840 sample->pid = event->fork.pid;
841 }
842 print_sample_start(sample, thread, evsel);
843 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300844 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900845
846 return 0;
847}
848static int process_exit_event(struct perf_tool *tool,
849 union perf_event *event,
850 struct perf_sample *sample,
851 struct machine *machine)
852{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300853 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900854 struct thread *thread;
855 struct perf_script *script = container_of(tool, struct perf_script, tool);
856 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300857 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900858
859 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
860 if (thread == NULL) {
861 pr_debug("problem processing EXIT event, skipping it.\n");
862 return -1;
863 }
864
865 if (!evsel->attr.sample_id_all) {
866 sample->cpu = 0;
867 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +0300868 sample->tid = event->fork.tid;
869 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900870 }
871 print_sample_start(sample, thread, evsel);
872 perf_event__fprintf(event, stdout);
873
874 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300875 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900876
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300877 thread__put(thread);
878 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900879}
880
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900881static int process_mmap_event(struct perf_tool *tool,
882 union perf_event *event,
883 struct perf_sample *sample,
884 struct machine *machine)
885{
886 struct thread *thread;
887 struct perf_script *script = container_of(tool, struct perf_script, tool);
888 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300889 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900890
891 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
892 return -1;
893
894 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
895 if (thread == NULL) {
896 pr_debug("problem processing MMAP event, skipping it.\n");
897 return -1;
898 }
899
900 if (!evsel->attr.sample_id_all) {
901 sample->cpu = 0;
902 sample->time = 0;
903 sample->tid = event->mmap.tid;
904 sample->pid = event->mmap.pid;
905 }
906 print_sample_start(sample, thread, evsel);
907 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300908 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900909 return 0;
910}
911
912static int process_mmap2_event(struct perf_tool *tool,
913 union perf_event *event,
914 struct perf_sample *sample,
915 struct machine *machine)
916{
917 struct thread *thread;
918 struct perf_script *script = container_of(tool, struct perf_script, tool);
919 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300920 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900921
922 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
923 return -1;
924
925 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
926 if (thread == NULL) {
927 pr_debug("problem processing MMAP2 event, skipping it.\n");
928 return -1;
929 }
930
931 if (!evsel->attr.sample_id_all) {
932 sample->cpu = 0;
933 sample->time = 0;
934 sample->tid = event->mmap2.tid;
935 sample->pid = event->mmap2.pid;
936 }
937 print_sample_start(sample, thread, evsel);
938 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300939 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900940 return 0;
941}
942
Adrian Hunter7c148982015-07-21 12:44:06 +0300943static int process_switch_event(struct perf_tool *tool,
944 union perf_event *event,
945 struct perf_sample *sample,
946 struct machine *machine)
947{
948 struct thread *thread;
949 struct perf_script *script = container_of(tool, struct perf_script, tool);
950 struct perf_session *session = script->session;
951 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
952
953 if (perf_event__process_switch(tool, event, sample, machine) < 0)
954 return -1;
955
956 thread = machine__findnew_thread(machine, sample->pid,
957 sample->tid);
958 if (thread == NULL) {
959 pr_debug("problem processing SWITCH event, skipping it.\n");
960 return -1;
961 }
962
963 print_sample_start(sample, thread, evsel);
964 perf_event__fprintf(event, stdout);
965 thread__put(thread);
966 return 0;
967}
968
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300969static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500970{
971 session_done = 1;
972}
973
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300974static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200975{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200976 int ret;
977
Tom Zanussic239da32010-04-01 23:59:18 -0500978 signal(SIGINT, sig_handler);
979
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900980 /* override event processing functions */
981 if (script->show_task_events) {
982 script->tool.comm = process_comm_event;
983 script->tool.fork = process_fork_event;
984 script->tool.exit = process_exit_event;
985 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900986 if (script->show_mmap_events) {
987 script->tool.mmap = process_mmap_event;
988 script->tool.mmap2 = process_mmap2_event;
989 }
Adrian Hunter7c148982015-07-21 12:44:06 +0300990 if (script->show_switch_events)
991 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900992
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300993 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200994
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200995 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200996 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200997
998 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200999}
1000
Tom Zanussi956ffd02009-11-25 01:15:46 -06001001struct script_spec {
1002 struct list_head node;
1003 struct scripting_ops *ops;
1004 char spec[0];
1005};
1006
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001007static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001008
1009static struct script_spec *script_spec__new(const char *spec,
1010 struct scripting_ops *ops)
1011{
1012 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1013
1014 if (s != NULL) {
1015 strcpy(s->spec, spec);
1016 s->ops = ops;
1017 }
1018
1019 return s;
1020}
1021
Tom Zanussi956ffd02009-11-25 01:15:46 -06001022static void script_spec__add(struct script_spec *s)
1023{
1024 list_add_tail(&s->node, &script_specs);
1025}
1026
1027static struct script_spec *script_spec__find(const char *spec)
1028{
1029 struct script_spec *s;
1030
1031 list_for_each_entry(s, &script_specs, node)
1032 if (strcasecmp(s->spec, spec) == 0)
1033 return s;
1034 return NULL;
1035}
1036
1037static struct script_spec *script_spec__findnew(const char *spec,
1038 struct scripting_ops *ops)
1039{
1040 struct script_spec *s = script_spec__find(spec);
1041
1042 if (s)
1043 return s;
1044
1045 s = script_spec__new(spec, ops);
1046 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +09001047 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001048
1049 script_spec__add(s);
1050
1051 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001052}
1053
1054int script_spec_register(const char *spec, struct scripting_ops *ops)
1055{
1056 struct script_spec *s;
1057
1058 s = script_spec__find(spec);
1059 if (s)
1060 return -1;
1061
1062 s = script_spec__findnew(spec, ops);
1063 if (!s)
1064 return -1;
1065
1066 return 0;
1067}
1068
1069static struct scripting_ops *script_spec__lookup(const char *spec)
1070{
1071 struct script_spec *s = script_spec__find(spec);
1072 if (!s)
1073 return NULL;
1074
1075 return s->ops;
1076}
1077
1078static void list_available_languages(void)
1079{
1080 struct script_spec *s;
1081
1082 fprintf(stderr, "\n");
1083 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001084 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001085
1086 list_for_each_entry(s, &script_specs, node)
1087 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1088
1089 fprintf(stderr, "\n");
1090}
1091
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001092static int parse_scriptname(const struct option *opt __maybe_unused,
1093 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001094{
1095 char spec[PATH_MAX];
1096 const char *script, *ext;
1097 int len;
1098
Tom Zanussif526d682010-01-27 02:27:52 -06001099 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001100 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001101 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001102 }
1103
1104 script = strchr(str, ':');
1105 if (script) {
1106 len = script - str;
1107 if (len >= PATH_MAX) {
1108 fprintf(stderr, "invalid language specifier");
1109 return -1;
1110 }
1111 strncpy(spec, str, len);
1112 spec[len] = '\0';
1113 scripting_ops = script_spec__lookup(spec);
1114 if (!scripting_ops) {
1115 fprintf(stderr, "invalid language specifier");
1116 return -1;
1117 }
1118 script++;
1119 } else {
1120 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001121 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001122 if (!ext) {
1123 fprintf(stderr, "invalid script extension");
1124 return -1;
1125 }
1126 scripting_ops = script_spec__lookup(++ext);
1127 if (!scripting_ops) {
1128 fprintf(stderr, "invalid script extension");
1129 return -1;
1130 }
1131 }
1132
1133 script_name = strdup(script);
1134
1135 return 0;
1136}
1137
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001138static int parse_output_fields(const struct option *opt __maybe_unused,
1139 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001140{
1141 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001142 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001143 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001144 int rc = 0;
1145 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001146 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001147
1148 if (!str)
1149 return -ENOMEM;
1150
David Ahern2c9e45f72011-03-17 10:03:21 -06001151 /* first word can state for which event type the user is specifying
1152 * the fields. If no type exists, the specified fields apply to all
1153 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001154 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001155 tok = strchr(str, ':');
1156 if (tok) {
1157 *tok = '\0';
1158 tok++;
1159 if (!strcmp(str, "hw"))
1160 type = PERF_TYPE_HARDWARE;
1161 else if (!strcmp(str, "sw"))
1162 type = PERF_TYPE_SOFTWARE;
1163 else if (!strcmp(str, "trace"))
1164 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001165 else if (!strcmp(str, "raw"))
1166 type = PERF_TYPE_RAW;
Wang Nan27cfef02015-12-08 02:25:43 +00001167 else if (!strcmp(str, "break"))
1168 type = PERF_TYPE_BREAKPOINT;
David Ahern2c9e45f72011-03-17 10:03:21 -06001169 else {
1170 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001171 rc = -EINVAL;
1172 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001173 }
1174
1175 if (output[type].user_set)
1176 pr_warning("Overriding previous field request for %s events.\n",
1177 event_type(type));
1178
1179 output[type].fields = 0;
1180 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001181 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001182
1183 } else {
1184 tok = str;
1185 if (strlen(str) == 0) {
1186 fprintf(stderr,
1187 "Cannot set fields to 'none' for all event types.\n");
1188 rc = -EINVAL;
1189 goto out;
1190 }
1191
1192 if (output_set_by_user())
1193 pr_warning("Overriding previous field request for all events.\n");
1194
1195 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1196 output[j].fields = 0;
1197 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001198 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001199 }
David Ahern1424dc92011-03-09 22:23:28 -07001200 }
1201
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001202 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001203 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001204 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001205 break;
David Ahern745f43e2011-03-09 22:23:26 -07001206 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001207 if (i == imax && strcmp(tok, "flags") == 0) {
1208 print_flags = true;
1209 continue;
1210 }
David Ahern745f43e2011-03-09 22:23:26 -07001211 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001212 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001213 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001214 goto out;
1215 }
1216
1217 if (type == -1) {
1218 /* add user option to all events types for
1219 * which it is valid
1220 */
1221 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1222 if (output[j].invalid_fields & all_output_options[i].field) {
1223 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1224 all_output_options[i].str, event_type(j));
1225 } else
1226 output[j].fields |= all_output_options[i].field;
1227 }
1228 } else {
1229 if (output[type].invalid_fields & all_output_options[i].field) {
1230 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1231 all_output_options[i].str, event_type(type));
1232
1233 rc = -EINVAL;
1234 goto out;
1235 }
1236 output[type].fields |= all_output_options[i].field;
1237 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001238 }
1239
1240 if (type >= 0) {
1241 if (output[type].fields == 0) {
1242 pr_debug("No fields requested for %s type. "
1243 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001244 }
David Ahern1424dc92011-03-09 22:23:28 -07001245 }
David Ahern745f43e2011-03-09 22:23:26 -07001246
David Ahern2c9e45f72011-03-17 10:03:21 -06001247out:
David Ahern745f43e2011-03-09 22:23:26 -07001248 free(str);
1249 return rc;
1250}
1251
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001252/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1253static int is_directory(const char *base_path, const struct dirent *dent)
1254{
1255 char path[PATH_MAX];
1256 struct stat st;
1257
1258 sprintf(path, "%s/%s", base_path, dent->d_name);
1259 if (stat(path, &st))
1260 return 0;
1261
1262 return S_ISDIR(st.st_mode);
1263}
1264
1265#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001266 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1267 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001268 if ((lang_dirent.d_type == DT_DIR || \
1269 (lang_dirent.d_type == DT_UNKNOWN && \
1270 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001271 (strcmp(lang_dirent.d_name, ".")) && \
1272 (strcmp(lang_dirent.d_name, "..")))
1273
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001274#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001275 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1276 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001277 if (script_dirent.d_type != DT_DIR && \
1278 (script_dirent.d_type != DT_UNKNOWN || \
1279 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001280
1281
1282#define RECORD_SUFFIX "-record"
1283#define REPORT_SUFFIX "-report"
1284
1285struct script_desc {
1286 struct list_head node;
1287 char *name;
1288 char *half_liner;
1289 char *args;
1290};
1291
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001292static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001293
1294static struct script_desc *script_desc__new(const char *name)
1295{
1296 struct script_desc *s = zalloc(sizeof(*s));
1297
Tom Zanussib5b87312010-11-10 08:16:51 -06001298 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001299 s->name = strdup(name);
1300
1301 return s;
1302}
1303
1304static void script_desc__delete(struct script_desc *s)
1305{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001306 zfree(&s->name);
1307 zfree(&s->half_liner);
1308 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001309 free(s);
1310}
1311
1312static void script_desc__add(struct script_desc *s)
1313{
1314 list_add_tail(&s->node, &script_descs);
1315}
1316
1317static struct script_desc *script_desc__find(const char *name)
1318{
1319 struct script_desc *s;
1320
1321 list_for_each_entry(s, &script_descs, node)
1322 if (strcasecmp(s->name, name) == 0)
1323 return s;
1324 return NULL;
1325}
1326
1327static struct script_desc *script_desc__findnew(const char *name)
1328{
1329 struct script_desc *s = script_desc__find(name);
1330
1331 if (s)
1332 return s;
1333
1334 s = script_desc__new(name);
1335 if (!s)
1336 goto out_delete_desc;
1337
1338 script_desc__add(s);
1339
1340 return s;
1341
1342out_delete_desc:
1343 script_desc__delete(s);
1344
1345 return NULL;
1346}
1347
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001348static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001349{
1350 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001351 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001352
1353 if (strlen(str) > suffix_len) {
1354 p = str + strlen(str) - suffix_len;
1355 if (!strncmp(p, suffix, suffix_len))
1356 return p;
1357 }
1358
1359 return NULL;
1360}
1361
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001362static int read_script_info(struct script_desc *desc, const char *filename)
1363{
1364 char line[BUFSIZ], *p;
1365 FILE *fp;
1366
1367 fp = fopen(filename, "r");
1368 if (!fp)
1369 return -1;
1370
1371 while (fgets(line, sizeof(line), fp)) {
1372 p = ltrim(line);
1373 if (strlen(p) == 0)
1374 continue;
1375 if (*p != '#')
1376 continue;
1377 p++;
1378 if (strlen(p) && *p == '!')
1379 continue;
1380
1381 p = ltrim(p);
1382 if (strlen(p) && p[strlen(p) - 1] == '\n')
1383 p[strlen(p) - 1] = '\0';
1384
1385 if (!strncmp(p, "description:", strlen("description:"))) {
1386 p += strlen("description:");
1387 desc->half_liner = strdup(ltrim(p));
1388 continue;
1389 }
1390
1391 if (!strncmp(p, "args:", strlen("args:"))) {
1392 p += strlen("args:");
1393 desc->args = strdup(ltrim(p));
1394 continue;
1395 }
1396 }
1397
1398 fclose(fp);
1399
1400 return 0;
1401}
1402
Robert Richter38efb532011-11-25 11:38:40 +01001403static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1404{
1405 char *script_root, *str;
1406
1407 script_root = strdup(script_dirent->d_name);
1408 if (!script_root)
1409 return NULL;
1410
1411 str = (char *)ends_with(script_root, suffix);
1412 if (!str) {
1413 free(script_root);
1414 return NULL;
1415 }
1416
1417 *str = '\0';
1418 return script_root;
1419}
1420
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001421static int list_available_scripts(const struct option *opt __maybe_unused,
1422 const char *s __maybe_unused,
1423 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001424{
1425 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1426 char scripts_path[MAXPATHLEN];
1427 DIR *scripts_dir, *lang_dir;
1428 char script_path[MAXPATHLEN];
1429 char lang_path[MAXPATHLEN];
1430 struct script_desc *desc;
1431 char first_half[BUFSIZ];
1432 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001433
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001434 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001435
1436 scripts_dir = opendir(scripts_path);
1437 if (!scripts_dir)
1438 return -1;
1439
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001440 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001441 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1442 lang_dirent.d_name);
1443 lang_dir = opendir(lang_path);
1444 if (!lang_dir)
1445 continue;
1446
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001447 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001448 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1449 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001450 desc = script_desc__findnew(script_root);
1451 snprintf(script_path, MAXPATHLEN, "%s/%s",
1452 lang_path, script_dirent.d_name);
1453 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001454 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001455 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001456 }
1457 }
1458
1459 fprintf(stdout, "List of available trace scripts:\n");
1460 list_for_each_entry(desc, &script_descs, node) {
1461 sprintf(first_half, "%s %s", desc->name,
1462 desc->args ? desc->args : "");
1463 fprintf(stdout, " %-36s %s\n", first_half,
1464 desc->half_liner ? desc->half_liner : "");
1465 }
1466
1467 exit(0);
1468}
1469
Feng Tange5f37052012-09-07 16:42:26 +08001470/*
Feng Tang49e639e2012-10-30 11:56:03 +08001471 * Some scripts specify the required events in their "xxx-record" file,
1472 * this function will check if the events in perf.data match those
1473 * mentioned in the "xxx-record".
1474 *
1475 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1476 * which is covered well now. And new parsing code should be added to
1477 * cover the future complexing formats like event groups etc.
1478 */
1479static int check_ev_match(char *dir_name, char *scriptname,
1480 struct perf_session *session)
1481{
1482 char filename[MAXPATHLEN], evname[128];
1483 char line[BUFSIZ], *p;
1484 struct perf_evsel *pos;
1485 int match, len;
1486 FILE *fp;
1487
1488 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1489
1490 fp = fopen(filename, "r");
1491 if (!fp)
1492 return -1;
1493
1494 while (fgets(line, sizeof(line), fp)) {
1495 p = ltrim(line);
1496 if (*p == '#')
1497 continue;
1498
1499 while (strlen(p)) {
1500 p = strstr(p, "-e");
1501 if (!p)
1502 break;
1503
1504 p += 2;
1505 p = ltrim(p);
1506 len = strcspn(p, " \t");
1507 if (!len)
1508 break;
1509
1510 snprintf(evname, len + 1, "%s", p);
1511
1512 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001513 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001514 if (!strcmp(perf_evsel__name(pos), evname)) {
1515 match = 1;
1516 break;
1517 }
1518 }
1519
1520 if (!match) {
1521 fclose(fp);
1522 return -1;
1523 }
1524 }
1525 }
1526
1527 fclose(fp);
1528 return 0;
1529}
1530
1531/*
Feng Tange5f37052012-09-07 16:42:26 +08001532 * Return -1 if none is found, otherwise the actual scripts number.
1533 *
1534 * Currently the only user of this function is the script browser, which
1535 * will list all statically runnable scripts, select one, execute it and
1536 * show the output in a perf browser.
1537 */
1538int find_scripts(char **scripts_array, char **scripts_path_array)
1539{
1540 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001541 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001542 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001543 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001544 struct perf_data_file file = {
1545 .path = input_name,
1546 .mode = PERF_DATA_MODE_READ,
1547 };
Feng Tange5f37052012-09-07 16:42:26 +08001548 char *temp;
1549 int i = 0;
1550
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001551 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001552 if (!session)
1553 return -1;
1554
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001555 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Feng Tange5f37052012-09-07 16:42:26 +08001556
1557 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001558 if (!scripts_dir) {
1559 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001560 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001561 }
Feng Tange5f37052012-09-07 16:42:26 +08001562
1563 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1564 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1565 lang_dirent.d_name);
1566#ifdef NO_LIBPERL
1567 if (strstr(lang_path, "perl"))
1568 continue;
1569#endif
1570#ifdef NO_LIBPYTHON
1571 if (strstr(lang_path, "python"))
1572 continue;
1573#endif
1574
1575 lang_dir = opendir(lang_path);
1576 if (!lang_dir)
1577 continue;
1578
1579 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1580 /* Skip those real time scripts: xxxtop.p[yl] */
1581 if (strstr(script_dirent.d_name, "top."))
1582 continue;
1583 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1584 script_dirent.d_name);
1585 temp = strchr(script_dirent.d_name, '.');
1586 snprintf(scripts_array[i],
1587 (temp - script_dirent.d_name) + 1,
1588 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001589
1590 if (check_ev_match(lang_path,
1591 scripts_array[i], session))
1592 continue;
1593
Feng Tange5f37052012-09-07 16:42:26 +08001594 i++;
1595 }
Feng Tang49e639e2012-10-30 11:56:03 +08001596 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001597 }
1598
Feng Tang49e639e2012-10-30 11:56:03 +08001599 closedir(scripts_dir);
1600 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001601 return i;
1602}
1603
Tom Zanussi38752942009-12-15 02:53:39 -06001604static char *get_script_path(const char *script_root, const char *suffix)
1605{
1606 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1607 char scripts_path[MAXPATHLEN];
1608 char script_path[MAXPATHLEN];
1609 DIR *scripts_dir, *lang_dir;
1610 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001611 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001612
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001613 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
Tom Zanussi38752942009-12-15 02:53:39 -06001614
1615 scripts_dir = opendir(scripts_path);
1616 if (!scripts_dir)
1617 return NULL;
1618
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001619 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001620 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1621 lang_dirent.d_name);
1622 lang_dir = opendir(lang_path);
1623 if (!lang_dir)
1624 continue;
1625
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001626 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001627 __script_root = get_script_root(&script_dirent, suffix);
1628 if (__script_root && !strcmp(script_root, __script_root)) {
1629 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001630 closedir(lang_dir);
1631 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001632 snprintf(script_path, MAXPATHLEN, "%s/%s",
1633 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001634 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001635 }
1636 free(__script_root);
1637 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001638 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001639 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001640 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001641
Robert Richter38efb532011-11-25 11:38:40 +01001642 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001643}
1644
Tom Zanussib5b87312010-11-10 08:16:51 -06001645static bool is_top_script(const char *script_path)
1646{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001647 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001648}
1649
1650static int has_required_arg(char *script_path)
1651{
1652 struct script_desc *desc;
1653 int n_args = 0;
1654 char *p;
1655
1656 desc = script_desc__new(NULL);
1657
1658 if (read_script_info(desc, script_path))
1659 goto out;
1660
1661 if (!desc->args)
1662 goto out;
1663
1664 for (p = desc->args; *p; p++)
1665 if (*p == '<')
1666 n_args++;
1667out:
1668 script_desc__delete(desc);
1669
1670 return n_args;
1671}
1672
David Ahernd54b1a92012-08-26 12:24:46 -06001673static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001674{
1675 char **__argv = malloc(sizeof(const char *) * argc);
1676
David Ahernd54b1a92012-08-26 12:24:46 -06001677 if (!__argv) {
1678 pr_err("malloc failed\n");
1679 return -1;
1680 }
1681
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001682 memcpy(__argv, argv, sizeof(const char *) * argc);
1683 argc = parse_options(argc, (const char **)__argv, record_options,
1684 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1685 free(__argv);
1686
David Ahernd54b1a92012-08-26 12:24:46 -06001687 system_wide = (argc == 0);
1688
1689 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001690}
1691
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001692static void script__setup_sample_type(struct perf_script *script)
1693{
1694 struct perf_session *session = script->session;
1695 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1696
1697 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1698 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1699 (sample_type & PERF_SAMPLE_STACK_USER))
1700 callchain_param.record_mode = CALLCHAIN_DWARF;
1701 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1702 callchain_param.record_mode = CALLCHAIN_LBR;
1703 else
1704 callchain_param.record_mode = CALLCHAIN_FP;
1705 }
1706}
1707
Jiri Olsae099eba2016-01-05 22:09:09 +01001708static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1709 union perf_event *event,
1710 struct perf_session *session)
1711{
1712 struct stat_round_event *round = &event->stat_round;
1713 struct perf_evsel *counter;
1714
1715 evlist__for_each(session->evlist, counter) {
1716 perf_stat_process_counter(&stat_config, counter);
1717 process_stat(counter, round->time);
1718 }
1719
1720 process_stat_interval(round->time);
1721 return 0;
1722}
1723
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001724static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1725 union perf_event *event,
1726 struct perf_session *session __maybe_unused)
1727{
1728 perf_event__read_stat_config(&stat_config, &event->stat_config);
1729 return 0;
1730}
1731
Jiri Olsacfc88742016-01-05 22:09:06 +01001732static int set_maps(struct perf_script *script)
1733{
1734 struct perf_evlist *evlist = script->session->evlist;
1735
1736 if (!script->cpus || !script->threads)
1737 return 0;
1738
1739 if (WARN_ONCE(script->allocated, "stats double allocation\n"))
1740 return -EINVAL;
1741
1742 perf_evlist__set_maps(evlist, script->cpus, script->threads);
1743
1744 if (perf_evlist__alloc_stats(evlist, true))
1745 return -ENOMEM;
1746
1747 script->allocated = true;
1748 return 0;
1749}
1750
1751static
1752int process_thread_map_event(struct perf_tool *tool,
1753 union perf_event *event,
1754 struct perf_session *session __maybe_unused)
1755{
1756 struct perf_script *script = container_of(tool, struct perf_script, tool);
1757
1758 if (script->threads) {
1759 pr_warning("Extra thread map event, ignoring.\n");
1760 return 0;
1761 }
1762
1763 script->threads = thread_map__new_event(&event->thread_map);
1764 if (!script->threads)
1765 return -ENOMEM;
1766
1767 return set_maps(script);
1768}
1769
1770static
1771int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1772 union perf_event *event,
1773 struct perf_session *session __maybe_unused)
1774{
1775 struct perf_script *script = container_of(tool, struct perf_script, tool);
1776
1777 if (script->cpus) {
1778 pr_warning("Extra cpu map event, ignoring.\n");
1779 return 0;
1780 }
1781
1782 script->cpus = cpu_map__new_data(&event->cpu_map.data);
1783 if (!script->cpus)
1784 return -ENOMEM;
1785
1786 return set_maps(script);
1787}
1788
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001789int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001790{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001791 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001792 bool header = false;
1793 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001794 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001795 char *rec_script_path = NULL;
1796 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001797 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001798 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001799 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001800 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001801 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001802 struct perf_script script = {
1803 .tool = {
1804 .sample = process_sample_event,
1805 .mmap = perf_event__process_mmap,
1806 .mmap2 = perf_event__process_mmap2,
1807 .comm = perf_event__process_comm,
1808 .exit = perf_event__process_exit,
1809 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001810 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001811 .tracing_data = perf_event__process_tracing_data,
1812 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001813 .id_index = perf_event__process_id_index,
1814 .auxtrace_info = perf_event__process_auxtrace_info,
1815 .auxtrace = perf_event__process_auxtrace,
1816 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsae099eba2016-01-05 22:09:09 +01001817 .stat = perf_event__process_stat_event,
1818 .stat_round = process_stat_round_event,
Jiri Olsa91a2c3d2016-01-05 22:09:07 +01001819 .stat_config = process_stat_config_event,
Jiri Olsacfc88742016-01-05 22:09:06 +01001820 .thread_map = process_thread_map_event,
1821 .cpu_map = process_cpu_map_event,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001822 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001823 .ordering_requires_timestamps = true,
1824 },
1825 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001826 struct perf_data_file file = {
1827 .mode = PERF_DATA_MODE_READ,
1828 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001829 const struct option options[] = {
1830 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1831 "dump raw trace in ASCII"),
1832 OPT_INCR('v', "verbose", &verbose,
1833 "be more verbose (show symbol address, etc)"),
1834 OPT_BOOLEAN('L', "Latency", &latency_format,
1835 "show latency attributes (irqs/preemption disabled, etc)"),
1836 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1837 list_available_scripts),
1838 OPT_CALLBACK('s', "script", NULL, "name",
1839 "script file name (lang:script name, script name, or *)",
1840 parse_scriptname),
1841 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1842 "generate perf-script.xx script in specified language"),
1843 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1844 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1845 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001846 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1847 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001848 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1849 "file", "vmlinux pathname"),
1850 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1851 "file", "kallsyms pathname"),
1852 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1853 "When printing symbols do not display call chain"),
1854 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1855 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001856 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001857 "comma separated output fields prepend with 'type:'. "
1858 "Valid types: hw,sw,trace,raw. "
1859 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001860 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001861 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1862 "system-wide collection from all CPUs"),
1863 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1864 "only consider these symbols"),
1865 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1866 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1867 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06001868 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1869 "only consider symbols in these pids"),
1870 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1871 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001872 OPT_BOOLEAN('I', "show-info", &show_full_info,
1873 "display extended information from perf.data file"),
1874 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1875 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001876 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1877 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001878 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1879 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03001880 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1881 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001882 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03001883 OPT_BOOLEAN(0, "ns", &nanosecs,
1884 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001885 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1886 "Instruction Tracing options",
1887 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07001888 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1889 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07001890 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1891 "Enable symbol demangling"),
1892 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1893 "Enable kernel symbol demangling"),
1894
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001895 OPT_END()
1896 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08001897 const char * const script_subcommands[] = { "record", "report", NULL };
1898 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001899 "perf script [<options>]",
1900 "perf script [<options>] record <script> [<record-options>] <command>",
1901 "perf script [<options>] report <script> [script-args]",
1902 "perf script [<options>] <script> [<record-options>] <command>",
1903 "perf script [<options>] <top-script> [script-args]",
1904 NULL
1905 };
Tom Zanussi38752942009-12-15 02:53:39 -06001906
Tom Zanussib5b87312010-11-10 08:16:51 -06001907 setup_scripting();
1908
Yunlong Song40cae2b2015-03-18 21:35:54 +08001909 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001910 PARSE_OPT_STOP_AT_NON_OPTION);
1911
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001912 file.path = input_name;
1913
Tom Zanussib5b87312010-11-10 08:16:51 -06001914 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1915 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1916 if (!rec_script_path)
1917 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001918 }
1919
Tom Zanussib5b87312010-11-10 08:16:51 -06001920 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1921 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1922 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001923 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001924 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001925 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001926 return -1;
1927 }
Tom Zanussi38752942009-12-15 02:53:39 -06001928 }
1929
Adrian Hunter3c5b6452015-09-25 16:15:51 +03001930 if (itrace_synth_opts.callchain &&
1931 itrace_synth_opts.callchain_sz > scripting_max_stack)
1932 scripting_max_stack = itrace_synth_opts.callchain_sz;
1933
Ben Hutchings44e668c2010-10-10 16:10:03 +01001934 /* make sure PERF_EXEC_PATH is set for scripts */
Josh Poimboeuf46113a52015-12-15 09:39:37 -06001935 set_argv_exec_path(get_argv_exec_path());
Ben Hutchings44e668c2010-10-10 16:10:03 +01001936
Tom Zanussib5b87312010-11-10 08:16:51 -06001937 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001938 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001939 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001940 pid_t pid;
1941
Tom Zanussib5b87312010-11-10 08:16:51 -06001942 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1943 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1944
1945 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001946 usage_with_options_msg(script_usage, options,
1947 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001948 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001949 }
1950
Tom Zanussib5b87312010-11-10 08:16:51 -06001951 if (is_top_script(argv[0])) {
1952 rep_args = argc - 1;
1953 } else {
1954 int rec_args;
1955
1956 rep_args = has_required_arg(rep_script_path);
1957 rec_args = (argc - 1) - rep_args;
1958 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001959 usage_with_options_msg(script_usage, options,
1960 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001961 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001962 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06001963 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001964 }
1965
1966 if (pipe(live_pipe) < 0) {
1967 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001968 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001969 }
1970
1971 pid = fork();
1972 if (pid < 0) {
1973 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001974 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001975 }
1976
1977 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001978 j = 0;
1979
Tom Zanussia0cccc22010-04-01 23:59:25 -05001980 dup2(live_pipe[1], 1);
1981 close(live_pipe[0]);
1982
Robert Richter317df652011-11-25 15:05:25 +01001983 if (is_top_script(argv[0])) {
1984 system_wide = true;
1985 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001986 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1987 err = -1;
1988 goto out;
1989 }
Robert Richter317df652011-11-25 15:05:25 +01001990 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001991
1992 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001993 if (!__argv) {
1994 pr_err("malloc failed\n");
1995 err = -ENOMEM;
1996 goto out;
1997 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001998
Tom Zanussib5b87312010-11-10 08:16:51 -06001999 __argv[j++] = "/bin/sh";
2000 __argv[j++] = rec_script_path;
2001 if (system_wide)
2002 __argv[j++] = "-a";
2003 __argv[j++] = "-q";
2004 __argv[j++] = "-o";
2005 __argv[j++] = "-";
2006 for (i = rep_args + 1; i < argc; i++)
2007 __argv[j++] = argv[i];
2008 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05002009
2010 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002011 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05002012 exit(-1);
2013 }
2014
2015 dup2(live_pipe[0], 0);
2016 close(live_pipe[1]);
2017
Tom Zanussib5b87312010-11-10 08:16:51 -06002018 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002019 if (!__argv) {
2020 pr_err("malloc failed\n");
2021 err = -ENOMEM;
2022 goto out;
2023 }
2024
Tom Zanussib5b87312010-11-10 08:16:51 -06002025 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002026 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06002027 __argv[j++] = rep_script_path;
2028 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002029 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06002030 __argv[j++] = "-i";
2031 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06002032 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06002033
2034 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06002035 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06002036 exit(-1);
2037 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06002038
Tom Zanussib5b87312010-11-10 08:16:51 -06002039 if (rec_script_path)
2040 script_path = rec_script_path;
2041 if (rep_script_path)
2042 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002043
Tom Zanussib5b87312010-11-10 08:16:51 -06002044 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06002045 j = 0;
2046
Robert Richter317df652011-11-25 15:05:25 +01002047 if (!rec_script_path)
2048 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06002049 else if (!system_wide) {
2050 if (have_cmd(argc - 1, &argv[1]) != 0) {
2051 err = -1;
2052 goto out;
2053 }
2054 }
Tom Zanussib5b87312010-11-10 08:16:51 -06002055
2056 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06002057 if (!__argv) {
2058 pr_err("malloc failed\n");
2059 err = -ENOMEM;
2060 goto out;
2061 }
2062
Tom Zanussib5b87312010-11-10 08:16:51 -06002063 __argv[j++] = "/bin/sh";
2064 __argv[j++] = script_path;
2065 if (system_wide)
2066 __argv[j++] = "-a";
2067 for (i = 2; i < argc; i++)
2068 __argv[j++] = argv[i];
2069 __argv[j++] = NULL;
2070
2071 execvp("/bin/sh", (char **)__argv);
2072 free(__argv);
2073 exit(-1);
2074 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002075
Tom Zanussicf4fee52010-03-03 01:04:33 -06002076 if (!script_name)
2077 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002078
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002079 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002080 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002081 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002082
Jiri Olsae90debd2013-12-09 11:02:50 +01002083 if (header || header_only) {
2084 perf_session__fprintf_info(session, stdout, show_full_info);
2085 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002086 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01002087 }
2088
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002089 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09002090 goto out_delete;
2091
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002092 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02002093 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002094
Adrian Hunter7a680eb2015-04-09 18:53:56 +03002095 session->itrace_synth_opts = &itrace_synth_opts;
2096
Anton Blanchard5d67be92011-07-04 21:57:50 +10002097 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002098 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2099 if (err < 0)
2100 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002101 }
2102
David Ahern1424dc92011-03-09 22:23:28 -07002103 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002104 symbol_conf.use_callchain = true;
2105 else
2106 symbol_conf.use_callchain = false;
2107
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002108 if (session->tevent.pevent &&
2109 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002110 machine__resolve_kernel_addr,
2111 &session->machines.host) < 0) {
2112 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2113 return -1;
2114 }
2115
Tom Zanussi956ffd02009-11-25 01:15:46 -06002116 if (generate_script_lang) {
2117 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002118 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002119
David Ahern2c9e45f72011-03-17 10:03:21 -06002120 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002121 fprintf(stderr,
2122 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002123 err = -EINVAL;
2124 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002125 }
2126
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002127 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002128 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002129 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002130 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002131 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002132 }
2133
2134 err = fstat(input, &perf_stat);
2135 if (err < 0) {
2136 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002137 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002138 }
2139
2140 if (!perf_stat.st_size) {
2141 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002142 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002143 }
2144
2145 scripting_ops = script_spec__lookup(generate_script_lang);
2146 if (!scripting_ops) {
2147 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002148 err = -ENOENT;
2149 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002150 }
2151
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002152 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002153 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002154 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002155 }
2156
2157 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002158 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002159 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002160 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002161 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002162 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002163 }
2164
David Ahern9cbdb702011-04-06 21:54:20 -06002165
2166 err = perf_session__check_output_opt(session);
2167 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002168 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002169
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002170 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002171
Adrian Hunterd445dd22014-08-15 22:08:37 +03002172 flush_scripting();
2173
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002174out_delete:
Jiri Olsacfc88742016-01-05 22:09:06 +01002175 perf_evlist__free_stats(session->evlist);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002176 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002177
2178 if (script_started)
2179 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002180out:
2181 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002182}