]>
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" |
e0faa8d3 | 39 | #include "util/symbol.h" |
89c69c0e | 40 | #include "util/debug.h" |
96c96612 | 41 | #include "util/debugfs.h" |
4ea42b18 | 42 | #include "util/parse-options.h" |
4ea42b18 | 43 | #include "util/probe-finder.h" |
50656eec | 44 | #include "util/probe-event.h" |
4ea42b18 | 45 | |
4ea42b18 | 46 | #define MAX_PATH_LEN 256 |
4ea42b18 MH |
47 | |
48 | /* Session management structure */ | |
49 | static struct { | |
fac13fd5 | 50 | bool list_events; |
d761b08b | 51 | bool force_add; |
631c9def | 52 | bool show_lines; |
cf6eb489 | 53 | bool show_vars; |
fb8c5a56 | 54 | bool show_ext_vars; |
cf6eb489 | 55 | bool mod_events; |
4235b045 MH |
56 | int nevents; |
57 | struct perf_probe_event events[MAX_PROBES]; | |
fa28244d | 58 | struct strlist *dellist; |
631c9def | 59 | struct line_range line_range; |
469b9b88 | 60 | const char *target_module; |
ef4a3565 | 61 | int max_probe_points; |
12a1fadb | 62 | } params; |
4ea42b18 | 63 | |
253977b0 | 64 | /* Parse an event definition. Note that any error must die. */ |
146a1439 | 65 | static int parse_probe_event(const char *str) |
4ea42b18 | 66 | { |
4235b045 | 67 | struct perf_probe_event *pev = ¶ms.events[params.nevents]; |
146a1439 | 68 | int ret; |
4ea42b18 | 69 | |
4235b045 | 70 | pr_debug("probe-definition(%d): %s\n", params.nevents, str); |
8a7ddad8 ACM |
71 | if (++params.nevents == MAX_PROBES) { |
72 | pr_err("Too many probes (> %d) were specified.", MAX_PROBES); | |
73 | return -1; | |
74 | } | |
4ea42b18 | 75 | |
4235b045 | 76 | /* Parse a perf-probe command into event */ |
146a1439 | 77 | ret = parse_perf_probe_command(str, pev); |
4235b045 | 78 | pr_debug("%d arguments\n", pev->nargs); |
146a1439 MH |
79 | |
80 | return ret; | |
46ab4926 MH |
81 | } |
82 | ||
146a1439 | 83 | static int parse_probe_event_argv(int argc, const char **argv) |
d1bde3f7 | 84 | { |
146a1439 | 85 | int i, len, ret; |
d1bde3f7 MH |
86 | char *buf; |
87 | ||
88 | /* Bind up rest arguments */ | |
89 | len = 0; | |
90 | for (i = 0; i < argc; i++) | |
91 | len += strlen(argv[i]) + 1; | |
8a7ddad8 ACM |
92 | buf = zalloc(len + 1); |
93 | if (buf == NULL) | |
94 | return -ENOMEM; | |
d1bde3f7 MH |
95 | len = 0; |
96 | for (i = 0; i < argc; i++) | |
97 | len += sprintf(&buf[len], "%s ", argv[i]); | |
cf6eb489 | 98 | params.mod_events = true; |
146a1439 | 99 | ret = parse_probe_event(buf); |
d1bde3f7 | 100 | free(buf); |
146a1439 | 101 | return ret; |
d1bde3f7 MH |
102 | } |
103 | ||
253977b0 | 104 | static int opt_add_probe_event(const struct option *opt __used, |
46ab4926 MH |
105 | const char *str, int unset __used) |
106 | { | |
cf6eb489 MH |
107 | if (str) { |
108 | params.mod_events = true; | |
146a1439 | 109 | return parse_probe_event(str); |
cf6eb489 | 110 | } else |
146a1439 | 111 | return 0; |
4ea42b18 MH |
112 | } |
113 | ||
fa28244d MH |
114 | static int opt_del_probe_event(const struct option *opt __used, |
115 | const char *str, int unset __used) | |
116 | { | |
117 | if (str) { | |
cf6eb489 | 118 | params.mod_events = true; |
12a1fadb MH |
119 | if (!params.dellist) |
120 | params.dellist = strlist__new(true, NULL); | |
121 | strlist__add(params.dellist, str); | |
fa28244d MH |
122 | } |
123 | return 0; | |
124 | } | |
125 | ||
4b4da7f7 | 126 | #ifdef DWARF_SUPPORT |
0eda7385 HM |
127 | static int opt_show_lines(const struct option *opt __used, |
128 | const char *str, int unset __used) | |
129 | { | |
146a1439 MH |
130 | int ret = 0; |
131 | ||
0eda7385 | 132 | if (str) |
146a1439 | 133 | ret = parse_line_range_desc(str, ¶ms.line_range); |
12a1fadb MH |
134 | INIT_LIST_HEAD(¶ms.line_range.line_list); |
135 | params.show_lines = true; | |
146a1439 MH |
136 | |
137 | return ret; | |
0eda7385 | 138 | } |
cf6eb489 MH |
139 | |
140 | static int opt_show_vars(const struct option *opt __used, | |
141 | const char *str, int unset __used) | |
142 | { | |
143 | struct perf_probe_event *pev = ¶ms.events[params.nevents]; | |
144 | int ret; | |
145 | ||
146 | if (!str) | |
147 | return 0; | |
148 | ||
149 | ret = parse_probe_event(str); | |
150 | if (!ret && pev->nargs != 0) { | |
151 | pr_err(" Error: '--vars' doesn't accept arguments.\n"); | |
152 | return -EINVAL; | |
153 | } | |
154 | params.show_vars = true; | |
155 | ||
156 | return ret; | |
157 | } | |
23e8ec0d | 158 | #endif |
4ea42b18 MH |
159 | |
160 | static const char * const probe_usage[] = { | |
46ab4926 MH |
161 | "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]", |
162 | "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]", | |
fa28244d | 163 | "perf probe [<options>] --del '[GROUP:]EVENT' ...", |
4de189fe | 164 | "perf probe --list", |
4b4da7f7 | 165 | #ifdef DWARF_SUPPORT |
469b9b88 MH |
166 | "perf probe [<options>] --line 'LINEDESC'", |
167 | "perf probe [<options>] --vars 'PROBEPOINT'", | |
f3ab481c | 168 | #endif |
4ea42b18 MH |
169 | NULL |
170 | }; | |
171 | ||
172 | static const struct option options[] = { | |
c0555642 | 173 | OPT_INCR('v', "verbose", &verbose, |
89c69c0e | 174 | "be more verbose (show parsed arguments, etc)"), |
12a1fadb | 175 | OPT_BOOLEAN('l', "list", ¶ms.list_events, |
fac13fd5 | 176 | "list up current probe events"), |
fa28244d MH |
177 | OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.", |
178 | opt_del_probe_event), | |
46ab4926 | 179 | OPT_CALLBACK('a', "add", NULL, |
4b4da7f7 | 180 | #ifdef DWARF_SUPPORT |
32cb0dd5 | 181 | "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT" |
48481938 | 182 | " [[NAME=]ARG ...]", |
4b4da7f7 | 183 | #else |
48481938 | 184 | "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]", |
23e8ec0d | 185 | #endif |
4ea42b18 | 186 | "probe point definition, where\n" |
af663d75 MH |
187 | "\t\tGROUP:\tGroup name (optional)\n" |
188 | "\t\tEVENT:\tEvent name\n" | |
4ea42b18 | 189 | "\t\tFUNC:\tFunction name\n" |
2a9c8c36 | 190 | "\t\tOFF:\tOffset from function entry (in byte)\n" |
253977b0 | 191 | "\t\t%return:\tPut the probe at function return\n" |
4b4da7f7 | 192 | #ifdef DWARF_SUPPORT |
4ea42b18 | 193 | "\t\tSRC:\tSource code path\n" |
2a9c8c36 MH |
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" | |
4ea42b18 | 197 | "\t\tARG:\tProbe argument (local variable name or\n" |
50656eec | 198 | "\t\t\tkprobe-tracer argument format.)\n", |
4b4da7f7 MH |
199 | #else |
200 | "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n", | |
201 | #endif | |
253977b0 | 202 | opt_add_probe_event), |
12a1fadb | 203 | OPT_BOOLEAN('f', "force", ¶ms.force_add, "forcibly add events" |
d761b08b | 204 | " with existing name"), |
4b4da7f7 | 205 | #ifdef DWARF_SUPPORT |
631c9def | 206 | OPT_CALLBACK('L', "line", NULL, |
085ea739 | 207 | "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]", |
631c9def | 208 | "Show source code lines.", opt_show_lines), |
cf6eb489 MH |
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), | |
fb8c5a56 MH |
212 | OPT_BOOLEAN('\0', "externs", ¶ms.show_ext_vars, |
213 | "Show external variables too (with --vars only)"), | |
4b4da7f7 MH |
214 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
215 | "file", "vmlinux pathname"), | |
9ed7e1b8 CD |
216 | OPT_STRING('s', "source", &symbol_conf.source_prefix, |
217 | "directory", "path to kernel source"), | |
469b9b88 MH |
218 | OPT_STRING('m', "module", ¶ms.target_module, |
219 | "modname", "target module name"), | |
631c9def | 220 | #endif |
f4d7da49 | 221 | OPT__DRY_RUN(&probe_event_dry_run), |
ef4a3565 MH |
222 | OPT_INTEGER('\0', "max-probes", ¶ms.max_probe_points, |
223 | "Set how many probe points can be found for a probe."), | |
4ea42b18 MH |
224 | OPT_END() |
225 | }; | |
226 | ||
4ea42b18 MH |
227 | int cmd_probe(int argc, const char **argv, const char *prefix __used) |
228 | { | |
146a1439 MH |
229 | int ret; |
230 | ||
4ea42b18 | 231 | argc = parse_options(argc, argv, options, probe_usage, |
46ab4926 | 232 | PARSE_OPT_STOP_AT_NON_OPTION); |
ce11a603 MH |
233 | if (argc > 0) { |
234 | if (strcmp(argv[0], "-") == 0) { | |
235 | pr_warning(" Error: '-' is not supported.\n"); | |
236 | usage_with_options(probe_usage, options); | |
237 | } | |
146a1439 MH |
238 | ret = parse_probe_event_argv(argc, argv); |
239 | if (ret < 0) { | |
240 | pr_err(" Error: Parse Error. (%d)\n", ret); | |
241 | return ret; | |
242 | } | |
ce11a603 | 243 | } |
46ab4926 | 244 | |
ef4a3565 MH |
245 | if (params.max_probe_points == 0) |
246 | params.max_probe_points = MAX_PROBES; | |
247 | ||
4235b045 | 248 | if ((!params.nevents && !params.dellist && !params.list_events && |
12a1fadb | 249 | !params.show_lines)) |
4ea42b18 MH |
250 | usage_with_options(probe_usage, options); |
251 | ||
12a1fadb | 252 | if (params.list_events) { |
cf6eb489 | 253 | if (params.mod_events) { |
146a1439 | 254 | pr_err(" Error: Don't use --list with --add/--del.\n"); |
fa28244d MH |
255 | usage_with_options(probe_usage, options); |
256 | } | |
12a1fadb | 257 | if (params.show_lines) { |
146a1439 | 258 | pr_err(" Error: Don't use --list with --line.\n"); |
631c9def MH |
259 | usage_with_options(probe_usage, options); |
260 | } | |
cf6eb489 MH |
261 | if (params.show_vars) { |
262 | pr_err(" Error: Don't use --list with --vars.\n"); | |
263 | usage_with_options(probe_usage, options); | |
264 | } | |
146a1439 MH |
265 | ret = show_perf_probe_events(); |
266 | if (ret < 0) | |
267 | pr_err(" Error: Failed to show event list. (%d)\n", | |
268 | ret); | |
269 | return ret; | |
4de189fe MH |
270 | } |
271 | ||
4b4da7f7 | 272 | #ifdef DWARF_SUPPORT |
12a1fadb | 273 | if (params.show_lines) { |
cf6eb489 MH |
274 | if (params.mod_events) { |
275 | pr_err(" Error: Don't use --line with" | |
276 | " --add/--del.\n"); | |
277 | usage_with_options(probe_usage, options); | |
278 | } | |
279 | if (params.show_vars) { | |
280 | pr_err(" Error: Don't use --line with --vars.\n"); | |
631c9def MH |
281 | usage_with_options(probe_usage, options); |
282 | } | |
e0faa8d3 | 283 | |
469b9b88 | 284 | ret = show_line_range(¶ms.line_range, params.target_module); |
146a1439 MH |
285 | if (ret < 0) |
286 | pr_err(" Error: Failed to show lines. (%d)\n", ret); | |
287 | return ret; | |
631c9def | 288 | } |
cf6eb489 MH |
289 | if (params.show_vars) { |
290 | if (params.mod_events) { | |
291 | pr_err(" Error: Don't use --vars with" | |
292 | " --add/--del.\n"); | |
293 | usage_with_options(probe_usage, options); | |
294 | } | |
295 | ret = show_available_vars(params.events, params.nevents, | |
fb8c5a56 | 296 | params.max_probe_points, |
469b9b88 | 297 | params.target_module, |
fb8c5a56 | 298 | params.show_ext_vars); |
cf6eb489 MH |
299 | if (ret < 0) |
300 | pr_err(" Error: Failed to show vars. (%d)\n", ret); | |
301 | return ret; | |
302 | } | |
631c9def MH |
303 | #endif |
304 | ||
12a1fadb | 305 | if (params.dellist) { |
146a1439 | 306 | ret = del_perf_probe_events(params.dellist); |
12a1fadb | 307 | strlist__delete(params.dellist); |
146a1439 MH |
308 | if (ret < 0) { |
309 | pr_err(" Error: Failed to delete events. (%d)\n", ret); | |
310 | return ret; | |
311 | } | |
fa28244d MH |
312 | } |
313 | ||
146a1439 MH |
314 | if (params.nevents) { |
315 | ret = add_perf_probe_events(params.events, params.nevents, | |
c82ec0a2 | 316 | params.max_probe_points, |
469b9b88 | 317 | params.target_module, |
c82ec0a2 | 318 | params.force_add); |
146a1439 MH |
319 | if (ret < 0) { |
320 | pr_err(" Error: Failed to add events. (%d)\n", ret); | |
321 | return ret; | |
322 | } | |
323 | } | |
4ea42b18 MH |
324 | return 0; |
325 | } |