blob: f7f18c774afd3befdd9207ac90d4ac3498942ae2 [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"
Anton Blanchard5d67be92011-07-04 21:57:50 +100017#include <linux/bitmap.h>
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020018
Tom Zanussi956ffd02009-11-25 01:15:46 -060019static char const *script_name;
20static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020021static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020022static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020023static u64 nr_unordered;
Tom Zanussi34c86ea2010-11-10 08:15:43 -060024extern const struct option record_options[];
David Ahernc0230b22011-03-09 22:23:27 -070025static bool no_callchain;
Stephane Eranianfbe96f22011-09-30 15:40:40 +020026static bool show_full_info;
Robert Richter317df652011-11-25 15:05:25 +010027static bool system_wide;
Anton Blanchard5d67be92011-07-04 21:57:50 +100028static const char *cpu_list;
29static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Tom Zanussi956ffd02009-11-25 01:15:46 -060030
David Ahern745f43e2011-03-09 22:23:26 -070031enum perf_output_field {
32 PERF_OUTPUT_COMM = 1U << 0,
33 PERF_OUTPUT_TID = 1U << 1,
34 PERF_OUTPUT_PID = 1U << 2,
35 PERF_OUTPUT_TIME = 1U << 3,
36 PERF_OUTPUT_CPU = 1U << 4,
37 PERF_OUTPUT_EVNAME = 1U << 5,
38 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060039 PERF_OUTPUT_IP = 1U << 7,
40 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060041 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060042 PERF_OUTPUT_ADDR = 1U << 10,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090043 PERF_OUTPUT_SYMOFFSET = 1U << 11,
David Ahern745f43e2011-03-09 22:23:26 -070044};
45
46struct output_option {
47 const char *str;
48 enum perf_output_field field;
49} all_output_options[] = {
50 {.str = "comm", .field = PERF_OUTPUT_COMM},
51 {.str = "tid", .field = PERF_OUTPUT_TID},
52 {.str = "pid", .field = PERF_OUTPUT_PID},
53 {.str = "time", .field = PERF_OUTPUT_TIME},
54 {.str = "cpu", .field = PERF_OUTPUT_CPU},
55 {.str = "event", .field = PERF_OUTPUT_EVNAME},
56 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060057 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070058 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060059 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060060 {.str = "addr", .field = PERF_OUTPUT_ADDR},
Akihiro Nagaia978f2a2012-01-30 13:43:15 +090061 {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
David Ahern745f43e2011-03-09 22:23:26 -070062};
63
64/* default set to maintain compatibility with current format */
David Ahern2c9e45f2011-03-17 10:03:21 -060065static struct {
66 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060067 bool wildcard_set;
David Ahern2c9e45f2011-03-17 10:03:21 -060068 u64 fields;
69 u64 invalid_fields;
70} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070071
David Ahern2c9e45f2011-03-17 10:03:21 -060072 [PERF_TYPE_HARDWARE] = {
73 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070074
David Ahern2c9e45f2011-03-17 10:03:21 -060075 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
76 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060077 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -060078 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
David Ahern2c9e45f2011-03-17 10:03:21 -060079
80 .invalid_fields = PERF_OUTPUT_TRACE,
81 },
82
83 [PERF_TYPE_SOFTWARE] = {
84 .user_set = false,
85
86 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
87 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060088 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -060089 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
David Ahern2c9e45f2011-03-17 10:03:21 -060090
91 .invalid_fields = PERF_OUTPUT_TRACE,
92 },
93
94 [PERF_TYPE_TRACEPOINT] = {
95 .user_set = false,
96
97 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
98 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
99 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
100 },
Arun Sharma0817a6a2011-04-14 10:38:18 -0700101
102 [PERF_TYPE_RAW] = {
103 .user_set = false,
104
105 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
106 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600107 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -0600108 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700109
110 .invalid_fields = PERF_OUTPUT_TRACE,
111 },
David Ahern1424dc92011-03-09 22:23:28 -0700112};
David Ahern745f43e2011-03-09 22:23:26 -0700113
David Ahern2c9e45f2011-03-17 10:03:21 -0600114static bool output_set_by_user(void)
115{
116 int j;
117 for (j = 0; j < PERF_TYPE_MAX; ++j) {
118 if (output[j].user_set)
119 return true;
120 }
121 return false;
122}
David Ahern745f43e2011-03-09 22:23:26 -0700123
David Ahern9cbdb702011-04-06 21:54:20 -0600124static const char *output_field2str(enum perf_output_field field)
125{
126 int i, imax = ARRAY_SIZE(all_output_options);
127 const char *str = "";
128
129 for (i = 0; i < imax; ++i) {
130 if (all_output_options[i].field == field) {
131 str = all_output_options[i].str;
132 break;
133 }
134 }
135 return str;
136}
137
David Ahern2c9e45f2011-03-17 10:03:21 -0600138#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700139
David Ahern9cbdb702011-04-06 21:54:20 -0600140static int perf_event_attr__check_stype(struct perf_event_attr *attr,
141 u64 sample_type, const char *sample_msg,
142 enum perf_output_field field)
David Ahern1424dc92011-03-09 22:23:28 -0700143{
David Ahern9cbdb702011-04-06 21:54:20 -0600144 int type = attr->type;
145 const char *evname;
146
147 if (attr->sample_type & sample_type)
148 return 0;
149
150 if (output[type].user_set) {
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400151 evname = __event_name(attr->type, attr->config, NULL);
David Ahern9cbdb702011-04-06 21:54:20 -0600152 pr_err("Samples for '%s' event do not have %s attribute set. "
153 "Cannot print '%s' field.\n",
154 evname, sample_msg, output_field2str(field));
155 return -1;
156 }
157
158 /* user did not ask for it explicitly so remove from the default list */
159 output[type].fields &= ~field;
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400160 evname = __event_name(attr->type, attr->config, NULL);
David Ahern9cbdb702011-04-06 21:54:20 -0600161 pr_debug("Samples for '%s' event do not have %s attribute set. "
162 "Skipping '%s' field.\n",
163 evname, sample_msg, output_field2str(field));
164
165 return 0;
166}
167
168static int perf_evsel__check_attr(struct perf_evsel *evsel,
169 struct perf_session *session)
170{
171 struct perf_event_attr *attr = &evsel->attr;
172
David Ahern1424dc92011-03-09 22:23:28 -0700173 if (PRINT_FIELD(TRACE) &&
174 !perf_session__has_traces(session, "record -R"))
175 return -EINVAL;
176
David Ahern787bef12011-05-27 14:28:43 -0600177 if (PRINT_FIELD(IP)) {
David Ahern9cbdb702011-04-06 21:54:20 -0600178 if (perf_event_attr__check_stype(attr, PERF_SAMPLE_IP, "IP",
David Ahern787bef12011-05-27 14:28:43 -0600179 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700180 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600181
David Ahern1424dc92011-03-09 22:23:28 -0700182 if (!no_callchain &&
David Ahern9cbdb702011-04-06 21:54:20 -0600183 !(attr->sample_type & PERF_SAMPLE_CALLCHAIN))
David Ahern1424dc92011-03-09 22:23:28 -0700184 symbol_conf.use_callchain = false;
185 }
David Ahern7cec0922011-05-30 13:08:23 -0600186
187 if (PRINT_FIELD(ADDR) &&
188 perf_event_attr__check_stype(attr, PERF_SAMPLE_ADDR, "ADDR",
189 PERF_OUTPUT_ADDR))
190 return -EINVAL;
191
192 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
193 pr_err("Display of symbols requested but neither sample IP nor "
194 "sample address\nis selected. Hence, no addresses to convert "
195 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600196 return -EINVAL;
197 }
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900198 if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
199 pr_err("Display of offsets requested but symbol is not"
200 "selected.\n");
201 return -EINVAL;
202 }
David Ahern7cec0922011-05-30 13:08:23 -0600203 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
204 pr_err("Display of DSO requested but neither sample IP nor "
205 "sample address\nis selected. Hence, no addresses to convert "
206 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600207 return -EINVAL;
208 }
David Ahern1424dc92011-03-09 22:23:28 -0700209
210 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
David Ahern9cbdb702011-04-06 21:54:20 -0600211 perf_event_attr__check_stype(attr, PERF_SAMPLE_TID, "TID",
212 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700213 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700214
215 if (PRINT_FIELD(TIME) &&
David Ahern9cbdb702011-04-06 21:54:20 -0600216 perf_event_attr__check_stype(attr, PERF_SAMPLE_TIME, "TIME",
217 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700218 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700219
220 if (PRINT_FIELD(CPU) &&
David Ahern9cbdb702011-04-06 21:54:20 -0600221 perf_event_attr__check_stype(attr, PERF_SAMPLE_CPU, "CPU",
222 PERF_OUTPUT_CPU))
David Ahern1424dc92011-03-09 22:23:28 -0700223 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600224
225 return 0;
226}
227
228/*
229 * verify all user requested events exist and the samples
230 * have the expected data
231 */
232static int perf_session__check_output_opt(struct perf_session *session)
233{
234 int j;
235 struct perf_evsel *evsel;
236
237 for (j = 0; j < PERF_TYPE_MAX; ++j) {
238 evsel = perf_session__find_first_evtype(session, j);
239
240 /*
241 * even if fields is set to 0 (ie., show nothing) event must
242 * exist if user explicitly includes it on the command line
243 */
244 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
245 pr_err("%s events do not exist. "
246 "Remove corresponding -f option to proceed.\n",
247 event_type(j));
248 return -1;
249 }
250
251 if (evsel && output[j].fields &&
252 perf_evsel__check_attr(evsel, session))
253 return -1;
David Ahern1424dc92011-03-09 22:23:28 -0700254 }
255
256 return 0;
257}
David Ahern745f43e2011-03-09 22:23:26 -0700258
David Ahernc70c94b2011-03-09 22:23:25 -0700259static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700260 struct thread *thread,
261 struct perf_event_attr *attr)
David Ahernc70c94b2011-03-09 22:23:25 -0700262{
263 int type;
264 struct event *event;
265 const char *evname = NULL;
266 unsigned long secs;
267 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700268 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700269
David Ahern745f43e2011-03-09 22:23:26 -0700270 if (PRINT_FIELD(COMM)) {
271 if (latency_format)
272 printf("%8.8s ", thread->comm);
David Ahern787bef12011-05-27 14:28:43 -0600273 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
David Ahernc0230b22011-03-09 22:23:27 -0700274 printf("%s ", thread->comm);
David Ahern745f43e2011-03-09 22:23:26 -0700275 else
276 printf("%16s ", thread->comm);
277 }
David Ahernc70c94b2011-03-09 22:23:25 -0700278
David Ahern745f43e2011-03-09 22:23:26 -0700279 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
280 printf("%5d/%-5d ", sample->pid, sample->tid);
281 else if (PRINT_FIELD(PID))
282 printf("%5d ", sample->pid);
283 else if (PRINT_FIELD(TID))
284 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700285
David Ahern745f43e2011-03-09 22:23:26 -0700286 if (PRINT_FIELD(CPU)) {
287 if (latency_format)
288 printf("%3d ", sample->cpu);
289 else
290 printf("[%03d] ", sample->cpu);
291 }
David Ahernc70c94b2011-03-09 22:23:25 -0700292
David Ahern745f43e2011-03-09 22:23:26 -0700293 if (PRINT_FIELD(TIME)) {
294 nsecs = sample->time;
295 secs = nsecs / NSECS_PER_SEC;
296 nsecs -= secs * NSECS_PER_SEC;
297 usecs = nsecs / NSECS_PER_USEC;
298 printf("%5lu.%06lu: ", secs, usecs);
299 }
300
301 if (PRINT_FIELD(EVNAME)) {
David Ahern1424dc92011-03-09 22:23:28 -0700302 if (attr->type == PERF_TYPE_TRACEPOINT) {
303 type = trace_parse_common_type(sample->raw_data);
304 event = trace_find_event(type);
305 if (event)
306 evname = event->name;
307 } else
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400308 evname = __event_name(attr->type, attr->config, NULL);
David Ahern745f43e2011-03-09 22:23:26 -0700309
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900310 printf("%s: ", evname ? evname : "[unknown]");
David Ahern745f43e2011-03-09 22:23:26 -0700311 }
David Ahernc70c94b2011-03-09 22:23:25 -0700312}
313
Akihiro Nagai95582592012-01-30 13:43:09 +0900314static bool is_bts_event(struct perf_event_attr *attr)
315{
316 return ((attr->type == PERF_TYPE_HARDWARE) &&
317 (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
318 (attr->sample_period == 1));
319}
320
David Ahern7cec0922011-05-30 13:08:23 -0600321static bool sample_addr_correlates_sym(struct perf_event_attr *attr)
322{
323 if ((attr->type == PERF_TYPE_SOFTWARE) &&
324 ((attr->config == PERF_COUNT_SW_PAGE_FAULTS) ||
325 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN) ||
326 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)))
327 return true;
328
Akihiro Nagai95582592012-01-30 13:43:09 +0900329 if (is_bts_event(attr))
330 return true;
331
David Ahern7cec0922011-05-30 13:08:23 -0600332 return false;
333}
334
335static void print_sample_addr(union perf_event *event,
336 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200337 struct machine *machine,
David Ahern7cec0922011-05-30 13:08:23 -0600338 struct thread *thread,
339 struct perf_event_attr *attr)
340{
341 struct addr_location al;
342 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
David Ahern7cec0922011-05-30 13:08:23 -0600343
344 printf("%16" PRIx64, sample->addr);
345
346 if (!sample_addr_correlates_sym(attr))
347 return;
348
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200349 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
350 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600351 if (!al.map)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200352 thread__find_addr_map(thread, machine, cpumode, MAP__VARIABLE,
353 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600354
355 al.cpu = sample->cpu;
356 al.sym = NULL;
357
358 if (al.map)
359 al.sym = map__find_symbol(al.map, al.addr, NULL);
360
361 if (PRINT_FIELD(SYM)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900362 printf(" ");
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900363 if (PRINT_FIELD(SYMOFFSET))
364 symbol__fprintf_symname_offs(al.sym, &al, stdout);
365 else
366 symbol__fprintf_symname(al.sym, stdout);
David Ahern7cec0922011-05-30 13:08:23 -0600367 }
368
369 if (PRINT_FIELD(DSO)) {
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900370 printf(" (");
371 map__fprintf_dsoname(al.map, stdout);
372 printf(")");
David Ahern7cec0922011-05-30 13:08:23 -0600373 }
374}
375
Akihiro Nagai95582592012-01-30 13:43:09 +0900376static void print_sample_bts(union perf_event *event,
377 struct perf_sample *sample,
378 struct perf_evsel *evsel,
379 struct machine *machine,
380 struct thread *thread)
381{
382 struct perf_event_attr *attr = &evsel->attr;
383
384 /* print branch_from information */
385 if (PRINT_FIELD(IP)) {
386 if (!symbol_conf.use_callchain)
387 printf(" ");
388 else
389 printf("\n");
390 perf_event__print_ip(event, sample, machine, evsel,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900391 PRINT_FIELD(SYM), PRINT_FIELD(DSO),
392 PRINT_FIELD(SYMOFFSET));
Akihiro Nagai95582592012-01-30 13:43:09 +0900393 }
394
395 printf(" => ");
396
397 /* print branch_to information */
398 if (PRINT_FIELD(ADDR))
399 print_sample_addr(event, sample, machine, thread, attr);
400
401 printf("\n");
402}
403
David Ahernbe6d8422011-03-09 22:23:23 -0700404static void process_event(union perf_event *event __unused,
405 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300406 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200407 struct machine *machine,
David Ahernbe6d8422011-03-09 22:23:23 -0700408 struct thread *thread)
409{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300410 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700411
David Ahern2c9e45f2011-03-17 10:03:21 -0600412 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700413 return;
414
David Ahern1424dc92011-03-09 22:23:28 -0700415 print_sample_start(sample, thread, attr);
David Ahern745f43e2011-03-09 22:23:26 -0700416
Akihiro Nagai95582592012-01-30 13:43:09 +0900417 if (is_bts_event(attr)) {
418 print_sample_bts(event, sample, evsel, machine, thread);
419 return;
420 }
421
David Ahern745f43e2011-03-09 22:23:26 -0700422 if (PRINT_FIELD(TRACE))
423 print_trace_event(sample->cpu, sample->raw_data,
424 sample->raw_size);
425
David Ahern7cec0922011-05-30 13:08:23 -0600426 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200427 print_sample_addr(event, sample, machine, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600428
David Ahern787bef12011-05-27 14:28:43 -0600429 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700430 if (!symbol_conf.use_callchain)
431 printf(" ");
432 else
433 printf("\n");
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200434 perf_event__print_ip(event, sample, machine, evsel,
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900435 PRINT_FIELD(SYM), PRINT_FIELD(DSO),
436 PRINT_FIELD(SYMOFFSET));
David Ahernc0230b22011-03-09 22:23:27 -0700437 }
438
David Ahernc70c94b2011-03-09 22:23:25 -0700439 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700440}
441
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600442static int default_start_script(const char *script __unused,
443 int argc __unused,
444 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600445{
446 return 0;
447}
448
449static int default_stop_script(void)
450{
451 return 0;
452}
453
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600454static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600455{
456 return 0;
457}
458
459static struct scripting_ops default_scripting_ops = {
460 .start_script = default_start_script,
461 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700462 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600463 .generate_script = default_generate_script,
464};
465
466static struct scripting_ops *scripting_ops;
467
468static void setup_scripting(void)
469{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600470 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600471 setup_python_scripting();
Ashwin Chaugulef2984452012-08-21 13:27:37 -0400472 setup_json_export();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600473
Tom Zanussi956ffd02009-11-25 01:15:46 -0600474 scripting_ops = &default_scripting_ops;
475}
476
477static int cleanup_scripting(void)
478{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100479 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500480
Tom Zanussi956ffd02009-11-25 01:15:46 -0600481 return scripting_ops->stop_script();
482}
483
Robert Richterefad1412011-12-07 10:02:54 +0100484static const char *input_name;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200485
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200486static int process_sample_event(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200487 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200488 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300489 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200490 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200491{
David Aherne7984b72011-11-21 10:02:52 -0700492 struct addr_location al;
David Ahern64aab932011-12-22 11:30:03 -0700493 struct thread *thread = machine__findnew_thread(machine, event->ip.tid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200494
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200495 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200496 pr_debug("problem processing %d event, skipping it.\n",
497 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200498 return -1;
499 }
500
David Ahern1424dc92011-03-09 22:23:28 -0700501 if (debug_mode) {
502 if (sample->time < last_timestamp) {
503 pr_err("Samples misordered, previous: %" PRIu64
504 " this: %" PRIu64 "\n", last_timestamp,
505 sample->time);
506 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200507 }
David Ahern1424dc92011-03-09 22:23:28 -0700508 last_timestamp = sample->time;
509 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200510 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000511
David Aherne7984b72011-11-21 10:02:52 -0700512 if (perf_event__preprocess_sample(event, machine, &al, sample, 0) < 0) {
513 pr_err("problem processing %d event, skipping it.\n",
514 event->header.type);
515 return -1;
516 }
517
518 if (al.filtered)
519 return 0;
520
Anton Blanchard5d67be92011-07-04 21:57:50 +1000521 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
522 return 0;
523
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200524 scripting_ops->process_event(event, sample, evsel, machine, thread);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200525
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200526 evsel->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200527 return 0;
528}
529
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200530static struct perf_tool perf_script = {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200531 .sample = process_sample_event,
David Ahernc0230b22011-03-09 22:23:27 -0700532 .mmap = perf_event__process_mmap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200533 .comm = perf_event__process_comm,
David Ahernc0230b22011-03-09 22:23:27 -0700534 .exit = perf_event__process_task,
535 .fork = perf_event__process_task,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200536 .attr = perf_event__process_attr,
537 .event_type = perf_event__process_event_type,
538 .tracing_data = perf_event__process_tracing_data,
539 .build_id = perf_event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200540 .ordered_samples = true,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200541 .ordering_requires_timestamps = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200542};
543
Tom Zanussic239da32010-04-01 23:59:18 -0500544extern volatile int session_done;
545
546static void sig_handler(int sig __unused)
547{
548 session_done = 1;
549}
550
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100551static int __cmd_script(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200552{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200553 int ret;
554
Tom Zanussic239da32010-04-01 23:59:18 -0500555 signal(SIGINT, sig_handler);
556
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200557 ret = perf_session__process_events(session, &perf_script);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200558
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200559 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200560 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200561
562 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200563}
564
Tom Zanussi956ffd02009-11-25 01:15:46 -0600565struct script_spec {
566 struct list_head node;
567 struct scripting_ops *ops;
568 char spec[0];
569};
570
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200571static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600572
573static struct script_spec *script_spec__new(const char *spec,
574 struct scripting_ops *ops)
575{
576 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
577
578 if (s != NULL) {
579 strcpy(s->spec, spec);
580 s->ops = ops;
581 }
582
583 return s;
584}
585
Tom Zanussi956ffd02009-11-25 01:15:46 -0600586static void script_spec__add(struct script_spec *s)
587{
588 list_add_tail(&s->node, &script_specs);
589}
590
591static struct script_spec *script_spec__find(const char *spec)
592{
593 struct script_spec *s;
594
595 list_for_each_entry(s, &script_specs, node)
596 if (strcasecmp(s->spec, spec) == 0)
597 return s;
598 return NULL;
599}
600
601static struct script_spec *script_spec__findnew(const char *spec,
602 struct scripting_ops *ops)
603{
604 struct script_spec *s = script_spec__find(spec);
605
606 if (s)
607 return s;
608
609 s = script_spec__new(spec, ops);
610 if (!s)
Namhyung Kim466e2872011-12-28 00:35:51 +0900611 return NULL;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600612
613 script_spec__add(s);
614
615 return s;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600616}
617
618int script_spec_register(const char *spec, struct scripting_ops *ops)
619{
620 struct script_spec *s;
621
622 s = script_spec__find(spec);
623 if (s)
624 return -1;
625
626 s = script_spec__findnew(spec, ops);
627 if (!s)
628 return -1;
629
630 return 0;
631}
632
633static struct scripting_ops *script_spec__lookup(const char *spec)
634{
635 struct script_spec *s = script_spec__find(spec);
636 if (!s)
637 return NULL;
638
639 return s->ops;
640}
641
642static void list_available_languages(void)
643{
644 struct script_spec *s;
645
646 fprintf(stderr, "\n");
647 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100648 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600649
650 list_for_each_entry(s, &script_specs, node)
651 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
652
653 fprintf(stderr, "\n");
654}
655
656static int parse_scriptname(const struct option *opt __used,
657 const char *str, int unset __used)
658{
659 char spec[PATH_MAX];
660 const char *script, *ext;
661 int len;
662
Tom Zanussif526d682010-01-27 02:27:52 -0600663 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600664 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600665 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600666 }
667
668 script = strchr(str, ':');
669 if (script) {
670 len = script - str;
671 if (len >= PATH_MAX) {
672 fprintf(stderr, "invalid language specifier");
673 return -1;
674 }
675 strncpy(spec, str, len);
676 spec[len] = '\0';
677 scripting_ops = script_spec__lookup(spec);
678 if (!scripting_ops) {
679 fprintf(stderr, "invalid language specifier");
680 return -1;
681 }
682 script++;
683 } else {
684 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100685 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600686 if (!ext) {
687 fprintf(stderr, "invalid script extension");
688 return -1;
689 }
690 scripting_ops = script_spec__lookup(++ext);
691 if (!scripting_ops) {
692 fprintf(stderr, "invalid script extension");
693 return -1;
694 }
695 }
696
697 script_name = strdup(script);
698
699 return 0;
700}
701
David Ahern745f43e2011-03-09 22:23:26 -0700702static int parse_output_fields(const struct option *opt __used,
703 const char *arg, int unset __used)
704{
705 char *tok;
706 int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
David Ahern2c9e45f2011-03-17 10:03:21 -0600707 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700708 int rc = 0;
709 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700710 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700711
712 if (!str)
713 return -ENOMEM;
714
David Ahern2c9e45f2011-03-17 10:03:21 -0600715 /* first word can state for which event type the user is specifying
716 * the fields. If no type exists, the specified fields apply to all
717 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700718 */
David Ahern2c9e45f2011-03-17 10:03:21 -0600719 tok = strchr(str, ':');
720 if (tok) {
721 *tok = '\0';
722 tok++;
723 if (!strcmp(str, "hw"))
724 type = PERF_TYPE_HARDWARE;
725 else if (!strcmp(str, "sw"))
726 type = PERF_TYPE_SOFTWARE;
727 else if (!strcmp(str, "trace"))
728 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -0700729 else if (!strcmp(str, "raw"))
730 type = PERF_TYPE_RAW;
David Ahern2c9e45f2011-03-17 10:03:21 -0600731 else {
732 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +0100733 rc = -EINVAL;
734 goto out;
David Ahern2c9e45f2011-03-17 10:03:21 -0600735 }
736
737 if (output[type].user_set)
738 pr_warning("Overriding previous field request for %s events.\n",
739 event_type(type));
740
741 output[type].fields = 0;
742 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600743 output[type].wildcard_set = false;
David Ahern2c9e45f2011-03-17 10:03:21 -0600744
745 } else {
746 tok = str;
747 if (strlen(str) == 0) {
748 fprintf(stderr,
749 "Cannot set fields to 'none' for all event types.\n");
750 rc = -EINVAL;
751 goto out;
752 }
753
754 if (output_set_by_user())
755 pr_warning("Overriding previous field request for all events.\n");
756
757 for (j = 0; j < PERF_TYPE_MAX; ++j) {
758 output[j].fields = 0;
759 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600760 output[j].wildcard_set = true;
David Ahern2c9e45f2011-03-17 10:03:21 -0600761 }
David Ahern1424dc92011-03-09 22:23:28 -0700762 }
763
David Ahern2c9e45f2011-03-17 10:03:21 -0600764 tok = strtok(tok, ",");
765 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -0700766 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f2011-03-17 10:03:21 -0600767 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -0700768 break;
David Ahern745f43e2011-03-09 22:23:26 -0700769 }
770 if (i == imax) {
David Ahern2c9e45f2011-03-17 10:03:21 -0600771 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -0700772 rc = -EINVAL;
David Ahern2c9e45f2011-03-17 10:03:21 -0600773 goto out;
774 }
775
776 if (type == -1) {
777 /* add user option to all events types for
778 * which it is valid
779 */
780 for (j = 0; j < PERF_TYPE_MAX; ++j) {
781 if (output[j].invalid_fields & all_output_options[i].field) {
782 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
783 all_output_options[i].str, event_type(j));
784 } else
785 output[j].fields |= all_output_options[i].field;
786 }
787 } else {
788 if (output[type].invalid_fields & all_output_options[i].field) {
789 fprintf(stderr, "\'%s\' not valid for %s events.\n",
790 all_output_options[i].str, event_type(type));
791
792 rc = -EINVAL;
793 goto out;
794 }
795 output[type].fields |= all_output_options[i].field;
796 }
797
798 tok = strtok(NULL, ",");
799 }
800
801 if (type >= 0) {
802 if (output[type].fields == 0) {
803 pr_debug("No fields requested for %s type. "
804 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -0700805 }
David Ahern1424dc92011-03-09 22:23:28 -0700806 }
David Ahern745f43e2011-03-09 22:23:26 -0700807
David Ahern2c9e45f2011-03-17 10:03:21 -0600808out:
David Ahern745f43e2011-03-09 22:23:26 -0700809 free(str);
810 return rc;
811}
812
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600813/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
814static int is_directory(const char *base_path, const struct dirent *dent)
815{
816 char path[PATH_MAX];
817 struct stat st;
818
819 sprintf(path, "%s/%s", base_path, dent->d_name);
820 if (stat(path, &st))
821 return 0;
822
823 return S_ISDIR(st.st_mode);
824}
825
826#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600827 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
828 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600829 if ((lang_dirent.d_type == DT_DIR || \
830 (lang_dirent.d_type == DT_UNKNOWN && \
831 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600832 (strcmp(lang_dirent.d_name, ".")) && \
833 (strcmp(lang_dirent.d_name, "..")))
834
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600835#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600836 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
837 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600838 if (script_dirent.d_type != DT_DIR && \
839 (script_dirent.d_type != DT_UNKNOWN || \
840 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600841
842
843#define RECORD_SUFFIX "-record"
844#define REPORT_SUFFIX "-report"
845
846struct script_desc {
847 struct list_head node;
848 char *name;
849 char *half_liner;
850 char *args;
851};
852
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200853static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600854
855static struct script_desc *script_desc__new(const char *name)
856{
857 struct script_desc *s = zalloc(sizeof(*s));
858
Tom Zanussib5b87312010-11-10 08:16:51 -0600859 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600860 s->name = strdup(name);
861
862 return s;
863}
864
865static void script_desc__delete(struct script_desc *s)
866{
867 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600868 free(s->half_liner);
869 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600870 free(s);
871}
872
873static void script_desc__add(struct script_desc *s)
874{
875 list_add_tail(&s->node, &script_descs);
876}
877
878static struct script_desc *script_desc__find(const char *name)
879{
880 struct script_desc *s;
881
882 list_for_each_entry(s, &script_descs, node)
883 if (strcasecmp(s->name, name) == 0)
884 return s;
885 return NULL;
886}
887
888static struct script_desc *script_desc__findnew(const char *name)
889{
890 struct script_desc *s = script_desc__find(name);
891
892 if (s)
893 return s;
894
895 s = script_desc__new(name);
896 if (!s)
897 goto out_delete_desc;
898
899 script_desc__add(s);
900
901 return s;
902
903out_delete_desc:
904 script_desc__delete(s);
905
906 return NULL;
907}
908
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200909static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600910{
911 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200912 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600913
914 if (strlen(str) > suffix_len) {
915 p = str + strlen(str) - suffix_len;
916 if (!strncmp(p, suffix, suffix_len))
917 return p;
918 }
919
920 return NULL;
921}
922
923static char *ltrim(char *str)
924{
925 int len = strlen(str);
926
927 while (len && isspace(*str)) {
928 len--;
929 str++;
930 }
931
932 return str;
933}
934
935static int read_script_info(struct script_desc *desc, const char *filename)
936{
937 char line[BUFSIZ], *p;
938 FILE *fp;
939
940 fp = fopen(filename, "r");
941 if (!fp)
942 return -1;
943
944 while (fgets(line, sizeof(line), fp)) {
945 p = ltrim(line);
946 if (strlen(p) == 0)
947 continue;
948 if (*p != '#')
949 continue;
950 p++;
951 if (strlen(p) && *p == '!')
952 continue;
953
954 p = ltrim(p);
955 if (strlen(p) && p[strlen(p) - 1] == '\n')
956 p[strlen(p) - 1] = '\0';
957
958 if (!strncmp(p, "description:", strlen("description:"))) {
959 p += strlen("description:");
960 desc->half_liner = strdup(ltrim(p));
961 continue;
962 }
963
964 if (!strncmp(p, "args:", strlen("args:"))) {
965 p += strlen("args:");
966 desc->args = strdup(ltrim(p));
967 continue;
968 }
969 }
970
971 fclose(fp);
972
973 return 0;
974}
975
Robert Richter38efb532011-11-25 11:38:40 +0100976static char *get_script_root(struct dirent *script_dirent, const char *suffix)
977{
978 char *script_root, *str;
979
980 script_root = strdup(script_dirent->d_name);
981 if (!script_root)
982 return NULL;
983
984 str = (char *)ends_with(script_root, suffix);
985 if (!str) {
986 free(script_root);
987 return NULL;
988 }
989
990 *str = '\0';
991 return script_root;
992}
993
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600994static int list_available_scripts(const struct option *opt __used,
995 const char *s __used, int unset __used)
996{
997 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
998 char scripts_path[MAXPATHLEN];
999 DIR *scripts_dir, *lang_dir;
1000 char script_path[MAXPATHLEN];
1001 char lang_path[MAXPATHLEN];
1002 struct script_desc *desc;
1003 char first_half[BUFSIZ];
1004 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001005
1006 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1007
1008 scripts_dir = opendir(scripts_path);
1009 if (!scripts_dir)
1010 return -1;
1011
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001012 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001013 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1014 lang_dirent.d_name);
1015 lang_dir = opendir(lang_path);
1016 if (!lang_dir)
1017 continue;
1018
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001019 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001020 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
1021 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001022 desc = script_desc__findnew(script_root);
1023 snprintf(script_path, MAXPATHLEN, "%s/%s",
1024 lang_path, script_dirent.d_name);
1025 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +01001026 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001027 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001028 }
1029 }
1030
1031 fprintf(stdout, "List of available trace scripts:\n");
1032 list_for_each_entry(desc, &script_descs, node) {
1033 sprintf(first_half, "%s %s", desc->name,
1034 desc->args ? desc->args : "");
1035 fprintf(stdout, " %-36s %s\n", first_half,
1036 desc->half_liner ? desc->half_liner : "");
1037 }
1038
1039 exit(0);
1040}
1041
Tom Zanussi38752942009-12-15 02:53:39 -06001042static char *get_script_path(const char *script_root, const char *suffix)
1043{
1044 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1045 char scripts_path[MAXPATHLEN];
1046 char script_path[MAXPATHLEN];
1047 DIR *scripts_dir, *lang_dir;
1048 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001049 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001050
1051 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1052
1053 scripts_dir = opendir(scripts_path);
1054 if (!scripts_dir)
1055 return NULL;
1056
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001057 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001058 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1059 lang_dirent.d_name);
1060 lang_dir = opendir(lang_path);
1061 if (!lang_dir)
1062 continue;
1063
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001064 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001065 __script_root = get_script_root(&script_dirent, suffix);
1066 if (__script_root && !strcmp(script_root, __script_root)) {
1067 free(__script_root);
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001068 closedir(lang_dir);
1069 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001070 snprintf(script_path, MAXPATHLEN, "%s/%s",
1071 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001072 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001073 }
1074 free(__script_root);
1075 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001076 closedir(lang_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001077 }
Namhyung Kim946ef2a2012-01-08 02:25:25 +09001078 closedir(scripts_dir);
Tom Zanussi38752942009-12-15 02:53:39 -06001079
Robert Richter38efb532011-11-25 11:38:40 +01001080 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001081}
1082
Tom Zanussib5b87312010-11-10 08:16:51 -06001083static bool is_top_script(const char *script_path)
1084{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001085 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001086}
1087
1088static int has_required_arg(char *script_path)
1089{
1090 struct script_desc *desc;
1091 int n_args = 0;
1092 char *p;
1093
1094 desc = script_desc__new(NULL);
1095
1096 if (read_script_info(desc, script_path))
1097 goto out;
1098
1099 if (!desc->args)
1100 goto out;
1101
1102 for (p = desc->args; *p; p++)
1103 if (*p == '<')
1104 n_args++;
1105out:
1106 script_desc__delete(desc);
1107
1108 return n_args;
1109}
1110
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001111static const char * const script_usage[] = {
1112 "perf script [<options>]",
1113 "perf script [<options>] record <script> [<record-options>] <command>",
1114 "perf script [<options>] report <script> [script-args]",
1115 "perf script [<options>] <script> [<record-options>] <command>",
1116 "perf script [<options>] <top-script> [script-args]",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001117 NULL
1118};
1119
1120static const struct option options[] = {
1121 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1122 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +10001123 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001124 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001125 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -04001126 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001127 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1128 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -06001129 OPT_CALLBACK('s', "script", NULL, "name",
1130 "script file name (lang:script name, script name, or *)",
1131 parse_scriptname),
1132 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001133 "generate perf-script.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +09001134 OPT_STRING('i', "input", &input_name, "file",
1135 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +02001136 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1137 "do various checks like samples ordering and lost events"),
David Ahernc0230b22011-03-09 22:23:27 -07001138 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1139 "file", "vmlinux pathname"),
1140 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1141 "file", "kallsyms pathname"),
1142 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1143 "When printing symbols do not display call chain"),
1144 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1145 "Look for files with symbols relative to this directory"),
David Ahern745f43e2011-03-09 22:23:26 -07001146 OPT_CALLBACK('f', "fields", NULL, "str",
Akihiro Nagaia978f2a2012-01-30 13:43:15 +09001147 "comma separated output fields prepend with 'type:'. "
1148 "Valid types: hw,sw,trace,raw. "
1149 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
1150 "addr,symoff",
David Ahern745f43e2011-03-09 22:23:26 -07001151 parse_output_fields),
Robert Richter317df652011-11-25 15:05:25 +01001152 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1153 "system-wide collection from all CPUs"),
David Ahernc8e66722011-11-13 11:30:08 -07001154 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
David Aherne7984b72011-11-21 10:02:52 -07001155 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1156 "only display events for these comms"),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001157 OPT_BOOLEAN('I', "show-info", &show_full_info,
1158 "display extended information from perf.data file"),
Akihiro Nagai0bc8d202012-01-30 13:43:20 +09001159 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
1160 "Show the path of [kernel.kallsyms]"),
1161
Masami Hiramatsu19096292009-08-21 14:56:03 -04001162 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001163};
1164
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001165static bool have_cmd(int argc, const char **argv)
1166{
1167 char **__argv = malloc(sizeof(const char *) * argc);
1168
1169 if (!__argv)
1170 die("malloc");
1171 memcpy(__argv, argv, sizeof(const char *) * argc);
1172 argc = parse_options(argc, (const char **)__argv, record_options,
1173 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1174 free(__argv);
1175
1176 return argc != 0;
1177}
1178
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001179int cmd_script(int argc, const char **argv, const char *prefix __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001180{
Tom Zanussib5b87312010-11-10 08:16:51 -06001181 char *rec_script_path = NULL;
1182 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001183 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -06001184 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001185 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -06001186 int i, j, err;
Tom Zanussi38752942009-12-15 02:53:39 -06001187
Tom Zanussib5b87312010-11-10 08:16:51 -06001188 setup_scripting();
1189
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001190 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001191 PARSE_OPT_STOP_AT_NON_OPTION);
1192
1193 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1194 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1195 if (!rec_script_path)
1196 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001197 }
1198
Tom Zanussib5b87312010-11-10 08:16:51 -06001199 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1200 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1201 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001202 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001203 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001204 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001205 return -1;
1206 }
Tom Zanussi38752942009-12-15 02:53:39 -06001207 }
1208
Ben Hutchings44e668c2010-10-10 16:10:03 +01001209 /* make sure PERF_EXEC_PATH is set for scripts */
1210 perf_set_argv_exec_path(perf_exec_path());
1211
Tom Zanussib5b87312010-11-10 08:16:51 -06001212 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001213 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001214 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001215 pid_t pid;
1216
Tom Zanussib5b87312010-11-10 08:16:51 -06001217 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1218 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1219
1220 if (!rec_script_path && !rep_script_path) {
1221 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001222 " script -l for available scripts.\n", argv[0]);
1223 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001224 }
1225
Tom Zanussib5b87312010-11-10 08:16:51 -06001226 if (is_top_script(argv[0])) {
1227 rep_args = argc - 1;
1228 } else {
1229 int rec_args;
1230
1231 rep_args = has_required_arg(rep_script_path);
1232 rec_args = (argc - 1) - rep_args;
1233 if (rec_args < 0) {
1234 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001235 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001236 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001237 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001238 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001239 }
1240
1241 if (pipe(live_pipe) < 0) {
1242 perror("failed to create pipe");
1243 exit(-1);
1244 }
1245
1246 pid = fork();
1247 if (pid < 0) {
1248 perror("failed to fork");
1249 exit(-1);
1250 }
1251
1252 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001253 j = 0;
1254
Tom Zanussia0cccc22010-04-01 23:59:25 -05001255 dup2(live_pipe[1], 1);
1256 close(live_pipe[0]);
1257
Robert Richter317df652011-11-25 15:05:25 +01001258 if (is_top_script(argv[0])) {
1259 system_wide = true;
1260 } else if (!system_wide) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001261 system_wide = !have_cmd(argc - rep_args,
1262 &argv[rep_args]);
Robert Richter317df652011-11-25 15:05:25 +01001263 }
Tom Zanussib5b87312010-11-10 08:16:51 -06001264
1265 __argv = malloc((argc + 6) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001266 if (!__argv)
1267 die("malloc");
1268
Tom Zanussib5b87312010-11-10 08:16:51 -06001269 __argv[j++] = "/bin/sh";
1270 __argv[j++] = rec_script_path;
1271 if (system_wide)
1272 __argv[j++] = "-a";
1273 __argv[j++] = "-q";
1274 __argv[j++] = "-o";
1275 __argv[j++] = "-";
1276 for (i = rep_args + 1; i < argc; i++)
1277 __argv[j++] = argv[i];
1278 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001279
1280 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001281 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001282 exit(-1);
1283 }
1284
1285 dup2(live_pipe[0], 0);
1286 close(live_pipe[1]);
1287
Tom Zanussib5b87312010-11-10 08:16:51 -06001288 __argv = malloc((argc + 4) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001289 if (!__argv)
1290 die("malloc");
Tom Zanussib5b87312010-11-10 08:16:51 -06001291 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001292 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001293 __argv[j++] = rep_script_path;
1294 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001295 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001296 __argv[j++] = "-i";
1297 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001298 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001299
1300 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001301 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001302 exit(-1);
1303 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001304
Tom Zanussib5b87312010-11-10 08:16:51 -06001305 if (rec_script_path)
1306 script_path = rec_script_path;
1307 if (rep_script_path)
1308 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001309
Tom Zanussib5b87312010-11-10 08:16:51 -06001310 if (script_path) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001311 j = 0;
1312
Robert Richter317df652011-11-25 15:05:25 +01001313 if (!rec_script_path)
1314 system_wide = false;
1315 else if (!system_wide)
Tom Zanussib5b87312010-11-10 08:16:51 -06001316 system_wide = !have_cmd(argc - 1, &argv[1]);
1317
1318 __argv = malloc((argc + 2) * sizeof(const char *));
1319 if (!__argv)
1320 die("malloc");
1321 __argv[j++] = "/bin/sh";
1322 __argv[j++] = script_path;
1323 if (system_wide)
1324 __argv[j++] = "-a";
1325 for (i = 2; i < argc; i++)
1326 __argv[j++] = argv[i];
1327 __argv[j++] = NULL;
1328
1329 execvp("/bin/sh", (char **)__argv);
1330 free(__argv);
1331 exit(-1);
1332 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001333
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001334 if (symbol__init() < 0)
1335 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -06001336 if (!script_name)
1337 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001338
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001339 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_script);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001340 if (session == NULL)
1341 return -ENOMEM;
1342
Anton Blanchard5d67be92011-07-04 21:57:50 +10001343 if (cpu_list) {
1344 if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
1345 return -1;
1346 }
1347
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001348 perf_session__fprintf_info(session, stdout, show_full_info);
1349
David Ahern1424dc92011-03-09 22:23:28 -07001350 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001351 symbol_conf.use_callchain = true;
1352 else
1353 symbol_conf.use_callchain = false;
1354
Tom Zanussi956ffd02009-11-25 01:15:46 -06001355 if (generate_script_lang) {
1356 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001357 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001358
David Ahern2c9e45f2011-03-17 10:03:21 -06001359 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001360 fprintf(stderr,
1361 "custom fields not supported for generated scripts");
1362 return -1;
1363 }
1364
Robert Richterefad1412011-12-07 10:02:54 +01001365 input = open(session->filename, O_RDONLY); /* input_name */
Tom Zanussi956ffd02009-11-25 01:15:46 -06001366 if (input < 0) {
1367 perror("failed to open file");
1368 exit(-1);
1369 }
1370
1371 err = fstat(input, &perf_stat);
1372 if (err < 0) {
1373 perror("failed to stat file");
1374 exit(-1);
1375 }
1376
1377 if (!perf_stat.st_size) {
1378 fprintf(stderr, "zero-sized file, nothing to do!\n");
1379 exit(0);
1380 }
1381
1382 scripting_ops = script_spec__lookup(generate_script_lang);
1383 if (!scripting_ops) {
1384 fprintf(stderr, "invalid language specifier");
1385 return -1;
1386 }
1387
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001388 err = scripting_ops->generate_script("perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001389 goto out;
1390 }
1391
1392 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001393 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001394 if (err)
1395 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001396 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001397 }
1398
David Ahern9cbdb702011-04-06 21:54:20 -06001399
1400 err = perf_session__check_output_opt(session);
1401 if (err < 0)
1402 goto out;
1403
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001404 err = __cmd_script(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001405
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001406 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001407 cleanup_scripting();
1408out:
1409 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001410}