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/symbol.h"
40 #include "util/debug.h"
41 #include "util/debugfs.h"
42 #include "util/parse-options.h"
43 #include "util/probe-finder.h"
44 #include "util/probe-event.h"
46 #define MAX_PATH_LEN 256
48 /* Session management structure */
57 struct perf_probe_event events
[MAX_PROBES
];
58 struct strlist
*dellist
;
59 struct line_range line_range
;
60 const char *target_module
;
64 /* Parse an event definition. Note that any error must die. */
65 static int parse_probe_event(const char *str
)
67 struct perf_probe_event
*pev
= ¶ms
.events
[params
.nevents
];
70 pr_debug("probe-definition(%d): %s\n", params
.nevents
, str
);
71 if (++params
.nevents
== MAX_PROBES
) {
72 pr_err("Too many probes (> %d) were specified.", MAX_PROBES
);
76 /* Parse a perf-probe command into event */
77 ret
= parse_perf_probe_command(str
, pev
);
78 pr_debug("%d arguments\n", pev
->nargs
);
83 static int parse_probe_event_argv(int argc
, const char **argv
)
88 /* Bind up rest arguments */
90 for (i
= 0; i
< argc
; i
++)
91 len
+= strlen(argv
[i
]) + 1;
92 buf
= zalloc(len
+ 1);
96 for (i
= 0; i
< argc
; i
++)
97 len
+= sprintf(&buf
[len
], "%s ", argv
[i
]);
98 params
.mod_events
= true;
99 ret
= parse_probe_event(buf
);
104 static int opt_add_probe_event(const struct option
*opt __used
,
105 const char *str
, int unset __used
)
108 params
.mod_events
= true;
109 return parse_probe_event(str
);
114 static int opt_del_probe_event(const struct option
*opt __used
,
115 const char *str
, int unset __used
)
118 params
.mod_events
= true;
120 params
.dellist
= strlist__new(true, NULL
);
121 strlist__add(params
.dellist
, str
);
127 static int opt_show_lines(const struct option
*opt __used
,
128 const char *str
, int unset __used
)
133 ret
= parse_line_range_desc(str
, ¶ms
.line_range
);
134 INIT_LIST_HEAD(¶ms
.line_range
.line_list
);
135 params
.show_lines
= true;
140 static int opt_show_vars(const struct option
*opt __used
,
141 const char *str
, int unset __used
)
143 struct perf_probe_event
*pev
= ¶ms
.events
[params
.nevents
];
149 ret
= parse_probe_event(str
);
150 if (!ret
&& pev
->nargs
!= 0) {
151 pr_err(" Error: '--vars' doesn't accept arguments.\n");
154 params
.show_vars
= true;
160 static const char * const probe_usage
[] = {
161 "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
162 "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
163 "perf probe [<options>] --del '[GROUP:]EVENT' ...",
166 "perf probe [<options>] --line 'LINEDESC'",
167 "perf probe [<options>] --vars 'PROBEPOINT'",
172 static const struct option options
[] = {
173 OPT_INCR('v', "verbose", &verbose
,
174 "be more verbose (show parsed arguments, etc)"),
175 OPT_BOOLEAN('l', "list", ¶ms
.list_events
,
176 "list up current probe events"),
177 OPT_CALLBACK('d', "del", NULL
, "[GROUP:]EVENT", "delete a probe event.",
178 opt_del_probe_event
),
179 OPT_CALLBACK('a', "add", NULL
,
181 "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
184 "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
186 "probe point definition, where\n"
187 "\t\tGROUP:\tGroup name (optional)\n"
188 "\t\tEVENT:\tEvent name\n"
189 "\t\tFUNC:\tFunction name\n"
190 "\t\tOFF:\tOffset from function entry (in byte)\n"
191 "\t\t%return:\tPut the probe at function return\n"
193 "\t\tSRC:\tSource code path\n"
194 "\t\tRL:\tRelative line number from function entry.\n"
195 "\t\tAL:\tAbsolute line number in file.\n"
196 "\t\tPT:\tLazy expression of line code.\n"
197 "\t\tARG:\tProbe argument (local variable name or\n"
198 "\t\t\tkprobe-tracer argument format.)\n",
200 "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
202 opt_add_probe_event
),
203 OPT_BOOLEAN('f', "force", ¶ms
.force_add
, "forcibly add events"
204 " with existing name"),
206 OPT_CALLBACK('L', "line", NULL
,
207 "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
208 "Show source code lines.", opt_show_lines
),
209 OPT_CALLBACK('V', "vars", NULL
,
210 "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
211 "Show accessible variables on PROBEDEF", opt_show_vars
),
212 OPT_BOOLEAN('\0', "externs", ¶ms
.show_ext_vars
,
213 "Show external variables too (with --vars only)"),
214 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
215 "file", "vmlinux pathname"),
216 OPT_STRING('s', "source", &symbol_conf
.source_prefix
,
217 "directory", "path to kernel source"),
218 OPT_STRING('m', "module", ¶ms
.target_module
,
219 "modname", "target module name"),
221 OPT__DRY_RUN(&probe_event_dry_run
),
222 OPT_INTEGER('\0', "max-probes", ¶ms
.max_probe_points
,
223 "Set how many probe points can be found for a probe."),
227 int cmd_probe(int argc
, const char **argv
, const char *prefix __used
)
231 argc
= parse_options(argc
, argv
, options
, probe_usage
,
232 PARSE_OPT_STOP_AT_NON_OPTION
);
234 if (strcmp(argv
[0], "-") == 0) {
235 pr_warning(" Error: '-' is not supported.\n");
236 usage_with_options(probe_usage
, options
);
238 ret
= parse_probe_event_argv(argc
, argv
);
240 pr_err(" Error: Parse Error. (%d)\n", ret
);
245 if (params
.max_probe_points
== 0)
246 params
.max_probe_points
= MAX_PROBES
;
248 if ((!params
.nevents
&& !params
.dellist
&& !params
.list_events
&&
250 usage_with_options(probe_usage
, options
);
252 if (params
.list_events
) {
253 if (params
.mod_events
) {
254 pr_err(" Error: Don't use --list with --add/--del.\n");
255 usage_with_options(probe_usage
, options
);
257 if (params
.show_lines
) {
258 pr_err(" Error: Don't use --list with --line.\n");
259 usage_with_options(probe_usage
, options
);
261 if (params
.show_vars
) {
262 pr_err(" Error: Don't use --list with --vars.\n");
263 usage_with_options(probe_usage
, options
);
265 ret
= show_perf_probe_events();
267 pr_err(" Error: Failed to show event list. (%d)\n",
273 if (params
.show_lines
) {
274 if (params
.mod_events
) {
275 pr_err(" Error: Don't use --line with"
277 usage_with_options(probe_usage
, options
);
279 if (params
.show_vars
) {
280 pr_err(" Error: Don't use --line with --vars.\n");
281 usage_with_options(probe_usage
, options
);
284 ret
= show_line_range(¶ms
.line_range
, params
.target_module
);
286 pr_err(" Error: Failed to show lines. (%d)\n", ret
);
289 if (params
.show_vars
) {
290 if (params
.mod_events
) {
291 pr_err(" Error: Don't use --vars with"
293 usage_with_options(probe_usage
, options
);
295 ret
= show_available_vars(params
.events
, params
.nevents
,
296 params
.max_probe_points
,
297 params
.target_module
,
298 params
.show_ext_vars
);
300 pr_err(" Error: Failed to show vars. (%d)\n", ret
);
305 if (params
.dellist
) {
306 ret
= del_perf_probe_events(params
.dellist
);
307 strlist__delete(params
.dellist
);
309 pr_err(" Error: Failed to delete events. (%d)\n", ret
);
314 if (params
.nevents
) {
315 ret
= add_perf_probe_events(params
.events
, params
.nevents
,
316 params
.max_probe_points
,
317 params
.target_module
,
320 pr_err(" Error: Failed to add events. (%d)\n", ret
);