blob: 4484886dcf0866a5fb26488de6c3d14bd0a08040 [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,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200417 struct thread *thread,
418 struct addr_location *al)
Akihiro Nagai95582592012-01-30 13:43:09 +0900419{
420 struct perf_event_attr *attr = &evsel->attr;
421
422 /* print branch_from information */
423 if (PRINT_FIELD(IP)) {
424 if (!symbol_conf.use_callchain)
425 printf(" ");
426 else
427 printf("\n");
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200428 perf_evsel__print_ip(evsel, sample, machine, al,
David Ahern307cbb92013-08-07 22:50:53 -0400429 output[attr->type].print_ip_opts,
430 PERF_MAX_STACK_DEPTH);
Akihiro Nagai95582592012-01-30 13:43:09 +0900431 }
432
433 printf(" => ");
434
435 /* print branch_to information */
Adrian Hunter243be3d2013-10-18 15:29:14 +0300436 if (PRINT_FIELD(ADDR) ||
437 ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
438 !output[attr->type].user_set))
Akihiro Nagai95582592012-01-30 13:43:09 +0900439 print_sample_addr(event, sample, machine, thread, attr);
440
441 printf("\n");
442}
443
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300444static void process_event(union perf_event *event, struct perf_sample *sample,
445 struct perf_evsel *evsel, struct machine *machine,
David Ahern2eaa1b42013-07-18 16:06:15 -0600446 struct thread *thread,
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200447 struct addr_location *al)
David Ahernbe6d8422011-03-09 22:23:23 -0700448{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300449 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700450
David Ahern2c9e45f72011-03-17 10:03:21 -0600451 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700452 return;
453
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300454 print_sample_start(sample, thread, evsel);
David Ahern745f43e2011-03-09 22:23:26 -0700455
Namhyung Kime944d3d2013-11-18 14:34:52 +0900456 if (PRINT_FIELD(EVNAME)) {
457 const char *evname = perf_evsel__name(evsel);
458 printf("%s: ", evname ? evname : "[unknown]");
459 }
460
Akihiro Nagai95582592012-01-30 13:43:09 +0900461 if (is_bts_event(attr)) {
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200462 print_sample_bts(event, sample, evsel, machine, thread, al);
Akihiro Nagai95582592012-01-30 13:43:09 +0900463 return;
464 }
465
David Ahern745f43e2011-03-09 22:23:26 -0700466 if (PRINT_FIELD(TRACE))
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300467 event_format__print(evsel->tp_format, sample->cpu,
468 sample->raw_data, sample->raw_size);
David Ahern7cec0922011-05-30 13:08:23 -0600469 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200470 print_sample_addr(event, sample, machine, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600471
David Ahern787bef12011-05-27 14:28:43 -0600472 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700473 if (!symbol_conf.use_callchain)
474 printf(" ");
475 else
476 printf("\n");
David Aherna6ffaf92013-08-07 22:50:51 -0400477
Adrian Huntera2cb3cf2013-12-04 16:16:36 +0200478 perf_evsel__print_ip(evsel, sample, machine, al,
David Ahern307cbb92013-08-07 22:50:53 -0400479 output[attr->type].print_ip_opts,
480 PERF_MAX_STACK_DEPTH);
David Ahernc0230b22011-03-09 22:23:27 -0700481 }
482
David Ahernc70c94b2011-03-09 22:23:25 -0700483 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700484}
485
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300486static int default_start_script(const char *script __maybe_unused,
487 int argc __maybe_unused,
488 const char **argv __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600489{
490 return 0;
491}
492
493static int default_stop_script(void)
494{
495 return 0;
496}
497
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300498static int default_generate_script(struct pevent *pevent __maybe_unused,
499 const char *outfile __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600500{
501 return 0;
502}
503
504static struct scripting_ops default_scripting_ops = {
505 .start_script = default_start_script,
506 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700507 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600508 .generate_script = default_generate_script,
509};
510
511static struct scripting_ops *scripting_ops;
512
513static void setup_scripting(void)
514{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600515 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600516 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600517
Tom Zanussi956ffd02009-11-25 01:15:46 -0600518 scripting_ops = &default_scripting_ops;
519}
520
521static int cleanup_scripting(void)
522{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100523 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500524
Tom Zanussi956ffd02009-11-25 01:15:46 -0600525 return scripting_ops->stop_script();
526}
527
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300528static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200529 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200530 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300531 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200532 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200533{
David Aherne7984b72011-11-21 10:02:52 -0700534 struct addr_location al;
Adrian Hunteref893252013-08-27 11:23:06 +0300535 struct thread *thread = machine__findnew_thread(machine, sample->pid,
536 sample->tid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200537
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200538 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200539 pr_debug("problem processing %d event, skipping it.\n",
540 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200541 return -1;
542 }
543
David Ahern1424dc92011-03-09 22:23:28 -0700544 if (debug_mode) {
545 if (sample->time < last_timestamp) {
546 pr_err("Samples misordered, previous: %" PRIu64
547 " this: %" PRIu64 "\n", last_timestamp,
548 sample->time);
549 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200550 }
David Ahern1424dc92011-03-09 22:23:28 -0700551 last_timestamp = sample->time;
552 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200553 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000554
Adrian Huntere44baa32013-08-08 14:32:25 +0300555 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
David Aherne7984b72011-11-21 10:02:52 -0700556 pr_err("problem processing %d event, skipping it.\n",
557 event->header.type);
558 return -1;
559 }
560
561 if (al.filtered)
562 return 0;
563
Anton Blanchard5d67be92011-07-04 21:57:50 +1000564 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
565 return 0;
566
David Ahern2eaa1b42013-07-18 16:06:15 -0600567 scripting_ops->process_event(event, sample, evsel, machine, thread, &al);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200568
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200569 evsel->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200570 return 0;
571}
572
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300573struct perf_script {
574 struct perf_tool tool;
575 struct perf_session *session;
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900576 bool show_task_events;
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900577 bool show_mmap_events;
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200578};
579
Adrian Hunter7ea95722013-11-01 15:51:30 +0200580static int process_attr(struct perf_tool *tool, union perf_event *event,
581 struct perf_evlist **pevlist)
582{
583 struct perf_script *scr = container_of(tool, struct perf_script, tool);
584 struct perf_evlist *evlist;
585 struct perf_evsel *evsel, *pos;
586 int err;
587
588 err = perf_event__process_attr(tool, event, pevlist);
589 if (err)
590 return err;
591
592 evlist = *pevlist;
593 evsel = perf_evlist__last(*pevlist);
594
595 if (evsel->attr.type >= PERF_TYPE_MAX)
596 return 0;
597
598 list_for_each_entry(pos, &evlist->entries, node) {
599 if (pos->attr.type == evsel->attr.type && pos != evsel)
600 return 0;
601 }
602
603 set_print_ip_opts(&evsel->attr);
604
605 return perf_evsel__check_attr(evsel, scr->session);
606}
607
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900608static int process_comm_event(struct perf_tool *tool,
609 union perf_event *event,
610 struct perf_sample *sample,
611 struct machine *machine)
612{
613 struct thread *thread;
614 struct perf_script *script = container_of(tool, struct perf_script, tool);
615 struct perf_session *session = script->session;
616 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
617 int ret = -1;
618
619 thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
620 if (thread == NULL) {
621 pr_debug("problem processing COMM event, skipping it.\n");
622 return -1;
623 }
624
625 if (perf_event__process_comm(tool, event, sample, machine) < 0)
626 goto out;
627
628 if (!evsel->attr.sample_id_all) {
629 sample->cpu = 0;
630 sample->time = 0;
631 sample->tid = event->comm.tid;
632 sample->pid = event->comm.pid;
633 }
634 print_sample_start(sample, thread, evsel);
635 perf_event__fprintf(event, stdout);
636 ret = 0;
637
638out:
639 return ret;
640}
641
642static int process_fork_event(struct perf_tool *tool,
643 union perf_event *event,
644 struct perf_sample *sample,
645 struct machine *machine)
646{
647 struct thread *thread;
648 struct perf_script *script = container_of(tool, struct perf_script, tool);
649 struct perf_session *session = script->session;
650 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
651
652 if (perf_event__process_fork(tool, event, sample, machine) < 0)
653 return -1;
654
655 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
656 if (thread == NULL) {
657 pr_debug("problem processing FORK event, skipping it.\n");
658 return -1;
659 }
660
661 if (!evsel->attr.sample_id_all) {
662 sample->cpu = 0;
663 sample->time = event->fork.time;
664 sample->tid = event->fork.tid;
665 sample->pid = event->fork.pid;
666 }
667 print_sample_start(sample, thread, evsel);
668 perf_event__fprintf(event, stdout);
669
670 return 0;
671}
672static int process_exit_event(struct perf_tool *tool,
673 union perf_event *event,
674 struct perf_sample *sample,
675 struct machine *machine)
676{
677 struct thread *thread;
678 struct perf_script *script = container_of(tool, struct perf_script, tool);
679 struct perf_session *session = script->session;
680 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
681
682 thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
683 if (thread == NULL) {
684 pr_debug("problem processing EXIT event, skipping it.\n");
685 return -1;
686 }
687
688 if (!evsel->attr.sample_id_all) {
689 sample->cpu = 0;
690 sample->time = 0;
691 sample->tid = event->comm.tid;
692 sample->pid = event->comm.pid;
693 }
694 print_sample_start(sample, thread, evsel);
695 perf_event__fprintf(event, stdout);
696
697 if (perf_event__process_exit(tool, event, sample, machine) < 0)
698 return -1;
699
700 return 0;
701}
702
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900703static int process_mmap_event(struct perf_tool *tool,
704 union perf_event *event,
705 struct perf_sample *sample,
706 struct machine *machine)
707{
708 struct thread *thread;
709 struct perf_script *script = container_of(tool, struct perf_script, tool);
710 struct perf_session *session = script->session;
711 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
712
713 if (perf_event__process_mmap(tool, event, sample, machine) < 0)
714 return -1;
715
716 thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
717 if (thread == NULL) {
718 pr_debug("problem processing MMAP event, skipping it.\n");
719 return -1;
720 }
721
722 if (!evsel->attr.sample_id_all) {
723 sample->cpu = 0;
724 sample->time = 0;
725 sample->tid = event->mmap.tid;
726 sample->pid = event->mmap.pid;
727 }
728 print_sample_start(sample, thread, evsel);
729 perf_event__fprintf(event, stdout);
730
731 return 0;
732}
733
734static int process_mmap2_event(struct perf_tool *tool,
735 union perf_event *event,
736 struct perf_sample *sample,
737 struct machine *machine)
738{
739 struct thread *thread;
740 struct perf_script *script = container_of(tool, struct perf_script, tool);
741 struct perf_session *session = script->session;
742 struct perf_evsel *evsel = perf_evlist__first(session->evlist);
743
744 if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
745 return -1;
746
747 thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
748 if (thread == NULL) {
749 pr_debug("problem processing MMAP2 event, skipping it.\n");
750 return -1;
751 }
752
753 if (!evsel->attr.sample_id_all) {
754 sample->cpu = 0;
755 sample->time = 0;
756 sample->tid = event->mmap2.tid;
757 sample->pid = event->mmap2.pid;
758 }
759 print_sample_start(sample, thread, evsel);
760 perf_event__fprintf(event, stdout);
761
762 return 0;
763}
764
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300765static void sig_handler(int sig __maybe_unused)
Tom Zanussic239da32010-04-01 23:59:18 -0500766{
767 session_done = 1;
768}
769
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300770static int __cmd_script(struct perf_script *script)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200771{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200772 int ret;
773
Tom Zanussic239da32010-04-01 23:59:18 -0500774 signal(SIGINT, sig_handler);
775
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900776 /* override event processing functions */
777 if (script->show_task_events) {
778 script->tool.comm = process_comm_event;
779 script->tool.fork = process_fork_event;
780 script->tool.exit = process_exit_event;
781 }
Namhyung Kimba1ddf42013-11-26 17:54:26 +0900782 if (script->show_mmap_events) {
783 script->tool.mmap = process_mmap_event;
784 script->tool.mmap2 = process_mmap2_event;
785 }
Namhyung Kimad7ebb92013-11-26 17:51:12 +0900786
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +0300787 ret = perf_session__process_events(script->session, &script->tool);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200788
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200789 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200790 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200791
792 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200793}
794
Tom Zanussi956ffd02009-11-25 01:15:46 -0600795struct script_spec {
796 struct list_head node;
797 struct scripting_ops *ops;
798 char spec[0];
799};
800
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200801static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600802
803static struct script_spec *script_spec__new(const char *spec,
804 struct scripting_ops *ops)
805{
806 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
807
808 if (s != NULL) {
809 strcpy(s->spec, spec);
810 s->ops = ops;
811 }
812
813 return s;
814}
815
Tom Zanussi956ffd02009-11-25 01:15:46 -0600816static void script_spec__add(struct script_spec *s)
817{
818 list_add_tail(&s->node, &script_specs);
819}
820
821static struct script_spec *script_spec__find(const char *spec)
822{
823 struct script_spec *s;
824
825 list_for_each_entry(s, &script_specs, node)
826 if (strcasecmp(s->spec, spec) == 0)
827 return s;
828 return NULL;
829}
830
831static struct script_spec *script_spec__findnew(const char *spec,
832 struct scripting_ops *ops)
833{
834 struct script_spec *s = script_spec__find(spec);
835
836 if (s)
837 return s;
838
839 s = script_spec__new(spec, ops);
840 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +0900841 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600842
843 script_spec__add(s);
844
845 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600846}
847
848int script_spec_register(const char *spec, struct scripting_ops *ops)
849{
850 struct script_spec *s;
851
852 s = script_spec__find(spec);
853 if (s)
854 return -1;
855
856 s = script_spec__findnew(spec, ops);
857 if (!s)
858 return -1;
859
860 return 0;
861}
862
863static struct scripting_ops *script_spec__lookup(const char *spec)
864{
865 struct script_spec *s = script_spec__find(spec);
866 if (!s)
867 return NULL;
868
869 return s->ops;
870}
871
872static void list_available_languages(void)
873{
874 struct script_spec *s;
875
876 fprintf(stderr, "\n");
877 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100878 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600879
880 list_for_each_entry(s, &script_specs, node)
881 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
882
883 fprintf(stderr, "\n");
884}
885
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300886static int parse_scriptname(const struct option *opt __maybe_unused,
887 const char *str, int unset __maybe_unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600888{
889 char spec[PATH_MAX];
890 const char *script, *ext;
891 int len;
892
Tom Zanussif526d682010-01-27 02:27:52 -0600893 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600894 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600895 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600896 }
897
898 script = strchr(str, ':');
899 if (script) {
900 len = script - str;
901 if (len >= PATH_MAX) {
902 fprintf(stderr, "invalid language specifier");
903 return -1;
904 }
905 strncpy(spec, str, len);
906 spec[len] = '\0';
907 scripting_ops = script_spec__lookup(spec);
908 if (!scripting_ops) {
909 fprintf(stderr, "invalid language specifier");
910 return -1;
911 }
912 script++;
913 } else {
914 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100915 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600916 if (!ext) {
917 fprintf(stderr, "invalid script extension");
918 return -1;
919 }
920 scripting_ops = script_spec__lookup(++ext);
921 if (!scripting_ops) {
922 fprintf(stderr, "invalid script extension");
923 return -1;
924 }
925 }
926
927 script_name = strdup(script);
928
929 return 0;
930}
931
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300932static int parse_output_fields(const struct option *opt __maybe_unused,
933 const char *arg, int unset __maybe_unused)
David Ahern745f43e2011-03-09 22:23:26 -0700934{
935 char *tok;
Sasha Levin50ca19a2012-12-20 14:11:19 -0500936 int i, imax = ARRAY_SIZE(all_output_options);
David Ahern2c9e45f72011-03-17 10:03:21 -0600937 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700938 int rc = 0;
939 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700940 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700941
942 if (!str)
943 return -ENOMEM;
944
David Ahern2c9e45f72011-03-17 10:03:21 -0600945 /* first word can state for which event type the user is specifying
946 * the fields. If no type exists, the specified fields apply to all
947 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700948 */
David Ahern2c9e45f72011-03-17 10:03:21 -0600949 tok = strchr(str, ':');
950 if (tok) {
951 *tok = '\0';
952 tok++;
953 if (!strcmp(str, "hw"))
954 type = PERF_TYPE_HARDWARE;
955 else if (!strcmp(str, "sw"))
956 type = PERF_TYPE_SOFTWARE;
957 else if (!strcmp(str, "trace"))
958 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -0700959 else if (!strcmp(str, "raw"))
960 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -0600961 else {
962 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +0100963 rc = -EINVAL;
964 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -0600965 }
966
967 if (output[type].user_set)
968 pr_warning("Overriding previous field request for %s events.\n",
969 event_type(type));
970
971 output[type].fields = 0;
972 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600973 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -0600974
975 } else {
976 tok = str;
977 if (strlen(str) == 0) {
978 fprintf(stderr,
979 "Cannot set fields to 'none' for all event types.\n");
980 rc = -EINVAL;
981 goto out;
982 }
983
984 if (output_set_by_user())
985 pr_warning("Overriding previous field request for all events.\n");
986
987 for (j = 0; j < PERF_TYPE_MAX; ++j) {
988 output[j].fields = 0;
989 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600990 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -0600991 }
David Ahern1424dc92011-03-09 22:23:28 -0700992 }
993
David Ahern2c9e45f72011-03-17 10:03:21 -0600994 tok = strtok(tok, ",");
995 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -0700996 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600997 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -0700998 break;
David Ahern745f43e2011-03-09 22:23:26 -0700999 }
1000 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -06001001 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -07001002 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -06001003 goto out;
1004 }
1005
1006 if (type == -1) {
1007 /* add user option to all events types for
1008 * which it is valid
1009 */
1010 for (j = 0; j < PERF_TYPE_MAX; ++j) {
1011 if (output[j].invalid_fields & all_output_options[i].field) {
1012 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
1013 all_output_options[i].str, event_type(j));
1014 } else
1015 output[j].fields |= all_output_options[i].field;
1016 }
1017 } else {
1018 if (output[type].invalid_fields & all_output_options[i].field) {
1019 fprintf(stderr, "\'%s\' not valid for %s events.\n",
1020 all_output_options[i].str, event_type(type));
1021
1022 rc = -EINVAL;
1023 goto out;
1024 }
1025 output[type].fields |= all_output_options[i].field;
1026 }
1027
1028 tok = strtok(NULL, ",");
1029 }
1030
1031 if (type >= 0) {
1032 if (output[type].fields == 0) {
1033 pr_debug("No fields requested for %s type. "
1034 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -07001035 }
David Ahern1424dc92011-03-09 22:23:28 -07001036 }
David Ahern745f43e2011-03-09 22:23:26 -07001037
David Ahern2c9e45f72011-03-17 10:03:21 -06001038out:
David Ahern745f43e2011-03-09 22:23:26 -07001039 free(str);
1040 return rc;
1041}
1042
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001043/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
1044static int is_directory(const char *base_path, const struct dirent *dent)
1045{
1046 char path[PATH_MAX];
1047 struct stat st;
1048
1049 sprintf(path, "%s/%s", base_path, dent->d_name);
1050 if (stat(path, &st))
1051 return 0;
1052
1053 return S_ISDIR(st.st_mode);
1054}
1055
1056#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001057 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
1058 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001059 if ((lang_dirent.d_type == DT_DIR || \
1060 (lang_dirent.d_type == DT_UNKNOWN && \
1061 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001062 (strcmp(lang_dirent.d_name, ".")) && \
1063 (strcmp(lang_dirent.d_name, "..")))
1064
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001065#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001066 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
1067 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001068 if (script_dirent.d_type != DT_DIR && \
1069 (script_dirent.d_type != DT_UNKNOWN || \
1070 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001071
1072
1073#define RECORD_SUFFIX "-record"
1074#define REPORT_SUFFIX "-report"
1075
1076struct script_desc {
1077 struct list_head node;
1078 char *name;
1079 char *half_liner;
1080 char *args;
1081};
1082
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -02001083static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001084
1085static struct script_desc *script_desc__new(const char *name)
1086{
1087 struct script_desc *s = zalloc(sizeof(*s));
1088
Tom Zanussib5b87312010-11-10 08:16:51 -06001089 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001090 s->name = strdup(name);
1091
1092 return s;
1093}
1094
1095static void script_desc__delete(struct script_desc *s)
1096{
1097 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001098 free(s->half_liner);
1099 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001100 free(s);
1101}
1102
1103static void script_desc__add(struct script_desc *s)
1104{
1105 list_add_tail(&s->node, &script_descs);
1106}
1107
1108static struct script_desc *script_desc__find(const char *name)
1109{
1110 struct script_desc *s;
1111
1112 list_for_each_entry(s, &script_descs, node)
1113 if (strcasecmp(s->name, name) == 0)
1114 return s;
1115 return NULL;
1116}
1117
1118static struct script_desc *script_desc__findnew(const char *name)
1119{
1120 struct script_desc *s = script_desc__find(name);
1121
1122 if (s)
1123 return s;
1124
1125 s = script_desc__new(name);
1126 if (!s)
1127 goto out_delete_desc;
1128
1129 script_desc__add(s);
1130
1131 return s;
1132
1133out_delete_desc:
1134 script_desc__delete(s);
1135
1136 return NULL;
1137}
1138
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001139static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001140{
1141 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001142 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001143
1144 if (strlen(str) > suffix_len) {
1145 p = str + strlen(str) - suffix_len;
1146 if (!strncmp(p, suffix, suffix_len))
1147 return p;
1148 }
1149
1150 return NULL;
1151}
1152
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001153static int read_script_info(struct script_desc *desc, const char *filename)
1154{
1155 char line[BUFSIZ], *p;
1156 FILE *fp;
1157
1158 fp = fopen(filename, "r");
1159 if (!fp)
1160 return -1;
1161
1162 while (fgets(line, sizeof(line), fp)) {
1163 p = ltrim(line);
1164 if (strlen(p) == 0)
1165 continue;
1166 if (*p != '#')
1167 continue;
1168 p++;
1169 if (strlen(p) && *p == '!')
1170 continue;
1171
1172 p = ltrim(p);
1173 if (strlen(p) && p[strlen(p) - 1] == '\n')
1174 p[strlen(p) - 1] = '\0';
1175
1176 if (!strncmp(p, "description:", strlen("description:"))) {
1177 p += strlen("description:");
1178 desc->half_liner = strdup(ltrim(p));
1179 continue;
1180 }
1181
1182 if (!strncmp(p, "args:", strlen("args:"))) {
1183 p += strlen("args:");
1184 desc->args = strdup(ltrim(p));
1185 continue;
1186 }
1187 }
1188
1189 fclose(fp);
1190
1191 return 0;
1192}
1193
Robert Richter38efb532011-11-25 11:38:40 +01001194static char *get_script_root(struct dirent *script_dirent, const char *suffix)
1195{
1196 char *script_root, *str;
1197
1198 script_root = strdup(script_dirent->d_name);
1199 if (!script_root)
1200 return NULL;
1201
1202 str = (char *)ends_with(script_root, suffix);
1203 if (!str) {
1204 free(script_root);
1205 return NULL;
1206 }
1207
1208 *str = '\0';
1209 return script_root;
1210}
1211
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001212static int list_available_scripts(const struct option *opt __maybe_unused,
1213 const char *s __maybe_unused,
1214 int unset __maybe_unused)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001215{
1216 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1217 char scripts_path[MAXPATHLEN];
1218 DIR *scripts_dir, *lang_dir;
1219 char script_path[MAXPATHLEN];
1220 char lang_path[MAXPATHLEN];
1221 struct script_desc *desc;
1222 char first_half[BUFSIZ];
1223 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001224
1225 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1226
1227 scripts_dir = opendir(scripts_path);
1228 if (!scripts_dir)
1229 return -1;
1230
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001231 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001232 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1233 lang_dirent.d_name);
1234 lang_dir = opendir(lang_path);
1235 if (!lang_dir)
1236 continue;
1237
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001238 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001239 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1240 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001241 desc = script_desc__findnew(script_root);
1242 snprintf(script_path, MAXPATHLEN, "%s/%s",
1243 lang_path, script_dirent.d_name);
1244 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001245 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001246 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001247 }
1248 }
1249
1250 fprintf(stdout, "List of available trace scripts:\n");
1251 list_for_each_entry(desc, &script_descs, node) {
1252 sprintf(first_half, "%s %s", desc->name,
1253 desc->args ? desc->args : "");
1254 fprintf(stdout, " %-36s %s\n", first_half,
1255 desc->half_liner ? desc->half_liner : "");
1256 }
1257
1258 exit(0);
1259}
1260
Feng Tange5f37052012-09-07 16:42:26 +08001261/*
Feng Tang49e639e2012-10-30 11:56:03 +08001262 * Some scripts specify the required events in their "xxx-record" file,
1263 * this function will check if the events in perf.data match those
1264 * mentioned in the "xxx-record".
1265 *
1266 * Fixme: All existing "xxx-record" are all in good formats "-e event ",
1267 * which is covered well now. And new parsing code should be added to
1268 * cover the future complexing formats like event groups etc.
1269 */
1270static int check_ev_match(char *dir_name, char *scriptname,
1271 struct perf_session *session)
1272{
1273 char filename[MAXPATHLEN], evname[128];
1274 char line[BUFSIZ], *p;
1275 struct perf_evsel *pos;
1276 int match, len;
1277 FILE *fp;
1278
1279 sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
1280
1281 fp = fopen(filename, "r");
1282 if (!fp)
1283 return -1;
1284
1285 while (fgets(line, sizeof(line), fp)) {
1286 p = ltrim(line);
1287 if (*p == '#')
1288 continue;
1289
1290 while (strlen(p)) {
1291 p = strstr(p, "-e");
1292 if (!p)
1293 break;
1294
1295 p += 2;
1296 p = ltrim(p);
1297 len = strcspn(p, " \t");
1298 if (!len)
1299 break;
1300
1301 snprintf(evname, len + 1, "%s", p);
1302
1303 match = 0;
1304 list_for_each_entry(pos,
1305 &session->evlist->entries, node) {
1306 if (!strcmp(perf_evsel__name(pos), evname)) {
1307 match = 1;
1308 break;
1309 }
1310 }
1311
1312 if (!match) {
1313 fclose(fp);
1314 return -1;
1315 }
1316 }
1317 }
1318
1319 fclose(fp);
1320 return 0;
1321}
1322
1323/*
Feng Tange5f37052012-09-07 16:42:26 +08001324 * Return -1 if none is found, otherwise the actual scripts number.
1325 *
1326 * Currently the only user of this function is the script browser, which
1327 * will list all statically runnable scripts, select one, execute it and
1328 * show the output in a perf browser.
1329 */
1330int find_scripts(char **scripts_array, char **scripts_path_array)
1331{
1332 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
Feng Tang49e639e2012-10-30 11:56:03 +08001333 char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
Feng Tange5f37052012-09-07 16:42:26 +08001334 DIR *scripts_dir, *lang_dir;
Feng Tang49e639e2012-10-30 11:56:03 +08001335 struct perf_session *session;
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001336 struct perf_data_file file = {
1337 .path = input_name,
1338 .mode = PERF_DATA_MODE_READ,
1339 };
Feng Tange5f37052012-09-07 16:42:26 +08001340 char *temp;
1341 int i = 0;
1342
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001343 session = perf_session__new(&file, false, NULL);
Feng Tang49e639e2012-10-30 11:56:03 +08001344 if (!session)
1345 return -1;
1346
Feng Tange5f37052012-09-07 16:42:26 +08001347 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1348
1349 scripts_dir = opendir(scripts_path);
Feng Tang49e639e2012-10-30 11:56:03 +08001350 if (!scripts_dir) {
1351 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001352 return -1;
Feng Tang49e639e2012-10-30 11:56:03 +08001353 }
Feng Tange5f37052012-09-07 16:42:26 +08001354
1355 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
1356 snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
1357 lang_dirent.d_name);
1358#ifdef NO_LIBPERL
1359 if (strstr(lang_path, "perl"))
1360 continue;
1361#endif
1362#ifdef NO_LIBPYTHON
1363 if (strstr(lang_path, "python"))
1364 continue;
1365#endif
1366
1367 lang_dir = opendir(lang_path);
1368 if (!lang_dir)
1369 continue;
1370
1371 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
1372 /* Skip those real time scripts: xxxtop.p[yl] */
1373 if (strstr(script_dirent.d_name, "top."))
1374 continue;
1375 sprintf(scripts_path_array[i], "%s/%s", lang_path,
1376 script_dirent.d_name);
1377 temp = strchr(script_dirent.d_name, '.');
1378 snprintf(scripts_array[i],
1379 (temp - script_dirent.d_name) + 1,
1380 "%s", script_dirent.d_name);
Feng Tang49e639e2012-10-30 11:56:03 +08001381
1382 if (check_ev_match(lang_path,
1383 scripts_array[i], session))
1384 continue;
1385
Feng Tange5f37052012-09-07 16:42:26 +08001386 i++;
1387 }
Feng Tang49e639e2012-10-30 11:56:03 +08001388 closedir(lang_dir);
Feng Tange5f37052012-09-07 16:42:26 +08001389 }
1390
Feng Tang49e639e2012-10-30 11:56:03 +08001391 closedir(scripts_dir);
1392 perf_session__delete(session);
Feng Tange5f37052012-09-07 16:42:26 +08001393 return i;
1394}
1395
Tom Zanussi38752942009-12-15 02:53:39 -06001396static char *get_script_path(const char *script_root, const char *suffix)
1397{
1398 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1399 char scripts_path[MAXPATHLEN];
1400 char script_path[MAXPATHLEN];
1401 DIR *scripts_dir, *lang_dir;
1402 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001403 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001404
1405 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1406
1407 scripts_dir = opendir(scripts_path);
1408 if (!scripts_dir)
1409 return NULL;
1410
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001411 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001412 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1413 lang_dirent.d_name);
1414 lang_dir = opendir(lang_path);
1415 if (!lang_dir)
1416 continue;
1417
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001418 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001419 __script_root = get_script_root(&script_dirent, suffix);
1420 if (__script_root && !strcmp(script_root, __script_root)) {
1421 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001422 closedir(lang_dir);
1423 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001424 snprintf(script_path, MAXPATHLEN, "%s/%s",
1425 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001426 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001427 }
1428 free(__script_root);
1429 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001430 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001431 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001432 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001433
Robert Richter38efb532011-11-25 11:38:40 +01001434 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001435}
1436
Tom Zanussib5b87312010-11-10 08:16:51 -06001437static bool is_top_script(const char *script_path)
1438{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001439 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001440}
1441
1442static int has_required_arg(char *script_path)
1443{
1444 struct script_desc *desc;
1445 int n_args = 0;
1446 char *p;
1447
1448 desc = script_desc__new(NULL);
1449
1450 if (read_script_info(desc, script_path))
1451 goto out;
1452
1453 if (!desc->args)
1454 goto out;
1455
1456 for (p = desc->args; *p; p++)
1457 if (*p == '<')
1458 n_args++;
1459out:
1460 script_desc__delete(desc);
1461
1462 return n_args;
1463}
1464
David Ahernd54b1a92012-08-26 12:24:46 -06001465static int have_cmd(int argc, const char **argv)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001466{
1467 char **__argv = malloc(sizeof(const char *) * argc);
1468
David Ahernd54b1a92012-08-26 12:24:46 -06001469 if (!__argv) {
1470 pr_err("malloc failed\n");
1471 return -1;
1472 }
1473
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001474 memcpy(__argv, argv, sizeof(const char *) * argc);
1475 argc = parse_options(argc, (const char **)__argv, record_options,
1476 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1477 free(__argv);
1478
David Ahernd54b1a92012-08-26 12:24:46 -06001479 system_wide = (argc == 0);
1480
1481 return 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001482}
1483
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001484int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001485{
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001486 bool show_full_info = false;
Tom Zanussib5b87312010-11-10 08:16:51 -06001487 char *rec_script_path = NULL;
1488 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001489 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -06001490 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001491 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -06001492 int i, j, err;
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001493 struct perf_script script = {
1494 .tool = {
1495 .sample = process_sample_event,
1496 .mmap = perf_event__process_mmap,
1497 .mmap2 = perf_event__process_mmap2,
1498 .comm = perf_event__process_comm,
1499 .exit = perf_event__process_exit,
1500 .fork = perf_event__process_fork,
Adrian Hunter7ea95722013-11-01 15:51:30 +02001501 .attr = process_attr,
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001502 .tracing_data = perf_event__process_tracing_data,
1503 .build_id = perf_event__process_build_id,
1504 .ordered_samples = true,
1505 .ordering_requires_timestamps = true,
1506 },
1507 };
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001508 const struct option options[] = {
1509 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1510 "dump raw trace in ASCII"),
1511 OPT_INCR('v', "verbose", &verbose,
1512 "be more verbose (show symbol address, etc)"),
1513 OPT_BOOLEAN('L', "Latency", &latency_format,
1514 "show latency attributes (irqs/preemption disabled, etc)"),
1515 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1516 list_available_scripts),
1517 OPT_CALLBACK('s', "script", NULL, "name",
1518 "script file name (lang:script name, script name, or *)",
1519 parse_scriptname),
1520 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
1521 "generate perf-script.xx script in specified language"),
1522 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1523 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1524 "do various checks like samples ordering and lost events"),
1525 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1526 "file", "vmlinux pathname"),
1527 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1528 "file", "kallsyms pathname"),
1529 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1530 "When printing symbols do not display call chain"),
1531 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1532 "Look for files with symbols relative to this directory"),
1533 OPT_CALLBACK('f', "fields", NULL, "str",
1534 "comma separated output fields prepend with 'type:'. "
1535 "Valid types: hw,sw,trace,raw. "
1536 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1537 "addr,symoff", parse_output_fields),
1538 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1539 "system-wide collection from all CPUs"),
1540 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1541 "only consider these symbols"),
1542 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1543 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1544 "only display events for these comms"),
1545 OPT_BOOLEAN('I', "show-info", &show_full_info,
1546 "display extended information from perf.data file"),
1547 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1548 "Show the path of [kernel.kallsyms]"),
Namhyung Kimad7ebb92013-11-26 17:51:12 +09001549 OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
1550 "Show the fork/comm/exit events"),
Namhyung Kimba1ddf42013-11-26 17:54:26 +09001551 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1552 "Show the mmap events"),
Arnaldo Carvalho de Melo69b64702012-10-01 15:20:58 -03001553 OPT_END()
1554 };
1555 const char * const script_usage[] = {
1556 "perf script [<options>]",
1557 "perf script [<options>] record <script> [<record-options>] <command>",
1558 "perf script [<options>] report <script> [script-args]",
1559 "perf script [<options>] <script> [<record-options>] <command>",
1560 "perf script [<options>] <top-script> [script-args]",
1561 NULL
1562 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001563 struct perf_data_file file = {
1564 .mode = PERF_DATA_MODE_READ,
1565 };
Tom Zanussi38752942009-12-15 02:53:39 -06001566
Tom Zanussib5b87312010-11-10 08:16:51 -06001567 setup_scripting();
1568
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001569 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001570 PARSE_OPT_STOP_AT_NON_OPTION);
1571
Jiri Olsaf5fc1412013-10-15 16:27:32 +02001572 file.path = input_name;
1573
Tom Zanussib5b87312010-11-10 08:16:51 -06001574 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1575 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1576 if (!rec_script_path)
1577 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001578 }
1579
Tom Zanussib5b87312010-11-10 08:16:51 -06001580 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1581 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1582 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001583 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001584 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001585 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001586 return -1;
1587 }
Tom Zanussi38752942009-12-15 02:53:39 -06001588 }
1589
Ben Hutchings44e668c2010-10-10 16:10:03 +01001590 /* make sure PERF_EXEC_PATH is set for scripts */
1591 perf_set_argv_exec_path(perf_exec_path());
1592
Tom Zanussib5b87312010-11-10 08:16:51 -06001593 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001594 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001595 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001596 pid_t pid;
1597
Tom Zanussib5b87312010-11-10 08:16:51 -06001598 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1599 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1600
1601 if (!rec_script_path && !rep_script_path) {
1602 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001603 " script -l for available scripts.\n", argv[0]);
1604 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001605 }
1606
Tom Zanussib5b87312010-11-10 08:16:51 -06001607 if (is_top_script(argv[0])) {
1608 rep_args = argc - 1;
1609 } else {
1610 int rec_args;
1611
1612 rep_args = has_required_arg(rep_script_path);
1613 rec_args = (argc - 1) - rep_args;
1614 if (rec_args < 0) {
1615 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001616 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001617 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001618 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001619 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001620 }
1621
1622 if (pipe(live_pipe) < 0) {
1623 perror("failed to create pipe");
David Ahernd54b1a92012-08-26 12:24:46 -06001624 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001625 }
1626
1627 pid = fork();
1628 if (pid < 0) {
1629 perror("failed to fork");
David Ahernd54b1a92012-08-26 12:24:46 -06001630 return -1;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001631 }
1632
1633 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001634 j = 0;
1635
Tom Zanussia0cccc22010-04-01 23:59:25 -05001636 dup2(live_pipe[1], 1);
1637 close(live_pipe[0]);
1638
Robert Richter317df652011-11-25 15:05:25 +01001639 if (is_top_script(argv[0])) {
1640 system_wide = true;
1641 } else if (!system_wide) {
David Ahernd54b1a92012-08-26 12:24:46 -06001642 if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
1643 err = -1;
1644 goto out;
1645 }
Robert Richter317df652011-11-25 15:05:25 +01001646 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001647
1648 __argv = malloc((argc + 6) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001649 if (!__argv) {
1650 pr_err("malloc failed\n");
1651 err = -ENOMEM;
1652 goto out;
1653 }
Tom Zanussie8719ad2010-11-10 07:52:32 -06001654
Tom Zanussib5b87312010-11-10 08:16:51 -06001655 __argv[j++] = "/bin/sh";
1656 __argv[j++] = rec_script_path;
1657 if (system_wide)
1658 __argv[j++] = "-a";
1659 __argv[j++] = "-q";
1660 __argv[j++] = "-o";
1661 __argv[j++] = "-";
1662 for (i = rep_args + 1; i < argc; i++)
1663 __argv[j++] = argv[i];
1664 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001665
1666 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001667 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001668 exit(-1);
1669 }
1670
1671 dup2(live_pipe[0], 0);
1672 close(live_pipe[1]);
1673
Tom Zanussib5b87312010-11-10 08:16:51 -06001674 __argv = malloc((argc + 4) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001675 if (!__argv) {
1676 pr_err("malloc failed\n");
1677 err = -ENOMEM;
1678 goto out;
1679 }
1680
Tom Zanussib5b87312010-11-10 08:16:51 -06001681 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001682 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001683 __argv[j++] = rep_script_path;
1684 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001685 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001686 __argv[j++] = "-i";
1687 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001688 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001689
1690 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001691 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001692 exit(-1);
1693 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001694
Tom Zanussib5b87312010-11-10 08:16:51 -06001695 if (rec_script_path)
1696 script_path = rec_script_path;
1697 if (rep_script_path)
1698 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001699
Tom Zanussib5b87312010-11-10 08:16:51 -06001700 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001701 j = 0;
1702
Robert Richter317df652011-11-25 15:05:25 +01001703 if (!rec_script_path)
1704 system_wide = false;
David Ahernd54b1a92012-08-26 12:24:46 -06001705 else if (!system_wide) {
1706 if (have_cmd(argc - 1, &argv[1]) != 0) {
1707 err = -1;
1708 goto out;
1709 }
1710 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001711
1712 __argv = malloc((argc + 2) * sizeof(const char *));
David Ahernd54b1a92012-08-26 12:24:46 -06001713 if (!__argv) {
1714 pr_err("malloc failed\n");
1715 err = -ENOMEM;
1716 goto out;
1717 }
1718
Tom Zanussib5b87312010-11-10 08:16:51 -06001719 __argv[j++] = "/bin/sh";
1720 __argv[j++] = script_path;
1721 if (system_wide)
1722 __argv[j++] = "-a";
1723 for (i = 2; i < argc; i++)
1724 __argv[j++] = argv[i];
1725 __argv[j++] = NULL;
1726
1727 execvp("/bin/sh", (char **)__argv);
1728 free(__argv);
1729 exit(-1);
1730 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001731
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001732 if (symbol__init() < 0)
1733 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -06001734 if (!script_name)
1735 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001736
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001737 session = perf_session__new(&file, false, &script.tool);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001738 if (session == NULL)
1739 return -ENOMEM;
1740
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001741 script.session = session;
1742
Anton Blanchard5d67be92011-07-04 21:57:50 +10001743 if (cpu_list) {
1744 if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
1745 return -1;
1746 }
1747
Tom Zanussibdb71db2013-01-18 13:51:26 -06001748 if (!script_name && !generate_script_lang)
1749 perf_session__fprintf_info(session, stdout, show_full_info);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001750
David Ahern1424dc92011-03-09 22:23:28 -07001751 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001752 symbol_conf.use_callchain = true;
1753 else
1754 symbol_conf.use_callchain = false;
1755
Tom Zanussi956ffd02009-11-25 01:15:46 -06001756 if (generate_script_lang) {
1757 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001758 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001759
David Ahern2c9e45f72011-03-17 10:03:21 -06001760 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001761 fprintf(stderr,
1762 "custom fields not supported for generated scripts");
1763 return -1;
1764 }
1765
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001766 input = open(file.path, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06001767 if (input < 0) {
1768 perror("failed to open file");
David Ahernd54b1a92012-08-26 12:24:46 -06001769 return -1;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001770 }
1771
1772 err = fstat(input, &perf_stat);
1773 if (err < 0) {
1774 perror("failed to stat file");
David Ahernd54b1a92012-08-26 12:24:46 -06001775 return -1;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001776 }
1777
1778 if (!perf_stat.st_size) {
1779 fprintf(stderr, "zero-sized file, nothing to do!\n");
David Ahernd54b1a92012-08-26 12:24:46 -06001780 return 0;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001781 }
1782
1783 scripting_ops = script_spec__lookup(generate_script_lang);
1784 if (!scripting_ops) {
1785 fprintf(stderr, "invalid language specifier");
1786 return -1;
1787 }
1788
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01001789 err = scripting_ops->generate_script(session->tevent.pevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001790 "perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001791 goto out;
1792 }
1793
1794 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001795 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001796 if (err)
1797 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001798 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001799 }
1800
David Ahern9cbdb702011-04-06 21:54:20 -06001801
1802 err = perf_session__check_output_opt(session);
1803 if (err < 0)
1804 goto out;
1805
Adrian Hunter6f3e5ed2013-10-22 10:34:07 +03001806 err = __cmd_script(&script);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001807
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001808 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001809 cleanup_scripting();
1810out:
1811 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001812}