]> Pileus Git - ~andy/linux/blob - tools/perf/builtin-script.c
perf script: Support custom field selection for output
[~andy/linux] / tools / perf / builtin-script.c
1 #include "builtin.h"
2
3 #include "perf.h"
4 #include "util/cache.h"
5 #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"
10 #include "util/symbol.h"
11 #include "util/thread.h"
12 #include "util/trace-event.h"
13 #include "util/parse-options.h"
14 #include "util/util.h"
15
16 static char const               *script_name;
17 static char const               *generate_script_lang;
18 static bool                     debug_mode;
19 static u64                      last_timestamp;
20 static u64                      nr_unordered;
21 extern const struct option      record_options[];
22
23 enum perf_output_field {
24         PERF_OUTPUT_COMM            = 1U << 0,
25         PERF_OUTPUT_TID             = 1U << 1,
26         PERF_OUTPUT_PID             = 1U << 2,
27         PERF_OUTPUT_TIME            = 1U << 3,
28         PERF_OUTPUT_CPU             = 1U << 4,
29         PERF_OUTPUT_EVNAME          = 1U << 5,
30         PERF_OUTPUT_TRACE           = 1U << 6,
31 };
32
33 struct output_option {
34         const char *str;
35         enum perf_output_field field;
36 } all_output_options[] = {
37         {.str = "comm",  .field = PERF_OUTPUT_COMM},
38         {.str = "tid",   .field = PERF_OUTPUT_TID},
39         {.str = "pid",   .field = PERF_OUTPUT_PID},
40         {.str = "time",  .field = PERF_OUTPUT_TIME},
41         {.str = "cpu",   .field = PERF_OUTPUT_CPU},
42         {.str = "event", .field = PERF_OUTPUT_EVNAME},
43         {.str = "trace", .field = PERF_OUTPUT_TRACE},
44 };
45
46 /* default set to maintain compatibility with current format */
47 static u64 output_fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | \
48                            PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | \
49                            PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE;
50
51 static bool output_set_by_user;
52
53 #define PRINT_FIELD(x)  (output_fields & PERF_OUTPUT_##x)
54
55 static void print_sample_start(struct perf_sample *sample,
56                                struct thread *thread)
57 {
58         int type;
59         struct event *event;
60         const char *evname = NULL;
61         unsigned long secs;
62         unsigned long usecs;
63         unsigned long long nsecs;
64
65         if (PRINT_FIELD(COMM)) {
66                 if (latency_format)
67                         printf("%8.8s ", thread->comm);
68                 else
69                         printf("%16s ", thread->comm);
70         }
71
72         if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
73                 printf("%5d/%-5d ", sample->pid, sample->tid);
74         else if (PRINT_FIELD(PID))
75                 printf("%5d ", sample->pid);
76         else if (PRINT_FIELD(TID))
77                 printf("%5d ", sample->tid);
78
79         if (PRINT_FIELD(CPU)) {
80                 if (latency_format)
81                         printf("%3d ", sample->cpu);
82                 else
83                         printf("[%03d] ", sample->cpu);
84         }
85
86         if (PRINT_FIELD(TIME)) {
87                 nsecs = sample->time;
88                 secs = nsecs / NSECS_PER_SEC;
89                 nsecs -= secs * NSECS_PER_SEC;
90                 usecs = nsecs / NSECS_PER_USEC;
91                 printf("%5lu.%06lu: ", secs, usecs);
92         }
93
94         if (PRINT_FIELD(EVNAME)) {
95                 type = trace_parse_common_type(sample->raw_data);
96                 event = trace_find_event(type);
97                 if (event)
98                         evname = event->name;
99
100                 printf("%s: ", evname ? evname : "(unknown)");
101         }
102 }
103
104 static void process_event(union perf_event *event __unused,
105                           struct perf_sample *sample,
106                           struct perf_session *session __unused,
107                           struct thread *thread)
108 {
109         print_sample_start(sample, thread);
110
111         if (PRINT_FIELD(TRACE))
112                 print_trace_event(sample->cpu, sample->raw_data,
113                                   sample->raw_size);
114
115         printf("\n");
116 }
117
118 static int default_start_script(const char *script __unused,
119                                 int argc __unused,
120                                 const char **argv __unused)
121 {
122         return 0;
123 }
124
125 static int default_stop_script(void)
126 {
127         return 0;
128 }
129
130 static int default_generate_script(const char *outfile __unused)
131 {
132         return 0;
133 }
134
135 static struct scripting_ops default_scripting_ops = {
136         .start_script           = default_start_script,
137         .stop_script            = default_stop_script,
138         .process_event          = process_event,
139         .generate_script        = default_generate_script,
140 };
141
142 static struct scripting_ops     *scripting_ops;
143
144 static void setup_scripting(void)
145 {
146         setup_perl_scripting();
147         setup_python_scripting();
148
149         scripting_ops = &default_scripting_ops;
150 }
151
152 static int cleanup_scripting(void)
153 {
154         pr_debug("\nperf script stopped\n");
155
156         return scripting_ops->stop_script();
157 }
158
159 static char const               *input_name = "perf.data";
160
161 static int process_sample_event(union perf_event *event,
162                                 struct perf_sample *sample,
163                                 struct perf_session *session)
164 {
165         struct thread *thread = perf_session__findnew(session, event->ip.pid);
166
167         if (thread == NULL) {
168                 pr_debug("problem processing %d event, skipping it.\n",
169                          event->header.type);
170                 return -1;
171         }
172
173         if (session->sample_type & PERF_SAMPLE_RAW) {
174                 if (debug_mode) {
175                         if (sample->time < last_timestamp) {
176                                 pr_err("Samples misordered, previous: %" PRIu64
177                                         " this: %" PRIu64 "\n", last_timestamp,
178                                         sample->time);
179                                 nr_unordered++;
180                         }
181                         last_timestamp = sample->time;
182                         return 0;
183                 }
184                 scripting_ops->process_event(event, sample, session, thread);
185         }
186
187         session->hists.stats.total_period += sample->period;
188         return 0;
189 }
190
191 static struct perf_event_ops event_ops = {
192         .sample          = process_sample_event,
193         .comm            = perf_event__process_comm,
194         .attr            = perf_event__process_attr,
195         .event_type      = perf_event__process_event_type,
196         .tracing_data    = perf_event__process_tracing_data,
197         .build_id        = perf_event__process_build_id,
198         .ordered_samples = true,
199         .ordering_requires_timestamps = true,
200 };
201
202 extern volatile int session_done;
203
204 static void sig_handler(int sig __unused)
205 {
206         session_done = 1;
207 }
208
209 static int __cmd_script(struct perf_session *session)
210 {
211         int ret;
212
213         signal(SIGINT, sig_handler);
214
215         ret = perf_session__process_events(session, &event_ops);
216
217         if (debug_mode)
218                 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
219
220         return ret;
221 }
222
223 struct script_spec {
224         struct list_head        node;
225         struct scripting_ops    *ops;
226         char                    spec[0];
227 };
228
229 static LIST_HEAD(script_specs);
230
231 static struct script_spec *script_spec__new(const char *spec,
232                                             struct scripting_ops *ops)
233 {
234         struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
235
236         if (s != NULL) {
237                 strcpy(s->spec, spec);
238                 s->ops = ops;
239         }
240
241         return s;
242 }
243
244 static void script_spec__delete(struct script_spec *s)
245 {
246         free(s->spec);
247         free(s);
248 }
249
250 static void script_spec__add(struct script_spec *s)
251 {
252         list_add_tail(&s->node, &script_specs);
253 }
254
255 static struct script_spec *script_spec__find(const char *spec)
256 {
257         struct script_spec *s;
258
259         list_for_each_entry(s, &script_specs, node)
260                 if (strcasecmp(s->spec, spec) == 0)
261                         return s;
262         return NULL;
263 }
264
265 static struct script_spec *script_spec__findnew(const char *spec,
266                                                 struct scripting_ops *ops)
267 {
268         struct script_spec *s = script_spec__find(spec);
269
270         if (s)
271                 return s;
272
273         s = script_spec__new(spec, ops);
274         if (!s)
275                 goto out_delete_spec;
276
277         script_spec__add(s);
278
279         return s;
280
281 out_delete_spec:
282         script_spec__delete(s);
283
284         return NULL;
285 }
286
287 int script_spec_register(const char *spec, struct scripting_ops *ops)
288 {
289         struct script_spec *s;
290
291         s = script_spec__find(spec);
292         if (s)
293                 return -1;
294
295         s = script_spec__findnew(spec, ops);
296         if (!s)
297                 return -1;
298
299         return 0;
300 }
301
302 static struct scripting_ops *script_spec__lookup(const char *spec)
303 {
304         struct script_spec *s = script_spec__find(spec);
305         if (!s)
306                 return NULL;
307
308         return s->ops;
309 }
310
311 static void list_available_languages(void)
312 {
313         struct script_spec *s;
314
315         fprintf(stderr, "\n");
316         fprintf(stderr, "Scripting language extensions (used in "
317                 "perf script -s [spec:]script.[spec]):\n\n");
318
319         list_for_each_entry(s, &script_specs, node)
320                 fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name);
321
322         fprintf(stderr, "\n");
323 }
324
325 static int parse_scriptname(const struct option *opt __used,
326                             const char *str, int unset __used)
327 {
328         char spec[PATH_MAX];
329         const char *script, *ext;
330         int len;
331
332         if (strcmp(str, "lang") == 0) {
333                 list_available_languages();
334                 exit(0);
335         }
336
337         script = strchr(str, ':');
338         if (script) {
339                 len = script - str;
340                 if (len >= PATH_MAX) {
341                         fprintf(stderr, "invalid language specifier");
342                         return -1;
343                 }
344                 strncpy(spec, str, len);
345                 spec[len] = '\0';
346                 scripting_ops = script_spec__lookup(spec);
347                 if (!scripting_ops) {
348                         fprintf(stderr, "invalid language specifier");
349                         return -1;
350                 }
351                 script++;
352         } else {
353                 script = str;
354                 ext = strrchr(script, '.');
355                 if (!ext) {
356                         fprintf(stderr, "invalid script extension");
357                         return -1;
358                 }
359                 scripting_ops = script_spec__lookup(++ext);
360                 if (!scripting_ops) {
361                         fprintf(stderr, "invalid script extension");
362                         return -1;
363                 }
364         }
365
366         script_name = strdup(script);
367
368         return 0;
369 }
370
371 static int parse_output_fields(const struct option *opt __used,
372                             const char *arg, int unset __used)
373 {
374         char *tok;
375         int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
376         int rc = 0;
377         char *str = strdup(arg);
378
379         if (!str)
380                 return -ENOMEM;
381
382         tok = strtok(str, ",");
383         if (!tok) {
384                 fprintf(stderr, "Invalid field string.");
385                 return -EINVAL;
386         }
387
388         output_fields = 0;
389         while (1) {
390                 for (i = 0; i < imax; ++i) {
391                         if (strcmp(tok, all_output_options[i].str) == 0) {
392                                 output_fields |= all_output_options[i].field;
393                                 break;
394                         }
395                 }
396                 if (i == imax) {
397                         fprintf(stderr, "Invalid field requested.");
398                         rc = -EINVAL;
399                         break;
400                 }
401
402                 tok = strtok(NULL, ",");
403                 if (!tok)
404                         break;
405         }
406
407         output_set_by_user = true;
408
409         free(str);
410         return rc;
411 }
412
413 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
414 static int is_directory(const char *base_path, const struct dirent *dent)
415 {
416         char path[PATH_MAX];
417         struct stat st;
418
419         sprintf(path, "%s/%s", base_path, dent->d_name);
420         if (stat(path, &st))
421                 return 0;
422
423         return S_ISDIR(st.st_mode);
424 }
425
426 #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
427         while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) &&     \
428                lang_next)                                               \
429                 if ((lang_dirent.d_type == DT_DIR ||                    \
430                      (lang_dirent.d_type == DT_UNKNOWN &&               \
431                       is_directory(scripts_path, &lang_dirent))) &&     \
432                     (strcmp(lang_dirent.d_name, ".")) &&                \
433                     (strcmp(lang_dirent.d_name, "..")))
434
435 #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
436         while (!readdir_r(lang_dir, &script_dirent, &script_next) &&    \
437                script_next)                                             \
438                 if (script_dirent.d_type != DT_DIR &&                   \
439                     (script_dirent.d_type != DT_UNKNOWN ||              \
440                      !is_directory(lang_path, &script_dirent)))
441
442
443 #define RECORD_SUFFIX                   "-record"
444 #define REPORT_SUFFIX                   "-report"
445
446 struct script_desc {
447         struct list_head        node;
448         char                    *name;
449         char                    *half_liner;
450         char                    *args;
451 };
452
453 static LIST_HEAD(script_descs);
454
455 static struct script_desc *script_desc__new(const char *name)
456 {
457         struct script_desc *s = zalloc(sizeof(*s));
458
459         if (s != NULL && name)
460                 s->name = strdup(name);
461
462         return s;
463 }
464
465 static void script_desc__delete(struct script_desc *s)
466 {
467         free(s->name);
468         free(s->half_liner);
469         free(s->args);
470         free(s);
471 }
472
473 static void script_desc__add(struct script_desc *s)
474 {
475         list_add_tail(&s->node, &script_descs);
476 }
477
478 static struct script_desc *script_desc__find(const char *name)
479 {
480         struct script_desc *s;
481
482         list_for_each_entry(s, &script_descs, node)
483                 if (strcasecmp(s->name, name) == 0)
484                         return s;
485         return NULL;
486 }
487
488 static struct script_desc *script_desc__findnew(const char *name)
489 {
490         struct script_desc *s = script_desc__find(name);
491
492         if (s)
493                 return s;
494
495         s = script_desc__new(name);
496         if (!s)
497                 goto out_delete_desc;
498
499         script_desc__add(s);
500
501         return s;
502
503 out_delete_desc:
504         script_desc__delete(s);
505
506         return NULL;
507 }
508
509 static const char *ends_with(const char *str, const char *suffix)
510 {
511         size_t suffix_len = strlen(suffix);
512         const char *p = str;
513
514         if (strlen(str) > suffix_len) {
515                 p = str + strlen(str) - suffix_len;
516                 if (!strncmp(p, suffix, suffix_len))
517                         return p;
518         }
519
520         return NULL;
521 }
522
523 static char *ltrim(char *str)
524 {
525         int len = strlen(str);
526
527         while (len && isspace(*str)) {
528                 len--;
529                 str++;
530         }
531
532         return str;
533 }
534
535 static int read_script_info(struct script_desc *desc, const char *filename)
536 {
537         char line[BUFSIZ], *p;
538         FILE *fp;
539
540         fp = fopen(filename, "r");
541         if (!fp)
542                 return -1;
543
544         while (fgets(line, sizeof(line), fp)) {
545                 p = ltrim(line);
546                 if (strlen(p) == 0)
547                         continue;
548                 if (*p != '#')
549                         continue;
550                 p++;
551                 if (strlen(p) && *p == '!')
552                         continue;
553
554                 p = ltrim(p);
555                 if (strlen(p) && p[strlen(p) - 1] == '\n')
556                         p[strlen(p) - 1] = '\0';
557
558                 if (!strncmp(p, "description:", strlen("description:"))) {
559                         p += strlen("description:");
560                         desc->half_liner = strdup(ltrim(p));
561                         continue;
562                 }
563
564                 if (!strncmp(p, "args:", strlen("args:"))) {
565                         p += strlen("args:");
566                         desc->args = strdup(ltrim(p));
567                         continue;
568                 }
569         }
570
571         fclose(fp);
572
573         return 0;
574 }
575
576 static int list_available_scripts(const struct option *opt __used,
577                                   const char *s __used, int unset __used)
578 {
579         struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
580         char scripts_path[MAXPATHLEN];
581         DIR *scripts_dir, *lang_dir;
582         char script_path[MAXPATHLEN];
583         char lang_path[MAXPATHLEN];
584         struct script_desc *desc;
585         char first_half[BUFSIZ];
586         char *script_root;
587         char *str;
588
589         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
590
591         scripts_dir = opendir(scripts_path);
592         if (!scripts_dir)
593                 return -1;
594
595         for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
596                 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
597                          lang_dirent.d_name);
598                 lang_dir = opendir(lang_path);
599                 if (!lang_dir)
600                         continue;
601
602                 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
603                         script_root = strdup(script_dirent.d_name);
604                         str = (char *)ends_with(script_root, REPORT_SUFFIX);
605                         if (str) {
606                                 *str = '\0';
607                                 desc = script_desc__findnew(script_root);
608                                 snprintf(script_path, MAXPATHLEN, "%s/%s",
609                                          lang_path, script_dirent.d_name);
610                                 read_script_info(desc, script_path);
611                         }
612                         free(script_root);
613                 }
614         }
615
616         fprintf(stdout, "List of available trace scripts:\n");
617         list_for_each_entry(desc, &script_descs, node) {
618                 sprintf(first_half, "%s %s", desc->name,
619                         desc->args ? desc->args : "");
620                 fprintf(stdout, "  %-36s %s\n", first_half,
621                         desc->half_liner ? desc->half_liner : "");
622         }
623
624         exit(0);
625 }
626
627 static char *get_script_path(const char *script_root, const char *suffix)
628 {
629         struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
630         char scripts_path[MAXPATHLEN];
631         char script_path[MAXPATHLEN];
632         DIR *scripts_dir, *lang_dir;
633         char lang_path[MAXPATHLEN];
634         char *str, *__script_root;
635         char *path = NULL;
636
637         snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
638
639         scripts_dir = opendir(scripts_path);
640         if (!scripts_dir)
641                 return NULL;
642
643         for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
644                 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
645                          lang_dirent.d_name);
646                 lang_dir = opendir(lang_path);
647                 if (!lang_dir)
648                         continue;
649
650                 for_each_script(lang_path, lang_dir, script_dirent, script_next) {
651                         __script_root = strdup(script_dirent.d_name);
652                         str = (char *)ends_with(__script_root, suffix);
653                         if (str) {
654                                 *str = '\0';
655                                 if (strcmp(__script_root, script_root))
656                                         continue;
657                                 snprintf(script_path, MAXPATHLEN, "%s/%s",
658                                          lang_path, script_dirent.d_name);
659                                 path = strdup(script_path);
660                                 free(__script_root);
661                                 break;
662                         }
663                         free(__script_root);
664                 }
665         }
666
667         return path;
668 }
669
670 static bool is_top_script(const char *script_path)
671 {
672         return ends_with(script_path, "top") == NULL ? false : true;
673 }
674
675 static int has_required_arg(char *script_path)
676 {
677         struct script_desc *desc;
678         int n_args = 0;
679         char *p;
680
681         desc = script_desc__new(NULL);
682
683         if (read_script_info(desc, script_path))
684                 goto out;
685
686         if (!desc->args)
687                 goto out;
688
689         for (p = desc->args; *p; p++)
690                 if (*p == '<')
691                         n_args++;
692 out:
693         script_desc__delete(desc);
694
695         return n_args;
696 }
697
698 static const char * const script_usage[] = {
699         "perf script [<options>]",
700         "perf script [<options>] record <script> [<record-options>] <command>",
701         "perf script [<options>] report <script> [script-args]",
702         "perf script [<options>] <script> [<record-options>] <command>",
703         "perf script [<options>] <top-script> [script-args]",
704         NULL
705 };
706
707 static const struct option options[] = {
708         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
709                     "dump raw trace in ASCII"),
710         OPT_INCR('v', "verbose", &verbose,
711                     "be more verbose (show symbol address, etc)"),
712         OPT_BOOLEAN('L', "Latency", &latency_format,
713                     "show latency attributes (irqs/preemption disabled, etc)"),
714         OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
715                            list_available_scripts),
716         OPT_CALLBACK('s', "script", NULL, "name",
717                      "script file name (lang:script name, script name, or *)",
718                      parse_scriptname),
719         OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
720                    "generate perf-script.xx script in specified language"),
721         OPT_STRING('i', "input", &input_name, "file",
722                     "input file name"),
723         OPT_BOOLEAN('d', "debug-mode", &debug_mode,
724                    "do various checks like samples ordering and lost events"),
725         OPT_CALLBACK('f', "fields", NULL, "str",
726                      "comma separated output fields. Options: comm,tid,pid,time,cpu,event,trace",
727                      parse_output_fields),
728
729         OPT_END()
730 };
731
732 static bool have_cmd(int argc, const char **argv)
733 {
734         char **__argv = malloc(sizeof(const char *) * argc);
735
736         if (!__argv)
737                 die("malloc");
738         memcpy(__argv, argv, sizeof(const char *) * argc);
739         argc = parse_options(argc, (const char **)__argv, record_options,
740                              NULL, PARSE_OPT_STOP_AT_NON_OPTION);
741         free(__argv);
742
743         return argc != 0;
744 }
745
746 int cmd_script(int argc, const char **argv, const char *prefix __used)
747 {
748         char *rec_script_path = NULL;
749         char *rep_script_path = NULL;
750         struct perf_session *session;
751         char *script_path = NULL;
752         const char **__argv;
753         bool system_wide;
754         int i, j, err;
755
756         setup_scripting();
757
758         argc = parse_options(argc, argv, options, script_usage,
759                              PARSE_OPT_STOP_AT_NON_OPTION);
760
761         if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
762                 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
763                 if (!rec_script_path)
764                         return cmd_record(argc, argv, NULL);
765         }
766
767         if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
768                 rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
769                 if (!rep_script_path) {
770                         fprintf(stderr,
771                                 "Please specify a valid report script"
772                                 "(see 'perf script -l' for listing)\n");
773                         return -1;
774                 }
775         }
776
777         /* make sure PERF_EXEC_PATH is set for scripts */
778         perf_set_argv_exec_path(perf_exec_path());
779
780         if (argc && !script_name && !rec_script_path && !rep_script_path) {
781                 int live_pipe[2];
782                 int rep_args;
783                 pid_t pid;
784
785                 rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
786                 rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
787
788                 if (!rec_script_path && !rep_script_path) {
789                         fprintf(stderr, " Couldn't find script %s\n\n See perf"
790                                 " script -l for available scripts.\n", argv[0]);
791                         usage_with_options(script_usage, options);
792                 }
793
794                 if (is_top_script(argv[0])) {
795                         rep_args = argc - 1;
796                 } else {
797                         int rec_args;
798
799                         rep_args = has_required_arg(rep_script_path);
800                         rec_args = (argc - 1) - rep_args;
801                         if (rec_args < 0) {
802                                 fprintf(stderr, " %s script requires options."
803                                         "\n\n See perf script -l for available "
804                                         "scripts and options.\n", argv[0]);
805                                 usage_with_options(script_usage, options);
806                         }
807                 }
808
809                 if (pipe(live_pipe) < 0) {
810                         perror("failed to create pipe");
811                         exit(-1);
812                 }
813
814                 pid = fork();
815                 if (pid < 0) {
816                         perror("failed to fork");
817                         exit(-1);
818                 }
819
820                 if (!pid) {
821                         system_wide = true;
822                         j = 0;
823
824                         dup2(live_pipe[1], 1);
825                         close(live_pipe[0]);
826
827                         if (!is_top_script(argv[0]))
828                                 system_wide = !have_cmd(argc - rep_args,
829                                                         &argv[rep_args]);
830
831                         __argv = malloc((argc + 6) * sizeof(const char *));
832                         if (!__argv)
833                                 die("malloc");
834
835                         __argv[j++] = "/bin/sh";
836                         __argv[j++] = rec_script_path;
837                         if (system_wide)
838                                 __argv[j++] = "-a";
839                         __argv[j++] = "-q";
840                         __argv[j++] = "-o";
841                         __argv[j++] = "-";
842                         for (i = rep_args + 1; i < argc; i++)
843                                 __argv[j++] = argv[i];
844                         __argv[j++] = NULL;
845
846                         execvp("/bin/sh", (char **)__argv);
847                         free(__argv);
848                         exit(-1);
849                 }
850
851                 dup2(live_pipe[0], 0);
852                 close(live_pipe[1]);
853
854                 __argv = malloc((argc + 4) * sizeof(const char *));
855                 if (!__argv)
856                         die("malloc");
857                 j = 0;
858                 __argv[j++] = "/bin/sh";
859                 __argv[j++] = rep_script_path;
860                 for (i = 1; i < rep_args + 1; i++)
861                         __argv[j++] = argv[i];
862                 __argv[j++] = "-i";
863                 __argv[j++] = "-";
864                 __argv[j++] = NULL;
865
866                 execvp("/bin/sh", (char **)__argv);
867                 free(__argv);
868                 exit(-1);
869         }
870
871         if (rec_script_path)
872                 script_path = rec_script_path;
873         if (rep_script_path)
874                 script_path = rep_script_path;
875
876         if (script_path) {
877                 system_wide = false;
878                 j = 0;
879
880                 if (rec_script_path)
881                         system_wide = !have_cmd(argc - 1, &argv[1]);
882
883                 __argv = malloc((argc + 2) * sizeof(const char *));
884                 if (!__argv)
885                         die("malloc");
886                 __argv[j++] = "/bin/sh";
887                 __argv[j++] = script_path;
888                 if (system_wide)
889                         __argv[j++] = "-a";
890                 for (i = 2; i < argc; i++)
891                         __argv[j++] = argv[i];
892                 __argv[j++] = NULL;
893
894                 execvp("/bin/sh", (char **)__argv);
895                 free(__argv);
896                 exit(-1);
897         }
898
899         if (symbol__init() < 0)
900                 return -1;
901         if (!script_name)
902                 setup_pager();
903
904         session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
905         if (session == NULL)
906                 return -ENOMEM;
907
908         if (strcmp(input_name, "-") &&
909             !perf_session__has_traces(session, "record -R"))
910                 return -EINVAL;
911
912         if (generate_script_lang) {
913                 struct stat perf_stat;
914                 int input;
915
916                 if (output_set_by_user) {
917                         fprintf(stderr,
918                                 "custom fields not supported for generated scripts");
919                         return -1;
920                 }
921
922                 input = open(input_name, O_RDONLY);
923                 if (input < 0) {
924                         perror("failed to open file");
925                         exit(-1);
926                 }
927
928                 err = fstat(input, &perf_stat);
929                 if (err < 0) {
930                         perror("failed to stat file");
931                         exit(-1);
932                 }
933
934                 if (!perf_stat.st_size) {
935                         fprintf(stderr, "zero-sized file, nothing to do!\n");
936                         exit(0);
937                 }
938
939                 scripting_ops = script_spec__lookup(generate_script_lang);
940                 if (!scripting_ops) {
941                         fprintf(stderr, "invalid language specifier");
942                         return -1;
943                 }
944
945                 err = scripting_ops->generate_script("perf-script");
946                 goto out;
947         }
948
949         if (script_name) {
950                 err = scripting_ops->start_script(script_name, argc, argv);
951                 if (err)
952                         goto out;
953                 pr_debug("perf script started with script %s\n\n", script_name);
954         }
955
956         err = __cmd_script(session);
957
958         perf_session__delete(session);
959         cleanup_scripting();
960 out:
961         return err;
962 }