perf probe: Fix add-probe command syntax without --add option
[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 #include "util/probe-event.h"
44
45 /* Default vmlinux search paths */
46 #define NR_SEARCH_PATH 3
47 const char *default_search_path[NR_SEARCH_PATH] = {
48 "/lib/modules/%s/build/vmlinux",                /* Custom build kernel */
49 "/usr/lib/debug/lib/modules/%s/vmlinux",        /* Red Hat debuginfo */
50 "/boot/vmlinux-debug-%s",                       /* Ubuntu */
51 };
52
53 #define MAX_PATH_LEN 256
54 #define MAX_PROBES 128
55
56 /* Session management structure */
57 static struct {
58         char *vmlinux;
59         char *release;
60         int need_dwarf;
61         int nr_probe;
62         struct probe_point probes[MAX_PROBES];
63 } session;
64
65 static bool listing;
66
67 /* Parse an event definition. Note that any error must die. */
68 static void parse_probe_event(const char *str)
69 {
70         struct probe_point *pp = &session.probes[session.nr_probe];
71
72         pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
73         if (++session.nr_probe == MAX_PROBES)
74                 die("Too many probes (> %d) are specified.", MAX_PROBES);
75
76         /* Parse perf-probe event into probe_point */
77         session.need_dwarf = parse_perf_probe_event(str, pp);
78
79         pr_debug("%d arguments\n", pp->nr_args);
80 }
81
82 static void parse_probe_event_argv(int argc, const char **argv)
83 {
84         int i, len;
85         char *buf;
86
87         /* Bind up rest arguments */
88         len = 0;
89         for (i = 0; i < argc; i++)
90                 len += strlen(argv[i]) + 1;
91         buf = zalloc(len + 1);
92         if (!buf)
93                 die("Failed to allocate memory for binding arguments.");
94         len = 0;
95         for (i = 0; i < argc; i++)
96                 len += sprintf(&buf[len], "%s ", argv[i]);
97         parse_probe_event(buf);
98         free(buf);
99 }
100
101 static int opt_add_probe_event(const struct option *opt __used,
102                               const char *str, int unset __used)
103 {
104         if (str)
105                 parse_probe_event(str);
106         return 0;
107 }
108
109 #ifndef NO_LIBDWARF
110 static int open_default_vmlinux(void)
111 {
112         struct utsname uts;
113         char fname[MAX_PATH_LEN];
114         int fd, ret, i;
115
116         ret = uname(&uts);
117         if (ret) {
118                 pr_debug("uname() failed.\n");
119                 return -errno;
120         }
121         session.release = uts.release;
122         for (i = 0; i < NR_SEARCH_PATH; i++) {
123                 ret = snprintf(fname, MAX_PATH_LEN,
124                                default_search_path[i], session.release);
125                 if (ret >= MAX_PATH_LEN || ret < 0) {
126                         pr_debug("Filename(%d,%s) is too long.\n", i,
127                                 uts.release);
128                         errno = E2BIG;
129                         return -E2BIG;
130                 }
131                 pr_debug("try to open %s\n", fname);
132                 fd = open(fname, O_RDONLY);
133                 if (fd >= 0)
134                         break;
135         }
136         return fd;
137 }
138 #endif
139
140 static const char * const probe_usage[] = {
141         "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
142         "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
143         "perf probe --list",
144         NULL
145 };
146
147 static const struct option options[] = {
148         OPT_BOOLEAN('v', "verbose", &verbose,
149                     "be more verbose (show parsed arguments, etc)"),
150 #ifndef NO_LIBDWARF
151         OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
152                 "vmlinux/module pathname"),
153 #endif
154         OPT_BOOLEAN('l', "list", &listing, "list up current probes"),
155         OPT_CALLBACK('a', "add", NULL,
156 #ifdef NO_LIBDWARF
157                 "FUNC[+OFFS|%return] [ARG ...]",
158 #else
159                 "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
160 #endif
161                 "probe point definition, where\n"
162                 "\t\tGRP:\tGroup name (optional)\n"
163                 "\t\tNAME:\tEvent name\n"
164                 "\t\tFUNC:\tFunction name\n"
165                 "\t\tOFFS:\tOffset from function entry (in byte)\n"
166                 "\t\t%return:\tPut the probe at function return\n"
167 #ifdef NO_LIBDWARF
168                 "\t\tARG:\tProbe argument (only \n"
169 #else
170                 "\t\tSRC:\tSource code path\n"
171                 "\t\tRLN:\tRelative line number from function entry.\n"
172                 "\t\tALN:\tAbsolute line number in file.\n"
173                 "\t\tARG:\tProbe argument (local variable name or\n"
174 #endif
175                 "\t\t\tkprobe-tracer argument format.)\n",
176                 opt_add_probe_event),
177         OPT_END()
178 };
179
180 int cmd_probe(int argc, const char **argv, const char *prefix __used)
181 {
182         int i, ret;
183 #ifndef NO_LIBDWARF
184         int fd;
185 #endif
186         struct probe_point *pp;
187
188         argc = parse_options(argc, argv, options, probe_usage,
189                              PARSE_OPT_STOP_AT_NON_OPTION);
190         if (argc > 0)
191                 parse_probe_event_argv(argc, argv);
192
193         if ((session.nr_probe == 0 && !listing) ||
194             (session.nr_probe != 0 && listing))
195                 usage_with_options(probe_usage, options);
196
197         if (listing) {
198                 show_perf_probe_events();
199                 return 0;
200         }
201
202         if (session.need_dwarf)
203 #ifdef NO_LIBDWARF
204                 die("Debuginfo-analysis is not supported");
205 #else   /* !NO_LIBDWARF */
206                 pr_debug("Some probes require debuginfo.\n");
207
208         if (session.vmlinux)
209                 fd = open(session.vmlinux, O_RDONLY);
210         else
211                 fd = open_default_vmlinux();
212         if (fd < 0) {
213                 if (session.need_dwarf)
214                         die("Could not open vmlinux/module file.");
215
216                 pr_debug("Could not open vmlinux/module file."
217                          " Try to use symbols.\n");
218                 goto end_dwarf;
219         }
220
221         /* Searching probe points */
222         for (i = 0; i < session.nr_probe; i++) {
223                 pp = &session.probes[i];
224                 if (pp->found)
225                         continue;
226
227                 lseek(fd, SEEK_SET, 0);
228                 ret = find_probepoint(fd, pp);
229                 if (ret < 0) {
230                         if (session.need_dwarf)
231                                 die("Could not analyze debuginfo.");
232
233                         pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n");
234                         break;
235                 }
236                 if (ret == 0)   /* No error but failed to find probe point. */
237                         die("No probe point found.");
238         }
239         close(fd);
240
241 end_dwarf:
242 #endif /* !NO_LIBDWARF */
243
244         /* Synthesize probes without dwarf */
245         for (i = 0; i < session.nr_probe; i++) {
246                 pp = &session.probes[i];
247                 if (pp->found)  /* This probe is already found. */
248                         continue;
249
250                 ret = synthesize_trace_kprobe_event(pp);
251                 if (ret == -E2BIG)
252                         die("probe point definition becomes too long.");
253                 else if (ret < 0)
254                         die("Failed to synthesize a probe point.");
255         }
256
257         /* Settng up probe points */
258         add_trace_kprobe_events(session.probes, session.nr_probe);
259         return 0;
260 }
261