blob: 7b376d215e9498417001c6c2cb7ab5ba2f770f53 [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"
9#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020010#include "util/tool.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020011#include "util/symbol.h"
12#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010013#include "util/trace-event.h"
Andrea Gelminib7eead82010-08-05 15:51:38 +020014#include "util/util.h"
David Ahern1424dc92011-03-09 22:23:28 -070015#include "util/evlist.h"
16#include "util/evsel.h"
Feng Tang36385be2012-09-07 16:42:24 +080017#include "util/sort.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020018#include "util/data.h"
Adrian Hunter7a680eb2015-04-09 18:53:56 +030019#include "util/auxtrace.h"
Anton Blanchard5d67be92011-07-04 21:57:50 +100020#include <linux/bitmap.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020021
Tom Zanussi956ffd02009-11-25 01:15:46 -060022static char const *script_name;
23static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020024static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020025static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020026static u64 nr_unordered;
David Ahernc0230b22011-03-09 22:23:27 -070027static bool no_callchain;
Namhyung Kim47390ae2013-06-04 14:20:28 +090028static bool latency_format;
Robert Richter317df652011-11-25 15:05:25 +010029static bool system_wide;
Adrian Hunter400ea6d2015-04-09 18:54:05 +030030static bool print_flags;
Anton Blanchard5d67be92011-07-04 21:57:50 +100031static const char *cpu_list;
32static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Tom Zanussi956ffd02009-11-25 01:15:46 -060033
David Ahern745f43e2011-03-09 22:23:26 -070034enum perf_output_field {
35 PERF_OUTPUT_COMM = 1U << 0,
36 PERF_OUTPUT_TID = 1U << 1,
37 PERF_OUTPUT_PID = 1U << 2,
38 PERF_OUTPUT_TIME = 1U << 3,
39 PERF_OUTPUT_CPU = 1U << 4,
40 PERF_OUTPUT_EVNAME = 1U << 5,
41 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060042 PERF_OUTPUT_IP = 1U << 7,
43 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060044 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060045 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090046 PERF_OUTPUT_SYMOFFSET = 1U << 11,
Adrian Huntercc8fae12013-12-06 09:42:57 +020047 PERF_OUTPUT_SRCLINE = 1U << 12,
Jiri Olsa535aeaa2014-08-25 16:45:42 +020048 PERF_OUTPUT_PERIOD = 1U << 13,
David Ahern745f43e2011-03-09 22:23:26 -070049};
50
51struct output_option {
52 const char *str;
53 enum perf_output_field field;
54} all_output_options[] = {
55 {.str = "comm", .field = PERF_OUTPUT_COMM},
56 {.str = "tid", .field = PERF_OUTPUT_TID},
57 {.str = "pid", .field = PERF_OUTPUT_PID},
58 {.str = "time", .field = PERF_OUTPUT_TIME},
59 {.str = "cpu", .field = PERF_OUTPUT_CPU},
60 {.str = "event", .field = PERF_OUTPUT_EVNAME},
61 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060062 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070063 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060064 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060065 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090066 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
Adrian Huntercc8fae12013-12-06 09:42:57 +020067 {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
Jiri Olsa535aeaa2014-08-25 16:45:42 +020068 {.str = "period", .field = PERF_OUTPUT_PERIOD},
David Ahern745f43e2011-03-09 22:23:26 -070069};
70
71/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060072static struct {
73 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060074 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040075 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060076 u64 fields;
77 u64 invalid_fields;
78} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070079
David Ahern2c9e45f72011-03-17 10:03:21 -060080 [PERF_TYPE_HARDWARE] = {
81 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070082
David Ahern2c9e45f72011-03-17 10:03:21 -060083 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
84 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060085 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +020086 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
87 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -060088
89 .invalid_fields = PERF_OUTPUT_TRACE,
90 },
91
92 [PERF_TYPE_SOFTWARE] = {
93 .user_set = false,
94
95 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
96 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060097 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +020098 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
99 PERF_OUTPUT_PERIOD,
David Ahern2c9e45f72011-03-17 10:03:21 -0600100
101 .invalid_fields = PERF_OUTPUT_TRACE,
102 },
103
104 [PERF_TYPE_TRACEPOINT] = {
105 .user_set = false,
106
107 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
108 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
109 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
110 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700111
112 [PERF_TYPE_RAW] = {
113 .user_set = false,
114
115 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
116 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600117 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
Jiri Olsae8564b72014-08-25 16:45:43 +0200118 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
119 PERF_OUTPUT_PERIOD,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700120
121 .invalid_fields = PERF_OUTPUT_TRACE,
122 },
David Ahern1424dc92011-03-09 22:23:28 -0700123};
David Ahern745f43e2011-03-09 22:23:26 -0700124
David Ahern2c9e45f72011-03-17 10:03:21 -0600125static bool output_set_by_user(void)
126{
127 int j;
128 for (j = 0; j < PERF_TYPE_MAX; ++j) {
129 if (output[j].user_set)
130 return true;
131 }
132 return false;
133}
David Ahern745f43e2011-03-09 22:23:26 -0700134
David Ahern9cbdb702011-04-06 21:54:20 -0600135static const char *output_field2str(enum perf_output_field field)
136{
137 int i, imax = ARRAY_SIZE(all_output_options);
138 const char *str = "";
139
140 for (i = 0; i < imax; ++i) {
141 if (all_output_options[i].field == field) {
142 str = all_output_options[i].str;
143 break;
144 }
145 }
146 return str;
147}
148
David Ahern2c9e45f72011-03-17 10:03:21 -0600149#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700150
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300151static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
152 u64 sample_type, const char *sample_msg,
153 enum perf_output_field field,
154 bool allow_user_set)
David Ahern1424dc92011-03-09 22:23:28 -0700155{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300156 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600157 int type = attr->type;
158 const char *evname;
159
160 if (attr->sample_type & sample_type)
161 return 0;
162
163 if (output[type].user_set) {
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300164 if (allow_user_set)
165 return 0;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300166 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600167 pr_err("Samples for '%s' event do not have %s attribute set. "
168 "Cannot print '%s' field.\n",
169 evname, sample_msg, output_field2str(field));
170 return -1;
171 }
172
173 /* user did not ask for it explicitly so remove from the default list */
174 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300175 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600176 pr_debug("Samples for '%s' event do not have %s attribute set. "
177 "Skipping '%s' field.\n",
178 evname, sample_msg, output_field2str(field));
179
180 return 0;
181}
182
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300183static int perf_evsel__check_stype(struct perf_evsel *evsel,
184 u64 sample_type, const char *sample_msg,
185 enum perf_output_field field)
186{
187 return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
188 false);
189}
190
David Ahern9cbdb702011-04-06 21:54:20 -0600191static int perf_evsel__check_attr(struct perf_evsel *evsel,
192 struct perf_session *session)
193{
194 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300195 bool allow_user_set;
196
197 allow_user_set = perf_header__has_feat(&session->header,
198 HEADER_AUXTRACE);
David Ahern9cbdb702011-04-06 21:54:20 -0600199
David Ahern1424dc92011-03-09 22:23:28 -0700200 if (PRINT_FIELD(TRACE) &&
201 !perf_session__has_traces(session, "record -R"))
202 return -EINVAL;
203
David Ahern787bef12011-05-27 14:28:43 -0600204 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300205 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
206 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700207 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700208 }
David Ahern7cec0922011-05-30 13:08:23 -0600209
210 if (PRINT_FIELD(ADDR) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300211 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
212 PERF_OUTPUT_ADDR, allow_user_set))
David Ahern7cec0922011-05-30 13:08:23 -0600213 return -EINVAL;
214
215 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
216 pr_err("Display of symbols requested but neither sample IP nor "
217 "sample address\nis selected. Hence, no addresses to convert "
218 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600219 return -EINVAL;
220 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900221 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
222 pr_err("Display of offsets requested but symbol is not"
223 "selected.\n");
224 return -EINVAL;
225 }
David Ahern7cec0922011-05-30 13:08:23 -0600226 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
227 pr_err("Display of DSO requested but neither sample IP nor "
228 "sample address\nis selected. Hence, no addresses to convert "
229 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600230 return -EINVAL;
231 }
Adrian Huntercc8fae12013-12-06 09:42:57 +0200232 if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
233 pr_err("Display of source line number requested but sample IP is not\n"
234 "selected. Hence, no address to lookup the source line number.\n");
235 return -EINVAL;
236 }
David Ahern1424dc92011-03-09 22:23:28 -0700237
238 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300239 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
240 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700241 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700242
243 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300244 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
245 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700246 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700247
248 if (PRINT_FIELD(CPU) &&
Adrian Hunter6d5cdd62015-04-24 22:29:44 +0300249 perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
250 PERF_OUTPUT_CPU, allow_user_set))
David Ahern1424dc92011-03-09 22:23:28 -0700251 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600252
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200253 if (PRINT_FIELD(PERIOD) &&
254 perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
255 PERF_OUTPUT_PERIOD))
256 return -EINVAL;
257
David Ahern9cbdb702011-04-06 21:54:20 -0600258 return 0;
259}
260
Adrian Hunter7ea95722013-11-01 15:51:30 +0200261static void set_print_ip_opts(struct perf_event_attr *attr)
262{
263 unsigned int type = attr->type;
264
265 output[type].print_ip_opts = 0;
266 if (PRINT_FIELD(IP))
267 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
268
269 if (PRINT_FIELD(SYM))
270 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
271
272 if (PRINT_FIELD(DSO))
273 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
274
275 if (PRINT_FIELD(SYMOFFSET))
276 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
Adrian Huntercc8fae12013-12-06 09:42:57 +0200277
278 if (PRINT_FIELD(SRCLINE))
279 output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
Adrian Hunter7ea95722013-11-01 15:51:30 +0200280}
281
David Ahern9cbdb702011-04-06 21:54:20 -0600282/*
283 * verify all user requested events exist and the samples
284 * have the expected data
285 */
286static int perf_session__check_output_opt(struct perf_session *session)
287{
288 int j;
289 struct perf_evsel *evsel;
290
291 for (j = 0; j < PERF_TYPE_MAX; ++j) {
292 evsel = perf_session__find_first_evtype(session, j);
293
294 /*
295 * even if fields is set to 0 (ie., show nothing) event must
296 * exist if user explicitly includes it on the command line
297 */
298 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
299 pr_err("%s events do not exist. "
300 "Remove corresponding -f option to proceed.\n",
301 event_type(j));
302 return -1;
303 }
304
305 if (evsel && output[j].fields &&
306 perf_evsel__check_attr(evsel, session))
307 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400308
309 if (evsel == NULL)
310 continue;
311
Adrian Hunter7ea95722013-11-01 15:51:30 +0200312 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700313 }
314
Adrian Hunter98526ee2014-07-31 09:00:59 +0300315 if (!no_callchain) {
316 bool use_callchain = false;
317
318 evlist__for_each(session->evlist, evsel) {
319 if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
320 use_callchain = true;
321 break;
322 }
323 }
324 if (!use_callchain)
325 symbol_conf.use_callchain = false;
326 }
327
David Ahern80b8b492013-11-19 21:07:37 -0700328 /*
329 * set default for tracepoints to print symbols only
330 * if callchains are present
331 */
332 if (symbol_conf.use_callchain &&
333 !output[PERF_TYPE_TRACEPOINT].user_set) {
334 struct perf_event_attr *attr;
335
336 j = PERF_TYPE_TRACEPOINT;
337 evsel = perf_session__find_first_evtype(session, j);
338 if (evsel == NULL)
339 goto out;
340
341 attr = &evsel->attr;
342
343 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
344 output[j].fields |= PERF_OUTPUT_IP;
345 output[j].fields |= PERF_OUTPUT_SYM;
346 output[j].fields |= PERF_OUTPUT_DSO;
347 set_print_ip_opts(attr);
348 }
349 }
350
351out:
David Ahern1424dc92011-03-09 22:23:28 -0700352 return 0;
353}
David Ahern745f43e2011-03-09 22:23:26 -0700354
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300355static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700356 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300357 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700358{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300359 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700360 unsigned long secs;
361 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700362 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700363
David Ahern745f43e2011-03-09 22:23:26 -0700364 if (PRINT_FIELD(COMM)) {
365 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200366 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600367 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200368 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700369 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200370 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700371 }
David Ahernc70c94b2011-03-09 22:23:25 -0700372
David Ahern745f43e2011-03-09 22:23:26 -0700373 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
374 printf("%5d/%-5d ", sample->pid, sample->tid);
375 else if (PRINT_FIELD(PID))
376 printf("%5d ", sample->pid);
377 else if (PRINT_FIELD(TID))
378 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700379
David Ahern745f43e2011-03-09 22:23:26 -0700380 if (PRINT_FIELD(CPU)) {
381 if (latency_format)
382 printf("%3d ", sample->cpu);
383 else
384 printf("[%03d] ", sample->cpu);
385 }
David Ahernc70c94b2011-03-09 22:23:25 -0700386
David Ahern745f43e2011-03-09 22:23:26 -0700387 if (PRINT_FIELD(TIME)) {
388 nsecs = sample->time;
389 secs = nsecs / NSECS_PER_SEC;
390 nsecs -= secs * NSECS_PER_SEC;
391 usecs = nsecs / NSECS_PER_USEC;
392 printf("%5lu.%06lu: ", secs, usecs);
393 }
David Ahernc70c94b2011-03-09 22:23:25 -0700394}
395
David Ahern7cec0922011-05-30 13:08:23 -0600396static void print_sample_addr(union perf_event *event,
397 struct perf_sample *sample,
David Ahern7cec0922011-05-30 13:08:23 -0600398 struct thread *thread,
399 struct perf_event_attr *attr)
400{
401 struct addr_location al;
David Ahern7cec0922011-05-30 13:08:23 -0600402
403 printf("%16" PRIx64, sample->addr);
404
405 if (!sample_addr_correlates_sym(attr))
406 return;
407
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300408 perf_event__preprocess_sample_addr(event, sample, thread, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600409
410 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900411 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900412 if (PRINT_FIELD(SYMOFFSET))
413 symbol__fprintf_symname_offs(al.sym, &al, stdout);
414 else
415 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600416 }
417
418 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900419 printf(" (");
420 map__fprintf_dsoname(al.map, stdout);
421 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600422 }
423}
424
Akihiro Nagai95582592012-01-30 13:43:09 +0900425static void print_sample_bts(union perf_event *event,
426 struct perf_sample *sample,
427 struct perf_evsel *evsel,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200428 struct thread *thread,
429 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900430{
431 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300432 bool print_srcline_last = false;
Akihiro Nagai95582592012-01-30 13:43:09 +0900433
434 /* print branch_from information */
435 if (PRINT_FIELD(IP)) {
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300436 unsigned int print_opts = output[attr->type].print_ip_opts;
437
438 if (symbol_conf.use_callchain && sample->callchain) {
Akihiro Nagai95582592012-01-30 13:43:09 +0900439 printf("\n");
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300440 } else {
441 printf(" ");
442 if (print_opts & PRINT_IP_OPT_SRCLINE) {
443 print_srcline_last = true;
444 print_opts &= ~PRINT_IP_OPT_SRCLINE;
445 }
446 }
447 perf_evsel__print_ip(evsel, sample, al, print_opts,
David Ahern307cbb92013-08-07 22:50:53 -0400448 PERF_MAX_STACK_DEPTH);
Akihiro Nagai95582592012-01-30 13:43:09 +0900449 }
450
Akihiro Nagai95582592012-01-30 13:43:09 +0900451 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300452 if (PRINT_FIELD(ADDR) ||
453 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
Adrian Hunter578bea42014-07-22 16:17:16 +0300454 !output[attr->type].user_set)) {
455 printf(" => ");
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300456 print_sample_addr(event, sample, thread, attr);
Adrian Hunter578bea42014-07-22 16:17:16 +0300457 }
Akihiro Nagai95582592012-01-30 13:43:09 +0900458
Adrian Hunter8066be5f2014-07-22 16:17:15 +0300459 if (print_srcline_last)
460 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
461
Akihiro Nagai95582592012-01-30 13:43:09 +0900462 printf("\n");
463}
464
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300465static void print_sample_flags(u32 flags)
466{
467 const char *chars = PERF_IP_FLAG_CHARS;
468 const int n = strlen(PERF_IP_FLAG_CHARS);
469 char str[33];
470 int i, pos = 0;
471
472 for (i = 0; i < n; i++, flags >>= 1) {
473 if (flags & 1)
474 str[pos++] = chars[i];
475 }
476 for (; i < 32; i++, flags >>= 1) {
477 if (flags & 1)
478 str[pos++] = '?';
479 }
480 str[pos] = 0;
481 printf(" %-4s ", str);
482}
483
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300484static void process_event(union perf_event *event, struct perf_sample *sample,
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300485 struct perf_evsel *evsel, struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700486{
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300487 struct thread *thread = al->thread;
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300488 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700489
David Ahern2c9e45f72011-03-17 10:03:21 -0600490 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700491 return;
492
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300493 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700494
Jiri Olsa535aeaa2014-08-25 16:45:42 +0200495 if (PRINT_FIELD(PERIOD))
496 printf("%10" PRIu64 " ", sample->period);
497
Namhyung Kime944d3d2013-11-18 14:34:52 +0900498 if (PRINT_FIELD(EVNAME)) {
499 const char *evname = perf_evsel__name(evsel);
500 printf("%s: ", evname ? evname : "[unknown]");
501 }
502
Adrian Hunter400ea6d2015-04-09 18:54:05 +0300503 if (print_flags)
504 print_sample_flags(sample->flags);
505
Akihiro Nagai95582592012-01-30 13:43:09 +0900506 if (is_bts_event(attr)) {
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300507 print_sample_bts(event, sample, evsel, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900508 return;
509 }
510
David Ahern745f43e2011-03-09 22:23:26 -0700511 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300512 event_format__print(evsel->tp_format, sample->cpu,
513 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600514 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300515 print_sample_addr(event, sample, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600516
David Ahern787bef12011-05-27 14:28:43 -0600517 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700518 if (!symbol_conf.use_callchain)
519 printf(" ");
520 else
521 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400522
Arnaldo Carvalho de Melocc22e572013-12-19 17:20:06 -0300523 perf_evsel__print_ip(evsel, sample, al,
David Ahern307cbb92013-08-07 22:50:53 -0400524 output[attr->type].print_ip_opts,
525 PERF_MAX_STACK_DEPTH);
David Ahernc0230b22011-03-09 22:23:27 -0700526 }
527
David Ahernc70c94b2011-03-09 22:23:25 -0700528 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700529}
530
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300531static int default_start_script(const char *script __maybe_unused,
532 int argc __maybe_unused,
533 const char **argv __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600534{
535 return 0;
536}
537
Adrian Hunterd445dd22014-08-15 22:08:37 +0300538static int default_flush_script(void)
539{
540 return 0;
541}
542
Tom Zanussi956ffd02009-11-25 01:15:46 -0600543static int default_stop_script(void)
544{
545 return 0;
546}
547
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300548static int default_generate_script(struct pevent *pevent __maybe_unused,
549 const char *outfile __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600550{
551 return 0;
552}
553
554static struct scripting_ops default_scripting_ops = {
555 .start_script = default_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300556 .flush_script = default_flush_script,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600557 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700558 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600559 .generate_script = default_generate_script,
560};
561
562static struct scripting_ops *scripting_ops;
563
564static void setup_scripting(void)
565{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600566 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600567 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600568
Tom Zanussi956ffd02009-11-25 01:15:46 -0600569 scripting_ops = &default_scripting_ops;
570}
571
Adrian Hunterd445dd22014-08-15 22:08:37 +0300572static int flush_scripting(void)
573{
574 return scripting_ops->flush_script();
575}
576
Tom Zanussi956ffd02009-11-25 01:15:46 -0600577static int cleanup_scripting(void)
578{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100579 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500580
Tom Zanussi956ffd02009-11-25 01:15:46 -0600581 return scripting_ops->stop_script();
582}
583
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300584static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200585 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200586 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300587 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200588 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200589{
David Aherne7984b72011-11-21 10:02:52 -0700590 struct addr_location al;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200591
David Ahern1424dc92011-03-09 22:23:28 -0700592 if (debug_mode) {
593 if (sample->time < last_timestamp) {
594 pr_err("Samples misordered, previous: %" PRIu64
595 " this: %" PRIu64 "\n", last_timestamp,
596 sample->time);
597 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200598 }
David Ahern1424dc92011-03-09 22:23:28 -0700599 last_timestamp = sample->time;
600 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200601 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000602
Adrian Huntere44baa32013-08-08 14:32:25 +0300603 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700604 pr_err("problem processing %d event, skipping it.\n",
605 event->header.type);
606 return -1;
607 }
608
609 if (al.filtered)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300610 goto out_put;
David Aherne7984b72011-11-21 10:02:52 -0700611
Anton Blanchard5d67be92011-07-04 21:57:50 +1000612 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300613 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000614
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300615 scripting_ops->process_event(event, sample, evsel, &al);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300616out_put:
617 addr_location__put(&al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200618 return 0;
619}
620
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300621struct perf_script {
622 struct perf_tool tool;
623 struct perf_session *session;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900624 bool show_task_events;
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900625 bool show_mmap_events;
Adrian Hunter7c148982015-07-21 12:44:06 +0300626 bool show_switch_events;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200627};
628
Adrian Hunter7ea95722013-11-01 15:51:30 +0200629static int process_attr(struct perf_tool *tool, union perf_event *event,
630 struct perf_evlist **pevlist)
631{
632 struct perf_script *scr = container_of(tool, struct perf_script, tool);
633 struct perf_evlist *evlist;
634 struct perf_evsel *evsel, *pos;
635 int err;
636
637 err = perf_event__process_attr(tool, event, pevlist);
638 if (err)
639 return err;
640
641 evlist = *pevlist;
642 evsel = perf_evlist__last(*pevlist);
643
644 if (evsel->attr.type >= PERF_TYPE_MAX)
645 return 0;
646
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300647 evlist__for_each(evlist, pos) {
Adrian Hunter7ea95722013-11-01 15:51:30 +0200648 if (pos->attr.type == evsel->attr.type && pos != evsel)
649 return 0;
650 }
651
652 set_print_ip_opts(&evsel->attr);
653
654 return perf_evsel__check_attr(evsel, scr->session);
655}
656
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900657static int process_comm_event(struct perf_tool *tool,
658 union perf_event *event,
659 struct perf_sample *sample,
660 struct machine *machine)
661{
662 struct thread *thread;
663 struct perf_script *script = container_of(tool, struct perf_script, tool);
664 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300665 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900666 int ret = -1;
667
668 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
669 if (thread == NULL) {
670 pr_debug("problem processing COMM event, skipping it.\n");
671 return -1;
672 }
673
674 if (perf_event__process_comm(tool, event, sample, machine) < 0)
675 goto out;
676
677 if (!evsel->attr.sample_id_all) {
678 sample->cpu = 0;
679 sample->time = 0;
680 sample->tid = event->comm.tid;
681 sample->pid = event->comm.pid;
682 }
683 print_sample_start(sample, thread, evsel);
684 perf_event__fprintf(event, stdout);
685 ret = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900686out:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300687 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900688 return ret;
689}
690
691static int process_fork_event(struct perf_tool *tool,
692 union perf_event *event,
693 struct perf_sample *sample,
694 struct machine *machine)
695{
696 struct thread *thread;
697 struct perf_script *script = container_of(tool, struct perf_script, tool);
698 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300699 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900700
701 if (perf_event__process_fork(tool, event, sample, machine) < 0)
702 return -1;
703
704 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
705 if (thread == NULL) {
706 pr_debug("problem processing FORK event, skipping it.\n");
707 return -1;
708 }
709
710 if (!evsel->attr.sample_id_all) {
711 sample->cpu = 0;
712 sample->time = event->fork.time;
713 sample->tid = event->fork.tid;
714 sample->pid = event->fork.pid;
715 }
716 print_sample_start(sample, thread, evsel);
717 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300718 thread__put(thread);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900719
720 return 0;
721}
722static int process_exit_event(struct perf_tool *tool,
723 union perf_event *event,
724 struct perf_sample *sample,
725 struct machine *machine)
726{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300727 int err = 0;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900728 struct thread *thread;
729 struct perf_script *script = container_of(tool, struct perf_script, tool);
730 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300731 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900732
733 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
734 if (thread == NULL) {
735 pr_debug("problem processing EXIT event, skipping it.\n");
736 return -1;
737 }
738
739 if (!evsel->attr.sample_id_all) {
740 sample->cpu = 0;
741 sample->time = 0;
742 sample->tid = event->comm.tid;
743 sample->pid = event->comm.pid;
744 }
745 print_sample_start(sample, thread, evsel);
746 perf_event__fprintf(event, stdout);
747
748 if (perf_event__process_exit(tool, event, sample, machine) < 0)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300749 err = -1;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900750
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300751 thread__put(thread);
752 return err;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900753}
754
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900755static int process_mmap_event(struct perf_tool *tool,
756 union perf_event *event,
757 struct perf_sample *sample,
758 struct machine *machine)
759{
760 struct thread *thread;
761 struct perf_script *script = container_of(tool, struct perf_script, tool);
762 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300763 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900764
765 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
766 return -1;
767
768 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
769 if (thread == NULL) {
770 pr_debug("problem processing MMAP event, skipping it.\n");
771 return -1;
772 }
773
774 if (!evsel->attr.sample_id_all) {
775 sample->cpu = 0;
776 sample->time = 0;
777 sample->tid = event->mmap.tid;
778 sample->pid = event->mmap.pid;
779 }
780 print_sample_start(sample, thread, evsel);
781 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300782 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900783 return 0;
784}
785
786static int process_mmap2_event(struct perf_tool *tool,
787 union perf_event *event,
788 struct perf_sample *sample,
789 struct machine *machine)
790{
791 struct thread *thread;
792 struct perf_script *script = container_of(tool, struct perf_script, tool);
793 struct perf_session *session = script->session;
Adrian Hunter06b234e2015-07-21 12:44:05 +0300794 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900795
796 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
797 return -1;
798
799 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
800 if (thread == NULL) {
801 pr_debug("problem processing MMAP2 event, skipping it.\n");
802 return -1;
803 }
804
805 if (!evsel->attr.sample_id_all) {
806 sample->cpu = 0;
807 sample->time = 0;
808 sample->tid = event->mmap2.tid;
809 sample->pid = event->mmap2.pid;
810 }
811 print_sample_start(sample, thread, evsel);
812 perf_event__fprintf(event, stdout);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300813 thread__put(thread);
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900814 return 0;
815}
816
Adrian Hunter7c148982015-07-21 12:44:06 +0300817static int process_switch_event(struct perf_tool *tool,
818 union perf_event *event,
819 struct perf_sample *sample,
820 struct machine *machine)
821{
822 struct thread *thread;
823 struct perf_script *script = container_of(tool, struct perf_script, tool);
824 struct perf_session *session = script->session;
825 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
826
827 if (perf_event__process_switch(tool, event, sample, machine) < 0)
828 return -1;
829
830 thread = machine__findnew_thread(machine, sample->pid,
831 sample->tid);
832 if (thread == NULL) {
833 pr_debug("problem processing SWITCH event, skipping it.\n");
834 return -1;
835 }
836
837 print_sample_start(sample, thread, evsel);
838 perf_event__fprintf(event, stdout);
839 thread__put(thread);
840 return 0;
841}
842
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300843static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500844{
845 session_done = 1;
846}
847
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300848static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200849{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200850 int ret;
851
Tom Zanussic239da32010-04-01 23:59:18 -0500852 signal(SIGINT, sig_handler);
853
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900854 /* override event processing functions */
855 if (script->show_task_events) {
856 script->tool.comm = process_comm_event;
857 script->tool.fork = process_fork_event;
858 script->tool.exit = process_exit_event;
859 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900860 if (script->show_mmap_events) {
861 script->tool.mmap = process_mmap_event;
862 script->tool.mmap2 = process_mmap2_event;
863 }
Adrian Hunter7c148982015-07-21 12:44:06 +0300864 if (script->show_switch_events)
865 script->tool.context_switch = process_switch_event;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900866
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300867 ret = perf_session__process_events(script->session);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200868
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200869 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200870 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200871
872 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200873}
874
Tom Zanussi956ffd02009-11-25 01:15:46 -0600875struct script_spec {
876 struct list_head node;
877 struct scripting_ops *ops;
878 char spec[0];
879};
880
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200881static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600882
883static struct script_spec *script_spec__new(const char *spec,
884 struct scripting_ops *ops)
885{
886 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
887
888 if (s != NULL) {
889 strcpy(s->spec, spec);
890 s->ops = ops;
891 }
892
893 return s;
894}
895
Tom Zanussi956ffd02009-11-25 01:15:46 -0600896static void script_spec__add(struct script_spec *s)
897{
898 list_add_tail(&s->node, &script_specs);
899}
900
901static struct script_spec *script_spec__find(const char *spec)
902{
903 struct script_spec *s;
904
905 list_for_each_entry(s, &script_specs, node)
906 if (strcasecmp(s->spec, spec) == 0)
907 return s;
908 return NULL;
909}
910
911static struct script_spec *script_spec__findnew(const char *spec,
912 struct scripting_ops *ops)
913{
914 struct script_spec *s = script_spec__find(spec);
915
916 if (s)
917 return s;
918
919 s = script_spec__new(spec, ops);
920 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +0900921 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600922
923 script_spec__add(s);
924
925 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600926}
927
928int script_spec_register(const char *spec, struct scripting_ops *ops)
929{
930 struct script_spec *s;
931
932 s = script_spec__find(spec);
933 if (s)
934 return -1;
935
936 s = script_spec__findnew(spec, ops);
937 if (!s)
938 return -1;
939
940 return 0;
941}
942
943static struct scripting_ops *script_spec__lookup(const char *spec)
944{
945 struct script_spec *s = script_spec__find(spec);
946 if (!s)
947 return NULL;
948
949 return s->ops;
950}
951
952static void list_available_languages(void)
953{
954 struct script_spec *s;
955
956 fprintf(stderr, "\n");
957 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100958 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600959
960 list_for_each_entry(s, &script_specs, node)
961 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
962
963 fprintf(stderr, "\n");
964}
965
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300966static int parse_scriptname(const struct option *opt __maybe_unused,
967 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600968{
969 char spec[PATH_MAX];
970 const char *script, *ext;
971 int len;
972
Tom Zanussif526d682010-01-27 02:27:52 -0600973 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600974 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600975 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600976 }
977
978 script = strchr(str, ':');
979 if (script) {
980 len = script - str;
981 if (len >= PATH_MAX) {
982 fprintf(stderr, "invalid language specifier");
983 return -1;
984 }
985 strncpy(spec, str, len);
986 spec[len] = '\0';
987 scripting_ops = script_spec__lookup(spec);
988 if (!scripting_ops) {
989 fprintf(stderr, "invalid language specifier");
990 return -1;
991 }
992 script++;
993 } else {
994 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100995 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600996 if (!ext) {
997 fprintf(stderr, "invalid script extension");
998 return -1;
999 }
1000 scripting_ops = script_spec__lookup(++ext);
1001 if (!scripting_ops) {
1002 fprintf(stderr, "invalid script extension");
1003 return -1;
1004 }
1005 }
1006
1007 script_name = strdup(script);
1008
1009 return 0;
1010}
1011
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001012static int parse_output_fields(const struct option *opt __maybe_unused,
1013 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -07001014{
1015 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -05001016 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -06001017 int j;
David Ahern745f43e2011-03-09 22:23:26 -07001018 int rc = 0;
1019 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -07001020 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -07001021
1022 if (!str)
1023 return -ENOMEM;
1024
David Ahern2c9e45f72011-03-17 10:03:21 -06001025 /* first word can state for which event type the user is specifying
1026 * the fields. If no type exists, the specified fields apply to all
1027 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -07001028 */
David Ahern2c9e45f72011-03-17 10:03:21 -06001029 tok = strchr(str, ':');
1030 if (tok) {
1031 *tok = '\0';
1032 tok++;
1033 if (!strcmp(str, "hw"))
1034 type = PERF_TYPE_HARDWARE;
1035 else if (!strcmp(str, "sw"))
1036 type = PERF_TYPE_SOFTWARE;
1037 else if (!strcmp(str, "trace"))
1038 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -07001039 else if (!strcmp(str, "raw"))
1040 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -06001041 else {
1042 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +01001043 rc = -EINVAL;
1044 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -06001045 }
1046
1047 if (output[type].user_set)
1048 pr_warning("Overriding previous field request for %s events.\n",
1049 event_type(type));
1050
1051 output[type].fields = 0;
1052 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001053 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -06001054
1055 } else {
1056 tok = str;
1057 if (strlen(str) == 0) {
1058 fprintf(stderr,
1059 "Cannot set fields to 'none' for all event types.\n");
1060 rc = -EINVAL;
1061 goto out;
1062 }
1063
1064 if (output_set_by_user())
1065 pr_warning("Overriding previous field request for all events.\n");
1066
1067 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1068 output[j].fields = 0;
1069 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -06001070 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -06001071 }
David Ahern1424dc92011-03-09 22:23:28 -07001072 }
1073
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001074 for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
David Ahern745f43e2011-03-09 22:23:26 -07001075 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001076 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -07001077 break;
David Ahern745f43e2011-03-09 22:23:26 -07001078 }
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001079 if (i == imax && strcmp(tok, "flags") == 0) {
1080 print_flags = true;
1081 continue;
1082 }
David Ahern745f43e2011-03-09 22:23:26 -07001083 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001084 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001085 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001086 goto out;
1087 }
1088
1089 if (type == -1) {
1090 /* add user option to all events types for
1091 * which it is valid
1092 */
1093 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1094 if (output[j].invalid_fields & all_output_options[i].field) {
1095 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1096 all_output_options[i].str, event_type(j));
1097 } else
1098 output[j].fields |= all_output_options[i].field;
1099 }
1100 } else {
1101 if (output[type].invalid_fields & all_output_options[i].field) {
1102 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1103 all_output_options[i].str, event_type(type));
1104
1105 rc = -EINVAL;
1106 goto out;
1107 }
1108 output[type].fields |= all_output_options[i].field;
1109 }
David Ahern2c9e45f72011-03-17 10:03:21 -06001110 }
1111
1112 if (type >= 0) {
1113 if (output[type].fields == 0) {
1114 pr_debug("No fields requested for %s type. "
1115 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001116 }
David Ahern1424dc92011-03-09 22:23:28 -07001117 }
David Ahern745f43e2011-03-09 22:23:26 -07001118
David Ahern2c9e45f72011-03-17 10:03:21 -06001119out:
David Ahern745f43e2011-03-09 22:23:26 -07001120 free(str);
1121 return rc;
1122}
1123
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001124/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1125static int is_directory(const char *base_path, const struct dirent *dent)
1126{
1127 char path[PATH_MAX];
1128 struct stat st;
1129
1130 sprintf(path, "%s/%s", base_path, dent->d_name);
1131 if (stat(path, &st))
1132 return 0;
1133
1134 return S_ISDIR(st.st_mode);
1135}
1136
1137#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001138 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1139 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001140 if ((lang_dirent.d_type == DT_DIR || \
1141 (lang_dirent.d_type == DT_UNKNOWN && \
1142 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001143 (strcmp(lang_dirent.d_name, ".")) && \
1144 (strcmp(lang_dirent.d_name, "..")))
1145
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001146#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001147 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1148 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001149 if (script_dirent.d_type != DT_DIR && \
1150 (script_dirent.d_type != DT_UNKNOWN || \
1151 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001152
1153
1154#define RECORD_SUFFIX "-record"
1155#define REPORT_SUFFIX "-report"
1156
1157struct script_desc {
1158 struct list_head node;
1159 char *name;
1160 char *half_liner;
1161 char *args;
1162};
1163
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001164static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001165
1166static struct script_desc *script_desc__new(const char *name)
1167{
1168 struct script_desc *s = zalloc(sizeof(*s));
1169
Tom Zanussib5b87312010-11-10 08:16:51 -06001170 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001171 s->name = strdup(name);
1172
1173 return s;
1174}
1175
1176static void script_desc__delete(struct script_desc *s)
1177{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001178 zfree(&s->name);
1179 zfree(&s->half_liner);
1180 zfree(&s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001181 free(s);
1182}
1183
1184static void script_desc__add(struct script_desc *s)
1185{
1186 list_add_tail(&s->node, &script_descs);
1187}
1188
1189static struct script_desc *script_desc__find(const char *name)
1190{
1191 struct script_desc *s;
1192
1193 list_for_each_entry(s, &script_descs, node)
1194 if (strcasecmp(s->name, name) == 0)
1195 return s;
1196 return NULL;
1197}
1198
1199static struct script_desc *script_desc__findnew(const char *name)
1200{
1201 struct script_desc *s = script_desc__find(name);
1202
1203 if (s)
1204 return s;
1205
1206 s = script_desc__new(name);
1207 if (!s)
1208 goto out_delete_desc;
1209
1210 script_desc__add(s);
1211
1212 return s;
1213
1214out_delete_desc:
1215 script_desc__delete(s);
1216
1217 return NULL;
1218}
1219
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001220static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001221{
1222 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001223 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001224
1225 if (strlen(str) > suffix_len) {
1226 p = str + strlen(str) - suffix_len;
1227 if (!strncmp(p, suffix, suffix_len))
1228 return p;
1229 }
1230
1231 return NULL;
1232}
1233
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001234static int read_script_info(struct script_desc *desc, const char *filename)
1235{
1236 char line[BUFSIZ], *p;
1237 FILE *fp;
1238
1239 fp = fopen(filename, "r");
1240 if (!fp)
1241 return -1;
1242
1243 while (fgets(line, sizeof(line), fp)) {
1244 p = ltrim(line);
1245 if (strlen(p) == 0)
1246 continue;
1247 if (*p != '#')
1248 continue;
1249 p++;
1250 if (strlen(p) && *p == '!')
1251 continue;
1252
1253 p = ltrim(p);
1254 if (strlen(p) && p[strlen(p) - 1] == '\n')
1255 p[strlen(p) - 1] = '\0';
1256
1257 if (!strncmp(p, "description:", strlen("description:"))) {
1258 p += strlen("description:");
1259 desc->half_liner = strdup(ltrim(p));
1260 continue;
1261 }
1262
1263 if (!strncmp(p, "args:", strlen("args:"))) {
1264 p += strlen("args:");
1265 desc->args = strdup(ltrim(p));
1266 continue;
1267 }
1268 }
1269
1270 fclose(fp);
1271
1272 return 0;
1273}
1274
Robert Richter38efb532011-11-25 11:38:40 +01001275static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1276{
1277 char *script_root, *str;
1278
1279 script_root = strdup(script_dirent->d_name);
1280 if (!script_root)
1281 return NULL;
1282
1283 str = (char *)ends_with(script_root, suffix);
1284 if (!str) {
1285 free(script_root);
1286 return NULL;
1287 }
1288
1289 *str = '\0';
1290 return script_root;
1291}
1292
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001293static int list_available_scripts(const struct option *opt __maybe_unused,
1294 const char *s __maybe_unused,
1295 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001296{
1297 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1298 char scripts_path[MAXPATHLEN];
1299 DIR *scripts_dir, *lang_dir;
1300 char script_path[MAXPATHLEN];
1301 char lang_path[MAXPATHLEN];
1302 struct script_desc *desc;
1303 char first_half[BUFSIZ];
1304 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001305
1306 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1307
1308 scripts_dir = opendir(scripts_path);
1309 if (!scripts_dir)
1310 return -1;
1311
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001312 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001313 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1314 lang_dirent.d_name);
1315 lang_dir = opendir(lang_path);
1316 if (!lang_dir)
1317 continue;
1318
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001319 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001320 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1321 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001322 desc = script_desc__findnew(script_root);
1323 snprintf(script_path, MAXPATHLEN, "%s/%s",
1324 lang_path, script_dirent.d_name);
1325 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001326 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001327 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001328 }
1329 }
1330
1331 fprintf(stdout, "List of available trace scripts:\n");
1332 list_for_each_entry(desc, &script_descs, node) {
1333 sprintf(first_half, "%s %s", desc->name,
1334 desc->args ? desc->args : "");
1335 fprintf(stdout, " %-36s %s\n", first_half,
1336 desc->half_liner ? desc->half_liner : "");
1337 }
1338
1339 exit(0);
1340}
1341
Feng Tange5f37052012-09-07 16:42:26 +08001342/*
Feng Tang49e639e2012-10-30 11:56:03 +08001343 * Some scripts specify the required events in their "xxx-record" file,
1344 * this function will check if the events in perf.data match those
1345 * mentioned in the "xxx-record".
1346 *
1347 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1348 * which is covered well now. And new parsing code should be added to
1349 * cover the future complexing formats like event groups etc.
1350 */
1351static int check_ev_match(char *dir_name, char *scriptname,
1352 struct perf_session *session)
1353{
1354 char filename[MAXPATHLEN], evname[128];
1355 char line[BUFSIZ], *p;
1356 struct perf_evsel *pos;
1357 int match, len;
1358 FILE *fp;
1359
1360 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1361
1362 fp = fopen(filename, "r");
1363 if (!fp)
1364 return -1;
1365
1366 while (fgets(line, sizeof(line), fp)) {
1367 p = ltrim(line);
1368 if (*p == '#')
1369 continue;
1370
1371 while (strlen(p)) {
1372 p = strstr(p, "-e");
1373 if (!p)
1374 break;
1375
1376 p += 2;
1377 p = ltrim(p);
1378 len = strcspn(p, " \t");
1379 if (!len)
1380 break;
1381
1382 snprintf(evname, len + 1, "%s", p);
1383
1384 match = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001385 evlist__for_each(session->evlist, pos) {
Feng Tang49e639e2012-10-30 11:56:03 +08001386 if (!strcmp(perf_evsel__name(pos), evname)) {
1387 match = 1;
1388 break;
1389 }
1390 }
1391
1392 if (!match) {
1393 fclose(fp);
1394 return -1;
1395 }
1396 }
1397 }
1398
1399 fclose(fp);
1400 return 0;
1401}
1402
1403/*
Feng Tange5f37052012-09-07 16:42:26 +08001404 * Return -1 if none is found, otherwise the actual scripts number.
1405 *
1406 * Currently the only user of this function is the script browser, which
1407 * will list all statically runnable scripts, select one, execute it and
1408 * show the output in a perf browser.
1409 */
1410int find_scripts(char **scripts_array, char **scripts_path_array)
1411{
1412 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001413 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001414 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001415 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001416 struct perf_data_file file = {
1417 .path = input_name,
1418 .mode = PERF_DATA_MODE_READ,
1419 };
Feng Tange5f37052012-09-07 16:42:26 +08001420 char *temp;
1421 int i = 0;
1422
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001423 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001424 if (!session)
1425 return -1;
1426
Feng Tange5f37052012-09-07 16:42:26 +08001427 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1428
1429 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001430 if (!scripts_dir) {
1431 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001432 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001433 }
Feng Tange5f37052012-09-07 16:42:26 +08001434
1435 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1436 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1437 lang_dirent.d_name);
1438#ifdef NO_LIBPERL
1439 if (strstr(lang_path, "perl"))
1440 continue;
1441#endif
1442#ifdef NO_LIBPYTHON
1443 if (strstr(lang_path, "python"))
1444 continue;
1445#endif
1446
1447 lang_dir = opendir(lang_path);
1448 if (!lang_dir)
1449 continue;
1450
1451 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1452 /* Skip those real time scripts: xxxtop.p[yl] */
1453 if (strstr(script_dirent.d_name, "top."))
1454 continue;
1455 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1456 script_dirent.d_name);
1457 temp = strchr(script_dirent.d_name, '.');
1458 snprintf(scripts_array[i],
1459 (temp - script_dirent.d_name) + 1,
1460 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001461
1462 if (check_ev_match(lang_path,
1463 scripts_array[i], session))
1464 continue;
1465
Feng Tange5f37052012-09-07 16:42:26 +08001466 i++;
1467 }
Feng Tang49e639e2012-10-30 11:56:03 +08001468 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001469 }
1470
Feng Tang49e639e2012-10-30 11:56:03 +08001471 closedir(scripts_dir);
1472 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001473 return i;
1474}
1475
Tom Zanussi38752942009-12-15 02:53:39 -06001476static char *get_script_path(const char *script_root, const char *suffix)
1477{
1478 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1479 char scripts_path[MAXPATHLEN];
1480 char script_path[MAXPATHLEN];
1481 DIR *scripts_dir, *lang_dir;
1482 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001483 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001484
1485 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1486
1487 scripts_dir = opendir(scripts_path);
1488 if (!scripts_dir)
1489 return NULL;
1490
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001491 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001492 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1493 lang_dirent.d_name);
1494 lang_dir = opendir(lang_path);
1495 if (!lang_dir)
1496 continue;
1497
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001498 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001499 __script_root = get_script_root(&script_dirent, suffix);
1500 if (__script_root && !strcmp(script_root, __script_root)) {
1501 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001502 closedir(lang_dir);
1503 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001504 snprintf(script_path, MAXPATHLEN, "%s/%s",
1505 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001506 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001507 }
1508 free(__script_root);
1509 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001510 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001511 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001512 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001513
Robert Richter38efb532011-11-25 11:38:40 +01001514 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001515}
1516
Tom Zanussib5b87312010-11-10 08:16:51 -06001517static bool is_top_script(const char *script_path)
1518{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001519 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001520}
1521
1522static int has_required_arg(char *script_path)
1523{
1524 struct script_desc *desc;
1525 int n_args = 0;
1526 char *p;
1527
1528 desc = script_desc__new(NULL);
1529
1530 if (read_script_info(desc, script_path))
1531 goto out;
1532
1533 if (!desc->args)
1534 goto out;
1535
1536 for (p = desc->args; *p; p++)
1537 if (*p == '<')
1538 n_args++;
1539out:
1540 script_desc__delete(desc);
1541
1542 return n_args;
1543}
1544
David Ahernd54b1a92012-08-26 12:24:46 -06001545static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001546{
1547 char **__argv = malloc(sizeof(const char *) * argc);
1548
David Ahernd54b1a92012-08-26 12:24:46 -06001549 if (!__argv) {
1550 pr_err("malloc failed\n");
1551 return -1;
1552 }
1553
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001554 memcpy(__argv, argv, sizeof(const char *) * argc);
1555 argc = parse_options(argc, (const char **)__argv, record_options,
1556 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1557 free(__argv);
1558
David Ahernd54b1a92012-08-26 12:24:46 -06001559 system_wide = (argc == 0);
1560
1561 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001562}
1563
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001564int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001565{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001566 bool show_full_info = false;
Jiri Olsae90debd2013-12-09 11:02:50 +01001567 bool header = false;
1568 bool header_only = false;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001569 bool script_started = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001570 char *rec_script_path = NULL;
1571 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001572 struct perf_session *session;
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001573 struct itrace_synth_opts itrace_synth_opts = { .set = false, };
Tom Zanussib5b87312010-11-10 08:16:51 -06001574 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001575 const char **__argv;
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001576 int i, j, err = 0;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001577 struct perf_script script = {
1578 .tool = {
1579 .sample = process_sample_event,
1580 .mmap = perf_event__process_mmap,
1581 .mmap2 = perf_event__process_mmap2,
1582 .comm = perf_event__process_comm,
1583 .exit = perf_event__process_exit,
1584 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001585 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001586 .tracing_data = perf_event__process_tracing_data,
1587 .build_id = perf_event__process_build_id,
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001588 .id_index = perf_event__process_id_index,
1589 .auxtrace_info = perf_event__process_auxtrace_info,
1590 .auxtrace = perf_event__process_auxtrace,
1591 .auxtrace_error = perf_event__process_auxtrace_error,
Jiri Olsa0a8cb852014-07-06 14:18:21 +02001592 .ordered_events = true,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001593 .ordering_requires_timestamps = true,
1594 },
1595 };
Yunlong Song06af0f22015-04-02 21:47:16 +08001596 struct perf_data_file file = {
1597 .mode = PERF_DATA_MODE_READ,
1598 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001599 const struct option options[] = {
1600 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1601 "dump raw trace in ASCII"),
1602 OPT_INCR('v', "verbose", &verbose,
1603 "be more verbose (show symbol address, etc)"),
1604 OPT_BOOLEAN('L', "Latency", &latency_format,
1605 "show latency attributes (irqs/preemption disabled, etc)"),
1606 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1607 list_available_scripts),
1608 OPT_CALLBACK('s', "script", NULL, "name",
1609 "script file name (lang:script name, script name, or *)",
1610 parse_scriptname),
1611 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1612 "generate perf-script.xx script in specified language"),
1613 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1614 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1615 "do various checks like samples ordering and lost events"),
Jiri Olsae90debd2013-12-09 11:02:50 +01001616 OPT_BOOLEAN(0, "header", &header, "Show data header."),
1617 OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001618 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1619 "file", "vmlinux pathname"),
1620 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1621 "file", "kallsyms pathname"),
1622 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1623 "When printing symbols do not display call chain"),
1624 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1625 "Look for files with symbols relative to this directory"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001626 OPT_CALLBACK('F', "fields", NULL, "str",
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001627 "comma separated output fields prepend with 'type:'. "
1628 "Valid types: hw,sw,trace,raw. "
1629 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
Adrian Hunter400ea6d2015-04-09 18:54:05 +03001630 "addr,symoff,period,flags", parse_output_fields),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001631 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1632 "system-wide collection from all CPUs"),
1633 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1634 "only consider these symbols"),
1635 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1636 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1637 "only display events for these comms"),
David Aherne03eaa42015-03-24 09:52:41 -06001638 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1639 "only consider symbols in these pids"),
1640 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1641 "only consider symbols in these tids"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001642 OPT_BOOLEAN('I', "show-info", &show_full_info,
1643 "display extended information from perf.data file"),
1644 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1645 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001646 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1647 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001648 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1649 "Show the mmap events"),
Adrian Hunter7c148982015-07-21 12:44:06 +03001650 OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
1651 "Show context switch events (if recorded)"),
Yunlong Song06af0f22015-04-02 21:47:16 +08001652 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001653 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1654 "Instruction Tracing options",
1655 itrace_parse_synth_opts),
Andi Kleena9710ba2015-08-07 15:24:05 -07001656 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1657 "Show full source file name path for source lines"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001658 OPT_END()
1659 };
Yunlong Song40cae2b2015-03-18 21:35:54 +08001660 const char * const script_subcommands[] = { "record", "report", NULL };
1661 const char *script_usage[] = {
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001662 "perf script [<options>]",
1663 "perf script [<options>] record <script> [<record-options>] <command>",
1664 "perf script [<options>] report <script> [script-args]",
1665 "perf script [<options>] <script> [<record-options>] <command>",
1666 "perf script [<options>] <top-script> [script-args]",
1667 NULL
1668 };
Tom Zanussi38752942009-12-15 02:53:39 -06001669
Tom Zanussib5b87312010-11-10 08:16:51 -06001670 setup_scripting();
1671
Yunlong Song40cae2b2015-03-18 21:35:54 +08001672 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001673 PARSE_OPT_STOP_AT_NON_OPTION);
1674
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001675 file.path = input_name;
1676
Tom Zanussib5b87312010-11-10 08:16:51 -06001677 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1678 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1679 if (!rec_script_path)
1680 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001681 }
1682
Tom Zanussib5b87312010-11-10 08:16:51 -06001683 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1684 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1685 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001686 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001687 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001688 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001689 return -1;
1690 }
Tom Zanussi38752942009-12-15 02:53:39 -06001691 }
1692
Ben Hutchings44e668c2010-10-10 16:10:03 +01001693 /* make sure PERF_EXEC_PATH is set for scripts */
1694 perf_set_argv_exec_path(perf_exec_path());
1695
Tom Zanussib5b87312010-11-10 08:16:51 -06001696 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001697 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001698 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001699 pid_t pid;
1700
Tom Zanussib5b87312010-11-10 08:16:51 -06001701 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1702 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1703
1704 if (!rec_script_path && !rep_script_path) {
1705 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001706 " script -l for available scripts.\n", argv[0]);
1707 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001708 }
1709
Tom Zanussib5b87312010-11-10 08:16:51 -06001710 if (is_top_script(argv[0])) {
1711 rep_args = argc - 1;
1712 } else {
1713 int rec_args;
1714
1715 rep_args = has_required_arg(rep_script_path);
1716 rec_args = (argc - 1) - rep_args;
1717 if (rec_args < 0) {
1718 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001719 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001720 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001721 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001722 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001723 }
1724
1725 if (pipe(live_pipe) < 0) {
1726 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001727 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001728 }
1729
1730 pid = fork();
1731 if (pid < 0) {
1732 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001733 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001734 }
1735
1736 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001737 j = 0;
1738
Tom Zanussia0cccc22010-04-01 23:59:25 -05001739 dup2(live_pipe[1], 1);
1740 close(live_pipe[0]);
1741
Robert Richter317df652011-11-25 15:05:25 +01001742 if (is_top_script(argv[0])) {
1743 system_wide = true;
1744 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001745 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1746 err = -1;
1747 goto out;
1748 }
Robert Richter317df652011-11-25 15:05:25 +01001749 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001750
1751 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001752 if (!__argv) {
1753 pr_err("malloc failed\n");
1754 err = -ENOMEM;
1755 goto out;
1756 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001757
Tom Zanussib5b87312010-11-10 08:16:51 -06001758 __argv[j++] = "/bin/sh";
1759 __argv[j++] = rec_script_path;
1760 if (system_wide)
1761 __argv[j++] = "-a";
1762 __argv[j++] = "-q";
1763 __argv[j++] = "-o";
1764 __argv[j++] = "-";
1765 for (i = rep_args + 1; i < argc; i++)
1766 __argv[j++] = argv[i];
1767 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001768
1769 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001770 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001771 exit(-1);
1772 }
1773
1774 dup2(live_pipe[0], 0);
1775 close(live_pipe[1]);
1776
Tom Zanussib5b87312010-11-10 08:16:51 -06001777 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001778 if (!__argv) {
1779 pr_err("malloc failed\n");
1780 err = -ENOMEM;
1781 goto out;
1782 }
1783
Tom Zanussib5b87312010-11-10 08:16:51 -06001784 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001785 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001786 __argv[j++] = rep_script_path;
1787 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001788 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001789 __argv[j++] = "-i";
1790 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001791 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001792
1793 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001794 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001795 exit(-1);
1796 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001797
Tom Zanussib5b87312010-11-10 08:16:51 -06001798 if (rec_script_path)
1799 script_path = rec_script_path;
1800 if (rep_script_path)
1801 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001802
Tom Zanussib5b87312010-11-10 08:16:51 -06001803 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001804 j = 0;
1805
Robert Richter317df652011-11-25 15:05:25 +01001806 if (!rec_script_path)
1807 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001808 else if (!system_wide) {
1809 if (have_cmd(argc - 1, &argv[1]) != 0) {
1810 err = -1;
1811 goto out;
1812 }
1813 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001814
1815 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001816 if (!__argv) {
1817 pr_err("malloc failed\n");
1818 err = -ENOMEM;
1819 goto out;
1820 }
1821
Tom Zanussib5b87312010-11-10 08:16:51 -06001822 __argv[j++] = "/bin/sh";
1823 __argv[j++] = script_path;
1824 if (system_wide)
1825 __argv[j++] = "-a";
1826 for (i = 2; i < argc; i++)
1827 __argv[j++] = argv[i];
1828 __argv[j++] = NULL;
1829
1830 execvp("/bin/sh", (char **)__argv);
1831 free(__argv);
1832 exit(-1);
1833 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001834
Tom Zanussicf4fee52010-03-03 01:04:33 -06001835 if (!script_name)
1836 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001837
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001838 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001839 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09001840 return -1;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001841
Jiri Olsae90debd2013-12-09 11:02:50 +01001842 if (header || header_only) {
1843 perf_session__fprintf_info(session, stdout, show_full_info);
1844 if (header_only)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001845 goto out_delete;
Jiri Olsae90debd2013-12-09 11:02:50 +01001846 }
1847
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001848 if (symbol__init(&session->header.env) < 0)
Namhyung Kim38520dc2014-08-12 15:40:42 +09001849 goto out_delete;
1850
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001851 script.session = session;
1852
Adrian Hunter7a680eb2015-04-09 18:53:56 +03001853 session->itrace_synth_opts = &itrace_synth_opts;
1854
Anton Blanchard5d67be92011-07-04 21:57:50 +10001855 if (cpu_list) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001856 err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
1857 if (err < 0)
1858 goto out_delete;
Anton Blanchard5d67be92011-07-04 21:57:50 +10001859 }
1860
David Ahern1424dc92011-03-09 22:23:28 -07001861 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001862 symbol_conf.use_callchain = true;
1863 else
1864 symbol_conf.use_callchain = false;
1865
Arnaldo Carvalho de Melo9ee67422015-08-03 16:27:40 -03001866 if (session->tevent.pevent &&
1867 pevent_set_function_resolver(session->tevent.pevent,
Arnaldo Carvalho de Meloccb3a822015-07-22 16:43:37 -03001868 machine__resolve_kernel_addr,
1869 &session->machines.host) < 0) {
1870 pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
1871 return -1;
1872 }
1873
Tom Zanussi956ffd02009-11-25 01:15:46 -06001874 if (generate_script_lang) {
1875 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001876 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001877
David Ahern2c9e45f72011-03-17 10:03:21 -06001878 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001879 fprintf(stderr,
1880 "custom fields not supported for generated scripts");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001881 err = -EINVAL;
1882 goto out_delete;
David Ahern745f43e2011-03-09 22:23:26 -07001883 }
1884
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001885 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06001886 if (input < 0) {
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001887 err = -errno;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001888 perror("failed to open file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001889 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001890 }
1891
1892 err = fstat(input, &perf_stat);
1893 if (err < 0) {
1894 perror("failed to stat file");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001895 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001896 }
1897
1898 if (!perf_stat.st_size) {
1899 fprintf(stderr, "zero-sized file, nothing to do!\n");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001900 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001901 }
1902
1903 scripting_ops = script_spec__lookup(generate_script_lang);
1904 if (!scripting_ops) {
1905 fprintf(stderr, "invalid language specifier");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001906 err = -ENOENT;
1907 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001908 }
1909
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01001910 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001911 "perf-script");
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001912 goto out_delete;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001913 }
1914
1915 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001916 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001917 if (err)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001918 goto out_delete;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001919 pr_debug("perf script started with script %s\n\n", script_name);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001920 script_started = true;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001921 }
1922
David Ahern9cbdb702011-04-06 21:54:20 -06001923
1924 err = perf_session__check_output_opt(session);
1925 if (err < 0)
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001926 goto out_delete;
David Ahern9cbdb702011-04-06 21:54:20 -06001927
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001928 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001929
Adrian Hunterd445dd22014-08-15 22:08:37 +03001930 flush_scripting();
1931
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001932out_delete:
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001933 perf_session__delete(session);
Namhyung Kim6cc870f2014-08-12 15:40:33 +09001934
1935 if (script_started)
1936 cleanup_scripting();
Tom Zanussi956ffd02009-11-25 01:15:46 -06001937out:
1938 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001939}