blob: 0ae88c2538a1abb82fa9ef32e71ccc4474851872 [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"
Anton Blanchard5d67be92011-07-04 21:57:50 +100019#include <linux/bitmap.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020020
Tom Zanussi956ffd02009-11-25 01:15:46 -060021static char const *script_name;
22static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020023static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020024static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020025static u64 nr_unordered;
Tom Zanussi34c86ea2010-11-10 08:15:43 -060026extern const struct option record_options[];
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;
Anton Blanchard5d67be92011-07-04 21:57:50 +100030static const char *cpu_list;
31static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Tom Zanussi956ffd02009-11-25 01:15:46 -060032
David Ahern745f43e2011-03-09 22:23:26 -070033enum perf_output_field {
34 PERF_OUTPUT_COMM = 1U << 0,
35 PERF_OUTPUT_TID = 1U << 1,
36 PERF_OUTPUT_PID = 1U << 2,
37 PERF_OUTPUT_TIME = 1U << 3,
38 PERF_OUTPUT_CPU = 1U << 4,
39 PERF_OUTPUT_EVNAME = 1U << 5,
40 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060041 PERF_OUTPUT_IP = 1U << 7,
42 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060043 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060044 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090045 PERF_OUTPUT_SYMOFFSET = 1U << 11,
David Ahern745f43e2011-03-09 22:23:26 -070046};
47
48struct output_option {
49 const char *str;
50 enum perf_output_field field;
51} all_output_options[] = {
52 {.str = "comm", .field = PERF_OUTPUT_COMM},
53 {.str = "tid", .field = PERF_OUTPUT_TID},
54 {.str = "pid", .field = PERF_OUTPUT_PID},
55 {.str = "time", .field = PERF_OUTPUT_TIME},
56 {.str = "cpu", .field = PERF_OUTPUT_CPU},
57 {.str = "event", .field = PERF_OUTPUT_EVNAME},
58 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060059 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070060 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060061 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060062 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090063 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
David Ahern745f43e2011-03-09 22:23:26 -070064};
65
66/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060067static struct {
68 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060069 bool wildcard_set;
David Aherna6ffaf92013-08-07 22:50:51 -040070 unsigned int print_ip_opts;
David Ahern2c9e45f72011-03-17 10:03:21 -060071 u64 fields;
72 u64 invalid_fields;
73} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070074
David Ahern2c9e45f72011-03-17 10:03:21 -060075 [PERF_TYPE_HARDWARE] = {
76 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070077
David Ahern2c9e45f72011-03-17 10:03:21 -060078 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
79 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060080 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -060081 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
David Ahern2c9e45f72011-03-17 10:03:21 -060082
83 .invalid_fields = PERF_OUTPUT_TRACE,
84 },
85
86 [PERF_TYPE_SOFTWARE] = {
87 .user_set = false,
88
89 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
90 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060091 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -060092 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
David Ahern2c9e45f72011-03-17 10:03:21 -060093
94 .invalid_fields = PERF_OUTPUT_TRACE,
95 },
96
97 [PERF_TYPE_TRACEPOINT] = {
98 .user_set = false,
99
100 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
101 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
102 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
103 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700104
105 [PERF_TYPE_RAW] = {
106 .user_set = false,
107
108 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
109 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600110 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -0600111 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700112
113 .invalid_fields = PERF_OUTPUT_TRACE,
114 },
David Ahern1424dc92011-03-09 22:23:28 -0700115};
David Ahern745f43e2011-03-09 22:23:26 -0700116
David Ahern2c9e45f72011-03-17 10:03:21 -0600117static bool output_set_by_user(void)
118{
119 int j;
120 for (j = 0; j < PERF_TYPE_MAX; ++j) {
121 if (output[j].user_set)
122 return true;
123 }
124 return false;
125}
David Ahern745f43e2011-03-09 22:23:26 -0700126
David Ahern9cbdb702011-04-06 21:54:20 -0600127static const char *output_field2str(enum perf_output_field field)
128{
129 int i, imax = ARRAY_SIZE(all_output_options);
130 const char *str = "";
131
132 for (i = 0; i < imax; ++i) {
133 if (all_output_options[i].field == field) {
134 str = all_output_options[i].str;
135 break;
136 }
137 }
138 return str;
139}
140
David Ahern2c9e45f72011-03-17 10:03:21 -0600141#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700142
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300143static int perf_evsel__check_stype(struct perf_evsel *evsel,
144 u64 sample_type, const char *sample_msg,
145 enum perf_output_field field)
David Ahern1424dc92011-03-09 22:23:28 -0700146{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300147 struct perf_event_attr *attr = &evsel->attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600148 int type = attr->type;
149 const char *evname;
150
151 if (attr->sample_type & sample_type)
152 return 0;
153
154 if (output[type].user_set) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300155 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600156 pr_err("Samples for '%s' event do not have %s attribute set. "
157 "Cannot print '%s' field.\n",
158 evname, sample_msg, output_field2str(field));
159 return -1;
160 }
161
162 /* user did not ask for it explicitly so remove from the default list */
163 output[type].fields &= ~field;
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300164 evname = perf_evsel__name(evsel);
David Ahern9cbdb702011-04-06 21:54:20 -0600165 pr_debug("Samples for '%s' event do not have %s attribute set. "
166 "Skipping '%s' field.\n",
167 evname, sample_msg, output_field2str(field));
168
169 return 0;
170}
171
172static int perf_evsel__check_attr(struct perf_evsel *evsel,
173 struct perf_session *session)
174{
175 struct perf_event_attr *attr = &evsel->attr;
176
David Ahern1424dc92011-03-09 22:23:28 -0700177 if (PRINT_FIELD(TRACE) &&
178 !perf_session__has_traces(session, "record -R"))
179 return -EINVAL;
180
David Ahern787bef12011-05-27 14:28:43 -0600181 if (PRINT_FIELD(IP)) {
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300182 if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
183 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700184 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600185
David Ahern1424dc92011-03-09 22:23:28 -0700186 if (!no_callchain &&
David Ahern9cbdb702011-04-06 21:54:20 -0600187 !(attr->sample_type & PERF_SAMPLE_CALLCHAIN))
David Ahern1424dc92011-03-09 22:23:28 -0700188 symbol_conf.use_callchain = false;
189 }
David Ahern7cec0922011-05-30 13:08:23 -0600190
191 if (PRINT_FIELD(ADDR) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300192 perf_evsel__check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
193 PERF_OUTPUT_ADDR))
David Ahern7cec0922011-05-30 13:08:23 -0600194 return -EINVAL;
195
196 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
197 pr_err("Display of symbols requested but neither sample IP nor "
198 "sample address\nis selected. Hence, no addresses to convert "
199 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600200 return -EINVAL;
201 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900202 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
203 pr_err("Display of offsets requested but symbol is not"
204 "selected.\n");
205 return -EINVAL;
206 }
David Ahern7cec0922011-05-30 13:08:23 -0600207 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
208 pr_err("Display of DSO requested but neither sample IP nor "
209 "sample address\nis selected. Hence, no addresses to convert "
210 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600211 return -EINVAL;
212 }
David Ahern1424dc92011-03-09 22:23:28 -0700213
214 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300215 perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
216 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700217 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700218
219 if (PRINT_FIELD(TIME) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300220 perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
221 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700222 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700223
224 if (PRINT_FIELD(CPU) &&
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300225 perf_evsel__check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
226 PERF_OUTPUT_CPU))
David Ahern1424dc92011-03-09 22:23:28 -0700227 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600228
229 return 0;
230}
231
232/*
233 * verify all user requested events exist and the samples
234 * have the expected data
235 */
236static int perf_session__check_output_opt(struct perf_session *session)
237{
238 int j;
239 struct perf_evsel *evsel;
David Aherna6ffaf92013-08-07 22:50:51 -0400240 struct perf_event_attr *attr;
David Ahern9cbdb702011-04-06 21:54:20 -0600241
242 for (j = 0; j < PERF_TYPE_MAX; ++j) {
243 evsel = perf_session__find_first_evtype(session, j);
244
245 /*
246 * even if fields is set to 0 (ie., show nothing) event must
247 * exist if user explicitly includes it on the command line
248 */
249 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
250 pr_err("%s events do not exist. "
251 "Remove corresponding -f option to proceed.\n",
252 event_type(j));
253 return -1;
254 }
255
256 if (evsel && output[j].fields &&
257 perf_evsel__check_attr(evsel, session))
258 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400259
260 if (evsel == NULL)
261 continue;
262
263 attr = &evsel->attr;
264
265 output[j].print_ip_opts = 0;
266 if (PRINT_FIELD(IP))
267 output[j].print_ip_opts |= PRINT_IP_OPT_IP;
268
269 if (PRINT_FIELD(SYM))
270 output[j].print_ip_opts |= PRINT_IP_OPT_SYM;
271
272 if (PRINT_FIELD(DSO))
273 output[j].print_ip_opts |= PRINT_IP_OPT_DSO;
274
275 if (PRINT_FIELD(SYMOFFSET))
276 output[j].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
David Ahern1424dc92011-03-09 22:23:28 -0700277 }
278
279 return 0;
280}
David Ahern745f43e2011-03-09 22:23:26 -0700281
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300282static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700283 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300284 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700285{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300286 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700287 const char *evname = NULL;
288 unsigned long secs;
289 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700290 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700291
David Ahern745f43e2011-03-09 22:23:26 -0700292 if (PRINT_FIELD(COMM)) {
293 if (latency_format)
294 printf("%8.8s ", thread->comm);
David Ahern787bef12011-05-27 14:28:43 -0600295 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
David Ahernc0230b22011-03-09 22:23:27 -0700296 printf("%s ", thread->comm);
David Ahern745f43e2011-03-09 22:23:26 -0700297 else
298 printf("%16s ", thread->comm);
299 }
David Ahernc70c94b2011-03-09 22:23:25 -0700300
David Ahern745f43e2011-03-09 22:23:26 -0700301 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
302 printf("%5d/%-5d ", sample->pid, sample->tid);
303 else if (PRINT_FIELD(PID))
304 printf("%5d ", sample->pid);
305 else if (PRINT_FIELD(TID))
306 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700307
David Ahern745f43e2011-03-09 22:23:26 -0700308 if (PRINT_FIELD(CPU)) {
309 if (latency_format)
310 printf("%3d ", sample->cpu);
311 else
312 printf("[%03d] ", sample->cpu);
313 }
David Ahernc70c94b2011-03-09 22:23:25 -0700314
David Ahern745f43e2011-03-09 22:23:26 -0700315 if (PRINT_FIELD(TIME)) {
316 nsecs = sample->time;
317 secs = nsecs / NSECS_PER_SEC;
318 nsecs -= secs * NSECS_PER_SEC;
319 usecs = nsecs / NSECS_PER_USEC;
320 printf("%5lu.%06lu: ", secs, usecs);
321 }
322
323 if (PRINT_FIELD(EVNAME)) {
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300324 evname = perf_evsel__name(evsel);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900325 printf("%s: ", evname ? evname : "[unknown]");
David Ahern745f43e2011-03-09 22:23:26 -0700326 }
David Ahernc70c94b2011-03-09 22:23:25 -0700327}
328
Akihiro Nagai95582592012-01-30 13:43:09 +0900329static bool is_bts_event(struct perf_event_attr *attr)
330{
331 return ((attr->type == PERF_TYPE_HARDWARE) &&
332 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
333 (attr->sample_period == 1));
334}
335
David Ahern7cec0922011-05-30 13:08:23 -0600336static bool sample_addr_correlates_sym(struct perf_event_attr *attr)
337{
338 if ((attr->type == PERF_TYPE_SOFTWARE) &&
339 ((attr->config == PERF_COUNT_SW_PAGE_FAULTS) ||
340 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN) ||
341 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)))
342 return true;
343
Akihiro Nagai95582592012-01-30 13:43:09 +0900344 if (is_bts_event(attr))
345 return true;
346
David Ahern7cec0922011-05-30 13:08:23 -0600347 return false;
348}
349
350static void print_sample_addr(union perf_event *event,
351 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200352 struct machine *machine,
David Ahern7cec0922011-05-30 13:08:23 -0600353 struct thread *thread,
354 struct perf_event_attr *attr)
355{
356 struct addr_location al;
357 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
David Ahern7cec0922011-05-30 13:08:23 -0600358
359 printf("%16" PRIx64, sample->addr);
360
361 if (!sample_addr_correlates_sym(attr))
362 return;
363
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200364 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300365 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600366 if (!al.map)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200367 thread__find_addr_map(thread, machine, cpumode, MAP__VARIABLE,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300368 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600369
370 al.cpu = sample->cpu;
371 al.sym = NULL;
372
373 if (al.map)
374 al.sym = map__find_symbol(al.map, al.addr, NULL);
375
376 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900377 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900378 if (PRINT_FIELD(SYMOFFSET))
379 symbol__fprintf_symname_offs(al.sym, &al, stdout);
380 else
381 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600382 }
383
384 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900385 printf(" (");
386 map__fprintf_dsoname(al.map, stdout);
387 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600388 }
389}
390
Akihiro Nagai95582592012-01-30 13:43:09 +0900391static void print_sample_bts(union perf_event *event,
392 struct perf_sample *sample,
393 struct perf_evsel *evsel,
394 struct machine *machine,
395 struct thread *thread)
396{
397 struct perf_event_attr *attr = &evsel->attr;
398
399 /* print branch_from information */
400 if (PRINT_FIELD(IP)) {
401 if (!symbol_conf.use_callchain)
402 printf(" ");
403 else
404 printf("\n");
Jiri Olsa71ad0f52012-08-07 15:20:46 +0200405 perf_evsel__print_ip(evsel, event, sample, machine,
David Ahern307cbb92013-08-07 22:50:53 -0400406 output[attr->type].print_ip_opts,
407 PERF_MAX_STACK_DEPTH);
Akihiro Nagai95582592012-01-30 13:43:09 +0900408 }
409
410 printf(" => ");
411
412 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300413 if (PRINT_FIELD(ADDR) ||
414 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
415 !output[attr->type].user_set))
Akihiro Nagai95582592012-01-30 13:43:09 +0900416 print_sample_addr(event, sample, machine, thread, attr);
417
418 printf("\n");
419}
420
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300421static void process_event(union perf_event *event, struct perf_sample *sample,
422 struct perf_evsel *evsel, struct machine *machine,
David Ahern2eaa1b42013-07-18 16:06:15 -0600423 struct thread *thread,
424 struct addr_location *al __maybe_unused)
David Ahernbe6d8422011-03-09 22:23:23 -0700425{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300426 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700427
David Ahern2c9e45f72011-03-17 10:03:21 -0600428 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700429 return;
430
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300431 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700432
Akihiro Nagai95582592012-01-30 13:43:09 +0900433 if (is_bts_event(attr)) {
434 print_sample_bts(event, sample, evsel, machine, thread);
435 return;
436 }
437
David Ahern745f43e2011-03-09 22:23:26 -0700438 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300439 event_format__print(evsel->tp_format, sample->cpu,
440 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600441 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200442 print_sample_addr(event, sample, machine, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600443
David Ahern787bef12011-05-27 14:28:43 -0600444 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700445 if (!symbol_conf.use_callchain)
446 printf(" ");
447 else
448 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400449
Jiri Olsa71ad0f52012-08-07 15:20:46 +0200450 perf_evsel__print_ip(evsel, event, sample, machine,
David Ahern307cbb92013-08-07 22:50:53 -0400451 output[attr->type].print_ip_opts,
452 PERF_MAX_STACK_DEPTH);
David Ahernc0230b22011-03-09 22:23:27 -0700453 }
454
David Ahernc70c94b2011-03-09 22:23:25 -0700455 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700456}
457
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300458static int default_start_script(const char *script __maybe_unused,
459 int argc __maybe_unused,
460 const char **argv __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600461{
462 return 0;
463}
464
465static int default_stop_script(void)
466{
467 return 0;
468}
469
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300470static int default_generate_script(struct pevent *pevent __maybe_unused,
471 const char *outfile __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600472{
473 return 0;
474}
475
476static struct scripting_ops default_scripting_ops = {
477 .start_script = default_start_script,
478 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700479 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600480 .generate_script = default_generate_script,
481};
482
483static struct scripting_ops *scripting_ops;
484
485static void setup_scripting(void)
486{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600487 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600488 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600489
Tom Zanussi956ffd02009-11-25 01:15:46 -0600490 scripting_ops = &default_scripting_ops;
491}
492
493static int cleanup_scripting(void)
494{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100495 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500496
Tom Zanussi956ffd02009-11-25 01:15:46 -0600497 return scripting_ops->stop_script();
498}
499
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300500static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200501 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200502 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300503 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200504 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200505{
David Aherne7984b72011-11-21 10:02:52 -0700506 struct addr_location al;
Adrian Hunteref893252013-08-27 11:23:06 +0300507 struct thread *thread = machine__findnew_thread(machine, sample->pid,
508 sample->tid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200509
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200510 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200511 pr_debug("problem processing %d event, skipping it.\n",
512 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200513 return -1;
514 }
515
David Ahern1424dc92011-03-09 22:23:28 -0700516 if (debug_mode) {
517 if (sample->time < last_timestamp) {
518 pr_err("Samples misordered, previous: %" PRIu64
519 " this: %" PRIu64 "\n", last_timestamp,
520 sample->time);
521 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200522 }
David Ahern1424dc92011-03-09 22:23:28 -0700523 last_timestamp = sample->time;
524 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200525 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000526
Adrian Huntere44baa32013-08-08 14:32:25 +0300527 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700528 pr_err("problem processing %d event, skipping it.\n",
529 event->header.type);
530 return -1;
531 }
532
533 if (al.filtered)
534 return 0;
535
Anton Blanchard5d67be92011-07-04 21:57:50 +1000536 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
537 return 0;
538
David Ahern2eaa1b42013-07-18 16:06:15 -0600539 scripting_ops->process_event(event, sample, evsel, machine, thread, &al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200540
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200541 evsel->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200542 return 0;
543}
544
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300545struct perf_script {
546 struct perf_tool tool;
547 struct perf_session *session;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200548};
549
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300550static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500551{
552 session_done = 1;
553}
554
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300555static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200556{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200557 int ret;
558
Tom Zanussic239da32010-04-01 23:59:18 -0500559 signal(SIGINT, sig_handler);
560
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300561 ret = perf_session__process_events(script->session, &script->tool);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200562
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200563 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200564 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200565
566 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200567}
568
Tom Zanussi956ffd02009-11-25 01:15:46 -0600569struct script_spec {
570 struct list_head node;
571 struct scripting_ops *ops;
572 char spec[0];
573};
574
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200575static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600576
577static struct script_spec *script_spec__new(const char *spec,
578 struct scripting_ops *ops)
579{
580 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
581
582 if (s != NULL) {
583 strcpy(s->spec, spec);
584 s->ops = ops;
585 }
586
587 return s;
588}
589
Tom Zanussi956ffd02009-11-25 01:15:46 -0600590static void script_spec__add(struct script_spec *s)
591{
592 list_add_tail(&s->node, &script_specs);
593}
594
595static struct script_spec *script_spec__find(const char *spec)
596{
597 struct script_spec *s;
598
599 list_for_each_entry(s, &script_specs, node)
600 if (strcasecmp(s->spec, spec) == 0)
601 return s;
602 return NULL;
603}
604
605static struct script_spec *script_spec__findnew(const char *spec,
606 struct scripting_ops *ops)
607{
608 struct script_spec *s = script_spec__find(spec);
609
610 if (s)
611 return s;
612
613 s = script_spec__new(spec, ops);
614 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +0900615 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600616
617 script_spec__add(s);
618
619 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600620}
621
622int script_spec_register(const char *spec, struct scripting_ops *ops)
623{
624 struct script_spec *s;
625
626 s = script_spec__find(spec);
627 if (s)
628 return -1;
629
630 s = script_spec__findnew(spec, ops);
631 if (!s)
632 return -1;
633
634 return 0;
635}
636
637static struct scripting_ops *script_spec__lookup(const char *spec)
638{
639 struct script_spec *s = script_spec__find(spec);
640 if (!s)
641 return NULL;
642
643 return s->ops;
644}
645
646static void list_available_languages(void)
647{
648 struct script_spec *s;
649
650 fprintf(stderr, "\n");
651 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100652 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600653
654 list_for_each_entry(s, &script_specs, node)
655 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
656
657 fprintf(stderr, "\n");
658}
659
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300660static int parse_scriptname(const struct option *opt __maybe_unused,
661 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600662{
663 char spec[PATH_MAX];
664 const char *script, *ext;
665 int len;
666
Tom Zanussif526d682010-01-27 02:27:52 -0600667 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600668 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600669 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600670 }
671
672 script = strchr(str, ':');
673 if (script) {
674 len = script - str;
675 if (len >= PATH_MAX) {
676 fprintf(stderr, "invalid language specifier");
677 return -1;
678 }
679 strncpy(spec, str, len);
680 spec[len] = '\0';
681 scripting_ops = script_spec__lookup(spec);
682 if (!scripting_ops) {
683 fprintf(stderr, "invalid language specifier");
684 return -1;
685 }
686 script++;
687 } else {
688 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100689 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600690 if (!ext) {
691 fprintf(stderr, "invalid script extension");
692 return -1;
693 }
694 scripting_ops = script_spec__lookup(++ext);
695 if (!scripting_ops) {
696 fprintf(stderr, "invalid script extension");
697 return -1;
698 }
699 }
700
701 script_name = strdup(script);
702
703 return 0;
704}
705
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300706static int parse_output_fields(const struct option *opt __maybe_unused,
707 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -0700708{
709 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -0500710 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -0600711 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700712 int rc = 0;
713 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700714 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700715
716 if (!str)
717 return -ENOMEM;
718
David Ahern2c9e45f72011-03-17 10:03:21 -0600719 /* first word can state for which event type the user is specifying
720 * the fields. If no type exists, the specified fields apply to all
721 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700722 */
David Ahern2c9e45f72011-03-17 10:03:21 -0600723 tok = strchr(str, ':');
724 if (tok) {
725 *tok = '\0';
726 tok++;
727 if (!strcmp(str, "hw"))
728 type = PERF_TYPE_HARDWARE;
729 else if (!strcmp(str, "sw"))
730 type = PERF_TYPE_SOFTWARE;
731 else if (!strcmp(str, "trace"))
732 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -0700733 else if (!strcmp(str, "raw"))
734 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -0600735 else {
736 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +0100737 rc = -EINVAL;
738 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -0600739 }
740
741 if (output[type].user_set)
742 pr_warning("Overriding previous field request for %s events.\n",
743 event_type(type));
744
745 output[type].fields = 0;
746 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600747 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -0600748
749 } else {
750 tok = str;
751 if (strlen(str) == 0) {
752 fprintf(stderr,
753 "Cannot set fields to 'none' for all event types.\n");
754 rc = -EINVAL;
755 goto out;
756 }
757
758 if (output_set_by_user())
759 pr_warning("Overriding previous field request for all events.\n");
760
761 for (j = 0; j < PERF_TYPE_MAX; ++j) {
762 output[j].fields = 0;
763 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600764 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -0600765 }
David Ahern1424dc92011-03-09 22:23:28 -0700766 }
767
David Ahern2c9e45f72011-03-17 10:03:21 -0600768 tok = strtok(tok, ",");
769 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -0700770 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600771 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -0700772 break;
David Ahern745f43e2011-03-09 22:23:26 -0700773 }
774 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600775 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -0700776 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -0600777 goto out;
778 }
779
780 if (type == -1) {
781 /* add user option to all events types for
782 * which it is valid
783 */
784 for (j = 0; j < PERF_TYPE_MAX; ++j) {
785 if (output[j].invalid_fields & all_output_options[i].field) {
786 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
787 all_output_options[i].str, event_type(j));
788 } else
789 output[j].fields |= all_output_options[i].field;
790 }
791 } else {
792 if (output[type].invalid_fields & all_output_options[i].field) {
793 fprintf(stderr, "\'%s\' not valid for %s events.\n",
794 all_output_options[i].str, event_type(type));
795
796 rc = -EINVAL;
797 goto out;
798 }
799 output[type].fields |= all_output_options[i].field;
800 }
801
802 tok = strtok(NULL, ",");
803 }
804
805 if (type >= 0) {
806 if (output[type].fields == 0) {
807 pr_debug("No fields requested for %s type. "
808 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -0700809 }
David Ahern1424dc92011-03-09 22:23:28 -0700810 }
David Ahern745f43e2011-03-09 22:23:26 -0700811
David Ahern2c9e45f72011-03-17 10:03:21 -0600812out:
David Ahern745f43e2011-03-09 22:23:26 -0700813 free(str);
814 return rc;
815}
816
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600817/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
818static int is_directory(const char *base_path, const struct dirent *dent)
819{
820 char path[PATH_MAX];
821 struct stat st;
822
823 sprintf(path, "%s/%s", base_path, dent->d_name);
824 if (stat(path, &st))
825 return 0;
826
827 return S_ISDIR(st.st_mode);
828}
829
830#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600831 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
832 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600833 if ((lang_dirent.d_type == DT_DIR || \
834 (lang_dirent.d_type == DT_UNKNOWN && \
835 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600836 (strcmp(lang_dirent.d_name, ".")) && \
837 (strcmp(lang_dirent.d_name, "..")))
838
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600839#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600840 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
841 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600842 if (script_dirent.d_type != DT_DIR && \
843 (script_dirent.d_type != DT_UNKNOWN || \
844 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600845
846
847#define RECORD_SUFFIX "-record"
848#define REPORT_SUFFIX "-report"
849
850struct script_desc {
851 struct list_head node;
852 char *name;
853 char *half_liner;
854 char *args;
855};
856
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200857static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600858
859static struct script_desc *script_desc__new(const char *name)
860{
861 struct script_desc *s = zalloc(sizeof(*s));
862
Tom Zanussib5b87312010-11-10 08:16:51 -0600863 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600864 s->name = strdup(name);
865
866 return s;
867}
868
869static void script_desc__delete(struct script_desc *s)
870{
871 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600872 free(s->half_liner);
873 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600874 free(s);
875}
876
877static void script_desc__add(struct script_desc *s)
878{
879 list_add_tail(&s->node, &script_descs);
880}
881
882static struct script_desc *script_desc__find(const char *name)
883{
884 struct script_desc *s;
885
886 list_for_each_entry(s, &script_descs, node)
887 if (strcasecmp(s->name, name) == 0)
888 return s;
889 return NULL;
890}
891
892static struct script_desc *script_desc__findnew(const char *name)
893{
894 struct script_desc *s = script_desc__find(name);
895
896 if (s)
897 return s;
898
899 s = script_desc__new(name);
900 if (!s)
901 goto out_delete_desc;
902
903 script_desc__add(s);
904
905 return s;
906
907out_delete_desc:
908 script_desc__delete(s);
909
910 return NULL;
911}
912
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200913static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600914{
915 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200916 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600917
918 if (strlen(str) > suffix_len) {
919 p = str + strlen(str) - suffix_len;
920 if (!strncmp(p, suffix, suffix_len))
921 return p;
922 }
923
924 return NULL;
925}
926
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600927static int read_script_info(struct script_desc *desc, const char *filename)
928{
929 char line[BUFSIZ], *p;
930 FILE *fp;
931
932 fp = fopen(filename, "r");
933 if (!fp)
934 return -1;
935
936 while (fgets(line, sizeof(line), fp)) {
937 p = ltrim(line);
938 if (strlen(p) == 0)
939 continue;
940 if (*p != '#')
941 continue;
942 p++;
943 if (strlen(p) && *p == '!')
944 continue;
945
946 p = ltrim(p);
947 if (strlen(p) && p[strlen(p) - 1] == '\n')
948 p[strlen(p) - 1] = '\0';
949
950 if (!strncmp(p, "description:", strlen("description:"))) {
951 p += strlen("description:");
952 desc->half_liner = strdup(ltrim(p));
953 continue;
954 }
955
956 if (!strncmp(p, "args:", strlen("args:"))) {
957 p += strlen("args:");
958 desc->args = strdup(ltrim(p));
959 continue;
960 }
961 }
962
963 fclose(fp);
964
965 return 0;
966}
967
Robert Richter38efb532011-11-25 11:38:40 +0100968static char *get_script_root(struct dirent *script_dirent, const char *suffix)
969{
970 char *script_root, *str;
971
972 script_root = strdup(script_dirent->d_name);
973 if (!script_root)
974 return NULL;
975
976 str = (char *)ends_with(script_root, suffix);
977 if (!str) {
978 free(script_root);
979 return NULL;
980 }
981
982 *str = '\0';
983 return script_root;
984}
985
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300986static int list_available_scripts(const struct option *opt __maybe_unused,
987 const char *s __maybe_unused,
988 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600989{
990 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
991 char scripts_path[MAXPATHLEN];
992 DIR *scripts_dir, *lang_dir;
993 char script_path[MAXPATHLEN];
994 char lang_path[MAXPATHLEN];
995 struct script_desc *desc;
996 char first_half[BUFSIZ];
997 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600998
999 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1000
1001 scripts_dir = opendir(scripts_path);
1002 if (!scripts_dir)
1003 return -1;
1004
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001005 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001006 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1007 lang_dirent.d_name);
1008 lang_dir = opendir(lang_path);
1009 if (!lang_dir)
1010 continue;
1011
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001012 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001013 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1014 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001015 desc = script_desc__findnew(script_root);
1016 snprintf(script_path, MAXPATHLEN, "%s/%s",
1017 lang_path, script_dirent.d_name);
1018 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001019 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001020 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001021 }
1022 }
1023
1024 fprintf(stdout, "List of available trace scripts:\n");
1025 list_for_each_entry(desc, &script_descs, node) {
1026 sprintf(first_half, "%s %s", desc->name,
1027 desc->args ? desc->args : "");
1028 fprintf(stdout, " %-36s %s\n", first_half,
1029 desc->half_liner ? desc->half_liner : "");
1030 }
1031
1032 exit(0);
1033}
1034
Feng Tange5f37052012-09-07 16:42:26 +08001035/*
Feng Tang49e639e2012-10-30 11:56:03 +08001036 * Some scripts specify the required events in their "xxx-record" file,
1037 * this function will check if the events in perf.data match those
1038 * mentioned in the "xxx-record".
1039 *
1040 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1041 * which is covered well now. And new parsing code should be added to
1042 * cover the future complexing formats like event groups etc.
1043 */
1044static int check_ev_match(char *dir_name, char *scriptname,
1045 struct perf_session *session)
1046{
1047 char filename[MAXPATHLEN], evname[128];
1048 char line[BUFSIZ], *p;
1049 struct perf_evsel *pos;
1050 int match, len;
1051 FILE *fp;
1052
1053 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1054
1055 fp = fopen(filename, "r");
1056 if (!fp)
1057 return -1;
1058
1059 while (fgets(line, sizeof(line), fp)) {
1060 p = ltrim(line);
1061 if (*p == '#')
1062 continue;
1063
1064 while (strlen(p)) {
1065 p = strstr(p, "-e");
1066 if (!p)
1067 break;
1068
1069 p += 2;
1070 p = ltrim(p);
1071 len = strcspn(p, " \t");
1072 if (!len)
1073 break;
1074
1075 snprintf(evname, len + 1, "%s", p);
1076
1077 match = 0;
1078 list_for_each_entry(pos,
1079 &session->evlist->entries, node) {
1080 if (!strcmp(perf_evsel__name(pos), evname)) {
1081 match = 1;
1082 break;
1083 }
1084 }
1085
1086 if (!match) {
1087 fclose(fp);
1088 return -1;
1089 }
1090 }
1091 }
1092
1093 fclose(fp);
1094 return 0;
1095}
1096
1097/*
Feng Tange5f37052012-09-07 16:42:26 +08001098 * Return -1 if none is found, otherwise the actual scripts number.
1099 *
1100 * Currently the only user of this function is the script browser, which
1101 * will list all statically runnable scripts, select one, execute it and
1102 * show the output in a perf browser.
1103 */
1104int find_scripts(char **scripts_array, char **scripts_path_array)
1105{
1106 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001107 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001108 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001109 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001110 struct perf_data_file file = {
1111 .path = input_name,
1112 .mode = PERF_DATA_MODE_READ,
1113 };
Feng Tange5f37052012-09-07 16:42:26 +08001114 char *temp;
1115 int i = 0;
1116
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001117 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001118 if (!session)
1119 return -1;
1120
Feng Tange5f37052012-09-07 16:42:26 +08001121 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1122
1123 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001124 if (!scripts_dir) {
1125 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001126 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001127 }
Feng Tange5f37052012-09-07 16:42:26 +08001128
1129 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1130 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1131 lang_dirent.d_name);
1132#ifdef NO_LIBPERL
1133 if (strstr(lang_path, "perl"))
1134 continue;
1135#endif
1136#ifdef NO_LIBPYTHON
1137 if (strstr(lang_path, "python"))
1138 continue;
1139#endif
1140
1141 lang_dir = opendir(lang_path);
1142 if (!lang_dir)
1143 continue;
1144
1145 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1146 /* Skip those real time scripts: xxxtop.p[yl] */
1147 if (strstr(script_dirent.d_name, "top."))
1148 continue;
1149 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1150 script_dirent.d_name);
1151 temp = strchr(script_dirent.d_name, '.');
1152 snprintf(scripts_array[i],
1153 (temp - script_dirent.d_name) + 1,
1154 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001155
1156 if (check_ev_match(lang_path,
1157 scripts_array[i], session))
1158 continue;
1159
Feng Tange5f37052012-09-07 16:42:26 +08001160 i++;
1161 }
Feng Tang49e639e2012-10-30 11:56:03 +08001162 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001163 }
1164
Feng Tang49e639e2012-10-30 11:56:03 +08001165 closedir(scripts_dir);
1166 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001167 return i;
1168}
1169
Tom Zanussi38752942009-12-15 02:53:39 -06001170static char *get_script_path(const char *script_root, const char *suffix)
1171{
1172 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1173 char scripts_path[MAXPATHLEN];
1174 char script_path[MAXPATHLEN];
1175 DIR *scripts_dir, *lang_dir;
1176 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001177 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001178
1179 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1180
1181 scripts_dir = opendir(scripts_path);
1182 if (!scripts_dir)
1183 return NULL;
1184
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001185 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001186 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1187 lang_dirent.d_name);
1188 lang_dir = opendir(lang_path);
1189 if (!lang_dir)
1190 continue;
1191
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001192 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001193 __script_root = get_script_root(&script_dirent, suffix);
1194 if (__script_root && !strcmp(script_root, __script_root)) {
1195 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001196 closedir(lang_dir);
1197 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001198 snprintf(script_path, MAXPATHLEN, "%s/%s",
1199 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001200 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001201 }
1202 free(__script_root);
1203 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001204 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001205 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001206 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001207
Robert Richter38efb532011-11-25 11:38:40 +01001208 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001209}
1210
Tom Zanussib5b87312010-11-10 08:16:51 -06001211static bool is_top_script(const char *script_path)
1212{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001213 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001214}
1215
1216static int has_required_arg(char *script_path)
1217{
1218 struct script_desc *desc;
1219 int n_args = 0;
1220 char *p;
1221
1222 desc = script_desc__new(NULL);
1223
1224 if (read_script_info(desc, script_path))
1225 goto out;
1226
1227 if (!desc->args)
1228 goto out;
1229
1230 for (p = desc->args; *p; p++)
1231 if (*p == '<')
1232 n_args++;
1233out:
1234 script_desc__delete(desc);
1235
1236 return n_args;
1237}
1238
David Ahernd54b1a92012-08-26 12:24:46 -06001239static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001240{
1241 char **__argv = malloc(sizeof(const char *) * argc);
1242
David Ahernd54b1a92012-08-26 12:24:46 -06001243 if (!__argv) {
1244 pr_err("malloc failed\n");
1245 return -1;
1246 }
1247
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001248 memcpy(__argv, argv, sizeof(const char *) * argc);
1249 argc = parse_options(argc, (const char **)__argv, record_options,
1250 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1251 free(__argv);
1252
David Ahernd54b1a92012-08-26 12:24:46 -06001253 system_wide = (argc == 0);
1254
1255 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001256}
1257
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001258int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001259{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001260 bool show_full_info = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001261 char *rec_script_path = NULL;
1262 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001263 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -06001264 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001265 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -06001266 int i, j, err;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001267 struct perf_script script = {
1268 .tool = {
1269 .sample = process_sample_event,
1270 .mmap = perf_event__process_mmap,
1271 .mmap2 = perf_event__process_mmap2,
1272 .comm = perf_event__process_comm,
1273 .exit = perf_event__process_exit,
1274 .fork = perf_event__process_fork,
1275 .attr = perf_event__process_attr,
1276 .tracing_data = perf_event__process_tracing_data,
1277 .build_id = perf_event__process_build_id,
1278 .ordered_samples = true,
1279 .ordering_requires_timestamps = true,
1280 },
1281 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001282 const struct option options[] = {
1283 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1284 "dump raw trace in ASCII"),
1285 OPT_INCR('v', "verbose", &verbose,
1286 "be more verbose (show symbol address, etc)"),
1287 OPT_BOOLEAN('L', "Latency", &latency_format,
1288 "show latency attributes (irqs/preemption disabled, etc)"),
1289 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1290 list_available_scripts),
1291 OPT_CALLBACK('s', "script", NULL, "name",
1292 "script file name (lang:script name, script name, or *)",
1293 parse_scriptname),
1294 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1295 "generate perf-script.xx script in specified language"),
1296 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1297 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1298 "do various checks like samples ordering and lost events"),
1299 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1300 "file", "vmlinux pathname"),
1301 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1302 "file", "kallsyms pathname"),
1303 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1304 "When printing symbols do not display call chain"),
1305 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1306 "Look for files with symbols relative to this directory"),
1307 OPT_CALLBACK('f', "fields", NULL, "str",
1308 "comma separated output fields prepend with 'type:'. "
1309 "Valid types: hw,sw,trace,raw. "
1310 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1311 "addr,symoff", parse_output_fields),
1312 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1313 "system-wide collection from all CPUs"),
1314 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1315 "only consider these symbols"),
1316 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1317 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1318 "only display events for these comms"),
1319 OPT_BOOLEAN('I', "show-info", &show_full_info,
1320 "display extended information from perf.data file"),
1321 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1322 "Show the path of [kernel.kallsyms]"),
1323 OPT_END()
1324 };
1325 const char * const script_usage[] = {
1326 "perf script [<options>]",
1327 "perf script [<options>] record <script> [<record-options>] <command>",
1328 "perf script [<options>] report <script> [script-args]",
1329 "perf script [<options>] <script> [<record-options>] <command>",
1330 "perf script [<options>] <top-script> [script-args]",
1331 NULL
1332 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001333 struct perf_data_file file = {
1334 .mode = PERF_DATA_MODE_READ,
1335 };
Tom Zanussi38752942009-12-15 02:53:39 -06001336
Tom Zanussib5b87312010-11-10 08:16:51 -06001337 setup_scripting();
1338
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001339 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001340 PARSE_OPT_STOP_AT_NON_OPTION);
1341
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001342 file.path = input_name;
1343
Tom Zanussib5b87312010-11-10 08:16:51 -06001344 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1345 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1346 if (!rec_script_path)
1347 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001348 }
1349
Tom Zanussib5b87312010-11-10 08:16:51 -06001350 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1351 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1352 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001353 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001354 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001355 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001356 return -1;
1357 }
Tom Zanussi38752942009-12-15 02:53:39 -06001358 }
1359
Ben Hutchings44e668c2010-10-10 16:10:03 +01001360 /* make sure PERF_EXEC_PATH is set for scripts */
1361 perf_set_argv_exec_path(perf_exec_path());
1362
Tom Zanussib5b87312010-11-10 08:16:51 -06001363 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001364 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001365 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001366 pid_t pid;
1367
Tom Zanussib5b87312010-11-10 08:16:51 -06001368 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1369 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1370
1371 if (!rec_script_path && !rep_script_path) {
1372 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001373 " script -l for available scripts.\n", argv[0]);
1374 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001375 }
1376
Tom Zanussib5b87312010-11-10 08:16:51 -06001377 if (is_top_script(argv[0])) {
1378 rep_args = argc - 1;
1379 } else {
1380 int rec_args;
1381
1382 rep_args = has_required_arg(rep_script_path);
1383 rec_args = (argc - 1) - rep_args;
1384 if (rec_args < 0) {
1385 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001386 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001387 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001388 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001389 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001390 }
1391
1392 if (pipe(live_pipe) < 0) {
1393 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001394 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001395 }
1396
1397 pid = fork();
1398 if (pid < 0) {
1399 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001400 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001401 }
1402
1403 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001404 j = 0;
1405
Tom Zanussia0cccc22010-04-01 23:59:25 -05001406 dup2(live_pipe[1], 1);
1407 close(live_pipe[0]);
1408
Robert Richter317df652011-11-25 15:05:25 +01001409 if (is_top_script(argv[0])) {
1410 system_wide = true;
1411 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001412 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1413 err = -1;
1414 goto out;
1415 }
Robert Richter317df652011-11-25 15:05:25 +01001416 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001417
1418 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001419 if (!__argv) {
1420 pr_err("malloc failed\n");
1421 err = -ENOMEM;
1422 goto out;
1423 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001424
Tom Zanussib5b87312010-11-10 08:16:51 -06001425 __argv[j++] = "/bin/sh";
1426 __argv[j++] = rec_script_path;
1427 if (system_wide)
1428 __argv[j++] = "-a";
1429 __argv[j++] = "-q";
1430 __argv[j++] = "-o";
1431 __argv[j++] = "-";
1432 for (i = rep_args + 1; i < argc; i++)
1433 __argv[j++] = argv[i];
1434 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001435
1436 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001437 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001438 exit(-1);
1439 }
1440
1441 dup2(live_pipe[0], 0);
1442 close(live_pipe[1]);
1443
Tom Zanussib5b87312010-11-10 08:16:51 -06001444 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001445 if (!__argv) {
1446 pr_err("malloc failed\n");
1447 err = -ENOMEM;
1448 goto out;
1449 }
1450
Tom Zanussib5b87312010-11-10 08:16:51 -06001451 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001452 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001453 __argv[j++] = rep_script_path;
1454 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001455 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001456 __argv[j++] = "-i";
1457 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001458 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001459
1460 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001461 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001462 exit(-1);
1463 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001464
Tom Zanussib5b87312010-11-10 08:16:51 -06001465 if (rec_script_path)
1466 script_path = rec_script_path;
1467 if (rep_script_path)
1468 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001469
Tom Zanussib5b87312010-11-10 08:16:51 -06001470 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001471 j = 0;
1472
Robert Richter317df652011-11-25 15:05:25 +01001473 if (!rec_script_path)
1474 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001475 else if (!system_wide) {
1476 if (have_cmd(argc - 1, &argv[1]) != 0) {
1477 err = -1;
1478 goto out;
1479 }
1480 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001481
1482 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001483 if (!__argv) {
1484 pr_err("malloc failed\n");
1485 err = -ENOMEM;
1486 goto out;
1487 }
1488
Tom Zanussib5b87312010-11-10 08:16:51 -06001489 __argv[j++] = "/bin/sh";
1490 __argv[j++] = script_path;
1491 if (system_wide)
1492 __argv[j++] = "-a";
1493 for (i = 2; i < argc; i++)
1494 __argv[j++] = argv[i];
1495 __argv[j++] = NULL;
1496
1497 execvp("/bin/sh", (char **)__argv);
1498 free(__argv);
1499 exit(-1);
1500 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001501
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001502 if (symbol__init() < 0)
1503 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -06001504 if (!script_name)
1505 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001506
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001507 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001508 if (session == NULL)
1509 return -ENOMEM;
1510
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001511 script.session = session;
1512
Anton Blanchard5d67be92011-07-04 21:57:50 +10001513 if (cpu_list) {
1514 if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
1515 return -1;
1516 }
1517
Tom Zanussibdb71db2013-01-18 13:51:26 -06001518 if (!script_name && !generate_script_lang)
1519 perf_session__fprintf_info(session, stdout, show_full_info);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001520
David Ahern1424dc92011-03-09 22:23:28 -07001521 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001522 symbol_conf.use_callchain = true;
1523 else
1524 symbol_conf.use_callchain = false;
1525
Tom Zanussi956ffd02009-11-25 01:15:46 -06001526 if (generate_script_lang) {
1527 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001528 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001529
David Ahern2c9e45f72011-03-17 10:03:21 -06001530 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001531 fprintf(stderr,
1532 "custom fields not supported for generated scripts");
1533 return -1;
1534 }
1535
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001536 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06001537 if (input < 0) {
1538 perror("failed to open file");
David Ahernd54b1a92012-08-26 12:24:46 -06001539 return -1;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001540 }
1541
1542 err = fstat(input, &perf_stat);
1543 if (err < 0) {
1544 perror("failed to stat file");
David Ahernd54b1a92012-08-26 12:24:46 -06001545 return -1;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001546 }
1547
1548 if (!perf_stat.st_size) {
1549 fprintf(stderr, "zero-sized file, nothing to do!\n");
David Ahernd54b1a92012-08-26 12:24:46 -06001550 return 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001551 }
1552
1553 scripting_ops = script_spec__lookup(generate_script_lang);
1554 if (!scripting_ops) {
1555 fprintf(stderr, "invalid language specifier");
1556 return -1;
1557 }
1558
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001559 err = scripting_ops->generate_script(session->pevent,
1560 "perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001561 goto out;
1562 }
1563
1564 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001565 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001566 if (err)
1567 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001568 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001569 }
1570
David Ahern9cbdb702011-04-06 21:54:20 -06001571
1572 err = perf_session__check_output_opt(session);
1573 if (err < 0)
1574 goto out;
1575
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001576 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001577
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001578 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001579 cleanup_scripting();
1580out:
1581 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001582}