perf/probes: Improve error messages
[linux-2.6/linux-2.6-openrd.git] / tools / perf / builtin-probe.c
blob65bcaed0ef49cd2cbab9aa31058fb0443cdc3a18
1 /*
2 * builtin-probe.c
4 * Builtin probe command: Set up probe events by C expression
6 * Written by Masami Hiramatsu <mhiramat@redhat.com>
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.
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.
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.
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>
34 #undef _GNU_SOURCE
35 #include "perf.h"
36 #include "builtin.h"
37 #include "util/util.h"
38 #include "util/event.h"
39 #include "util/debug.h"
40 #include "util/parse-options.h"
41 #include "util/parse-events.h" /* For debugfs_path */
42 #include "util/probe-finder.h"
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 */
52 #define MAX_PATH_LEN 256
53 #define MAX_PROBES 128
54 #define MAX_PROBE_ARGS 128
55 #define PERFPROBE_GROUP "perfprobe"
57 /* Session management structure */
58 static struct {
59 char *vmlinux;
60 char *release;
61 int need_dwarf;
62 int nr_probe;
63 struct probe_point probes[MAX_PROBES];
64 } session;
66 #define semantic_error(msg ...) die("Semantic error :" msg)
68 /* Parse probe point. Return 1 if return probe */
69 static void parse_probe_point(char *arg, struct probe_point *pp)
71 char *ptr, *tmp;
72 char c, nc = 0;
74 * <Syntax>
75 * perf probe SRC:LN
76 * perf probe FUNC[+OFFS|%return][@SRC]
79 ptr = strpbrk(arg, ":+@%");
80 if (ptr) {
81 nc = *ptr;
82 *ptr++ = '\0';
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);
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';
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;
135 /* Exclusion check */
136 if (pp->line && pp->offset)
137 semantic_error("Offset can't be used with line number.");
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.");
144 if ((pp->offset || pp->line) && pp->retprobe)
145 semantic_error("Offset/Line can't be used with return probe.");
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);
151 /* Parse an event definition. Note that any error must die. */
152 static void parse_probe_event(const char *str)
154 char *argv[MAX_PROBE_ARGS + 2]; /* Event + probe + args */
155 int argc, i;
156 struct probe_point *pp = &session.probes[session.nr_probe];
158 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
159 if (++session.nr_probe == MAX_PROBES)
160 semantic_error("Too many probes");
162 /* Separate arguments, similar to argv_split */
163 argc = 0;
164 do {
165 /* Skip separators */
166 while (isspace(*str))
167 str++;
169 /* Add an argument */
170 if (*str != '\0') {
171 const char *s = str;
173 /* Skip the argument */
174 while (!isspace(*str) && *str != '\0')
175 str++;
177 /* Duplicate the argument */
178 argv[argc] = strndup(s, str - s);
179 if (argv[argc] == NULL)
180 die("strndup");
181 if (++argc == MAX_PROBE_ARGS)
182 semantic_error("Too many arguments");
183 pr_debug("argv[%d]=%s\n", argc, argv[argc - 1]);
185 } while (*str != '\0');
186 if (!argc)
187 semantic_error("An empty argument.");
189 /* Parse probe point */
190 parse_probe_point(argv[0], pp);
191 free(argv[0]);
192 if (pp->file)
193 session.need_dwarf = 1;
195 /* Copy arguments */
196 pp->nr_args = argc - 1;
197 if (pp->nr_args > 0) {
198 pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
199 if (!pp->args)
200 die("malloc");
201 memcpy(pp->args, &argv[1], sizeof(char *) * pp->nr_args);
204 /* Ensure return probe has no C argument */
205 for (i = 0; i < pp->nr_args; i++)
206 if (is_c_varname(pp->args[i])) {
207 if (pp->retprobe)
208 semantic_error("You can't specify local"
209 " variable for kretprobe");
210 session.need_dwarf = 1;
213 pr_debug("%d arguments\n", pp->nr_args);
216 static int opt_add_probe_event(const struct option *opt __used,
217 const char *str, int unset __used)
219 if (str)
220 parse_probe_event(str);
221 return 0;
224 #ifndef NO_LIBDWARF
225 static int open_default_vmlinux(void)
227 struct utsname uts;
228 char fname[MAX_PATH_LEN];
229 int fd, ret, i;
231 ret = uname(&uts);
232 if (ret) {
233 pr_debug("uname() failed.\n");
234 return -errno;
236 session.release = uts.release;
237 for (i = 0; i < NR_SEARCH_PATH; i++) {
238 ret = snprintf(fname, MAX_PATH_LEN,
239 default_search_path[i], session.release);
240 if (ret >= MAX_PATH_LEN || ret < 0) {
241 pr_debug("Filename(%d,%s) is too long.\n", i,
242 uts.release);
243 errno = E2BIG;
244 return -E2BIG;
246 pr_debug("try to open %s\n", fname);
247 fd = open(fname, O_RDONLY);
248 if (fd >= 0)
249 break;
251 return fd;
253 #endif
255 static const char * const probe_usage[] = {
256 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
257 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
258 NULL
261 static const struct option options[] = {
262 OPT_BOOLEAN('v', "verbose", &verbose,
263 "be more verbose (show parsed arguments, etc)"),
264 #ifndef NO_LIBDWARF
265 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
266 "vmlinux/module pathname"),
267 #endif
268 OPT_CALLBACK('a', "add", NULL,
269 #ifdef NO_LIBDWARF
270 "FUNC[+OFFS|%return] [ARG ...]",
271 #else
272 "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
273 #endif
274 "probe point definition, where\n"
275 "\t\tGRP:\tGroup name (optional)\n"
276 "\t\tNAME:\tEvent name\n"
277 "\t\tFUNC:\tFunction name\n"
278 "\t\tOFFS:\tOffset from function entry (in byte)\n"
279 "\t\t%return:\tPut the probe at function return\n"
280 #ifdef NO_LIBDWARF
281 "\t\tARG:\tProbe argument (only \n"
282 #else
283 "\t\tSRC:\tSource code path\n"
284 "\t\tRLN:\tRelative line number from function entry.\n"
285 "\t\tALN:\tAbsolute line number in file.\n"
286 "\t\tARG:\tProbe argument (local variable name or\n"
287 #endif
288 "\t\t\tkprobe-tracer argument format is supported.)\n",
289 opt_add_probe_event),
290 OPT_END()
293 static int write_new_event(int fd, const char *buf)
295 int ret;
297 ret = write(fd, buf, strlen(buf));
298 if (ret <= 0)
299 die("Failed to create event.");
300 else
301 printf("Added new event: %s\n", buf);
303 return ret;
306 #define MAX_CMDLEN 256
308 static int synthesize_probe_event(struct probe_point *pp)
310 char *buf;
311 int i, len, ret;
312 pp->probes[0] = buf = (char *)calloc(MAX_CMDLEN, sizeof(char));
313 if (!buf)
314 die("Failed to allocate memory by calloc.");
315 ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
316 if (ret <= 0 || ret >= MAX_CMDLEN)
317 goto error;
318 len = ret;
320 for (i = 0; i < pp->nr_args; i++) {
321 ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
322 pp->args[i]);
323 if (ret <= 0 || ret >= MAX_CMDLEN - len)
324 goto error;
325 len += ret;
327 pp->found = 1;
328 return pp->found;
329 error:
330 free(pp->probes[0]);
331 if (ret > 0)
332 ret = -E2BIG;
333 return ret;
336 int cmd_probe(int argc, const char **argv, const char *prefix __used)
338 int i, j, fd, ret;
339 struct probe_point *pp;
340 char buf[MAX_CMDLEN];
342 argc = parse_options(argc, argv, options, probe_usage,
343 PARSE_OPT_STOP_AT_NON_OPTION);
344 for (i = 0; i < argc; i++)
345 parse_probe_event(argv[i]);
347 if (session.nr_probe == 0)
348 usage_with_options(probe_usage, options);
350 #ifdef NO_LIBDWARF
351 if (session.need_dwarf)
352 semantic_error("Dwarf-analysis is not supported");
353 #endif
355 /* Synthesize probes without dwarf */
356 for (j = 0; j < session.nr_probe; j++) {
357 #ifndef NO_LIBDWARF
358 if (!session.probes[j].retprobe) {
359 session.need_dwarf = 1;
360 continue;
362 #endif
363 ret = synthesize_probe_event(&session.probes[j]);
364 if (ret == -E2BIG)
365 semantic_error("probe point is too long.");
366 else if (ret < 0)
367 die("Failed to synthesize a probe point.");
370 #ifndef NO_LIBDWARF
371 if (!session.need_dwarf)
372 goto setup_probes;
374 if (session.vmlinux)
375 fd = open(session.vmlinux, O_RDONLY);
376 else
377 fd = open_default_vmlinux();
378 if (fd < 0)
379 die("Could not open vmlinux/module file.");
381 /* Searching probe points */
382 for (j = 0; j < session.nr_probe; j++) {
383 pp = &session.probes[j];
384 if (pp->found)
385 continue;
387 lseek(fd, SEEK_SET, 0);
388 ret = find_probepoint(fd, pp);
389 if (ret <= 0)
390 die("No probe point found.\n");
392 close(fd);
394 setup_probes:
395 #endif /* !NO_LIBDWARF */
397 /* Settng up probe points */
398 snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path);
399 fd = open(buf, O_WRONLY, O_APPEND);
400 if (fd < 0) {
401 if (errno == ENOENT)
402 die("kprobe_events file does not exist - please rebuild with CONFIG_KPROBE_TRACER.");
403 else
404 die("Could not open kprobe_events file: %s",
405 strerror(errno));
407 for (j = 0; j < session.nr_probe; j++) {
408 pp = &session.probes[j];
409 if (pp->found == 1) {
410 snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n",
411 pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP,
412 pp->function, pp->offset, pp->probes[0]);
413 write_new_event(fd, buf);
414 } else
415 for (i = 0; i < pp->found; i++) {
416 snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n",
417 pp->retprobe ? 'r' : 'p',
418 PERFPROBE_GROUP,
419 pp->function, pp->offset, i,
420 pp->probes[0]);
421 write_new_event(fd, buf);
424 close(fd);
425 return 0;