blob: ccbfd56d7549cd7973331b38201661a30b2841e4 [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{
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 Ahern2c9e45f72011-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 Ahern2c9e45f72011-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 Ahern2c9e45f72011-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 Ahern2c9e45f72011-03-17 10:03:21 -0600694 else {
695 fprintf(stderr, "Invalid event type in field string.\n");
Robert Richter38efb532011-11-25 11:38:40 +0100696 rc = -EINVAL;
697 goto out;
David Ahern2c9e45f72011-03-17 10:03:21 -0600698 }
699
700 if (output[type].user_set)
701 pr_warning("Overriding previous field request for %s events.\n",
702 event_type(type));
703
704 output[type].fields = 0;
705 output[type].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600706 output[type].wildcard_set = false;
David Ahern2c9e45f72011-03-17 10:03:21 -0600707
708 } else {
709 tok = str;
710 if (strlen(str) == 0) {
711 fprintf(stderr,
712 "Cannot set fields to 'none' for all event types.\n");
713 rc = -EINVAL;
714 goto out;
715 }
716
717 if (output_set_by_user())
718 pr_warning("Overriding previous field request for all events.\n");
719
720 for (j = 0; j < PERF_TYPE_MAX; ++j) {
721 output[j].fields = 0;
722 output[j].user_set = true;
David Ahern9cbdb702011-04-06 21:54:20 -0600723 output[j].wildcard_set = true;
David Ahern2c9e45f72011-03-17 10:03:21 -0600724 }
David Ahern1424dc92011-03-09 22:23:28 -0700725 }
726
David Ahern2c9e45f72011-03-17 10:03:21 -0600727 tok = strtok(tok, ",");
728 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -0700729 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600730 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -0700731 break;
David Ahern745f43e2011-03-09 22:23:26 -0700732 }
733 if (i == imax) {
David Ahern2c9e45f72011-03-17 10:03:21 -0600734 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -0700735 rc = -EINVAL;
David Ahern2c9e45f72011-03-17 10:03:21 -0600736 goto out;
737 }
738
739 if (type == -1) {
740 /* add user option to all events types for
741 * which it is valid
742 */
743 for (j = 0; j < PERF_TYPE_MAX; ++j) {
744 if (output[j].invalid_fields & all_output_options[i].field) {
745 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
746 all_output_options[i].str, event_type(j));
747 } else
748 output[j].fields |= all_output_options[i].field;
749 }
750 } else {
751 if (output[type].invalid_fields & all_output_options[i].field) {
752 fprintf(stderr, "\'%s\' not valid for %s events.\n",
753 all_output_options[i].str, event_type(type));
754
755 rc = -EINVAL;
756 goto out;
757 }
758 output[type].fields |= all_output_options[i].field;
759 }
760
761 tok = strtok(NULL, ",");
762 }
763
764 if (type >= 0) {
765 if (output[type].fields == 0) {
766 pr_debug("No fields requested for %s type. "
767 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -0700768 }
David Ahern1424dc92011-03-09 22:23:28 -0700769 }
David Ahern745f43e2011-03-09 22:23:26 -0700770
David Ahern2c9e45f72011-03-17 10:03:21 -0600771out:
David Ahern745f43e2011-03-09 22:23:26 -0700772 free(str);
773 return rc;
774}
775
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600776/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
777static int is_directory(const char *base_path, const struct dirent *dent)
778{
779 char path[PATH_MAX];
780 struct stat st;
781
782 sprintf(path, "%s/%s", base_path, dent->d_name);
783 if (stat(path, &st))
784 return 0;
785
786 return S_ISDIR(st.st_mode);
787}
788
789#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600790 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
791 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600792 if ((lang_dirent.d_type == DT_DIR || \
793 (lang_dirent.d_type == DT_UNKNOWN && \
794 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600795 (strcmp(lang_dirent.d_name, ".")) && \
796 (strcmp(lang_dirent.d_name, "..")))
797
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600798#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600799 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
800 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600801 if (script_dirent.d_type != DT_DIR && \
802 (script_dirent.d_type != DT_UNKNOWN || \
803 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600804
805
806#define RECORD_SUFFIX "-record"
807#define REPORT_SUFFIX "-report"
808
809struct script_desc {
810 struct list_head node;
811 char *name;
812 char *half_liner;
813 char *args;
814};
815
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200816static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600817
818static struct script_desc *script_desc__new(const char *name)
819{
820 struct script_desc *s = zalloc(sizeof(*s));
821
Tom Zanussib5b87312010-11-10 08:16:51 -0600822 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600823 s->name = strdup(name);
824
825 return s;
826}
827
828static void script_desc__delete(struct script_desc *s)
829{
830 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600831 free(s->half_liner);
832 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600833 free(s);
834}
835
836static void script_desc__add(struct script_desc *s)
837{
838 list_add_tail(&s->node, &script_descs);
839}
840
841static struct script_desc *script_desc__find(const char *name)
842{
843 struct script_desc *s;
844
845 list_for_each_entry(s, &script_descs, node)
846 if (strcasecmp(s->name, name) == 0)
847 return s;
848 return NULL;
849}
850
851static struct script_desc *script_desc__findnew(const char *name)
852{
853 struct script_desc *s = script_desc__find(name);
854
855 if (s)
856 return s;
857
858 s = script_desc__new(name);
859 if (!s)
860 goto out_delete_desc;
861
862 script_desc__add(s);
863
864 return s;
865
866out_delete_desc:
867 script_desc__delete(s);
868
869 return NULL;
870}
871
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200872static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600873{
874 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200875 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600876
877 if (strlen(str) > suffix_len) {
878 p = str + strlen(str) - suffix_len;
879 if (!strncmp(p, suffix, suffix_len))
880 return p;
881 }
882
883 return NULL;
884}
885
886static char *ltrim(char *str)
887{
888 int len = strlen(str);
889
890 while (len && isspace(*str)) {
891 len--;
892 str++;
893 }
894
895 return str;
896}
897
898static int read_script_info(struct script_desc *desc, const char *filename)
899{
900 char line[BUFSIZ], *p;
901 FILE *fp;
902
903 fp = fopen(filename, "r");
904 if (!fp)
905 return -1;
906
907 while (fgets(line, sizeof(line), fp)) {
908 p = ltrim(line);
909 if (strlen(p) == 0)
910 continue;
911 if (*p != '#')
912 continue;
913 p++;
914 if (strlen(p) && *p == '!')
915 continue;
916
917 p = ltrim(p);
918 if (strlen(p) && p[strlen(p) - 1] == '\n')
919 p[strlen(p) - 1] = '\0';
920
921 if (!strncmp(p, "description:", strlen("description:"))) {
922 p += strlen("description:");
923 desc->half_liner = strdup(ltrim(p));
924 continue;
925 }
926
927 if (!strncmp(p, "args:", strlen("args:"))) {
928 p += strlen("args:");
929 desc->args = strdup(ltrim(p));
930 continue;
931 }
932 }
933
934 fclose(fp);
935
936 return 0;
937}
938
Robert Richter38efb532011-11-25 11:38:40 +0100939static char *get_script_root(struct dirent *script_dirent, const char *suffix)
940{
941 char *script_root, *str;
942
943 script_root = strdup(script_dirent->d_name);
944 if (!script_root)
945 return NULL;
946
947 str = (char *)ends_with(script_root, suffix);
948 if (!str) {
949 free(script_root);
950 return NULL;
951 }
952
953 *str = '\0';
954 return script_root;
955}
956
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600957static int list_available_scripts(const struct option *opt __used,
958 const char *s __used, int unset __used)
959{
960 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
961 char scripts_path[MAXPATHLEN];
962 DIR *scripts_dir, *lang_dir;
963 char script_path[MAXPATHLEN];
964 char lang_path[MAXPATHLEN];
965 struct script_desc *desc;
966 char first_half[BUFSIZ];
967 char *script_root;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600968
969 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
970
971 scripts_dir = opendir(scripts_path);
972 if (!scripts_dir)
973 return -1;
974
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600975 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600976 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
977 lang_dirent.d_name);
978 lang_dir = opendir(lang_path);
979 if (!lang_dir)
980 continue;
981
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600982 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +0100983 script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
984 if (script_root) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600985 desc = script_desc__findnew(script_root);
986 snprintf(script_path, MAXPATHLEN, "%s/%s",
987 lang_path, script_dirent.d_name);
988 read_script_info(desc, script_path);
Robert Richter38efb532011-11-25 11:38:40 +0100989 free(script_root);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600990 }
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600991 }
992 }
993
994 fprintf(stdout, "List of available trace scripts:\n");
995 list_for_each_entry(desc, &script_descs, node) {
996 sprintf(first_half, "%s %s", desc->name,
997 desc->args ? desc->args : "");
998 fprintf(stdout, " %-36s %s\n", first_half,
999 desc->half_liner ? desc->half_liner : "");
1000 }
1001
1002 exit(0);
1003}
1004
Tom Zanussi38752942009-12-15 02:53:39 -06001005static char *get_script_path(const char *script_root, const char *suffix)
1006{
1007 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
1008 char scripts_path[MAXPATHLEN];
1009 char script_path[MAXPATHLEN];
1010 DIR *scripts_dir, *lang_dir;
1011 char lang_path[MAXPATHLEN];
Robert Richter38efb532011-11-25 11:38:40 +01001012 char *__script_root;
Tom Zanussi38752942009-12-15 02:53:39 -06001013
1014 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
1015
1016 scripts_dir = opendir(scripts_path);
1017 if (!scripts_dir)
1018 return NULL;
1019
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001020 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -06001021 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
1022 lang_dirent.d_name);
1023 lang_dir = opendir(lang_path);
1024 if (!lang_dir)
1025 continue;
1026
Shawn Bohrer008f29d2010-11-21 10:09:39 -06001027 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Robert Richter38efb532011-11-25 11:38:40 +01001028 __script_root = get_script_root(&script_dirent, suffix);
1029 if (__script_root && !strcmp(script_root, __script_root)) {
1030 free(__script_root);
Tom Zanussi38752942009-12-15 02:53:39 -06001031 snprintf(script_path, MAXPATHLEN, "%s/%s",
1032 lang_path, script_dirent.d_name);
Robert Richter38efb532011-11-25 11:38:40 +01001033 return strdup(script_path);
Tom Zanussi38752942009-12-15 02:53:39 -06001034 }
1035 free(__script_root);
1036 }
1037 }
1038
Robert Richter38efb532011-11-25 11:38:40 +01001039 return NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001040}
1041
Tom Zanussib5b87312010-11-10 08:16:51 -06001042static bool is_top_script(const char *script_path)
1043{
Stephane Eranian965bb6b2010-12-03 17:52:01 +02001044 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -06001045}
1046
1047static int has_required_arg(char *script_path)
1048{
1049 struct script_desc *desc;
1050 int n_args = 0;
1051 char *p;
1052
1053 desc = script_desc__new(NULL);
1054
1055 if (read_script_info(desc, script_path))
1056 goto out;
1057
1058 if (!desc->args)
1059 goto out;
1060
1061 for (p = desc->args; *p; p++)
1062 if (*p == '<')
1063 n_args++;
1064out:
1065 script_desc__delete(desc);
1066
1067 return n_args;
1068}
1069
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001070static const char * const script_usage[] = {
1071 "perf script [<options>]",
1072 "perf script [<options>] record <script> [<record-options>] <command>",
1073 "perf script [<options>] report <script> [script-args]",
1074 "perf script [<options>] <script> [<record-options>] <command>",
1075 "perf script [<options>] <top-script> [script-args]",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001076 NULL
1077};
1078
1079static const struct option options[] = {
1080 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1081 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +10001082 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001083 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001084 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -04001085 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -06001086 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
1087 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -06001088 OPT_CALLBACK('s', "script", NULL, "name",
1089 "script file name (lang:script name, script name, or *)",
1090 parse_scriptname),
1091 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001092 "generate perf-script.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +09001093 OPT_STRING('i', "input", &input_name, "file",
1094 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +02001095 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
1096 "do various checks like samples ordering and lost events"),
David Ahernc0230b22011-03-09 22:23:27 -07001097 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1098 "file", "vmlinux pathname"),
1099 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1100 "file", "kallsyms pathname"),
1101 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
1102 "When printing symbols do not display call chain"),
1103 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1104 "Look for files with symbols relative to this directory"),
David Ahern745f43e2011-03-09 22:23:26 -07001105 OPT_CALLBACK('f', "fields", NULL, "str",
David Ahern7cec0922011-05-30 13:08:23 -06001106 "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 -07001107 parse_output_fields),
David Ahernc8e66722011-11-13 11:30:08 -07001108 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
David Aherne7984b72011-11-21 10:02:52 -07001109 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1110 "only display events for these comms"),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001111 OPT_BOOLEAN('I', "show-info", &show_full_info,
1112 "display extended information from perf.data file"),
Masami Hiramatsu19096292009-08-21 14:56:03 -04001113 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001114};
1115
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001116static bool have_cmd(int argc, const char **argv)
1117{
1118 char **__argv = malloc(sizeof(const char *) * argc);
1119
1120 if (!__argv)
1121 die("malloc");
1122 memcpy(__argv, argv, sizeof(const char *) * argc);
1123 argc = parse_options(argc, (const char **)__argv, record_options,
1124 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
1125 free(__argv);
1126
1127 return argc != 0;
1128}
1129
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001130int cmd_script(int argc, const char **argv, const char *prefix __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001131{
Tom Zanussib5b87312010-11-10 08:16:51 -06001132 char *rec_script_path = NULL;
1133 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001134 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -06001135 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001136 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -06001137 bool system_wide;
1138 int i, j, err;
Tom Zanussi38752942009-12-15 02:53:39 -06001139
Tom Zanussib5b87312010-11-10 08:16:51 -06001140 setup_scripting();
1141
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001142 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -06001143 PARSE_OPT_STOP_AT_NON_OPTION);
1144
1145 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
1146 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
1147 if (!rec_script_path)
1148 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -06001149 }
1150
Tom Zanussib5b87312010-11-10 08:16:51 -06001151 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
1152 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
1153 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -06001154 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -06001155 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001156 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -06001157 return -1;
1158 }
Tom Zanussi38752942009-12-15 02:53:39 -06001159 }
1160
Ben Hutchings44e668c2010-10-10 16:10:03 +01001161 /* make sure PERF_EXEC_PATH is set for scripts */
1162 perf_set_argv_exec_path(perf_exec_path());
1163
Tom Zanussib5b87312010-11-10 08:16:51 -06001164 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -05001165 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -06001166 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001167 pid_t pid;
1168
Tom Zanussib5b87312010-11-10 08:16:51 -06001169 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
1170 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
1171
1172 if (!rec_script_path && !rep_script_path) {
1173 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001174 " script -l for available scripts.\n", argv[0]);
1175 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001176 }
1177
Tom Zanussib5b87312010-11-10 08:16:51 -06001178 if (is_top_script(argv[0])) {
1179 rep_args = argc - 1;
1180 } else {
1181 int rec_args;
1182
1183 rep_args = has_required_arg(rep_script_path);
1184 rec_args = (argc - 1) - rep_args;
1185 if (rec_args < 0) {
1186 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001187 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -06001188 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001189 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -06001190 }
Tom Zanussia0cccc22010-04-01 23:59:25 -05001191 }
1192
1193 if (pipe(live_pipe) < 0) {
1194 perror("failed to create pipe");
1195 exit(-1);
1196 }
1197
1198 pid = fork();
1199 if (pid < 0) {
1200 perror("failed to fork");
1201 exit(-1);
1202 }
1203
1204 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001205 system_wide = true;
1206 j = 0;
1207
Tom Zanussia0cccc22010-04-01 23:59:25 -05001208 dup2(live_pipe[1], 1);
1209 close(live_pipe[0]);
1210
Tom Zanussib5b87312010-11-10 08:16:51 -06001211 if (!is_top_script(argv[0]))
1212 system_wide = !have_cmd(argc - rep_args,
1213 &argv[rep_args]);
1214
1215 __argv = malloc((argc + 6) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001216 if (!__argv)
1217 die("malloc");
1218
Tom Zanussib5b87312010-11-10 08:16:51 -06001219 __argv[j++] = "/bin/sh";
1220 __argv[j++] = rec_script_path;
1221 if (system_wide)
1222 __argv[j++] = "-a";
1223 __argv[j++] = "-q";
1224 __argv[j++] = "-o";
1225 __argv[j++] = "-";
1226 for (i = rep_args + 1; i < argc; i++)
1227 __argv[j++] = argv[i];
1228 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001229
1230 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001231 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001232 exit(-1);
1233 }
1234
1235 dup2(live_pipe[0], 0);
1236 close(live_pipe[1]);
1237
Tom Zanussib5b87312010-11-10 08:16:51 -06001238 __argv = malloc((argc + 4) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001239 if (!__argv)
1240 die("malloc");
Tom Zanussib5b87312010-11-10 08:16:51 -06001241 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001242 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001243 __argv[j++] = rep_script_path;
1244 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001245 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001246 __argv[j++] = "-i";
1247 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001248 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001249
1250 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001251 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001252 exit(-1);
1253 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001254
Tom Zanussib5b87312010-11-10 08:16:51 -06001255 if (rec_script_path)
1256 script_path = rec_script_path;
1257 if (rep_script_path)
1258 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001259
Tom Zanussib5b87312010-11-10 08:16:51 -06001260 if (script_path) {
1261 system_wide = false;
1262 j = 0;
1263
1264 if (rec_script_path)
1265 system_wide = !have_cmd(argc - 1, &argv[1]);
1266
1267 __argv = malloc((argc + 2) * sizeof(const char *));
1268 if (!__argv)
1269 die("malloc");
1270 __argv[j++] = "/bin/sh";
1271 __argv[j++] = script_path;
1272 if (system_wide)
1273 __argv[j++] = "-a";
1274 for (i = 2; i < argc; i++)
1275 __argv[j++] = argv[i];
1276 __argv[j++] = NULL;
1277
1278 execvp("/bin/sh", (char **)__argv);
1279 free(__argv);
1280 exit(-1);
1281 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001282
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001283 if (symbol__init() < 0)
1284 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -06001285 if (!script_name)
1286 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001287
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02001288 session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_script);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001289 if (session == NULL)
1290 return -ENOMEM;
1291
Anton Blanchard5d67be92011-07-04 21:57:50 +10001292 if (cpu_list) {
1293 if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
1294 return -1;
1295 }
1296
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001297 perf_session__fprintf_info(session, stdout, show_full_info);
1298
David Ahern1424dc92011-03-09 22:23:28 -07001299 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001300 symbol_conf.use_callchain = true;
1301 else
1302 symbol_conf.use_callchain = false;
1303
Tom Zanussi956ffd02009-11-25 01:15:46 -06001304 if (generate_script_lang) {
1305 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001306 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001307
David Ahern2c9e45f72011-03-17 10:03:21 -06001308 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001309 fprintf(stderr,
1310 "custom fields not supported for generated scripts");
1311 return -1;
1312 }
1313
1314 input = open(input_name, O_RDONLY);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001315 if (input < 0) {
1316 perror("failed to open file");
1317 exit(-1);
1318 }
1319
1320 err = fstat(input, &perf_stat);
1321 if (err < 0) {
1322 perror("failed to stat file");
1323 exit(-1);
1324 }
1325
1326 if (!perf_stat.st_size) {
1327 fprintf(stderr, "zero-sized file, nothing to do!\n");
1328 exit(0);
1329 }
1330
1331 scripting_ops = script_spec__lookup(generate_script_lang);
1332 if (!scripting_ops) {
1333 fprintf(stderr, "invalid language specifier");
1334 return -1;
1335 }
1336
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001337 err = scripting_ops->generate_script("perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001338 goto out;
1339 }
1340
1341 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001342 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001343 if (err)
1344 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001345 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001346 }
1347
David Ahern9cbdb702011-04-06 21:54:20 -06001348
1349 err = perf_session__check_output_opt(session);
1350 if (err < 0)
1351 goto out;
1352
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001353 err = __cmd_script(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001354
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001355 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001356 cleanup_scripting();
1357out:
1358 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001359}