blob: 9c483e92e8dbae08a960a45ea52ceab72edb2a1b [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#include "builtin.h"
2
3#include "util/util.h"
4#include "util/cache.h"
5#include "util/symbol.h"
6#include "util/thread.h"
7#include "util/header.h"
Ingo Molnarcf723442009-11-28 10:11:00 +01008#include "util/exec_cmd.h"
9#include "util/trace-event.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020010#include "util/session.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020011
Tom Zanussi956ffd02009-11-25 01:15:46 -060012static char const *script_name;
13static char const *generate_script_lang;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020014static bool debug_ordering;
15static u64 last_timestamp;
Tom Zanussi956ffd02009-11-25 01:15:46 -060016
Tom Zanussi586bc5c2009-12-15 02:53:35 -060017static int default_start_script(const char *script __unused,
18 int argc __unused,
19 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060020{
21 return 0;
22}
23
24static int default_stop_script(void)
25{
26 return 0;
27}
28
Tom Zanussi586bc5c2009-12-15 02:53:35 -060029static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060030{
31 return 0;
32}
33
34static struct scripting_ops default_scripting_ops = {
35 .start_script = default_start_script,
36 .stop_script = default_stop_script,
37 .process_event = print_event,
38 .generate_script = default_generate_script,
39};
40
41static struct scripting_ops *scripting_ops;
42
43static void setup_scripting(void)
44{
45 /* make sure PERF_EXEC_PATH is set for scripts */
46 perf_set_argv_exec_path(perf_exec_path());
47
Tom Zanussi16c632d2009-11-25 01:15:48 -060048 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060049 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -060050
Tom Zanussi956ffd02009-11-25 01:15:46 -060051 scripting_ops = &default_scripting_ops;
52}
53
54static int cleanup_scripting(void)
55{
56 return scripting_ops->stop_script();
57}
58
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020059#include "util/parse-options.h"
60
61#include "perf.h"
62#include "util/debug.h"
63
64#include "util/trace-event.h"
Tom Zanussi956ffd02009-11-25 01:15:46 -060065#include "util/exec_cmd.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020066
Tom Zanussi956ffd02009-11-25 01:15:46 -060067static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020068
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020069static int process_sample_event(event_t *event, struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020070{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090071 struct sample_data data;
72 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020073
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090074 memset(&data, 0, sizeof(data));
75 data.time = -1;
76 data.cpu = -1;
77 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020078
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020079 event__parse_sample(event, session->sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020080
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -020081 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
82 data.pid, data.tid, data.ip, data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020084 thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020085 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020086 pr_debug("problem processing %d event, skipping it.\n",
87 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020088 return -1;
89 }
90
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020091 if (session->sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020092 if (debug_ordering) {
93 if (data.time < last_timestamp) {
94 pr_err("Samples misordered, previous: %llu "
95 "this: %llu\n", last_timestamp,
96 data.time);
97 }
98 last_timestamp = data.time;
99 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200100 /*
101 * FIXME: better resolve from pid from the struct trace_entry
102 * field, although it should be the same than this perf
103 * event pid
104 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900105 scripting_ops->process_event(data.cpu, data.raw_data,
106 data.raw_size,
107 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200108 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200109
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200110 session->events_stats.total += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200111 return 0;
112}
113
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200114static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200115 .sample = process_sample_event,
116 .comm = event__process_comm,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500117 .attr = event__process_attr,
Tom Zanussicd19a032010-04-01 23:59:20 -0500118 .event_type = event__process_event_type,
Tom Zanussi92155452010-04-01 23:59:21 -0500119 .tracing_data = event__process_tracing_data,
Tom Zanussic7929e42010-04-01 23:59:22 -0500120 .build_id = event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200121 .ordered_samples = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200122};
123
Tom Zanussic239da32010-04-01 23:59:18 -0500124extern volatile int session_done;
125
126static void sig_handler(int sig __unused)
127{
128 session_done = 1;
129}
130
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200131static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200132{
Tom Zanussic239da32010-04-01 23:59:18 -0500133 signal(SIGINT, sig_handler);
134
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200135 return perf_session__process_events(session, &event_ops);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200136}
137
Tom Zanussi956ffd02009-11-25 01:15:46 -0600138struct script_spec {
139 struct list_head node;
140 struct scripting_ops *ops;
141 char spec[0];
142};
143
144LIST_HEAD(script_specs);
145
146static struct script_spec *script_spec__new(const char *spec,
147 struct scripting_ops *ops)
148{
149 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
150
151 if (s != NULL) {
152 strcpy(s->spec, spec);
153 s->ops = ops;
154 }
155
156 return s;
157}
158
159static void script_spec__delete(struct script_spec *s)
160{
161 free(s->spec);
162 free(s);
163}
164
165static void script_spec__add(struct script_spec *s)
166{
167 list_add_tail(&s->node, &script_specs);
168}
169
170static struct script_spec *script_spec__find(const char *spec)
171{
172 struct script_spec *s;
173
174 list_for_each_entry(s, &script_specs, node)
175 if (strcasecmp(s->spec, spec) == 0)
176 return s;
177 return NULL;
178}
179
180static struct script_spec *script_spec__findnew(const char *spec,
181 struct scripting_ops *ops)
182{
183 struct script_spec *s = script_spec__find(spec);
184
185 if (s)
186 return s;
187
188 s = script_spec__new(spec, ops);
189 if (!s)
190 goto out_delete_spec;
191
192 script_spec__add(s);
193
194 return s;
195
196out_delete_spec:
197 script_spec__delete(s);
198
199 return NULL;
200}
201
202int script_spec_register(const char *spec, struct scripting_ops *ops)
203{
204 struct script_spec *s;
205
206 s = script_spec__find(spec);
207 if (s)
208 return -1;
209
210 s = script_spec__findnew(spec, ops);
211 if (!s)
212 return -1;
213
214 return 0;
215}
216
217static struct scripting_ops *script_spec__lookup(const char *spec)
218{
219 struct script_spec *s = script_spec__find(spec);
220 if (!s)
221 return NULL;
222
223 return s->ops;
224}
225
226static void list_available_languages(void)
227{
228 struct script_spec *s;
229
230 fprintf(stderr, "\n");
231 fprintf(stderr, "Scripting language extensions (used in "
232 "perf trace -s [spec:]script.[spec]):\n\n");
233
234 list_for_each_entry(s, &script_specs, node)
235 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
236
237 fprintf(stderr, "\n");
238}
239
240static int parse_scriptname(const struct option *opt __used,
241 const char *str, int unset __used)
242{
243 char spec[PATH_MAX];
244 const char *script, *ext;
245 int len;
246
Tom Zanussif526d682010-01-27 02:27:52 -0600247 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600248 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600249 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600250 }
251
252 script = strchr(str, ':');
253 if (script) {
254 len = script - str;
255 if (len >= PATH_MAX) {
256 fprintf(stderr, "invalid language specifier");
257 return -1;
258 }
259 strncpy(spec, str, len);
260 spec[len] = '\0';
261 scripting_ops = script_spec__lookup(spec);
262 if (!scripting_ops) {
263 fprintf(stderr, "invalid language specifier");
264 return -1;
265 }
266 script++;
267 } else {
268 script = str;
269 ext = strchr(script, '.');
270 if (!ext) {
271 fprintf(stderr, "invalid script extension");
272 return -1;
273 }
274 scripting_ops = script_spec__lookup(++ext);
275 if (!scripting_ops) {
276 fprintf(stderr, "invalid script extension");
277 return -1;
278 }
279 }
280
281 script_name = strdup(script);
282
283 return 0;
284}
285
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600286#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
287 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
288 lang_next) \
289 if (lang_dirent.d_type == DT_DIR && \
290 (strcmp(lang_dirent.d_name, ".")) && \
291 (strcmp(lang_dirent.d_name, "..")))
292
293#define for_each_script(lang_dir, script_dirent, script_next) \
294 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
295 script_next) \
296 if (script_dirent.d_type != DT_DIR)
297
298
299#define RECORD_SUFFIX "-record"
300#define REPORT_SUFFIX "-report"
301
302struct script_desc {
303 struct list_head node;
304 char *name;
305 char *half_liner;
306 char *args;
307};
308
309LIST_HEAD(script_descs);
310
311static struct script_desc *script_desc__new(const char *name)
312{
313 struct script_desc *s = zalloc(sizeof(*s));
314
315 if (s != NULL)
316 s->name = strdup(name);
317
318 return s;
319}
320
321static void script_desc__delete(struct script_desc *s)
322{
323 free(s->name);
324 free(s);
325}
326
327static void script_desc__add(struct script_desc *s)
328{
329 list_add_tail(&s->node, &script_descs);
330}
331
332static struct script_desc *script_desc__find(const char *name)
333{
334 struct script_desc *s;
335
336 list_for_each_entry(s, &script_descs, node)
337 if (strcasecmp(s->name, name) == 0)
338 return s;
339 return NULL;
340}
341
342static struct script_desc *script_desc__findnew(const char *name)
343{
344 struct script_desc *s = script_desc__find(name);
345
346 if (s)
347 return s;
348
349 s = script_desc__new(name);
350 if (!s)
351 goto out_delete_desc;
352
353 script_desc__add(s);
354
355 return s;
356
357out_delete_desc:
358 script_desc__delete(s);
359
360 return NULL;
361}
362
363static char *ends_with(char *str, const char *suffix)
364{
365 size_t suffix_len = strlen(suffix);
366 char *p = str;
367
368 if (strlen(str) > suffix_len) {
369 p = str + strlen(str) - suffix_len;
370 if (!strncmp(p, suffix, suffix_len))
371 return p;
372 }
373
374 return NULL;
375}
376
377static char *ltrim(char *str)
378{
379 int len = strlen(str);
380
381 while (len && isspace(*str)) {
382 len--;
383 str++;
384 }
385
386 return str;
387}
388
389static int read_script_info(struct script_desc *desc, const char *filename)
390{
391 char line[BUFSIZ], *p;
392 FILE *fp;
393
394 fp = fopen(filename, "r");
395 if (!fp)
396 return -1;
397
398 while (fgets(line, sizeof(line), fp)) {
399 p = ltrim(line);
400 if (strlen(p) == 0)
401 continue;
402 if (*p != '#')
403 continue;
404 p++;
405 if (strlen(p) && *p == '!')
406 continue;
407
408 p = ltrim(p);
409 if (strlen(p) && p[strlen(p) - 1] == '\n')
410 p[strlen(p) - 1] = '\0';
411
412 if (!strncmp(p, "description:", strlen("description:"))) {
413 p += strlen("description:");
414 desc->half_liner = strdup(ltrim(p));
415 continue;
416 }
417
418 if (!strncmp(p, "args:", strlen("args:"))) {
419 p += strlen("args:");
420 desc->args = strdup(ltrim(p));
421 continue;
422 }
423 }
424
425 fclose(fp);
426
427 return 0;
428}
429
430static int list_available_scripts(const struct option *opt __used,
431 const char *s __used, int unset __used)
432{
433 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
434 char scripts_path[MAXPATHLEN];
435 DIR *scripts_dir, *lang_dir;
436 char script_path[MAXPATHLEN];
437 char lang_path[MAXPATHLEN];
438 struct script_desc *desc;
439 char first_half[BUFSIZ];
440 char *script_root;
441 char *str;
442
443 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
444
445 scripts_dir = opendir(scripts_path);
446 if (!scripts_dir)
447 return -1;
448
449 for_each_lang(scripts_dir, lang_dirent, lang_next) {
450 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
451 lang_dirent.d_name);
452 lang_dir = opendir(lang_path);
453 if (!lang_dir)
454 continue;
455
456 for_each_script(lang_dir, script_dirent, script_next) {
457 script_root = strdup(script_dirent.d_name);
458 str = ends_with(script_root, REPORT_SUFFIX);
459 if (str) {
460 *str = '\0';
461 desc = script_desc__findnew(script_root);
462 snprintf(script_path, MAXPATHLEN, "%s/%s",
463 lang_path, script_dirent.d_name);
464 read_script_info(desc, script_path);
465 }
466 free(script_root);
467 }
468 }
469
470 fprintf(stdout, "List of available trace scripts:\n");
471 list_for_each_entry(desc, &script_descs, node) {
472 sprintf(first_half, "%s %s", desc->name,
473 desc->args ? desc->args : "");
474 fprintf(stdout, " %-36s %s\n", first_half,
475 desc->half_liner ? desc->half_liner : "");
476 }
477
478 exit(0);
479}
480
Tom Zanussi38752942009-12-15 02:53:39 -0600481static char *get_script_path(const char *script_root, const char *suffix)
482{
483 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
484 char scripts_path[MAXPATHLEN];
485 char script_path[MAXPATHLEN];
486 DIR *scripts_dir, *lang_dir;
487 char lang_path[MAXPATHLEN];
488 char *str, *__script_root;
489 char *path = NULL;
490
491 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
492
493 scripts_dir = opendir(scripts_path);
494 if (!scripts_dir)
495 return NULL;
496
497 for_each_lang(scripts_dir, lang_dirent, lang_next) {
498 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
499 lang_dirent.d_name);
500 lang_dir = opendir(lang_path);
501 if (!lang_dir)
502 continue;
503
504 for_each_script(lang_dir, script_dirent, script_next) {
505 __script_root = strdup(script_dirent.d_name);
506 str = ends_with(__script_root, suffix);
507 if (str) {
508 *str = '\0';
509 if (strcmp(__script_root, script_root))
510 continue;
511 snprintf(script_path, MAXPATHLEN, "%s/%s",
512 lang_path, script_dirent.d_name);
513 path = strdup(script_path);
514 free(__script_root);
515 break;
516 }
517 free(__script_root);
518 }
519 }
520
521 return path;
522}
523
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200524static const char * const trace_usage[] = {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200525 "perf trace [<options>] <command>",
526 NULL
527};
528
529static const struct option options[] = {
530 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
531 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000532 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200533 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600534 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400535 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600536 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
537 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600538 OPT_CALLBACK('s', "script", NULL, "name",
539 "script file name (lang:script name, script name, or *)",
540 parse_scriptname),
541 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
542 "generate perf-trace.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900543 OPT_STRING('i', "input", &input_name, "file",
544 "input file name"),
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200545 OPT_BOOLEAN('d', "debug-ordering", &debug_ordering,
546 "check that samples time ordering is monotonic"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600547
Masami Hiramatsu19096292009-08-21 14:56:03 -0400548 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200549};
550
551int cmd_trace(int argc, const char **argv, const char *prefix __used)
552{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200553 struct perf_session *session;
Tom Zanussi38752942009-12-15 02:53:39 -0600554 const char *suffix = NULL;
555 const char **__argv;
556 char *script_path;
557 int i, err;
558
559 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
560 if (argc < 3) {
561 fprintf(stderr,
562 "Please specify a record script\n");
563 return -1;
564 }
565 suffix = RECORD_SUFFIX;
566 }
567
568 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
569 if (argc < 3) {
570 fprintf(stderr,
571 "Please specify a report script\n");
572 return -1;
573 }
574 suffix = REPORT_SUFFIX;
575 }
576
Tom Zanussia0cccc22010-04-01 23:59:25 -0500577 if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
578 char *record_script_path, *report_script_path;
579 int live_pipe[2];
580 pid_t pid;
581
582 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
583 if (!record_script_path) {
584 fprintf(stderr, "record script not found\n");
585 return -1;
586 }
587
588 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
589 if (!report_script_path) {
590 fprintf(stderr, "report script not found\n");
591 return -1;
592 }
593
594 if (pipe(live_pipe) < 0) {
595 perror("failed to create pipe");
596 exit(-1);
597 }
598
599 pid = fork();
600 if (pid < 0) {
601 perror("failed to fork");
602 exit(-1);
603 }
604
605 if (!pid) {
606 dup2(live_pipe[1], 1);
607 close(live_pipe[0]);
608
609 __argv = malloc(5 * sizeof(const char *));
610 __argv[0] = "/bin/sh";
611 __argv[1] = record_script_path;
612 __argv[2] = "-o";
613 __argv[3] = "-";
614 __argv[4] = NULL;
615
616 execvp("/bin/sh", (char **)__argv);
617 exit(-1);
618 }
619
620 dup2(live_pipe[0], 0);
621 close(live_pipe[1]);
622
623 __argv = malloc((argc + 3) * sizeof(const char *));
624 __argv[0] = "/bin/sh";
625 __argv[1] = report_script_path;
626 for (i = 2; i < argc; i++)
627 __argv[i] = argv[i];
628 __argv[i++] = "-i";
629 __argv[i++] = "-";
630 __argv[i++] = NULL;
631
632 execvp("/bin/sh", (char **)__argv);
633 exit(-1);
634 }
635
Tom Zanussi38752942009-12-15 02:53:39 -0600636 if (suffix) {
637 script_path = get_script_path(argv[2], suffix);
638 if (!script_path) {
639 fprintf(stderr, "script not found\n");
640 return -1;
641 }
642
643 __argv = malloc((argc + 1) * sizeof(const char *));
644 __argv[0] = "/bin/sh";
645 __argv[1] = script_path;
646 for (i = 3; i < argc; i++)
647 __argv[i - 1] = argv[i];
648 __argv[argc - 1] = NULL;
649
650 execvp("/bin/sh", (char **)__argv);
651 exit(-1);
652 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600653
Tom Zanussi956ffd02009-11-25 01:15:46 -0600654 setup_scripting();
655
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200656 argc = parse_options(argc, argv, options, trace_usage,
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600657 PARSE_OPT_STOP_AT_NON_OPTION);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200658
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200659 if (symbol__init() < 0)
660 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600661 if (!script_name)
662 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200663
Tom Zanussi454c4072010-05-01 01:41:20 -0500664 session = perf_session__new(input_name, O_RDONLY, 0, false);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200665 if (session == NULL)
666 return -ENOMEM;
667
Tom Zanussic239da32010-04-01 23:59:18 -0500668 if (strcmp(input_name, "-") &&
669 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200670 return -EINVAL;
671
Tom Zanussi956ffd02009-11-25 01:15:46 -0600672 if (generate_script_lang) {
673 struct stat perf_stat;
674
675 int input = open(input_name, O_RDONLY);
676 if (input < 0) {
677 perror("failed to open file");
678 exit(-1);
679 }
680
681 err = fstat(input, &perf_stat);
682 if (err < 0) {
683 perror("failed to stat file");
684 exit(-1);
685 }
686
687 if (!perf_stat.st_size) {
688 fprintf(stderr, "zero-sized file, nothing to do!\n");
689 exit(0);
690 }
691
692 scripting_ops = script_spec__lookup(generate_script_lang);
693 if (!scripting_ops) {
694 fprintf(stderr, "invalid language specifier");
695 return -1;
696 }
697
Tom Zanussi956ffd02009-11-25 01:15:46 -0600698 err = scripting_ops->generate_script("perf-trace");
699 goto out;
700 }
701
702 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600703 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600704 if (err)
705 goto out;
706 }
707
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200708 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600709
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200710 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600711 cleanup_scripting();
712out:
713 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200714}