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