blob: c046e6e8600a61083b58b91b38395630c7b1bd67 [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"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020015
Tom Zanussi956ffd02009-11-25 01:15:46 -060016static char const *script_name;
17static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020018static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020019static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020020static u64 nr_unordered;
Tom Zanussi34c86ea2010-11-10 08:15:43 -060021extern const struct option record_options[];
Tom Zanussi956ffd02009-11-25 01:15:46 -060022
David Ahern745f43e2011-03-09 22:23:26 -070023enum perf_output_field {
24 PERF_OUTPUT_COMM = 1U << 0,
25 PERF_OUTPUT_TID = 1U << 1,
26 PERF_OUTPUT_PID = 1U << 2,
27 PERF_OUTPUT_TIME = 1U << 3,
28 PERF_OUTPUT_CPU = 1U << 4,
29 PERF_OUTPUT_EVNAME = 1U << 5,
30 PERF_OUTPUT_TRACE = 1U << 6,
31};
32
33struct output_option {
34 const char *str;
35 enum perf_output_field field;
36} all_output_options[] = {
37 {.str = "comm", .field = PERF_OUTPUT_COMM},
38 {.str = "tid", .field = PERF_OUTPUT_TID},
39 {.str = "pid", .field = PERF_OUTPUT_PID},
40 {.str = "time", .field = PERF_OUTPUT_TIME},
41 {.str = "cpu", .field = PERF_OUTPUT_CPU},
42 {.str = "event", .field = PERF_OUTPUT_EVNAME},
43 {.str = "trace", .field = PERF_OUTPUT_TRACE},
44};
45
46/* default set to maintain compatibility with current format */
47static u64 output_fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | \
48 PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | \
49 PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE;
50
51static bool output_set_by_user;
52
53#define PRINT_FIELD(x) (output_fields & PERF_OUTPUT_##x)
54
David Ahernc70c94b2011-03-09 22:23:25 -070055static void print_sample_start(struct perf_sample *sample,
56 struct thread *thread)
57{
58 int type;
59 struct event *event;
60 const char *evname = NULL;
61 unsigned long secs;
62 unsigned long usecs;
David Ahern745f43e2011-03-09 22:23:26 -070063 unsigned long long nsecs;
David Ahernc70c94b2011-03-09 22:23:25 -070064
David Ahern745f43e2011-03-09 22:23:26 -070065 if (PRINT_FIELD(COMM)) {
66 if (latency_format)
67 printf("%8.8s ", thread->comm);
68 else
69 printf("%16s ", thread->comm);
70 }
David Ahernc70c94b2011-03-09 22:23:25 -070071
David Ahern745f43e2011-03-09 22:23:26 -070072 if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
73 printf("%5d/%-5d ", sample->pid, sample->tid);
74 else if (PRINT_FIELD(PID))
75 printf("%5d ", sample->pid);
76 else if (PRINT_FIELD(TID))
77 printf("%5d ", sample->tid);
David Ahernc70c94b2011-03-09 22:23:25 -070078
David Ahern745f43e2011-03-09 22:23:26 -070079 if (PRINT_FIELD(CPU)) {
80 if (latency_format)
81 printf("%3d ", sample->cpu);
82 else
83 printf("[%03d] ", sample->cpu);
84 }
David Ahernc70c94b2011-03-09 22:23:25 -070085
David Ahern745f43e2011-03-09 22:23:26 -070086 if (PRINT_FIELD(TIME)) {
87 nsecs = sample->time;
88 secs = nsecs / NSECS_PER_SEC;
89 nsecs -= secs * NSECS_PER_SEC;
90 usecs = nsecs / NSECS_PER_USEC;
91 printf("%5lu.%06lu: ", secs, usecs);
92 }
93
94 if (PRINT_FIELD(EVNAME)) {
95 type = trace_parse_common_type(sample->raw_data);
96 event = trace_find_event(type);
97 if (event)
98 evname = event->name;
99
100 printf("%s: ", evname ? evname : "(unknown)");
101 }
David Ahernc70c94b2011-03-09 22:23:25 -0700102}
103
David Ahernbe6d8422011-03-09 22:23:23 -0700104static void process_event(union perf_event *event __unused,
105 struct perf_sample *sample,
106 struct perf_session *session __unused,
107 struct thread *thread)
108{
David Ahernc70c94b2011-03-09 22:23:25 -0700109 print_sample_start(sample, thread);
David Ahern745f43e2011-03-09 22:23:26 -0700110
111 if (PRINT_FIELD(TRACE))
112 print_trace_event(sample->cpu, sample->raw_data,
113 sample->raw_size);
114
David Ahernc70c94b2011-03-09 22:23:25 -0700115 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -0700116}
117
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600118static int default_start_script(const char *script __unused,
119 int argc __unused,
120 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600121{
122 return 0;
123}
124
125static int default_stop_script(void)
126{
127 return 0;
128}
129
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600130static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -0600131{
132 return 0;
133}
134
135static struct scripting_ops default_scripting_ops = {
136 .start_script = default_start_script,
137 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -0700138 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -0600139 .generate_script = default_generate_script,
140};
141
142static struct scripting_ops *scripting_ops;
143
144static void setup_scripting(void)
145{
Tom Zanussi16c632d2009-11-25 01:15:48 -0600146 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600147 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -0600148
Tom Zanussi956ffd02009-11-25 01:15:46 -0600149 scripting_ops = &default_scripting_ops;
150}
151
152static int cleanup_scripting(void)
153{
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100154 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500155
Tom Zanussi956ffd02009-11-25 01:15:46 -0600156 return scripting_ops->stop_script();
157}
158
Tom Zanussi956ffd02009-11-25 01:15:46 -0600159static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200160
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200161static int process_sample_event(union perf_event *event,
162 struct perf_sample *sample,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200163 struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200164{
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200165 struct thread *thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200166
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200167 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200168 pr_debug("problem processing %d event, skipping it.\n",
169 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200170 return -1;
171 }
172
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -0200173 if (session->sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200174 if (debug_mode) {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200175 if (sample->time < last_timestamp) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200176 pr_err("Samples misordered, previous: %" PRIu64
177 " this: %" PRIu64 "\n", last_timestamp,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200178 sample->time);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200179 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200180 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200181 last_timestamp = sample->time;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200182 return 0;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200183 }
David Ahernbe6d8422011-03-09 22:23:23 -0700184 scripting_ops->process_event(event, sample, session, thread);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200185 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200186
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200187 session->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200188 return 0;
189}
190
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200191static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200192 .sample = process_sample_event,
193 .comm = perf_event__process_comm,
194 .attr = perf_event__process_attr,
195 .event_type = perf_event__process_event_type,
196 .tracing_data = perf_event__process_tracing_data,
197 .build_id = perf_event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200198 .ordered_samples = true,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200199 .ordering_requires_timestamps = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200200};
201
Tom Zanussic239da32010-04-01 23:59:18 -0500202extern volatile int session_done;
203
204static void sig_handler(int sig __unused)
205{
206 session_done = 1;
207}
208
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100209static int __cmd_script(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200210{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200211 int ret;
212
Tom Zanussic239da32010-04-01 23:59:18 -0500213 signal(SIGINT, sig_handler);
214
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200215 ret = perf_session__process_events(session, &event_ops);
216
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200217 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200218 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200219
220 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200221}
222
Tom Zanussi956ffd02009-11-25 01:15:46 -0600223struct script_spec {
224 struct list_head node;
225 struct scripting_ops *ops;
226 char spec[0];
227};
228
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200229static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600230
231static struct script_spec *script_spec__new(const char *spec,
232 struct scripting_ops *ops)
233{
234 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
235
236 if (s != NULL) {
237 strcpy(s->spec, spec);
238 s->ops = ops;
239 }
240
241 return s;
242}
243
244static void script_spec__delete(struct script_spec *s)
245{
246 free(s->spec);
247 free(s);
248}
249
250static void script_spec__add(struct script_spec *s)
251{
252 list_add_tail(&s->node, &script_specs);
253}
254
255static struct script_spec *script_spec__find(const char *spec)
256{
257 struct script_spec *s;
258
259 list_for_each_entry(s, &script_specs, node)
260 if (strcasecmp(s->spec, spec) == 0)
261 return s;
262 return NULL;
263}
264
265static struct script_spec *script_spec__findnew(const char *spec,
266 struct scripting_ops *ops)
267{
268 struct script_spec *s = script_spec__find(spec);
269
270 if (s)
271 return s;
272
273 s = script_spec__new(spec, ops);
274 if (!s)
275 goto out_delete_spec;
276
277 script_spec__add(s);
278
279 return s;
280
281out_delete_spec:
282 script_spec__delete(s);
283
284 return NULL;
285}
286
287int script_spec_register(const char *spec, struct scripting_ops *ops)
288{
289 struct script_spec *s;
290
291 s = script_spec__find(spec);
292 if (s)
293 return -1;
294
295 s = script_spec__findnew(spec, ops);
296 if (!s)
297 return -1;
298
299 return 0;
300}
301
302static struct scripting_ops *script_spec__lookup(const char *spec)
303{
304 struct script_spec *s = script_spec__find(spec);
305 if (!s)
306 return NULL;
307
308 return s->ops;
309}
310
311static void list_available_languages(void)
312{
313 struct script_spec *s;
314
315 fprintf(stderr, "\n");
316 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100317 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600318
319 list_for_each_entry(s, &script_specs, node)
320 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
321
322 fprintf(stderr, "\n");
323}
324
325static int parse_scriptname(const struct option *opt __used,
326 const char *str, int unset __used)
327{
328 char spec[PATH_MAX];
329 const char *script, *ext;
330 int len;
331
Tom Zanussif526d682010-01-27 02:27:52 -0600332 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600333 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600334 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600335 }
336
337 script = strchr(str, ':');
338 if (script) {
339 len = script - str;
340 if (len >= PATH_MAX) {
341 fprintf(stderr, "invalid language specifier");
342 return -1;
343 }
344 strncpy(spec, str, len);
345 spec[len] = '\0';
346 scripting_ops = script_spec__lookup(spec);
347 if (!scripting_ops) {
348 fprintf(stderr, "invalid language specifier");
349 return -1;
350 }
351 script++;
352 } else {
353 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100354 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600355 if (!ext) {
356 fprintf(stderr, "invalid script extension");
357 return -1;
358 }
359 scripting_ops = script_spec__lookup(++ext);
360 if (!scripting_ops) {
361 fprintf(stderr, "invalid script extension");
362 return -1;
363 }
364 }
365
366 script_name = strdup(script);
367
368 return 0;
369}
370
David Ahern745f43e2011-03-09 22:23:26 -0700371static int parse_output_fields(const struct option *opt __used,
372 const char *arg, int unset __used)
373{
374 char *tok;
375 int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
376 int rc = 0;
377 char *str = strdup(arg);
378
379 if (!str)
380 return -ENOMEM;
381
382 tok = strtok(str, ",");
383 if (!tok) {
384 fprintf(stderr, "Invalid field string.");
385 return -EINVAL;
386 }
387
388 output_fields = 0;
389 while (1) {
390 for (i = 0; i < imax; ++i) {
391 if (strcmp(tok, all_output_options[i].str) == 0) {
392 output_fields |= all_output_options[i].field;
393 break;
394 }
395 }
396 if (i == imax) {
397 fprintf(stderr, "Invalid field requested.");
398 rc = -EINVAL;
399 break;
400 }
401
402 tok = strtok(NULL, ",");
403 if (!tok)
404 break;
405 }
406
407 output_set_by_user = true;
408
409 free(str);
410 return rc;
411}
412
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600413/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
414static int is_directory(const char *base_path, const struct dirent *dent)
415{
416 char path[PATH_MAX];
417 struct stat st;
418
419 sprintf(path, "%s/%s", base_path, dent->d_name);
420 if (stat(path, &st))
421 return 0;
422
423 return S_ISDIR(st.st_mode);
424}
425
426#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600427 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
428 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600429 if ((lang_dirent.d_type == DT_DIR || \
430 (lang_dirent.d_type == DT_UNKNOWN && \
431 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600432 (strcmp(lang_dirent.d_name, ".")) && \
433 (strcmp(lang_dirent.d_name, "..")))
434
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600435#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600436 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
437 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600438 if (script_dirent.d_type != DT_DIR && \
439 (script_dirent.d_type != DT_UNKNOWN || \
440 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600441
442
443#define RECORD_SUFFIX "-record"
444#define REPORT_SUFFIX "-report"
445
446struct script_desc {
447 struct list_head node;
448 char *name;
449 char *half_liner;
450 char *args;
451};
452
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200453static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600454
455static struct script_desc *script_desc__new(const char *name)
456{
457 struct script_desc *s = zalloc(sizeof(*s));
458
Tom Zanussib5b87312010-11-10 08:16:51 -0600459 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600460 s->name = strdup(name);
461
462 return s;
463}
464
465static void script_desc__delete(struct script_desc *s)
466{
467 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600468 free(s->half_liner);
469 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600470 free(s);
471}
472
473static void script_desc__add(struct script_desc *s)
474{
475 list_add_tail(&s->node, &script_descs);
476}
477
478static struct script_desc *script_desc__find(const char *name)
479{
480 struct script_desc *s;
481
482 list_for_each_entry(s, &script_descs, node)
483 if (strcasecmp(s->name, name) == 0)
484 return s;
485 return NULL;
486}
487
488static struct script_desc *script_desc__findnew(const char *name)
489{
490 struct script_desc *s = script_desc__find(name);
491
492 if (s)
493 return s;
494
495 s = script_desc__new(name);
496 if (!s)
497 goto out_delete_desc;
498
499 script_desc__add(s);
500
501 return s;
502
503out_delete_desc:
504 script_desc__delete(s);
505
506 return NULL;
507}
508
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200509static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600510{
511 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200512 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600513
514 if (strlen(str) > suffix_len) {
515 p = str + strlen(str) - suffix_len;
516 if (!strncmp(p, suffix, suffix_len))
517 return p;
518 }
519
520 return NULL;
521}
522
523static char *ltrim(char *str)
524{
525 int len = strlen(str);
526
527 while (len && isspace(*str)) {
528 len--;
529 str++;
530 }
531
532 return str;
533}
534
535static int read_script_info(struct script_desc *desc, const char *filename)
536{
537 char line[BUFSIZ], *p;
538 FILE *fp;
539
540 fp = fopen(filename, "r");
541 if (!fp)
542 return -1;
543
544 while (fgets(line, sizeof(line), fp)) {
545 p = ltrim(line);
546 if (strlen(p) == 0)
547 continue;
548 if (*p != '#')
549 continue;
550 p++;
551 if (strlen(p) && *p == '!')
552 continue;
553
554 p = ltrim(p);
555 if (strlen(p) && p[strlen(p) - 1] == '\n')
556 p[strlen(p) - 1] = '\0';
557
558 if (!strncmp(p, "description:", strlen("description:"))) {
559 p += strlen("description:");
560 desc->half_liner = strdup(ltrim(p));
561 continue;
562 }
563
564 if (!strncmp(p, "args:", strlen("args:"))) {
565 p += strlen("args:");
566 desc->args = strdup(ltrim(p));
567 continue;
568 }
569 }
570
571 fclose(fp);
572
573 return 0;
574}
575
576static int list_available_scripts(const struct option *opt __used,
577 const char *s __used, int unset __used)
578{
579 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
580 char scripts_path[MAXPATHLEN];
581 DIR *scripts_dir, *lang_dir;
582 char script_path[MAXPATHLEN];
583 char lang_path[MAXPATHLEN];
584 struct script_desc *desc;
585 char first_half[BUFSIZ];
586 char *script_root;
587 char *str;
588
589 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
590
591 scripts_dir = opendir(scripts_path);
592 if (!scripts_dir)
593 return -1;
594
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600595 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600596 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
597 lang_dirent.d_name);
598 lang_dir = opendir(lang_path);
599 if (!lang_dir)
600 continue;
601
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600602 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600603 script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200604 str = (char *)ends_with(script_root, REPORT_SUFFIX);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600605 if (str) {
606 *str = '\0';
607 desc = script_desc__findnew(script_root);
608 snprintf(script_path, MAXPATHLEN, "%s/%s",
609 lang_path, script_dirent.d_name);
610 read_script_info(desc, script_path);
611 }
612 free(script_root);
613 }
614 }
615
616 fprintf(stdout, "List of available trace scripts:\n");
617 list_for_each_entry(desc, &script_descs, node) {
618 sprintf(first_half, "%s %s", desc->name,
619 desc->args ? desc->args : "");
620 fprintf(stdout, " %-36s %s\n", first_half,
621 desc->half_liner ? desc->half_liner : "");
622 }
623
624 exit(0);
625}
626
Tom Zanussi38752942009-12-15 02:53:39 -0600627static char *get_script_path(const char *script_root, const char *suffix)
628{
629 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
630 char scripts_path[MAXPATHLEN];
631 char script_path[MAXPATHLEN];
632 DIR *scripts_dir, *lang_dir;
633 char lang_path[MAXPATHLEN];
634 char *str, *__script_root;
635 char *path = NULL;
636
637 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
638
639 scripts_dir = opendir(scripts_path);
640 if (!scripts_dir)
641 return NULL;
642
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600643 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -0600644 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
645 lang_dirent.d_name);
646 lang_dir = opendir(lang_path);
647 if (!lang_dir)
648 continue;
649
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600650 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi38752942009-12-15 02:53:39 -0600651 __script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200652 str = (char *)ends_with(__script_root, suffix);
Tom Zanussi38752942009-12-15 02:53:39 -0600653 if (str) {
654 *str = '\0';
655 if (strcmp(__script_root, script_root))
656 continue;
657 snprintf(script_path, MAXPATHLEN, "%s/%s",
658 lang_path, script_dirent.d_name);
659 path = strdup(script_path);
660 free(__script_root);
661 break;
662 }
663 free(__script_root);
664 }
665 }
666
667 return path;
668}
669
Tom Zanussib5b87312010-11-10 08:16:51 -0600670static bool is_top_script(const char *script_path)
671{
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200672 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -0600673}
674
675static int has_required_arg(char *script_path)
676{
677 struct script_desc *desc;
678 int n_args = 0;
679 char *p;
680
681 desc = script_desc__new(NULL);
682
683 if (read_script_info(desc, script_path))
684 goto out;
685
686 if (!desc->args)
687 goto out;
688
689 for (p = desc->args; *p; p++)
690 if (*p == '<')
691 n_args++;
692out:
693 script_desc__delete(desc);
694
695 return n_args;
696}
697
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100698static const char * const script_usage[] = {
699 "perf script [<options>]",
700 "perf script [<options>] record <script> [<record-options>] <command>",
701 "perf script [<options>] report <script> [script-args]",
702 "perf script [<options>] <script> [<record-options>] <command>",
703 "perf script [<options>] <top-script> [script-args]",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200704 NULL
705};
706
707static const struct option options[] = {
708 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
709 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000710 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200711 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600712 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400713 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600714 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
715 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600716 OPT_CALLBACK('s', "script", NULL, "name",
717 "script file name (lang:script name, script name, or *)",
718 parse_scriptname),
719 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100720 "generate perf-script.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900721 OPT_STRING('i', "input", &input_name, "file",
722 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200723 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
724 "do various checks like samples ordering and lost events"),
David Ahern745f43e2011-03-09 22:23:26 -0700725 OPT_CALLBACK('f', "fields", NULL, "str",
726 "comma separated output fields. Options: comm,tid,pid,time,cpu,event,trace",
727 parse_output_fields),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600728
Masami Hiramatsu19096292009-08-21 14:56:03 -0400729 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200730};
731
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600732static bool have_cmd(int argc, const char **argv)
733{
734 char **__argv = malloc(sizeof(const char *) * argc);
735
736 if (!__argv)
737 die("malloc");
738 memcpy(__argv, argv, sizeof(const char *) * argc);
739 argc = parse_options(argc, (const char **)__argv, record_options,
740 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
741 free(__argv);
742
743 return argc != 0;
744}
745
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100746int cmd_script(int argc, const char **argv, const char *prefix __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200747{
Tom Zanussib5b87312010-11-10 08:16:51 -0600748 char *rec_script_path = NULL;
749 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200750 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -0600751 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -0600752 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -0600753 bool system_wide;
754 int i, j, err;
Tom Zanussi38752942009-12-15 02:53:39 -0600755
Tom Zanussib5b87312010-11-10 08:16:51 -0600756 setup_scripting();
757
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100758 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -0600759 PARSE_OPT_STOP_AT_NON_OPTION);
760
761 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
762 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
763 if (!rec_script_path)
764 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -0600765 }
766
Tom Zanussib5b87312010-11-10 08:16:51 -0600767 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
768 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
769 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -0600770 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -0600771 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100772 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -0600773 return -1;
774 }
Tom Zanussi38752942009-12-15 02:53:39 -0600775 }
776
Ben Hutchings44e668c2010-10-10 16:10:03 +0100777 /* make sure PERF_EXEC_PATH is set for scripts */
778 perf_set_argv_exec_path(perf_exec_path());
779
Tom Zanussib5b87312010-11-10 08:16:51 -0600780 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -0500781 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -0600782 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -0500783 pid_t pid;
784
Tom Zanussib5b87312010-11-10 08:16:51 -0600785 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
786 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
787
788 if (!rec_script_path && !rep_script_path) {
789 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100790 " script -l for available scripts.\n", argv[0]);
791 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -0500792 }
793
Tom Zanussib5b87312010-11-10 08:16:51 -0600794 if (is_top_script(argv[0])) {
795 rep_args = argc - 1;
796 } else {
797 int rec_args;
798
799 rep_args = has_required_arg(rep_script_path);
800 rec_args = (argc - 1) - rep_args;
801 if (rec_args < 0) {
802 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100803 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -0600804 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100805 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -0600806 }
Tom Zanussia0cccc22010-04-01 23:59:25 -0500807 }
808
809 if (pipe(live_pipe) < 0) {
810 perror("failed to create pipe");
811 exit(-1);
812 }
813
814 pid = fork();
815 if (pid < 0) {
816 perror("failed to fork");
817 exit(-1);
818 }
819
820 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -0600821 system_wide = true;
822 j = 0;
823
Tom Zanussia0cccc22010-04-01 23:59:25 -0500824 dup2(live_pipe[1], 1);
825 close(live_pipe[0]);
826
Tom Zanussib5b87312010-11-10 08:16:51 -0600827 if (!is_top_script(argv[0]))
828 system_wide = !have_cmd(argc - rep_args,
829 &argv[rep_args]);
830
831 __argv = malloc((argc + 6) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -0600832 if (!__argv)
833 die("malloc");
834
Tom Zanussib5b87312010-11-10 08:16:51 -0600835 __argv[j++] = "/bin/sh";
836 __argv[j++] = rec_script_path;
837 if (system_wide)
838 __argv[j++] = "-a";
839 __argv[j++] = "-q";
840 __argv[j++] = "-o";
841 __argv[j++] = "-";
842 for (i = rep_args + 1; i < argc; i++)
843 __argv[j++] = argv[i];
844 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -0500845
846 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600847 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -0500848 exit(-1);
849 }
850
851 dup2(live_pipe[0], 0);
852 close(live_pipe[1]);
853
Tom Zanussib5b87312010-11-10 08:16:51 -0600854 __argv = malloc((argc + 4) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -0600855 if (!__argv)
856 die("malloc");
Tom Zanussib5b87312010-11-10 08:16:51 -0600857 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600858 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -0600859 __argv[j++] = rep_script_path;
860 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600861 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -0600862 __argv[j++] = "-i";
863 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600864 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -0600865
866 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600867 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -0600868 exit(-1);
869 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600870
Tom Zanussib5b87312010-11-10 08:16:51 -0600871 if (rec_script_path)
872 script_path = rec_script_path;
873 if (rep_script_path)
874 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600875
Tom Zanussib5b87312010-11-10 08:16:51 -0600876 if (script_path) {
877 system_wide = false;
878 j = 0;
879
880 if (rec_script_path)
881 system_wide = !have_cmd(argc - 1, &argv[1]);
882
883 __argv = malloc((argc + 2) * sizeof(const char *));
884 if (!__argv)
885 die("malloc");
886 __argv[j++] = "/bin/sh";
887 __argv[j++] = script_path;
888 if (system_wide)
889 __argv[j++] = "-a";
890 for (i = 2; i < argc; i++)
891 __argv[j++] = argv[i];
892 __argv[j++] = NULL;
893
894 execvp("/bin/sh", (char **)__argv);
895 free(__argv);
896 exit(-1);
897 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200898
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200899 if (symbol__init() < 0)
900 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600901 if (!script_name)
902 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200903
Ian Munsie21ef97f2010-12-10 14:09:16 +1100904 session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200905 if (session == NULL)
906 return -ENOMEM;
907
Tom Zanussic239da32010-04-01 23:59:18 -0500908 if (strcmp(input_name, "-") &&
909 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200910 return -EINVAL;
911
Tom Zanussi956ffd02009-11-25 01:15:46 -0600912 if (generate_script_lang) {
913 struct stat perf_stat;
David Ahern745f43e2011-03-09 22:23:26 -0700914 int input;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600915
David Ahern745f43e2011-03-09 22:23:26 -0700916 if (output_set_by_user) {
917 fprintf(stderr,
918 "custom fields not supported for generated scripts");
919 return -1;
920 }
921
922 input = open(input_name, O_RDONLY);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600923 if (input < 0) {
924 perror("failed to open file");
925 exit(-1);
926 }
927
928 err = fstat(input, &perf_stat);
929 if (err < 0) {
930 perror("failed to stat file");
931 exit(-1);
932 }
933
934 if (!perf_stat.st_size) {
935 fprintf(stderr, "zero-sized file, nothing to do!\n");
936 exit(0);
937 }
938
939 scripting_ops = script_spec__lookup(generate_script_lang);
940 if (!scripting_ops) {
941 fprintf(stderr, "invalid language specifier");
942 return -1;
943 }
944
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100945 err = scripting_ops->generate_script("perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600946 goto out;
947 }
948
949 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600950 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600951 if (err)
952 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100953 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600954 }
955
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100956 err = __cmd_script(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600957
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200958 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600959 cleanup_scripting();
960out:
961 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200962}