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