blob: 368e6249290aeb3b39e1428d9c3def48c8a376c4 [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"
Andrea Gelminib7eead82010-08-05 15:51:38 +020013#include "util/util.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020014
Tom Zanussi956ffd02009-11-25 01:15:46 -060015static char const *script_name;
16static char const *generate_script_lang;
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020017static bool debug_mode;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020018static u64 last_timestamp;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020019static u64 nr_unordered;
Tom Zanussi956ffd02009-11-25 01:15:46 -060020
Tom Zanussi586bc5c2009-12-15 02:53:35 -060021static int default_start_script(const char *script __unused,
22 int argc __unused,
23 const char **argv __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060024{
25 return 0;
26}
27
28static int default_stop_script(void)
29{
30 return 0;
31}
32
Tom Zanussi586bc5c2009-12-15 02:53:35 -060033static int default_generate_script(const char *outfile __unused)
Tom Zanussi956ffd02009-11-25 01:15:46 -060034{
35 return 0;
36}
37
38static struct scripting_ops default_scripting_ops = {
39 .start_script = default_start_script,
40 .stop_script = default_stop_script,
41 .process_event = print_event,
42 .generate_script = default_generate_script,
43};
44
45static struct scripting_ops *scripting_ops;
46
47static void setup_scripting(void)
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
Tom Zanussi956ffd02009-11-25 01:15:46 -060062static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020063
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020064static int process_sample_event(event_t *event, struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020065{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090066 struct sample_data data;
67 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020068
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090069 memset(&data, 0, sizeof(data));
70 data.time = -1;
71 data.cpu = -1;
72 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020073
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020074 event__parse_sample(event, session->sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020075
Arnaldo Carvalho de Melo0d755032010-01-14 12:23:09 -020076 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
77 data.pid, data.tid, data.ip, data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020078
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020079 thread = perf_session__findnew(session, event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020080 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020081 pr_debug("problem processing %d event, skipping it.\n",
82 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083 return -1;
84 }
85
Arnaldo Carvalho de Meloc0198792009-12-14 14:23:00 -020086 if (session->sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbeckerffabd992010-05-27 16:27:47 +020087 if (debug_mode) {
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020088 if (data.time < last_timestamp) {
89 pr_err("Samples misordered, previous: %llu "
90 "this: %llu\n", last_timestamp,
91 data.time);
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020092 nr_unordered++;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020093 }
94 last_timestamp = data.time;
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +020095 return 0;
Frederic Weisbeckere1889d72010-04-24 01:55:09 +020096 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020097 /*
98 * FIXME: better resolve from pid from the struct trace_entry
99 * field, although it should be the same than this perf
100 * event pid
101 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900102 scripting_ops->process_event(data.cpu, data.raw_data,
103 data.raw_size,
104 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200105 }
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200106
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300107 session->hists.stats.total_period += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200108 return 0;
109}
110
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200111static u64 nr_lost;
112
113static int process_lost_event(event_t *event, struct perf_session *session __used)
114{
115 nr_lost += event->lost.lost;
116
117 return 0;
118}
119
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200120static struct perf_event_ops event_ops = {
Arnaldo Carvalho de Melo55aa6402009-12-27 21:37:05 -0200121 .sample = process_sample_event,
122 .comm = event__process_comm,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500123 .attr = event__process_attr,
Tom Zanussicd19a032010-04-01 23:59:20 -0500124 .event_type = event__process_event_type,
Tom Zanussi92155452010-04-01 23:59:21 -0500125 .tracing_data = event__process_tracing_data,
Tom Zanussic7929e42010-04-01 23:59:22 -0500126 .build_id = event__process_build_id,
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200127 .lost = process_lost_event,
Frederic Weisbeckere0a808c2010-04-24 00:38:33 +0200128 .ordered_samples = true,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200129};
130
Tom Zanussic239da32010-04-01 23:59:18 -0500131extern volatile int session_done;
132
133static void sig_handler(int sig __unused)
134{
135 session_done = 1;
136}
137
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200138static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200139{
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200140 int ret;
141
Tom Zanussic239da32010-04-01 23:59:18 -0500142 signal(SIGINT, sig_handler);
143
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200144 ret = perf_session__process_events(session, &event_ops);
145
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200146 if (debug_mode) {
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200147 pr_err("Misordered timestamps: %llu\n", nr_unordered);
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200148 pr_err("Lost events: %llu\n", nr_lost);
149 }
Frederic Weisbecker6fcf7dd2010-05-27 15:46:25 +0200150
151 return ret;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200152}
153
Tom Zanussi956ffd02009-11-25 01:15:46 -0600154struct script_spec {
155 struct list_head node;
156 struct scripting_ops *ops;
157 char spec[0];
158};
159
160LIST_HEAD(script_specs);
161
162static struct script_spec *script_spec__new(const char *spec,
163 struct scripting_ops *ops)
164{
165 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
166
167 if (s != NULL) {
168 strcpy(s->spec, spec);
169 s->ops = ops;
170 }
171
172 return s;
173}
174
175static void script_spec__delete(struct script_spec *s)
176{
177 free(s->spec);
178 free(s);
179}
180
181static void script_spec__add(struct script_spec *s)
182{
183 list_add_tail(&s->node, &script_specs);
184}
185
186static struct script_spec *script_spec__find(const char *spec)
187{
188 struct script_spec *s;
189
190 list_for_each_entry(s, &script_specs, node)
191 if (strcasecmp(s->spec, spec) == 0)
192 return s;
193 return NULL;
194}
195
196static struct script_spec *script_spec__findnew(const char *spec,
197 struct scripting_ops *ops)
198{
199 struct script_spec *s = script_spec__find(spec);
200
201 if (s)
202 return s;
203
204 s = script_spec__new(spec, ops);
205 if (!s)
206 goto out_delete_spec;
207
208 script_spec__add(s);
209
210 return s;
211
212out_delete_spec:
213 script_spec__delete(s);
214
215 return NULL;
216}
217
218int script_spec_register(const char *spec, struct scripting_ops *ops)
219{
220 struct script_spec *s;
221
222 s = script_spec__find(spec);
223 if (s)
224 return -1;
225
226 s = script_spec__findnew(spec, ops);
227 if (!s)
228 return -1;
229
230 return 0;
231}
232
233static struct scripting_ops *script_spec__lookup(const char *spec)
234{
235 struct script_spec *s = script_spec__find(spec);
236 if (!s)
237 return NULL;
238
239 return s->ops;
240}
241
242static void list_available_languages(void)
243{
244 struct script_spec *s;
245
246 fprintf(stderr, "\n");
247 fprintf(stderr, "Scripting language extensions (used in "
248 "perf trace -s [spec:]script.[spec]):\n\n");
249
250 list_for_each_entry(s, &script_specs, node)
251 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
252
253 fprintf(stderr, "\n");
254}
255
256static int parse_scriptname(const struct option *opt __used,
257 const char *str, int unset __used)
258{
259 char spec[PATH_MAX];
260 const char *script, *ext;
261 int len;
262
Tom Zanussif526d682010-01-27 02:27:52 -0600263 if (strcmp(str, "lang") == 0) {
Tom Zanussi956ffd02009-11-25 01:15:46 -0600264 list_available_languages();
Tom Zanussif526d682010-01-27 02:27:52 -0600265 exit(0);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600266 }
267
268 script = strchr(str, ':');
269 if (script) {
270 len = script - str;
271 if (len >= PATH_MAX) {
272 fprintf(stderr, "invalid language specifier");
273 return -1;
274 }
275 strncpy(spec, str, len);
276 spec[len] = '\0';
277 scripting_ops = script_spec__lookup(spec);
278 if (!scripting_ops) {
279 fprintf(stderr, "invalid language specifier");
280 return -1;
281 }
282 script++;
283 } else {
284 script = str;
Ben Hutchingsd1e95bb2010-10-10 16:11:02 +0100285 ext = strrchr(script, '.');
Tom Zanussi956ffd02009-11-25 01:15:46 -0600286 if (!ext) {
287 fprintf(stderr, "invalid script extension");
288 return -1;
289 }
290 scripting_ops = script_spec__lookup(++ext);
291 if (!scripting_ops) {
292 fprintf(stderr, "invalid script extension");
293 return -1;
294 }
295 }
296
297 script_name = strdup(script);
298
299 return 0;
300}
301
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600302#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
303 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
304 lang_next) \
305 if (lang_dirent.d_type == DT_DIR && \
306 (strcmp(lang_dirent.d_name, ".")) && \
307 (strcmp(lang_dirent.d_name, "..")))
308
309#define for_each_script(lang_dir, script_dirent, script_next) \
310 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
311 script_next) \
312 if (script_dirent.d_type != DT_DIR)
313
314
315#define RECORD_SUFFIX "-record"
316#define REPORT_SUFFIX "-report"
317
318struct script_desc {
319 struct list_head node;
320 char *name;
321 char *half_liner;
322 char *args;
323};
324
325LIST_HEAD(script_descs);
326
327static struct script_desc *script_desc__new(const char *name)
328{
329 struct script_desc *s = zalloc(sizeof(*s));
330
331 if (s != NULL)
332 s->name = strdup(name);
333
334 return s;
335}
336
337static void script_desc__delete(struct script_desc *s)
338{
339 free(s->name);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600340 free(s->half_liner);
341 free(s->args);
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600342 free(s);
343}
344
345static void script_desc__add(struct script_desc *s)
346{
347 list_add_tail(&s->node, &script_descs);
348}
349
350static struct script_desc *script_desc__find(const char *name)
351{
352 struct script_desc *s;
353
354 list_for_each_entry(s, &script_descs, node)
355 if (strcasecmp(s->name, name) == 0)
356 return s;
357 return NULL;
358}
359
360static struct script_desc *script_desc__findnew(const char *name)
361{
362 struct script_desc *s = script_desc__find(name);
363
364 if (s)
365 return s;
366
367 s = script_desc__new(name);
368 if (!s)
369 goto out_delete_desc;
370
371 script_desc__add(s);
372
373 return s;
374
375out_delete_desc:
376 script_desc__delete(s);
377
378 return NULL;
379}
380
381static char *ends_with(char *str, const char *suffix)
382{
383 size_t suffix_len = strlen(suffix);
384 char *p = str;
385
386 if (strlen(str) > suffix_len) {
387 p = str + strlen(str) - suffix_len;
388 if (!strncmp(p, suffix, suffix_len))
389 return p;
390 }
391
392 return NULL;
393}
394
395static char *ltrim(char *str)
396{
397 int len = strlen(str);
398
399 while (len && isspace(*str)) {
400 len--;
401 str++;
402 }
403
404 return str;
405}
406
407static int read_script_info(struct script_desc *desc, const char *filename)
408{
409 char line[BUFSIZ], *p;
410 FILE *fp;
411
412 fp = fopen(filename, "r");
413 if (!fp)
414 return -1;
415
416 while (fgets(line, sizeof(line), fp)) {
417 p = ltrim(line);
418 if (strlen(p) == 0)
419 continue;
420 if (*p != '#')
421 continue;
422 p++;
423 if (strlen(p) && *p == '!')
424 continue;
425
426 p = ltrim(p);
427 if (strlen(p) && p[strlen(p) - 1] == '\n')
428 p[strlen(p) - 1] = '\0';
429
430 if (!strncmp(p, "description:", strlen("description:"))) {
431 p += strlen("description:");
432 desc->half_liner = strdup(ltrim(p));
433 continue;
434 }
435
436 if (!strncmp(p, "args:", strlen("args:"))) {
437 p += strlen("args:");
438 desc->args = strdup(ltrim(p));
439 continue;
440 }
441 }
442
443 fclose(fp);
444
445 return 0;
446}
447
448static int list_available_scripts(const struct option *opt __used,
449 const char *s __used, int unset __used)
450{
451 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
452 char scripts_path[MAXPATHLEN];
453 DIR *scripts_dir, *lang_dir;
454 char script_path[MAXPATHLEN];
455 char lang_path[MAXPATHLEN];
456 struct script_desc *desc;
457 char first_half[BUFSIZ];
458 char *script_root;
459 char *str;
460
461 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
462
463 scripts_dir = opendir(scripts_path);
464 if (!scripts_dir)
465 return -1;
466
467 for_each_lang(scripts_dir, lang_dirent, lang_next) {
468 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
469 lang_dirent.d_name);
470 lang_dir = opendir(lang_path);
471 if (!lang_dir)
472 continue;
473
474 for_each_script(lang_dir, script_dirent, script_next) {
475 script_root = strdup(script_dirent.d_name);
476 str = ends_with(script_root, REPORT_SUFFIX);
477 if (str) {
478 *str = '\0';
479 desc = script_desc__findnew(script_root);
480 snprintf(script_path, MAXPATHLEN, "%s/%s",
481 lang_path, script_dirent.d_name);
482 read_script_info(desc, script_path);
483 }
484 free(script_root);
485 }
486 }
487
488 fprintf(stdout, "List of available trace scripts:\n");
489 list_for_each_entry(desc, &script_descs, node) {
490 sprintf(first_half, "%s %s", desc->name,
491 desc->args ? desc->args : "");
492 fprintf(stdout, " %-36s %s\n", first_half,
493 desc->half_liner ? desc->half_liner : "");
494 }
495
496 exit(0);
497}
498
Tom Zanussi38752942009-12-15 02:53:39 -0600499static char *get_script_path(const char *script_root, const char *suffix)
500{
501 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
502 char scripts_path[MAXPATHLEN];
503 char script_path[MAXPATHLEN];
504 DIR *scripts_dir, *lang_dir;
505 char lang_path[MAXPATHLEN];
506 char *str, *__script_root;
507 char *path = NULL;
508
509 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
510
511 scripts_dir = opendir(scripts_path);
512 if (!scripts_dir)
513 return NULL;
514
515 for_each_lang(scripts_dir, lang_dirent, lang_next) {
516 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
517 lang_dirent.d_name);
518 lang_dir = opendir(lang_path);
519 if (!lang_dir)
520 continue;
521
522 for_each_script(lang_dir, script_dirent, script_next) {
523 __script_root = strdup(script_dirent.d_name);
524 str = ends_with(__script_root, suffix);
525 if (str) {
526 *str = '\0';
527 if (strcmp(__script_root, script_root))
528 continue;
529 snprintf(script_path, MAXPATHLEN, "%s/%s",
530 lang_path, script_dirent.d_name);
531 path = strdup(script_path);
532 free(__script_root);
533 break;
534 }
535 free(__script_root);
536 }
537 }
538
539 return path;
540}
541
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200542static const char * const trace_usage[] = {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200543 "perf trace [<options>] <command>",
544 NULL
545};
546
547static const struct option options[] = {
548 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
549 "dump raw trace in ASCII"),
Ian Munsiec0555642010-04-13 18:37:33 +1000550 OPT_INCR('v', "verbose", &verbose,
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200551 "be more verbose (show symbol address, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600552 OPT_BOOLEAN('L', "Latency", &latency_format,
Steven Rostedtcda48462009-10-14 15:43:42 -0400553 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi4b9c0c52009-12-15 02:53:38 -0600554 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
555 list_available_scripts),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600556 OPT_CALLBACK('s', "script", NULL, "name",
557 "script file name (lang:script name, script name, or *)",
558 parse_scriptname),
559 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
560 "generate perf-trace.xx script in specified language"),
Hitoshi Mitake408f0d12010-01-22 22:45:29 +0900561 OPT_STRING('i', "input", &input_name, "file",
562 "input file name"),
Frederic Weisbeckerffabd992010-05-27 16:27:47 +0200563 OPT_BOOLEAN('d', "debug-mode", &debug_mode,
564 "do various checks like samples ordering and lost events"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600565
Masami Hiramatsu19096292009-08-21 14:56:03 -0400566 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200567};
568
569int cmd_trace(int argc, const char **argv, const char *prefix __used)
570{
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200571 struct perf_session *session;
Tom Zanussi38752942009-12-15 02:53:39 -0600572 const char *suffix = NULL;
573 const char **__argv;
574 char *script_path;
575 int i, err;
576
577 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
578 if (argc < 3) {
579 fprintf(stderr,
580 "Please specify a record script\n");
581 return -1;
582 }
583 suffix = RECORD_SUFFIX;
584 }
585
586 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
587 if (argc < 3) {
588 fprintf(stderr,
589 "Please specify a report script\n");
590 return -1;
591 }
592 suffix = REPORT_SUFFIX;
593 }
594
Ben Hutchings44e668c2010-10-10 16:10:03 +0100595 /* make sure PERF_EXEC_PATH is set for scripts */
596 perf_set_argv_exec_path(perf_exec_path());
597
Tom Zanussia0cccc22010-04-01 23:59:25 -0500598 if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
599 char *record_script_path, *report_script_path;
600 int live_pipe[2];
601 pid_t pid;
602
603 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
604 if (!record_script_path) {
605 fprintf(stderr, "record script not found\n");
606 return -1;
607 }
608
609 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
610 if (!report_script_path) {
611 fprintf(stderr, "report script not found\n");
612 return -1;
613 }
614
615 if (pipe(live_pipe) < 0) {
616 perror("failed to create pipe");
617 exit(-1);
618 }
619
620 pid = fork();
621 if (pid < 0) {
622 perror("failed to fork");
623 exit(-1);
624 }
625
626 if (!pid) {
627 dup2(live_pipe[1], 1);
628 close(live_pipe[0]);
629
Arnaldo Carvalho de Melob44308f2010-10-26 15:20:09 -0200630 __argv = malloc(6 * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -0600631 if (!__argv)
632 die("malloc");
633
Tom Zanussia0cccc22010-04-01 23:59:25 -0500634 __argv[0] = "/bin/sh";
635 __argv[1] = record_script_path;
Arnaldo Carvalho de Melob44308f2010-10-26 15:20:09 -0200636 __argv[2] = "-q";
637 __argv[3] = "-o";
638 __argv[4] = "-";
639 __argv[5] = NULL;
Tom Zanussia0cccc22010-04-01 23:59:25 -0500640
641 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600642 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -0500643 exit(-1);
644 }
645
646 dup2(live_pipe[0], 0);
647 close(live_pipe[1]);
648
649 __argv = malloc((argc + 3) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -0600650 if (!__argv)
651 die("malloc");
Tom Zanussia0cccc22010-04-01 23:59:25 -0500652 __argv[0] = "/bin/sh";
653 __argv[1] = report_script_path;
654 for (i = 2; i < argc; i++)
655 __argv[i] = argv[i];
656 __argv[i++] = "-i";
657 __argv[i++] = "-";
658 __argv[i++] = NULL;
659
660 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600661 free(__argv);
Tom Zanussia0cccc22010-04-01 23:59:25 -0500662 exit(-1);
663 }
664
Tom Zanussi38752942009-12-15 02:53:39 -0600665 if (suffix) {
666 script_path = get_script_path(argv[2], suffix);
667 if (!script_path) {
668 fprintf(stderr, "script not found\n");
669 return -1;
670 }
671
672 __argv = malloc((argc + 1) * sizeof(const char *));
Tom Zanussie8719ad2010-11-10 07:52:32 -0600673 if (!__argv)
674 die("malloc");
Tom Zanussi38752942009-12-15 02:53:39 -0600675 __argv[0] = "/bin/sh";
676 __argv[1] = script_path;
677 for (i = 3; i < argc; i++)
678 __argv[i - 1] = argv[i];
679 __argv[argc - 1] = NULL;
680
681 execvp("/bin/sh", (char **)__argv);
Tom Zanussie8719ad2010-11-10 07:52:32 -0600682 free(__argv);
Tom Zanussi38752942009-12-15 02:53:39 -0600683 exit(-1);
684 }
Tom Zanussi956ffd02009-11-25 01:15:46 -0600685
Tom Zanussi956ffd02009-11-25 01:15:46 -0600686 setup_scripting();
687
Arnaldo Carvalho de Melo0422a4f2009-12-18 16:35:58 -0200688 argc = parse_options(argc, argv, options, trace_usage,
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600689 PARSE_OPT_STOP_AT_NON_OPTION);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200690
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200691 if (symbol__init() < 0)
692 return -1;
Tom Zanussicf4fee52010-03-03 01:04:33 -0600693 if (!script_name)
694 setup_pager();
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200695
Tom Zanussi454c4072010-05-01 01:41:20 -0500696 session = perf_session__new(input_name, O_RDONLY, 0, false);
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200697 if (session == NULL)
698 return -ENOMEM;
699
Tom Zanussic239da32010-04-01 23:59:18 -0500700 if (strcmp(input_name, "-") &&
701 !perf_session__has_traces(session, "record -R"))
Arnaldo Carvalho de Melod549c762009-12-27 21:37:02 -0200702 return -EINVAL;
703
Tom Zanussi956ffd02009-11-25 01:15:46 -0600704 if (generate_script_lang) {
705 struct stat perf_stat;
706
707 int input = open(input_name, O_RDONLY);
708 if (input < 0) {
709 perror("failed to open file");
710 exit(-1);
711 }
712
713 err = fstat(input, &perf_stat);
714 if (err < 0) {
715 perror("failed to stat file");
716 exit(-1);
717 }
718
719 if (!perf_stat.st_size) {
720 fprintf(stderr, "zero-sized file, nothing to do!\n");
721 exit(0);
722 }
723
724 scripting_ops = script_spec__lookup(generate_script_lang);
725 if (!scripting_ops) {
726 fprintf(stderr, "invalid language specifier");
727 return -1;
728 }
729
Tom Zanussi956ffd02009-11-25 01:15:46 -0600730 err = scripting_ops->generate_script("perf-trace");
731 goto out;
732 }
733
734 if (script_name) {
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600735 err = scripting_ops->start_script(script_name, argc, argv);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600736 if (err)
737 goto out;
Tom Zanussi3824a4e2010-05-09 23:46:57 -0500738 pr_debug("perf trace started with script %s\n\n", script_name);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600739 }
740
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200741 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600742
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200743 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600744 cleanup_scripting();
745out:
746 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200747}