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