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.
24 #include <sys/utsname.h>
25 #include <sys/types.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 */
63 struct probe_point probes[MAX_PROBES];
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)
76 * perf probe FUNC[+OFFS|%return][@SRC]
79 ptr = strpbrk(arg, ":+@%");
85 /* Check arg is function or file and copy it */
86 if (strchr(arg, '.')) /* File */
87 pp->file = strdup(arg);
89 pp->function = strdup(arg);
90 DIE_IF(pp->file == NULL && pp->function == NULL);
92 /* Parse other options */
96 ptr = strpbrk(arg, ":+@%");
102 case ':': /* Line number */
103 pp->line = strtoul(arg, &tmp, 0);
105 semantic_error("There is non-digit charactor"
108 case '+': /* Byte offset from a symbol */
109 pp->offset = strtoul(arg, &tmp, 0);
111 semantic_error("There is non-digit charactor"
114 case '@': /* File name */
116 semantic_error("SRC@SRC is not allowed.");
117 pp->file = strdup(arg);
118 DIE_IF(pp->file == NULL);
120 semantic_error("@SRC must be the last "
123 case '%': /* Probe places */
124 if (strcmp(arg, "return") == 0) {
126 } else /* Others not supported yet */
127 semantic_error("%%%s is not supported.", arg);
130 DIE_IF("Program has a bug.");
135 /* Exclusion check */
136 if (pp->line && pp->function)
137 semantic_error("Function-relative line number is not"
139 if (!pp->line && pp->file && !pp->function)
140 semantic_error("File always requires line number.");
141 if (pp->offset && !pp->function)
142 semantic_error("Offset requires an entry function.");
143 if (pp->retprobe && !pp->function)
144 semantic_error("Return probe requires an entry function.");
145 if (pp->offset && pp->retprobe)
146 semantic_error("Offset can't be used with return probe.");
148 pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
149 pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
152 /* Parse an event definition. Note that any error must die. */
153 static void parse_probe_event(const char *str)
155 char *argv[MAX_PROBE_ARGS + 2]; /* Event + probe + args */
157 struct probe_point *pp = &session.probes[session.nr_probe];
159 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
160 if (++session.nr_probe == MAX_PROBES)
161 semantic_error("Too many probes");
163 /* Separate arguments, similar to argv_split */
166 /* Skip separators */
167 while (isspace(*str))
170 /* Add an argument */
174 /* Skip the argument */
175 while (!isspace(*str) && *str != '\0')
178 /* Duplicate the argument */
179 argv[argc] = strndup(s, str - s);
180 if (argv[argc] == NULL)
182 if (++argc == MAX_PROBE_ARGS)
183 semantic_error("Too many arguments");
184 pr_debug("argv[%d]=%s\n", argc, argv[argc - 1]);
186 } while (*str != '\0');
188 semantic_error("An empty argument.");
190 /* Parse probe point */
191 parse_probe_point(argv[0], pp);
194 session.need_dwarf = 1;
197 pp->nr_args = argc - 1;
198 if (pp->nr_args > 0) {
199 pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
202 memcpy(pp->args, &argv[1], sizeof(char *) * pp->nr_args);
205 /* Ensure return probe has no C argument */
206 for (i = 0; i < pp->nr_args; i++)
207 if (is_c_varname(pp->args[i])) {
209 semantic_error("You can't specify local"
210 " variable for kretprobe");
211 session.need_dwarf = 1;
214 pr_debug("%d arguments\n", pp->nr_args);
217 static int opt_add_probe_event(const struct option *opt __used,
218 const char *str, int unset __used)
221 parse_probe_event(str);
226 static int open_default_vmlinux(void)
229 char fname[MAX_PATH_LEN];
234 pr_debug("uname() failed.\n");
237 session.release = uts.release;
238 for (i = 0; i < NR_SEARCH_PATH; i++) {
239 ret = snprintf(fname, MAX_PATH_LEN,
240 default_search_path[i], session.release);
241 if (ret >= MAX_PATH_LEN || ret < 0) {
242 pr_debug("Filename(%d,%s) is too long.\n", i,
247 pr_debug("try to open %s\n", fname);
248 fd = open(fname, O_RDONLY);
256 static const char * const probe_usage[] = {
257 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
258 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
262 static const struct option options[] = {
263 OPT_BOOLEAN('v', "verbose", &verbose,
264 "be more verbose (show parsed arguments, etc)"),
266 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
267 "vmlinux/module pathname"),
269 OPT_CALLBACK('a', "add", NULL,
271 "FUNC[+OFFS|%return] [ARG ...]",
273 "FUNC[+OFFS|%return][@SRC]|SRC:LINE [ARG ...]",
275 "probe point definition, where\n"
276 "\t\tGRP:\tGroup name (optional)\n"
277 "\t\tNAME:\tEvent name\n"
278 "\t\tFUNC:\tFunction name\n"
279 "\t\tOFFS:\tOffset from function entry (in byte)\n"
280 "\t\t%return:\tPut the probe at function return\n"
282 "\t\tARG:\tProbe argument (only \n"
284 "\t\tSRC:\tSource code path\n"
285 "\t\tLINE:\tLine number\n"
286 "\t\tARG:\tProbe argument (local variable name or\n"
288 "\t\t\tkprobe-tracer argument format is supported.)\n",
289 opt_add_probe_event),
293 static int write_new_event(int fd, const char *buf)
297 printf("Adding new event: %s\n", buf);
298 ret = write(fd, buf, strlen(buf));
300 die("failed to create event.");
305 #define MAX_CMDLEN 256
307 static int synthesize_probe_event(struct probe_point *pp)
311 pp->probes[0] = buf = (char *)calloc(MAX_CMDLEN, sizeof(char));
314 ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
315 if (ret <= 0 || ret >= MAX_CMDLEN)
319 for (i = 0; i < pp->nr_args; i++) {
320 ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
322 if (ret <= 0 || ret >= MAX_CMDLEN - len)
335 int cmd_probe(int argc, const char **argv, const char *prefix __used)
338 struct probe_point *pp;
339 char buf[MAX_CMDLEN];
341 argc = parse_options(argc, argv, options, probe_usage,
342 PARSE_OPT_STOP_AT_NON_OPTION);
343 for (i = 0; i < argc; i++)
344 parse_probe_event(argv[i]);
346 if (session.nr_probe == 0)
347 usage_with_options(probe_usage, options);
350 if (session.need_dwarf)
351 semantic_error("Dwarf-analysis is not supported");
354 /* Synthesize probes without dwarf */
355 for (j = 0; j < session.nr_probe; j++) {
357 if (!session.probes[j].retprobe) {
358 session.need_dwarf = 1;
362 ret = synthesize_probe_event(&session.probes[j]);
364 semantic_error("probe point is too long.");
370 if (!session.need_dwarf)
374 fd = open(session.vmlinux, O_RDONLY);
376 fd = open_default_vmlinux();
378 die("vmlinux/module file open");
380 /* Searching probe points */
381 for (j = 0; j < session.nr_probe; j++) {
382 pp = &session.probes[j];
386 lseek(fd, SEEK_SET, 0);
387 ret = find_probepoint(fd, pp);
389 die("No probe point found.\n");
394 #endif /* !NO_LIBDWARF */
396 /* Settng up probe points */
397 snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path);
398 fd = open(buf, O_WRONLY, O_APPEND);
400 die("kprobe_events open");
401 for (j = 0; j < session.nr_probe; j++) {
402 pp = &session.probes[j];
403 if (pp->found == 1) {
404 snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n",
405 pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP,
406 pp->function, pp->offset, pp->probes[0]);
407 write_new_event(fd, buf);
409 for (i = 0; i < pp->found; i++) {
410 snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n",
411 pp->retprobe ? 'r' : 'p',
413 pp->function, pp->offset, i,
415 write_new_event(fd, buf);