aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/bpf-loader.c
diff options
context:
space:
mode:
authorWang Nan <wangnan0@huawei.com>2015-11-16 07:10:05 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2015-11-18 15:51:03 -0500
commit361f2b1d1d7231b8685d990b886f599378a4d5a5 (patch)
tree00a5bce805e0470aaa27300b4cb72f6f0dfa1f29 /tools/perf/util/bpf-loader.c
parent30433a3a52b951faab95944e0f8b9d33a1e322ce (diff)
perf bpf: Allow BPF program attach to uprobe events
This patch adds a new syntax to the BPF object section name to support probing at uprobe event. Now we can use BPF program like this: SEC( "exec=/lib64/libc.so.6;" "libcwrite=__write" ) int libcwrite(void *ctx) { return 1; } Where, in section name of a program, before the main config string, we can use 'key=value' style options. Now the only option key is "exec", for uprobes. Signed-off-by: Wang Nan <wangnan0@huawei.com> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1447675815-166222-4-git-send-email-wangnan0@huawei.com [ Changed the separator from \n to ; ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/bpf-loader.c')
-rw-r--r--tools/perf/util/bpf-loader.c120
1 files changed, 114 insertions, 6 deletions
diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index 4c50411371db..84169d6f2585 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -110,6 +110,113 @@ bpf_prog_priv__clear(struct bpf_program *prog __maybe_unused,
110} 110}
111 111
112static int 112static int
113config__exec(const char *value, struct perf_probe_event *pev)
114{
115 pev->uprobes = true;
116 pev->target = strdup(value);
117 if (!pev->target)
118 return -ENOMEM;
119 return 0;
120}
121
122static struct {
123 const char *key;
124 const char *usage;
125 const char *desc;
126 int (*func)(const char *, struct perf_probe_event *);
127} bpf_config_terms[] = {
128 {
129 .key = "exec",
130 .usage = "exec=<full path of file>",
131 .desc = "Set uprobe target",
132 .func = config__exec,
133 },
134};
135
136static int
137do_config(const char *key, const char *value,
138 struct perf_probe_event *pev)
139{
140 unsigned int i;
141
142 pr_debug("config bpf program: %s=%s\n", key, value);
143 for (i = 0; i < ARRAY_SIZE(bpf_config_terms); i++)
144 if (strcmp(key, bpf_config_terms[i].key) == 0)
145 return bpf_config_terms[i].func(value, pev);
146
147 pr_debug("BPF: ERROR: invalid config option in object: %s=%s\n",
148 key, value);
149
150 pr_debug("\nHint: Currently valid options are:\n");
151 for (i = 0; i < ARRAY_SIZE(bpf_config_terms); i++)
152 pr_debug("\t%s:\t%s\n", bpf_config_terms[i].usage,
153 bpf_config_terms[i].desc);
154 pr_debug("\n");
155
156 return -BPF_LOADER_ERRNO__CONFIG_TERM;
157}
158
159static const char *
160parse_config_kvpair(const char *config_str, struct perf_probe_event *pev)
161{
162 char *text = strdup(config_str);
163 char *sep, *line;
164 const char *main_str = NULL;
165 int err = 0;
166
167 if (!text) {
168 pr_debug("No enough memory: dup config_str failed\n");
169 return ERR_PTR(-ENOMEM);
170 }
171
172 line = text;
173 while ((sep = strchr(line, ';'))) {
174 char *equ;
175
176 *sep = '\0';
177 equ = strchr(line, '=');
178 if (!equ) {
179 pr_warning("WARNING: invalid config in BPF object: %s\n",
180 line);
181 pr_warning("\tShould be 'key=value'.\n");
182 goto nextline;
183 }
184 *equ = '\0';
185
186 err = do_config(line, equ + 1, pev);
187 if (err)
188 break;
189nextline:
190 line = sep + 1;
191 }
192
193 if (!err)
194 main_str = config_str + (line - text);
195 free(text);
196
197 return err ? ERR_PTR(err) : main_str;
198}
199
200static int
201parse_config(const char *config_str, struct perf_probe_event *pev)
202{
203 int err;
204 const char *main_str = parse_config_kvpair(config_str, pev);
205
206 if (IS_ERR(main_str))
207 return PTR_ERR(main_str);
208
209 err = parse_perf_probe_command(main_str, pev);
210 if (err < 0) {
211 pr_debug("bpf: '%s' is not a valid config string\n",
212 config_str);
213 /* parse failed, don't need clear pev. */
214 return -BPF_LOADER_ERRNO__CONFIG;
215 }
216 return 0;
217}
218
219static int
113config_bpf_program(struct bpf_program *prog) 220config_bpf_program(struct bpf_program *prog)
114{ 221{
115 struct perf_probe_event *pev = NULL; 222 struct perf_probe_event *pev = NULL;
@@ -131,13 +238,9 @@ config_bpf_program(struct bpf_program *prog)
131 pev = &priv->pev; 238 pev = &priv->pev;
132 239
133 pr_debug("bpf: config program '%s'\n", config_str); 240 pr_debug("bpf: config program '%s'\n", config_str);
134 err = parse_perf_probe_command(config_str, pev); 241 err = parse_config(config_str, pev);
135 if (err < 0) { 242 if (err)
136 pr_debug("bpf: '%s' is not a valid config string\n",
137 config_str);
138 err = -BPF_LOADER_ERRNO__CONFIG;
139 goto errout; 243 goto errout;
140 }
141 244
142 if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) { 245 if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) {
143 pr_debug("bpf: '%s': group for event is set and not '%s'.\n", 246 pr_debug("bpf: '%s': group for event is set and not '%s'.\n",
@@ -340,6 +443,7 @@ static const char *bpf_loader_strerror_table[NR_ERRNO] = {
340 [ERRCODE_OFFSET(EVENTNAME)] = "No event name found in config string", 443 [ERRCODE_OFFSET(EVENTNAME)] = "No event name found in config string",
341 [ERRCODE_OFFSET(INTERNAL)] = "BPF loader internal error", 444 [ERRCODE_OFFSET(INTERNAL)] = "BPF loader internal error",
342 [ERRCODE_OFFSET(COMPILE)] = "Error when compiling BPF scriptlet", 445 [ERRCODE_OFFSET(COMPILE)] = "Error when compiling BPF scriptlet",
446 [ERRCODE_OFFSET(CONFIG_TERM)] = "Invalid config term in config string",
343}; 447};
344 448
345static int 449static int
@@ -420,6 +524,10 @@ int bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
420 int err, char *buf, size_t size) 524 int err, char *buf, size_t size)
421{ 525{
422 bpf__strerror_head(err, buf, size); 526 bpf__strerror_head(err, buf, size);
527 case BPF_LOADER_ERRNO__CONFIG_TERM: {
528 scnprintf(buf, size, "%s (add -v to see detail)", emsg);
529 break;
530 }
423 bpf__strerror_entry(EEXIST, "Probe point exist. Try use 'perf probe -d \"*\"'"); 531 bpf__strerror_entry(EEXIST, "Probe point exist. Try use 'perf probe -d \"*\"'");
424 bpf__strerror_entry(EACCES, "You need to be root"); 532 bpf__strerror_entry(EACCES, "You need to be root");
425 bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0"); 533 bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0");