Commit | Line | Data |
---|---|---|
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" |
bd09d7b5 | 39 | #include "util/strfilter.h" |
e0faa8d3 | 40 | #include "util/symbol.h" |
89c69c0e | 41 | #include "util/debug.h" |
96c96612 | 42 | #include "util/debugfs.h" |
4ea42b18 | 43 | #include "util/parse-options.h" |
4ea42b18 | 44 | #include "util/probe-finder.h" |
50656eec | 45 | #include "util/probe-event.h" |
4ea42b18 | 46 | |
bd09d7b5 | 47 | #define DEFAULT_VAR_FILTER "!__k???tab_* & !__crc_*" |
3c42258c | 48 | #define DEFAULT_FUNC_FILTER "!_*" |
4ea42b18 MH |
49 | |
50 | /* Session management structure */ | |
51 | static struct { | |
fac13fd5 | 52 | bool list_events; |
d761b08b | 53 | bool force_add; |
631c9def | 54 | bool show_lines; |
cf6eb489 | 55 | bool show_vars; |
fb8c5a56 | 56 | bool show_ext_vars; |
e80711ca | 57 | bool show_funcs; |
cf6eb489 | 58 | bool mod_events; |
4235b045 MH |
59 | int nevents; |
60 | struct perf_probe_event events[MAX_PROBES]; | |
fa28244d | 61 | struct strlist *dellist; |
631c9def | 62 | struct line_range line_range; |
469b9b88 | 63 | const char *target_module; |
ef4a3565 | 64 | int max_probe_points; |
bd09d7b5 | 65 | struct strfilter *filter; |
12a1fadb | 66 | } params; |
4ea42b18 | 67 | |
253977b0 | 68 | /* Parse an event definition. Note that any error must die. */ |
146a1439 | 69 | static int parse_probe_event(const char *str) |
4ea42b18 | 70 | { |
4235b045 | 71 | struct perf_probe_event *pev = ¶ms.events[params.nevents]; |
146a1439 | 72 | int ret; |
4ea42b18 | 73 | |
4235b045 | 74 | pr_debug("probe-definition(%d): %s\n", params.nevents, str); |
8a7ddad8 ACM |
75 | if (++params.nevents == MAX_PROBES) { |
76 | pr_err("Too many probes (> %d) were specified.", MAX_PROBES); | |
77 | return -1; | |
78 | } | |
4ea42b18 | 79 | |
4235b045 | 80 | /* Parse a perf-probe command into event */ |
146a1439 | 81 | ret = parse_perf_probe_command(str, pev); |
4235b045 | 82 | pr_debug("%d arguments\n", pev->nargs); |
146a1439 MH |
83 | |
84 | return ret; | |
46ab4926 MH |
85 | } |
86 | ||
146a1439 | 87 | static int parse_probe_event_argv(int argc, const char **argv) |
d1bde3f7 | 88 | { |
146a1439 | 89 | int i, len, ret; |
d1bde3f7 MH |
90 | char *buf; |
91 | ||
92 | /* Bind up rest arguments */ | |
93 | len = 0; | |
94 | for (i = 0; i < argc; i++) | |
95 | len += strlen(argv[i]) + 1; | |
8a7ddad8 ACM |
96 | buf = zalloc(len + 1); |
97 | if (buf == NULL) | |
98 | return -ENOMEM; | |
d1bde3f7 MH |
99 | len = 0; |
100 | for (i = 0; i < argc; i++) | |
101 | len += sprintf(&buf[len], "%s ", argv[i]); | |
cf6eb489 | 102 | params.mod_events = true; |
146a1439 | 103 | ret = parse_probe_event(buf); |
d1bde3f7 | 104 | free(buf); |
146a1439 | 105 | return ret; |
d1bde3f7 MH |
106 | } |
107 | ||
253977b0 | 108 | static int opt_add_probe_event(const struct option *opt __used, |
46ab4926 MH |
109 | const char *str, int unset __used) |
110 | { | |
cf6eb489 MH |
111 | if (str) { |
112 | params.mod_events = true; | |
146a1439 | 113 | return parse_probe_event(str); |
cf6eb489 | 114 | } else |
146a1439 | 115 | return 0; |
4ea42b18 MH |
116 | } |
117 | ||
fa28244d MH |
118 | static int opt_del_probe_event(const struct option *opt __used, |
119 | const char *str, int unset __used) | |
120 | { | |
121 | if (str) { | |
cf6eb489 | 122 | params.mod_events = true; |
12a1fadb MH |
123 | if (!params.dellist) |
124 | params.dellist = strlist__new(true, NULL); | |
125 | strlist__add(params.dellist, str); | |
fa28244d MH |
126 | } |
127 | return 0; | |
128 | } | |
129 | ||
4b4da7f7 | 130 | #ifdef DWARF_SUPPORT |
0eda7385 HM |
131 | static int opt_show_lines(const struct option *opt __used, |
132 | const char *str, int unset __used) | |
133 | { | |
146a1439 MH |
134 | int ret = 0; |
135 | ||
13e27d76 MH |
136 | if (!str) |
137 | return 0; | |
138 | ||
139 | if (params.show_lines) { | |
140 | pr_warning("Warning: more than one --line options are" | |
141 | " detected. Only the first one is valid.\n"); | |
142 | return 0; | |
143 | } | |
144 | ||
12a1fadb | 145 | params.show_lines = true; |
13e27d76 MH |
146 | ret = parse_line_range_desc(str, ¶ms.line_range); |
147 | INIT_LIST_HEAD(¶ms.line_range.line_list); | |
146a1439 MH |
148 | |
149 | return ret; | |
0eda7385 | 150 | } |
cf6eb489 MH |
151 | |
152 | static int opt_show_vars(const struct option *opt __used, | |
153 | const char *str, int unset __used) | |
154 | { | |
155 | struct perf_probe_event *pev = ¶ms.events[params.nevents]; | |
156 | int ret; | |
157 | ||
158 | if (!str) | |
159 | return 0; | |
160 | ||
161 | ret = parse_probe_event(str); | |
162 | if (!ret && pev->nargs != 0) { | |
163 | pr_err(" Error: '--vars' doesn't accept arguments.\n"); | |
164 | return -EINVAL; | |
165 | } | |
166 | params.show_vars = true; | |
167 | ||
168 | return ret; | |
169 | } | |
3c42258c | 170 | #endif |
bd09d7b5 MH |
171 | |
172 | static int opt_set_filter(const struct option *opt __used, | |
173 | const char *str, int unset __used) | |
174 | { | |
175 | const char *err; | |
176 | ||
177 | if (str) { | |
178 | pr_debug2("Set filter: %s\n", str); | |
179 | if (params.filter) | |
180 | strfilter__delete(params.filter); | |
181 | params.filter = strfilter__new(str, &err); | |
182 | if (!params.filter) { | |
823c7164 | 183 | pr_err("Filter parse error at %td.\n", err - str + 1); |
bd09d7b5 MH |
184 | pr_err("Source: \"%s\"\n", str); |
185 | pr_err(" %*c\n", (int)(err - str + 1), '^'); | |
186 | return -EINVAL; | |
187 | } | |
188 | } | |
189 | ||
190 | return 0; | |
191 | } | |
4ea42b18 MH |
192 | |
193 | static const char * const probe_usage[] = { | |
46ab4926 MH |
194 | "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]", |
195 | "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]", | |
fa28244d | 196 | "perf probe [<options>] --del '[GROUP:]EVENT' ...", |
4de189fe | 197 | "perf probe --list", |
4b4da7f7 | 198 | #ifdef DWARF_SUPPORT |
469b9b88 MH |
199 | "perf probe [<options>] --line 'LINEDESC'", |
200 | "perf probe [<options>] --vars 'PROBEPOINT'", | |
f3ab481c | 201 | #endif |
4ea42b18 MH |
202 | NULL |
203 | }; | |
204 | ||
205 | static const struct option options[] = { | |
c0555642 | 206 | OPT_INCR('v', "verbose", &verbose, |
89c69c0e | 207 | "be more verbose (show parsed arguments, etc)"), |
12a1fadb | 208 | OPT_BOOLEAN('l', "list", ¶ms.list_events, |
fac13fd5 | 209 | "list up current probe events"), |
fa28244d MH |
210 | OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.", |
211 | opt_del_probe_event), | |
46ab4926 | 212 | OPT_CALLBACK('a', "add", NULL, |
4b4da7f7 | 213 | #ifdef DWARF_SUPPORT |
32cb0dd5 | 214 | "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT" |
48481938 | 215 | " [[NAME=]ARG ...]", |
4b4da7f7 | 216 | #else |
48481938 | 217 | "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]", |
23e8ec0d | 218 | #endif |
4ea42b18 | 219 | "probe point definition, where\n" |
af663d75 MH |
220 | "\t\tGROUP:\tGroup name (optional)\n" |
221 | "\t\tEVENT:\tEvent name\n" | |
4ea42b18 | 222 | "\t\tFUNC:\tFunction name\n" |
2a9c8c36 | 223 | "\t\tOFF:\tOffset from function entry (in byte)\n" |
253977b0 | 224 | "\t\t%return:\tPut the probe at function return\n" |
4b4da7f7 | 225 | #ifdef DWARF_SUPPORT |
4ea42b18 | 226 | "\t\tSRC:\tSource code path\n" |
2a9c8c36 MH |
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" | |
4ea42b18 | 230 | "\t\tARG:\tProbe argument (local variable name or\n" |
50656eec | 231 | "\t\t\tkprobe-tracer argument format.)\n", |
4b4da7f7 MH |
232 | #else |
233 | "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n", | |
234 | #endif | |
253977b0 | 235 | opt_add_probe_event), |
12a1fadb | 236 | OPT_BOOLEAN('f', "force", ¶ms.force_add, "forcibly add events" |
d761b08b | 237 | " with existing name"), |
4b4da7f7 | 238 | #ifdef DWARF_SUPPORT |
631c9def | 239 | OPT_CALLBACK('L', "line", NULL, |
085ea739 | 240 | "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]", |
631c9def | 241 | "Show source code lines.", opt_show_lines), |
cf6eb489 MH |
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), | |
fb8c5a56 MH |
245 | OPT_BOOLEAN('\0', "externs", ¶ms.show_ext_vars, |
246 | "Show external variables too (with --vars only)"), | |
4b4da7f7 MH |
247 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
248 | "file", "vmlinux pathname"), | |
9ed7e1b8 CD |
249 | OPT_STRING('s', "source", &symbol_conf.source_prefix, |
250 | "directory", "path to kernel source"), | |
469b9b88 | 251 | OPT_STRING('m', "module", ¶ms.target_module, |
14a8fd7c MH |
252 | "modname|path", |
253 | "target module name (for online) or path (for offline)"), | |
631c9def | 254 | #endif |
f4d7da49 | 255 | OPT__DRY_RUN(&probe_event_dry_run), |
ef4a3565 MH |
256 | OPT_INTEGER('\0', "max-probes", ¶ms.max_probe_points, |
257 | "Set how many probe points can be found for a probe."), | |
e80711ca MH |
258 | OPT_BOOLEAN('F', "funcs", ¶ms.show_funcs, |
259 | "Show potential probe-able functions."), | |
3c42258c MH |
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)", | |
264 | opt_set_filter), | |
4ea42b18 MH |
265 | OPT_END() |
266 | }; | |
267 | ||
4ea42b18 MH |
268 | int cmd_probe(int argc, const char **argv, const char *prefix __used) |
269 | { | |
146a1439 MH |
270 | int ret; |
271 | ||
4ea42b18 | 272 | argc = parse_options(argc, argv, options, probe_usage, |
46ab4926 | 273 | PARSE_OPT_STOP_AT_NON_OPTION); |
ce11a603 MH |
274 | if (argc > 0) { |
275 | if (strcmp(argv[0], "-") == 0) { | |
276 | pr_warning(" Error: '-' is not supported.\n"); | |
277 | usage_with_options(probe_usage, options); | |
278 | } | |
146a1439 MH |
279 | ret = parse_probe_event_argv(argc, argv); |
280 | if (ret < 0) { | |
281 | pr_err(" Error: Parse Error. (%d)\n", ret); | |
282 | return ret; | |
283 | } | |
ce11a603 | 284 | } |
46ab4926 | 285 | |
ef4a3565 MH |
286 | if (params.max_probe_points == 0) |
287 | params.max_probe_points = MAX_PROBES; | |
288 | ||
4235b045 | 289 | if ((!params.nevents && !params.dellist && !params.list_events && |
e80711ca | 290 | !params.show_lines && !params.show_funcs)) |
4ea42b18 MH |
291 | usage_with_options(probe_usage, options); |
292 | ||
fd930ff9 FBH |
293 | /* |
294 | * Only consider the user's kernel image path if given. | |
295 | */ | |
296 | symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL); | |
297 | ||
12a1fadb | 298 | if (params.list_events) { |
cf6eb489 | 299 | if (params.mod_events) { |
146a1439 | 300 | pr_err(" Error: Don't use --list with --add/--del.\n"); |
fa28244d MH |
301 | usage_with_options(probe_usage, options); |
302 | } | |
12a1fadb | 303 | if (params.show_lines) { |
146a1439 | 304 | pr_err(" Error: Don't use --list with --line.\n"); |
631c9def MH |
305 | usage_with_options(probe_usage, options); |
306 | } | |
cf6eb489 MH |
307 | if (params.show_vars) { |
308 | pr_err(" Error: Don't use --list with --vars.\n"); | |
309 | usage_with_options(probe_usage, options); | |
310 | } | |
e80711ca MH |
311 | if (params.show_funcs) { |
312 | pr_err(" Error: Don't use --list with --funcs.\n"); | |
313 | usage_with_options(probe_usage, options); | |
314 | } | |
146a1439 MH |
315 | ret = show_perf_probe_events(); |
316 | if (ret < 0) | |
317 | pr_err(" Error: Failed to show event list. (%d)\n", | |
318 | ret); | |
319 | return ret; | |
4de189fe | 320 | } |
e80711ca MH |
321 | if (params.show_funcs) { |
322 | if (params.nevents != 0 || params.dellist) { | |
323 | pr_err(" Error: Don't use --funcs with" | |
324 | " --add/--del.\n"); | |
325 | usage_with_options(probe_usage, options); | |
326 | } | |
327 | if (params.show_lines) { | |
328 | pr_err(" Error: Don't use --funcs with --line.\n"); | |
329 | usage_with_options(probe_usage, options); | |
330 | } | |
331 | if (params.show_vars) { | |
332 | pr_err(" Error: Don't use --funcs with --vars.\n"); | |
333 | usage_with_options(probe_usage, options); | |
334 | } | |
3c42258c MH |
335 | if (!params.filter) |
336 | params.filter = strfilter__new(DEFAULT_FUNC_FILTER, | |
337 | NULL); | |
338 | ret = show_available_funcs(params.target_module, | |
339 | params.filter); | |
340 | strfilter__delete(params.filter); | |
e80711ca MH |
341 | if (ret < 0) |
342 | pr_err(" Error: Failed to show functions." | |
343 | " (%d)\n", ret); | |
344 | return ret; | |
345 | } | |
4de189fe | 346 | |
4b4da7f7 | 347 | #ifdef DWARF_SUPPORT |
12a1fadb | 348 | if (params.show_lines) { |
cf6eb489 MH |
349 | if (params.mod_events) { |
350 | pr_err(" Error: Don't use --line with" | |
351 | " --add/--del.\n"); | |
352 | usage_with_options(probe_usage, options); | |
353 | } | |
354 | if (params.show_vars) { | |
355 | pr_err(" Error: Don't use --line with --vars.\n"); | |
631c9def MH |
356 | usage_with_options(probe_usage, options); |
357 | } | |
e0faa8d3 | 358 | |
469b9b88 | 359 | ret = show_line_range(¶ms.line_range, params.target_module); |
146a1439 MH |
360 | if (ret < 0) |
361 | pr_err(" Error: Failed to show lines. (%d)\n", ret); | |
362 | return ret; | |
631c9def | 363 | } |
cf6eb489 MH |
364 | if (params.show_vars) { |
365 | if (params.mod_events) { | |
366 | pr_err(" Error: Don't use --vars with" | |
367 | " --add/--del.\n"); | |
368 | usage_with_options(probe_usage, options); | |
369 | } | |
bd09d7b5 MH |
370 | if (!params.filter) |
371 | params.filter = strfilter__new(DEFAULT_VAR_FILTER, | |
372 | NULL); | |
373 | ||
cf6eb489 | 374 | ret = show_available_vars(params.events, params.nevents, |
fb8c5a56 | 375 | params.max_probe_points, |
469b9b88 | 376 | params.target_module, |
bd09d7b5 | 377 | params.filter, |
fb8c5a56 | 378 | params.show_ext_vars); |
bd09d7b5 | 379 | strfilter__delete(params.filter); |
cf6eb489 MH |
380 | if (ret < 0) |
381 | pr_err(" Error: Failed to show vars. (%d)\n", ret); | |
382 | return ret; | |
383 | } | |
631c9def MH |
384 | #endif |
385 | ||
12a1fadb | 386 | if (params.dellist) { |
146a1439 | 387 | ret = del_perf_probe_events(params.dellist); |
12a1fadb | 388 | strlist__delete(params.dellist); |
146a1439 MH |
389 | if (ret < 0) { |
390 | pr_err(" Error: Failed to delete events. (%d)\n", ret); | |
391 | return ret; | |
392 | } | |
fa28244d MH |
393 | } |
394 | ||
146a1439 MH |
395 | if (params.nevents) { |
396 | ret = add_perf_probe_events(params.events, params.nevents, | |
c82ec0a2 | 397 | params.max_probe_points, |
469b9b88 | 398 | params.target_module, |
c82ec0a2 | 399 | params.force_add); |
146a1439 MH |
400 | if (ret < 0) { |
401 | pr_err(" Error: Failed to add events. (%d)\n", ret); | |
402 | return ret; | |
403 | } | |
404 | } | |
4ea42b18 MH |
405 | return 0; |
406 | } |