blob: 6cf811acc41b0313cef295b11e413447a41800e4 [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"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020010#include "util/symbol.h"
11#include "util/thread.h"
Ingo Molnarcf723442009-11-28 10:11:00 +010012#include "util/trace-event.h"
Tom Zanussi34c86ea2010-11-10 08:15:43 -060013#include "util/parse-options.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"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020017
Tom Zanussi956ffd02009-11-25 01:15:46 -060018static char const *script_name;
19static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020020static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020021static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020022static u64 nr_unordered;
Tom Zanussi34c86ea2010-11-10 08:15:43 -060023extern const struct option record_options[];
David Ahernc0230b22011-03-09 22:23:27 -070024static bool no_callchain;
Tom Zanussi956ffd02009-11-25 01:15:46 -060025
David Ahern745f43e2011-03-09 22:23:26 -070026enum perf_output_field {
27 PERF_OUTPUT_COMM = 1U << 0,
28 PERF_OUTPUT_TID = 1U << 1,
29 PERF_OUTPUT_PID = 1U << 2,
30 PERF_OUTPUT_TIME = 1U << 3,
31 PERF_OUTPUT_CPU = 1U << 4,
32 PERF_OUTPUT_EVNAME = 1U << 5,
33 PERF_OUTPUT_TRACE = 1U << 6,
David Ahernc0230b22011-03-09 22:23:27 -070034 PERF_OUTPUT_SYM = 1U << 7,
David Ahern745f43e2011-03-09 22:23:26 -070035};
36
37struct output_option {
38 const char *str;
39 enum perf_output_field field;
40} all_output_options[] = {
41 {.str = "comm", .field = PERF_OUTPUT_COMM},
42 {.str = "tid", .field = PERF_OUTPUT_TID},
43 {.str = "pid", .field = PERF_OUTPUT_PID},
44 {.str = "time", .field = PERF_OUTPUT_TIME},
45 {.str = "cpu", .field = PERF_OUTPUT_CPU},
46 {.str = "event", .field = PERF_OUTPUT_EVNAME},
47 {.str = "trace", .field = PERF_OUTPUT_TRACE},
David Ahernc0230b22011-03-09 22:23:27 -070048 {.str = "sym", .field = PERF_OUTPUT_SYM},
David Ahern745f43e2011-03-09 22:23:26 -070049};
50
51/* default set to maintain compatibility with current format */
David Ahern2c9e45f2011-03-17 10:03:21 -060052static struct {
53 bool user_set;
54 u64 fields;
55 u64 invalid_fields;
56} output[PERF_TYPE_MAX] = {
David Ahern1424dc92011-03-09 22:23:28 -070057
David Ahern2c9e45f2011-03-17 10:03:21 -060058 [PERF_TYPE_HARDWARE] = {
59 .user_set = false,
David Ahern1424dc92011-03-09 22:23:28 -070060
David Ahern2c9e45f2011-03-17 10:03:21 -060061 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
62 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
63 PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM,
64
65 .invalid_fields = PERF_OUTPUT_TRACE,
66 },
67
68 [PERF_TYPE_SOFTWARE] = {
69 .user_set = false,
70
71 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
72 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
73 PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM,
74
75 .invalid_fields = PERF_OUTPUT_TRACE,
76 },
77
78 [PERF_TYPE_TRACEPOINT] = {
79 .user_set = false,
80
81 .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
82 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
83 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
84 },
David Ahern1424dc92011-03-09 22:23:28 -070085};
David Ahern745f43e2011-03-09 22:23:26 -070086
David Ahern2c9e45f2011-03-17 10:03:21 -060087static bool output_set_by_user(void)
88{
89 int j;
90 for (j = 0; j < PERF_TYPE_MAX; ++j) {
91 if (output[j].user_set)
92 return true;
93 }
94 return false;
95}
David Ahern745f43e2011-03-09 22:23:26 -070096
David Ahern2c9e45f2011-03-17 10:03:21 -060097#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
David Ahern1424dc92011-03-09 22:23:28 -070098
99static int perf_session__check_attr(struct perf_session *session,
100 struct perf_event_attr *attr)
101{
102 if (PRINT_FIELD(TRACE) &&
103 !perf_session__has_traces(session, "record -R"))
104 return -EINVAL;
105
106 if (PRINT_FIELD(SYM)) {
107 if (!(session->sample_type & PERF_SAMPLE_IP)) {
108 pr_err("Samples do not contain IP data.\n");
109 return -EINVAL;
110 }
111 if (!no_callchain &&
112 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
113 symbol_conf.use_callchain = false;
114 }
115
116 if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
117 !(session->sample_type & PERF_SAMPLE_TID)) {
118 pr_err("Samples do not contain TID/PID data.\n");
119 return -EINVAL;
120 }
121
122 if (PRINT_FIELD(TIME) &&
123 !(session->sample_type & PERF_SAMPLE_TIME)) {
124 pr_err("Samples do not contain timestamps.\n");
125 return -EINVAL;
126 }
127
128 if (PRINT_FIELD(CPU) &&
129 !(session->sample_type & PERF_SAMPLE_CPU)) {
130 pr_err("Samples do not contain cpu.\n");
131 return -EINVAL;
132 }
133
134 return 0;
135}
David Ahern745f43e2011-03-09 22:23:26 -0700136
David Ahernc70c94b2011-03-09 22:23:25 -0700137static void print_sample_start(struct perf_sample *sample,
David Ahern1424dc92011-03-09 22:23:28 -0700138 struct thread *thread,
139 struct perf_event_attr *attr)
David Ahernc70c94b2011-03-09 22:23:25 -0700140{
141 int type;
142 struct event *event;
143 const char *evname = NULL;
144 unsigned long secs;
145 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -0700146 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -0700147
David Ahern745f43e2011-03-09 22:23:26 -0700148 if (PRINT_FIELD(COMM)) {
149 if (latency_format)
150 printf("%8.8s ", thread->comm);
David Ahernc0230b22011-03-09 22:23:27 -0700151 else if (PRINT_FIELD(SYM) && symbol_conf.use_callchain)
152 printf("%s ", thread->comm);
David Ahern745f43e2011-03-09 22:23:26 -0700153 else
154 printf("%16s ", thread->comm);
155 }
David Ahernc70c94b2011-03-09 22:23:25 -0700156
David Ahern745f43e2011-03-09 22:23:26 -0700157 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
158 printf("%5d/%-5d ", sample->pid, sample->tid);
159 else if (PRINT_FIELD(PID))
160 printf("%5d ", sample->pid);
161 else if (PRINT_FIELD(TID))
162 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -0700163
David Ahern745f43e2011-03-09 22:23:26 -0700164 if (PRINT_FIELD(CPU)) {
165 if (latency_format)
166 printf("%3d ", sample->cpu);
167 else
168 printf("[%03d] ", sample->cpu);
169 }
David Ahernc70c94b2011-03-09 22:23:25 -0700170
David Ahern745f43e2011-03-09 22:23:26 -0700171 if (PRINT_FIELD(TIME)) {
172 nsecs = sample->time;
173 secs = nsecs / NSECS_PER_SEC;
174 nsecs -= secs * NSECS_PER_SEC;
175 usecs = nsecs / NSECS_PER_USEC;
176 printf("%5lu.%06lu: ", secs, usecs);
177 }
178
179 if (PRINT_FIELD(EVNAME)) {
David Ahern1424dc92011-03-09 22:23:28 -0700180 if (attr->type == PERF_TYPE_TRACEPOINT) {
181 type = trace_parse_common_type(sample->raw_data);
182 event = trace_find_event(type);
183 if (event)
184 evname = event->name;
185 } else
186 evname = __event_name(attr->type, attr->config);
David Ahern745f43e2011-03-09 22:23:26 -0700187
188 printf("%s: ", evname ? evname : "(unknown)");
189 }
David Ahernc70c94b2011-03-09 22:23:25 -0700190}
191
David Ahernbe6d8422011-03-09 22:23:23 -0700192static void process_event(union perf_event *event __unused,
193 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300194 struct perf_evsel *evsel,
David Ahern1424dc92011-03-09 22:23:28 -0700195 struct perf_session *session,
David Ahernbe6d8422011-03-09 22:23:23 -0700196 struct thread *thread)
197{
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300198 struct perf_event_attr *attr = &evsel->attr;
David Ahern1424dc92011-03-09 22:23:28 -0700199
David Ahern2c9e45f2011-03-17 10:03:21 -0600200 if (output[attr->type].fields == 0)
David Ahern1424dc92011-03-09 22:23:28 -0700201 return;
202
203 if (perf_session__check_attr(session, attr) < 0)
204 return;
205
206 print_sample_start(sample, thread, attr);
David Ahern745f43e2011-03-09 22:23:26 -0700207
208 if (PRINT_FIELD(TRACE))
209 print_trace_event(sample->cpu, sample->raw_data,
210 sample->raw_size);
211
David Ahernc0230b22011-03-09 22:23:27 -0700212 if (PRINT_FIELD(SYM)) {
213 if (!symbol_conf.use_callchain)
214 printf(" ");
215 else
216 printf("\n");
217 perf_session__print_symbols(event, sample, session);
218 }
219
David Ahernc70c94b2011-03-09 22:23:25 -0700220 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700221}
222
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600223static int default_start_script(const char *script __unused,
224 int argc __unused,
225 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600226{
227 return 0;
228}
229
230static int default_stop_script(void)
231{
232 return 0;
233}
234
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600235static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600236{
237 return 0;
238}
239
240static struct scripting_ops default_scripting_ops = {
241 .start_script = default_start_script,
242 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700243 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600244 .generate_script = default_generate_script,
245};
246
247static struct scripting_ops *scripting_ops;
248
249static void setup_scripting(void)
250{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600251 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600252 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600253
Tom Zanussi956ffd02009-11-25 01:15:46 -0600254 scripting_ops = &default_scripting_ops;
255}
256
257static int cleanup_scripting(void)
258{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100259 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500260
Tom Zanussi956ffd02009-11-25 01:15:46 -0600261 return scripting_ops->stop_script();
262}
263
Tom Zanussi956ffd02009-11-25 01:15:46 -0600264static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200265
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200266static int process_sample_event(union perf_event *event,
267 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300268 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200269 struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200270{
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200271 struct thread *thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200272
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200273 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200274 pr_debug("problem processing %d event, skipping it.\n",
275 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200276 return -1;
277 }
278
David Ahern1424dc92011-03-09 22:23:28 -0700279 if (debug_mode) {
280 if (sample->time < last_timestamp) {
281 pr_err("Samples misordered, previous: %" PRIu64
282 " this: %" PRIu64 "\n", last_timestamp,
283 sample->time);
284 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200285 }
David Ahern1424dc92011-03-09 22:23:28 -0700286 last_timestamp = sample->time;
287 return 0;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200288 }
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300289 scripting_ops->process_event(event, sample, evsel, session, thread);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200290
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200291 session->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200292 return 0;
293}
294
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200295static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200296 .sample = process_sample_event,
David Ahernc0230b22011-03-09 22:23:27 -0700297 .mmap = perf_event__process_mmap,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200298 .comm = perf_event__process_comm,
David Ahernc0230b22011-03-09 22:23:27 -0700299 .exit = perf_event__process_task,
300 .fork = perf_event__process_task,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200301 .attr = perf_event__process_attr,
302 .event_type = perf_event__process_event_type,
303 .tracing_data = perf_event__process_tracing_data,
304 .build_id = perf_event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200305 .ordered_samples = true,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200306 .ordering_requires_timestamps = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200307};
308
Tom Zanussic239da32010-04-01 23:59:18 -0500309extern volatile int session_done;
310
311static void sig_handler(int sig __unused)
312{
313 session_done = 1;
314}
315
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100316static int __cmd_script(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200317{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200318 int ret;
319
Tom Zanussic239da32010-04-01 23:59:18 -0500320 signal(SIGINT, sig_handler);
321
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200322 ret = perf_session__process_events(session, &event_ops);
323
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200324 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200325 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200326
327 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200328}
329
Tom Zanussi956ffd02009-11-25 01:15:46 -0600330struct script_spec {
331 struct list_head node;
332 struct scripting_ops *ops;
333 char spec[0];
334};
335
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200336static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600337
338static struct script_spec *script_spec__new(const char *spec,
339 struct scripting_ops *ops)
340{
341 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
342
343 if (s != NULL) {
344 strcpy(s->spec, spec);
345 s->ops = ops;
346 }
347
348 return s;
349}
350
351static void script_spec__delete(struct script_spec *s)
352{
353 free(s->spec);
354 free(s);
355}
356
357static void script_spec__add(struct script_spec *s)
358{
359 list_add_tail(&s->node, &script_specs);
360}
361
362static struct script_spec *script_spec__find(const char *spec)
363{
364 struct script_spec *s;
365
366 list_for_each_entry(s, &script_specs, node)
367 if (strcasecmp(s->spec, spec) == 0)
368 return s;
369 return NULL;
370}
371
372static struct script_spec *script_spec__findnew(const char *spec,
373 struct scripting_ops *ops)
374{
375 struct script_spec *s = script_spec__find(spec);
376
377 if (s)
378 return s;
379
380 s = script_spec__new(spec, ops);
381 if (!s)
382 goto out_delete_spec;
383
384 script_spec__add(s);
385
386 return s;
387
388out_delete_spec:
389 script_spec__delete(s);
390
391 return NULL;
392}
393
394int script_spec_register(const char *spec, struct scripting_ops *ops)
395{
396 struct script_spec *s;
397
398 s = script_spec__find(spec);
399 if (s)
400 return -1;
401
402 s = script_spec__findnew(spec, ops);
403 if (!s)
404 return -1;
405
406 return 0;
407}
408
409static struct scripting_ops *script_spec__lookup(const char *spec)
410{
411 struct script_spec *s = script_spec__find(spec);
412 if (!s)
413 return NULL;
414
415 return s->ops;
416}
417
418static void list_available_languages(void)
419{
420 struct script_spec *s;
421
422 fprintf(stderr, "\n");
423 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100424 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600425
426 list_for_each_entry(s, &script_specs, node)
427 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
428
429 fprintf(stderr, "\n");
430}
431
432static int parse_scriptname(const struct option *opt __used,
433 const char *str, int unset __used)
434{
435 char spec[PATH_MAX];
436 const char *script, *ext;
437 int len;
438
Tom Zanussif526d682010-01-27 02:27:52 -0600439 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600440 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600441 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600442 }
443
444 script = strchr(str, ':');
445 if (script) {
446 len = script - str;
447 if (len >= PATH_MAX) {
448 fprintf(stderr, "invalid language specifier");
449 return -1;
450 }
451 strncpy(spec, str, len);
452 spec[len] = '\0';
453 scripting_ops = script_spec__lookup(spec);
454 if (!scripting_ops) {
455 fprintf(stderr, "invalid language specifier");
456 return -1;
457 }
458 script++;
459 } else {
460 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100461 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600462 if (!ext) {
463 fprintf(stderr, "invalid script extension");
464 return -1;
465 }
466 scripting_ops = script_spec__lookup(++ext);
467 if (!scripting_ops) {
468 fprintf(stderr, "invalid script extension");
469 return -1;
470 }
471 }
472
473 script_name = strdup(script);
474
475 return 0;
476}
477
David Ahern745f43e2011-03-09 22:23:26 -0700478static int parse_output_fields(const struct option *opt __used,
479 const char *arg, int unset __used)
480{
481 char *tok;
482 int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
David Ahern2c9e45f2011-03-17 10:03:21 -0600483 int j;
David Ahern745f43e2011-03-09 22:23:26 -0700484 int rc = 0;
485 char *str = strdup(arg);
David Ahern1424dc92011-03-09 22:23:28 -0700486 int type = -1;
David Ahern745f43e2011-03-09 22:23:26 -0700487
488 if (!str)
489 return -ENOMEM;
490
David Ahern2c9e45f2011-03-17 10:03:21 -0600491 /* first word can state for which event type the user is specifying
492 * the fields. If no type exists, the specified fields apply to all
493 * event types found in the file minus the invalid fields for a type.
David Ahern1424dc92011-03-09 22:23:28 -0700494 */
David Ahern2c9e45f2011-03-17 10:03:21 -0600495 tok = strchr(str, ':');
496 if (tok) {
497 *tok = '\0';
498 tok++;
499 if (!strcmp(str, "hw"))
500 type = PERF_TYPE_HARDWARE;
501 else if (!strcmp(str, "sw"))
502 type = PERF_TYPE_SOFTWARE;
503 else if (!strcmp(str, "trace"))
504 type = PERF_TYPE_TRACEPOINT;
505 else {
506 fprintf(stderr, "Invalid event type in field string.\n");
507 return -EINVAL;
508 }
509
510 if (output[type].user_set)
511 pr_warning("Overriding previous field request for %s events.\n",
512 event_type(type));
513
514 output[type].fields = 0;
515 output[type].user_set = true;
516
517 } else {
518 tok = str;
519 if (strlen(str) == 0) {
520 fprintf(stderr,
521 "Cannot set fields to 'none' for all event types.\n");
522 rc = -EINVAL;
523 goto out;
524 }
525
526 if (output_set_by_user())
527 pr_warning("Overriding previous field request for all events.\n");
528
529 for (j = 0; j < PERF_TYPE_MAX; ++j) {
530 output[j].fields = 0;
531 output[j].user_set = true;
532 }
David Ahern1424dc92011-03-09 22:23:28 -0700533 }
534
David Ahern2c9e45f2011-03-17 10:03:21 -0600535 tok = strtok(tok, ",");
536 while (tok) {
David Ahern745f43e2011-03-09 22:23:26 -0700537 for (i = 0; i < imax; ++i) {
David Ahern2c9e45f2011-03-17 10:03:21 -0600538 if (strcmp(tok, all_output_options[i].str) == 0)
David Ahern745f43e2011-03-09 22:23:26 -0700539 break;
David Ahern745f43e2011-03-09 22:23:26 -0700540 }
541 if (i == imax) {
David Ahern2c9e45f2011-03-17 10:03:21 -0600542 fprintf(stderr, "Invalid field requested.\n");
David Ahern745f43e2011-03-09 22:23:26 -0700543 rc = -EINVAL;
David Ahern2c9e45f2011-03-17 10:03:21 -0600544 goto out;
545 }
546
547 if (type == -1) {
548 /* add user option to all events types for
549 * which it is valid
550 */
551 for (j = 0; j < PERF_TYPE_MAX; ++j) {
552 if (output[j].invalid_fields & all_output_options[i].field) {
553 pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
554 all_output_options[i].str, event_type(j));
555 } else
556 output[j].fields |= all_output_options[i].field;
557 }
558 } else {
559 if (output[type].invalid_fields & all_output_options[i].field) {
560 fprintf(stderr, "\'%s\' not valid for %s events.\n",
561 all_output_options[i].str, event_type(type));
562
563 rc = -EINVAL;
564 goto out;
565 }
566 output[type].fields |= all_output_options[i].field;
567 }
568
569 tok = strtok(NULL, ",");
570 }
571
572 if (type >= 0) {
573 if (output[type].fields == 0) {
574 pr_debug("No fields requested for %s type. "
575 "Events will not be displayed.\n", event_type(type));
David Ahern745f43e2011-03-09 22:23:26 -0700576 }
David Ahern1424dc92011-03-09 22:23:28 -0700577 }
David Ahern745f43e2011-03-09 22:23:26 -0700578
David Ahern2c9e45f2011-03-17 10:03:21 -0600579out:
David Ahern745f43e2011-03-09 22:23:26 -0700580 free(str);
581 return rc;
582}
583
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600584/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
585static int is_directory(const char *base_path, const struct dirent *dent)
586{
587 char path[PATH_MAX];
588 struct stat st;
589
590 sprintf(path, "%s/%s", base_path, dent->d_name);
591 if (stat(path, &st))
592 return 0;
593
594 return S_ISDIR(st.st_mode);
595}
596
597#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600598 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
599 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600600 if ((lang_dirent.d_type == DT_DIR || \
601 (lang_dirent.d_type == DT_UNKNOWN && \
602 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600603 (strcmp(lang_dirent.d_name, ".")) && \
604 (strcmp(lang_dirent.d_name, "..")))
605
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600606#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600607 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
608 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600609 if (script_dirent.d_type != DT_DIR && \
610 (script_dirent.d_type != DT_UNKNOWN || \
611 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600612
613
614#define RECORD_SUFFIX "-record"
615#define REPORT_SUFFIX "-report"
616
617struct script_desc {
618 struct list_head node;
619 char *name;
620 char *half_liner;
621 char *args;
622};
623
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200624static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600625
626static struct script_desc *script_desc__new(const char *name)
627{
628 struct script_desc *s = zalloc(sizeof(*s));
629
Tom Zanussib5b87312010-11-10 08:16:51 -0600630 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600631 s->name = strdup(name);
632
633 return s;
634}
635
636static void script_desc__delete(struct script_desc *s)
637{
638 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600639 free(s->half_liner);
640 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600641 free(s);
642}
643
644static void script_desc__add(struct script_desc *s)
645{
646 list_add_tail(&s->node, &script_descs);
647}
648
649static struct script_desc *script_desc__find(const char *name)
650{
651 struct script_desc *s;
652
653 list_for_each_entry(s, &script_descs, node)
654 if (strcasecmp(s->name, name) == 0)
655 return s;
656 return NULL;
657}
658
659static struct script_desc *script_desc__findnew(const char *name)
660{
661 struct script_desc *s = script_desc__find(name);
662
663 if (s)
664 return s;
665
666 s = script_desc__new(name);
667 if (!s)
668 goto out_delete_desc;
669
670 script_desc__add(s);
671
672 return s;
673
674out_delete_desc:
675 script_desc__delete(s);
676
677 return NULL;
678}
679
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200680static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600681{
682 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200683 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600684
685 if (strlen(str) > suffix_len) {
686 p = str + strlen(str) - suffix_len;
687 if (!strncmp(p, suffix, suffix_len))
688 return p;
689 }
690
691 return NULL;
692}
693
694static char *ltrim(char *str)
695{
696 int len = strlen(str);
697
698 while (len && isspace(*str)) {
699 len--;
700 str++;
701 }
702
703 return str;
704}
705
706static int read_script_info(struct script_desc *desc, const char *filename)
707{
708 char line[BUFSIZ], *p;
709 FILE *fp;
710
711 fp = fopen(filename, "r");
712 if (!fp)
713 return -1;
714
715 while (fgets(line, sizeof(line), fp)) {
716 p = ltrim(line);
717 if (strlen(p) == 0)
718 continue;
719 if (*p != '#')
720 continue;
721 p++;
722 if (strlen(p) && *p == '!')
723 continue;
724
725 p = ltrim(p);
726 if (strlen(p) && p[strlen(p) - 1] == '\n')
727 p[strlen(p) - 1] = '\0';
728
729 if (!strncmp(p, "description:", strlen("description:"))) {
730 p += strlen("description:");
731 desc->half_liner = strdup(ltrim(p));
732 continue;
733 }
734
735 if (!strncmp(p, "args:", strlen("args:"))) {
736 p += strlen("args:");
737 desc->args = strdup(ltrim(p));
738 continue;
739 }
740 }
741
742 fclose(fp);
743
744 return 0;
745}
746
747static int list_available_scripts(const struct option *opt __used,
748 const char *s __used, int unset __used)
749{
750 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
751 char scripts_path[MAXPATHLEN];
752 DIR *scripts_dir, *lang_dir;
753 char script_path[MAXPATHLEN];
754 char lang_path[MAXPATHLEN];
755 struct script_desc *desc;
756 char first_half[BUFSIZ];
757 char *script_root;
758 char *str;
759
760 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
761
762 scripts_dir = opendir(scripts_path);
763 if (!scripts_dir)
764 return -1;
765
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600766 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600767 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
768 lang_dirent.d_name);
769 lang_dir = opendir(lang_path);
770 if (!lang_dir)
771 continue;
772
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600773 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600774 script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200775 str = (char *)ends_with(script_root, REPORT_SUFFIX);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600776 if (str) {
777 *str = '\0';
778 desc = script_desc__findnew(script_root);
779 snprintf(script_path, MAXPATHLEN, "%s/%s",
780 lang_path, script_dirent.d_name);
781 read_script_info(desc, script_path);
782 }
783 free(script_root);
784 }
785 }
786
787 fprintf(stdout, "List of available trace scripts:\n");
788 list_for_each_entry(desc, &script_descs, node) {
789 sprintf(first_half, "%s %s", desc->name,
790 desc->args ? desc->args : "");
791 fprintf(stdout, " %-36s %s\n", first_half,
792 desc->half_liner ? desc->half_liner : "");
793 }
794
795 exit(0);
796}
797
Tom Zanussi38752942009-12-15 02:53:39 -0600798static char *get_script_path(const char *script_root, const char *suffix)
799{
800 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
801 char scripts_path[MAXPATHLEN];
802 char script_path[MAXPATHLEN];
803 DIR *scripts_dir, *lang_dir;
804 char lang_path[MAXPATHLEN];
805 char *str, *__script_root;
806 char *path = NULL;
807
808 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
809
810 scripts_dir = opendir(scripts_path);
811 if (!scripts_dir)
812 return NULL;
813
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600814 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -0600815 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
816 lang_dirent.d_name);
817 lang_dir = opendir(lang_path);
818 if (!lang_dir)
819 continue;
820
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600821 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi38752942009-12-15 02:53:39 -0600822 __script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200823 str = (char *)ends_with(__script_root, suffix);
Tom Zanussi38752942009-12-15 02:53:39 -0600824 if (str) {
825 *str = '\0';
826 if (strcmp(__script_root, script_root))
827 continue;
828 snprintf(script_path, MAXPATHLEN, "%s/%s",
829 lang_path, script_dirent.d_name);
830 path = strdup(script_path);
831 free(__script_root);
832 break;
833 }
834 free(__script_root);
835 }
836 }
837
838 return path;
839}
840
Tom Zanussib5b87312010-11-10 08:16:51 -0600841static bool is_top_script(const char *script_path)
842{
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200843 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -0600844}
845
846static int has_required_arg(char *script_path)
847{
848 struct script_desc *desc;
849 int n_args = 0;
850 char *p;
851
852 desc = script_desc__new(NULL);
853
854 if (read_script_info(desc, script_path))
855 goto out;
856
857 if (!desc->args)
858 goto out;
859
860 for (p = desc->args; *p; p++)
861 if (*p == '<')
862 n_args++;
863out:
864 script_desc__delete(desc);
865
866 return n_args;
867}
868
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100869static const char * const script_usage[] = {
870 "perf script [<options>]",
871 "perf script [<options>] record <script> [<record-options>] <command>",
872 "perf script [<options>] report <script> [script-args]",
873 "perf script [<options>] <script> [<record-options>] <command>",
874 "perf script [<options>] <top-script> [script-args]",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200875 NULL
876};
877
878static const struct option options[] = {
879 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
880 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000881 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200882 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600883 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400884 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600885 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
886 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600887 OPT_CALLBACK('s', "script", NULL, "name",
888 "script file name (lang:script name, script name, or *)",
889 parse_scriptname),
890 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100891 "generate perf-script.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900892 OPT_STRING('i', "input", &input_name, "file",
893 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200894 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
895 "do various checks like samples ordering and lost events"),
David Ahernc0230b22011-03-09 22:23:27 -0700896 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
897 "file", "vmlinux pathname"),
898 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
899 "file", "kallsyms pathname"),
900 OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
901 "When printing symbols do not display call chain"),
902 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
903 "Look for files with symbols relative to this directory"),
David Ahern745f43e2011-03-09 22:23:26 -0700904 OPT_CALLBACK('f', "fields", NULL, "str",
David Ahern1424dc92011-03-09 22:23:28 -0700905 "comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace. Fields: comm,tid,pid,time,cpu,event,trace,sym",
David Ahern745f43e2011-03-09 22:23:26 -0700906 parse_output_fields),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600907
Masami Hiramatsu19096292009-08-21 14:56:03 -0400908 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200909};
910
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600911static bool have_cmd(int argc, const char **argv)
912{
913 char **__argv = malloc(sizeof(const char *) * argc);
914
915 if (!__argv)
916 die("malloc");
917 memcpy(__argv, argv, sizeof(const char *) * argc);
918 argc = parse_options(argc, (const char **)__argv, record_options,
919 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
920 free(__argv);
921
922 return argc != 0;
923}
924
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100925int cmd_script(int argc, const char **argv, const char *prefix __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200926{
Tom Zanussib5b87312010-11-10 08:16:51 -0600927 char *rec_script_path = NULL;
928 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200929 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -0600930 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -0600931 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -0600932 bool system_wide;
933 int i, j, err;
Tom Zanussi38752942009-12-15 02:53:39 -0600934
Tom Zanussib5b87312010-11-10 08:16:51 -0600935 setup_scripting();
936
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100937 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -0600938 PARSE_OPT_STOP_AT_NON_OPTION);
939
940 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
941 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
942 if (!rec_script_path)
943 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -0600944 }
945
Tom Zanussib5b87312010-11-10 08:16:51 -0600946 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
947 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
948 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -0600949 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -0600950 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100951 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -0600952 return -1;
953 }
Tom Zanussi38752942009-12-15 02:53:39 -0600954 }
955
Ben Hutchings44e668c2010-10-10 16:10:03 +0100956 /* make sure PERF_EXEC_PATH is set for scripts */
957 perf_set_argv_exec_path(perf_exec_path());
958
Tom Zanussib5b87312010-11-10 08:16:51 -0600959 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -0500960 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -0600961 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -0500962 pid_t pid;
963
Tom Zanussib5b87312010-11-10 08:16:51 -0600964 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
965 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
966
967 if (!rec_script_path && !rep_script_path) {
968 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100969 " script -l for available scripts.\n", argv[0]);
970 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -0500971 }
972
Tom Zanussib5b87312010-11-10 08:16:51 -0600973 if (is_top_script(argv[0])) {
974 rep_args = argc - 1;
975 } else {
976 int rec_args;
977
978 rep_args = has_required_arg(rep_script_path);
979 rec_args = (argc - 1) - rep_args;
980 if (rec_args < 0) {
981 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100982 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -0600983 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100984 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -0600985 }
Tom Zanussia0cccc22010-04-01 23:59:25 -0500986 }
987
988 if (pipe(live_pipe) < 0) {
989 perror("failed to create pipe");
990 exit(-1);
991 }
992
993 pid = fork();
994 if (pid < 0) {
995 perror("failed to fork");
996 exit(-1);
997 }
998
999 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -06001000 system_wide = true;
1001 j = 0;
1002
Tom Zanussia0cccc22010-04-01 23:59:25 -05001003 dup2(live_pipe[1], 1);
1004 close(live_pipe[0]);
1005
Tom Zanussib5b87312010-11-10 08:16:51 -06001006 if (!is_top_script(argv[0]))
1007 system_wide = !have_cmd(argc - rep_args,
1008 &argv[rep_args]);
1009
1010 __argv = malloc((argc + 6) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001011 if (!__argv)
1012 die("malloc");
1013
Tom Zanussib5b87312010-11-10 08:16:51 -06001014 __argv[j++] = "/bin/sh";
1015 __argv[j++] = rec_script_path;
1016 if (system_wide)
1017 __argv[j++] = "-a";
1018 __argv[j++] = "-q";
1019 __argv[j++] = "-o";
1020 __argv[j++] = "-";
1021 for (i = rep_args + 1; i < argc; i++)
1022 __argv[j++] = argv[i];
1023 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -05001024
1025 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001026 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -05001027 exit(-1);
1028 }
1029
1030 dup2(live_pipe[0], 0);
1031 close(live_pipe[1]);
1032
Tom Zanussib5b87312010-11-10 08:16:51 -06001033 __argv = malloc((argc + 4) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -06001034 if (!__argv)
1035 die("malloc");
Tom Zanussib5b87312010-11-10 08:16:51 -06001036 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001037 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -06001038 __argv[j++] = rep_script_path;
1039 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001040 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -06001041 __argv[j++] = "-i";
1042 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -06001043 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -06001044
1045 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -06001046 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -06001047 exit(-1);
1048 }
Tom Zanussi956ffd02009-11-25 01:15:46 -06001049
Tom Zanussib5b87312010-11-10 08:16:51 -06001050 if (rec_script_path)
1051 script_path = rec_script_path;
1052 if (rep_script_path)
1053 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001054
Tom Zanussib5b87312010-11-10 08:16:51 -06001055 if (script_path) {
1056 system_wide = false;
1057 j = 0;
1058
1059 if (rec_script_path)
1060 system_wide = !have_cmd(argc - 1, &argv[1]);
1061
1062 __argv = malloc((argc + 2) * sizeof(const char *));
1063 if (!__argv)
1064 die("malloc");
1065 __argv[j++] = "/bin/sh";
1066 __argv[j++] = script_path;
1067 if (system_wide)
1068 __argv[j++] = "-a";
1069 for (i = 2; i < argc; i++)
1070 __argv[j++] = argv[i];
1071 __argv[j++] = NULL;
1072
1073 execvp("/bin/sh", (char **)__argv);
1074 free(__argv);
1075 exit(-1);
1076 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001077
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001078 if (symbol__init() < 0)
1079 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -06001080 if (!script_name)
1081 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001082
Ian Munsie21ef97f2010-12-10 14:09:16 +11001083 session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001084 if (session == NULL)
1085 return -ENOMEM;
1086
David Ahern1424dc92011-03-09 22:23:28 -07001087 if (!no_callchain)
David Ahernc0230b22011-03-09 22:23:27 -07001088 symbol_conf.use_callchain = true;
1089 else
1090 symbol_conf.use_callchain = false;
1091
Tom Zanussi956ffd02009-11-25 01:15:46 -06001092 if (generate_script_lang) {
1093 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -07001094 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -06001095
David Ahern2c9e45f2011-03-17 10:03:21 -06001096 if (output_set_by_user()) {
David Ahern745f43e2011-03-09 22:23:26 -07001097 fprintf(stderr,
1098 "custom fields not supported for generated scripts");
1099 return -1;
1100 }
1101
1102 input = open(input_name, O_RDONLY);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001103 if (input < 0) {
1104 perror("failed to open file");
1105 exit(-1);
1106 }
1107
1108 err = fstat(input, &perf_stat);
1109 if (err < 0) {
1110 perror("failed to stat file");
1111 exit(-1);
1112 }
1113
1114 if (!perf_stat.st_size) {
1115 fprintf(stderr, "zero-sized file, nothing to do!\n");
1116 exit(0);
1117 }
1118
1119 scripting_ops = script_spec__lookup(generate_script_lang);
1120 if (!scripting_ops) {
1121 fprintf(stderr, "invalid language specifier");
1122 return -1;
1123 }
1124
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001125 err = scripting_ops->generate_script("perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -06001126 goto out;
1127 }
1128
1129 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -06001130 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001131 if (err)
1132 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001133 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001134 }
1135
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001136 err = __cmd_script(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001137
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -02001138 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -06001139 cleanup_scripting();
1140out:
1141 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001142}