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