4 * Builtin probe command: Set up probe events by C expression
6 * Written by Masami Hiramatsu <mhiramat@redhat.com>
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.
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.
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.
24 #include <sys/utsname.h>
25 #include <sys/types.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"
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 */
55 #define MAX_PATH_LEN 256
56 #define MAX_PROBES 128
58 /* Session management structure */
66 struct probe_point probes
[MAX_PROBES
];
67 struct strlist
*dellist
;
71 /* Parse an event definition. Note that any error must die. */
72 static void parse_probe_event(const char *str
)
74 struct probe_point
*pp
= &session
.probes
[session
.nr_probe
];
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
);
80 /* Parse perf-probe event into probe_point */
81 parse_perf_probe_event(str
, pp
, &session
.need_dwarf
);
83 pr_debug("%d arguments\n", pp
->nr_args
);
86 static void parse_probe_event_argv(int argc
, const char **argv
)
91 /* Bind up rest arguments */
93 for (i
= 0; i
< argc
; i
++)
94 len
+= strlen(argv
[i
]) + 1;
95 buf
= zalloc(len
+ 1);
97 die("Failed to allocate memory for binding arguments.");
99 for (i
= 0; i
< argc
; i
++)
100 len
+= sprintf(&buf
[len
], "%s ", argv
[i
]);
101 parse_probe_event(buf
);
105 static int opt_add_probe_event(const struct option
*opt __used
,
106 const char *str
, int unset __used
)
109 parse_probe_event(str
);
113 static int opt_del_probe_event(const struct option
*opt __used
,
114 const char *str
, int unset __used
)
117 if (!session
.dellist
)
118 session
.dellist
= strlist__new(true, NULL
);
119 strlist__add(session
.dellist
, str
);
125 static int open_default_vmlinux(void)
128 char fname
[MAX_PATH_LEN
];
133 pr_debug("uname() failed.\n");
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
,
146 pr_debug("try to open %s\n", fname
);
147 fd
= open(fname
, O_RDONLY
);
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' ...",
163 static const struct option options
[] = {
164 OPT_BOOLEAN('v', "verbose", &verbose
,
165 "be more verbose (show parsed arguments, etc)"),
167 OPT_STRING('k', "vmlinux", &session
.vmlinux
, "file",
168 "vmlinux/module pathname"),
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
,
176 "[EVENT=]FUNC[+OFFS|%return] [ARG ...]",
178 "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
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"
187 "\t\tARG:\tProbe argument (only \n"
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"
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"),
201 int cmd_probe(int argc
, const char **argv
, const char *prefix __used
)
207 struct probe_point
*pp
;
209 argc
= parse_options(argc
, argv
, options
, probe_usage
,
210 PARSE_OPT_STOP_AT_NON_OPTION
);
212 if (strcmp(argv
[0], "-") == 0) {
213 pr_warning(" Error: '-' is not supported.\n");
214 usage_with_options(probe_usage
, options
);
216 parse_probe_event_argv(argc
, argv
);
219 if ((!session
.nr_probe
&& !session
.dellist
&& !session
.list_events
))
220 usage_with_options(probe_usage
, options
);
222 if (session
.list_events
) {
223 if (session
.nr_probe
!= 0 || session
.dellist
) {
224 pr_warning(" Error: Don't use --list with"
226 usage_with_options(probe_usage
, options
);
228 show_perf_probe_events();
232 if (session
.dellist
) {
233 del_trace_kprobe_events(session
.dellist
);
234 strlist__delete(session
.dellist
);
235 if (session
.nr_probe
== 0)
239 if (session
.need_dwarf
)
241 die("Debuginfo-analysis is not supported");
242 #else /* !NO_LIBDWARF */
243 pr_debug("Some probes require debuginfo.\n");
245 if (session
.vmlinux
) {
246 pr_debug("Try to open %s.", session
.vmlinux
);
247 fd
= open(session
.vmlinux
, O_RDONLY
);
249 fd
= open_default_vmlinux();
251 if (session
.need_dwarf
)
252 die("Could not open debuginfo file.");
254 pr_debug("Could not open vmlinux/module file."
255 " Try to use symbols.\n");
259 /* Searching probe points */
260 for (i
= 0; i
< session
.nr_probe
; i
++) {
261 pp
= &session
.probes
[i
];
265 lseek(fd
, SEEK_SET
, 0);
266 ret
= find_probepoint(fd
, pp
);
269 if (ret
== 0) /* No error but failed to find probe point. */
270 die("No probe point found.");
272 if (session
.need_dwarf
) {
274 pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
275 die("Could not analyze debuginfo.");
277 pr_debug("An error occurred in debuginfo analysis."
278 " Try to use symbols.\n");
284 #endif /* !NO_LIBDWARF */
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. */
292 ret
= synthesize_trace_kprobe_event(pp
);
294 die("probe point definition becomes too long.");
296 die("Failed to synthesize a probe point.");
299 /* Settng up probe points */
300 add_trace_kprobe_events(session
.probes
, session
.nr_probe
,