blob: 3c3f8d0e3064ecdd623808085ceb1155a5edaa0b [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 Olsaf5fc14122013-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 Olsa535aeaae2014-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 Olsa535aeaae2014-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 Olsa535aeaae2014-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
Jiri Olsa809e9422015-11-26 18:55:21 +0100591struct perf_script {
592 struct perf_tool tool;
593 struct perf_session *session;
594 bool show_task_events;
595 bool show_mmap_events;
596 bool show_switch_events;
597};
598
599static void process_event(struct perf_script *script __maybe_unused, union perf_event *event,
600 struct perf_sample *sample, struct perf_evsel *evsel,
601 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700602{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300603 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300604 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700605
David Ahern2c9e45f72011-03-17 10:03:21 -0600606 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700607 return;
608
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300609 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700610
Jiri Olsa535aeaae2014-08-25 16:45:42 +0200611 if (PRINT_FIELD(PERIOD))
612 printf("%10" PRIu64 " ", sample->period);
613
Namhyung Kime944d3d2013-11-18 14:34:52 +0900614 if (PRINT_FIELD(EVNAME)) {
615 const char *evname = perf_evsel__name(evsel);
616 printf("%s: ", evname ? evname : "[unknown]");
617 }
618
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300619 if (print_flags)
620 print_sample_flags(sample->flags);
621
Akihiro Nagai95582592012-01-30 13:43:09 +0900622 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300623 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900624 return;
625 }
626
David Ahern745f43e2011-03-09 22:23:26 -0700627 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300628 event_format__print(evsel->tp_format, sample->cpu,
629 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600630 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300631 print_sample_addr(event, sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600632
David Ahern787bef12011-05-27 14:28:43 -0600633 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700634 if (!symbol_conf.use_callchain)
635 printf(" ");
636 else
637 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400638
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300639 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400640 output[attr->type].print_ip_opts,
Adrian Hunter03cd1fe2015-09-25 16:15:49 +0300641 scripting_max_stack);
David Ahernc0230b22011-03-09 22:23:27 -0700642 }
643
Stephane Eranianfc36f942015-08-31 18:41:10 +0200644 if (PRINT_FIELD(IREGS))
645 print_sample_iregs(event, sample, thread, attr);
646
Stephane Eraniandc323ce2015-08-31 18:41:13 +0200647 if (PRINT_FIELD(BRSTACK))
648 print_sample_brstack(event, sample, thread, attr);
649 else if (PRINT_FIELD(BRSTACKSYM))
650 print_sample_brstacksym(event, sample, thread, attr);
651
David Ahernc70c94b2011-03-09 22:23:25 -0700652 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700653}
654
Tom Zanussi956ffd02009-11-25 01:15:46 -0600655static struct scripting_ops *scripting_ops;
656
657static void setup_scripting(void)
658{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600659 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600660 setup_python_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -0600661}
662
Adrian Hunterd445dd22014-08-15 22:08:37 +0300663static int flush_scripting(void)
664{
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100665 return scripting_ops ? scripting_ops->flush_script() : 0;
Adrian Hunterd445dd22014-08-15 22:08:37 +0300666}
667
Tom Zanussi956ffd02009-11-25 01:15:46 -0600668static int cleanup_scripting(void)
669{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100670 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500671
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100672 return scripting_ops ? scripting_ops->stop_script() : 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600673}
674
Jiri Olsa809e9422015-11-26 18:55:21 +0100675static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200676 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200677 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300678 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200679 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200680{
Jiri Olsa809e9422015-11-26 18:55:21 +0100681 struct perf_script *scr = container_of(tool, struct perf_script, tool);
David Aherne7984b72011-11-21 10:02:52 -0700682 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200683
David Ahern1424dc92011-03-09 22:23:28 -0700684 if (debug_mode) {
685 if (sample->time < last_timestamp) {
686 pr_err("Samples misordered, previous: %" PRIu64
687 " this: %" PRIu64 "\n", last_timestamp,
688 sample->time);
689 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200690 }
David Ahern1424dc92011-03-09 22:23:28 -0700691 last_timestamp = sample->time;
692 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200693 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000694
Adrian Huntere44baa32013-08-08 14:32:25 +0300695 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700696 pr_err("problem processing %d event, skipping it.\n",
697 event->header.type);
698 return -1;
699 }
700
701 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300702 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700703
Anton Blanchard5d67be92011-07-04 21:57:50 +1000704 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300705 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000706
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100707 if (scripting_ops)
708 scripting_ops->process_event(event, sample, evsel, &al);
709 else
Jiri Olsa809e9422015-11-26 18:55:21 +0100710 process_event(scr, event, sample, evsel, &al);
Jiri Olsa2aaecfc2015-11-26 14:55:23 +0100711
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300712out_put:
713 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200714 return 0;
715}
716
Adrian Hunter7ea95722013-11-01 15:51:30 +0200717static int process_attr(struct perf_tool *tool, union perf_event *event,
718 struct perf_evlist **pevlist)
719{
720 struct perf_script *scr = container_of(tool, struct perf_script, tool);
721 struct perf_evlist *evlist;
722 struct perf_evsel *evsel, *pos;
723 int err;
724
725 err = perf_event__process_attr(tool, event, pevlist);
726 if (err)
727 return err;
728
729 evlist = *pevlist;
730 evsel = perf_evlist__last(*pevlist);
731
732 if (evsel->attr.type >= PERF_TYPE_MAX)
733 return 0;
734
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300735 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200736 if (pos->attr.type == evsel->attr.type && pos != evsel)
737 return 0;
738 }
739
740 set_print_ip_opts(&evsel->attr);
741
Jiri Olsad2b5a312015-10-16 12:41:25 +0200742 if (evsel->attr.sample_type)
743 err = perf_evsel__check_attr(evsel, scr->session);
744
745 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200746}
747
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900748static int process_comm_event(struct perf_tool *tool,
749 union perf_event *event,
750 struct perf_sample *sample,
751 struct machine *machine)
752{
753 struct thread *thread;
754 struct perf_script *script = container_of(tool, struct perf_script, tool);
755 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300756 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900757 int ret = -1;
758
759 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
760 if (thread == NULL) {
761 pr_debug("problem processing COMM event, skipping it.\n");
762 return -1;
763 }
764
765 if (perf_event__process_comm(tool, event, sample, machine) < 0)
766 goto out;
767
768 if (!evsel->attr.sample_id_all) {
769 sample->cpu = 0;
770 sample->time = 0;
771 sample->tid = event->comm.tid;
772 sample->pid = event->comm.pid;
773 }
774 print_sample_start(sample, thread, evsel);
775 perf_event__fprintf(event, stdout);
776 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900777out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300778 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900779 return ret;
780}
781
782static int process_fork_event(struct perf_tool *tool,
783 union perf_event *event,
784 struct perf_sample *sample,
785 struct machine *machine)
786{
787 struct thread *thread;
788 struct perf_script *script = container_of(tool, struct perf_script, tool);
789 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300790 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900791
792 if (perf_event__process_fork(tool, event, sample, machine) < 0)
793 return -1;
794
795 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
796 if (thread == NULL) {
797 pr_debug("problem processing FORK event, skipping it.\n");
798 return -1;
799 }
800
801 if (!evsel->attr.sample_id_all) {
802 sample->cpu = 0;
803 sample->time = event->fork.time;
804 sample->tid = event->fork.tid;
805 sample->pid = event->fork.pid;
806 }
807 print_sample_start(sample, thread, evsel);
808 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300809 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900810
811 return 0;
812}
813static int process_exit_event(struct perf_tool *tool,
814 union perf_event *event,
815 struct perf_sample *sample,
816 struct machine *machine)
817{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300818 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900819 struct thread *thread;
820 struct perf_script *script = container_of(tool, struct perf_script, tool);
821 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300822 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900823
824 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
825 if (thread == NULL) {
826 pr_debug("problem processing EXIT event, skipping it.\n");
827 return -1;
828 }
829
830 if (!evsel->attr.sample_id_all) {
831 sample->cpu = 0;
832 sample->time = 0;
Adrian Hunter53ff6bc32015-08-18 12:07:05 +0300833 sample->tid = event->fork.tid;
834 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900835 }
836 print_sample_start(sample, thread, evsel);
837 perf_event__fprintf(event, stdout);
838
839 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300840 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900841
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300842 thread__put(thread);
843 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900844}
845
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900846static int process_mmap_event(struct perf_tool *tool,
847 union perf_event *event,
848 struct perf_sample *sample,
849 struct machine *machine)
850{
851 struct thread *thread;
852 struct perf_script *script = container_of(tool, struct perf_script, tool);
853 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300854 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900855
856 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
857 return -1;
858
859 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
860 if (thread == NULL) {
861 pr_debug("problem processing MMAP event, skipping it.\n");
862 return -1;
863 }
864
865 if (!evsel->attr.sample_id_all) {
866 sample->cpu = 0;
867 sample->time = 0;
868 sample->tid = event->mmap.tid;
869 sample->pid = event->mmap.pid;
870 }
871 print_sample_start(sample, thread, evsel);
872 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300873 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900874 return 0;
875}
876
877static int process_mmap2_event(struct perf_tool *tool,
878 union perf_event *event,
879 struct perf_sample *sample,
880 struct machine *machine)
881{
882 struct thread *thread;
883 struct perf_script *script = container_of(tool, struct perf_script, tool);
884 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300885 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900886
887 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
888 return -1;
889
890 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
891 if (thread == NULL) {
892 pr_debug("problem processing MMAP2 event, skipping it.\n");
893 return -1;
894 }
895
896 if (!evsel->attr.sample_id_all) {
897 sample->cpu = 0;
898 sample->time = 0;
899 sample->tid = event->mmap2.tid;
900 sample->pid = event->mmap2.pid;
901 }
902 print_sample_start(sample, thread, evsel);
903 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300904 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900905 return 0;
906}
907
Adrian Hunter7c148982015-07-21 12:44:06 +0300908static int process_switch_event(struct perf_tool *tool,
909 union perf_event *event,
910 struct perf_sample *sample,
911 struct machine *machine)
912{
913 struct thread *thread;
914 struct perf_script *script = container_of(tool, struct perf_script, tool);
915 struct perf_session *session = script->session;
916 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
917
918 if (perf_event__process_switch(tool, event, sample, machine) < 0)
919 return -1;
920
921 thread = machine__findnew_thread(machine, sample->pid,
922 sample->tid);
923 if (thread == NULL) {
924 pr_debug("problem processing SWITCH event, skipping it.\n");
925 return -1;
926 }
927
928 print_sample_start(sample, thread, evsel);
929 perf_event__fprintf(event, stdout);
930 thread__put(thread);
931 return 0;
932}
933
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300934static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500935{
936 session_done = 1;
937}
938
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300939static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200940{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200941 int ret;
942
Tom Zanussic239da32010-04-01 23:59:18 -0500943 signal(SIGINT, sig_handler);
944
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900945 /* override event processing functions */
946 if (script->show_task_events) {
947 script->tool.comm = process_comm_event;
948 script->tool.fork = process_fork_event;
949 script->tool.exit = process_exit_event;
950 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900951 if (script->show_mmap_events) {
952 script->tool.mmap = process_mmap_event;
953 script->tool.mmap2 = process_mmap2_event;
954 }
Adrian Hunter7c148982015-07-21 12:44:06 +0300955 if (script->show_switch_events)
956 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900957
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300958 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200959
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200960 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200961 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200962
963 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200964}
965
Tom Zanussi956ffd02009-11-25 01:15:46 -0600966struct script_spec {
967 struct list_head node;
968 struct scripting_ops *ops;
969 char spec[0];
970};
971
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200972static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600973
974static struct script_spec *script_spec__new(const char *spec,
975 struct scripting_ops *ops)
976{
977 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
978
979 if (s != NULL) {
980 strcpy(s->spec, spec);
981 s->ops = ops;
982 }
983
984 return s;
985}
986
Tom Zanussi956ffd02009-11-25 01:15:46 -0600987static void script_spec__add(struct script_spec *s)
988{
989 list_add_tail(&s->node, &script_specs);
990}
991
992static struct script_spec *script_spec__find(const char *spec)
993{
994 struct script_spec *s;
995
996 list_for_each_entry(s, &script_specs, node)
997 if (strcasecmp(s->spec, spec) == 0)
998 return s;
999 return NULL;
1000}
1001
1002static struct script_spec *script_spec__findnew(const char *spec,
1003 struct scripting_ops *ops)
1004{
1005 struct script_spec *s = script_spec__find(spec);
1006
1007 if (s)
1008 return s;
1009
1010 s = script_spec__new(spec, ops);
1011 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +09001012 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001013
1014 script_spec__add(s);
1015
1016 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001017}
1018
1019int script_spec_register(const char *spec, struct scripting_ops *ops)
1020{
1021 struct script_spec *s;
1022
1023 s = script_spec__find(spec);
1024 if (s)
1025 return -1;
1026
1027 s = script_spec__findnew(spec, ops);
1028 if (!s)
1029 return -1;
1030
1031 return 0;
1032}
1033
1034static struct scripting_ops *script_spec__lookup(const char *spec)
1035{
1036 struct script_spec *s = script_spec__find(spec);
1037 if (!s)
1038 return NULL;
1039
1040 return s->ops;
1041}
1042
1043static void list_available_languages(void)
1044{
1045 struct script_spec *s;
1046
1047 fprintf(stderr, "\n");
1048 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001049 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001050
1051 list_for_each_entry(s, &script_specs, node)
1052 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1053
1054 fprintf(stderr, "\n");
1055}
1056
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001057static int parse_scriptname(const struct option *opt __maybe_unused,
1058 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001059{
1060 char spec[PATH_MAX];
1061 const char *script, *ext;
1062 int len;
1063
Tom Zanussif526d682010-01-27 02:27:52 -06001064 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001065 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001066 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001067 }
1068
1069 script = strchr(str, ':');
1070 if (script) {
1071 len = script - str;
1072 if (len >= PATH_MAX) {
1073 fprintf(stderr, "invalid language specifier");
1074 return -1;
1075 }
1076 strncpy(spec, str, len);
1077 spec[len] = '\0';
1078 scripting_ops = script_spec__lookup(spec);
1079 if (!scripting_ops) {
1080 fprintf(stderr, "invalid language specifier");
1081 return -1;
1082 }
1083 script++;
1084 } else {
1085 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001086 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001087 if (!ext) {
1088 fprintf(stderr, "invalid script extension");
1089 return -1;
1090 }
1091 scripting_ops = script_spec__lookup(++ext);
1092 if (!scripting_ops) {
1093 fprintf(stderr, "invalid script extension");
1094 return -1;
1095 }
1096 }
1097
1098 script_name = strdup(script);
1099
1100 return 0;
1101}
1102
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001103static int parse_output_fields(const struct option *opt __maybe_unused,
1104 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001105{
1106 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001107 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001108 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001109 int rc = 0;
1110 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001111 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001112
1113 if (!str)
1114 return -ENOMEM;
1115
David Ahern2c9e45f72011-03-17 10:03:21 -06001116 /* first word can state for which event type the user is specifying
1117 * the fields. If no type exists, the specified fields apply to all
1118 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001119 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001120 tok = strchr(str, ':');
1121 if (tok) {
1122 *tok = '\0';
1123 tok++;
1124 if (!strcmp(str, "hw"))
1125 type = PERF_TYPE_HARDWARE;
1126 else if (!strcmp(str, "sw"))
1127 type = PERF_TYPE_SOFTWARE;
1128 else if (!strcmp(str, "trace"))
1129 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001130 else if (!strcmp(str, "raw"))
1131 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -06001132 else {
1133 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001134 rc = -EINVAL;
1135 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001136 }
1137
1138 if (output[type].user_set)
1139 pr_warning("Overriding previous field request for %s events.\n",
1140 event_type(type));
1141
1142 output[type].fields = 0;
1143 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001144 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001145
1146 } else {
1147 tok = str;
1148 if (strlen(str) == 0) {
1149 fprintf(stderr,
1150 "Cannot set fields to 'none' for all event types.\n");
1151 rc = -EINVAL;
1152 goto out;
1153 }
1154
1155 if (output_set_by_user())
1156 pr_warning("Overriding previous field request for all events.\n");
1157
1158 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1159 output[j].fields = 0;
1160 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001161 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001162 }
David Ahern1424dc92011-03-09 22:23:28 -07001163 }
1164
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001165 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001166 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001167 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001168 break;
David Ahern745f43e2011-03-09 22:23:26 -07001169 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001170 if (i == imax && strcmp(tok, "flags") == 0) {
1171 print_flags = true;
1172 continue;
1173 }
David Ahern745f43e2011-03-09 22:23:26 -07001174 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001175 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001176 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001177 goto out;
1178 }
1179
1180 if (type == -1) {
1181 /* add user option to all events types for
1182 * which it is valid
1183 */
1184 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1185 if (output[j].invalid_fields & all_output_options[i].field) {
1186 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1187 all_output_options[i].str, event_type(j));
1188 } else
1189 output[j].fields |= all_output_options[i].field;
1190 }
1191 } else {
1192 if (output[type].invalid_fields & all_output_options[i].field) {
1193 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1194 all_output_options[i].str, event_type(type));
1195
1196 rc = -EINVAL;
1197 goto out;
1198 }
1199 output[type].fields |= all_output_options[i].field;
1200 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001201 }
1202
1203 if (type >= 0) {
1204 if (output[type].fields == 0) {
1205 pr_debug("No fields requested for %s type. "
1206 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001207 }
David Ahern1424dc92011-03-09 22:23:28 -07001208 }
David Ahern745f43e2011-03-09 22:23:26 -07001209
David Ahern2c9e45f72011-03-17 10:03:21 -06001210out:
David Ahern745f43e2011-03-09 22:23:26 -07001211 free(str);
1212 return rc;
1213}
1214
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001215/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1216static int is_directory(const char *base_path, const struct dirent *dent)
1217{
1218 char path[PATH_MAX];
1219 struct stat st;
1220
1221 sprintf(path, "%s/%s", base_path, dent->d_name);
1222 if (stat(path, &st))
1223 return 0;
1224
1225 return S_ISDIR(st.st_mode);
1226}
1227
1228#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001229 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1230 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001231 if ((lang_dirent.d_type == DT_DIR || \
1232 (lang_dirent.d_type == DT_UNKNOWN && \
1233 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001234 (strcmp(lang_dirent.d_name, ".")) && \
1235 (strcmp(lang_dirent.d_name, "..")))
1236
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001237#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001238 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1239 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001240 if (script_dirent.d_type != DT_DIR && \
1241 (script_dirent.d_type != DT_UNKNOWN || \
1242 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001243
1244
1245#define RECORD_SUFFIX "-record"
1246#define REPORT_SUFFIX "-report"
1247
1248struct script_desc {
1249 struct list_head node;
1250 char *name;
1251 char *half_liner;
1252 char *args;
1253};
1254
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001255static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001256
1257static struct script_desc *script_desc__new(const char *name)
1258{
1259 struct script_desc *s = zalloc(sizeof(*s));
1260
Tom Zanussib5b87312010-11-10 08:16:51 -06001261 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001262 s->name = strdup(name);
1263
1264 return s;
1265}
1266
1267static void script_desc__delete(struct script_desc *s)
1268{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001269 zfree(&s->name);
1270 zfree(&s->half_liner);
1271 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001272 free(s);
1273}
1274
1275static void script_desc__add(struct script_desc *s)
1276{
1277 list_add_tail(&s->node, &script_descs);
1278}
1279
1280static struct script_desc *script_desc__find(const char *name)
1281{
1282 struct script_desc *s;
1283
1284 list_for_each_entry(s, &script_descs, node)
1285 if (strcasecmp(s->name, name) == 0)
1286 return s;
1287 return NULL;
1288}
1289
1290static struct script_desc *script_desc__findnew(const char *name)
1291{
1292 struct script_desc *s = script_desc__find(name);
1293
1294 if (s)
1295 return s;
1296
1297 s = script_desc__new(name);
1298 if (!s)
1299 goto out_delete_desc;
1300
1301 script_desc__add(s);
1302
1303 return s;
1304
1305out_delete_desc:
1306 script_desc__delete(s);
1307
1308 return NULL;
1309}
1310
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001311static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001312{
1313 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001314 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001315
1316 if (strlen(str) > suffix_len) {
1317 p = str + strlen(str) - suffix_len;
1318 if (!strncmp(p, suffix, suffix_len))
1319 return p;
1320 }
1321
1322 return NULL;
1323}
1324
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001325static int read_script_info(struct script_desc *desc, const char *filename)
1326{
1327 char line[BUFSIZ], *p;
1328 FILE *fp;
1329
1330 fp = fopen(filename, "r");
1331 if (!fp)
1332 return -1;
1333
1334 while (fgets(line, sizeof(line), fp)) {
1335 p = ltrim(line);
1336 if (strlen(p) == 0)
1337 continue;
1338 if (*p != '#')
1339 continue;
1340 p++;
1341 if (strlen(p) && *p == '!')
1342 continue;
1343
1344 p = ltrim(p);
1345 if (strlen(p) && p[strlen(p) - 1] == '\n')
1346 p[strlen(p) - 1] = '\0';
1347
1348 if (!strncmp(p, "description:", strlen("description:"))) {
1349 p += strlen("description:");
1350 desc->half_liner = strdup(ltrim(p));
1351 continue;
1352 }
1353
1354 if (!strncmp(p, "args:", strlen("args:"))) {
1355 p += strlen("args:");
1356 desc->args = strdup(ltrim(p));
1357 continue;
1358 }
1359 }
1360
1361 fclose(fp);
1362
1363 return 0;
1364}
1365
Robert Richter38efb532011-11-25 11:38:40 +01001366static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1367{
1368 char *script_root, *str;
1369
1370 script_root = strdup(script_dirent->d_name);
1371 if (!script_root)
1372 return NULL;
1373
1374 str = (char *)ends_with(script_root, suffix);
1375 if (!str) {
1376 free(script_root);
1377 return NULL;
1378 }
1379
1380 *str = '\0';
1381 return script_root;
1382}
1383
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001384static int list_available_scripts(const struct option *opt __maybe_unused,
1385 const char *s __maybe_unused,
1386 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001387{
1388 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1389 char scripts_path[MAXPATHLEN];
1390 DIR *scripts_dir, *lang_dir;
1391 char script_path[MAXPATHLEN];
1392 char lang_path[MAXPATHLEN];
1393 struct script_desc *desc;
1394 char first_half[BUFSIZ];
1395 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001396
1397 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1398
1399 scripts_dir = opendir(scripts_path);
1400 if (!scripts_dir)
1401 return -1;
1402
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001403 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001404 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1405 lang_dirent.d_name);
1406 lang_dir = opendir(lang_path);
1407 if (!lang_dir)
1408 continue;
1409
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001410 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001411 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1412 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001413 desc = script_desc__findnew(script_root);
1414 snprintf(script_path, MAXPATHLEN, "%s/%s",
1415 lang_path, script_dirent.d_name);
1416 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001417 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001418 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001419 }
1420 }
1421
1422 fprintf(stdout, "List of available trace scripts:\n");
1423 list_for_each_entry(desc, &script_descs, node) {
1424 sprintf(first_half, "%s %s", desc->name,
1425 desc->args ? desc->args : "");
1426 fprintf(stdout, " %-36s %s\n", first_half,
1427 desc->half_liner ? desc->half_liner : "");
1428 }
1429
1430 exit(0);
1431}
1432
Feng Tange5f37052012-09-07 16:42:26 +08001433/*
Feng Tang49e639e2012-10-30 11:56:03 +08001434 * Some scripts specify the required events in their "xxx-record" file,
1435 * this function will check if the events in perf.data match those
1436 * mentioned in the "xxx-record".
1437 *
1438 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1439 * which is covered well now. And new parsing code should be added to
1440 * cover the future complexing formats like event groups etc.
1441 */
1442static int check_ev_match(char *dir_name, char *scriptname,
1443 struct perf_session *session)
1444{
1445 char filename[MAXPATHLEN], evname[128];
1446 char line[BUFSIZ], *p;
1447 struct perf_evsel *pos;
1448 int match, len;
1449 FILE *fp;
1450
1451 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1452
1453 fp = fopen(filename, "r");
1454 if (!fp)
1455 return -1;
1456
1457 while (fgets(line, sizeof(line), fp)) {
1458 p = ltrim(line);
1459 if (*p == '#')
1460 continue;
1461
1462 while (strlen(p)) {
1463 p = strstr(p, "-e");
1464 if (!p)
1465 break;
1466
1467 p += 2;
1468 p = ltrim(p);
1469 len = strcspn(p, " \t");
1470 if (!len)
1471 break;
1472
1473 snprintf(evname, len + 1, "%s", p);
1474
1475 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001476 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001477 if (!strcmp(perf_evsel__name(pos), evname)) {
1478 match = 1;
1479 break;
1480 }
1481 }
1482
1483 if (!match) {
1484 fclose(fp);
1485 return -1;
1486 }
1487 }
1488 }
1489
1490 fclose(fp);
1491 return 0;
1492}
1493
1494/*
Feng Tange5f37052012-09-07 16:42:26 +08001495 * Return -1 if none is found, otherwise the actual scripts number.
1496 *
1497 * Currently the only user of this function is the script browser, which
1498 * will list all statically runnable scripts, select one, execute it and
1499 * show the output in a perf browser.
1500 */
1501int find_scripts(char **scripts_array, char **scripts_path_array)
1502{
1503 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001504 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001505 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001506 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001507 struct perf_data_file file = {
1508 .path = input_name,
1509 .mode = PERF_DATA_MODE_READ,
1510 };
Feng Tange5f37052012-09-07 16:42:26 +08001511 char *temp;
1512 int i = 0;
1513
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001514 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001515 if (!session)
1516 return -1;
1517
Feng Tange5f37052012-09-07 16:42:26 +08001518 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1519
1520 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001521 if (!scripts_dir) {
1522 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001523 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001524 }
Feng Tange5f37052012-09-07 16:42:26 +08001525
1526 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1527 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1528 lang_dirent.d_name);
1529#ifdef NO_LIBPERL
1530 if (strstr(lang_path, "perl"))
1531 continue;
1532#endif
1533#ifdef NO_LIBPYTHON
1534 if (strstr(lang_path, "python"))
1535 continue;
1536#endif
1537
1538 lang_dir = opendir(lang_path);
1539 if (!lang_dir)
1540 continue;
1541
1542 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1543 /* Skip those real time scripts: xxxtop.p[yl] */
1544 if (strstr(script_dirent.d_name, "top."))
1545 continue;
1546 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1547 script_dirent.d_name);
1548 temp = strchr(script_dirent.d_name, '.');
1549 snprintf(scripts_array[i],
1550 (temp - script_dirent.d_name) + 1,
1551 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001552
1553 if (check_ev_match(lang_path,
1554 scripts_array[i], session))
1555 continue;
1556
Feng Tange5f37052012-09-07 16:42:26 +08001557 i++;
1558 }
Feng Tang49e639e2012-10-30 11:56:03 +08001559 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001560 }
1561
Feng Tang49e639e2012-10-30 11:56:03 +08001562 closedir(scripts_dir);
1563 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001564 return i;
1565}
1566
Tom Zanussi38752942009-12-15 02:53:39 -06001567static char *get_script_path(const char *script_root, const char *suffix)
1568{
1569 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1570 char scripts_path[MAXPATHLEN];
1571 char script_path[MAXPATHLEN];
1572 DIR *scripts_dir, *lang_dir;
1573 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001574 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001575
1576 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1577
1578 scripts_dir = opendir(scripts_path);
1579 if (!scripts_dir)
1580 return NULL;
1581
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001582 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001583 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1584 lang_dirent.d_name);
1585 lang_dir = opendir(lang_path);
1586 if (!lang_dir)
1587 continue;
1588
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001589 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001590 __script_root = get_script_root(&script_dirent, suffix);
1591 if (__script_root && !strcmp(script_root, __script_root)) {
1592 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001593 closedir(lang_dir);
1594 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001595 snprintf(script_path, MAXPATHLEN, "%s/%s",
1596 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001597 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001598 }
1599 free(__script_root);
1600 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001601 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001602 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001603 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001604
Robert Richter38efb532011-11-25 11:38:40 +01001605 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001606}
1607
Tom Zanussib5b87312010-11-10 08:16:51 -06001608static bool is_top_script(const char *script_path)
1609{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001610 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001611}
1612
1613static int has_required_arg(char *script_path)
1614{
1615 struct script_desc *desc;
1616 int n_args = 0;
1617 char *p;
1618
1619 desc = script_desc__new(NULL);
1620
1621 if (read_script_info(desc, script_path))
1622 goto out;
1623
1624 if (!desc->args)
1625 goto out;
1626
1627 for (p = desc->args; *p; p++)
1628 if (*p == '<')
1629 n_args++;
1630out:
1631 script_desc__delete(desc);
1632
1633 return n_args;
1634}
1635
David Ahernd54b1a92012-08-26 12:24:46 -06001636static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001637{
1638 char **__argv = malloc(sizeof(const char *) * argc);
1639
David Ahernd54b1a92012-08-26 12:24:46 -06001640 if (!__argv) {
1641 pr_err("malloc failed\n");
1642 return -1;
1643 }
1644
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001645 memcpy(__argv, argv, sizeof(const char *) * argc);
1646 argc = parse_options(argc, (const char **)__argv, record_options,
1647 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1648 free(__argv);
1649
David Ahernd54b1a92012-08-26 12:24:46 -06001650 system_wide = (argc == 0);
1651
1652 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001653}
1654
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001655static void script__setup_sample_type(struct perf_script *script)
1656{
1657 struct perf_session *session = script->session;
1658 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1659
1660 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1661 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1662 (sample_type & PERF_SAMPLE_STACK_USER))
1663 callchain_param.record_mode = CALLCHAIN_DWARF;
1664 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1665 callchain_param.record_mode = CALLCHAIN_LBR;
1666 else
1667 callchain_param.record_mode = CALLCHAIN_FP;
1668 }
1669}
1670
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001671int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001672{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001673 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001674 bool header = false;
1675 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001676 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001677 char *rec_script_path = NULL;
1678 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001679 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001680 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001681 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001682 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001683 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001684 struct perf_script script = {
1685 .tool = {
1686 .sample = process_sample_event,
1687 .mmap = perf_event__process_mmap,
1688 .mmap2 = perf_event__process_mmap2,
1689 .comm = perf_event__process_comm,
1690 .exit = perf_event__process_exit,
1691 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001692 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001693 .tracing_data = perf_event__process_tracing_data,
1694 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001695 .id_index = perf_event__process_id_index,
1696 .auxtrace_info = perf_event__process_auxtrace_info,
1697 .auxtrace = perf_event__process_auxtrace,
1698 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001699 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001700 .ordering_requires_timestamps = true,
1701 },
1702 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001703 struct perf_data_file file = {
1704 .mode = PERF_DATA_MODE_READ,
1705 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001706 const struct option options[] = {
1707 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1708 "dump raw trace in ASCII"),
1709 OPT_INCR('v', "verbose", &verbose,
1710 "be more verbose (show symbol address, etc)"),
1711 OPT_BOOLEAN('L', "Latency", &latency_format,
1712 "show latency attributes (irqs/preemption disabled, etc)"),
1713 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1714 list_available_scripts),
1715 OPT_CALLBACK('s', "script", NULL, "name",
1716 "script file name (lang:script name, script name, or *)",
1717 parse_scriptname),
1718 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1719 "generate perf-script.xx script in specified language"),
1720 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1721 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1722 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001723 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1724 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001725 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1726 "file", "vmlinux pathname"),
1727 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1728 "file", "kallsyms pathname"),
1729 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1730 "When printing symbols do not display call chain"),
1731 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1732 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001733 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001734 "comma separated output fields prepend with 'type:'. "
1735 "Valid types: hw,sw,trace,raw. "
1736 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001737 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001738 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1739 "system-wide collection from all CPUs"),
1740 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1741 "only consider these symbols"),
1742 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1743 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1744 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06001745 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1746 "only consider symbols in these pids"),
1747 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1748 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001749 OPT_BOOLEAN('I', "show-info", &show_full_info,
1750 "display extended information from perf.data file"),
1751 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1752 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001753 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1754 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001755 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1756 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03001757 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1758 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001759 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03001760 OPT_BOOLEAN(0, "ns", &nanosecs,
1761 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001762 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1763 "Instruction Tracing options",
1764 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07001765 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1766 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07001767 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1768 "Enable symbol demangling"),
1769 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1770 "Enable kernel symbol demangling"),
1771
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001772 OPT_END()
1773 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08001774 const char * const script_subcommands[] = { "record", "report", NULL };
1775 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001776 "perf script [<options>]",
1777 "perf script [<options>] record <script> [<record-options>] <command>",
1778 "perf script [<options>] report <script> [script-args]",
1779 "perf script [<options>] <script> [<record-options>] <command>",
1780 "perf script [<options>] <top-script> [script-args]",
1781 NULL
1782 };
Tom Zanussi38752942009-12-15 02:53:39 -06001783
Tom Zanussib5b87312010-11-10 08:16:51 -06001784 setup_scripting();
1785
Yunlong Song40cae2b2015-03-18 21:35:54 +08001786 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001787 PARSE_OPT_STOP_AT_NON_OPTION);
1788
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001789 file.path = input_name;
1790
Tom Zanussib5b87312010-11-10 08:16:51 -06001791 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1792 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1793 if (!rec_script_path)
1794 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001795 }
1796
Tom Zanussib5b87312010-11-10 08:16:51 -06001797 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1798 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1799 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001800 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001801 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001802 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001803 return -1;
1804 }
Tom Zanussi38752942009-12-15 02:53:39 -06001805 }
1806
Adrian Hunter3c5b6452015-09-25 16:15:51 +03001807 if (itrace_synth_opts.callchain &&
1808 itrace_synth_opts.callchain_sz > scripting_max_stack)
1809 scripting_max_stack = itrace_synth_opts.callchain_sz;
1810
Ben Hutchings44e668c2010-10-10 16:10:03 +01001811 /* make sure PERF_EXEC_PATH is set for scripts */
1812 perf_set_argv_exec_path(perf_exec_path());
1813
Tom Zanussib5b87312010-11-10 08:16:51 -06001814 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001815 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001816 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001817 pid_t pid;
1818
Tom Zanussib5b87312010-11-10 08:16:51 -06001819 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1820 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1821
1822 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001823 usage_with_options_msg(script_usage, options,
1824 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001825 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001826 }
1827
Tom Zanussib5b87312010-11-10 08:16:51 -06001828 if (is_top_script(argv[0])) {
1829 rep_args = argc - 1;
1830 } else {
1831 int rec_args;
1832
1833 rep_args = has_required_arg(rep_script_path);
1834 rec_args = (argc - 1) - rep_args;
1835 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001836 usage_with_options_msg(script_usage, options,
1837 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001838 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001839 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06001840 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001841 }
1842
1843 if (pipe(live_pipe) < 0) {
1844 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001845 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001846 }
1847
1848 pid = fork();
1849 if (pid < 0) {
1850 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001851 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001852 }
1853
1854 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001855 j = 0;
1856
Tom Zanussia0cccc22010-04-01 23:59:25 -05001857 dup2(live_pipe[1], 1);
1858 close(live_pipe[0]);
1859
Robert Richter317df652011-11-25 15:05:25 +01001860 if (is_top_script(argv[0])) {
1861 system_wide = true;
1862 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001863 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1864 err = -1;
1865 goto out;
1866 }
Robert Richter317df652011-11-25 15:05:25 +01001867 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001868
1869 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001870 if (!__argv) {
1871 pr_err("malloc failed\n");
1872 err = -ENOMEM;
1873 goto out;
1874 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001875
Tom Zanussib5b87312010-11-10 08:16:51 -06001876 __argv[j++] = "/bin/sh";
1877 __argv[j++] = rec_script_path;
1878 if (system_wide)
1879 __argv[j++] = "-a";
1880 __argv[j++] = "-q";
1881 __argv[j++] = "-o";
1882 __argv[j++] = "-";
1883 for (i = rep_args + 1; i < argc; i++)
1884 __argv[j++] = argv[i];
1885 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001886
1887 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001888 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001889 exit(-1);
1890 }
1891
1892 dup2(live_pipe[0], 0);
1893 close(live_pipe[1]);
1894
Tom Zanussib5b87312010-11-10 08:16:51 -06001895 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001896 if (!__argv) {
1897 pr_err("malloc failed\n");
1898 err = -ENOMEM;
1899 goto out;
1900 }
1901
Tom Zanussib5b87312010-11-10 08:16:51 -06001902 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001903 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001904 __argv[j++] = rep_script_path;
1905 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001906 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001907 __argv[j++] = "-i";
1908 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001909 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001910
1911 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001912 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001913 exit(-1);
1914 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001915
Tom Zanussib5b87312010-11-10 08:16:51 -06001916 if (rec_script_path)
1917 script_path = rec_script_path;
1918 if (rep_script_path)
1919 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001920
Tom Zanussib5b87312010-11-10 08:16:51 -06001921 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001922 j = 0;
1923
Robert Richter317df652011-11-25 15:05:25 +01001924 if (!rec_script_path)
1925 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001926 else if (!system_wide) {
1927 if (have_cmd(argc - 1, &argv[1]) != 0) {
1928 err = -1;
1929 goto out;
1930 }
1931 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001932
1933 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001934 if (!__argv) {
1935 pr_err("malloc failed\n");
1936 err = -ENOMEM;
1937 goto out;
1938 }
1939
Tom Zanussib5b87312010-11-10 08:16:51 -06001940 __argv[j++] = "/bin/sh";
1941 __argv[j++] = script_path;
1942 if (system_wide)
1943 __argv[j++] = "-a";
1944 for (i = 2; i < argc; i++)
1945 __argv[j++] = argv[i];
1946 __argv[j++] = NULL;
1947
1948 execvp("/bin/sh", (char **)__argv);
1949 free(__argv);
1950 exit(-1);
1951 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001952
Tom Zanussicf4fee52010-03-03 01:04:33 -06001953 if (!script_name)
1954 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001955
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001956 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001957 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001958 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001959
Jiri Olsae90debd2013-12-09 11:02:50 +01001960 if (header || header_only) {
1961 perf_session__fprintf_info(session, stdout, show_full_info);
1962 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001963 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01001964 }
1965
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001966 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09001967 goto out_delete;
1968
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001969 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001970 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001971
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001972 session->itrace_synth_opts = &itrace_synth_opts;
1973
Anton Blanchard5d67be92011-07-04 21:57:50 +10001974 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001975 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1976 if (err < 0)
1977 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001978 }
1979
David Ahern1424dc92011-03-09 22:23:28 -07001980 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001981 symbol_conf.use_callchain = true;
1982 else
1983 symbol_conf.use_callchain = false;
1984
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03001985 if (session->tevent.pevent &&
1986 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03001987 machine__resolve_kernel_addr,
1988 &session->machines.host) < 0) {
1989 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
1990 return -1;
1991 }
1992
Tom Zanussi956ffd02009-11-25 01:15:46 -06001993 if (generate_script_lang) {
1994 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001995 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001996
David Ahern2c9e45f72011-03-17 10:03:21 -06001997 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001998 fprintf(stderr,
1999 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002000 err = -EINVAL;
2001 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002002 }
2003
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002004 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002005 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002006 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002007 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002008 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002009 }
2010
2011 err = fstat(input, &perf_stat);
2012 if (err < 0) {
2013 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002014 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002015 }
2016
2017 if (!perf_stat.st_size) {
2018 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002019 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002020 }
2021
2022 scripting_ops = script_spec__lookup(generate_script_lang);
2023 if (!scripting_ops) {
2024 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002025 err = -ENOENT;
2026 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002027 }
2028
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002029 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002030 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002031 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002032 }
2033
2034 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002035 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002036 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002037 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002038 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002039 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002040 }
2041
David Ahern9cbdb702011-04-06 21:54:20 -06002042
2043 err = perf_session__check_output_opt(session);
2044 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002045 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002046
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002047 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002048
Adrian Hunterd445dd22014-08-15 22:08:37 +03002049 flush_scripting();
2050
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002051out_delete:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002052 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002053
2054 if (script_started)
2055 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002056out:
2057 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002058}