1
2#include "../../util/sort.h"
3#include "../../util/hist.h"
4#include "../../util/debug.h"
5#include "../../util/symbol.h"
6#include "../browser.h"
7#include "../libslang.h"
8#include "config.h"
9#include <linux/zalloc.h>
10
11#define SCRIPT_NAMELEN 128
12#define SCRIPT_MAX_NO 64
13
14
15
16
17
18
19#define SCRIPT_FULLPATH_LEN 256
20
21struct script_config {
22 const char **names;
23 char **paths;
24 int index;
25 const char *perf;
26 char extra_format[256];
27};
28
29void attr_to_script(char *extra_format, struct perf_event_attr *attr)
30{
31 extra_format[0] = 0;
32 if (attr->read_format & PERF_FORMAT_GROUP)
33 strcat(extra_format, " -F +metric");
34 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK)
35 strcat(extra_format, " -F +brstackinsn --xed");
36 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
37 strcat(extra_format, " -F +iregs");
38 if (attr->sample_type & PERF_SAMPLE_REGS_USER)
39 strcat(extra_format, " -F +uregs");
40 if (attr->sample_type & PERF_SAMPLE_PHYS_ADDR)
41 strcat(extra_format, " -F +phys_addr");
42}
43
44static int add_script_option(const char *name, const char *opt,
45 struct script_config *c)
46{
47 c->names[c->index] = name;
48 if (asprintf(&c->paths[c->index],
49 "%s script %s -F +metric %s %s",
50 c->perf, opt, symbol_conf.inline_name ? " --inline" : "",
51 c->extra_format) < 0)
52 return -1;
53 c->index++;
54 return 0;
55}
56
57static int scripts_config(const char *var, const char *value, void *data)
58{
59 struct script_config *c = data;
60
61 if (!strstarts(var, "scripts."))
62 return -1;
63 if (c->index >= SCRIPT_MAX_NO)
64 return -1;
65 c->names[c->index] = strdup(var + 7);
66 if (!c->names[c->index])
67 return -1;
68 if (asprintf(&c->paths[c->index], "%s %s", value,
69 c->extra_format) < 0)
70 return -1;
71 c->index++;
72 return 0;
73}
74
75
76
77
78
79
80static int list_scripts(char *script_name, bool *custom,
81 struct perf_evsel *evsel)
82{
83 char *buf, *paths[SCRIPT_MAX_NO], *names[SCRIPT_MAX_NO];
84 int i, num, choice;
85 int ret = 0;
86 int max_std, custom_perf;
87 char pbuf[256];
88 const char *perf = perf_exe(pbuf, sizeof pbuf);
89 struct script_config scriptc = {
90 .names = (const char **)names,
91 .paths = paths,
92 .perf = perf
93 };
94
95 script_name[0] = 0;
96
97
98 buf = malloc(SCRIPT_MAX_NO * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN));
99 if (!buf)
100 return -1;
101
102 if (evsel)
103 attr_to_script(scriptc.extra_format, &evsel->attr);
104 add_script_option("Show individual samples", "", &scriptc);
105 add_script_option("Show individual samples with assembler", "-F +insn --xed",
106 &scriptc);
107 add_script_option("Show individual samples with source", "-F +srcline,+srccode",
108 &scriptc);
109 perf_config(scripts_config, &scriptc);
110 custom_perf = scriptc.index;
111 add_script_option("Show samples with custom perf script arguments", "", &scriptc);
112 i = scriptc.index;
113 max_std = i;
114
115 for (; i < SCRIPT_MAX_NO; i++) {
116 names[i] = buf + (i - max_std) * (SCRIPT_NAMELEN + SCRIPT_FULLPATH_LEN);
117 paths[i] = names[i] + SCRIPT_NAMELEN;
118 }
119
120 num = find_scripts(names + max_std, paths + max_std, SCRIPT_MAX_NO - max_std,
121 SCRIPT_FULLPATH_LEN);
122 if (num < 0)
123 num = 0;
124 choice = ui__popup_menu(num + max_std, (char * const *)names);
125 if (choice < 0) {
126 ret = -1;
127 goto out;
128 }
129 if (choice == custom_perf) {
130 char script_args[50];
131 int key = ui_browser__input_window("perf script command",
132 "Enter perf script command line (without perf script prefix)",
133 script_args, "", 0);
134 if (key != K_ENTER)
135 return -1;
136 sprintf(script_name, "%s script %s", perf, script_args);
137 } else if (choice < num + max_std) {
138 strcpy(script_name, paths[choice]);
139 }
140 *custom = choice >= max_std;
141
142out:
143 free(buf);
144 for (i = 0; i < max_std; i++)
145 zfree(&paths[i]);
146 return ret;
147}
148
149void run_script(char *cmd)
150{
151 pr_debug("Running %s\n", cmd);
152 SLang_reset_tty();
153 if (system(cmd) < 0)
154 pr_warning("Cannot run %s\n", cmd);
155
156
157
158
159 printf("\033[c\033[H\033[J");
160 fflush(stdout);
161 SLang_init_tty(0, 0, 0);
162 SLsmg_refresh();
163}
164
165int script_browse(const char *script_opt, struct perf_evsel *evsel)
166{
167 char *cmd, script_name[SCRIPT_FULLPATH_LEN];
168 bool custom = false;
169
170 memset(script_name, 0, SCRIPT_FULLPATH_LEN);
171 if (list_scripts(script_name, &custom, evsel))
172 return -1;
173
174 if (asprintf(&cmd, "%s%s %s %s%s 2>&1 | less",
175 custom ? "perf script -s " : "",
176 script_name,
177 script_opt ? script_opt : "",
178 input_name ? "-i " : "",
179 input_name ? input_name : "") < 0)
180 return -1;
181
182 run_script(cmd);
183 free(cmd);
184
185 return 0;
186}
187