blob: 72b5deb4bd7961bc4fdb86c50689253b23660b0e [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
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300646static int default_start_script(const char *script __maybe_unused,
647 int argc __maybe_unused,
648 const char **argv __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600649{
650 return 0;
651}
652
Adrian Hunterd445dd22014-08-15 22:08:37 +0300653static int default_flush_script(void)
654{
655 return 0;
656}
657
Tom Zanussi956ffd02009-11-25 01:15:46 -0600658static int default_stop_script(void)
659{
660 return 0;
661}
662
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300663static int default_generate_script(struct pevent *pevent __maybe_unused,
664 const char *outfile __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600665{
666 return 0;
667}
668
669static struct scripting_ops default_scripting_ops = {
670 .start_script = default_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300671 .flush_script = default_flush_script,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600672 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700673 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600674 .generate_script = default_generate_script,
675};
676
677static struct scripting_ops *scripting_ops;
678
679static void setup_scripting(void)
680{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600681 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600682 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600683
Tom Zanussi956ffd02009-11-25 01:15:46 -0600684 scripting_ops = &default_scripting_ops;
685}
686
Adrian Hunterd445dd22014-08-15 22:08:37 +0300687static int flush_scripting(void)
688{
689 return scripting_ops->flush_script();
690}
691
Tom Zanussi956ffd02009-11-25 01:15:46 -0600692static int cleanup_scripting(void)
693{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100694 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500695
Tom Zanussi956ffd02009-11-25 01:15:46 -0600696 return scripting_ops->stop_script();
697}
698
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300699static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200700 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200701 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300702 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200703 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200704{
David Aherne7984b72011-11-21 10:02:52 -0700705 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200706
David Ahern1424dc92011-03-09 22:23:28 -0700707 if (debug_mode) {
708 if (sample->time < last_timestamp) {
709 pr_err("Samples misordered, previous: %" PRIu64
710 " this: %" PRIu64 "\n", last_timestamp,
711 sample->time);
712 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200713 }
David Ahern1424dc92011-03-09 22:23:28 -0700714 last_timestamp = sample->time;
715 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200716 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000717
Adrian Huntere44baa32013-08-08 14:32:25 +0300718 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700719 pr_err("problem processing %d event, skipping it.\n",
720 event->header.type);
721 return -1;
722 }
723
724 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300725 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700726
Anton Blanchard5d67be92011-07-04 21:57:50 +1000727 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300728 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000729
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300730 scripting_ops->process_event(event, sample, evsel, &al);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300731out_put:
732 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200733 return 0;
734}
735
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300736struct perf_script {
737 struct perf_tool tool;
738 struct perf_session *session;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900739 bool show_task_events;
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900740 bool show_mmap_events;
Adrian Hunter7c148982015-07-21 12:44:06 +0300741 bool show_switch_events;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200742};
743
Adrian Hunter7ea95722013-11-01 15:51:30 +0200744static int process_attr(struct perf_tool *tool, union perf_event *event,
745 struct perf_evlist **pevlist)
746{
747 struct perf_script *scr = container_of(tool, struct perf_script, tool);
748 struct perf_evlist *evlist;
749 struct perf_evsel *evsel, *pos;
750 int err;
751
752 err = perf_event__process_attr(tool, event, pevlist);
753 if (err)
754 return err;
755
756 evlist = *pevlist;
757 evsel = perf_evlist__last(*pevlist);
758
759 if (evsel->attr.type >= PERF_TYPE_MAX)
760 return 0;
761
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300762 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200763 if (pos->attr.type == evsel->attr.type && pos != evsel)
764 return 0;
765 }
766
767 set_print_ip_opts(&evsel->attr);
768
Jiri Olsad2b5a312015-10-16 12:41:25 +0200769 if (evsel->attr.sample_type)
770 err = perf_evsel__check_attr(evsel, scr->session);
771
772 return err;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200773}
774
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900775static int process_comm_event(struct perf_tool *tool,
776 union perf_event *event,
777 struct perf_sample *sample,
778 struct machine *machine)
779{
780 struct thread *thread;
781 struct perf_script *script = container_of(tool, struct perf_script, tool);
782 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300783 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900784 int ret = -1;
785
786 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
787 if (thread == NULL) {
788 pr_debug("problem processing COMM event, skipping it.\n");
789 return -1;
790 }
791
792 if (perf_event__process_comm(tool, event, sample, machine) < 0)
793 goto out;
794
795 if (!evsel->attr.sample_id_all) {
796 sample->cpu = 0;
797 sample->time = 0;
798 sample->tid = event->comm.tid;
799 sample->pid = event->comm.pid;
800 }
801 print_sample_start(sample, thread, evsel);
802 perf_event__fprintf(event, stdout);
803 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900804out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300805 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900806 return ret;
807}
808
809static int process_fork_event(struct perf_tool *tool,
810 union perf_event *event,
811 struct perf_sample *sample,
812 struct machine *machine)
813{
814 struct thread *thread;
815 struct perf_script *script = container_of(tool, struct perf_script, tool);
816 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300817 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900818
819 if (perf_event__process_fork(tool, event, sample, machine) < 0)
820 return -1;
821
822 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
823 if (thread == NULL) {
824 pr_debug("problem processing FORK event, skipping it.\n");
825 return -1;
826 }
827
828 if (!evsel->attr.sample_id_all) {
829 sample->cpu = 0;
830 sample->time = event->fork.time;
831 sample->tid = event->fork.tid;
832 sample->pid = event->fork.pid;
833 }
834 print_sample_start(sample, thread, evsel);
835 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300836 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900837
838 return 0;
839}
840static int process_exit_event(struct perf_tool *tool,
841 union perf_event *event,
842 struct perf_sample *sample,
843 struct machine *machine)
844{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300845 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900846 struct thread *thread;
847 struct perf_script *script = container_of(tool, struct perf_script, tool);
848 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300849 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900850
851 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
852 if (thread == NULL) {
853 pr_debug("problem processing EXIT event, skipping it.\n");
854 return -1;
855 }
856
857 if (!evsel->attr.sample_id_all) {
858 sample->cpu = 0;
859 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +0300860 sample->tid = event->fork.tid;
861 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900862 }
863 print_sample_start(sample, thread, evsel);
864 perf_event__fprintf(event, stdout);
865
866 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300867 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900868
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300869 thread__put(thread);
870 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900871}
872
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900873static int process_mmap_event(struct perf_tool *tool,
874 union perf_event *event,
875 struct perf_sample *sample,
876 struct machine *machine)
877{
878 struct thread *thread;
879 struct perf_script *script = container_of(tool, struct perf_script, tool);
880 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300881 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900882
883 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
884 return -1;
885
886 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
887 if (thread == NULL) {
888 pr_debug("problem processing MMAP event, skipping it.\n");
889 return -1;
890 }
891
892 if (!evsel->attr.sample_id_all) {
893 sample->cpu = 0;
894 sample->time = 0;
895 sample->tid = event->mmap.tid;
896 sample->pid = event->mmap.pid;
897 }
898 print_sample_start(sample, thread, evsel);
899 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300900 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900901 return 0;
902}
903
904static int process_mmap2_event(struct perf_tool *tool,
905 union perf_event *event,
906 struct perf_sample *sample,
907 struct machine *machine)
908{
909 struct thread *thread;
910 struct perf_script *script = container_of(tool, struct perf_script, tool);
911 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300912 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900913
914 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
915 return -1;
916
917 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
918 if (thread == NULL) {
919 pr_debug("problem processing MMAP2 event, skipping it.\n");
920 return -1;
921 }
922
923 if (!evsel->attr.sample_id_all) {
924 sample->cpu = 0;
925 sample->time = 0;
926 sample->tid = event->mmap2.tid;
927 sample->pid = event->mmap2.pid;
928 }
929 print_sample_start(sample, thread, evsel);
930 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300931 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900932 return 0;
933}
934
Adrian Hunter7c148982015-07-21 12:44:06 +0300935static int process_switch_event(struct perf_tool *tool,
936 union perf_event *event,
937 struct perf_sample *sample,
938 struct machine *machine)
939{
940 struct thread *thread;
941 struct perf_script *script = container_of(tool, struct perf_script, tool);
942 struct perf_session *session = script->session;
943 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
944
945 if (perf_event__process_switch(tool, event, sample, machine) < 0)
946 return -1;
947
948 thread = machine__findnew_thread(machine, sample->pid,
949 sample->tid);
950 if (thread == NULL) {
951 pr_debug("problem processing SWITCH event, skipping it.\n");
952 return -1;
953 }
954
955 print_sample_start(sample, thread, evsel);
956 perf_event__fprintf(event, stdout);
957 thread__put(thread);
958 return 0;
959}
960
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300961static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500962{
963 session_done = 1;
964}
965
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300966static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200967{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200968 int ret;
969
Tom Zanussic239da32010-04-01 23:59:18 -0500970 signal(SIGINT, sig_handler);
971
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900972 /* override event processing functions */
973 if (script->show_task_events) {
974 script->tool.comm = process_comm_event;
975 script->tool.fork = process_fork_event;
976 script->tool.exit = process_exit_event;
977 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900978 if (script->show_mmap_events) {
979 script->tool.mmap = process_mmap_event;
980 script->tool.mmap2 = process_mmap2_event;
981 }
Adrian Hunter7c148982015-07-21 12:44:06 +0300982 if (script->show_switch_events)
983 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900984
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300985 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200986
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200987 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200988 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200989
990 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200991}
992
Tom Zanussi956ffd02009-11-25 01:15:46 -0600993struct script_spec {
994 struct list_head node;
995 struct scripting_ops *ops;
996 char spec[0];
997};
998
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200999static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001000
1001static struct script_spec *script_spec__new(const char *spec,
1002 struct scripting_ops *ops)
1003{
1004 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
1005
1006 if (s != NULL) {
1007 strcpy(s->spec, spec);
1008 s->ops = ops;
1009 }
1010
1011 return s;
1012}
1013
Tom Zanussi956ffd02009-11-25 01:15:46 -06001014static void script_spec__add(struct script_spec *s)
1015{
1016 list_add_tail(&s->node, &script_specs);
1017}
1018
1019static struct script_spec *script_spec__find(const char *spec)
1020{
1021 struct script_spec *s;
1022
1023 list_for_each_entry(s, &script_specs, node)
1024 if (strcasecmp(s->spec, spec) == 0)
1025 return s;
1026 return NULL;
1027}
1028
1029static struct script_spec *script_spec__findnew(const char *spec,
1030 struct scripting_ops *ops)
1031{
1032 struct script_spec *s = script_spec__find(spec);
1033
1034 if (s)
1035 return s;
1036
1037 s = script_spec__new(spec, ops);
1038 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +09001039 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001040
1041 script_spec__add(s);
1042
1043 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001044}
1045
1046int script_spec_register(const char *spec, struct scripting_ops *ops)
1047{
1048 struct script_spec *s;
1049
1050 s = script_spec__find(spec);
1051 if (s)
1052 return -1;
1053
1054 s = script_spec__findnew(spec, ops);
1055 if (!s)
1056 return -1;
1057
1058 return 0;
1059}
1060
1061static struct scripting_ops *script_spec__lookup(const char *spec)
1062{
1063 struct script_spec *s = script_spec__find(spec);
1064 if (!s)
1065 return NULL;
1066
1067 return s->ops;
1068}
1069
1070static void list_available_languages(void)
1071{
1072 struct script_spec *s;
1073
1074 fprintf(stderr, "\n");
1075 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001076 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001077
1078 list_for_each_entry(s, &script_specs, node)
1079 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
1080
1081 fprintf(stderr, "\n");
1082}
1083
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001084static int parse_scriptname(const struct option *opt __maybe_unused,
1085 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -06001086{
1087 char spec[PATH_MAX];
1088 const char *script, *ext;
1089 int len;
1090
Tom Zanussif526d682010-01-27 02:27:52 -06001091 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001092 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001093 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001094 }
1095
1096 script = strchr(str, ':');
1097 if (script) {
1098 len = script - str;
1099 if (len >= PATH_MAX) {
1100 fprintf(stderr, "invalid language specifier");
1101 return -1;
1102 }
1103 strncpy(spec, str, len);
1104 spec[len] = '\0';
1105 scripting_ops = script_spec__lookup(spec);
1106 if (!scripting_ops) {
1107 fprintf(stderr, "invalid language specifier");
1108 return -1;
1109 }
1110 script++;
1111 } else {
1112 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001113 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001114 if (!ext) {
1115 fprintf(stderr, "invalid script extension");
1116 return -1;
1117 }
1118 scripting_ops = script_spec__lookup(++ext);
1119 if (!scripting_ops) {
1120 fprintf(stderr, "invalid script extension");
1121 return -1;
1122 }
1123 }
1124
1125 script_name = strdup(script);
1126
1127 return 0;
1128}
1129
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001130static int parse_output_fields(const struct option *opt __maybe_unused,
1131 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001132{
1133 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001134 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001135 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001136 int rc = 0;
1137 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001138 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001139
1140 if (!str)
1141 return -ENOMEM;
1142
David Ahern2c9e45f72011-03-17 10:03:21 -06001143 /* first word can state for which event type the user is specifying
1144 * the fields. If no type exists, the specified fields apply to all
1145 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001146 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001147 tok = strchr(str, ':');
1148 if (tok) {
1149 *tok = '\0';
1150 tok++;
1151 if (!strcmp(str, "hw"))
1152 type = PERF_TYPE_HARDWARE;
1153 else if (!strcmp(str, "sw"))
1154 type = PERF_TYPE_SOFTWARE;
1155 else if (!strcmp(str, "trace"))
1156 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001157 else if (!strcmp(str, "raw"))
1158 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -06001159 else {
1160 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001161 rc = -EINVAL;
1162 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001163 }
1164
1165 if (output[type].user_set)
1166 pr_warning("Overriding previous field request for %s events.\n",
1167 event_type(type));
1168
1169 output[type].fields = 0;
1170 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001171 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001172
1173 } else {
1174 tok = str;
1175 if (strlen(str) == 0) {
1176 fprintf(stderr,
1177 "Cannot set fields to 'none' for all event types.\n");
1178 rc = -EINVAL;
1179 goto out;
1180 }
1181
1182 if (output_set_by_user())
1183 pr_warning("Overriding previous field request for all events.\n");
1184
1185 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1186 output[j].fields = 0;
1187 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001188 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001189 }
David Ahern1424dc92011-03-09 22:23:28 -07001190 }
1191
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001192 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001193 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001194 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001195 break;
David Ahern745f43e2011-03-09 22:23:26 -07001196 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001197 if (i == imax && strcmp(tok, "flags") == 0) {
1198 print_flags = true;
1199 continue;
1200 }
David Ahern745f43e2011-03-09 22:23:26 -07001201 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001202 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001203 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001204 goto out;
1205 }
1206
1207 if (type == -1) {
1208 /* add user option to all events types for
1209 * which it is valid
1210 */
1211 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1212 if (output[j].invalid_fields & all_output_options[i].field) {
1213 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1214 all_output_options[i].str, event_type(j));
1215 } else
1216 output[j].fields |= all_output_options[i].field;
1217 }
1218 } else {
1219 if (output[type].invalid_fields & all_output_options[i].field) {
1220 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1221 all_output_options[i].str, event_type(type));
1222
1223 rc = -EINVAL;
1224 goto out;
1225 }
1226 output[type].fields |= all_output_options[i].field;
1227 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001228 }
1229
1230 if (type >= 0) {
1231 if (output[type].fields == 0) {
1232 pr_debug("No fields requested for %s type. "
1233 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001234 }
David Ahern1424dc92011-03-09 22:23:28 -07001235 }
David Ahern745f43e2011-03-09 22:23:26 -07001236
David Ahern2c9e45f72011-03-17 10:03:21 -06001237out:
David Ahern745f43e2011-03-09 22:23:26 -07001238 free(str);
1239 return rc;
1240}
1241
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001242/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1243static int is_directory(const char *base_path, const struct dirent *dent)
1244{
1245 char path[PATH_MAX];
1246 struct stat st;
1247
1248 sprintf(path, "%s/%s", base_path, dent->d_name);
1249 if (stat(path, &st))
1250 return 0;
1251
1252 return S_ISDIR(st.st_mode);
1253}
1254
1255#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001256 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1257 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001258 if ((lang_dirent.d_type == DT_DIR || \
1259 (lang_dirent.d_type == DT_UNKNOWN && \
1260 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001261 (strcmp(lang_dirent.d_name, ".")) && \
1262 (strcmp(lang_dirent.d_name, "..")))
1263
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001264#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001265 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1266 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001267 if (script_dirent.d_type != DT_DIR && \
1268 (script_dirent.d_type != DT_UNKNOWN || \
1269 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001270
1271
1272#define RECORD_SUFFIX "-record"
1273#define REPORT_SUFFIX "-report"
1274
1275struct script_desc {
1276 struct list_head node;
1277 char *name;
1278 char *half_liner;
1279 char *args;
1280};
1281
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001282static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001283
1284static struct script_desc *script_desc__new(const char *name)
1285{
1286 struct script_desc *s = zalloc(sizeof(*s));
1287
Tom Zanussib5b87312010-11-10 08:16:51 -06001288 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001289 s->name = strdup(name);
1290
1291 return s;
1292}
1293
1294static void script_desc__delete(struct script_desc *s)
1295{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001296 zfree(&s->name);
1297 zfree(&s->half_liner);
1298 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001299 free(s);
1300}
1301
1302static void script_desc__add(struct script_desc *s)
1303{
1304 list_add_tail(&s->node, &script_descs);
1305}
1306
1307static struct script_desc *script_desc__find(const char *name)
1308{
1309 struct script_desc *s;
1310
1311 list_for_each_entry(s, &script_descs, node)
1312 if (strcasecmp(s->name, name) == 0)
1313 return s;
1314 return NULL;
1315}
1316
1317static struct script_desc *script_desc__findnew(const char *name)
1318{
1319 struct script_desc *s = script_desc__find(name);
1320
1321 if (s)
1322 return s;
1323
1324 s = script_desc__new(name);
1325 if (!s)
1326 goto out_delete_desc;
1327
1328 script_desc__add(s);
1329
1330 return s;
1331
1332out_delete_desc:
1333 script_desc__delete(s);
1334
1335 return NULL;
1336}
1337
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001338static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001339{
1340 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001341 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001342
1343 if (strlen(str) > suffix_len) {
1344 p = str + strlen(str) - suffix_len;
1345 if (!strncmp(p, suffix, suffix_len))
1346 return p;
1347 }
1348
1349 return NULL;
1350}
1351
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001352static int read_script_info(struct script_desc *desc, const char *filename)
1353{
1354 char line[BUFSIZ], *p;
1355 FILE *fp;
1356
1357 fp = fopen(filename, "r");
1358 if (!fp)
1359 return -1;
1360
1361 while (fgets(line, sizeof(line), fp)) {
1362 p = ltrim(line);
1363 if (strlen(p) == 0)
1364 continue;
1365 if (*p != '#')
1366 continue;
1367 p++;
1368 if (strlen(p) && *p == '!')
1369 continue;
1370
1371 p = ltrim(p);
1372 if (strlen(p) && p[strlen(p) - 1] == '\n')
1373 p[strlen(p) - 1] = '\0';
1374
1375 if (!strncmp(p, "description:", strlen("description:"))) {
1376 p += strlen("description:");
1377 desc->half_liner = strdup(ltrim(p));
1378 continue;
1379 }
1380
1381 if (!strncmp(p, "args:", strlen("args:"))) {
1382 p += strlen("args:");
1383 desc->args = strdup(ltrim(p));
1384 continue;
1385 }
1386 }
1387
1388 fclose(fp);
1389
1390 return 0;
1391}
1392
Robert Richter38efb532011-11-25 11:38:40 +01001393static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1394{
1395 char *script_root, *str;
1396
1397 script_root = strdup(script_dirent->d_name);
1398 if (!script_root)
1399 return NULL;
1400
1401 str = (char *)ends_with(script_root, suffix);
1402 if (!str) {
1403 free(script_root);
1404 return NULL;
1405 }
1406
1407 *str = '\0';
1408 return script_root;
1409}
1410
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001411static int list_available_scripts(const struct option *opt __maybe_unused,
1412 const char *s __maybe_unused,
1413 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001414{
1415 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1416 char scripts_path[MAXPATHLEN];
1417 DIR *scripts_dir, *lang_dir;
1418 char script_path[MAXPATHLEN];
1419 char lang_path[MAXPATHLEN];
1420 struct script_desc *desc;
1421 char first_half[BUFSIZ];
1422 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001423
1424 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1425
1426 scripts_dir = opendir(scripts_path);
1427 if (!scripts_dir)
1428 return -1;
1429
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001430 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001431 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1432 lang_dirent.d_name);
1433 lang_dir = opendir(lang_path);
1434 if (!lang_dir)
1435 continue;
1436
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001437 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001438 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1439 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001440 desc = script_desc__findnew(script_root);
1441 snprintf(script_path, MAXPATHLEN, "%s/%s",
1442 lang_path, script_dirent.d_name);
1443 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001444 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001445 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001446 }
1447 }
1448
1449 fprintf(stdout, "List of available trace scripts:\n");
1450 list_for_each_entry(desc, &script_descs, node) {
1451 sprintf(first_half, "%s %s", desc->name,
1452 desc->args ? desc->args : "");
1453 fprintf(stdout, " %-36s %s\n", first_half,
1454 desc->half_liner ? desc->half_liner : "");
1455 }
1456
1457 exit(0);
1458}
1459
Feng Tange5f37052012-09-07 16:42:26 +08001460/*
Feng Tang49e639e2012-10-30 11:56:03 +08001461 * Some scripts specify the required events in their "xxx-record" file,
1462 * this function will check if the events in perf.data match those
1463 * mentioned in the "xxx-record".
1464 *
1465 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1466 * which is covered well now. And new parsing code should be added to
1467 * cover the future complexing formats like event groups etc.
1468 */
1469static int check_ev_match(char *dir_name, char *scriptname,
1470 struct perf_session *session)
1471{
1472 char filename[MAXPATHLEN], evname[128];
1473 char line[BUFSIZ], *p;
1474 struct perf_evsel *pos;
1475 int match, len;
1476 FILE *fp;
1477
1478 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1479
1480 fp = fopen(filename, "r");
1481 if (!fp)
1482 return -1;
1483
1484 while (fgets(line, sizeof(line), fp)) {
1485 p = ltrim(line);
1486 if (*p == '#')
1487 continue;
1488
1489 while (strlen(p)) {
1490 p = strstr(p, "-e");
1491 if (!p)
1492 break;
1493
1494 p += 2;
1495 p = ltrim(p);
1496 len = strcspn(p, " \t");
1497 if (!len)
1498 break;
1499
1500 snprintf(evname, len + 1, "%s", p);
1501
1502 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001503 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001504 if (!strcmp(perf_evsel__name(pos), evname)) {
1505 match = 1;
1506 break;
1507 }
1508 }
1509
1510 if (!match) {
1511 fclose(fp);
1512 return -1;
1513 }
1514 }
1515 }
1516
1517 fclose(fp);
1518 return 0;
1519}
1520
1521/*
Feng Tange5f37052012-09-07 16:42:26 +08001522 * Return -1 if none is found, otherwise the actual scripts number.
1523 *
1524 * Currently the only user of this function is the script browser, which
1525 * will list all statically runnable scripts, select one, execute it and
1526 * show the output in a perf browser.
1527 */
1528int find_scripts(char **scripts_array, char **scripts_path_array)
1529{
1530 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001531 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001532 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001533 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001534 struct perf_data_file file = {
1535 .path = input_name,
1536 .mode = PERF_DATA_MODE_READ,
1537 };
Feng Tange5f37052012-09-07 16:42:26 +08001538 char *temp;
1539 int i = 0;
1540
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001541 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001542 if (!session)
1543 return -1;
1544
Feng Tange5f37052012-09-07 16:42:26 +08001545 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1546
1547 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001548 if (!scripts_dir) {
1549 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001550 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001551 }
Feng Tange5f37052012-09-07 16:42:26 +08001552
1553 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1554 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1555 lang_dirent.d_name);
1556#ifdef NO_LIBPERL
1557 if (strstr(lang_path, "perl"))
1558 continue;
1559#endif
1560#ifdef NO_LIBPYTHON
1561 if (strstr(lang_path, "python"))
1562 continue;
1563#endif
1564
1565 lang_dir = opendir(lang_path);
1566 if (!lang_dir)
1567 continue;
1568
1569 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1570 /* Skip those real time scripts: xxxtop.p[yl] */
1571 if (strstr(script_dirent.d_name, "top."))
1572 continue;
1573 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1574 script_dirent.d_name);
1575 temp = strchr(script_dirent.d_name, '.');
1576 snprintf(scripts_array[i],
1577 (temp - script_dirent.d_name) + 1,
1578 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001579
1580 if (check_ev_match(lang_path,
1581 scripts_array[i], session))
1582 continue;
1583
Feng Tange5f37052012-09-07 16:42:26 +08001584 i++;
1585 }
Feng Tang49e639e2012-10-30 11:56:03 +08001586 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001587 }
1588
Feng Tang49e639e2012-10-30 11:56:03 +08001589 closedir(scripts_dir);
1590 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001591 return i;
1592}
1593
Tom Zanussi38752942009-12-15 02:53:39 -06001594static char *get_script_path(const char *script_root, const char *suffix)
1595{
1596 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1597 char scripts_path[MAXPATHLEN];
1598 char script_path[MAXPATHLEN];
1599 DIR *scripts_dir, *lang_dir;
1600 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001601 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001602
1603 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1604
1605 scripts_dir = opendir(scripts_path);
1606 if (!scripts_dir)
1607 return NULL;
1608
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001609 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001610 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1611 lang_dirent.d_name);
1612 lang_dir = opendir(lang_path);
1613 if (!lang_dir)
1614 continue;
1615
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001616 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001617 __script_root = get_script_root(&script_dirent, suffix);
1618 if (__script_root && !strcmp(script_root, __script_root)) {
1619 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001620 closedir(lang_dir);
1621 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001622 snprintf(script_path, MAXPATHLEN, "%s/%s",
1623 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001624 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001625 }
1626 free(__script_root);
1627 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001628 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001629 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001630 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001631
Robert Richter38efb532011-11-25 11:38:40 +01001632 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001633}
1634
Tom Zanussib5b87312010-11-10 08:16:51 -06001635static bool is_top_script(const char *script_path)
1636{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001637 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001638}
1639
1640static int has_required_arg(char *script_path)
1641{
1642 struct script_desc *desc;
1643 int n_args = 0;
1644 char *p;
1645
1646 desc = script_desc__new(NULL);
1647
1648 if (read_script_info(desc, script_path))
1649 goto out;
1650
1651 if (!desc->args)
1652 goto out;
1653
1654 for (p = desc->args; *p; p++)
1655 if (*p == '<')
1656 n_args++;
1657out:
1658 script_desc__delete(desc);
1659
1660 return n_args;
1661}
1662
David Ahernd54b1a92012-08-26 12:24:46 -06001663static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001664{
1665 char **__argv = malloc(sizeof(const char *) * argc);
1666
David Ahernd54b1a92012-08-26 12:24:46 -06001667 if (!__argv) {
1668 pr_err("malloc failed\n");
1669 return -1;
1670 }
1671
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001672 memcpy(__argv, argv, sizeof(const char *) * argc);
1673 argc = parse_options(argc, (const char **)__argv, record_options,
1674 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1675 free(__argv);
1676
David Ahernd54b1a92012-08-26 12:24:46 -06001677 system_wide = (argc == 0);
1678
1679 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001680}
1681
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001682static void script__setup_sample_type(struct perf_script *script)
1683{
1684 struct perf_session *session = script->session;
1685 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1686
1687 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1688 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1689 (sample_type & PERF_SAMPLE_STACK_USER))
1690 callchain_param.record_mode = CALLCHAIN_DWARF;
1691 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1692 callchain_param.record_mode = CALLCHAIN_LBR;
1693 else
1694 callchain_param.record_mode = CALLCHAIN_FP;
1695 }
1696}
1697
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001698int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001699{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001700 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001701 bool header = false;
1702 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001703 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001704 char *rec_script_path = NULL;
1705 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001706 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001707 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001708 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001709 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001710 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001711 struct perf_script script = {
1712 .tool = {
1713 .sample = process_sample_event,
1714 .mmap = perf_event__process_mmap,
1715 .mmap2 = perf_event__process_mmap2,
1716 .comm = perf_event__process_comm,
1717 .exit = perf_event__process_exit,
1718 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001719 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001720 .tracing_data = perf_event__process_tracing_data,
1721 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001722 .id_index = perf_event__process_id_index,
1723 .auxtrace_info = perf_event__process_auxtrace_info,
1724 .auxtrace = perf_event__process_auxtrace,
1725 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001726 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001727 .ordering_requires_timestamps = true,
1728 },
1729 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001730 struct perf_data_file file = {
1731 .mode = PERF_DATA_MODE_READ,
1732 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001733 const struct option options[] = {
1734 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1735 "dump raw trace in ASCII"),
1736 OPT_INCR('v', "verbose", &verbose,
1737 "be more verbose (show symbol address, etc)"),
1738 OPT_BOOLEAN('L', "Latency", &latency_format,
1739 "show latency attributes (irqs/preemption disabled, etc)"),
1740 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1741 list_available_scripts),
1742 OPT_CALLBACK('s', "script", NULL, "name",
1743 "script file name (lang:script name, script name, or *)",
1744 parse_scriptname),
1745 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1746 "generate perf-script.xx script in specified language"),
1747 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1748 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1749 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001750 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1751 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001752 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1753 "file", "vmlinux pathname"),
1754 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1755 "file", "kallsyms pathname"),
1756 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1757 "When printing symbols do not display call chain"),
1758 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1759 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001760 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001761 "comma separated output fields prepend with 'type:'. "
1762 "Valid types: hw,sw,trace,raw. "
1763 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Stephane Eraniandc323ce2015-08-31 18:41:13 +02001764 "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001765 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1766 "system-wide collection from all CPUs"),
1767 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1768 "only consider these symbols"),
1769 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1770 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1771 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06001772 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1773 "only consider symbols in these pids"),
1774 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1775 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001776 OPT_BOOLEAN('I', "show-info", &show_full_info,
1777 "display extended information from perf.data file"),
1778 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1779 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001780 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1781 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001782 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1783 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03001784 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1785 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001786 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter83e19862015-09-25 16:15:36 +03001787 OPT_BOOLEAN(0, "ns", &nanosecs,
1788 "Use 9 decimal places when displaying time"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001789 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1790 "Instruction Tracing options",
1791 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07001792 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1793 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07001794 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1795 "Enable symbol demangling"),
1796 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1797 "Enable kernel symbol demangling"),
1798
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001799 OPT_END()
1800 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08001801 const char * const script_subcommands[] = { "record", "report", NULL };
1802 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001803 "perf script [<options>]",
1804 "perf script [<options>] record <script> [<record-options>] <command>",
1805 "perf script [<options>] report <script> [script-args]",
1806 "perf script [<options>] <script> [<record-options>] <command>",
1807 "perf script [<options>] <top-script> [script-args]",
1808 NULL
1809 };
Tom Zanussi38752942009-12-15 02:53:39 -06001810
Tom Zanussib5b87312010-11-10 08:16:51 -06001811 setup_scripting();
1812
Yunlong Song40cae2b2015-03-18 21:35:54 +08001813 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001814 PARSE_OPT_STOP_AT_NON_OPTION);
1815
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001816 file.path = input_name;
1817
Tom Zanussib5b87312010-11-10 08:16:51 -06001818 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1819 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1820 if (!rec_script_path)
1821 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001822 }
1823
Tom Zanussib5b87312010-11-10 08:16:51 -06001824 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1825 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1826 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001827 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001828 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001829 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001830 return -1;
1831 }
Tom Zanussi38752942009-12-15 02:53:39 -06001832 }
1833
Adrian Hunter3c5b6452015-09-25 16:15:51 +03001834 if (itrace_synth_opts.callchain &&
1835 itrace_synth_opts.callchain_sz > scripting_max_stack)
1836 scripting_max_stack = itrace_synth_opts.callchain_sz;
1837
Ben Hutchings44e668c2010-10-10 16:10:03 +01001838 /* make sure PERF_EXEC_PATH is set for scripts */
1839 perf_set_argv_exec_path(perf_exec_path());
1840
Tom Zanussib5b87312010-11-10 08:16:51 -06001841 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001842 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001843 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001844 pid_t pid;
1845
Tom Zanussib5b87312010-11-10 08:16:51 -06001846 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1847 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1848
1849 if (!rec_script_path && !rep_script_path) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001850 usage_with_options_msg(script_usage, options,
1851 "Couldn't find script `%s'\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001852 " script -l for available scripts.\n", argv[0]);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001853 }
1854
Tom Zanussib5b87312010-11-10 08:16:51 -06001855 if (is_top_script(argv[0])) {
1856 rep_args = argc - 1;
1857 } else {
1858 int rec_args;
1859
1860 rep_args = has_required_arg(rep_script_path);
1861 rec_args = (argc - 1) - rep_args;
1862 if (rec_args < 0) {
Namhyung Kimc7118362015-10-25 00:49:27 +09001863 usage_with_options_msg(script_usage, options,
1864 "`%s' script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001865 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001866 "scripts and options.\n", argv[0]);
Tom Zanussib5b87312010-11-10 08:16:51 -06001867 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001868 }
1869
1870 if (pipe(live_pipe) < 0) {
1871 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001872 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001873 }
1874
1875 pid = fork();
1876 if (pid < 0) {
1877 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001878 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001879 }
1880
1881 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001882 j = 0;
1883
Tom Zanussia0cccc22010-04-01 23:59:25 -05001884 dup2(live_pipe[1], 1);
1885 close(live_pipe[0]);
1886
Robert Richter317df652011-11-25 15:05:25 +01001887 if (is_top_script(argv[0])) {
1888 system_wide = true;
1889 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001890 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1891 err = -1;
1892 goto out;
1893 }
Robert Richter317df652011-11-25 15:05:25 +01001894 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001895
1896 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001897 if (!__argv) {
1898 pr_err("malloc failed\n");
1899 err = -ENOMEM;
1900 goto out;
1901 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001902
Tom Zanussib5b87312010-11-10 08:16:51 -06001903 __argv[j++] = "/bin/sh";
1904 __argv[j++] = rec_script_path;
1905 if (system_wide)
1906 __argv[j++] = "-a";
1907 __argv[j++] = "-q";
1908 __argv[j++] = "-o";
1909 __argv[j++] = "-";
1910 for (i = rep_args + 1; i < argc; i++)
1911 __argv[j++] = argv[i];
1912 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001913
1914 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001915 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001916 exit(-1);
1917 }
1918
1919 dup2(live_pipe[0], 0);
1920 close(live_pipe[1]);
1921
Tom Zanussib5b87312010-11-10 08:16:51 -06001922 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001923 if (!__argv) {
1924 pr_err("malloc failed\n");
1925 err = -ENOMEM;
1926 goto out;
1927 }
1928
Tom Zanussib5b87312010-11-10 08:16:51 -06001929 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001930 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001931 __argv[j++] = rep_script_path;
1932 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001933 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001934 __argv[j++] = "-i";
1935 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001936 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001937
1938 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001939 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001940 exit(-1);
1941 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001942
Tom Zanussib5b87312010-11-10 08:16:51 -06001943 if (rec_script_path)
1944 script_path = rec_script_path;
1945 if (rep_script_path)
1946 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001947
Tom Zanussib5b87312010-11-10 08:16:51 -06001948 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001949 j = 0;
1950
Robert Richter317df652011-11-25 15:05:25 +01001951 if (!rec_script_path)
1952 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001953 else if (!system_wide) {
1954 if (have_cmd(argc - 1, &argv[1]) != 0) {
1955 err = -1;
1956 goto out;
1957 }
1958 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001959
1960 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001961 if (!__argv) {
1962 pr_err("malloc failed\n");
1963 err = -ENOMEM;
1964 goto out;
1965 }
1966
Tom Zanussib5b87312010-11-10 08:16:51 -06001967 __argv[j++] = "/bin/sh";
1968 __argv[j++] = script_path;
1969 if (system_wide)
1970 __argv[j++] = "-a";
1971 for (i = 2; i < argc; i++)
1972 __argv[j++] = argv[i];
1973 __argv[j++] = NULL;
1974
1975 execvp("/bin/sh", (char **)__argv);
1976 free(__argv);
1977 exit(-1);
1978 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001979
Tom Zanussicf4fee52010-03-03 01:04:33 -06001980 if (!script_name)
1981 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001982
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001983 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001984 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001985 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001986
Jiri Olsae90debd2013-12-09 11:02:50 +01001987 if (header || header_only) {
1988 perf_session__fprintf_info(session, stdout, show_full_info);
1989 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001990 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01001991 }
1992
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001993 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09001994 goto out_delete;
1995
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001996 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001997 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001998
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001999 session->itrace_synth_opts = &itrace_synth_opts;
2000
Anton Blanchard5d67be92011-07-04 21:57:50 +10002001 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002002 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
2003 if (err < 0)
2004 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10002005 }
2006
David Ahern1424dc92011-03-09 22:23:28 -07002007 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07002008 symbol_conf.use_callchain = true;
2009 else
2010 symbol_conf.use_callchain = false;
2011
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03002012 if (session->tevent.pevent &&
2013 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03002014 machine__resolve_kernel_addr,
2015 &session->machines.host) < 0) {
2016 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
2017 return -1;
2018 }
2019
Tom Zanussi956ffd02009-11-25 01:15:46 -06002020 if (generate_script_lang) {
2021 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07002022 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002023
David Ahern2c9e45f72011-03-17 10:03:21 -06002024 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07002025 fprintf(stderr,
2026 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002027 err = -EINVAL;
2028 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07002029 }
2030
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002031 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06002032 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002033 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002034 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002035 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002036 }
2037
2038 err = fstat(input, &perf_stat);
2039 if (err < 0) {
2040 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002041 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002042 }
2043
2044 if (!perf_stat.st_size) {
2045 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002046 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002047 }
2048
2049 scripting_ops = script_spec__lookup(generate_script_lang);
2050 if (!scripting_ops) {
2051 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002052 err = -ENOENT;
2053 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002054 }
2055
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002056 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002057 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002058 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002059 }
2060
2061 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06002062 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002063 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002064 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002065 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002066 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06002067 }
2068
David Ahern9cbdb702011-04-06 21:54:20 -06002069
2070 err = perf_session__check_output_opt(session);
2071 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002072 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06002073
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03002074 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06002075
Adrian Hunterd445dd22014-08-15 22:08:37 +03002076 flush_scripting();
2077
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002078out_delete:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02002079 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09002080
2081 if (script_started)
2082 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06002083out:
2084 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02002085}