blob: 83df8db4f3588a78cea6448b5edab9d0872ac60f [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;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020016static u64 nr_unordered;
Tom Zanussi956ffd02009-11-25 01:15:46 -060017
Tom Zanussi586bc5c2009-12-15 02:53:35 -060018static int default_start_script(const char *script __unused,
19 int argc __unused,
20 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060021{
22 return 0;
23}
24
25static int default_stop_script(void)
26{
27 return 0;
28}
29
Tom Zanussi586bc5c2009-12-15 02:53:35 -060030static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060031{
32 return 0;
33}
34
35static struct scripting_ops default_scripting_ops = {
36 .start_script = default_start_script,
37 .stop_script = default_stop_script,
38 .process_event = print_event,
39 .generate_script = default_generate_script,
40};
41
42static struct scripting_ops *scripting_ops;
43
44static void setup_scripting(void)
45{
46 /* make sure PERF_EXEC_PATH is set for scripts */
47 perf_set_argv_exec_path(perf_exec_path());
48
Tom Zanussi16c632d2009-11-25 01:15:48 -060049 setup_perl_scripting();
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060050 setup_python_scripting();
Tom Zanussi16c632d2009-11-25 01:15:48 -060051
Tom Zanussi956ffd02009-11-25 01:15:46 -060052 scripting_ops = &default_scripting_ops;
53}
54
55static int cleanup_scripting(void)
56{
Tom Zanussi3824a4e2010-05-09 23:46:57 -050057 pr_debug("\nperf trace script stopped\n");
58
Tom Zanussi956ffd02009-11-25 01:15:46 -060059 return scripting_ops->stop_script();
60}
61
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020062#include "util/parse-options.h"
63
64#include "perf.h"
65#include "util/debug.h"
66
67#include "util/trace-event.h"
Tom Zanussi956ffd02009-11-25 01:15:46 -060068#include "util/exec_cmd.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020069
Tom Zanussi956ffd02009-11-25 01:15:46 -060070static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020071
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020072static int process_sample_event(event_t *event, struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020073{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090074 struct sample_data data;
75 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020076
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090077 memset(&data, 0, sizeof(data));
78 data.time = -1;
79 data.cpu = -1;
80 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020081
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020082 event__parse_sample(event, session->sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -020084 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
85 data.pid, data.tid, data.ip, data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020086
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020087 thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020088 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020089 pr_debug("problem processing %d event, skipping it.\n",
90 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020091 return -1;
92 }
93
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020094 if (session->sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020095 if (debug_ordering) {
96 if (data.time < last_timestamp) {
97 pr_err("Samples misordered, previous: %llu "
98 "this: %llu\n", last_timestamp,
99 data.time);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200100 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200101 }
102 last_timestamp = data.time;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200103 return 0;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200104 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200105 /*
106 * FIXME: better resolve from pid from the struct trace_entry
107 * field, although it should be the same than this perf
108 * event pid
109 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900110 scripting_ops->process_event(data.cpu, data.raw_data,
111 data.raw_size,
112 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200113 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200114
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300115 session->hists.stats.total_period += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200116 return 0;
117}
118
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200119static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200120 .sample = process_sample_event,
121 .comm = event__process_comm,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500122 .attr = event__process_attr,
Tom Zanussicd19a032010-04-01 23:59:20 -0500123 .event_type = event__process_event_type,
Tom Zanussi92155452010-04-01 23:59:21 -0500124 .tracing_data = event__process_tracing_data,
Tom Zanussic7929e42010-04-01 23:59:22 -0500125 .build_id = event__process_build_id,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200126 .ordered_samples = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200127};
128
Tom Zanussic239da32010-04-01 23:59:18 -0500129extern volatile int session_done;
130
131static void sig_handler(int sig __unused)
132{
133 session_done = 1;
134}
135
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200136static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200137{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200138 int ret;
139
Tom Zanussic239da32010-04-01 23:59:18 -0500140 signal(SIGINT, sig_handler);
141
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200142 ret = perf_session__process_events(session, &event_ops);
143
144 if (debug_ordering)
145 pr_err("Misordered timestamps: %llu\n", nr_unordered);
146
147 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200148}
149
Tom Zanussi956ffd02009-11-25 01:15:46 -0600150struct script_spec {
151 struct list_head node;
152 struct scripting_ops *ops;
153 char spec[0];
154};
155
156LIST_HEAD(script_specs);
157
158static struct script_spec *script_spec__new(const char *spec,
159 struct scripting_ops *ops)
160{
161 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
162
163 if (s != NULL) {
164 strcpy(s->spec, spec);
165 s->ops = ops;
166 }
167
168 return s;
169}
170
171static void script_spec__delete(struct script_spec *s)
172{
173 free(s->spec);
174 free(s);
175}
176
177static void script_spec__add(struct script_spec *s)
178{
179 list_add_tail(&s->node, &script_specs);
180}
181
182static struct script_spec *script_spec__find(const char *spec)
183{
184 struct script_spec *s;
185
186 list_for_each_entry(s, &script_specs, node)
187 if (strcasecmp(s->spec, spec) == 0)
188 return s;
189 return NULL;
190}
191
192static struct script_spec *script_spec__findnew(const char *spec,
193 struct scripting_ops *ops)
194{
195 struct script_spec *s = script_spec__find(spec);
196
197 if (s)
198 return s;
199
200 s = script_spec__new(spec, ops);
201 if (!s)
202 goto out_delete_spec;
203
204 script_spec__add(s);
205
206 return s;
207
208out_delete_spec:
209 script_spec__delete(s);
210
211 return NULL;
212}
213
214int script_spec_register(const char *spec, struct scripting_ops *ops)
215{
216 struct script_spec *s;
217
218 s = script_spec__find(spec);
219 if (s)
220 return -1;
221
222 s = script_spec__findnew(spec, ops);
223 if (!s)
224 return -1;
225
226 return 0;
227}
228
229static struct scripting_ops *script_spec__lookup(const char *spec)
230{
231 struct script_spec *s = script_spec__find(spec);
232 if (!s)
233 return NULL;
234
235 return s->ops;
236}
237
238static void list_available_languages(void)
239{
240 struct script_spec *s;
241
242 fprintf(stderr, "\n");
243 fprintf(stderr, "Scripting language extensions (used in "
244 "perf trace -s [spec:]script.[spec]):\n\n");
245
246 list_for_each_entry(s, &script_specs, node)
247 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
248
249 fprintf(stderr, "\n");
250}
251
252static int parse_scriptname(const struct option *opt __used,
253 const char *str, int unset __used)
254{
255 char spec[PATH_MAX];
256 const char *script, *ext;
257 int len;
258
Tom Zanussif526d682010-01-27 02:27:52 -0600259 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600260 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600261 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600262 }
263
264 script = strchr(str, ':');
265 if (script) {
266 len = script - str;
267 if (len >= PATH_MAX) {
268 fprintf(stderr, "invalid language specifier");
269 return -1;
270 }
271 strncpy(spec, str, len);
272 spec[len] = '\0';
273 scripting_ops = script_spec__lookup(spec);
274 if (!scripting_ops) {
275 fprintf(stderr, "invalid language specifier");
276 return -1;
277 }
278 script++;
279 } else {
280 script = str;
281 ext = strchr(script, '.');
282 if (!ext) {
283 fprintf(stderr, "invalid script extension");
284 return -1;
285 }
286 scripting_ops = script_spec__lookup(++ext);
287 if (!scripting_ops) {
288 fprintf(stderr, "invalid script extension");
289 return -1;
290 }
291 }
292
293 script_name = strdup(script);
294
295 return 0;
296}
297
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600298#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
299 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
300 lang_next) \
301 if (lang_dirent.d_type == DT_DIR && \
302 (strcmp(lang_dirent.d_name, ".")) && \
303 (strcmp(lang_dirent.d_name, "..")))
304
305#define for_each_script(lang_dir, script_dirent, script_next) \
306 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
307 script_next) \
308 if (script_dirent.d_type != DT_DIR)
309
310
311#define RECORD_SUFFIX "-record"
312#define REPORT_SUFFIX "-report"
313
314struct script_desc {
315 struct list_head node;
316 char *name;
317 char *half_liner;
318 char *args;
319};
320
321LIST_HEAD(script_descs);
322
323static struct script_desc *script_desc__new(const char *name)
324{
325 struct script_desc *s = zalloc(sizeof(*s));
326
327 if (s != NULL)
328 s->name = strdup(name);
329
330 return s;
331}
332
333static void script_desc__delete(struct script_desc *s)
334{
335 free(s->name);
336 free(s);
337}
338
339static void script_desc__add(struct script_desc *s)
340{
341 list_add_tail(&s->node, &script_descs);
342}
343
344static struct script_desc *script_desc__find(const char *name)
345{
346 struct script_desc *s;
347
348 list_for_each_entry(s, &script_descs, node)
349 if (strcasecmp(s->name, name) == 0)
350 return s;
351 return NULL;
352}
353
354static struct script_desc *script_desc__findnew(const char *name)
355{
356 struct script_desc *s = script_desc__find(name);
357
358 if (s)
359 return s;
360
361 s = script_desc__new(name);
362 if (!s)
363 goto out_delete_desc;
364
365 script_desc__add(s);
366
367 return s;
368
369out_delete_desc:
370 script_desc__delete(s);
371
372 return NULL;
373}
374
375static char *ends_with(char *str, const char *suffix)
376{
377 size_t suffix_len = strlen(suffix);
378 char *p = str;
379
380 if (strlen(str) > suffix_len) {
381 p = str + strlen(str) - suffix_len;
382 if (!strncmp(p, suffix, suffix_len))
383 return p;
384 }
385
386 return NULL;
387}
388
389static char *ltrim(char *str)
390{
391 int len = strlen(str);
392
393 while (len && isspace(*str)) {
394 len--;
395 str++;
396 }
397
398 return str;
399}
400
401static int read_script_info(struct script_desc *desc, const char *filename)
402{
403 char line[BUFSIZ], *p;
404 FILE *fp;
405
406 fp = fopen(filename, "r");
407 if (!fp)
408 return -1;
409
410 while (fgets(line, sizeof(line), fp)) {
411 p = ltrim(line);
412 if (strlen(p) == 0)
413 continue;
414 if (*p != '#')
415 continue;
416 p++;
417 if (strlen(p) && *p == '!')
418 continue;
419
420 p = ltrim(p);
421 if (strlen(p) && p[strlen(p) - 1] == '\n')
422 p[strlen(p) - 1] = '\0';
423
424 if (!strncmp(p, "description:", strlen("description:"))) {
425 p += strlen("description:");
426 desc->half_liner = strdup(ltrim(p));
427 continue;
428 }
429
430 if (!strncmp(p, "args:", strlen("args:"))) {
431 p += strlen("args:");
432 desc->args = strdup(ltrim(p));
433 continue;
434 }
435 }
436
437 fclose(fp);
438
439 return 0;
440}
441
442static int list_available_scripts(const struct option *opt __used,
443 const char *s __used, int unset __used)
444{
445 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
446 char scripts_path[MAXPATHLEN];
447 DIR *scripts_dir, *lang_dir;
448 char script_path[MAXPATHLEN];
449 char lang_path[MAXPATHLEN];
450 struct script_desc *desc;
451 char first_half[BUFSIZ];
452 char *script_root;
453 char *str;
454
455 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
456
457 scripts_dir = opendir(scripts_path);
458 if (!scripts_dir)
459 return -1;
460
461 for_each_lang(scripts_dir, lang_dirent, lang_next) {
462 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
463 lang_dirent.d_name);
464 lang_dir = opendir(lang_path);
465 if (!lang_dir)
466 continue;
467
468 for_each_script(lang_dir, script_dirent, script_next) {
469 script_root = strdup(script_dirent.d_name);
470 str = ends_with(script_root, REPORT_SUFFIX);
471 if (str) {
472 *str = '\0';
473 desc = script_desc__findnew(script_root);
474 snprintf(script_path, MAXPATHLEN, "%s/%s",
475 lang_path, script_dirent.d_name);
476 read_script_info(desc, script_path);
477 }
478 free(script_root);
479 }
480 }
481
482 fprintf(stdout, "List of available trace scripts:\n");
483 list_for_each_entry(desc, &script_descs, node) {
484 sprintf(first_half, "%s %s", desc->name,
485 desc->args ? desc->args : "");
486 fprintf(stdout, " %-36s %s\n", first_half,
487 desc->half_liner ? desc->half_liner : "");
488 }
489
490 exit(0);
491}
492
Tom Zanussi38752942009-12-15 02:53:39 -0600493static char *get_script_path(const char *script_root, const char *suffix)
494{
495 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
496 char scripts_path[MAXPATHLEN];
497 char script_path[MAXPATHLEN];
498 DIR *scripts_dir, *lang_dir;
499 char lang_path[MAXPATHLEN];
500 char *str, *__script_root;
501 char *path = NULL;
502
503 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
504
505 scripts_dir = opendir(scripts_path);
506 if (!scripts_dir)
507 return NULL;
508
509 for_each_lang(scripts_dir, lang_dirent, lang_next) {
510 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
511 lang_dirent.d_name);
512 lang_dir = opendir(lang_path);
513 if (!lang_dir)
514 continue;
515
516 for_each_script(lang_dir, script_dirent, script_next) {
517 __script_root = strdup(script_dirent.d_name);
518 str = ends_with(__script_root, suffix);
519 if (str) {
520 *str = '\0';
521 if (strcmp(__script_root, script_root))
522 continue;
523 snprintf(script_path, MAXPATHLEN, "%s/%s",
524 lang_path, script_dirent.d_name);
525 path = strdup(script_path);
526 free(__script_root);
527 break;
528 }
529 free(__script_root);
530 }
531 }
532
533 return path;
534}
535
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200536static const char * const trace_usage[] = {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200537 "perf trace [<options>] <command>",
538 NULL
539};
540
541static const struct option options[] = {
542 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
543 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000544 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200545 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600546 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400547 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600548 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
549 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600550 OPT_CALLBACK('s', "script", NULL, "name",
551 "script file name (lang:script name, script name, or *)",
552 parse_scriptname),
553 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
554 "generate perf-trace.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900555 OPT_STRING('i', "input", &input_name, "file",
556 "input file name"),
Frederic Weisbeckere1889d72010-04-24 01:55:09 +0200557 OPT_BOOLEAN('d', "debug-ordering", &debug_ordering,
558 "check that samples time ordering is monotonic"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600559
Masami Hiramatsu19096292009-08-21 14:56:03 -0400560 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200561};
562
563int cmd_trace(int argc, const char **argv, const char *prefix __used)
564{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200565 struct perf_session *session;
Tom Zanussi38752942009-12-15 02:53:39 -0600566 const char *suffix = NULL;
567 const char **__argv;
568 char *script_path;
569 int i, err;
570
571 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
572 if (argc < 3) {
573 fprintf(stderr,
574 "Please specify a record script\n");
575 return -1;
576 }
577 suffix = RECORD_SUFFIX;
578 }
579
580 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
581 if (argc < 3) {
582 fprintf(stderr,
583 "Please specify a report script\n");
584 return -1;
585 }
586 suffix = REPORT_SUFFIX;
587 }
588
Tom Zanussia0cccc22010-04-01 23:59:25 -0500589 if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
590 char *record_script_path, *report_script_path;
591 int live_pipe[2];
592 pid_t pid;
593
594 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
595 if (!record_script_path) {
596 fprintf(stderr, "record script not found\n");
597 return -1;
598 }
599
600 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
601 if (!report_script_path) {
602 fprintf(stderr, "report script not found\n");
603 return -1;
604 }
605
606 if (pipe(live_pipe) < 0) {
607 perror("failed to create pipe");
608 exit(-1);
609 }
610
611 pid = fork();
612 if (pid < 0) {
613 perror("failed to fork");
614 exit(-1);
615 }
616
617 if (!pid) {
618 dup2(live_pipe[1], 1);
619 close(live_pipe[0]);
620
621 __argv = malloc(5 * sizeof(const char *));
622 __argv[0] = "/bin/sh";
623 __argv[1] = record_script_path;
624 __argv[2] = "-o";
625 __argv[3] = "-";
626 __argv[4] = NULL;
627
628 execvp("/bin/sh", (char **)__argv);
629 exit(-1);
630 }
631
632 dup2(live_pipe[0], 0);
633 close(live_pipe[1]);
634
635 __argv = malloc((argc + 3) * sizeof(const char *));
636 __argv[0] = "/bin/sh";
637 __argv[1] = report_script_path;
638 for (i = 2; i < argc; i++)
639 __argv[i] = argv[i];
640 __argv[i++] = "-i";
641 __argv[i++] = "-";
642 __argv[i++] = NULL;
643
644 execvp("/bin/sh", (char **)__argv);
645 exit(-1);
646 }
647
Tom Zanussi38752942009-12-15 02:53:39 -0600648 if (suffix) {
649 script_path = get_script_path(argv[2], suffix);
650 if (!script_path) {
651 fprintf(stderr, "script not found\n");
652 return -1;
653 }
654
655 __argv = malloc((argc + 1) * sizeof(const char *));
656 __argv[0] = "/bin/sh";
657 __argv[1] = script_path;
658 for (i = 3; i < argc; i++)
659 __argv[i - 1] = argv[i];
660 __argv[argc - 1] = NULL;
661
662 execvp("/bin/sh", (char **)__argv);
663 exit(-1);
664 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600665
Tom Zanussi956ffd02009-11-25 01:15:46 -0600666 setup_scripting();
667
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200668 argc = parse_options(argc, argv, options, trace_usage,
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600669 PARSE_OPT_STOP_AT_NON_OPTION);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200670
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200671 if (symbol__init() < 0)
672 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600673 if (!script_name)
674 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200675
Tom Zanussi454c4072010-05-01 01:41:20 -0500676 session = perf_session__new(input_name, O_RDONLY, 0, false);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200677 if (session == NULL)
678 return -ENOMEM;
679
Tom Zanussic239da32010-04-01 23:59:18 -0500680 if (strcmp(input_name, "-") &&
681 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200682 return -EINVAL;
683
Tom Zanussi956ffd02009-11-25 01:15:46 -0600684 if (generate_script_lang) {
685 struct stat perf_stat;
686
687 int input = open(input_name, O_RDONLY);
688 if (input < 0) {
689 perror("failed to open file");
690 exit(-1);
691 }
692
693 err = fstat(input, &perf_stat);
694 if (err < 0) {
695 perror("failed to stat file");
696 exit(-1);
697 }
698
699 if (!perf_stat.st_size) {
700 fprintf(stderr, "zero-sized file, nothing to do!\n");
701 exit(0);
702 }
703
704 scripting_ops = script_spec__lookup(generate_script_lang);
705 if (!scripting_ops) {
706 fprintf(stderr, "invalid language specifier");
707 return -1;
708 }
709
Tom Zanussi956ffd02009-11-25 01:15:46 -0600710 err = scripting_ops->generate_script("perf-trace");
711 goto out;
712 }
713
714 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600715 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600716 if (err)
717 goto out;
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500718 pr_debug("perf trace started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600719 }
720
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200721 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600722
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200723 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600724 cleanup_scripting();
725out:
726 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200727}