blob: b392770766ddd040c040fccf825ba41332ebde7b [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 Olsaf5fc14122013-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
283 return 0;
284}
David Ahern745f43e2011-03-09 22:23:26 -0700285
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300286static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700287 struct thread *thread,
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300288 struct perf_evsel *evsel)
David Ahernc70c94b2011-03-09 22:23:25 -0700289{
Arnaldo Carvalho de Melo5bff01f2012-06-12 13:35:44 -0300290 struct perf_event_attr *attr = &evsel->attr;
David Ahernc70c94b2011-03-09 22:23:25 -0700291 unsigned long secs;
292 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700293 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700294
David Ahern745f43e2011-03-09 22:23:26 -0700295 if (PRINT_FIELD(COMM)) {
296 if (latency_format)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200297 printf("%8.8s ", thread__comm_str(thread));
David Ahern787bef12011-05-27 14:28:43 -0600298 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200299 printf("%s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700300 else
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200301 printf("%16s ", thread__comm_str(thread));
David Ahern745f43e2011-03-09 22:23:26 -0700302 }
David Ahernc70c94b2011-03-09 22:23:25 -0700303
David Ahern745f43e2011-03-09 22:23:26 -0700304 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
305 printf("%5d/%-5d ", sample->pid, sample->tid);
306 else if (PRINT_FIELD(PID))
307 printf("%5d ", sample->pid);
308 else if (PRINT_FIELD(TID))
309 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700310
David Ahern745f43e2011-03-09 22:23:26 -0700311 if (PRINT_FIELD(CPU)) {
312 if (latency_format)
313 printf("%3d ", sample->cpu);
314 else
315 printf("[%03d] ", sample->cpu);
316 }
David Ahernc70c94b2011-03-09 22:23:25 -0700317
David Ahern745f43e2011-03-09 22:23:26 -0700318 if (PRINT_FIELD(TIME)) {
319 nsecs = sample->time;
320 secs = nsecs / NSECS_PER_SEC;
321 nsecs -= secs * NSECS_PER_SEC;
322 usecs = nsecs / NSECS_PER_USEC;
323 printf("%5lu.%06lu: ", secs, usecs);
324 }
David Ahernc70c94b2011-03-09 22:23:25 -0700325}
326
Akihiro Nagai95582592012-01-30 13:43:09 +0900327static bool is_bts_event(struct perf_event_attr *attr)
328{
329 return ((attr->type == PERF_TYPE_HARDWARE) &&
330 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
331 (attr->sample_period == 1));
332}
333
David Ahern7cec0922011-05-30 13:08:23 -0600334static bool sample_addr_correlates_sym(struct perf_event_attr *attr)
335{
336 if ((attr->type == PERF_TYPE_SOFTWARE) &&
337 ((attr->config == PERF_COUNT_SW_PAGE_FAULTS) ||
338 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN) ||
339 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)))
340 return true;
341
Akihiro Nagai95582592012-01-30 13:43:09 +0900342 if (is_bts_event(attr))
343 return true;
344
David Ahern7cec0922011-05-30 13:08:23 -0600345 return false;
346}
347
348static void print_sample_addr(union perf_event *event,
349 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200350 struct machine *machine,
David Ahern7cec0922011-05-30 13:08:23 -0600351 struct thread *thread,
352 struct perf_event_attr *attr)
353{
354 struct addr_location al;
355 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
David Ahern7cec0922011-05-30 13:08:23 -0600356
357 printf("%16" PRIx64, sample->addr);
358
359 if (!sample_addr_correlates_sym(attr))
360 return;
361
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200362 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300363 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600364 if (!al.map)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200365 thread__find_addr_map(thread, machine, cpumode, MAP__VARIABLE,
Adrian Hunter326f59b2013-08-08 14:32:27 +0300366 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600367
368 al.cpu = sample->cpu;
369 al.sym = NULL;
370
371 if (al.map)
372 al.sym = map__find_symbol(al.map, al.addr, NULL);
373
374 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900375 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900376 if (PRINT_FIELD(SYMOFFSET))
377 symbol__fprintf_symname_offs(al.sym, &al, stdout);
378 else
379 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600380 }
381
382 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900383 printf(" (");
384 map__fprintf_dsoname(al.map, stdout);
385 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600386 }
387}
388
Akihiro Nagai95582592012-01-30 13:43:09 +0900389static void print_sample_bts(union perf_event *event,
390 struct perf_sample *sample,
391 struct perf_evsel *evsel,
392 struct machine *machine,
393 struct thread *thread)
394{
395 struct perf_event_attr *attr = &evsel->attr;
396
397 /* print branch_from information */
398 if (PRINT_FIELD(IP)) {
399 if (!symbol_conf.use_callchain)
400 printf(" ");
401 else
402 printf("\n");
Jiri Olsa71ad0f52012-08-07 15:20:46 +0200403 perf_evsel__print_ip(evsel, event, sample, machine,
David Ahern307cbb92013-08-07 22:50:53 -0400404 output[attr->type].print_ip_opts,
405 PERF_MAX_STACK_DEPTH);
Akihiro Nagai95582592012-01-30 13:43:09 +0900406 }
407
408 printf(" => ");
409
410 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300411 if (PRINT_FIELD(ADDR) ||
412 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
413 !output[attr->type].user_set))
Akihiro Nagai95582592012-01-30 13:43:09 +0900414 print_sample_addr(event, sample, machine, thread, attr);
415
416 printf("\n");
417}
418
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300419static void process_event(union perf_event *event, struct perf_sample *sample,
420 struct perf_evsel *evsel, struct machine *machine,
David Ahern2eaa1b42013-07-18 16:06:15 -0600421 struct thread *thread,
422 struct addr_location *al __maybe_unused)
David Ahernbe6d8422011-03-09 22:23:23 -0700423{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300424 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700425
David Ahern2c9e45f72011-03-17 10:03:21 -0600426 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700427 return;
428
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300429 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700430
Namhyung Kime944d3d2013-11-18 14:34:52 +0900431 if (PRINT_FIELD(EVNAME)) {
432 const char *evname = perf_evsel__name(evsel);
433 printf("%s: ", evname ? evname : "[unknown]");
434 }
435
Akihiro Nagai95582592012-01-30 13:43:09 +0900436 if (is_bts_event(attr)) {
437 print_sample_bts(event, sample, evsel, machine, thread);
438 return;
439 }
440
David Ahern745f43e2011-03-09 22:23:26 -0700441 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300442 event_format__print(evsel->tp_format, sample->cpu,
443 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600444 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200445 print_sample_addr(event, sample, machine, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600446
David Ahern787bef12011-05-27 14:28:43 -0600447 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700448 if (!symbol_conf.use_callchain)
449 printf(" ");
450 else
451 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400452
Jiri Olsa71ad0f52012-08-07 15:20:46 +0200453 perf_evsel__print_ip(evsel, event, sample, machine,
David Ahern307cbb92013-08-07 22:50:53 -0400454 output[attr->type].print_ip_opts,
455 PERF_MAX_STACK_DEPTH);
David Ahernc0230b22011-03-09 22:23:27 -0700456 }
457
David Ahernc70c94b2011-03-09 22:23:25 -0700458 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700459}
460
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300461static int default_start_script(const char *script __maybe_unused,
462 int argc __maybe_unused,
463 const char **argv __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600464{
465 return 0;
466}
467
468static int default_stop_script(void)
469{
470 return 0;
471}
472
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300473static int default_generate_script(struct pevent *pevent __maybe_unused,
474 const char *outfile __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600475{
476 return 0;
477}
478
479static struct scripting_ops default_scripting_ops = {
480 .start_script = default_start_script,
481 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700482 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600483 .generate_script = default_generate_script,
484};
485
486static struct scripting_ops *scripting_ops;
487
488static void setup_scripting(void)
489{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600490 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600491 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600492
Tom Zanussi956ffd02009-11-25 01:15:46 -0600493 scripting_ops = &default_scripting_ops;
494}
495
496static int cleanup_scripting(void)
497{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100498 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500499
Tom Zanussi956ffd02009-11-25 01:15:46 -0600500 return scripting_ops->stop_script();
501}
502
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300503static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200504 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200505 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300506 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200507 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200508{
David Aherne7984b72011-11-21 10:02:52 -0700509 struct addr_location al;
Adrian Hunteref893252013-08-27 11:23:06 +0300510 struct thread *thread = machine__findnew_thread(machine, sample->pid,
511 sample->tid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200512
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200513 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200514 pr_debug("problem processing %d event, skipping it.\n",
515 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200516 return -1;
517 }
518
David Ahern1424dc92011-03-09 22:23:28 -0700519 if (debug_mode) {
520 if (sample->time < last_timestamp) {
521 pr_err("Samples misordered, previous: %" PRIu64
522 " this: %" PRIu64 "\n", last_timestamp,
523 sample->time);
524 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200525 }
David Ahern1424dc92011-03-09 22:23:28 -0700526 last_timestamp = sample->time;
527 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200528 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000529
Adrian Huntere44baa32013-08-08 14:32:25 +0300530 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700531 pr_err("problem processing %d event, skipping it.\n",
532 event->header.type);
533 return -1;
534 }
535
536 if (al.filtered)
537 return 0;
538
Anton Blanchard5d67be92011-07-04 21:57:50 +1000539 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
540 return 0;
541
David Ahern2eaa1b42013-07-18 16:06:15 -0600542 scripting_ops->process_event(event, sample, evsel, machine, thread, &al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200543
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200544 evsel->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200545 return 0;
546}
547
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300548struct perf_script {
549 struct perf_tool tool;
550 struct perf_session *session;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200551};
552
Adrian Hunter7ea95722013-11-01 15:51:30 +0200553static int process_attr(struct perf_tool *tool, union perf_event *event,
554 struct perf_evlist **pevlist)
555{
556 struct perf_script *scr = container_of(tool, struct perf_script, tool);
557 struct perf_evlist *evlist;
558 struct perf_evsel *evsel, *pos;
559 int err;
560
561 err = perf_event__process_attr(tool, event, pevlist);
562 if (err)
563 return err;
564
565 evlist = *pevlist;
566 evsel = perf_evlist__last(*pevlist);
567
568 if (evsel->attr.type >= PERF_TYPE_MAX)
569 return 0;
570
571 list_for_each_entry(pos, &evlist->entries, node) {
572 if (pos->attr.type == evsel->attr.type && pos != evsel)
573 return 0;
574 }
575
576 set_print_ip_opts(&evsel->attr);
577
578 return perf_evsel__check_attr(evsel, scr->session);
579}
580
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300581static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500582{
583 session_done = 1;
584}
585
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300586static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200587{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200588 int ret;
589
Tom Zanussic239da32010-04-01 23:59:18 -0500590 signal(SIGINT, sig_handler);
591
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300592 ret = perf_session__process_events(script->session, &script->tool);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200593
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200594 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200595 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200596
597 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200598}
599
Tom Zanussi956ffd02009-11-25 01:15:46 -0600600struct script_spec {
601 struct list_head node;
602 struct scripting_ops *ops;
603 char spec[0];
604};
605
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200606static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600607
608static struct script_spec *script_spec__new(const char *spec,
609 struct scripting_ops *ops)
610{
611 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
612
613 if (s != NULL) {
614 strcpy(s->spec, spec);
615 s->ops = ops;
616 }
617
618 return s;
619}
620
Tom Zanussi956ffd02009-11-25 01:15:46 -0600621static void script_spec__add(struct script_spec *s)
622{
623 list_add_tail(&s->node, &script_specs);
624}
625
626static struct script_spec *script_spec__find(const char *spec)
627{
628 struct script_spec *s;
629
630 list_for_each_entry(s, &script_specs, node)
631 if (strcasecmp(s->spec, spec) == 0)
632 return s;
633 return NULL;
634}
635
636static struct script_spec *script_spec__findnew(const char *spec,
637 struct scripting_ops *ops)
638{
639 struct script_spec *s = script_spec__find(spec);
640
641 if (s)
642 return s;
643
644 s = script_spec__new(spec, ops);
645 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +0900646 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600647
648 script_spec__add(s);
649
650 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600651}
652
653int script_spec_register(const char *spec, struct scripting_ops *ops)
654{
655 struct script_spec *s;
656
657 s = script_spec__find(spec);
658 if (s)
659 return -1;
660
661 s = script_spec__findnew(spec, ops);
662 if (!s)
663 return -1;
664
665 return 0;
666}
667
668static struct scripting_ops *script_spec__lookup(const char *spec)
669{
670 struct script_spec *s = script_spec__find(spec);
671 if (!s)
672 return NULL;
673
674 return s->ops;
675}
676
677static void list_available_languages(void)
678{
679 struct script_spec *s;
680
681 fprintf(stderr, "\n");
682 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100683 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600684
685 list_for_each_entry(s, &script_specs, node)
686 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
687
688 fprintf(stderr, "\n");
689}
690
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300691static int parse_scriptname(const struct option *opt __maybe_unused,
692 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600693{
694 char spec[PATH_MAX];
695 const char *script, *ext;
696 int len;
697
Tom Zanussif526d682010-01-27 02:27:52 -0600698 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600699 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600700 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600701 }
702
703 script = strchr(str, ':');
704 if (script) {
705 len = script - str;
706 if (len >= PATH_MAX) {
707 fprintf(stderr, "invalid language specifier");
708 return -1;
709 }
710 strncpy(spec, str, len);
711 spec[len] = '\0';
712 scripting_ops = script_spec__lookup(spec);
713 if (!scripting_ops) {
714 fprintf(stderr, "invalid language specifier");
715 return -1;
716 }
717 script++;
718 } else {
719 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100720 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600721 if (!ext) {
722 fprintf(stderr, "invalid script extension");
723 return -1;
724 }
725 scripting_ops = script_spec__lookup(++ext);
726 if (!scripting_ops) {
727 fprintf(stderr, "invalid script extension");
728 return -1;
729 }
730 }
731
732 script_name = strdup(script);
733
734 return 0;
735}
736
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300737static int parse_output_fields(const struct option *opt __maybe_unused,
738 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -0700739{
740 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -0500741 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -0600742 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700743 int rc = 0;
744 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700745 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700746
747 if (!str)
748 return -ENOMEM;
749
David Ahern2c9e45f72011-03-17 10:03:21 -0600750 /* first word can state for which event type the user is specifying
751 * the fields. If no type exists, the specified fields apply to all
752 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700753 */
David Ahern2c9e45f72011-03-17 10:03:21 -0600754 tok = strchr(str, ':');
755 if (tok) {
756 *tok = '\0';
757 tok++;
758 if (!strcmp(str, "hw"))
759 type = PERF_TYPE_HARDWARE;
760 else if (!strcmp(str, "sw"))
761 type = PERF_TYPE_SOFTWARE;
762 else if (!strcmp(str, "trace"))
763 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -0700764 else if (!strcmp(str, "raw"))
765 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -0600766 else {
767 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +0100768 rc = -EINVAL;
769 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -0600770 }
771
772 if (output[type].user_set)
773 pr_warning("Overriding previous field request for %s events.\n",
774 event_type(type));
775
776 output[type].fields = 0;
777 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600778 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -0600779
780 } else {
781 tok = str;
782 if (strlen(str) == 0) {
783 fprintf(stderr,
784 "Cannot set fields to 'none' for all event types.\n");
785 rc = -EINVAL;
786 goto out;
787 }
788
789 if (output_set_by_user())
790 pr_warning("Overriding previous field request for all events.\n");
791
792 for (j = 0; j < PERF_TYPE_MAX; ++j) {
793 output[j].fields = 0;
794 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600795 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -0600796 }
David Ahern1424dc92011-03-09 22:23:28 -0700797 }
798
David Ahern2c9e45f72011-03-17 10:03:21 -0600799 tok = strtok(tok, ",");
800 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -0700801 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600802 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -0700803 break;
David Ahern745f43e2011-03-09 22:23:26 -0700804 }
805 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600806 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -0700807 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -0600808 goto out;
809 }
810
811 if (type == -1) {
812 /* add user option to all events types for
813 * which it is valid
814 */
815 for (j = 0; j < PERF_TYPE_MAX; ++j) {
816 if (output[j].invalid_fields & all_output_options[i].field) {
817 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
818 all_output_options[i].str, event_type(j));
819 } else
820 output[j].fields |= all_output_options[i].field;
821 }
822 } else {
823 if (output[type].invalid_fields & all_output_options[i].field) {
824 fprintf(stderr, "\'%s\' not valid for %s events.\n",
825 all_output_options[i].str, event_type(type));
826
827 rc = -EINVAL;
828 goto out;
829 }
830 output[type].fields |= all_output_options[i].field;
831 }
832
833 tok = strtok(NULL, ",");
834 }
835
836 if (type >= 0) {
837 if (output[type].fields == 0) {
838 pr_debug("No fields requested for %s type. "
839 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -0700840 }
David Ahern1424dc92011-03-09 22:23:28 -0700841 }
David Ahern745f43e2011-03-09 22:23:26 -0700842
David Ahern2c9e45f72011-03-17 10:03:21 -0600843out:
David Ahern745f43e2011-03-09 22:23:26 -0700844 free(str);
845 return rc;
846}
847
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600848/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
849static int is_directory(const char *base_path, const struct dirent *dent)
850{
851 char path[PATH_MAX];
852 struct stat st;
853
854 sprintf(path, "%s/%s", base_path, dent->d_name);
855 if (stat(path, &st))
856 return 0;
857
858 return S_ISDIR(st.st_mode);
859}
860
861#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600862 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
863 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600864 if ((lang_dirent.d_type == DT_DIR || \
865 (lang_dirent.d_type == DT_UNKNOWN && \
866 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600867 (strcmp(lang_dirent.d_name, ".")) && \
868 (strcmp(lang_dirent.d_name, "..")))
869
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600870#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600871 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
872 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600873 if (script_dirent.d_type != DT_DIR && \
874 (script_dirent.d_type != DT_UNKNOWN || \
875 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600876
877
878#define RECORD_SUFFIX "-record"
879#define REPORT_SUFFIX "-report"
880
881struct script_desc {
882 struct list_head node;
883 char *name;
884 char *half_liner;
885 char *args;
886};
887
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200888static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600889
890static struct script_desc *script_desc__new(const char *name)
891{
892 struct script_desc *s = zalloc(sizeof(*s));
893
Tom Zanussib5b87312010-11-10 08:16:51 -0600894 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600895 s->name = strdup(name);
896
897 return s;
898}
899
900static void script_desc__delete(struct script_desc *s)
901{
902 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600903 free(s->half_liner);
904 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600905 free(s);
906}
907
908static void script_desc__add(struct script_desc *s)
909{
910 list_add_tail(&s->node, &script_descs);
911}
912
913static struct script_desc *script_desc__find(const char *name)
914{
915 struct script_desc *s;
916
917 list_for_each_entry(s, &script_descs, node)
918 if (strcasecmp(s->name, name) == 0)
919 return s;
920 return NULL;
921}
922
923static struct script_desc *script_desc__findnew(const char *name)
924{
925 struct script_desc *s = script_desc__find(name);
926
927 if (s)
928 return s;
929
930 s = script_desc__new(name);
931 if (!s)
932 goto out_delete_desc;
933
934 script_desc__add(s);
935
936 return s;
937
938out_delete_desc:
939 script_desc__delete(s);
940
941 return NULL;
942}
943
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200944static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600945{
946 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200947 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600948
949 if (strlen(str) > suffix_len) {
950 p = str + strlen(str) - suffix_len;
951 if (!strncmp(p, suffix, suffix_len))
952 return p;
953 }
954
955 return NULL;
956}
957
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600958static int read_script_info(struct script_desc *desc, const char *filename)
959{
960 char line[BUFSIZ], *p;
961 FILE *fp;
962
963 fp = fopen(filename, "r");
964 if (!fp)
965 return -1;
966
967 while (fgets(line, sizeof(line), fp)) {
968 p = ltrim(line);
969 if (strlen(p) == 0)
970 continue;
971 if (*p != '#')
972 continue;
973 p++;
974 if (strlen(p) && *p == '!')
975 continue;
976
977 p = ltrim(p);
978 if (strlen(p) && p[strlen(p) - 1] == '\n')
979 p[strlen(p) - 1] = '\0';
980
981 if (!strncmp(p, "description:", strlen("description:"))) {
982 p += strlen("description:");
983 desc->half_liner = strdup(ltrim(p));
984 continue;
985 }
986
987 if (!strncmp(p, "args:", strlen("args:"))) {
988 p += strlen("args:");
989 desc->args = strdup(ltrim(p));
990 continue;
991 }
992 }
993
994 fclose(fp);
995
996 return 0;
997}
998
Robert Richter38efb532011-11-25 11:38:40 +0100999static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1000{
1001 char *script_root, *str;
1002
1003 script_root = strdup(script_dirent->d_name);
1004 if (!script_root)
1005 return NULL;
1006
1007 str = (char *)ends_with(script_root, suffix);
1008 if (!str) {
1009 free(script_root);
1010 return NULL;
1011 }
1012
1013 *str = '\0';
1014 return script_root;
1015}
1016
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001017static int list_available_scripts(const struct option *opt __maybe_unused,
1018 const char *s __maybe_unused,
1019 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001020{
1021 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1022 char scripts_path[MAXPATHLEN];
1023 DIR *scripts_dir, *lang_dir;
1024 char script_path[MAXPATHLEN];
1025 char lang_path[MAXPATHLEN];
1026 struct script_desc *desc;
1027 char first_half[BUFSIZ];
1028 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001029
1030 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1031
1032 scripts_dir = opendir(scripts_path);
1033 if (!scripts_dir)
1034 return -1;
1035
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001036 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001037 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1038 lang_dirent.d_name);
1039 lang_dir = opendir(lang_path);
1040 if (!lang_dir)
1041 continue;
1042
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001043 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001044 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1045 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001046 desc = script_desc__findnew(script_root);
1047 snprintf(script_path, MAXPATHLEN, "%s/%s",
1048 lang_path, script_dirent.d_name);
1049 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001050 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001051 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001052 }
1053 }
1054
1055 fprintf(stdout, "List of available trace scripts:\n");
1056 list_for_each_entry(desc, &script_descs, node) {
1057 sprintf(first_half, "%s %s", desc->name,
1058 desc->args ? desc->args : "");
1059 fprintf(stdout, " %-36s %s\n", first_half,
1060 desc->half_liner ? desc->half_liner : "");
1061 }
1062
1063 exit(0);
1064}
1065
Feng Tange5f37052012-09-07 16:42:26 +08001066/*
Feng Tang49e639e2012-10-30 11:56:03 +08001067 * Some scripts specify the required events in their "xxx-record" file,
1068 * this function will check if the events in perf.data match those
1069 * mentioned in the "xxx-record".
1070 *
1071 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1072 * which is covered well now. And new parsing code should be added to
1073 * cover the future complexing formats like event groups etc.
1074 */
1075static int check_ev_match(char *dir_name, char *scriptname,
1076 struct perf_session *session)
1077{
1078 char filename[MAXPATHLEN], evname[128];
1079 char line[BUFSIZ], *p;
1080 struct perf_evsel *pos;
1081 int match, len;
1082 FILE *fp;
1083
1084 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1085
1086 fp = fopen(filename, "r");
1087 if (!fp)
1088 return -1;
1089
1090 while (fgets(line, sizeof(line), fp)) {
1091 p = ltrim(line);
1092 if (*p == '#')
1093 continue;
1094
1095 while (strlen(p)) {
1096 p = strstr(p, "-e");
1097 if (!p)
1098 break;
1099
1100 p += 2;
1101 p = ltrim(p);
1102 len = strcspn(p, " \t");
1103 if (!len)
1104 break;
1105
1106 snprintf(evname, len + 1, "%s", p);
1107
1108 match = 0;
1109 list_for_each_entry(pos,
1110 &session->evlist->entries, node) {
1111 if (!strcmp(perf_evsel__name(pos), evname)) {
1112 match = 1;
1113 break;
1114 }
1115 }
1116
1117 if (!match) {
1118 fclose(fp);
1119 return -1;
1120 }
1121 }
1122 }
1123
1124 fclose(fp);
1125 return 0;
1126}
1127
1128/*
Feng Tange5f37052012-09-07 16:42:26 +08001129 * Return -1 if none is found, otherwise the actual scripts number.
1130 *
1131 * Currently the only user of this function is the script browser, which
1132 * will list all statically runnable scripts, select one, execute it and
1133 * show the output in a perf browser.
1134 */
1135int find_scripts(char **scripts_array, char **scripts_path_array)
1136{
1137 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001138 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001139 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001140 struct perf_session *session;
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001141 struct perf_data_file file = {
1142 .path = input_name,
1143 .mode = PERF_DATA_MODE_READ,
1144 };
Feng Tange5f37052012-09-07 16:42:26 +08001145 char *temp;
1146 int i = 0;
1147
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001148 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001149 if (!session)
1150 return -1;
1151
Feng Tange5f37052012-09-07 16:42:26 +08001152 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1153
1154 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001155 if (!scripts_dir) {
1156 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001157 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001158 }
Feng Tange5f37052012-09-07 16:42:26 +08001159
1160 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1161 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1162 lang_dirent.d_name);
1163#ifdef NO_LIBPERL
1164 if (strstr(lang_path, "perl"))
1165 continue;
1166#endif
1167#ifdef NO_LIBPYTHON
1168 if (strstr(lang_path, "python"))
1169 continue;
1170#endif
1171
1172 lang_dir = opendir(lang_path);
1173 if (!lang_dir)
1174 continue;
1175
1176 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1177 /* Skip those real time scripts: xxxtop.p[yl] */
1178 if (strstr(script_dirent.d_name, "top."))
1179 continue;
1180 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1181 script_dirent.d_name);
1182 temp = strchr(script_dirent.d_name, '.');
1183 snprintf(scripts_array[i],
1184 (temp - script_dirent.d_name) + 1,
1185 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001186
1187 if (check_ev_match(lang_path,
1188 scripts_array[i], session))
1189 continue;
1190
Feng Tange5f37052012-09-07 16:42:26 +08001191 i++;
1192 }
Feng Tang49e639e2012-10-30 11:56:03 +08001193 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001194 }
1195
Feng Tang49e639e2012-10-30 11:56:03 +08001196 closedir(scripts_dir);
1197 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001198 return i;
1199}
1200
Tom Zanussi38752942009-12-15 02:53:39 -06001201static char *get_script_path(const char *script_root, const char *suffix)
1202{
1203 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1204 char scripts_path[MAXPATHLEN];
1205 char script_path[MAXPATHLEN];
1206 DIR *scripts_dir, *lang_dir;
1207 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001208 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001209
1210 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1211
1212 scripts_dir = opendir(scripts_path);
1213 if (!scripts_dir)
1214 return NULL;
1215
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001216 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001217 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1218 lang_dirent.d_name);
1219 lang_dir = opendir(lang_path);
1220 if (!lang_dir)
1221 continue;
1222
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001223 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001224 __script_root = get_script_root(&script_dirent, suffix);
1225 if (__script_root && !strcmp(script_root, __script_root)) {
1226 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001227 closedir(lang_dir);
1228 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001229 snprintf(script_path, MAXPATHLEN, "%s/%s",
1230 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001231 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001232 }
1233 free(__script_root);
1234 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001235 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001236 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001237 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001238
Robert Richter38efb532011-11-25 11:38:40 +01001239 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001240}
1241
Tom Zanussib5b87312010-11-10 08:16:51 -06001242static bool is_top_script(const char *script_path)
1243{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001244 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001245}
1246
1247static int has_required_arg(char *script_path)
1248{
1249 struct script_desc *desc;
1250 int n_args = 0;
1251 char *p;
1252
1253 desc = script_desc__new(NULL);
1254
1255 if (read_script_info(desc, script_path))
1256 goto out;
1257
1258 if (!desc->args)
1259 goto out;
1260
1261 for (p = desc->args; *p; p++)
1262 if (*p == '<')
1263 n_args++;
1264out:
1265 script_desc__delete(desc);
1266
1267 return n_args;
1268}
1269
David Ahernd54b1a92012-08-26 12:24:46 -06001270static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001271{
1272 char **__argv = malloc(sizeof(const char *) * argc);
1273
David Ahernd54b1a92012-08-26 12:24:46 -06001274 if (!__argv) {
1275 pr_err("malloc failed\n");
1276 return -1;
1277 }
1278
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001279 memcpy(__argv, argv, sizeof(const char *) * argc);
1280 argc = parse_options(argc, (const char **)__argv, record_options,
1281 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1282 free(__argv);
1283
David Ahernd54b1a92012-08-26 12:24:46 -06001284 system_wide = (argc == 0);
1285
1286 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001287}
1288
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001289int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001290{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001291 bool show_full_info = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001292 char *rec_script_path = NULL;
1293 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001294 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -06001295 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001296 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -06001297 int i, j, err;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001298 struct perf_script script = {
1299 .tool = {
1300 .sample = process_sample_event,
1301 .mmap = perf_event__process_mmap,
1302 .mmap2 = perf_event__process_mmap2,
1303 .comm = perf_event__process_comm,
1304 .exit = perf_event__process_exit,
1305 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001306 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001307 .tracing_data = perf_event__process_tracing_data,
1308 .build_id = perf_event__process_build_id,
1309 .ordered_samples = true,
1310 .ordering_requires_timestamps = true,
1311 },
1312 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001313 const struct option options[] = {
1314 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1315 "dump raw trace in ASCII"),
1316 OPT_INCR('v', "verbose", &verbose,
1317 "be more verbose (show symbol address, etc)"),
1318 OPT_BOOLEAN('L', "Latency", &latency_format,
1319 "show latency attributes (irqs/preemption disabled, etc)"),
1320 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1321 list_available_scripts),
1322 OPT_CALLBACK('s', "script", NULL, "name",
1323 "script file name (lang:script name, script name, or *)",
1324 parse_scriptname),
1325 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1326 "generate perf-script.xx script in specified language"),
1327 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1328 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1329 "do various checks like samples ordering and lost events"),
1330 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1331 "file", "vmlinux pathname"),
1332 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1333 "file", "kallsyms pathname"),
1334 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1335 "When printing symbols do not display call chain"),
1336 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1337 "Look for files with symbols relative to this directory"),
1338 OPT_CALLBACK('f', "fields", NULL, "str",
1339 "comma separated output fields prepend with 'type:'. "
1340 "Valid types: hw,sw,trace,raw. "
1341 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1342 "addr,symoff", parse_output_fields),
1343 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1344 "system-wide collection from all CPUs"),
1345 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1346 "only consider these symbols"),
1347 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1348 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1349 "only display events for these comms"),
1350 OPT_BOOLEAN('I', "show-info", &show_full_info,
1351 "display extended information from perf.data file"),
1352 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1353 "Show the path of [kernel.kallsyms]"),
1354 OPT_END()
1355 };
1356 const char * const script_usage[] = {
1357 "perf script [<options>]",
1358 "perf script [<options>] record <script> [<record-options>] <command>",
1359 "perf script [<options>] report <script> [script-args]",
1360 "perf script [<options>] <script> [<record-options>] <command>",
1361 "perf script [<options>] <top-script> [script-args]",
1362 NULL
1363 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001364 struct perf_data_file file = {
1365 .mode = PERF_DATA_MODE_READ,
1366 };
Tom Zanussi38752942009-12-15 02:53:39 -06001367
Tom Zanussib5b87312010-11-10 08:16:51 -06001368 setup_scripting();
1369
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001370 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001371 PARSE_OPT_STOP_AT_NON_OPTION);
1372
Jiri Olsaf5fc14122013-10-15 16:27:32 +02001373 file.path = input_name;
1374
Tom Zanussib5b87312010-11-10 08:16:51 -06001375 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1376 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1377 if (!rec_script_path)
1378 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001379 }
1380
Tom Zanussib5b87312010-11-10 08:16:51 -06001381 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1382 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1383 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001384 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001385 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001386 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001387 return -1;
1388 }
Tom Zanussi38752942009-12-15 02:53:39 -06001389 }
1390
Ben Hutchings44e668c2010-10-10 16:10:03 +01001391 /* make sure PERF_EXEC_PATH is set for scripts */
1392 perf_set_argv_exec_path(perf_exec_path());
1393
Tom Zanussib5b87312010-11-10 08:16:51 -06001394 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001395 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001396 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001397 pid_t pid;
1398
Tom Zanussib5b87312010-11-10 08:16:51 -06001399 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1400 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1401
1402 if (!rec_script_path && !rep_script_path) {
1403 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001404 " script -l for available scripts.\n", argv[0]);
1405 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001406 }
1407
Tom Zanussib5b87312010-11-10 08:16:51 -06001408 if (is_top_script(argv[0])) {
1409 rep_args = argc - 1;
1410 } else {
1411 int rec_args;
1412
1413 rep_args = has_required_arg(rep_script_path);
1414 rec_args = (argc - 1) - rep_args;
1415 if (rec_args < 0) {
1416 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001417 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001418 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001419 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001420 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001421 }
1422
1423 if (pipe(live_pipe) < 0) {
1424 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001425 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001426 }
1427
1428 pid = fork();
1429 if (pid < 0) {
1430 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001431 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001432 }
1433
1434 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001435 j = 0;
1436
Tom Zanussia0cccc22010-04-01 23:59:25 -05001437 dup2(live_pipe[1], 1);
1438 close(live_pipe[0]);
1439
Robert Richter317df652011-11-25 15:05:25 +01001440 if (is_top_script(argv[0])) {
1441 system_wide = true;
1442 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001443 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1444 err = -1;
1445 goto out;
1446 }
Robert Richter317df652011-11-25 15:05:25 +01001447 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001448
1449 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001450 if (!__argv) {
1451 pr_err("malloc failed\n");
1452 err = -ENOMEM;
1453 goto out;
1454 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001455
Tom Zanussib5b87312010-11-10 08:16:51 -06001456 __argv[j++] = "/bin/sh";
1457 __argv[j++] = rec_script_path;
1458 if (system_wide)
1459 __argv[j++] = "-a";
1460 __argv[j++] = "-q";
1461 __argv[j++] = "-o";
1462 __argv[j++] = "-";
1463 for (i = rep_args + 1; i < argc; i++)
1464 __argv[j++] = argv[i];
1465 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001466
1467 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001468 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001469 exit(-1);
1470 }
1471
1472 dup2(live_pipe[0], 0);
1473 close(live_pipe[1]);
1474
Tom Zanussib5b87312010-11-10 08:16:51 -06001475 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001476 if (!__argv) {
1477 pr_err("malloc failed\n");
1478 err = -ENOMEM;
1479 goto out;
1480 }
1481
Tom Zanussib5b87312010-11-10 08:16:51 -06001482 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001483 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001484 __argv[j++] = rep_script_path;
1485 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001486 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001487 __argv[j++] = "-i";
1488 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001489 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001490
1491 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001492 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001493 exit(-1);
1494 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001495
Tom Zanussib5b87312010-11-10 08:16:51 -06001496 if (rec_script_path)
1497 script_path = rec_script_path;
1498 if (rep_script_path)
1499 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001500
Tom Zanussib5b87312010-11-10 08:16:51 -06001501 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001502 j = 0;
1503
Robert Richter317df652011-11-25 15:05:25 +01001504 if (!rec_script_path)
1505 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001506 else if (!system_wide) {
1507 if (have_cmd(argc - 1, &argv[1]) != 0) {
1508 err = -1;
1509 goto out;
1510 }
1511 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001512
1513 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001514 if (!__argv) {
1515 pr_err("malloc failed\n");
1516 err = -ENOMEM;
1517 goto out;
1518 }
1519
Tom Zanussib5b87312010-11-10 08:16:51 -06001520 __argv[j++] = "/bin/sh";
1521 __argv[j++] = script_path;
1522 if (system_wide)
1523 __argv[j++] = "-a";
1524 for (i = 2; i < argc; i++)
1525 __argv[j++] = argv[i];
1526 __argv[j++] = NULL;
1527
1528 execvp("/bin/sh", (char **)__argv);
1529 free(__argv);
1530 exit(-1);
1531 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001532
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001533 if (symbol__init() < 0)
1534 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -06001535 if (!script_name)
1536 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001537
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001538 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001539 if (session == NULL)
1540 return -ENOMEM;
1541
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001542 script.session = session;
1543
Anton Blanchard5d67be92011-07-04 21:57:50 +10001544 if (cpu_list) {
1545 if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
1546 return -1;
1547 }
1548
Tom Zanussibdb71db2013-01-18 13:51:26 -06001549 if (!script_name && !generate_script_lang)
1550 perf_session__fprintf_info(session, stdout, show_full_info);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001551
David Ahern1424dc92011-03-09 22:23:28 -07001552 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001553 symbol_conf.use_callchain = true;
1554 else
1555 symbol_conf.use_callchain = false;
1556
Tom Zanussi956ffd02009-11-25 01:15:46 -06001557 if (generate_script_lang) {
1558 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001559 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001560
David Ahern2c9e45f72011-03-17 10:03:21 -06001561 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001562 fprintf(stderr,
1563 "custom fields not supported for generated scripts");
1564 return -1;
1565 }
1566
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001567 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06001568 if (input < 0) {
1569 perror("failed to open file");
David Ahernd54b1a92012-08-26 12:24:46 -06001570 return -1;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001571 }
1572
1573 err = fstat(input, &perf_stat);
1574 if (err < 0) {
1575 perror("failed to stat file");
David Ahernd54b1a92012-08-26 12:24:46 -06001576 return -1;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001577 }
1578
1579 if (!perf_stat.st_size) {
1580 fprintf(stderr, "zero-sized file, nothing to do!\n");
David Ahernd54b1a92012-08-26 12:24:46 -06001581 return 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001582 }
1583
1584 scripting_ops = script_spec__lookup(generate_script_lang);
1585 if (!scripting_ops) {
1586 fprintf(stderr, "invalid language specifier");
1587 return -1;
1588 }
1589
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001590 err = scripting_ops->generate_script(session->pevent,
1591 "perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001592 goto out;
1593 }
1594
1595 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001596 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001597 if (err)
1598 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001599 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001600 }
1601
David Ahern9cbdb702011-04-06 21:54:20 -06001602
1603 err = perf_session__check_output_opt(session);
1604 if (err < 0)
1605 goto out;
1606
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001607 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001608
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001609 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001610 cleanup_scripting();
1611out:
1612 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001613}