blob: e2b9aff6506ec232d532f02edb47b315caef5802 [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
Adrian Hunter7ea95722013-11-01 15:51:30 +0200232static void set_print_ip_opts(struct perf_event_attr *attr)
233{
234 unsigned int type = attr->type;
235
236 output[type].print_ip_opts = 0;
237 if (PRINT_FIELD(IP))
238 output[type].print_ip_opts |= PRINT_IP_OPT_IP;
239
240 if (PRINT_FIELD(SYM))
241 output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
242
243 if (PRINT_FIELD(DSO))
244 output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
245
246 if (PRINT_FIELD(SYMOFFSET))
247 output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
248}
249
David Ahern9cbdb702011-04-06 21:54:20 -0600250/*
251 * verify all user requested events exist and the samples
252 * have the expected data
253 */
254static int perf_session__check_output_opt(struct perf_session *session)
255{
256 int j;
257 struct perf_evsel *evsel;
258
259 for (j = 0; j < PERF_TYPE_MAX; ++j) {
260 evsel = perf_session__find_first_evtype(session, j);
261
262 /*
263 * even if fields is set to 0 (ie., show nothing) event must
264 * exist if user explicitly includes it on the command line
265 */
266 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
267 pr_err("%s events do not exist. "
268 "Remove corresponding -f option to proceed.\n",
269 event_type(j));
270 return -1;
271 }
272
273 if (evsel && output[j].fields &&
274 perf_evsel__check_attr(evsel, session))
275 return -1;
David Aherna6ffaf92013-08-07 22:50:51 -0400276
277 if (evsel == NULL)
278 continue;
279
Adrian Hunter7ea95722013-11-01 15:51:30 +0200280 set_print_ip_opts(&evsel->attr);
David Ahern1424dc92011-03-09 22:23:28 -0700281 }
282
David Ahern80b8b492013-11-19 21:07:37 -0700283 /*
284 * set default for tracepoints to print symbols only
285 * if callchains are present
286 */
287 if (symbol_conf.use_callchain &&
288 !output[PERF_TYPE_TRACEPOINT].user_set) {
289 struct perf_event_attr *attr;
290
291 j = PERF_TYPE_TRACEPOINT;
292 evsel = perf_session__find_first_evtype(session, j);
293 if (evsel == NULL)
294 goto out;
295
296 attr = &evsel->attr;
297
298 if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
299 output[j].fields |= PERF_OUTPUT_IP;
300 output[j].fields |= PERF_OUTPUT_SYM;
301 output[j].fields |= PERF_OUTPUT_DSO;
302 set_print_ip_opts(attr);
303 }
304 }
305
306out:
David Ahern1424dc92011-03-09 22:23:28 -0700307 return 0;
308}
David Ahern745f43e2011-03-09 22:23:26 -0700309
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300310static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700311 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300312 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700313{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300314 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700315 unsigned long secs;
316 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700317 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700318
David Ahern745f43e2011-03-09 22:23:26 -0700319 if (PRINT_FIELD(COMM)) {
320 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200321 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600322 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200323 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700324 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200325 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700326 }
David Ahernc70c94b2011-03-09 22:23:25 -0700327
David Ahern745f43e2011-03-09 22:23:26 -0700328 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
329 printf("%5d/%-5d ", sample->pid, sample->tid);
330 else if (PRINT_FIELD(PID))
331 printf("%5d ", sample->pid);
332 else if (PRINT_FIELD(TID))
333 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700334
David Ahern745f43e2011-03-09 22:23:26 -0700335 if (PRINT_FIELD(CPU)) {
336 if (latency_format)
337 printf("%3d ", sample->cpu);
338 else
339 printf("[%03d] ", sample->cpu);
340 }
David Ahernc70c94b2011-03-09 22:23:25 -0700341
David Ahern745f43e2011-03-09 22:23:26 -0700342 if (PRINT_FIELD(TIME)) {
343 nsecs = sample->time;
344 secs = nsecs / NSECS_PER_SEC;
345 nsecs -= secs * NSECS_PER_SEC;
346 usecs = nsecs / NSECS_PER_USEC;
347 printf("%5lu.%06lu: ", secs, usecs);
348 }
David Ahernc70c94b2011-03-09 22:23:25 -0700349}
350
Akihiro Nagai95582592012-01-30 13:43:09 +0900351static bool is_bts_event(struct perf_event_attr *attr)
352{
353 return ((attr->type == PERF_TYPE_HARDWARE) &&
354 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
355 (attr->sample_period == 1));
356}
357
David Ahern7cec0922011-05-30 13:08:23 -0600358static bool sample_addr_correlates_sym(struct perf_event_attr *attr)
359{
360 if ((attr->type == PERF_TYPE_SOFTWARE) &&
361 ((attr->config == PERF_COUNT_SW_PAGE_FAULTS) ||
362 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN) ||
363 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)))
364 return true;
365
Akihiro Nagai95582592012-01-30 13:43:09 +0900366 if (is_bts_event(attr))
367 return true;
368
David Ahern7cec0922011-05-30 13:08:23 -0600369 return false;
370}
371
372static void print_sample_addr(union perf_event *event,
373 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200374 struct machine *machine,
David Ahern7cec0922011-05-30 13:08:23 -0600375 struct thread *thread,
376 struct perf_event_attr *attr)
377{
378 struct addr_location al;
379 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
David Ahern7cec0922011-05-30 13:08:23 -0600380
381 printf("%16" PRIx64, sample->addr);
382
383 if (!sample_addr_correlates_sym(attr))
384 return;
385
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200386 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300387 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600388 if (!al.map)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200389 thread__find_addr_map(thread, machine, cpumode, MAP__VARIABLE,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300390 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600391
392 al.cpu = sample->cpu;
393 al.sym = NULL;
394
395 if (al.map)
396 al.sym = map__find_symbol(al.map, al.addr, NULL);
397
398 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900399 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900400 if (PRINT_FIELD(SYMOFFSET))
401 symbol__fprintf_symname_offs(al.sym, &al, stdout);
402 else
403 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600404 }
405
406 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900407 printf(" (");
408 map__fprintf_dsoname(al.map, stdout);
409 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600410 }
411}
412
Akihiro Nagai95582592012-01-30 13:43:09 +0900413static void print_sample_bts(union perf_event *event,
414 struct perf_sample *sample,
415 struct perf_evsel *evsel,
416 struct machine *machine,
417 struct thread *thread)
418{
419 struct perf_event_attr *attr = &evsel->attr;
420
421 /* print branch_from information */
422 if (PRINT_FIELD(IP)) {
423 if (!symbol_conf.use_callchain)
424 printf(" ");
425 else
426 printf("\n");
Jiri Olsa71ad0f52012-08-07 15:20:46 +0200427 perf_evsel__print_ip(evsel, event, sample, machine,
David Ahern307cbb92013-08-07 22:50:53 -0400428 output[attr->type].print_ip_opts,
429 PERF_MAX_STACK_DEPTH);
Akihiro Nagai95582592012-01-30 13:43:09 +0900430 }
431
432 printf(" => ");
433
434 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300435 if (PRINT_FIELD(ADDR) ||
436 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
437 !output[attr->type].user_set))
Akihiro Nagai95582592012-01-30 13:43:09 +0900438 print_sample_addr(event, sample, machine, thread, attr);
439
440 printf("\n");
441}
442
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300443static void process_event(union perf_event *event, struct perf_sample *sample,
444 struct perf_evsel *evsel, struct machine *machine,
David Ahern2eaa1b42013-07-18 16:06:15 -0600445 struct thread *thread,
446 struct addr_location *al __maybe_unused)
David Ahernbe6d8422011-03-09 22:23:23 -0700447{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300448 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700449
David Ahern2c9e45f72011-03-17 10:03:21 -0600450 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700451 return;
452
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300453 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700454
Namhyung Kime944d3d2013-11-18 14:34:52 +0900455 if (PRINT_FIELD(EVNAME)) {
456 const char *evname = perf_evsel__name(evsel);
457 printf("%s: ", evname ? evname : "[unknown]");
458 }
459
Akihiro Nagai95582592012-01-30 13:43:09 +0900460 if (is_bts_event(attr)) {
461 print_sample_bts(event, sample, evsel, machine, thread);
462 return;
463 }
464
David Ahern745f43e2011-03-09 22:23:26 -0700465 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300466 event_format__print(evsel->tp_format, sample->cpu,
467 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600468 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200469 print_sample_addr(event, sample, machine, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600470
David Ahern787bef12011-05-27 14:28:43 -0600471 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700472 if (!symbol_conf.use_callchain)
473 printf(" ");
474 else
475 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400476
Jiri Olsa71ad0f52012-08-07 15:20:46 +0200477 perf_evsel__print_ip(evsel, event, sample, machine,
David Ahern307cbb92013-08-07 22:50:53 -0400478 output[attr->type].print_ip_opts,
479 PERF_MAX_STACK_DEPTH);
David Ahernc0230b22011-03-09 22:23:27 -0700480 }
481
David Ahernc70c94b2011-03-09 22:23:25 -0700482 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700483}
484
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300485static int default_start_script(const char *script __maybe_unused,
486 int argc __maybe_unused,
487 const char **argv __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600488{
489 return 0;
490}
491
492static int default_stop_script(void)
493{
494 return 0;
495}
496
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300497static int default_generate_script(struct pevent *pevent __maybe_unused,
498 const char *outfile __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600499{
500 return 0;
501}
502
503static struct scripting_ops default_scripting_ops = {
504 .start_script = default_start_script,
505 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700506 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600507 .generate_script = default_generate_script,
508};
509
510static struct scripting_ops *scripting_ops;
511
512static void setup_scripting(void)
513{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600514 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600515 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600516
Tom Zanussi956ffd02009-11-25 01:15:46 -0600517 scripting_ops = &default_scripting_ops;
518}
519
520static int cleanup_scripting(void)
521{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100522 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500523
Tom Zanussi956ffd02009-11-25 01:15:46 -0600524 return scripting_ops->stop_script();
525}
526
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300527static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200528 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200529 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300530 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200531 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200532{
David Aherne7984b72011-11-21 10:02:52 -0700533 struct addr_location al;
Adrian Hunteref893252013-08-27 11:23:06 +0300534 struct thread *thread = machine__findnew_thread(machine, sample->pid,
535 sample->tid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200536
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200537 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200538 pr_debug("problem processing %d event, skipping it.\n",
539 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200540 return -1;
541 }
542
David Ahern1424dc92011-03-09 22:23:28 -0700543 if (debug_mode) {
544 if (sample->time < last_timestamp) {
545 pr_err("Samples misordered, previous: %" PRIu64
546 " this: %" PRIu64 "\n", last_timestamp,
547 sample->time);
548 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200549 }
David Ahern1424dc92011-03-09 22:23:28 -0700550 last_timestamp = sample->time;
551 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200552 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000553
Adrian Huntere44baa32013-08-08 14:32:25 +0300554 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700555 pr_err("problem processing %d event, skipping it.\n",
556 event->header.type);
557 return -1;
558 }
559
560 if (al.filtered)
561 return 0;
562
Anton Blanchard5d67be92011-07-04 21:57:50 +1000563 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
564 return 0;
565
David Ahern2eaa1b42013-07-18 16:06:15 -0600566 scripting_ops->process_event(event, sample, evsel, machine, thread, &al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200567
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200568 evsel->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200569 return 0;
570}
571
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300572struct perf_script {
573 struct perf_tool tool;
574 struct perf_session *session;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900575 bool show_task_events;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200576};
577
Adrian Hunter7ea95722013-11-01 15:51:30 +0200578static int process_attr(struct perf_tool *tool, union perf_event *event,
579 struct perf_evlist **pevlist)
580{
581 struct perf_script *scr = container_of(tool, struct perf_script, tool);
582 struct perf_evlist *evlist;
583 struct perf_evsel *evsel, *pos;
584 int err;
585
586 err = perf_event__process_attr(tool, event, pevlist);
587 if (err)
588 return err;
589
590 evlist = *pevlist;
591 evsel = perf_evlist__last(*pevlist);
592
593 if (evsel->attr.type >= PERF_TYPE_MAX)
594 return 0;
595
596 list_for_each_entry(pos, &evlist->entries, node) {
597 if (pos->attr.type == evsel->attr.type && pos != evsel)
598 return 0;
599 }
600
601 set_print_ip_opts(&evsel->attr);
602
603 return perf_evsel__check_attr(evsel, scr->session);
604}
605
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900606static int process_comm_event(struct perf_tool *tool,
607 union perf_event *event,
608 struct perf_sample *sample,
609 struct machine *machine)
610{
611 struct thread *thread;
612 struct perf_script *script = container_of(tool, struct perf_script, tool);
613 struct perf_session *session = script->session;
614 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
615 int ret = -1;
616
617 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
618 if (thread == NULL) {
619 pr_debug("problem processing COMM event, skipping it.\n");
620 return -1;
621 }
622
623 if (perf_event__process_comm(tool, event, sample, machine) < 0)
624 goto out;
625
626 if (!evsel->attr.sample_id_all) {
627 sample->cpu = 0;
628 sample->time = 0;
629 sample->tid = event->comm.tid;
630 sample->pid = event->comm.pid;
631 }
632 print_sample_start(sample, thread, evsel);
633 perf_event__fprintf(event, stdout);
634 ret = 0;
635
636out:
637 return ret;
638}
639
640static int process_fork_event(struct perf_tool *tool,
641 union perf_event *event,
642 struct perf_sample *sample,
643 struct machine *machine)
644{
645 struct thread *thread;
646 struct perf_script *script = container_of(tool, struct perf_script, tool);
647 struct perf_session *session = script->session;
648 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
649
650 if (perf_event__process_fork(tool, event, sample, machine) < 0)
651 return -1;
652
653 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
654 if (thread == NULL) {
655 pr_debug("problem processing FORK event, skipping it.\n");
656 return -1;
657 }
658
659 if (!evsel->attr.sample_id_all) {
660 sample->cpu = 0;
661 sample->time = event->fork.time;
662 sample->tid = event->fork.tid;
663 sample->pid = event->fork.pid;
664 }
665 print_sample_start(sample, thread, evsel);
666 perf_event__fprintf(event, stdout);
667
668 return 0;
669}
670static int process_exit_event(struct perf_tool *tool,
671 union perf_event *event,
672 struct perf_sample *sample,
673 struct machine *machine)
674{
675 struct thread *thread;
676 struct perf_script *script = container_of(tool, struct perf_script, tool);
677 struct perf_session *session = script->session;
678 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
679
680 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
681 if (thread == NULL) {
682 pr_debug("problem processing EXIT event, skipping it.\n");
683 return -1;
684 }
685
686 if (!evsel->attr.sample_id_all) {
687 sample->cpu = 0;
688 sample->time = 0;
689 sample->tid = event->comm.tid;
690 sample->pid = event->comm.pid;
691 }
692 print_sample_start(sample, thread, evsel);
693 perf_event__fprintf(event, stdout);
694
695 if (perf_event__process_exit(tool, event, sample, machine) < 0)
696 return -1;
697
698 return 0;
699}
700
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300701static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500702{
703 session_done = 1;
704}
705
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300706static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200707{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200708 int ret;
709
Tom Zanussic239da32010-04-01 23:59:18 -0500710 signal(SIGINT, sig_handler);
711
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900712 /* override event processing functions */
713 if (script->show_task_events) {
714 script->tool.comm = process_comm_event;
715 script->tool.fork = process_fork_event;
716 script->tool.exit = process_exit_event;
717 }
718
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300719 ret = perf_session__process_events(script->session, &script->tool);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200720
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200721 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200722 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200723
724 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200725}
726
Tom Zanussi956ffd02009-11-25 01:15:46 -0600727struct script_spec {
728 struct list_head node;
729 struct scripting_ops *ops;
730 char spec[0];
731};
732
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200733static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600734
735static struct script_spec *script_spec__new(const char *spec,
736 struct scripting_ops *ops)
737{
738 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
739
740 if (s != NULL) {
741 strcpy(s->spec, spec);
742 s->ops = ops;
743 }
744
745 return s;
746}
747
Tom Zanussi956ffd02009-11-25 01:15:46 -0600748static void script_spec__add(struct script_spec *s)
749{
750 list_add_tail(&s->node, &script_specs);
751}
752
753static struct script_spec *script_spec__find(const char *spec)
754{
755 struct script_spec *s;
756
757 list_for_each_entry(s, &script_specs, node)
758 if (strcasecmp(s->spec, spec) == 0)
759 return s;
760 return NULL;
761}
762
763static struct script_spec *script_spec__findnew(const char *spec,
764 struct scripting_ops *ops)
765{
766 struct script_spec *s = script_spec__find(spec);
767
768 if (s)
769 return s;
770
771 s = script_spec__new(spec, ops);
772 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +0900773 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600774
775 script_spec__add(s);
776
777 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600778}
779
780int script_spec_register(const char *spec, struct scripting_ops *ops)
781{
782 struct script_spec *s;
783
784 s = script_spec__find(spec);
785 if (s)
786 return -1;
787
788 s = script_spec__findnew(spec, ops);
789 if (!s)
790 return -1;
791
792 return 0;
793}
794
795static struct scripting_ops *script_spec__lookup(const char *spec)
796{
797 struct script_spec *s = script_spec__find(spec);
798 if (!s)
799 return NULL;
800
801 return s->ops;
802}
803
804static void list_available_languages(void)
805{
806 struct script_spec *s;
807
808 fprintf(stderr, "\n");
809 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100810 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600811
812 list_for_each_entry(s, &script_specs, node)
813 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
814
815 fprintf(stderr, "\n");
816}
817
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300818static int parse_scriptname(const struct option *opt __maybe_unused,
819 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600820{
821 char spec[PATH_MAX];
822 const char *script, *ext;
823 int len;
824
Tom Zanussif526d68b2010-01-27 02:27:52 -0600825 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600826 list_available_languages();
Tom Zanussif526d68b2010-01-27 02:27:52 -0600827 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600828 }
829
830 script = strchr(str, ':');
831 if (script) {
832 len = script - str;
833 if (len >= PATH_MAX) {
834 fprintf(stderr, "invalid language specifier");
835 return -1;
836 }
837 strncpy(spec, str, len);
838 spec[len] = '\0';
839 scripting_ops = script_spec__lookup(spec);
840 if (!scripting_ops) {
841 fprintf(stderr, "invalid language specifier");
842 return -1;
843 }
844 script++;
845 } else {
846 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100847 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600848 if (!ext) {
849 fprintf(stderr, "invalid script extension");
850 return -1;
851 }
852 scripting_ops = script_spec__lookup(++ext);
853 if (!scripting_ops) {
854 fprintf(stderr, "invalid script extension");
855 return -1;
856 }
857 }
858
859 script_name = strdup(script);
860
861 return 0;
862}
863
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300864static int parse_output_fields(const struct option *opt __maybe_unused,
865 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -0700866{
867 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -0500868 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -0600869 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700870 int rc = 0;
871 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700872 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700873
874 if (!str)
875 return -ENOMEM;
876
David Ahern2c9e45f72011-03-17 10:03:21 -0600877 /* first word can state for which event type the user is specifying
878 * the fields. If no type exists, the specified fields apply to all
879 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700880 */
David Ahern2c9e45f72011-03-17 10:03:21 -0600881 tok = strchr(str, ':');
882 if (tok) {
883 *tok = '\0';
884 tok++;
885 if (!strcmp(str, "hw"))
886 type = PERF_TYPE_HARDWARE;
887 else if (!strcmp(str, "sw"))
888 type = PERF_TYPE_SOFTWARE;
889 else if (!strcmp(str, "trace"))
890 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -0700891 else if (!strcmp(str, "raw"))
892 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -0600893 else {
894 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +0100895 rc = -EINVAL;
896 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -0600897 }
898
899 if (output[type].user_set)
900 pr_warning("Overriding previous field request for %s events.\n",
901 event_type(type));
902
903 output[type].fields = 0;
904 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600905 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -0600906
907 } else {
908 tok = str;
909 if (strlen(str) == 0) {
910 fprintf(stderr,
911 "Cannot set fields to 'none' for all event types.\n");
912 rc = -EINVAL;
913 goto out;
914 }
915
916 if (output_set_by_user())
917 pr_warning("Overriding previous field request for all events.\n");
918
919 for (j = 0; j < PERF_TYPE_MAX; ++j) {
920 output[j].fields = 0;
921 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600922 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -0600923 }
David Ahern1424dc92011-03-09 22:23:28 -0700924 }
925
David Ahern2c9e45f72011-03-17 10:03:21 -0600926 tok = strtok(tok, ",");
927 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -0700928 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600929 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -0700930 break;
David Ahern745f43e2011-03-09 22:23:26 -0700931 }
932 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600933 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -0700934 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -0600935 goto out;
936 }
937
938 if (type == -1) {
939 /* add user option to all events types for
940 * which it is valid
941 */
942 for (j = 0; j < PERF_TYPE_MAX; ++j) {
943 if (output[j].invalid_fields & all_output_options[i].field) {
944 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
945 all_output_options[i].str, event_type(j));
946 } else
947 output[j].fields |= all_output_options[i].field;
948 }
949 } else {
950 if (output[type].invalid_fields & all_output_options[i].field) {
951 fprintf(stderr, "\'%s\' not valid for %s events.\n",
952 all_output_options[i].str, event_type(type));
953
954 rc = -EINVAL;
955 goto out;
956 }
957 output[type].fields |= all_output_options[i].field;
958 }
959
960 tok = strtok(NULL, ",");
961 }
962
963 if (type >= 0) {
964 if (output[type].fields == 0) {
965 pr_debug("No fields requested for %s type. "
966 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -0700967 }
David Ahern1424dc92011-03-09 22:23:28 -0700968 }
David Ahern745f43e2011-03-09 22:23:26 -0700969
David Ahern2c9e45f72011-03-17 10:03:21 -0600970out:
David Ahern745f43e2011-03-09 22:23:26 -0700971 free(str);
972 return rc;
973}
974
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600975/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
976static int is_directory(const char *base_path, const struct dirent *dent)
977{
978 char path[PATH_MAX];
979 struct stat st;
980
981 sprintf(path, "%s/%s", base_path, dent->d_name);
982 if (stat(path, &st))
983 return 0;
984
985 return S_ISDIR(st.st_mode);
986}
987
988#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600989 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
990 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600991 if ((lang_dirent.d_type == DT_DIR || \
992 (lang_dirent.d_type == DT_UNKNOWN && \
993 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600994 (strcmp(lang_dirent.d_name, ".")) && \
995 (strcmp(lang_dirent.d_name, "..")))
996
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600997#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600998 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
999 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001000 if (script_dirent.d_type != DT_DIR && \
1001 (script_dirent.d_type != DT_UNKNOWN || \
1002 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001003
1004
1005#define RECORD_SUFFIX "-record"
1006#define REPORT_SUFFIX "-report"
1007
1008struct script_desc {
1009 struct list_head node;
1010 char *name;
1011 char *half_liner;
1012 char *args;
1013};
1014
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001015static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001016
1017static struct script_desc *script_desc__new(const char *name)
1018{
1019 struct script_desc *s = zalloc(sizeof(*s));
1020
Tom Zanussib5b87312010-11-10 08:16:51 -06001021 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001022 s->name = strdup(name);
1023
1024 return s;
1025}
1026
1027static void script_desc__delete(struct script_desc *s)
1028{
1029 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001030 free(s->half_liner);
1031 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001032 free(s);
1033}
1034
1035static void script_desc__add(struct script_desc *s)
1036{
1037 list_add_tail(&s->node, &script_descs);
1038}
1039
1040static struct script_desc *script_desc__find(const char *name)
1041{
1042 struct script_desc *s;
1043
1044 list_for_each_entry(s, &script_descs, node)
1045 if (strcasecmp(s->name, name) == 0)
1046 return s;
1047 return NULL;
1048}
1049
1050static struct script_desc *script_desc__findnew(const char *name)
1051{
1052 struct script_desc *s = script_desc__find(name);
1053
1054 if (s)
1055 return s;
1056
1057 s = script_desc__new(name);
1058 if (!s)
1059 goto out_delete_desc;
1060
1061 script_desc__add(s);
1062
1063 return s;
1064
1065out_delete_desc:
1066 script_desc__delete(s);
1067
1068 return NULL;
1069}
1070
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001071static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001072{
1073 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001074 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001075
1076 if (strlen(str) > suffix_len) {
1077 p = str + strlen(str) - suffix_len;
1078 if (!strncmp(p, suffix, suffix_len))
1079 return p;
1080 }
1081
1082 return NULL;
1083}
1084
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001085static int read_script_info(struct script_desc *desc, const char *filename)
1086{
1087 char line[BUFSIZ], *p;
1088 FILE *fp;
1089
1090 fp = fopen(filename, "r");
1091 if (!fp)
1092 return -1;
1093
1094 while (fgets(line, sizeof(line), fp)) {
1095 p = ltrim(line);
1096 if (strlen(p) == 0)
1097 continue;
1098 if (*p != '#')
1099 continue;
1100 p++;
1101 if (strlen(p) && *p == '!')
1102 continue;
1103
1104 p = ltrim(p);
1105 if (strlen(p) && p[strlen(p) - 1] == '\n')
1106 p[strlen(p) - 1] = '\0';
1107
1108 if (!strncmp(p, "description:", strlen("description:"))) {
1109 p += strlen("description:");
1110 desc->half_liner = strdup(ltrim(p));
1111 continue;
1112 }
1113
1114 if (!strncmp(p, "args:", strlen("args:"))) {
1115 p += strlen("args:");
1116 desc->args = strdup(ltrim(p));
1117 continue;
1118 }
1119 }
1120
1121 fclose(fp);
1122
1123 return 0;
1124}
1125
Robert Richter38efb532011-11-25 11:38:40 +01001126static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1127{
1128 char *script_root, *str;
1129
1130 script_root = strdup(script_dirent->d_name);
1131 if (!script_root)
1132 return NULL;
1133
1134 str = (char *)ends_with(script_root, suffix);
1135 if (!str) {
1136 free(script_root);
1137 return NULL;
1138 }
1139
1140 *str = '\0';
1141 return script_root;
1142}
1143
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001144static int list_available_scripts(const struct option *opt __maybe_unused,
1145 const char *s __maybe_unused,
1146 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001147{
1148 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1149 char scripts_path[MAXPATHLEN];
1150 DIR *scripts_dir, *lang_dir;
1151 char script_path[MAXPATHLEN];
1152 char lang_path[MAXPATHLEN];
1153 struct script_desc *desc;
1154 char first_half[BUFSIZ];
1155 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001156
1157 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1158
1159 scripts_dir = opendir(scripts_path);
1160 if (!scripts_dir)
1161 return -1;
1162
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001163 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001164 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1165 lang_dirent.d_name);
1166 lang_dir = opendir(lang_path);
1167 if (!lang_dir)
1168 continue;
1169
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001170 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001171 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1172 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001173 desc = script_desc__findnew(script_root);
1174 snprintf(script_path, MAXPATHLEN, "%s/%s",
1175 lang_path, script_dirent.d_name);
1176 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001177 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001178 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001179 }
1180 }
1181
1182 fprintf(stdout, "List of available trace scripts:\n");
1183 list_for_each_entry(desc, &script_descs, node) {
1184 sprintf(first_half, "%s %s", desc->name,
1185 desc->args ? desc->args : "");
1186 fprintf(stdout, " %-36s %s\n", first_half,
1187 desc->half_liner ? desc->half_liner : "");
1188 }
1189
1190 exit(0);
1191}
1192
Feng Tange5f37052012-09-07 16:42:26 +08001193/*
Feng Tang49e639e2012-10-30 11:56:03 +08001194 * Some scripts specify the required events in their "xxx-record" file,
1195 * this function will check if the events in perf.data match those
1196 * mentioned in the "xxx-record".
1197 *
1198 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1199 * which is covered well now. And new parsing code should be added to
1200 * cover the future complexing formats like event groups etc.
1201 */
1202static int check_ev_match(char *dir_name, char *scriptname,
1203 struct perf_session *session)
1204{
1205 char filename[MAXPATHLEN], evname[128];
1206 char line[BUFSIZ], *p;
1207 struct perf_evsel *pos;
1208 int match, len;
1209 FILE *fp;
1210
1211 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1212
1213 fp = fopen(filename, "r");
1214 if (!fp)
1215 return -1;
1216
1217 while (fgets(line, sizeof(line), fp)) {
1218 p = ltrim(line);
1219 if (*p == '#')
1220 continue;
1221
1222 while (strlen(p)) {
1223 p = strstr(p, "-e");
1224 if (!p)
1225 break;
1226
1227 p += 2;
1228 p = ltrim(p);
1229 len = strcspn(p, " \t");
1230 if (!len)
1231 break;
1232
1233 snprintf(evname, len + 1, "%s", p);
1234
1235 match = 0;
1236 list_for_each_entry(pos,
1237 &session->evlist->entries, node) {
1238 if (!strcmp(perf_evsel__name(pos), evname)) {
1239 match = 1;
1240 break;
1241 }
1242 }
1243
1244 if (!match) {
1245 fclose(fp);
1246 return -1;
1247 }
1248 }
1249 }
1250
1251 fclose(fp);
1252 return 0;
1253}
1254
1255/*
Feng Tange5f37052012-09-07 16:42:26 +08001256 * Return -1 if none is found, otherwise the actual scripts number.
1257 *
1258 * Currently the only user of this function is the script browser, which
1259 * will list all statically runnable scripts, select one, execute it and
1260 * show the output in a perf browser.
1261 */
1262int find_scripts(char **scripts_array, char **scripts_path_array)
1263{
1264 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001265 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001266 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001267 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001268 struct perf_data_file file = {
1269 .path = input_name,
1270 .mode = PERF_DATA_MODE_READ,
1271 };
Feng Tange5f37052012-09-07 16:42:26 +08001272 char *temp;
1273 int i = 0;
1274
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001275 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001276 if (!session)
1277 return -1;
1278
Feng Tange5f37052012-09-07 16:42:26 +08001279 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1280
1281 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001282 if (!scripts_dir) {
1283 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001284 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001285 }
Feng Tange5f37052012-09-07 16:42:26 +08001286
1287 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1288 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1289 lang_dirent.d_name);
1290#ifdef NO_LIBPERL
1291 if (strstr(lang_path, "perl"))
1292 continue;
1293#endif
1294#ifdef NO_LIBPYTHON
1295 if (strstr(lang_path, "python"))
1296 continue;
1297#endif
1298
1299 lang_dir = opendir(lang_path);
1300 if (!lang_dir)
1301 continue;
1302
1303 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1304 /* Skip those real time scripts: xxxtop.p[yl] */
1305 if (strstr(script_dirent.d_name, "top."))
1306 continue;
1307 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1308 script_dirent.d_name);
1309 temp = strchr(script_dirent.d_name, '.');
1310 snprintf(scripts_array[i],
1311 (temp - script_dirent.d_name) + 1,
1312 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001313
1314 if (check_ev_match(lang_path,
1315 scripts_array[i], session))
1316 continue;
1317
Feng Tange5f37052012-09-07 16:42:26 +08001318 i++;
1319 }
Feng Tang49e639e2012-10-30 11:56:03 +08001320 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001321 }
1322
Feng Tang49e639e2012-10-30 11:56:03 +08001323 closedir(scripts_dir);
1324 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001325 return i;
1326}
1327
Tom Zanussi38752942009-12-15 02:53:39 -06001328static char *get_script_path(const char *script_root, const char *suffix)
1329{
1330 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1331 char scripts_path[MAXPATHLEN];
1332 char script_path[MAXPATHLEN];
1333 DIR *scripts_dir, *lang_dir;
1334 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001335 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001336
1337 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1338
1339 scripts_dir = opendir(scripts_path);
1340 if (!scripts_dir)
1341 return NULL;
1342
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001343 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001344 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1345 lang_dirent.d_name);
1346 lang_dir = opendir(lang_path);
1347 if (!lang_dir)
1348 continue;
1349
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001350 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001351 __script_root = get_script_root(&script_dirent, suffix);
1352 if (__script_root && !strcmp(script_root, __script_root)) {
1353 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001354 closedir(lang_dir);
1355 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001356 snprintf(script_path, MAXPATHLEN, "%s/%s",
1357 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001358 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001359 }
1360 free(__script_root);
1361 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001362 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001363 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001364 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001365
Robert Richter38efb532011-11-25 11:38:40 +01001366 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001367}
1368
Tom Zanussib5b87312010-11-10 08:16:51 -06001369static bool is_top_script(const char *script_path)
1370{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001371 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001372}
1373
1374static int has_required_arg(char *script_path)
1375{
1376 struct script_desc *desc;
1377 int n_args = 0;
1378 char *p;
1379
1380 desc = script_desc__new(NULL);
1381
1382 if (read_script_info(desc, script_path))
1383 goto out;
1384
1385 if (!desc->args)
1386 goto out;
1387
1388 for (p = desc->args; *p; p++)
1389 if (*p == '<')
1390 n_args++;
1391out:
1392 script_desc__delete(desc);
1393
1394 return n_args;
1395}
1396
David Ahernd54b1a92012-08-26 12:24:46 -06001397static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001398{
1399 char **__argv = malloc(sizeof(const char *) * argc);
1400
David Ahernd54b1a92012-08-26 12:24:46 -06001401 if (!__argv) {
1402 pr_err("malloc failed\n");
1403 return -1;
1404 }
1405
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001406 memcpy(__argv, argv, sizeof(const char *) * argc);
1407 argc = parse_options(argc, (const char **)__argv, record_options,
1408 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1409 free(__argv);
1410
David Ahernd54b1a92012-08-26 12:24:46 -06001411 system_wide = (argc == 0);
1412
1413 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001414}
1415
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001416int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001417{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001418 bool show_full_info = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001419 char *rec_script_path = NULL;
1420 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001421 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -06001422 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001423 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -06001424 int i, j, err;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001425 struct perf_script script = {
1426 .tool = {
1427 .sample = process_sample_event,
1428 .mmap = perf_event__process_mmap,
1429 .mmap2 = perf_event__process_mmap2,
1430 .comm = perf_event__process_comm,
1431 .exit = perf_event__process_exit,
1432 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001433 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001434 .tracing_data = perf_event__process_tracing_data,
1435 .build_id = perf_event__process_build_id,
1436 .ordered_samples = true,
1437 .ordering_requires_timestamps = true,
1438 },
1439 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001440 const struct option options[] = {
1441 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1442 "dump raw trace in ASCII"),
1443 OPT_INCR('v', "verbose", &verbose,
1444 "be more verbose (show symbol address, etc)"),
1445 OPT_BOOLEAN('L', "Latency", &latency_format,
1446 "show latency attributes (irqs/preemption disabled, etc)"),
1447 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1448 list_available_scripts),
1449 OPT_CALLBACK('s', "script", NULL, "name",
1450 "script file name (lang:script name, script name, or *)",
1451 parse_scriptname),
1452 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1453 "generate perf-script.xx script in specified language"),
1454 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1455 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1456 "do various checks like samples ordering and lost events"),
1457 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1458 "file", "vmlinux pathname"),
1459 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1460 "file", "kallsyms pathname"),
1461 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1462 "When printing symbols do not display call chain"),
1463 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1464 "Look for files with symbols relative to this directory"),
1465 OPT_CALLBACK('f', "fields", NULL, "str",
1466 "comma separated output fields prepend with 'type:'. "
1467 "Valid types: hw,sw,trace,raw. "
1468 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1469 "addr,symoff", parse_output_fields),
1470 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1471 "system-wide collection from all CPUs"),
1472 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1473 "only consider these symbols"),
1474 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1475 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1476 "only display events for these comms"),
1477 OPT_BOOLEAN('I', "show-info", &show_full_info,
1478 "display extended information from perf.data file"),
1479 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1480 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001481 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1482 "Show the fork/comm/exit events"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001483 OPT_END()
1484 };
1485 const char * const script_usage[] = {
1486 "perf script [<options>]",
1487 "perf script [<options>] record <script> [<record-options>] <command>",
1488 "perf script [<options>] report <script> [script-args]",
1489 "perf script [<options>] <script> [<record-options>] <command>",
1490 "perf script [<options>] <top-script> [script-args]",
1491 NULL
1492 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001493 struct perf_data_file file = {
1494 .mode = PERF_DATA_MODE_READ,
1495 };
Tom Zanussi38752942009-12-15 02:53:39 -06001496
Tom Zanussib5b87312010-11-10 08:16:51 -06001497 setup_scripting();
1498
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001499 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001500 PARSE_OPT_STOP_AT_NON_OPTION);
1501
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001502 file.path = input_name;
1503
Tom Zanussib5b87312010-11-10 08:16:51 -06001504 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1505 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1506 if (!rec_script_path)
1507 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001508 }
1509
Tom Zanussib5b87312010-11-10 08:16:51 -06001510 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1511 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1512 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001513 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001514 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001515 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001516 return -1;
1517 }
Tom Zanussi38752942009-12-15 02:53:39 -06001518 }
1519
Ben Hutchings44e668c2010-10-10 16:10:03 +01001520 /* make sure PERF_EXEC_PATH is set for scripts */
1521 perf_set_argv_exec_path(perf_exec_path());
1522
Tom Zanussib5b87312010-11-10 08:16:51 -06001523 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001524 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001525 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001526 pid_t pid;
1527
Tom Zanussib5b87312010-11-10 08:16:51 -06001528 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1529 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1530
1531 if (!rec_script_path && !rep_script_path) {
1532 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001533 " script -l for available scripts.\n", argv[0]);
1534 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001535 }
1536
Tom Zanussib5b87312010-11-10 08:16:51 -06001537 if (is_top_script(argv[0])) {
1538 rep_args = argc - 1;
1539 } else {
1540 int rec_args;
1541
1542 rep_args = has_required_arg(rep_script_path);
1543 rec_args = (argc - 1) - rep_args;
1544 if (rec_args < 0) {
1545 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001546 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001547 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001548 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001549 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001550 }
1551
1552 if (pipe(live_pipe) < 0) {
1553 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001554 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001555 }
1556
1557 pid = fork();
1558 if (pid < 0) {
1559 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001560 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001561 }
1562
1563 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001564 j = 0;
1565
Tom Zanussia0cccc22010-04-01 23:59:25 -05001566 dup2(live_pipe[1], 1);
1567 close(live_pipe[0]);
1568
Robert Richter317df652011-11-25 15:05:25 +01001569 if (is_top_script(argv[0])) {
1570 system_wide = true;
1571 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001572 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1573 err = -1;
1574 goto out;
1575 }
Robert Richter317df652011-11-25 15:05:25 +01001576 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001577
1578 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001579 if (!__argv) {
1580 pr_err("malloc failed\n");
1581 err = -ENOMEM;
1582 goto out;
1583 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001584
Tom Zanussib5b87312010-11-10 08:16:51 -06001585 __argv[j++] = "/bin/sh";
1586 __argv[j++] = rec_script_path;
1587 if (system_wide)
1588 __argv[j++] = "-a";
1589 __argv[j++] = "-q";
1590 __argv[j++] = "-o";
1591 __argv[j++] = "-";
1592 for (i = rep_args + 1; i < argc; i++)
1593 __argv[j++] = argv[i];
1594 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001595
1596 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001597 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001598 exit(-1);
1599 }
1600
1601 dup2(live_pipe[0], 0);
1602 close(live_pipe[1]);
1603
Tom Zanussib5b87312010-11-10 08:16:51 -06001604 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001605 if (!__argv) {
1606 pr_err("malloc failed\n");
1607 err = -ENOMEM;
1608 goto out;
1609 }
1610
Tom Zanussib5b87312010-11-10 08:16:51 -06001611 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001612 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001613 __argv[j++] = rep_script_path;
1614 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001615 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001616 __argv[j++] = "-i";
1617 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001618 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001619
1620 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001621 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001622 exit(-1);
1623 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001624
Tom Zanussib5b87312010-11-10 08:16:51 -06001625 if (rec_script_path)
1626 script_path = rec_script_path;
1627 if (rep_script_path)
1628 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001629
Tom Zanussib5b87312010-11-10 08:16:51 -06001630 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001631 j = 0;
1632
Robert Richter317df652011-11-25 15:05:25 +01001633 if (!rec_script_path)
1634 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001635 else if (!system_wide) {
1636 if (have_cmd(argc - 1, &argv[1]) != 0) {
1637 err = -1;
1638 goto out;
1639 }
1640 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001641
1642 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001643 if (!__argv) {
1644 pr_err("malloc failed\n");
1645 err = -ENOMEM;
1646 goto out;
1647 }
1648
Tom Zanussib5b87312010-11-10 08:16:51 -06001649 __argv[j++] = "/bin/sh";
1650 __argv[j++] = script_path;
1651 if (system_wide)
1652 __argv[j++] = "-a";
1653 for (i = 2; i < argc; i++)
1654 __argv[j++] = argv[i];
1655 __argv[j++] = NULL;
1656
1657 execvp("/bin/sh", (char **)__argv);
1658 free(__argv);
1659 exit(-1);
1660 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001661
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001662 if (symbol__init() < 0)
1663 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -06001664 if (!script_name)
1665 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001666
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001667 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001668 if (session == NULL)
1669 return -ENOMEM;
1670
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001671 script.session = session;
1672
Anton Blanchard5d67be92011-07-04 21:57:50 +10001673 if (cpu_list) {
1674 if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
1675 return -1;
1676 }
1677
Tom Zanussibdb71db2013-01-18 13:51:26 -06001678 if (!script_name && !generate_script_lang)
1679 perf_session__fprintf_info(session, stdout, show_full_info);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001680
David Ahern1424dc92011-03-09 22:23:28 -07001681 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001682 symbol_conf.use_callchain = true;
1683 else
1684 symbol_conf.use_callchain = false;
1685
Tom Zanussi956ffd02009-11-25 01:15:46 -06001686 if (generate_script_lang) {
1687 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001688 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001689
David Ahern2c9e45f72011-03-17 10:03:21 -06001690 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001691 fprintf(stderr,
1692 "custom fields not supported for generated scripts");
1693 return -1;
1694 }
1695
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001696 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06001697 if (input < 0) {
1698 perror("failed to open file");
David Ahernd54b1a92012-08-26 12:24:46 -06001699 return -1;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001700 }
1701
1702 err = fstat(input, &perf_stat);
1703 if (err < 0) {
1704 perror("failed to stat file");
David Ahernd54b1a92012-08-26 12:24:46 -06001705 return -1;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001706 }
1707
1708 if (!perf_stat.st_size) {
1709 fprintf(stderr, "zero-sized file, nothing to do!\n");
David Ahernd54b1a92012-08-26 12:24:46 -06001710 return 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001711 }
1712
1713 scripting_ops = script_spec__lookup(generate_script_lang);
1714 if (!scripting_ops) {
1715 fprintf(stderr, "invalid language specifier");
1716 return -1;
1717 }
1718
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001719 err = scripting_ops->generate_script(session->pevent,
1720 "perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001721 goto out;
1722 }
1723
1724 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001725 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001726 if (err)
1727 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001728 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001729 }
1730
David Ahern9cbdb702011-04-06 21:54:20 -06001731
1732 err = perf_session__check_output_opt(session);
1733 if (err < 0)
1734 goto out;
1735
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001736 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001737
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001738 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001739 cleanup_scripting();
1740out:
1741 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001742}