blob: 294da725a57d12867788baa9c5ea98c410486cdf [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 Weisbeckerffabd992010-05-27 16:27:47 +020014static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020015static 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 Weisbeckerffabd992010-05-27 16:27:47 +020095 if (debug_mode) {
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020096 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
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200119static u64 nr_lost;
120
121static int process_lost_event(event_t *event, struct perf_session *session __used)
122{
123 nr_lost += event->lost.lost;
124
125 return 0;
126}
127
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200128static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa640f2009-12-27 21:37:05 -0200129 .sample = process_sample_event,
130 .comm = event__process_comm,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500131 .attr = event__process_attr,
Tom Zanussicd19a032010-04-01 23:59:20 -0500132 .event_type = event__process_event_type,
Tom Zanussi92155452010-04-01 23:59:21 -0500133 .tracing_data = event__process_tracing_data,
Tom Zanussic7929e42010-04-01 23:59:22 -0500134 .build_id = event__process_build_id,
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200135 .lost = process_lost_event,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200136 .ordered_samples = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200137};
138
Tom Zanussic239da32010-04-01 23:59:18 -0500139extern volatile int session_done;
140
141static void sig_handler(int sig __unused)
142{
143 session_done = 1;
144}
145
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200146static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200147{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200148 int ret;
149
Tom Zanussic239da32010-04-01 23:59:18 -0500150 signal(SIGINT, sig_handler);
151
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200152 ret = perf_session__process_events(session, &event_ops);
153
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200154 if (debug_mode) {
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200155 pr_err("Misordered timestamps: %llu\n", nr_unordered);
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200156 pr_err("Lost events: %llu\n", nr_lost);
157 }
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200158
159 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200160}
161
Tom Zanussi956ffd02009-11-25 01:15:46 -0600162struct script_spec {
163 struct list_head node;
164 struct scripting_ops *ops;
165 char spec[0];
166};
167
168LIST_HEAD(script_specs);
169
170static struct script_spec *script_spec__new(const char *spec,
171 struct scripting_ops *ops)
172{
173 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
174
175 if (s != NULL) {
176 strcpy(s->spec, spec);
177 s->ops = ops;
178 }
179
180 return s;
181}
182
183static void script_spec__delete(struct script_spec *s)
184{
185 free(s->spec);
186 free(s);
187}
188
189static void script_spec__add(struct script_spec *s)
190{
191 list_add_tail(&s->node, &script_specs);
192}
193
194static struct script_spec *script_spec__find(const char *spec)
195{
196 struct script_spec *s;
197
198 list_for_each_entry(s, &script_specs, node)
199 if (strcasecmp(s->spec, spec) == 0)
200 return s;
201 return NULL;
202}
203
204static struct script_spec *script_spec__findnew(const char *spec,
205 struct scripting_ops *ops)
206{
207 struct script_spec *s = script_spec__find(spec);
208
209 if (s)
210 return s;
211
212 s = script_spec__new(spec, ops);
213 if (!s)
214 goto out_delete_spec;
215
216 script_spec__add(s);
217
218 return s;
219
220out_delete_spec:
221 script_spec__delete(s);
222
223 return NULL;
224}
225
226int script_spec_register(const char *spec, struct scripting_ops *ops)
227{
228 struct script_spec *s;
229
230 s = script_spec__find(spec);
231 if (s)
232 return -1;
233
234 s = script_spec__findnew(spec, ops);
235 if (!s)
236 return -1;
237
238 return 0;
239}
240
241static struct scripting_ops *script_spec__lookup(const char *spec)
242{
243 struct script_spec *s = script_spec__find(spec);
244 if (!s)
245 return NULL;
246
247 return s->ops;
248}
249
250static void list_available_languages(void)
251{
252 struct script_spec *s;
253
254 fprintf(stderr, "\n");
255 fprintf(stderr, "Scripting language extensions (used in "
256 "perf trace -s [spec:]script.[spec]):\n\n");
257
258 list_for_each_entry(s, &script_specs, node)
259 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
260
261 fprintf(stderr, "\n");
262}
263
264static int parse_scriptname(const struct option *opt __used,
265 const char *str, int unset __used)
266{
267 char spec[PATH_MAX];
268 const char *script, *ext;
269 int len;
270
Tom Zanussif526d682010-01-27 02:27:52 -0600271 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600272 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600273 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600274 }
275
276 script = strchr(str, ':');
277 if (script) {
278 len = script - str;
279 if (len >= PATH_MAX) {
280 fprintf(stderr, "invalid language specifier");
281 return -1;
282 }
283 strncpy(spec, str, len);
284 spec[len] = '\0';
285 scripting_ops = script_spec__lookup(spec);
286 if (!scripting_ops) {
287 fprintf(stderr, "invalid language specifier");
288 return -1;
289 }
290 script++;
291 } else {
292 script = str;
293 ext = strchr(script, '.');
294 if (!ext) {
295 fprintf(stderr, "invalid script extension");
296 return -1;
297 }
298 scripting_ops = script_spec__lookup(++ext);
299 if (!scripting_ops) {
300 fprintf(stderr, "invalid script extension");
301 return -1;
302 }
303 }
304
305 script_name = strdup(script);
306
307 return 0;
308}
309
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600310#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
311 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
312 lang_next) \
313 if (lang_dirent.d_type == DT_DIR && \
314 (strcmp(lang_dirent.d_name, ".")) && \
315 (strcmp(lang_dirent.d_name, "..")))
316
317#define for_each_script(lang_dir, script_dirent, script_next) \
318 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
319 script_next) \
320 if (script_dirent.d_type != DT_DIR)
321
322
323#define RECORD_SUFFIX "-record"
324#define REPORT_SUFFIX "-report"
325
326struct script_desc {
327 struct list_head node;
328 char *name;
329 char *half_liner;
330 char *args;
331};
332
333LIST_HEAD(script_descs);
334
335static struct script_desc *script_desc__new(const char *name)
336{
337 struct script_desc *s = zalloc(sizeof(*s));
338
339 if (s != NULL)
340 s->name = strdup(name);
341
342 return s;
343}
344
345static void script_desc__delete(struct script_desc *s)
346{
347 free(s->name);
348 free(s);
349}
350
351static void script_desc__add(struct script_desc *s)
352{
353 list_add_tail(&s->node, &script_descs);
354}
355
356static struct script_desc *script_desc__find(const char *name)
357{
358 struct script_desc *s;
359
360 list_for_each_entry(s, &script_descs, node)
361 if (strcasecmp(s->name, name) == 0)
362 return s;
363 return NULL;
364}
365
366static struct script_desc *script_desc__findnew(const char *name)
367{
368 struct script_desc *s = script_desc__find(name);
369
370 if (s)
371 return s;
372
373 s = script_desc__new(name);
374 if (!s)
375 goto out_delete_desc;
376
377 script_desc__add(s);
378
379 return s;
380
381out_delete_desc:
382 script_desc__delete(s);
383
384 return NULL;
385}
386
387static char *ends_with(char *str, const char *suffix)
388{
389 size_t suffix_len = strlen(suffix);
390 char *p = str;
391
392 if (strlen(str) > suffix_len) {
393 p = str + strlen(str) - suffix_len;
394 if (!strncmp(p, suffix, suffix_len))
395 return p;
396 }
397
398 return NULL;
399}
400
401static char *ltrim(char *str)
402{
403 int len = strlen(str);
404
405 while (len && isspace(*str)) {
406 len--;
407 str++;
408 }
409
410 return str;
411}
412
413static int read_script_info(struct script_desc *desc, const char *filename)
414{
415 char line[BUFSIZ], *p;
416 FILE *fp;
417
418 fp = fopen(filename, "r");
419 if (!fp)
420 return -1;
421
422 while (fgets(line, sizeof(line), fp)) {
423 p = ltrim(line);
424 if (strlen(p) == 0)
425 continue;
426 if (*p != '#')
427 continue;
428 p++;
429 if (strlen(p) && *p == '!')
430 continue;
431
432 p = ltrim(p);
433 if (strlen(p) && p[strlen(p) - 1] == '\n')
434 p[strlen(p) - 1] = '\0';
435
436 if (!strncmp(p, "description:", strlen("description:"))) {
437 p += strlen("description:");
438 desc->half_liner = strdup(ltrim(p));
439 continue;
440 }
441
442 if (!strncmp(p, "args:", strlen("args:"))) {
443 p += strlen("args:");
444 desc->args = strdup(ltrim(p));
445 continue;
446 }
447 }
448
449 fclose(fp);
450
451 return 0;
452}
453
454static int list_available_scripts(const struct option *opt __used,
455 const char *s __used, int unset __used)
456{
457 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
458 char scripts_path[MAXPATHLEN];
459 DIR *scripts_dir, *lang_dir;
460 char script_path[MAXPATHLEN];
461 char lang_path[MAXPATHLEN];
462 struct script_desc *desc;
463 char first_half[BUFSIZ];
464 char *script_root;
465 char *str;
466
467 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
468
469 scripts_dir = opendir(scripts_path);
470 if (!scripts_dir)
471 return -1;
472
473 for_each_lang(scripts_dir, lang_dirent, lang_next) {
474 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
475 lang_dirent.d_name);
476 lang_dir = opendir(lang_path);
477 if (!lang_dir)
478 continue;
479
480 for_each_script(lang_dir, script_dirent, script_next) {
481 script_root = strdup(script_dirent.d_name);
482 str = ends_with(script_root, REPORT_SUFFIX);
483 if (str) {
484 *str = '\0';
485 desc = script_desc__findnew(script_root);
486 snprintf(script_path, MAXPATHLEN, "%s/%s",
487 lang_path, script_dirent.d_name);
488 read_script_info(desc, script_path);
489 }
490 free(script_root);
491 }
492 }
493
494 fprintf(stdout, "List of available trace scripts:\n");
495 list_for_each_entry(desc, &script_descs, node) {
496 sprintf(first_half, "%s %s", desc->name,
497 desc->args ? desc->args : "");
498 fprintf(stdout, " %-36s %s\n", first_half,
499 desc->half_liner ? desc->half_liner : "");
500 }
501
502 exit(0);
503}
504
Tom Zanussi38752942009-12-15 02:53:39 -0600505static char *get_script_path(const char *script_root, const char *suffix)
506{
507 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
508 char scripts_path[MAXPATHLEN];
509 char script_path[MAXPATHLEN];
510 DIR *scripts_dir, *lang_dir;
511 char lang_path[MAXPATHLEN];
512 char *str, *__script_root;
513 char *path = NULL;
514
515 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
516
517 scripts_dir = opendir(scripts_path);
518 if (!scripts_dir)
519 return NULL;
520
521 for_each_lang(scripts_dir, lang_dirent, lang_next) {
522 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
523 lang_dirent.d_name);
524 lang_dir = opendir(lang_path);
525 if (!lang_dir)
526 continue;
527
528 for_each_script(lang_dir, script_dirent, script_next) {
529 __script_root = strdup(script_dirent.d_name);
530 str = ends_with(__script_root, suffix);
531 if (str) {
532 *str = '\0';
533 if (strcmp(__script_root, script_root))
534 continue;
535 snprintf(script_path, MAXPATHLEN, "%s/%s",
536 lang_path, script_dirent.d_name);
537 path = strdup(script_path);
538 free(__script_root);
539 break;
540 }
541 free(__script_root);
542 }
543 }
544
545 return path;
546}
547
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200548static const char * const trace_usage[] = {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200549 "perf trace [<options>] <command>",
550 NULL
551};
552
553static const struct option options[] = {
554 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
555 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000556 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200557 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600558 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400559 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600560 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
561 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600562 OPT_CALLBACK('s', "script", NULL, "name",
563 "script file name (lang:script name, script name, or *)",
564 parse_scriptname),
565 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
566 "generate perf-trace.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900567 OPT_STRING('i', "input", &input_name, "file",
568 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200569 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
570 "do various checks like samples ordering and lost events"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600571
Masami Hiramatsu19096292009-08-21 14:56:03 -0400572 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200573};
574
575int cmd_trace(int argc, const char **argv, const char *prefix __used)
576{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200577 struct perf_session *session;
Tom Zanussi38752942009-12-15 02:53:39 -0600578 const char *suffix = NULL;
579 const char **__argv;
580 char *script_path;
581 int i, err;
582
583 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
584 if (argc < 3) {
585 fprintf(stderr,
586 "Please specify a record script\n");
587 return -1;
588 }
589 suffix = RECORD_SUFFIX;
590 }
591
592 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
593 if (argc < 3) {
594 fprintf(stderr,
595 "Please specify a report script\n");
596 return -1;
597 }
598 suffix = REPORT_SUFFIX;
599 }
600
Tom Zanussia0cccc22010-04-01 23:59:25 -0500601 if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
602 char *record_script_path, *report_script_path;
603 int live_pipe[2];
604 pid_t pid;
605
606 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
607 if (!record_script_path) {
608 fprintf(stderr, "record script not found\n");
609 return -1;
610 }
611
612 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
613 if (!report_script_path) {
614 fprintf(stderr, "report script not found\n");
615 return -1;
616 }
617
618 if (pipe(live_pipe) < 0) {
619 perror("failed to create pipe");
620 exit(-1);
621 }
622
623 pid = fork();
624 if (pid < 0) {
625 perror("failed to fork");
626 exit(-1);
627 }
628
629 if (!pid) {
630 dup2(live_pipe[1], 1);
631 close(live_pipe[0]);
632
633 __argv = malloc(5 * sizeof(const char *));
634 __argv[0] = "/bin/sh";
635 __argv[1] = record_script_path;
636 __argv[2] = "-o";
637 __argv[3] = "-";
638 __argv[4] = NULL;
639
640 execvp("/bin/sh", (char **)__argv);
641 exit(-1);
642 }
643
644 dup2(live_pipe[0], 0);
645 close(live_pipe[1]);
646
647 __argv = malloc((argc + 3) * sizeof(const char *));
648 __argv[0] = "/bin/sh";
649 __argv[1] = report_script_path;
650 for (i = 2; i < argc; i++)
651 __argv[i] = argv[i];
652 __argv[i++] = "-i";
653 __argv[i++] = "-";
654 __argv[i++] = NULL;
655
656 execvp("/bin/sh", (char **)__argv);
657 exit(-1);
658 }
659
Tom Zanussi38752942009-12-15 02:53:39 -0600660 if (suffix) {
661 script_path = get_script_path(argv[2], suffix);
662 if (!script_path) {
663 fprintf(stderr, "script not found\n");
664 return -1;
665 }
666
667 __argv = malloc((argc + 1) * sizeof(const char *));
668 __argv[0] = "/bin/sh";
669 __argv[1] = script_path;
670 for (i = 3; i < argc; i++)
671 __argv[i - 1] = argv[i];
672 __argv[argc - 1] = NULL;
673
674 execvp("/bin/sh", (char **)__argv);
675 exit(-1);
676 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600677
Tom Zanussi956ffd02009-11-25 01:15:46 -0600678 setup_scripting();
679
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200680 argc = parse_options(argc, argv, options, trace_usage,
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600681 PARSE_OPT_STOP_AT_NON_OPTION);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200682
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200683 if (symbol__init() < 0)
684 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600685 if (!script_name)
686 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200687
Tom Zanussi454c4072010-05-01 01:41:20 -0500688 session = perf_session__new(input_name, O_RDONLY, 0, false);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200689 if (session == NULL)
690 return -ENOMEM;
691
Tom Zanussic239da32010-04-01 23:59:18 -0500692 if (strcmp(input_name, "-") &&
693 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c7692009-12-27 21:37:02 -0200694 return -EINVAL;
695
Tom Zanussi956ffd02009-11-25 01:15:46 -0600696 if (generate_script_lang) {
697 struct stat perf_stat;
698
699 int input = open(input_name, O_RDONLY);
700 if (input < 0) {
701 perror("failed to open file");
702 exit(-1);
703 }
704
705 err = fstat(input, &perf_stat);
706 if (err < 0) {
707 perror("failed to stat file");
708 exit(-1);
709 }
710
711 if (!perf_stat.st_size) {
712 fprintf(stderr, "zero-sized file, nothing to do!\n");
713 exit(0);
714 }
715
716 scripting_ops = script_spec__lookup(generate_script_lang);
717 if (!scripting_ops) {
718 fprintf(stderr, "invalid language specifier");
719 return -1;
720 }
721
Tom Zanussi956ffd02009-11-25 01:15:46 -0600722 err = scripting_ops->generate_script("perf-trace");
723 goto out;
724 }
725
726 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600727 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600728 if (err)
729 goto out;
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500730 pr_debug("perf trace started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600731 }
732
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200733 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600734
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200735 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600736 cleanup_scripting();
737out:
738 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200739}