blob: 7731a09e975c4b17d9409fdf2715dd4e3c28583e [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;
Anton Blanchard5d67be92011-07-04 21:57:50 +100027static const char *cpu_list;
28static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Tom Zanussi956ffd02009-11-25 01:15:46 -060029
David Ahern745f43e2011-03-09 22:23:26 -070030enum perf_output_field {
31 PERF_OUTPUT_COMM = 1U << 0,
32 PERF_OUTPUT_TID = 1U << 1,
33 PERF_OUTPUT_PID = 1U << 2,
34 PERF_OUTPUT_TIME = 1U << 3,
35 PERF_OUTPUT_CPU = 1U << 4,
36 PERF_OUTPUT_EVNAME = 1U << 5,
37 PERF_OUTPUT_TRACE = 1U << 6,
David Ahern787bef12011-05-27 14:28:43 -060038 PERF_OUTPUT_IP = 1U << 7,
39 PERF_OUTPUT_SYM = 1U << 8,
David Ahern610723f2011-05-27 14:28:44 -060040 PERF_OUTPUT_DSO = 1U << 9,
David Ahern7cec0922011-05-30 13:08:23 -060041 PERF_OUTPUT_ADDR = 1U << 10,
David Ahern745f43e2011-03-09 22:23:26 -070042};
43
44struct output_option {
45 const char *str;
46 enum perf_output_field field;
47} all_output_options[] = {
48 {.str = "comm", .field = PERF_OUTPUT_COMM},
49 {.str = "tid", .field = PERF_OUTPUT_TID},
50 {.str = "pid", .field = PERF_OUTPUT_PID},
51 {.str = "time", .field = PERF_OUTPUT_TIME},
52 {.str = "cpu", .field = PERF_OUTPUT_CPU},
53 {.str = "event", .field = PERF_OUTPUT_EVNAME},
54 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahern787bef12011-05-27 14:28:43 -060055 {.str = "ip", .field = PERF_OUTPUT_IP},
David Ahernc0230b22011-03-09 22:23:27 -070056 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern610723f2011-05-27 14:28:44 -060057 {.str = "dso", .field = PERF_OUTPUT_DSO},
David Ahern7cec0922011-05-30 13:08:23 -060058 {.str = "addr", .field = PERF_OUTPUT_ADDR},
David Ahern745f43e2011-03-09 22:23:26 -070059};
60
61/* default set to maintain compatibility with current format */
David Ahern2c9e45f72011-03-17 10:03:21 -060062static struct {
63 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060064 bool wildcard_set;
David Ahern2c9e45f72011-03-17 10:03:21 -060065 u64 fields;
66 u64 invalid_fields;
67} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070068
David Ahern2c9e45f72011-03-17 10:03:21 -060069 [PERF_TYPE_HARDWARE] = {
70 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070071
David Ahern2c9e45f72011-03-17 10:03:21 -060072 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
73 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060074 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -060075 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
David Ahern2c9e45f72011-03-17 10:03:21 -060076
77 .invalid_fields = PERF_OUTPUT_TRACE,
78 },
79
80 [PERF_TYPE_SOFTWARE] = {
81 .user_set = false,
82
83 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
84 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -060085 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -060086 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
David Ahern2c9e45f72011-03-17 10:03:21 -060087
88 .invalid_fields = PERF_OUTPUT_TRACE,
89 },
90
91 [PERF_TYPE_TRACEPOINT] = {
92 .user_set = false,
93
94 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
95 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
96 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
97 },
Arun Sharma0817a6a2011-04-14 10:38:18 -070098
99 [PERF_TYPE_RAW] = {
100 .user_set = false,
101
102 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
103 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
David Ahern787bef12011-05-27 14:28:43 -0600104 PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
David Ahern610723f2011-05-27 14:28:44 -0600105 PERF_OUTPUT_SYM | PERF_OUTPUT_DSO,
Arun Sharma0817a6a2011-04-14 10:38:18 -0700106
107 .invalid_fields = PERF_OUTPUT_TRACE,
108 },
David Ahern1424dc92011-03-09 22:23:28 -0700109};
David Ahern745f43e2011-03-09 22:23:26 -0700110
David Ahern2c9e45f72011-03-17 10:03:21 -0600111static bool output_set_by_user(void)
112{
113 int j;
114 for (j = 0; j < PERF_TYPE_MAX; ++j) {
115 if (output[j].user_set)
116 return true;
117 }
118 return false;
119}
David Ahern745f43e2011-03-09 22:23:26 -0700120
David Ahern9cbdb702011-04-06 21:54:20 -0600121static const char *output_field2str(enum perf_output_field field)
122{
123 int i, imax = ARRAY_SIZE(all_output_options);
124 const char *str = "";
125
126 for (i = 0; i < imax; ++i) {
127 if (all_output_options[i].field == field) {
128 str = all_output_options[i].str;
129 break;
130 }
131 }
132 return str;
133}
134
David Ahern2c9e45f72011-03-17 10:03:21 -0600135#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -0700136
David Ahern9cbdb702011-04-06 21:54:20 -0600137static int perf_event_attr__check_stype(struct perf_event_attr *attr,
138 u64 sample_type, const char *sample_msg,
139 enum perf_output_field field)
David Ahern1424dc92011-03-09 22:23:28 -0700140{
David Ahern9cbdb702011-04-06 21:54:20 -0600141 int type = attr->type;
142 const char *evname;
143
144 if (attr->sample_type & sample_type)
145 return 0;
146
147 if (output[type].user_set) {
148 evname = __event_name(attr->type, attr->config);
149 pr_err("Samples for '%s' event do not have %s attribute set. "
150 "Cannot print '%s' field.\n",
151 evname, sample_msg, output_field2str(field));
152 return -1;
153 }
154
155 /* user did not ask for it explicitly so remove from the default list */
156 output[type].fields &= ~field;
157 evname = __event_name(attr->type, attr->config);
158 pr_debug("Samples for '%s' event do not have %s attribute set. "
159 "Skipping '%s' field.\n",
160 evname, sample_msg, output_field2str(field));
161
162 return 0;
163}
164
165static int perf_evsel__check_attr(struct perf_evsel *evsel,
166 struct perf_session *session)
167{
168 struct perf_event_attr *attr = &evsel->attr;
169
David Ahern1424dc92011-03-09 22:23:28 -0700170 if (PRINT_FIELD(TRACE) &&
171 !perf_session__has_traces(session, "record -R"))
172 return -EINVAL;
173
David Ahern787bef12011-05-27 14:28:43 -0600174 if (PRINT_FIELD(IP)) {
David Ahern9cbdb702011-04-06 21:54:20 -0600175 if (perf_event_attr__check_stype(attr, PERF_SAMPLE_IP, "IP",
David Ahern787bef12011-05-27 14:28:43 -0600176 PERF_OUTPUT_IP))
David Ahern1424dc92011-03-09 22:23:28 -0700177 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600178
David Ahern1424dc92011-03-09 22:23:28 -0700179 if (!no_callchain &&
David Ahern9cbdb702011-04-06 21:54:20 -0600180 !(attr->sample_type & PERF_SAMPLE_CALLCHAIN))
David Ahern1424dc92011-03-09 22:23:28 -0700181 symbol_conf.use_callchain = false;
182 }
David Ahern7cec0922011-05-30 13:08:23 -0600183
184 if (PRINT_FIELD(ADDR) &&
185 perf_event_attr__check_stype(attr, PERF_SAMPLE_ADDR, "ADDR",
186 PERF_OUTPUT_ADDR))
187 return -EINVAL;
188
189 if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
190 pr_err("Display of symbols requested but neither sample IP nor "
191 "sample address\nis selected. Hence, no addresses to convert "
192 "to symbols.\n");
David Ahern787bef12011-05-27 14:28:43 -0600193 return -EINVAL;
194 }
David Ahern7cec0922011-05-30 13:08:23 -0600195 if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
196 pr_err("Display of DSO requested but neither sample IP nor "
197 "sample address\nis selected. Hence, no addresses to convert "
198 "to DSO.\n");
David Ahern610723f2011-05-27 14:28:44 -0600199 return -EINVAL;
200 }
David Ahern1424dc92011-03-09 22:23:28 -0700201
202 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
David Ahern9cbdb702011-04-06 21:54:20 -0600203 perf_event_attr__check_stype(attr, PERF_SAMPLE_TID, "TID",
204 PERF_OUTPUT_TID|PERF_OUTPUT_PID))
David Ahern1424dc92011-03-09 22:23:28 -0700205 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700206
207 if (PRINT_FIELD(TIME) &&
David Ahern9cbdb702011-04-06 21:54:20 -0600208 perf_event_attr__check_stype(attr, PERF_SAMPLE_TIME, "TIME",
209 PERF_OUTPUT_TIME))
David Ahern1424dc92011-03-09 22:23:28 -0700210 return -EINVAL;
David Ahern1424dc92011-03-09 22:23:28 -0700211
212 if (PRINT_FIELD(CPU) &&
David Ahern9cbdb702011-04-06 21:54:20 -0600213 perf_event_attr__check_stype(attr, PERF_SAMPLE_CPU, "CPU",
214 PERF_OUTPUT_CPU))
David Ahern1424dc92011-03-09 22:23:28 -0700215 return -EINVAL;
David Ahern9cbdb702011-04-06 21:54:20 -0600216
217 return 0;
218}
219
220/*
221 * verify all user requested events exist and the samples
222 * have the expected data
223 */
224static int perf_session__check_output_opt(struct perf_session *session)
225{
226 int j;
227 struct perf_evsel *evsel;
228
229 for (j = 0; j < PERF_TYPE_MAX; ++j) {
230 evsel = perf_session__find_first_evtype(session, j);
231
232 /*
233 * even if fields is set to 0 (ie., show nothing) event must
234 * exist if user explicitly includes it on the command line
235 */
236 if (!evsel && output[j].user_set && !output[j].wildcard_set) {
237 pr_err("%s events do not exist. "
238 "Remove corresponding -f option to proceed.\n",
239 event_type(j));
240 return -1;
241 }
242
243 if (evsel && output[j].fields &&
244 perf_evsel__check_attr(evsel, session))
245 return -1;
David Ahern1424dc92011-03-09 22:23:28 -0700246 }
247
248 return 0;
249}
David Ahern745f43e2011-03-09 22:23:26 -0700250
David Ahernc70c94b2011-03-09 22:23:25 -0700251static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700252 struct thread *thread,
253 struct perf_event_attr *attr)
David Ahernc70c94b2011-03-09 22:23:25 -0700254{
255 int type;
256 struct event *event;
257 const char *evname = NULL;
258 unsigned long secs;
259 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700260 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700261
David Ahern745f43e2011-03-09 22:23:26 -0700262 if (PRINT_FIELD(COMM)) {
263 if (latency_format)
264 printf("%8.8s ", thread->comm);
David Ahern787bef12011-05-27 14:28:43 -0600265 else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
David Ahernc0230b22011-03-09 22:23:27 -0700266 printf("%s ", thread->comm);
David Ahern745f43e2011-03-09 22:23:26 -0700267 else
268 printf("%16s ", thread->comm);
269 }
David Ahernc70c94b2011-03-09 22:23:25 -0700270
David Ahern745f43e2011-03-09 22:23:26 -0700271 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
272 printf("%5d/%-5d ", sample->pid, sample->tid);
273 else if (PRINT_FIELD(PID))
274 printf("%5d ", sample->pid);
275 else if (PRINT_FIELD(TID))
276 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700277
David Ahern745f43e2011-03-09 22:23:26 -0700278 if (PRINT_FIELD(CPU)) {
279 if (latency_format)
280 printf("%3d ", sample->cpu);
281 else
282 printf("[%03d] ", sample->cpu);
283 }
David Ahernc70c94b2011-03-09 22:23:25 -0700284
David Ahern745f43e2011-03-09 22:23:26 -0700285 if (PRINT_FIELD(TIME)) {
286 nsecs = sample->time;
287 secs = nsecs / NSECS_PER_SEC;
288 nsecs -= secs * NSECS_PER_SEC;
289 usecs = nsecs / NSECS_PER_USEC;
290 printf("%5lu.%06lu: ", secs, usecs);
291 }
292
293 if (PRINT_FIELD(EVNAME)) {
David Ahern1424dc92011-03-09 22:23:28 -0700294 if (attr->type == PERF_TYPE_TRACEPOINT) {
295 type = trace_parse_common_type(sample->raw_data);
296 event = trace_find_event(type);
297 if (event)
298 evname = event->name;
299 } else
300 evname = __event_name(attr->type, attr->config);
David Ahern745f43e2011-03-09 22:23:26 -0700301
302 printf("%s: ", evname ? evname : "(unknown)");
303 }
David Ahernc70c94b2011-03-09 22:23:25 -0700304}
305
David Ahern7cec0922011-05-30 13:08:23 -0600306static bool sample_addr_correlates_sym(struct perf_event_attr *attr)
307{
308 if ((attr->type == PERF_TYPE_SOFTWARE) &&
309 ((attr->config == PERF_COUNT_SW_PAGE_FAULTS) ||
310 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN) ||
311 (attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)))
312 return true;
313
314 return false;
315}
316
317static void print_sample_addr(union perf_event *event,
318 struct perf_sample *sample,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200319 struct machine *machine,
David Ahern7cec0922011-05-30 13:08:23 -0600320 struct thread *thread,
321 struct perf_event_attr *attr)
322{
323 struct addr_location al;
324 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
325 const char *symname, *dsoname;
326
327 printf("%16" PRIx64, sample->addr);
328
329 if (!sample_addr_correlates_sym(attr))
330 return;
331
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200332 thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
333 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600334 if (!al.map)
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200335 thread__find_addr_map(thread, machine, cpumode, MAP__VARIABLE,
336 sample->addr, &al);
David Ahern7cec0922011-05-30 13:08:23 -0600337
338 al.cpu = sample->cpu;
339 al.sym = NULL;
340
341 if (al.map)
342 al.sym = map__find_symbol(al.map, al.addr, NULL);
343
344 if (PRINT_FIELD(SYM)) {
345 if (al.sym && al.sym->name)
346 symname = al.sym->name;
347 else
348 symname = "";
349
350 printf(" %16s", symname);
351 }
352
353 if (PRINT_FIELD(DSO)) {
354 if (al.map && al.map->dso && al.map->dso->name)
355 dsoname = al.map->dso->name;
356 else
357 dsoname = "";
358
359 printf(" (%s)", dsoname);
360 }
361}
362
David Ahernbe6d8422011-03-09 22:23:23 -0700363static void process_event(union perf_event *event __unused,
364 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300365 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200366 struct machine *machine,
David Ahernbe6d8422011-03-09 22:23:23 -0700367 struct thread *thread)
368{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300369 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700370
David Ahern2c9e45f72011-03-17 10:03:21 -0600371 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700372 return;
373
David Ahern1424dc92011-03-09 22:23:28 -0700374 print_sample_start(sample, thread, attr);
David Ahern745f43e2011-03-09 22:23:26 -0700375
376 if (PRINT_FIELD(TRACE))
377 print_trace_event(sample->cpu, sample->raw_data,
378 sample->raw_size);
379
David Ahern7cec0922011-05-30 13:08:23 -0600380 if (PRINT_FIELD(ADDR))
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200381 print_sample_addr(event, sample, machine, thread, attr);
David Ahern7cec0922011-05-30 13:08:23 -0600382
David Ahern787bef12011-05-27 14:28:43 -0600383 if (PRINT_FIELD(IP)) {
David Ahernc0230b22011-03-09 22:23:27 -0700384 if (!symbol_conf.use_callchain)
385 printf(" ");
386 else
387 printf("\n");
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200388 perf_event__print_ip(event, sample, machine, evsel,
389 PRINT_FIELD(SYM), PRINT_FIELD(DSO));
David Ahernc0230b22011-03-09 22:23:27 -0700390 }
391
David Ahernc70c94b2011-03-09 22:23:25 -0700392 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700393}
394
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600395static int default_start_script(const char *script __unused,
396 int argc __unused,
397 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600398{
399 return 0;
400}
401
402static int default_stop_script(void)
403{
404 return 0;
405}
406
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600407static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600408{
409 return 0;
410}
411
412static struct scripting_ops default_scripting_ops = {
413 .start_script = default_start_script,
414 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700415 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600416 .generate_script = default_generate_script,
417};
418
419static struct scripting_ops *scripting_ops;
420
421static void setup_scripting(void)
422{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600423 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600424 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600425
Tom Zanussi956ffd02009-11-25 01:15:46 -0600426 scripting_ops = &default_scripting_ops;
427}
428
429static int cleanup_scripting(void)
430{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100431 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500432
Tom Zanussi956ffd02009-11-25 01:15:46 -0600433 return scripting_ops->stop_script();
434}
435
Tom Zanussi956ffd02009-11-25 01:15:46 -0600436static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200437
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200438static int process_sample_event(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200439 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200440 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300441 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200442 struct machine *machine)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200443{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200444 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200445
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200446 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200447 pr_debug("problem processing %d event, skipping it.\n",
448 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200449 return -1;
450 }
451
David Ahern1424dc92011-03-09 22:23:28 -0700452 if (debug_mode) {
453 if (sample->time < last_timestamp) {
454 pr_err("Samples misordered, previous: %" PRIu64
455 " this: %" PRIu64 "\n", last_timestamp,
456 sample->time);
457 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200458 }
David Ahern1424dc92011-03-09 22:23:28 -0700459 last_timestamp = sample->time;
460 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200461 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000462
463 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
464 return 0;
465
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200466 scripting_ops->process_event(event, sample, evsel, machine, thread);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200467
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200468 evsel->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200469 return 0;
470}
471
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200472static struct perf_tool perf_script = {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200473 .sample = process_sample_event,
David Ahernc0230b22011-03-09 22:23:27 -0700474 .mmap = perf_event__process_mmap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200475 .comm = perf_event__process_comm,
David Ahernc0230b22011-03-09 22:23:27 -0700476 .exit = perf_event__process_task,
477 .fork = perf_event__process_task,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200478 .attr = perf_event__process_attr,
479 .event_type = perf_event__process_event_type,
480 .tracing_data = perf_event__process_tracing_data,
481 .build_id = perf_event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200482 .ordered_samples = true,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200483 .ordering_requires_timestamps = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200484};
485
Tom Zanussic239da32010-04-01 23:59:18 -0500486extern volatile int session_done;
487
488static void sig_handler(int sig __unused)
489{
490 session_done = 1;
491}
492
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100493static int __cmd_script(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200494{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200495 int ret;
496
Tom Zanussic239da32010-04-01 23:59:18 -0500497 signal(SIGINT, sig_handler);
498
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200499 ret = perf_session__process_events(session, &perf_script);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200500
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200501 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200502 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200503
504 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200505}
506
Tom Zanussi956ffd02009-11-25 01:15:46 -0600507struct script_spec {
508 struct list_head node;
509 struct scripting_ops *ops;
510 char spec[0];
511};
512
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200513static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600514
515static struct script_spec *script_spec__new(const char *spec,
516 struct scripting_ops *ops)
517{
518 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
519
520 if (s != NULL) {
521 strcpy(s->spec, spec);
522 s->ops = ops;
523 }
524
525 return s;
526}
527
528static void script_spec__delete(struct script_spec *s)
529{
530 free(s->spec);
531 free(s);
532}
533
534static void script_spec__add(struct script_spec *s)
535{
536 list_add_tail(&s->node, &script_specs);
537}
538
539static struct script_spec *script_spec__find(const char *spec)
540{
541 struct script_spec *s;
542
543 list_for_each_entry(s, &script_specs, node)
544 if (strcasecmp(s->spec, spec) == 0)
545 return s;
546 return NULL;
547}
548
549static struct script_spec *script_spec__findnew(const char *spec,
550 struct scripting_ops *ops)
551{
552 struct script_spec *s = script_spec__find(spec);
553
554 if (s)
555 return s;
556
557 s = script_spec__new(spec, ops);
558 if (!s)
559 goto out_delete_spec;
560
561 script_spec__add(s);
562
563 return s;
564
565out_delete_spec:
566 script_spec__delete(s);
567
568 return NULL;
569}
570
571int script_spec_register(const char *spec, struct scripting_ops *ops)
572{
573 struct script_spec *s;
574
575 s = script_spec__find(spec);
576 if (s)
577 return -1;
578
579 s = script_spec__findnew(spec, ops);
580 if (!s)
581 return -1;
582
583 return 0;
584}
585
586static struct scripting_ops *script_spec__lookup(const char *spec)
587{
588 struct script_spec *s = script_spec__find(spec);
589 if (!s)
590 return NULL;
591
592 return s->ops;
593}
594
595static void list_available_languages(void)
596{
597 struct script_spec *s;
598
599 fprintf(stderr, "\n");
600 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100601 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600602
603 list_for_each_entry(s, &script_specs, node)
604 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
605
606 fprintf(stderr, "\n");
607}
608
609static int parse_scriptname(const struct option *opt __used,
610 const char *str, int unset __used)
611{
612 char spec[PATH_MAX];
613 const char *script, *ext;
614 int len;
615
Tom Zanussif526d682010-01-27 02:27:52 -0600616 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600617 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600618 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600619 }
620
621 script = strchr(str, ':');
622 if (script) {
623 len = script - str;
624 if (len >= PATH_MAX) {
625 fprintf(stderr, "invalid language specifier");
626 return -1;
627 }
628 strncpy(spec, str, len);
629 spec[len] = '\0';
630 scripting_ops = script_spec__lookup(spec);
631 if (!scripting_ops) {
632 fprintf(stderr, "invalid language specifier");
633 return -1;
634 }
635 script++;
636 } else {
637 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100638 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600639 if (!ext) {
640 fprintf(stderr, "invalid script extension");
641 return -1;
642 }
643 scripting_ops = script_spec__lookup(++ext);
644 if (!scripting_ops) {
645 fprintf(stderr, "invalid script extension");
646 return -1;
647 }
648 }
649
650 script_name = strdup(script);
651
652 return 0;
653}
654
David Ahern745f43e2011-03-09 22:23:26 -0700655static int parse_output_fields(const struct option *opt __used,
656 const char *arg, int unset __used)
657{
658 char *tok;
659 int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
David Ahern2c9e45f72011-03-17 10:03:21 -0600660 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700661 int rc = 0;
662 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700663 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700664
665 if (!str)
666 return -ENOMEM;
667
David Ahern2c9e45f72011-03-17 10:03:21 -0600668 /* first word can state for which event type the user is specifying
669 * the fields. If no type exists, the specified fields apply to all
670 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700671 */
David Ahern2c9e45f72011-03-17 10:03:21 -0600672 tok = strchr(str, ':');
673 if (tok) {
674 *tok = '\0';
675 tok++;
676 if (!strcmp(str, "hw"))
677 type = PERF_TYPE_HARDWARE;
678 else if (!strcmp(str, "sw"))
679 type = PERF_TYPE_SOFTWARE;
680 else if (!strcmp(str, "trace"))
681 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -0700682 else if (!strcmp(str, "raw"))
683 type = PERF_TYPE_RAW;
David Ahern2c9e45f72011-03-17 10:03:21 -0600684 else {
685 fprintf(stderr, "Invalid event type in field string.\n");
686 return -EINVAL;
687 }
688
689 if (output[type].user_set)
690 pr_warning("Overriding previous field request for %s events.\n",
691 event_type(type));
692
693 output[type].fields = 0;
694 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600695 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -0600696
697 } else {
698 tok = str;
699 if (strlen(str) == 0) {
700 fprintf(stderr,
701 "Cannot set fields to 'none' for all event types.\n");
702 rc = -EINVAL;
703 goto out;
704 }
705
706 if (output_set_by_user())
707 pr_warning("Overriding previous field request for all events.\n");
708
709 for (j = 0; j < PERF_TYPE_MAX; ++j) {
710 output[j].fields = 0;
711 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600712 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -0600713 }
David Ahern1424dc92011-03-09 22:23:28 -0700714 }
715
David Ahern2c9e45f72011-03-17 10:03:21 -0600716 tok = strtok(tok, ",");
717 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -0700718 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600719 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -0700720 break;
David Ahern745f43e2011-03-09 22:23:26 -0700721 }
722 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600723 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -0700724 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -0600725 goto out;
726 }
727
728 if (type == -1) {
729 /* add user option to all events types for
730 * which it is valid
731 */
732 for (j = 0; j < PERF_TYPE_MAX; ++j) {
733 if (output[j].invalid_fields & all_output_options[i].field) {
734 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
735 all_output_options[i].str, event_type(j));
736 } else
737 output[j].fields |= all_output_options[i].field;
738 }
739 } else {
740 if (output[type].invalid_fields & all_output_options[i].field) {
741 fprintf(stderr, "\'%s\' not valid for %s events.\n",
742 all_output_options[i].str, event_type(type));
743
744 rc = -EINVAL;
745 goto out;
746 }
747 output[type].fields |= all_output_options[i].field;
748 }
749
750 tok = strtok(NULL, ",");
751 }
752
753 if (type >= 0) {
754 if (output[type].fields == 0) {
755 pr_debug("No fields requested for %s type. "
756 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -0700757 }
David Ahern1424dc92011-03-09 22:23:28 -0700758 }
David Ahern745f43e2011-03-09 22:23:26 -0700759
David Ahern2c9e45f72011-03-17 10:03:21 -0600760out:
David Ahern745f43e2011-03-09 22:23:26 -0700761 free(str);
762 return rc;
763}
764
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600765/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
766static int is_directory(const char *base_path, const struct dirent *dent)
767{
768 char path[PATH_MAX];
769 struct stat st;
770
771 sprintf(path, "%s/%s", base_path, dent->d_name);
772 if (stat(path, &st))
773 return 0;
774
775 return S_ISDIR(st.st_mode);
776}
777
778#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600779 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
780 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600781 if ((lang_dirent.d_type == DT_DIR || \
782 (lang_dirent.d_type == DT_UNKNOWN && \
783 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600784 (strcmp(lang_dirent.d_name, ".")) && \
785 (strcmp(lang_dirent.d_name, "..")))
786
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600787#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600788 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
789 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600790 if (script_dirent.d_type != DT_DIR && \
791 (script_dirent.d_type != DT_UNKNOWN || \
792 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600793
794
795#define RECORD_SUFFIX "-record"
796#define REPORT_SUFFIX "-report"
797
798struct script_desc {
799 struct list_head node;
800 char *name;
801 char *half_liner;
802 char *args;
803};
804
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200805static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600806
807static struct script_desc *script_desc__new(const char *name)
808{
809 struct script_desc *s = zalloc(sizeof(*s));
810
Tom Zanussib5b87312010-11-10 08:16:51 -0600811 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600812 s->name = strdup(name);
813
814 return s;
815}
816
817static void script_desc__delete(struct script_desc *s)
818{
819 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600820 free(s->half_liner);
821 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600822 free(s);
823}
824
825static void script_desc__add(struct script_desc *s)
826{
827 list_add_tail(&s->node, &script_descs);
828}
829
830static struct script_desc *script_desc__find(const char *name)
831{
832 struct script_desc *s;
833
834 list_for_each_entry(s, &script_descs, node)
835 if (strcasecmp(s->name, name) == 0)
836 return s;
837 return NULL;
838}
839
840static struct script_desc *script_desc__findnew(const char *name)
841{
842 struct script_desc *s = script_desc__find(name);
843
844 if (s)
845 return s;
846
847 s = script_desc__new(name);
848 if (!s)
849 goto out_delete_desc;
850
851 script_desc__add(s);
852
853 return s;
854
855out_delete_desc:
856 script_desc__delete(s);
857
858 return NULL;
859}
860
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200861static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600862{
863 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200864 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600865
866 if (strlen(str) > suffix_len) {
867 p = str + strlen(str) - suffix_len;
868 if (!strncmp(p, suffix, suffix_len))
869 return p;
870 }
871
872 return NULL;
873}
874
875static char *ltrim(char *str)
876{
877 int len = strlen(str);
878
879 while (len && isspace(*str)) {
880 len--;
881 str++;
882 }
883
884 return str;
885}
886
887static int read_script_info(struct script_desc *desc, const char *filename)
888{
889 char line[BUFSIZ], *p;
890 FILE *fp;
891
892 fp = fopen(filename, "r");
893 if (!fp)
894 return -1;
895
896 while (fgets(line, sizeof(line), fp)) {
897 p = ltrim(line);
898 if (strlen(p) == 0)
899 continue;
900 if (*p != '#')
901 continue;
902 p++;
903 if (strlen(p) && *p == '!')
904 continue;
905
906 p = ltrim(p);
907 if (strlen(p) && p[strlen(p) - 1] == '\n')
908 p[strlen(p) - 1] = '\0';
909
910 if (!strncmp(p, "description:", strlen("description:"))) {
911 p += strlen("description:");
912 desc->half_liner = strdup(ltrim(p));
913 continue;
914 }
915
916 if (!strncmp(p, "args:", strlen("args:"))) {
917 p += strlen("args:");
918 desc->args = strdup(ltrim(p));
919 continue;
920 }
921 }
922
923 fclose(fp);
924
925 return 0;
926}
927
928static int list_available_scripts(const struct option *opt __used,
929 const char *s __used, int unset __used)
930{
931 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
932 char scripts_path[MAXPATHLEN];
933 DIR *scripts_dir, *lang_dir;
934 char script_path[MAXPATHLEN];
935 char lang_path[MAXPATHLEN];
936 struct script_desc *desc;
937 char first_half[BUFSIZ];
938 char *script_root;
939 char *str;
940
941 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
942
943 scripts_dir = opendir(scripts_path);
944 if (!scripts_dir)
945 return -1;
946
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600947 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600948 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
949 lang_dirent.d_name);
950 lang_dir = opendir(lang_path);
951 if (!lang_dir)
952 continue;
953
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600954 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600955 script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200956 str = (char *)ends_with(script_root, REPORT_SUFFIX);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600957 if (str) {
958 *str = '\0';
959 desc = script_desc__findnew(script_root);
960 snprintf(script_path, MAXPATHLEN, "%s/%s",
961 lang_path, script_dirent.d_name);
962 read_script_info(desc, script_path);
963 }
964 free(script_root);
965 }
966 }
967
968 fprintf(stdout, "List of available trace scripts:\n");
969 list_for_each_entry(desc, &script_descs, node) {
970 sprintf(first_half, "%s %s", desc->name,
971 desc->args ? desc->args : "");
972 fprintf(stdout, " %-36s %s\n", first_half,
973 desc->half_liner ? desc->half_liner : "");
974 }
975
976 exit(0);
977}
978
Tom Zanussi38752942009-12-15 02:53:39 -0600979static char *get_script_path(const char *script_root, const char *suffix)
980{
981 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
982 char scripts_path[MAXPATHLEN];
983 char script_path[MAXPATHLEN];
984 DIR *scripts_dir, *lang_dir;
985 char lang_path[MAXPATHLEN];
986 char *str, *__script_root;
987 char *path = NULL;
988
989 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
990
991 scripts_dir = opendir(scripts_path);
992 if (!scripts_dir)
993 return NULL;
994
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600995 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -0600996 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
997 lang_dirent.d_name);
998 lang_dir = opendir(lang_path);
999 if (!lang_dir)
1000 continue;
1001
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001002 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001003 __script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001004 str = (char *)ends_with(__script_root, suffix);
Tom Zanussi38752942009-12-15 02:53:39 -06001005 if (str) {
1006 *str = '\0';
1007 if (strcmp(__script_root, script_root))
1008 continue;
1009 snprintf(script_path, MAXPATHLEN, "%s/%s",
1010 lang_path, script_dirent.d_name);
1011 path = strdup(script_path);
1012 free(__script_root);
1013 break;
1014 }
1015 free(__script_root);
1016 }
1017 }
1018
1019 return path;
1020}
1021
Tom Zanussib5b87312010-11-10 08:16:51 -06001022static bool is_top_script(const char *script_path)
1023{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001024 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001025}
1026
1027static int has_required_arg(char *script_path)
1028{
1029 struct script_desc *desc;
1030 int n_args = 0;
1031 char *p;
1032
1033 desc = script_desc__new(NULL);
1034
1035 if (read_script_info(desc, script_path))
1036 goto out;
1037
1038 if (!desc->args)
1039 goto out;
1040
1041 for (p = desc->args; *p; p++)
1042 if (*p == '<')
1043 n_args++;
1044out:
1045 script_desc__delete(desc);
1046
1047 return n_args;
1048}
1049
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001050static const char * const script_usage[] = {
1051 "perf script [<options>]",
1052 "perf script [<options>] record <script> [<record-options>] <command>",
1053 "perf script [<options>] report <script> [script-args]",
1054 "perf script [<options>] <script> [<record-options>] <command>",
1055 "perf script [<options>] <top-script> [script-args]",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001056 NULL
1057};
1058
1059static const struct option options[] = {
1060 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1061 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +10001062 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001063 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001064 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -04001065 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001066 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1067 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -06001068 OPT_CALLBACK('s', "script", NULL, "name",
1069 "script file name (lang:script name, script name, or *)",
1070 parse_scriptname),
1071 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001072 "generate perf-script.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +09001073 OPT_STRING('i', "input", &input_name, "file",
1074 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +02001075 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1076 "do various checks like samples ordering and lost events"),
David Ahernc0230b22011-03-09 22:23:27 -07001077 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1078 "file", "vmlinux pathname"),
1079 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1080 "file", "kallsyms pathname"),
1081 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1082 "When printing symbols do not display call chain"),
1083 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1084 "Look for files with symbols relative to this directory"),
David Ahern745f43e2011-03-09 22:23:26 -07001085 OPT_CALLBACK('f', "fields", NULL, "str",
David Ahern7cec0922011-05-30 13:08:23 -06001086 "comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace,raw. Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,addr",
David Ahern745f43e2011-03-09 22:23:26 -07001087 parse_output_fields),
David Ahernc8e66722011-11-13 11:30:08 -07001088 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001089 OPT_BOOLEAN('I', "show-info", &show_full_info,
1090 "display extended information from perf.data file"),
Masami Hiramatsu19096292009-08-21 14:56:03 -04001091 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001092};
1093
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001094static bool have_cmd(int argc, const char **argv)
1095{
1096 char **__argv = malloc(sizeof(const char *) * argc);
1097
1098 if (!__argv)
1099 die("malloc");
1100 memcpy(__argv, argv, sizeof(const char *) * argc);
1101 argc = parse_options(argc, (const char **)__argv, record_options,
1102 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1103 free(__argv);
1104
1105 return argc != 0;
1106}
1107
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001108int cmd_script(int argc, const char **argv, const char *prefix __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001109{
Tom Zanussib5b87312010-11-10 08:16:51 -06001110 char *rec_script_path = NULL;
1111 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001112 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -06001113 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001114 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -06001115 bool system_wide;
1116 int i, j, err;
Tom Zanussi38752942009-12-15 02:53:39 -06001117
Tom Zanussib5b87312010-11-10 08:16:51 -06001118 setup_scripting();
1119
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001120 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001121 PARSE_OPT_STOP_AT_NON_OPTION);
1122
1123 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1124 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1125 if (!rec_script_path)
1126 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001127 }
1128
Tom Zanussib5b87312010-11-10 08:16:51 -06001129 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1130 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1131 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001132 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001133 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001134 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001135 return -1;
1136 }
Tom Zanussi38752942009-12-15 02:53:39 -06001137 }
1138
Ben Hutchings44e668c2010-10-10 16:10:03 +01001139 /* make sure PERF_EXEC_PATH is set for scripts */
1140 perf_set_argv_exec_path(perf_exec_path());
1141
Tom Zanussib5b87312010-11-10 08:16:51 -06001142 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001143 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001144 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001145 pid_t pid;
1146
Tom Zanussib5b87312010-11-10 08:16:51 -06001147 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1148 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1149
1150 if (!rec_script_path && !rep_script_path) {
1151 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001152 " script -l for available scripts.\n", argv[0]);
1153 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001154 }
1155
Tom Zanussib5b87312010-11-10 08:16:51 -06001156 if (is_top_script(argv[0])) {
1157 rep_args = argc - 1;
1158 } else {
1159 int rec_args;
1160
1161 rep_args = has_required_arg(rep_script_path);
1162 rec_args = (argc - 1) - rep_args;
1163 if (rec_args < 0) {
1164 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001165 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001166 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001167 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001168 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001169 }
1170
1171 if (pipe(live_pipe) < 0) {
1172 perror("failed to create pipe");
1173 exit(-1);
1174 }
1175
1176 pid = fork();
1177 if (pid < 0) {
1178 perror("failed to fork");
1179 exit(-1);
1180 }
1181
1182 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001183 system_wide = true;
1184 j = 0;
1185
Tom Zanussia0cccc22010-04-01 23:59:25 -05001186 dup2(live_pipe[1], 1);
1187 close(live_pipe[0]);
1188
Tom Zanussib5b87312010-11-10 08:16:51 -06001189 if (!is_top_script(argv[0]))
1190 system_wide = !have_cmd(argc - rep_args,
1191 &argv[rep_args]);
1192
1193 __argv = malloc((argc + 6) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001194 if (!__argv)
1195 die("malloc");
1196
Tom Zanussib5b87312010-11-10 08:16:51 -06001197 __argv[j++] = "/bin/sh";
1198 __argv[j++] = rec_script_path;
1199 if (system_wide)
1200 __argv[j++] = "-a";
1201 __argv[j++] = "-q";
1202 __argv[j++] = "-o";
1203 __argv[j++] = "-";
1204 for (i = rep_args + 1; i < argc; i++)
1205 __argv[j++] = argv[i];
1206 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001207
1208 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001209 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001210 exit(-1);
1211 }
1212
1213 dup2(live_pipe[0], 0);
1214 close(live_pipe[1]);
1215
Tom Zanussib5b87312010-11-10 08:16:51 -06001216 __argv = malloc((argc + 4) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001217 if (!__argv)
1218 die("malloc");
Tom Zanussib5b87312010-11-10 08:16:51 -06001219 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001220 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001221 __argv[j++] = rep_script_path;
1222 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001223 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001224 __argv[j++] = "-i";
1225 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001226 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001227
1228 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001229 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001230 exit(-1);
1231 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001232
Tom Zanussib5b87312010-11-10 08:16:51 -06001233 if (rec_script_path)
1234 script_path = rec_script_path;
1235 if (rep_script_path)
1236 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001237
Tom Zanussib5b87312010-11-10 08:16:51 -06001238 if (script_path) {
1239 system_wide = false;
1240 j = 0;
1241
1242 if (rec_script_path)
1243 system_wide = !have_cmd(argc - 1, &argv[1]);
1244
1245 __argv = malloc((argc + 2) * sizeof(const char *));
1246 if (!__argv)
1247 die("malloc");
1248 __argv[j++] = "/bin/sh";
1249 __argv[j++] = script_path;
1250 if (system_wide)
1251 __argv[j++] = "-a";
1252 for (i = 2; i < argc; i++)
1253 __argv[j++] = argv[i];
1254 __argv[j++] = NULL;
1255
1256 execvp("/bin/sh", (char **)__argv);
1257 free(__argv);
1258 exit(-1);
1259 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001260
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001261 if (symbol__init() < 0)
1262 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -06001263 if (!script_name)
1264 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001265
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001266 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_script);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001267 if (session == NULL)
1268 return -ENOMEM;
1269
Anton Blanchard5d67be92011-07-04 21:57:50 +10001270 if (cpu_list) {
1271 if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
1272 return -1;
1273 }
1274
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001275 perf_session__fprintf_info(session, stdout, show_full_info);
1276
David Ahern1424dc92011-03-09 22:23:28 -07001277 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001278 symbol_conf.use_callchain = true;
1279 else
1280 symbol_conf.use_callchain = false;
1281
Tom Zanussi956ffd02009-11-25 01:15:46 -06001282 if (generate_script_lang) {
1283 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001284 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001285
David Ahern2c9e45f72011-03-17 10:03:21 -06001286 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001287 fprintf(stderr,
1288 "custom fields not supported for generated scripts");
1289 return -1;
1290 }
1291
1292 input = open(input_name, O_RDONLY);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001293 if (input < 0) {
1294 perror("failed to open file");
1295 exit(-1);
1296 }
1297
1298 err = fstat(input, &perf_stat);
1299 if (err < 0) {
1300 perror("failed to stat file");
1301 exit(-1);
1302 }
1303
1304 if (!perf_stat.st_size) {
1305 fprintf(stderr, "zero-sized file, nothing to do!\n");
1306 exit(0);
1307 }
1308
1309 scripting_ops = script_spec__lookup(generate_script_lang);
1310 if (!scripting_ops) {
1311 fprintf(stderr, "invalid language specifier");
1312 return -1;
1313 }
1314
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001315 err = scripting_ops->generate_script("perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001316 goto out;
1317 }
1318
1319 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001320 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001321 if (err)
1322 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001323 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001324 }
1325
David Ahern9cbdb702011-04-06 21:54:20 -06001326
1327 err = perf_session__check_output_opt(session);
1328 if (err < 0)
1329 goto out;
1330
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001331 err = __cmd_script(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001332
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001333 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001334 cleanup_scripting();
1335out:
1336 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001337}