Commit | Line | Data |
---|---|---|
50656eec MH |
1 | /* |
2 | * probe-event.c : perf-probe definition to kprobe_events format converter | |
3 | * | |
4 | * Written by Masami Hiramatsu <mhiramat@redhat.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
19 | * | |
20 | */ | |
21 | ||
22 | #define _GNU_SOURCE | |
23 | #include <sys/utsname.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
26 | #include <fcntl.h> | |
27 | #include <errno.h> | |
28 | #include <stdio.h> | |
29 | #include <unistd.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | ||
33 | #undef _GNU_SOURCE | |
34 | #include "event.h" | |
e1c01d61 | 35 | #include "string.h" |
50656eec MH |
36 | #include "debug.h" |
37 | #include "parse-events.h" /* For debugfs_path */ | |
38 | #include "probe-event.h" | |
39 | ||
40 | #define MAX_CMDLEN 256 | |
41 | #define MAX_PROBE_ARGS 128 | |
42 | #define PERFPROBE_GROUP "probe" | |
43 | ||
44 | #define semantic_error(msg ...) die("Semantic error :" msg) | |
45 | ||
46 | /* Parse probepoint definition. */ | |
47 | static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) | |
48 | { | |
49 | char *ptr, *tmp; | |
50 | char c, nc = 0; | |
51 | /* | |
52 | * <Syntax> | |
53 | * perf probe SRC:LN | |
54 | * perf probe FUNC[+OFFS|%return][@SRC] | |
55 | */ | |
56 | ||
57 | ptr = strpbrk(arg, ":+@%"); | |
58 | if (ptr) { | |
59 | nc = *ptr; | |
60 | *ptr++ = '\0'; | |
61 | } | |
62 | ||
63 | /* Check arg is function or file and copy it */ | |
64 | if (strchr(arg, '.')) /* File */ | |
65 | pp->file = strdup(arg); | |
66 | else /* Function */ | |
67 | pp->function = strdup(arg); | |
68 | DIE_IF(pp->file == NULL && pp->function == NULL); | |
69 | ||
70 | /* Parse other options */ | |
71 | while (ptr) { | |
72 | arg = ptr; | |
73 | c = nc; | |
74 | ptr = strpbrk(arg, ":+@%"); | |
75 | if (ptr) { | |
76 | nc = *ptr; | |
77 | *ptr++ = '\0'; | |
78 | } | |
79 | switch (c) { | |
80 | case ':': /* Line number */ | |
81 | pp->line = strtoul(arg, &tmp, 0); | |
82 | if (*tmp != '\0') | |
83 | semantic_error("There is non-digit charactor" | |
84 | " in line number."); | |
85 | break; | |
86 | case '+': /* Byte offset from a symbol */ | |
87 | pp->offset = strtoul(arg, &tmp, 0); | |
88 | if (*tmp != '\0') | |
89 | semantic_error("There is non-digit charactor" | |
90 | " in offset."); | |
91 | break; | |
92 | case '@': /* File name */ | |
93 | if (pp->file) | |
94 | semantic_error("SRC@SRC is not allowed."); | |
95 | pp->file = strdup(arg); | |
96 | DIE_IF(pp->file == NULL); | |
97 | if (ptr) | |
98 | semantic_error("@SRC must be the last " | |
99 | "option."); | |
100 | break; | |
101 | case '%': /* Probe places */ | |
102 | if (strcmp(arg, "return") == 0) { | |
103 | pp->retprobe = 1; | |
104 | } else /* Others not supported yet */ | |
105 | semantic_error("%%%s is not supported.", arg); | |
106 | break; | |
107 | default: | |
108 | DIE_IF("Program has a bug."); | |
109 | break; | |
110 | } | |
111 | } | |
112 | ||
113 | /* Exclusion check */ | |
114 | if (pp->line && pp->offset) | |
115 | semantic_error("Offset can't be used with line number."); | |
116 | ||
117 | if (!pp->line && pp->file && !pp->function) | |
118 | semantic_error("File always requires line number."); | |
119 | ||
120 | if (pp->offset && !pp->function) | |
121 | semantic_error("Offset requires an entry function."); | |
122 | ||
123 | if (pp->retprobe && !pp->function) | |
124 | semantic_error("Return probe requires an entry function."); | |
125 | ||
126 | if ((pp->offset || pp->line) && pp->retprobe) | |
127 | semantic_error("Offset/Line can't be used with return probe."); | |
128 | ||
129 | pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n", | |
130 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe); | |
131 | } | |
132 | ||
133 | /* Parse perf-probe event definition */ | |
134 | int parse_perf_probe_event(const char *str, struct probe_point *pp) | |
135 | { | |
e1c01d61 | 136 | char **argv; |
50656eec MH |
137 | int argc, i, need_dwarf = 0; |
138 | ||
e1c01d61 MH |
139 | argv = argv_split(str, &argc); |
140 | if (!argv) | |
141 | die("argv_split failed."); | |
142 | if (argc > MAX_PROBE_ARGS + 1) | |
143 | semantic_error("Too many arguments"); | |
50656eec MH |
144 | |
145 | /* Parse probe point */ | |
146 | parse_perf_probe_probepoint(argv[0], pp); | |
50656eec MH |
147 | if (pp->file || pp->line) |
148 | need_dwarf = 1; | |
149 | ||
e1c01d61 | 150 | /* Copy arguments and ensure return probe has no C argument */ |
50656eec | 151 | pp->nr_args = argc - 1; |
e1c01d61 MH |
152 | pp->args = zalloc(sizeof(char *) * pp->nr_args); |
153 | for (i = 0; i < pp->nr_args; i++) { | |
154 | pp->args[i] = strdup(argv[i + 1]); | |
155 | if (!pp->args[i]) | |
156 | die("Failed to copy argument."); | |
50656eec MH |
157 | if (is_c_varname(pp->args[i])) { |
158 | if (pp->retprobe) | |
159 | semantic_error("You can't specify local" | |
160 | " variable for kretprobe"); | |
161 | need_dwarf = 1; | |
162 | } | |
e1c01d61 | 163 | } |
50656eec | 164 | |
e1c01d61 | 165 | argv_free(argv); |
50656eec MH |
166 | return need_dwarf; |
167 | } | |
168 | ||
169 | int synthesize_trace_kprobe_event(struct probe_point *pp) | |
170 | { | |
171 | char *buf; | |
172 | int i, len, ret; | |
173 | ||
174 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); | |
175 | if (!buf) | |
176 | die("Failed to allocate memory by zalloc."); | |
177 | ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); | |
178 | if (ret <= 0 || ret >= MAX_CMDLEN) | |
179 | goto error; | |
180 | len = ret; | |
181 | ||
182 | for (i = 0; i < pp->nr_args; i++) { | |
183 | ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s", | |
184 | pp->args[i]); | |
185 | if (ret <= 0 || ret >= MAX_CMDLEN - len) | |
186 | goto error; | |
187 | len += ret; | |
188 | } | |
189 | pp->found = 1; | |
190 | ||
191 | return pp->found; | |
192 | error: | |
193 | free(pp->probes[0]); | |
194 | if (ret > 0) | |
195 | ret = -E2BIG; | |
196 | ||
197 | return ret; | |
198 | } | |
199 | ||
200 | static int write_trace_kprobe_event(int fd, const char *buf) | |
201 | { | |
202 | int ret; | |
203 | ||
204 | ret = write(fd, buf, strlen(buf)); | |
205 | if (ret <= 0) | |
206 | die("Failed to create event."); | |
207 | else | |
208 | printf("Added new event: %s\n", buf); | |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
213 | void add_trace_kprobe_events(struct probe_point *probes, int nr_probes) | |
214 | { | |
215 | int i, j, fd; | |
216 | struct probe_point *pp; | |
217 | char buf[MAX_CMDLEN]; | |
218 | ||
219 | snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path); | |
220 | fd = open(buf, O_WRONLY, O_APPEND); | |
221 | if (fd < 0) { | |
222 | if (errno == ENOENT) | |
223 | die("kprobe_events file does not exist -" | |
224 | " please rebuild with CONFIG_KPROBE_TRACER."); | |
225 | else | |
226 | die("Could not open kprobe_events file: %s", | |
227 | strerror(errno)); | |
228 | } | |
229 | ||
230 | for (j = 0; j < nr_probes; j++) { | |
231 | pp = probes + j; | |
232 | if (pp->found == 1) { | |
233 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n", | |
234 | pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP, | |
235 | pp->function, pp->offset, pp->probes[0]); | |
236 | write_trace_kprobe_event(fd, buf); | |
237 | } else | |
238 | for (i = 0; i < pp->found; i++) { | |
239 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n", | |
240 | pp->retprobe ? 'r' : 'p', | |
241 | PERFPROBE_GROUP, | |
242 | pp->function, pp->offset, i, | |
243 | pp->probes[i]); | |
244 | write_trace_kprobe_event(fd, buf); | |
245 | } | |
246 | } | |
247 | close(fd); | |
248 | } |