perf probe: Reject second attempt of adding same-name event
[deliverable/linux.git] / tools / perf / builtin-probe.c
CommitLineData
4ea42b18
MH
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"
fa28244d 38#include "util/strlist.h"
89c69c0e
MH
39#include "util/event.h"
40#include "util/debug.h"
4ea42b18
MH
41#include "util/parse-options.h"
42#include "util/parse-events.h" /* For debugfs_path */
43#include "util/probe-finder.h"
50656eec 44#include "util/probe-event.h"
4ea42b18
MH
45
46/* Default vmlinux search paths */
f984f03d 47#define NR_SEARCH_PATH 4
4ea42b18
MH
48const 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 */
f984f03d 52"./vmlinux", /* CWD */
4ea42b18
MH
53};
54
55#define MAX_PATH_LEN 256
56#define MAX_PROBES 128
57
58/* Session management structure */
59static struct {
60 char *vmlinux;
61 char *release;
fac13fd5
MH
62 bool need_dwarf;
63 bool list_events;
d761b08b 64 bool force_add;
4ea42b18
MH
65 int nr_probe;
66 struct probe_point probes[MAX_PROBES];
fa28244d 67 struct strlist *dellist;
4ea42b18
MH
68} session;
69
4de189fe 70
253977b0
MH
71/* Parse an event definition. Note that any error must die. */
72static void parse_probe_event(const char *str)
4ea42b18 73{
4ea42b18 74 struct probe_point *pp = &session.probes[session.nr_probe];
4ea42b18 75
b7cb10e7 76 pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
4ea42b18 77 if (++session.nr_probe == MAX_PROBES)
50656eec 78 die("Too many probes (> %d) are specified.", MAX_PROBES);
4ea42b18 79
50656eec 80 /* Parse perf-probe event into probe_point */
fac13fd5 81 parse_perf_probe_event(str, pp, &session.need_dwarf);
23e8ec0d 82
b7cb10e7 83 pr_debug("%d arguments\n", pp->nr_args);
46ab4926
MH
84}
85
d1bde3f7
MH
86static 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
253977b0 105static int opt_add_probe_event(const struct option *opt __used,
46ab4926
MH
106 const char *str, int unset __used)
107{
108 if (str)
253977b0 109 parse_probe_event(str);
4ea42b18
MH
110 return 0;
111}
112
fa28244d
MH
113static 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
23e8ec0d 124#ifndef NO_LIBDWARF
4ea42b18
MH
125static 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) {
b7cb10e7 133 pr_debug("uname() failed.\n");
4ea42b18
MH
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) {
b7cb10e7 141 pr_debug("Filename(%d,%s) is too long.\n", i,
89c69c0e 142 uts.release);
4ea42b18
MH
143 errno = E2BIG;
144 return -E2BIG;
145 }
b7cb10e7 146 pr_debug("try to open %s\n", fname);
4ea42b18
MH
147 fd = open(fname, O_RDONLY);
148 if (fd >= 0)
149 break;
150 }
151 return fd;
152}
23e8ec0d 153#endif
4ea42b18
MH
154
155static const char * const probe_usage[] = {
46ab4926
MH
156 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
157 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
fa28244d 158 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
4de189fe 159 "perf probe --list",
4ea42b18
MH
160 NULL
161};
162
163static const struct option options[] = {
89c69c0e
MH
164 OPT_BOOLEAN('v', "verbose", &verbose,
165 "be more verbose (show parsed arguments, etc)"),
23e8ec0d 166#ifndef NO_LIBDWARF
4ea42b18
MH
167 OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
168 "vmlinux/module pathname"),
23e8ec0d 169#endif
fac13fd5
MH
170 OPT_BOOLEAN('l', "list", &session.list_events,
171 "list up current probe events"),
fa28244d
MH
172 OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
173 opt_del_probe_event),
46ab4926 174 OPT_CALLBACK('a', "add", NULL,
23e8ec0d 175#ifdef NO_LIBDWARF
af663d75 176 "[EVENT=]FUNC[+OFFS|%return] [ARG ...]",
23e8ec0d 177#else
af663d75 178 "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
23e8ec0d 179#endif
4ea42b18 180 "probe point definition, where\n"
af663d75
MH
181 "\t\tGROUP:\tGroup name (optional)\n"
182 "\t\tEVENT:\tEvent name\n"
4ea42b18
MH
183 "\t\tFUNC:\tFunction name\n"
184 "\t\tOFFS:\tOffset from function entry (in byte)\n"
253977b0 185 "\t\t%return:\tPut the probe at function return\n"
23e8ec0d
MH
186#ifdef NO_LIBDWARF
187 "\t\tARG:\tProbe argument (only \n"
188#else
4ea42b18 189 "\t\tSRC:\tSource code path\n"
b0ef0732
MH
190 "\t\tRLN:\tRelative line number from function entry.\n"
191 "\t\tALN:\tAbsolute line number in file.\n"
4ea42b18 192 "\t\tARG:\tProbe argument (local variable name or\n"
23e8ec0d 193#endif
50656eec 194 "\t\t\tkprobe-tracer argument format.)\n",
253977b0 195 opt_add_probe_event),
d761b08b
MH
196 OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
197 " with existing name"),
4ea42b18
MH
198 OPT_END()
199};
200
4ea42b18
MH
201int cmd_probe(int argc, const char **argv, const char *prefix __used)
202{
d1bde3f7 203 int i, ret;
bdad0db7
XG
204#ifndef NO_LIBDWARF
205 int fd;
206#endif
4ea42b18 207 struct probe_point *pp;
4ea42b18
MH
208
209 argc = parse_options(argc, argv, options, probe_usage,
46ab4926 210 PARSE_OPT_STOP_AT_NON_OPTION);
ce11a603
MH
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 }
d1bde3f7 216 parse_probe_event_argv(argc, argv);
ce11a603 217 }
46ab4926 218
fac13fd5 219 if ((!session.nr_probe && !session.dellist && !session.list_events))
4ea42b18
MH
220 usage_with_options(probe_usage, options);
221
fac13fd5 222 if (session.list_events) {
fa28244d
MH
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 }
4de189fe
MH
228 show_perf_probe_events();
229 return 0;
230 }
231
fa28244d
MH
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
23e8ec0d 239 if (session.need_dwarf)
a225a1d9 240#ifdef NO_LIBDWARF
50656eec 241 die("Debuginfo-analysis is not supported");
a225a1d9 242#else /* !NO_LIBDWARF */
f41b1e43 243 pr_debug("Some probes require debuginfo.\n");
4ea42b18 244
f984f03d
MH
245 if (session.vmlinux) {
246 pr_debug("Try to open %s.", session.vmlinux);
4ea42b18 247 fd = open(session.vmlinux, O_RDONLY);
f984f03d 248 } else
4ea42b18 249 fd = open_default_vmlinux();
a225a1d9
MH
250 if (fd < 0) {
251 if (session.need_dwarf)
f984f03d 252 die("Could not open debuginfo file.");
a225a1d9 253
d3a2dbf8
MH
254 pr_debug("Could not open vmlinux/module file."
255 " Try to use symbols.\n");
a225a1d9
MH
256 goto end_dwarf;
257 }
4ea42b18
MH
258
259 /* Searching probe points */
d1bde3f7
MH
260 for (i = 0; i < session.nr_probe; i++) {
261 pp = &session.probes[i];
4ea42b18
MH
262 if (pp->found)
263 continue;
264
265 lseek(fd, SEEK_SET, 0);
266 ret = find_probepoint(fd, pp);
411edfe5
MH
267 if (ret > 0)
268 continue;
a225a1d9
MH
269 if (ret == 0) /* No error but failed to find probe point. */
270 die("No probe point found.");
411edfe5
MH
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;
4ea42b18
MH
280 }
281 close(fd);
282
a225a1d9 283end_dwarf:
23e8ec0d
MH
284#endif /* !NO_LIBDWARF */
285
a225a1d9 286 /* Synthesize probes without dwarf */
d1bde3f7
MH
287 for (i = 0; i < session.nr_probe; i++) {
288 pp = &session.probes[i];
a225a1d9
MH
289 if (pp->found) /* This probe is already found. */
290 continue;
291
50656eec 292 ret = synthesize_trace_kprobe_event(pp);
a225a1d9 293 if (ret == -E2BIG)
50656eec 294 die("probe point definition becomes too long.");
a225a1d9
MH
295 else if (ret < 0)
296 die("Failed to synthesize a probe point.");
297 }
298
4ea42b18 299 /* Settng up probe points */
d761b08b
MH
300 add_trace_kprobe_events(session.probes, session.nr_probe,
301 session.force_add);
4ea42b18
MH
302 return 0;
303}
304
This page took 0.051027 seconds and 5 git commands to generate.