blob: 284a76e046284983cd769da89cb45604b114a41a [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;
Anton Blanchard5d67be92011-07-04 21:57:50 +100032static const char *cpu_list;
33static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Tom Zanussi956ffd02009-11-25 01:15:46 -060034
David Ahern745f43e2011-03-09 22:23:26 -070035enum perf_output_field {
36 PERF_OUTPUT_COMM = 1U << 0,
37 PERF_OUTPUT_TID = 1U << 1,
38 PERF_OUTPUT_PID = 1U << 2,
39 PERF_OUTPUT_TIME = 1U << 3,
40 PERF_OUTPUT_CPU = 1U << 4,
41 PERF_OUTPUT_EVNAME = 1U << 5,
42 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060043 PERF_OUTPUT_IP = 1U << 7,
44 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060045 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060046 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090047 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020048 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaa2014-08-25 16:45:42 +020049 PERF_OUTPUT_PERIOD = 1U << 13,
Stephane Eranianfc36f942015-08-31 18:41:10 +020050 PERF_OUTPUT_IREGS = 1U << 14,
David Ahern745f43e2011-03-09 22:23:26 -070051};
52
53struct output_option {
54 const char *str;
55 enum perf_output_field field;
56} all_output_options[] = {
57 {.str = "comm", .field = PERF_OUTPUT_COMM},
58 {.str = "tid", .field = PERF_OUTPUT_TID},
59 {.str = "pid", .field = PERF_OUTPUT_PID},
60 {.str = "time", .field = PERF_OUTPUT_TIME},
61 {.str = "cpu", .field = PERF_OUTPUT_CPU},
62 {.str = "event", .field = PERF_OUTPUT_EVNAME},
63 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060064 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070065 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060066 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060067 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090068 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020069 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaa2014-08-25 16:45:42 +020070 {.str = "period", .field = PERF_OUTPUT_PERIOD},
Stephane Eranianfc36f942015-08-31 18:41:10 +020071 {.str = "iregs", .field = PERF_OUTPUT_IREGS},
David Ahern745f43e2011-03-09 22:23:26 -070072};
73
74/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060075static struct {
76 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060077 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040078 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060079 u64 fields;
80 u64 invalid_fields;
81} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070082
David Ahern2c9e45f72011-03-17 10:03:21 -060083 [PERF_TYPE_HARDWARE] = {
84 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070085
David Ahern2c9e45f72011-03-17 10:03:21 -060086 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
87 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060088 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +020089 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
90 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -060091
92 .invalid_fields = PERF_OUTPUT_TRACE,
93 },
94
95 [PERF_TYPE_SOFTWARE] = {
96 .user_set = false,
97
98 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
99 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600100 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200101 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
102 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600103
104 .invalid_fields = PERF_OUTPUT_TRACE,
105 },
106
107 [PERF_TYPE_TRACEPOINT] = {
108 .user_set = false,
109
110 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
111 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
112 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
113 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700114
115 [PERF_TYPE_RAW] = {
116 .user_set = false,
117
118 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
119 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600120 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200121 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
122 PERF_OUTPUT_PERIOD,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700123
124 .invalid_fields = PERF_OUTPUT_TRACE,
125 },
David Ahern1424dc92011-03-09 22:23:28 -0700126};
David Ahern745f43e2011-03-09 22:23:26 -0700127
David Ahern2c9e45f72011-03-17 10:03:21 -0600128static bool output_set_by_user(void)
129{
130 int j;
131 for (j = 0; j < PERF_TYPE_MAX; ++j) {
132 if (output[j].user_set)
133 return true;
134 }
135 return false;
136}
David Ahern745f43e2011-03-09 22:23:26 -0700137
David Ahern9cbdb702011-04-06 21:54:20 -0600138static const char *output_field2str(enum perf_output_field field)
139{
140 int i, imax = ARRAY_SIZE(all_output_options);
141 const char *str = "";
142
143 for (i = 0; i < imax; ++i) {
144 if (all_output_options[i].field == field) {
145 str = all_output_options[i].str;
146 break;
147 }
148 }
149 return str;
150}
151
David Ahern2c9e45f72011-03-17 10:03:21 -0600152#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700153
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300154static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
155 u64 sample_type, const char *sample_msg,
156 enum perf_output_field field,
157 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700158{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300159 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600160 int type = attr->type;
161 const char *evname;
162
163 if (attr->sample_type & sample_type)
164 return 0;
165
166 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300167 if (allow_user_set)
168 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300169 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600170 pr_err("Samples for '%s' event do not have %s attribute set. "
171 "Cannot print '%s' field.\n",
172 evname, sample_msg, output_field2str(field));
173 return -1;
174 }
175
176 /* user did not ask for it explicitly so remove from the default list */
177 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300178 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600179 pr_debug("Samples for '%s' event do not have %s attribute set. "
180 "Skipping '%s' field.\n",
181 evname, sample_msg, output_field2str(field));
182
183 return 0;
184}
185
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300186static int perf_evsel__check_stype(struct perf_evsel *evsel,
187 u64 sample_type, const char *sample_msg,
188 enum perf_output_field field)
189{
190 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
191 false);
192}
193
David Ahern9cbdb702011-04-06 21:54:20 -0600194static int perf_evsel__check_attr(struct perf_evsel *evsel,
195 struct perf_session *session)
196{
197 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300198 bool allow_user_set;
199
200 allow_user_set = perf_header__has_feat(&session->header,
201 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600202
David Ahern1424dc92011-03-09 22:23:28 -0700203 if (PRINT_FIELD(TRACE) &&
204 !perf_session__has_traces(session, "record -R"))
205 return -EINVAL;
206
David Ahern787bef12011-05-27 14:28:43 -0600207 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300208 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
209 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700210 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700211 }
David Ahern7cec0922011-05-30 13:08:23 -0600212
213 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300214 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
215 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600216 return -EINVAL;
217
218 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
219 pr_err("Display of symbols requested but neither sample IP nor "
220 "sample address\nis selected. Hence, no addresses to convert "
221 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600222 return -EINVAL;
223 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900224 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
225 pr_err("Display of offsets requested but symbol is not"
226 "selected.\n");
227 return -EINVAL;
228 }
David Ahern7cec0922011-05-30 13:08:23 -0600229 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
230 pr_err("Display of DSO requested but neither sample IP nor "
231 "sample address\nis selected. Hence, no addresses to convert "
232 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600233 return -EINVAL;
234 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200235 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
236 pr_err("Display of source line number requested but sample IP is not\n"
237 "selected. Hence, no address to lookup the source line number.\n");
238 return -EINVAL;
239 }
David Ahern1424dc92011-03-09 22:23:28 -0700240
241 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300242 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
243 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700244 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700245
246 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300247 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
248 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700249 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700250
251 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300252 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
253 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700254 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600255
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200256 if (PRINT_FIELD(PERIOD) &&
257 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
258 PERF_OUTPUT_PERIOD))
259 return -EINVAL;
260
Stephane Eranianfc36f942015-08-31 18:41:10 +0200261 if (PRINT_FIELD(IREGS) &&
262 perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
263 PERF_OUTPUT_IREGS))
264 return -EINVAL;
265
David Ahern9cbdb702011-04-06 21:54:20 -0600266 return 0;
267}
268
Adrian Hunter7ea95722013-11-01 15:51:30 +0200269static void set_print_ip_opts(struct perf_event_attr *attr)
270{
271 unsigned int type = attr->type;
272
273 output[type].print_ip_opts = 0;
274 if (PRINT_FIELD(IP))
275 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
276
277 if (PRINT_FIELD(SYM))
278 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
279
280 if (PRINT_FIELD(DSO))
281 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
282
283 if (PRINT_FIELD(SYMOFFSET))
284 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200285
286 if (PRINT_FIELD(SRCLINE))
287 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200288}
289
David Ahern9cbdb702011-04-06 21:54:20 -0600290/*
291 * verify all user requested events exist and the samples
292 * have the expected data
293 */
294static int perf_session__check_output_opt(struct perf_session *session)
295{
296 int j;
297 struct perf_evsel *evsel;
298
299 for (j = 0; j < PERF_TYPE_MAX; ++j) {
300 evsel = perf_session__find_first_evtype(session, j);
301
302 /*
303 * even if fields is set to 0 (ie., show nothing) event must
304 * exist if user explicitly includes it on the command line
305 */
306 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
307 pr_err("%s events do not exist. "
308 "Remove corresponding -f option to proceed.\n",
309 event_type(j));
310 return -1;
311 }
312
313 if (evsel && output[j].fields &&
314 perf_evsel__check_attr(evsel, session))
315 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400316
317 if (evsel == NULL)
318 continue;
319
Adrian Hunter7ea95722013-11-01 15:51:30 +0200320 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700321 }
322
Adrian Hunter98526ee2014-07-31 09:00:59 +0300323 if (!no_callchain) {
324 bool use_callchain = false;
325
326 evlist__for_each(session->evlist, evsel) {
327 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
328 use_callchain = true;
329 break;
330 }
331 }
332 if (!use_callchain)
333 symbol_conf.use_callchain = false;
334 }
335
David Ahern80b8b492013-11-19 21:07:37 -0700336 /*
337 * set default for tracepoints to print symbols only
338 * if callchains are present
339 */
340 if (symbol_conf.use_callchain &&
341 !output[PERF_TYPE_TRACEPOINT].user_set) {
342 struct perf_event_attr *attr;
343
344 j = PERF_TYPE_TRACEPOINT;
345 evsel = perf_session__find_first_evtype(session, j);
346 if (evsel == NULL)
347 goto out;
348
349 attr = &evsel->attr;
350
351 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
352 output[j].fields |= PERF_OUTPUT_IP;
353 output[j].fields |= PERF_OUTPUT_SYM;
354 output[j].fields |= PERF_OUTPUT_DSO;
355 set_print_ip_opts(attr);
356 }
357 }
358
359out:
David Ahern1424dc92011-03-09 22:23:28 -0700360 return 0;
361}
David Ahern745f43e2011-03-09 22:23:26 -0700362
Stephane Eranianfc36f942015-08-31 18:41:10 +0200363static void print_sample_iregs(union perf_event *event __maybe_unused,
364 struct perf_sample *sample,
365 struct thread *thread __maybe_unused,
366 struct perf_event_attr *attr)
367{
368 struct regs_dump *regs = &sample->intr_regs;
369 uint64_t mask = attr->sample_regs_intr;
370 unsigned i = 0, r;
371
372 if (!regs)
373 return;
374
375 for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
376 u64 val = regs->regs[i++];
377 printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
378 }
379}
380
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300381static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700382 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300383 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700384{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300385 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700386 unsigned long secs;
387 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700388 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700389
David Ahern745f43e2011-03-09 22:23:26 -0700390 if (PRINT_FIELD(COMM)) {
391 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200392 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600393 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200394 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700395 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200396 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700397 }
David Ahernc70c94b2011-03-09 22:23:25 -0700398
David Ahern745f43e2011-03-09 22:23:26 -0700399 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
400 printf("%5d/%-5d ", sample->pid, sample->tid);
401 else if (PRINT_FIELD(PID))
402 printf("%5d ", sample->pid);
403 else if (PRINT_FIELD(TID))
404 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700405
David Ahern745f43e2011-03-09 22:23:26 -0700406 if (PRINT_FIELD(CPU)) {
407 if (latency_format)
408 printf("%3d ", sample->cpu);
409 else
410 printf("[%03d] ", sample->cpu);
411 }
David Ahernc70c94b2011-03-09 22:23:25 -0700412
David Ahern745f43e2011-03-09 22:23:26 -0700413 if (PRINT_FIELD(TIME)) {
414 nsecs = sample->time;
415 secs = nsecs / NSECS_PER_SEC;
416 nsecs -= secs * NSECS_PER_SEC;
417 usecs = nsecs / NSECS_PER_USEC;
418 printf("%5lu.%06lu: ", secs, usecs);
419 }
David Ahernc70c94b2011-03-09 22:23:25 -0700420}
421
David Ahern7cec0922011-05-30 13:08:23 -0600422static void print_sample_addr(union perf_event *event,
423 struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600424 struct thread *thread,
425 struct perf_event_attr *attr)
426{
427 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600428
429 printf("%16" PRIx64, sample->addr);
430
431 if (!sample_addr_correlates_sym(attr))
432 return;
433
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300434 perf_event__preprocess_sample_addr(event, sample, thread, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600435
436 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900437 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900438 if (PRINT_FIELD(SYMOFFSET))
439 symbol__fprintf_symname_offs(al.sym, &al, stdout);
440 else
441 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600442 }
443
444 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900445 printf(" (");
446 map__fprintf_dsoname(al.map, stdout);
447 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600448 }
449}
450
Akihiro Nagai95582592012-01-30 13:43:09 +0900451static void print_sample_bts(union perf_event *event,
452 struct perf_sample *sample,
453 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200454 struct thread *thread,
455 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900456{
457 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300458 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900459
460 /* print branch_from information */
461 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300462 unsigned int print_opts = output[attr->type].print_ip_opts;
463
464 if (symbol_conf.use_callchain && sample->callchain) {
Akihiro Nagai95582592012-01-30 13:43:09 +0900465 printf("\n");
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300466 } else {
467 printf(" ");
468 if (print_opts & PRINT_IP_OPT_SRCLINE) {
469 print_srcline_last = true;
470 print_opts &= ~PRINT_IP_OPT_SRCLINE;
471 }
472 }
473 perf_evsel__print_ip(evsel, sample, al, print_opts,
David Ahern307cbb92013-08-07 22:50:53 -0400474 PERF_MAX_STACK_DEPTH);
Akihiro Nagai95582592012-01-30 13:43:09 +0900475 }
476
Akihiro Nagai95582592012-01-30 13:43:09 +0900477 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300478 if (PRINT_FIELD(ADDR) ||
479 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300480 !output[attr->type].user_set)) {
481 printf(" => ");
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300482 print_sample_addr(event, sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300483 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900484
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300485 if (print_srcline_last)
486 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
487
Akihiro Nagai95582592012-01-30 13:43:09 +0900488 printf("\n");
489}
490
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300491static void print_sample_flags(u32 flags)
492{
493 const char *chars = PERF_IP_FLAG_CHARS;
494 const int n = strlen(PERF_IP_FLAG_CHARS);
495 char str[33];
496 int i, pos = 0;
497
498 for (i = 0; i < n; i++, flags >>= 1) {
499 if (flags & 1)
500 str[pos++] = chars[i];
501 }
502 for (; i < 32; i++, flags >>= 1) {
503 if (flags & 1)
504 str[pos++] = '?';
505 }
506 str[pos] = 0;
507 printf(" %-4s ", str);
508}
509
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300510static void process_event(union perf_event *event, struct perf_sample *sample,
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300511 struct perf_evsel *evsel, struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700512{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300513 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300514 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700515
David Ahern2c9e45f72011-03-17 10:03:21 -0600516 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700517 return;
518
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300519 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700520
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200521 if (PRINT_FIELD(PERIOD))
522 printf("%10" PRIu64 " ", sample->period);
523
Namhyung Kime944d3d2013-11-18 14:34:52 +0900524 if (PRINT_FIELD(EVNAME)) {
525 const char *evname = perf_evsel__name(evsel);
526 printf("%s: ", evname ? evname : "[unknown]");
527 }
528
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300529 if (print_flags)
530 print_sample_flags(sample->flags);
531
Akihiro Nagai95582592012-01-30 13:43:09 +0900532 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300533 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900534 return;
535 }
536
David Ahern745f43e2011-03-09 22:23:26 -0700537 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300538 event_format__print(evsel->tp_format, sample->cpu,
539 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600540 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300541 print_sample_addr(event, sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600542
David Ahern787bef12011-05-27 14:28:43 -0600543 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700544 if (!symbol_conf.use_callchain)
545 printf(" ");
546 else
547 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400548
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300549 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400550 output[attr->type].print_ip_opts,
551 PERF_MAX_STACK_DEPTH);
David Ahernc0230b22011-03-09 22:23:27 -0700552 }
553
Stephane Eranianfc36f942015-08-31 18:41:10 +0200554 if (PRINT_FIELD(IREGS))
555 print_sample_iregs(event, sample, thread, attr);
556
David Ahernc70c94b2011-03-09 22:23:25 -0700557 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700558}
559
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300560static int default_start_script(const char *script __maybe_unused,
561 int argc __maybe_unused,
562 const char **argv __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600563{
564 return 0;
565}
566
Adrian Hunterd445dd22014-08-15 22:08:37 +0300567static int default_flush_script(void)
568{
569 return 0;
570}
571
Tom Zanussi956ffd02009-11-25 01:15:46 -0600572static int default_stop_script(void)
573{
574 return 0;
575}
576
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300577static int default_generate_script(struct pevent *pevent __maybe_unused,
578 const char *outfile __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600579{
580 return 0;
581}
582
583static struct scripting_ops default_scripting_ops = {
584 .start_script = default_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300585 .flush_script = default_flush_script,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600586 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700587 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600588 .generate_script = default_generate_script,
589};
590
591static struct scripting_ops *scripting_ops;
592
593static void setup_scripting(void)
594{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600595 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600596 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600597
Tom Zanussi956ffd02009-11-25 01:15:46 -0600598 scripting_ops = &default_scripting_ops;
599}
600
Adrian Hunterd445dd22014-08-15 22:08:37 +0300601static int flush_scripting(void)
602{
603 return scripting_ops->flush_script();
604}
605
Tom Zanussi956ffd02009-11-25 01:15:46 -0600606static int cleanup_scripting(void)
607{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100608 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500609
Tom Zanussi956ffd02009-11-25 01:15:46 -0600610 return scripting_ops->stop_script();
611}
612
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300613static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200614 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200615 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300616 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200617 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200618{
David Aherne7984b72011-11-21 10:02:52 -0700619 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200620
David Ahern1424dc92011-03-09 22:23:28 -0700621 if (debug_mode) {
622 if (sample->time < last_timestamp) {
623 pr_err("Samples misordered, previous: %" PRIu64
624 " this: %" PRIu64 "\n", last_timestamp,
625 sample->time);
626 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200627 }
David Ahern1424dc92011-03-09 22:23:28 -0700628 last_timestamp = sample->time;
629 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200630 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000631
Adrian Huntere44baa32013-08-08 14:32:25 +0300632 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700633 pr_err("problem processing %d event, skipping it.\n",
634 event->header.type);
635 return -1;
636 }
637
638 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300639 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700640
Anton Blanchard5d67be92011-07-04 21:57:50 +1000641 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300642 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000643
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300644 scripting_ops->process_event(event, sample, evsel, &al);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300645out_put:
646 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200647 return 0;
648}
649
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300650struct perf_script {
651 struct perf_tool tool;
652 struct perf_session *session;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900653 bool show_task_events;
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900654 bool show_mmap_events;
Adrian Hunter7c148982015-07-21 12:44:06 +0300655 bool show_switch_events;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200656};
657
Adrian Hunter7ea95722013-11-01 15:51:30 +0200658static int process_attr(struct perf_tool *tool, union perf_event *event,
659 struct perf_evlist **pevlist)
660{
661 struct perf_script *scr = container_of(tool, struct perf_script, tool);
662 struct perf_evlist *evlist;
663 struct perf_evsel *evsel, *pos;
664 int err;
665
666 err = perf_event__process_attr(tool, event, pevlist);
667 if (err)
668 return err;
669
670 evlist = *pevlist;
671 evsel = perf_evlist__last(*pevlist);
672
673 if (evsel->attr.type >= PERF_TYPE_MAX)
674 return 0;
675
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300676 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200677 if (pos->attr.type == evsel->attr.type && pos != evsel)
678 return 0;
679 }
680
681 set_print_ip_opts(&evsel->attr);
682
683 return perf_evsel__check_attr(evsel, scr->session);
684}
685
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900686static int process_comm_event(struct perf_tool *tool,
687 union perf_event *event,
688 struct perf_sample *sample,
689 struct machine *machine)
690{
691 struct thread *thread;
692 struct perf_script *script = container_of(tool, struct perf_script, tool);
693 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300694 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900695 int ret = -1;
696
697 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
698 if (thread == NULL) {
699 pr_debug("problem processing COMM event, skipping it.\n");
700 return -1;
701 }
702
703 if (perf_event__process_comm(tool, event, sample, machine) < 0)
704 goto out;
705
706 if (!evsel->attr.sample_id_all) {
707 sample->cpu = 0;
708 sample->time = 0;
709 sample->tid = event->comm.tid;
710 sample->pid = event->comm.pid;
711 }
712 print_sample_start(sample, thread, evsel);
713 perf_event__fprintf(event, stdout);
714 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900715out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300716 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900717 return ret;
718}
719
720static int process_fork_event(struct perf_tool *tool,
721 union perf_event *event,
722 struct perf_sample *sample,
723 struct machine *machine)
724{
725 struct thread *thread;
726 struct perf_script *script = container_of(tool, struct perf_script, tool);
727 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300728 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900729
730 if (perf_event__process_fork(tool, event, sample, machine) < 0)
731 return -1;
732
733 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
734 if (thread == NULL) {
735 pr_debug("problem processing FORK event, skipping it.\n");
736 return -1;
737 }
738
739 if (!evsel->attr.sample_id_all) {
740 sample->cpu = 0;
741 sample->time = event->fork.time;
742 sample->tid = event->fork.tid;
743 sample->pid = event->fork.pid;
744 }
745 print_sample_start(sample, thread, evsel);
746 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300747 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900748
749 return 0;
750}
751static int process_exit_event(struct perf_tool *tool,
752 union perf_event *event,
753 struct perf_sample *sample,
754 struct machine *machine)
755{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300756 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900757 struct thread *thread;
758 struct perf_script *script = container_of(tool, struct perf_script, tool);
759 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300760 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900761
762 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
763 if (thread == NULL) {
764 pr_debug("problem processing EXIT event, skipping it.\n");
765 return -1;
766 }
767
768 if (!evsel->attr.sample_id_all) {
769 sample->cpu = 0;
770 sample->time = 0;
Adrian Hunter53ff6bc2015-08-18 12:07:05 +0300771 sample->tid = event->fork.tid;
772 sample->pid = event->fork.pid;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900773 }
774 print_sample_start(sample, thread, evsel);
775 perf_event__fprintf(event, stdout);
776
777 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300778 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900779
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300780 thread__put(thread);
781 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900782}
783
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900784static int process_mmap_event(struct perf_tool *tool,
785 union perf_event *event,
786 struct perf_sample *sample,
787 struct machine *machine)
788{
789 struct thread *thread;
790 struct perf_script *script = container_of(tool, struct perf_script, tool);
791 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300792 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900793
794 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
795 return -1;
796
797 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
798 if (thread == NULL) {
799 pr_debug("problem processing MMAP event, skipping it.\n");
800 return -1;
801 }
802
803 if (!evsel->attr.sample_id_all) {
804 sample->cpu = 0;
805 sample->time = 0;
806 sample->tid = event->mmap.tid;
807 sample->pid = event->mmap.pid;
808 }
809 print_sample_start(sample, thread, evsel);
810 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300811 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900812 return 0;
813}
814
815static int process_mmap2_event(struct perf_tool *tool,
816 union perf_event *event,
817 struct perf_sample *sample,
818 struct machine *machine)
819{
820 struct thread *thread;
821 struct perf_script *script = container_of(tool, struct perf_script, tool);
822 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300823 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900824
825 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
826 return -1;
827
828 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
829 if (thread == NULL) {
830 pr_debug("problem processing MMAP2 event, skipping it.\n");
831 return -1;
832 }
833
834 if (!evsel->attr.sample_id_all) {
835 sample->cpu = 0;
836 sample->time = 0;
837 sample->tid = event->mmap2.tid;
838 sample->pid = event->mmap2.pid;
839 }
840 print_sample_start(sample, thread, evsel);
841 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300842 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900843 return 0;
844}
845
Adrian Hunter7c148982015-07-21 12:44:06 +0300846static int process_switch_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;
854 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
855
856 if (perf_event__process_switch(tool, event, sample, machine) < 0)
857 return -1;
858
859 thread = machine__findnew_thread(machine, sample->pid,
860 sample->tid);
861 if (thread == NULL) {
862 pr_debug("problem processing SWITCH event, skipping it.\n");
863 return -1;
864 }
865
866 print_sample_start(sample, thread, evsel);
867 perf_event__fprintf(event, stdout);
868 thread__put(thread);
869 return 0;
870}
871
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300872static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500873{
874 session_done = 1;
875}
876
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300877static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200878{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200879 int ret;
880
Tom Zanussic239da32010-04-01 23:59:18 -0500881 signal(SIGINT, sig_handler);
882
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900883 /* override event processing functions */
884 if (script->show_task_events) {
885 script->tool.comm = process_comm_event;
886 script->tool.fork = process_fork_event;
887 script->tool.exit = process_exit_event;
888 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900889 if (script->show_mmap_events) {
890 script->tool.mmap = process_mmap_event;
891 script->tool.mmap2 = process_mmap2_event;
892 }
Adrian Hunter7c148982015-07-21 12:44:06 +0300893 if (script->show_switch_events)
894 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900895
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300896 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200897
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200898 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200899 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200900
901 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200902}
903
Tom Zanussi956ffd02009-11-25 01:15:46 -0600904struct script_spec {
905 struct list_head node;
906 struct scripting_ops *ops;
907 char spec[0];
908};
909
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200910static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600911
912static struct script_spec *script_spec__new(const char *spec,
913 struct scripting_ops *ops)
914{
915 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
916
917 if (s != NULL) {
918 strcpy(s->spec, spec);
919 s->ops = ops;
920 }
921
922 return s;
923}
924
Tom Zanussi956ffd02009-11-25 01:15:46 -0600925static void script_spec__add(struct script_spec *s)
926{
927 list_add_tail(&s->node, &script_specs);
928}
929
930static struct script_spec *script_spec__find(const char *spec)
931{
932 struct script_spec *s;
933
934 list_for_each_entry(s, &script_specs, node)
935 if (strcasecmp(s->spec, spec) == 0)
936 return s;
937 return NULL;
938}
939
940static struct script_spec *script_spec__findnew(const char *spec,
941 struct scripting_ops *ops)
942{
943 struct script_spec *s = script_spec__find(spec);
944
945 if (s)
946 return s;
947
948 s = script_spec__new(spec, ops);
949 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +0900950 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600951
952 script_spec__add(s);
953
954 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600955}
956
957int script_spec_register(const char *spec, struct scripting_ops *ops)
958{
959 struct script_spec *s;
960
961 s = script_spec__find(spec);
962 if (s)
963 return -1;
964
965 s = script_spec__findnew(spec, ops);
966 if (!s)
967 return -1;
968
969 return 0;
970}
971
972static struct scripting_ops *script_spec__lookup(const char *spec)
973{
974 struct script_spec *s = script_spec__find(spec);
975 if (!s)
976 return NULL;
977
978 return s->ops;
979}
980
981static void list_available_languages(void)
982{
983 struct script_spec *s;
984
985 fprintf(stderr, "\n");
986 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100987 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600988
989 list_for_each_entry(s, &script_specs, node)
990 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
991
992 fprintf(stderr, "\n");
993}
994
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300995static int parse_scriptname(const struct option *opt __maybe_unused,
996 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600997{
998 char spec[PATH_MAX];
999 const char *script, *ext;
1000 int len;
1001
Tom Zanussif526d682010-01-27 02:27:52 -06001002 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -06001003 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -06001004 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001005 }
1006
1007 script = strchr(str, ':');
1008 if (script) {
1009 len = script - str;
1010 if (len >= PATH_MAX) {
1011 fprintf(stderr, "invalid language specifier");
1012 return -1;
1013 }
1014 strncpy(spec, str, len);
1015 spec[len] = '\0';
1016 scripting_ops = script_spec__lookup(spec);
1017 if (!scripting_ops) {
1018 fprintf(stderr, "invalid language specifier");
1019 return -1;
1020 }
1021 script++;
1022 } else {
1023 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +01001024 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -06001025 if (!ext) {
1026 fprintf(stderr, "invalid script extension");
1027 return -1;
1028 }
1029 scripting_ops = script_spec__lookup(++ext);
1030 if (!scripting_ops) {
1031 fprintf(stderr, "invalid script extension");
1032 return -1;
1033 }
1034 }
1035
1036 script_name = strdup(script);
1037
1038 return 0;
1039}
1040
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001041static int parse_output_fields(const struct option *opt __maybe_unused,
1042 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001043{
1044 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001045 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001046 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001047 int rc = 0;
1048 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001049 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001050
1051 if (!str)
1052 return -ENOMEM;
1053
David Ahern2c9e45f72011-03-17 10:03:21 -06001054 /* first word can state for which event type the user is specifying
1055 * the fields. If no type exists, the specified fields apply to all
1056 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001057 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001058 tok = strchr(str, ':');
1059 if (tok) {
1060 *tok = '\0';
1061 tok++;
1062 if (!strcmp(str, "hw"))
1063 type = PERF_TYPE_HARDWARE;
1064 else if (!strcmp(str, "sw"))
1065 type = PERF_TYPE_SOFTWARE;
1066 else if (!strcmp(str, "trace"))
1067 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001068 else if (!strcmp(str, "raw"))
1069 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -06001070 else {
1071 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001072 rc = -EINVAL;
1073 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001074 }
1075
1076 if (output[type].user_set)
1077 pr_warning("Overriding previous field request for %s events.\n",
1078 event_type(type));
1079
1080 output[type].fields = 0;
1081 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001082 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001083
1084 } else {
1085 tok = str;
1086 if (strlen(str) == 0) {
1087 fprintf(stderr,
1088 "Cannot set fields to 'none' for all event types.\n");
1089 rc = -EINVAL;
1090 goto out;
1091 }
1092
1093 if (output_set_by_user())
1094 pr_warning("Overriding previous field request for all events.\n");
1095
1096 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1097 output[j].fields = 0;
1098 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001099 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001100 }
David Ahern1424dc92011-03-09 22:23:28 -07001101 }
1102
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001103 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001104 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001105 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001106 break;
David Ahern745f43e2011-03-09 22:23:26 -07001107 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001108 if (i == imax && strcmp(tok, "flags") == 0) {
1109 print_flags = true;
1110 continue;
1111 }
David Ahern745f43e2011-03-09 22:23:26 -07001112 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001113 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001114 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001115 goto out;
1116 }
1117
1118 if (type == -1) {
1119 /* add user option to all events types for
1120 * which it is valid
1121 */
1122 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1123 if (output[j].invalid_fields & all_output_options[i].field) {
1124 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1125 all_output_options[i].str, event_type(j));
1126 } else
1127 output[j].fields |= all_output_options[i].field;
1128 }
1129 } else {
1130 if (output[type].invalid_fields & all_output_options[i].field) {
1131 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1132 all_output_options[i].str, event_type(type));
1133
1134 rc = -EINVAL;
1135 goto out;
1136 }
1137 output[type].fields |= all_output_options[i].field;
1138 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001139 }
1140
1141 if (type >= 0) {
1142 if (output[type].fields == 0) {
1143 pr_debug("No fields requested for %s type. "
1144 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001145 }
David Ahern1424dc92011-03-09 22:23:28 -07001146 }
David Ahern745f43e2011-03-09 22:23:26 -07001147
David Ahern2c9e45f72011-03-17 10:03:21 -06001148out:
David Ahern745f43e2011-03-09 22:23:26 -07001149 free(str);
1150 return rc;
1151}
1152
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001153/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1154static int is_directory(const char *base_path, const struct dirent *dent)
1155{
1156 char path[PATH_MAX];
1157 struct stat st;
1158
1159 sprintf(path, "%s/%s", base_path, dent->d_name);
1160 if (stat(path, &st))
1161 return 0;
1162
1163 return S_ISDIR(st.st_mode);
1164}
1165
1166#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001167 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1168 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001169 if ((lang_dirent.d_type == DT_DIR || \
1170 (lang_dirent.d_type == DT_UNKNOWN && \
1171 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001172 (strcmp(lang_dirent.d_name, ".")) && \
1173 (strcmp(lang_dirent.d_name, "..")))
1174
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001175#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001176 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1177 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001178 if (script_dirent.d_type != DT_DIR && \
1179 (script_dirent.d_type != DT_UNKNOWN || \
1180 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001181
1182
1183#define RECORD_SUFFIX "-record"
1184#define REPORT_SUFFIX "-report"
1185
1186struct script_desc {
1187 struct list_head node;
1188 char *name;
1189 char *half_liner;
1190 char *args;
1191};
1192
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001193static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001194
1195static struct script_desc *script_desc__new(const char *name)
1196{
1197 struct script_desc *s = zalloc(sizeof(*s));
1198
Tom Zanussib5b87312010-11-10 08:16:51 -06001199 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001200 s->name = strdup(name);
1201
1202 return s;
1203}
1204
1205static void script_desc__delete(struct script_desc *s)
1206{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001207 zfree(&s->name);
1208 zfree(&s->half_liner);
1209 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001210 free(s);
1211}
1212
1213static void script_desc__add(struct script_desc *s)
1214{
1215 list_add_tail(&s->node, &script_descs);
1216}
1217
1218static struct script_desc *script_desc__find(const char *name)
1219{
1220 struct script_desc *s;
1221
1222 list_for_each_entry(s, &script_descs, node)
1223 if (strcasecmp(s->name, name) == 0)
1224 return s;
1225 return NULL;
1226}
1227
1228static struct script_desc *script_desc__findnew(const char *name)
1229{
1230 struct script_desc *s = script_desc__find(name);
1231
1232 if (s)
1233 return s;
1234
1235 s = script_desc__new(name);
1236 if (!s)
1237 goto out_delete_desc;
1238
1239 script_desc__add(s);
1240
1241 return s;
1242
1243out_delete_desc:
1244 script_desc__delete(s);
1245
1246 return NULL;
1247}
1248
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001249static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001250{
1251 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001252 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001253
1254 if (strlen(str) > suffix_len) {
1255 p = str + strlen(str) - suffix_len;
1256 if (!strncmp(p, suffix, suffix_len))
1257 return p;
1258 }
1259
1260 return NULL;
1261}
1262
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001263static int read_script_info(struct script_desc *desc, const char *filename)
1264{
1265 char line[BUFSIZ], *p;
1266 FILE *fp;
1267
1268 fp = fopen(filename, "r");
1269 if (!fp)
1270 return -1;
1271
1272 while (fgets(line, sizeof(line), fp)) {
1273 p = ltrim(line);
1274 if (strlen(p) == 0)
1275 continue;
1276 if (*p != '#')
1277 continue;
1278 p++;
1279 if (strlen(p) && *p == '!')
1280 continue;
1281
1282 p = ltrim(p);
1283 if (strlen(p) && p[strlen(p) - 1] == '\n')
1284 p[strlen(p) - 1] = '\0';
1285
1286 if (!strncmp(p, "description:", strlen("description:"))) {
1287 p += strlen("description:");
1288 desc->half_liner = strdup(ltrim(p));
1289 continue;
1290 }
1291
1292 if (!strncmp(p, "args:", strlen("args:"))) {
1293 p += strlen("args:");
1294 desc->args = strdup(ltrim(p));
1295 continue;
1296 }
1297 }
1298
1299 fclose(fp);
1300
1301 return 0;
1302}
1303
Robert Richter38efb532011-11-25 11:38:40 +01001304static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1305{
1306 char *script_root, *str;
1307
1308 script_root = strdup(script_dirent->d_name);
1309 if (!script_root)
1310 return NULL;
1311
1312 str = (char *)ends_with(script_root, suffix);
1313 if (!str) {
1314 free(script_root);
1315 return NULL;
1316 }
1317
1318 *str = '\0';
1319 return script_root;
1320}
1321
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001322static int list_available_scripts(const struct option *opt __maybe_unused,
1323 const char *s __maybe_unused,
1324 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001325{
1326 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1327 char scripts_path[MAXPATHLEN];
1328 DIR *scripts_dir, *lang_dir;
1329 char script_path[MAXPATHLEN];
1330 char lang_path[MAXPATHLEN];
1331 struct script_desc *desc;
1332 char first_half[BUFSIZ];
1333 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001334
1335 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1336
1337 scripts_dir = opendir(scripts_path);
1338 if (!scripts_dir)
1339 return -1;
1340
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001341 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001342 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1343 lang_dirent.d_name);
1344 lang_dir = opendir(lang_path);
1345 if (!lang_dir)
1346 continue;
1347
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001348 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001349 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1350 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001351 desc = script_desc__findnew(script_root);
1352 snprintf(script_path, MAXPATHLEN, "%s/%s",
1353 lang_path, script_dirent.d_name);
1354 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001355 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001356 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001357 }
1358 }
1359
1360 fprintf(stdout, "List of available trace scripts:\n");
1361 list_for_each_entry(desc, &script_descs, node) {
1362 sprintf(first_half, "%s %s", desc->name,
1363 desc->args ? desc->args : "");
1364 fprintf(stdout, " %-36s %s\n", first_half,
1365 desc->half_liner ? desc->half_liner : "");
1366 }
1367
1368 exit(0);
1369}
1370
Feng Tange5f37052012-09-07 16:42:26 +08001371/*
Feng Tang49e639e2012-10-30 11:56:03 +08001372 * Some scripts specify the required events in their "xxx-record" file,
1373 * this function will check if the events in perf.data match those
1374 * mentioned in the "xxx-record".
1375 *
1376 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1377 * which is covered well now. And new parsing code should be added to
1378 * cover the future complexing formats like event groups etc.
1379 */
1380static int check_ev_match(char *dir_name, char *scriptname,
1381 struct perf_session *session)
1382{
1383 char filename[MAXPATHLEN], evname[128];
1384 char line[BUFSIZ], *p;
1385 struct perf_evsel *pos;
1386 int match, len;
1387 FILE *fp;
1388
1389 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1390
1391 fp = fopen(filename, "r");
1392 if (!fp)
1393 return -1;
1394
1395 while (fgets(line, sizeof(line), fp)) {
1396 p = ltrim(line);
1397 if (*p == '#')
1398 continue;
1399
1400 while (strlen(p)) {
1401 p = strstr(p, "-e");
1402 if (!p)
1403 break;
1404
1405 p += 2;
1406 p = ltrim(p);
1407 len = strcspn(p, " \t");
1408 if (!len)
1409 break;
1410
1411 snprintf(evname, len + 1, "%s", p);
1412
1413 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001414 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001415 if (!strcmp(perf_evsel__name(pos), evname)) {
1416 match = 1;
1417 break;
1418 }
1419 }
1420
1421 if (!match) {
1422 fclose(fp);
1423 return -1;
1424 }
1425 }
1426 }
1427
1428 fclose(fp);
1429 return 0;
1430}
1431
1432/*
Feng Tange5f37052012-09-07 16:42:26 +08001433 * Return -1 if none is found, otherwise the actual scripts number.
1434 *
1435 * Currently the only user of this function is the script browser, which
1436 * will list all statically runnable scripts, select one, execute it and
1437 * show the output in a perf browser.
1438 */
1439int find_scripts(char **scripts_array, char **scripts_path_array)
1440{
1441 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001442 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001443 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001444 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001445 struct perf_data_file file = {
1446 .path = input_name,
1447 .mode = PERF_DATA_MODE_READ,
1448 };
Feng Tange5f37052012-09-07 16:42:26 +08001449 char *temp;
1450 int i = 0;
1451
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001452 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001453 if (!session)
1454 return -1;
1455
Feng Tange5f37052012-09-07 16:42:26 +08001456 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1457
1458 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001459 if (!scripts_dir) {
1460 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001461 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001462 }
Feng Tange5f37052012-09-07 16:42:26 +08001463
1464 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1465 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1466 lang_dirent.d_name);
1467#ifdef NO_LIBPERL
1468 if (strstr(lang_path, "perl"))
1469 continue;
1470#endif
1471#ifdef NO_LIBPYTHON
1472 if (strstr(lang_path, "python"))
1473 continue;
1474#endif
1475
1476 lang_dir = opendir(lang_path);
1477 if (!lang_dir)
1478 continue;
1479
1480 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1481 /* Skip those real time scripts: xxxtop.p[yl] */
1482 if (strstr(script_dirent.d_name, "top."))
1483 continue;
1484 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1485 script_dirent.d_name);
1486 temp = strchr(script_dirent.d_name, '.');
1487 snprintf(scripts_array[i],
1488 (temp - script_dirent.d_name) + 1,
1489 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001490
1491 if (check_ev_match(lang_path,
1492 scripts_array[i], session))
1493 continue;
1494
Feng Tange5f37052012-09-07 16:42:26 +08001495 i++;
1496 }
Feng Tang49e639e2012-10-30 11:56:03 +08001497 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001498 }
1499
Feng Tang49e639e2012-10-30 11:56:03 +08001500 closedir(scripts_dir);
1501 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001502 return i;
1503}
1504
Tom Zanussi38752942009-12-15 02:53:39 -06001505static char *get_script_path(const char *script_root, const char *suffix)
1506{
1507 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1508 char scripts_path[MAXPATHLEN];
1509 char script_path[MAXPATHLEN];
1510 DIR *scripts_dir, *lang_dir;
1511 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001512 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001513
1514 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1515
1516 scripts_dir = opendir(scripts_path);
1517 if (!scripts_dir)
1518 return NULL;
1519
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001520 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001521 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1522 lang_dirent.d_name);
1523 lang_dir = opendir(lang_path);
1524 if (!lang_dir)
1525 continue;
1526
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001527 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001528 __script_root = get_script_root(&script_dirent, suffix);
1529 if (__script_root && !strcmp(script_root, __script_root)) {
1530 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001531 closedir(lang_dir);
1532 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001533 snprintf(script_path, MAXPATHLEN, "%s/%s",
1534 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001535 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001536 }
1537 free(__script_root);
1538 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001539 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001540 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001541 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001542
Robert Richter38efb532011-11-25 11:38:40 +01001543 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001544}
1545
Tom Zanussib5b87312010-11-10 08:16:51 -06001546static bool is_top_script(const char *script_path)
1547{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001548 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001549}
1550
1551static int has_required_arg(char *script_path)
1552{
1553 struct script_desc *desc;
1554 int n_args = 0;
1555 char *p;
1556
1557 desc = script_desc__new(NULL);
1558
1559 if (read_script_info(desc, script_path))
1560 goto out;
1561
1562 if (!desc->args)
1563 goto out;
1564
1565 for (p = desc->args; *p; p++)
1566 if (*p == '<')
1567 n_args++;
1568out:
1569 script_desc__delete(desc);
1570
1571 return n_args;
1572}
1573
David Ahernd54b1a92012-08-26 12:24:46 -06001574static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001575{
1576 char **__argv = malloc(sizeof(const char *) * argc);
1577
David Ahernd54b1a92012-08-26 12:24:46 -06001578 if (!__argv) {
1579 pr_err("malloc failed\n");
1580 return -1;
1581 }
1582
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001583 memcpy(__argv, argv, sizeof(const char *) * argc);
1584 argc = parse_options(argc, (const char **)__argv, record_options,
1585 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1586 free(__argv);
1587
David Ahernd54b1a92012-08-26 12:24:46 -06001588 system_wide = (argc == 0);
1589
1590 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001591}
1592
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001593static void script__setup_sample_type(struct perf_script *script)
1594{
1595 struct perf_session *session = script->session;
1596 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
1597
1598 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1599 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1600 (sample_type & PERF_SAMPLE_STACK_USER))
1601 callchain_param.record_mode = CALLCHAIN_DWARF;
1602 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1603 callchain_param.record_mode = CALLCHAIN_LBR;
1604 else
1605 callchain_param.record_mode = CALLCHAIN_FP;
1606 }
1607}
1608
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001609int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001610{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001611 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001612 bool header = false;
1613 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001614 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001615 char *rec_script_path = NULL;
1616 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001617 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001618 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001619 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001620 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001621 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001622 struct perf_script script = {
1623 .tool = {
1624 .sample = process_sample_event,
1625 .mmap = perf_event__process_mmap,
1626 .mmap2 = perf_event__process_mmap2,
1627 .comm = perf_event__process_comm,
1628 .exit = perf_event__process_exit,
1629 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001630 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001631 .tracing_data = perf_event__process_tracing_data,
1632 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001633 .id_index = perf_event__process_id_index,
1634 .auxtrace_info = perf_event__process_auxtrace_info,
1635 .auxtrace = perf_event__process_auxtrace,
1636 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001637 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001638 .ordering_requires_timestamps = true,
1639 },
1640 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001641 struct perf_data_file file = {
1642 .mode = PERF_DATA_MODE_READ,
1643 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001644 const struct option options[] = {
1645 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1646 "dump raw trace in ASCII"),
1647 OPT_INCR('v', "verbose", &verbose,
1648 "be more verbose (show symbol address, etc)"),
1649 OPT_BOOLEAN('L', "Latency", &latency_format,
1650 "show latency attributes (irqs/preemption disabled, etc)"),
1651 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1652 list_available_scripts),
1653 OPT_CALLBACK('s', "script", NULL, "name",
1654 "script file name (lang:script name, script name, or *)",
1655 parse_scriptname),
1656 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1657 "generate perf-script.xx script in specified language"),
1658 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1659 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1660 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001661 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1662 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001663 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1664 "file", "vmlinux pathname"),
1665 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1666 "file", "kallsyms pathname"),
1667 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1668 "When printing symbols do not display call chain"),
1669 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1670 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001671 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001672 "comma separated output fields prepend with 'type:'. "
1673 "Valid types: hw,sw,trace,raw. "
1674 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Stephane Eranianfc36f942015-08-31 18:41:10 +02001675 "addr,symoff,period,iregs,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001676 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1677 "system-wide collection from all CPUs"),
1678 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1679 "only consider these symbols"),
1680 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1681 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1682 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06001683 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1684 "only consider symbols in these pids"),
1685 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1686 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001687 OPT_BOOLEAN('I', "show-info", &show_full_info,
1688 "display extended information from perf.data file"),
1689 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1690 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001691 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1692 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001693 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1694 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03001695 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1696 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001697 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001698 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1699 "Instruction Tracing options",
1700 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07001701 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1702 "Show full source file name path for source lines"),
Mark Drayton77e00702015-08-26 12:18:15 -07001703 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1704 "Enable symbol demangling"),
1705 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1706 "Enable kernel symbol demangling"),
1707
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001708 OPT_END()
1709 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08001710 const char * const script_subcommands[] = { "record", "report", NULL };
1711 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001712 "perf script [<options>]",
1713 "perf script [<options>] record <script> [<record-options>] <command>",
1714 "perf script [<options>] report <script> [script-args]",
1715 "perf script [<options>] <script> [<record-options>] <command>",
1716 "perf script [<options>] <top-script> [script-args]",
1717 NULL
1718 };
Tom Zanussi38752942009-12-15 02:53:39 -06001719
Tom Zanussib5b87312010-11-10 08:16:51 -06001720 setup_scripting();
1721
Yunlong Song40cae2b2015-03-18 21:35:54 +08001722 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001723 PARSE_OPT_STOP_AT_NON_OPTION);
1724
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001725 file.path = input_name;
1726
Tom Zanussib5b87312010-11-10 08:16:51 -06001727 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1728 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1729 if (!rec_script_path)
1730 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001731 }
1732
Tom Zanussib5b87312010-11-10 08:16:51 -06001733 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1734 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1735 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001736 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001737 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001738 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001739 return -1;
1740 }
Tom Zanussi38752942009-12-15 02:53:39 -06001741 }
1742
Ben Hutchings44e668c2010-10-10 16:10:03 +01001743 /* make sure PERF_EXEC_PATH is set for scripts */
1744 perf_set_argv_exec_path(perf_exec_path());
1745
Tom Zanussib5b87312010-11-10 08:16:51 -06001746 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001747 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001748 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001749 pid_t pid;
1750
Tom Zanussib5b87312010-11-10 08:16:51 -06001751 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1752 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1753
1754 if (!rec_script_path && !rep_script_path) {
1755 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001756 " script -l for available scripts.\n", argv[0]);
1757 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001758 }
1759
Tom Zanussib5b87312010-11-10 08:16:51 -06001760 if (is_top_script(argv[0])) {
1761 rep_args = argc - 1;
1762 } else {
1763 int rec_args;
1764
1765 rep_args = has_required_arg(rep_script_path);
1766 rec_args = (argc - 1) - rep_args;
1767 if (rec_args < 0) {
1768 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001769 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001770 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001771 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001772 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001773 }
1774
1775 if (pipe(live_pipe) < 0) {
1776 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001777 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001778 }
1779
1780 pid = fork();
1781 if (pid < 0) {
1782 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001783 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001784 }
1785
1786 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001787 j = 0;
1788
Tom Zanussia0cccc22010-04-01 23:59:25 -05001789 dup2(live_pipe[1], 1);
1790 close(live_pipe[0]);
1791
Robert Richter317df652011-11-25 15:05:25 +01001792 if (is_top_script(argv[0])) {
1793 system_wide = true;
1794 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001795 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1796 err = -1;
1797 goto out;
1798 }
Robert Richter317df652011-11-25 15:05:25 +01001799 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001800
1801 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001802 if (!__argv) {
1803 pr_err("malloc failed\n");
1804 err = -ENOMEM;
1805 goto out;
1806 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001807
Tom Zanussib5b87312010-11-10 08:16:51 -06001808 __argv[j++] = "/bin/sh";
1809 __argv[j++] = rec_script_path;
1810 if (system_wide)
1811 __argv[j++] = "-a";
1812 __argv[j++] = "-q";
1813 __argv[j++] = "-o";
1814 __argv[j++] = "-";
1815 for (i = rep_args + 1; i < argc; i++)
1816 __argv[j++] = argv[i];
1817 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001818
1819 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001820 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001821 exit(-1);
1822 }
1823
1824 dup2(live_pipe[0], 0);
1825 close(live_pipe[1]);
1826
Tom Zanussib5b87312010-11-10 08:16:51 -06001827 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001828 if (!__argv) {
1829 pr_err("malloc failed\n");
1830 err = -ENOMEM;
1831 goto out;
1832 }
1833
Tom Zanussib5b87312010-11-10 08:16:51 -06001834 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001835 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001836 __argv[j++] = rep_script_path;
1837 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001838 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001839 __argv[j++] = "-i";
1840 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001841 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001842
1843 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001844 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001845 exit(-1);
1846 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001847
Tom Zanussib5b87312010-11-10 08:16:51 -06001848 if (rec_script_path)
1849 script_path = rec_script_path;
1850 if (rep_script_path)
1851 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001852
Tom Zanussib5b87312010-11-10 08:16:51 -06001853 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001854 j = 0;
1855
Robert Richter317df652011-11-25 15:05:25 +01001856 if (!rec_script_path)
1857 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001858 else if (!system_wide) {
1859 if (have_cmd(argc - 1, &argv[1]) != 0) {
1860 err = -1;
1861 goto out;
1862 }
1863 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001864
1865 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001866 if (!__argv) {
1867 pr_err("malloc failed\n");
1868 err = -ENOMEM;
1869 goto out;
1870 }
1871
Tom Zanussib5b87312010-11-10 08:16:51 -06001872 __argv[j++] = "/bin/sh";
1873 __argv[j++] = script_path;
1874 if (system_wide)
1875 __argv[j++] = "-a";
1876 for (i = 2; i < argc; i++)
1877 __argv[j++] = argv[i];
1878 __argv[j++] = NULL;
1879
1880 execvp("/bin/sh", (char **)__argv);
1881 free(__argv);
1882 exit(-1);
1883 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001884
Tom Zanussicf4fee52010-03-03 01:04:33 -06001885 if (!script_name)
1886 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001887
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001888 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001889 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001890 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001891
Jiri Olsae90debd2013-12-09 11:02:50 +01001892 if (header || header_only) {
1893 perf_session__fprintf_info(session, stdout, show_full_info);
1894 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001895 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01001896 }
1897
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001898 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09001899 goto out_delete;
1900
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001901 script.session = session;
Jiri Olsa7322d6c2015-08-13 09:17:24 +02001902 script__setup_sample_type(&script);
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001903
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001904 session->itrace_synth_opts = &itrace_synth_opts;
1905
Anton Blanchard5d67be92011-07-04 21:57:50 +10001906 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001907 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1908 if (err < 0)
1909 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001910 }
1911
David Ahern1424dc92011-03-09 22:23:28 -07001912 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001913 symbol_conf.use_callchain = true;
1914 else
1915 symbol_conf.use_callchain = false;
1916
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03001917 if (session->tevent.pevent &&
1918 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03001919 machine__resolve_kernel_addr,
1920 &session->machines.host) < 0) {
1921 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
1922 return -1;
1923 }
1924
Tom Zanussi956ffd02009-11-25 01:15:46 -06001925 if (generate_script_lang) {
1926 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001927 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001928
David Ahern2c9e45f72011-03-17 10:03:21 -06001929 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001930 fprintf(stderr,
1931 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001932 err = -EINVAL;
1933 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07001934 }
1935
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001936 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06001937 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001938 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001939 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001940 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001941 }
1942
1943 err = fstat(input, &perf_stat);
1944 if (err < 0) {
1945 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001946 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001947 }
1948
1949 if (!perf_stat.st_size) {
1950 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001951 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001952 }
1953
1954 scripting_ops = script_spec__lookup(generate_script_lang);
1955 if (!scripting_ops) {
1956 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001957 err = -ENOENT;
1958 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001959 }
1960
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01001961 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001962 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001963 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001964 }
1965
1966 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001967 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001968 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001969 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001970 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001971 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001972 }
1973
David Ahern9cbdb702011-04-06 21:54:20 -06001974
1975 err = perf_session__check_output_opt(session);
1976 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001977 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06001978
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001979 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001980
Adrian Hunterd445dd22014-08-15 22:08:37 +03001981 flush_scripting();
1982
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001983out_delete:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001984 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001985
1986 if (script_started)
1987 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001988out:
1989 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001990}