perf/probes: Improve probe point syntax of perf-probe
[safe/jmp/linux-2.6] / tools / perf / builtin-probe.c
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"
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"
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
54 #define MAX_PROBE_ARGS 128
55 #define PERFPROBE_GROUP "perfprobe"
56
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;
65
66 #define semantic_error(msg ...) die("Semantic error :" msg)
67
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;
72         char c, nc;
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 */
136         if (pp->line && pp->function)
137                 semantic_error("Function-relative line number is not"
138                                 " supported yet.");
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.");
147
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);
150 }
151
152 /* Parse an event definition. Note that any error must die. */
153 static void parse_probe_event(const char *str)
154 {
155         char *argv[MAX_PROBE_ARGS + 2]; /* Event + probe + args */
156         int argc, i;
157         struct probe_point *pp = &session.probes[session.nr_probe];
158
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");
162
163         /* Separate arguments, similar to argv_split */
164         argc = 0;
165         do {
166                 /* Skip separators */
167                 while (isspace(*str))
168                         str++;
169
170                 /* Add an argument */
171                 if (*str != '\0') {
172                         const char *s = str;
173
174                         /* Skip the argument */
175                         while (!isspace(*str) && *str != '\0')
176                                 str++;
177
178                         /* Duplicate the argument */
179                         argv[argc] = strndup(s, str - s);
180                         if (argv[argc] == NULL)
181                                 die("strndup");
182                         if (++argc == MAX_PROBE_ARGS)
183                                 semantic_error("Too many arguments");
184                         pr_debug("argv[%d]=%s\n", argc, argv[argc - 1]);
185                 }
186         } while (*str != '\0');
187         if (!argc)
188                 semantic_error("An empty argument.");
189
190         /* Parse probe point */
191         parse_probe_point(argv[0], pp);
192         free(argv[0]);
193         if (pp->file)
194                 session.need_dwarf = 1;
195
196         /* Copy arguments */
197         pp->nr_args = argc - 1;
198         if (pp->nr_args > 0) {
199                 pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
200                 if (!pp->args)
201                         die("malloc");
202                 memcpy(pp->args, &argv[1], sizeof(char *) * pp->nr_args);
203         }
204
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])) {
208                         if (pp->retprobe)
209                                 semantic_error("You can't specify local"
210                                                 " variable for kretprobe");
211                         session.need_dwarf = 1;
212                 }
213
214         pr_debug("%d arguments\n", pp->nr_args);
215 }
216
217 static int opt_add_probe_event(const struct option *opt __used,
218                               const char *str, int unset __used)
219 {
220         if (str)
221                 parse_probe_event(str);
222         return 0;
223 }
224
225 #ifndef NO_LIBDWARF
226 static int open_default_vmlinux(void)
227 {
228         struct utsname uts;
229         char fname[MAX_PATH_LEN];
230         int fd, ret, i;
231
232         ret = uname(&uts);
233         if (ret) {
234                 pr_debug("uname() failed.\n");
235                 return -errno;
236         }
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,
243                                 uts.release);
244                         errno = E2BIG;
245                         return -E2BIG;
246                 }
247                 pr_debug("try to open %s\n", fname);
248                 fd = open(fname, O_RDONLY);
249                 if (fd >= 0)
250                         break;
251         }
252         return fd;
253 }
254 #endif
255
256 static const char * const probe_usage[] = {
257         "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
258         "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
259         NULL
260 };
261
262 static const struct option options[] = {
263         OPT_BOOLEAN('v', "verbose", &verbose,
264                     "be more verbose (show parsed arguments, etc)"),
265 #ifndef NO_LIBDWARF
266         OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
267                 "vmlinux/module pathname"),
268 #endif
269         OPT_CALLBACK('a', "add", NULL,
270 #ifdef NO_LIBDWARF
271                 "FUNC[+OFFS|%return] [ARG ...]",
272 #else
273                 "FUNC[+OFFS|%return][@SRC]|SRC:LINE [ARG ...]",
274 #endif
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"
281 #ifdef NO_LIBDWARF
282                 "\t\tARG:\tProbe argument (only \n"
283 #else
284                 "\t\tSRC:\tSource code path\n"
285                 "\t\tLINE:\tLine number\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()
291 };
292
293 static int write_new_event(int fd, const char *buf)
294 {
295         int ret;
296
297         printf("Adding new event: %s\n", buf);
298         ret = write(fd, buf, strlen(buf));
299         if (ret <= 0)
300                 die("failed to create event.");
301
302         return ret;
303 }
304
305 #define MAX_CMDLEN 256
306
307 static int synthesize_probe_event(struct probe_point *pp)
308 {
309         char *buf;
310         int i, len, ret;
311         pp->probes[0] = buf = (char *)calloc(MAX_CMDLEN, sizeof(char));
312         if (!buf)
313                 die("calloc");
314         ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
315         if (ret <= 0 || ret >= MAX_CMDLEN)
316                 goto error;
317         len = ret;
318
319         for (i = 0; i < pp->nr_args; i++) {
320                 ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
321                                pp->args[i]);
322                 if (ret <= 0 || ret >= MAX_CMDLEN - len)
323                         goto error;
324                 len += ret;
325         }
326         pp->found = 1;
327         return pp->found;
328 error:
329         free(pp->probes[0]);
330         if (ret > 0)
331                 ret = -E2BIG;
332         return ret;
333 }
334
335 int cmd_probe(int argc, const char **argv, const char *prefix __used)
336 {
337         int i, j, fd, ret;
338         struct probe_point *pp;
339         char buf[MAX_CMDLEN];
340
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]);
345
346         if (session.nr_probe == 0)
347                 usage_with_options(probe_usage, options);
348
349 #ifdef NO_LIBDWARF
350         if (session.need_dwarf)
351                 semantic_error("Dwarf-analysis is not supported");
352 #endif
353
354         /* Synthesize probes without dwarf */
355         for (j = 0; j < session.nr_probe; j++) {
356 #ifndef NO_LIBDWARF
357                 if (!session.probes[j].retprobe) {
358                         session.need_dwarf = 1;
359                         continue;
360                 }
361 #endif
362                 ret = synthesize_probe_event(&session.probes[j]);
363                 if (ret == -E2BIG)
364                         semantic_error("probe point is too long.");
365                 else if (ret < 0)
366                         die("snprintf");
367         }
368
369 #ifndef NO_LIBDWARF
370         if (!session.need_dwarf)
371                 goto setup_probes;
372
373         if (session.vmlinux)
374                 fd = open(session.vmlinux, O_RDONLY);
375         else
376                 fd = open_default_vmlinux();
377         if (fd < 0)
378                 die("vmlinux/module file open");
379
380         /* Searching probe points */
381         for (j = 0; j < session.nr_probe; j++) {
382                 pp = &session.probes[j];
383                 if (pp->found)
384                         continue;
385
386                 lseek(fd, SEEK_SET, 0);
387                 ret = find_probepoint(fd, pp);
388                 if (ret <= 0)
389                         die("No probe point found.\n");
390         }
391         close(fd);
392
393 setup_probes:
394 #endif /* !NO_LIBDWARF */
395
396         /* Settng up probe points */
397         snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path);
398         fd = open(buf, O_WRONLY, O_APPEND);
399         if (fd < 0)
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);
408                 } else
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',
412                                         PERFPROBE_GROUP,
413                                         pp->function, pp->offset, i,
414                                         pp->probes[0]);
415                                 write_new_event(fd, buf);
416                         }
417         }
418         close(fd);
419         return 0;
420 }
421