blob: 0a79da21df23488f111ed6a2c875aae4df6478a1 [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 Ahernc70c94b2011-03-09 22:23:25 -070023static void print_sample_start(struct perf_sample *sample,
24 struct thread *thread)
25{
26 int type;
27 struct event *event;
28 const char *evname = NULL;
29 unsigned long secs;
30 unsigned long usecs;
31 unsigned long long nsecs = sample->time;
32
33 if (latency_format)
34 printf("%8.8s-%-5d %3d", thread->comm, sample->tid, sample->cpu);
35 else
36 printf("%16s-%-5d [%03d]", thread->comm, sample->tid, sample->cpu);
37
38 secs = nsecs / NSECS_PER_SEC;
39 nsecs -= secs * NSECS_PER_SEC;
40 usecs = nsecs / NSECS_PER_USEC;
41 printf(" %5lu.%06lu: ", secs, usecs);
42
43 type = trace_parse_common_type(sample->raw_data);
44 event = trace_find_event(type);
45 if (event)
46 evname = event->name;
47
48 printf("%s: ", evname ? evname : "(unknown)");
49}
50
David Ahernbe6d8422011-03-09 22:23:23 -070051static void process_event(union perf_event *event __unused,
52 struct perf_sample *sample,
53 struct perf_session *session __unused,
54 struct thread *thread)
55{
David Ahernc70c94b2011-03-09 22:23:25 -070056 print_sample_start(sample, thread);
57 print_trace_event(sample->cpu, sample->raw_data, sample->raw_size);
58 printf("\n");
David Ahernbe6d8422011-03-09 22:23:23 -070059}
60
Tom Zanussi586bc5c2009-12-15 02:53:35 -060061static int default_start_script(const char *script __unused,
62 int argc __unused,
63 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060064{
65 return 0;
66}
67
68static int default_stop_script(void)
69{
70 return 0;
71}
72
Tom Zanussi586bc5c2009-12-15 02:53:35 -060073static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060074{
75 return 0;
76}
77
78static struct scripting_ops default_scripting_ops = {
79 .start_script = default_start_script,
80 .stop_script = default_stop_script,
David Ahernbe6d8422011-03-09 22:23:23 -070081 .process_event = process_event,
Tom Zanussi956ffd02009-11-25 01:15:46 -060082 .generate_script = default_generate_script,
83};
84
85static struct scripting_ops *scripting_ops;
86
87static void setup_scripting(void)
88{
Tom Zanussi16c632d2009-11-25 01:15:48 -060089 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060090 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -060091
Tom Zanussi956ffd02009-11-25 01:15:46 -060092 scripting_ops = &default_scripting_ops;
93}
94
95static int cleanup_scripting(void)
96{
Ingo Molnar133dc4c2010-11-16 18:45:39 +010097 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -050098
Tom Zanussi956ffd02009-11-25 01:15:46 -060099 return scripting_ops->stop_script();
100}
101
Tom Zanussi956ffd02009-11-25 01:15:46 -0600102static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200103
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200104static int process_sample_event(union perf_event *event,
105 struct perf_sample *sample,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200106 struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200107{
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200108 struct thread *thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200109
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200110 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200111 pr_debug("problem processing %d event, skipping it.\n",
112 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200113 return -1;
114 }
115
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -0200116 if (session->sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200117 if (debug_mode) {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200118 if (sample->time < last_timestamp) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200119 pr_err("Samples misordered, previous: %" PRIu64
120 " this: %" PRIu64 "\n", last_timestamp,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200121 sample->time);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200122 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200123 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200124 last_timestamp = sample->time;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200125 return 0;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200126 }
David Ahernbe6d8422011-03-09 22:23:23 -0700127 scripting_ops->process_event(event, sample, session, thread);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200128 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200129
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -0200130 session->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200131 return 0;
132}
133
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200134static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200135 .sample = process_sample_event,
136 .comm = perf_event__process_comm,
137 .attr = perf_event__process_attr,
138 .event_type = perf_event__process_event_type,
139 .tracing_data = perf_event__process_tracing_data,
140 .build_id = perf_event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200141 .ordered_samples = true,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200142 .ordering_requires_timestamps = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200143};
144
Tom Zanussic239da32010-04-01 23:59:18 -0500145extern volatile int session_done;
146
147static void sig_handler(int sig __unused)
148{
149 session_done = 1;
150}
151
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100152static int __cmd_script(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200153{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200154 int ret;
155
Tom Zanussic239da32010-04-01 23:59:18 -0500156 signal(SIGINT, sig_handler);
157
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200158 ret = perf_session__process_events(session, &event_ops);
159
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200160 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200161 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200162
163 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200164}
165
Tom Zanussi956ffd02009-11-25 01:15:46 -0600166struct script_spec {
167 struct list_head node;
168 struct scripting_ops *ops;
169 char spec[0];
170};
171
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200172static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600173
174static struct script_spec *script_spec__new(const char *spec,
175 struct scripting_ops *ops)
176{
177 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
178
179 if (s != NULL) {
180 strcpy(s->spec, spec);
181 s->ops = ops;
182 }
183
184 return s;
185}
186
187static void script_spec__delete(struct script_spec *s)
188{
189 free(s->spec);
190 free(s);
191}
192
193static void script_spec__add(struct script_spec *s)
194{
195 list_add_tail(&s->node, &script_specs);
196}
197
198static struct script_spec *script_spec__find(const char *spec)
199{
200 struct script_spec *s;
201
202 list_for_each_entry(s, &script_specs, node)
203 if (strcasecmp(s->spec, spec) == 0)
204 return s;
205 return NULL;
206}
207
208static struct script_spec *script_spec__findnew(const char *spec,
209 struct scripting_ops *ops)
210{
211 struct script_spec *s = script_spec__find(spec);
212
213 if (s)
214 return s;
215
216 s = script_spec__new(spec, ops);
217 if (!s)
218 goto out_delete_spec;
219
220 script_spec__add(s);
221
222 return s;
223
224out_delete_spec:
225 script_spec__delete(s);
226
227 return NULL;
228}
229
230int script_spec_register(const char *spec, struct scripting_ops *ops)
231{
232 struct script_spec *s;
233
234 s = script_spec__find(spec);
235 if (s)
236 return -1;
237
238 s = script_spec__findnew(spec, ops);
239 if (!s)
240 return -1;
241
242 return 0;
243}
244
245static struct scripting_ops *script_spec__lookup(const char *spec)
246{
247 struct script_spec *s = script_spec__find(spec);
248 if (!s)
249 return NULL;
250
251 return s->ops;
252}
253
254static void list_available_languages(void)
255{
256 struct script_spec *s;
257
258 fprintf(stderr, "\n");
259 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100260 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600261
262 list_for_each_entry(s, &script_specs, node)
263 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
264
265 fprintf(stderr, "\n");
266}
267
268static int parse_scriptname(const struct option *opt __used,
269 const char *str, int unset __used)
270{
271 char spec[PATH_MAX];
272 const char *script, *ext;
273 int len;
274
Tom Zanussif526d682010-01-27 02:27:52 -0600275 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600276 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600277 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600278 }
279
280 script = strchr(str, ':');
281 if (script) {
282 len = script - str;
283 if (len >= PATH_MAX) {
284 fprintf(stderr, "invalid language specifier");
285 return -1;
286 }
287 strncpy(spec, str, len);
288 spec[len] = '\0';
289 scripting_ops = script_spec__lookup(spec);
290 if (!scripting_ops) {
291 fprintf(stderr, "invalid language specifier");
292 return -1;
293 }
294 script++;
295 } else {
296 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100297 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600298 if (!ext) {
299 fprintf(stderr, "invalid script extension");
300 return -1;
301 }
302 scripting_ops = script_spec__lookup(++ext);
303 if (!scripting_ops) {
304 fprintf(stderr, "invalid script extension");
305 return -1;
306 }
307 }
308
309 script_name = strdup(script);
310
311 return 0;
312}
313
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600314/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
315static int is_directory(const char *base_path, const struct dirent *dent)
316{
317 char path[PATH_MAX];
318 struct stat st;
319
320 sprintf(path, "%s/%s", base_path, dent->d_name);
321 if (stat(path, &st))
322 return 0;
323
324 return S_ISDIR(st.st_mode);
325}
326
327#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600328 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
329 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600330 if ((lang_dirent.d_type == DT_DIR || \
331 (lang_dirent.d_type == DT_UNKNOWN && \
332 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600333 (strcmp(lang_dirent.d_name, ".")) && \
334 (strcmp(lang_dirent.d_name, "..")))
335
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600336#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600337 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
338 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600339 if (script_dirent.d_type != DT_DIR && \
340 (script_dirent.d_type != DT_UNKNOWN || \
341 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600342
343
344#define RECORD_SUFFIX "-record"
345#define REPORT_SUFFIX "-report"
346
347struct script_desc {
348 struct list_head node;
349 char *name;
350 char *half_liner;
351 char *args;
352};
353
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200354static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600355
356static struct script_desc *script_desc__new(const char *name)
357{
358 struct script_desc *s = zalloc(sizeof(*s));
359
Tom Zanussib5b87312010-11-10 08:16:51 -0600360 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600361 s->name = strdup(name);
362
363 return s;
364}
365
366static void script_desc__delete(struct script_desc *s)
367{
368 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600369 free(s->half_liner);
370 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600371 free(s);
372}
373
374static void script_desc__add(struct script_desc *s)
375{
376 list_add_tail(&s->node, &script_descs);
377}
378
379static struct script_desc *script_desc__find(const char *name)
380{
381 struct script_desc *s;
382
383 list_for_each_entry(s, &script_descs, node)
384 if (strcasecmp(s->name, name) == 0)
385 return s;
386 return NULL;
387}
388
389static struct script_desc *script_desc__findnew(const char *name)
390{
391 struct script_desc *s = script_desc__find(name);
392
393 if (s)
394 return s;
395
396 s = script_desc__new(name);
397 if (!s)
398 goto out_delete_desc;
399
400 script_desc__add(s);
401
402 return s;
403
404out_delete_desc:
405 script_desc__delete(s);
406
407 return NULL;
408}
409
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200410static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600411{
412 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200413 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600414
415 if (strlen(str) > suffix_len) {
416 p = str + strlen(str) - suffix_len;
417 if (!strncmp(p, suffix, suffix_len))
418 return p;
419 }
420
421 return NULL;
422}
423
424static char *ltrim(char *str)
425{
426 int len = strlen(str);
427
428 while (len && isspace(*str)) {
429 len--;
430 str++;
431 }
432
433 return str;
434}
435
436static int read_script_info(struct script_desc *desc, const char *filename)
437{
438 char line[BUFSIZ], *p;
439 FILE *fp;
440
441 fp = fopen(filename, "r");
442 if (!fp)
443 return -1;
444
445 while (fgets(line, sizeof(line), fp)) {
446 p = ltrim(line);
447 if (strlen(p) == 0)
448 continue;
449 if (*p != '#')
450 continue;
451 p++;
452 if (strlen(p) && *p == '!')
453 continue;
454
455 p = ltrim(p);
456 if (strlen(p) && p[strlen(p) - 1] == '\n')
457 p[strlen(p) - 1] = '\0';
458
459 if (!strncmp(p, "description:", strlen("description:"))) {
460 p += strlen("description:");
461 desc->half_liner = strdup(ltrim(p));
462 continue;
463 }
464
465 if (!strncmp(p, "args:", strlen("args:"))) {
466 p += strlen("args:");
467 desc->args = strdup(ltrim(p));
468 continue;
469 }
470 }
471
472 fclose(fp);
473
474 return 0;
475}
476
477static int list_available_scripts(const struct option *opt __used,
478 const char *s __used, int unset __used)
479{
480 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
481 char scripts_path[MAXPATHLEN];
482 DIR *scripts_dir, *lang_dir;
483 char script_path[MAXPATHLEN];
484 char lang_path[MAXPATHLEN];
485 struct script_desc *desc;
486 char first_half[BUFSIZ];
487 char *script_root;
488 char *str;
489
490 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
491
492 scripts_dir = opendir(scripts_path);
493 if (!scripts_dir)
494 return -1;
495
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600496 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600497 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
498 lang_dirent.d_name);
499 lang_dir = opendir(lang_path);
500 if (!lang_dir)
501 continue;
502
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600503 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600504 script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200505 str = (char *)ends_with(script_root, REPORT_SUFFIX);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600506 if (str) {
507 *str = '\0';
508 desc = script_desc__findnew(script_root);
509 snprintf(script_path, MAXPATHLEN, "%s/%s",
510 lang_path, script_dirent.d_name);
511 read_script_info(desc, script_path);
512 }
513 free(script_root);
514 }
515 }
516
517 fprintf(stdout, "List of available trace scripts:\n");
518 list_for_each_entry(desc, &script_descs, node) {
519 sprintf(first_half, "%s %s", desc->name,
520 desc->args ? desc->args : "");
521 fprintf(stdout, " %-36s %s\n", first_half,
522 desc->half_liner ? desc->half_liner : "");
523 }
524
525 exit(0);
526}
527
Tom Zanussi38752942009-12-15 02:53:39 -0600528static char *get_script_path(const char *script_root, const char *suffix)
529{
530 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
531 char scripts_path[MAXPATHLEN];
532 char script_path[MAXPATHLEN];
533 DIR *scripts_dir, *lang_dir;
534 char lang_path[MAXPATHLEN];
535 char *str, *__script_root;
536 char *path = NULL;
537
538 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
539
540 scripts_dir = opendir(scripts_path);
541 if (!scripts_dir)
542 return NULL;
543
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600544 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -0600545 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
546 lang_dirent.d_name);
547 lang_dir = opendir(lang_path);
548 if (!lang_dir)
549 continue;
550
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600551 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi38752942009-12-15 02:53:39 -0600552 __script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200553 str = (char *)ends_with(__script_root, suffix);
Tom Zanussi38752942009-12-15 02:53:39 -0600554 if (str) {
555 *str = '\0';
556 if (strcmp(__script_root, script_root))
557 continue;
558 snprintf(script_path, MAXPATHLEN, "%s/%s",
559 lang_path, script_dirent.d_name);
560 path = strdup(script_path);
561 free(__script_root);
562 break;
563 }
564 free(__script_root);
565 }
566 }
567
568 return path;
569}
570
Tom Zanussib5b87312010-11-10 08:16:51 -0600571static bool is_top_script(const char *script_path)
572{
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200573 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -0600574}
575
576static int has_required_arg(char *script_path)
577{
578 struct script_desc *desc;
579 int n_args = 0;
580 char *p;
581
582 desc = script_desc__new(NULL);
583
584 if (read_script_info(desc, script_path))
585 goto out;
586
587 if (!desc->args)
588 goto out;
589
590 for (p = desc->args; *p; p++)
591 if (*p == '<')
592 n_args++;
593out:
594 script_desc__delete(desc);
595
596 return n_args;
597}
598
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100599static const char * const script_usage[] = {
600 "perf script [<options>]",
601 "perf script [<options>] record <script> [<record-options>] <command>",
602 "perf script [<options>] report <script> [script-args]",
603 "perf script [<options>] <script> [<record-options>] <command>",
604 "perf script [<options>] <top-script> [script-args]",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200605 NULL
606};
607
608static const struct option options[] = {
609 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
610 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000611 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200612 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600613 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400614 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600615 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
616 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600617 OPT_CALLBACK('s', "script", NULL, "name",
618 "script file name (lang:script name, script name, or *)",
619 parse_scriptname),
620 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100621 "generate perf-script.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900622 OPT_STRING('i', "input", &input_name, "file",
623 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200624 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
625 "do various checks like samples ordering and lost events"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600626
Masami Hiramatsu19096292009-08-21 14:56:03 -0400627 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200628};
629
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600630static bool have_cmd(int argc, const char **argv)
631{
632 char **__argv = malloc(sizeof(const char *) * argc);
633
634 if (!__argv)
635 die("malloc");
636 memcpy(__argv, argv, sizeof(const char *) * argc);
637 argc = parse_options(argc, (const char **)__argv, record_options,
638 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
639 free(__argv);
640
641 return argc != 0;
642}
643
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100644int cmd_script(int argc, const char **argv, const char *prefix __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200645{
Tom Zanussib5b87312010-11-10 08:16:51 -0600646 char *rec_script_path = NULL;
647 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200648 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -0600649 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -0600650 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -0600651 bool system_wide;
652 int i, j, err;
Tom Zanussi38752942009-12-15 02:53:39 -0600653
Tom Zanussib5b87312010-11-10 08:16:51 -0600654 setup_scripting();
655
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100656 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -0600657 PARSE_OPT_STOP_AT_NON_OPTION);
658
659 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
660 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
661 if (!rec_script_path)
662 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -0600663 }
664
Tom Zanussib5b87312010-11-10 08:16:51 -0600665 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
666 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
667 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -0600668 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -0600669 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100670 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -0600671 return -1;
672 }
Tom Zanussi38752942009-12-15 02:53:39 -0600673 }
674
Ben Hutchings44e668c2010-10-10 16:10:03 +0100675 /* make sure PERF_EXEC_PATH is set for scripts */
676 perf_set_argv_exec_path(perf_exec_path());
677
Tom Zanussib5b87312010-11-10 08:16:51 -0600678 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -0500679 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -0600680 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -0500681 pid_t pid;
682
Tom Zanussib5b87312010-11-10 08:16:51 -0600683 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
684 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
685
686 if (!rec_script_path && !rep_script_path) {
687 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100688 " script -l for available scripts.\n", argv[0]);
689 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -0500690 }
691
Tom Zanussib5b87312010-11-10 08:16:51 -0600692 if (is_top_script(argv[0])) {
693 rep_args = argc - 1;
694 } else {
695 int rec_args;
696
697 rep_args = has_required_arg(rep_script_path);
698 rec_args = (argc - 1) - rep_args;
699 if (rec_args < 0) {
700 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100701 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -0600702 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100703 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -0600704 }
Tom Zanussia0cccc22010-04-01 23:59:25 -0500705 }
706
707 if (pipe(live_pipe) < 0) {
708 perror("failed to create pipe");
709 exit(-1);
710 }
711
712 pid = fork();
713 if (pid < 0) {
714 perror("failed to fork");
715 exit(-1);
716 }
717
718 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -0600719 system_wide = true;
720 j = 0;
721
Tom Zanussia0cccc22010-04-01 23:59:25 -0500722 dup2(live_pipe[1], 1);
723 close(live_pipe[0]);
724
Tom Zanussib5b87312010-11-10 08:16:51 -0600725 if (!is_top_script(argv[0]))
726 system_wide = !have_cmd(argc - rep_args,
727 &argv[rep_args]);
728
729 __argv = malloc((argc + 6) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -0600730 if (!__argv)
731 die("malloc");
732
Tom Zanussib5b87312010-11-10 08:16:51 -0600733 __argv[j++] = "/bin/sh";
734 __argv[j++] = rec_script_path;
735 if (system_wide)
736 __argv[j++] = "-a";
737 __argv[j++] = "-q";
738 __argv[j++] = "-o";
739 __argv[j++] = "-";
740 for (i = rep_args + 1; i < argc; i++)
741 __argv[j++] = argv[i];
742 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -0500743
744 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600745 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -0500746 exit(-1);
747 }
748
749 dup2(live_pipe[0], 0);
750 close(live_pipe[1]);
751
Tom Zanussib5b87312010-11-10 08:16:51 -0600752 __argv = malloc((argc + 4) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -0600753 if (!__argv)
754 die("malloc");
Tom Zanussib5b87312010-11-10 08:16:51 -0600755 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600756 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -0600757 __argv[j++] = rep_script_path;
758 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600759 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -0600760 __argv[j++] = "-i";
761 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600762 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -0600763
764 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600765 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -0600766 exit(-1);
767 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600768
Tom Zanussib5b87312010-11-10 08:16:51 -0600769 if (rec_script_path)
770 script_path = rec_script_path;
771 if (rep_script_path)
772 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600773
Tom Zanussib5b87312010-11-10 08:16:51 -0600774 if (script_path) {
775 system_wide = false;
776 j = 0;
777
778 if (rec_script_path)
779 system_wide = !have_cmd(argc - 1, &argv[1]);
780
781 __argv = malloc((argc + 2) * sizeof(const char *));
782 if (!__argv)
783 die("malloc");
784 __argv[j++] = "/bin/sh";
785 __argv[j++] = script_path;
786 if (system_wide)
787 __argv[j++] = "-a";
788 for (i = 2; i < argc; i++)
789 __argv[j++] = argv[i];
790 __argv[j++] = NULL;
791
792 execvp("/bin/sh", (char **)__argv);
793 free(__argv);
794 exit(-1);
795 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200796
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200797 if (symbol__init() < 0)
798 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600799 if (!script_name)
800 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200801
Ian Munsie21ef97f2010-12-10 14:09:16 +1100802 session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200803 if (session == NULL)
804 return -ENOMEM;
805
Tom Zanussic239da32010-04-01 23:59:18 -0500806 if (strcmp(input_name, "-") &&
807 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200808 return -EINVAL;
809
Tom Zanussi956ffd02009-11-25 01:15:46 -0600810 if (generate_script_lang) {
811 struct stat perf_stat;
812
813 int input = open(input_name, O_RDONLY);
814 if (input < 0) {
815 perror("failed to open file");
816 exit(-1);
817 }
818
819 err = fstat(input, &perf_stat);
820 if (err < 0) {
821 perror("failed to stat file");
822 exit(-1);
823 }
824
825 if (!perf_stat.st_size) {
826 fprintf(stderr, "zero-sized file, nothing to do!\n");
827 exit(0);
828 }
829
830 scripting_ops = script_spec__lookup(generate_script_lang);
831 if (!scripting_ops) {
832 fprintf(stderr, "invalid language specifier");
833 return -1;
834 }
835
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100836 err = scripting_ops->generate_script("perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600837 goto out;
838 }
839
840 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600841 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600842 if (err)
843 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100844 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600845 }
846
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100847 err = __cmd_script(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600848
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200849 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600850 cleanup_scripting();
851out:
852 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200853}