blob: 1d034f6fa28a6fafc151468be80cfa8178105f01 [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;
14
Tom Zanussi586bc5c2009-12-15 02:53:35 -060015static int default_start_script(const char *script __unused,
16 int argc __unused,
17 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060018{
19 return 0;
20}
21
22static int default_stop_script(void)
23{
24 return 0;
25}
26
Tom Zanussi586bc5c2009-12-15 02:53:35 -060027static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060028{
29 return 0;
30}
31
32static struct scripting_ops default_scripting_ops = {
33 .start_script = default_start_script,
34 .stop_script = default_stop_script,
35 .process_event = print_event,
36 .generate_script = default_generate_script,
37};
38
39static struct scripting_ops *scripting_ops;
40
41static void setup_scripting(void)
42{
43 /* make sure PERF_EXEC_PATH is set for scripts */
44 perf_set_argv_exec_path(perf_exec_path());
45
Tom Zanussi16c632d2009-11-25 01:15:48 -060046 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060047 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -060048
Tom Zanussi956ffd02009-11-25 01:15:46 -060049 scripting_ops = &default_scripting_ops;
50}
51
52static int cleanup_scripting(void)
53{
54 return scripting_ops->stop_script();
55}
56
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020057#include "util/parse-options.h"
58
59#include "perf.h"
60#include "util/debug.h"
61
62#include "util/trace-event.h"
Tom Zanussi956ffd02009-11-25 01:15:46 -060063#include "util/exec_cmd.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020064
Tom Zanussi956ffd02009-11-25 01:15:46 -060065static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020066
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020067static int process_sample_event(event_t *event, struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020068{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090069 struct sample_data data;
70 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020071
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090072 memset(&data, 0, sizeof(data));
73 data.time = -1;
74 data.cpu = -1;
75 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020076
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020077 event__parse_sample(event, session->sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020078
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -020079 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
80 data.pid, data.tid, data.ip, data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020081
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020082 thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020084 pr_debug("problem processing %d event, skipping it.\n",
85 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020086 return -1;
87 }
88
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020089 if (session->sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020090 /*
91 * FIXME: better resolve from pid from the struct trace_entry
92 * field, although it should be the same than this perf
93 * event pid
94 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090095 scripting_ops->process_event(data.cpu, data.raw_data,
96 data.raw_size,
97 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020098 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020099
Arnaldo Carvalho de Melof823e442009-12-14 15:06:01 -0200100 session->events_stats.total += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200101 return 0;
102}
103
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200104static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200105 .sample = process_sample_event,
106 .comm = event__process_comm,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500107 .attr = event__process_attr,
Tom Zanussicd19a032010-04-01 23:59:20 -0500108 .event_type = event__process_event_type,
Tom Zanussi92155452010-04-01 23:59:21 -0500109 .tracing_data = event__process_tracing_data,
Tom Zanussic7929e42010-04-01 23:59:22 -0500110 .build_id = event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200111 .ordered_samples = 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
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200121static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200122{
Tom Zanussic239da32010-04-01 23:59:18 -0500123 signal(SIGINT, sig_handler);
124
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200125 return perf_session__process_events(session, &event_ops);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200126}
127
Tom Zanussi956ffd02009-11-25 01:15:46 -0600128struct script_spec {
129 struct list_head node;
130 struct scripting_ops *ops;
131 char spec[0];
132};
133
134LIST_HEAD(script_specs);
135
136static struct script_spec *script_spec__new(const char *spec,
137 struct scripting_ops *ops)
138{
139 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
140
141 if (s != NULL) {
142 strcpy(s->spec, spec);
143 s->ops = ops;
144 }
145
146 return s;
147}
148
149static void script_spec__delete(struct script_spec *s)
150{
151 free(s->spec);
152 free(s);
153}
154
155static void script_spec__add(struct script_spec *s)
156{
157 list_add_tail(&s->node, &script_specs);
158}
159
160static struct script_spec *script_spec__find(const char *spec)
161{
162 struct script_spec *s;
163
164 list_for_each_entry(s, &script_specs, node)
165 if (strcasecmp(s->spec, spec) == 0)
166 return s;
167 return NULL;
168}
169
170static struct script_spec *script_spec__findnew(const char *spec,
171 struct scripting_ops *ops)
172{
173 struct script_spec *s = script_spec__find(spec);
174
175 if (s)
176 return s;
177
178 s = script_spec__new(spec, ops);
179 if (!s)
180 goto out_delete_spec;
181
182 script_spec__add(s);
183
184 return s;
185
186out_delete_spec:
187 script_spec__delete(s);
188
189 return NULL;
190}
191
192int script_spec_register(const char *spec, struct scripting_ops *ops)
193{
194 struct script_spec *s;
195
196 s = script_spec__find(spec);
197 if (s)
198 return -1;
199
200 s = script_spec__findnew(spec, ops);
201 if (!s)
202 return -1;
203
204 return 0;
205}
206
207static struct scripting_ops *script_spec__lookup(const char *spec)
208{
209 struct script_spec *s = script_spec__find(spec);
210 if (!s)
211 return NULL;
212
213 return s->ops;
214}
215
216static void list_available_languages(void)
217{
218 struct script_spec *s;
219
220 fprintf(stderr, "\n");
221 fprintf(stderr, "Scripting language extensions (used in "
222 "perf trace -s [spec:]script.[spec]):\n\n");
223
224 list_for_each_entry(s, &script_specs, node)
225 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
226
227 fprintf(stderr, "\n");
228}
229
230static int parse_scriptname(const struct option *opt __used,
231 const char *str, int unset __used)
232{
233 char spec[PATH_MAX];
234 const char *script, *ext;
235 int len;
236
Tom Zanussif526d682010-01-27 02:27:52 -0600237 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600238 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600239 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600240 }
241
242 script = strchr(str, ':');
243 if (script) {
244 len = script - str;
245 if (len >= PATH_MAX) {
246 fprintf(stderr, "invalid language specifier");
247 return -1;
248 }
249 strncpy(spec, str, len);
250 spec[len] = '\0';
251 scripting_ops = script_spec__lookup(spec);
252 if (!scripting_ops) {
253 fprintf(stderr, "invalid language specifier");
254 return -1;
255 }
256 script++;
257 } else {
258 script = str;
259 ext = strchr(script, '.');
260 if (!ext) {
261 fprintf(stderr, "invalid script extension");
262 return -1;
263 }
264 scripting_ops = script_spec__lookup(++ext);
265 if (!scripting_ops) {
266 fprintf(stderr, "invalid script extension");
267 return -1;
268 }
269 }
270
271 script_name = strdup(script);
272
273 return 0;
274}
275
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600276#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
277 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
278 lang_next) \
279 if (lang_dirent.d_type == DT_DIR && \
280 (strcmp(lang_dirent.d_name, ".")) && \
281 (strcmp(lang_dirent.d_name, "..")))
282
283#define for_each_script(lang_dir, script_dirent, script_next) \
284 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
285 script_next) \
286 if (script_dirent.d_type != DT_DIR)
287
288
289#define RECORD_SUFFIX "-record"
290#define REPORT_SUFFIX "-report"
291
292struct script_desc {
293 struct list_head node;
294 char *name;
295 char *half_liner;
296 char *args;
297};
298
299LIST_HEAD(script_descs);
300
301static struct script_desc *script_desc__new(const char *name)
302{
303 struct script_desc *s = zalloc(sizeof(*s));
304
305 if (s != NULL)
306 s->name = strdup(name);
307
308 return s;
309}
310
311static void script_desc__delete(struct script_desc *s)
312{
313 free(s->name);
314 free(s);
315}
316
317static void script_desc__add(struct script_desc *s)
318{
319 list_add_tail(&s->node, &script_descs);
320}
321
322static struct script_desc *script_desc__find(const char *name)
323{
324 struct script_desc *s;
325
326 list_for_each_entry(s, &script_descs, node)
327 if (strcasecmp(s->name, name) == 0)
328 return s;
329 return NULL;
330}
331
332static struct script_desc *script_desc__findnew(const char *name)
333{
334 struct script_desc *s = script_desc__find(name);
335
336 if (s)
337 return s;
338
339 s = script_desc__new(name);
340 if (!s)
341 goto out_delete_desc;
342
343 script_desc__add(s);
344
345 return s;
346
347out_delete_desc:
348 script_desc__delete(s);
349
350 return NULL;
351}
352
353static char *ends_with(char *str, const char *suffix)
354{
355 size_t suffix_len = strlen(suffix);
356 char *p = str;
357
358 if (strlen(str) > suffix_len) {
359 p = str + strlen(str) - suffix_len;
360 if (!strncmp(p, suffix, suffix_len))
361 return p;
362 }
363
364 return NULL;
365}
366
367static char *ltrim(char *str)
368{
369 int len = strlen(str);
370
371 while (len && isspace(*str)) {
372 len--;
373 str++;
374 }
375
376 return str;
377}
378
379static int read_script_info(struct script_desc *desc, const char *filename)
380{
381 char line[BUFSIZ], *p;
382 FILE *fp;
383
384 fp = fopen(filename, "r");
385 if (!fp)
386 return -1;
387
388 while (fgets(line, sizeof(line), fp)) {
389 p = ltrim(line);
390 if (strlen(p) == 0)
391 continue;
392 if (*p != '#')
393 continue;
394 p++;
395 if (strlen(p) && *p == '!')
396 continue;
397
398 p = ltrim(p);
399 if (strlen(p) && p[strlen(p) - 1] == '\n')
400 p[strlen(p) - 1] = '\0';
401
402 if (!strncmp(p, "description:", strlen("description:"))) {
403 p += strlen("description:");
404 desc->half_liner = strdup(ltrim(p));
405 continue;
406 }
407
408 if (!strncmp(p, "args:", strlen("args:"))) {
409 p += strlen("args:");
410 desc->args = strdup(ltrim(p));
411 continue;
412 }
413 }
414
415 fclose(fp);
416
417 return 0;
418}
419
420static int list_available_scripts(const struct option *opt __used,
421 const char *s __used, int unset __used)
422{
423 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
424 char scripts_path[MAXPATHLEN];
425 DIR *scripts_dir, *lang_dir;
426 char script_path[MAXPATHLEN];
427 char lang_path[MAXPATHLEN];
428 struct script_desc *desc;
429 char first_half[BUFSIZ];
430 char *script_root;
431 char *str;
432
433 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
434
435 scripts_dir = opendir(scripts_path);
436 if (!scripts_dir)
437 return -1;
438
439 for_each_lang(scripts_dir, lang_dirent, lang_next) {
440 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
441 lang_dirent.d_name);
442 lang_dir = opendir(lang_path);
443 if (!lang_dir)
444 continue;
445
446 for_each_script(lang_dir, script_dirent, script_next) {
447 script_root = strdup(script_dirent.d_name);
448 str = ends_with(script_root, REPORT_SUFFIX);
449 if (str) {
450 *str = '\0';
451 desc = script_desc__findnew(script_root);
452 snprintf(script_path, MAXPATHLEN, "%s/%s",
453 lang_path, script_dirent.d_name);
454 read_script_info(desc, script_path);
455 }
456 free(script_root);
457 }
458 }
459
460 fprintf(stdout, "List of available trace scripts:\n");
461 list_for_each_entry(desc, &script_descs, node) {
462 sprintf(first_half, "%s %s", desc->name,
463 desc->args ? desc->args : "");
464 fprintf(stdout, " %-36s %s\n", first_half,
465 desc->half_liner ? desc->half_liner : "");
466 }
467
468 exit(0);
469}
470
Tom Zanussi38752942009-12-15 02:53:39 -0600471static char *get_script_path(const char *script_root, const char *suffix)
472{
473 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
474 char scripts_path[MAXPATHLEN];
475 char script_path[MAXPATHLEN];
476 DIR *scripts_dir, *lang_dir;
477 char lang_path[MAXPATHLEN];
478 char *str, *__script_root;
479 char *path = NULL;
480
481 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
482
483 scripts_dir = opendir(scripts_path);
484 if (!scripts_dir)
485 return NULL;
486
487 for_each_lang(scripts_dir, lang_dirent, lang_next) {
488 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
489 lang_dirent.d_name);
490 lang_dir = opendir(lang_path);
491 if (!lang_dir)
492 continue;
493
494 for_each_script(lang_dir, script_dirent, script_next) {
495 __script_root = strdup(script_dirent.d_name);
496 str = ends_with(__script_root, suffix);
497 if (str) {
498 *str = '\0';
499 if (strcmp(__script_root, script_root))
500 continue;
501 snprintf(script_path, MAXPATHLEN, "%s/%s",
502 lang_path, script_dirent.d_name);
503 path = strdup(script_path);
504 free(__script_root);
505 break;
506 }
507 free(__script_root);
508 }
509 }
510
511 return path;
512}
513
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200514static const char * const trace_usage[] = {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200515 "perf trace [<options>] <command>",
516 NULL
517};
518
519static const struct option options[] = {
520 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
521 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000522 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200523 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600524 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400525 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600526 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
527 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600528 OPT_CALLBACK('s', "script", NULL, "name",
529 "script file name (lang:script name, script name, or *)",
530 parse_scriptname),
531 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
532 "generate perf-trace.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900533 OPT_STRING('i', "input", &input_name, "file",
534 "input file name"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600535
Masami Hiramatsu19096292009-08-21 14:56:03 -0400536 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200537};
538
539int cmd_trace(int argc, const char **argv, const char *prefix __used)
540{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200541 struct perf_session *session;
Tom Zanussi38752942009-12-15 02:53:39 -0600542 const char *suffix = NULL;
543 const char **__argv;
544 char *script_path;
545 int i, err;
546
547 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
548 if (argc < 3) {
549 fprintf(stderr,
550 "Please specify a record script\n");
551 return -1;
552 }
553 suffix = RECORD_SUFFIX;
554 }
555
556 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
557 if (argc < 3) {
558 fprintf(stderr,
559 "Please specify a report script\n");
560 return -1;
561 }
562 suffix = REPORT_SUFFIX;
563 }
564
Tom Zanussia0cccc22010-04-01 23:59:25 -0500565 if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
566 char *record_script_path, *report_script_path;
567 int live_pipe[2];
568 pid_t pid;
569
570 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
571 if (!record_script_path) {
572 fprintf(stderr, "record script not found\n");
573 return -1;
574 }
575
576 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
577 if (!report_script_path) {
578 fprintf(stderr, "report script not found\n");
579 return -1;
580 }
581
582 if (pipe(live_pipe) < 0) {
583 perror("failed to create pipe");
584 exit(-1);
585 }
586
587 pid = fork();
588 if (pid < 0) {
589 perror("failed to fork");
590 exit(-1);
591 }
592
593 if (!pid) {
594 dup2(live_pipe[1], 1);
595 close(live_pipe[0]);
596
597 __argv = malloc(5 * sizeof(const char *));
598 __argv[0] = "/bin/sh";
599 __argv[1] = record_script_path;
600 __argv[2] = "-o";
601 __argv[3] = "-";
602 __argv[4] = NULL;
603
604 execvp("/bin/sh", (char **)__argv);
605 exit(-1);
606 }
607
608 dup2(live_pipe[0], 0);
609 close(live_pipe[1]);
610
611 __argv = malloc((argc + 3) * sizeof(const char *));
612 __argv[0] = "/bin/sh";
613 __argv[1] = report_script_path;
614 for (i = 2; i < argc; i++)
615 __argv[i] = argv[i];
616 __argv[i++] = "-i";
617 __argv[i++] = "-";
618 __argv[i++] = NULL;
619
620 execvp("/bin/sh", (char **)__argv);
621 exit(-1);
622 }
623
Tom Zanussi38752942009-12-15 02:53:39 -0600624 if (suffix) {
625 script_path = get_script_path(argv[2], suffix);
626 if (!script_path) {
627 fprintf(stderr, "script not found\n");
628 return -1;
629 }
630
631 __argv = malloc((argc + 1) * sizeof(const char *));
632 __argv[0] = "/bin/sh";
633 __argv[1] = script_path;
634 for (i = 3; i < argc; i++)
635 __argv[i - 1] = argv[i];
636 __argv[argc - 1] = NULL;
637
638 execvp("/bin/sh", (char **)__argv);
639 exit(-1);
640 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600641
Tom Zanussi956ffd02009-11-25 01:15:46 -0600642 setup_scripting();
643
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200644 argc = parse_options(argc, argv, options, trace_usage,
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600645 PARSE_OPT_STOP_AT_NON_OPTION);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200646
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200647 if (symbol__init() < 0)
648 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600649 if (!script_name)
650 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200651
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200652 session = perf_session__new(input_name, O_RDONLY, 0);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200653 if (session == NULL)
654 return -ENOMEM;
655
Tom Zanussic239da32010-04-01 23:59:18 -0500656 if (strcmp(input_name, "-") &&
657 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -0200658 return -EINVAL;
659
Tom Zanussi956ffd02009-11-25 01:15:46 -0600660 if (generate_script_lang) {
661 struct stat perf_stat;
662
663 int input = open(input_name, O_RDONLY);
664 if (input < 0) {
665 perror("failed to open file");
666 exit(-1);
667 }
668
669 err = fstat(input, &perf_stat);
670 if (err < 0) {
671 perror("failed to stat file");
672 exit(-1);
673 }
674
675 if (!perf_stat.st_size) {
676 fprintf(stderr, "zero-sized file, nothing to do!\n");
677 exit(0);
678 }
679
680 scripting_ops = script_spec__lookup(generate_script_lang);
681 if (!scripting_ops) {
682 fprintf(stderr, "invalid language specifier");
683 return -1;
684 }
685
Tom Zanussi956ffd02009-11-25 01:15:46 -0600686 err = scripting_ops->generate_script("perf-trace");
687 goto out;
688 }
689
690 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600691 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600692 if (err)
693 goto out;
694 }
695
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200696 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600697
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200698 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600699 cleanup_scripting();
700out:
701 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200702}