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" | |
89c69c0e MH |
38 | #include "util/event.h" |
39 | #include "util/debug.h" | |
4ea42b18 MH |
40 | #include "util/parse-options.h" |
41 | #include "util/parse-events.h" /* For debugfs_path */ | |
42 | #include "util/probe-finder.h" | |
50656eec | 43 | #include "util/probe-event.h" |
4ea42b18 MH |
44 | |
45 | /* Default vmlinux search paths */ | |
46 | #define NR_SEARCH_PATH 3 | |
47 | const char *default_search_path[NR_SEARCH_PATH] = { | |
48 | "/lib/modules/%s/build/vmlinux", /* Custom build kernel */ | |
49 | "/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */ | |
50 | "/boot/vmlinux-debug-%s", /* Ubuntu */ | |
51 | }; | |
52 | ||
53 | #define MAX_PATH_LEN 256 | |
54 | #define MAX_PROBES 128 | |
55 | ||
56 | /* Session management structure */ | |
57 | static struct { | |
58 | char *vmlinux; | |
59 | char *release; | |
23e8ec0d | 60 | int need_dwarf; |
4ea42b18 MH |
61 | int nr_probe; |
62 | struct probe_point probes[MAX_PROBES]; | |
4ea42b18 MH |
63 | } session; |
64 | ||
4de189fe MH |
65 | static bool listing; |
66 | ||
253977b0 MH |
67 | /* Parse an event definition. Note that any error must die. */ |
68 | static void parse_probe_event(const char *str) | |
4ea42b18 | 69 | { |
4ea42b18 | 70 | struct probe_point *pp = &session.probes[session.nr_probe]; |
4ea42b18 | 71 | |
b7cb10e7 | 72 | pr_debug("probe-definition(%d): %s\n", session.nr_probe, str); |
4ea42b18 | 73 | if (++session.nr_probe == MAX_PROBES) |
50656eec | 74 | die("Too many probes (> %d) are specified.", MAX_PROBES); |
4ea42b18 | 75 | |
50656eec MH |
76 | /* Parse perf-probe event into probe_point */ |
77 | session.need_dwarf = parse_perf_probe_event(str, pp); | |
23e8ec0d | 78 | |
b7cb10e7 | 79 | pr_debug("%d arguments\n", pp->nr_args); |
46ab4926 MH |
80 | } |
81 | ||
253977b0 | 82 | static int opt_add_probe_event(const struct option *opt __used, |
46ab4926 MH |
83 | const char *str, int unset __used) |
84 | { | |
85 | if (str) | |
253977b0 | 86 | parse_probe_event(str); |
4ea42b18 MH |
87 | return 0; |
88 | } | |
89 | ||
23e8ec0d | 90 | #ifndef NO_LIBDWARF |
4ea42b18 MH |
91 | static int open_default_vmlinux(void) |
92 | { | |
93 | struct utsname uts; | |
94 | char fname[MAX_PATH_LEN]; | |
95 | int fd, ret, i; | |
96 | ||
97 | ret = uname(&uts); | |
98 | if (ret) { | |
b7cb10e7 | 99 | pr_debug("uname() failed.\n"); |
4ea42b18 MH |
100 | return -errno; |
101 | } | |
102 | session.release = uts.release; | |
103 | for (i = 0; i < NR_SEARCH_PATH; i++) { | |
104 | ret = snprintf(fname, MAX_PATH_LEN, | |
105 | default_search_path[i], session.release); | |
106 | if (ret >= MAX_PATH_LEN || ret < 0) { | |
b7cb10e7 | 107 | pr_debug("Filename(%d,%s) is too long.\n", i, |
89c69c0e | 108 | uts.release); |
4ea42b18 MH |
109 | errno = E2BIG; |
110 | return -E2BIG; | |
111 | } | |
b7cb10e7 | 112 | pr_debug("try to open %s\n", fname); |
4ea42b18 MH |
113 | fd = open(fname, O_RDONLY); |
114 | if (fd >= 0) | |
115 | break; | |
116 | } | |
117 | return fd; | |
118 | } | |
23e8ec0d | 119 | #endif |
4ea42b18 MH |
120 | |
121 | static const char * const probe_usage[] = { | |
46ab4926 MH |
122 | "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]", |
123 | "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]", | |
4de189fe | 124 | "perf probe --list", |
4ea42b18 MH |
125 | NULL |
126 | }; | |
127 | ||
128 | static const struct option options[] = { | |
89c69c0e MH |
129 | OPT_BOOLEAN('v', "verbose", &verbose, |
130 | "be more verbose (show parsed arguments, etc)"), | |
23e8ec0d | 131 | #ifndef NO_LIBDWARF |
4ea42b18 MH |
132 | OPT_STRING('k', "vmlinux", &session.vmlinux, "file", |
133 | "vmlinux/module pathname"), | |
23e8ec0d | 134 | #endif |
4de189fe | 135 | OPT_BOOLEAN('l', "list", &listing, "list up current probes"), |
46ab4926 | 136 | OPT_CALLBACK('a', "add", NULL, |
23e8ec0d | 137 | #ifdef NO_LIBDWARF |
253977b0 | 138 | "FUNC[+OFFS|%return] [ARG ...]", |
23e8ec0d | 139 | #else |
b0ef0732 | 140 | "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]", |
23e8ec0d | 141 | #endif |
4ea42b18 | 142 | "probe point definition, where\n" |
4ea42b18 MH |
143 | "\t\tGRP:\tGroup name (optional)\n" |
144 | "\t\tNAME:\tEvent name\n" | |
145 | "\t\tFUNC:\tFunction name\n" | |
146 | "\t\tOFFS:\tOffset from function entry (in byte)\n" | |
253977b0 | 147 | "\t\t%return:\tPut the probe at function return\n" |
23e8ec0d MH |
148 | #ifdef NO_LIBDWARF |
149 | "\t\tARG:\tProbe argument (only \n" | |
150 | #else | |
4ea42b18 | 151 | "\t\tSRC:\tSource code path\n" |
b0ef0732 MH |
152 | "\t\tRLN:\tRelative line number from function entry.\n" |
153 | "\t\tALN:\tAbsolute line number in file.\n" | |
4ea42b18 | 154 | "\t\tARG:\tProbe argument (local variable name or\n" |
23e8ec0d | 155 | #endif |
50656eec | 156 | "\t\t\tkprobe-tracer argument format.)\n", |
253977b0 | 157 | opt_add_probe_event), |
4ea42b18 MH |
158 | OPT_END() |
159 | }; | |
160 | ||
4ea42b18 MH |
161 | int cmd_probe(int argc, const char **argv, const char *prefix __used) |
162 | { | |
bdad0db7 XG |
163 | int i, j, ret; |
164 | #ifndef NO_LIBDWARF | |
165 | int fd; | |
166 | #endif | |
4ea42b18 | 167 | struct probe_point *pp; |
4ea42b18 MH |
168 | |
169 | argc = parse_options(argc, argv, options, probe_usage, | |
46ab4926 MH |
170 | PARSE_OPT_STOP_AT_NON_OPTION); |
171 | for (i = 0; i < argc; i++) | |
172 | parse_probe_event(argv[i]); | |
173 | ||
4de189fe MH |
174 | if ((session.nr_probe == 0 && !listing) || |
175 | (session.nr_probe != 0 && listing)) | |
4ea42b18 MH |
176 | usage_with_options(probe_usage, options); |
177 | ||
4de189fe MH |
178 | if (listing) { |
179 | show_perf_probe_events(); | |
180 | return 0; | |
181 | } | |
182 | ||
23e8ec0d | 183 | if (session.need_dwarf) |
a225a1d9 | 184 | #ifdef NO_LIBDWARF |
50656eec | 185 | die("Debuginfo-analysis is not supported"); |
a225a1d9 | 186 | #else /* !NO_LIBDWARF */ |
f41b1e43 | 187 | pr_debug("Some probes require debuginfo.\n"); |
4ea42b18 MH |
188 | |
189 | if (session.vmlinux) | |
190 | fd = open(session.vmlinux, O_RDONLY); | |
191 | else | |
192 | fd = open_default_vmlinux(); | |
a225a1d9 MH |
193 | if (fd < 0) { |
194 | if (session.need_dwarf) | |
195 | die("Could not open vmlinux/module file."); | |
196 | ||
d3a2dbf8 MH |
197 | pr_debug("Could not open vmlinux/module file." |
198 | " Try to use symbols.\n"); | |
a225a1d9 MH |
199 | goto end_dwarf; |
200 | } | |
4ea42b18 MH |
201 | |
202 | /* Searching probe points */ | |
203 | for (j = 0; j < session.nr_probe; j++) { | |
204 | pp = &session.probes[j]; | |
205 | if (pp->found) | |
206 | continue; | |
207 | ||
208 | lseek(fd, SEEK_SET, 0); | |
209 | ret = find_probepoint(fd, pp); | |
a225a1d9 MH |
210 | if (ret < 0) { |
211 | if (session.need_dwarf) | |
212 | die("Could not analyze debuginfo."); | |
213 | ||
214 | pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n"); | |
215 | break; | |
216 | } | |
217 | if (ret == 0) /* No error but failed to find probe point. */ | |
218 | die("No probe point found."); | |
4ea42b18 MH |
219 | } |
220 | close(fd); | |
221 | ||
a225a1d9 | 222 | end_dwarf: |
23e8ec0d MH |
223 | #endif /* !NO_LIBDWARF */ |
224 | ||
a225a1d9 MH |
225 | /* Synthesize probes without dwarf */ |
226 | for (j = 0; j < session.nr_probe; j++) { | |
227 | pp = &session.probes[j]; | |
228 | if (pp->found) /* This probe is already found. */ | |
229 | continue; | |
230 | ||
50656eec | 231 | ret = synthesize_trace_kprobe_event(pp); |
a225a1d9 | 232 | if (ret == -E2BIG) |
50656eec | 233 | die("probe point definition becomes too long."); |
a225a1d9 MH |
234 | else if (ret < 0) |
235 | die("Failed to synthesize a probe point."); | |
236 | } | |
237 | ||
4ea42b18 | 238 | /* Settng up probe points */ |
50656eec | 239 | add_trace_kprobe_events(session.probes, session.nr_probe); |
4ea42b18 MH |
240 | return 0; |
241 | } | |
242 |