blob: 3370dabed15d085bbb8196695a415035256089c2 [file] [log] [blame]
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -04001/*
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"
Masami Hiramatsu89c69c02009-10-16 20:08:10 -040038#include "util/event.h"
39#include "util/debug.h"
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040040#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
46const 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
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040054#define MAX_PROBE_ARGS 128
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040055
56/* Session management structure */
57static struct {
58 char *vmlinux;
59 char *release;
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040060 int need_dwarf;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040061 int nr_probe;
62 struct probe_point probes[MAX_PROBES];
63 char *events[MAX_PROBES];
64} session;
65
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -040066#define semantic_error(msg ...) die("Semantic error :" msg)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040067
Masami Hiramatsu46ab4922009-10-27 16:43:02 -040068/* Parse a probe point. Note that any error must die. */
69static void parse_probepoint(const char *str)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040070{
71 char *argv[MAX_PROBE_ARGS + 2]; /* Event + probe + args */
72 int argc, i;
73 char *arg, *ptr;
74 struct probe_point *pp = &session.probes[session.nr_probe];
75 char **event = &session.events[session.nr_probe];
76 int retp = 0;
77
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -020078 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040079 if (++session.nr_probe == MAX_PROBES)
80 semantic_error("Too many probes");
81
82 /* Separate arguments, similar to argv_split */
83 argc = 0;
84 do {
85 /* Skip separators */
86 while (isspace(*str))
87 str++;
88
89 /* Add an argument */
90 if (*str != '\0') {
91 const char *s = str;
92
93 /* Skip the argument */
94 while (!isspace(*str) && *str != '\0')
95 str++;
96
97 /* Duplicate the argument */
98 argv[argc] = strndup(s, str - s);
99 if (argv[argc] == NULL)
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400100 die("strndup");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400101 if (++argc == MAX_PROBE_ARGS)
102 semantic_error("Too many arguments");
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200103 pr_debug("argv[%d]=%s\n", argc, argv[argc - 1]);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400104 }
105 } while (*str != '\0');
106 if (argc < 2)
107 semantic_error("Need event-name and probe-point at least.");
108
109 /* Parse the event name */
110 if (argv[0][0] == 'r')
111 retp = 1;
112 else if (argv[0][0] != 'p')
113 semantic_error("You must specify 'p'(kprobe) or"
114 " 'r'(kretprobe) first.");
115 /* TODO: check event name */
116 *event = argv[0];
117
118 /* Parse probe point */
119 arg = argv[1];
120 if (arg[0] == '@') {
121 /* Source Line */
122 arg++;
123 ptr = strchr(arg, ':');
124 if (!ptr || !isdigit(ptr[1]))
125 semantic_error("Line number is required.");
126 *ptr++ = '\0';
127 if (strlen(arg) == 0)
128 semantic_error("No file name.");
129 pp->file = strdup(arg);
130 pp->line = atoi(ptr);
131 if (!pp->file || !pp->line)
132 semantic_error("Failed to parse line.");
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200133 pr_debug("file:%s line:%d\n", pp->file, pp->line);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400134 } else {
135 /* Function name */
136 ptr = strchr(arg, '+');
137 if (ptr) {
138 if (!isdigit(ptr[1]))
139 semantic_error("Offset is required.");
140 *ptr++ = '\0';
141 pp->offset = atoi(ptr);
142 } else
143 ptr = arg;
144 ptr = strchr(ptr, '@');
145 if (ptr) {
146 *ptr++ = '\0';
147 pp->file = strdup(ptr);
148 }
149 pp->function = strdup(arg);
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200150 pr_debug("symbol:%s file:%s offset:%d\n",
151 pp->function, pp->file, pp->offset);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400152 }
153 free(argv[1]);
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400154 if (pp->file)
155 session.need_dwarf = 1;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400156
157 /* Copy arguments */
158 pp->nr_args = argc - 2;
159 if (pp->nr_args > 0) {
160 pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
161 if (!pp->args)
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400162 die("malloc");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400163 memcpy(pp->args, &argv[2], sizeof(char *) * pp->nr_args);
164 }
165
166 /* Ensure return probe has no C argument */
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400167 for (i = 0; i < pp->nr_args; i++)
168 if (is_c_varname(pp->args[i])) {
169 if (retp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400170 semantic_error("You can't specify local"
171 " variable for kretprobe");
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400172 session.need_dwarf = 1;
173 }
174
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200175 pr_debug("%d arguments\n", pp->nr_args);
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400176}
177
178static int opt_add_probepoint(const struct option *opt __used,
179 const char *str, int unset __used)
180{
181 if (str)
182 parse_probepoint(str);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400183 return 0;
184}
185
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400186#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400187static int open_default_vmlinux(void)
188{
189 struct utsname uts;
190 char fname[MAX_PATH_LEN];
191 int fd, ret, i;
192
193 ret = uname(&uts);
194 if (ret) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200195 pr_debug("uname() failed.\n");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400196 return -errno;
197 }
198 session.release = uts.release;
199 for (i = 0; i < NR_SEARCH_PATH; i++) {
200 ret = snprintf(fname, MAX_PATH_LEN,
201 default_search_path[i], session.release);
202 if (ret >= MAX_PATH_LEN || ret < 0) {
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200203 pr_debug("Filename(%d,%s) is too long.\n", i,
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400204 uts.release);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400205 errno = E2BIG;
206 return -E2BIG;
207 }
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200208 pr_debug("try to open %s\n", fname);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400209 fd = open(fname, O_RDONLY);
210 if (fd >= 0)
211 break;
212 }
213 return fd;
214}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400215#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400216
217static const char * const probe_usage[] = {
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400218 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
219 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400220 NULL
221};
222
223static const struct option options[] = {
Masami Hiramatsu89c69c02009-10-16 20:08:10 -0400224 OPT_BOOLEAN('v', "verbose", &verbose,
225 "be more verbose (show parsed arguments, etc)"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400226#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400227 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
228 "vmlinux/module pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400229#endif
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400230 OPT_CALLBACK('a', "add", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400231#ifdef NO_LIBDWARF
232 "p|r:[GRP/]NAME FUNC[+OFFS] [ARG ...]",
233#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400234 "p|r:[GRP/]NAME FUNC[+OFFS][@SRC]|@SRC:LINE [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400235#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400236 "probe point definition, where\n"
237 "\t\tp:\tkprobe probe\n"
238 "\t\tr:\tkretprobe probe\n"
239 "\t\tGRP:\tGroup name (optional)\n"
240 "\t\tNAME:\tEvent name\n"
241 "\t\tFUNC:\tFunction name\n"
242 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400243#ifdef NO_LIBDWARF
244 "\t\tARG:\tProbe argument (only \n"
245#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400246 "\t\tSRC:\tSource code path\n"
247 "\t\tLINE:\tLine number\n"
248 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400249#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400250 "\t\t\tkprobe-tracer argument format is supported.)\n",
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400251 opt_add_probepoint),
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400252 OPT_END()
253};
254
255static int write_new_event(int fd, const char *buf)
256{
257 int ret;
258
259 printf("Adding new event: %s\n", buf);
260 ret = write(fd, buf, strlen(buf));
261 if (ret <= 0)
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400262 die("failed to create event.");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400263
264 return ret;
265}
266
267#define MAX_CMDLEN 256
268
269static int synthesize_probepoint(struct probe_point *pp)
270{
271 char *buf;
272 int i, len, ret;
273 pp->probes[0] = buf = (char *)calloc(MAX_CMDLEN, sizeof(char));
274 if (!buf)
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400275 die("calloc");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400276 ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
277 if (ret <= 0 || ret >= MAX_CMDLEN)
278 goto error;
279 len = ret;
280
281 for (i = 0; i < pp->nr_args; i++) {
282 ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
283 pp->args[i]);
284 if (ret <= 0 || ret >= MAX_CMDLEN - len)
285 goto error;
286 len += ret;
287 }
288 pp->found = 1;
289 return pp->found;
290error:
291 free(pp->probes[0]);
292 if (ret > 0)
293 ret = -E2BIG;
294 return ret;
295}
296
297int cmd_probe(int argc, const char **argv, const char *prefix __used)
298{
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400299 int i, j, fd, ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400300 struct probe_point *pp;
301 char buf[MAX_CMDLEN];
302
303 argc = parse_options(argc, argv, options, probe_usage,
Masami Hiramatsu46ab4922009-10-27 16:43:02 -0400304 PARSE_OPT_STOP_AT_NON_OPTION);
305 for (i = 0; i < argc; i++)
306 parse_probe_event(argv[i]);
307
308 if (session.nr_probe == 0)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400309 usage_with_options(probe_usage, options);
310
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400311#ifdef NO_LIBDWARF
312 if (session.need_dwarf)
313 semantic_error("Dwarf-analysis is not supported");
314#endif
315
316 /* Synthesize probes without dwarf */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400317 for (j = 0; j < session.nr_probe; j++) {
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400318#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400319 if (session.events[j][0] != 'r') {
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400320 session.need_dwarf = 1;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400321 continue;
322 }
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400323#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400324 ret = synthesize_probepoint(&session.probes[j]);
325 if (ret == -E2BIG)
326 semantic_error("probe point is too long.");
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400327 else if (ret < 0)
328 die("snprintf");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400329 }
330
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400331#ifndef NO_LIBDWARF
332 if (!session.need_dwarf)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400333 goto setup_probes;
334
335 if (session.vmlinux)
336 fd = open(session.vmlinux, O_RDONLY);
337 else
338 fd = open_default_vmlinux();
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400339 if (fd < 0)
340 die("vmlinux/module file open");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400341
342 /* Searching probe points */
343 for (j = 0; j < session.nr_probe; j++) {
344 pp = &session.probes[j];
345 if (pp->found)
346 continue;
347
348 lseek(fd, SEEK_SET, 0);
349 ret = find_probepoint(fd, pp);
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400350 if (ret <= 0)
351 die("No probe point found.\n");
Arnaldo Carvalho de Melob7cb10e2009-10-21 17:34:06 -0200352 pr_debug("probe event %s found\n", session.events[j]);
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400353 }
354 close(fd);
355
356setup_probes:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400357#endif /* !NO_LIBDWARF */
358
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400359 /* Settng up probe points */
360 snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path);
361 fd = open(buf, O_WRONLY, O_APPEND);
Masami Hiramatsu074fc0e2009-10-16 20:08:01 -0400362 if (fd < 0)
363 die("kprobe_events open");
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400364 for (j = 0; j < session.nr_probe; j++) {
365 pp = &session.probes[j];
366 if (pp->found == 1) {
367 snprintf(buf, MAX_CMDLEN, "%s %s\n",
368 session.events[j], pp->probes[0]);
369 write_new_event(fd, buf);
370 } else
371 for (i = 0; i < pp->found; i++) {
372 snprintf(buf, MAX_CMDLEN, "%s%d %s\n",
373 session.events[j], i, pp->probes[i]);
374 write_new_event(fd, buf);
375 }
376 }
377 close(fd);
378 return 0;
379}
380