Commit | Line | Data |
---|---|---|
4ea42b18 MH |
1 | /* |
2 | * builtin-probe.c | |
3 | * | |
4 | * Builtin probe command: Set up probe events by C expression | |
5 | * | |
6 | * Written by Masami Hiramatsu <mhiramat@redhat.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
21 | * | |
22 | */ | |
23 | #define _GNU_SOURCE | |
24 | #include <sys/utsname.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/stat.h> | |
27 | #include <fcntl.h> | |
28 | #include <errno.h> | |
29 | #include <stdio.h> | |
30 | #include <unistd.h> | |
31 | #include <stdlib.h> | |
32 | #include <string.h> | |
33 | ||
34 | #undef _GNU_SOURCE | |
35 | #include "perf.h" | |
36 | #include "builtin.h" | |
37 | #include "util/util.h" | |
89c69c0e MH |
38 | #include "util/event.h" |
39 | #include "util/debug.h" | |
4ea42b18 MH |
40 | #include "util/parse-options.h" |
41 | #include "util/parse-events.h" /* For debugfs_path */ | |
42 | #include "util/probe-finder.h" | |
43 | ||
44 | /* Default vmlinux search paths */ | |
45 | #define NR_SEARCH_PATH 3 | |
46 | const char *default_search_path[NR_SEARCH_PATH] = { | |
47 | "/lib/modules/%s/build/vmlinux", /* Custom build kernel */ | |
48 | "/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */ | |
49 | "/boot/vmlinux-debug-%s", /* Ubuntu */ | |
50 | }; | |
51 | ||
52 | #define MAX_PATH_LEN 256 | |
53 | #define MAX_PROBES 128 | |
074fc0e4 | 54 | #define MAX_PROBE_ARGS 128 |
91365bbe | 55 | #define PERFPROBE_GROUP "probe" |
4ea42b18 MH |
56 | |
57 | /* Session management structure */ | |
58 | static struct { | |
59 | char *vmlinux; | |
60 | char *release; | |
23e8ec0d | 61 | int need_dwarf; |
4ea42b18 MH |
62 | int nr_probe; |
63 | struct probe_point probes[MAX_PROBES]; | |
4ea42b18 MH |
64 | } session; |
65 | ||
074fc0e4 | 66 | #define semantic_error(msg ...) die("Semantic error :" msg) |
4ea42b18 | 67 | |
253977b0 MH |
68 | /* Parse probe point. Return 1 if return probe */ |
69 | static void parse_probe_point(char *arg, struct probe_point *pp) | |
70 | { | |
71 | char *ptr, *tmp; | |
12e4db47 | 72 | char c, nc = 0; |
253977b0 MH |
73 | /* |
74 | * <Syntax> | |
75 | * perf probe SRC:LN | |
76 | * perf probe FUNC[+OFFS|%return][@SRC] | |
77 | */ | |
78 | ||
79 | ptr = strpbrk(arg, ":+@%"); | |
80 | if (ptr) { | |
81 | nc = *ptr; | |
82 | *ptr++ = '\0'; | |
83 | } | |
84 | ||
85 | /* Check arg is function or file and copy it */ | |
86 | if (strchr(arg, '.')) /* File */ | |
87 | pp->file = strdup(arg); | |
88 | else /* Function */ | |
89 | pp->function = strdup(arg); | |
90 | DIE_IF(pp->file == NULL && pp->function == NULL); | |
91 | ||
92 | /* Parse other options */ | |
93 | while (ptr) { | |
94 | arg = ptr; | |
95 | c = nc; | |
96 | ptr = strpbrk(arg, ":+@%"); | |
97 | if (ptr) { | |
98 | nc = *ptr; | |
99 | *ptr++ = '\0'; | |
100 | } | |
101 | switch (c) { | |
102 | case ':': /* Line number */ | |
103 | pp->line = strtoul(arg, &tmp, 0); | |
104 | if (*tmp != '\0') | |
105 | semantic_error("There is non-digit charactor" | |
106 | " in line number."); | |
107 | break; | |
108 | case '+': /* Byte offset from a symbol */ | |
109 | pp->offset = strtoul(arg, &tmp, 0); | |
110 | if (*tmp != '\0') | |
111 | semantic_error("There is non-digit charactor" | |
112 | " in offset."); | |
113 | break; | |
114 | case '@': /* File name */ | |
115 | if (pp->file) | |
116 | semantic_error("SRC@SRC is not allowed."); | |
117 | pp->file = strdup(arg); | |
118 | DIE_IF(pp->file == NULL); | |
119 | if (ptr) | |
120 | semantic_error("@SRC must be the last " | |
121 | "option."); | |
122 | break; | |
123 | case '%': /* Probe places */ | |
124 | if (strcmp(arg, "return") == 0) { | |
125 | pp->retprobe = 1; | |
126 | } else /* Others not supported yet */ | |
127 | semantic_error("%%%s is not supported.", arg); | |
128 | break; | |
129 | default: | |
130 | DIE_IF("Program has a bug."); | |
131 | break; | |
132 | } | |
133 | } | |
134 | ||
135 | /* Exclusion check */ | |
b0ef0732 MH |
136 | if (pp->line && pp->offset) |
137 | semantic_error("Offset can't be used with line number."); | |
253977b0 MH |
138 | if (!pp->line && pp->file && !pp->function) |
139 | semantic_error("File always requires line number."); | |
140 | if (pp->offset && !pp->function) | |
141 | semantic_error("Offset requires an entry function."); | |
142 | if (pp->retprobe && !pp->function) | |
143 | semantic_error("Return probe requires an entry function."); | |
b0ef0732 MH |
144 | if ((pp->offset || pp->line) && pp->retprobe) |
145 | semantic_error("Offset/Line can't be used with return probe."); | |
253977b0 MH |
146 | |
147 | pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n", | |
148 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe); | |
149 | } | |
150 | ||
151 | /* Parse an event definition. Note that any error must die. */ | |
152 | static void parse_probe_event(const char *str) | |
4ea42b18 | 153 | { |
74ca4c0e | 154 | char *argv[MAX_PROBE_ARGS + 1]; /* probe + args */ |
4ea42b18 | 155 | int argc, i; |
4ea42b18 | 156 | struct probe_point *pp = &session.probes[session.nr_probe]; |
4ea42b18 | 157 | |
b7cb10e7 | 158 | pr_debug("probe-definition(%d): %s\n", session.nr_probe, str); |
4ea42b18 MH |
159 | if (++session.nr_probe == MAX_PROBES) |
160 | semantic_error("Too many probes"); | |
161 | ||
162 | /* Separate arguments, similar to argv_split */ | |
163 | argc = 0; | |
164 | do { | |
165 | /* Skip separators */ | |
166 | while (isspace(*str)) | |
167 | str++; | |
168 | ||
169 | /* Add an argument */ | |
170 | if (*str != '\0') { | |
171 | const char *s = str; | |
74ca4c0e MH |
172 | /* Check the limit number of arguments */ |
173 | if (argc == MAX_PROBE_ARGS + 1) | |
174 | semantic_error("Too many arguments"); | |
4ea42b18 MH |
175 | |
176 | /* Skip the argument */ | |
177 | while (!isspace(*str) && *str != '\0') | |
178 | str++; | |
179 | ||
180 | /* Duplicate the argument */ | |
181 | argv[argc] = strndup(s, str - s); | |
182 | if (argv[argc] == NULL) | |
074fc0e4 | 183 | die("strndup"); |
74ca4c0e MH |
184 | pr_debug("argv[%d]=%s\n", argc, argv[argc]); |
185 | argc++; | |
186 | ||
4ea42b18 MH |
187 | } |
188 | } while (*str != '\0'); | |
253977b0 MH |
189 | if (!argc) |
190 | semantic_error("An empty argument."); | |
4ea42b18 MH |
191 | |
192 | /* Parse probe point */ | |
253977b0 MH |
193 | parse_probe_point(argv[0], pp); |
194 | free(argv[0]); | |
a225a1d9 | 195 | if (pp->file || pp->line) |
23e8ec0d | 196 | session.need_dwarf = 1; |
4ea42b18 MH |
197 | |
198 | /* Copy arguments */ | |
253977b0 | 199 | pp->nr_args = argc - 1; |
4ea42b18 MH |
200 | if (pp->nr_args > 0) { |
201 | pp->args = (char **)malloc(sizeof(char *) * pp->nr_args); | |
202 | if (!pp->args) | |
074fc0e4 | 203 | die("malloc"); |
253977b0 | 204 | memcpy(pp->args, &argv[1], sizeof(char *) * pp->nr_args); |
4ea42b18 MH |
205 | } |
206 | ||
207 | /* Ensure return probe has no C argument */ | |
23e8ec0d MH |
208 | for (i = 0; i < pp->nr_args; i++) |
209 | if (is_c_varname(pp->args[i])) { | |
253977b0 | 210 | if (pp->retprobe) |
4ea42b18 MH |
211 | semantic_error("You can't specify local" |
212 | " variable for kretprobe"); | |
23e8ec0d MH |
213 | session.need_dwarf = 1; |
214 | } | |
215 | ||
b7cb10e7 | 216 | pr_debug("%d arguments\n", pp->nr_args); |
46ab4926 MH |
217 | } |
218 | ||
253977b0 | 219 | static int opt_add_probe_event(const struct option *opt __used, |
46ab4926 MH |
220 | const char *str, int unset __used) |
221 | { | |
222 | if (str) | |
253977b0 | 223 | parse_probe_event(str); |
4ea42b18 MH |
224 | return 0; |
225 | } | |
226 | ||
23e8ec0d | 227 | #ifndef NO_LIBDWARF |
4ea42b18 MH |
228 | static int open_default_vmlinux(void) |
229 | { | |
230 | struct utsname uts; | |
231 | char fname[MAX_PATH_LEN]; | |
232 | int fd, ret, i; | |
233 | ||
234 | ret = uname(&uts); | |
235 | if (ret) { | |
b7cb10e7 | 236 | pr_debug("uname() failed.\n"); |
4ea42b18 MH |
237 | return -errno; |
238 | } | |
239 | session.release = uts.release; | |
240 | for (i = 0; i < NR_SEARCH_PATH; i++) { | |
241 | ret = snprintf(fname, MAX_PATH_LEN, | |
242 | default_search_path[i], session.release); | |
243 | if (ret >= MAX_PATH_LEN || ret < 0) { | |
b7cb10e7 | 244 | pr_debug("Filename(%d,%s) is too long.\n", i, |
89c69c0e | 245 | uts.release); |
4ea42b18 MH |
246 | errno = E2BIG; |
247 | return -E2BIG; | |
248 | } | |
b7cb10e7 | 249 | pr_debug("try to open %s\n", fname); |
4ea42b18 MH |
250 | fd = open(fname, O_RDONLY); |
251 | if (fd >= 0) | |
252 | break; | |
253 | } | |
254 | return fd; | |
255 | } | |
23e8ec0d | 256 | #endif |
4ea42b18 MH |
257 | |
258 | static const char * const probe_usage[] = { | |
46ab4926 MH |
259 | "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]", |
260 | "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]", | |
4ea42b18 MH |
261 | NULL |
262 | }; | |
263 | ||
264 | static const struct option options[] = { | |
89c69c0e MH |
265 | OPT_BOOLEAN('v', "verbose", &verbose, |
266 | "be more verbose (show parsed arguments, etc)"), | |
23e8ec0d | 267 | #ifndef NO_LIBDWARF |
4ea42b18 MH |
268 | OPT_STRING('k', "vmlinux", &session.vmlinux, "file", |
269 | "vmlinux/module pathname"), | |
23e8ec0d | 270 | #endif |
46ab4926 | 271 | OPT_CALLBACK('a', "add", NULL, |
23e8ec0d | 272 | #ifdef NO_LIBDWARF |
253977b0 | 273 | "FUNC[+OFFS|%return] [ARG ...]", |
23e8ec0d | 274 | #else |
b0ef0732 | 275 | "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]", |
23e8ec0d | 276 | #endif |
4ea42b18 | 277 | "probe point definition, where\n" |
4ea42b18 MH |
278 | "\t\tGRP:\tGroup name (optional)\n" |
279 | "\t\tNAME:\tEvent name\n" | |
280 | "\t\tFUNC:\tFunction name\n" | |
281 | "\t\tOFFS:\tOffset from function entry (in byte)\n" | |
253977b0 | 282 | "\t\t%return:\tPut the probe at function return\n" |
23e8ec0d MH |
283 | #ifdef NO_LIBDWARF |
284 | "\t\tARG:\tProbe argument (only \n" | |
285 | #else | |
4ea42b18 | 286 | "\t\tSRC:\tSource code path\n" |
b0ef0732 MH |
287 | "\t\tRLN:\tRelative line number from function entry.\n" |
288 | "\t\tALN:\tAbsolute line number in file.\n" | |
4ea42b18 | 289 | "\t\tARG:\tProbe argument (local variable name or\n" |
23e8ec0d | 290 | #endif |
4ea42b18 | 291 | "\t\t\tkprobe-tracer argument format is supported.)\n", |
253977b0 | 292 | opt_add_probe_event), |
4ea42b18 MH |
293 | OPT_END() |
294 | }; | |
295 | ||
296 | static int write_new_event(int fd, const char *buf) | |
297 | { | |
298 | int ret; | |
299 | ||
4ea42b18 MH |
300 | ret = write(fd, buf, strlen(buf)); |
301 | if (ret <= 0) | |
a7f4328b MH |
302 | die("Failed to create event."); |
303 | else | |
304 | printf("Added new event: %s\n", buf); | |
4ea42b18 MH |
305 | |
306 | return ret; | |
307 | } | |
308 | ||
309 | #define MAX_CMDLEN 256 | |
310 | ||
253977b0 | 311 | static int synthesize_probe_event(struct probe_point *pp) |
4ea42b18 MH |
312 | { |
313 | char *buf; | |
314 | int i, len, ret; | |
36479484 | 315 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); |
4ea42b18 | 316 | if (!buf) |
36479484 | 317 | die("Failed to allocate memory by zalloc."); |
4ea42b18 MH |
318 | ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); |
319 | if (ret <= 0 || ret >= MAX_CMDLEN) | |
320 | goto error; | |
321 | len = ret; | |
322 | ||
323 | for (i = 0; i < pp->nr_args; i++) { | |
324 | ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s", | |
325 | pp->args[i]); | |
326 | if (ret <= 0 || ret >= MAX_CMDLEN - len) | |
327 | goto error; | |
328 | len += ret; | |
329 | } | |
330 | pp->found = 1; | |
331 | return pp->found; | |
332 | error: | |
333 | free(pp->probes[0]); | |
334 | if (ret > 0) | |
335 | ret = -E2BIG; | |
336 | return ret; | |
337 | } | |
338 | ||
339 | int cmd_probe(int argc, const char **argv, const char *prefix __used) | |
340 | { | |
23e8ec0d | 341 | int i, j, fd, ret; |
4ea42b18 MH |
342 | struct probe_point *pp; |
343 | char buf[MAX_CMDLEN]; | |
344 | ||
345 | argc = parse_options(argc, argv, options, probe_usage, | |
46ab4926 MH |
346 | PARSE_OPT_STOP_AT_NON_OPTION); |
347 | for (i = 0; i < argc; i++) | |
348 | parse_probe_event(argv[i]); | |
349 | ||
350 | if (session.nr_probe == 0) | |
4ea42b18 MH |
351 | usage_with_options(probe_usage, options); |
352 | ||
23e8ec0d | 353 | if (session.need_dwarf) |
a225a1d9 MH |
354 | #ifdef NO_LIBDWARF |
355 | semantic_error("Debuginfo-analysis is not supported"); | |
356 | #else /* !NO_LIBDWARF */ | |
f41b1e43 | 357 | pr_debug("Some probes require debuginfo.\n"); |
4ea42b18 MH |
358 | |
359 | if (session.vmlinux) | |
360 | fd = open(session.vmlinux, O_RDONLY); | |
361 | else | |
362 | fd = open_default_vmlinux(); | |
a225a1d9 MH |
363 | if (fd < 0) { |
364 | if (session.need_dwarf) | |
365 | die("Could not open vmlinux/module file."); | |
366 | ||
367 | pr_warning("Could not open vmlinux/module file." | |
368 | " Try to use symbols.\n"); | |
369 | goto end_dwarf; | |
370 | } | |
4ea42b18 MH |
371 | |
372 | /* Searching probe points */ | |
373 | for (j = 0; j < session.nr_probe; j++) { | |
374 | pp = &session.probes[j]; | |
375 | if (pp->found) | |
376 | continue; | |
377 | ||
378 | lseek(fd, SEEK_SET, 0); | |
379 | ret = find_probepoint(fd, pp); | |
a225a1d9 MH |
380 | if (ret < 0) { |
381 | if (session.need_dwarf) | |
382 | die("Could not analyze debuginfo."); | |
383 | ||
384 | pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n"); | |
385 | break; | |
386 | } | |
387 | if (ret == 0) /* No error but failed to find probe point. */ | |
388 | die("No probe point found."); | |
4ea42b18 MH |
389 | } |
390 | close(fd); | |
391 | ||
a225a1d9 | 392 | end_dwarf: |
23e8ec0d MH |
393 | #endif /* !NO_LIBDWARF */ |
394 | ||
a225a1d9 MH |
395 | /* Synthesize probes without dwarf */ |
396 | for (j = 0; j < session.nr_probe; j++) { | |
397 | pp = &session.probes[j]; | |
398 | if (pp->found) /* This probe is already found. */ | |
399 | continue; | |
400 | ||
401 | ret = synthesize_probe_event(pp); | |
402 | if (ret == -E2BIG) | |
403 | semantic_error("probe point is too long."); | |
404 | else if (ret < 0) | |
405 | die("Failed to synthesize a probe point."); | |
406 | } | |
407 | ||
4ea42b18 MH |
408 | /* Settng up probe points */ |
409 | snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path); | |
410 | fd = open(buf, O_WRONLY, O_APPEND); | |
a7f4328b MH |
411 | if (fd < 0) { |
412 | if (errno == ENOENT) | |
413 | die("kprobe_events file does not exist - please rebuild with CONFIG_KPROBE_TRACER."); | |
414 | else | |
415 | die("Could not open kprobe_events file: %s", | |
416 | strerror(errno)); | |
417 | } | |
4ea42b18 MH |
418 | for (j = 0; j < session.nr_probe; j++) { |
419 | pp = &session.probes[j]; | |
420 | if (pp->found == 1) { | |
253977b0 MH |
421 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n", |
422 | pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP, | |
423 | pp->function, pp->offset, pp->probes[0]); | |
4ea42b18 MH |
424 | write_new_event(fd, buf); |
425 | } else | |
426 | for (i = 0; i < pp->found; i++) { | |
253977b0 MH |
427 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n", |
428 | pp->retprobe ? 'r' : 'p', | |
429 | PERFPROBE_GROUP, | |
430 | pp->function, pp->offset, i, | |
934b1f5f | 431 | pp->probes[i]); |
4ea42b18 MH |
432 | write_new_event(fd, buf); |
433 | } | |
434 | } | |
435 | close(fd); | |
436 | return 0; | |
437 | } | |
438 |