blob: 73c883b715cc461c4454ac3c09e11fcc23323061 [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"
38#include "util/parse-options.h"
39#include "util/parse-events.h" /* For debugfs_path */
40#include "util/probe-finder.h"
41
42/* Default vmlinux search paths */
43#define NR_SEARCH_PATH 3
44const char *default_search_path[NR_SEARCH_PATH] = {
45"/lib/modules/%s/build/vmlinux", /* Custom build kernel */
46"/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */
47"/boot/vmlinux-debug-%s", /* Ubuntu */
48};
49
50#define MAX_PATH_LEN 256
51#define MAX_PROBES 128
52
53/* Session management structure */
54static struct {
55 char *vmlinux;
56 char *release;
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -040057 int need_dwarf;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -040058 int nr_probe;
59 struct probe_point probes[MAX_PROBES];
60 char *events[MAX_PROBES];
61} session;
62
63static void semantic_error(const char *msg)
64{
65 fprintf(stderr, "Semantic error: %s\n", msg);
66 exit(1);
67}
68
69static void perror_exit(const char *msg)
70{
71 perror(msg);
72 exit(1);
73}
74
75#define MAX_PROBE_ARGS 128
76
77static int parse_probepoint(const struct option *opt __used,
78 const char *str, int unset __used)
79{
80 char *argv[MAX_PROBE_ARGS + 2]; /* Event + probe + args */
81 int argc, i;
82 char *arg, *ptr;
83 struct probe_point *pp = &session.probes[session.nr_probe];
84 char **event = &session.events[session.nr_probe];
85 int retp = 0;
86
87 if (!str) /* The end of probe points */
88 return 0;
89
90 debug("Probe-define(%d): %s\n", session.nr_probe, str);
91 if (++session.nr_probe == MAX_PROBES)
92 semantic_error("Too many probes");
93
94 /* Separate arguments, similar to argv_split */
95 argc = 0;
96 do {
97 /* Skip separators */
98 while (isspace(*str))
99 str++;
100
101 /* Add an argument */
102 if (*str != '\0') {
103 const char *s = str;
104
105 /* Skip the argument */
106 while (!isspace(*str) && *str != '\0')
107 str++;
108
109 /* Duplicate the argument */
110 argv[argc] = strndup(s, str - s);
111 if (argv[argc] == NULL)
112 perror_exit("strndup");
113 if (++argc == MAX_PROBE_ARGS)
114 semantic_error("Too many arguments");
115 debug("argv[%d]=%s\n", argc, argv[argc - 1]);
116 }
117 } while (*str != '\0');
118 if (argc < 2)
119 semantic_error("Need event-name and probe-point at least.");
120
121 /* Parse the event name */
122 if (argv[0][0] == 'r')
123 retp = 1;
124 else if (argv[0][0] != 'p')
125 semantic_error("You must specify 'p'(kprobe) or"
126 " 'r'(kretprobe) first.");
127 /* TODO: check event name */
128 *event = argv[0];
129
130 /* Parse probe point */
131 arg = argv[1];
132 if (arg[0] == '@') {
133 /* Source Line */
134 arg++;
135 ptr = strchr(arg, ':');
136 if (!ptr || !isdigit(ptr[1]))
137 semantic_error("Line number is required.");
138 *ptr++ = '\0';
139 if (strlen(arg) == 0)
140 semantic_error("No file name.");
141 pp->file = strdup(arg);
142 pp->line = atoi(ptr);
143 if (!pp->file || !pp->line)
144 semantic_error("Failed to parse line.");
145 debug("file:%s line:%d\n", pp->file, pp->line);
146 } else {
147 /* Function name */
148 ptr = strchr(arg, '+');
149 if (ptr) {
150 if (!isdigit(ptr[1]))
151 semantic_error("Offset is required.");
152 *ptr++ = '\0';
153 pp->offset = atoi(ptr);
154 } else
155 ptr = arg;
156 ptr = strchr(ptr, '@');
157 if (ptr) {
158 *ptr++ = '\0';
159 pp->file = strdup(ptr);
160 }
161 pp->function = strdup(arg);
162 debug("symbol:%s file:%s offset:%d\n",
163 pp->function, pp->file, pp->offset);
164 }
165 free(argv[1]);
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400166 if (pp->file)
167 session.need_dwarf = 1;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400168
169 /* Copy arguments */
170 pp->nr_args = argc - 2;
171 if (pp->nr_args > 0) {
172 pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
173 if (!pp->args)
174 perror_exit("malloc");
175 memcpy(pp->args, &argv[2], sizeof(char *) * pp->nr_args);
176 }
177
178 /* Ensure return probe has no C argument */
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400179 for (i = 0; i < pp->nr_args; i++)
180 if (is_c_varname(pp->args[i])) {
181 if (retp)
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400182 semantic_error("You can't specify local"
183 " variable for kretprobe");
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400184 session.need_dwarf = 1;
185 }
186
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400187 debug("%d arguments\n", pp->nr_args);
188 return 0;
189}
190
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400191#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400192static int open_default_vmlinux(void)
193{
194 struct utsname uts;
195 char fname[MAX_PATH_LEN];
196 int fd, ret, i;
197
198 ret = uname(&uts);
199 if (ret) {
200 debug("uname() failed.\n");
201 return -errno;
202 }
203 session.release = uts.release;
204 for (i = 0; i < NR_SEARCH_PATH; i++) {
205 ret = snprintf(fname, MAX_PATH_LEN,
206 default_search_path[i], session.release);
207 if (ret >= MAX_PATH_LEN || ret < 0) {
208 debug("Filename(%d,%s) is too long.\n", i, uts.release);
209 errno = E2BIG;
210 return -E2BIG;
211 }
212 debug("try to open %s\n", fname);
213 fd = open(fname, O_RDONLY);
214 if (fd >= 0)
215 break;
216 }
217 return fd;
218}
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400219#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400220
221static const char * const probe_usage[] = {
222 "perf probe [<options>] -P 'PROBEDEF' [-P 'PROBEDEF' ...]",
223 NULL
224};
225
226static const struct option options[] = {
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400227#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400228 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
229 "vmlinux/module pathname"),
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400230#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400231 OPT_CALLBACK('P', "probe", NULL,
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400232#ifdef NO_LIBDWARF
233 "p|r:[GRP/]NAME FUNC[+OFFS] [ARG ...]",
234#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400235 "p|r:[GRP/]NAME FUNC[+OFFS][@SRC]|@SRC:LINE [ARG ...]",
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400236#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400237 "probe point definition, where\n"
238 "\t\tp:\tkprobe probe\n"
239 "\t\tr:\tkretprobe probe\n"
240 "\t\tGRP:\tGroup name (optional)\n"
241 "\t\tNAME:\tEvent name\n"
242 "\t\tFUNC:\tFunction name\n"
243 "\t\tOFFS:\tOffset from function entry (in byte)\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400244#ifdef NO_LIBDWARF
245 "\t\tARG:\tProbe argument (only \n"
246#else
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400247 "\t\tSRC:\tSource code path\n"
248 "\t\tLINE:\tLine number\n"
249 "\t\tARG:\tProbe argument (local variable name or\n"
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400250#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400251 "\t\t\tkprobe-tracer argument format is supported.)\n",
252 parse_probepoint),
253 OPT_END()
254};
255
256static int write_new_event(int fd, const char *buf)
257{
258 int ret;
259
260 printf("Adding new event: %s\n", buf);
261 ret = write(fd, buf, strlen(buf));
262 if (ret <= 0)
263 perror("Error: Failed to create event");
264
265 return ret;
266}
267
268#define MAX_CMDLEN 256
269
270static int synthesize_probepoint(struct probe_point *pp)
271{
272 char *buf;
273 int i, len, ret;
274 pp->probes[0] = buf = (char *)calloc(MAX_CMDLEN, sizeof(char));
275 if (!buf)
276 perror_exit("calloc");
277 ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
278 if (ret <= 0 || ret >= MAX_CMDLEN)
279 goto error;
280 len = ret;
281
282 for (i = 0; i < pp->nr_args; i++) {
283 ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
284 pp->args[i]);
285 if (ret <= 0 || ret >= MAX_CMDLEN - len)
286 goto error;
287 len += ret;
288 }
289 pp->found = 1;
290 return pp->found;
291error:
292 free(pp->probes[0]);
293 if (ret > 0)
294 ret = -E2BIG;
295 return ret;
296}
297
298int cmd_probe(int argc, const char **argv, const char *prefix __used)
299{
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400300 int i, j, fd, ret;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400301 struct probe_point *pp;
302 char buf[MAX_CMDLEN];
303
304 argc = parse_options(argc, argv, options, probe_usage,
305 PARSE_OPT_STOP_AT_NON_OPTION);
306 if (argc || session.nr_probe == 0)
307 usage_with_options(probe_usage, options);
308
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400309#ifdef NO_LIBDWARF
310 if (session.need_dwarf)
311 semantic_error("Dwarf-analysis is not supported");
312#endif
313
314 /* Synthesize probes without dwarf */
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400315 for (j = 0; j < session.nr_probe; j++) {
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400316#ifndef NO_LIBDWARF
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400317 if (session.events[j][0] != 'r') {
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400318 session.need_dwarf = 1;
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400319 continue;
320 }
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400321#endif
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400322 ret = synthesize_probepoint(&session.probes[j]);
323 if (ret == -E2BIG)
324 semantic_error("probe point is too long.");
325 else if (ret < 0) {
326 perror("snprintf");
327 return -1;
328 }
329 }
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();
339 if (fd < 0) {
340 perror("vmlinux/module file open");
341 return -1;
342 }
343
344 /* Searching probe points */
345 for (j = 0; j < session.nr_probe; j++) {
346 pp = &session.probes[j];
347 if (pp->found)
348 continue;
349
350 lseek(fd, SEEK_SET, 0);
351 ret = find_probepoint(fd, pp);
352 if (ret <= 0) {
353 fprintf(stderr, "Error: No probe point found.\n");
354 return -1;
355 }
356 debug("probe event %s found\n", session.events[j]);
357 }
358 close(fd);
359
360setup_probes:
Masami Hiramatsu23e8ec02009-10-07 18:28:30 -0400361#endif /* !NO_LIBDWARF */
362
Masami Hiramatsu4ea42b12009-10-08 17:17:38 -0400363 /* Settng up probe points */
364 snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path);
365 fd = open(buf, O_WRONLY, O_APPEND);
366 if (fd < 0) {
367 perror("kprobe_events open");
368 return -1;
369 }
370 for (j = 0; j < session.nr_probe; j++) {
371 pp = &session.probes[j];
372 if (pp->found == 1) {
373 snprintf(buf, MAX_CMDLEN, "%s %s\n",
374 session.events[j], pp->probes[0]);
375 write_new_event(fd, buf);
376 } else
377 for (i = 0; i < pp->found; i++) {
378 snprintf(buf, MAX_CMDLEN, "%s%d %s\n",
379 session.events[j], i, pp->probes[i]);
380 write_new_event(fd, buf);
381 }
382 }
383 close(fd);
384 return 0;
385}
386