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> | |
4de189fe MH |
32 | #include <stdarg.h> |
33 | #include <limits.h> | |
50656eec MH |
34 | |
35 | #undef _GNU_SOURCE | |
31facc5f | 36 | #include "util.h" |
50656eec | 37 | #include "event.h" |
e1c01d61 | 38 | #include "string.h" |
4de189fe | 39 | #include "strlist.h" |
50656eec | 40 | #include "debug.h" |
72041334 | 41 | #include "cache.h" |
631c9def | 42 | #include "color.h" |
e0faa8d3 MH |
43 | #include "symbol.h" |
44 | #include "thread.h" | |
50656eec MH |
45 | #include "parse-events.h" /* For debugfs_path */ |
46 | #include "probe-event.h" | |
47 | ||
48 | #define MAX_CMDLEN 256 | |
49 | #define MAX_PROBE_ARGS 128 | |
50 | #define PERFPROBE_GROUP "probe" | |
51 | ||
52 | #define semantic_error(msg ...) die("Semantic error :" msg) | |
53 | ||
4de189fe | 54 | /* If there is no space to write, returns -E2BIG. */ |
84988450 MH |
55 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
56 | __attribute__((format(printf, 3, 4))); | |
57 | ||
4de189fe MH |
58 | static int e_snprintf(char *str, size_t size, const char *format, ...) |
59 | { | |
60 | int ret; | |
61 | va_list ap; | |
62 | va_start(ap, format); | |
63 | ret = vsnprintf(str, size, format, ap); | |
64 | va_end(ap); | |
65 | if (ret >= (int)size) | |
66 | ret = -E2BIG; | |
67 | return ret; | |
68 | } | |
69 | ||
e0faa8d3 MH |
70 | |
71 | static struct map_groups kmap_groups; | |
72 | static struct map *kmaps[MAP__NR_TYPES]; | |
73 | ||
74 | /* Initialize symbol maps for vmlinux */ | |
75 | static void init_vmlinux(void) | |
76 | { | |
77 | symbol_conf.sort_by_name = true; | |
78 | if (symbol_conf.vmlinux_name == NULL) | |
79 | symbol_conf.try_vmlinux_path = true; | |
80 | else | |
81 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); | |
82 | if (symbol__init() < 0) | |
83 | die("Failed to init symbol map."); | |
84 | ||
85 | map_groups__init(&kmap_groups); | |
86 | if (map_groups__create_kernel_maps(&kmap_groups, kmaps) < 0) | |
87 | die("Failed to create kernel maps."); | |
88 | } | |
89 | ||
90 | #ifndef NO_DWARF_SUPPORT | |
91 | static int open_vmlinux(void) | |
92 | { | |
93 | if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) { | |
94 | pr_debug("Failed to load kernel map.\n"); | |
95 | return -EINVAL; | |
96 | } | |
97 | pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name); | |
98 | return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY); | |
99 | } | |
100 | #endif | |
101 | ||
631c9def MH |
102 | void parse_line_range_desc(const char *arg, struct line_range *lr) |
103 | { | |
104 | const char *ptr; | |
105 | char *tmp; | |
106 | /* | |
107 | * <Syntax> | |
108 | * SRC:SLN[+NUM|-ELN] | |
109 | * FUNC[:SLN[+NUM|-ELN]] | |
110 | */ | |
111 | ptr = strchr(arg, ':'); | |
112 | if (ptr) { | |
113 | lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0); | |
114 | if (*tmp == '+') | |
115 | lr->end = lr->start + (unsigned int)strtoul(tmp + 1, | |
116 | &tmp, 0); | |
117 | else if (*tmp == '-') | |
118 | lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0); | |
119 | else | |
120 | lr->end = 0; | |
121 | pr_debug("Line range is %u to %u\n", lr->start, lr->end); | |
122 | if (lr->end && lr->start > lr->end) | |
123 | semantic_error("Start line must be smaller" | |
124 | " than end line."); | |
125 | if (*tmp != '\0') | |
126 | semantic_error("Tailing with invalid character '%d'.", | |
127 | *tmp); | |
31facc5f | 128 | tmp = xstrndup(arg, (ptr - arg)); |
631c9def | 129 | } else |
31facc5f | 130 | tmp = xstrdup(arg); |
631c9def MH |
131 | |
132 | if (strchr(tmp, '.')) | |
133 | lr->file = tmp; | |
134 | else | |
135 | lr->function = tmp; | |
136 | } | |
137 | ||
b7702a21 MH |
138 | /* Check the name is good for event/group */ |
139 | static bool check_event_name(const char *name) | |
140 | { | |
141 | if (!isalpha(*name) && *name != '_') | |
142 | return false; | |
143 | while (*++name != '\0') { | |
144 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') | |
145 | return false; | |
146 | } | |
147 | return true; | |
148 | } | |
149 | ||
50656eec MH |
150 | /* Parse probepoint definition. */ |
151 | static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) | |
152 | { | |
153 | char *ptr, *tmp; | |
154 | char c, nc = 0; | |
155 | /* | |
156 | * <Syntax> | |
2a9c8c36 MH |
157 | * perf probe [EVENT=]SRC[:LN|;PTN] |
158 | * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] | |
af663d75 MH |
159 | * |
160 | * TODO:Group name support | |
50656eec MH |
161 | */ |
162 | ||
2a9c8c36 MH |
163 | ptr = strpbrk(arg, ";=@+%"); |
164 | if (ptr && *ptr == '=') { /* Event name */ | |
af663d75 MH |
165 | *ptr = '\0'; |
166 | tmp = ptr + 1; | |
167 | ptr = strchr(arg, ':'); | |
168 | if (ptr) /* Group name is not supported yet. */ | |
169 | semantic_error("Group name is not supported yet."); | |
b7702a21 MH |
170 | if (!check_event_name(arg)) |
171 | semantic_error("%s is bad for event name -it must " | |
172 | "follow C symbol-naming rule.", arg); | |
31facc5f | 173 | pp->event = xstrdup(arg); |
af663d75 MH |
174 | arg = tmp; |
175 | } | |
176 | ||
2a9c8c36 | 177 | ptr = strpbrk(arg, ";:+@%"); |
50656eec MH |
178 | if (ptr) { |
179 | nc = *ptr; | |
180 | *ptr++ = '\0'; | |
181 | } | |
182 | ||
183 | /* Check arg is function or file and copy it */ | |
184 | if (strchr(arg, '.')) /* File */ | |
31facc5f | 185 | pp->file = xstrdup(arg); |
50656eec | 186 | else /* Function */ |
31facc5f | 187 | pp->function = xstrdup(arg); |
50656eec MH |
188 | |
189 | /* Parse other options */ | |
190 | while (ptr) { | |
191 | arg = ptr; | |
192 | c = nc; | |
2a9c8c36 | 193 | if (c == ';') { /* Lazy pattern must be the last part */ |
31facc5f | 194 | pp->lazy_line = xstrdup(arg); |
2a9c8c36 MH |
195 | break; |
196 | } | |
197 | ptr = strpbrk(arg, ";:+@%"); | |
50656eec MH |
198 | if (ptr) { |
199 | nc = *ptr; | |
200 | *ptr++ = '\0'; | |
201 | } | |
202 | switch (c) { | |
203 | case ':': /* Line number */ | |
204 | pp->line = strtoul(arg, &tmp, 0); | |
205 | if (*tmp != '\0') | |
2a9c8c36 MH |
206 | semantic_error("There is non-digit char" |
207 | " in line number."); | |
50656eec MH |
208 | break; |
209 | case '+': /* Byte offset from a symbol */ | |
210 | pp->offset = strtoul(arg, &tmp, 0); | |
211 | if (*tmp != '\0') | |
2a9c8c36 | 212 | semantic_error("There is non-digit character" |
50656eec MH |
213 | " in offset."); |
214 | break; | |
215 | case '@': /* File name */ | |
216 | if (pp->file) | |
217 | semantic_error("SRC@SRC is not allowed."); | |
31facc5f | 218 | pp->file = xstrdup(arg); |
50656eec MH |
219 | break; |
220 | case '%': /* Probe places */ | |
221 | if (strcmp(arg, "return") == 0) { | |
222 | pp->retprobe = 1; | |
223 | } else /* Others not supported yet */ | |
224 | semantic_error("%%%s is not supported.", arg); | |
225 | break; | |
226 | default: | |
227 | DIE_IF("Program has a bug."); | |
228 | break; | |
229 | } | |
230 | } | |
231 | ||
232 | /* Exclusion check */ | |
2a9c8c36 MH |
233 | if (pp->lazy_line && pp->line) |
234 | semantic_error("Lazy pattern can't be used with line number."); | |
235 | ||
236 | if (pp->lazy_line && pp->offset) | |
237 | semantic_error("Lazy pattern can't be used with offset."); | |
238 | ||
50656eec MH |
239 | if (pp->line && pp->offset) |
240 | semantic_error("Offset can't be used with line number."); | |
241 | ||
2a9c8c36 MH |
242 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) |
243 | semantic_error("File always requires line number or " | |
244 | "lazy pattern."); | |
50656eec MH |
245 | |
246 | if (pp->offset && !pp->function) | |
247 | semantic_error("Offset requires an entry function."); | |
248 | ||
249 | if (pp->retprobe && !pp->function) | |
250 | semantic_error("Return probe requires an entry function."); | |
251 | ||
2a9c8c36 MH |
252 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) |
253 | semantic_error("Offset/Line/Lazy pattern can't be used with " | |
254 | "return probe."); | |
50656eec | 255 | |
2a9c8c36 MH |
256 | pr_debug("symbol:%s file:%s line:%d offset:%d return:%d lazy:%s\n", |
257 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, | |
258 | pp->lazy_line); | |
50656eec MH |
259 | } |
260 | ||
261 | /* Parse perf-probe event definition */ | |
fac13fd5 MH |
262 | void parse_perf_probe_event(const char *str, struct probe_point *pp, |
263 | bool *need_dwarf) | |
50656eec | 264 | { |
e1c01d61 | 265 | char **argv; |
fac13fd5 MH |
266 | int argc, i; |
267 | ||
268 | *need_dwarf = false; | |
50656eec | 269 | |
e1c01d61 MH |
270 | argv = argv_split(str, &argc); |
271 | if (!argv) | |
272 | die("argv_split failed."); | |
273 | if (argc > MAX_PROBE_ARGS + 1) | |
274 | semantic_error("Too many arguments"); | |
50656eec MH |
275 | |
276 | /* Parse probe point */ | |
277 | parse_perf_probe_probepoint(argv[0], pp); | |
fc6ceea0 | 278 | if (pp->file || pp->line || pp->lazy_line) |
fac13fd5 | 279 | *need_dwarf = true; |
50656eec | 280 | |
e1c01d61 | 281 | /* Copy arguments and ensure return probe has no C argument */ |
50656eec | 282 | pp->nr_args = argc - 1; |
31facc5f | 283 | pp->args = xzalloc(sizeof(char *) * pp->nr_args); |
e1c01d61 | 284 | for (i = 0; i < pp->nr_args; i++) { |
31facc5f | 285 | pp->args[i] = xstrdup(argv[i + 1]); |
50656eec MH |
286 | if (is_c_varname(pp->args[i])) { |
287 | if (pp->retprobe) | |
288 | semantic_error("You can't specify local" | |
289 | " variable for kretprobe"); | |
fac13fd5 | 290 | *need_dwarf = true; |
50656eec | 291 | } |
e1c01d61 | 292 | } |
50656eec | 293 | |
e1c01d61 | 294 | argv_free(argv); |
50656eec MH |
295 | } |
296 | ||
4de189fe | 297 | /* Parse kprobe_events event into struct probe_point */ |
af663d75 | 298 | void parse_trace_kprobe_event(const char *str, struct probe_point *pp) |
4de189fe MH |
299 | { |
300 | char pr; | |
301 | char *p; | |
302 | int ret, i, argc; | |
303 | char **argv; | |
304 | ||
305 | pr_debug("Parsing kprobe_events: %s\n", str); | |
306 | argv = argv_split(str, &argc); | |
307 | if (!argv) | |
308 | die("argv_split failed."); | |
309 | if (argc < 2) | |
310 | semantic_error("Too less arguments."); | |
311 | ||
312 | /* Scan event and group name. */ | |
93aaa45a | 313 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", |
af663d75 MH |
314 | &pr, (float *)(void *)&pp->group, |
315 | (float *)(void *)&pp->event); | |
4de189fe MH |
316 | if (ret != 3) |
317 | semantic_error("Failed to parse event name: %s", argv[0]); | |
af663d75 | 318 | pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr); |
4de189fe MH |
319 | |
320 | pp->retprobe = (pr == 'r'); | |
321 | ||
322 | /* Scan function name and offset */ | |
af663d75 MH |
323 | ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, |
324 | &pp->offset); | |
4de189fe MH |
325 | if (ret == 1) |
326 | pp->offset = 0; | |
327 | ||
328 | /* kprobe_events doesn't have this information */ | |
329 | pp->line = 0; | |
330 | pp->file = NULL; | |
331 | ||
332 | pp->nr_args = argc - 2; | |
31facc5f | 333 | pp->args = xzalloc(sizeof(char *) * pp->nr_args); |
4de189fe MH |
334 | for (i = 0; i < pp->nr_args; i++) { |
335 | p = strchr(argv[i + 2], '='); | |
336 | if (p) /* We don't need which register is assigned. */ | |
337 | *p = '\0'; | |
31facc5f | 338 | pp->args[i] = xstrdup(argv[i + 2]); |
4de189fe MH |
339 | } |
340 | ||
4de189fe MH |
341 | argv_free(argv); |
342 | } | |
343 | ||
7ef17aaf MH |
344 | /* Synthesize only probe point (not argument) */ |
345 | int synthesize_perf_probe_point(struct probe_point *pp) | |
4de189fe MH |
346 | { |
347 | char *buf; | |
348 | char offs[64] = "", line[64] = ""; | |
7ef17aaf | 349 | int ret; |
4de189fe | 350 | |
31facc5f | 351 | pp->probes[0] = buf = xzalloc(MAX_CMDLEN); |
388c3aab | 352 | pp->found = 1; |
4de189fe MH |
353 | if (pp->offset) { |
354 | ret = e_snprintf(offs, 64, "+%d", pp->offset); | |
355 | if (ret <= 0) | |
356 | goto error; | |
357 | } | |
358 | if (pp->line) { | |
359 | ret = e_snprintf(line, 64, ":%d", pp->line); | |
360 | if (ret <= 0) | |
361 | goto error; | |
362 | } | |
363 | ||
364 | if (pp->function) | |
365 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function, | |
366 | offs, pp->retprobe ? "%return" : "", line); | |
367 | else | |
84988450 | 368 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line); |
7ef17aaf MH |
369 | if (ret <= 0) { |
370 | error: | |
371 | free(pp->probes[0]); | |
372 | pp->probes[0] = NULL; | |
388c3aab | 373 | pp->found = 0; |
7ef17aaf MH |
374 | } |
375 | return ret; | |
376 | } | |
377 | ||
378 | int synthesize_perf_probe_event(struct probe_point *pp) | |
379 | { | |
380 | char *buf; | |
381 | int i, len, ret; | |
382 | ||
383 | len = synthesize_perf_probe_point(pp); | |
384 | if (len < 0) | |
385 | return 0; | |
4de189fe | 386 | |
7ef17aaf | 387 | buf = pp->probes[0]; |
4de189fe MH |
388 | for (i = 0; i < pp->nr_args; i++) { |
389 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", | |
390 | pp->args[i]); | |
391 | if (ret <= 0) | |
392 | goto error; | |
393 | len += ret; | |
394 | } | |
395 | pp->found = 1; | |
396 | ||
397 | return pp->found; | |
398 | error: | |
399 | free(pp->probes[0]); | |
7ef17aaf | 400 | pp->probes[0] = NULL; |
4de189fe MH |
401 | |
402 | return ret; | |
403 | } | |
404 | ||
50656eec MH |
405 | int synthesize_trace_kprobe_event(struct probe_point *pp) |
406 | { | |
407 | char *buf; | |
408 | int i, len, ret; | |
409 | ||
31facc5f | 410 | pp->probes[0] = buf = xzalloc(MAX_CMDLEN); |
4de189fe MH |
411 | ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); |
412 | if (ret <= 0) | |
50656eec MH |
413 | goto error; |
414 | len = ret; | |
415 | ||
416 | for (i = 0; i < pp->nr_args; i++) { | |
4de189fe MH |
417 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", |
418 | pp->args[i]); | |
419 | if (ret <= 0) | |
50656eec MH |
420 | goto error; |
421 | len += ret; | |
422 | } | |
423 | pp->found = 1; | |
424 | ||
425 | return pp->found; | |
426 | error: | |
427 | free(pp->probes[0]); | |
7ef17aaf | 428 | pp->probes[0] = NULL; |
50656eec MH |
429 | |
430 | return ret; | |
431 | } | |
432 | ||
4de189fe MH |
433 | static int open_kprobe_events(int flags, int mode) |
434 | { | |
435 | char buf[PATH_MAX]; | |
436 | int ret; | |
437 | ||
438 | ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path); | |
439 | if (ret < 0) | |
440 | die("Failed to make kprobe_events path."); | |
441 | ||
442 | ret = open(buf, flags, mode); | |
443 | if (ret < 0) { | |
444 | if (errno == ENOENT) | |
445 | die("kprobe_events file does not exist -" | |
63bbd5e2 | 446 | " please rebuild with CONFIG_KPROBE_EVENT."); |
4de189fe MH |
447 | else |
448 | die("Could not open kprobe_events file: %s", | |
449 | strerror(errno)); | |
450 | } | |
451 | return ret; | |
452 | } | |
453 | ||
454 | /* Get raw string list of current kprobe_events */ | |
455 | static struct strlist *get_trace_kprobe_event_rawlist(int fd) | |
456 | { | |
457 | int ret, idx; | |
458 | FILE *fp; | |
459 | char buf[MAX_CMDLEN]; | |
460 | char *p; | |
461 | struct strlist *sl; | |
462 | ||
463 | sl = strlist__new(true, NULL); | |
464 | ||
465 | fp = fdopen(dup(fd), "r"); | |
466 | while (!feof(fp)) { | |
467 | p = fgets(buf, MAX_CMDLEN, fp); | |
468 | if (!p) | |
469 | break; | |
470 | ||
471 | idx = strlen(p) - 1; | |
472 | if (p[idx] == '\n') | |
473 | p[idx] = '\0'; | |
474 | ret = strlist__add(sl, buf); | |
475 | if (ret < 0) | |
476 | die("strlist__add failed: %s", strerror(-ret)); | |
477 | } | |
478 | fclose(fp); | |
479 | ||
480 | return sl; | |
481 | } | |
482 | ||
483 | /* Free and zero clear probe_point */ | |
484 | static void clear_probe_point(struct probe_point *pp) | |
485 | { | |
486 | int i; | |
487 | ||
af663d75 MH |
488 | if (pp->event) |
489 | free(pp->event); | |
490 | if (pp->group) | |
491 | free(pp->group); | |
4de189fe MH |
492 | if (pp->function) |
493 | free(pp->function); | |
494 | if (pp->file) | |
495 | free(pp->file); | |
2a9c8c36 MH |
496 | if (pp->lazy_line) |
497 | free(pp->lazy_line); | |
4de189fe MH |
498 | for (i = 0; i < pp->nr_args; i++) |
499 | free(pp->args[i]); | |
500 | if (pp->args) | |
501 | free(pp->args); | |
502 | for (i = 0; i < pp->found; i++) | |
503 | free(pp->probes[i]); | |
5660ce34 | 504 | memset(pp, 0, sizeof(*pp)); |
4de189fe MH |
505 | } |
506 | ||
278498d4 | 507 | /* Show an event */ |
af663d75 MH |
508 | static void show_perf_probe_event(const char *event, const char *place, |
509 | struct probe_point *pp) | |
278498d4 | 510 | { |
7e990a51 | 511 | int i, ret; |
278498d4 MH |
512 | char buf[128]; |
513 | ||
af663d75 | 514 | ret = e_snprintf(buf, 128, "%s:%s", pp->group, event); |
7e990a51 MH |
515 | if (ret < 0) |
516 | die("Failed to copy event: %s", strerror(-ret)); | |
278498d4 MH |
517 | printf(" %-40s (on %s", buf, place); |
518 | ||
519 | if (pp->nr_args > 0) { | |
520 | printf(" with"); | |
521 | for (i = 0; i < pp->nr_args; i++) | |
522 | printf(" %s", pp->args[i]); | |
523 | } | |
524 | printf(")\n"); | |
525 | } | |
526 | ||
4de189fe MH |
527 | /* List up current perf-probe events */ |
528 | void show_perf_probe_events(void) | |
529 | { | |
7ef17aaf | 530 | int fd; |
4de189fe MH |
531 | struct probe_point pp; |
532 | struct strlist *rawlist; | |
533 | struct str_node *ent; | |
534 | ||
72041334 | 535 | setup_pager(); |
388c3aab | 536 | memset(&pp, 0, sizeof(pp)); |
72041334 | 537 | |
4de189fe MH |
538 | fd = open_kprobe_events(O_RDONLY, 0); |
539 | rawlist = get_trace_kprobe_event_rawlist(fd); | |
540 | close(fd); | |
541 | ||
adf365f4 | 542 | strlist__for_each(ent, rawlist) { |
af663d75 | 543 | parse_trace_kprobe_event(ent->s, &pp); |
278498d4 | 544 | /* Synthesize only event probe point */ |
7ef17aaf | 545 | synthesize_perf_probe_point(&pp); |
278498d4 | 546 | /* Show an event */ |
af663d75 | 547 | show_perf_probe_event(pp.event, pp.probes[0], &pp); |
4de189fe MH |
548 | clear_probe_point(&pp); |
549 | } | |
550 | ||
551 | strlist__delete(rawlist); | |
552 | } | |
553 | ||
b498ce1f | 554 | /* Get current perf-probe event names */ |
fa28244d | 555 | static struct strlist *get_perf_event_names(int fd, bool include_group) |
b498ce1f | 556 | { |
fa28244d | 557 | char buf[128]; |
b498ce1f MH |
558 | struct strlist *sl, *rawlist; |
559 | struct str_node *ent; | |
af663d75 | 560 | struct probe_point pp; |
b498ce1f | 561 | |
af663d75 | 562 | memset(&pp, 0, sizeof(pp)); |
b498ce1f MH |
563 | rawlist = get_trace_kprobe_event_rawlist(fd); |
564 | ||
e1d2017b | 565 | sl = strlist__new(true, NULL); |
adf365f4 | 566 | strlist__for_each(ent, rawlist) { |
af663d75 | 567 | parse_trace_kprobe_event(ent->s, &pp); |
fa28244d | 568 | if (include_group) { |
af663d75 MH |
569 | if (e_snprintf(buf, 128, "%s:%s", pp.group, |
570 | pp.event) < 0) | |
fa28244d MH |
571 | die("Failed to copy group:event name."); |
572 | strlist__add(sl, buf); | |
573 | } else | |
af663d75 MH |
574 | strlist__add(sl, pp.event); |
575 | clear_probe_point(&pp); | |
b498ce1f MH |
576 | } |
577 | ||
578 | strlist__delete(rawlist); | |
579 | ||
580 | return sl; | |
581 | } | |
582 | ||
a9b495b0 | 583 | static void write_trace_kprobe_event(int fd, const char *buf) |
50656eec MH |
584 | { |
585 | int ret; | |
586 | ||
fa28244d | 587 | pr_debug("Writing event: %s\n", buf); |
50656eec MH |
588 | ret = write(fd, buf, strlen(buf)); |
589 | if (ret <= 0) | |
fa28244d | 590 | die("Failed to write event: %s", strerror(errno)); |
50656eec MH |
591 | } |
592 | ||
b498ce1f | 593 | static void get_new_event_name(char *buf, size_t len, const char *base, |
d761b08b | 594 | struct strlist *namelist, bool allow_suffix) |
b498ce1f MH |
595 | { |
596 | int i, ret; | |
17f88fcd MH |
597 | |
598 | /* Try no suffix */ | |
599 | ret = e_snprintf(buf, len, "%s", base); | |
600 | if (ret < 0) | |
601 | die("snprintf() failed: %s", strerror(-ret)); | |
602 | if (!strlist__has_entry(namelist, buf)) | |
603 | return; | |
604 | ||
d761b08b MH |
605 | if (!allow_suffix) { |
606 | pr_warning("Error: event \"%s\" already exists. " | |
607 | "(Use -f to force duplicates.)\n", base); | |
608 | die("Can't add new event."); | |
609 | } | |
610 | ||
17f88fcd MH |
611 | /* Try to add suffix */ |
612 | for (i = 1; i < MAX_EVENT_INDEX; i++) { | |
b498ce1f MH |
613 | ret = e_snprintf(buf, len, "%s_%d", base, i); |
614 | if (ret < 0) | |
615 | die("snprintf() failed: %s", strerror(-ret)); | |
616 | if (!strlist__has_entry(namelist, buf)) | |
617 | break; | |
618 | } | |
619 | if (i == MAX_EVENT_INDEX) | |
620 | die("Too many events are on the same function."); | |
621 | } | |
622 | ||
e0faa8d3 MH |
623 | static void __add_trace_kprobe_events(struct probe_point *probes, |
624 | int nr_probes, bool force_add) | |
50656eec MH |
625 | { |
626 | int i, j, fd; | |
627 | struct probe_point *pp; | |
628 | char buf[MAX_CMDLEN]; | |
b498ce1f MH |
629 | char event[64]; |
630 | struct strlist *namelist; | |
d761b08b | 631 | bool allow_suffix; |
50656eec | 632 | |
b498ce1f MH |
633 | fd = open_kprobe_events(O_RDWR, O_APPEND); |
634 | /* Get current event names */ | |
fa28244d | 635 | namelist = get_perf_event_names(fd, false); |
50656eec MH |
636 | |
637 | for (j = 0; j < nr_probes; j++) { | |
638 | pp = probes + j; | |
af663d75 | 639 | if (!pp->event) |
31facc5f | 640 | pp->event = xstrdup(pp->function); |
af663d75 | 641 | if (!pp->group) |
31facc5f | 642 | pp->group = xstrdup(PERFPROBE_GROUP); |
d761b08b MH |
643 | /* If force_add is true, suffix search is allowed */ |
644 | allow_suffix = force_add; | |
b498ce1f MH |
645 | for (i = 0; i < pp->found; i++) { |
646 | /* Get an unused new event name */ | |
d761b08b MH |
647 | get_new_event_name(event, 64, pp->event, namelist, |
648 | allow_suffix); | |
b498ce1f MH |
649 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n", |
650 | pp->retprobe ? 'r' : 'p', | |
af663d75 | 651 | pp->group, event, |
b498ce1f | 652 | pp->probes[i]); |
50656eec | 653 | write_trace_kprobe_event(fd, buf); |
a9b495b0 MH |
654 | printf("Added new event:\n"); |
655 | /* Get the first parameter (probe-point) */ | |
656 | sscanf(pp->probes[i], "%s", buf); | |
af663d75 | 657 | show_perf_probe_event(event, buf, pp); |
b498ce1f MH |
658 | /* Add added event name to namelist */ |
659 | strlist__add(namelist, event); | |
d761b08b MH |
660 | /* |
661 | * Probes after the first probe which comes from same | |
662 | * user input are always allowed to add suffix, because | |
663 | * there might be several addresses corresponding to | |
664 | * one code line. | |
665 | */ | |
666 | allow_suffix = true; | |
b498ce1f | 667 | } |
50656eec | 668 | } |
a9b495b0 MH |
669 | /* Show how to use the event. */ |
670 | printf("\nYou can now use it on all perf tools, such as:\n\n"); | |
671 | printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event); | |
672 | ||
e1d2017b | 673 | strlist__delete(namelist); |
50656eec MH |
674 | close(fd); |
675 | } | |
fa28244d | 676 | |
e0faa8d3 MH |
677 | /* Currently just checking function name from symbol map */ |
678 | static void evaluate_probe_point(struct probe_point *pp) | |
679 | { | |
680 | struct symbol *sym; | |
681 | sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION], | |
682 | pp->function, NULL); | |
683 | if (!sym) | |
684 | die("Kernel symbol \'%s\' not found - probe not added.", | |
685 | pp->function); | |
686 | } | |
687 | ||
688 | void add_trace_kprobe_events(struct probe_point *probes, int nr_probes, | |
689 | bool force_add, bool need_dwarf) | |
690 | { | |
691 | int i, ret; | |
692 | struct probe_point *pp; | |
693 | #ifndef NO_DWARF_SUPPORT | |
694 | int fd; | |
695 | #endif | |
696 | /* Add probes */ | |
697 | init_vmlinux(); | |
698 | ||
699 | if (need_dwarf) | |
700 | #ifdef NO_DWARF_SUPPORT | |
701 | die("Debuginfo-analysis is not supported"); | |
702 | #else /* !NO_DWARF_SUPPORT */ | |
703 | pr_debug("Some probes require debuginfo.\n"); | |
704 | ||
705 | fd = open_vmlinux(); | |
706 | if (fd < 0) { | |
707 | if (need_dwarf) | |
708 | die("Could not open debuginfo file."); | |
709 | ||
710 | pr_debug("Could not open vmlinux/module file." | |
711 | " Try to use symbols.\n"); | |
712 | goto end_dwarf; | |
713 | } | |
714 | ||
715 | /* Searching probe points */ | |
716 | for (i = 0; i < nr_probes; i++) { | |
717 | pp = &probes[i]; | |
718 | if (pp->found) | |
719 | continue; | |
720 | ||
721 | lseek(fd, SEEK_SET, 0); | |
722 | ret = find_probe_point(fd, pp); | |
723 | if (ret > 0) | |
724 | continue; | |
725 | if (ret == 0) { /* No error but failed to find probe point. */ | |
726 | synthesize_perf_probe_point(pp); | |
727 | die("Probe point '%s' not found. - probe not added.", | |
728 | pp->probes[0]); | |
729 | } | |
730 | /* Error path */ | |
731 | if (need_dwarf) { | |
732 | if (ret == -ENOENT) | |
733 | pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n"); | |
734 | die("Could not analyze debuginfo."); | |
735 | } | |
736 | pr_debug("An error occurred in debuginfo analysis." | |
737 | " Try to use symbols.\n"); | |
738 | break; | |
739 | } | |
740 | close(fd); | |
741 | ||
742 | end_dwarf: | |
743 | #endif /* !NO_DWARF_SUPPORT */ | |
744 | ||
745 | /* Synthesize probes without dwarf */ | |
746 | for (i = 0; i < nr_probes; i++) { | |
747 | pp = &probes[i]; | |
748 | if (pp->found) /* This probe is already found. */ | |
749 | continue; | |
750 | ||
751 | evaluate_probe_point(pp); | |
752 | ret = synthesize_trace_kprobe_event(pp); | |
753 | if (ret == -E2BIG) | |
754 | die("probe point definition becomes too long."); | |
755 | else if (ret < 0) | |
756 | die("Failed to synthesize a probe point."); | |
757 | } | |
758 | ||
759 | /* Settng up probe points */ | |
760 | __add_trace_kprobe_events(probes, nr_probes, force_add); | |
761 | } | |
762 | ||
bbbb521b MH |
763 | static void __del_trace_kprobe_event(int fd, struct str_node *ent) |
764 | { | |
765 | char *p; | |
766 | char buf[128]; | |
767 | ||
768 | /* Convert from perf-probe event to trace-kprobe event */ | |
769 | if (e_snprintf(buf, 128, "-:%s", ent->s) < 0) | |
770 | die("Failed to copy event."); | |
771 | p = strchr(buf + 2, ':'); | |
772 | if (!p) | |
773 | die("Internal error: %s should have ':' but not.", ent->s); | |
774 | *p = '/'; | |
775 | ||
776 | write_trace_kprobe_event(fd, buf); | |
777 | printf("Remove event: %s\n", ent->s); | |
778 | } | |
779 | ||
fa28244d MH |
780 | static void del_trace_kprobe_event(int fd, const char *group, |
781 | const char *event, struct strlist *namelist) | |
782 | { | |
783 | char buf[128]; | |
bbbb521b MH |
784 | struct str_node *ent, *n; |
785 | int found = 0; | |
fa28244d MH |
786 | |
787 | if (e_snprintf(buf, 128, "%s:%s", group, event) < 0) | |
788 | die("Failed to copy event."); | |
fa28244d | 789 | |
bbbb521b MH |
790 | if (strpbrk(buf, "*?")) { /* Glob-exp */ |
791 | strlist__for_each_safe(ent, n, namelist) | |
792 | if (strglobmatch(ent->s, buf)) { | |
793 | found++; | |
794 | __del_trace_kprobe_event(fd, ent); | |
795 | strlist__remove(namelist, ent); | |
796 | } | |
797 | } else { | |
798 | ent = strlist__find(namelist, buf); | |
799 | if (ent) { | |
800 | found++; | |
801 | __del_trace_kprobe_event(fd, ent); | |
802 | strlist__remove(namelist, ent); | |
803 | } | |
804 | } | |
805 | if (found == 0) | |
806 | pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf); | |
fa28244d MH |
807 | } |
808 | ||
809 | void del_trace_kprobe_events(struct strlist *dellist) | |
810 | { | |
811 | int fd; | |
fa28244d MH |
812 | const char *group, *event; |
813 | char *p, *str; | |
814 | struct str_node *ent; | |
815 | struct strlist *namelist; | |
816 | ||
817 | fd = open_kprobe_events(O_RDWR, O_APPEND); | |
818 | /* Get current event names */ | |
819 | namelist = get_perf_event_names(fd, true); | |
820 | ||
adf365f4 | 821 | strlist__for_each(ent, dellist) { |
31facc5f | 822 | str = xstrdup(ent->s); |
bbbb521b | 823 | pr_debug("Parsing: %s\n", str); |
fa28244d MH |
824 | p = strchr(str, ':'); |
825 | if (p) { | |
826 | group = str; | |
827 | *p = '\0'; | |
828 | event = p + 1; | |
829 | } else { | |
bbbb521b | 830 | group = "*"; |
fa28244d MH |
831 | event = str; |
832 | } | |
bbbb521b | 833 | pr_debug("Group: %s, Event: %s\n", group, event); |
fa28244d MH |
834 | del_trace_kprobe_event(fd, group, event, namelist); |
835 | free(str); | |
836 | } | |
837 | strlist__delete(namelist); | |
838 | close(fd); | |
839 | } | |
840 | ||
631c9def | 841 | #define LINEBUF_SIZE 256 |
5c8d1cbb | 842 | #define NR_ADDITIONAL_LINES 2 |
631c9def MH |
843 | |
844 | static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num) | |
845 | { | |
846 | char buf[LINEBUF_SIZE]; | |
847 | const char *color = PERF_COLOR_BLUE; | |
848 | ||
849 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) | |
850 | goto error; | |
851 | if (!skip) { | |
852 | if (show_num) | |
853 | fprintf(stdout, "%7u %s", l, buf); | |
854 | else | |
855 | color_fprintf(stdout, color, " %s", buf); | |
856 | } | |
857 | ||
858 | while (strlen(buf) == LINEBUF_SIZE - 1 && | |
859 | buf[LINEBUF_SIZE - 2] != '\n') { | |
860 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) | |
861 | goto error; | |
862 | if (!skip) { | |
863 | if (show_num) | |
864 | fprintf(stdout, "%s", buf); | |
865 | else | |
866 | color_fprintf(stdout, color, "%s", buf); | |
867 | } | |
868 | } | |
869 | return; | |
870 | error: | |
871 | if (feof(fp)) | |
872 | die("Source file is shorter than expected."); | |
873 | else | |
874 | die("File read error: %s", strerror(errno)); | |
875 | } | |
876 | ||
877 | void show_line_range(struct line_range *lr) | |
878 | { | |
879 | unsigned int l = 1; | |
880 | struct line_node *ln; | |
881 | FILE *fp; | |
e0faa8d3 MH |
882 | int fd, ret; |
883 | ||
884 | /* Search a line range */ | |
885 | init_vmlinux(); | |
886 | fd = open_vmlinux(); | |
887 | if (fd < 0) | |
888 | die("Could not open debuginfo file."); | |
889 | ret = find_line_range(fd, lr); | |
890 | if (ret <= 0) | |
891 | die("Source line is not found.\n"); | |
892 | close(fd); | |
631c9def MH |
893 | |
894 | setup_pager(); | |
895 | ||
896 | if (lr->function) | |
897 | fprintf(stdout, "<%s:%d>\n", lr->function, | |
898 | lr->start - lr->offset); | |
899 | else | |
900 | fprintf(stdout, "<%s:%d>\n", lr->file, lr->start); | |
901 | ||
902 | fp = fopen(lr->path, "r"); | |
903 | if (fp == NULL) | |
904 | die("Failed to open %s: %s", lr->path, strerror(errno)); | |
905 | /* Skip to starting line number */ | |
906 | while (l < lr->start) | |
907 | show_one_line(fp, l++, true, false); | |
908 | ||
909 | list_for_each_entry(ln, &lr->line_list, list) { | |
910 | while (ln->line > l) | |
911 | show_one_line(fp, (l++) - lr->offset, false, false); | |
912 | show_one_line(fp, (l++) - lr->offset, false, true); | |
913 | } | |
5c8d1cbb MH |
914 | |
915 | if (lr->end == INT_MAX) | |
916 | lr->end = l + NR_ADDITIONAL_LINES; | |
917 | while (l < lr->end && !feof(fp)) | |
918 | show_one_line(fp, (l++) - lr->offset, false, false); | |
919 | ||
631c9def MH |
920 | fclose(fp); |
921 | } | |
e0faa8d3 MH |
922 | |
923 |