blob: 8e3f8048d2d08a55315e243cd5ceeaa18d11a6ee [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"
6#include "util/exec_cmd.h"
7#include "util/header.h"
8#include "util/parse-options.h"
Stephane Eranianfc36f942015-08-31 18:41:10 +02009#include "util/perf_regs.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020010#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020011#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020012#include "util/symbol.h"
13#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010014#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020015#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070016#include "util/evlist.h"
17#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080018#include "util/sort.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020019#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030020#include "util/auxtrace.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100021#include <linux/bitmap.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020022
Tom Zanussi956ffd02009-11-25 01:15:46 -060023static char const *script_name;
24static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020025static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020026static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020027static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070028static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090029static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010030static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030031static bool print_flags;
Adrian Hunter83e19862015-09-25 16:15:36 +030032static bool nanosecs;
Anton Blanchard5d67be92011-07-04 21:57:50 +100033static const char *cpu_list;
34static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Tom Zanussi956ffd02009-11-25 01:15:46 -060035
Adrian Hunter44cbe722015-09-25 16:15:50 +030036unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
Adrian Hunter03cd1fe2015-09-25 16:15:49 +030037
David Ahern745f43e2011-03-09 22:23:26 -070038enum perf_output_field {
39 PERF_OUTPUT_COMM = 1U << 0,
40 PERF_OUTPUT_TID = 1U << 1,
41 PERF_OUTPUT_PID = 1U << 2,
42 PERF_OUTPUT_TIME = 1U << 3,
43 PERF_OUTPUT_CPU = 1U << 4,
44 PERF_OUTPUT_EVNAME = 1U << 5,
45 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060046 PERF_OUTPUT_IP = 1U << 7,
47 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060048 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060049 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090050 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020051 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaa2014-08-25 16:45:42 +020052 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020053 PERF_OUTPUT_IREGS = 1U << 14,
Stephane Eraniandc323ce2015-08-31 18:41:13 +020054 PERF_OUTPUT_BRSTACK = 1U << 15,
55 PERF_OUTPUT_BRSTACKSYM = 1U << 16,
David Ahern745f43e2011-03-09 22:23:26 -070056};
57
58struct output_option {
59 const char *str;
60 enum perf_output_field field;
61} all_output_options[] = {
62 {.str = "comm", .field = PERF_OUTPUT_COMM},
63 {.str = "tid", .field = PERF_OUTPUT_TID},
64 {.str = "pid", .field = PERF_OUTPUT_PID},
65 {.str = "time", .field = PERF_OUTPUT_TIME},
66 {.str = "cpu", .field = PERF_OUTPUT_CPU},
67 {.str = "event", .field = PERF_OUTPUT_EVNAME},
68 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060069 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070070 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060071 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060072 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090073 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020074 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaa2014-08-25 16:45:42 +020075 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020076 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
Stephane Eraniandc323ce2015-08-31 18:41:13 +020077 {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
78 {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
David Ahern745f43e2011-03-09 22:23:26 -070079};
80
81/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060082static struct {
83 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060084 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040085 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060086 u64 fields;
87 u64 invalid_fields;
88} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070089
David Ahern2c9e45f72011-03-17 10:03:21 -060090 [PERF_TYPE_HARDWARE] = {
91 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070092
David Ahern2c9e45f72011-03-17 10:03:21 -060093 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
94 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060095 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +020096 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
97 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -060098
99 .invalid_fields = PERF_OUTPUT_TRACE,
100 },
101
102 [PERF_TYPE_SOFTWARE] = {
103 .user_set = false,
104
105 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
106 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600107 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200108 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
109 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600110
111 .invalid_fields = PERF_OUTPUT_TRACE,
112 },
113
114 [PERF_TYPE_TRACEPOINT] = {
115 .user_set = false,
116
117 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
118 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
119 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
120 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700121
122 [PERF_TYPE_RAW] = {
123 .user_set = false,
124
125 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
126 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600127 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200128 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
129 PERF_OUTPUT_PERIOD,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700130
131 .invalid_fields = PERF_OUTPUT_TRACE,
132 },
David Ahern1424dc92011-03-09 22:23:28 -0700133};
David Ahern745f43e2011-03-09 22:23:26 -0700134
David Ahern2c9e45f72011-03-17 10:03:21 -0600135static bool output_set_by_user(void)
136{
137 int j;
138 for (j = 0; j < PERF_TYPE_MAX; ++j) {
139 if (output[j].user_set)
140 return true;
141 }
142 return false;
143}
David Ahern745f43e2011-03-09 22:23:26 -0700144
David Ahern9cbdb702011-04-06 21:54:20 -0600145static const char *output_field2str(enum perf_output_field field)
146{
147 int i, imax = ARRAY_SIZE(all_output_options);
148 const char *str = "";
149
150 for (i = 0; i < imax; ++i) {
151 if (all_output_options[i].field == field) {
152 str = all_output_options[i].str;
153 break;
154 }
155 }
156 return str;
157}
158
David Ahern2c9e45f72011-03-17 10:03:21 -0600159#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700160
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300161static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
162 u64 sample_type, const char *sample_msg,
163 enum perf_output_field field,
164 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700165{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300166 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600167 int type = attr->type;
168 const char *evname;
169
170 if (attr->sample_type & sample_type)
171 return 0;
172
173 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300174 if (allow_user_set)
175 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300176 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600177 pr_err("Samples for '%s' event do not have %s attribute set. "
178 "Cannot print '%s' field.\n",
179 evname, sample_msg, output_field2str(field));
180 return -1;
181 }
182
183 /* user did not ask for it explicitly so remove from the default list */
184 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300185 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600186 pr_debug("Samples for '%s' event do not have %s attribute set. "
187 "Skipping '%s' field.\n",
188 evname, sample_msg, output_field2str(field));
189
190 return 0;
191}
192
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300193static int perf_evsel__check_stype(struct perf_evsel *evsel,
194 u64 sample_type, const char *sample_msg,
195 enum perf_output_field field)
196{
197 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
198 false);
199}
200
David Ahern9cbdb702011-04-06 21:54:20 -0600201static int perf_evsel__check_attr(struct perf_evsel *evsel,
202 struct perf_session *session)
203{
204 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300205 bool allow_user_set;
206
207 allow_user_set = perf_header__has_feat(&session->header,
208 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600209
David Ahern1424dc92011-03-09 22:23:28 -0700210 if (PRINT_FIELD(TRACE) &&
211 !perf_session__has_traces(session, "record -R"))
212 return -EINVAL;
213
David Ahern787bef12011-05-27 14:28:43 -0600214 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300215 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
216 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700217 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700218 }
David Ahern7cec0922011-05-30 13:08:23 -0600219
220 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300221 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
222 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600223 return -EINVAL;
224
225 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
226 pr_err("Display of symbols requested but neither sample IP nor "
227 "sample address\nis selected. Hence, no addresses to convert "
228 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600229 return -EINVAL;
230 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900231 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
232 pr_err("Display of offsets requested but symbol is not"
233 "selected.\n");
234 return -EINVAL;
235 }
David Ahern7cec0922011-05-30 13:08:23 -0600236 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
237 pr_err("Display of DSO requested but neither sample IP nor "
238 "sample address\nis selected. Hence, no addresses to convert "
239 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600240 return -EINVAL;
241 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200242 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
243 pr_err("Display of source line number requested but sample IP is not\n"
244 "selected. Hence, no address to lookup the source line number.\n");
245 return -EINVAL;
246 }
David Ahern1424dc92011-03-09 22:23:28 -0700247
248 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300249 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
250 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700251 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700252
253 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300254 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
255 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700256 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700257
258 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300259 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
260 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700261 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600262
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200263 if (PRINT_FIELD(PERIOD) &&
264 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
265 PERF_OUTPUT_PERIOD))
266 return -EINVAL;
267
Stephane Eranianfc36f942015-08-31 18:41:10 +0200268 if (PRINT_FIELD(IREGS) &&
269 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
270 PERF_OUTPUT_IREGS))
271 return -EINVAL;
272
David Ahern9cbdb702011-04-06 21:54:20 -0600273 return 0;
274}
275
Adrian Hunter7ea95722013-11-01 15:51:30 +0200276static void set_print_ip_opts(struct perf_event_attr *attr)
277{
278 unsigned int type = attr->type;
279
280 output[type].print_ip_opts = 0;
281 if (PRINT_FIELD(IP))
282 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
283
284 if (PRINT_FIELD(SYM))
285 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
286
287 if (PRINT_FIELD(DSO))
288 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
289
290 if (PRINT_FIELD(SYMOFFSET))
291 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200292
293 if (PRINT_FIELD(SRCLINE))
294 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200295}
296
David Ahern9cbdb702011-04-06 21:54:20 -0600297/*
298 * verify all user requested events exist and the samples
299 * have the expected data
300 */
301static int perf_session__check_output_opt(struct perf_session *session)
302{
303 int j;
304 struct perf_evsel *evsel;
305
306 for (j = 0; j < PERF_TYPE_MAX; ++j) {
307 evsel = perf_session__find_first_evtype(session, j);
308
309 /*
310 * even if fields is set to 0 (ie., show nothing) event must
311 * exist if user explicitly includes it on the command line
312 */
313 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
314 pr_err("%s events do not exist. "
315 "Remove corresponding -f option to proceed.\n",
316 event_type(j));
317 return -1;
318 }
319
320 if (evsel && output[j].fields &&
321 perf_evsel__check_attr(evsel, session))
322 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400323
324 if (evsel == NULL)
325 continue;
326
Adrian Hunter7ea95722013-11-01 15:51:30 +0200327 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700328 }
329
Adrian Hunter98526ee2014-07-31 09:00:59 +0300330 if (!no_callchain) {
331 bool use_callchain = false;
332
333 evlist__for_each(session->evlist, evsel) {
334 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
335 use_callchain = true;
336 break;
337 }
338 }
339 if (!use_callchain)
340 symbol_conf.use_callchain = false;
341 }
342
David Ahern80b8b492013-11-19 21:07:37 -0700343 /*
344 * set default for tracepoints to print symbols only
345 * if callchains are present
346 */
347 if (symbol_conf.use_callchain &&
348 !output[PERF_TYPE_TRACEPOINT].user_set) {
349 struct perf_event_attr *attr;
350
351 j = PERF_TYPE_TRACEPOINT;
352 evsel = perf_session__find_first_evtype(session, j);
353 if (evsel == NULL)
354 goto out;
355
356 attr = &evsel->attr;
357
358 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
359 output[j].fields |= PERF_OUTPUT_IP;
360 output[j].fields |= PERF_OUTPUT_SYM;
361 output[j].fields |= PERF_OUTPUT_DSO;
362 set_print_ip_opts(attr);
363 }
364 }
365
366out:
David Ahern1424dc92011-03-09 22:23:28 -0700367 return 0;
368}
David Ahern745f43e2011-03-09 22:23:26 -0700369
Stephane Eranianfc36f942015-08-31 18:41:10 +0200370static void print_sample_iregs(union perf_event *event __maybe_unused,
371 struct perf_sample *sample,
372 struct thread *thread __maybe_unused,
373 struct perf_event_attr *attr)
374{
375 struct regs_dump *regs = &sample->intr_regs;
376 uint64_t mask = attr->sample_regs_intr;
377 unsigned i = 0, r;
378
379 if (!regs)
380 return;
381
382 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
383 u64 val = regs->regs[i++];
384 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
385 }
386}
387
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300388static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700389 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300390 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700391{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300392 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700393 unsigned long secs;
394 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700395 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700396
David Ahern745f43e2011-03-09 22:23:26 -0700397 if (PRINT_FIELD(COMM)) {
398 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200399 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600400 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200401 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700402 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200403 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700404 }
David Ahernc70c94b2011-03-09 22:23:25 -0700405
David Ahern745f43e2011-03-09 22:23:26 -0700406 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
407 printf("%5d/%-5d ", sample->pid, sample->tid);
408 else if (PRINT_FIELD(PID))
409 printf("%5d ", sample->pid);
410 else if (PRINT_FIELD(TID))
411 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700412
David Ahern745f43e2011-03-09 22:23:26 -0700413 if (PRINT_FIELD(CPU)) {
414 if (latency_format)
415 printf("%3d ", sample->cpu);
416 else
417 printf("[%03d] ", sample->cpu);
418 }
David Ahernc70c94b2011-03-09 22:23:25 -0700419
David Ahern745f43e2011-03-09 22:23:26 -0700420 if (PRINT_FIELD(TIME)) {
421 nsecs = sample->time;
422 secs = nsecs / NSECS_PER_SEC;
423 nsecs -= secs * NSECS_PER_SEC;
424 usecs = nsecs / NSECS_PER_USEC;
Adrian Hunter83e19862015-09-25 16:15:36 +0300425 if (nanosecs)
426 printf("%5lu.%09llu: ", secs, nsecs);
427 else
428 printf("%5lu.%06lu: ", secs, usecs);
David Ahern745f43e2011-03-09 22:23:26 -0700429 }
David Ahernc70c94b2011-03-09 22:23:25 -0700430}
431
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200432static inline char
433mispred_str(struct branch_entry *br)
434{
435 if (!(br->flags.mispred || br->flags.predicted))
436 return '-';
437
438 return br->flags.predicted ? 'P' : 'M';
439}
440
441static void print_sample_brstack(union perf_event *event __maybe_unused,
442 struct perf_sample *sample,
443 struct thread *thread __maybe_unused,
444 struct perf_event_attr *attr __maybe_unused)
445{
446 struct branch_stack *br = sample->branch_stack;
447 u64 i;
448
449 if (!(br && br->nr))
450 return;
451
452 for (i = 0; i < br->nr; i++) {
453 printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
454 br->entries[i].from,
455 br->entries[i].to,
456 mispred_str( br->entries + i),
457 br->entries[i].flags.in_tx? 'X' : '-',
458 br->entries[i].flags.abort? 'A' : '-',
459 br->entries[i].flags.cycles);
460 }
461}
462
463static void print_sample_brstacksym(union perf_event *event __maybe_unused,
464 struct perf_sample *sample,
465 struct thread *thread __maybe_unused,
466 struct perf_event_attr *attr __maybe_unused)
467{
468 struct branch_stack *br = sample->branch_stack;
469 struct addr_location alf, alt;
470 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
471 u64 i, from, to;
472
473 if (!(br && br->nr))
474 return;
475
476 for (i = 0; i < br->nr; i++) {
477
478 memset(&alf, 0, sizeof(alf));
479 memset(&alt, 0, sizeof(alt));
480 from = br->entries[i].from;
481 to = br->entries[i].to;
482
483 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
484 if (alf.map)
485 alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
486
487 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
488 if (alt.map)
489 alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
490
491 symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
492 putchar('/');
493 symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
494 printf("/%c/%c/%c/%d ",
495 mispred_str( br->entries + i),
496 br->entries[i].flags.in_tx? 'X' : '-',
497 br->entries[i].flags.abort? 'A' : '-',
498 br->entries[i].flags.cycles);
499 }
500}
501
502
David Ahern7cec0922011-05-30 13:08:23 -0600503static void print_sample_addr(union perf_event *event,
504 struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600505 struct thread *thread,
506 struct perf_event_attr *attr)
507{
508 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600509
510 printf("%16" PRIx64, sample->addr);
511
512 if (!sample_addr_correlates_sym(attr))
513 return;
514
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300515 perf_event__preprocess_sample_addr(event, sample, thread, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600516
517 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900518 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900519 if (PRINT_FIELD(SYMOFFSET))
520 symbol__fprintf_symname_offs(al.sym, &al, stdout);
521 else
522 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600523 }
524
525 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900526 printf(" (");
527 map__fprintf_dsoname(al.map, stdout);
528 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600529 }
530}
531
Akihiro Nagai95582592012-01-30 13:43:09 +0900532static void print_sample_bts(union perf_event *event,
533 struct perf_sample *sample,
534 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200535 struct thread *thread,
536 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900537{
538 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300539 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900540
541 /* print branch_from information */
542 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300543 unsigned int print_opts = output[attr->type].print_ip_opts;
544
545 if (symbol_conf.use_callchain && sample->callchain) {
Akihiro Nagai95582592012-01-30 13:43:09 +0900546 printf("\n");
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300547 } else {
548 printf(" ");
549 if (print_opts & PRINT_IP_OPT_SRCLINE) {
550 print_srcline_last = true;
551 print_opts &= ~PRINT_IP_OPT_SRCLINE;
552 }
553 }
554 perf_evsel__print_ip(evsel, sample, al, print_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300555 scripting_max_stack);
Akihiro Nagai95582592012-01-30 13:43:09 +0900556 }
557
Akihiro Nagai95582592012-01-30 13:43:09 +0900558 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300559 if (PRINT_FIELD(ADDR) ||
560 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300561 !output[attr->type].user_set)) {
562 printf(" => ");
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300563 print_sample_addr(event, sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300564 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900565
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300566 if (print_srcline_last)
567 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
568
Akihiro Nagai95582592012-01-30 13:43:09 +0900569 printf("\n");
570}
571
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300572static void print_sample_flags(u32 flags)
573{
574 const char *chars = PERF_IP_FLAG_CHARS;
575 const int n = strlen(PERF_IP_FLAG_CHARS);
576 char str[33];
577 int i, pos = 0;
578
579 for (i = 0; i < n; i++, flags >>= 1) {
580 if (flags & 1)
581 str[pos++] = chars[i];
582 }
583 for (; i < 32; i++, flags >>= 1) {
584 if (flags & 1)
585 str[pos++] = '?';
586 }
587 str[pos] = 0;
588 printf(" %-4s ", str);
589}
590
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300591static void process_event(union perf_event *event, struct perf_sample *sample,
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300592 struct perf_evsel *evsel, struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700593{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300594 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300595 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700596
David Ahern2c9e45f72011-03-17 10:03:21 -0600597 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700598 return;
599
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300600 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700601
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200602 if (PRINT_FIELD(PERIOD))
603 printf("%10" PRIu64 " ", sample->period);
604
Namhyung Kime944d3d2013-11-18 14:34:52 +0900605 if (PRINT_FIELD(EVNAME)) {
606 const char *evname = perf_evsel__name(evsel);
607 printf("%s: ", evname ? evname : "[unknown]");
608 }
609
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300610 if (print_flags)
611 print_sample_flags(sample->flags);
612
Akihiro Nagai95582592012-01-30 13:43:09 +0900613 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300614 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900615 return;
616 }
617
David Ahern745f43e2011-03-09 22:23:26 -0700618 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300619 event_format__print(evsel->tp_format, sample->cpu,
620 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600621 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300622 print_sample_addr(event, sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600623
David Ahern787bef12011-05-27 14:28:43 -0600624 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700625 if (!symbol_conf.use_callchain)
626 printf(" ");
627 else
628 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400629
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300630 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400631 output[attr->type].print_ip_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300632 scripting_max_stack);
David Ahernc0230b22011-03-09 22:23:27 -0700633 }
634
Stephane Eranianfc36f942015-08-31 18:41:10 +0200635 if (PRINT_FIELD(IREGS))
636 print_sample_iregs(event, sample, thread, attr);
637
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200638 if (PRINT_FIELD(BRSTACK))
639 print_sample_brstack(event, sample, thread, attr);
640 else if (PRINT_FIELD(BRSTACKSYM))
641 print_sample_brstacksym(event, sample, thread, attr);
642
David Ahernc70c94b2011-03-09 22:23:25 -0700643 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700644}
645
Tom Zanussi956ffd02009-11-25 01:15:46 -0600646static struct scripting_ops *scripting_ops;
647
648static void setup_scripting(void)
649{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600650 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600651 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600652}
653
Adrian Hunterd445dd22014-08-15 22:08:37 +0300654static int flush_scripting(void)
655{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100656 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300657}
658
Tom Zanussi956ffd02009-11-25 01:15:46 -0600659static int cleanup_scripting(void)
660{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100661 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500662
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100663 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600664}
665
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300666static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200667 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200668 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300669 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200670 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200671{
David Aherne7984b72011-11-21 10:02:52 -0700672 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200673
David Ahern1424dc92011-03-09 22:23:28 -0700674 if (debug_mode) {
675 if (sample->time < last_timestamp) {
676 pr_err("Samples misordered, previous: %" PRIu64
677 " this: %" PRIu64 "\n", last_timestamp,
678 sample->time);
679 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200680 }
David Ahern1424dc92011-03-09 22:23:28 -0700681 last_timestamp = sample->time;
682 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200683 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000684
Adrian Huntere44baa32013-08-08 14:32:25 +0300685 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700686 pr_err("problem processing %d event, skipping it.\n",
687 event->header.type);
688 return -1;
689 }
690
691 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300692 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700693
Anton Blanchard5d67be92011-07-04 21:57:50 +1000694 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300695 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000696
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100697 if (scripting_ops)
698 scripting_ops->process_event(event, sample, evsel, &al);
699 else
700 process_event(event, sample, evsel, &al);
701
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300702out_put:
703 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200704 return 0;
705}
706
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300707struct perf_script {
708 struct perf_tool tool;
709 struct perf_session *session;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900710 bool show_task_events;
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900711 bool show_mmap_events;
Adrian Hunter7c148982015-07-21 12:44:06 +0300712 bool show_switch_events;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200713};
714
Adrian Hunter7ea95722013-11-01 15:51:30 +0200715static int process_attr(struct perf_tool *tool, union perf_event *event,
716 struct perf_evlist **pevlist)
717{
718 struct perf_script *scr = container_of(tool, struct perf_script, tool);
719 struct perf_evlist *evlist;
720 struct perf_evsel *evsel, *pos;
721 int err;
722
723 err = perf_event__process_attr(tool, event, pevlist);
724 if (err)
725 return err;
726
727 evlist = *pevlist;
728 evsel = perf_evlist__last(*pevlist);
729
730 if (evsel->attr.type >= PERF_TYPE_MAX)
731 return 0;
732
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300733 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200734 if (pos->attr.type == evsel->attr.type && pos != evsel)
735 return 0;
736 }
737
738 set_print_ip_opts(&evsel->attr);
739
Jiri Olsad2b5a312015-10-16 12:41:25 +0200740 if (evsel->attr.sample_type)
741 err = perf_evsel__check_attr(evsel, scr->session);
742
743 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200744}
745
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900746static int process_comm_event(struct perf_tool *tool,
747 union perf_event *event,
748 struct perf_sample *sample,
749 struct machine *machine)
750{
751 struct thread *thread;
752 struct perf_script *script = container_of(tool, struct perf_script, tool);
753 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300754 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900755 int ret = -1;
756
757 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
758 if (thread == NULL) {
759 pr_debug("problem processing COMM event, skipping it.\n");
760 return -1;
761 }
762
763 if (perf_event__process_comm(tool, event, sample, machine) < 0)
764 goto out;
765
766 if (!evsel->attr.sample_id_all) {
767 sample->cpu = 0;
768 sample->time = 0;
769 sample->tid = event->comm.tid;
770 sample->pid = event->comm.pid;
771 }
772 print_sample_start(sample, thread, evsel);
773 perf_event__fprintf(event, stdout);
774 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900775out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300776 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900777 return ret;
778}
779
780static int process_fork_event(struct perf_tool *tool,
781 union perf_event *event,
782 struct perf_sample *sample,
783 struct machine *machine)
784{
785 struct thread *thread;
786 struct perf_script *script = container_of(tool, struct perf_script, tool);
787 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300788 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900789
790 if (perf_event__process_fork(tool, event, sample, machine) < 0)
791 return -1;
792
793 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
794 if (thread == NULL) {
795 pr_debug("problem processing FORK event, skipping it.\n");
796 return -1;
797 }
798
799 if (!evsel->attr.sample_id_all) {
800 sample->cpu = 0;
801 sample->time = event->fork.time;
802 sample->tid = event->fork.tid;
803 sample->pid = event->fork.pid;
804 }
805 print_sample_start(sample, thread, evsel);
806 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300807 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900808
809 return 0;
810}
811static int process_exit_event(struct perf_tool *tool,
812 union perf_event *event,
813 struct perf_sample *sample,
814 struct machine *machine)
815{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300816 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900817 struct thread *thread;
818 struct perf_script *script = container_of(tool, struct perf_script, tool);
819 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300820 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900821
822 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
823 if (thread == NULL) {
824 pr_debug("problem processing EXIT event, skipping it.\n");
825 return -1;
826 }
827
828 if (!evsel->attr.sample_id_all) {
829 sample->cpu = 0;
830 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +0300831 sample->tid = event->fork.tid;
832 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900833 }
834 print_sample_start(sample, thread, evsel);
835 perf_event__fprintf(event, stdout);
836
837 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300838 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900839
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300840 thread__put(thread);
841 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900842}
843
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900844static int process_mmap_event(struct perf_tool *tool,
845 union perf_event *event,
846 struct perf_sample *sample,
847 struct machine *machine)
848{
849 struct thread *thread;
850 struct perf_script *script = container_of(tool, struct perf_script, tool);
851 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300852 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900853
854 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
855 return -1;
856
857 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
858 if (thread == NULL) {
859 pr_debug("problem processing MMAP event, skipping it.\n");
860 return -1;
861 }
862
863 if (!evsel->attr.sample_id_all) {
864 sample->cpu = 0;
865 sample->time = 0;
866 sample->tid = event->mmap.tid;
867 sample->pid = event->mmap.pid;
868 }
869 print_sample_start(sample, thread, evsel);
870 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300871 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900872 return 0;
873}
874
875static int process_mmap2_event(struct perf_tool *tool,
876 union perf_event *event,
877 struct perf_sample *sample,
878 struct machine *machine)
879{
880 struct thread *thread;
881 struct perf_script *script = container_of(tool, struct perf_script, tool);
882 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300883 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900884
885 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
886 return -1;
887
888 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
889 if (thread == NULL) {
890 pr_debug("problem processing MMAP2 event, skipping it.\n");
891 return -1;
892 }
893
894 if (!evsel->attr.sample_id_all) {
895 sample->cpu = 0;
896 sample->time = 0;
897 sample->tid = event->mmap2.tid;
898 sample->pid = event->mmap2.pid;
899 }
900 print_sample_start(sample, thread, evsel);
901 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300902 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900903 return 0;
904}
905
Adrian Hunter7c148982015-07-21 12:44:06 +0300906static int process_switch_event(struct perf_tool *tool,
907 union perf_event *event,
908 struct perf_sample *sample,
909 struct machine *machine)
910{
911 struct thread *thread;
912 struct perf_script *script = container_of(tool, struct perf_script, tool);
913 struct perf_session *session = script->session;
914 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
915
916 if (perf_event__process_switch(tool, event, sample, machine) < 0)
917 return -1;
918
919 thread = machine__findnew_thread(machine, sample->pid,
920 sample->tid);
921 if (thread == NULL) {
922 pr_debug("problem processing SWITCH event, skipping it.\n");
923 return -1;
924 }
925
926 print_sample_start(sample, thread, evsel);
927 perf_event__fprintf(event, stdout);
928 thread__put(thread);
929 return 0;
930}
931
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300932static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500933{
934 session_done = 1;
935}
936
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300937static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200938{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200939 int ret;
940
Tom Zanussic239da32010-04-01 23:59:18 -0500941 signal(SIGINT, sig_handler);
942
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900943 /* override event processing functions */
944 if (script->show_task_events) {
945 script->tool.comm = process_comm_event;
946 script->tool.fork = process_fork_event;
947 script->tool.exit = process_exit_event;
948 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900949 if (script->show_mmap_events) {
950 script->tool.mmap = process_mmap_event;
951 script->tool.mmap2 = process_mmap2_event;
952 }
Adrian Hunter7c148982015-07-21 12:44:06 +0300953 if (script->show_switch_events)
954 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900955
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300956 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200957
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200958 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200959 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200960
961 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200962}
963
Tom Zanussi956ffd02009-11-25 01:15:46 -0600964struct script_spec {
965 struct list_head node;
966 struct scripting_ops *ops;
967 char spec[0];
968};
969
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200970static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600971
972static struct script_spec *script_spec__new(const char *spec,
973 struct scripting_ops *ops)
974{
975 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
976
977 if (s != NULL) {
978 strcpy(s->spec, spec);
979 s->ops = ops;
980 }
981
982 return s;
983}
984
Tom Zanussi956ffd02009-11-25 01:15:46 -0600985static void script_spec__add(struct script_spec *s)
986{
987 list_add_tail(&s->node, &script_specs);
988}
989
990static struct script_spec *script_spec__find(const char *spec)
991{
992 struct script_spec *s;
993
994 list_for_each_entry(s, &script_specs, node)
995 if (strcasecmp(s->spec, spec) == 0)
996 return s;
997 return NULL;
998}
999
1000static struct script_spec *script_spec__findnew(const char *spec,
1001 struct scripting_ops *ops)
1002{
1003 struct script_spec *s = script_spec__find(spec);
1004
1005 if (s)
1006 return s;
1007
1008 s = script_spec__new(spec, ops);
1009 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +09001010 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001011
1012 script_spec__add(s);
1013
1014 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001015}
1016
1017int script_spec_register(const char *spec, struct scripting_ops *ops)
1018{
1019 struct script_spec *s;
1020
1021 s = script_spec__find(spec);
1022 if (s)
1023 return -1;
1024
1025 s = script_spec__findnew(spec, ops);
1026 if (!s)
1027 return -1;
1028
1029 return 0;
1030}
1031
1032static struct scripting_ops *script_spec__lookup(const char *spec)
1033{
1034 struct script_spec *s = script_spec__find(spec);
1035 if (!s)
1036 return NULL;
1037
1038 return s->ops;
1039}
1040
1041static void list_available_languages(void)
1042{
1043 struct script_spec *s;
1044
1045 fprintf(stderr, "\n");
1046 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001047 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001048
1049 list_for_each_entry(s, &script_specs, node)
1050 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1051
1052 fprintf(stderr, "\n");
1053}
1054
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001055static int parse_scriptname(const struct option *opt __maybe_unused,
1056 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001057{
1058 char spec[PATH_MAX];
1059 const char *script, *ext;
1060 int len;
1061
Tom Zanussif526d682010-01-27 02:27:52 -06001062 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001063 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001064 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001065 }
1066
1067 script = strchr(str, ':');
1068 if (script) {
1069 len = script - str;
1070 if (len >= PATH_MAX) {
1071 fprintf(stderr, "invalid language specifier");
1072 return -1;
1073 }
1074 strncpy(spec, str, len);
1075 spec[len] = '\0';
1076 scripting_ops = script_spec__lookup(spec);
1077 if (!scripting_ops) {
1078 fprintf(stderr, "invalid language specifier");
1079 return -1;
1080 }
1081 script++;
1082 } else {
1083 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001084 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001085 if (!ext) {
1086 fprintf(stderr, "invalid script extension");
1087 return -1;
1088 }
1089 scripting_ops = script_spec__lookup(++ext);
1090 if (!scripting_ops) {
1091 fprintf(stderr, "invalid script extension");
1092 return -1;
1093 }
1094 }
1095
1096 script_name = strdup(script);
1097
1098 return 0;
1099}
1100
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001101static int parse_output_fields(const struct option *opt __maybe_unused,
1102 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001103{
1104 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001105 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001106 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001107 int rc = 0;
1108 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001109 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001110
1111 if (!str)
1112 return -ENOMEM;
1113
David Ahern2c9e45f72011-03-17 10:03:21 -06001114 /* first word can state for which event type the user is specifying
1115 * the fields. If no type exists, the specified fields apply to all
1116 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001117 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001118 tok = strchr(str, ':');
1119 if (tok) {
1120 *tok = '\0';
1121 tok++;
1122 if (!strcmp(str, "hw"))
1123 type = PERF_TYPE_HARDWARE;
1124 else if (!strcmp(str, "sw"))
1125 type = PERF_TYPE_SOFTWARE;
1126 else if (!strcmp(str, "trace"))
1127 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001128 else if (!strcmp(str, "raw"))
1129 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -06001130 else {
1131 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001132 rc = -EINVAL;
1133 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001134 }
1135
1136 if (output[type].user_set)
1137 pr_warning("Overriding previous field request for %s events.\n",
1138 event_type(type));
1139
1140 output[type].fields = 0;
1141 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001142 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001143
1144 } else {
1145 tok = str;
1146 if (strlen(str) == 0) {
1147 fprintf(stderr,
1148 "Cannot set fields to 'none' for all event types.\n");
1149 rc = -EINVAL;
1150 goto out;
1151 }
1152
1153 if (output_set_by_user())
1154 pr_warning("Overriding previous field request for all events.\n");
1155
1156 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1157 output[j].fields = 0;
1158 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001159 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001160 }
David Ahern1424dc92011-03-09 22:23:28 -07001161 }
1162
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001163 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001164 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001165 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001166 break;
David Ahern745f43e2011-03-09 22:23:26 -07001167 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001168 if (i == imax && strcmp(tok, "flags") == 0) {
1169 print_flags = true;
1170 continue;
1171 }
David Ahern745f43e2011-03-09 22:23:26 -07001172 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001173 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001174 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001175 goto out;
1176 }
1177
1178 if (type == -1) {
1179 /* add user option to all events types for
1180 * which it is valid
1181 */
1182 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1183 if (output[j].invalid_fields & all_output_options[i].field) {
1184 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1185 all_output_options[i].str, event_type(j));
1186 } else
1187 output[j].fields |= all_output_options[i].field;
1188 }
1189 } else {
1190 if (output[type].invalid_fields & all_output_options[i].field) {
1191 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1192 all_output_options[i].str, event_type(type));
1193
1194 rc = -EINVAL;
1195 goto out;
1196 }
1197 output[type].fields |= all_output_options[i].field;
1198 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001199 }
1200
1201 if (type >= 0) {
1202 if (output[type].fields == 0) {
1203 pr_debug("No fields requested for %s type. "
1204 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001205 }
David Ahern1424dc92011-03-09 22:23:28 -07001206 }
David Ahern745f43e2011-03-09 22:23:26 -07001207
David Ahern2c9e45f72011-03-17 10:03:21 -06001208out:
David Ahern745f43e2011-03-09 22:23:26 -07001209 free(str);
1210 return rc;
1211}
1212
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001213/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1214static int is_directory(const char *base_path, const struct dirent *dent)
1215{
1216 char path[PATH_MAX];
1217 struct stat st;
1218
1219 sprintf(path, "%s/%s", base_path, dent->d_name);
1220 if (stat(path, &st))
1221 return 0;
1222
1223 return S_ISDIR(st.st_mode);
1224}
1225
1226#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001227 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1228 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001229 if ((lang_dirent.d_type == DT_DIR || \
1230 (lang_dirent.d_type == DT_UNKNOWN && \
1231 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001232 (strcmp(lang_dirent.d_name, ".")) && \
1233 (strcmp(lang_dirent.d_name, "..")))
1234
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001235#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001236 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1237 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001238 if (script_dirent.d_type != DT_DIR && \
1239 (script_dirent.d_type != DT_UNKNOWN || \
1240 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001241
1242
1243#define RECORD_SUFFIX "-record"
1244#define REPORT_SUFFIX "-report"
1245
1246struct script_desc {
1247 struct list_head node;
1248 char *name;
1249 char *half_liner;
1250 char *args;
1251};
1252
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001253static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001254
1255static struct script_desc *script_desc__new(const char *name)
1256{
1257 struct script_desc *s = zalloc(sizeof(*s));
1258
Tom Zanussib5b87312010-11-10 08:16:51 -06001259 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001260 s->name = strdup(name);
1261
1262 return s;
1263}
1264
1265static void script_desc__delete(struct script_desc *s)
1266{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001267 zfree(&s->name);
1268 zfree(&s->half_liner);
1269 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001270 free(s);
1271}
1272
1273static void script_desc__add(struct script_desc *s)
1274{
1275 list_add_tail(&s->node, &script_descs);
1276}
1277
1278static struct script_desc *script_desc__find(const char *name)
1279{
1280 struct script_desc *s;
1281
1282 list_for_each_entry(s, &script_descs, node)
1283 if (strcasecmp(s->name, name) == 0)
1284 return s;
1285 return NULL;
1286}
1287
1288static struct script_desc *script_desc__findnew(const char *name)
1289{
1290 struct script_desc *s = script_desc__find(name);
1291
1292 if (s)
1293 return s;
1294
1295 s = script_desc__new(name);
1296 if (!s)
1297 goto out_delete_desc;
1298
1299 script_desc__add(s);
1300
1301 return s;
1302
1303out_delete_desc:
1304 script_desc__delete(s);
1305
1306 return NULL;
1307}
1308
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001309static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001310{
1311 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001312 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001313
1314 if (strlen(str) > suffix_len) {
1315 p = str + strlen(str) - suffix_len;
1316 if (!strncmp(p, suffix, suffix_len))
1317 return p;
1318 }
1319
1320 return NULL;
1321}
1322
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001323static int read_script_info(struct script_desc *desc, const char *filename)
1324{
1325 char line[BUFSIZ], *p;
1326 FILE *fp;
1327
1328 fp = fopen(filename, "r");
1329 if (!fp)
1330 return -1;
1331
1332 while (fgets(line, sizeof(line), fp)) {
1333 p = ltrim(line);
1334 if (strlen(p) == 0)
1335 continue;
1336 if (*p != '#')
1337 continue;
1338 p++;
1339 if (strlen(p) && *p == '!')
1340 continue;
1341
1342 p = ltrim(p);
1343 if (strlen(p) && p[strlen(p) - 1] == '\n')
1344 p[strlen(p) - 1] = '\0';
1345
1346 if (!strncmp(p, "description:", strlen("description:"))) {
1347 p += strlen("description:");
1348 desc->half_liner = strdup(ltrim(p));
1349 continue;
1350 }
1351
1352 if (!strncmp(p, "args:", strlen("args:"))) {
1353 p += strlen("args:");
1354 desc->args = strdup(ltrim(p));
1355 continue;
1356 }
1357 }
1358
1359 fclose(fp);
1360
1361 return 0;
1362}
1363
Robert Richter38efb532011-11-25 11:38:40 +01001364static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1365{
1366 char *script_root, *str;
1367
1368 script_root = strdup(script_dirent->d_name);
1369 if (!script_root)
1370 return NULL;
1371
1372 str = (char *)ends_with(script_root, suffix);
1373 if (!str) {
1374 free(script_root);
1375 return NULL;
1376 }
1377
1378 *str = '\0';
1379 return script_root;
1380}
1381
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001382static int list_available_scripts(const struct option *opt __maybe_unused,
1383 const char *s __maybe_unused,
1384 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001385{
1386 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1387 char scripts_path[MAXPATHLEN];
1388 DIR *scripts_dir, *lang_dir;
1389 char script_path[MAXPATHLEN];
1390 char lang_path[MAXPATHLEN];
1391 struct script_desc *desc;
1392 char first_half[BUFSIZ];
1393 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001394
1395 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1396
1397 scripts_dir = opendir(scripts_path);
1398 if (!scripts_dir)
1399 return -1;
1400
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001401 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001402 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1403 lang_dirent.d_name);
1404 lang_dir = opendir(lang_path);
1405 if (!lang_dir)
1406 continue;
1407
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001408 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001409 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1410 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001411 desc = script_desc__findnew(script_root);
1412 snprintf(script_path, MAXPATHLEN, "%s/%s",
1413 lang_path, script_dirent.d_name);
1414 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001415 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001416 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001417 }
1418 }
1419
1420 fprintf(stdout, "List of available trace scripts:\n");
1421 list_for_each_entry(desc, &script_descs, node) {
1422 sprintf(first_half, "%s %s", desc->name,
1423 desc->args ? desc->args : "");
1424 fprintf(stdout, " %-36s %s\n", first_half,
1425 desc->half_liner ? desc->half_liner : "");
1426 }
1427
1428 exit(0);
1429}
1430
Feng Tange5f37052012-09-07 16:42:26 +08001431/*
Feng Tang49e639e2012-10-30 11:56:03 +08001432 * Some scripts specify the required events in their "xxx-record" file,
1433 * this function will check if the events in perf.data match those
1434 * mentioned in the "xxx-record".
1435 *
1436 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1437 * which is covered well now. And new parsing code should be added to
1438 * cover the future complexing formats like event groups etc.
1439 */
1440static int check_ev_match(char *dir_name, char *scriptname,
1441 struct perf_session *session)
1442{
1443 char filename[MAXPATHLEN], evname[128];
1444 char line[BUFSIZ], *p;
1445 struct perf_evsel *pos;
1446 int match, len;
1447 FILE *fp;
1448
1449 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1450
1451 fp = fopen(filename, "r");
1452 if (!fp)
1453 return -1;
1454
1455 while (fgets(line, sizeof(line), fp)) {
1456 p = ltrim(line);
1457 if (*p == '#')
1458 continue;
1459
1460 while (strlen(p)) {
1461 p = strstr(p, "-e");
1462 if (!p)
1463 break;
1464
1465 p += 2;
1466 p = ltrim(p);
1467 len = strcspn(p, " \t");
1468 if (!len)
1469 break;
1470
1471 snprintf(evname, len + 1, "%s", p);
1472
1473 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001474 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001475 if (!strcmp(perf_evsel__name(pos), evname)) {
1476 match = 1;
1477 break;
1478 }
1479 }
1480
1481 if (!match) {
1482 fclose(fp);
1483 return -1;
1484 }
1485 }
1486 }
1487
1488 fclose(fp);
1489 return 0;
1490}
1491
1492/*
Feng Tange5f37052012-09-07 16:42:26 +08001493 * Return -1 if none is found, otherwise the actual scripts number.
1494 *
1495 * Currently the only user of this function is the script browser, which
1496 * will list all statically runnable scripts, select one, execute it and
1497 * show the output in a perf browser.
1498 */
1499int find_scripts(char **scripts_array, char **scripts_path_array)
1500{
1501 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001502 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001503 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001504 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001505 struct perf_data_file file = {
1506 .path = input_name,
1507 .mode = PERF_DATA_MODE_READ,
1508 };
Feng Tange5f37052012-09-07 16:42:26 +08001509 char *temp;
1510 int i = 0;
1511
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001512 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001513 if (!session)
1514 return -1;
1515
Feng Tange5f37052012-09-07 16:42:26 +08001516 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1517
1518 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001519 if (!scripts_dir) {
1520 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001521 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001522 }
Feng Tange5f37052012-09-07 16:42:26 +08001523
1524 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1525 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1526 lang_dirent.d_name);
1527#ifdef NO_LIBPERL
1528 if (strstr(lang_path, "perl"))
1529 continue;
1530#endif
1531#ifdef NO_LIBPYTHON
1532 if (strstr(lang_path, "python"))
1533 continue;
1534#endif
1535
1536 lang_dir = opendir(lang_path);
1537 if (!lang_dir)
1538 continue;
1539
1540 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1541 /* Skip those real time scripts: xxxtop.p[yl] */
1542 if (strstr(script_dirent.d_name, "top."))
1543 continue;
1544 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1545 script_dirent.d_name);
1546 temp = strchr(script_dirent.d_name, '.');
1547 snprintf(scripts_array[i],
1548 (temp - script_dirent.d_name) + 1,
1549 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001550
1551 if (check_ev_match(lang_path,
1552 scripts_array[i], session))
1553 continue;
1554
Feng Tange5f37052012-09-07 16:42:26 +08001555 i++;
1556 }
Feng Tang49e639e2012-10-30 11:56:03 +08001557 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001558 }
1559
Feng Tang49e639e2012-10-30 11:56:03 +08001560 closedir(scripts_dir);
1561 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001562 return i;
1563}
1564
Tom Zanussi38752942009-12-15 02:53:39 -06001565static char *get_script_path(const char *script_root, const char *suffix)
1566{
1567 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1568 char scripts_path[MAXPATHLEN];
1569 char script_path[MAXPATHLEN];
1570 DIR *scripts_dir, *lang_dir;
1571 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001572 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001573
1574 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1575
1576 scripts_dir = opendir(scripts_path);
1577 if (!scripts_dir)
1578 return NULL;
1579
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001580 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001581 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1582 lang_dirent.d_name);
1583 lang_dir = opendir(lang_path);
1584 if (!lang_dir)
1585 continue;
1586
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001587 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001588 __script_root = get_script_root(&script_dirent, suffix);
1589 if (__script_root && !strcmp(script_root, __script_root)) {
1590 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001591 closedir(lang_dir);
1592 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001593 snprintf(script_path, MAXPATHLEN, "%s/%s",
1594 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001595 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001596 }
1597 free(__script_root);
1598 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001599 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001600 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001601 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001602
Robert Richter38efb532011-11-25 11:38:40 +01001603 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001604}
1605
Tom Zanussib5b87312010-11-10 08:16:51 -06001606static bool is_top_script(const char *script_path)
1607{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001608 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001609}
1610
1611static int has_required_arg(char *script_path)
1612{
1613 struct script_desc *desc;
1614 int n_args = 0;
1615 char *p;
1616
1617 desc = script_desc__new(NULL);
1618
1619 if (read_script_info(desc, script_path))
1620 goto out;
1621
1622 if (!desc->args)
1623 goto out;
1624
1625 for (p = desc->args; *p; p++)
1626 if (*p == '<')
1627 n_args++;
1628out:
1629 script_desc__delete(desc);
1630
1631 return n_args;
1632}
1633
David Ahernd54b1a92012-08-26 12:24:46 -06001634static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001635{
1636 char **__argv = malloc(sizeof(const char *) * argc);
1637
David Ahernd54b1a92012-08-26 12:24:46 -06001638 if (!__argv) {
1639 pr_err("malloc failed\n");
1640 return -1;
1641 }
1642
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001643 memcpy(__argv, argv, sizeof(const char *) * argc);
1644 argc = parse_options(argc, (const char **)__argv, record_options,
1645 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1646 free(__argv);
1647
David Ahernd54b1a92012-08-26 12:24:46 -06001648 system_wide = (argc == 0);
1649
1650 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001651}
1652
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001653static void script__setup_sample_type(struct perf_script *script)
1654{
1655 struct perf_session *session = script->session;
1656 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1657
1658 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1659 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1660 (sample_type & PERF_SAMPLE_STACK_USER))
1661 callchain_param.record_mode = CALLCHAIN_DWARF;
1662 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1663 callchain_param.record_mode = CALLCHAIN_LBR;
1664 else
1665 callchain_param.record_mode = CALLCHAIN_FP;
1666 }
1667}
1668
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001669int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001670{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001671 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001672 bool header = false;
1673 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001674 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001675 char *rec_script_path = NULL;
1676 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001677 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001678 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001679 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001680 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001681 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001682 struct perf_script script = {
1683 .tool = {
1684 .sample = process_sample_event,
1685 .mmap = perf_event__process_mmap,
1686 .mmap2 = perf_event__process_mmap2,
1687 .comm = perf_event__process_comm,
1688 .exit = perf_event__process_exit,
1689 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001690 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001691 .tracing_data = perf_event__process_tracing_data,
1692 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001693 .id_index = perf_event__process_id_index,
1694 .auxtrace_info = perf_event__process_auxtrace_info,
1695 .auxtrace = perf_event__process_auxtrace,
1696 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001697 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001698 .ordering_requires_timestamps = true,
1699 },
1700 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001701 struct perf_data_file file = {
1702 .mode = PERF_DATA_MODE_READ,
1703 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001704 const struct option options[] = {
1705 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1706 "dump raw trace in ASCII"),
1707 OPT_INCR('v', "verbose", &verbose,
1708 "be more verbose (show symbol address, etc)"),
1709 OPT_BOOLEAN('L', "Latency", &latency_format,
1710 "show latency attributes (irqs/preemption disabled, etc)"),
1711 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1712 list_available_scripts),
1713 OPT_CALLBACK('s', "script", NULL, "name",
1714 "script file name (lang:script name, script name, or *)",
1715 parse_scriptname),
1716 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1717 "generate perf-script.xx script in specified language"),
1718 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1719 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1720 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001721 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1722 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001723 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1724 "file", "vmlinux pathname"),
1725 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1726 "file", "kallsyms pathname"),
1727 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1728 "When printing symbols do not display call chain"),
1729 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1730 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001731 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001732 "comma separated output fields prepend with 'type:'. "
1733 "Valid types: hw,sw,trace,raw. "
1734 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001735 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001736 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1737 "system-wide collection from all CPUs"),
1738 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1739 "only consider these symbols"),
1740 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1741 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1742 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06001743 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1744 "only consider symbols in these pids"),
1745 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1746 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001747 OPT_BOOLEAN('I', "show-info", &show_full_info,
1748 "display extended information from perf.data file"),
1749 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1750 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001751 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1752 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001753 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1754 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03001755 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1756 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001757 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03001758 OPT_BOOLEAN(0, "ns", &nanosecs,
1759 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001760 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1761 "Instruction Tracing options",
1762 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07001763 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1764 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07001765 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1766 "Enable symbol demangling"),
1767 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1768 "Enable kernel symbol demangling"),
1769
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001770 OPT_END()
1771 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08001772 const char * const script_subcommands[] = { "record", "report", NULL };
1773 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001774 "perf script [<options>]",
1775 "perf script [<options>] record <script> [<record-options>] <command>",
1776 "perf script [<options>] report <script> [script-args]",
1777 "perf script [<options>] <script> [<record-options>] <command>",
1778 "perf script [<options>] <top-script> [script-args]",
1779 NULL
1780 };
Tom Zanussi38752942009-12-15 02:53:39 -06001781
Tom Zanussib5b87312010-11-10 08:16:51 -06001782 setup_scripting();
1783
Yunlong Song40cae2b2015-03-18 21:35:54 +08001784 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001785 PARSE_OPT_STOP_AT_NON_OPTION);
1786
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001787 file.path = input_name;
1788
Tom Zanussib5b87312010-11-10 08:16:51 -06001789 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1790 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1791 if (!rec_script_path)
1792 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001793 }
1794
Tom Zanussib5b87312010-11-10 08:16:51 -06001795 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1796 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1797 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001798 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001799 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001800 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001801 return -1;
1802 }
Tom Zanussi38752942009-12-15 02:53:39 -06001803 }
1804
Adrian Hunter3c5b6452015-09-25 16:15:51 +03001805 if (itrace_synth_opts.callchain &&
1806 itrace_synth_opts.callchain_sz > scripting_max_stack)
1807 scripting_max_stack = itrace_synth_opts.callchain_sz;
1808
Ben Hutchings44e668c2010-10-10 16:10:03 +01001809 /* make sure PERF_EXEC_PATH is set for scripts */
1810 perf_set_argv_exec_path(perf_exec_path());
1811
Tom Zanussib5b87312010-11-10 08:16:51 -06001812 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001813 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001814 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001815 pid_t pid;
1816
Tom Zanussib5b87312010-11-10 08:16:51 -06001817 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1818 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1819
1820 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001821 usage_with_options_msg(script_usage, options,
1822 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001823 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001824 }
1825
Tom Zanussib5b87312010-11-10 08:16:51 -06001826 if (is_top_script(argv[0])) {
1827 rep_args = argc - 1;
1828 } else {
1829 int rec_args;
1830
1831 rep_args = has_required_arg(rep_script_path);
1832 rec_args = (argc - 1) - rep_args;
1833 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001834 usage_with_options_msg(script_usage, options,
1835 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001836 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001837 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06001838 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001839 }
1840
1841 if (pipe(live_pipe) < 0) {
1842 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001843 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001844 }
1845
1846 pid = fork();
1847 if (pid < 0) {
1848 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001849 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001850 }
1851
1852 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001853 j = 0;
1854
Tom Zanussia0cccc22010-04-01 23:59:25 -05001855 dup2(live_pipe[1], 1);
1856 close(live_pipe[0]);
1857
Robert Richter317df652011-11-25 15:05:25 +01001858 if (is_top_script(argv[0])) {
1859 system_wide = true;
1860 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001861 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1862 err = -1;
1863 goto out;
1864 }
Robert Richter317df652011-11-25 15:05:25 +01001865 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001866
1867 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001868 if (!__argv) {
1869 pr_err("malloc failed\n");
1870 err = -ENOMEM;
1871 goto out;
1872 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001873
Tom Zanussib5b87312010-11-10 08:16:51 -06001874 __argv[j++] = "/bin/sh";
1875 __argv[j++] = rec_script_path;
1876 if (system_wide)
1877 __argv[j++] = "-a";
1878 __argv[j++] = "-q";
1879 __argv[j++] = "-o";
1880 __argv[j++] = "-";
1881 for (i = rep_args + 1; i < argc; i++)
1882 __argv[j++] = argv[i];
1883 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001884
1885 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001886 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001887 exit(-1);
1888 }
1889
1890 dup2(live_pipe[0], 0);
1891 close(live_pipe[1]);
1892
Tom Zanussib5b87312010-11-10 08:16:51 -06001893 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001894 if (!__argv) {
1895 pr_err("malloc failed\n");
1896 err = -ENOMEM;
1897 goto out;
1898 }
1899
Tom Zanussib5b87312010-11-10 08:16:51 -06001900 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001901 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001902 __argv[j++] = rep_script_path;
1903 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001904 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001905 __argv[j++] = "-i";
1906 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001907 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001908
1909 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001910 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001911 exit(-1);
1912 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001913
Tom Zanussib5b87312010-11-10 08:16:51 -06001914 if (rec_script_path)
1915 script_path = rec_script_path;
1916 if (rep_script_path)
1917 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001918
Tom Zanussib5b87312010-11-10 08:16:51 -06001919 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001920 j = 0;
1921
Robert Richter317df652011-11-25 15:05:25 +01001922 if (!rec_script_path)
1923 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001924 else if (!system_wide) {
1925 if (have_cmd(argc - 1, &argv[1]) != 0) {
1926 err = -1;
1927 goto out;
1928 }
1929 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001930
1931 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001932 if (!__argv) {
1933 pr_err("malloc failed\n");
1934 err = -ENOMEM;
1935 goto out;
1936 }
1937
Tom Zanussib5b87312010-11-10 08:16:51 -06001938 __argv[j++] = "/bin/sh";
1939 __argv[j++] = script_path;
1940 if (system_wide)
1941 __argv[j++] = "-a";
1942 for (i = 2; i < argc; i++)
1943 __argv[j++] = argv[i];
1944 __argv[j++] = NULL;
1945
1946 execvp("/bin/sh", (char **)__argv);
1947 free(__argv);
1948 exit(-1);
1949 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001950
Tom Zanussicf4fee52010-03-03 01:04:33 -06001951 if (!script_name)
1952 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001953
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001954 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001955 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001956 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001957
Jiri Olsae90debd2013-12-09 11:02:50 +01001958 if (header || header_only) {
1959 perf_session__fprintf_info(session, stdout, show_full_info);
1960 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001961 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01001962 }
1963
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001964 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09001965 goto out_delete;
1966
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001967 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001968 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001969
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001970 session->itrace_synth_opts = &itrace_synth_opts;
1971
Anton Blanchard5d67be92011-07-04 21:57:50 +10001972 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001973 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1974 if (err < 0)
1975 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001976 }
1977
David Ahern1424dc92011-03-09 22:23:28 -07001978 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001979 symbol_conf.use_callchain = true;
1980 else
1981 symbol_conf.use_callchain = false;
1982
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03001983 if (session->tevent.pevent &&
1984 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03001985 machine__resolve_kernel_addr,
1986 &session->machines.host) < 0) {
1987 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
1988 return -1;
1989 }
1990
Tom Zanussi956ffd02009-11-25 01:15:46 -06001991 if (generate_script_lang) {
1992 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001993 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001994
David Ahern2c9e45f72011-03-17 10:03:21 -06001995 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001996 fprintf(stderr,
1997 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001998 err = -EINVAL;
1999 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002000 }
2001
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002002 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002003 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002004 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002005 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002006 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002007 }
2008
2009 err = fstat(input, &perf_stat);
2010 if (err < 0) {
2011 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002012 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002013 }
2014
2015 if (!perf_stat.st_size) {
2016 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002017 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002018 }
2019
2020 scripting_ops = script_spec__lookup(generate_script_lang);
2021 if (!scripting_ops) {
2022 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002023 err = -ENOENT;
2024 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002025 }
2026
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002027 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002028 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002029 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002030 }
2031
2032 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002033 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002034 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002035 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002036 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002037 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002038 }
2039
David Ahern9cbdb702011-04-06 21:54:20 -06002040
2041 err = perf_session__check_output_opt(session);
2042 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002043 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002044
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002045 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002046
Adrian Hunterd445dd22014-08-15 22:08:37 +03002047 flush_scripting();
2048
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002049out_delete:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002050 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002051
2052 if (script_started)
2053 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002054out:
2055 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002056}