blob: 5f40df635dcb0dbaddd6e65aa7f6b9678120b6d8 [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
Tom Zanussi586bc5c2009-12-15 02:53:35 -060023static int default_start_script(const char *script __unused,
24 int argc __unused,
25 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060026{
27 return 0;
28}
29
30static int default_stop_script(void)
31{
32 return 0;
33}
34
Tom Zanussi586bc5c2009-12-15 02:53:35 -060035static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060036{
37 return 0;
38}
39
40static struct scripting_ops default_scripting_ops = {
41 .start_script = default_start_script,
42 .stop_script = default_stop_script,
43 .process_event = print_event,
44 .generate_script = default_generate_script,
45};
46
47static struct scripting_ops *scripting_ops;
48
49static void setup_scripting(void)
50{
Tom Zanussi16c632d2009-11-25 01:15:48 -060051 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060052 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -060053
Tom Zanussi956ffd02009-11-25 01:15:46 -060054 scripting_ops = &default_scripting_ops;
55}
56
57static int cleanup_scripting(void)
58{
Ingo Molnar133dc4c2010-11-16 18:45:39 +010059 pr_debug("\nperf script stopped\n");
Tom Zanussi3824a4e2010-05-09 23:46:57 -050060
Tom Zanussi956ffd02009-11-25 01:15:46 -060061 return scripting_ops->stop_script();
62}
63
Tom Zanussi956ffd02009-11-25 01:15:46 -060064static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020065
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020066static int process_sample_event(union perf_event *event,
67 struct perf_sample *sample,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020068 struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020069{
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020070 struct thread *thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020071
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020072 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020073 pr_debug("problem processing %d event, skipping it.\n",
74 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020075 return -1;
76 }
77
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020078 if (session->sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020079 if (debug_mode) {
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020080 if (sample->time < last_timestamp) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -020081 pr_err("Samples misordered, previous: %" PRIu64
82 " this: %" PRIu64 "\n", last_timestamp,
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020083 sample->time);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020084 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020085 }
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020086 last_timestamp = sample->time;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020087 return 0;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020088 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020089 /*
90 * FIXME: better resolve from pid from the struct trace_entry
91 * field, although it should be the same than this perf
92 * event pid
93 */
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020094 scripting_ops->process_event(sample->cpu, sample->raw_data,
95 sample->raw_size,
96 sample->time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020097 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020098
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -020099 session->hists.stats.total_period += sample->period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200100 return 0;
101}
102
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200103static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200104 .sample = process_sample_event,
105 .comm = perf_event__process_comm,
106 .attr = perf_event__process_attr,
107 .event_type = perf_event__process_event_type,
108 .tracing_data = perf_event__process_tracing_data,
109 .build_id = perf_event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200110 .ordered_samples = true,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200111 .ordering_requires_timestamps = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200112};
113
Tom Zanussic239da32010-04-01 23:59:18 -0500114extern volatile int session_done;
115
116static void sig_handler(int sig __unused)
117{
118 session_done = 1;
119}
120
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100121static int __cmd_script(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200122{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200123 int ret;
124
Tom Zanussic239da32010-04-01 23:59:18 -0500125 signal(SIGINT, sig_handler);
126
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200127 ret = perf_session__process_events(session, &event_ops);
128
Arnaldo Carvalho de Melo6d8afb52011-01-04 16:27:30 -0200129 if (debug_mode)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200130 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200131
132 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200133}
134
Tom Zanussi956ffd02009-11-25 01:15:46 -0600135struct script_spec {
136 struct list_head node;
137 struct scripting_ops *ops;
138 char spec[0];
139};
140
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200141static LIST_HEAD(script_specs);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600142
143static struct script_spec *script_spec__new(const char *spec,
144 struct scripting_ops *ops)
145{
146 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
147
148 if (s != NULL) {
149 strcpy(s->spec, spec);
150 s->ops = ops;
151 }
152
153 return s;
154}
155
156static void script_spec__delete(struct script_spec *s)
157{
158 free(s->spec);
159 free(s);
160}
161
162static void script_spec__add(struct script_spec *s)
163{
164 list_add_tail(&s->node, &script_specs);
165}
166
167static struct script_spec *script_spec__find(const char *spec)
168{
169 struct script_spec *s;
170
171 list_for_each_entry(s, &script_specs, node)
172 if (strcasecmp(s->spec, spec) == 0)
173 return s;
174 return NULL;
175}
176
177static struct script_spec *script_spec__findnew(const char *spec,
178 struct scripting_ops *ops)
179{
180 struct script_spec *s = script_spec__find(spec);
181
182 if (s)
183 return s;
184
185 s = script_spec__new(spec, ops);
186 if (!s)
187 goto out_delete_spec;
188
189 script_spec__add(s);
190
191 return s;
192
193out_delete_spec:
194 script_spec__delete(s);
195
196 return NULL;
197}
198
199int script_spec_register(const char *spec, struct scripting_ops *ops)
200{
201 struct script_spec *s;
202
203 s = script_spec__find(spec);
204 if (s)
205 return -1;
206
207 s = script_spec__findnew(spec, ops);
208 if (!s)
209 return -1;
210
211 return 0;
212}
213
214static struct scripting_ops *script_spec__lookup(const char *spec)
215{
216 struct script_spec *s = script_spec__find(spec);
217 if (!s)
218 return NULL;
219
220 return s->ops;
221}
222
223static void list_available_languages(void)
224{
225 struct script_spec *s;
226
227 fprintf(stderr, "\n");
228 fprintf(stderr, "Scripting language extensions (used in "
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100229 "perf script -s [spec:]script.[spec]):\n\n");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600230
231 list_for_each_entry(s, &script_specs, node)
232 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
233
234 fprintf(stderr, "\n");
235}
236
237static int parse_scriptname(const struct option *opt __used,
238 const char *str, int unset __used)
239{
240 char spec[PATH_MAX];
241 const char *script, *ext;
242 int len;
243
Tom Zanussif526d682010-01-27 02:27:52 -0600244 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600245 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600246 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600247 }
248
249 script = strchr(str, ':');
250 if (script) {
251 len = script - str;
252 if (len >= PATH_MAX) {
253 fprintf(stderr, "invalid language specifier");
254 return -1;
255 }
256 strncpy(spec, str, len);
257 spec[len] = '\0';
258 scripting_ops = script_spec__lookup(spec);
259 if (!scripting_ops) {
260 fprintf(stderr, "invalid language specifier");
261 return -1;
262 }
263 script++;
264 } else {
265 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100266 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600267 if (!ext) {
268 fprintf(stderr, "invalid script extension");
269 return -1;
270 }
271 scripting_ops = script_spec__lookup(++ext);
272 if (!scripting_ops) {
273 fprintf(stderr, "invalid script extension");
274 return -1;
275 }
276 }
277
278 script_name = strdup(script);
279
280 return 0;
281}
282
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600283/* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
284static int is_directory(const char *base_path, const struct dirent *dent)
285{
286 char path[PATH_MAX];
287 struct stat st;
288
289 sprintf(path, "%s/%s", base_path, dent->d_name);
290 if (stat(path, &st))
291 return 0;
292
293 return S_ISDIR(st.st_mode);
294}
295
296#define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600297 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
298 lang_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600299 if ((lang_dirent.d_type == DT_DIR || \
300 (lang_dirent.d_type == DT_UNKNOWN && \
301 is_directory(scripts_path, &lang_dirent))) && \
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600302 (strcmp(lang_dirent.d_name, ".")) && \
303 (strcmp(lang_dirent.d_name, "..")))
304
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600305#define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600306 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
307 script_next) \
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600308 if (script_dirent.d_type != DT_DIR && \
309 (script_dirent.d_type != DT_UNKNOWN || \
310 !is_directory(lang_path, &script_dirent)))
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600311
312
313#define RECORD_SUFFIX "-record"
314#define REPORT_SUFFIX "-report"
315
316struct script_desc {
317 struct list_head node;
318 char *name;
319 char *half_liner;
320 char *args;
321};
322
Arnaldo Carvalho de Meloeccdfe22011-01-04 16:32:52 -0200323static LIST_HEAD(script_descs);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600324
325static struct script_desc *script_desc__new(const char *name)
326{
327 struct script_desc *s = zalloc(sizeof(*s));
328
Tom Zanussib5b87312010-11-10 08:16:51 -0600329 if (s != NULL && name)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600330 s->name = strdup(name);
331
332 return s;
333}
334
335static void script_desc__delete(struct script_desc *s)
336{
337 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600338 free(s->half_liner);
339 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600340 free(s);
341}
342
343static void script_desc__add(struct script_desc *s)
344{
345 list_add_tail(&s->node, &script_descs);
346}
347
348static struct script_desc *script_desc__find(const char *name)
349{
350 struct script_desc *s;
351
352 list_for_each_entry(s, &script_descs, node)
353 if (strcasecmp(s->name, name) == 0)
354 return s;
355 return NULL;
356}
357
358static struct script_desc *script_desc__findnew(const char *name)
359{
360 struct script_desc *s = script_desc__find(name);
361
362 if (s)
363 return s;
364
365 s = script_desc__new(name);
366 if (!s)
367 goto out_delete_desc;
368
369 script_desc__add(s);
370
371 return s;
372
373out_delete_desc:
374 script_desc__delete(s);
375
376 return NULL;
377}
378
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200379static const char *ends_with(const char *str, const char *suffix)
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600380{
381 size_t suffix_len = strlen(suffix);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200382 const char *p = str;
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600383
384 if (strlen(str) > suffix_len) {
385 p = str + strlen(str) - suffix_len;
386 if (!strncmp(p, suffix, suffix_len))
387 return p;
388 }
389
390 return NULL;
391}
392
393static char *ltrim(char *str)
394{
395 int len = strlen(str);
396
397 while (len && isspace(*str)) {
398 len--;
399 str++;
400 }
401
402 return str;
403}
404
405static int read_script_info(struct script_desc *desc, const char *filename)
406{
407 char line[BUFSIZ], *p;
408 FILE *fp;
409
410 fp = fopen(filename, "r");
411 if (!fp)
412 return -1;
413
414 while (fgets(line, sizeof(line), fp)) {
415 p = ltrim(line);
416 if (strlen(p) == 0)
417 continue;
418 if (*p != '#')
419 continue;
420 p++;
421 if (strlen(p) && *p == '!')
422 continue;
423
424 p = ltrim(p);
425 if (strlen(p) && p[strlen(p) - 1] == '\n')
426 p[strlen(p) - 1] = '\0';
427
428 if (!strncmp(p, "description:", strlen("description:"))) {
429 p += strlen("description:");
430 desc->half_liner = strdup(ltrim(p));
431 continue;
432 }
433
434 if (!strncmp(p, "args:", strlen("args:"))) {
435 p += strlen("args:");
436 desc->args = strdup(ltrim(p));
437 continue;
438 }
439 }
440
441 fclose(fp);
442
443 return 0;
444}
445
446static int list_available_scripts(const struct option *opt __used,
447 const char *s __used, int unset __used)
448{
449 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
450 char scripts_path[MAXPATHLEN];
451 DIR *scripts_dir, *lang_dir;
452 char script_path[MAXPATHLEN];
453 char lang_path[MAXPATHLEN];
454 struct script_desc *desc;
455 char first_half[BUFSIZ];
456 char *script_root;
457 char *str;
458
459 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
460
461 scripts_dir = opendir(scripts_path);
462 if (!scripts_dir)
463 return -1;
464
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600465 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600466 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
467 lang_dirent.d_name);
468 lang_dir = opendir(lang_path);
469 if (!lang_dir)
470 continue;
471
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600472 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600473 script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200474 str = (char *)ends_with(script_root, REPORT_SUFFIX);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600475 if (str) {
476 *str = '\0';
477 desc = script_desc__findnew(script_root);
478 snprintf(script_path, MAXPATHLEN, "%s/%s",
479 lang_path, script_dirent.d_name);
480 read_script_info(desc, script_path);
481 }
482 free(script_root);
483 }
484 }
485
486 fprintf(stdout, "List of available trace scripts:\n");
487 list_for_each_entry(desc, &script_descs, node) {
488 sprintf(first_half, "%s %s", desc->name,
489 desc->args ? desc->args : "");
490 fprintf(stdout, " %-36s %s\n", first_half,
491 desc->half_liner ? desc->half_liner : "");
492 }
493
494 exit(0);
495}
496
Tom Zanussi38752942009-12-15 02:53:39 -0600497static char *get_script_path(const char *script_root, const char *suffix)
498{
499 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
500 char scripts_path[MAXPATHLEN];
501 char script_path[MAXPATHLEN];
502 DIR *scripts_dir, *lang_dir;
503 char lang_path[MAXPATHLEN];
504 char *str, *__script_root;
505 char *path = NULL;
506
507 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
508
509 scripts_dir = opendir(scripts_path);
510 if (!scripts_dir)
511 return NULL;
512
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600513 for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
Tom Zanussi38752942009-12-15 02:53:39 -0600514 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
515 lang_dirent.d_name);
516 lang_dir = opendir(lang_path);
517 if (!lang_dir)
518 continue;
519
Shawn Bohrer008f29d2010-11-21 10:09:39 -0600520 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
Tom Zanussi38752942009-12-15 02:53:39 -0600521 __script_root = strdup(script_dirent.d_name);
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200522 str = (char *)ends_with(__script_root, suffix);
Tom Zanussi38752942009-12-15 02:53:39 -0600523 if (str) {
524 *str = '\0';
525 if (strcmp(__script_root, script_root))
526 continue;
527 snprintf(script_path, MAXPATHLEN, "%s/%s",
528 lang_path, script_dirent.d_name);
529 path = strdup(script_path);
530 free(__script_root);
531 break;
532 }
533 free(__script_root);
534 }
535 }
536
537 return path;
538}
539
Tom Zanussib5b87312010-11-10 08:16:51 -0600540static bool is_top_script(const char *script_path)
541{
Stephane Eranian965bb6b2010-12-03 17:52:01 +0200542 return ends_with(script_path, "top") == NULL ? false : true;
Tom Zanussib5b87312010-11-10 08:16:51 -0600543}
544
545static int has_required_arg(char *script_path)
546{
547 struct script_desc *desc;
548 int n_args = 0;
549 char *p;
550
551 desc = script_desc__new(NULL);
552
553 if (read_script_info(desc, script_path))
554 goto out;
555
556 if (!desc->args)
557 goto out;
558
559 for (p = desc->args; *p; p++)
560 if (*p == '<')
561 n_args++;
562out:
563 script_desc__delete(desc);
564
565 return n_args;
566}
567
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100568static const char * const script_usage[] = {
569 "perf script [<options>]",
570 "perf script [<options>] record <script> [<record-options>] <command>",
571 "perf script [<options>] report <script> [script-args]",
572 "perf script [<options>] <script> [<record-options>] <command>",
573 "perf script [<options>] <top-script> [script-args]",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200574 NULL
575};
576
577static const struct option options[] = {
578 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
579 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000580 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200581 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600582 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400583 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600584 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
585 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600586 OPT_CALLBACK('s', "script", NULL, "name",
587 "script file name (lang:script name, script name, or *)",
588 parse_scriptname),
589 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100590 "generate perf-script.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900591 OPT_STRING('i', "input", &input_name, "file",
592 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200593 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
594 "do various checks like samples ordering and lost events"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600595
Masami Hiramatsu19096292009-08-21 14:56:03 -0400596 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200597};
598
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600599static bool have_cmd(int argc, const char **argv)
600{
601 char **__argv = malloc(sizeof(const char *) * argc);
602
603 if (!__argv)
604 die("malloc");
605 memcpy(__argv, argv, sizeof(const char *) * argc);
606 argc = parse_options(argc, (const char **)__argv, record_options,
607 NULL, PARSE_OPT_STOP_AT_NON_OPTION);
608 free(__argv);
609
610 return argc != 0;
611}
612
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100613int cmd_script(int argc, const char **argv, const char *prefix __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200614{
Tom Zanussib5b87312010-11-10 08:16:51 -0600615 char *rec_script_path = NULL;
616 char *rep_script_path = NULL;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200617 struct perf_session *session;
Tom Zanussib5b87312010-11-10 08:16:51 -0600618 char *script_path = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -0600619 const char **__argv;
Tom Zanussib5b87312010-11-10 08:16:51 -0600620 bool system_wide;
621 int i, j, err;
Tom Zanussi38752942009-12-15 02:53:39 -0600622
Tom Zanussib5b87312010-11-10 08:16:51 -0600623 setup_scripting();
624
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100625 argc = parse_options(argc, argv, options, script_usage,
Tom Zanussib5b87312010-11-10 08:16:51 -0600626 PARSE_OPT_STOP_AT_NON_OPTION);
627
628 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
629 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
630 if (!rec_script_path)
631 return cmd_record(argc, argv, NULL);
Tom Zanussi38752942009-12-15 02:53:39 -0600632 }
633
Tom Zanussib5b87312010-11-10 08:16:51 -0600634 if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
635 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
636 if (!rep_script_path) {
Tom Zanussi38752942009-12-15 02:53:39 -0600637 fprintf(stderr,
Tom Zanussib5b87312010-11-10 08:16:51 -0600638 "Please specify a valid report script"
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100639 "(see 'perf script -l' for listing)\n");
Tom Zanussi38752942009-12-15 02:53:39 -0600640 return -1;
641 }
Tom Zanussi38752942009-12-15 02:53:39 -0600642 }
643
Ben Hutchings44e668c2010-10-10 16:10:03 +0100644 /* make sure PERF_EXEC_PATH is set for scripts */
645 perf_set_argv_exec_path(perf_exec_path());
646
Tom Zanussib5b87312010-11-10 08:16:51 -0600647 if (argc && !script_name && !rec_script_path && !rep_script_path) {
Tom Zanussia0cccc22010-04-01 23:59:25 -0500648 int live_pipe[2];
Tom Zanussib5b87312010-11-10 08:16:51 -0600649 int rep_args;
Tom Zanussia0cccc22010-04-01 23:59:25 -0500650 pid_t pid;
651
Tom Zanussib5b87312010-11-10 08:16:51 -0600652 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
653 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
654
655 if (!rec_script_path && !rep_script_path) {
656 fprintf(stderr, " Couldn't find script %s\n\n See perf"
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100657 " script -l for available scripts.\n", argv[0]);
658 usage_with_options(script_usage, options);
Tom Zanussia0cccc22010-04-01 23:59:25 -0500659 }
660
Tom Zanussib5b87312010-11-10 08:16:51 -0600661 if (is_top_script(argv[0])) {
662 rep_args = argc - 1;
663 } else {
664 int rec_args;
665
666 rep_args = has_required_arg(rep_script_path);
667 rec_args = (argc - 1) - rep_args;
668 if (rec_args < 0) {
669 fprintf(stderr, " %s script requires options."
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100670 "\n\n See perf script -l for available "
Tom Zanussib5b87312010-11-10 08:16:51 -0600671 "scripts and options.\n", argv[0]);
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100672 usage_with_options(script_usage, options);
Tom Zanussib5b87312010-11-10 08:16:51 -0600673 }
Tom Zanussia0cccc22010-04-01 23:59:25 -0500674 }
675
676 if (pipe(live_pipe) < 0) {
677 perror("failed to create pipe");
678 exit(-1);
679 }
680
681 pid = fork();
682 if (pid < 0) {
683 perror("failed to fork");
684 exit(-1);
685 }
686
687 if (!pid) {
Tom Zanussib5b87312010-11-10 08:16:51 -0600688 system_wide = true;
689 j = 0;
690
Tom Zanussia0cccc22010-04-01 23:59:25 -0500691 dup2(live_pipe[1], 1);
692 close(live_pipe[0]);
693
Tom Zanussib5b87312010-11-10 08:16:51 -0600694 if (!is_top_script(argv[0]))
695 system_wide = !have_cmd(argc - rep_args,
696 &argv[rep_args]);
697
698 __argv = malloc((argc + 6) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -0600699 if (!__argv)
700 die("malloc");
701
Tom Zanussib5b87312010-11-10 08:16:51 -0600702 __argv[j++] = "/bin/sh";
703 __argv[j++] = rec_script_path;
704 if (system_wide)
705 __argv[j++] = "-a";
706 __argv[j++] = "-q";
707 __argv[j++] = "-o";
708 __argv[j++] = "-";
709 for (i = rep_args + 1; i < argc; i++)
710 __argv[j++] = argv[i];
711 __argv[j++] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -0500712
713 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600714 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -0500715 exit(-1);
716 }
717
718 dup2(live_pipe[0], 0);
719 close(live_pipe[1]);
720
Tom Zanussib5b87312010-11-10 08:16:51 -0600721 __argv = malloc((argc + 4) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -0600722 if (!__argv)
723 die("malloc");
Tom Zanussib5b87312010-11-10 08:16:51 -0600724 j = 0;
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600725 __argv[j++] = "/bin/sh";
Tom Zanussib5b87312010-11-10 08:16:51 -0600726 __argv[j++] = rep_script_path;
727 for (i = 1; i < rep_args + 1; i++)
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600728 __argv[j++] = argv[i];
Tom Zanussib5b87312010-11-10 08:16:51 -0600729 __argv[j++] = "-i";
730 __argv[j++] = "-";
Tom Zanussi34c86ea2010-11-10 08:15:43 -0600731 __argv[j++] = NULL;
Tom Zanussi38752942009-12-15 02:53:39 -0600732
733 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600734 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -0600735 exit(-1);
736 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600737
Tom Zanussib5b87312010-11-10 08:16:51 -0600738 if (rec_script_path)
739 script_path = rec_script_path;
740 if (rep_script_path)
741 script_path = rep_script_path;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600742
Tom Zanussib5b87312010-11-10 08:16:51 -0600743 if (script_path) {
744 system_wide = false;
745 j = 0;
746
747 if (rec_script_path)
748 system_wide = !have_cmd(argc - 1, &argv[1]);
749
750 __argv = malloc((argc + 2) * sizeof(const char *));
751 if (!__argv)
752 die("malloc");
753 __argv[j++] = "/bin/sh";
754 __argv[j++] = script_path;
755 if (system_wide)
756 __argv[j++] = "-a";
757 for (i = 2; i < argc; i++)
758 __argv[j++] = argv[i];
759 __argv[j++] = NULL;
760
761 execvp("/bin/sh", (char **)__argv);
762 free(__argv);
763 exit(-1);
764 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200765
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200766 if (symbol__init() < 0)
767 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600768 if (!script_name)
769 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200770
Ian Munsie21ef97f2010-12-10 14:09:16 +1100771 session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200772 if (session == NULL)
773 return -ENOMEM;
774
Tom Zanussic239da32010-04-01 23:59:18 -0500775 if (strcmp(input_name, "-") &&
776 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200777 return -EINVAL;
778
Tom Zanussi956ffd02009-11-25 01:15:46 -0600779 if (generate_script_lang) {
780 struct stat perf_stat;
781
782 int input = open(input_name, O_RDONLY);
783 if (input < 0) {
784 perror("failed to open file");
785 exit(-1);
786 }
787
788 err = fstat(input, &perf_stat);
789 if (err < 0) {
790 perror("failed to stat file");
791 exit(-1);
792 }
793
794 if (!perf_stat.st_size) {
795 fprintf(stderr, "zero-sized file, nothing to do!\n");
796 exit(0);
797 }
798
799 scripting_ops = script_spec__lookup(generate_script_lang);
800 if (!scripting_ops) {
801 fprintf(stderr, "invalid language specifier");
802 return -1;
803 }
804
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100805 err = scripting_ops->generate_script("perf-script");
Tom Zanussi956ffd02009-11-25 01:15:46 -0600806 goto out;
807 }
808
809 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600810 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600811 if (err)
812 goto out;
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100813 pr_debug("perf script started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600814 }
815
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100816 err = __cmd_script(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600817
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200818 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600819 cleanup_scripting();
820out:
821 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200822}