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/strfilter.h"
40 #include "util/symbol.h"
41 #include "util/debug.h"
42 #include "util/debugfs.h"
43 #include "util/parse-options.h"
44 #include "util/probe-finder.h"
45 #include "util/probe-event.h"
47 #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*"
48 #define DEFAULT_FUNC_FILTER "!_*"
50 /* Session management structure */
60 struct perf_probe_event events
[MAX_PROBES
];
61 struct strlist
*dellist
;
62 struct line_range line_range
;
63 const char *target_module
;
65 struct strfilter
*filter
;
68 /* Parse an event definition. Note that any error must die. */
69 static int parse_probe_event(const char *str
)
71 struct perf_probe_event
*pev
= ¶ms
.events
[params
.nevents
];
74 pr_debug("probe-definition(%d): %s\n", params
.nevents
, str
);
75 if (++params
.nevents
== MAX_PROBES
) {
76 pr_err("Too many probes (> %d) were specified.", MAX_PROBES
);
80 /* Parse a perf-probe command into event */
81 ret
= parse_perf_probe_command(str
, pev
);
82 pr_debug("%d arguments\n", pev
->nargs
);
87 static int parse_probe_event_argv(int argc
, const char **argv
)
92 /* Bind up rest arguments */
94 for (i
= 0; i
< argc
; i
++)
95 len
+= strlen(argv
[i
]) + 1;
96 buf
= zalloc(len
+ 1);
100 for (i
= 0; i
< argc
; i
++)
101 len
+= sprintf(&buf
[len
], "%s ", argv
[i
]);
102 params
.mod_events
= true;
103 ret
= parse_probe_event(buf
);
108 static int opt_add_probe_event(const struct option
*opt __used
,
109 const char *str
, int unset __used
)
112 params
.mod_events
= true;
113 return parse_probe_event(str
);
118 static int opt_del_probe_event(const struct option
*opt __used
,
119 const char *str
, int unset __used
)
122 params
.mod_events
= true;
124 params
.dellist
= strlist__new(true, NULL
);
125 strlist__add(params
.dellist
, str
);
131 static int opt_show_lines(const struct option
*opt __used
,
132 const char *str
, int unset __used
)
139 if (params
.show_lines
) {
140 pr_warning("Warning: more than one --line options are"
141 " detected. Only the first one is valid.\n");
145 params
.show_lines
= true;
146 ret
= parse_line_range_desc(str
, ¶ms
.line_range
);
147 INIT_LIST_HEAD(¶ms
.line_range
.line_list
);
152 static int opt_show_vars(const struct option
*opt __used
,
153 const char *str
, int unset __used
)
155 struct perf_probe_event
*pev
= ¶ms
.events
[params
.nevents
];
161 ret
= parse_probe_event(str
);
162 if (!ret
&& pev
->nargs
!= 0) {
163 pr_err(" Error: '--vars' doesn't accept arguments.\n");
166 params
.show_vars
= true;
172 static int opt_set_filter(const struct option
*opt __used
,
173 const char *str
, int unset __used
)
178 pr_debug2("Set filter: %s\n", str
);
180 strfilter__delete(params
.filter
);
181 params
.filter
= strfilter__new(str
, &err
);
182 if (!params
.filter
) {
183 pr_err("Filter parse error at %td.\n", err
- str
+ 1);
184 pr_err("Source: \"%s\"\n", str
);
185 pr_err(" %*c\n", (int)(err
- str
+ 1), '^');
193 static const char * const probe_usage
[] = {
194 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
195 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
196 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
199 "perf probe [<options>] --line 'LINEDESC'",
200 "perf probe [<options>] --vars 'PROBEPOINT'",
205 static const struct option options
[] = {
206 OPT_INCR('v', "verbose", &verbose
,
207 "be more verbose (show parsed arguments, etc)"),
208 OPT_BOOLEAN('l', "list", ¶ms
.list_events
,
209 "list up current probe events"),
210 OPT_CALLBACK('d', "del", NULL
, "[GROUP:]EVENT", "delete a probe event.",
211 opt_del_probe_event
),
212 OPT_CALLBACK('a', "add", NULL
,
214 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
217 "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
219 "probe point definition, where\n"
220 "\t\tGROUP:\tGroup name (optional)\n"
221 "\t\tEVENT:\tEvent name\n"
222 "\t\tFUNC:\tFunction name\n"
223 "\t\tOFF:\tOffset from function entry (in byte)\n"
224 "\t\t%return:\tPut the probe at function return\n"
226 "\t\tSRC:\tSource code path\n"
227 "\t\tRL:\tRelative line number from function entry.\n"
228 "\t\tAL:\tAbsolute line number in file.\n"
229 "\t\tPT:\tLazy expression of line code.\n"
230 "\t\tARG:\tProbe argument (local variable name or\n"
231 "\t\t\tkprobe-tracer argument format.)\n",
233 "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
235 opt_add_probe_event
),
236 OPT_BOOLEAN('f', "force", ¶ms
.force_add
, "forcibly add events"
237 " with existing name"),
239 OPT_CALLBACK('L', "line", NULL
,
240 "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
241 "Show source code lines.", opt_show_lines
),
242 OPT_CALLBACK('V', "vars", NULL
,
243 "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
244 "Show accessible variables on PROBEDEF", opt_show_vars
),
245 OPT_BOOLEAN('\0', "externs", ¶ms
.show_ext_vars
,
246 "Show external variables too (with --vars only)"),
247 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
248 "file", "vmlinux pathname"),
249 OPT_STRING('s', "source", &symbol_conf
.source_prefix
,
250 "directory", "path to kernel source"),
251 OPT_STRING('m', "module", ¶ms
.target_module
,
253 "target module name (for online) or path (for offline)"),
255 OPT__DRY_RUN(&probe_event_dry_run
),
256 OPT_INTEGER('\0', "max-probes", ¶ms
.max_probe_points
,
257 "Set how many probe points can be found for a probe."),
258 OPT_BOOLEAN('F', "funcs", ¶ms
.show_funcs
,
259 "Show potential probe-able functions."),
260 OPT_CALLBACK('\0', "filter", NULL
,
261 "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
262 "\t\t\t(default: \"" DEFAULT_VAR_FILTER
"\" for --vars,\n"
263 "\t\t\t \"" DEFAULT_FUNC_FILTER
"\" for --funcs)",
268 int cmd_probe(int argc
, const char **argv
, const char *prefix __used
)
272 argc
= parse_options(argc
, argv
, options
, probe_usage
,
273 PARSE_OPT_STOP_AT_NON_OPTION
);
275 if (strcmp(argv
[0], "-") == 0) {
276 pr_warning(" Error: '-' is not supported.\n");
277 usage_with_options(probe_usage
, options
);
279 ret
= parse_probe_event_argv(argc
, argv
);
281 pr_err(" Error: Parse Error. (%d)\n", ret
);
286 if (params
.max_probe_points
== 0)
287 params
.max_probe_points
= MAX_PROBES
;
289 if ((!params
.nevents
&& !params
.dellist
&& !params
.list_events
&&
290 !params
.show_lines
&& !params
.show_funcs
))
291 usage_with_options(probe_usage
, options
);
294 * Only consider the user's kernel image path if given.
296 symbol_conf
.try_vmlinux_path
= (symbol_conf
.vmlinux_name
== NULL
);
298 if (params
.list_events
) {
299 if (params
.mod_events
) {
300 pr_err(" Error: Don't use --list with --add/--del.\n");
301 usage_with_options(probe_usage
, options
);
303 if (params
.show_lines
) {
304 pr_err(" Error: Don't use --list with --line.\n");
305 usage_with_options(probe_usage
, options
);
307 if (params
.show_vars
) {
308 pr_err(" Error: Don't use --list with --vars.\n");
309 usage_with_options(probe_usage
, options
);
311 if (params
.show_funcs
) {
312 pr_err(" Error: Don't use --list with --funcs.\n");
313 usage_with_options(probe_usage
, options
);
315 ret
= show_perf_probe_events();
317 pr_err(" Error: Failed to show event list. (%d)\n",
321 if (params
.show_funcs
) {
322 if (params
.nevents
!= 0 || params
.dellist
) {
323 pr_err(" Error: Don't use --funcs with"
325 usage_with_options(probe_usage
, options
);
327 if (params
.show_lines
) {
328 pr_err(" Error: Don't use --funcs with --line.\n");
329 usage_with_options(probe_usage
, options
);
331 if (params
.show_vars
) {
332 pr_err(" Error: Don't use --funcs with --vars.\n");
333 usage_with_options(probe_usage
, options
);
336 params
.filter
= strfilter__new(DEFAULT_FUNC_FILTER
,
338 ret
= show_available_funcs(params
.target_module
,
340 strfilter__delete(params
.filter
);
342 pr_err(" Error: Failed to show functions."
348 if (params
.show_lines
) {
349 if (params
.mod_events
) {
350 pr_err(" Error: Don't use --line with"
352 usage_with_options(probe_usage
, options
);
354 if (params
.show_vars
) {
355 pr_err(" Error: Don't use --line with --vars.\n");
356 usage_with_options(probe_usage
, options
);
359 ret
= show_line_range(¶ms
.line_range
, params
.target_module
);
361 pr_err(" Error: Failed to show lines. (%d)\n", ret
);
364 if (params
.show_vars
) {
365 if (params
.mod_events
) {
366 pr_err(" Error: Don't use --vars with"
368 usage_with_options(probe_usage
, options
);
371 params
.filter
= strfilter__new(DEFAULT_VAR_FILTER
,
374 ret
= show_available_vars(params
.events
, params
.nevents
,
375 params
.max_probe_points
,
376 params
.target_module
,
378 params
.show_ext_vars
);
379 strfilter__delete(params
.filter
);
381 pr_err(" Error: Failed to show vars. (%d)\n", ret
);
386 if (params
.dellist
) {
387 ret
= del_perf_probe_events(params
.dellist
);
388 strlist__delete(params
.dellist
);
390 pr_err(" Error: Failed to delete events. (%d)\n", ret
);
395 if (params
.nevents
) {
396 ret
= add_perf_probe_events(params
.events
, params
.nevents
,
397 params
.max_probe_points
,
398 params
.target_module
,
401 pr_err(" Error: Failed to add events. (%d)\n", ret
);