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