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.
23 #include <sys/utsname.h>
24 #include <sys/types.h>
35 #include "util/util.h"
36 #include "util/strlist.h"
37 #include "util/strfilter.h"
38 #include "util/symbol.h"
39 #include "util/debug.h"
40 #include "util/debugfs.h"
41 #include "util/parse-options.h"
42 #include "util/probe-finder.h"
43 #include "util/probe-event.h"
45 #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*"
46 #define DEFAULT_FUNC_FILTER "!_*"
48 /* Session management structure */
58 struct perf_probe_event events
[MAX_PROBES
];
59 struct strlist
*dellist
;
60 struct line_range line_range
;
61 const char *target_module
;
63 struct strfilter
*filter
;
66 /* Parse an event definition. Note that any error must die. */
67 static int parse_probe_event(const char *str
)
69 struct perf_probe_event
*pev
= ¶ms
.events
[params
.nevents
];
72 pr_debug("probe-definition(%d): %s\n", params
.nevents
, str
);
73 if (++params
.nevents
== MAX_PROBES
) {
74 pr_err("Too many probes (> %d) were specified.", MAX_PROBES
);
78 /* Parse a perf-probe command into event */
79 ret
= parse_perf_probe_command(str
, pev
);
80 pr_debug("%d arguments\n", pev
->nargs
);
85 static int parse_probe_event_argv(int argc
, const char **argv
)
90 /* Bind up rest arguments */
92 for (i
= 0; i
< argc
; i
++)
93 len
+= strlen(argv
[i
]) + 1;
94 buf
= zalloc(len
+ 1);
98 for (i
= 0; i
< argc
; i
++)
99 len
+= sprintf(&buf
[len
], "%s ", argv
[i
]);
100 params
.mod_events
= true;
101 ret
= parse_probe_event(buf
);
106 static int opt_add_probe_event(const struct option
*opt __used
,
107 const char *str
, int unset __used
)
110 params
.mod_events
= true;
111 return parse_probe_event(str
);
116 static int opt_del_probe_event(const struct option
*opt __used
,
117 const char *str
, int unset __used
)
120 params
.mod_events
= true;
122 params
.dellist
= strlist__new(true, NULL
);
123 strlist__add(params
.dellist
, str
);
129 static int opt_show_lines(const struct option
*opt __used
,
130 const char *str
, int unset __used
)
137 if (params
.show_lines
) {
138 pr_warning("Warning: more than one --line options are"
139 " detected. Only the first one is valid.\n");
143 params
.show_lines
= true;
144 ret
= parse_line_range_desc(str
, ¶ms
.line_range
);
145 INIT_LIST_HEAD(¶ms
.line_range
.line_list
);
150 static int opt_show_vars(const struct option
*opt __used
,
151 const char *str
, int unset __used
)
153 struct perf_probe_event
*pev
= ¶ms
.events
[params
.nevents
];
159 ret
= parse_probe_event(str
);
160 if (!ret
&& pev
->nargs
!= 0) {
161 pr_err(" Error: '--vars' doesn't accept arguments.\n");
164 params
.show_vars
= true;
170 static int opt_set_filter(const struct option
*opt __used
,
171 const char *str
, int unset __used
)
176 pr_debug2("Set filter: %s\n", str
);
178 strfilter__delete(params
.filter
);
179 params
.filter
= strfilter__new(str
, &err
);
180 if (!params
.filter
) {
181 pr_err("Filter parse error at %td.\n", err
- str
+ 1);
182 pr_err("Source: \"%s\"\n", str
);
183 pr_err(" %*c\n", (int)(err
- str
+ 1), '^');
191 static const char * const probe_usage
[] = {
192 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
193 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
194 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
197 "perf probe [<options>] --line 'LINEDESC'",
198 "perf probe [<options>] --vars 'PROBEPOINT'",
203 static const struct option options
[] = {
204 OPT_INCR('v', "verbose", &verbose
,
205 "be more verbose (show parsed arguments, etc)"),
206 OPT_BOOLEAN('l', "list", ¶ms
.list_events
,
207 "list up current probe events"),
208 OPT_CALLBACK('d', "del", NULL
, "[GROUP:]EVENT", "delete a probe event.",
209 opt_del_probe_event
),
210 OPT_CALLBACK('a', "add", NULL
,
212 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
215 "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
217 "probe point definition, where\n"
218 "\t\tGROUP:\tGroup name (optional)\n"
219 "\t\tEVENT:\tEvent name\n"
220 "\t\tFUNC:\tFunction name\n"
221 "\t\tOFF:\tOffset from function entry (in byte)\n"
222 "\t\t%return:\tPut the probe at function return\n"
224 "\t\tSRC:\tSource code path\n"
225 "\t\tRL:\tRelative line number from function entry.\n"
226 "\t\tAL:\tAbsolute line number in file.\n"
227 "\t\tPT:\tLazy expression of line code.\n"
228 "\t\tARG:\tProbe argument (local variable name or\n"
229 "\t\t\tkprobe-tracer argument format.)\n",
231 "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
233 opt_add_probe_event
),
234 OPT_BOOLEAN('f', "force", ¶ms
.force_add
, "forcibly add events"
235 " with existing name"),
237 OPT_CALLBACK('L', "line", NULL
,
238 "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
239 "Show source code lines.", opt_show_lines
),
240 OPT_CALLBACK('V', "vars", NULL
,
241 "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
242 "Show accessible variables on PROBEDEF", opt_show_vars
),
243 OPT_BOOLEAN('\0', "externs", ¶ms
.show_ext_vars
,
244 "Show external variables too (with --vars only)"),
245 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
246 "file", "vmlinux pathname"),
247 OPT_STRING('s', "source", &symbol_conf
.source_prefix
,
248 "directory", "path to kernel source"),
249 OPT_STRING('m', "module", ¶ms
.target_module
,
251 "target module name (for online) or path (for offline)"),
253 OPT__DRY_RUN(&probe_event_dry_run
),
254 OPT_INTEGER('\0', "max-probes", ¶ms
.max_probe_points
,
255 "Set how many probe points can be found for a probe."),
256 OPT_BOOLEAN('F', "funcs", ¶ms
.show_funcs
,
257 "Show potential probe-able functions."),
258 OPT_CALLBACK('\0', "filter", NULL
,
259 "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
260 "\t\t\t(default: \"" DEFAULT_VAR_FILTER
"\" for --vars,\n"
261 "\t\t\t \"" DEFAULT_FUNC_FILTER
"\" for --funcs)",
266 int cmd_probe(int argc
, const char **argv
, const char *prefix __used
)
270 argc
= parse_options(argc
, argv
, options
, probe_usage
,
271 PARSE_OPT_STOP_AT_NON_OPTION
);
273 if (strcmp(argv
[0], "-") == 0) {
274 pr_warning(" Error: '-' is not supported.\n");
275 usage_with_options(probe_usage
, options
);
277 ret
= parse_probe_event_argv(argc
, argv
);
279 pr_err(" Error: Parse Error. (%d)\n", ret
);
284 if (params
.max_probe_points
== 0)
285 params
.max_probe_points
= MAX_PROBES
;
287 if ((!params
.nevents
&& !params
.dellist
&& !params
.list_events
&&
288 !params
.show_lines
&& !params
.show_funcs
))
289 usage_with_options(probe_usage
, options
);
292 * Only consider the user's kernel image path if given.
294 symbol_conf
.try_vmlinux_path
= (symbol_conf
.vmlinux_name
== NULL
);
296 if (params
.list_events
) {
297 if (params
.mod_events
) {
298 pr_err(" Error: Don't use --list with --add/--del.\n");
299 usage_with_options(probe_usage
, options
);
301 if (params
.show_lines
) {
302 pr_err(" Error: Don't use --list with --line.\n");
303 usage_with_options(probe_usage
, options
);
305 if (params
.show_vars
) {
306 pr_err(" Error: Don't use --list with --vars.\n");
307 usage_with_options(probe_usage
, options
);
309 if (params
.show_funcs
) {
310 pr_err(" Error: Don't use --list with --funcs.\n");
311 usage_with_options(probe_usage
, options
);
313 ret
= show_perf_probe_events();
315 pr_err(" Error: Failed to show event list. (%d)\n",
319 if (params
.show_funcs
) {
320 if (params
.nevents
!= 0 || params
.dellist
) {
321 pr_err(" Error: Don't use --funcs with"
323 usage_with_options(probe_usage
, options
);
325 if (params
.show_lines
) {
326 pr_err(" Error: Don't use --funcs with --line.\n");
327 usage_with_options(probe_usage
, options
);
329 if (params
.show_vars
) {
330 pr_err(" Error: Don't use --funcs with --vars.\n");
331 usage_with_options(probe_usage
, options
);
334 params
.filter
= strfilter__new(DEFAULT_FUNC_FILTER
,
336 ret
= show_available_funcs(params
.target_module
,
338 strfilter__delete(params
.filter
);
340 pr_err(" Error: Failed to show functions."
346 if (params
.show_lines
) {
347 if (params
.mod_events
) {
348 pr_err(" Error: Don't use --line with"
350 usage_with_options(probe_usage
, options
);
352 if (params
.show_vars
) {
353 pr_err(" Error: Don't use --line with --vars.\n");
354 usage_with_options(probe_usage
, options
);
357 ret
= show_line_range(¶ms
.line_range
, params
.target_module
);
359 pr_err(" Error: Failed to show lines. (%d)\n", ret
);
362 if (params
.show_vars
) {
363 if (params
.mod_events
) {
364 pr_err(" Error: Don't use --vars with"
366 usage_with_options(probe_usage
, options
);
369 params
.filter
= strfilter__new(DEFAULT_VAR_FILTER
,
372 ret
= show_available_vars(params
.events
, params
.nevents
,
373 params
.max_probe_points
,
374 params
.target_module
,
376 params
.show_ext_vars
);
377 strfilter__delete(params
.filter
);
379 pr_err(" Error: Failed to show vars. (%d)\n", ret
);
384 if (params
.dellist
) {
385 ret
= del_perf_probe_events(params
.dellist
);
386 strlist__delete(params
.dellist
);
388 pr_err(" Error: Failed to delete events. (%d)\n", ret
);
393 if (params
.nevents
) {
394 ret
= add_perf_probe_events(params
.events
, params
.nevents
,
395 params
.max_probe_points
,
396 params
.target_module
,
399 pr_err(" Error: Failed to add events. (%d)\n", ret
);