blob: 619d6dcaa1d972203fe8ee1be8b43428b1fa2cbe [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 Ahern2c9e45f2011-03-17 10:03:21 -060062static struct {
63 bool user_set;
David Ahern9cbdb702011-04-06 21:54:20 -060064 bool wildcard_set;
David Ahern2c9e45f2011-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 Ahern2c9e45f2011-03-17 10:03:21 -060069 [PERF_TYPE_HARDWARE] = {
70 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070071
David Ahern2c9e45f2011-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 Ahern2c9e45f2011-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 Ahern2c9e45f2011-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 Ahern2c9e45f2011-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 Ahern2c9e45f2011-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 Ahern2c9e45f2011-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{
David Aherne7984b72011-11-21 10:02:52 -0700444 struct addr_location al;
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200445 struct thread *thread = machine__findnew_thread(machine, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200446
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200447 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200448 pr_debug("problem processing %d event, skipping it.\n",
449 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200450 return -1;
451 }
452
David Ahern1424dc92011-03-09 22:23:28 -0700453 if (debug_mode) {
454 if (sample->time < last_timestamp) {
455 pr_err("Samples misordered, previous: %" PRIu64
456 " this: %" PRIu64 "\n", last_timestamp,
457 sample->time);
458 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200459 }
David Ahern1424dc92011-03-09 22:23:28 -0700460 last_timestamp = sample->time;
461 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200462 }
Anton Blanchard5d67be92011-07-04 21:57:50 +1000463
David Aherne7984b72011-11-21 10:02:52 -0700464 if (perf_event__preprocess_sample(event, machine, &al, sample, 0) < 0) {
465 pr_err("problem processing %d event, skipping it.\n",
466 event->header.type);
467 return -1;
468 }
469
470 if (al.filtered)
471 return 0;
472
Anton Blanchard5d67be92011-07-04 21:57:50 +1000473 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
474 return 0;
475
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200476 scripting_ops->process_event(event, sample, evsel, machine, thread);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200477
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200478 evsel->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200479 return 0;
480}
481
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200482static struct perf_tool perf_script = {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200483 .sample = process_sample_event,
David Ahernc0230b22011-03-09 22:23:27 -0700484 .mmap = perf_event__process_mmap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200485 .comm = perf_event__process_comm,
David Ahernc0230b22011-03-09 22:23:27 -0700486 .exit = perf_event__process_task,
487 .fork = perf_event__process_task,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200488 .attr = perf_event__process_attr,
489 .event_type = perf_event__process_event_type,
490 .tracing_data = perf_event__process_tracing_data,
491 .build_id = perf_event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200492 .ordered_samples = true,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200493 .ordering_requires_timestamps = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200494};
495
Tom Zanussic239da32010-04-01 23:59:18 -0500496extern volatile int session_done;
497
498static void sig_handler(int sig __unused)
499{
500 session_done = 1;
501}
502
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100503static int __cmd_script(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200504{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200505 int ret;
506
Tom Zanussic239da32010-04-01 23:59:18 -0500507 signal(SIGINT, sig_handler);
508
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200509 ret = perf_session__process_events(session, &perf_script);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200510
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200511 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200512 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200513
514 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200515}
516
Tom Zanussi956ffd02009-11-25 01:15:46 -0600517struct script_spec {
518 struct list_head node;
519 struct scripting_ops *ops;
520 char spec[0];
521};
522
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200523static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600524
525static struct script_spec *script_spec__new(const char *spec,
526 struct scripting_ops *ops)
527{
528 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
529
530 if (s != NULL) {
531 strcpy(s->spec, spec);
532 s->ops = ops;
533 }
534
535 return s;
536}
537
538static void script_spec__delete(struct script_spec *s)
539{
540 free(s->spec);
541 free(s);
542}
543
544static void script_spec__add(struct script_spec *s)
545{
546 list_add_tail(&s->node, &script_specs);
547}
548
549static struct script_spec *script_spec__find(const char *spec)
550{
551 struct script_spec *s;
552
553 list_for_each_entry(s, &script_specs, node)
554 if (strcasecmp(s->spec, spec) == 0)
555 return s;
556 return NULL;
557}
558
559static struct script_spec *script_spec__findnew(const char *spec,
560 struct scripting_ops *ops)
561{
562 struct script_spec *s = script_spec__find(spec);
563
564 if (s)
565 return s;
566
567 s = script_spec__new(spec, ops);
568 if (!s)
569 goto out_delete_spec;
570
571 script_spec__add(s);
572
573 return s;
574
575out_delete_spec:
576 script_spec__delete(s);
577
578 return NULL;
579}
580
581int script_spec_register(const char *spec, struct scripting_ops *ops)
582{
583 struct script_spec *s;
584
585 s = script_spec__find(spec);
586 if (s)
587 return -1;
588
589 s = script_spec__findnew(spec, ops);
590 if (!s)
591 return -1;
592
593 return 0;
594}
595
596static struct scripting_ops *script_spec__lookup(const char *spec)
597{
598 struct script_spec *s = script_spec__find(spec);
599 if (!s)
600 return NULL;
601
602 return s->ops;
603}
604
605static void list_available_languages(void)
606{
607 struct script_spec *s;
608
609 fprintf(stderr, "\n");
610 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100611 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600612
613 list_for_each_entry(s, &script_specs, node)
614 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
615
616 fprintf(stderr, "\n");
617}
618
619static int parse_scriptname(const struct option *opt __used,
620 const char *str, int unset __used)
621{
622 char spec[PATH_MAX];
623 const char *script, *ext;
624 int len;
625
Tom Zanussif526d682010-01-27 02:27:52 -0600626 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600627 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600628 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600629 }
630
631 script = strchr(str, ':');
632 if (script) {
633 len = script - str;
634 if (len >= PATH_MAX) {
635 fprintf(stderr, "invalid language specifier");
636 return -1;
637 }
638 strncpy(spec, str, len);
639 spec[len] = '\0';
640 scripting_ops = script_spec__lookup(spec);
641 if (!scripting_ops) {
642 fprintf(stderr, "invalid language specifier");
643 return -1;
644 }
645 script++;
646 } else {
647 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100648 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600649 if (!ext) {
650 fprintf(stderr, "invalid script extension");
651 return -1;
652 }
653 scripting_ops = script_spec__lookup(++ext);
654 if (!scripting_ops) {
655 fprintf(stderr, "invalid script extension");
656 return -1;
657 }
658 }
659
660 script_name = strdup(script);
661
662 return 0;
663}
664
David Ahern745f43e2011-03-09 22:23:26 -0700665static int parse_output_fields(const struct option *opt __used,
666 const char *arg, int unset __used)
667{
668 char *tok;
669 int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
David Ahern2c9e45f2011-03-17 10:03:21 -0600670 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700671 int rc = 0;
672 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700673 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700674
675 if (!str)
676 return -ENOMEM;
677
David Ahern2c9e45f2011-03-17 10:03:21 -0600678 /* first word can state for which event type the user is specifying
679 * the fields. If no type exists, the specified fields apply to all
680 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700681 */
David Ahern2c9e45f2011-03-17 10:03:21 -0600682 tok = strchr(str, ':');
683 if (tok) {
684 *tok = '\0';
685 tok++;
686 if (!strcmp(str, "hw"))
687 type = PERF_TYPE_HARDWARE;
688 else if (!strcmp(str, "sw"))
689 type = PERF_TYPE_SOFTWARE;
690 else if (!strcmp(str, "trace"))
691 type = PERF_TYPE_TRACEPOINT;
Arun Sharma0817a6a2011-04-14 10:38:18 -0700692 else if (!strcmp(str, "raw"))
693 type = PERF_TYPE_RAW;
David Ahern2c9e45f2011-03-17 10:03:21 -0600694 else {
695 fprintf(stderr, "Invalid event type in field string.\n");
696 return -EINVAL;
697 }
698
699 if (output[type].user_set)
700 pr_warning("Overriding previous field request for %s events.\n",
701 event_type(type));
702
703 output[type].fields = 0;
704 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600705 output[type].wildcard_set = false;
David Ahern2c9e45f2011-03-17 10:03:21 -0600706
707 } else {
708 tok = str;
709 if (strlen(str) == 0) {
710 fprintf(stderr,
711 "Cannot set fields to 'none' for all event types.\n");
712 rc = -EINVAL;
713 goto out;
714 }
715
716 if (output_set_by_user())
717 pr_warning("Overriding previous field request for all events.\n");
718
719 for (j = 0; j < PERF_TYPE_MAX; ++j) {
720 output[j].fields = 0;
721 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600722 output[j].wildcard_set = true;
David Ahern2c9e45f2011-03-17 10:03:21 -0600723 }
David Ahern1424dc92011-03-09 22:23:28 -0700724 }
725
David Ahern2c9e45f2011-03-17 10:03:21 -0600726 tok = strtok(tok, ",");
727 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -0700728 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f2011-03-17 10:03:21 -0600729 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -0700730 break;
David Ahern745f43e2011-03-09 22:23:26 -0700731 }
732 if (i == imax) {
David Ahern2c9e45f2011-03-17 10:03:21 -0600733 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -0700734 rc = -EINVAL;
David Ahern2c9e45f2011-03-17 10:03:21 -0600735 goto out;
736 }
737
738 if (type == -1) {
739 /* add user option to all events types for
740 * which it is valid
741 */
742 for (j = 0; j < PERF_TYPE_MAX; ++j) {
743 if (output[j].invalid_fields & all_output_options[i].field) {
744 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
745 all_output_options[i].str, event_type(j));
746 } else
747 output[j].fields |= all_output_options[i].field;
748 }
749 } else {
750 if (output[type].invalid_fields & all_output_options[i].field) {
751 fprintf(stderr, "\'%s\' not valid for %s events.\n",
752 all_output_options[i].str, event_type(type));
753
754 rc = -EINVAL;
755 goto out;
756 }
757 output[type].fields |= all_output_options[i].field;
758 }
759
760 tok = strtok(NULL, ",");
761 }
762
763 if (type >= 0) {
764 if (output[type].fields == 0) {
765 pr_debug("No fields requested for %s type. "
766 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -0700767 }
David Ahern1424dc92011-03-09 22:23:28 -0700768 }
David Ahern745f43e2011-03-09 22:23:26 -0700769
David Ahern2c9e45f2011-03-17 10:03:21 -0600770out:
David Ahern745f43e2011-03-09 22:23:26 -0700771 free(str);
772 return rc;
773}
774
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600775/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
776static int is_directory(const char *base_path, const struct dirent *dent)
777{
778 char path[PATH_MAX];
779 struct stat st;
780
781 sprintf(path, "%s/%s", base_path, dent->d_name);
782 if (stat(path, &st))
783 return 0;
784
785 return S_ISDIR(st.st_mode);
786}
787
788#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600789 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
790 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600791 if ((lang_dirent.d_type == DT_DIR || \
792 (lang_dirent.d_type == DT_UNKNOWN && \
793 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600794 (strcmp(lang_dirent.d_name, ".")) && \
795 (strcmp(lang_dirent.d_name, "..")))
796
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600797#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600798 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
799 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600800 if (script_dirent.d_type != DT_DIR && \
801 (script_dirent.d_type != DT_UNKNOWN || \
802 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600803
804
805#define RECORD_SUFFIX "-record"
806#define REPORT_SUFFIX "-report"
807
808struct script_desc {
809 struct list_head node;
810 char *name;
811 char *half_liner;
812 char *args;
813};
814
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200815static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600816
817static struct script_desc *script_desc__new(const char *name)
818{
819 struct script_desc *s = zalloc(sizeof(*s));
820
Tom Zanussib5b87312010-11-10 08:16:51 -0600821 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600822 s->name = strdup(name);
823
824 return s;
825}
826
827static void script_desc__delete(struct script_desc *s)
828{
829 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600830 free(s->half_liner);
831 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600832 free(s);
833}
834
835static void script_desc__add(struct script_desc *s)
836{
837 list_add_tail(&s->node, &script_descs);
838}
839
840static struct script_desc *script_desc__find(const char *name)
841{
842 struct script_desc *s;
843
844 list_for_each_entry(s, &script_descs, node)
845 if (strcasecmp(s->name, name) == 0)
846 return s;
847 return NULL;
848}
849
850static struct script_desc *script_desc__findnew(const char *name)
851{
852 struct script_desc *s = script_desc__find(name);
853
854 if (s)
855 return s;
856
857 s = script_desc__new(name);
858 if (!s)
859 goto out_delete_desc;
860
861 script_desc__add(s);
862
863 return s;
864
865out_delete_desc:
866 script_desc__delete(s);
867
868 return NULL;
869}
870
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200871static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600872{
873 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200874 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600875
876 if (strlen(str) > suffix_len) {
877 p = str + strlen(str) - suffix_len;
878 if (!strncmp(p, suffix, suffix_len))
879 return p;
880 }
881
882 return NULL;
883}
884
885static char *ltrim(char *str)
886{
887 int len = strlen(str);
888
889 while (len && isspace(*str)) {
890 len--;
891 str++;
892 }
893
894 return str;
895}
896
897static int read_script_info(struct script_desc *desc, const char *filename)
898{
899 char line[BUFSIZ], *p;
900 FILE *fp;
901
902 fp = fopen(filename, "r");
903 if (!fp)
904 return -1;
905
906 while (fgets(line, sizeof(line), fp)) {
907 p = ltrim(line);
908 if (strlen(p) == 0)
909 continue;
910 if (*p != '#')
911 continue;
912 p++;
913 if (strlen(p) && *p == '!')
914 continue;
915
916 p = ltrim(p);
917 if (strlen(p) && p[strlen(p) - 1] == '\n')
918 p[strlen(p) - 1] = '\0';
919
920 if (!strncmp(p, "description:", strlen("description:"))) {
921 p += strlen("description:");
922 desc->half_liner = strdup(ltrim(p));
923 continue;
924 }
925
926 if (!strncmp(p, "args:", strlen("args:"))) {
927 p += strlen("args:");
928 desc->args = strdup(ltrim(p));
929 continue;
930 }
931 }
932
933 fclose(fp);
934
935 return 0;
936}
937
938static int list_available_scripts(const struct option *opt __used,
939 const char *s __used, int unset __used)
940{
941 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
942 char scripts_path[MAXPATHLEN];
943 DIR *scripts_dir, *lang_dir;
944 char script_path[MAXPATHLEN];
945 char lang_path[MAXPATHLEN];
946 struct script_desc *desc;
947 char first_half[BUFSIZ];
948 char *script_root;
949 char *str;
950
951 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
952
953 scripts_dir = opendir(scripts_path);
954 if (!scripts_dir)
955 return -1;
956
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600957 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600958 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
959 lang_dirent.d_name);
960 lang_dir = opendir(lang_path);
961 if (!lang_dir)
962 continue;
963
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600964 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600965 script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200966 str = (char *)ends_with(script_root, REPORT_SUFFIX);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600967 if (str) {
968 *str = '\0';
969 desc = script_desc__findnew(script_root);
970 snprintf(script_path, MAXPATHLEN, "%s/%s",
971 lang_path, script_dirent.d_name);
972 read_script_info(desc, script_path);
973 }
974 free(script_root);
975 }
976 }
977
978 fprintf(stdout, "List of available trace scripts:\n");
979 list_for_each_entry(desc, &script_descs, node) {
980 sprintf(first_half, "%s %s", desc->name,
981 desc->args ? desc->args : "");
982 fprintf(stdout, " %-36s %s\n", first_half,
983 desc->half_liner ? desc->half_liner : "");
984 }
985
986 exit(0);
987}
988
Tom Zanussi38752942009-12-15 02:53:39 -0600989static char *get_script_path(const char *script_root, const char *suffix)
990{
991 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
992 char scripts_path[MAXPATHLEN];
993 char script_path[MAXPATHLEN];
994 DIR *scripts_dir, *lang_dir;
995 char lang_path[MAXPATHLEN];
996 char *str, *__script_root;
997 char *path = NULL;
998
999 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1000
1001 scripts_dir = opendir(scripts_path);
1002 if (!scripts_dir)
1003 return NULL;
1004
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001005 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001006 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1007 lang_dirent.d_name);
1008 lang_dir = opendir(lang_path);
1009 if (!lang_dir)
1010 continue;
1011
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001012 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001013 __script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001014 str = (char *)ends_with(__script_root, suffix);
Tom Zanussi38752942009-12-15 02:53:39 -06001015 if (str) {
1016 *str = '\0';
1017 if (strcmp(__script_root, script_root))
1018 continue;
1019 snprintf(script_path, MAXPATHLEN, "%s/%s",
1020 lang_path, script_dirent.d_name);
1021 path = strdup(script_path);
1022 free(__script_root);
1023 break;
1024 }
1025 free(__script_root);
1026 }
1027 }
1028
1029 return path;
1030}
1031
Tom Zanussib5b87312010-11-10 08:16:51 -06001032static bool is_top_script(const char *script_path)
1033{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001034 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001035}
1036
1037static int has_required_arg(char *script_path)
1038{
1039 struct script_desc *desc;
1040 int n_args = 0;
1041 char *p;
1042
1043 desc = script_desc__new(NULL);
1044
1045 if (read_script_info(desc, script_path))
1046 goto out;
1047
1048 if (!desc->args)
1049 goto out;
1050
1051 for (p = desc->args; *p; p++)
1052 if (*p == '<')
1053 n_args++;
1054out:
1055 script_desc__delete(desc);
1056
1057 return n_args;
1058}
1059
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001060static const char * const script_usage[] = {
1061 "perf script [<options>]",
1062 "perf script [<options>] record <script> [<record-options>] <command>",
1063 "perf script [<options>] report <script> [script-args]",
1064 "perf script [<options>] <script> [<record-options>] <command>",
1065 "perf script [<options>] <top-script> [script-args]",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001066 NULL
1067};
1068
1069static const struct option options[] = {
1070 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1071 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +10001072 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001073 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001074 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -04001075 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001076 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1077 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -06001078 OPT_CALLBACK('s', "script", NULL, "name",
1079 "script file name (lang:script name, script name, or *)",
1080 parse_scriptname),
1081 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001082 "generate perf-script.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +09001083 OPT_STRING('i', "input", &input_name, "file",
1084 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +02001085 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1086 "do various checks like samples ordering and lost events"),
David Ahernc0230b22011-03-09 22:23:27 -07001087 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1088 "file", "vmlinux pathname"),
1089 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1090 "file", "kallsyms pathname"),
1091 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1092 "When printing symbols do not display call chain"),
1093 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1094 "Look for files with symbols relative to this directory"),
David Ahern745f43e2011-03-09 22:23:26 -07001095 OPT_CALLBACK('f', "fields", NULL, "str",
David Ahern7cec0922011-05-30 13:08:23 -06001096 "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 -07001097 parse_output_fields),
David Ahernc8e66722011-11-13 11:30:08 -07001098 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
David Aherne7984b72011-11-21 10:02:52 -07001099 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1100 "only display events for these comms"),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001101 OPT_BOOLEAN('I', "show-info", &show_full_info,
1102 "display extended information from perf.data file"),
Masami Hiramatsu19096292009-08-21 14:56:03 -04001103 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001104};
1105
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001106static bool have_cmd(int argc, const char **argv)
1107{
1108 char **__argv = malloc(sizeof(const char *) * argc);
1109
1110 if (!__argv)
1111 die("malloc");
1112 memcpy(__argv, argv, sizeof(const char *) * argc);
1113 argc = parse_options(argc, (const char **)__argv, record_options,
1114 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1115 free(__argv);
1116
1117 return argc != 0;
1118}
1119
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001120int cmd_script(int argc, const char **argv, const char *prefix __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001121{
Tom Zanussib5b87312010-11-10 08:16:51 -06001122 char *rec_script_path = NULL;
1123 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001124 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -06001125 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001126 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -06001127 bool system_wide;
1128 int i, j, err;
Tom Zanussi38752942009-12-15 02:53:39 -06001129
Tom Zanussib5b87312010-11-10 08:16:51 -06001130 setup_scripting();
1131
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001132 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001133 PARSE_OPT_STOP_AT_NON_OPTION);
1134
1135 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1136 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1137 if (!rec_script_path)
1138 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001139 }
1140
Tom Zanussib5b87312010-11-10 08:16:51 -06001141 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1142 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1143 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001144 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001145 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001146 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001147 return -1;
1148 }
Tom Zanussi38752942009-12-15 02:53:39 -06001149 }
1150
Ben Hutchings44e668c2010-10-10 16:10:03 +01001151 /* make sure PERF_EXEC_PATH is set for scripts */
1152 perf_set_argv_exec_path(perf_exec_path());
1153
Tom Zanussib5b87312010-11-10 08:16:51 -06001154 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001155 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001156 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001157 pid_t pid;
1158
Tom Zanussib5b87312010-11-10 08:16:51 -06001159 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1160 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1161
1162 if (!rec_script_path && !rep_script_path) {
1163 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001164 " script -l for available scripts.\n", argv[0]);
1165 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001166 }
1167
Tom Zanussib5b87312010-11-10 08:16:51 -06001168 if (is_top_script(argv[0])) {
1169 rep_args = argc - 1;
1170 } else {
1171 int rec_args;
1172
1173 rep_args = has_required_arg(rep_script_path);
1174 rec_args = (argc - 1) - rep_args;
1175 if (rec_args < 0) {
1176 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001177 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001178 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001179 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001180 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001181 }
1182
1183 if (pipe(live_pipe) < 0) {
1184 perror("failed to create pipe");
1185 exit(-1);
1186 }
1187
1188 pid = fork();
1189 if (pid < 0) {
1190 perror("failed to fork");
1191 exit(-1);
1192 }
1193
1194 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001195 system_wide = true;
1196 j = 0;
1197
Tom Zanussia0cccc22010-04-01 23:59:25 -05001198 dup2(live_pipe[1], 1);
1199 close(live_pipe[0]);
1200
Tom Zanussib5b87312010-11-10 08:16:51 -06001201 if (!is_top_script(argv[0]))
1202 system_wide = !have_cmd(argc - rep_args,
1203 &argv[rep_args]);
1204
1205 __argv = malloc((argc + 6) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001206 if (!__argv)
1207 die("malloc");
1208
Tom Zanussib5b87312010-11-10 08:16:51 -06001209 __argv[j++] = "/bin/sh";
1210 __argv[j++] = rec_script_path;
1211 if (system_wide)
1212 __argv[j++] = "-a";
1213 __argv[j++] = "-q";
1214 __argv[j++] = "-o";
1215 __argv[j++] = "-";
1216 for (i = rep_args + 1; i < argc; i++)
1217 __argv[j++] = argv[i];
1218 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001219
1220 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001221 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001222 exit(-1);
1223 }
1224
1225 dup2(live_pipe[0], 0);
1226 close(live_pipe[1]);
1227
Tom Zanussib5b87312010-11-10 08:16:51 -06001228 __argv = malloc((argc + 4) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001229 if (!__argv)
1230 die("malloc");
Tom Zanussib5b87312010-11-10 08:16:51 -06001231 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001232 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001233 __argv[j++] = rep_script_path;
1234 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001235 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001236 __argv[j++] = "-i";
1237 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001238 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001239
1240 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001241 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001242 exit(-1);
1243 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001244
Tom Zanussib5b87312010-11-10 08:16:51 -06001245 if (rec_script_path)
1246 script_path = rec_script_path;
1247 if (rep_script_path)
1248 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001249
Tom Zanussib5b87312010-11-10 08:16:51 -06001250 if (script_path) {
1251 system_wide = false;
1252 j = 0;
1253
1254 if (rec_script_path)
1255 system_wide = !have_cmd(argc - 1, &argv[1]);
1256
1257 __argv = malloc((argc + 2) * sizeof(const char *));
1258 if (!__argv)
1259 die("malloc");
1260 __argv[j++] = "/bin/sh";
1261 __argv[j++] = script_path;
1262 if (system_wide)
1263 __argv[j++] = "-a";
1264 for (i = 2; i < argc; i++)
1265 __argv[j++] = argv[i];
1266 __argv[j++] = NULL;
1267
1268 execvp("/bin/sh", (char **)__argv);
1269 free(__argv);
1270 exit(-1);
1271 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001272
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001273 if (symbol__init() < 0)
1274 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -06001275 if (!script_name)
1276 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001277
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001278 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_script);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001279 if (session == NULL)
1280 return -ENOMEM;
1281
Anton Blanchard5d67be92011-07-04 21:57:50 +10001282 if (cpu_list) {
1283 if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
1284 return -1;
1285 }
1286
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001287 perf_session__fprintf_info(session, stdout, show_full_info);
1288
David Ahern1424dc92011-03-09 22:23:28 -07001289 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001290 symbol_conf.use_callchain = true;
1291 else
1292 symbol_conf.use_callchain = false;
1293
Tom Zanussi956ffd02009-11-25 01:15:46 -06001294 if (generate_script_lang) {
1295 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001296 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001297
David Ahern2c9e45f2011-03-17 10:03:21 -06001298 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001299 fprintf(stderr,
1300 "custom fields not supported for generated scripts");
1301 return -1;
1302 }
1303
1304 input = open(input_name, O_RDONLY);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001305 if (input < 0) {
1306 perror("failed to open file");
1307 exit(-1);
1308 }
1309
1310 err = fstat(input, &perf_stat);
1311 if (err < 0) {
1312 perror("failed to stat file");
1313 exit(-1);
1314 }
1315
1316 if (!perf_stat.st_size) {
1317 fprintf(stderr, "zero-sized file, nothing to do!\n");
1318 exit(0);
1319 }
1320
1321 scripting_ops = script_spec__lookup(generate_script_lang);
1322 if (!scripting_ops) {
1323 fprintf(stderr, "invalid language specifier");
1324 return -1;
1325 }
1326
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001327 err = scripting_ops->generate_script("perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001328 goto out;
1329 }
1330
1331 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001332 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001333 if (err)
1334 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001335 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001336 }
1337
David Ahern9cbdb702011-04-06 21:54:20 -06001338
1339 err = perf_session__check_output_opt(session);
1340 if (err < 0)
1341 goto out;
1342
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001343 err = __cmd_script(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001344
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001345 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001346 cleanup_scripting();
1347out:
1348 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001349}