]>
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" |
89c69c0e MH |
39 | #include "util/event.h" |
40 | #include "util/debug.h" | |
a128168d MH |
41 | #include "util/symbol.h" |
42 | #include "util/thread.h" | |
43 | #include "util/session.h" | |
4ea42b18 MH |
44 | #include "util/parse-options.h" |
45 | #include "util/parse-events.h" /* For debugfs_path */ | |
46 | #include "util/probe-finder.h" | |
50656eec | 47 | #include "util/probe-event.h" |
4ea42b18 | 48 | |
4ea42b18 MH |
49 | #define MAX_PATH_LEN 256 |
50 | #define MAX_PROBES 128 | |
51 | ||
52 | /* Session management structure */ | |
53 | static struct { | |
fac13fd5 MH |
54 | bool need_dwarf; |
55 | bool list_events; | |
d761b08b | 56 | bool force_add; |
4ea42b18 MH |
57 | int nr_probe; |
58 | struct probe_point probes[MAX_PROBES]; | |
fa28244d | 59 | struct strlist *dellist; |
a128168d | 60 | struct perf_session *psession; |
62bdc1b3 | 61 | struct map *kmap; |
4ea42b18 MH |
62 | } session; |
63 | ||
4de189fe | 64 | |
253977b0 MH |
65 | /* Parse an event definition. Note that any error must die. */ |
66 | static void parse_probe_event(const char *str) | |
4ea42b18 | 67 | { |
4ea42b18 | 68 | struct probe_point *pp = &session.probes[session.nr_probe]; |
4ea42b18 | 69 | |
b7cb10e7 | 70 | pr_debug("probe-definition(%d): %s\n", session.nr_probe, str); |
4ea42b18 | 71 | if (++session.nr_probe == MAX_PROBES) |
50656eec | 72 | die("Too many probes (> %d) are specified.", MAX_PROBES); |
4ea42b18 | 73 | |
50656eec | 74 | /* Parse perf-probe event into probe_point */ |
fac13fd5 | 75 | parse_perf_probe_event(str, pp, &session.need_dwarf); |
23e8ec0d | 76 | |
b7cb10e7 | 77 | pr_debug("%d arguments\n", pp->nr_args); |
46ab4926 MH |
78 | } |
79 | ||
d1bde3f7 MH |
80 | static void parse_probe_event_argv(int argc, const char **argv) |
81 | { | |
82 | int i, len; | |
83 | char *buf; | |
84 | ||
85 | /* Bind up rest arguments */ | |
86 | len = 0; | |
87 | for (i = 0; i < argc; i++) | |
88 | len += strlen(argv[i]) + 1; | |
89 | buf = zalloc(len + 1); | |
90 | if (!buf) | |
91 | die("Failed to allocate memory for binding arguments."); | |
92 | len = 0; | |
93 | for (i = 0; i < argc; i++) | |
94 | len += sprintf(&buf[len], "%s ", argv[i]); | |
95 | parse_probe_event(buf); | |
96 | free(buf); | |
97 | } | |
98 | ||
253977b0 | 99 | static int opt_add_probe_event(const struct option *opt __used, |
46ab4926 MH |
100 | const char *str, int unset __used) |
101 | { | |
102 | if (str) | |
253977b0 | 103 | parse_probe_event(str); |
4ea42b18 MH |
104 | return 0; |
105 | } | |
106 | ||
fa28244d MH |
107 | static int opt_del_probe_event(const struct option *opt __used, |
108 | const char *str, int unset __used) | |
109 | { | |
110 | if (str) { | |
111 | if (!session.dellist) | |
112 | session.dellist = strlist__new(true, NULL); | |
113 | strlist__add(session.dellist, str); | |
114 | } | |
115 | return 0; | |
116 | } | |
117 | ||
62bdc1b3 MH |
118 | /* Currently just checking function name from symbol map */ |
119 | static void evaluate_probe_point(struct probe_point *pp) | |
120 | { | |
121 | struct symbol *sym; | |
122 | sym = map__find_symbol_by_name(session.kmap, pp->function, | |
123 | session.psession, NULL); | |
124 | if (!sym) | |
125 | die("Kernel symbol \'%s\' not found - probe not added.", | |
126 | pp->function); | |
127 | } | |
128 | ||
23e8ec0d | 129 | #ifndef NO_LIBDWARF |
a128168d | 130 | static int open_vmlinux(void) |
4ea42b18 | 131 | { |
62bdc1b3 | 132 | if (map__load(session.kmap, session.psession, NULL) < 0) { |
a128168d MH |
133 | pr_debug("Failed to load kernel map.\n"); |
134 | return -EINVAL; | |
4ea42b18 | 135 | } |
62bdc1b3 MH |
136 | pr_debug("Try to open %s\n", session.kmap->dso->long_name); |
137 | return open(session.kmap->dso->long_name, O_RDONLY); | |
4ea42b18 | 138 | } |
23e8ec0d | 139 | #endif |
4ea42b18 MH |
140 | |
141 | static const char * const probe_usage[] = { | |
46ab4926 MH |
142 | "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]", |
143 | "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]", | |
fa28244d | 144 | "perf probe [<options>] --del '[GROUP:]EVENT' ...", |
4de189fe | 145 | "perf probe --list", |
4ea42b18 MH |
146 | NULL |
147 | }; | |
148 | ||
149 | static const struct option options[] = { | |
89c69c0e MH |
150 | OPT_BOOLEAN('v', "verbose", &verbose, |
151 | "be more verbose (show parsed arguments, etc)"), | |
23e8ec0d | 152 | #ifndef NO_LIBDWARF |
75be6cf4 | 153 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
a128168d | 154 | "file", "vmlinux pathname"), |
23e8ec0d | 155 | #endif |
fac13fd5 MH |
156 | OPT_BOOLEAN('l', "list", &session.list_events, |
157 | "list up current probe events"), | |
fa28244d MH |
158 | OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.", |
159 | opt_del_probe_event), | |
46ab4926 | 160 | OPT_CALLBACK('a', "add", NULL, |
23e8ec0d | 161 | #ifdef NO_LIBDWARF |
af663d75 | 162 | "[EVENT=]FUNC[+OFFS|%return] [ARG ...]", |
23e8ec0d | 163 | #else |
af663d75 | 164 | "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]", |
23e8ec0d | 165 | #endif |
4ea42b18 | 166 | "probe point definition, where\n" |
af663d75 MH |
167 | "\t\tGROUP:\tGroup name (optional)\n" |
168 | "\t\tEVENT:\tEvent name\n" | |
4ea42b18 MH |
169 | "\t\tFUNC:\tFunction name\n" |
170 | "\t\tOFFS:\tOffset from function entry (in byte)\n" | |
253977b0 | 171 | "\t\t%return:\tPut the probe at function return\n" |
23e8ec0d MH |
172 | #ifdef NO_LIBDWARF |
173 | "\t\tARG:\tProbe argument (only \n" | |
174 | #else | |
4ea42b18 | 175 | "\t\tSRC:\tSource code path\n" |
b0ef0732 MH |
176 | "\t\tRLN:\tRelative line number from function entry.\n" |
177 | "\t\tALN:\tAbsolute line number in file.\n" | |
4ea42b18 | 178 | "\t\tARG:\tProbe argument (local variable name or\n" |
23e8ec0d | 179 | #endif |
50656eec | 180 | "\t\t\tkprobe-tracer argument format.)\n", |
253977b0 | 181 | opt_add_probe_event), |
d761b08b MH |
182 | OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events" |
183 | " with existing name"), | |
4ea42b18 MH |
184 | OPT_END() |
185 | }; | |
186 | ||
4ea42b18 MH |
187 | int cmd_probe(int argc, const char **argv, const char *prefix __used) |
188 | { | |
d1bde3f7 | 189 | int i, ret; |
bdad0db7 XG |
190 | #ifndef NO_LIBDWARF |
191 | int fd; | |
192 | #endif | |
4ea42b18 | 193 | struct probe_point *pp; |
4ea42b18 MH |
194 | |
195 | argc = parse_options(argc, argv, options, probe_usage, | |
46ab4926 | 196 | PARSE_OPT_STOP_AT_NON_OPTION); |
ce11a603 MH |
197 | if (argc > 0) { |
198 | if (strcmp(argv[0], "-") == 0) { | |
199 | pr_warning(" Error: '-' is not supported.\n"); | |
200 | usage_with_options(probe_usage, options); | |
201 | } | |
d1bde3f7 | 202 | parse_probe_event_argv(argc, argv); |
ce11a603 | 203 | } |
46ab4926 | 204 | |
fac13fd5 | 205 | if ((!session.nr_probe && !session.dellist && !session.list_events)) |
4ea42b18 MH |
206 | usage_with_options(probe_usage, options); |
207 | ||
fac13fd5 | 208 | if (session.list_events) { |
fa28244d MH |
209 | if (session.nr_probe != 0 || session.dellist) { |
210 | pr_warning(" Error: Don't use --list with" | |
211 | " --add/--del.\n"); | |
212 | usage_with_options(probe_usage, options); | |
213 | } | |
4de189fe MH |
214 | show_perf_probe_events(); |
215 | return 0; | |
216 | } | |
217 | ||
fa28244d MH |
218 | if (session.dellist) { |
219 | del_trace_kprobe_events(session.dellist); | |
220 | strlist__delete(session.dellist); | |
221 | if (session.nr_probe == 0) | |
222 | return 0; | |
223 | } | |
224 | ||
a128168d | 225 | /* Initialize symbol maps for vmlinux */ |
75be6cf4 ACM |
226 | symbol_conf.sort_by_name = true; |
227 | if (symbol_conf.vmlinux_name == NULL) | |
228 | symbol_conf.try_vmlinux_path = true; | |
229 | if (symbol__init() < 0) | |
a128168d | 230 | die("Failed to init symbol map."); |
75be6cf4 | 231 | session.psession = perf_session__new(NULL, O_WRONLY, false); |
a128168d MH |
232 | if (session.psession == NULL) |
233 | die("Failed to init perf_session."); | |
62bdc1b3 MH |
234 | session.kmap = map_groups__find_by_name(&session.psession->kmaps, |
235 | MAP__FUNCTION, | |
236 | "[kernel.kallsyms]"); | |
237 | if (!session.kmap) | |
238 | die("Could not find kernel map.\n"); | |
a128168d | 239 | |
23e8ec0d | 240 | if (session.need_dwarf) |
a225a1d9 | 241 | #ifdef NO_LIBDWARF |
50656eec | 242 | die("Debuginfo-analysis is not supported"); |
a225a1d9 | 243 | #else /* !NO_LIBDWARF */ |
f41b1e43 | 244 | pr_debug("Some probes require debuginfo.\n"); |
4ea42b18 | 245 | |
a128168d | 246 | fd = open_vmlinux(); |
a225a1d9 MH |
247 | if (fd < 0) { |
248 | if (session.need_dwarf) | |
f984f03d | 249 | die("Could not open debuginfo file."); |
a225a1d9 | 250 | |
d3a2dbf8 MH |
251 | pr_debug("Could not open vmlinux/module file." |
252 | " Try to use symbols.\n"); | |
a225a1d9 MH |
253 | goto end_dwarf; |
254 | } | |
4ea42b18 MH |
255 | |
256 | /* Searching probe points */ | |
d1bde3f7 MH |
257 | for (i = 0; i < session.nr_probe; i++) { |
258 | pp = &session.probes[i]; | |
4ea42b18 MH |
259 | if (pp->found) |
260 | continue; | |
261 | ||
262 | lseek(fd, SEEK_SET, 0); | |
263 | ret = find_probepoint(fd, pp); | |
411edfe5 MH |
264 | if (ret > 0) |
265 | continue; | |
7ef17aaf MH |
266 | if (ret == 0) { /* No error but failed to find probe point. */ |
267 | synthesize_perf_probe_point(pp); | |
268 | die("Probe point '%s' not found. - probe not added.", | |
269 | pp->probes[0]); | |
270 | } | |
411edfe5 MH |
271 | /* Error path */ |
272 | if (session.need_dwarf) { | |
273 | if (ret == -ENOENT) | |
274 | pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n"); | |
275 | die("Could not analyze debuginfo."); | |
276 | } | |
277 | pr_debug("An error occurred in debuginfo analysis." | |
278 | " Try to use symbols.\n"); | |
279 | break; | |
4ea42b18 MH |
280 | } |
281 | close(fd); | |
282 | ||
a225a1d9 | 283 | end_dwarf: |
23e8ec0d MH |
284 | #endif /* !NO_LIBDWARF */ |
285 | ||
a225a1d9 | 286 | /* Synthesize probes without dwarf */ |
d1bde3f7 MH |
287 | for (i = 0; i < session.nr_probe; i++) { |
288 | pp = &session.probes[i]; | |
a225a1d9 MH |
289 | if (pp->found) /* This probe is already found. */ |
290 | continue; | |
291 | ||
62bdc1b3 | 292 | evaluate_probe_point(pp); |
50656eec | 293 | ret = synthesize_trace_kprobe_event(pp); |
a225a1d9 | 294 | if (ret == -E2BIG) |
50656eec | 295 | die("probe point definition becomes too long."); |
a225a1d9 MH |
296 | else if (ret < 0) |
297 | die("Failed to synthesize a probe point."); | |
298 | } | |
299 | ||
4ea42b18 | 300 | /* Settng up probe points */ |
d761b08b MH |
301 | add_trace_kprobe_events(session.probes, session.nr_probe, |
302 | session.force_add); | |
4ea42b18 MH |
303 | return 0; |
304 | } | |
305 |